|
34 | 34 | from ess.reflectometry.workflow import with_filenames |
35 | 35 |
|
36 | 36 |
|
| 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 | + |
37 | 50 | class DetectorView(widgets.HBox): |
38 | 51 | is_active_tab = Bool(False).tag(sync=True) |
39 | 52 |
|
@@ -86,13 +99,12 @@ def run_when_active_tab(change): |
86 | 99 | self.observe(run_when_active_tab, 'is_active_tab') |
87 | 100 |
|
88 | 101 | 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: |
91 | 104 | return |
92 | 105 |
|
93 | 106 | 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'] |
96 | 108 |
|
97 | 109 | workflow = amor.AmorWorkflow() |
98 | 110 | workflow[SampleSize[SampleRun]] = sc.scalar(10, unit='mm') |
@@ -223,14 +235,12 @@ def create_node(name, obj, path=''): |
223 | 235 |
|
224 | 236 | def update_nexus_view(self, *_): |
225 | 237 | """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: |
228 | 240 | self.nexus_tree.nodes = [Node("Select a run to view its structure")] |
229 | 241 | return |
230 | 242 |
|
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'] |
234 | 244 | filepath = self.run_to_filepath(run) |
235 | 245 |
|
236 | 246 | # Create and display the tree for this file |
@@ -277,13 +287,11 @@ def on_tree_select(self, event): |
277 | 287 | # Get the path from the custom attribute |
278 | 288 | path = getattr(selected_node, 'nexus_path', selected_node.name) |
279 | 289 |
|
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: |
283 | 292 | return |
284 | 293 |
|
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'] |
287 | 295 | filepath = self.run_to_filepath(run) |
288 | 296 |
|
289 | 297 | with h5py.File(filepath, 'r') as f: |
@@ -497,15 +505,8 @@ def plot_results(_): |
497 | 505 |
|
498 | 506 | def add_row(_): |
499 | 507 | 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: |
509 | 510 | row = pd.DataFrame( |
510 | 511 | [ |
511 | 512 | { |
@@ -913,9 +914,9 @@ def get_row_key(self, row): |
913 | 914 |
|
914 | 915 | def get_selected_rows(self): |
915 | 916 | chunks = [ |
916 | | - table.get_visible_data().iloc[s['r1'] : s['r2'] + 1] |
| 917 | + rows |
917 | 918 | 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 |
919 | 920 | ] |
920 | 921 | # Select everything if nothing is selected |
921 | 922 | if len(chunks) == 0: |
|
0 commit comments