Skip to content

Commit 2cb9005

Browse files
committed
fix: helper function to get selected rows
1 parent fab8776 commit 2cb9005

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/ess/reflectometry/gui.py

Lines changed: 26 additions & 25 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

@@ -86,13 +99,12 @@ def run_when_active_tab(change):
8699
self.observe(run_when_active_tab, 'is_active_tab')
87100

88101
def run_workflow(self):
89-
selections = self.runs_table.selections
90-
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:
91104
return
92105

93106
self.working_label.layout.display = ''
94-
row_idx = selections[0]['r1']
95-
run = self.runs_table.get_visible_data().iloc[row_idx]['Run']
107+
run = selected_rows.iloc[0]['Run']
96108

97109
workflow = amor.AmorWorkflow()
98110
workflow[SampleSize[SampleRun]] = sc.scalar(10, unit='mm')
@@ -223,14 +235,12 @@ def create_node(name, obj, path=''):
223235

224236
def update_nexus_view(self, *_):
225237
"""Update the Nexus file viewer based on selected run."""
226-
selections = self.runs_table.selections
227-
if not selections:
238+
selected_rows = _get_selected_rows(self.runs_table)
239+
if selected_rows is None:
228240
self.nexus_tree.nodes = [Node("Select a run to view its structure")]
229241
return
230242

231-
# Get the first selected row
232-
row_idx = selections[0]['r1']
233-
run = self.runs_table.get_visible_data().iloc[row_idx]['Run']
243+
run = selected_rows.iloc[0]['Run']
234244
filepath = self.run_to_filepath(run)
235245

236246
# Create and display the tree for this file
@@ -277,13 +287,11 @@ def on_tree_select(self, event):
277287
# Get the path from the custom attribute
278288
path = getattr(selected_node, 'nexus_path', selected_node.name)
279289

280-
# Get the currently selected run
281-
selections = self.runs_table.selections
282-
if not selections:
290+
selected_rows = _get_selected_rows(self.runs_table)
291+
if selected_rows is None:
283292
return
284293

285-
row_idx = selections[0]['r1']
286-
run = self.runs_table.get_visible_data().iloc[row_idx]['Run']
294+
run = selected_rows.iloc[0]['Run']
287295
filepath = self.run_to_filepath(run)
288296

289297
with h5py.File(filepath, 'r') as f:
@@ -497,15 +505,8 @@ def plot_results(_):
497505

498506
def add_row(_):
499507
self.log("add row")
500-
# Check if there are any selections in the reduction table
501-
if len(self.reduction_table.selections) > 0:
502-
# Get the first selected row
503-
selection = self.reduction_table.selections[0]
504-
row = self.reduction_table.get_visible_data().iloc[
505-
selection['r1'] : selection['r2'] + 1
506-
]
507-
else:
508-
# Create an empty row with default values
508+
row = _get_selected_rows(self.reduction_table)
509+
if row is None:
509510
row = pd.DataFrame(
510511
[
511512
{
@@ -913,9 +914,9 @@ def get_row_key(self, row):
913914

914915
def get_selected_rows(self):
915916
chunks = [
916-
table.get_visible_data().iloc[s['r1'] : s['r2'] + 1]
917+
rows
917918
for table in (self.reduction_table, self.custom_reduction_table)
918-
for s in table.selections
919+
if (rows := _get_selected_rows(table)) is not None
919920
]
920921
# Select everything if nothing is selected
921922
if len(chunks) == 0:

0 commit comments

Comments
 (0)