"""Module for implementing collections of features within a Sample."""importloggingfromtypingimportOptionalfromplaid.containers.utilsimport(_check_names,)fromplaid.typesimportScalarlogger=logging.getLogger(__name__)logging.basicConfig(format="[%(asctime)s:%(levelname)s:%(filename)s:%(funcName)s(%(lineno)d)]:%(message)s",level=logging.INFO,)
[docs]classSampleScalars:"""A container for scalar features within a Sample. Provides dict-like operations for adding, retrieving, and removing scalars. Names must be unique and may not contain the character ``/``. Args: scalars (dict[str, Scalar], optional): a dict containing the pairs of (name, value) for each scalar in the `Sample`. """def__init__(self,scalars:Optional[dict[str,Scalar]])->None:
[docs]defadd(self,name:str,value:Scalar)->None:"""Add a scalar value to a dictionary. Args: name (str): The name of the scalar value. value (Scalar): The scalar value to add or update in the dictionary. """_check_names(name)self.data[name]=value
[docs]defremove(self,name:str)->Scalar:"""Delete a scalar value from the dictionary. Args: name (str): The name of the scalar value to be deleted. Raises: KeyError: Raised when there is no scalar / there is no scalar with the provided name. Returns: Scalar: The value of the deleted scalar. """ifnamenotinself.data:raiseKeyError(f"There is no scalar value with name {name}.")returnself.data.pop(name)
[docs]defget(self,name:str)->Optional[Scalar]:"""Retrieve a scalar value associated with the given name. Args: name (str): The name of the scalar value to retrieve. Returns: Scalar or None: The scalar value associated with the given name, or None if the name is not found. """returnself.data.get(name)
[docs]defget_names(self)->list[str]:"""Get a set of scalar names available in the object. Returns: list[str]: A set containing the names of the available scalars. """returnsorted(self.data.keys())