Return subcomponents of a model under a specified path.
Source code in kiara/utils/models.py
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 | def get_subcomponent_from_model(data: "KiaraModel", path: str) -> "KiaraModel":
"""Return subcomponents of a model under a specified path."""
if "." in path:
first_token, rest = path.split(".", maxsplit=1)
sc = data.get_subcomponent(first_token)
return sc.get_subcomponent(rest)
if hasattr(data, "__custom_root_type__") and data.__custom_root_type__:
if isinstance(data.__root__, Mapping): # type: ignore
if path in data.__root__.keys(): # type: ignore
return data.__root__[path] # type: ignore
else:
matches = {}
for k in data.__root__.keys(): # type: ignore
if k.startswith(f"{path}."):
rest = k[len(path) + 1 :]
matches[rest] = data.__root__[k] # type: ignore
if not matches:
raise KeyError(f"No child models under '{path}'.")
else:
raise NotImplementedError()
# subcomponent_group = KiaraModelGroup.create_from_child_models(**matches)
# return subcomponent_group
else:
raise NotImplementedError()
else:
if path in data.__fields__.keys():
return getattr(data, path)
else:
raise KeyError(
f"No subcomponent for key '{path}' in model: {data.instance_id}."
)
|