Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d83c5f
commit 403420f
Showing
2 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |