-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbMapManager.py
322 lines (259 loc) · 8.27 KB
/
bMapManager.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#20160625
#Author: Robert H Cudmore
#Web: http://robertcudmore.org
#for ipython notebook
'''
%matplotlib inline
from plotly.offline import init_notebook_mode
init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
'''
import os, math
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
#import matplotlib.dates as md
#from matplotlib.patches import Rectangle
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
#import plotly as py
from plotly.graph_objs import Scatter, Layout
#for ipython notebook
#init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
import tifffile #requires pip install tifffile
################################################
class bStackPool():
'''
Load 'Export (all stack'
ToDo: make this load a directory of .tif and /stackdb/ pairs
'''
def __init__(self, filePath=''):
self.filePath = filePath
self.stackdb = None
self.header = {}
if self.filePath:
self.load(self.filePath)
def load(self, filePath):
#check if path exists
headerLines = 0
#header = self.loadHeader(filepath)
self.stackdb = pd.read_csv(
filePath,
header=headerLines)
'''
def loadHeader(self, filePath):
#read header as dict
with open(filePath, 'U') as f:
header = f.readline()
header = header.rstrip()
if header.endswith(','):
header = header[:-1] #remove last char ','
if header.endswith(';'):
header = header[:-1] #remove last char ';'
d = dict(s.split('=') for s in header.split(';'))
return d
'''
def getSpines(self, parentID=None):
return self.getObj(roiType='spineROI', parentID=parentID)
def getObj(self, roiType='spineROI', parentID=None):
obj = self.stackdb[self.stackdb['roiType']==roiType]
if parentID>=0:
obj = obj[obj['parentID']==parentID]
return obj
################################################
class bStack():
def __init__(self, path='', filePrefix=''):
self.path = ''
self.prefix = ''
self.stackdb = []
self.line = []
self.int1 = []
self.int2 = []
self.header = {}
self.data = None
self.currentChannel = 1
if path and filePrefix:
self.load(path, filePrefix)
def load(self, path, filePrefix):
'''
load map manager files from disk (does not load .tif stacks
'''
#check if path exists
self.path = path
self.prefix = filePrefix
#load stackdb
stackdbFile = filePrefix + '_db2.txt'
filepath = path + '/stackdb/' + stackdbFile
header = self.loadHeader(filepath)
self.header['voxelx'] = header['voxelx']
self.header['voxely'] = header['voxely']
self.header['voxelz'] = header['voxelz']
self.stackdb = pd.read_csv(
filepath,
header=1)
#load line
lineFile = filePrefix + '_l.txt'
filepath = path + '/line/' + lineFile
header = self.loadHeader(filepath)
numHeaderRow = int(header['numHeaderRow'])
numHeaderRow += 2 # 1 for file header, 1 for linedb header
self.line = pd.read_csv(
filepath,
header=numHeaderRow)
#load int 1
intFile = filePrefix + '_Int1.txt'
filepath = path + '/stackdb/' + intFile
self.int1 = pd.read_csv(
filepath,
header=1)
#load int 1
#IMPLEMENT THIS
def loadHeader(self, filePath):
#read header as dict
with open(filePath, 'U') as f:
header = f.readline()
header = header.rstrip()
if header.endswith(','):
header = header[:-1] #remove last char ','
if header.endswith(';'):
header = header[:-1] #remove last char ';'
d = dict(s.split('=') for s in header.split(';'))
return d
def loadtiff(self):
'''
for a single stack, .tif is just /Raw/ + self.prefix + '.tif'
for a map, we need to know session
for a5n (bSPine), i20110331_a5n_s0_ch1
'''
specialPrefix = ''
if self.prefix.startswith('a5n_'):
specialPrefix = 'i20110331_'
ch1path = self.path + '/Raw/' + specialPrefix + self.prefix + '_ch1.tif'
if not os.path.isfile(ch1path):
print 'ERROR: bStack.loadtiff() did not find:', ch1path
return
ch2path = self.path + '/Raw/' + specialPrefix + self.prefix + '_ch2.tif'
if not os.path.isfile(ch2path):
print 'ERROR: bStack.loadtiff() did not find:', ch2path
#return
self.data = tifffile.imread(ch2path)
print 'bStack.loadtiff() loaded', ch1path, 'shape:', self.data.shape, 'dtype:', self.data.dtype
def unloadtiff(self):
self.data = None
def getSlice(self, num):
# check sanity of slice num
if num>=0 and num < self.data.shape[0]-1:
return self.data[num]
else:
return None
def printInfo(self):
print 'prefix:', self.prefix
print ' dx,dy,dz:', self.header['voxelx'],self.header['voxely'],self.header['voxelz']
print ' pnts:', self.numPoints()
print ' spines:', len(self.getSpines())
print ' segments:', self.numSegments()
def numPoints(self): return len(self.stackdb.index)
#def getStackdb(self,col): return self.stackdb[col]
def getSpines(self, parentID=None):
return self.getObj(roiType='spineROI', parentID=parentID)
def getSpinesInt(self, parentID=None):
objIdx = self.getObj(roiType='spineROI',parentID=parentID).index.tolist()
return self.int1.loc[objIdx]
def getObj(self, roiType='spineROI', parentID=None):
obj = self.stackdb[self.stackdb['roiType']==roiType]
if parentID>=0:
obj = obj[obj['parentID']==parentID]
return obj
def getLine(self, parentID=None):
obj = self.line
if parentID>=0:
obj = obj[obj['ID']==parentID]
return obj
def numSegments(self):
#make numSegments a data member and calculate once on load
df2=self.stackdb[self.stackdb['parentID'] >=0]
numSeg = max(df2['parentID']) - min(df2['parentID']) + 1
return math.trunc(numSeg)
def getMask(self,z, parentID=None):
#df[df.c > 0.5][['b', 'e']].values
zTop = z - 1
zBottom = z + 1
spines = self.getSpines(parentID=parentID)
ret = spines[(spines.z >= zTop) & (spines.z <= zTop)][['x','y','z']].values
return ret
################################################
class bStackPlot():
def __init__(self, stack):
self.stack = stack
def plot(self, roiType='spineROI', parentID=None, xStat='x', yStat='y'):
obj = self.stack.getObj(roiType=roiType, parentID=parentID)
plt.scatter(obj[xStat], obj[yStat])
plt.ylabel(yStat)
plt.xlabel(xStat)
def plotLine(self, roiType='spineROI', parentID=None, xStat='x', yStat='y'):
obj = self.stack.getLine(parentID=parentID)
plt.scatter(obj[xStat], obj[yStat])
plt.ylabel(yStat)
plt.xlabel(xStat)
def plotInt(self, roiType='spineROI', parentID=None, yStat='sSum', xStat='dSum'):
objIdx = self.stack.getObj(roiType=roiType,parentID=parentID).index.tolist()
tmpInt = self.stack.int1.loc[objIdx]
plt.scatter(tmpInt[xStat], tmpInt[yStat])
plt.ylabel(yStat)
plt.xlabel(xStat)
def plotly(self, roiType='spineROI', parentID=None, xStat='x', yStat='y'):
obj = self.stack.getObj(roiType=roiType, parentID=parentID)
fig = {
'data': [
{
'x': obj[xStat],
'y': obj[yStat],
'text': '',
'mode': 'markers',
'name': '2007'
},
],
'layout': {
#'xaxis': {'title': 'GDP per Capita', 'type': 'log'},
'xaxis': {'title': xStat},
'yaxis': {'title': yStat},
'height': 400,
'width': 400,
}
}
iplot(fig)
#py.iplot(fig, filename='pandas/multiple-scatter')
#url = py.plot(fig, filename='myscatter')
#print url
################################################
class bMap():
def __init__(self, path=''):
self.stackList = []
if path:
self.load(path)
def load(self,path):
'''
loads a map manager map, a map is a sequence of stacks
does NOT load .tif stacks
'''
#path is path to map folder, it contains folders for (stackdb, line)
#check if path exists
#load stackdb
stackdb_dir = os.listdir(path + '/stackdb')
for file in stackdb_dir:
#files ending in _db2.txt are stackdb files
if file.endswith('_db2.txt'):
#print 'loading file:', file
stackPrefix = file.replace('_db2.txt','')
#this does not work because aStack is only local?
#aStack = bStack()
#aStack = aStack.load(path, stackPrefix)
self.stackList.append(bStack())
self.stackList[-1].load(path, stackPrefix)
def printInfo(self):
print 'number of timepoints:', self.numTimepoints()
for stack in self.stackList:
print stack.printInfo()
def numTimepoints(self): return len(self.stackList)
def stacks(self): return self.stackList
def loadtiff(self, tp):
self.stacklist[tp].loadtiff()