Skip to content

Commit

Permalink
bump up to patest release of zarrnii (#7)
Browse files Browse the repository at this point in the history
includes fix to zarrnii affine and flipping
  • Loading branch information
akhanf authored Jun 19, 2024
1 parent c56f604 commit ff40c00
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 5 additions & 23 deletions spimquant/workflow/scripts/ome_zarr_to_nii.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,6 +21,7 @@

level=int(snakemake.wildcards.level)


#read coordinate transform from ome-zarr
transforms = attrs['multiscales'][0]['datasets'][level]['coordinateTransformations']

Expand All @@ -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)


0 comments on commit ff40c00

Please sign in to comment.