Commit 5009b9db54301cc0d7995f97c24c50fb9c499b7f

Authored by Thiago Franco de Moraes
1 parent e63ef4f4
Exists in master

Setting threshold was not working when mask.index is different from its index in mask_dict

invesalius/data/slice_.py
... ... @@ -379,7 +379,9 @@ class Slice(metaclass=utils.Singleton):
379 379 def __set_current_mask_threshold(self, threshold_range):
380 380 if self.current_mask is None:
381 381 return
382   - index = self.current_mask.index
  382 + proj = Project()
  383 + index = proj.mask_dict.get_key(self.current_mask)
  384 + print(f"\n\n\n{index=}\n\n\n")
383 385 self.num_gradient += 1
384 386 self.current_mask.matrix[:] = 0
385 387 self.current_mask.clear_history()
... ... @@ -434,7 +436,8 @@ class Slice(metaclass=utils.Singleton):
434 436 def __set_current_mask_threshold_actual_slice(self, threshold_range):
435 437 if self.current_mask is None:
436 438 return
437   - index = self.current_mask.index
  439 + proj = Project()
  440 + index = proj.mask_dict.get_key(self.current_mask)
438 441 for orientation in self.buffer_slices:
439 442 self.buffer_slices[orientation].discard_vtk_mask()
440 443 self.SetMaskThreshold(
... ... @@ -1114,10 +1117,11 @@ class Slice(metaclass=utils.Singleton):
1114 1117 If slice_number is None then all the threshold is calculated for all
1115 1118 slices, otherwise only to indicated slice.
1116 1119 """
1117   - self.current_mask.was_edited = False
1118 1120 thresh_min, thresh_max = threshold_range
1119 1121  
1120   - if self.current_mask.index == index:
  1122 + proj = Project()
  1123 + if proj.mask_dict[index] == self.current_mask:
  1124 + self.current_mask.was_edited = False
1121 1125 # TODO: find out a better way to do threshold
1122 1126 if slice_number is None:
1123 1127 for n, slice_ in enumerate(self.matrix):
... ... @@ -1140,15 +1144,10 @@ class Slice(metaclass=utils.Singleton):
1140 1144 # Update data notebook (GUI)
1141 1145 Publisher.sendMessage(
1142 1146 "Set mask threshold in notebook",
1143   - index=self.current_mask.index,
  1147 + index=index,
1144 1148 threshold_range=self.current_mask.threshold_range,
1145 1149 )
1146   - else:
1147   - proj = Project()
1148   - proj.mask_dict[index].threshold_range = threshold_range
1149   -
1150   - proj = Project()
1151   - proj.mask_dict[self.current_mask.index].threshold_range = threshold_range
  1150 + proj.mask_dict[index].threshold_range = threshold_range
1152 1151  
1153 1152 def ShowMask(self, index, value):
1154 1153 "Show a mask given its index and 'show' value (0: hide, other: show)"
... ...
invesalius/gui/task_slice.py
... ... @@ -572,7 +572,7 @@ class MaskProperties(wx.Panel):
572 572 self.bind_evt_gradient = True
573 573 thresh = (thresh_min, thresh_max)
574 574 if thresh in Project().threshold_modes.values():
575   - preset_name = Project().threshold_modes.get_key(thresh)[0]
  575 + preset_name = Project().threshold_modes.get_key(thresh)
576 576 index = self.threshold_modes_names.index(preset_name)
577 577 self.combo_thresh.SetSelection(index)
578 578 else:
... ... @@ -586,7 +586,7 @@ class MaskProperties(wx.Panel):
586 586 self.gradient.SetMaxValue(thresh_max)
587 587 thresh = (thresh_min, thresh_max)
588 588 if thresh in Project().threshold_modes.values():
589   - preset_name = Project().threshold_modes.get_key(thresh)[0]
  589 + preset_name = Project().threshold_modes.get_key(thresh)
590 590 index = self.threshold_modes_names.index(preset_name)
591 591 self.combo_thresh.SetSelection(index)
592 592 else:
... ... @@ -628,7 +628,7 @@ class MaskProperties(wx.Panel):
628 628 thresh_min, thresh_max = self.threshold_modes[default_thresh]
629 629  
630 630 elif default_thresh in proj.threshold_modes.values():
631   - preset_name = proj.threshold_modes.get_key(default_thresh)[0]
  631 + preset_name = proj.threshold_modes.get_key(default_thresh)
632 632 index = self.threshold_modes_names.index(preset_name)
633 633 self.combo_thresh.SetSelection(index)
634 634 thresh_min, thresh_max = default_thresh
... ...
invesalius/project.py
... ... @@ -38,7 +38,7 @@ import invesalius.version as version
38 38 from invesalius import inv_paths
39 39 from invesalius.data import imagedata_utils
40 40 from invesalius.presets import Presets
41   -from invesalius.utils import Singleton, debug, decode, touch
  41 +from invesalius.utils import Singleton, debug, decode, touch, TwoWaysDictionary
42 42  
43 43 if sys.platform == 'win32':
44 44 try:
... ... @@ -62,7 +62,7 @@ class Project(metaclass=Singleton):
62 62 self.affine = ''
63 63  
64 64 # Masks (vtkImageData)
65   - self.mask_dict = {}
  65 + self.mask_dict = TwoWaysDictionary()
66 66  
67 67 # Surfaces are (vtkPolyData)
68 68 self.surface_dict = {}
... ... @@ -118,7 +118,7 @@ class Project(metaclass=Singleton):
118 118 return index
119 119  
120 120 def RemoveMask(self, index):
121   - new_dict = {}
  121 + new_dict = TwoWaysDictionary()
122 122 new_index = 0
123 123 for i in self.mask_dict:
124 124 if i == index:
... ... @@ -331,7 +331,7 @@ class Project(metaclass=Singleton):
331 331 self.matrix_dtype = project["matrix"]['dtype']
332 332  
333 333 # Opening the masks
334   - self.mask_dict = {}
  334 + self.mask_dict = TwoWaysDictionary()
335 335 for index in project.get("masks", []):
336 336 filename = project["masks"][index]
337 337 filepath = os.path.join(dirpath, filename)
... ...
invesalius/utils.py
... ... @@ -181,6 +181,12 @@ class TwoWaysDictionary(dict):
181 181  
182 182 def get_key(self, value):
183 183 """
  184 + Find the key (first) with the given value
  185 + """
  186 + return self.get_keys(value)[0]
  187 +
  188 + def get_keys(self, value):
  189 + """
184 190 Find the key(s) as a list given a value.
185 191 """
186 192 return [item[0] for item in self.items() if item[1] == value]
... ...