From ea5d89c7098eeeaae6eb90b4c73fe9900c323427 Mon Sep 17 00:00:00 2001 From: chiara Date: Thu, 20 Jun 2024 16:15:33 +0200 Subject: [PATCH 1/5] Added continuum spectrum source in 7-9 keV --- hexsample/source.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/hexsample/source.py b/hexsample/source.py index 8cacc21c..553f03e5 100644 --- a/hexsample/source.py +++ b/hexsample/source.py @@ -205,8 +205,6 @@ def plot(self) -> None: """ raise NotImplementedError - - class LineForest(SpectrumBase): """Class describing a set of X-ray emission lines for a given element and @@ -269,8 +267,46 @@ def __str__(self): """String formatting. """ return f'{self.line_dict}' + +class ContinuumSpectrum(SpectrumBase): + """Class describing a continuum and uniform x-ray energy spectrum between + a minimum and a maximum value. By now the continuum spectrum is set in the + range [7000, 9000] eV, the possibility to choose it in the sim has to be + implemented. + Arguments + --------- + min_energy : float + Minimum energy of the spectrum + max_energy : float + Maximum energy of the spectrum + """ + def __init__(self, min_energy: float=7000, max_energy: float=9000) -> None: + self.minimum_energy = min_energy + self.maximum_energy = max_energy + def rvs(self, size: int = 1) -> np.ndarray: + """Throw random energies from the line forest. + + Arguments + --------- + size : int + The number of X-ray energies to be generated. + + Returns + ------- + energy : np.ndarray of shape ``size`` + The photon energies in eV. + """ + # Random generating the energies in [minimum_energy, maximum_energy] + return rng.generator.uniform(self.minimum_energy, self.maximum_energy, size) + + def plot(self) -> None: + """Plot the continuum spectrum for 1e4 generated photons. + """ + # pylint: disable=invalid-name + plt.hist(self.rvs(10000), density=True, width=0.0001, color='black') + setup_gca(xlabel='Energy [eV]', ylabel='pdf', logy=True, grids=True) class Source: From 75517cf0a4155cb6e2fdfddaa84538870d525b2d Mon Sep 17 00:00:00 2001 From: chiara Date: Thu, 20 Jun 2024 16:15:57 +0200 Subject: [PATCH 2/5] Added source option in hxsim --- hexsample/app.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hexsample/app.py b/hexsample/app.py index 307d9c98..12b1c1ba 100644 --- a/hexsample/app.py +++ b/hexsample/app.py @@ -188,6 +188,10 @@ def add_source_options(self) -> None: """Add an option group for the source properties. """ group = self.add_argument_group('source', 'X-ray source properties') + types = ['lines', 'continuum'] + group.add_argument('--spectrumtype', type=str, choices=types, default='lines', + help='type of spectrum, choices are between a discrete\ + and a continuum spectrum in the range [7000, 9000] eV.') group.add_argument('--srcelement', type=str, default='Cu', help='element generating the line forest') group.add_argument('--srclevel', type=str, default='K', From ed5fb79fc31abe1b7b846fc468106d2abceab79b Mon Sep 17 00:00:00 2001 From: chiara Date: Thu, 20 Jun 2024 16:16:21 +0200 Subject: [PATCH 3/5] minor --- hexsample/bin/hxsim.py | 8 ++++++-- scripts/analyze_sim.py | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hexsample/bin/hxsim.py b/hexsample/bin/hxsim.py index 8326af71..2fc5675a 100755 --- a/hexsample/bin/hxsim.py +++ b/hexsample/bin/hxsim.py @@ -35,7 +35,7 @@ from hexsample.hexagon import HexagonalLayout from hexsample.mc import PhotonList from hexsample.roi import Padding -from hexsample.source import LineForest, GaussianBeam, Source +from hexsample.source import ContinuumSpectrum, LineForest, GaussianBeam, Source from hexsample.sensor import Material, Sensor @@ -58,7 +58,11 @@ def hxsim(**kwargs): """ # pylint: disable=too-many-locals, invalid-name rng.initialize(seed=kwargs['seed']) - spectrum = LineForest(kwargs['srcelement'], kwargs['srclevel']) + spectrum_type = kwargs['spectrumtype'] + if spectrum_type == 'continuum': + spectrum = ContinuumSpectrum() + else: + spectrum = LineForest(kwargs['srcelement'], kwargs['srclevel']) beam = GaussianBeam(kwargs['srcposx'], kwargs['srcposy'], kwargs['srcsigma']) source = Source(spectrum, beam) material = Material(kwargs['actmedium'], kwargs['fano']) diff --git a/scripts/analyze_sim.py b/scripts/analyze_sim.py index 7f94458b..8f4f4f3c 100644 --- a/scripts/analyze_sim.py +++ b/scripts/analyze_sim.py @@ -39,8 +39,6 @@ def analyze_sim(thick : int, noise : int) -> None: """ thr = 2 * noise file_path = f'/Users/chiara/hexsampledata/hxsim_recon.h5' - #file_path = f'/Users/chiara/hexsampledata/sim_HexagonalLayout.ODD_Rum_0enc_srcsigma200um_recon.h5' - #file_path = f'/Users/chiara/hexsampledata/sim_{thick}um_{noise}enc_recon_nn2_thr{thr}.h5' recon_file = ReconInputFile(file_path) #Constructing the 1px mask cluster_size = recon_file.column('cluster_size') From 1f6046ea4526491a888d54cdf7ef7662eae41b12 Mon Sep 17 00:00:00 2001 From: chiara Date: Tue, 23 Jul 2024 10:56:55 +0200 Subject: [PATCH 4/5] minor --- hexsample/fileio.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hexsample/fileio.py b/hexsample/fileio.py index 74daa489..a2f27f8d 100644 --- a/hexsample/fileio.py +++ b/hexsample/fileio.py @@ -262,6 +262,7 @@ def _fill_recon_row(row: tables.tableextension.Row, event: ReconEvent) -> None: row['trigger_id'] = event.trigger_id row['timestamp'] = event.timestamp row['livetime'] = event.livetime + # Roi size is not implemented in all event type classes #row['roi_size'] = event.roi_size row['cluster_size'] = event.cluster.size() row['energy'] = event.energy() From e7919202cfb02c2a0dc7ea5c45a35a7aa428f16f Mon Sep 17 00:00:00 2001 From: chiara Date: Wed, 25 Sep 2024 21:33:31 +0200 Subject: [PATCH 5/5] minor --- hexsample/source.py | 5 +++-- scripts/analyze_sim.py | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/hexsample/source.py b/hexsample/source.py index 553f03e5..1f7e158b 100644 --- a/hexsample/source.py +++ b/hexsample/source.py @@ -305,8 +305,9 @@ def plot(self) -> None: """Plot the continuum spectrum for 1e4 generated photons. """ # pylint: disable=invalid-name - plt.hist(self.rvs(10000), density=True, width=0.0001, color='black') - setup_gca(xlabel='Energy [eV]', ylabel='pdf', logy=True, grids=True) + energies_ = self.rvs(10000) + plt.hist(energies_, density=True, color='tab:blue') + setup_gca(xlabel='Energy [eV]', ylabel='PDF', logy=True, grids=True) class Source: diff --git a/scripts/analyze_sim.py b/scripts/analyze_sim.py index 8f4f4f3c..7f94458b 100644 --- a/scripts/analyze_sim.py +++ b/scripts/analyze_sim.py @@ -39,6 +39,8 @@ def analyze_sim(thick : int, noise : int) -> None: """ thr = 2 * noise file_path = f'/Users/chiara/hexsampledata/hxsim_recon.h5' + #file_path = f'/Users/chiara/hexsampledata/sim_HexagonalLayout.ODD_Rum_0enc_srcsigma200um_recon.h5' + #file_path = f'/Users/chiara/hexsampledata/sim_{thick}um_{noise}enc_recon_nn2_thr{thr}.h5' recon_file = ReconInputFile(file_path) #Constructing the 1px mask cluster_size = recon_file.column('cluster_size')