def execute(self, args):

        if not args.struct_name == 'skip':
            if not args.workflow_name == 'template':
                print('only use either workflow_name or struct_name, should use both.')
                quit()
            args.workflow_name = args.struct_name
        
        try:
            if args.wrapper_dir == '_internal_':
                module_name = 'aicssegmentation.structure_wrapper.seg_' + args.workflow_name
                seg_module = importlib.import_module(module_name)   
            else:
                func_path = args.wrapper_dir
                spec = importlib.util.spec_from_file_location('seg_'+ args.workflow_name, func_path + '/seg_'+args.workflow_name+'.py')
                seg_module = importlib.util.module_from_spec(spec)
                try:
                    spec.loader.exec_module(seg_module)
                except Exception as e: 
                    print('check errors in wrapper script')
                    print(str(e))
            class_name = 'Workflow_'+ args.workflow_name
            SegModule = getattr(seg_module, class_name)
        except:
            print('{} structure not found'.format(args.workflow_name))
            sys.exit(1)

        output_path = pathlib.Path(args.output_dir)
        
        if not os.path.exists(output_path):
            os.mkdir(output_path)
            
        ##########################################################################
        if args.mode == PER_IMAGE:

            fname = os.path.basename(os.path.splitext(args.input_fname)[0])

            image_reader = aicsimageio.AICSImage(args.input_fname)
            img = image_reader.data
            struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

            SegModule(struct_img, self.rescale_ratio, args.output_type, output_path, fname)

        elif args.mode == PER_DIR:

            filenames = [os.path.basename(os.path.splitext(f)[0])
                         for f in os.listdir(args.input_dir)
                         if f.endswith(args.data_type)]
            filenames.sort()

            for _, fn in enumerate(filenames):

                image_reader = aicsimageio.AICSImage(os.path.join(args.input_dir, f'{fn}{args.data_type}'))
                img = image_reader.data
                struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

                SegModule(struct_img, self.rescale_ratio, args.output_type, output_path, fn)
    def execute(self, args):

        if not args.workflow_name == 'ignore':
            # overwrite struct_name
            args.struct_name = args.workflow_name

        if args.struct_name not in STRUCTURE_MAPPING.keys():
            # this is not a off-the-shelf structure, will interpreted differently
            try:
                module_name = 'aicssegmentation.structure_wrapper.seg_' + args.struct_name
                seg_module = importlib.import_module(module_name)
                class_name = 'Workflow_' + args.struct_name
                SegModule = getattr(seg_module, class_name)
            except:
                print('{} structure not found'.format(args.struct_name))
                sys.exit(1)
        else:
            # Pull module info for this structure
            seg_module_info = STRUCTURE_MAPPING[args.struct_name]
            # Import the module specified for that structure
            seg_module = importlib.import_module(seg_module_info['module'])
            # Pull out the segmentation class from that module
            SegModule = getattr(seg_module, seg_module_info['class'])

        output_path = pathlib.Path(args.output_dir)

        ##########################################################################
        if args.mode == PER_IMAGE:

            fname = os.path.basename(os.path.splitext(args.input_fname)[0])

            image_reader = aicsimageio.AICSImage(args.input_fname)
            img = image_reader.data
            struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

            SegModule(struct_img, self.rescale_ratio, args.output_type,
                      output_path, fname)

        elif args.mode == PER_DIR:

            filenames = [
                os.path.basename(os.path.splitext(f)[0])
                for f in os.listdir(args.input_dir)
                if f.endswith(args.data_type)
            ]
            filenames.sort()

            for _, fn in enumerate(filenames):

                image_reader = aicsimageio.AICSImage(
                    os.path.join(args.input_dir, f'{fn}{args.data_type}'))
                img = image_reader.data
                struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

                SegModule(struct_img, self.rescale_ratio, args.output_type,
                          output_path, fn)
Exemplo n.º 3
0
    def execute(self, args):

        if args.struct_name not in STRUCTURE_MAPPING.keys():
            print('{} structure not found'.format(args.struct_name))
            sys.exit(1)
        # Pull module info for this structure
        seg_module_info = STRUCTURE_MAPPING[args.struct_name]
        # Import the module specified for that structure
        seg_module = importlib.import_module(seg_module_info['module'])
        # Pull out the segmentation class from that module
        SegModule = getattr(seg_module, seg_module_info['class'])

        output_path = pathlib.Path(args.output_dir)

        ##########################################################################
        if args.mode == PER_IMAGE:

            fname = os.path.basename(os.path.splitext(args.input_fname)[0])

            image_reader = aicsimageio.AICSImage(args.input_fname)
            img = image_reader.data
            struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

            SegModule(struct_img, self.rescale_ratio, args.output_type, output_path, fname)

        elif args.mode == PER_DIR:

            filenames = [os.path.basename(os.path.splitext(f)[0])
                         for f in os.listdir(args.input_dir)
                         if f.endswith(args.data_type)]
            filenames.sort()

            for fi, fn in enumerate(filenames):

                image_reader = aicsimageio.AICSImage(os.path.join(args.input_dir, f'{fn}{args.data_type}'))
                img = image_reader.data
                struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

                SegModule(struct_img, self.rescale_ratio, args.output_type, output_path, fn)
def make_one_thumbnail(
    infile: str,
    outfile: str,
    channels: List[int],
    colors: List[Tuple[float, float, float]],
    size: int,
    projection: str = "max",
    axis: int = 2,
    apply_mask: bool = False,
    mask_channel: int = 0,
    label: str = "",
):
    axistranspose = (1, 0, 2, 3)
    if axis == 2:  # Z
        axistranspose = (1, 0, 2, 3)
    elif axis == 0:  # X
        axistranspose = (2, 0, 1, 3)
    elif axis == 1:  # Y
        axistranspose = (3, 0, 2, 1)
    else:
        raise ValueError(f"Unknown axis value: {axis}")

    with aicsimageio.AICSImage(infile) as image:
        imagedata = image.get_image_data("CZYX", T=0)
    generator = ThumbnailGenerator(
        channel_indices=channels,
        size=size,
        mask_channel_index=mask_channel,
        colors=colors,
        projection=projection,
    )
    # take zeroth time, and transpose projection axis and c
    thumbnail = generator.make_thumbnail(imagedata.transpose(axistranspose),
                                         apply_cell_mask=apply_mask)
    if label:
        # Untested on MacOS
        if platform.system() == "Windows":
            font_path = "/Windows/Fonts/consola.ttf"
        else:
            font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
        font = ImageFont.truetype(font_path, 12)
        img = Image.fromarray(thumbnail.transpose((1, 2, 0)))
        draw = ImageDraw.Draw(img)
        draw.text((2, 2), label, (255, 255, 255), font=font)
        thumbnail = np.array(img)
        thumbnail = thumbnail.transpose(2, 0, 1)

    with aicsimageio.writers.PngWriter(file_path=outfile,
                                       overwrite_file=True) as writer:
        writer.save(thumbnail)
    return thumbnail
Exemplo n.º 5
0
def test_aicsimageio_no_networking(resources_dir, filename, expected_shape):
    # This should test and make sure that distributed isn't imported when aicsimageio is
    # Importing distributed on a machine (or container) that doesn't have any
    # networking capabilities results in socket errors, _during the import_
    # See: https://github.com/AllenCellModeling/aicsimageio/issues/82
    if "distributed" in sys.modules:
        del sys.modules["distributed"]

    # Re import
    import aicsimageio  # noqa: F401

    # Some basic operation to ensure that distributed is not imported
    # anywhere down the line
    img = aicsimageio.AICSImage(resources_dir / filename)
    assert img.data.shape == expected_shape

    # Assert not imported
    assert "distributed" not in sys.modules
Exemplo n.º 6
0
def microscopy_czi(viewer, im_path, im_labels_path):
    img = aicsimageio.AICSImage(im_path)
    cells = img.data[0]

    print("image shape: {}".format(cells.shape))

    layer_names = [layer.name for layer in viewer.layers]

    ch_nums = [1, 2, 3, 4, 0]
    ch_names = ["probe488", "probe561", "probe638", "dapi", "brightfield"]
    ch_types = ["fluor", "fluor", "fluor", "fluor", "bf"]
    ch_colors = [
        [1.0, 0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0, 1.0],
        [1.0, 1.0, 0.0, 1.0],
        [0.0, 0.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0],
    ]

    for ch_num, ch_name, ch_type, ch_color in zip(ch_nums, ch_names, ch_types,
                                                  ch_colors):
        if ch_num < cells.shape[0]:
            channel = cells[ch_num, :, :, :]
        else:
            channel = np.zeros([1, 1, 1])

        if ch_name not in layer_names:
            ch = viewer.add_image(channel, name=ch_name)
        else:
            ch = viewer.layers[ch_name]
            ch.image = channel

        ch.colormap = Colormap([(0, 0, 0, 1), ch_color])
        ch.clim = get_default_range(channel, ch_type)
        ch.blending = "additive"

    if os.path.exists(im_labels_path):
        labels = imread(im_labels_path)
    else:
        labels = np.zeros(cells[0, 0, :, :].shape, dtype=np.int)

    return (viewer, layer_names, labels)
Exemplo n.º 7
0
def _run_benchmark(
    resources_dir: Path,
    extensions: List[str],
    non_aicsimageio_reader: List[Callable],
    iterations: int = 3,
):
    # Collect files matching the extensions provided
    files = []
    for ext in extensions:
        files += list(resources_dir.glob(ext))

    # Run reads for each file and store details in results
    results = []
    for file in files:
        info_read = aicsimageio.AICSImage(file)
        yx_planes = np.prod(info_read.size("STCZ"))
        for reader in [aicsimageio.imread, non_aicsimageio_reader]:
            reader_path = f"{reader.__module__}.{reader.__name__}"
            for i in tqdm(range(iterations),
                          desc=f"{reader_path}: {file.name}"):
                start = time.perf_counter()
                reader(str(file))
                results.append({
                    "file_name":
                    file.name,
                    "file_size_gb":
                    file.stat().st_size / 10e8,
                    "reader": ("aicsimageio"
                               if "aicsimageio" in reader_path else "other"),
                    "yx_planes":
                    int(yx_planes),
                    "read_duration":
                    time.perf_counter() - start,
                })

    return results
    def execute(self, args):

        if not args.struct_name == "skip":
            if not args.workflow_name == "template":
                print(
                    "only use either workflow_name or struct_name, should use both."
                )
                quit()
            args.workflow_name = args.struct_name

        try:
            if args.wrapper_dir == "_internal_":
                module_name = ("aicssegmentation.structure_wrapper.seg_" +
                               args.workflow_name)
                seg_module = importlib.import_module(module_name)
            else:
                func_path = args.wrapper_dir
                spec = importlib.util.spec_from_file_location(
                    "seg_" + args.workflow_name,
                    func_path + "/seg_" + args.workflow_name + ".py",
                )
                seg_module = importlib.util.module_from_spec(spec)
                try:
                    spec.loader.exec_module(seg_module)
                except Exception as e:
                    print("check errors in wrapper script")
                    print(str(e))
            class_name = "Workflow_" + args.workflow_name
            SegModule = getattr(seg_module, class_name)
        except Exception as e:
            print(e)
            print("{} structure not found".format(args.workflow_name))
            sys.exit(1)

        output_path = pathlib.Path(args.output_dir)

        if not os.path.exists(output_path):
            os.mkdir(output_path)

        ##########################################################################
        if args.mode == PER_IMAGE:

            fname = os.path.basename(os.path.splitext(args.input_fname)[0])

            image_reader = aicsimageio.AICSImage(args.input_fname)
            img = image_reader.data
            if len(img.shape) == 6:
                struct_img = img[0, 0,
                                 args.struct_ch, :, :, :].astype(np.float32)
            else:
                struct_img = img[0, args.struct_ch, :, :, :].astype(np.float32)

            # if args.mitotic_label == 'y':
            #     mitosis_seg = (args.input_fname).replace("raw", "mito_seg")
            #     mito_seg_reader = aicsimageio.AICSImage(mitosis_seg)
            #     mitosis_seg_img = mito_seg_reader.data

            #     mseg_img = mitosis_seg_img[0,0,:, 0, :, :].astype(np.float32)
            #     struct_img =struct_img * mseg_img

            if args.mitotic_stage is None:
                SegModule(struct_img, self.rescale_ratio, args.output_type,
                          output_path, fname)
            else:
                SegModule(
                    struct_img,
                    args.mitotic_stage,
                    self.rescale_ratio,
                    args.output_type,
                    output_path,
                    fname,
                )

        elif args.mode == PER_DIR:

            filenames = glob(args.input_dir + "/*" + args.data_type)
            # [os.path.basename(os.path.splitext(f)[0])
            #             for f in os.listdir(args.input_dir)
            #             if f.endswith(args.data_type)]
            filenames.sort()

            for _, fn in enumerate(filenames):

                if os.path.exists(
                        str(output_path /
                            (os.path.splitext(os.path.basename(fn))[0] +
                             "_struct_segmentation.tiff"))):
                    print(f"skipping {fn} ....")
                    continue

                image_reader = aicsimageio.AICSImage(fn)
                img = image_reader.data
                # import pdb; pdb.set_trace()

                # fixing the image reading
                if len(img.shape) == 6:
                    # when z and c is not in order
                    if img.shape[-3] < img.shape[-4]:
                        img = np.transpose(img, (0, 1, 3, 2, 4, 5))
                    struct_img = img[0, 0, args.struct_ch, :, :, :].astype(
                        np.float32)
                else:
                    # when z and c is not in order
                    if img.shape[-3] < img.shape[-4]:
                        img = np.transpose(
                            img,
                            (
                                0,
                                2,
                                1,
                                3,
                                4,
                            ),
                        )
                    struct_img = img[0, args.struct_ch, :, :, :].astype(
                        np.float32)

                # Check if the segmenation is mitotic stage specific
                if args.mitotic_stage is None:
                    SegModule(
                        struct_img,
                        self.rescale_ratio,
                        args.output_type,
                        output_path,
                        os.path.splitext(os.path.basename(fn))[0],
                    )
                else:
                    SegModule(
                        struct_img,
                        args.mitotic_stage,
                        self.rescale_ratio,
                        args.output_type,
                        output_path,
                        fn,
                    )