/
set_timestamps.py
129 lines (107 loc) · 4.89 KB
/
set_timestamps.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# -----------------------------------------------------------------------------
# Copyright (C) 2017 University of Dundee. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ------------------------------------------------------------------------------
"""
This script sets the timestamps for all images within a specified dataset
for users user-1 through user-50
"""
import argparse
import omero
from omero.gateway import BlitzGateway
from omero.model.enums import UnitsTime
from omero.model import PlaneInfoI
from omero.model import PixelsI
from omero.model import TimeI
from omero.rtypes import rint, unwrap
def run(password, target, host, port):
for i in range(1, 51):
username = "user-%s" % i
print(username)
conn = BlitzGateway(username, password, host=host, port=port)
try:
conn.connect()
params = omero.sys.ParametersI()
params.addString('username', username)
query = "from Dataset where name='%s' \
AND details.owner.omeName=:username" % target
query_service = conn.getQueryService()
datasets = query_service.findAllByQuery(query, params,
conn.SERVICE_OPTS)
if len(datasets) == 0:
print("No datasets with name %s found" % target)
continue
dataset_id = datasets[0].getId().getValue()
print('dataset', dataset_id)
params2 = omero.sys.ParametersI()
params2.addId(dataset_id)
query = "select l.child.id from DatasetImageLink l \
where l.parent.id = :id"
images = query_service.projection(query, params2,
conn.SERVICE_OPTS)
for k in range(0, len(images)):
image_id = images[k][0].getValue()
delta_t = 300
image = conn.getObject("Image", image_id)
params = omero.sys.ParametersI()
params.addLong('pid', image.getPixelsId())
query = "from PlaneInfo as Info where \
Info.theZ=0 and Info.theC=0 and pixels.id=:pid"
info_list = query_service.findAllByQuery(query, params,
conn.SERVICE_OPTS)
print('info_list', len(info_list))
if len(info_list) == 0:
print("Creating info...", image.getSizeT())
info_list = []
for t_index in range(image.getSizeT()):
print(' t', t_index)
info = PlaneInfoI()
info.theT = rint(t_index)
info.theZ = rint(0)
info.theC = rint(0)
info.pixels = PixelsI(image.getPixelsId(), False)
dt = t_index * delta_t
info.deltaT = TimeI(dt, UnitsTime.SECOND)
info_list.append(info)
else:
for info in info_list:
unwrap_t = unwrap(info.theT)
unwrap_z = unwrap(info.theZ)
print('theT %s, theZ %s' % (unwrap_t, unwrap_z))
t_index = info.theT.getValue()
dt = t_index * delta_t
info.deltaT = TimeI(dt, UnitsTime.SECOND)
print("Saving info_list", len(info_list))
conn.getUpdateService().saveArray(info_list)
except Exception as exc:
print("Error when setting the timestamps: %s" % str(exc))
finally:
conn.close()
def main(args):
parser = argparse.ArgumentParser()
parser.add_argument('password')
parser.add_argument('target')
parser.add_argument('--server', default="workshop.openmicroscopy.org",
help="OMERO server hostname")
parser.add_argument('--port', default=4064, help="OMERO server port")
args = parser.parse_args(args)
run(args.password, args.target, args.server, args.port)
if __name__ == '__main__':
import sys
main(sys.argv[1:])