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
def test_load_seg(self, data_test_dir): seg = LoadROI.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 = LoadROI.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_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_info.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 = LoadROI.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].roi_extraction_parameters["parameters"]) == str(cmp_dict)
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( file_path=image_data.image.file_path, image=image_data.image, mask=None, roi_info=res.roi_info, selected_components=list(range(1, num)), roi_extraction_parameters=data_dict, ) SaveROI.save(os.path.join(tmpdir, "segmentation2.seg"), to_save, {"relative_path": False}) seg2 = LoadROI.load([os.path.join(tmpdir, "segmentation2.seg")]) assert seg2 is not None
def napari_get_reader(path: str): for extension in LoadROI.get_extensions(): if path.endswith(extension): return functools.partial(partseg_loader, LoadROI)
def load_segmentation(self): dial = PLoadDialog( { LoadROI.get_name(): LoadROI, LoadROIParameters.get_name(): LoadROIParameters, LoadROIFromTIFF.get_name(): LoadROIFromTIFF, }, settings=self.settings, path="io.open_segmentation_directory", ) if not dial.exec_(): return load_property = dial.get_result() 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 not dial.exec_(): return result = dial.get_result() if result is None: QMessageBox.critical(self, "Data Load fail", "Fail of loading data") return if result.roi is not None: try: self.settings.set_project_info(dial.get_result()) return except ValueError as e: if e.args != (ROI_NOT_FIT, ): 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.roi_extraction_parameters) self.segmentation_dialog.show()