fractal-server 1.4.10__py3-none-any.whl → 2.0.0a0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. fractal_server/__init__.py +1 -1
  2. fractal_server/app/models/__init__.py +4 -7
  3. fractal_server/app/models/linkuserproject.py +9 -0
  4. fractal_server/app/models/security.py +6 -0
  5. fractal_server/app/models/state.py +1 -1
  6. fractal_server/app/models/v1/__init__.py +10 -0
  7. fractal_server/app/models/{dataset.py → v1/dataset.py} +5 -5
  8. fractal_server/app/models/{job.py → v1/job.py} +5 -5
  9. fractal_server/app/models/{project.py → v1/project.py} +5 -5
  10. fractal_server/app/models/{task.py → v1/task.py} +7 -2
  11. fractal_server/app/models/{workflow.py → v1/workflow.py} +5 -5
  12. fractal_server/app/models/v2/__init__.py +20 -0
  13. fractal_server/app/models/v2/dataset.py +55 -0
  14. fractal_server/app/models/v2/job.py +51 -0
  15. fractal_server/app/models/v2/project.py +31 -0
  16. fractal_server/app/models/v2/task.py +93 -0
  17. fractal_server/app/models/v2/workflow.py +43 -0
  18. fractal_server/app/models/v2/workflowtask.py +90 -0
  19. fractal_server/app/routes/{admin.py → admin/v1.py} +42 -42
  20. fractal_server/app/routes/admin/v2.py +275 -0
  21. fractal_server/app/routes/api/v1/__init__.py +7 -7
  22. fractal_server/app/routes/api/v1/_aux_functions.py +2 -2
  23. fractal_server/app/routes/api/v1/dataset.py +37 -37
  24. fractal_server/app/routes/api/v1/job.py +12 -12
  25. fractal_server/app/routes/api/v1/project.py +23 -21
  26. fractal_server/app/routes/api/v1/task.py +24 -14
  27. fractal_server/app/routes/api/v1/task_collection.py +16 -14
  28. fractal_server/app/routes/api/v1/workflow.py +24 -24
  29. fractal_server/app/routes/api/v1/workflowtask.py +10 -10
  30. fractal_server/app/routes/api/v2/__init__.py +28 -0
  31. fractal_server/app/routes/api/v2/_aux_functions.py +497 -0
  32. fractal_server/app/routes/api/v2/apply.py +220 -0
  33. fractal_server/app/routes/api/v2/dataset.py +310 -0
  34. fractal_server/app/routes/api/v2/images.py +212 -0
  35. fractal_server/app/routes/api/v2/job.py +200 -0
  36. fractal_server/app/routes/api/v2/project.py +205 -0
  37. fractal_server/app/routes/api/v2/task.py +222 -0
  38. fractal_server/app/routes/api/v2/task_collection.py +229 -0
  39. fractal_server/app/routes/api/v2/workflow.py +398 -0
  40. fractal_server/app/routes/api/v2/workflowtask.py +269 -0
  41. fractal_server/app/routes/aux/_job.py +1 -1
  42. fractal_server/app/runner/async_wrap.py +27 -0
  43. fractal_server/app/runner/exceptions.py +129 -0
  44. fractal_server/app/runner/executors/local/__init__.py +3 -0
  45. fractal_server/app/runner/{_local → executors/local}/executor.py +2 -2
  46. fractal_server/app/runner/executors/slurm/__init__.py +3 -0
  47. fractal_server/app/runner/{_slurm → executors/slurm}/_batching.py +1 -1
  48. fractal_server/app/runner/{_slurm → executors/slurm}/_check_jobs_status.py +1 -1
  49. fractal_server/app/runner/{_slurm → executors/slurm}/_executor_wait_thread.py +1 -1
  50. fractal_server/app/runner/{_slurm → executors/slurm}/_slurm_config.py +3 -152
  51. fractal_server/app/runner/{_slurm → executors/slurm}/_subprocess_run_as_user.py +1 -1
  52. fractal_server/app/runner/{_slurm → executors/slurm}/executor.py +9 -9
  53. fractal_server/app/runner/filenames.py +6 -0
  54. fractal_server/app/runner/set_start_and_last_task_index.py +39 -0
  55. fractal_server/app/runner/task_files.py +105 -0
  56. fractal_server/app/runner/{__init__.py → v1/__init__.py} +24 -22
  57. fractal_server/app/runner/{_common.py → v1/_common.py} +13 -120
  58. fractal_server/app/runner/{_local → v1/_local}/__init__.py +6 -6
  59. fractal_server/app/runner/{_local → v1/_local}/_local_config.py +6 -7
  60. fractal_server/app/runner/{_local → v1/_local}/_submit_setup.py +1 -5
  61. fractal_server/app/runner/v1/_slurm/__init__.py +310 -0
  62. fractal_server/app/runner/{_slurm → v1/_slurm}/_submit_setup.py +3 -9
  63. fractal_server/app/runner/v1/_slurm/get_slurm_config.py +163 -0
  64. fractal_server/app/runner/v1/common.py +117 -0
  65. fractal_server/app/runner/{handle_failed_job.py → v1/handle_failed_job.py} +8 -8
  66. fractal_server/app/runner/v2/__init__.py +337 -0
  67. fractal_server/app/runner/v2/_local/__init__.py +169 -0
  68. fractal_server/app/runner/v2/_local/_local_config.py +118 -0
  69. fractal_server/app/runner/v2/_local/_submit_setup.py +52 -0
  70. fractal_server/app/runner/v2/_slurm/__init__.py +157 -0
  71. fractal_server/app/runner/v2/_slurm/_submit_setup.py +83 -0
  72. fractal_server/app/runner/v2/_slurm/get_slurm_config.py +179 -0
  73. fractal_server/app/runner/v2/components.py +5 -0
  74. fractal_server/app/runner/v2/deduplicate_list.py +24 -0
  75. fractal_server/app/runner/v2/handle_failed_job.py +156 -0
  76. fractal_server/app/runner/v2/merge_outputs.py +41 -0
  77. fractal_server/app/runner/v2/runner.py +264 -0
  78. fractal_server/app/runner/v2/runner_functions.py +339 -0
  79. fractal_server/app/runner/v2/runner_functions_low_level.py +134 -0
  80. fractal_server/app/runner/v2/task_interface.py +43 -0
  81. fractal_server/app/runner/v2/v1_compat.py +21 -0
  82. fractal_server/app/schemas/__init__.py +4 -42
  83. fractal_server/app/schemas/v1/__init__.py +42 -0
  84. fractal_server/app/schemas/{applyworkflow.py → v1/applyworkflow.py} +18 -18
  85. fractal_server/app/schemas/{dataset.py → v1/dataset.py} +30 -30
  86. fractal_server/app/schemas/{dumps.py → v1/dumps.py} +8 -8
  87. fractal_server/app/schemas/{manifest.py → v1/manifest.py} +5 -5
  88. fractal_server/app/schemas/{project.py → v1/project.py} +9 -9
  89. fractal_server/app/schemas/{task.py → v1/task.py} +12 -12
  90. fractal_server/app/schemas/{task_collection.py → v1/task_collection.py} +7 -7
  91. fractal_server/app/schemas/{workflow.py → v1/workflow.py} +38 -38
  92. fractal_server/app/schemas/v2/__init__.py +34 -0
  93. fractal_server/app/schemas/v2/dataset.py +88 -0
  94. fractal_server/app/schemas/v2/dumps.py +87 -0
  95. fractal_server/app/schemas/v2/job.py +113 -0
  96. fractal_server/app/schemas/v2/manifest.py +109 -0
  97. fractal_server/app/schemas/v2/project.py +36 -0
  98. fractal_server/app/schemas/v2/task.py +121 -0
  99. fractal_server/app/schemas/v2/task_collection.py +105 -0
  100. fractal_server/app/schemas/v2/workflow.py +78 -0
  101. fractal_server/app/schemas/v2/workflowtask.py +118 -0
  102. fractal_server/config.py +5 -4
  103. fractal_server/images/__init__.py +50 -0
  104. fractal_server/images/tools.py +86 -0
  105. fractal_server/main.py +11 -3
  106. fractal_server/migrations/versions/4b35c5cefbe3_tmp_is_v2_compatible.py +39 -0
  107. fractal_server/migrations/versions/56af171b0159_v2.py +217 -0
  108. fractal_server/migrations/versions/876f28db9d4e_tmp_split_task_and_wftask_meta.py +68 -0
  109. fractal_server/migrations/versions/974c802f0dd0_tmp_workflowtaskv2_type_in_db.py +37 -0
  110. fractal_server/migrations/versions/9cd305cd6023_tmp_workflowtaskv2.py +40 -0
  111. fractal_server/migrations/versions/a6231ed6273c_tmp_args_schemas_in_taskv2.py +42 -0
  112. fractal_server/migrations/versions/b9e9eed9d442_tmp_taskv2_type.py +37 -0
  113. fractal_server/migrations/versions/e3e639454d4b_tmp_make_task_meta_non_optional.py +50 -0
  114. fractal_server/tasks/__init__.py +0 -5
  115. fractal_server/tasks/endpoint_operations.py +13 -19
  116. fractal_server/tasks/utils.py +35 -0
  117. fractal_server/tasks/{_TaskCollectPip.py → v1/_TaskCollectPip.py} +3 -3
  118. fractal_server/tasks/{background_operations.py → v1/background_operations.py} +18 -50
  119. fractal_server/tasks/v1/get_collection_data.py +14 -0
  120. fractal_server/tasks/v2/_TaskCollectPip.py +103 -0
  121. fractal_server/tasks/v2/background_operations.py +382 -0
  122. fractal_server/tasks/v2/get_collection_data.py +14 -0
  123. {fractal_server-1.4.10.dist-info → fractal_server-2.0.0a0.dist-info}/METADATA +1 -1
  124. fractal_server-2.0.0a0.dist-info/RECORD +166 -0
  125. fractal_server/app/runner/_slurm/.gitignore +0 -2
  126. fractal_server/app/runner/_slurm/__init__.py +0 -150
  127. fractal_server/app/runner/common.py +0 -311
  128. fractal_server-1.4.10.dist-info/RECORD +0 -98
  129. /fractal_server/app/runner/{_slurm → executors/slurm}/remote.py +0 -0
  130. {fractal_server-1.4.10.dist-info → fractal_server-2.0.0a0.dist-info}/LICENSE +0 -0
  131. {fractal_server-1.4.10.dist-info → fractal_server-2.0.0a0.dist-info}/WHEEL +0 -0
  132. {fractal_server-1.4.10.dist-info → fractal_server-2.0.0a0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,264 @@
1
+ import json
2
+ from concurrent.futures import ThreadPoolExecutor
3
+ from copy import copy
4
+ from copy import deepcopy
5
+ from pathlib import Path
6
+ from typing import Callable
7
+ from typing import Optional
8
+
9
+ from ....images import Filters
10
+ from ....images import SingleImage
11
+ from ....images.tools import _filter_image_list
12
+ from ....images.tools import find_image_by_path
13
+ from ....images.tools import match_filter
14
+ from ..filenames import FILTERS_FILENAME
15
+ from ..filenames import HISTORY_FILENAME
16
+ from ..filenames import IMAGES_FILENAME
17
+ from .runner_functions import no_op_submit_setup_call
18
+ from .runner_functions import run_v1_task_parallel
19
+ from .runner_functions import run_v2_task_compound
20
+ from .runner_functions import run_v2_task_non_parallel
21
+ from .runner_functions import run_v2_task_parallel
22
+ from fractal_server.app.models.v2 import DatasetV2
23
+ from fractal_server.app.models.v2 import WorkflowTaskV2
24
+ from fractal_server.app.schemas.v2.dataset import _DatasetHistoryItemV2
25
+ from fractal_server.app.schemas.v2.workflowtask import WorkflowTaskStatusTypeV2
26
+
27
+ # FIXME: define RESERVED_ARGUMENTS = [", ...]
28
+
29
+
30
+ def execute_tasks_v2(
31
+ wf_task_list: list[WorkflowTaskV2],
32
+ dataset: DatasetV2,
33
+ executor: ThreadPoolExecutor,
34
+ workflow_dir: Path,
35
+ workflow_dir_user: Optional[Path] = None,
36
+ logger_name: Optional[str] = None,
37
+ submit_setup_call: Callable = no_op_submit_setup_call,
38
+ ) -> DatasetV2:
39
+
40
+ if not workflow_dir.exists(): # FIXME: this should have already happened
41
+ workflow_dir.mkdir()
42
+
43
+ # Initialize local dataset attributes
44
+ zarr_dir = dataset.zarr_dir
45
+ tmp_images = deepcopy(dataset.images)
46
+ tmp_filters = deepcopy(dataset.filters)
47
+ tmp_history = []
48
+
49
+ for wftask in wf_task_list:
50
+ task = wftask.task
51
+
52
+ # PRE TASK EXECUTION
53
+
54
+ # Get filtered images
55
+ pre_type_filters = copy(tmp_filters["types"])
56
+ pre_type_filters.update(wftask.input_filters["types"])
57
+ pre_attribute_filters = copy(tmp_filters["attributes"])
58
+ pre_attribute_filters.update(wftask.input_filters["attributes"])
59
+ filtered_images = _filter_image_list(
60
+ images=tmp_images,
61
+ filters=Filters(
62
+ types=pre_type_filters,
63
+ attributes=pre_attribute_filters,
64
+ ),
65
+ )
66
+ # Verify that filtered images comply with task input_types
67
+ for image in filtered_images:
68
+ if not match_filter(image, Filters(types=task.input_types)):
69
+ raise ValueError(
70
+ f"Filtered images include {image.dict()}, which does "
71
+ f"not comply with {task.input_types=}."
72
+ )
73
+
74
+ # TASK EXECUTION (V2)
75
+ if not wftask.is_legacy_task:
76
+ if task.type == "non_parallel":
77
+ current_task_output = run_v2_task_non_parallel(
78
+ images=filtered_images,
79
+ zarr_dir=zarr_dir,
80
+ wftask=wftask,
81
+ task=wftask.task,
82
+ workflow_dir=workflow_dir,
83
+ workflow_dir_user=workflow_dir_user,
84
+ executor=executor,
85
+ logger_name=logger_name,
86
+ submit_setup_call=submit_setup_call,
87
+ )
88
+ elif task.type == "parallel":
89
+ current_task_output = run_v2_task_parallel(
90
+ images=filtered_images,
91
+ wftask=wftask,
92
+ task=wftask.task,
93
+ workflow_dir=workflow_dir,
94
+ workflow_dir_user=workflow_dir_user,
95
+ executor=executor,
96
+ logger_name=logger_name,
97
+ submit_setup_call=submit_setup_call,
98
+ )
99
+ elif task.type == "compound":
100
+ current_task_output = run_v2_task_compound(
101
+ images=filtered_images,
102
+ zarr_dir=zarr_dir,
103
+ wftask=wftask,
104
+ task=wftask.task,
105
+ workflow_dir=workflow_dir,
106
+ workflow_dir_user=workflow_dir_user,
107
+ executor=executor,
108
+ logger_name=logger_name,
109
+ submit_setup_call=submit_setup_call,
110
+ )
111
+ else:
112
+ raise ValueError(f"Invalid {task.type=}.")
113
+ # TASK EXECUTION (V1)
114
+ else:
115
+ current_task_output = run_v1_task_parallel(
116
+ images=filtered_images,
117
+ wftask=wftask,
118
+ task_legacy=wftask.task_legacy,
119
+ executor=executor,
120
+ logger_name=logger_name,
121
+ submit_setup_call=submit_setup_call,
122
+ )
123
+
124
+ # POST TASK EXECUTION
125
+
126
+ # Update image list
127
+ current_task_output.check_paths_are_unique()
128
+ for image_obj in current_task_output.image_list_updates:
129
+ image = image_obj.dict()
130
+ # Edit existing image
131
+ if image["path"] in [_image["path"] for _image in tmp_images]:
132
+ if (
133
+ image["origin"] is not None
134
+ and image["origin"] != image["path"]
135
+ ):
136
+ raise ValueError(
137
+ f"Trying to edit an image with {image['path']=} "
138
+ f"and {image['origin']=}."
139
+ )
140
+ image_search = find_image_by_path(
141
+ images=tmp_images,
142
+ path=image["path"],
143
+ )
144
+ if image_search is None:
145
+ raise ValueError(
146
+ f"Image with path {image['path']} not found, while "
147
+ "updating image list."
148
+ )
149
+ original_img = image_search["image"]
150
+ original_index = image_search["index"]
151
+ updated_attributes = copy(original_img["attributes"])
152
+ updated_types = copy(original_img["types"])
153
+
154
+ # Update image attributes/types with task output and manifest
155
+ updated_attributes.update(image["attributes"])
156
+ updated_types.update(image["types"])
157
+ updated_types.update(task.output_types)
158
+
159
+ # Update image in the dataset image list
160
+ tmp_images[original_index]["attributes"] = updated_attributes
161
+ tmp_images[original_index]["types"] = updated_types
162
+ # Add new image
163
+ else:
164
+ # Check that image['path'] is relative to zarr_dir
165
+ if not image["path"].startswith(zarr_dir):
166
+ raise ValueError(
167
+ f"{zarr_dir} is not a parent directory of "
168
+ f"{image['path']}"
169
+ )
170
+ # Propagate attributes and types from `origin` (if any)
171
+ updated_attributes = {}
172
+ updated_types = {}
173
+ if image["origin"] is not None:
174
+ image_search = find_image_by_path(
175
+ images=tmp_images,
176
+ path=image["origin"],
177
+ )
178
+ if image_search is not None:
179
+ original_img = image_search["image"]
180
+ updated_attributes = copy(original_img["attributes"])
181
+ updated_types = copy(original_img["types"])
182
+ # Update image attributes/types with task output and manifest
183
+ updated_attributes.update(image["attributes"])
184
+ updated_types.update(image["types"])
185
+ updated_types.update(task.output_types)
186
+ new_image = SingleImage(
187
+ path=image["path"],
188
+ origin=image["origin"],
189
+ attributes=updated_attributes,
190
+ types=updated_types,
191
+ )
192
+ # Add image into the dataset image list
193
+ tmp_images.append(new_image.dict())
194
+
195
+ # Remove images from tmp_images
196
+ for image in current_task_output.image_list_removals:
197
+ image_search = find_image_by_path(
198
+ images=tmp_images, path=image["path"]
199
+ )
200
+ if image_search["index"] is None:
201
+ raise
202
+ else:
203
+ tmp_images.pop(image_search["index"])
204
+
205
+ # Update filters.attributes:
206
+ # current + (task_output: not really, in current examples..)
207
+ if current_task_output.filters is not None:
208
+ tmp_filters["attributes"].update(
209
+ current_task_output.filters.attributes
210
+ )
211
+
212
+ # Update filters.types: current + (task_output + task_manifest)
213
+ if wftask.is_legacy_task:
214
+ types_from_manifest = {}
215
+ else:
216
+ types_from_manifest = task.output_types
217
+ if current_task_output.filters is not None:
218
+ types_from_task = current_task_output.filters.types
219
+ else:
220
+ types_from_task = {}
221
+ # Check that key sets are disjoint
222
+ set_types_from_manifest = set(types_from_manifest.keys())
223
+ set_types_from_task = set(types_from_task.keys())
224
+ if not set_types_from_manifest.isdisjoint(set_types_from_task):
225
+ overlap = set_types_from_manifest.intersection(set_types_from_task)
226
+ raise ValueError(
227
+ "Both task and task manifest did set the same"
228
+ f"output type. Overlapping keys: {overlap}."
229
+ )
230
+ # Update filters.types
231
+ tmp_filters["types"].update(types_from_manifest)
232
+ tmp_filters["types"].update(types_from_task)
233
+
234
+ # Update history (based on _DatasetHistoryItemV2)
235
+ history_item = _DatasetHistoryItemV2(
236
+ workflowtask=wftask,
237
+ status=WorkflowTaskStatusTypeV2.DONE,
238
+ parallelization=dict(
239
+ # task_type=wftask.task.type, # FIXME: breaks for V1 tasks
240
+ # component_list=fil, #FIXME
241
+ ),
242
+ ).dict()
243
+ tmp_history.append(history_item)
244
+
245
+ # Write current dataset attributes (history, images, filters) into
246
+ # temporary files which can be used (1) to retrieve the latest state
247
+ # when the job fails, (2) from within endpoints that need up-to-date
248
+ # information
249
+ with open(workflow_dir / HISTORY_FILENAME, "w") as f:
250
+ json.dump(tmp_history, f, indent=2)
251
+ with open(workflow_dir / FILTERS_FILENAME, "w") as f:
252
+ json.dump(tmp_filters, f, indent=2)
253
+ with open(workflow_dir / IMAGES_FILENAME, "w") as f:
254
+ json.dump(tmp_images, f, indent=2)
255
+
256
+ # NOTE: tmp_history only contains the newly-added history items (to be
257
+ # appended to the original history), while tmp_filters and tmp_images
258
+ # represent the new attributes (to replace the original ones)
259
+ result = dict(
260
+ history=tmp_history,
261
+ filters=tmp_filters,
262
+ images=tmp_images,
263
+ )
264
+ return result
@@ -0,0 +1,339 @@
1
+ import functools
2
+ import traceback
3
+ from concurrent.futures import Executor
4
+ from pathlib import Path
5
+ from typing import Callable
6
+ from typing import Literal
7
+ from typing import Optional
8
+
9
+ from ....images import SingleImage
10
+ from .deduplicate_list import deduplicate_list
11
+ from .merge_outputs import merge_outputs
12
+ from .runner_functions_low_level import run_single_task
13
+ from .task_interface import InitArgsModel
14
+ from .task_interface import InitTaskOutput
15
+ from .task_interface import TaskOutput
16
+ from .v1_compat import convert_v2_args_into_v1
17
+ from fractal_server.app.models.v1 import Task as TaskV1
18
+ from fractal_server.app.models.v2 import TaskV2
19
+ from fractal_server.app.models.v2 import WorkflowTaskV2
20
+ from fractal_server.app.runner.v2.components import _COMPONENT_KEY_
21
+ from fractal_server.app.runner.v2.components import _index_to_component
22
+
23
+
24
+ __all__ = [
25
+ "run_v2_task_non_parallel",
26
+ "run_v2_task_parallel",
27
+ "run_v2_task_compound",
28
+ "run_v1_task_parallel",
29
+ ]
30
+
31
+ MAX_PARALLELIZATION_LIST_SIZE = 20_000
32
+
33
+
34
+ def no_op_submit_setup_call(
35
+ *,
36
+ wftask: WorkflowTaskV2,
37
+ workflow_dir: Path,
38
+ workflow_dir_user: Path,
39
+ which_type: Literal["non_parallel", "parallel"],
40
+ ) -> dict:
41
+ """
42
+ Default (no-operation) interface of submit_setup_call in V2.
43
+ """
44
+ return {}
45
+
46
+
47
+ # Backend-specific configuration
48
+ def _get_executor_options(
49
+ *,
50
+ wftask: WorkflowTaskV2,
51
+ workflow_dir: Path,
52
+ workflow_dir_user: Path,
53
+ submit_setup_call: Callable,
54
+ which_type: Literal["non_parallel", "parallel"],
55
+ ) -> dict:
56
+ try:
57
+ options = submit_setup_call(
58
+ wftask=wftask,
59
+ workflow_dir=workflow_dir,
60
+ workflow_dir_user=workflow_dir_user,
61
+ which_type=which_type,
62
+ )
63
+ except Exception as e:
64
+ tb = "".join(traceback.format_tb(e.__traceback__))
65
+ raise RuntimeError(
66
+ f"{type(e)} error in {submit_setup_call=}\n"
67
+ f"Original traceback:\n{tb}"
68
+ )
69
+ return options
70
+
71
+
72
+ def _check_parallelization_list_size(my_list):
73
+ if len(my_list) > MAX_PARALLELIZATION_LIST_SIZE:
74
+ raise ValueError(
75
+ "Too many parallelization items.\n"
76
+ f" {len(my_list)}\n"
77
+ f" {MAX_PARALLELIZATION_LIST_SIZE=}\n"
78
+ )
79
+
80
+
81
+ def run_v2_task_non_parallel(
82
+ *,
83
+ images: list[SingleImage],
84
+ zarr_dir: str,
85
+ task: TaskV2,
86
+ wftask: WorkflowTaskV2,
87
+ workflow_dir: Path,
88
+ workflow_dir_user: Optional[Path] = None,
89
+ executor: Executor,
90
+ logger_name: Optional[str] = None,
91
+ submit_setup_call: Callable = no_op_submit_setup_call,
92
+ ) -> TaskOutput:
93
+ """
94
+ This runs server-side (see `executor` argument)
95
+ """
96
+
97
+ if not workflow_dir_user:
98
+ workflow_dir_user = workflow_dir
99
+
100
+ executor_options = _get_executor_options(
101
+ wftask=wftask,
102
+ workflow_dir=workflow_dir,
103
+ workflow_dir_user=workflow_dir_user,
104
+ submit_setup_call=submit_setup_call,
105
+ which_type="non_parallel",
106
+ )
107
+
108
+ function_kwargs = dict(
109
+ paths=[image["path"] for image in images],
110
+ zarr_dir=zarr_dir,
111
+ **(wftask.args_non_parallel or {}),
112
+ )
113
+ future = executor.submit(
114
+ functools.partial(
115
+ run_single_task,
116
+ wftask=wftask,
117
+ command=task.command_non_parallel,
118
+ workflow_dir=workflow_dir,
119
+ workflow_dir_user=workflow_dir_user,
120
+ ),
121
+ function_kwargs,
122
+ **executor_options,
123
+ )
124
+ output = future.result()
125
+ # FIXME V2: handle validation errors
126
+ if output is None:
127
+ return TaskOutput()
128
+ else:
129
+ validated_output = TaskOutput(**output)
130
+ return validated_output
131
+
132
+
133
+ def run_v2_task_parallel(
134
+ *,
135
+ images: list[SingleImage],
136
+ task: TaskV2,
137
+ wftask: WorkflowTaskV2,
138
+ executor: Executor,
139
+ workflow_dir: Path,
140
+ workflow_dir_user: Optional[Path] = None,
141
+ logger_name: Optional[str] = None,
142
+ submit_setup_call: Callable = no_op_submit_setup_call,
143
+ ) -> TaskOutput:
144
+
145
+ _check_parallelization_list_size(images)
146
+
147
+ executor_options = _get_executor_options(
148
+ wftask=wftask,
149
+ workflow_dir=workflow_dir,
150
+ workflow_dir_user=workflow_dir_user,
151
+ submit_setup_call=submit_setup_call,
152
+ which_type="parallel",
153
+ )
154
+
155
+ list_function_kwargs = []
156
+ for ind, image in enumerate(images):
157
+ list_function_kwargs.append(
158
+ dict(
159
+ path=image["path"],
160
+ **(wftask.args_parallel or {}),
161
+ ),
162
+ )
163
+ list_function_kwargs[-1][_COMPONENT_KEY_] = _index_to_component(ind)
164
+
165
+ results_iterator = executor.map(
166
+ functools.partial(
167
+ run_single_task,
168
+ wftask=wftask,
169
+ command=task.command_parallel,
170
+ workflow_dir=workflow_dir,
171
+ workflow_dir_user=workflow_dir_user,
172
+ ),
173
+ list_function_kwargs,
174
+ **executor_options,
175
+ )
176
+ # Explicitly iterate over the whole list, so that all futures are waited
177
+ outputs = list(results_iterator)
178
+
179
+ # Validate all non-None outputs
180
+ for ind, output in enumerate(outputs):
181
+ if output is None:
182
+ outputs[ind] = TaskOutput()
183
+ else:
184
+ # FIXME: improve handling of validation errors
185
+ validated_output = TaskOutput(**output)
186
+ outputs[ind] = validated_output
187
+
188
+ merged_output = merge_outputs(outputs)
189
+ return merged_output
190
+
191
+
192
+ def run_v2_task_compound(
193
+ *,
194
+ images: list[SingleImage],
195
+ zarr_dir: str,
196
+ task: TaskV2,
197
+ wftask: WorkflowTaskV2,
198
+ executor: Executor,
199
+ workflow_dir: Path,
200
+ workflow_dir_user: Optional[Path] = None,
201
+ logger_name: Optional[str] = None,
202
+ submit_setup_call: Callable = no_op_submit_setup_call,
203
+ ) -> TaskOutput:
204
+
205
+ executor_options_init = _get_executor_options(
206
+ wftask=wftask,
207
+ workflow_dir=workflow_dir,
208
+ workflow_dir_user=workflow_dir_user,
209
+ submit_setup_call=submit_setup_call,
210
+ which_type="non_parallel",
211
+ )
212
+ executor_options_compute = _get_executor_options(
213
+ wftask=wftask,
214
+ workflow_dir=workflow_dir,
215
+ workflow_dir_user=workflow_dir_user,
216
+ submit_setup_call=submit_setup_call,
217
+ which_type="parallel",
218
+ )
219
+
220
+ # 3/A: non-parallel init task
221
+ function_kwargs = dict(
222
+ paths=[image["path"] for image in images],
223
+ zarr_dir=zarr_dir,
224
+ **(wftask.args_non_parallel or {}),
225
+ )
226
+ future = executor.submit(
227
+ functools.partial(
228
+ run_single_task,
229
+ wftask=wftask,
230
+ command=task.command_non_parallel,
231
+ workflow_dir=workflow_dir,
232
+ workflow_dir_user=workflow_dir_user,
233
+ ),
234
+ function_kwargs,
235
+ **executor_options_init,
236
+ )
237
+ output = future.result()
238
+ if output is None:
239
+ init_task_output = InitTaskOutput()
240
+ else:
241
+ init_task_output = InitTaskOutput(**output)
242
+ parallelization_list = init_task_output.parallelization_list
243
+ parallelization_list = deduplicate_list(
244
+ parallelization_list, PydanticModel=InitArgsModel
245
+ )
246
+
247
+ # 3/B: parallel part of a compound task
248
+ _check_parallelization_list_size(parallelization_list)
249
+
250
+ list_function_kwargs = []
251
+ for ind, parallelization_item in enumerate(parallelization_list):
252
+ list_function_kwargs.append(
253
+ dict(
254
+ path=parallelization_item.path,
255
+ init_args=parallelization_item.init_args,
256
+ **(wftask.args_parallel or {}),
257
+ ),
258
+ )
259
+ list_function_kwargs[-1][_COMPONENT_KEY_] = _index_to_component(ind)
260
+
261
+ results_iterator = executor.map(
262
+ functools.partial(
263
+ run_single_task,
264
+ wftask=wftask,
265
+ command=task.command_parallel,
266
+ workflow_dir=workflow_dir,
267
+ workflow_dir_user=workflow_dir_user,
268
+ ),
269
+ list_function_kwargs,
270
+ **executor_options_compute,
271
+ )
272
+ # Explicitly iterate over the whole list, so that all futures are waited
273
+ outputs = list(results_iterator)
274
+
275
+ # Validate all non-None outputs
276
+ for ind, output in enumerate(outputs):
277
+ if output is None:
278
+ outputs[ind] = TaskOutput()
279
+ else:
280
+ # FIXME: improve handling of validation errors
281
+ validated_output = TaskOutput(**output)
282
+ outputs[ind] = validated_output
283
+
284
+ merged_output = merge_outputs(outputs)
285
+ return merged_output
286
+
287
+
288
+ def run_v1_task_parallel(
289
+ *,
290
+ images: list[SingleImage],
291
+ task_legacy: TaskV1,
292
+ wftask: WorkflowTaskV2,
293
+ executor: Executor,
294
+ workflow_dir: Path,
295
+ workflow_dir_user: Optional[Path] = None,
296
+ logger_name: Optional[str] = None,
297
+ submit_setup_call: Callable = no_op_submit_setup_call,
298
+ ) -> TaskOutput:
299
+
300
+ _check_parallelization_list_size(images)
301
+
302
+ executor_options = _get_executor_options(
303
+ wftask=wftask,
304
+ workflow_dir=workflow_dir,
305
+ workflow_dir_user=workflow_dir_user,
306
+ submit_setup_call=submit_setup_call,
307
+ which_type="parallel",
308
+ )
309
+
310
+ list_function_kwargs = []
311
+ for ind, image in enumerate(images):
312
+ list_function_kwargs.append(
313
+ convert_v2_args_into_v1(
314
+ dict(
315
+ path=image["path"],
316
+ **(wftask.args_parallel or {}),
317
+ )
318
+ ),
319
+ )
320
+ list_function_kwargs[-1][_COMPONENT_KEY_] = _index_to_component(ind)
321
+
322
+ results_iterator = executor.map(
323
+ functools.partial(
324
+ run_single_task,
325
+ wftask=wftask,
326
+ command=task_legacy.command,
327
+ workflow_dir=workflow_dir,
328
+ workflow_dir_user=workflow_dir_user,
329
+ is_task_v1=True,
330
+ ),
331
+ list_function_kwargs,
332
+ **executor_options,
333
+ )
334
+ # Explicitly iterate over the whole list, so that all futures are waited
335
+ list(results_iterator)
336
+
337
+ # Ignore any output metadata for V1 tasks, and return an empty object
338
+ out = TaskOutput()
339
+ return out