-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.yaml
More file actions
214 lines (210 loc) · 7.19 KB
/
Copy pathexample.yaml
File metadata and controls
214 lines (210 loc) · 7.19 KB
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
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: hera-example-
annotations:
workflows.argoproj.io/description: |-
Replicates the functionality of
example.yaml
workflows.argoproj.io/title: example remade via hera
workflows.diamond.ac.uk/repository: https://github.com/DiamondLightSource/python-interface-to-workflows
labels:
workflows.diamond.ac.uk/science-group-examples: 'true'
spec:
entrypoint: workflowentry
templates:
- name: workflowentry
dag:
tasks:
- name: install
template: install-dependencies
- name: params
template: generate-parameters
arguments:
parameters:
- name: png
value: 'True'
- name: jpg
value: 'True'
- name: jpeg
value: 'True'
- name: tif
value: 'True'
- name: tiff
value: 'True'
- name: create-image
depends: install && params
template: create-image
withParam: '{{tasks.params.outputs.parameters.out-parameters}}'
arguments:
parameters:
- name: width
value: '{{item.width}}'
- name: height
value: '{{item.height}}'
- name: weights
value: '{{item.weights}}'
- name: extension
value: '{{item.extension}}'
- name: to-hdf5
depends: create-image
template: to-hdf5
arguments:
parameters:
- name: paths
value: '{{tasks.create-image.outputs.parameters.out-paths}}'
- name: install-dependencies
script:
image: python:3.10
source: |-
import os
import sys
sys.path.append(os.getcwd())
import subprocess
print('creating venv')
subprocess.check_call(['python', '-m', 'venv', '/tmp/venv'])
subprocess.check_call(['/tmp/venv/bin/pip', 'install', 'pillow', 'h5py', 'numpy', 'hera'])
command:
- python
volumeMounts:
- name: tmpdir
mountPath: /tmp
- name: generate-parameters
inputs:
parameters:
- name: png
- name: jpg
- name: jpeg
- name: tif
- name: tiff
outputs:
parameters:
- name: out-parameters
valueFrom:
path: /tmp/parameters.json
script:
image: python:3.10
source: |-
import os
import sys
sys.path.append(os.getcwd())
import json
try: jpeg = json.loads(r'''{{inputs.parameters.jpeg}}''')
except: jpeg = r'''{{inputs.parameters.jpeg}}'''
try: jpg = json.loads(r'''{{inputs.parameters.jpg}}''')
except: jpg = r'''{{inputs.parameters.jpg}}'''
try: png = json.loads(r'''{{inputs.parameters.png}}''')
except: png = r'''{{inputs.parameters.png}}'''
try: tif = json.loads(r'''{{inputs.parameters.tif}}''')
except: tif = r'''{{inputs.parameters.tif}}'''
try: tiff = json.loads(r'''{{inputs.parameters.tiff}}''')
except: tiff = r'''{{inputs.parameters.tiff}}'''
import json
params: list[dict[str, int | list[int] | str] | None] = [{'width': 500, 'height': 500, 'weights': [255, 1, 100], 'extension': 'png'} if png.lower() == 'true' else None, {'width': 600, 'height': 200, 'weights': [100, 150, 100], 'extension': 'jpg'} if jpg.lower() == 'true' else None, {'width': 300, 'height': 400, 'weights': [100, 150, 100], 'extension': 'jpeg'} if jpeg.lower() == 'true' else None, {'width': 300, 'height': 200, 'weights': [230, 100, 1], 'extension': 'tif'} if tif.lower() == 'true' else None, {'width': 200, 'height': 300, 'weights': [230, 100, 1], 'extension': 'tiff'} if tiff.lower() == 'true' else None]
params_to_write: list[dict[str, int | list[int] | str]] = [image_params for image_params in params if image_params is not None]
with open('/tmp/parameters.json', 'w') as f:
json.dump(params_to_write, f)
command:
- python
volumeMounts:
- name: tmpdir
mountPath: /tmp
- name: create-image
inputs:
parameters:
- name: width
- name: height
- name: weights
- name: extension
outputs:
artifacts:
- name: '{{inputs.parameters.extension}}-image'
path: /tmp/{{inputs.parameters.extension}}-image.{{inputs.parameters.extension}}
archive:
none: {}
parameters:
- name: out-paths
valueFrom:
path: /tmp/{{inputs.parameters.extension}}-path.json
script:
image: python:3.10
source: |-
import os
import sys
sys.path.append(os.getcwd())
import json
try: extension = json.loads(r'''{{inputs.parameters.extension}}''')
except: extension = r'''{{inputs.parameters.extension}}'''
try: height = json.loads(r'''{{inputs.parameters.height}}''')
except: height = r'''{{inputs.parameters.height}}'''
try: weights = json.loads(r'''{{inputs.parameters.weights}}''')
except: weights = r'''{{inputs.parameters.weights}}'''
try: width = json.loads(r'''{{inputs.parameters.width}}''')
except: width = r'''{{inputs.parameters.width}}'''
import json
from PIL import Image
def create_pattern(width: int, height: int, weights: tuple[int, int, int]) -> Image.Image:
print(f'width: {width}')
print(f'height: {height}')
print(f'RBG weights: {weights}')
image = Image.new('RGB', (width, height))
pixels = image.load()
for i in range(width):
for j in range(height):
pixels[i, j] = ((i + j * 50) % weights[0], weights[1], (i * 300 + j) % weights[2])
return image
image = create_pattern(width, height, weights)
path = f'/tmp/{extension}-image.{extension}'
image.save(path)
with open(f'/tmp/{extension}-path.json', 'w') as f:
json.dump(path, f)
command:
- /tmp/venv/bin/python
volumeMounts:
- name: tmpdir
mountPath: /tmp
- name: to-hdf5
inputs:
parameters:
- name: paths
outputs:
artifacts:
- name: hdf5output
path: /tmp/images.hdf5
archive:
none: {}
script:
image: python:3.10
source: |-
import os
import sys
sys.path.append(os.getcwd())
import json
try: paths = json.loads(r'''{{inputs.parameters.paths}}''')
except: paths = r'''{{inputs.parameters.paths}}'''
import h5py
import numpy as np
from PIL import Image
print('creating hdf5 file')
with h5py.File('/tmp/images.hdf5', 'w') as f:
for i, path in enumerate(paths):
path = path.strip('"')
print(f'Got {path}')
with Image.open(path) as image:
arr = np.array(image)
f.create_dataset(f'image_{i}', data=arr, dtype=arr.dtype)
print('done')
command:
- /tmp/venv/bin/python
volumeMounts:
- name: tmpdir
mountPath: /tmp
volumeClaimTemplates:
- metadata:
name: tmpdir
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi