Skip to content

Commit 5fac8ef

Browse files
Merge pull request #140 from lucabaldini/uniform_spectrum
Uniform spectrum
2 parents 6d0660b + 2c9a895 commit 5fac8ef

5 files changed

Lines changed: 65 additions & 14 deletions

File tree

docs/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
Release notes
44
=============
55

6+
* Uniform spectrum added to the `source.py` module.
67
* Minor bug fixes to equalization calibration.
78
* Pull requests merged and issues closed:
89

10+
- https://github.com/lucabaldini/hexsample/pull/140
911
- https://github.com/lucabaldini/hexsample/pull/137
1012

1113

src/hexsample/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ def add_source_options(parser: argparse.ArgumentParser) -> None:
348348
help="element generating the line forest")
349349
group.add_argument("--initial_level", type=str, default=source.LineForest.initial_level,
350350
help="initial level for the line forest")
351+
group.add_argument("--emin", type=float, default=source.UniformSpectrum.emin,
352+
help="minimum energy of the uniform spectrum in eV")
353+
group.add_argument("--emax", type=float, default=source.UniformSpectrum.emax,
354+
help="maximum energy of the uniform spectrum in eV")
351355
# ... morphological part...
352356
group.add_argument(f"--{source.BeamProxy.key()}", type=str,
353357
choices=source.BeamProxy.choices(),

src/hexsample/hexagon.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,65 +125,65 @@ def neighbors_even_q(col: int, row: int) -> tuple:
125125

126126

127127
_N_ADC_CHANNELS = 7
128-
_ADC_SEQUENCE_EVEN = (0, 2, 5, 0, 3, 5, 1, 3, 6, 1, 4, 6, 2, 4)
129-
_ADC_SEQUENCE_ODD = (0, 3, 5, 1, 3, 6, 1, 4, 6, 2, 4, 0, 2, 5)
128+
_ADC_SEQUENCE_EVEN = np.array([0, 2, 5, 0, 3, 5, 1, 3, 6, 1, 4, 6, 2, 4])
129+
_ADC_SEQUENCE_ODD = np.array([0, 3, 5, 1, 3, 6, 1, 4, 6, 2, 4, 0, 2, 5])
130130
_ADC_SEQUENCE_LENGTH = len(_ADC_SEQUENCE_EVEN)
131131

132132

133-
def adc_channel_odd_r(col: int, row: int) -> int:
133+
def adc_channel_odd_r(col: np.ndarray, row: np.ndarray) -> np.ndarray:
134134
"""Transformation from offset coordinates (col, row) into 7-adc channel label,
135135
that is an int between 0 and 6, for ODD_R grid layout.
136136
137137
Arguments
138138
---------
139-
col: int
139+
col: array_like
140140
column pixel logical coordinate
141-
row: int
141+
row: array_like
142142
row pixel logical coordinate
143143
"""
144144
start = _ADC_SEQUENCE_ODD[row % _ADC_SEQUENCE_LENGTH]
145145
index = (col + start) % _N_ADC_CHANNELS
146146
return index
147147

148-
def adc_channel_even_r(col: int, row: int) -> int:
148+
def adc_channel_even_r(col: np.ndarray, row: np.ndarray) -> np.ndarray:
149149
"""Transformation from offset coordinates (col, row) into 7-adc channel label,
150150
that is an int between 0 and 6, for EVEN_R grid layout.
151151
152152
Arguments
153153
---------
154-
col: int
154+
col: array_like
155155
column pixel logical coordinate
156-
row: int
156+
row: array_like
157157
row pixel logical coordinate
158158
"""
159159
start = _ADC_SEQUENCE_EVEN[row % _ADC_SEQUENCE_LENGTH]
160160
index = (col + start) % _N_ADC_CHANNELS
161161
return index
162162

163-
def adc_channel_odd_q(col: int, row: int) -> int:
163+
def adc_channel_odd_q(col: np.ndarray, row: np.ndarray) -> np.ndarray:
164164
"""Transformation from offset coordinates (col, row) into 7-adc channel label,
165165
that is an int between 0 and 6, for ODD_Q grid layout.
166166
167167
Arguments
168168
---------
169-
col: int
169+
col: array_like
170170
column pixel logical coordinate
171-
row: int
171+
row: array_like
172172
row pixel logical coordinate
173173
"""
174174
start = _ADC_SEQUENCE_ODD[col % _ADC_SEQUENCE_LENGTH]
175175
index = (row + start) % _N_ADC_CHANNELS
176176
return index
177177

178-
def adc_channel_even_q(col: int, row: int) -> int:
178+
def adc_channel_even_q(col: np.ndarray, row: np.ndarray) -> np.ndarray:
179179
"""Transformation from offset coordinates (col, row) into 7-adc channel label,
180180
that is an int between 0 and 6, for EVEN_Q grid layout.
181181
182182
Arguments
183183
---------
184-
col: int
184+
col: array_like
185185
column pixel logical coordinate
186-
row: int
186+
row: array_like
187187
row pixel logical coordinate
188188
"""
189189
start = _ADC_SEQUENCE_EVEN[col % _ADC_SEQUENCE_LENGTH]

src/hexsample/source.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"SpectrumProxy",
4545
"BeamProxy",
4646
"Source",
47+
"UniformSpectrum",
4748
]
4849

4950

@@ -140,6 +141,37 @@ def render(self, axes: matplotlib.axes.Axes, **kwargs) -> None:
140141
setup_gca(xlabel="Energy [eV]", ylabel="Relative intensity", logy=True, grids=True)
141142

142143

144+
@dataclass
145+
class UniformSpectrum(AbstractSpectrum):
146+
147+
"""Class describing a uniform X-ray energy spectrum between a minimum
148+
and a maximum energy.
149+
150+
Arguments
151+
---------
152+
emin : float
153+
The minimum energy in eV.
154+
155+
emax : float
156+
The maximum energy in eV.
157+
"""
158+
159+
emin: float = 1000.
160+
emax: float = 10000.
161+
162+
def rvs(self, size: int = 1) -> np.ndarray:
163+
"""Overloaded method.
164+
"""
165+
return rng.generator.uniform(self.emin, self.emax, size=size)
166+
167+
def render(self, axes: matplotlib.axes.Axes, **kwargs) -> None:
168+
"""Overloaded method.
169+
"""
170+
kwargs.setdefault("color", "black")
171+
axes.hlines(1., self.emin, self.emax, **kwargs)
172+
setup_gca(xlabel="Energy [eV]", ylabel="Relative intensity", grids=True)
173+
174+
143175
@dataclass
144176
class AbstractBeam(AbstractRandomGenerator):
145177

@@ -432,6 +464,7 @@ def rvs(self, size: int = 1) -> Tuple[np.ndarray, np.ndarray]:
432464
SpectrumProxy = TypeProxy("spectrum") # pylint: disable=invalid-name
433465
SpectrumProxy.register("line", Line, default=True)
434466
SpectrumProxy.register("forest", LineForest)
467+
SpectrumProxy.register("uniform", UniformSpectrum)
435468

436469

437470
# Definition of the type proxies for beam types.

tests/test_source.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Source,
3636
SquareBeam,
3737
TriangularBeam,
38+
UniformSpectrum,
3839
)
3940

4041
rng.initialize()
@@ -97,6 +98,17 @@ def test_mn_k_forest():
9798
_test_forest("Mn", chisq_test=False)
9899

99100

101+
def test_uniform_spectrum(emin: float = 1000., emax: float = 10000., num_events: int = 10000):
102+
"""Test the uniform spectrum.
103+
"""
104+
spectrum = UniformSpectrum(emin=emin, emax=emax)
105+
rvs = spectrum.rvs(num_events)
106+
assert len(rvs) == num_events
107+
assert np.all((rvs >= emin) & (rvs <= emax))
108+
plt.figure("Uniform spectrum")
109+
spectrum.plot()
110+
111+
100112
def test_point_beam(x0 : float = 1., y0 : float = -1., num_photons : int = 1000):
101113
"""Unit test for the point beam.
102114
"""

0 commit comments

Comments
 (0)