Skip to content

Commit b30b874

Browse files
committed
Uncertainties adapter
1 parent c048cee commit b30b874

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

order/adapters/order.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from order.adapters.base import Adapter, Materialized
1919
from order.models.dataset import LazyDataset
2020
from order.models.process import LazyProcess
21-
21+
from order.models.uncertainty import LazyUncertainty
2222

2323
class OrderAdapter(Adapter):
2424

@@ -211,3 +211,36 @@ def retrieve_data(
211211
return Materialized(process=entry)
212212

213213
raise Exception(f"no process entry with name '{process_name}' found in {path}")
214+
215+
216+
217+
class uncertaintiesAdapter(OrderAdapter):
218+
name ="order_uncertainties"
219+
def retrieve_data(
220+
self,
221+
data_location: str,
222+
*,
223+
directories: list[str]
224+
) -> Materialized:
225+
# only supporting local evaluation for now
226+
if not self.location_is_local(data_location):
227+
raise NotImplementedError(f"non-local location {data_location} not handled by {self!r}")
228+
229+
# build the yaml file path.
230+
# We need to find the directory by looking at the deepest existent
231+
# directory of the uncertainty_type hierarchy.
232+
uncertainties = []
233+
basepath = os.path.join(self.remove_scheme(data_location), "uncertainties")
234+
for directory in directories:
235+
# loop over all file in the directory
236+
for file in os.listdir(os.path.join(basepath, directory)):
237+
if os.path.isfile(os.path.join(basepath, directory, file)):
238+
#load the file and loop over entities
239+
with open(os.path.join(basepath, directory, file), "r") as f:
240+
stream = yaml.load_all(f, Loader=yaml.SafeLoader)
241+
for entry in stream:
242+
uncertainties.append(LazyUncertainty.create_lazy_dict(
243+
entry["name"], entry["id"],
244+
entry["uncertainty_type"], os.path.join(directory,file)))
245+
246+
return Materialized(uncertainties=uncertainties)

order/models/uncertainty.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ class LazyUncertainty(LazyUniqueObject):
2828
class_name: NonEmptyStrictStr = Field(default="Uncertainty", frozen=True)
2929

3030
@classmethod
31-
def create_lazy_dict(cls, name: str, id: int) -> dict:
31+
def create_lazy_dict(cls, name: str, id: int, uncertainty_type:str, filename:str) -> dict:
32+
# Get the classname checking with the class_label
33+
uncertainty_class = Uncertainty.get_class(uncertainty_type)
3234
return {
3335
"name": name,
3436
"id": id,
35-
"class_name": "Uncertainty",
37+
"class_name": uncertainty_class.__name__,
3638
"adapter": {
3739
"adapter": "order_uncertainty",
3840
"key": "uncertainty",
3941
"arguments": {
40-
"uncertainty_name": name,
42+
"filename": filename,
4143
},
4244
},
4345
}
@@ -52,19 +54,20 @@ def __new__(metacls, cls_name, bases, cls_dict):
5254
cls = super().__new__(metacls, cls_name, bases, cls_dict)
5355
# Store the new type in the _classes dictionary
5456
if hasattr(cls, "class_label"):
57+
metacls._classes[cls.class_label] = cls
58+
# check base class label
59+
if isinstance(bases[0], metacls):
5560
if hasattr(bases[0], "class_label"):
56-
metacls._classes["_".join([bases[0].class_label, cls.class_label])] = cls
57-
else:
58-
metacls._classes[cls.class_label] = cls
59-
print(metacls._classes)
61+
if not cls.class_label.startswith(bases[0].class_label):
62+
raise ValueError(f"Uncertainty class label {cls.class_label} does not inherit from {bases[0].class_label}")
63+
6064
# Save the class in the class cache of all the base classes
6165
return cls
6266

6367

6468
def get_class(cls, class_label:str):
6569
'''This function splits the class_label using _ and look for the
6670
closest match in the available classes dict'''
67-
breakpoint()
6871
toks = class_label.split("_")
6972
for i in range(len(toks), 0, -1):
7073
label = "_".join(toks[:i])
@@ -94,15 +97,20 @@ def create(cls, uncertainty_type:str, **kwargs) -> Uncertainty:
9497

9598
class ExperimentalUncertainty(Uncertainty):
9699
'''Model that represents an experimental uncertainty'''
97-
class_label: ClassVar[str] = "exp"
100+
class_label: ClassVar[str] = "syst_exp"
98101
pog : NonEmptyStrictStr = Field(default="", description="POG that provides the uncertainty")
99102

100103
pass
101104

105+
class JESUncertainty(ExperimentalUncertainty):
106+
'''Model that represents an experimental uncertainty'''
107+
class_label: ClassVar[str] = "syst_exp_JES"
108+
pass
109+
102110

103111
class TheoryUncertainty(Uncertainty):
104112
'''Model that represents an experimental uncertainty'''
105-
class_label: ClassVar[str] = "theory"
113+
class_label: ClassVar[str] = "syst_theory"
106114
generator: NonEmptyStrictStr = Field(default="", description="Generator that provides the uncertainty")
107115

108116
pass

0 commit comments

Comments
 (0)