Skip to content

Commit

Permalink
Black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyzubov committed Oct 11, 2024
1 parent 562ebbe commit e7b58ee
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 94 deletions.
2 changes: 1 addition & 1 deletion tif_to_zarr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from importlib.metadata import version as _version

__version__ = _version(__name__)
__version__ = _version(__name__)
157 changes: 81 additions & 76 deletions tif_to_zarr/conversion_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,120 +6,125 @@
import logging
from numcodecs import Zstd


class Conversion:
def __init__(self, input_filepath, output_filepath, axes, translation, scale, units):
def __init__(
self, input_filepath, output_filepath, axes, translation, scale, units
):
self.input_filepath = input_filepath
self.output_filepath = output_filepath
self.zarr_metadata = {"axes" : axes, "translation" : translation, "scale" : scale, "units" : units}
self.zarr_metadata = {
"axes": axes,
"translation": translation,
"scale": scale,
"units": units,
}

#read tiff file and store array, axes and metadata in a dictionary.
# read tiff file and store array, axes and metadata in a dictionary.
def read_tiff(self):
try:
with TiffFile(self.input_filepath) as tiff:
volume_numpy = tiff.asarray()
#volume_dask = da.from_array(tiff.asarray(), chunks=chunks)
# volume_dask = da.from_array(tiff.asarray(), chunks=chunks)
axes = tiff.series[0].axes
imagej_metadata = tiff.imagej_metadata
except IOError as e:
logging.error("Failed to open {0}. Error reason: {1}".format(self.input_filepath, e))
logging.error(
"Failed to open {0}. Error reason: {1}".format(self.input_filepath, e)
)
sys.exit(1)
return [volume_numpy, axes, imagej_metadata]

# store numpy array in a zarr file
def dask_to_zarray(self, tiff_data, chunks):

#create root group
root = zarr.group(store = zarr.NestedDirectoryStore(self.output_filepath), overwrite=True)
# create root group
root = zarr.group(
store=zarr.NestedDirectoryStore(self.output_filepath), overwrite=True
)
# create zarr array
#zarr_data = root.create_dataset('data', shape=tiff_data[0].shape, chunks=chunks, dtype=tiff_data[0].dtype)
# zarr_data = root.create_dataset('data', shape=tiff_data[0].shape, chunks=chunks, dtype=tiff_data[0].dtype)
dask_arr = da.from_array(tiff_data[0], chunks=chunks)
zarr_data = zarr.create(store=zarr.NestedDirectoryStore(self.output_filepath),
path='s0',
shape = dask_arr.shape,
chunks=chunks,
dtype=dask_arr.dtype,
compressor=Zstd(level=6))
zarr_data = zarr.create(
store=zarr.NestedDirectoryStore(self.output_filepath),
path="s0",
shape=dask_arr.shape,
chunks=chunks,
dtype=dask_arr.dtype,
compressor=Zstd(level=6),
)

#store .tiff data in a .zarr file
# store .tiff data in a .zarr file
da.store(dask_arr, zarr_data)

# add metadata to zarr .attrs.
self.populate_zarr_attrs(root, tiff_data[1], self.zarr_metadata, zarr_data.name.lstrip('/'))

# add metadata to zarr .attrs.
self.populate_zarr_attrs(
root, tiff_data[1], self.zarr_metadata, zarr_data.name.lstrip("/")
)

def numpy_to_zarray(self, tiff_data, chunks):
#create root group
root = zarr.group(store = zarr.NestedDirectoryStore(self.output_filepath), overwrite=True)
# create root group
root = zarr.group(
store=zarr.NestedDirectoryStore(self.output_filepath), overwrite=True
)
# create zarr array
zarr_data = zarr.create(store=zarr.NestedDirectoryStore(self.output_filepath),
path='s0',
shape = tiff_data[0].shape,
chunks=chunks,
dtype=tiff_data[0].dtype)
zarr_data = zarr.create(
store=zarr.NestedDirectoryStore(self.output_filepath),
path="s0",
shape=tiff_data[0].shape,
chunks=chunks,
dtype=tiff_data[0].dtype,
)
zarr_data[:] = tiff_data[0]

self.populate_zarr_attrs(
root, tiff_data[1], self.zarr_metadata, zarr_data.name.lstrip("/")
)

self.populate_zarr_attrs(root, tiff_data[1], self.zarr_metadata, zarr_data.name.lstrip('/'))


# add selected tiff metadata to zarr .attrs
# add selected tiff metadata to zarr .attrs
def populate_zarr_attrs(self, root, axes, zarr_metadata, data_address):
tiff_axes=[*axes]
tiff_axes = [*axes]

# json template for a multiscale structure
multscale_dict = {
"multiscales": [
{
"axes": [

],
"coordinateTransformations": [
"multiscales": [
{
"scale": [
1.0,
1.0,
1.0
"axes": [],
"coordinateTransformations": [
{"scale": [1.0, 1.0, 1.0], "type": "scale"}
],
"type": "scale"
}
],
"datasets": [
{
"coordinateTransformations": [
{
"scale": [

],
"type": "scale"
},
"datasets": [
{
"translation": [

"coordinateTransformations": [
{"scale": [], "type": "scale"},
{"translation": [], "type": "translation"},
],
"type": "translation"
"path": "unknown",
}
],
"path": "unknown"
}
],
"name": "unknown",
"version": "0.4"
}
]
],
"name": "unknown",
"version": "0.4",
}
]
}

# write metadata info into a multiscale scheme
for axis, scale, offset, unit in zip(zarr_metadata["axes"], zarr_metadata["scale"], zarr_metadata["translation"], zarr_metadata["units"]):
multscale_dict["multiscales"][0]["axes"].append( {
"name": axis,
"type": "space",
"unit" : unit
})
multscale_dict["multiscales"][0]["datasets"][0]["coordinateTransformations"][0]["scale"].append(scale)
multscale_dict["multiscales"][0]["datasets"][0]["coordinateTransformations"][1]["translation"].append(offset)
multscale_dict["multiscales"][0]["datasets"][0]["path"] = data_address ##
# write metadata info into a multiscale scheme
for axis, scale, offset, unit in zip(
zarr_metadata["axes"],
zarr_metadata["scale"],
zarr_metadata["translation"],
zarr_metadata["units"],
):
multscale_dict["multiscales"][0]["axes"].append(
{"name": axis, "type": "space", "unit": unit}
)
multscale_dict["multiscales"][0]["datasets"][0][
"coordinateTransformations"
][0]["scale"].append(scale)
multscale_dict["multiscales"][0]["datasets"][0][
"coordinateTransformations"
][1]["translation"].append(offset)
multscale_dict["multiscales"][0]["datasets"][0]["path"] = data_address ##

# add multiscale template to .attrs
# add multiscale template to .attrs
root.attrs["multiscales"] = multscale_dict["multiscales"]


65 changes: 48 additions & 17 deletions tif_to_zarr/tif_to_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,58 @@
import click
import numpy as np


@click.command()
@click.option('--src', '-s', type=click.Path(exists = True), help='Input .tiff file location.')
@click.option("--dest", '-d', type=click.Path(), help = 'Output .tiff file location.')
@click.option('--axes', '-a',nargs=3, default =("z", "y", "x"), type=str, help = "Metadata axis names. Order matters. \n Example: -a z y x")
@click.option('--translation', '-t', nargs=3, default =(0.0, 0.0, 0.0), type=float, help = "Metadata translation(offset) value. Order matters. \n Example: -t 1.0 2.0 3.0")
@click.option('--scale', '-s', nargs=3, default = (1.0, 1.0, 1.0), type=float, help = "Metadata scale value. Order matters. \n Example: -s 1.0 2.0 3.0")
@click.option('--units', '-u',nargs=3, default =("nm", "nm", "nm"), type=str, help = "Metadata unit names. Order matters. \n Example: -t nanometer nanometer nanometer")
@click.option(
"--src", "-s", type=click.Path(exists=True), help="Input .tiff file location."
)
@click.option("--dest", "-d", type=click.Path(), help="Output .tiff file location.")
@click.option(
"--axes",
"-a",
nargs=3,
default=("z", "y", "x"),
type=str,
help="Metadata axis names. Order matters. \n Example: -a z y x",
)
@click.option(
"--translation",
"-t",
nargs=3,
default=(0.0, 0.0, 0.0),
type=float,
help="Metadata translation(offset) value. Order matters. \n Example: -t 1.0 2.0 3.0",
)
@click.option(
"--scale",
"-s",
nargs=3,
default=(1.0, 1.0, 1.0),
type=float,
help="Metadata scale value. Order matters. \n Example: -s 1.0 2.0 3.0",
)
@click.option(
"--units",
"-u",
nargs=3,
default=("nm", "nm", "nm"),
type=str,
help="Metadata unit names. Order matters. \n Example: -t nanometer nanometer nanometer",
)
def tif_to_zarr(src, dest, axes, translation, scale, units):
# load tiff data
c = conv.Conversion(src, dest, axes, translation, scale, units)
c = conv.Conversion(src, dest, axes, translation, scale, units)
tiff_data = c.read_tiff()
#transpose tiff array
#tiff_data_mod = [np.transpose(tiff_data[0]), tiff_data[1]]
#flip values along y axis
tiff_data_mod = [tiff_data[0], tiff_data[1]]
#tiff_data_mod = [np.flip(tiff_data[0], axis=1), tiff_data[1]]
# transpose tiff array
# tiff_data_mod = [np.transpose(tiff_data[0]), tiff_data[1]]
# flip values along y axis
tiff_data_mod = [tiff_data[0], tiff_data[1]]
# tiff_data_mod = [np.flip(tiff_data[0], axis=1), tiff_data[1]]
print(tiff_data_mod[0].shape)
# store tiff data in a .zarr file
# store tiff data in a .zarr file
chunks = (64, 64, 64)
c.dask_to_zarray(tiff_data_mod, chunks)

if __name__ == '__main__':
tif_to_zarr()
c.dask_to_zarray(tiff_data_mod, chunks)


if __name__ == "__main__":
tif_to_zarr()

0 comments on commit e7b58ee

Please sign in to comment.