Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi channel images #40

Open
tischi opened this issue Feb 1, 2022 · 6 comments
Open

multi channel images #40

tischi opened this issue Feb 1, 2022 · 6 comments
Labels
question Further information is requested

Comments

@tischi
Copy link

tischi commented Feb 1, 2022

@martinschorb does the python library support multi channel images as an input?
if so, what names does it give the individual channels?
(cc @K-Meech)

@tischi tischi added the question Further information is requested label Feb 1, 2022
@constantinpape
Copy link
Contributor

No, it currently doesn't support multi channel images. This would need to be handled by a user in preprocessing.

@martinschorb
Copy link
Contributor

here's my hacks to enable the functionality to extract single channels from multi-channel stacks while importing them in MoBIE:

constantinpape/cluster_tools#42

constantinpape/elf#81

@martinschorb
Copy link
Contributor

This works now (at least with OME-Zarr), correct?
Would it be relevant to adjust LUT in the corresponding views as well when adding such a source, or do we want to leave that up to the user?

@JonasHell
Copy link
Contributor

JonasHell commented Jan 30, 2024

Not sure if this is still under discussion but I had the same issue and tried to solve it like this:

import argparse
import os
import mobie
import xml.etree.ElementTree as ET


def get_number_of_channels_from_ome_metadata(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    # all the tags have this weird prefix
    channel_tag = "{http://www.openmicroscopy.org/Schemas/OME/2016-06}Channel"
    channels = list(root.iter(channel_tag))
    # potential alternative:
    # root[1][2].findall("{http://www.openmicroscopy.org/Schemas/OME/2016-06}Channel")
    # other alternative:
    # open data and read number of channels from shape
    return len(channels)


def add_multichannel_zarr_image(
    zarr_file,
    zarr_key,
    mobie_project_directory,
    dataset_name,
    is_default_dataset=False,
):
    file_format = "ome.zarr"
    image_name = zarr_file.split('/')[-1].split('.')[0]
    xml_file = os.path.join(zarr_file, "OME/METADATA.ome.xml")
    num_channels = get_number_of_channels_from_ome_metadata(xml_file)
    sources = [f"{image_name}_ch{channel}" for channel in range(num_channels)]
    # add image volume once (by this added as a source, but never used as one)
    mobie.add_image(
            input_path=zarr_file,
            input_key=zarr_key,
            root=mobie_project_directory,
            dataset_name=dataset_name,
            image_name=image_name,
            file_format=file_format,
            view={},  # manually add view at the end
            is_default_dataset=is_default_dataset,
            move_only=True,
            resolution=None,  # not needed since we just move data
            chunks=None,  # not needed since we just move data
            scale_factors=None,  # not needed since we just move data
        )
    # add each channel as a seperate source
    for channel, channel_name in enumerate(sources):
        dataset_folder = os.path.join(mobie_project_directory, dataset_name)
        _, image_metadata_path = mobie.utils.get_internal_paths(
            dataset_folder,
            file_format,
            image_name
        )
        mobie.metadata.add_source_to_dataset(
            dataset_folder=dataset_folder,
            source_type="image",
            source_name=channel_name,
            image_metadata_path=image_metadata_path,
            view={},
            channel=channel
        )
    display_settings = [
        {"contrastLimits": [0, 65535], "color": "white"},
        {"contrastLimits": [50, 65535], "color": "green"},
        {"contrastLimits": [10, 65535], "color": "blue"}
    ]
    # add one view to visualize all channels at once
    mobie.view_utils.create_view(
        dataset_folder=dataset_folder,
        view_name=image_name,
        sources=[[source] for source in sources],
        display_settings=display_settings,
        menu_name="volumes",
        overwrite=True
    )


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--input_file",
        "-f",
        type=str,
        help="Path to the input file. Must be a zarr file converted by"
        "bioformats2raw.",
    )
    parser.add_argument(
        "--input_key",
        "-k",
        default="0",
        type=str,
        help="Key of the data inside the input file.",
    )
    parser.add_argument(
        "--mobie_project_folder",
        "-p",
        default="./data",  # or os.getcwd()? or even hardcode?
        type=str,
        help="Path to the MoBIE project folder.",
    )
    parser.add_argument(
        "--dataset_name",
        "-d",
        default="all_volumes",
        type=str,
        help="Name of the dataset to add the data to."
        "If it does not exist, it will be created.",
    )
    parser.add_argument(
        "--is_default_dataset",
        "-i",
        default=False,
        type=bool,
        help="Whether this dataset should be the default one.",
    )
    return parser.parse_args()


def main():
    args = get_args()
    add_multichannel_zarr_image(
        args.input_file,
        args.input_key,
        args.mobie_project_folder,
        args.dataset_name,
        args.is_default_dataset,
    )


if __name__ == "__main__":
    main()

Sry for copy pasting, the code is not on GitHub yet...

@constantinpape
Copy link
Contributor

I will follow up in mobie/mobie-viewer-fiji#1093 to see if this works it all. Once we have figured that out we can see about the color maps (best to do that in a separate issue then.)

@martinschorb
Copy link
Contributor

WIP to enable adding a single channel to MoBIE
#130

We might be able to extend that to general multi-channel application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants