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 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 = LoadSegmentationImage.load([task.data]) else: project_tuple = LoadStackImage.load([task.data]) elif isinstance(task.data, SegmentationTuple): project_tuple: SegmentationTuple = task.data file_path = project_tuple.image.file_path else: continue try: name = path.basename(file_path) blank = get_mask(project_tuple.segmentation, 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.segmentation, 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" SaveSegmentation.save(path.join(task.save_prefix[0], name), state2, parameters=task.save_prefix[1]) else: self.multiple_result.emit(state2) except Exception as e: self.error_signal.emit("Exception occurred during proceed {}. Exception info {}".format(file_path, e)) self.index += 1 self.index = 0 self.execution_done.emit()
def test_save_segmentation(self, tmpdir, data_test_dir): seg = LoadSegmentationImage.load( [os.path.join(data_test_dir, "test_nucleus_1_1.seg")], metadata={"default_spacing": (1, 1, 1)}) SaveSegmentation.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.segmentation, 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 execute_all_action(self): dial = SaveDialog( {SaveSegmentation.get_name(): SaveSegmentation}, 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()
def test_save_segmentation_without_image(self, tmpdir, data_test_dir): seg = LoadSegmentationImage.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, segmentation=reduce_array( seg.segmentation)) SaveSegmentation.save(os.path.join(tmpdir, "segmentation.seg"), seg_clean, {"relative_path": False}) SaveSegmentation.save( os.path.join(tmpdir, "segmentation1.seg"), seg_clean, { "relative_path": False, "spacing": (210 * 10**-6, 70 * 10**-6, 70 * 10**-6) }, )
def test_save_project_with_history(self, tmp_path, stack_segmentation1, mask_property): SaveSegmentation.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.segmentation, ) SaveSegmentation.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")
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.segmentation) + 1 data_dict = {str(i): deepcopy(res.parameters) for i in range(1, num)} to_save = SegmentationTuple(image_data.image.file_path, image_data.image, None, res.segmentation, list(range(1, num)), data_dict) SaveSegmentation.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