jveitchmichaelis / rascal

RAnsac Assisted Spectral CALibration
BSD 3-Clause "New" or "Revised" License
12 stars 2 forks source link

Problem Fitting #91

Closed nicolerelogio closed 1 year ago

nicolerelogio commented 1 year ago

Hello! I'm trying to use rascal to perform wavelength calibration of a long-slit spectra, but I'm having problems with the fitting function. I think this is due to my personalized atlas, because it says that the function add_user_atlas() is deprecated, and I should use the new atlas class, but I can't find what the new class is (when I try load_user_atlas() it still doesn't work).

I'm sending my code just in case. Everything works well until the "add_user_atlas" function. I hope you can help me, thank you so much for your time! :)

lines = [3650.1530, 4046.5630, 4358.3280, 5460.7350, 5769.5980, 5790.6630] # known Hg lines in angstrom

Open the lamp file

spectrum2D = fits.open("lampada1.fits")[0].data header_lamp = fits.open("lampada1.fits")[0].header

Get important values from lamp`

pressure_atm = header_lamp['ENVPRE'] #Atmospheric Pressure [hPS] at start of exposure temperature_atm = header_lamp['ENVTEM'] #Outside Temperature [C] at start of exposure humidity_atm = header_lamp['ENVHUM'] #Relative Humidity at start of exposure

Get the median along the spectral direction

spectrum = np.median(spectrum2D, axis=0)

Get the spectral lines

peaks, _ = find_peaks(spectrum, height=10000) peaks_refined = refine_peaks(spectrum, peaks) intensity = spectrum[peaks]

Set up the Calibrator object

Initialise the calibrator object.

c = Calibrator(peaks_refined, spectrum) c.set_calibrator_properties(num_pix=len(spectrum), plotting_library='matplotlib', log_level='info')

c.set_hough_properties(num_slopes=1000, xbins=50, ybins=50, min_wavelength=3000, max_wavelength=8000, range_tolerance=500, linearity_tolerance=50)

c.set_ransac_properties(sample_size=5, top_n_candidate=6, filter_close=False, candidate_weighted=True)

c.add_user_atlas(elements=['Hg', 'Hg', 'Hg', 'Hg', 'Hg', 'Hg'], wavelengths=lines, #angstrom intensities=intensity,
pressure=pressure_atm*100, #pascal temperature=temperature_atm+273.15, #kelvin relative_humidity=humidity_atm,
constrain_poly=False,
vacuum=False)

error: Using add_user_atlas is now deprecated. Please use the new Atlas class.

c.do_hough_transform()

fit_coeff, rms, residual, peak_utilisation = c.fit(max_tries=5000, fit_deg=4, fit_type='poly', brute_force=True, fit_tolerance=10, progress=True, fit_coeff=None)

plot_search_space(fit_coeff=fit_coeff, top_n_candidates=3)

WARNING [calibrator.py:1640] Size of sample_size is larger than the size of atlas, the sample_size is set to match the size of atlas = 6.

WARNING [calibrator.py:1692] Invalid fit

WARNING [calibrator.py:1695] RMS too large 1e+50 > 10

mattodeg commented 1 year ago

Hi! I'm using rascal too and I had no struggle to use the add_user_atlas() function, despite the warning about the new Atlas class.

I checked your code and seems everything is fine. The only differences are that I did not use the humidity and temperature datas as well as the intensity. Try without them just to see if it works.

And just a suggestion: for the elements use elements=len(wavelengths)*["Hg"]

jveitchmichaelis commented 1 year ago

Hi @nicolerelogio

The reason the fit is failing is because of this: Size of sample_size is larger than the size of atlas, the sample_size is set to match the size of atlas = 6. The issue is likely that you're not using a normal atlas in conjunction with your user lines, so you could also add the normal Hg line list and add your 6 on top (i.e. rascal is trying to fit with only the lines you provide).

You could also try adding some more strong lines, or try only using the default Hg atlas alone.

That said, there may be a bug as your sample size is set to 5 and your atlas is larger (unless lines are being removed before the fit). @cylammarco?

Best, Josh

cylammarco commented 1 year ago

I would also try looking at the number of peaks from the find_peaks().

along with this:

c.set_hough_properties(num_slopes=1000,
xbins=50,
ybins=50,
min_wavelength=3000,
max_wavelength=8000,
range_tolerance=500,
linearity_tolerance=50)

the min, max and bin numbers probably need a bit more refining, especially when there are no lines in the region of 6000-8000 A for Mercury.

image

Please let us know if anything works/not works.

jveitchmichaelis commented 1 year ago

I don't know which lamp you're using, but there may be a secondary gas in the tube. For example HgAr is fairly common and you can use the Ar lines in the blue to assist the calibration (though from memory they're much weaker).

nicolerelogio commented 1 year ago

Thank you so much, everybody! I added more Hg lines (and Ar too, since my lamp is HgAr) and adjusted some of the hough properties as suggested Everything is working now 😄 Thanks again

jveitchmichaelis commented 1 year ago

Great, feel free to reopen if you have any other issues. Thanks!