def test_reimport_calibrate_write_read(temp_file, region1, region2, peak, calfactor1, calfactor2, calfactor3): """ Write/Read fit and change calibration in between Fit should appear at the new calibrated position """ test_store_fit(region1, region2, peak, calfactor1, calfactor2) spectra.ActivateFit(None) fit = list(spectra.Get("0").dict.items())[0][1] markers_initial = get_markers(fit) integral_initial = fit.ExtractIntegralParams()[0][0] fitxml.WriteXML(spectra.Get("0").ID, temp_file) spectra.Get("0").Clear() spectra.ApplyCalibration(0, [0, calfactor3]) fitxml.ReadXML(spectra.Get("0").ID, temp_file) fit = list(spectra.Get("0").dict.items())[0][1] markers_final = get_markers(fit) integral_final = fit.ExtractIntegralParams()[0][0] assert isclose(markers_initial[1], markers_final[1] / calfactor3 * calfactor2) assert isclose(markers_initial[3], markers_final[3] / calfactor3 * calfactor2) assert isclose(markers_initial[5], markers_final[5] / calfactor3 * calfactor2) for key in ["pos", "width_cal"]: assert isclose( integral_initial[key].nominal_value, integral_final[key].nominal_value / calfactor3 * calfactor2)
def fit_write_and_save(filename): spectra.ExecuteFit() spectra.StoreFit() spectra.ClearFit() out_original = list_fit() print('Saving fits to file %s' % filename) fitxml.WriteXML(spectra.Get("0").ID, filename) print('Deleting all fits') spectra.Get("0").Clear() print('Reading fits from file %s' % filename) fitxml.ReadXML(spectra.Get("0").ID, filename) assert out_original == list_fit()
def test_write_read_xml(temp_file, region1, region2, peak, calfactor1, calfactor2): """ Write/read fit to/from xml Fit should appear at the same position as before """ test_store_fit(region1, region2, peak, calfactor1, calfactor2) spectra.ActivateFit(None) out_original = list_fit() spectra.ClearFit() fitxml.WriteXML(spectra.Get("0").ID, temp_file) spectra.Get("0").Clear() fitxml.ReadXML(spectra.Get("0").ID, temp_file) assert out_original == list_fit()
def test_backgroundRegions( model, nparams, step, nregions, settings, errormessage, bgparams, bg_volume, temp_file, test_spectrum, ): spec_interface.LoadSpectra(test_spectrum.filename) command = ["fit function background activate " + model] if settings != "": command.append(settings) for i in range(nregions): command.append( "fit marker background set %f" % ((step + test_spectrum.bg_regions[i][0]) * test_spectrum.nbins_per_step) ) command.append( "fit marker background set %f" % ((step + test_spectrum.bg_regions[i][1]) * test_spectrum.nbins_per_step) ) command.append( "fit marker region set %f" % ( (step + 0.5) * test_spectrum.nbins_per_step - 3.0 * test_spectrum.peak_width * test_spectrum.nbins_per_step ) ) command.append( "fit marker region set %f" % ( (step + 0.5) * test_spectrum.nbins_per_step + 3.0 * test_spectrum.peak_width * test_spectrum.nbins_per_step ) ) command.append( "fit marker peak set %f" % ((step + 0.5) * test_spectrum.nbins_per_step) ) command.append("fit execute") command.append("fit store") for i in range(nregions): command.append("fit marker background delete %i" % (i)) command.append("fit marker region delete 0") command.append("fit marker peak delete 0") f, ferr = hdtvcmd(*command) if WRITE_BATCHFILE: batchfile = os.path.join( os.path.curdir, "test", "share", model + "_" + str(nparams) + "_" + str(step) + "_background.hdtv", ) bfile = open(batchfile, "w") bfile.write("spectrum get " + test_spectrum.filename + "\n") for c in command: bfile.write(c + "\n") bfile.close() if errormessage != "": hdtvcmd("fit delete 0", "spectrum delete 0") assert errormessage in ferr else: assert ferr == "" fitxml.WriteXML(spectra.Get("0").ID, temp_file) fitxml.ReadXML(spectra.Get("0").ID, temp_file, refit=True, interactive=False) hdtvcmd("fit delete 0", "spectrum delete 0") # Parse xml file manually and check correct output tree = ET.parse(temp_file) root = tree.getroot() # Check the number of fits fits = root.findall("fit") assert len(fits) == 1 # Check the number and positions of background markers bgMarkers = fits[0].findall("bgMarker") assert len(bgMarkers) == nregions for i in range(len(bgMarkers)): assert isclose( float(bgMarkers[i].find("begin").find("cal").text), (step + test_spectrum.bg_regions[i][0]) * test_spectrum.nbins_per_step, abs_tol=BG_MARKER_TOLERANCE, ) assert isclose( float(bgMarkers[i].find("end").find("cal").text), (step + test_spectrum.bg_regions[i][1]) * test_spectrum.nbins_per_step, abs_tol=BG_MARKER_TOLERANCE, ) # Check the number (and values) of background parameters background = fits[0].find("background") assert background.get("backgroundModel") == model assert int(background.get("nparams")) == nparams # Check the background parameters if len(bgparams) > 0: params = background.findall("param") for i in range(len(params)): if i < len(bgparams): assert isclose( float(params[i].find("value").text), bgparams[i], abs_tol=N_SIGMA * float(params[i].find("error").text), ) # Check the fit result # All results will be compared assert isclose( float(fits[0].find("peak").find("cal").find("vol").find("value").text), test_spectrum.peak_volume, abs_tol=N_SIGMA * float(fits[0].find("peak").find("cal").find("vol").find("error").text), ) assert isclose( float(fits[0].find("peak").find("cal").find("width").find("value").text), SIGMA_TO_FWHM * test_spectrum.peak_width * test_spectrum.nbins_per_step, abs_tol=N_SIGMA * float(fits[0].find("peak").find("cal").find("width").find("error").text), ) # Check the integration result integrals = fits[0].findall("integral") assert len(integrals) == 3 assert integrals[0].get("integraltype") == "tot" assert integrals[1].get("integraltype") == "bg" assert integrals[2].get("integraltype") == "sub" # Peak volume assert isclose( float(integrals[2].find("uncal").find("vol").find("value").text), test_spectrum.peak_volume, abs_tol=N_SIGMA * float(integrals[2].find("uncal").find("vol").find("error").text), ) # Background volume if bg_volume > 0.0: assert isclose( float(integrals[1].find("uncal").find("vol").find("value").text), bg_volume, abs_tol=sqrt(bg_volume), ) else: assert isclose( float(integrals[1].find("uncal").find("vol").find("value").text), test_spectrum.bg_per_bin * 6.0 * test_spectrum.peak_width * test_spectrum.nbins_per_step, abs_tol=N_SIGMA * sqrt( test_spectrum.bg_per_bin * 6.0 * test_spectrum.peak_width * test_spectrum.nbins_per_step ), )
def test_backgroundRegions(model, nparams, step, nregions, settings, errormessage, bgparams, bg_volume, temp_file, test_spectrum): spec_interface.LoadSpectra(test_spectrum.filename) command = ['fit function background activate ' + model] if settings is not "": command.append(settings) for i in range(nregions): command.append('fit marker background set %f' % ((step+test_spectrum.bg_regions[i][0])*test_spectrum.nbins_per_step)) command.append('fit marker background set %f' % ((step+test_spectrum.bg_regions[i][1])*test_spectrum.nbins_per_step)) command.append('fit marker region set %f' % ((step+0.5)*test_spectrum.nbins_per_step - 3.*test_spectrum.peak_width*test_spectrum.nbins_per_step)) command.append('fit marker region set %f' % ((step+0.5)*test_spectrum.nbins_per_step + 3.*test_spectrum.peak_width*test_spectrum.nbins_per_step)) command.append('fit marker peak set %f' % ((step+0.5)*test_spectrum.nbins_per_step)) command.append('fit execute') command.append('fit store') for i in range(nregions): command.append('fit marker background delete %i' % (i)) command.append('fit marker region delete 0') command.append('fit marker peak delete 0') f, ferr = hdtvcmd(*command) if WRITE_BATCHFILE: batchfile = os.path.join(os.path.curdir, 'test', 'share', model + '_' + str(nparams) + '_' + str(step) + '_background.hdtv') bfile = open(batchfile, 'w') bfile.write('spectrum get '+ test_spectrum.filename + '\n') for c in command: bfile.write(c + '\n') bfile.close() if errormessage is not '': hdtvcmd('fit delete 0', 'spectrum delete 0') assert errormessage in ferr else: assert ferr == '' fitxml.WriteXML(spectra.Get("0").ID, temp_file) fitxml.ReadXML(spectra.Get("0").ID, temp_file, refit=True, interactive=False) hdtvcmd('fit delete 0', 'spectrum delete 0') # Parse xml file manually and check correct output tree = ET.parse(temp_file) root = tree.getroot() # Check the number of fits fits = root.findall('fit') assert len(fits) == 1 # Check the number and positions of background markers bgMarkers = fits[0].findall('bgMarker') assert len(bgMarkers) == nregions for i in range(len(bgMarkers)): assert isclose(float(bgMarkers[i].find('begin').find('cal').text), (step+test_spectrum.bg_regions[i][0])*test_spectrum.nbins_per_step, abs_tol=BG_MARKER_TOLERANCE) assert isclose(float(bgMarkers[i].find('end').find('cal').text), (step+test_spectrum.bg_regions[i][1])*test_spectrum.nbins_per_step, abs_tol=BG_MARKER_TOLERANCE) # Check the number (and values) of background parameters background = fits[0].find('background') assert background.get('backgroundModel') == model assert int(background.get('nparams')) == nparams # Check the background parameters if len(bgparams) > 0: params = background.findall('param') for i in range(len(params)): if i < len(bgparams): assert isclose(float(params[i].find('value').text), bgparams[i], abs_tol=N_SIGMA*float(params[i].find('error').text)) # Check the fit result # All results will be compared assert isclose(float(fits[0].find('peak').find('cal').find('vol').find('value').text), test_spectrum.peak_volume, abs_tol=N_SIGMA*float(fits[0].find('peak').find('cal').find('vol').find('error').text)) assert isclose(float(fits[0].find('peak').find('cal').find('width').find('value').text), SIGMA_TO_FWHM*test_spectrum.peak_width*test_spectrum.nbins_per_step, abs_tol=N_SIGMA*float(fits[0].find('peak').find('cal').find('width').find('error').text)) # Check the integration result integrals = fits[0].findall('integral') assert len(integrals) == 3 assert integrals[0].get('integraltype') == 'tot' assert integrals[1].get('integraltype') == 'bg' assert integrals[2].get('integraltype') == 'sub' # Peak volume assert isclose(float(integrals[2].find('uncal').find('vol').find('value').text), test_spectrum.peak_volume, abs_tol=N_SIGMA*float(integrals[2].find('uncal').find('vol').find('error').text)) # Background volume if bg_volume > 0.: assert isclose(float(integrals[1].find('uncal').find('vol').find('value').text), bg_volume, abs_tol=sqrt(bg_volume)) else: assert isclose(float(integrals[1].find('uncal').find('vol').find('value').text), test_spectrum.bg_per_bin*6.*test_spectrum.peak_width*test_spectrum.nbins_per_step, abs_tol=N_SIGMA*sqrt(test_spectrum.bg_per_bin*6.*test_spectrum.peak_width*test_spectrum.nbins_per_step))
def test_calbin(temp_file, test_spectrum): command = ['spectrum get ' + test_spectrum.filename] step = 2 # Use the third spectrum which has a constant background and Poissonian fluctuations # Fit the peak in step 2 command.append('fit marker background set %f' % ((step + test_spectrum.bg_regions[0][0]) * test_spectrum.nbins_per_step)) command.append('fit marker background set %f' % ((step + test_spectrum.bg_regions[0][1]) * test_spectrum.nbins_per_step)) command.append('fit marker background set %f' % ((step + test_spectrum.bg_regions[1][0]) * test_spectrum.nbins_per_step)) command.append('fit marker background set %f' % ((step + test_spectrum.bg_regions[1][1]) * test_spectrum.nbins_per_step)) command.append( 'fit marker region set %f' % ((step + 0.5) * test_spectrum.nbins_per_step - 3. * test_spectrum.peak_width * test_spectrum.nbins_per_step)) command.append( 'fit marker region set %f' % ((step + 0.5) * test_spectrum.nbins_per_step + 3. * test_spectrum.peak_width * test_spectrum.nbins_per_step)) command.append('fit marker peak set %f' % ((step + 0.5) * test_spectrum.nbins_per_step)) command.append('fit execute') command.append('fit store') f, ferr = hdtvcmd(*command) if WRITE_BATCHFILE: batchfile = os.path.join(os.path.curdir, 'test', 'share', 'calbin.hdtv') with open(batchfile, 'w') as bfile: for c in command: bfile.write(c + '\n') assert ferr == '' # Write the fit result to a temporary XML file fitxml.WriteXML(spectra.Get("0").ID, temp_file) fitxml.ReadXML(spectra.Get("0").ID, temp_file, refit=True, interactive=False) # Parse XML file manually tree = ET.parse(temp_file) root = tree.getroot() # Check the number of fits fits = root.findall('fit') assert len(fits) == 1 # Read out the main fitted peak properties (position, volume, width) and their uncertainties pos_value_init = float( fits[0].find('peak').find('cal').find('pos').find('value').text) pos_error_init = abs( float(fits[0].find('peak').find('cal').find('pos').find('error').text)) vol_value_init = float( fits[0].find('peak').find('cal').find('vol').find('value').text) vol_error_init = abs( float(fits[0].find('peak').find('cal').find('vol').find('error').text)) width_value_init = float( fits[0].find('peak').find('cal').find('width').find('value').text) width_error_init = abs( float( fits[0].find('peak').find('cal').find('width').find('error').text)) # Calbin the spectrum with standard settings, read in the fitted value again and check whether they have changed command = ['spectrum calbin 0 -s 1'] command.append('fit execute') command.append('fit store') f, ferr = hdtvcmd(*command) assert ferr == '' if WRITE_BATCHFILE: batchfile = os.path.join(os.path.curdir, 'test', 'share', 'calbin.hdtv') with open(batchfile, 'a') as bfile: for c in command: bfile.write(c + '\n') assert ferr == '' fitxml.WriteXML(spectra.Get("0").ID, temp_file) fitxml.ReadXML(spectra.Get("0").ID, temp_file, refit=False, interactive=False) tree = ET.parse(temp_file) root = tree.getroot() fits = root.findall('fit') # It is a little bit unsatisfactory to accumulate the fits multiple times in temp_file, but I found no way to erase the content of temp_file # open(temp_file, 'w').close() wouldn't work assert len(fits) == 3 pos_value_1 = float( fits[2].find('peak').find('cal').find('pos').find('value').text) pos_error_1 = abs( float(fits[2].find('peak').find('cal').find('pos').find('error').text)) vol_value_1 = float( fits[2].find('peak').find('cal').find('vol').find('value').text) vol_error_1 = abs( float(fits[2].find('peak').find('cal').find('vol').find('error').text)) width_value_1 = float( fits[2].find('peak').find('cal').find('width').find('value').text) width_error_1 = abs( float( fits[2].find('peak').find('cal').find('width').find('error').text)) # For the fit of the position, allow it to deviate by N_SIGMA times the combined standard deviations of the fitted positions PLUS the width of a single bin. assert isclose( pos_value_init - pos_value_1, 0., abs_tol=N_SIGMA * sqrt(pos_error_init * pos_error_init + pos_error_1 * pos_error_1) + 1.) assert isclose( vol_value_init - vol_value_1, 0., abs_tol=N_SIGMA * sqrt(vol_error_init * vol_error_init + vol_error_1 * vol_error_1)) assert isclose(width_value_init - width_value_1, 0., abs_tol=N_SIGMA * sqrt(width_error_init * width_error_init + width_error_1 * width_error_1)) # Calbin the spectrum with a factor of 2, read in the fitted value again and check whether they have changed command = ['spectrum calbin 0 -b 2 -s 0'] command.append('fit execute') command.append('fit store') f, ferr = hdtvcmd(*command) assert ferr == '' if WRITE_BATCHFILE: batchfile = os.path.join(os.path.curdir, 'test', 'share', 'calbin.hdtv') with open(batchfile, 'a') as bfile: for c in command: bfile.write(c + '\n') assert ferr == '' fitxml.WriteXML(spectra.Get("0").ID, temp_file) fitxml.ReadXML(spectra.Get("0").ID, temp_file, refit=False, interactive=False) tree = ET.parse(temp_file) root = tree.getroot() fits = root.findall('fit') assert len(fits) == 7 pos_value_2 = float( fits[6].find('peak').find('cal').find('pos').find('value').text) pos_error_2 = abs( float(fits[6].find('peak').find('cal').find('pos').find('error').text)) vol_value_2 = float( fits[6].find('peak').find('cal').find('vol').find('value').text) vol_error_2 = abs( float(fits[6].find('peak').find('cal').find('vol').find('error').text)) width_value_2 = float( fits[6].find('peak').find('cal').find('width').find('value').text) width_error_2 = abs( float( fits[6].find('peak').find('cal').find('width').find('error').text)) assert isclose( pos_value_init - pos_value_2, 0., abs_tol=N_SIGMA * sqrt(pos_error_init * pos_error_init + pos_error_2 * pos_error_2) + 2.) assert isclose( vol_value_init - vol_value_2, 0., abs_tol=N_SIGMA * sqrt(vol_error_init * vol_error_init + vol_error_2 * vol_error_2)) assert isclose(width_value_init - width_value_2, 0., abs_tol=N_SIGMA * sqrt(width_error_init * width_error_init + width_error_2 * width_error_2)) assert isclose(pos_error_init, pos_error_2, rel_tol=0.2) assert isclose(vol_error_init, vol_error_2, rel_tol=0.2) assert isclose(pos_error_init, pos_error_2, rel_tol=0.2)