From ff40c006f17fb9fa028f665651aa1947f34209b2 Mon Sep 17 00:00:00 2001 From: Ali Khan Date: Wed, 19 Jun 2024 09:59:03 -0400 Subject: [PATCH] bump up to patest release of zarrnii (#7) includes fix to zarrnii affine and flipping --- poetry.lock | 8 +++--- pyproject.toml | 2 +- spimquant/workflow/scripts/ome_zarr_to_nii.py | 28 ++++--------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/poetry.lock b/poetry.lock index e65f5fd..eec89b7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -6492,14 +6492,14 @@ jupyter = ["ipytree (>=0.2.2)", "ipywidgets (>=8.0.0)", "notebook"] [[package]] name = "zarrnii" -version = "0.1.1a1" +version = "0.1.3a1" description = "Package for working with OME-Zarr and NIFTI images in a unified manner, with a focus on spatial transformations" category = "main" optional = false python-versions = "<4.0,>=3.9" files = [ - {file = "zarrnii-0.1.1a1-py3-none-any.whl", hash = "sha256:c61b0cad748f5b1e862560be4f92e5695d37cd23a1534e781809772704c98e1f"}, - {file = "zarrnii-0.1.1a1.tar.gz", hash = "sha256:0ffc8e5e8d48b5ef9ce65d58dcf6604f342115bb16d9785092573f4fd2111526"}, + {file = "zarrnii-0.1.3a1-py3-none-any.whl", hash = "sha256:487aeebf45e16b6cd721d8d9ed05a7fdc2196c66f01ebaf5e3e4f27a3747954f"}, + {file = "zarrnii-0.1.3a1.tar.gz", hash = "sha256:51b46c8b52d4842fe8daf66cbd8498d4287207d90006326ca1f82a3a4454507d"}, ] [package.dependencies] @@ -6542,4 +6542,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.11,<3.12" -content-hash = "2429d5819d1f2ff3e8ce5f760cba5b03ceb090e3579ac30c4ed8583741e73282" +content-hash = "849dcf3085436b8d5dce08635b303f581efee7adee34d338154225711ded35f7" diff --git a/pyproject.toml b/pyproject.toml index a0b7c45..9ab0abb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,8 +30,8 @@ dask-image = "^2023.8.1" ome-zarr = "^0.9.0" pybids = "^0.16.5" sparse = "^0.15.1" -zarrnii = "^0.1.1a1" bokeh = "^3.4.1" +zarrnii = "0.1.3a1" [tool.poetry.scripts] spimquant = "spimquant.run:main" diff --git a/spimquant/workflow/scripts/ome_zarr_to_nii.py b/spimquant/workflow/scripts/ome_zarr_to_nii.py index 912d1eb..f080408 100644 --- a/spimquant/workflow/scripts/ome_zarr_to_nii.py +++ b/spimquant/workflow/scripts/ome_zarr_to_nii.py @@ -7,7 +7,7 @@ from ome_zarr.io import parse_url from ome_zarr.reader import Reader from dask.diagnostics import ProgressBar - +from zarrnii import ZarrNii in_zarr = snakemake.input.zarr @@ -21,6 +21,7 @@ level=int(snakemake.wildcards.level) + #read coordinate transform from ome-zarr transforms = attrs['multiscales'][0]['datasets'][level]['coordinateTransformations'] @@ -33,33 +34,14 @@ affine[2,2]=-transforms[0]['scale'][-3] #z -#grab the channel index corresponding to the stain -darr = da.from_zarr(in_zarr,component=f'/{level}')[channel_index,:,:,:].squeeze() - #downsample in z #calculate scaling between x and z -- if scaling is higher in z, then leave as is.. #if z is smaller, then we downsample by power of 2, one less than what would make it greater than x z_ratio = transforms[0]['scale'][-1] / transforms[0]['scale'][-3] # x / z scaling (if >1, z needs to be downsampled) zdownsampling = 2**(floor(log2(z_ratio))) -if zdownsampling > 1: - - affine[2,2]=zdownsampling*affine[2,2] - - # we achieve this by rechunking, then performing mean over axis-0 in each block - in_chunksize=(zdownsampling,darr.shape[1],darr.shape[2]) - out_chunksize=(1,darr.shape[1],darr.shape[2]) - - darr = darr.rechunk(in_chunksize).map_blocks(lambda x: np.mean(x,axis=0).reshape(1,x.shape[1],x.shape[2]),chunks=out_chunksize) - - -#input array axes are ZYX -#writing to nifti we want XYZ -darr_mvax = da.moveaxis(darr,(0,1,2),(2,1,0)) with ProgressBar(): - nii = nib.Nifti1Image(darr_mvax.compute(), - affine=affine - ) - -nii.to_filename(snakemake.output.nii) + ZarrNii.from_path(in_zarr,level=level).downsample(along_z=zdownsampling).to_nifti(snakemake.output.nii) + +