Skip to content

Commit ee97aa4

Browse files
committed
add sympy to runtime dependencies and extract coordinate system determination from logs into a method
1 parent 472b270 commit ee97aa4

File tree

8 files changed

+192
-122
lines changed

8 files changed

+192
-122
lines changed

pixi.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ matplotlib = "==3.9.4"
104104
plot-publisher = ">=1.0,<2.0"
105105
pyqt = ">=5,<6"
106106
qtpy = ">=2.4.3"
107+
sympy = ">=1.14.0,<2"
107108

108109
[tool.pixi.package]
109110
name = "lr_reduction"

src/lr_reduction/data_info.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,66 @@ def from_workspace(cls, input_workspace: MantidWorkspace):
3737
sample_logs = SampleLogValues(input_workspace)
3838
value = cls.REFLECTED_BEAM
3939
try:
40+
coordinate_system = CoordinateSystem.from_workspace(input_workspace)
4041
# Determine whether this is a direct beam based on the geometry
41-
if (("BL4B:CS:Mode:Coordinates" in sample_logs and sample_logs["BL4B:CS:Mode:Coordinates"] == 0) or # This is a new log for earth-centered
42-
sample_logs["BL4B:CS:ExpPl:OperatingMode"] == "Free Liquid"): # This is for backward compatibility from before the new log value
43-
# Earth-centered coordinate system
42+
if coordinate_system == CoordinateSystem.EARTH_CENTERED:
4443
thi = sample_logs["thi"]
4544
tthd = sample_logs["tthd"]
4645
if np.isclose(thi, tthd, atol=0.01):
4746
value = cls.DIRECT_BEAM
48-
else:
47+
elif coordinate_system == CoordinateSystem.BEAM_CENTERED:
4948
# Beam-centered coordinate system
5049
ths = sample_logs["ths"]
5150
tthd = sample_logs["tthd"]
5251
if np.fabs(tthd) < 0.001 and np.fabs(ths) < 0.001:
5352
value = cls.DIRECT_BEAM
54-
except KeyError as e:
53+
else:
54+
logger.warning("Unknown coordinate system; assuming reflected beam")
55+
except RuntimeError as e:
5556
logger.warning(f"Missing sample log {e}, assuming reflected beam")
5657
return value
5758

5859
def __str__(self):
5960
return self.name
61+
62+
63+
class CoordinateSystem(IntEnum):
64+
"""
65+
Enum to represent the coordinate system used in the experiment.
66+
67+
Attributes:
68+
UNKNOWN (int): Represents unknown coordinate system.
69+
EARTH_CENTERED (int): Represents the Earth-centered coordinate system.
70+
BEAM_CENTERED (int): Represents the Beam-centered coordinate system.
71+
"""
72+
73+
UNKNOWN = -1
74+
EARTH_CENTERED = 0
75+
BEAM_CENTERED = 1
76+
77+
@classmethod
78+
def from_workspace(cls, input_workspace: MantidWorkspace):
79+
"""
80+
Determine the coordinate system from the given workspace.
81+
"""
82+
sample_logs = SampleLogValues(input_workspace)
83+
value = cls.UNKNOWN
84+
try:
85+
# This is the new log for coordinate system mode
86+
if "BL4B:CS:Mode:Coordinates" in sample_logs:
87+
if sample_logs["BL4B:CS:Mode:Coordinates"] == 0:
88+
value = cls.EARTH_CENTERED
89+
else:
90+
value = cls.BEAM_CENTERED
91+
# Fallback to older method using operating mode
92+
elif "BL4B:CS:ExpPl:OperatingMode" in sample_logs:
93+
if sample_logs["BL4B:CS:ExpPl:OperatingMode"] == "Free Liquid":
94+
value = cls.EARTH_CENTERED
95+
else:
96+
value = cls.BEAM_CENTERED
97+
except RuntimeError as e:
98+
logger.warning(f"Missing sample log {e}, unable to determine coordinate system")
99+
return value
100+
101+
def __str__(self):
102+
return self.name

src/lr_reduction/event_reduction.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sympy as sp
1414
from scipy.optimize import brentq
1515

16+
from lr_reduction.data_info import CoordinateSystem
1617
from lr_reduction.gravity_correction import GravityDirection, gravity_correction
1718
from lr_reduction.instrument_settings import InstrumentSettings
1819
from lr_reduction.utils import mantid_algorithm_exec
@@ -574,7 +575,6 @@ def to_dict(self):
574575
norm_run=norm_run,
575576
time=time.ctime(),
576577
dq0=dq0,
577-
#dq_over_q=self.dq_over_q, # This is no longer a single value. Is it ok just to remove from the meta_data?
578578
sequence_number=sequence_number,
579579
sequence_id=sequence_id,
580580
q_summing=self.q_summing,
@@ -1292,13 +1292,8 @@ def compute_theta_resolution(ws, default_dq=0.027, theta=None, q_summing=False):
12921292
settings = read_settings(ws)
12931293

12941294
if theta is None:
1295-
if (
1296-
"BL4B:CS:ExpPl:OperatingMode" in ws.getRun()
1297-
and ws.getRun().getProperty("BL4B:CS:ExpPl:OperatingMode").value[0] == "Free Liquid"
1298-
) or (
1299-
"BL4B:CS:Mode:Coordinates" in ws.getRun()
1300-
and ws.getRun().getProperty("BL4B:Mode:Coordinates").value[0] == 0 # Earth-centered=0
1301-
):
1295+
coordinate_system = CoordinateSystem.from_workspace(ws)
1296+
if coordinate_system == CoordinateSystem.EARTH_CENTERED:
13021297
Theta_deg = abs(ws.getRun().getProperty("thi").value[0])
13031298
theta = np.radians(Theta_deg)
13041299
else:
@@ -1418,13 +1413,8 @@ def trapezoidal_distribution_params(ws, Theta_deg=None, FootPrint=None, SlitRati
14181413
dS1Si = dS1Samp - dSiSamp
14191414

14201415
if Theta_deg is None:
1421-
if (
1422-
"BL4B:CS:ExpPl:OperatingMode" in ws.getRun()
1423-
and ws.getRun().getProperty("BL4B:CS:ExpPl:OperatingMode").value[0] == "Free Liquid"
1424-
) or (
1425-
"BL4B:CS:Mode:Coordinates" in ws.getRun()
1426-
and ws.getRun().getProperty("BL4B:Mode:Coordinates").value[0] == 0 # Earth-centered=0
1427-
):
1416+
coordinate_system = CoordinateSystem.from_workspace(ws)
1417+
if coordinate_system == CoordinateSystem.EARTH_CENTERED:
14281418
Theta_deg = abs(ws.getRun().getProperty("thi").value[0])
14291419
else:
14301420
Theta_deg = abs(ws.getRun().getProperty("ths").value[0])

src/lr_reduction/gravity_correction.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44

5+
from lr_reduction.data_info import CoordinateSystem
56
from lr_reduction.typing import MantidWorkspace
67
from lr_reduction.utils import workspace_handle
78

@@ -94,15 +95,11 @@ def _theta_in(workspace: MantidWorkspace) -> float:
9495

9596
# Angle calculated from thi and a flag on earth-centered vs beam-centered
9697
thi = _log_value(run, "BL4B:Mot:thi.RBV")
97-
if "BL4B:CS:Mode:Coordinates" in run:
98-
if _log_value(run, "BL4B:CS:Mode:Coordinates") == 0: # Earth-centered=0
99-
theta_in = thi
100-
else:
101-
theta_in = thi - 4.0 # Beamline optics gives -4 deg incline. In future will have PV.
102-
elif _log_value(run, "BL4B:CS:ExpPl:OperatingMode", default="") == "Free Liquid":
98+
coordinate_system = CoordinateSystem.from_workspace(workspace)
99+
if coordinate_system == CoordinateSystem.EARTH_CENTERED:
103100
theta_in = thi
104101
else:
105-
theta_in = thi - 4.0
102+
theta_in = thi - 4.0 # Beamline optics gives -4 deg incline. In future will have PV.
106103
return abs(theta_in)
107104

108105

src/lr_reduction/template.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mantid.simpleapi import logger
1212

1313
from lr_reduction import event_reduction, peak_finding, reduction_template_reader
14+
from lr_reduction.data_info import CoordinateSystem
1415
from lr_reduction.instrument_settings import InstrumentSettings
1516
from lr_reduction.reduction_template_reader import ReductionParameters
1617
from lr_reduction.typing import MantidWorkspace
@@ -262,13 +263,8 @@ def process_from_template_ws(
262263
if theta_value is not None:
263264
theta = theta_value * np.pi / 180.0
264265
else:
265-
if (
266-
"BL4B:CS:ExpPl:OperatingMode" in ws_sc.getRun()
267-
and ws_sc.getRun().getProperty("BL4B:CS:ExpPl:OperatingMode").value[0] == "Free Liquid"
268-
) or (
269-
"BL4B:CS:Mode:Coordinates" in ws_sc.getRun()
270-
and ws_sc.getRun().getProperty("BL4B:Mode:Coordinates").value[0] == 0 # Earth-centered=0
271-
):
266+
coordinate_system = CoordinateSystem.from_workspace(ws_sc)
267+
if coordinate_system == CoordinateSystem.EARTH_CENTERED:
272268
theta = thi_value * np.pi / 180.0
273269
else:
274270
theta = ths_value * np.pi / 180.0

0 commit comments

Comments
 (0)