예제 #1
0
def _prepare_layers(image: Image, param: ImageParameters,
                    replace: bool) -> Tuple[ImageInfo, bool]:
    image_layers = []
    for i in range(image.channels):
        lim = list(param.limits[i])
        if lim[1] == lim[0]:
            lim[1] += 1
        blending = "additive" if i != 0 else "translucent"
        data = image.get_channel(i)

        layer = NapariImage(
            data,
            colormap=param.colormaps[i],
            visible=param.visibility[i],
            blending=blending,
            scale=param.scaling,
            contrast_limits=lim,
            gamma=param.gamma[i],
            name=f"channel {i}; {param.layers + i}",
        )
        image_layers.append(layer)
    return ImageInfo(image, image_layers, []), replace
예제 #2
0
    def calculate(
        self,
        image: Image,
        channel_num: int,
        roi: Union[np.ndarray, ROIInfo],
        result_units: Units,
        range_changed: Callable[[int, int], Any] = empty_fun,
        step_changed: Callable[[int], Any] = empty_fun,
        time: int = 0,
    ) -> MeasurementResult:
        """
        Calculate measurements on given set of parameters

        :param image: image on which measurements should be calculated
        :param roi: array with segmentation labeled as positive integers
        :param result_units: units which should be used to present results.
        :param range_changed: callback function to set information about steps range
        :param step_changed: callback function fo set information about steps done
        :param time: which data point should be measured
        :return: measurements
        """
        def get_time(array: np.ndarray):
            if array is not None and array.ndim == 4:
                return array.take(time, axis=image.time_pos)
            return array

        if self._need_mask and image.mask is None:
            raise ValueError("measurement need mask")
        channel = image.get_channel(channel_num).astype(np.float)
        cache_dict = {}
        result_scalar = UNIT_SCALE[result_units.value]
        roi_alternative = {}
        if isinstance(roi, ROIInfo):
            for name, array in roi.alternative.items():
                roi_alternative[name] = get_time(array)
        kw = {
            "image":
            image,
            "channel":
            get_time(channel),
            "segmentation":
            get_time(roi if isinstance(roi, np.ndarray) else roi.roi),
            "mask":
            get_time(image.mask),
            "voxel_size":
            image.spacing,
            "result_scalar":
            result_scalar,
            "roi_alternative":
            roi_alternative,
            "roi_annotation":
            roi.annotations if isinstance(roi, ROIInfo) else {},
        }
        segmentation_mask_map = self.get_segmentation_to_mask_component(
            kw["segmentation"], kw["mask"])
        result = MeasurementResult(segmentation_mask_map)
        for num in self.get_channels_num():
            kw["channel_{num}"] = get_time(image.get_channel(num))
        if any(
                self._need_mask_without_segmentation(el.calculation_tree)
                for el in self.chosen_fields):
            mm = kw["mask"].copy()
            mm[kw["segmentation"] > 0] = 0
            kw["mask_without_segmentation"] = mm

        range_changed(0, len(self.chosen_fields))
        for i, entry in enumerate(self.chosen_fields):
            step_changed(i)
            result[self.name_prefix + entry.name] = self._calc_single_field(
                entry, segmentation_mask_map, cache_dict, kw, result_units)

        return result