Esempio n. 1
0
 def test_save_segmentation(self, tmpdir, data_test_dir):
     seg = LoadROIImage.load(
         [os.path.join(data_test_dir, "test_nucleus_1_1.seg")], metadata={"default_spacing": (1, 1, 1)}
     )
     SaveROI.save(os.path.join(tmpdir, "segmentation.seg"), seg, {"relative_path": False})
     assert os.path.exists(os.path.join(tmpdir, "segmentation.seg"))
     os.makedirs(os.path.join(tmpdir, "seg_save"))
     save_components(
         seg.image,
         seg.selected_components,
         os.path.join(tmpdir, "seg_save"),
         seg.roi_info,
         SaveComponents.get_default_values(),
     )
     assert os.path.isdir(os.path.join(tmpdir, "seg_save"))
     assert len(glob(os.path.join(tmpdir, "seg_save", "*"))) == 4
     seg2 = LoadROI.load([os.path.join(tmpdir, "segmentation.seg")])
     assert seg2 is not None
     save_components(
         seg.image,
         [],
         os.path.join(tmpdir, "seg_save2"),
         seg.roi_info,
         SaveComponents.get_default_values(),
     )
     assert os.path.isdir(os.path.join(tmpdir, "seg_save2"))
     assert len(glob(os.path.join(tmpdir, "seg_save2", "*"))) == 8
Esempio n. 2
0
def napari_write_labels(path: str, data: Any, meta: dict) -> Optional[str]:
    if not isinstance(data, numpy.ndarray):
        return
    ext = os.path.splitext(path)[1]
    if ext in SaveROI.get_extensions():
        project = MaskProjectTuple(file_path="", image=None, roi=data)
        SaveROI.save(path, project, parameters={})
        return path
Esempio n. 3
0
 def test_save_segmentation_without_image(self, tmpdir, data_test_dir):
     seg = LoadROIImage.load(
         [os.path.join(data_test_dir, "test_nucleus_1_1.seg")], metadata={"default_spacing": (1, 1, 1)}
     )
     seg_clean = dataclasses.replace(seg, image=None, roi=reduce_array(seg.roi))
     SaveROI.save(os.path.join(tmpdir, "segmentation.seg"), seg_clean, {"relative_path": False})
     SaveROI.save(
         os.path.join(tmpdir, "segmentation1.seg"),
         seg_clean,
         {"relative_path": False, "spacing": (210 * 10 ** -6, 70 * 10 ** -6, 70 * 10 ** -6)},
     )
Esempio n. 4
0
 def test_save_project_with_history(self, tmp_path, stack_segmentation1, mask_property):
     SaveROI.save(tmp_path / "test1.seg", stack_segmentation1, {"relative_path": False})
     seg2 = dataclasses.replace(
         stack_segmentation1,
         history=[create_history_element_from_segmentation_tuple(stack_segmentation1, mask_property)],
         selected_components=[1],
         mask=stack_segmentation1.roi,
     )
     SaveROI.save(tmp_path / "test1.seg", seg2, {"relative_path": False})
     with tarfile.open(tmp_path / "test1.seg", "r") as tf:
         tf.getmember("mask.tif")
         tf.getmember("segmentation.tif")
         tf.getmember("history/history.json")
         tf.getmember("history/arrays_0.npz")
Esempio n. 5
0
 def run_calculation(self):
     while not self.queue.empty():
         task: BatchTask = self.queue.get()
         if isinstance(task.data, str):
             file_path = task.data
             if path.splitext(task.data)[1] == ".seg":
                 project_tuple = LoadROIImage.load([task.data])
             else:
                 project_tuple = LoadStackImage.load([task.data])
         elif isinstance(task.data, MaskProjectTuple):
             project_tuple: MaskProjectTuple = task.data
             file_path = project_tuple.image.file_path
         else:
             continue
         try:
             name = path.basename(file_path)
             blank = get_mask(project_tuple.roi, project_tuple.mask, project_tuple.selected_components)
             algorithm: StackAlgorithm = mask_algorithm_dict[task.parameters.algorithm]()
             algorithm.set_image(project_tuple.image)
             algorithm.set_mask(blank)
             algorithm.set_parameters(**task.parameters.values)
             if isinstance(task.save_prefix, tuple):
                 self.range_signal.emit(0, algorithm.get_steps_num() + 1)
             else:
                 self.range_signal.emit(0, algorithm.get_steps_num())
             # noinspection PyTypeChecker
             segmentation = algorithm.calculation_run(partial(self.progress_info, name))
             state2 = StackSettings.transform_state(
                 project_tuple, segmentation.roi_info, defaultdict(lambda: segmentation.parameters), []
             )
             if isinstance(task.save_prefix, tuple):
                 self.progress_info(name, "saving", algorithm.get_steps_num())
                 name = path.splitext(path.basename(file_path))[0] + ".seg"
                 re_end = re.compile(r"(.*_version)(\d+)\.seg$")
                 while path.exists(path.join(task.save_prefix[0], name)):
                     match = re_end.match(name)
                     if match:
                         num = int(match.group(2)) + 1
                         name = match.group(1) + str(num) + ".seg"
                     else:
                         name = path.splitext(path.basename(file_path))[0] + "_version1.seg"
                 SaveROI.save(path.join(task.save_prefix[0], name), state2, parameters=task.save_prefix[1])
             else:
                 self.multiple_result.emit(state2)
         except Exception as e:  # pylint: disable=W0703
             self.error_signal.emit(f"Exception occurred during proceed {file_path}. Exception info {e}")
         self.index += 1
     self.index = 0
     self.execution_done.emit()
Esempio n. 6
0
def napari_write_labels(path: str, data: Any, meta: dict) -> Optional[str]:
    if not isinstance(data, numpy.ndarray):
        return
    ext = os.path.splitext(path)[1]
    if ext in SaveROI.get_extensions():
        project = MaskProjectTuple(file_path="",
                                   image=None,
                                   roi_info=ROIInfo(data))
        SaveROI.save(path,
                     project,
                     parameters={
                         "spacing":
                         numpy.divide(meta["scale"], DEFAULT_SCALE_FACTOR)[-3:]
                     })
        return path
Esempio n. 7
0
    def test_loading_new_segmentation(self, tmpdir, data_test_dir):
        image_data = LoadStackImage.load([os.path.join(data_test_dir, "test_nucleus.tif")])
        algorithm = ThresholdAlgorithm()
        algorithm.set_image(image_data.image)
        param = algorithm.get_default_values()
        param["channel"] = 0
        algorithm.set_parameters(**param)
        res = algorithm.calculation_run(lambda x, y: None)
        num = np.max(res.roi) + 1
        data_dict = {str(i): deepcopy(res.parameters) for i in range(1, num)}

        to_save = MaskProjectTuple(
            image_data.image.file_path, image_data.image, None, res.roi, list(range(1, num)), data_dict
        )

        SaveROI.save(os.path.join(tmpdir, "segmentation2.seg"), to_save, {"relative_path": False})
        seg2 = LoadSegmentation.load([os.path.join(tmpdir, "segmentation2.seg")])
        assert seg2 is not None
Esempio n. 8
0
 def test_load_project_with_history(self, tmp_path, stack_segmentation1, mask_property):
     image_location = tmp_path / "test1.tif"
     SaveAsTiff.save(image_location, stack_segmentation1)
     seg2 = dataclasses.replace(
         stack_segmentation1,
         history=[create_history_element_from_segmentation_tuple(stack_segmentation1, mask_property)],
         selected_components=[1],
         mask=stack_segmentation1.roi,
         image=stack_segmentation1.image.substitute(file_path=image_location),
         file_path=image_location,
     )
     SaveROI.save(tmp_path / "test1.seg", seg2, {"relative_path": False})
     res = LoadSegmentation.load([tmp_path / "test1.seg"])
     assert res.image == str(image_location)
     assert res.mask is not None
     assert len(res.history) == 1
     assert res.history[0].mask_property == mask_property
     cmp_dict = {str(k): v for k, v in stack_segmentation1.roi_extraction_parameters.items()}
     assert str(res.history[0].segmentation_parameters["parameters"]) == str(cmp_dict)
Esempio n. 9
0
 def perform_roi_info_test(self, project, save_path, save_method: Type[SaveBase], load_method: Type[LoadBase]):
     alt1 = np.copy(project.roi_info.roi)
     alt1[alt1 > 0] += 3
     roi_info = ROIInfo(
         roi=project.roi_info.roi, annotations={i: f"a{i}" for i in range(1, 5)}, alternative={"test": alt1}
     )
     proj = dataclasses.replace(project, roi_info=roi_info)
     save_method.save(save_path / "data.tgz", proj, SaveROI.get_default_values())
     proj2 = load_method.load([save_path / "data.tgz"])
     assert np.all(proj2.roi_info.roi == project.roi_info.roi)
     assert set(proj2.roi_info.annotations) == {1, 2, 3, 4}
     assert proj2.roi_info.annotations == {i: f"a{i}" for i in range(1, 5)}
     assert "test" in proj2.roi_info.alternative
     assert np.all(proj2.roi_info.alternative["test"] == alt1)
Esempio n. 10
0
    def execute_all_action(self):
        dial = SaveDialog(
            {SaveROI.get_name(): SaveROI},
            history=self.settings.get_path_history(),
            system_widget=False,
        )
        dial.setFileMode(QFileDialog.Directory)
        dial.setDirectory(self.settings.get("io.save_batch", self.settings.get("io.save_segmentation_directory", "")))
        if not dial.exec_():
            return
        folder_path = str(dial.selectedFiles()[0])
        self.settings.set("io.save_batch", folder_path)

        widget = self.algorithm_choose_widget.current_widget()

        save_parameters = dial.values
        segmentation_profile = widget.get_segmentation_profile()
        for file_path in self.file_list:
            task = BatchTask(file_path, segmentation_profile, (folder_path, save_parameters))
            self.batch_process.add_task(task)
        self.progress_bar2.setRange(0, self.progress_bar2.maximum() + len(self.file_list))
        self._execute_in_background_init()
Esempio n. 11
0
 def perform_roi_info_history_test(
     self, project, save_path, mask_property, save_method: Type[SaveBase], load_method: Type[LoadBase]
 ):
     alt1 = np.copy(project.roi_info.roi)
     alt1[alt1 > 0] += 3
     roi_info = ROIInfo(
         roi=project.roi_info.roi, annotations={i: f"a{i}" for i in range(1, 5)}, alternative={"test": alt1}
     )
     history = []
     for i in range(3):
         alt2 = np.copy(alt1)
         alt2[alt2 > 0] = i + 5
         roi_info2 = ROIInfo(
             roi=project.roi_info.roi,
             annotations={i: f"a{i}_{j}" for j in range(1, 5)},
             alternative={f"test{i}": alt2},
         )
         history.append(
             HistoryElement.create(
                 roi_info2, alt1, {"algorithm_name": f"task_{i}", "values": {"a": 1}}, mask_property
             )
         )
     proj = dataclasses.replace(project, roi_info=roi_info, history=history)
     save_method.save(save_path / "data.tgz", proj, SaveROI.get_default_values())
     proj2: ProjectInfoBase = load_method.load([save_path / "data.tgz"])
     assert np.all(proj2.roi_info.roi == project.roi_info.roi)
     assert set(proj2.roi_info.annotations) == {1, 2, 3, 4}
     assert proj2.roi_info.annotations == {i: f"a{i}" for i in range(1, 5)}
     assert "test" in proj2.roi_info.alternative
     assert np.all(proj2.roi_info.alternative["test"] == alt1)
     assert len(proj2.history) == 3
     for i in range(3):
         roi_info3, mask2 = proj2.history[i].get_roi_info_and_mask()
         assert np.all(mask2 == alt1)
         assert set(roi_info3.alternative) == {f"test{i}"}
         assert np.all(roi_info3.alternative[f"test{i}"][alt1 > 0] == i + 5)
         assert np.all(roi_info3.alternative[f"test{i}"][alt1 == 0] == 0)
         assert roi_info3.annotations == {i: f"a{i}_{j}" for j in range(1, 5)}
         assert proj2.history[i].roi_extraction_parameters == {"algorithm_name": f"task_{i}", "values": {"a": 1}}