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 when first initializing columns.
64 Can be used to complete further initialization.
65 """
66 self.__table = tbl
67
69 """
70 Called by tables.py to give columns. By default, does nothing.
71 """
72 pass
73
80
81 - def read(self, tbl, start, stop):
84
86 """
87 Any method which does not use the "values" field
88 will need to override this method.
89 """
90 if self.values is None:
91 return None
92 else:
93 return len(self.values)
94
96 """
97 Any method which does not use the "values" field
98 will need to override this method.
99 """
100 if size is None:
101 self.values = None
102 else:
103 self.values = [None for x in range(size)]
104
106 """
107 Any method which does not use the "values" field
108 will need to override this method.
109 """
110 return [self.name]
111
113 """
114 Any method which does not use the "values" field
115 will need to override this method.
116 """
117 return [numpy.array(self.values, dtype=self.recarrtypes[0][1])]
118
120 """
121 Any method which does not use the "values" field
122 will need to override this method.
123 """
124 self.values = rows[self.name]
125
126
127
128
129
130
131
132 self.values = self.values.tolist()
133
134 -class FileColumnI(AbstractColumn, omero.grid.FileColumn):
135
136 - def __init__(self, name = "Unknown", *args):
139
141 return tables.Int64Col(pos=pos)
142
144
145 - def __init__(self, name = "Unknown", *args):
148
150 return tables.Int64Col(pos=pos)
151
152 -class WellColumnI(AbstractColumn, omero.grid.WellColumn):
153
154 - def __init__(self, name = "Unknown", *args):
157
159 return tables.Int64Col(pos=pos)
160
161 -class RoiColumnI(AbstractColumn, omero.grid.RoiColumn):
162
163 - def __init__(self, name = "Unknown", *args):
166
168 return tables.Int64Col(pos=pos)
169
171
172 - def __init__(self, name = "Unknown", *args):
175
177 return tables.Int64Col(pos=pos)
178
179 -class BoolColumnI(AbstractColumn, omero.grid.BoolColumn):
180
181 - def __init__(self, name = "Unknown", *args):
184
186 return tables.BoolCol(pos=pos)
187
189
190 - def __init__(self, name = "Unknown", *args):
193
195 return tables.Float64Col(pos=pos)
196
197 -class LongColumnI(AbstractColumn, omero.grid.LongColumn):
198
199 - def __init__(self, name = "Unknown", *args):
202
204 return tables.Int64Col(pos=pos)
205
207
208 - def __init__(self, name = "Unknown", *args):
211
215
217 """
218 Overriding to correct for size.
219 """
220 sz = self.size
221 return [numpy.array(self.values, dtype="S%s"%sz)]
222
224
225
226 if not self.size or self.size < 0:
227 self.size = 1
228 return tables.StringCol(pos=pos, itemsize=self.size)
229
230 -class MaskColumnI(AbstractColumn, omero.grid.MaskColumn):
231
232 - def __init__(self, name = "Unknown", *args):
235
237 if a is None:
238 if b is None:
239 return
240
241 if b is not None:
242 if len(a) == len(b):
243 return
244 raise omero.ValidationException(None, None, "Columns don't match")
245
254
256 class MaskDescription(tables.IsDescription):
257 _v_pos = pos
258 i = tables.Int64Col(pos=0)
259 z = tables.Int32Col(pos=1)
260 t = tables.Int32Col(pos=2)
261 x = tables.Float64Col(pos=3)
262 y = tables.Float64Col(pos=4)
263 w = tables.Float64Col(pos=5)
264 h = tables.Float64Col(pos=6)
265 return MaskDescription()
266
268 return [x[0] for x in self.recarrtypes]
269
271 self.__sanitycheck()
272 a = [
273 numpy.array(self.imageId, dtype=self.recarrtypes[0][1]),
274 numpy.array(self.theZ, dtype=self.recarrtypes[1][1]),
275 numpy.array(self.theT, dtype=self.recarrtypes[2][1]),
276 numpy.array(self.x, dtype=self.recarrtypes[3][1]),
277 numpy.array(self.y, dtype=self.recarrtypes[4][1]),
278 numpy.array(self.w, dtype=self.recarrtypes[5][1]),
279 numpy.array(self.h, dtype=self.recarrtypes[6][1]),
280 ]
281 return a
282
284 self.__sanitycheck()
285 if self.imageId is None:
286 return None
287 else:
288 return len(self.imageId)
289
291 if size is None:
292 self.imageId = None
293 self.theZ = None
294 self.theT = None
295 self.x = None
296 self.y = None
297 self.w = None
298 self.h = None
299 else:
300 self.imageId = numpy.zeroes(size, dtype = self.recarrtypes[0][1])
301 self.theZ = numpy.zeroes(size, dtype = self.recarrtypes[1][1])
302 self.theT = numpy.zeroes(size, dtype = self.recarrtypes[2][1])
303 self.x = numpy.zeroes(size, dtype = self.recarrtypes[3][1])
304 self.y = numpy.zeroes(size, dtype = self.recarrtypes[4][1])
305 self.w = numpy.zeroes(size, dtype = self.recarrtypes[5][1])
306 self.h = numpy.zeroes(size, dtype = self.recarrtypes[6][1])
307
315
316 - def read(self, tbl, start, stop):
322
324 self.bytes = []
325 for idx in rowNumbers:
326 self.bytes.append(masks[idx].tolist())
327
329 rows = all_rows[self.name]
330
331 self.imageId = rows["i"].tolist()
332 self.theZ = rows["z"].tolist()
333 self.theT = rows["t"].tolist()
334 self.x = rows["x"]
335 self.y = rows["y"]
336 self.w = rows["w"]
337 self.h = rows["h"]
338
344
346 n = tbl._v_name
347 f = tbl._v_file
348 p = tbl._v_parent
349
350
351 try:
352 masks = getattr(p, "%s_masks" % n)
353 except tables.NoSuchNodeError:
354 masks = f.createVLArray(p, "%s_masks" % n, tables.UInt8Atom())
355 return masks
356
357
358
359
360
361
363
365 self.id = cls.ice_staticId()
366 self.f = f
367
370
373
375 ic.addObjectFactory(self, self.id)
376
377
378
379
380
381 ObjectFactories = {
382 FileColumnI: ObjectFactory(FileColumnI, lambda: FileColumnI()),
383 ImageColumnI: ObjectFactory(ImageColumnI, lambda: ImageColumnI()),
384 RoiColumnI: ObjectFactory(RoiColumnI, lambda: RoiColumnI()),
385 WellColumnI: ObjectFactory(WellColumnI, lambda: WellColumnI()),
386 BoolColumnI: ObjectFactory(BoolColumnI, lambda: BoolColumnI()),
387 DoubleColumnI: ObjectFactory(DoubleColumnI, lambda: DoubleColumnI()),
388 LongColumnI: ObjectFactory(LongColumnI, lambda: LongColumnI()),
389 StringColumnI: ObjectFactory(StringColumnI, lambda: StringColumnI()),
390 MaskColumnI: ObjectFactory(MaskColumnI, lambda: MaskColumnI())
391 }
392