Пример #1
0
    def channel_labels(self) -> Series:
        """Return a Series with a string for each channel in the ROIs stack."""
        if self._channel_labels is not None:
            return self._channel_labels
        sample = cast(self.sample)

        # read channel labels specifically for ROI
        channel_labels_file = Path(self.channel_labels_file
                                   or sample.root_dir / self.stacks_dir /
                                   (self.name + "_full.csv"))
        if not channel_labels_file.exists():
            msg = ("`channel_labels` was not given upon initialization "
                   f"and '{channel_labels_file}' could not be found!")
            raise FileNotFoundError(msg)
        # self._channel_labels = pd.read_csv(channel_labels_file, header=None, squeeze=True)
        preview = pd.read_csv(channel_labels_file, header=None, squeeze=True)
        if isinstance(preview, pd.Series):
            order = preview.to_frame(
                name="ChannelName").set_index("ChannelName")
            # read reference
            ref: DataFrame = cast(sample.panel_metadata)
            ref = ref.loc[ref["AcquisitionID"].isin(
                [self.roi_number, str(self.roi_number)])]
            self._channel_labels = (order.join(ref.reset_index().set_index(
                "ChannelName"))["index"].reset_index(
                    drop=True).rename("channel"))
        else:
            preview = preview.dropna().set_index(0).squeeze().rename("channel")
            preview.index = preview.index.astype(int)
            self._channel_labels = preview
        return self._channel_labels
Пример #2
0
Файл: utils.py Проект: bzrry/imc
def read_image_from_file(file: Path, equalize: bool = False) -> Array:
    """
    Read images from a tiff or hdf5 file into a numpy array.
    Channels, if existing will be in first array dimension.
    If `equalize` is :obj:`True`, convert to float type bounded at [0, 1].
    """
    if not file.exists():
        raise FileNotFoundError(f"Could not find file: '{file}")
    # if str(file).endswith("_mask.tiff"):
    # arr = tifffile.imread(file) > 0
    if file.endswith(".ome.tiff"):
        arr = tifffile.imread(str(file), is_ome=True)
    elif file.endswith(".tiff"):
        arr = tifffile.imread(str(file))
    elif file.endswith(".h5"):
        with h5py.File(file, "r") as __f:
            arr = np.asarray(__f[list(__f.keys())[0]])

    if len(arr.shape) == 3:
        if min(arr.shape) == arr.shape[-1]:
            arr = np.moveaxis(arr, -1, 0)
    if equalize:
        arr = eq(arr)
    return arr