Skip to content

Commit aa35aba

Browse files
Fix add or replace stack in dataset (#2317)
2 parents 31f6373 + ccfc1d0 commit aa35aba

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#2315: Fix adding or replacing a stack within a dataset

mantidimaging/gui/test/gui_system_loading_test.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def setUp(self) -> None:
2828

2929
def tearDown(self) -> None:
3030
self._close_image_stacks()
31+
self._check_datasets_consistent()
3132
super().tearDown()
3233
self.assertFalse(self.main_window.isVisible())
3334

@@ -43,6 +44,7 @@ def test_func() -> bool:
4344
return (current_stacks - initial_stacks) >= 1
4445

4546
wait_until(test_func, max_retry=600)
47+
self._check_datasets_consistent()
4648

4749
@classmethod
4850
def _click_stack_selector(cls):
@@ -89,6 +91,7 @@ def test_load_180(self, mocked_select_file):
8991
self.assertEqual(len(stacks_after), 5)
9092
self.assertIn(stacks[0], stacks_after)
9193
self.assertTrue(stacks[0].presenter.images.has_proj180deg())
94+
self._check_datasets_consistent()
9295

9396
def _get_log_angle(self, log_path):
9497
with open(log_path) as log_file:
@@ -224,3 +227,74 @@ def test_func() -> bool:
224227
self.assertEqual(image_shape, [128, 128])
225228
self.assertEqual(image_count, expected_count)
226229
self.assertEqual(len(sample.real_projection_angles().value), expected_count)
230+
231+
@mock.patch("mantidimaging.gui.windows.main.MainWindowView._get_file_name")
232+
def test_replace_image_stack(self, mocked_select_file):
233+
new_stack = Path(LOAD_SAMPLE).parents[1] / "Flat_Before/" / "IMAT_Flower_Flat_Before_000000.tif"
234+
mocked_select_file.return_value = new_stack
235+
self.assertEqual(len(self.main_window.presenter.get_active_stack_visualisers()), 0)
236+
self._load_data_set()
237+
self.assertEqual(len(self.main_window.presenter.get_active_stack_visualisers()), 5)
238+
self.assertEqual(100, list(self.main_window.presenter.datasets)[0].sample.data.shape[0])
239+
240+
self.main_window.dataset_tree_widget.topLevelItem(0).setSelected(True)
241+
self._check_datasets_consistent()
242+
243+
self.main_window._add_images_to_existing_dataset()
244+
QTest.qWait(SHORT_DELAY)
245+
246+
with mock.patch(
247+
"mantidimaging.gui.windows.add_images_to_dataset_dialog.view.QFileDialog.getOpenFileName") as gofn:
248+
gofn.return_value = (str(new_stack), None)
249+
self.main_window.add_to_dataset_dialog.chooseFileButton.click()
250+
251+
self.main_window.add_to_dataset_dialog.accepted.emit()
252+
QTest.qWait(SHORT_DELAY)
253+
self._check_datasets_consistent()
254+
self.assertEqual(20, list(self.main_window.presenter.datasets)[0].sample.data.shape[0])
255+
256+
def _check_datasets_consistent(self, show_datasets=False) -> None:
257+
print("_check_datasets_consistent")
258+
if show_datasets:
259+
print("Main window datasets")
260+
for k, v in self.main_window.presenter.model.datasets.items():
261+
print(" dataset:", k)
262+
for image_stack in v.all:
263+
print(" ", image_stack.id, image_stack.name)
264+
print("Main window visualisers/tabs")
265+
for vis in self.main_window.presenter.get_active_stack_visualisers():
266+
print(" ", vis.id, vis.name)
267+
print("Main window treeview")
268+
for i in range(self.main_window.dataset_tree_widget.topLevelItemCount()):
269+
tree_ds = self.main_window.dataset_tree_widget.topLevelItem(i)
270+
print(f" dataset: {tree_ds.id} {tree_ds.text(0)}")
271+
for j in range(tree_ds.childCount()):
272+
tree_is = tree_ds.child(j)
273+
print(f" {tree_is.id} {tree_is.text(0)}")
274+
275+
# Datasets
276+
open_dataset_ids = list(self.main_window.presenter.model.datasets.keys())
277+
self.assertEqual(len(open_dataset_ids), len(set(open_dataset_ids)))
278+
image_stack_ids = self.main_window.presenter.model.image_ids
279+
self.assertEqual(len(image_stack_ids), len(set(image_stack_ids)))
280+
281+
# Visualisers/Tabs
282+
visualiser_ids = [vis.id for vis in self.main_window.presenter.get_active_stack_visualisers()]
283+
self.assertEqual(len(visualiser_ids), len(set(visualiser_ids)))
284+
self.assertEqual(len(visualiser_ids), len(image_stack_ids))
285+
for visualiser_id in visualiser_ids:
286+
self.assertIn(visualiser_id, image_stack_ids)
287+
288+
#Tree view
289+
tree_datasets = [
290+
self.main_window.dataset_tree_widget.topLevelItem(i)
291+
for i in range(self.main_window.dataset_tree_widget.topLevelItemCount())
292+
]
293+
tree_image_stack_ids = []
294+
self.assertEqual(len(open_dataset_ids), len(tree_datasets))
295+
for tree_dataset in tree_datasets:
296+
self.assertIn(tree_dataset.id, open_dataset_ids)
297+
for i in range(tree_dataset.childCount()):
298+
tree_image_stack_ids.append(tree_dataset.child(i).id)
299+
self.assertEqual(len(tree_image_stack_ids), len(set(tree_image_stack_ids)))
300+
self.assertEqual(len(tree_image_stack_ids), len(image_stack_ids))

mantidimaging/gui/windows/main/model.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ def load(im_param: ImageParameters) -> ImageStack:
6161
return ds
6262

6363
def load_images_into_mixed_dataset(self, file_path: str, progress: Progress) -> MixedDataset:
64-
group = FilenameGroup.from_file(Path(file_path))
65-
group.find_all_files()
66-
images = loader.load_stack_from_group(group, progress)
64+
images = self.load_image_stack(file_path, progress)
6765
sd = MixedDataset(stacks=[images], name=images.name)
6866
self.datasets[sd.id] = sd
6967
return sd
7068

69+
def load_image_stack(self, file_path: str, progress: Progress) -> ImageStack:
70+
group = FilenameGroup.from_file(Path(file_path))
71+
group.find_all_files()
72+
images = loader.load_stack_from_group(group, progress)
73+
return images
74+
7175
def do_images_saving(self, images_id: uuid.UUID, output_dir: str, name_prefix: str, image_format: str,
7276
overwrite: bool, pixel_depth: str, progress: Progress) -> bool:
7377
images = self.get_images_by_uuid(images_id)

mantidimaging/gui/windows/main/presenter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ def _add_images_to_existing_strict_dataset(self, dataset: StrictDataset, new_ima
792792
else:
793793
# the image type already exists in the dataset and needs to be replaced
794794
prev_images_id = getattr(dataset, image_attr).id
795+
if image_attr == "sample" and dataset.proj180deg:
796+
self._delete_stack(dataset.proj180deg.id)
797+
self.remove_item_from_tree_view(dataset.proj180deg.id)
795798
self.replace_child_item_id(dataset.id, prev_images_id, new_images.id)
796799
self._delete_stack(prev_images_id)
797800

0 commit comments

Comments
 (0)