Skip to content

Commit

Permalink
Uncertainties adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
valsdav committed Feb 23, 2024
1 parent c048cee commit b30b874
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
35 changes: 34 additions & 1 deletion order/adapters/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from order.adapters.base import Adapter, Materialized
from order.models.dataset import LazyDataset
from order.models.process import LazyProcess

from order.models.uncertainty import LazyUncertainty

class OrderAdapter(Adapter):

Expand Down Expand Up @@ -211,3 +211,36 @@ def retrieve_data(
return Materialized(process=entry)

raise Exception(f"no process entry with name '{process_name}' found in {path}")



class uncertaintiesAdapter(OrderAdapter):
name ="order_uncertainties"
def retrieve_data(
self,
data_location: str,
*,
directories: list[str]
) -> Materialized:
# only supporting local evaluation for now
if not self.location_is_local(data_location):
raise NotImplementedError(f"non-local location {data_location} not handled by {self!r}")

# build the yaml file path.
# We need to find the directory by looking at the deepest existent
# directory of the uncertainty_type hierarchy.
uncertainties = []
basepath = os.path.join(self.remove_scheme(data_location), "uncertainties")
for directory in directories:
# loop over all file in the directory
for file in os.listdir(os.path.join(basepath, directory)):
if os.path.isfile(os.path.join(basepath, directory, file)):
#load the file and loop over entities
with open(os.path.join(basepath, directory, file), "r") as f:
stream = yaml.load_all(f, Loader=yaml.SafeLoader)
for entry in stream:
uncertainties.append(LazyUncertainty.create_lazy_dict(
entry["name"], entry["id"],
entry["uncertainty_type"], os.path.join(directory,file)))

return Materialized(uncertainties=uncertainties)
28 changes: 18 additions & 10 deletions order/models/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ class LazyUncertainty(LazyUniqueObject):
class_name: NonEmptyStrictStr = Field(default="Uncertainty", frozen=True)

@classmethod
def create_lazy_dict(cls, name: str, id: int) -> dict:
def create_lazy_dict(cls, name: str, id: int, uncertainty_type:str, filename:str) -> dict:
# Get the classname checking with the class_label
uncertainty_class = Uncertainty.get_class(uncertainty_type)
return {
"name": name,
"id": id,
"class_name": "Uncertainty",
"class_name": uncertainty_class.__name__,
"adapter": {
"adapter": "order_uncertainty",
"key": "uncertainty",
"arguments": {
"uncertainty_name": name,
"filename": filename,
},
},
}
Expand All @@ -52,19 +54,20 @@ def __new__(metacls, cls_name, bases, cls_dict):
cls = super().__new__(metacls, cls_name, bases, cls_dict)
# Store the new type in the _classes dictionary
if hasattr(cls, "class_label"):
metacls._classes[cls.class_label] = cls
# check base class label
if isinstance(bases[0], metacls):
if hasattr(bases[0], "class_label"):
metacls._classes["_".join([bases[0].class_label, cls.class_label])] = cls
else:
metacls._classes[cls.class_label] = cls
print(metacls._classes)
if not cls.class_label.startswith(bases[0].class_label):
raise ValueError(f"Uncertainty class label {cls.class_label} does not inherit from {bases[0].class_label}")

# Save the class in the class cache of all the base classes
return cls


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

class ExperimentalUncertainty(Uncertainty):
'''Model that represents an experimental uncertainty'''
class_label: ClassVar[str] = "exp"
class_label: ClassVar[str] = "syst_exp"
pog : NonEmptyStrictStr = Field(default="", description="POG that provides the uncertainty")

pass

class JESUncertainty(ExperimentalUncertainty):
'''Model that represents an experimental uncertainty'''
class_label: ClassVar[str] = "syst_exp_JES"
pass


class TheoryUncertainty(Uncertainty):
'''Model that represents an experimental uncertainty'''
class_label: ClassVar[str] = "theory"
class_label: ClassVar[str] = "syst_theory"
generator: NonEmptyStrictStr = Field(default="", description="Generator that provides the uncertainty")

pass

0 comments on commit b30b874

Please sign in to comment.