-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReverseAndFlipNrrds.txt
130 lines (116 loc) · 3.21 KB
/
ReverseAndFlipNrrds.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// "ReverseAndFlipNrrds"
//
// This macro batch processes all the nrrd files in a folder hierarchy
// reversing them in Z and flipping them horizontally
// Adapted by Greg Jefferis from code at
// http://rsb.info.nih.gov/ij/macros/BatchProcessFolders.txt
// jefferis@gmail.com
/* Run from the command line as follows
fiji -eval '
runMacro("/Volumes/JData/JPeople/Common/CommonCode/ImageJMacros/ReverseAndFlipNrrds.txt",
"/Volumes/JData/JPeople/Sebastian/fruitless/Registration/IS2Reg/images.unsorted/,/Volumes/JData/JPeople/Sebastian/fruitless/Registration/IS2Reg/images.unflipped/SAJ/");
' -batch --headless
*/
requires("1.42k");
file = getArgument;
dir=""
outputDir=""
//print("file = "+file);
if (file!=""){
arg = split(file,",");
if (arg.length!=2) {
exit();
} else if(arg[0]=="" || arg[1]==""){
exit();
} else {
outputDir=arg[1];
if(!endsWith(outputDir,"/")) outputDir=outputDir+"/";
if(File.isDirectory(arg[0])) {
// we're dealing with a directory
dir=arg[0];
if(!endsWith(dir,"/")) dir=dir+"/";
} else {
// single file
dir=File.getParent(arg[0])+"/";
file=File.getName(arg[0]);
processFile(dir,outputDir,file);
exit();
}
}
}
if(dir=="") dir = getDirectory("stacks directory");
if(outputDir=="") outputDir = getDirectory("output directory");
setBatchMode(true);
count = 0;
countFiles(dir);
print("Total files: "+count);
n = 0;
processFiles(dir, outputDir);
function countFiles(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
countFiles(""+dir+list[i]);
else
count++;
}
}
function processFiles(dir,outputDir) {
list = getFileList(dir);
// Stops multiple processes racing each other to do the same file
shuffle(list);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
processFiles(""+dir+list[i], outputDir+list[i]);
else {
showProgress(n++, count);
processFile(dir,outputDir,list[i]);
}
}
}
function processFile(dir,outputDir,file) {
lfile=toLowerCase(file);
if (endsWith(lfile, ".nrrd") /*|| endsWith(lfile, ".pic.gz") */) {
path = dir+file;
outfile=file;
outpath=outputDir+outfile;
lockpath=outputDir+outfile+'.lock';
if(File.exists(outpath)){
print("Skipping file: " + file + " since " + outfile + " already exists");
return;
}
if(File.exists(lockpath)){
print("Skipping file: " + file + " since someone else is working on it");
return;
}
File.saveString("",lockpath)
print("Inpath = "+path);
print("Outpath = "+outpath);
print("lockpath = "+lockpath);
run("Nrrd ...", "load=[" + path + "]");
processImage();
setKeyDown("alt");
run("Nrrd ... ", "nrrd=[" + outpath + "]");
setKeyDown("none");
close();
File.delete(lockpath);
}
}
function processImage() {
run("Flip Horizontally", "stack");
run("Stack Reverser");
}
function shuffle(array) {
n = array.length; // The number of items left to shuffle (loop invariant).
while (n > 1) {
k = randomInt(n); // 0 <= k < n.
n--; // n is now the last pertinent index;
temp = array[n]; // swap array[n] with array[k] (does nothing if k==n).
array[n] = array[k];
array[k] = temp;
}
}
// returns a random number, 0 <= k < n
function randomInt(n) {
return n * random();
}