From 5009b9db54301cc0d7995f97c24c50fb9c499b7f Mon Sep 17 00:00:00 2001 From: Thiago Franco de Moraes Date: Tue, 15 Jun 2021 19:15:06 -0300 Subject: [PATCH] Setting threshold was not working when mask.index is different from its index in mask_dict --- invesalius/data/slice_.py | 21 ++++++++++----------- invesalius/gui/task_slice.py | 6 +++--- invesalius/project.py | 8 ++++---- invesalius/utils.py | 6 ++++++ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/invesalius/data/slice_.py b/invesalius/data/slice_.py index 28f2a8b..3e6c943 100644 --- a/invesalius/data/slice_.py +++ b/invesalius/data/slice_.py @@ -379,7 +379,9 @@ class Slice(metaclass=utils.Singleton): def __set_current_mask_threshold(self, threshold_range): if self.current_mask is None: return - index = self.current_mask.index + proj = Project() + index = proj.mask_dict.get_key(self.current_mask) + print(f"\n\n\n{index=}\n\n\n") self.num_gradient += 1 self.current_mask.matrix[:] = 0 self.current_mask.clear_history() @@ -434,7 +436,8 @@ class Slice(metaclass=utils.Singleton): def __set_current_mask_threshold_actual_slice(self, threshold_range): if self.current_mask is None: return - index = self.current_mask.index + proj = Project() + index = proj.mask_dict.get_key(self.current_mask) for orientation in self.buffer_slices: self.buffer_slices[orientation].discard_vtk_mask() self.SetMaskThreshold( @@ -1114,10 +1117,11 @@ class Slice(metaclass=utils.Singleton): If slice_number is None then all the threshold is calculated for all slices, otherwise only to indicated slice. """ - self.current_mask.was_edited = False thresh_min, thresh_max = threshold_range - if self.current_mask.index == index: + proj = Project() + if proj.mask_dict[index] == self.current_mask: + self.current_mask.was_edited = False # TODO: find out a better way to do threshold if slice_number is None: for n, slice_ in enumerate(self.matrix): @@ -1140,15 +1144,10 @@ class Slice(metaclass=utils.Singleton): # Update data notebook (GUI) Publisher.sendMessage( "Set mask threshold in notebook", - index=self.current_mask.index, + index=index, threshold_range=self.current_mask.threshold_range, ) - else: - proj = Project() - proj.mask_dict[index].threshold_range = threshold_range - - proj = Project() - proj.mask_dict[self.current_mask.index].threshold_range = threshold_range + proj.mask_dict[index].threshold_range = threshold_range def ShowMask(self, index, value): "Show a mask given its index and 'show' value (0: hide, other: show)" diff --git a/invesalius/gui/task_slice.py b/invesalius/gui/task_slice.py index a57613a..ce7a8c6 100644 --- a/invesalius/gui/task_slice.py +++ b/invesalius/gui/task_slice.py @@ -572,7 +572,7 @@ class MaskProperties(wx.Panel): self.bind_evt_gradient = True thresh = (thresh_min, thresh_max) if thresh in Project().threshold_modes.values(): - preset_name = Project().threshold_modes.get_key(thresh)[0] + preset_name = Project().threshold_modes.get_key(thresh) index = self.threshold_modes_names.index(preset_name) self.combo_thresh.SetSelection(index) else: @@ -586,7 +586,7 @@ class MaskProperties(wx.Panel): self.gradient.SetMaxValue(thresh_max) thresh = (thresh_min, thresh_max) if thresh in Project().threshold_modes.values(): - preset_name = Project().threshold_modes.get_key(thresh)[0] + preset_name = Project().threshold_modes.get_key(thresh) index = self.threshold_modes_names.index(preset_name) self.combo_thresh.SetSelection(index) else: @@ -628,7 +628,7 @@ class MaskProperties(wx.Panel): thresh_min, thresh_max = self.threshold_modes[default_thresh] elif default_thresh in proj.threshold_modes.values(): - preset_name = proj.threshold_modes.get_key(default_thresh)[0] + preset_name = proj.threshold_modes.get_key(default_thresh) index = self.threshold_modes_names.index(preset_name) self.combo_thresh.SetSelection(index) thresh_min, thresh_max = default_thresh diff --git a/invesalius/project.py b/invesalius/project.py index 92602e1..7dcd508 100644 --- a/invesalius/project.py +++ b/invesalius/project.py @@ -38,7 +38,7 @@ import invesalius.version as version from invesalius import inv_paths from invesalius.data import imagedata_utils from invesalius.presets import Presets -from invesalius.utils import Singleton, debug, decode, touch +from invesalius.utils import Singleton, debug, decode, touch, TwoWaysDictionary if sys.platform == 'win32': try: @@ -62,7 +62,7 @@ class Project(metaclass=Singleton): self.affine = '' # Masks (vtkImageData) - self.mask_dict = {} + self.mask_dict = TwoWaysDictionary() # Surfaces are (vtkPolyData) self.surface_dict = {} @@ -118,7 +118,7 @@ class Project(metaclass=Singleton): return index def RemoveMask(self, index): - new_dict = {} + new_dict = TwoWaysDictionary() new_index = 0 for i in self.mask_dict: if i == index: @@ -331,7 +331,7 @@ class Project(metaclass=Singleton): self.matrix_dtype = project["matrix"]['dtype'] # Opening the masks - self.mask_dict = {} + self.mask_dict = TwoWaysDictionary() for index in project.get("masks", []): filename = project["masks"][index] filepath = os.path.join(dirpath, filename) diff --git a/invesalius/utils.py b/invesalius/utils.py index 99a4cba..663f43a 100644 --- a/invesalius/utils.py +++ b/invesalius/utils.py @@ -181,6 +181,12 @@ class TwoWaysDictionary(dict): def get_key(self, value): """ + Find the key (first) with the given value + """ + return self.get_keys(value)[0] + + def get_keys(self, value): + """ Find the key(s) as a list given a value. """ return [item[0] for item in self.items() if item[1] == value] -- libgit2 0.21.2