def test_parameter_json(self): data_settings = fit2x.DataSettings(time_axis=x, data=irf_y, irf_histogram=irf_y) lifetime_settings = fit2x.LifetimeSettings(lifetime_spectrum=[1, 4]) convolution_settings = fit2x.ConvolutionSettings() scatter_settings = fit2x.PatternSettings() background_settings = fit2x.PatternSettings() pileup_settings = fit2x.PileupSettings() linearization_settings = fit2x.LinearizationSettings() scaling_settings = fit2x.ScalingSettings() score_settings = fit2x.ScoreSettings() dc = fit2x.Decay(data_settings=data_settings, lifetime_settings=lifetime_settings, scatter_settings=scatter_settings, convolution_settings=convolution_settings, background_settings=background_settings, pileup_settings=pileup_settings, linearization_settings=linearization_settings, scaling_settings=scaling_settings, score_settings=score_settings) dc.update() # Test filling, reading and writing. Fill with random data fill_random(dc) # JSON parameter comparison json_str = dc.to_json() p_json = json.loads(json_str) p_obj = dc.parameter self.assertEqual(compare_parameter(p_json, p_obj), True) # Write to JSON file and restore Decay object from JSON fh, fn = tempfile.mkstemp(".json") fp = open(fh, "w") fp.write(json_str) fp.close() decay_from_json_str = fit2x.Decay.from_json(json_str) decay_from_json_fn = fit2x.Decay.from_json(pathlib.Path(fn)) self.assertEqual( compare_parameter(decay_from_json_str.parameter, p_obj), True) self.assertEqual( compare_parameter(decay_from_json_fn.parameter, p_obj), True)
def test_decay_convolution_lifetime(self): np.random.seed(42) x = np.linspace(0, 20, 32) irf_position = 2.0 irf_width = 0.25 irf_y = scipy.stats.norm.pdf(x, loc=irf_position, scale=irf_width) data_settings = fit2x.DataSettings(time_axis=x, data=irf_y * 100000, irf_histogram=irf_y) lifetime_settings = fit2x.LifetimeSettings(lifetime_spectrum=[1, 2]) convolution_settings = fit2x.ConvolutionSettings() background_settings = fit2x.PatternSettings() pileup_settings = fit2x.PileupSettings() linearization_settings = fit2x.LinearizationSettings() scaling_settings = fit2x.ScalingSettings() score_settings = fit2x.ScoreSettings() dc = fit2x.Decay(data_settings=data_settings, lifetime_settings=lifetime_settings, convolution_settings=convolution_settings, background_settings=background_settings, pileup_settings=pileup_settings, linearization_settings=linearization_settings, scaling_settings=scaling_settings, score_settings=score_settings) dc.update() dc.data = np.random.poisson(1000 / max(dc.y) * dc.y) scores = list() lts = np.linspace(0.0, 4, 11) for lt in lts: dc.lifetime_spectrum = [1, lt] scores.append(dc.score) scores_ref = [ 3641.4913153320936, 3023.436739258023, 1648.7875155218765, 731.3338936915842, 190.31400668610308, 23.229701466698423, 377.664429861769, 1529.1599392280614, 3840.3130365516517, 7698.247948184009, 13453.340913686912 ] self.assertEqual(np.allclose(scores, scores_ref), True)
def test_file_io(self): dc = fit2x.Decay() dc.update() fill_random(dc) # JSON parameter comparison json_str = dc.to_json() # Write to JSON file and restore Decay object from JSON fh, fn = tempfile.mkstemp(".json") fp = open(fh, "w") fp.write(json_str) fp.close() decay_from_json_str = fit2x.Decay.from_json(json_str) decay_from_json_fn = fit2x.Decay.from_json(pathlib.Path(fn)) self.assertEqual( compare_parameter(decay_from_json_str.parameter, dc.parameter), True) self.assertEqual( compare_parameter(decay_from_json_fn.parameter, dc.parameter), True)
data_settings = fit2x.DataSettings(time_axis=x, data=irf_y * 100000, irf_histogram=irf_y) lifetime_settings = fit2x.LifetimeSettings(lifetime_spectrum=[1, 4]) convolution_settings = fit2x.ConvolutionSettings() background_settings = fit2x.PatternSettings() pileup_settings = fit2x.PileupSettings() linearization_settings = fit2x.LinearizationSettings() scaling_settings = fit2x.ScalingSettings() score_settings = fit2x.ScoreSettings() dc = fit2x.Decay(data_settings=data_settings, lifetime_settings=lifetime_settings, convolution_settings=convolution_settings, background_settings=background_settings, pileup_settings=pileup_settings, linearization_settings=linearization_settings, scaling_settings=scaling_settings, score_settings=score_settings) dc.update() # lifetime import pylab as plt dc.lifetime_spectrum = [1, 4] plt.semilogy(dc.x, dc.y, label="lt 4") dc.lifetime_spectrum = [1, 2] dc.update() plt.semilogy(dc.x, dc.y, label="lt 2") plt.legend() plt.show()
n_ph_max=5, macro_time_calibration=data.header.macro_time_resolution, invert=True) high_count_selection = data_ch0[cr_selection] data_decay, _ = np.histogram(high_count_selection.micro_times, bins=n_bins) time_axis = np.arange( 0, n_bins ) * data.header.micro_time_resolution * 4096 * 1e9 / n_bins # in [ns] ################################### # Make decay object ################################### decay = fit2x.Decay(data=data_decay.astype(np.float64), irf_histogram=irf.astype(np.float64), time_axis=time_axis, scale_model_to_data=True, excitation_period=max(time_axis), lifetime_spectrum=[1., 1.2, 1., 3.5], abs_lifetime_spectrum=True, convolution_method=fit2x.ConvFastPeriodicTime) decay.update_model() # A minimum number of photons should be in each channel # as no MLE is used and Gaussian distributed errors are assumed sel = np.where(data_decay > 1)[0] x_min = 2 #int(min(sel)) # The BH card are not linear at the end of the TAC. Thus # fit not the full range x_max = 110 #max(sel) # Set some initial values for the fit