Skip to content

Commit 7de87cc

Browse files
committed
Fixed conversion to energy. Cleaned up code. this refs ##230
1 parent 0311550 commit 7de87cc

File tree

4 files changed

+34
-224
lines changed

4 files changed

+34
-224
lines changed

notebooks/__code/normalization_tof/normalization_for_timepix.py

Lines changed: 11 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
from IPython.core.display import HTML
1414
import pandas as pd
1515
import matplotlib.pyplot as plt
16-
from enum import Enum
17-
from scipy.constants import h, c, electron_volt, m_n
16+
# from enum import Enum
17+
# from scipy.constants import h, c, electron_volt, m_n
18+
19+
from __code.normalization_tof.units import convert_array_from_time_to_lambda, convert_array_from_time_to_energy
20+
from __code.normalization_tof.units import TimeUnitOptions, DistanceUnitOptions, EnergyUnitOptions
1821

1922
LOG_PATH = "/SNS/VENUS/shared/log/"
2023
LOAD_DTYPE = np.uint16
@@ -84,194 +87,6 @@ def retrieve_list_of_tif(folder: str) -> list:
8487
return list_tif
8588

8689

87-
class TimeUnitOptions(str, Enum):
88-
s = "s"
89-
ms = "ms"
90-
us = "us"
91-
ns = "ns"
92-
ps = "ps"
93-
94-
95-
class DistanceUnitOptions(str, Enum):
96-
cm = "cm"
97-
nm = "nm"
98-
pm = "pm"
99-
m = "m"
100-
angstrom = "angstrom"
101-
102-
103-
class EnergyUnitOptions(str, Enum):
104-
meV = "meV"
105-
eV = "eV"
106-
keV = "keV"
107-
MeV = "MeV"
108-
GeV = "GeV"
109-
J = "J"
110-
111-
112-
def convert_time_units(from_unit, to_unit):
113-
"""Convert time from one unit to another unit
114-
based on TimeUnitOptions options
115-
116-
Args:
117-
from_unit (TimeUnitOptions): Unit to convert from.
118-
to_unit (TimeUnitOptions): Unit to convert to.
119-
120-
Returns:
121-
float: Time in the new unit.
122-
"""
123-
124-
# Conversion factors
125-
conversion_factors = {
126-
TimeUnitOptions.s: 1,
127-
TimeUnitOptions.ms: 1e-3,
128-
TimeUnitOptions.us: 1e-6,
129-
TimeUnitOptions.ns: 1e-9,
130-
TimeUnitOptions.ps: 1e-12,
131-
}
132-
133-
return conversion_factors[from_unit] / conversion_factors[to_unit]
134-
135-
136-
def convert_to_energy(from_unit, to_unit):
137-
"""Convert energy from one unit to another unit
138-
based on EnergyUnitOptions options
139-
140-
Args:
141-
from_unit (EnergyUnitOptions): Unit to convert from.
142-
to_unit (EnergyUnitOptions): Unit to convert to.
143-
144-
Returns:
145-
float: Energy in the new unit.
146-
"""
147-
148-
# Conversion factors
149-
conversion_factors = {
150-
EnergyUnitOptions.meV: 1e-3,
151-
EnergyUnitOptions.eV: 1,
152-
EnergyUnitOptions.keV: 1e3,
153-
EnergyUnitOptions.MeV: 1e6,
154-
EnergyUnitOptions.GeV: 1e9,
155-
# EnergyUnitOptions.J: 6.242e12,
156-
EnergyUnitOptions.J: 1/electron_volt,
157-
}
158-
159-
return conversion_factors[from_unit] / conversion_factors[to_unit]
160-
161-
162-
def convert_distance_units(from_unit, to_unit):
163-
"""Convert distance from one unit to another unit
164-
based on DistanceUnitOptions options
165-
166-
Args:
167-
from_unit (DistanceUnitOptions): Unit to convert from.
168-
to_unit (DistanceUnitOptions): Unit to convert to.
169-
170-
Returns:
171-
float: distance in the new unit.
172-
"""
173-
174-
# Conversion factors
175-
conversion_factors = {
176-
DistanceUnitOptions.nm: 1e-9,
177-
DistanceUnitOptions.cm: 1e-2,
178-
DistanceUnitOptions.pm: 1e-12,
179-
DistanceUnitOptions.m: 1,
180-
DistanceUnitOptions.angstrom: 1e-10,
181-
}
182-
183-
return conversion_factors[from_unit] / conversion_factors[to_unit]
184-
185-
186-
def convert_array_from_time_to_lambda(time_array: np.ndarray,
187-
time_unit: TimeUnitOptions,
188-
distance_source_detector: float,
189-
distance_source_detector_unit: DistanceUnitOptions,
190-
detector_offset: float,
191-
detector_offset_unit: DistanceUnitOptions,
192-
lambda_unit: DistanceUnitOptions) -> np.ndarray:
193-
"""Convert an array of time values to wavelength values.
194-
195-
Args:
196-
time_array (np.ndarray): Array of time values.
197-
time_unit (TimeUnitOptions): Unit of the input time.
198-
distance_source_detector (float): Distance from the source to the detector.
199-
distance_source_detector_unit (DistanceUnitOptions): Unit of the distance.
200-
detector_offset (float): Offset of the detector.
201-
detector_offset_unit (DistanceUnitOptions): Unit of the offset.
202-
lambda_unit (DistanceUnitOptions): Unit of the output wavelength.
203-
204-
This is using the formula: lambda_m = h/(m_n * distance_source_detector_m) * (time_array_s + detector_offset_s)
205-
206-
Returns:
207-
np.ndarray: Array of wavelength values.
208-
"""
209-
time_array_s = time_array * convert_time_units(time_unit, TimeUnitOptions.s)
210-
detector_offset_s = detector_offset * convert_time_units(detector_offset_unit, TimeUnitOptions.s)
211-
distance_source_detector_m = distance_source_detector * convert_distance_units(distance_source_detector_unit, DistanceUnitOptions.m)
212-
213-
h_over_mn = h / m_n
214-
lambda_m = h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m
215-
216-
lambda_converted = lambda_m * convert_distance_units(DistanceUnitOptions.m, lambda_unit)
217-
218-
return lambda_converted
219-
220-
221-
def convert_from_wavelength_to_energy_ev(wavelength,
222-
unit_from=DistanceUnitOptions.angstrom):
223-
"""Convert wavelength to energy based on the given units.
224-
225-
Args:
226-
wavelength (float): Wavelength value.
227-
unit_from (WavelengthUnitOptions): Unit of the input wavelength.
228-
229-
Returns:
230-
float: Energy in the new unit.
231-
"""
232-
wavelength_m = wavelength * convert_distance_units(unit_from, DistanceUnitOptions.m)
233-
energy = h * c / wavelength_m
234-
energy = energy * convert_to_energy(EnergyUnitOptions.J, EnergyUnitOptions.eV)
235-
return energy
236-
237-
238-
def convert_array_from_time_to_energy(time_array: np.ndarray,
239-
time_unit: TimeUnitOptions,
240-
distance_source_detector: float,
241-
distance_source_detector_unit: DistanceUnitOptions,
242-
detector_offset: float,
243-
detector_offset_unit: DistanceUnitOptions,
244-
energy_unit: EnergyUnitOptions) -> np.ndarray:
245-
"""Convert an array of time values to energy values.
246-
247-
Args:
248-
time_array (np.ndarray): Array of time values.
249-
time_unit (TimeUnitOptions): Unit of the input time.
250-
distance_source_detector (float): Distance from the source to the detector.
251-
distance_source_detector_unit (DistanceUnitOptions): Unit of the distance.
252-
detector_offset (float): Offset of the detector.
253-
detector_offset_unit (DistanceUnitOptions): Unit of the offset.
254-
energy_unit (EnergyUnitOptions): Unit of the output energy.
255-
256-
This is using the formula: E = h/(m_n * distance_source_detector_m) * (time_array_s + detector_offset_s)
257-
258-
Returns:
259-
np.ndarray: Array of energy values.
260-
"""
261-
lambda_in_angstroms = convert_array_from_time_to_lambda(time_array,
262-
time_unit,
263-
distance_source_detector,
264-
distance_source_detector_unit,
265-
detector_offset,
266-
detector_offset_unit,
267-
DistanceUnitOptions.angstrom)
268-
269-
energy_array_ev = [convert_from_wavelength_to_energy_ev(l, DistanceUnitOptions.angstrom) for l in lambda_in_angstroms]
270-
energy_array = np.array(energy_array_ev)
271-
272-
return energy_array
273-
274-
27590
def normalization_with_list_of_runs(sample_run_numbers: list = None,
27691
ob_run_numbers: list = None,
27792
output_folder: str ="./",
@@ -448,12 +263,12 @@ def normalization_with_list_of_runs(sample_run_numbers: list = None,
448263
detector_offset_unit=TimeUnitOptions.us,
449264
lambda_unit=DistanceUnitOptions.angstrom)
450265
energy_array = convert_array_from_time_to_energy(time_array=time_spectra,
451-
time_unit=TimeUnitOptions.s,
452-
distance_source_detector=distance_source_detector_m,
453-
distance_source_detector_unit=DistanceUnitOptions.m,
454-
detector_offset=detector_delay_us,
455-
detector_offset_unit=TimeUnitOptions.us,
456-
energy_unit=EnergyUnitOptions.eV)
266+
time_unit=TimeUnitOptions.s,
267+
distance_source_detector=distance_source_detector_m,
268+
distance_source_detector_unit=DistanceUnitOptions.m,
269+
detector_offset=detector_delay_us,
270+
detector_offset_unit=TimeUnitOptions.us,
271+
energy_unit=EnergyUnitOptions.eV)
457272

458273
if preview:
459274

notebooks/__code/normalization_tof/normalization_tof.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ def manually_set_runs(self):
6969

7070
display(HTML("<span style='font-size: 16px; color:red'>You have the option here to enter the runs manually or just use the widgets (following cells) to define them!</span>"))
7171

72-
73-
7472
if self.debug:
7573
sample_runs = DEBUG_DATA.sample_runs_selected
7674
sample_run_numbers_list = []

notebooks/__code/normalization_tof/units.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,31 @@ def convert_array_from_time_to_energy(time_array: np.ndarray,
205205
detector_offset_unit (DistanceUnitOptions): Unit of the offset.
206206
energy_unit (EnergyUnitOptions): Unit of the output energy.
207207

208-
This is using the formula: E = h/(m_n * distance_source_detector_m) * (time_array_s + detector_offset_s)
209-
208+
#This is using the formula: E = h/(m_n * distance_source_detector_m) * (time_array_s + detector_offset_s)
209+
this is using the formula: E_ev = 1/2 m_n v^2
210+
with t_tof = L/v (L distance source to detector in m, v velocity of the neutron in m/s)
211+
so E_ev = 1/2 m_n (L/t_tof)^2
212+
210213
Returns:
211214
np.ndarray: Array of energy values.
212215
"""
213-
lambda_in_angstroms = convert_array_from_time_to_lambda(time_array,
214-
time_unit,
215-
distance_source_detector,
216-
distance_source_detector_unit,
217-
detector_offset,
218-
detector_offset_unit,
219-
DistanceUnitOptions.angstrom)
220-
221-
energy_array_ev = [convert_from_wavelength_to_energy_ev(l, DistanceUnitOptions.angstrom) for l in lambda_in_angstroms]
222-
energy_array = np.array(energy_array_ev)
216+
217+
time_units_factor = convert_time_units(time_unit, TimeUnitOptions.s)
218+
time_array_s = time_array * time_units_factor
219+
220+
detector_units_factor = convert_time_units(detector_offset_unit, TimeUnitOptions.s)
221+
detector_offset = detector_units_factor * detector_offset
222+
223+
distance_source_detector_factor = convert_distance_units(distance_source_detector_unit, DistanceUnitOptions.m)
224+
distance_source_detector_m = distance_source_detector * distance_source_detector_factor
225+
226+
# Calculate the energy in eV using the formula E_ev = 1/2 m_n (L/t_tof)^2 / electron_volt
227+
228+
full_time_array_s = time_array_s + detector_offset
229+
energy_array_ev = 0.5 * m_n * (distance_source_detector_m / full_time_array_s) ** 2 / electron_volt
230+
231+
energy_array_factor = convert_to_energy(EnergyUnitOptions.eV, energy_unit)
232+
energy_array = energy_array_ev * energy_array_factor
233+
energy_array = np.array(energy_array)
223234

224235
return energy_array

notebooks/normalization_tof.ipynb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,6 @@
178178
"source": [
179179
"o_norm.run_normalization_with_list_of_runs(preview=True)"
180180
]
181-
},
182-
{
183-
"cell_type": "code",
184-
"execution_count": null,
185-
"metadata": {},
186-
"outputs": [],
187-
"source": []
188-
},
189-
{
190-
"cell_type": "code",
191-
"execution_count": null,
192-
"metadata": {},
193-
"outputs": [],
194-
"source": []
195181
}
196182
],
197183
"metadata": {

0 commit comments

Comments
 (0)