Skip to content

Commit f75cfe2

Browse files
authored
Merge pull request #575 from OpenCOMPES/fix_pose_adjustment
Fix pose adjustment
2 parents 3fb7d77 + 26ce57f commit f75cfe2

File tree

5 files changed

+93
-16
lines changed

5 files changed

+93
-16
lines changed

src/sed/calibrator/momentum.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,10 @@ def reset_deformation(self, **kwds):
874874
stackaxis=0,
875875
).astype("float64")
876876

877+
# reset image
878+
if self.slice_corrected is not None:
879+
self.slice_corrected = self.slice
880+
877881
self.rdeform_field = coordmat[1, ...]
878882
self.cdeform_field = coordmat[0, ...]
879883

src/sed/config/default.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ energy:
3737
# Number of bins to use for energy calibration traces
3838
bins: 1000
3939
# Bin ranges to use for energy calibration curves (for tof_binning=1)
40-
ranges: [100000, 150000]
40+
ranges: [256000, 276000]
4141
# Option to normalize energy calibration traces
4242
normalize: True
4343
# Pixel range for smoothing
@@ -54,7 +54,7 @@ energy:
5454
energy_scale: "kinetic"
5555
# Approximate position of the high-energy-cutoff in tof_column bins,
5656
# used for displaying a graph to choose the energy correction function parameters.
57-
tof_fermi: 132250
57+
tof_fermi: 264500
5858
# TOF range to visualize for the correction tool around tof_fermi
5959
tof_width: [-600, 1000]
6060
# x-integration range for the correction tool around the center pixel
@@ -72,7 +72,7 @@ momentum:
7272
# Bin numbers used for the respective axes
7373
bins: [512, 512, 300]
7474
# bin ranges to use (in unbinned detector coordinates)
75-
ranges: [[-256, 1792], [-256, 1792], [132000, 138000]]
75+
ranges: [[-256, 1792], [-256, 1792], [252000, 280000]]
7676
# The x/y pixel ranges of the detector
7777
detector_ranges: [[0, 2048], [0, 2048]]
7878
# The center pixel of the detector in the binned x/y coordinates
@@ -110,4 +110,4 @@ histogram:
110110
# Axes names starting with "@" refer to keys in the "dataframe" section
111111
axes: ["@x", "@y", "@tof"]
112112
# default ranges to use for histogram visualization (in unbinned detector coordinates)
113-
ranges: [[0, 1800], [0, 1800], [0, 150000]]
113+
ranges: [[0, 1800], [0, 1800], [0, 300000]]

src/sed/core/processor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ def pose_adjustment(
762762

763763
if not use_correction:
764764
self.mc.reset_deformation()
765+
reset = False
765766

766767
if self.mc.cdeform_field is None or self.mc.rdeform_field is None:
767768
# Generate distortion correction from config values

tests/calibrator/test_energy.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,12 @@ def test_adjust_energy_correction_raises(correction_type: str) -> None:
455455
Args:
456456
correction_type (str): type of correction to test
457457
"""
458-
config = parse_config(config={}, folder_config={}, user_config={}, system_config={})
458+
config = parse_config(
459+
config={"dataframe": {"tof_binning": 2}},
460+
folder_config={},
461+
user_config={},
462+
system_config={},
463+
)
459464
ec = EnergyCalibrator(
460465
config=config,
461466
loader=get_loader("mpes", config=config),
@@ -499,7 +504,12 @@ def test_energy_correction_from_dict_kwds(correction_type: str, correction_kwd:
499504
correction_type (str): type of correction to test
500505
correction_kwd (dict): parameters to pass to the function
501506
"""
502-
config = parse_config(config={}, folder_config={}, user_config={}, system_config={})
507+
config = parse_config(
508+
config={"dataframe": {"tof_binning": 2}},
509+
folder_config={},
510+
user_config={},
511+
system_config={},
512+
)
503513
sample_df = pd.DataFrame(sample, columns=columns)
504514
ec = EnergyCalibrator(
505515
config=config,

tests/test_processor.py

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def test_copy_tool() -> None:
287287
)
288288
def test_momentum_correction_workflow(features: np.ndarray) -> None:
289289
"""Test for the momentum correction workflow"""
290-
config = {"core": {"loader": "mpes"}}
290+
config = {"core": {"loader": "mpes"}, "dataframe": {"tof_binning": 2}}
291291
processor = SedProcessor(
292292
folder=df_folder,
293293
config=config,
@@ -296,7 +296,7 @@ def test_momentum_correction_workflow(features: np.ndarray) -> None:
296296
system_config={},
297297
verbose=True,
298298
)
299-
processor.bin_and_load_momentum_calibration(apply=True)
299+
processor.bin_and_load_momentum_calibration(plane=200, width=20, apply=True)
300300
assert processor.mc.slice is not None
301301
assert processor.mc.pouter is None
302302
if len(features) == 5 or len(features) == 7:
@@ -339,7 +339,7 @@ def test_momentum_correction_workflow(features: np.ndarray) -> None:
339339

340340
def test_pose_adjustment() -> None:
341341
"""Test for the pose correction and application of momentum correction workflow"""
342-
config = {"core": {"loader": "mpes"}}
342+
config = {"core": {"loader": "mpes"}, "dataframe": {"tof_binning": 2}}
343343
processor = SedProcessor(
344344
folder=df_folder,
345345
config=config,
@@ -351,7 +351,7 @@ def test_pose_adjustment() -> None:
351351
# pose adjustment w/o loaded image
352352
processor.pose_adjustment(**adjust_params, use_correction=False, apply=True)
353353

354-
processor.bin_and_load_momentum_calibration(apply=True)
354+
processor.bin_and_load_momentum_calibration(plane=200, width=20, apply=True)
355355
# test pose adjustment
356356
processor.pose_adjustment(**adjust_params, use_correction=False, apply=True)
357357

@@ -365,7 +365,7 @@ def test_pose_adjustment() -> None:
365365
)
366366
with pytest.raises(ValueError):
367367
processor.apply_momentum_correction()
368-
processor.bin_and_load_momentum_calibration(apply=True)
368+
processor.bin_and_load_momentum_calibration(plane=200, width=20, apply=True)
369369
processor.define_features(
370370
features=feature7,
371371
rotation_symmetry=6,
@@ -383,10 +383,72 @@ def test_pose_adjustment() -> None:
383383
processor.pose_adjustment(**adjust_params, apply=True, illegal_kwd=True)
384384

385385

386+
def test_pose_adjustment_use_correction() -> None:
387+
"""Test for the use of momentum correction in the pose adjustment workflow"""
388+
config = {"core": {"loader": "mpes"}, "dataframe": {"tof_binning": 2}}
389+
processor = SedProcessor(
390+
folder=df_folder,
391+
config=config,
392+
folder_config={},
393+
user_config={},
394+
system_config={},
395+
verbose=True,
396+
)
397+
processor.bin_and_load_momentum_calibration(plane=200, width=20, apply=True)
398+
# uncorrected reference
399+
processor.pose_adjustment(**adjust_params, use_correction=False, apply=True)
400+
uncorrected_slice = processor.mc.slice_transformed.copy()
401+
uncorrected_cdeform_field = processor.mc.cdeform_field.copy()
402+
uncorrected_rdeform_field = processor.mc.rdeform_field.copy()
403+
# apply correction, and use pose adjustment without correction
404+
processor.define_features(
405+
features=feature7,
406+
rotation_symmetry=6,
407+
include_center=True,
408+
apply=True,
409+
)
410+
processor.generate_splinewarp(use_center=True)
411+
processor.save_splinewarp(filename="sed_config_pose_adjustments_use_corrections.yaml")
412+
processor.pose_adjustment(**adjust_params, use_correction=False, apply=True)
413+
np.testing.assert_allclose(processor.mc.slice_transformed, uncorrected_slice)
414+
np.testing.assert_allclose(processor.mc.cdeform_field, uncorrected_cdeform_field)
415+
np.testing.assert_allclose(processor.mc.rdeform_field, uncorrected_rdeform_field)
416+
# apply correction, and use pose adjustment with correction
417+
processor.pose_adjustment(**adjust_params, use_correction=True, apply=True)
418+
assert not np.array_equal(processor.mc.slice_transformed, uncorrected_slice)
419+
assert not np.array_equal(processor.mc.cdeform_field, uncorrected_cdeform_field)
420+
assert not np.array_equal(processor.mc.rdeform_field, uncorrected_rdeform_field)
421+
corrected_cdeform_field = processor.mc.cdeform_field.copy()
422+
corrected_rdeform_field = processor.mc.rdeform_field.copy()
423+
424+
processor = SedProcessor(
425+
folder=df_folder,
426+
config=config,
427+
folder_config="sed_config_pose_adjustments_use_corrections.yaml",
428+
user_config={},
429+
system_config={},
430+
verbose=True,
431+
)
432+
# apply pose correction without loaded image
433+
# uncorrected reference
434+
processor.pose_adjustment(**adjust_params, use_correction=False, apply=True)
435+
np.testing.assert_allclose(processor.mc.cdeform_field, uncorrected_cdeform_field)
436+
np.testing.assert_allclose(processor.mc.rdeform_field, uncorrected_rdeform_field)
437+
# apply correction, and use pose adjustment with correction
438+
processor.generate_splinewarp(use_center=True)
439+
processor.pose_adjustment(**adjust_params, use_correction=True, apply=True)
440+
np.testing.assert_allclose(processor.mc.cdeform_field, corrected_cdeform_field)
441+
np.testing.assert_allclose(processor.mc.rdeform_field, corrected_rdeform_field)
442+
# apply correction, and use pose adjustment without correction
443+
processor.pose_adjustment(**adjust_params, use_correction=False, apply=True)
444+
np.testing.assert_allclose(processor.mc.cdeform_field, uncorrected_cdeform_field)
445+
np.testing.assert_allclose(processor.mc.rdeform_field, uncorrected_rdeform_field)
446+
447+
386448
def test_pose_adjustment_save_load() -> None:
387449
"""Test for the saving and loading of pose correction and application of momentum correction
388450
workflow"""
389-
config = {"core": {"loader": "mpes"}}
451+
config = {"core": {"loader": "mpes"}, "dataframe": {"tof_binning": 2}}
390452
# pose adjustment w/ loaded image
391453
processor = SedProcessor(
392454
folder=df_folder,
@@ -396,7 +458,7 @@ def test_pose_adjustment_save_load() -> None:
396458
system_config={},
397459
verbose=True,
398460
)
399-
processor.bin_and_load_momentum_calibration(apply=True)
461+
processor.bin_and_load_momentum_calibration(plane=200, width=20, apply=True)
400462
processor.define_features(
401463
features=feature7,
402464
rotation_symmetry=6,
@@ -458,7 +520,7 @@ def test_pose_adjustment_save_load() -> None:
458520

459521
def test_momentum_calibration_workflow() -> None:
460522
"""Test the calibration of the momentum axes"""
461-
config = {"core": {"loader": "mpes"}}
523+
config = {"core": {"loader": "mpes"}, "dataframe": {"tof_binning": 2}}
462524
processor = SedProcessor(
463525
folder=df_folder,
464526
config=config,
@@ -471,7 +533,7 @@ def test_momentum_calibration_workflow() -> None:
471533
processor.apply_momentum_calibration()
472534
with pytest.raises(ValueError):
473535
processor.calibrate_momentum_axes(apply=True)
474-
processor.bin_and_load_momentum_calibration(apply=True)
536+
processor.bin_and_load_momentum_calibration(plane=200, width=20, apply=True)
475537
with pytest.raises(AssertionError):
476538
processor.calibrate_momentum_axes(point_a=point_a, apply=True)
477539
processor.calibrate_momentum_axes(point_a=point_a, k_distance=k_distance, apply=True)
@@ -508,7 +570,7 @@ def test_momentum_calibration_workflow() -> None:
508570

509571
def test_energy_correction() -> None:
510572
"""Test energy correction workflow."""
511-
config = {"core": {"loader": "mpes"}}
573+
config = {"core": {"loader": "mpes"}, "dataframe": {"tof_binning": 2}}
512574
processor = SedProcessor(
513575
folder=df_folder,
514576
config=config,

0 commit comments

Comments
 (0)