Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Feb 15, 2020
1 parent 6bb443a commit 70a4706
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/nd2_deskew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# pip install pims_nd2
from pims import ND2_Reader
from pycudadecon.affine import affineGPU
from skimage.io import imsave
import numpy as np
import os

# needed to flip the sign on the transform
DEFAULT_TMAT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0.70711, 0, 1, 0], [0, 0, 0, 1]])


def deskew_file(path, tmat=DEFAULT_TMAT, mip=True):
"""Deskews an nd2 file at `path` using matrix tmat.
Will create a new folder (with the same name as the file)
of multichannel deskewed files for each timepoint.
Args:
path (str): the nd2 file to process
tmat (np.ndarray): the transformation matrix
mip (bool): Whether to write a MIP file
"""
tmat = np.array(tmat)
dirname, fname = os.path.split(path)
fname, ext = os.path.splitext(fname)
outdir = os.path.join(dirname, fname + "_deskewed")
os.makedirs(outdir, exist_ok=True)
with ND2_Reader(path) as frames:
frames.bundle_axes = "czyx"
for frame in frames:
print(f"processing frame: {frame.frame_no + 1:4} of {len(frames)}")

# do the actual deskew to each channel
# the .T rotates it 90 degrees
out = np.stack([affineGPU(chan, tmat).T for chan in frame])
dst = os.path.join(outdir, f"{fname}_{frame.frame_no:04}.tif")

# could add metadata here for voxel sizes
imsave(dst, np.transpose(out, (1, 0, 2, 3)), imagej=True)
if mip:
mipdir = os.path.join(outdir, "MIPs")
os.makedirs(mipdir, exist_ok=True)
dst = os.path.join(mipdir, f"{fname}_{frame.frame_no:04}_mip.tif")
imsave(dst, out.max(1), imagej=True)


if __name__ == "__main__":
import sys

deskew_file(sys.argv[1])

0 comments on commit 70a4706

Please sign in to comment.