Skip to content

Commit

Permalink
Adding row_col_swap.py
Browse files Browse the repository at this point in the history
  • Loading branch information
shanqing-cai committed Aug 4, 2013
1 parent 6d83c5f commit 403420f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
39 changes: 34 additions & 5 deletions dtiprep.py
Expand Up @@ -4,14 +4,17 @@
import sys
import argparse
import glob
from scai_utils import saydo
from scai_utils import *

DATA_DIR = "/users/cais/STUT/DATA"
DATA_DIR_RHY = "/users/cais/RHY/DATA"
FSDATA_DIR = "/users/cais/STUT/FSDATA"
DTIPREP_PATH = "/software/DTIPrep/1.1.6"
# DTIPREP_PATH = "/software/DTIPrep/1.1.6"
DTIPREP_PATH = "/software/DTIPrep/130630"
OUTPUT_DIR = "/users/cais/STUT/analysis/dti"
OUTPUT_DIR_RHY = "/users/cais/RHY/analysis/dwi"
NRRD2NIFTI_BIN = "/software/DTIPrep/dev/DWI_NiftiNrrdConversion"
DWICONVERT_BIN = "/software/DTIPrep/dev/DWIConvert"
DWICONVERT_BIN = "/software/DTIPrep/3f0a2b4/DWIConvert"

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run DTIPrep on STUT data")
Expand Down Expand Up @@ -58,11 +61,12 @@
if not os.path.isfile(bve):
raise Exception, "Cannot find bvecs file: %s"%(bve)

else: # MIT STUT study
elif len(sID) == 3 and sID.startswith("S"): # MIT STUT study
rawdifdir = os.path.join(DATA_DIR, sID, "diffusion");
if not os.path.isdir(rawdifdir):
raise Exception, "Cannot find raw NIFTI diffusion data directory: %s"\
%(rawdifdir)

bva = glob.glob(os.path.join(rawdifdir, "%s_*.mghdti.bvals"%(sID)))
if len(bva) == 0:
raise Exception, "Found no DTI volumes in raw data directory: %s"\
Expand All @@ -82,6 +86,29 @@
raise Exception, "Cannot find raw 4D DTI series: %s"%(raw4d)
if not os.path.isfile(bve):
raise Exception, "Cannot find bvecs file: %s"%(bve)
else:
DATA_DIR = DATA_DIR_RHY
OUTPUT_DIR = OUTPUT_DIR_RHY
rawdifdir = os.path.join(DATA_DIR, sID, "diffusion")

if not os.path.isdir(rawdifdir):
raise Exception, "Cannot find raw NIFTI diffusion data directory: %s"\
%(rawdifdir)

bva = glob.glob(os.path.join(rawdifdir, "%s_diffusion_*.bval" % sID))
if len(bva) == 0:
raise Exception, "Found no DTI volumes in raw data directory: %s"\
%(rawdifdir)

bva = bva[-1]
rootname = os.path.split(bva)[1].replace('.bval', '')
print("INFO: rootname = %s"%(rootname))

raw4d = os.path.join(rawdifdir, "%s.nii.gz"%(rootname))
check_file(raw4d)

bve = os.path.join(rawdifdir, "%s.bvec"%(rootname))
check_file(bve)

print("INFO: raw4d = %s"%(raw4d))
print("INFO: bva = %s"%(bva))
Expand Down Expand Up @@ -112,7 +139,7 @@
print("INFO: nifti-to-nrrd conversion done: output = %s\n"%(nrrdfn))

if bConvertOnly:
sys.exit(0)
sys.exit(0)

# ------- Convert to Nrrd ------ #
# Find the diffusion_dcm directory
Expand Down Expand Up @@ -161,6 +188,8 @@
if not os.path.isfile(qced_nrrd):
raise Exception, "It appears that DTIPrep has failed: cannot find series: %s"%(qced_nrrd)

sys.exit(0)

# ------ Convert QCed back to nifti ------ #
qced_ngz = os.path.join(qcDir, "%s_QCed.nii.gz"%(sID))

Expand Down
55 changes: 55 additions & 0 deletions row_col_swap.py
@@ -0,0 +1,55 @@
#!/usr/bin/python

import os
import sys
import numpy as np
import argparse

from scai_utils import *

if __name__ == "__main__":
ap = argparse.ArgumentParser(description="Swap rows and columns in an ASCII data file of numbers (e.g., diffusion-weighted MRI gradient orientation vectors")
ap.add_argument("inFN", help="Input file name")
ap.add_argument("outFN", help="Output file name")

if len(sys.argv) == 1:
ap.print_help()
sys.exit(0)

args = ap.parse_args()
check_file(args.inFN)

f = open(args.inFN, "rt")
txt = remove_empty_strings(f.read().split("\n"))
f.close()

nrows = len(txt)

a_ncols = np.zeros(nrows)
for (i0, tline) in enumerate(txt):
items = remove_empty_strings(tline.split(" "))
a_ncols[i0] = len(items)

assert(len(np.unique(a_ncols)) == 1)

ncols = int(a_ncols[0])
dat = np.zeros([nrows, ncols])

for (i0, tline) in enumerate(txt):
items = remove_empty_strings(tline.split(" "))

for (i1, t_item) in enumerate(items):
dat[i0, i1] = float(t_item)


dat = np.transpose(dat)


f = open(args.outFN, "wt")
for i0 in range(ncols):
for i1 in range(nrows):
f.write("%.14f " % dat[i0, i1])
f.write("\n")

f.close()
check_file(args.outFN)

0 comments on commit 403420f

Please sign in to comment.