1 """
2 ::
3 /*
4 * $Id$
5 *
6 * Copyright 2009 Glencoe Software, Inc. All rights reserved.
7 * Use is subject to license terms supplied in LICENSE.txt
8 *
9 */
10 """
11
12 """
13 Concrete implementations of the omero.grid.Column
14 type hierarchy which know how to convert themselves
15 to PyTables types.
16 """
17
18 import omero, Ice
19 import omero_Tables_ice
20
21 try:
22 import numpy
23 tables = __import__("tables")
24 has_pytables = True
25 except ImportError:
26 has_pytables = False
27
28
30 """
31 Takes a list of columns and converts them into a map
32 from names to tables.* column descriptors
33 """
34 definition = {}
35 for i in range(len(cols)):
36 column = cols[i]
37 instance = column.descriptor(pos=i)
38 definition[column.name] = instance
39
40 return definition
41
43 """
44 Base logic for all columns
45 """
46
48 d = self.descriptor(0)
49 if isinstance(d, tables.IsDescription):
50 cols = d.columns
51 try:
52 del cols["_v_pos"]
53 except KeyError:
54 pass
55 self.recarrtypes = [None for x in range(len(cols))]
56 for k, v in cols.items():
57 self.recarrtypes[v._v_pos] = ("%s/%s" % (self.name, k), v.recarrtype)
58 else:
59 self.recarrtypes = [(self.name, d.recarrtype)]
60
62 """
63 Called by tables.py to give columns. By default, does nothing.
64 """
65 pass
66
73
74 - def read(self, tbl, start, stop):
77
79 """
80 Any method which does not use the "values" field
81 will need to override this method.
82 """
83 if self.values is None:
84 return None
85 else:
86 return len(self.values)
87
89 """
90 Any method which does not use the "values" field
91 will need to override this method.
92 """
93 if size is None:
94 self.values = None
95 else:
96 self.values = [None for x in range(size)]
97
99 """
100 Any method which does not use the "values" field
101 will need to override this method.
102 """
103 return [self.name]
104
106 """
107 Any method which does not use the "values" field
108 will need to override this method.
109 """
110 return [numpy.array(self.values, dtype=self.recarrtypes[0][1])]
111
113 """
114 Any method which does not use the "values" field
115 will need to override this method.
116 """
117 self.values = rows[self.name]
118
119 d = self.recarrtypes[0][1]
120 if isinstance(d, str):
121 if d.endswith("i8"):
122 self.values = self.values.tolist()
123 elif d.kind == "i" and d.itemsize == "8":
124 self.values = self.values.tolist()
125
126 -class FileColumnI(AbstractColumn, omero.grid.FileColumn):
127
128 - def __init__(self, name = "Unknown", *args):
131
133 return tables.Int64Col(pos=pos)
134
136
137 - def __init__(self, name = "Unknown", *args):
140
142 return tables.Int64Col(pos=pos)
143
144 -class WellColumnI(AbstractColumn, omero.grid.WellColumn):
145
146 - def __init__(self, name = "Unknown", *args):
149
151 return tables.Int64Col(pos=pos)
152
153 -class RoiColumnI(AbstractColumn, omero.grid.RoiColumn):
154
155 - def __init__(self, name = "Unknown", *args):
158
160 return tables.Int64Col(pos=pos)
161
163
164 - def __init__(self, name = "Unknown", *args):
167
169 return tables.Int64Col(pos=pos)
170
171 -class BoolColumnI(AbstractColumn, omero.grid.BoolColumn):
172
173 - def __init__(self, name = "Unknown", *args):
176
178 return tables.BoolCol(pos=pos)
179
181
182 - def __init__(self, name = "Unknown", *args):
185
187 return tables.Float64Col(pos=pos)
188
189 -class LongColumnI(AbstractColumn, omero.grid.LongColumn):
190
191 - def __init__(self, name = "Unknown", *args):
194
196 return tables.Int64Col(pos=pos)
197
199
200 - def __init__(self, name = "Unknown", *args):
203
205 return tables.StringCol(pos=pos)
206
207 -class MaskColumnI(AbstractColumn, omero.grid.MaskColumn):
208
209 - def __init__(self, name = "Unknown", *args):
212
214 if a is None:
215 if b is None:
216 return
217
218 if b is not None:
219 if len(a) == len(b):
220 return
221 raise omero.ValidationException(None, None, "Columns don't match")
222
231
233 class MaskDescription(tables.IsDescription):
234 _v_pos = pos
235 i = tables.Int64Col(pos=0)
236 z = tables.Int32Col(pos=1)
237 t = tables.Int32Col(pos=2)
238 x = tables.Float64Col(pos=3)
239 y = tables.Float64Col(pos=4)
240 w = tables.Float64Col(pos=5)
241 h = tables.Float64Col(pos=6)
242 return MaskDescription()
243
245 return [x[0] for x in self.recarrtypes]
246
248 self.__sanitycheck()
249 a = [
250 numpy.array(self.imageId, dtype=self.recarrtypes[0][1]),
251 numpy.array(self.theZ, dtype=self.recarrtypes[1][1]),
252 numpy.array(self.theT, dtype=self.recarrtypes[2][1]),
253 numpy.array(self.x, dtype=self.recarrtypes[3][1]),
254 numpy.array(self.y, dtype=self.recarrtypes[4][1]),
255 numpy.array(self.w, dtype=self.recarrtypes[5][1]),
256 numpy.array(self.h, dtype=self.recarrtypes[6][1]),
257 ]
258 return a
259
261 self.__sanitycheck()
262 if self.imageId is None:
263 return None
264 else:
265 return len(self.imageId)
266
268 if size is None:
269 self.imageId = None
270 self.theZ = None
271 self.theT = None
272 self.x = None
273 self.y = None
274 self.w = None
275 self.h = None
276 else:
277 self.imageId = numpy.zeroes(size, dtype = self.recarrtypes[0][1])
278 self.theZ = numpy.zeroes(size, dtype = self.recarrtypes[1][1])
279 self.theT = numpy.zeroes(size, dtype = self.recarrtypes[2][1])
280 self.x = numpy.zeroes(size, dtype = self.recarrtypes[3][1])
281 self.y = numpy.zeroes(size, dtype = self.recarrtypes[4][1])
282 self.w = numpy.zeroes(size, dtype = self.recarrtypes[5][1])
283 self.h = numpy.zeroes(size, dtype = self.recarrtypes[6][1])
284
292
293 - def read(self, tbl, start, stop):
299
301 self.bytes = []
302 for idx in rowNumbers:
303 self.bytes.append(masks[idx].tolist())
304
306 rows = all_rows[self.name]
307
308 self.imageId = rows["i"].tolist()
309 self.theZ = rows["z"].tolist()
310 self.theT = rows["t"].tolist()
311 self.x = rows["x"]
312 self.y = rows["y"]
313 self.w = rows["w"]
314 self.h = rows["h"]
315
321
323 n = tbl._v_name
324 f = tbl._v_file
325 p = tbl._v_parent
326
327
328 try:
329 masks = getattr(p, "%s_masks" % n)
330 except tables.NoSuchNodeError:
331 masks = f.createVLArray(p, "%s_masks" % n, tables.UInt8Atom())
332 return masks
333
334
335
336
337
338
340
342 self.id = cls.ice_staticId()
343 self.f = f
344
347
350
352 ic.addObjectFactory(self, self.id)
353
354
355
356
357
358 ObjectFactories = {
359 FileColumnI: ObjectFactory(FileColumnI, lambda: FileColumnI()),
360 ImageColumnI: ObjectFactory(ImageColumnI, lambda: ImageColumnI()),
361 RoiColumnI: ObjectFactory(RoiColumnI, lambda: RoiColumnI()),
362 WellColumnI: ObjectFactory(WellColumnI, lambda: WellColumnI()),
363 BoolColumnI: ObjectFactory(BoolColumnI, lambda: BoolColumnI()),
364 DoubleColumnI: ObjectFactory(DoubleColumnI, lambda: DoubleColumnI()),
365 LongColumnI: ObjectFactory(LongColumnI, lambda: LongColumnI()),
366 StringColumnI: ObjectFactory(StringColumnI, lambda: StringColumnI()),
367 MaskColumnI: ObjectFactory(MaskColumnI, lambda: MaskColumnI())
368 }
369