Skip to content

Commit

Permalink
Merge pull request #107 from neutrons/skip_auto_peak_finder
Browse files Browse the repository at this point in the history
Skip auto-peak finder also when loading a different run
  • Loading branch information
backmari authored Dec 4, 2023
2 parents 937cacc + f82c414 commit f34c7cd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
23 changes: 4 additions & 19 deletions RefRed/calculations/check_list_run_compatibility_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,14 @@ def update_lconfigdataset(self):
_lconfig.norm_runs_compatible = runs_are_compatible

big_table_data[self.row, 2] = _lconfig
self.parent.big_table_data = big_table_data

def loading_lr_data(self):
"""Updates data table with the new run."""
wks = self.wks
lrdata = LRData(wks, parent=self.parent)
self.lrdata = lrdata
big_table_data = self.parent.big_table_data
col_index = 0 if self.is_working_with_data_column else 1
# keep existing user config if the new run number is the same as the old one
if self.is_reloading_same_run():
lrdata.peak = big_table_data[self.row, col_index].peak
lrdata.back = big_table_data[self.row, col_index].back
lrdata.low_res = big_table_data[self.row, col_index].low_res
big_table_cell = big_table_data[self.row, col_index]
# when loading a run in an existing cell, big_table_cell is not None and LRData will copy existing config
lrdata = LRData(wks, parent=self.parent, reduction_table_cell=big_table_cell)
self.lrdata = lrdata
big_table_data[self.row, col_index] = lrdata
self.parent.big_table_data = big_table_data

def is_reloading_same_run(self):
"""Checks if the new run number is the same as the old, i.e. the run is being reloaded."""
col_index = 0 if self.is_working_with_data_column else 1
big_table_data_run = self.parent.big_table_data[self.row, col_index]
if big_table_data_run is not None and (
big_table_data_run.run_number == self.parent.ui.reductionTable.item(self.row, self.col).text()
):
return True
return False
55 changes: 39 additions & 16 deletions RefRed/calculations/lr_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,21 @@ class LRData(object):
is_better_chopper_coverage = True
total_counts = 0

def __init__(self, workspace, lconfig=None, is_data=True, parent=None):
def __init__(self, workspace, lconfig=None, is_data=True, parent=None, reduction_table_cell=None):
"""
Constructor for LRData class
:param workspace: run workspace
:param lconfig: if defined, use for peak, background, low res, TOF range
:param is_data: if True, this is the data run, otherwise it is the normalization run
:param parent: parent object
:param reduction_table_cell: if defined, use for peak, background, low res, TOF range
"""
# TODO: there appears to be duplicate instances of this class being
# created when loading a file.
if lconfig and reduction_table_cell:
raise ValueError("Can't use both lconfig and reduction_table_cell")

self.parent = parent
self._tof_axis = []
self.Ixyt = []
Expand Down Expand Up @@ -162,21 +174,7 @@ def __init__(self, workspace, lconfig=None, is_data=True, parent=None):
self.data_loaded = False
self.read_data()

if lconfig is None:
pf = PeakFinderDerivation(list(range(len(self.ycountsdata))), self.ycountsdata)
[peak1, peak2] = pf.getPeaks()
self.peak = [str(peak1), str(peak2)]

backOffsetFromPeak = self.read_options['back_offset_from_peak']
back1 = int(peak1 - backOffsetFromPeak)
back2 = int(peak2 + backOffsetFromPeak)
self.back = [str(back1), str(back2)]

lw_pf = LowResFinder(list(range(len(self.countsxdata))), self.countsxdata)
[lowres1, lowres2] = lw_pf.get_low_res()
self.low_res = [str(lowres1), str(lowres2)]

else:
if lconfig is not None:
self.tof_auto_flag = bool(lconfig.tof_auto_flag)

if is_data:
Expand All @@ -192,6 +190,31 @@ def __init__(self, workspace, lconfig=None, is_data=True, parent=None):
self.low_res_flag = bool(lconfig.norm_low_res_flag)
self.back_flag = bool(lconfig.norm_back_flag)

elif reduction_table_cell is not None:
self.tof_auto_flag = reduction_table_cell.tof_auto_flag
self.tof_range_auto = reduction_table_cell.tof_range_auto
self.tof_range = reduction_table_cell.tof_range_auto
self.tof_range_manual = reduction_table_cell.tof_range_auto
self.peak = reduction_table_cell.peak
self.back = reduction_table_cell.back
self.low_res = reduction_table_cell.low_res
self.low_res_flag = reduction_table_cell.low_res_flag
self.back_flag = reduction_table_cell.back_flag

else:
pf = PeakFinderDerivation(list(range(len(self.ycountsdata))), self.ycountsdata)
[peak1, peak2] = pf.getPeaks()
self.peak = [str(peak1), str(peak2)]

backOffsetFromPeak = self.read_options['back_offset_from_peak']
back1 = int(peak1 - backOffsetFromPeak)
back2 = int(peak2 + backOffsetFromPeak)
self.back = [str(back1), str(back2)]

lw_pf = LowResFinder(list(range(len(self.countsxdata))), self.countsxdata)
[lowres1, lowres2] = lw_pf.get_low_res()
self.low_res = [str(lowres1), str(lowres2)]

# Properties for easy data access
# return the size of the data stored in memory for this dataset
@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,41 @@ def test_load_run_auto_peak_finder(mock_file_finder_find_runs, mock_display_plot
qtbot.addWidget(window_main)

run1 = "188300"
expected_peak_run1 = ["130", "141"]
expected_back_run1 = ["127", "144"]
run2 = "188301"
expected_peak_run2 = ["130", "139"]
expected_back_run2 = ["127", "142"]
expected_peak = ["130", "141"]
expected_back = ["127", "144"]
expected_tof_range_auto = [31420.7610, 44712.6203]

# load the first run
load_run_from_reduction_table(window_main, row=0, col=1, run=run1)
qtbot.wait(wait)
# check the result of the auto-peak finder
assert window_main.big_table_data[0, 0].peak == expected_peak_run1
assert window_main.big_table_data[0, 0].back == expected_back_run1
assert window_main.big_table_data[0, 0].peak == expected_peak
assert window_main.big_table_data[0, 0].back == expected_back
assert window_main.big_table_data[0, 0].tof_range_auto == pytest.approx(expected_tof_range_auto, 1e-6)

# modify the peak and background ranges (users can change these in the UI)
user_set_peak = ["132", "143"]
user_set_back = ["128", "147"]
user_set_tof = [31500.0, 44700.0]
window_main.big_table_data[0, 0].peak = user_set_peak
window_main.big_table_data[0, 0].back = user_set_back
window_main.big_table_data[0, 0].tof_range_auto = user_set_tof
# reload the same run
load_run_from_reduction_table(window_main, row=0, col=1, run=run1)
qtbot.wait(wait)
# check that the auto-peak finder did not change the peak and background ranges
assert window_main.big_table_data[0, 0].peak == user_set_peak
assert window_main.big_table_data[0, 0].back == user_set_back
assert window_main.big_table_data[0, 0].tof_range_auto == pytest.approx(user_set_tof, 1e-6)

# load a different run in the cell
load_run_from_reduction_table(window_main, row=0, col=1, run=run2)
qtbot.wait(wait)
# check that the peak and background ranges were updated
assert window_main.big_table_data[0, 0].peak == expected_peak_run2
assert window_main.big_table_data[0, 0].back == expected_back_run2
assert window_main.big_table_data[0, 0].peak == user_set_peak
assert window_main.big_table_data[0, 0].back == user_set_back
assert window_main.big_table_data[0, 0].tof_range_auto == pytest.approx(user_set_tof, 1e-6)


if __name__ == '__main__':
Expand Down

1 comment on commit f34c7cd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitLab pipeline for refred-dev has been submitted for this commit: "https://code.ornl.gov/sns-hfir-scse/deployments/conda-legacy-deploy/-/pipelines/492632"

Please sign in to comment.