-
Notifications
You must be signed in to change notification settings - Fork 100
/
marshal.py
474 lines (434 loc) · 18.2 KB
/
marshal.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2014 University of Dundee & Open Microscopy Environment.
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import omero
import time
import re
import logging
import traceback
logger = logging.getLogger(__name__)
from omero.rtypes import unwrap
# OMERO.insight point list regular expression
INSIGHT_POINT_LIST_RE = re.compile(r'points\[([^\]]+)\]')
# OME model point list regular expression
OME_MODEL_POINT_LIST_RE = re.compile(r'([\d.]+),([\d.]+)')
def channelMarshal(channel):
"""
return a dict with all there is to know about a channel
@param channel: L{omero.gateway.ChannelWrapper}
@return: Dict
"""
return {'emissionWave': channel.getEmissionWave(),
'label': channel.getLabel(),
'color': channel.getColor().getHtml(),
'window': {'min': channel.getWindowMin(),
'max': channel.getWindowMax(),
'start': channel.getWindowStart(),
'end': channel.getWindowEnd()},
'active': channel.isActive()}
def imageMarshal(image, key=None, request=None):
"""
return a dict with pretty much everything we know and care about an image,
all wrapped in a pretty structure.
@param image: L{omero.gateway.ImageWrapper}
@param key: key of specific attributes to select
@return: Dict
"""
image.loadRenderOptions()
pr = image.getProject()
ds = None
wellsample = None
well = None
try:
# Replicating the functionality of the deprecated
# ImageWrapper.getDataset() with shares in mind.
# -- Tue Sep 6 10:48:47 BST 2011 (See #6660)
parents = image.listParents()
if parents is not None and len(parents) == 1:
if parents[0].OMERO_CLASS == 'Dataset':
ds = parents[0]
elif parents[0].OMERO_CLASS == 'WellSample':
wellsample = parents[0]
if wellsample.well is not None:
well = wellsample.well
except omero.SecurityViolation, e:
# We're in a share so the Image's parent Dataset cannot be loaded
# or some other permissions related issue has tripped us up.
logger.warn('Security violation while retrieving Dataset when '
'marshaling image metadata: %s' % e.message)
rv = {
'id': image.id,
'meta': {
'imageName': image.name or '',
'imageDescription': image.description or '',
'imageAuthor': image.getAuthor(),
'projectName': pr and pr.name or 'Multiple',
'projectId': pr and pr.id or None,
'projectDescription': pr and pr.description or '',
'datasetName': ds and ds.name or 'Multiple',
'datasetId': ds and ds.id or None,
'datasetDescription': ds and ds.description or '',
'wellSampleId': wellsample and wellsample.id or '',
'wellId': well and well.id.val or '',
'imageTimestamp': time.mktime(
image.getDate().timetuple()),
'imageId': image.id,
'pixelsType': image.getPixelsType(),
},
'perms': {
'canAnnotate': image.canAnnotate(),
'canEdit': image.canEdit(),
'canDelete': image.canDelete(),
'canLink': image.canLink()
}
}
try:
reOK = image._prepareRenderingEngine()
if not reOK:
logger.debug(
"Failed to prepare Rendering Engine for imageMarshal")
return rv
except omero.ConcurrencyException, ce:
backOff = ce.backOff
rv = {
'ConcurrencyException': {
'backOff': backOff
}
}
return rv
except Exception, ex: # Handle everything else.
rv['Exception'] = ex.message
logger.error(traceback.format_exc())
return rv # Return what we have already, in case it's useful
# big images
tiles = image._re.requiresPixelsPyramid()
rv['tiles'] = tiles
if (tiles):
width, height = image._re.getTileSize()
levels = image._re.getResolutionLevels()
zoomLevelScaling = image.getZoomLevelScaling()
rv.update({'tile_size': {'width': width,
'height': height},
'levels': levels})
if zoomLevelScaling is not None:
rv['zoomLevelScaling'] = zoomLevelScaling
nominalMagnification = image.getObjectiveSettings() is not None \
and image.getObjectiveSettings().getObjective().getNominalMagnification() \
or None
try:
server_settings = request.session.get('server_settings', {}) \
.get('viewer', {})
except:
server_settings = {}
init_zoom = server_settings.get('initial_zoom_level', 0)
if init_zoom < 0:
init_zoom = levels + init_zoom
interpolate = server_settings.get('interpolate_pixels', True)
try:
def pixel_size_in_microns(method):
try:
size = method('MICROMETER')
return size.getValue() if size else None
except:
logger.debug(
'Unable to convert physical pixel size to microns',
exc_info=True
)
return None
rv.update({
'interpolate': interpolate,
'size': {'width': image.getSizeX(),
'height': image.getSizeY(),
'z': image.getSizeZ(),
't': image.getSizeT(),
'c': image.getSizeC()},
'pixel_size': {'x': pixel_size_in_microns(image.getPixelSizeX),
'y': pixel_size_in_microns(image.getPixelSizeY),
'z': pixel_size_in_microns(image.getPixelSizeZ)},
})
if init_zoom is not None:
rv['init_zoom'] = init_zoom
if nominalMagnification is not None:
rv.update({'nominalMagnification': nominalMagnification})
try:
rv['pixel_range'] = image.getPixelRange()
rv['channels'] = map(lambda x: channelMarshal(x),
image.getChannels())
rv['split_channel'] = image.splitChannelDims()
rv['rdefs'] = {'model': (image.isGreyscaleRenderingModel() and
'greyscale' or 'color'),
'projection': image.getProjection(),
'defaultZ': image._re.getDefaultZ(),
'defaultT': image._re.getDefaultT(),
'invertAxis': image.isInvertedAxis()}
except TypeError:
# Will happen if an image has bad or missing pixel data
logger.error('imageMarshal', exc_info=True)
rv['pixel_range'] = (0, 0)
rv['channels'] = ()
rv['split_channel'] = ()
rv['rdefs'] = {'model': 'color',
'projection': image.getProjection(),
'defaultZ': 0,
'defaultT': 0,
'invertAxis': image.isInvertedAxis()}
except AttributeError:
rv = None
raise
if key is not None and rv is not None:
for k in key.split('.'):
rv = rv.get(k, {})
if rv == {}:
rv = None
return rv
def shapeMarshal(shape):
"""
return a dict with all there is to know about a shape
@param channel: L{omero.model.ShapeI}
@return: Dict
"""
rv = {}
def set_if(k, v, func=lambda a: a is not None):
"""
Sets the key L{k} with the value of L{v} if the unwrapped value L{v}
passed to L{func} evaluates to True. In the default case this is
True if the unwrapped value L{v} is not None.
"""
v = unwrap(v)
if func(v):
rv[k] = v
rv['id'] = shape.getId().getValue()
set_if('theT', shape.getTheT())
set_if('theZ', shape.getTheZ())
shape_type = type(shape)
if shape_type == omero.model.RectangleI:
rv['type'] = 'Rectangle'
rv['x'] = shape.getX().getValue()
rv['y'] = shape.getY().getValue()
rv['width'] = shape.getWidth().getValue()
rv['height'] = shape.getHeight().getValue()
elif shape_type == omero.model.MaskI:
rv['type'] = 'Mask'
rv['x'] = shape.getX().getValue()
rv['y'] = shape.getY().getValue()
rv['width'] = shape.getWidth().getValue()
rv['height'] = shape.getHeight().getValue()
# TODO: support for mask
elif shape_type == omero.model.EllipseI:
rv['type'] = 'Ellipse'
rv['cx'] = shape.getCx().getValue()
rv['cy'] = shape.getCy().getValue()
rv['rx'] = shape.getRx().getValue()
rv['ry'] = shape.getRy().getValue()
elif shape_type == omero.model.PolylineI:
rv['type'] = 'PolyLine'
rv['points'] = stringToSvg(shape.getPoints().getValue())
elif shape_type == omero.model.LineI:
rv['type'] = 'Line'
rv['x1'] = shape.getX1().getValue()
rv['x2'] = shape.getX2().getValue()
rv['y1'] = shape.getY1().getValue()
rv['y2'] = shape.getY2().getValue()
elif shape_type == omero.model.PointI:
rv['type'] = 'Point'
rv['cx'] = shape.getCx().getValue()
rv['cy'] = shape.getCy().getValue()
elif shape_type == omero.model.PolygonI:
rv['type'] = 'Polygon'
# z = closed line
rv['points'] = stringToSvg(shape.getPoints().getValue()) + " z"
elif shape_type == omero.model.LabelI:
rv['type'] = 'Label'
rv['x'] = shape.getX().getValue()
rv['y'] = shape.getY().getValue()
else:
logger.debug("Shape type not supported: %s" % str(shape_type))
text_value = unwrap(shape.getTextValue())
if text_value is not None:
# only populate json with font styles if we have some text
rv['textValue'] = text_value
# FIXME: units ignored for font size
if shape.getFontSize() is not None:
set_if('fontSize', shape.getFontSize().getValue())
set_if('fontStyle', shape.getFontStyle())
set_if('fontFamily', shape.getFontFamily())
set_if('transform', shape.getTransform(),
func=lambda a: a is not None and a != 'None')
fill_color = unwrap(shape.getFillColor())
if fill_color is not None:
rv['fillColor'], rv['fillAlpha'] = rgb_int2css(fill_color)
stroke_color = unwrap(shape.getStrokeColor())
if stroke_color is not None:
rv['strokeColor'], rv['strokeAlpha'] = rgb_int2css(stroke_color)
if shape.getStrokeWidth() is not None:
# FIXME: units ignored for stroke width
set_if('strokeWidth', shape.getStrokeWidth().getValue())
return rv
def stringToSvg(string):
"""
Method for converting the string returned from
omero.model.ShapeI.getPoints() into an SVG for display on web.
E.g: "points[309,427, 366,503, 190,491] points1[309,427, 366,503, 190,491]
points2[309,427, 366,503, 190,491]"
To: M 309 427 L 366 503 L 190 491 z
"""
point_list = string.strip()
match = INSIGHT_POINT_LIST_RE.search(point_list)
if match is not None:
point_list = match.group(1)
point_list = OME_MODEL_POINT_LIST_RE.findall(point_list)
if len(point_list) == 0:
logger.error("Unrecognised ROI shape 'points' string: %r" % string)
return ""
point_list = ' L '.join([' '.join(point) for point in point_list])
return "M %s" % point_list
def rgb_int2css(rgbint):
"""
converts a bin int number into css colour and alpha fraction.
E.g. -1006567680 to '#00ff00', 0.5
"""
alpha = rgbint // 256 // 256 // 256 % 256
alpha = float(alpha) / 256
r, g, b = (rgbint // 256 // 256 % 256, rgbint // 256 % 256, rgbint % 256)
return "#%02x%02x%02x" % (r, g, b), alpha
def rgb_int2rgba(rgbint):
"""
converts a bin int number into (r, g, b, alpha) tuple.
E.g. 1694433280 to (255, 0, 0, 0.390625)
"""
alpha = rgbint // 256 // 256 // 256 % 256
alpha = float(alpha) / 256
r, g, b = (rgbint // 256 // 256 % 256, rgbint // 256 % 256, rgbint % 256)
return (r, g, b, alpha)
def chgrpMarshal(conn, rsp):
"""
Helper for marshalling a Chgrp response.
Uses conn to lookup unlinked objects.
Returns dict of e.g.
{'includedObjects': {'Datasets':[1,2,3]},
'unlinkedDetails': {'Tags':[{'id':1, 'name':'t'}]}
}
"""
rv = {}
if isinstance(rsp, omero.cmd.ERR):
rsp_params = ", ".join(["%s: %s" % (k, v) for k, v in
rsp.parameters.items()])
rv['error'] = rsp.message
rv['report'] = "%s %s" % (rsp.name, rsp_params)
else:
included = rsp.responses[0].includedObjects
deleted = rsp.responses[0].deletedObjects
# Included: just simplify the key, e.g. -> Projects, Datasets etc
includedObjects = {}
objKeys = ['ome.model.containers.Project',
'ome.model.containers.Dataset',
'ome.model.core.Image',
'ome.model.screen.Screen',
'ome.model.screen.Plate',
'ome.model.screen.Well']
for k in objKeys:
if k in included:
otype = k.split(".")[-1]
oids = included[k]
oids.sort() # makes testing easier
includedObjects[otype + 's'] = oids
rv['includedObjects'] = includedObjects
# Annotation links - need to get info on linked objects
tags = {}
files = {}
comments = 0
others = 0
annotationLinks = ['ome.model.annotations.ProjectAnnotationLink',
'ome.model.annotations.DatasetAnnotationLink',
'ome.model.annotations.ImageAnnotationLink',
'ome.model.annotations.ScreenAnnotationLink',
'ome.model.annotations.PlateAnnotationLink',
'ome.model.annotations.WellAnnotationLink']
for l in annotationLinks:
if l in deleted:
linkType = l.split(".")[-1]
params = omero.sys.ParametersI()
params.addIds(deleted[l])
query = ("select annLink from %s as annLink "
"join fetch annLink.child as ann "
"left outer join fetch ann.file "
"where annLink.id in (:ids)" % linkType)
links = conn.getQueryService().findAllByQuery(
query, params, conn.SERVICE_OPTS)
for lnk in links:
ann = lnk.child
if isinstance(ann, omero.model.FileAnnotationI):
name = unwrap(ann.getFile().getName())
files[ann.id.val] = {'id': ann.id.val,
'name': name}
elif isinstance(ann, omero.model.TagAnnotationI):
name = unwrap(ann.getTextValue())
tags[ann.id.val] = {'id': ann.id.val,
'name': name}
elif isinstance(ann, omero.model.CommentAnnotationI):
comments += 1
else:
others += 1
# sort tags & comments
tags = tags.values()
tags.sort(key=lambda x: x['name'])
files = files.values()
files.sort(key=lambda x: x['name'])
rv['unlinkedDetails'] = {'Tags': tags,
'Files': files,
'Comments': comments,
'Others': others
}
# Container links - only report these if we are moving the *parent*,
# E.g. DatasetImageLinks are only reported if we are moving the Dataset
# (and the image is left behind). If we were moving the Image then we'd
# expect the link to be broken (can ignore)
objects = {}
containerLinks = {
'ome.model.containers.ProjectDatasetLink': 'Datasets',
'ome.model.containers.DatasetImageLink': 'Images',
'ome.model.screen.ScreenPlateLink': 'Screens'}
for l, ch in containerLinks.items():
if l in deleted:
linkType = l.split(".")[-1]
params = omero.sys.ParametersI()
params.addIds(deleted[l])
query = ("select conLink from %s as conLink "
"join fetch conLink.child as ann "
"where conLink.id in (:ids)" % linkType)
links = conn.getQueryService().findAllByQuery(
query, params, conn.SERVICE_OPTS)
for lnk in links:
child = lnk.child
if (ch not in includedObjects or
child.id.val not in includedObjects[ch]):
name = unwrap(child.getName())
# Put objects in a dictionary to avoid duplicates
if ch not in objects:
objects[ch] = {}
# E.g. objects['Dataset']['1'] = {}
objects[ch][child.id.val] = {'id': child.id.val,
'name': name}
# sort objects
for otype, objs in objects.items():
objs = objs.values()
objs.sort(key=lambda x: x['name'])
# E.g. 'Dataset' objects in 'Datasets'
rv['unlinkedDetails'][otype] = objs
return rv