-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushLive.py
executable file
·88 lines (81 loc) · 4.85 KB
/
PushLive.py
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
import os, sys, nrrd, cmtk, gc, subprocess, shutil, png
import numpy as np
import warpScoring.CheckImages as ci
from cmtk import cur, tempfolder, active, run_stage, cmtkdir, template, checkDir, host, templatedir
from PIL import Image
wlzDir = "/partition/bocian/VFBTools/Woolz2013Full/bin/"
Fiji = "nice /partition/bocian/VFBTools/Fiji/ImageJ-linux64 --headless"
def pushLive(id, name, sgfile):
if os.path.exists(sgfile):
if (id > 9999):
first = str(id)[:-4]
last = str(id)[-4:]
else:
first = "000"
last = str(id)
while len(first) < 3:
first = "0" + first
while len(last) < 4:
last = "0" + last
first = "a" + first
if not os.path.exists(tempfolder + "../../IMAGE_DATA/VFB/i/" + first):
os.makedirs(tempfolder + "../../IMAGE_DATA/VFB/i/" + first)
if not os.path.exists(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last):
os.makedirs(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last)
print 'Linking ' + sgfile + ' to ' + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd"
if os.path.exists(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd"):
os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd")
shutil.copyfile(sgfile, tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd")
data, head = nrrd.read(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd")
if head['sizes'][2] == 185:
print "Inflating from " + str(head['sizes'][2]) + " slices to 270..."
dataNew = np.zeros([head['sizes'][0],head['sizes'][1],270],dtype=np.uint8)
dataNew[:,:,25:210]=data;
head['encoding'] = 'gzip'
if head.has_key('space direction'):
head.pop("space directions", None)
data = dataNew
im = np.transpose(np.max(data,axis=2))
if (data[0][0][0] < 1):
data[0][0][0] = np.uint8(100)
filesize = np.subtract(data.shape, 1)
if (data[filesize[0]][filesize[1]][filesize[2]] < 1):
data[filesize[0]][filesize[1]][filesize[2]] = np.uint8(100)
nrrd.write(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd", data, options=head)
print "Creating Thumbnail"
im = Image.fromarray(im)
if np.shape(im)[0] < np.shape(im)[1]:
im = im.resize((120,60), Image.ANTIALIAS)
elif np.shape(im)[0] > np.shape(im)[1]:
im = im.resize((60,120), Image.ANTIALIAS)
else:
im = im.resize((60,60), Image.ANTIALIAS)
im.save(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/thumbnail.png","PNG")
print "Converting to Tiff"
subprocess.call(Fiji + " -macro nrrd2tif.ijm " + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd" + " -batch", shell=True)
if os.path.isfile(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz"):
os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz")
print "Clearing old woolz data..."
print "Creating wlz: " + "/VFB/i/" + first + "/" + last + "/volume.wlz"
print "Using voxel sizes: Z:" + str(head['space directions'][2][2]) + ", X:" + str(head['space directions'][0][0]) + ", Y:" + str(head['space directions'][1][1])
subprocess.call("nice " + wlzDir + "WlzExtFFConvert -f tif -F wlz " + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.tif |" + wlzDir + "WlzThreshold -v50 |" + wlzDir + "WlzSetVoxelSize -z" + str(head['space directions'][2][2]) + " -x" + str(head['space directions'][0][0]) + " -y" + str(head['space directions'][1][1]) + " >" + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz" , shell=True)
cur.execute("UPDATE images_alignment SET alignment_stage = 21, notes = %s WHERE id = %s", [str("VFB_"+first+last),str(id)])
cur.connection.commit()
print "Clearing up..."
os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.tif")
else:
print "Skipping " + name
gc.collect()
if __name__ == "__main__":
if active and '5' in run_stage:
cur.execute("SELECT id, name, aligned_sg FROM images_alignment WHERE alignment_stage = 11 ORDER BY images_alignment.id")
records = cur.fetchall()
for line in records:
try:
pushLive(line[0], line[1], sgfile=(tempfolder + line[2]))
except:
cur.execute("UPDATE images_alignment SET alignment_stage = 21, notes = 'FAILED' WHERE id = %s", [str(line[0])])
cur.connection.commit()
print 'done'
else:
print 'inactive or stage 4 not selected'