-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrapper.py
110 lines (86 loc) · 4.47 KB
/
wrapper.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
import os
import sys
from subprocess import call
from cytomine import CytomineJob
from cytomine.models import Annotation, Job, ImageGroupCollection, AnnotationCollection,AttachedFile
from workflow import workflow
#from shapely.affinity import affine_transform
#from skimage import io
#from mask_to_objects import mask_to_objects_2d
def main(argv):
# 0. Initialize Cytomine client and job
with CytomineJob.from_cli(argv) as cj:
cj.job.update(status=Job.RUNNING, progress=0, statusComment="Initialisation...")
# 1. Create working directories on the machine:
# - WORKING_PATH/in: input images
# - WORKING_PATH/out: output images
# - WORKING_PATH/ground_truth: ground truth images
base_path = "{}".format(os.getenv("HOME"))
gt_suffix = "_lbl"
working_path = os.path.join(base_path, str(cj.job.id))
in_path = os.path.join(working_path, "in")
out_path = os.path.join(working_path, "out")
gt_path = os.path.join(working_path, "ground_truth")
if not os.path.exists(working_path):
os.makedirs(working_path)
os.makedirs(in_path)
os.makedirs(out_path)
os.makedirs(gt_path)
# 2. Download the images (first input, then ground truth image)
cj.job.update(progress=1, statusComment="Downloading images (to {})...".format(in_path))
image_group = ImageGroupCollection().fetch_with_filter("project", cj.parameters.cytomine_id_project)
input_images = [i for i in image_group if gt_suffix not in i.name]
gt_images = [i for i in image_group if gt_suffix in i.name]
for input_image in input_images:
input_image.download(os.path.join(in_path, "{id}.tif"))
for gt_image in gt_images:
related_name = gt_image.name.replace(gt_suffix, '')
related_image = [i for i in input_images if related_name == i.name]
if len(related_image) == 1:
gt_image.download(os.path.join(gt_path, "{}.tif".format(related_image[0].id)))
# 3. Call the image analysis workflow using the run script
cj.job.update(progress=25, statusComment="Launching workflow...")
#TODO: error handling
workflow(in_path, out_path)
# if return_code != 0:
# err_desc = "Failed to execute the ImageJ macro (return code: {})".format(return_code)
# cj.job.update(progress=50, statusComment=err_desc)
# raise ValueError(err_desc)
# 4. Upload .swc and attach to correponding image
# ! not needed if we compute directly the metric
for image in cj.monitor(input_images, start=60, end=80, period=0.1, prefix="Extracting and uploading polygons from masks"):
afile = "{}.swc".format(image.id)
path = os.path.join(out_path, afile)
AttachedFile( image,
filename=path
).upload()
# 4. Upload the annotation and labels to Cytomine (annotations are extracted from the mask using
# the AnnotationExporter module)
# for image in cj.monitor(input_images, start=60, end=80, period=0.1, prefix="Extracting and uploading polygons from masks"):
# file = "{}.tif".format(image.id)
# path = os.path.join(out_path, file)
# data = io.imread(path)
# extract objects
# slices = mask_to_objects_2d(data)
# print("Found {} polygons in this image {}.".format(len(slices), image.id))
# upload
# collection = AnnotationCollection()
# for obj_slice in slices:
# collection.append(Annotation(
# location=affine_transform(obj_slice.polygon, [1, 0, 0, -1, 0, image.height]).wkt,
# id_image=image.id, id_project=cj.parameters.cytomine_id_project, property=[
# {"key": "index", "value": str(obj_slice.label)}
# ]
# ))
# collection.save()
# 5. Compute the metrics
cj.job.update(progress=80, statusComment="Computing metrics...")
# TODO: compute metrics:
# in /out: output files {id}.tiff
# in /ground_truth: label files {id}.tiff
cj.job.update(progress=99, statusComment="Cleaning...")
for image in input_images:
os.remove(os.path.join(in_path, "{}.tif".format(image.id)))
cj.job.update(status=Job.TERMINATED, progress=100, statusComment="Finished.")
if __name__ == "__main__":
main(sys.argv[1:])