def write_preprocessed_data(self, dir_output):
        if all(x is None for x in self._stacks):
            raise exceptions.ObjectNotCreated("run")

        # Write all slices
        for i in range(0, self._N_stacks):
            slices = self._stacks[i].write(directory=dir_output,
                                           write_mask=True,
                                           write_slices=False)
示例#2
0
    def get_data(self):

        if type(self._stacks) is not list:
            raise exceptions.ObjectNotCreated("read_data")

        return [st.Stack.from_stack(s) for s in self._stacks]
    def evaluate_slice_similarities(self):
        if self._slice_projections is None:
            raise exceptions.ObjectNotCreated("compute_slice_projections")

        self._slice_similarities = {
            stack.get_filename(): {}
            for stack in self._stacks
        }

        similarity_measures = {
            m: SimilarityMeasures.similarity_measures[m]
            for m in self._measures
        }

        for i_stack, stack in enumerate(self._stacks):
            slices = stack.get_slices()
            N_slices = self._get_original_number_of_slices(stack)
            stack_name = stack.get_filename()
            self._slice_similarities[stack_name] = {
                m: np.ones(N_slices) * self._init_value
                for m in self._measures
            }
            if self._verbose:
                ph.print_info("Stack %d/%d: Compute similarity measures ... " %
                              (i_stack + 1, len(self._stacks)),
                              newline=False)

            for slice in slices:
                i_slice = slice.get_slice_number()
                slice_nda = np.squeeze(sitk.GetArrayFromImage(slice.sitk))
                slice_proj_nda = np.squeeze(
                    sitk.GetArrayFromImage(
                        self._slice_projections[i_stack][i_slice].sitk))

                mask_nda = np.ones_like(slice_nda)

                if self._use_slice_masks:
                    mask_nda *= np.squeeze(
                        sitk.GetArrayFromImage(slice.sitk_mask))
                if self._use_reference_mask:
                    mask_nda *= np.squeeze(
                        sitk.GetArrayFromImage(self._slice_projections[i_stack]
                                               [i_slice].sitk_mask))
                indices = np.where(mask_nda > 0)

                if len(indices[0]) > 0:
                    for m in self._measures:
                        try:
                            self._slice_similarities[stack_name][m][i_slice] = \
                                similarity_measures[m](
                                    slice_nda[indices], slice_proj_nda[indices])
                        except ValueError as e:
                            # Error in case only a few/to less non-zero entries
                            # exist
                            if m == "SSIM":
                                self._slice_similarities[
                                    stack_name][m][i_slice] = \
                                    SimilarityMeasures.UNDEF[m]
                            else:
                                raise ValueError(e.message)
                else:
                    for m in self._measures:
                        self._slice_similarities[
                            stack_name][m][i_slice] = \
                            SimilarityMeasures.UNDEF[m]
            if self._verbose:
                print("done")
示例#4
0
    def get_data(self):

        if type(self._stacks) is not list:
            raise exceptions.ObjectNotCreated("read_data")

        return self._stacks