Пример #1
0
    def step_save(self, operation: Save):
        """
        Perform save operation selected in plan.

        :param Save operation: save definition
        """
        save_class = save_dict[operation.algorithm]
        project_tuple = ProjectTuple(
            file_path="",
            image=self.image,
            segmentation=self.segmentation,
            additional_layers=self.additional_layers,
            mask=self.mask,
            history=self.history,
            algorithm_parameters=self.algorithm_parameters,
        )
        save_path = get_save_path(operation, self.calculation)
        save_class.save(save_path, project_tuple, operation.values)
Пример #2
0
 def get_project_info(self) -> ProjectTuple:
     algorithm_name = self.last_executed_algorithm
     if algorithm_name:
         algorithm_val = {
             "algorithm_name": algorithm_name,
             "values": deepcopy(self.get(f"algorithms.{algorithm_name}")),
         }
     else:
         algorithm_val = {}
     return ProjectTuple(
         file_path=self.image.file_path,
         image=self.image.substitute(),
         roi=self.roi,
         roi_info=self.roi_info,
         additional_layers=self.additional_layers,
         mask=self.mask,
         history=self.history[:self.history_index + 1],
         algorithm_parameters=algorithm_val,
     )
Пример #3
0
    def step_save(self, operation: Save):
        """
        Perform save operation selected in plan.

        :param Save operation: save definition
        """
        save_class = save_dict[operation.algorithm]
        project_tuple = ProjectTuple(
            file_path="",
            image=self.image,
            roi_info=self.roi_info,
            additional_layers=self.additional_layers,
            mask=self.mask,
            history=self.history,
            algorithm_parameters=self.algorithm_parameters,
        )
        save_path = get_save_path(operation, self.calculation)
        save_dir = os.path.dirname(save_path)
        os.makedirs(save_dir, exist_ok=True)
        save_class.save(save_path, project_tuple, operation.values)
Пример #4
0
 def get_project_info(self) -> ProjectTuple:
     algorithm_name = self.last_executed_algorithm
     if algorithm_name:
         value = self.get(f"algorithms.{algorithm_name}")
         if isinstance(value, EventedDict):
             value = value.as_dict_deep()
         algorithm_val = {
             "algorithm_name": algorithm_name,
             "values": deepcopy(value),
         }
     else:
         algorithm_val = {}
     return ProjectTuple(
         file_path=self.image.file_path,
         image=self.image.substitute(),
         roi_info=self.roi_info,
         additional_layers=self.additional_layers,
         mask=self.mask,
         history=self.history[:self.history_index + 1],
         algorithm_parameters=algorithm_val,
         points=self.points,
     )
Пример #5
0
    def load(
        cls,
        load_locations: typing.List[typing.Union[str, BytesIO, Path]],
        range_changed: typing.Callable[[int, int], typing.Any] = None,
        step_changed: typing.Callable[[int], typing.Any] = None,
        metadata: typing.Optional[dict] = None,
    ):
        """Load project from archive old format"""
        file_ob: typing.Union[str, tarfile.TarFile, TextIOBase, BufferedIOBase,
                              RawIOBase, IOBase] = load_locations[0]
        if isinstance(file_ob, tarfile.TarFile):
            tar_file = file_ob
            file_path = ""
        elif isinstance(file_ob, str):
            tar_file = tarfile.open(file_ob)
            file_path = file_ob
        elif isinstance(file_ob,
                        (TextIOBase, BufferedIOBase, RawIOBase, IOBase)):
            tar_file = tarfile.open(fileobj=file_ob)
            file_path = ""
        else:
            raise ValueError(f"wrong type of file_ argument: {type(file_ob)}")
        image_buffer = BytesIO()
        image_tar = tar_file.extractfile(tar_file.getmember("image.npy"))
        image_buffer.write(image_tar.read())
        image_buffer.seek(0)
        image_arr = np.load(image_buffer)
        try:
            res_buffer = BytesIO()
            res_tar = tar_file.extractfile(tar_file.getmember("res_mask.npy"))
            res_buffer.write(res_tar.read())
            res_buffer.seek(0)
            seg_array = np.load(res_buffer)
        except KeyError:
            seg_array = None
        algorithm_str = tar_file.extractfile("data.json").read()
        algorithm_dict = json.loads(algorithm_str)
        spacing = np.array(
            algorithm_dict["spacing"][::-1]) / UNIT_SCALE[Units.nm.value]
        image = Image(image_arr.reshape((1, ) + image_arr.shape + (1, )),
                      spacing, file_path)
        values = {
            "channel": 0,
            "minimum_size": algorithm_dict["minimum_size"],
            "threshold": {
                "name": "Manual",
                "values": {
                    "threshold": algorithm_dict["threshold"]
                }
            },
            "noise_removal": {
                "name": "Gauss",
                "values": {
                    "gauss_type": DimensionType.Layer,
                    "radius": 1.0
                }
            } if algorithm_dict["use_gauss"] else {
                "name": "None",
                "values": {}
            },
            "side_connection": True,
        }

        algorithm_parameters = {
            "name":
            "Upper threshold" if algorithm_dict["threshold_type"] == "Upper"
            else "Lower threshold",
            "values":
            values,
            "threshold_list":
            algorithm_dict["threshold_list"],
        }

        return ProjectTuple(file_path,
                            image,
                            seg_array,
                            algorithm_parameters=algorithm_parameters)