Package omero :: Module columns
[hide private]
[frames] | no frames]

Source Code for Module omero.columns

  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") # Pytables 
 24      has_pytables = True 
 25  except ImportError: 
 26      has_pytables = False 
 27  
 
 28  
 
29 -def columns2definition(cols):
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 # Descriptions are handled separately 40 return definition
41
42 -class AbstractColumn(object):
43 """ 44 Base logic for all columns 45 """ 46
47 - def __init__(self):
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
61 - def append(self, tbl):
62 """ 63 Called by tables.py to give columns. By default, does nothing. 64 """ 65 pass
66
67 - def readCoordinates(self, tbl, rowNumbers):
68 if rowNumbers is None or len(rowNumbers) == 0: 69 rows = tbl.read() 70 else: 71 rows = tbl.readCoordinates(rowNumbers) 72 self.fromrows(rows)
73
74 - def read(self, tbl, start, stop):
75 rows = tbl.read(start, stop) 76 self.fromrows(rows)
77
78 - def getsize(self):
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
88 - def setsize(self, size):
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
98 - def names(self):
99 """ 100 Any method which does not use the "values" field 101 will need to override this method. 102 """ 103 return [self.name]
104
105 - def arrays(self):
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
112 - def fromrows(self, rows):
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 # WORKAROUND: http://www.zeroc.com/forums/bug-reports/4165-icepy-can-not-handle-buffers-longs-i64.html#post20468 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):
129 omero.grid.FileColumn.__init__(self, name, *args) 130 AbstractColumn.__init__(self)
131
132 - def descriptor(self, pos):
133 return tables.Int64Col(pos=pos)
134
135 -class ImageColumnI(AbstractColumn, omero.grid.ImageColumn):
136
137 - def __init__(self, name = "Unknown", *args):
138 omero.grid.ImageColumn.__init__(self, name, *args) 139 AbstractColumn.__init__(self)
140
141 - def descriptor(self, pos):
142 return tables.Int64Col(pos=pos)
143
144 -class WellColumnI(AbstractColumn, omero.grid.WellColumn):
145
146 - def __init__(self, name = "Unknown", *args):
147 omero.grid.WellColumn.__init__(self, name, *args) 148 AbstractColumn.__init__(self)
149
150 - def descriptor(self, pos):
151 return tables.Int64Col(pos=pos)
152
153 -class RoiColumnI(AbstractColumn, omero.grid.RoiColumn):
154
155 - def __init__(self, name = "Unknown", *args):
156 omero.grid.RoiColumn.__init__(self, name, *args) 157 AbstractColumn.__init__(self)
158
159 - def descriptor(self, pos):
160 return tables.Int64Col(pos=pos)
161
162 -class ImageColumnI(AbstractColumn, omero.grid.ImageColumn):
163
164 - def __init__(self, name = "Unknown", *args):
165 omero.grid.ImageColumn.__init__(self, name, *args) 166 AbstractColumn.__init__(self)
167
168 - def descriptor(self, pos):
169 return tables.Int64Col(pos=pos)
170
171 -class BoolColumnI(AbstractColumn, omero.grid.BoolColumn):
172
173 - def __init__(self, name = "Unknown", *args):
174 omero.grid.BoolColumn.__init__(self, name, *args) 175 AbstractColumn.__init__(self)
176
177 - def descriptor(self, pos):
178 return tables.BoolCol(pos=pos)
179
180 -class DoubleColumnI(AbstractColumn, omero.grid.DoubleColumn):
181
182 - def __init__(self, name = "Unknown", *args):
183 omero.grid.DoubleColumn.__init__(self, name, *args) 184 AbstractColumn.__init__(self)
185
186 - def descriptor(self, pos):
187 return tables.Float64Col(pos=pos)
188
189 -class LongColumnI(AbstractColumn, omero.grid.LongColumn):
190
191 - def __init__(self, name = "Unknown", *args):
192 omero.grid.LongColumn.__init__(self, name, *args) 193 AbstractColumn.__init__(self)
194
195 - def descriptor(self, pos):
196 return tables.Int64Col(pos=pos)
197
198 -class StringColumnI(AbstractColumn, omero.grid.StringColumn):
199
200 - def __init__(self, name = "Unknown", *args):
201 omero.grid.StringColumn.__init__(self, name, *args) 202 AbstractColumn.__init__(self)
203
204 - def descriptor(self, pos):
205 return tables.StringCol(pos=pos)
206
207 -class MaskColumnI(AbstractColumn, omero.grid.MaskColumn):
208
209 - def __init__(self, name = "Unknown", *args):
210 omero.grid.MaskColumn.__init__(self, name, *args) 211 AbstractColumn.__init__(self)
212
213 - def __noneorsame(self, a, b):
214 if a is None: 215 if b is None: 216 return 217 # a not none 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
223 - def __sanitycheck(self):
224 self.__noneorsame(self.imageId, self.theZ) 225 self.__noneorsame(self.imageId, self.theT) 226 self.__noneorsame(self.imageId, self.x) 227 self.__noneorsame(self.imageId, self.y) 228 self.__noneorsame(self.imageId, self.w) 229 self.__noneorsame(self.imageId, self.h) 230 self.__noneorsame(self.imageId, self.bytes)
231
232 - def descriptor(self, pos):
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
244 - def names(self):
245 return [x[0] for x in self.recarrtypes]
246
247 - def arrays(self):
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
260 - def getsize(self):
261 self.__sanitycheck() 262 if self.imageId is None: 263 return None 264 else: 265 return len(self.imageId)
266
267 - def setsize(self, size):
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
285 - def readCoordinates(self, tbl, rowNumbers):
286 self.__sanitycheck() 287 AbstractColumn.readCoordinates(self, tbl, rowNumbers) # calls fromrows 288 masks = self._getmasks(tbl) 289 if rowNumbers is None or len(rowNumbers) == 0: 290 rowNumbers = range(masks.nrows) 291 self.getbytes(masks, rowNumbers)
292
293 - def read(self, tbl, start, stop):
294 self.__sanitycheck() 295 AbstractColumn.read(self, tbl, start, stop) # calls fromrows 296 masks = self._getmasks(tbl) 297 rowNumbers = range(start, stop) 298 self.getbytes(masks, rowNumbers)
299
300 - def getbytes(self, masks, rowNumbers):
301 self.bytes = [] 302 for idx in rowNumbers: 303 self.bytes.append(masks[idx].tolist())
304
305 - def fromrows(self, all_rows):
306 rows = all_rows[self.name] 307 # WORKAROUND: http://www.zeroc.com/forums/bug-reports/4165-icepy-can-not-handle-buffers-longs-i64.html#post20468 308 self.imageId = rows["i"].tolist() 309 self.theZ = rows["z"].tolist() # ticket:1665 310 self.theT = rows["t"].tolist() # ticket:1665 311 self.x = rows["x"] 312 self.y = rows["y"] 313 self.w = rows["w"] 314 self.h = rows["h"]
315
316 - def append(self, tbl):
317 self.__sanitycheck() 318 masks = self._getmasks(tbl) 319 for x in self.bytes: 320 masks.append(numpy.fromstring(x, count=len(x), dtype=tables.UInt8Atom()))
321
322 - def _getmasks(self, tbl):
323 n = tbl._v_name 324 f = tbl._v_file 325 p = tbl._v_parent 326 # http://www.zeroc.com/doc/Ice-3.3.1/manual/Slice.5.8.html#200 327 # Ice::Byte can be -128 to 127 OR 0 to 255, but using UInt8 for the moment 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 # Helpers 335 # ======================================================================== 336 337 # Conversion classes are for omero.model <--> ome.model only (no python) 338
339 -class ObjectFactory(Ice.ObjectFactory):
340
341 - def __init__(self, cls, f):
342 self.id = cls.ice_staticId() 343 self.f = f
344
345 - def create(self, string):
346 return self.f()
347
348 - def destroy(self):
349 pass
350
351 - def register(self, ic):
352 ic.addObjectFactory(self, self.id)
353 354 355 # Object factories 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