Skip to content

The output zarr file store float dtype #495

@Chenchao-is-CC

Description

@Chenchao-is-CC

Hello,

First, thank you to the ome-zarr team — this is a very convenient package that provides a consistent file format for biological image data storage. I really appreciate all the effort your team has put into this project.

Recently, I tried to save a .nii.gz file into OME-Zarr format using the ome-zarr package. However, I noticed that the output Zarr dataset has dtype float, even though the input ndarray is uint16. I also confirmed that the ndarray dtype is uint16 before passing it to the writer. Moreover, if I save the same ndarray directly with zarr, the output dtype remains uint16.

This makes me suspect that the issue comes from the Scaler() function used for generating pyramid levels. Is there any dtype check or casting step applied after pyramid construction and before writing the data to the local Zarr store?

store = parse_url(str(zarr_fn), mode="w", fmt=FormatV04()).store
root = zarr.group(store)

# using FindFileRegExp to find all channel in the directory
channel_list = FindFiles_RegExp(strFn_RegExp=regexp,
                                strDir=target_dir,
                                isRecursive=False)

# using natsort to sort the files list
channels_sorted = natsorted(channel_list)

# create each group as the ech channel
for fn, (ch, gene) in zip(channels_sorted, gene_map_ch.items()) :

    # find the corresponding channel file
    matchObj = re.search(ch, str(fn))
    if matchObj:
        print(f"Congratulation! {gene} match {fn.name}")
    else:
        print("not match the corresponding channel")
        return
    group_name = f"{ch}_{gene}"
    ch_grp = root.create_group(group_name)

    ch_grp.attrs["channel_info"] = {"channel":ch,
                                    "Gene":gene}

    scaler = Scaler(copy_metadata=False,
                                   downscale=2,
                                   in_place=False,
                                   labeled=False,
                                   max_layer=4,
                                   method='gaussian')
    img_array, img_size = load_nii_image(fn)
    x,y,z = img_size
    # check whether the image has converted into uint16
    img_dtype = img_array.dtype
    if img_dtype == np.uint16:
        print("the dtype is already UInt16 ")
    else:
        print("the data is not UInt16, which should be converted into UInt16")
        print("Now converting ")
        img_array = img_array.astype(np.uint16)

    # transpose the axis order to 'zyx'
    img_array = np.transpose(img_array, (2,1,0))
    write_image(image=img_array,
                group=ch_grp,
                scaler=scaler,
                axes="zyx",
                #fmt=FormatV04(),
                storage_options=dict(chunks=(x,y,1)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions