-
Notifications
You must be signed in to change notification settings - Fork 1
/
computation-server.py
executable file
·181 lines (149 loc) · 6.24 KB
/
computation-server.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python
# This is a simple HTTP server that provides an API for various computations
# performed on the server side.
#
# Usage:
# ./computation-server.py ../lightsheet/
# then go to URL http://localhost:8002/
#
# HTTP API:
# * /<filename>/box-intensity/<channel>/<group>/x1,y1,z1-x2,y2,z2 returns
# a JSON-formatted list of frame values representing the intensity
# of the bounding box contents
# * box-intensity append ?wholenorm to normalize this intensity by the global
# frame brightness
# * box-intensity append ?chnorm to normalize this intensity by the other
# channel intensity
# This is directory with the HDF5 files to be served in the /lightsheet/
# HTTP path.
lightsheet_dir = "../lightsheet/"
# Directory with JSON backbone data
backbone_data_dir = "meteor/data/"
# Neuron position data - either .json file or a directory with .nml files
neurons_data = "meteor/data/neurons.json"
http_port = 8002
import sys
if sys.argv[1:]:
lightsheet_dir = sys.argv[1]
if sys.argv[2:]:
http_port = sys.argv[2]
import tables
import numpy
import scipy.misc
import matplotlib.pyplot as plt
class BoxIntensity:
"""
Computation of box intensity over time.
"""
def __init__(self, filename, channel):
self.filename = lightsheet_dir + '/' + filename
self.h5file = tables.open_file(self.filename, mode = "r")
self.objpath = "/images/.ch" + str(channel)
self.objnode = self.h5file.get_node('/', self.objpath)
self.ch2objpath = "/images/.ch" + str(1 - channel)
self.ch2objnode = self.h5file.get_node('/', self.ch2objpath)
def of_subgroup(self, sgnode, box, wholenorm = 0):
imgdata = sgnode.read()
imgbox = imgdata[box[0][0]:box[1][0]+1, box[0][1]:box[1][1]+1]
# print ' ' + str(imgbox[0,0])
avg = numpy.average(imgbox)
if wholenorm:
avg /= numpy.average(imgdata)
return avg
def of_group(self, gnode, gpath, box, wholenorm = 0, chnorm = 0):
slicevals = []
for (i, node) in sorted(gnode._v_children.items(), key = lambda x: x[1].attrs['ls_z_measured']):
z = node.attrs['ls_z_measured']
if z >= box[0][2] and z <= box[1][2]:
# print ' ' + str(z)
value = self.of_subgroup(node, box, wholenorm)
if chnorm and self.ch2objnode:
ch2node = self.h5file.get_node(self.ch2objpath, gpath + '/' + str(i))
val2 = self.of_subgroup(ch2node, box, wholenorm);
value /= val2
slicevals.append(value)
return numpy.average(slicevals)
def of_all(self, box, wholenorm = 0, chnorm = 0):
values = []
for (i, node) in sorted(self.objnode._v_children.items(), key = lambda x: float(x[0])):
# print i
values.append(self.of_group(node, str(i), box, wholenorm, chnorm))
return values
import poselib
import nmllib
class NeuronPositions:
"""
List of neuron positions corresponding to a given worm pose and backbone.
"""
def __init__(self, hdf5file, frameno):
bbfilename = backbone_data_dir + hdf5file + '-' + str(frameno) + '-backbone.json'
(points, edgedists) = poselib.bbLoad(bbfilename)
(spline, bblength) = poselib.bbToSpline(points)
self.bbpoints = poselib.bbTraceSpline(spline, bblength)
def neurons_by_pose(self, poseinfo):
pneurons = []
for n in self.neurons:
pn_pos = poselib.projTranslateByBb(poselib.projCoord(n["pos"], poseinfo), self.bbpoints, n["name"], poseinfo)
if pn_pos is None:
continue
pn = n.copy()
pn["pos"] = pn_pos
pn["diameter"] = poselib.projDiameter(n["diameter"], poseinfo)
pneurons.append(pn)
return pneurons
neurons = nmllib.load_neurons(neurons_data)
from flask import *
from functools import update_wrapper
app = Flask(__name__)
# http://flask.pocoo.org/snippets/56/
def crossdomain(origin=None, methods=None, headers=None,
attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
@app.route('/<string:filename>/box-intensity/<int:channel>/<string:boxcoords>')
@crossdomain(origin='*')
def box_intensity(filename, channel, boxcoords):
boxi = BoxIntensity(filename, channel)
box = map(lambda k: map(lambda kk: float(kk), k),
map(lambda k: k.split(','),
boxcoords.split('-')))
return jsonify({'intensity': boxi.of_all(box,
wholenorm = request.args.has_key('wholenorm'),
chnorm = request.args.has_key('chnorm')
)})
@app.route('/<string:filename>/neuron-positions/<int:frameno>/<string:poseinfo_s>')
@crossdomain(origin='*')
def neuron_positions(filename, frameno, poseinfo_s):
npos = NeuronPositions(filename, frameno)
poseinfo = dict(zip(["zoom", "shift", "angle"], [float(f) for f in poseinfo_s.split(',')]))
return nmllib.jsondump_neurons(npos.neurons_by_pose(poseinfo))
if __name__ == '__main__':
# app.run(port = http_port)
app.run(port = http_port, debug = True)