-
Notifications
You must be signed in to change notification settings - Fork 0
/
iomero.py
695 lines (626 loc) · 27.4 KB
/
iomero.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#!/usr/bin/env python
"""
iomero.py
=========
A module for interfacing with OMERO (I/O), including both interactive
work and command line usage. Configure by editing SETUP CONSTANTS.
Create an Omg OMERO gateway instance to interact with OMERO.
The Im class provides easy access to image pixel data and core metadata.
"""
__author__ = "Graeme Ball ([email protected])"
__copyright__ = "Copyright (c) 2013 Graeme Ball"
__license__ = "GPL" # http://www.gnu.org/licenses/gpl.txt
# SETUP CONSTANTS
OMERO_PYTHON = "/usr/local/Cellar/omero/5.0.0-rc1/lib/python"
#ICE_PATH = "no longer needed on Mac as of Ice3.5"
SERVER = "localhost" # default
PORT = 4064 # default
import os
import sys
import argparse
import pprint
import numpy as np
import getpass
sys.path.append(OMERO_PYTHON)
#sys.path.append(ICE_PATH)
import omero.cli
import omero.model
import omero.rtypes
from omero.gateway import BlitzGateway
from omero.util import script_utils
import pylab, matplotlib
class Omg(object):
"""
OMERO gateway that wraps Blitz gateway and CLI, intended for
scripting and interactive work.
Attributes
----------
conn : Blitz gateway connection
"""
def __init__(self, conn=None, user=None, server=SERVER, port=PORT, skey=None):
"""
Requires active Blitz connection OR username plus password or sesskey
"""
passwd = None
if skey is None:
passwd = getpass.getpass()
# TODO: clean this up!!
if conn is None and (user is None or (passwd is None and skey is None)):
raise ValueError("Bad parameters," + self.__init__.__doc__)
if conn is not None:
if conn.isConnected():
self.conn = conn
else:
raise ValueError("Cannot initialize with closed connection!")
else:
if passwd is not None:
self.conn = BlitzGateway(user, passwd, host=server, port=port)
self.conn.connect()
else:
self.conn = BlitzGateway(user, host=server, port=port)
self.conn.connect(skey)
if self.conn.isConnected():
self._server = self.conn.host
self._port = self.conn.port
self._user = self.conn.getUser().getName()
self._key = self.conn.getSession().getUuid().getValue()
print("Connected to {0} (port {1}) as {2}, session key={3}".format(
self._server, self._port, self._user, self._key))
else:
print("Failed to open connection :-(")
def ls(self, uid=None):
"""
Print groups, then projects/datasets/images for current group.
"""
print("Groups for {0}:-".format(self.conn.getUser().getName()))
for gid, gname in self._ls_groups():
print(" {0} ({1})".format(gname, str(gid)))
curr_grp = self.conn.getGroupFromContext()
gid, gname = curr_grp.getId(), curr_grp.getName()
print("\nData for current group, {0} ({1}):-".format(gname, gid))
if uid is None:
uid = self.conn.getUserId()
for pid, pname in self._ls_projects(uid=uid):
print(" Project: {0} ({1})".format(pname, str(pid)))
for did, dname in self._ls_datasets(pid):
print(" Dataset: {0} ({1})".format(dname, str(did)))
for iid, iname in self._ls_images(did):
print(" Image: {0} ({1})".format(iname, str(iid)))
# TODO, list orphaned Datasets and Images
def _ls_groups(self):
"""list groups (id, name) this session is a member of"""
groups = self.conn.getGroupsMemberOf()
return [(group.getId(), group.getName()) for group in groups]
def _ls_projects(self, uid=None):
"""list projects (id, name) in the current session group"""
if uid is not None:
projs = self.conn.listProjects(uid)
else:
projs = self.conn.listProjects()
return [(proj.getId(), proj.getName()) for proj in projs]
def _ls_datasets(self, proj_id):
"""list datasets (id, name) within the project id given"""
dsets = self.conn.getObject("Project", proj_id).listChildren()
return [(dset.getId(), dset.getName()) for dset in dsets]
def _ls_images(self, dset_id):
"""list images (id, name) within the dataset id given"""
imgs = self.conn.getObject("Dataset", dset_id).listChildren()
return [(img.getId(), img.getName()) for img in imgs]
def groups(self):
"""
List all groups with group Ids.
"""
groups = [group.getName() + " (" + str(group.getId()) + ")"
for group in self.conn.listGroups()]
return sorted(groups)
def users(self):
"""
List all users with user Ids.
"""
def isAd(user):
user = self.conn.getObject("Experimenter", oid=user.getId())
if user.isAdmin():
return "A"
else:
return ""
users = [user.getName() + " (" + str(user.getId()) + isAd(user) + ")"
for user in self.conn.findExperimenters()]
return sorted(users)
def roi_list(self, iid):
"""Return a list of ROI objects for the given image id iid."""
return self.conn.getRoiService().findByImage(iid, None).rois
def _roi_shape_list(self, rois):
"""Return a list of shapes present in a list of ROIs."""
all_shapes = []
for roi in rois:
all_shapes.extend(roi.copyShapes())
return all_shapes
def roi_disp(self, iid):
"""
Display human-readable list of ROIs for a given image id (iid).
"""
def _disp_shape_info(s):
x, y = s.getCx().getValue(), s.getCy().getValue()
def _get_coord(s, dim): # macro-tastic
if eval("s.getThe" + dim + "()") is not None:
return eval("s.getThe" + dim + "().getValue()")
else:
return 0 # if coord not defined
z, t, c = [_get_coord(s, dim) for dim in ["Z", "T", "C"]]
print " "*6 + "X,Y,Z,T,C=%.1f,%.1f,%d,%d,%d" % (x, y, z, t, c)
rois = self.roi_list(iid)
print "Image Id %d has %d ROIs:" % (iid, len(rois))
for roi in rois:
roi_id, shapes = roi.getId().getValue(), roi.copyShapes()
print " ROI Id: %d (%d shapes)" % (roi_id, len(shapes))
for shape in shapes:
shape_id = shape.getId().getValue()
shape_type = type(shape)
print " Shape %d %s" % (shape_id, str(shape_type))
print " comment: %s" % shape.getTextValue().getValue()
_disp_shape_info(shape)
def roi_copy(self, iid_src, iid_dest):
"""
Copy all ROIs from image iid_src to iid_dest.
"""
img_src = self.conn.getObject("Image", iid_src)
img_dest = self.conn.getObject("Image", iid_dest)
# TODO: warn if dimensions do not match
us = self.conn.getUpdateService() # TODO, all services as attrs?
def _clone_shape(shape):
# avert your eyes...
shape_type = type(shape)
new_shape = shape_type()
getters = [m for m in dir(shape) if m[0:3] == "get"]
getters.remove("getId")
getters.remove("getDetails")
for meth in getters:
stuff = eval("shape." + meth + "()")
eval("new_shape.set" + meth[3:] + "(stuff)")
return new_shape
for roi in self.roi_list(iid_src):
roi_new = omero.model.RoiI()
roi_new.setImage(img_dest._obj)
for shape in roi.copyShapes():
roi_new.addShape(_clone_shape(shape))
r = us.saveAndReturnObject(roi_new)
def roi_shape_move(self, iid, shape_id,
x=None, y=None, z=None, t=None, c=None,
dx=None, dy=None, dz=None, dt=None, dc=None):
"""
Move a shape specified by id:
x, y, ... are new values to overwrite existing coords,
dx, dy, ... are adjustments to existing coords.
"""
kwargs = {'x': x, 'y': y, 'z': z, 't': t, 'c': c,
'dx': dx, 'dy': dy, 'dz': dz, 'dt': dt, 'dc': dc}
setter = {'x': "setCx", 'y': "setCy", 'z': "setTheZ",
't': "setTheT", 'c': "setTheC"}
shape = filter(lambda x: x.getId().getValue() == shape_id,
self._roi_shape_list(self.roi_list(iid)))[0]
def _wrap(num):
if type(num) == float:
return omero.rtypes.rdouble(num) # cx and cy are RDouble
else:
return omero.rtypes.rint(num) # z is RInt (TODO: test t, c)
for k, v in kwargs.iteritems():
if v is not None:
if k[0] != 'd':
getattr(shape, setter[k])(_wrap(v))
else:
k = k[1:] # snip off the 'd'
# add the coord change to the existing coord
v += getattr(shape, "get" + setter[k][3:])().getValue()
getattr(shape, setter[k])(_wrap(v))
self.conn.getUpdateService().saveObject(shape)
def ustats(self):
"""
Report per user statistics: projects, datasets, images, bytes.
"""
users = [user for user in self.conn.findExperimenters()]
stats = []
tproj, tdset, tim, tbyte = 0, 0, 0, 0
for user in users:
nproj, ndset, nim, nbyte = 0, 0, 0, 0
for pid, pname in self._ls_projects(user.getId()):
nproj += 1
for did, dname in self._ls_datasets(pid):
ndset += 1
for iid, iname in self._ls_images(did):
nim += 1
nbyte += self._nbytes(iid)
stat = "{0} ({1}): ".format(user.getName(), user.getId())
stat += "{0} projects, {1} datasets, {2} images, {3} bytes".format(
nproj, ndset, nim, nbyte)
stats.append(stat)
tproj += nproj
tdset += ndset
tim += nim
tbyte += nbyte
stat = "~TOTAL: "
stat += "{0} projects, {1} datasets, {2} images, {3} GB".format(
tproj, tdset, tim, tbyte * 1.0 / 1E9)
stats.append(stat)
return sorted(stats)
def _nbytes(self, im_id):
"""Estimate number of bytes in an image"""
iobj = self.conn.getObject("Image", oid=im_id)
npix = iobj.getSizeC() * iobj.getSizeT() * iobj.getSizeZ()
npix *= iobj.getSizeY() * iobj.getSizeX()
bytes_per_pix = np.dtype(iobj.getPixelsType()).itemsize
return npix * bytes_per_pix
def chgrp(self, group_id):
"""
Change group for this session to the group_id given.
"""
self.conn.setGroupForSession(group_id)
def get(self, im_id, get_att=True):
"""
Download the specified image as an OME-TIFF to current directory,
with attachments also downloaded to folder: img_path + '_attachments'
Return : path to downloaded image
"""
img = self.conn.getObject("Image", oid=im_id)
img_name = self._unique_name(img.getName(), im_id)
img_path = os.path.join(os.getcwd(), img_name) + ".ome.tiff"
img_file = open(img_path, "wb")
fsize, blockgen = img.exportOmeTiff(bufsize=65536)
for block in blockgen:
img_file.write(block)
img_file.close()
fa_type = omero.model.FileAnnotationI
attachments = [ann for ann in img.listAnnotations()
if ann.OMERO_TYPE == fa_type]
if get_att and len(attachments) > 0:
att_dir = img_path + "_attachments"
os.mkdir(att_dir)
def download_attachment(att, att_dir):
"""download OMERO file annotation to att_dir"""
att_file = open(os.path.join(att_dir, att.getFileName()), "wb")
for att_chunk in att.getFileInChunks():
att_file.write(att_chunk)
att_file.close()
for att in attachments:
download_attachment(att, att_dir)
return img_path
def cp(self, im_id):
"""
Create a copy of an image in the same dataset, named name_id_CPY.
Set description to a note of original group,proj,dset,image
and return id of the new image copy.
"""
img = self.conn.getObject("Image", im_id)
# re-using PrimaryPixels means img_cpy is stuck in original group :-(
#zct = [(z, c, t) for t in range(img.getSizeT())
# for c in range(img.getSizeC())
# for z in range(img.getSizeZ())]
#pplanes = img.getPrimaryPixels().getPlanes(zct)
#name_cpy = self._unique_name(img.getName(), im_id) + "_CPY"
#img_cpy = self.conn.createImageFromNumpySeq(
# pplanes, name_cpy, sourceImageId=im_id)
# this is suboptimal...
im_path = self.get(im_id)
cpy_name = self._unique_name(img.getName(), im_id) + "_CPY"
img_cpy = self.put(im_path, name=cpy_name,
dataset=img.getParent().getId())
# identify original source image in description
origin = "\nImage: {0} ({1})".format(img.getName(), img.getId())
origin += "\nDataset: " + str(img.getParent().getName())
origin += "\nProject: " + str(img.getParent().getParent().getName())
origin += "\nGroup: " + self.conn.getGroupFromContext().getName()
self.describe(img_cpy, "Copied from..." + origin)
return img_cpy
def _unique_name(self, img_name, im_id):
"""Make unique name combining a file basename & OMERO Image id"""
path_and_base, ext = os.path.splitext(img_name)
base = os.path.basename(path_and_base) # name in OMERO can has path
return "{0}_{1}".format(base, str(im_id))
def dget(self, dataset_id):
"""
Download an entire OMERO Dataset to the current directory.
"""
downloads = []
wdir = os.getcwd()
dset_name = self.conn.getObject("Dataset", dataset_id).getName()
dset_path = os.path.join(wdir, dset_name + "_D" + str(dataset_id))
os.mkdir(dset_path)
os.chdir(dset_path)
for img_id, img_name in self._ls_images(dataset_id):
downloads.append(self.get(img_id))
os.chdir(wdir)
return downloads
def pget(self, project_id):
"""
Download an entire OMERO Project to the current directory.
"""
downloads = []
wdir = os.getcwd()
proj_name = self.conn.getObject("Project", project_id).getName()
proj_path = os.path.join(wdir, proj_name + "_P" + str(project_id))
os.mkdir(proj_path)
os.chdir(proj_path)
for dset_id, dset_name in self._ls_datasets(project_id):
downloads.extend(self.dget(dset_id))
os.chdir(wdir)
return downloads
def put(self, filename, name=None, dataset=None):
"""
Import filename using OMERO CLI, optionally with a specified name
to a specified dataset (dataset_id).
Return : OMERO image Id
"""
cli = omero.cli.CLI()
cli.loadplugins()
import_args = ["import"]
import_args.extend(["-s", str(self._server)])
import_args.extend(["-k", str(self._key)])
if dataset is not None:
import_args.extend(["-d", str(dataset)])
if name is not None:
import_args.extend(["-n", str(name)])
clio = "cli.out"
clie = "cli.err"
import_args.extend(["---errs=" + clie, "---file=" + clio, "--"])
import_args.append(filename)
cli.invoke(import_args, strict=True)
pix_id = int(open(clio, 'r').read().rstrip())
im_id = self.conn.getQueryService().get("Pixels", pix_id).image.id.val
os.remove(clio)
os.remove(clie)
return im_id
def describe(self, im_id, description):
"""
Append to image description.
"""
img = self.conn.getObject("Image", oid=im_id)
old_description = img.getDescription() or ""
img.setDescription(old_description + "\n" + description)
img.save()
def attach(self, im_id, attachments):
"""
Attach a list of files to an image.
"""
img = self.conn.getObject("Image", oid=im_id)
for attachment in attachments.split():
fann = self.conn.createFileAnnfromLocalFile(attachment)
img.linkAnnotation(fann)
img.save()
# TODO: ls_tags() and tag() methods?
def mkp(self, project_name, description=None):
"""
Make new OMERO project in current group, returning the new project Id.
"""
# see: omero/lib/python/omeroweb/webclient/controller/container.py
proj = omero.model.ProjectI()
proj.name = omero.rtypes.rstring(str(project_name))
if description is not None and description != "":
proj.description = omero.rtypes.rstring(str(description))
return self._save_and_return_id(proj)
def mkd(self, dataset_name, project_id=None, description=None):
"""
Make new OMERO dataset, returning the new dataset Id.
"""
dset = omero.model.DatasetI()
dset.name = omero.rtypes.rstring(str(dataset_name))
if description is not None and description != "":
dset.description = omero.rtypes.rstring(str(description))
if project_id is not None:
l_proj_dset = omero.model.ProjectDatasetLinkI()
proj = self.conn.getObject("Project", project_id)
l_proj_dset.setParent(proj._obj)
l_proj_dset.setChild(dset)
dset.addProjectDatasetLink(l_proj_dset)
return self._save_and_return_id(dset)
def _save_and_return_id(self, obj):
"""Save new omero object and return id assgined to it"""
# see: OmeroWebGateway.saveAndReturnId
# in: lib/python/omeroweb/webclient/webclient_gateway.py
u_s = self.conn.getUpdateService()
res = u_s.saveAndReturnObject(obj, self.conn.SERVICE_OPTS)
res.unload()
return res.id.val
def im(self, im_id):
"""
Return an Im object for the image id specified.
"""
img = self.conn.getObject("Image", im_id)
# build pixel np.ndarray
nx, ny = img.getSizeX(), img.getSizeY()
nz, nt, nc = img.getSizeZ(), img.getSizeT(), img.getSizeC()
planes = [(z, c, t) for c in range(nc)
for t in range(nt)
for z in range(nz)]
pix_gen = img.getPrimaryPixels().getPlanes(planes)
pix = np.array([i for i in pix_gen]).reshape((nc, nt, nz, ny, nx))
# initialize Im using pix and extracted metadata
meta = self._extract_meta(img, im_id)
return Im(pix=pix, meta=meta)
def _extract_meta(self, img, im_id):
"""Extract metadata attributes from OMERO Blitz gateway Image"""
meta = {}
meta['name'] = self._unique_name(img.getName(), im_id)
meta['description'] = img.getDescription()
def _extract_ch_info(ch):
"""extract core metadata for for channel, return as dict"""
ch_info = {'label': ch.getLabel()}
ch_info['ex_wave'] = ch.getExcitationWave()
ch_info['em_wave'] = ch.getEmissionWave()
ch_info['color'] = ch.getColor().getRGB()
return ch_info
meta['channels'] = [_extract_ch_info(ch) for ch in img.getChannels()]
meta['pixel_size'] = {'x': img.getPixelSizeX(),
'y': img.getPixelSizeY(),
'z': img.getPixelSizeZ(),
'units': "um"}
tag_type = omero.model.TagAnnotationI
tags = [ann for ann in img.listAnnotations()
if ann.OMERO_TYPE == tag_type]
meta['tags'] = {tag.getValue() + " (" + str(tag.getId()) + ")":
tag.getDescription() for tag in tags}
fa_type = omero.model.FileAnnotationI
attachments = [ann for ann in img.listAnnotations()
if ann.OMERO_TYPE == fa_type]
meta['attachments'] = [att.getFileName() + " (" + str(att.getId()) +
")" for att in attachments]
user_id = self.conn.getUser().getName() + " (" + \
str(self.conn.getUser().getId()) + ") @" + self.conn.host
meta_ext = {}
meta_ext['user_id'] = user_id
meta['meta_ext'] = meta_ext
# TODO: ROIs, display settings?
# objective: Image.loadOriginalMetadata()[1][find 'Lens ID Number'][1],
return meta
def imput(self, im, dataset_id=None):
"""
Create a new OMERO Image using an Im object, returning new image id.
"""
# see: omero/lib/python/omero/util/script_utils.py
# see: omero/lib/python/omeroweb/webclient/webclient_gateway.py
# see: https://gist.github.com/will-moore/4141708
if not isinstance(im, Im):
raise TypeError("first imput argument must be of type Im")
nc, nt, nz, ny, nx = im.shape
ch_nums = range(nc)
q_s = self.conn.getQueryService()
p_s = self.conn.getPixelsService()
c_s = self.conn.getContainerService()
u_s = self.conn.getUpdateService()
pu_s = self.conn.c.sf.createRawPixelsStore()
q_ptype = "from PixelsType as p where p.value='{0}'".format(
str(im.dtype))
pixelsType = q_s.findByQuery(q_ptype, None)
im_id = p_s.createImage(nx, ny, nz, nt, ch_nums, pixelsType,
im.name, im.description)
img_i = c_s.getImages("Image", [im_id.getValue()], None)[0]
img = self.conn.getObject("Image", im_id.getValue())
pix_id = img_i.getPrimaryPixels().getId().getValue()
pu_s.setPixelsId(pix_id, True)
for c in range(nc):
for t in range(nt):
for z in range(nz):
plane = im.pix[c, t, z, :, :]
# TODO, convert to createImageFromNumpySeq
script_utils.uploadPlaneByRow(pu_s, plane, z, c, t)
l_dset_im = omero.model.DatasetImageLinkI()
dset = self.conn.getObject("Dataset", dataset_id)
l_dset_im.setParent(dset._obj)
l_dset_im.setChild(img._obj)
self._update_meta(im, im_id)
u_s.saveObject(l_dset_im, self.conn.SERVICE_OPTS)
return im_id.getValue()
def _update_meta(self, im, im_id):
"""Set OMERO Image metadata using Im metadata"""
# TODO: store im metadata in OMERO image with im_id
# channels [{'label': label, 'ex_wave': ex_wave,
# 'em_wave': em_wave, 'color': RGB},...]
# pixel_size [{'x': psx, 'y': psy, 'z': psz, 'units': ?},...]
# tags {'tag1 (id1)': 'desc1',...}
# user_id
# objective, attachments, ROIs, display settings
class Im(object):
"""
Image object based on numpy ndarray, with easily accessible core metadata.
Attributes
----------
name: image name (string)
pix : numpy ndarray of pixel data
dim_order: "CTZYX" (fixed, all images are 5D)
dtype: numpy dtype for pixels
shape: pixel array shape tuple, (nc, nt, nz, ny, nx)
pixel_size: dict of pixel sizes and units
ch_info: list of channel info dicts
description: image description (string)
tags: dict of tag {'value': description} pairs
attachments: list of attachment files
meta_ext: dict of extended metadata {'label': value} pairs
"""
def __init__(self, pix=None, meta=None):
"""
Construct Im object using numpy ndarray (pix) and metadata dictionary.
Default is to construct array of zeros of given size, or 1x1x1x256x256.
"""
channels = [{'label': None, 'em_wave': None, 'ex_wave': None,
'color': None}]
pixel_size = {'x': 1, 'y': 1, 'z': 1, 'units': None}
default_meta = {'name': "Unnamed", 'dim_order': "CTZYX",
'dtype': np.uint8, 'shape': (1, 1, 1, 256, 256),
'pixel_size': pixel_size, 'channels': channels,
'description': "", 'tags': {}, 'attachments': [],
'meta_ext': {}}
for key in default_meta:
setattr(self, key, default_meta[key])
if meta is not None:
for key in meta:
setattr(self, key, meta[key])
if pix is None:
# default, construct empty 8-bit image according to dimensions
self.pix = np.zeros((self.nc, self.nt, self.nz, self.ny, self.nx),
dtype=np.uint8)
else:
if isinstance(pix, np.ndarray):
self.pix = pix
else:
raise TypeError("pix must be " + str(np.ndarray))
self.shape = pix.shape
self.dtype = pix.dtype
def __repr__(self):
im_repr = 'Im object "{0}"\n'.format(self.name)
im_repr += pprint.pformat(vars(self))
return im_repr
# custom ArgumentParser
class _FriendlyParser(argparse.ArgumentParser):
"""
Display help message upon incorrect args -- no unfriendly error messages.
"""
def error(self, message):
"""override parser error handling"""
print(self.print_help())
print(message)
sys.exit(2)
def _main():
"""'iomero': a command line OMERO importer with Blitz gateway extras"""
parser = _FriendlyParser(description=_main.__doc__)
parser.add_argument('server', help='OMERO server name / IP address')
parser.add_argument('-u', '--user', action="store", type=str,
required=True, help="OMERO experimenter name (username)")
credentials = parser.add_mutually_exclusive_group(required=True)
credentials.add_argument('-w', '--password', action="store", type=str,
help="OMERO experimenter password")
credentials.add_argument('-k', '--sesskey', action="store", type=str,
help="OMERO session key (replaces -w)")
parser.add_argument('-f', '--filename', action="store", type=str,
required=True, help="Image file to upload")
parser.add_argument('-d', '--dataset', action="store", type=str,
required=False, help="OMERO dataset Id to import image into")
parser.add_argument('-n', '--name', action="store", type=str,
required=False, help="Image name to use")
parser.add_argument('-i', '--info', action="store", type=str,
required=False, help="Image description to use")
parser.add_argument('-a', '--attach', action="store", type=str,
required=False, help='Attach files in quotes (e.g. "file1 file2")')
parser.add_argument('-p', '--port', action="store", type=int,
required=False, help="OMERO server port, defaults to ")
args = parser.parse_args()
# create an Omg OMERO gateway
init_args = {'server': args.server, 'user': args.user}
if args.password:
init_args['passwd'] = args.password
else:
init_args['skey'] = args.sesskey
if args.port:
init_args['port'] = args.port
omg = Omg(**init_args)
# handle file upload (file, dataset, name)
put_args = {'filename': args.filename}
if args.name:
put_args['name'] = args.name
if args.dataset:
put_args['dataset'] = args.dataset
im_id = omg.put(**put_args)
# handle annotation and attachments
if args.info:
omg.describe(im_id, args.info)
if args.attach:
omg.attach(im_id, args.attach)
if __name__ == "__main__":
_main()