Ejemplo n.º 1
0
def test_pipeline(image, algorithm_parameters, mask_property, tmp_path):
    elem = SegmentationPipelineElement(
        segmentation=SegmentationProfile(
            name="",
            algorithm=algorithm_parameters["algorithm_name"],
            values=algorithm_parameters["values"]),
        mask_property=mask_property,
    )
    algorithm_parameters = deepcopy(algorithm_parameters)
    algorithm_parameters["values"]["channel"] = 1
    pipeline = SegmentationPipeline(
        name="",
        segmentation=SegmentationProfile(
            name="",
            algorithm=algorithm_parameters["algorithm_name"],
            values=algorithm_parameters["values"]),
        mask_history=[elem],
    )
    result = calculate_pipeline(image, None, pipeline, lambda x, y: None)
    assert np.max(result.segmentation) == 2
    pt = ProjectTuple(
        file_path=image.file_path,
        image=image,
        segmentation=result.segmentation,
        mask=result.mask,
        history=result.history,
        algorithm_parameters=algorithm_parameters,
    )
    SaveProject.save(tmp_path / "project.tgz", pt)
    assert os.path.exists(tmp_path / "project.tgz")
    loaded = LoadProject.load([tmp_path / "project.tgz"])
    assert np.all(loaded.segmentation == result.segmentation)
Ejemplo n.º 2
0
 def test_pipeline_saving(self, qtbot, tmp_path, image, algorithm_parameters, mask_property):
     settings = PartSettings(tmp_path)
     settings.image = image
     settings.last_executed_algorithm = algorithm_parameters["algorithm_name"]
     algorithm = analysis_algorithm_dict[algorithm_parameters["algorithm_name"]]()
     algorithm.set_image(settings.image)
     algorithm.set_parameters(**algorithm_parameters["values"])
     result = algorithm.calculation_run(lambda x, y: None)
     settings.set_segmentation_result(result)
     project_info = settings.get_project_info()
     mask = calculate_mask_from_project(mask_property, settings.get_project_info())
     settings.add_history_element(
         create_history_element_from_project(
             project_info,
             mask_property,
         )
     )
     settings.mask = mask
     calculate_mask_from_project(mask_property, settings.get_project_info())
     algorithm_parameters["values"]["channel"] = 1
     algorithm.set_parameters(**algorithm_parameters["values"])
     algorithm.set_mask(settings.mask)
     result2 = algorithm.calculation_run(lambda x, y: None)
     assert np.max(result2.roi) == 2
     settings.set_segmentation_result(result2)
     project_info = settings.get_project_info()
     SaveProject.save(tmp_path / "project.tgz", project_info)
     assert os.path.exists(tmp_path / "project.tgz")
     loaded = LoadProject.load([tmp_path / "project.tgz"])
     assert np.all(loaded.roi == result2.roi)
     assert len(loaded.history) == 1
Ejemplo n.º 3
0
def test_pipeline(image, algorithm_parameters, mask_property, tmp_path,
                  use_mask):
    elem = SegmentationPipelineElement(
        segmentation=ROIExtractionProfile(
            name="",
            algorithm=algorithm_parameters["algorithm_name"],
            values=algorithm_parameters["values"]),
        mask_property=mask_property,
    )
    algorithm_parameters = deepcopy(algorithm_parameters)
    algorithm_parameters["values"]["channel"] = 1
    pipeline = SegmentationPipeline(
        name="",
        segmentation=ROIExtractionProfile(
            name="",
            algorithm=algorithm_parameters["algorithm_name"],
            values=algorithm_parameters["values"]),
        mask_history=[elem],
    )
    mask = np.ones(image.get_channel(0).shape,
                   dtype=np.uint8) if use_mask else None
    result = calculate_pipeline(image, mask, pipeline, lambda x, y: None)
    assert np.max(result.roi) == 2
    pt = ProjectTuple(
        file_path=image.file_path,
        image=image,
        roi=result.roi,
        mask=result.mask,
        history=result.history,
        algorithm_parameters=algorithm_parameters,
    )
    SaveProject.save(tmp_path / "project.tgz", pt)
    assert os.path.exists(tmp_path / "project.tgz")
    loaded = LoadProject.load([tmp_path / "project.tgz"])
    assert np.all(loaded.roi == result.roi)
Ejemplo n.º 4
0
 def test_load_segmentation(self, part_settings, data_test_dir):
     path = os.path.join(data_test_dir, "stack1_component1_1.tgz")
     pi = LoadProject.load([path])
     part_settings.set_project_info(pi)
Ejemplo n.º 5
0
def napari_get_reader(path: str):
    for extension in LoadProject.get_extensions():
        if path.endswith(extension) and os.path.exists(
                LoadProject.get_next_file([path])):
            return functools.partial(partseg_loader, LoadProject)
Ejemplo n.º 6
0
 def test_save_project(self, tmpdir, analysis_project):
     SaveProject.save(os.path.join(tmpdir, "test1.tgz"), analysis_project)
     assert os.path.exists(os.path.join(tmpdir, "test1.tgz"))
     LoadProject.load([os.path.join(tmpdir, "test1.tgz")])
Ejemplo n.º 7
0
 def test_load_project(self, data_test_dir):
     load_data = LoadProject.load(
         [os.path.join(data_test_dir, "stack1_component1_1.tgz")])
     assert np.max(load_data.segmentation) == 2
Ejemplo n.º 8
0
    def do_calculation(self,
                       calculation: FileCalculation) -> CalculationResultList:
        """
        Main function for calculation process

        :param calculation: calculation to do.
        :return:
        """
        self.calculation = calculation
        self.reused_mask = calculation.calculation_plan.get_reused_mask()
        self.mask_dict = {}
        self.measurement = []
        self.results = []
        operation = calculation.calculation_plan.execution_tree.operation
        ext = path.splitext(calculation.file_path)[1]
        metadata = {"default_spacing": calculation.voxel_size}
        if operation == RootType.Image:
            for load_class in load_dict.values():
                if load_class.partial() or load_class.number_of_files() != 1:
                    continue
                if ext in load_class.get_extensions():
                    projects = load_class.load([calculation.file_path],
                                               metadata=metadata)
                    break
            else:
                raise ValueError("File type not supported")
        elif operation == RootType.Project:
            projects = LoadProject.load([calculation.file_path],
                                        metadata=metadata)
        else:  # operation == RootType.Mask_project
            try:
                projects = LoadProject.load([calculation.file_path],
                                            metadata=metadata)
            except (KeyError, WrongFileTypeException):
                # TODO identify exceptions
                projects = LoadMaskSegmentation.load([calculation.file_path],
                                                     metadata=metadata)

        if isinstance(projects, ProjectTuple):
            projects = [projects]
        for project in projects:
            project: ProjectTuple
            self.image = project.image
            if operation == RootType.Mask_project:
                self.mask = project.mask[0]
            if operation == RootType.Project:
                self.mask = project.mask[0]
                self.segmentation = project.segmentation
                self.additional_layers = project.additional_layers
                self.history = project.history
                self.algorithm_parameters = project.algorithm_parameters

            self.iterate_over(calculation.calculation_plan.execution_tree)
            for el in self.measurement:
                el.set_filename(
                    path.relpath(project.image.file_path,
                                 calculation.base_prefix))
            self.results.append(
                ResponseData(
                    path.relpath(project.image.file_path,
                                 calculation.base_prefix), self.measurement))
            self.measurement = []
        return self.results
Ejemplo n.º 9
0
 def test_load_old_project(self, data_test_dir):
     load_data = LoadProject.load([os.path.join(data_test_dir, "stack1_component1.tgz")])
     assert np.max(load_data.roi_info.roi) == 2