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] __main__.fitxml.WriteXML(spectra.Get("0").ID, temp_file) spectra.Get("0").Clear() spectra.ApplyCalibration(0, [0, calfactor3]) __main__.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 test_write_read_xml_calibrate(temp_file, region1, region2, peak, calfactor1, calfactor2, calfactor3): """ Write/Read fit and calibrate afterwards Fit should move with spectrum to new calibration """ test_write_read_xml(temp_file, region1, region2, peak, calfactor1, calfactor2) fit = list(spectra.Get("0").dict.items())[0][1] markers_initial = get_markers(fit) integral_initial = fit.ExtractIntegralParams()[0][0] spectra.ApplyCalibration("0", [0, calfactor3]) 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 markers_consistent(calfactor=1., region1=None, region2=None, fit=None): markers = get_markers(fit) assert isclose(markers[0], markers[1] / calfactor) assert isclose(markers[2], markers[3] / calfactor) assert isclose(markers[4], markers[5] / calfactor) if region1: assert isclose(markers[1], region1) if region2: assert isclose(markers[3], region2)
def test_change_calibration_while_stored_activate(region1, region2, peak, calfactor1, calfactor2, calfactor3): """ Reactivate fit after calibration has changed Active markers should appear at the same position as fit """ test_execute_reactivated_fit(region1, region2, peak, calfactor1, calfactor2) spectra.ActivateFit(None) spectra.ClearFit() spectra.ApplyCalibration("0", [0, calfactor3]) fit = list(spectra.Get("0").dict.items())[0][1] markers_initial = get_markers(fit) spectra.ActivateFit("0") markers_final = get_markers() assert isclose(markers_initial[1], markers_final[1]) assert isclose(markers_initial[3], markers_final[3]) assert isclose(markers_initial[5], markers_final[5])
def test_execute_reactivated_calibrated_fit(region1, region2, peak, calfactor1, calfactor2, calfactor3): """ Execute reactivated fit New fit should be at the same position as the stored fit """ test_execute_reactivated_fit(region1, region2, peak, calfactor1, calfactor2) spectra.ActivateFit(None) spectra.ClearFit() spectra.ApplyCalibration("0", [0, calfactor3]) spectra.ActivateFit("0") spectra.ExecuteFit() fit = list(spectra.Get("0").dict.items())[0][1] markers_work = get_markers(fit) markers_stored = get_markers() assert isclose(markers_stored[1], markers_work[1], rel_tol=1e-7) assert isclose(markers_stored[3], markers_work[3], rel_tol=1e-7) assert isclose(markers_stored[5], markers_work[5], rel_tol=1e-7)
def test_change_calibration_while_stored(region1, region2, peak, calfactor1, calfactor2, calfactor3): """ Change calibration after fit is stored Stored fit should move with spectrum to new position """ test_execute_reactivated_fit(region1, region2, peak, calfactor1, calfactor2) spectra.ActivateFit(None) spectra.ClearFit() fit = list(spectra.Get("0").dict.items())[0][1] markers_initial = get_markers(fit) spectra.ApplyCalibration("0", [0, calfactor3]) markers_final = get_markers(fit) 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)
def test_root_fit_volume_with_binning(region1, region2, peak, expected_volume, spectrum): # Mock parser so RootGet can be used # args = mock.Mock() args = type('Test', (object, ), {}) args.pattern = [os.path.join("test", "share", "binning.root", spectrum)] args.replace = False args.load_cal = False args.invisible = False hdtv.plugins.rootInterface.r.RootGet(args) session.SetMarker("region", region1) session.SetMarker("region", region2) session.SetMarker("peak", peak) session.ExecuteFit() fit = session.workFit fit_volume = fit.ExtractParams()[0][0]["vol"].nominal_value integral_volume = fit.ExtractIntegralParams()[0][0]["vol"].nominal_value assert isclose(integral_volume, expected_volume, abs_tol=10) assert isclose(fit_volume, expected_volume, abs_tol=200) hdtv.plugins.rootInterface.r.RootClose(None)
def test_mfile_fit_volume_with_binning(region1, region2, peak, expected_volume, calfactor, temp_file): spectra.Clear() bins = linspace(0.5, NBINS + 0.5, int(NBINS / calfactor)) spectrum = 1. / calfactor * BG_PER_BIN * ones(int(NBINS / calfactor)) spectrum = spectrum + calfactor * PEAK_VOLUME * norm.pdf( bins, loc=0.5 * NBINS, scale=PEAK_WIDTH) savetxt(temp_file, spectrum) spec_interface.LoadSpectra(temp_file) spectra.ApplyCalibration("0", [0, calfactor]) spectra.SetMarker("region", region1) spectra.SetMarker("region", region2) spectra.SetMarker("peak", peak) spectra.ExecuteFit() fit = spectra.workFit fit_volume = fit.ExtractParams()[0][0]["vol"].nominal_value integral_volume = fit.ExtractIntegralParams()[0][0]["vol"].nominal_value assert isclose(integral_volume, expected_volume, abs_tol=sqrt(PEAK_VOLUME)) assert isclose(fit_volume, expected_volume, abs_tol=sqrt(PEAK_VOLUME))
def test_root_to_root_conversion_for_unconventional_binning(): # Mock parser so RootGet can be used # args = mock.Mock() args = type('Test', (object, ), {}) args.replace = False args.load_cal = False args.invisible = False args.pattern = [os.path.join("test", "share", "binning.root", "h")] hdtv.plugins.rootInterface.r.RootGet(args) args.pattern = [os.path.join("test", "share", "binning.root", "h2")] hdtv.plugins.rootInterface.r.RootGet(args) assert not get_spec(0).cal assert isclose(get_spec(1).cal.GetCoeffs()[1], 0.5)
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)