-
Notifications
You must be signed in to change notification settings - Fork 26
/
component_spec.py
328 lines (278 loc) · 10.4 KB
/
component_spec.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""This module defines classes to represent an Fondant component specification."""
import ast
import copy
import json
import pkgutil
import types
import typing as t
from dataclasses import dataclass
from pathlib import Path
import jsonschema.exceptions
import yaml
from jsonschema import Draft4Validator
from jsonschema.validators import RefResolver
from fondant.exceptions import InvalidComponentSpec
from fondant.schema import Field, KubeflowCommandArguments, Type
# TODO: remove after upgrading to kfpv2
kubeflow2python_type = {
"String": str,
"Integer": int,
"Float": float,
"Boolean": ast.literal_eval,
"JsonObject": json.loads,
"JsonArray": json.loads,
}
# TODO: Change after upgrading to kfp v2
# :https://www.kubeflow.org/docs/components/pipelines/v2/data-types/parameters/
python2kubeflow_type = {
"str": "String",
"int": "Integer",
"float": "Float",
"bool": "Boolean",
"dict": "JsonObject",
"list": "JsonArray",
}
@dataclass
class Argument:
"""
Kubeflow component argument.
Args:
name: name of the argument
description: argument description
type: the python argument type (str, int, ...)
"""
name: str
description: str
type: str
class ComponentSubset:
"""
Class representing a Fondant Component subset.
Args:
specification: the part of the component json representing the subset
"""
def __init__(self, specification: t.Dict[str, t.Any]) -> None:
self._specification = specification
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._specification!r}"
@property
def fields(self) -> t.Mapping[str, Field]:
return types.MappingProxyType(
{
name: Field(name=name, type=Type.from_json(field))
for name, field in self._specification["fields"].items()
}
)
@property
def additional_fields(self) -> bool:
return self._specification.get("additionalFields", True)
class ComponentSpec:
"""
Class representing a Fondant component specification.
Args:
specification: The fondant component specification as a Python dict
"""
def __init__(self, specification: t.Dict[str, t.Any]) -> None:
self._specification = copy.deepcopy(specification)
self._validate_spec()
def _validate_spec(self) -> None:
"""Validate a component specification against the component schema.
Raises: InvalidComponent when the component specification is not valid.
"""
spec_data = pkgutil.get_data("fondant", "schemas/component_spec.json")
if spec_data is None:
raise FileNotFoundError("component_spec.json not found in fondant schema")
else:
spec_str = spec_data.decode("utf-8")
spec_schema = json.loads(spec_str)
base_uri = (Path(__file__).parent / "schemas").as_uri()
resolver = RefResolver(base_uri=f"{base_uri}/", referrer=spec_schema)
validator = Draft4Validator(spec_schema, resolver=resolver)
try:
validator.validate(self._specification)
except jsonschema.exceptions.ValidationError as e:
raise InvalidComponentSpec.create_from(e)
@classmethod
def from_file(cls, path: t.Union[str, Path]) -> "ComponentSpec":
"""Load the component spec from the file specified by the provided path."""
with open(path, encoding="utf-8") as file_:
specification = yaml.safe_load(file_)
return cls(specification)
def to_file(self, path) -> None:
"""Dump the component spec to the file specified by the provided path."""
with open(path, "w", encoding="utf-8") as file_:
yaml.dump(self._specification, file_)
@property
def name(self):
return self._specification["name"]
@property
def description(self):
return self._specification["description"]
@property
def image(self):
return self._specification["image"]
@property
def index(self):
return ComponentSubset({"fields": {}})
@property
def consumes(self) -> t.Mapping[str, ComponentSubset]:
"""The subsets consumed by the component as an immutable mapping."""
return types.MappingProxyType(
{
name: ComponentSubset(subset)
for name, subset in self._specification.get("consumes", {}).items()
if name != "additionalSubsets"
}
)
@property
def produces(self) -> t.Mapping[str, ComponentSubset]:
"""The subsets produced by the component as an immutable mapping."""
return types.MappingProxyType(
{
name: ComponentSubset(subset)
for name, subset in self._specification.get("produces", {}).items()
if name != "additionalSubsets"
}
)
@property
def accepts_additional_subsets(self) -> bool:
return self._specification.get("consumes", {}).get("additionalSubsets", True)
@property
def outputs_additional_subsets(self) -> bool:
return self._specification.get("produces", {}).get("additionalSubsets", True)
@property
def args(self) -> t.Dict[str, Argument]:
return {
name: Argument(
name=name,
description=arg_info["description"],
type=arg_info["type"],
)
for name, arg_info in self._specification.get("args", {}).items()
}
@property
def specification(self) -> t.Dict[str, t.Any]:
return copy.deepcopy(self._specification)
@property
def kubeflow_specification(self) -> "KubeflowComponentSpec":
return KubeflowComponentSpec.from_fondant_component_spec(self)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._specification!r}"
class KubeflowComponentSpec:
"""
Class representing a Kubeflow component specification.
Args:
specification: The kubeflow component specification as a Python dict
"""
def __init__(self, specification: t.Dict[str, t.Any]) -> None:
self._specification = specification
@classmethod
def from_fondant_component_spec(
cls, fondant_component: ComponentSpec
) -> "KubeflowComponentSpec":
"""Create a Kubeflow component spec from a Fondant component spec."""
specification = {
"name": fondant_component.name,
"description": fondant_component.description,
"inputs": [
{
"name": "input_manifest_path",
"description": "Path to the input manifest",
"type": "String",
},
{
"name": "metadata",
"description": "Metadata arguments containing the run id and base path",
"type": "String",
},
{
"name": "component_spec",
"description": "The component specification as a dictionary",
"type": "JsonObject",
},
*(
{
"name": arg.name,
"description": arg.description,
"type": python2kubeflow_type[arg.type],
}
for arg in fondant_component.args.values()
),
],
"outputs": [
{
"name": "output_manifest_path",
"description": "Path to the output manifest",
"type": "String",
},
],
"implementation": {
"container": {
"image": fondant_component.image,
"command": [
"python3",
"main.py",
"--input_manifest_path",
{"inputPath": "input_manifest_path"},
"--metadata",
{"inputValue": "metadata"},
"--component_spec",
{"inputValue": "component_spec"},
*cls._dump_args(fondant_component.args.values()),
"--output_manifest_path",
{"outputPath": "output_manifest_path"},
],
}
},
}
return cls(specification)
@staticmethod
def _dump_args(args: t.Iterable[Argument]) -> KubeflowCommandArguments:
"""Dump Fondant specification arguments to kfp command arguments."""
dumped_args: KubeflowCommandArguments = []
for arg in args:
arg_name = arg.name.strip().replace(" ", "_")
arg_name_cmd = f"--{arg_name}"
dumped_args.append(arg_name_cmd)
dumped_args.append({"inputValue": arg_name})
return dumped_args
def to_file(self, path: t.Union[str, Path]) -> None:
"""Dump the component specification to the file specified by the provided path."""
with open(path, "w", encoding="utf-8") as file_:
yaml.dump(
self._specification,
file_,
indent=4,
default_flow_style=False,
sort_keys=False,
)
def to_string(self) -> str:
"""Return the component specification as a string."""
return json.dumps(self._specification)
@property
def input_arguments(self) -> t.Mapping[str, Argument]:
"""The input arguments of the component as an immutable mapping."""
return types.MappingProxyType(
{
info["name"]: Argument(
name=info["name"],
description=info["description"],
type=info["type"],
)
for info in self._specification["inputs"]
}
)
@property
def output_arguments(self) -> t.Mapping[str, Argument]:
"""The output arguments of the component as an immutable mapping."""
return types.MappingProxyType(
{
info["name"]: Argument(
name=info["name"],
description=info["description"],
type=info["type"],
)
for info in self._specification["outputs"]
}
)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._specification!r}"