Skip to content

Commit 7d41c78

Browse files
authored
Merge pull request #149 from scipp/gui-select-reference
Fix selection bug in case of sorting / filtering
2 parents d9ec5d0 + a66c60a commit 7d41c78

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/ess/reflectometry/gui.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@
3434
from ess.reflectometry.workflow import with_filenames
3535

3636

37+
def _get_selected_rows(grid):
38+
return (
39+
pd.concat(
40+
[
41+
grid.get_visible_data().iloc[s['r1'] : s['r2'] + 1]
42+
for s in grid.selections
43+
]
44+
)
45+
if grid.selections
46+
else None
47+
)
48+
49+
3750
class DetectorView(widgets.HBox):
3851
is_active_tab = Bool(False).tag(sync=True)
3952

@@ -70,7 +83,11 @@ def __init__(
7083
)
7184

7285
def run_when_selected_row_changes(change):
73-
if not change['old'] or change['old'][0]['r1'] != change['new'][0]['r1']:
86+
# Runs when there are no previous selections,
87+
# or the new selection is different from the old.
88+
if not change['old'] or (
89+
change['new'] and change['new'][0]['r1'] != change['old'][0]['r1']
90+
):
7491
self.run_workflow()
7592

7693
self.runs_table.observe(run_when_selected_row_changes, names='selections')
@@ -82,13 +99,12 @@ def run_when_active_tab(change):
8299
self.observe(run_when_active_tab, 'is_active_tab')
83100

84101
def run_workflow(self):
85-
selections = self.runs_table.selections
86-
if not self.is_active_tab or not selections:
102+
selected_rows = _get_selected_rows(self.runs_table)
103+
if not self.is_active_tab or selected_rows is None:
87104
return
88105

89106
self.working_label.layout.display = ''
90-
row_idx = selections[0]['r1']
91-
run = self.runs_table.data.iloc[row_idx]['Run']
107+
run = selected_rows.iloc[0]['Run']
92108

93109
workflow = amor.AmorWorkflow()
94110
workflow[SampleSize[SampleRun]] = sc.scalar(10, unit='mm')
@@ -219,14 +235,12 @@ def create_node(name, obj, path=''):
219235

220236
def update_nexus_view(self, *_):
221237
"""Update the Nexus file viewer based on selected run."""
222-
selections = self.runs_table.selections
223-
if not selections:
238+
selected_rows = _get_selected_rows(self.runs_table)
239+
if selected_rows is None:
224240
self.nexus_tree.nodes = [Node("Select a run to view its structure")]
225241
return
226242

227-
# Get the first selected row
228-
row_idx = selections[0]['r1']
229-
run = self.runs_table.data.iloc[row_idx]['Run']
243+
run = selected_rows.iloc[0]['Run']
230244
filepath = self.run_to_filepath(run)
231245

232246
# Create and display the tree for this file
@@ -273,13 +287,11 @@ def on_tree_select(self, event):
273287
# Get the path from the custom attribute
274288
path = getattr(selected_node, 'nexus_path', selected_node.name)
275289

276-
# Get the currently selected run
277-
selections = self.runs_table.selections
278-
if not selections:
290+
selected_rows = _get_selected_rows(self.runs_table)
291+
if selected_rows is None:
279292
return
280293

281-
row_idx = selections[0]['r1']
282-
run = self.runs_table.data.iloc[row_idx]['Run']
294+
run = selected_rows.iloc[0]['Run']
283295
filepath = self.run_to_filepath(run)
284296

285297
with h5py.File(filepath, 'r') as f:
@@ -493,15 +505,8 @@ def plot_results(_):
493505

494506
def add_row(_):
495507
self.log("add row")
496-
# Check if there are any selections in the reduction table
497-
if len(self.reduction_table.selections) > 0:
498-
# Get the first selected row
499-
selection = self.reduction_table.selections[0]
500-
row = self.reduction_table.data.iloc[
501-
selection['r1'] : selection['r2'] + 1
502-
]
503-
else:
504-
# Create an empty row with default values
508+
row = _get_selected_rows(self.reduction_table)
509+
if row is None:
505510
row = pd.DataFrame(
506511
[
507512
{
@@ -661,9 +666,6 @@ def log_text(self, message):
661666
def log_progress(self, progress):
662667
self.progress_log.children = (progress,)
663668

664-
def log_plot(self, plot):
665-
"""Log a plot to the top of the plot log"""
666-
667669

668670
class AmorBatchReductionGUI(ReflectometryBatchReductionGUI):
669671
def __init__(self):
@@ -912,9 +914,9 @@ def get_row_key(self, row):
912914

913915
def get_selected_rows(self):
914916
chunks = [
915-
table.data.iloc[s['r1'] : s['r2'] + 1]
917+
rows
916918
for table in (self.reduction_table, self.custom_reduction_table)
917-
for s in table.selections
919+
if (rows := _get_selected_rows(table)) is not None
918920
]
919921
# Select everything if nothing is selected
920922
if len(chunks) == 0:

0 commit comments

Comments
 (0)