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.segmentation, image=stack_segmentation1.image.substitute( file_path=image_location), file_path=image_location, ) SaveSegmentation.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.segmentation_parameters.items() } assert str( res.history[0].segmentation_parameters["parameters"]) == str( cmp_dict)
def test_load_seg(self, data_test_dir): seg = LoadSegmentation.load([os.path.join(data_test_dir, "test_nucleus_1_1.seg")]) assert isinstance(seg.image, str) assert seg.selected_components == [1, 3] assert os.path.exists(os.path.join(data_test_dir, seg.image)) assert len(seg.roi_extraction_parameters) == 4 assert os.path.basename(seg.image) == "test_nucleus.tif"
def test_load_old_seg(self, data_test_dir): """ For PartSeg 0.9.4 and older """ seg = LoadSegmentation.load([os.path.join(data_test_dir, "test_nucleus.seg")]) assert isinstance(seg.image, str) assert seg.selected_components == [1, 3] assert os.path.exists(os.path.join(data_test_dir, seg.image)) assert os.path.basename(seg.image) == "test_nucleus.tif"
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, seg.roi, os.path.join(tmpdir, "seg_save")) assert os.path.isdir(os.path.join(tmpdir, "seg_save")) assert len(glob(os.path.join(tmpdir, "seg_save", "*"))) == 4 seg2 = LoadSegmentation.load([os.path.join(tmpdir, "segmentation.seg")]) assert seg2 is not None
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
def napari_get_reader(path: str): for extension in LoadSegmentation.get_extensions(): if path.endswith(extension): return functools.partial(partseg_loader, LoadSegmentation)
def load_segmentation(self): dial = CustomLoadDialog({ LoadSegmentation.get_name(): LoadSegmentation, LoadSegmentationParameters.get_name(): LoadSegmentationParameters, }) dial.setDirectory( self.settings.get("io.open_segmentation_directory", str(Path.home()))) dial.setHistory(dial.history() + self.settings.get_path_history()) if not dial.exec_(): return load_property = dial.get_result() self.settings.set("io.open_segmentation_directory", os.path.dirname(load_property.load_location[0])) self.settings.add_path_history( os.path.dirname(load_property.load_location[0])) def exception_hook(exception): mess = QMessageBox(self) if isinstance( exception, ValueError ) and exception.args[0] == "Segmentation do not fit to image": mess.warning(self, "Open error", "Segmentation do not fit to image") elif isinstance(exception, MemoryError): mess.warning(self, "Open error", "Not enough memory to read this image") elif isinstance(exception, IOError): mess.warning(self, "Open error", "Some problem with reading from disc") elif isinstance(exception, WrongFileTypeException): mess.warning( self, "Open error", "No needed files inside archive. Most probably you choose file from segmentation analysis", ) else: raise exception dial = ExecuteFunctionDialog( load_property.load_class.load, [load_property.load_location], text="Load segmentation", exception_hook=exception_hook, ) if dial.exec(): result = dial.get_result() if result is None: QMessageBox.critical(self, "Data Load fail", "Fail of loading data") return if result.segmentation is not None: try: self.settings.set_project_info(dial.get_result()) return except ValueError as e: if e.args != ("Segmentation do not fit to image", ): raise self.segmentation_dialog.set_additional_text( "Segmentation do not fit to image, maybe you would lie to load parameters only." ) except HistoryProblem: QMessageBox().warning( self, "Load Problem", "You set to save selected components when loading " "another segmentation but history is incomatybile", ) else: self.segmentation_dialog.set_additional_text("") self.segmentation_dialog.set_parameters_dict( result.segmentation_parameters) self.segmentation_dialog.show()