generated from ome/guide-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
public_s3_segmentation_parallel.py
143 lines (112 loc) · 3.76 KB
/
public_s3_segmentation_parallel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
# Copyright (c) 2020 University of Dundee.
#
# 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.
# FPBioimage was originally published in
# <https://www.nature.com/nphoton/journal/v11/n2/full/nphoton.2016.273.html>.
#
# 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/>.
#
# Version: 1.0
#
import dask
import dask.array as da
import dask_image.ndfilters
import dask_image.ndmeasure
import matplotlib.pyplot as plt
import numpy
from omero.gateway import BlitzGateway
import time
# Dask array loaded from S3
data = None
# Connect to the server
def connect(hostname, username, password):
conn = BlitzGateway(username, password,
host=hostname, secure=True)
print("Connected: %s" % conn.connect())
conn.c.enableKeepAlive(60)
return conn
# Load-image
def load_image(conn, image_id):
return conn.getObject('Image', image_id)
# Load-binary
def load_binary_from_s3(id, resolution='4'):
endpoint_url = 'https://s3.embassy.ebi.ac.uk/'
root = 'idr/zarr/v0.1/%s.zarr/%s/' % (id, resolution)
return da.from_zarr(endpoint_url + root)
# Segment-image
def analyze(t, c, z):
plane = data[t, c, z, :, :]
smoothed_image = dask_image.ndfilters.gaussian_filter(plane, sigma=[1, 1])
threshold_value = 0.75 * da.max(smoothed_image).compute()
threshold_image = smoothed_image > threshold_value
label_image, num_labels = dask_image.ndmeasure.label(threshold_image)
name = 't:%s, c: %s, z:%s' % (t, c, z)
print("Plane coordinates: %s" % name)
ref = 't_%s_c_%s_z_%s' % (t, c, z)
return label_image, ref
# Prepare-call
def prepare_call(image):
middle_z = image.getSizeZ() // 2
middle_t = image.getSizeT() // 2
range_t = 5
range_z = 5
number_c = image.getSizeC()
lazy_results = []
for t in range(middle_t - range_t, middle_t + range_t):
for z in range(middle_z - range_z, middle_z + range_z):
for c in range(number_c):
lazy_result = dask.delayed(analyze)(t, c, z)
lazy_results.append(lazy_result)
return lazy_results
# Compute
def compute(lazy_results):
return dask.compute(*lazy_results)
# Disconnect
def disconnect(conn):
conn.close()
# Save the first 5 results on disk
def save_results(results):
print("Saving locally the first 5 results as png")
for r, name in results[:5]:
array = numpy.asarray(r)
value = "image_%s.png" % name
plt.imsave(value, array)
# main
def main():
# Collect user credentials
try:
host = "ws://idr.openmicroscopy.org/omero-ws"
username = "public"
password = "public"
image_id = "4007801"
# Connect to the server
conn = connect(host, username, password)
# Load the image
image = load_image(conn, image_id)
global data
data = load_binary_from_s3(image_id)
print("Dask array: %s" % data)
lazy_results = prepare_call(image)
start = time.time()
results = compute(lazy_results)
elapsed = time.time() - start
print('Compute time (in seconds): %s' % elapsed)
save_results(results)
finally:
disconnect(conn)
print('done')
if __name__ == "__main__":
main()