Module scenariogeneration.xosc.parameters
scenariogeneration https://github.com/pyoscx/scenariogeneration
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
Copyright (c) 2022 The scenariogeneration Authors.
Classes
class Deterministic-
Expand source code
class Deterministic(VersionBase): """Creates the Deterministic type of parameter distributions. Attributes ---------- multi_distributions : list of DeterministicMultiParameterDistribution Multi-parameter distributions. single_distributions : dict of DistributionSet or DistributionRange Single-parameter distributions. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_multi_distribution(distribution) Adds a DeterministicMultiParameterDistribution. add_single_distribution(parametername, distribution) Adds a DeterministicSingleParameterDistribution. get_element(elementname) Returns the full ElementTree of the class. """ def __init__(self): """Initializes the Deterministic.""" self.multi_distributions = [] self.single_distributions = {} def __eq__(self, other: object) -> bool: if isinstance(other, Deterministic): if ( self.multi_distributions == other.multi_distributions and self.single_distributions == other.single_distributions ): return True return False @staticmethod def parse(element: ET.Element) -> "Deterministic": """Parses the XML element of Deterministic. Parameters ---------- element : xml.etree.ElementTree.Element A Deterministic element (same as generated by the class itself). Returns ------- Deterministic A Deterministic object. """ deterministic = Deterministic() for single_dist in element.findall( "DeterministicSingleParameterDistribution" ): param_name = single_dist.attrib["parameterName"] dist = _DeterministicFactory.parse_distribution(single_dist) deterministic.add_single_distribution(param_name, dist) for multi_dist in element.findall( "DeterministicMultiParameterDistribution" ): deterministic.add_multi_distribution( DeterministicMultiParameterDistribution.parse(multi_dist) ) return deterministic def add_multi_distribution( self, distribution: DeterministicMultiParameterDistribution ) -> None: """Adds a DeterministicMultiParameterDistribution. Parameters ---------- distribution : DeterministicMultiParameterDistribution The distribution of that parameter. Raises ------ TypeError If the distribution input is not of type DeterministicMultiParameterDistribution. """ if not isinstance( distribution, DeterministicMultiParameterDistribution ): raise TypeError( "distribution input is not of type DeterministicMultiParameterDistribution" ) self.multi_distributions.append(distribution) return self def add_single_distribution( self, parametername: str, distribution: Union[DistributionSet, DistributionRange], ) -> None: """Adds a parameter and a related distribution to it. Parameters ---------- parametername : str Reference to the parameter. distribution : DistributionSet or DistributionRange The distribution of that parameter. Raises ------ TypeError If the distribution input is not of type DeterministicMultiParameterDistribution. """ if not ( isinstance(distribution, (DistributionSet, DistributionRange)) ): raise TypeError( "distribution input is not of type DeterministicMultiParameterDistribution" ) self.single_distributions[parametername] = distribution return self def get_element(self) -> ET.Element: """Returns the ElementTree of the Deterministic. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Deterministic. """ element = ET.Element("Deterministic") for md in self.multi_distributions: element.append(md.get_element()) for d, sing_dist in self.single_distributions.items(): dist = ET.SubElement( element, "DeterministicSingleParameterDistribution", attrib={"parameterName": d}, ) dist.append(sing_dist.get_element()) return elementCreates the Deterministic type of parameter distributions.
Attributes
multi_distributions:listofDeterministicMultiParameterDistribution- Multi-parameter distributions.
single_distributions:dictofDistributionSetorDistributionRange- Single-parameter distributions.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_multi_distribution(distribution) Adds a DeterministicMultiParameterDistribution. add_single_distribution(parametername, distribution) Adds a DeterministicSingleParameterDistribution. get_element(elementname) Returns the full ElementTree of the class.
Initializes the Deterministic.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> Deterministic-
Expand source code
@staticmethod def parse(element: ET.Element) -> "Deterministic": """Parses the XML element of Deterministic. Parameters ---------- element : xml.etree.ElementTree.Element A Deterministic element (same as generated by the class itself). Returns ------- Deterministic A Deterministic object. """ deterministic = Deterministic() for single_dist in element.findall( "DeterministicSingleParameterDistribution" ): param_name = single_dist.attrib["parameterName"] dist = _DeterministicFactory.parse_distribution(single_dist) deterministic.add_single_distribution(param_name, dist) for multi_dist in element.findall( "DeterministicMultiParameterDistribution" ): deterministic.add_multi_distribution( DeterministicMultiParameterDistribution.parse(multi_dist) ) return deterministicParses the XML element of Deterministic.
Parameters
element:xml.etree.ElementTree.Element- A Deterministic element (same as generated by the class itself).
Returns
Deterministic- A Deterministic object.
Methods
def add_multi_distribution(self,
distribution: DeterministicMultiParameterDistribution) ‑> None-
Expand source code
def add_multi_distribution( self, distribution: DeterministicMultiParameterDistribution ) -> None: """Adds a DeterministicMultiParameterDistribution. Parameters ---------- distribution : DeterministicMultiParameterDistribution The distribution of that parameter. Raises ------ TypeError If the distribution input is not of type DeterministicMultiParameterDistribution. """ if not isinstance( distribution, DeterministicMultiParameterDistribution ): raise TypeError( "distribution input is not of type DeterministicMultiParameterDistribution" ) self.multi_distributions.append(distribution) return selfAdds a DeterministicMultiParameterDistribution.
Parameters
distribution:DeterministicMultiParameterDistribution- The distribution of that parameter.
Raises
TypeError- If the distribution input is not of type DeterministicMultiParameterDistribution.
def add_single_distribution(self,
parametername: str,
distribution: DistributionSet | DistributionRange) ‑> None-
Expand source code
def add_single_distribution( self, parametername: str, distribution: Union[DistributionSet, DistributionRange], ) -> None: """Adds a parameter and a related distribution to it. Parameters ---------- parametername : str Reference to the parameter. distribution : DistributionSet or DistributionRange The distribution of that parameter. Raises ------ TypeError If the distribution input is not of type DeterministicMultiParameterDistribution. """ if not ( isinstance(distribution, (DistributionSet, DistributionRange)) ): raise TypeError( "distribution input is not of type DeterministicMultiParameterDistribution" ) self.single_distributions[parametername] = distribution return selfAdds a parameter and a related distribution to it.
Parameters
parametername:str- Reference to the parameter.
distribution:DistributionSetorDistributionRange- The distribution of that parameter.
Raises
TypeError- If the distribution input is not of type DeterministicMultiParameterDistribution.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the Deterministic. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Deterministic. """ element = ET.Element("Deterministic") for md in self.multi_distributions: element.append(md.get_element()) for d, sing_dist in self.single_distributions.items(): dist = ET.SubElement( element, "DeterministicSingleParameterDistribution", attrib={"parameterName": d}, ) dist.append(sing_dist.get_element()) return elementReturns the ElementTree of the Deterministic.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the Deterministic.
class DeterministicMultiParameterDistribution-
Expand source code
class DeterministicMultiParameterDistribution(VersionBase): """Creates the DeterministicMultiParameterDistribution type of parameter distributions. Attributes ---------- distributions : list of ParameterValueSet The distributions for the parameters. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_value_set(parameter_value_set) Adds a parameter value set. get_element(elementname) Returns the full ElementTree of the class. """ def __init__(self): """Initializes the DeterministicMultiParameterDistribution.""" self.sets = [] def __eq__(self, other: object) -> bool: if isinstance(other, DeterministicMultiParameterDistribution): if self.sets == other.sets: return True return False @staticmethod def parse( element: ET.Element, ) -> "DeterministicMultiParameterDistribution": """Parses the XML element of DeterministicMultiParameterDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A DeterministicMultiParameterDistribution element (same as generated by the class itself). Returns ------- DeterministicMultiParameterDistribution A DeterministicMultiParameterDistribution object. """ dist = DeterministicMultiParameterDistribution() for h_element in element.findall( "ValueSetDistribution/ParameterValueSet" ): dist.sets.append(ParameterValueSet.parse(h_element)) return dist def add_value_set(self, parameter_value_set: ParameterValueSet) -> None: """Adds a parameter value set. Parameters ---------- parameter_value_set : ParameterValueSet A set of parameters. Raises ------ TypeError If the distribution input is not of type ParameterValueSet. """ if not isinstance(parameter_value_set, ParameterValueSet): raise TypeError( "distribution input is not of type ParameterValueSet" ) self.sets.append(parameter_value_set) return self def get_element(self) -> ET.Element: """Returns the ElementTree of the DeterministicMultiParameterDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the DeterministicMultiParameterDistribution. Raises ------ NotEnoughInputArguments If no sets have been added. """ element = ET.Element("DeterministicMultiParameterDistribution") value_set_element = ET.SubElement(element, "ValueSetDistribution") if not self.sets: raise NotEnoughInputArguments("No sets have been added") for d in self.sets: value_set_element.append(d.get_element()) return elementCreates the DeterministicMultiParameterDistribution type of parameter distributions.
Attributes
distributions:listofParameterValueSet- The distributions for the parameters.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_value_set(parameter_value_set) Adds a parameter value set. get_element(elementname) Returns the full ElementTree of the class.
Initializes the DeterministicMultiParameterDistribution.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> DeterministicMultiParameterDistribution-
Expand source code
@staticmethod def parse( element: ET.Element, ) -> "DeterministicMultiParameterDistribution": """Parses the XML element of DeterministicMultiParameterDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A DeterministicMultiParameterDistribution element (same as generated by the class itself). Returns ------- DeterministicMultiParameterDistribution A DeterministicMultiParameterDistribution object. """ dist = DeterministicMultiParameterDistribution() for h_element in element.findall( "ValueSetDistribution/ParameterValueSet" ): dist.sets.append(ParameterValueSet.parse(h_element)) return distParses the XML element of DeterministicMultiParameterDistribution.
Parameters
element:xml.etree.ElementTree.Element- A DeterministicMultiParameterDistribution element (same as generated by the class itself).
Returns
DeterministicMultiParameterDistribution- A DeterministicMultiParameterDistribution object.
Methods
def add_value_set(self,
parameter_value_set: ParameterValueSet) ‑> None-
Expand source code
def add_value_set(self, parameter_value_set: ParameterValueSet) -> None: """Adds a parameter value set. Parameters ---------- parameter_value_set : ParameterValueSet A set of parameters. Raises ------ TypeError If the distribution input is not of type ParameterValueSet. """ if not isinstance(parameter_value_set, ParameterValueSet): raise TypeError( "distribution input is not of type ParameterValueSet" ) self.sets.append(parameter_value_set) return selfAdds a parameter value set.
Parameters
parameter_value_set:ParameterValueSet- A set of parameters.
Raises
TypeError- If the distribution input is not of type ParameterValueSet.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the DeterministicMultiParameterDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the DeterministicMultiParameterDistribution. Raises ------ NotEnoughInputArguments If no sets have been added. """ element = ET.Element("DeterministicMultiParameterDistribution") value_set_element = ET.SubElement(element, "ValueSetDistribution") if not self.sets: raise NotEnoughInputArguments("No sets have been added") for d in self.sets: value_set_element.append(d.get_element()) return elementReturns the ElementTree of the DeterministicMultiParameterDistribution.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the DeterministicMultiParameterDistribution.
Raises
NotEnoughInputArguments- If no sets have been added.
class DistributionRange (step_width: float,
range: Range)-
Expand source code
class DistributionRange(VersionBase): """Creates the DistributionRange used to define a single parameter distribution. Parameters ---------- step_width : float Step size of the distribution. range : Range The range of the parameter. Attributes ---------- step_width : float Step size of the distribution. range : Range The range of the parameter. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class. """ def __init__(self, step_width: float, range: Range): """Initializes the DistributionRange. Parameters ---------- step_width : float Step size of the distribution. range : Range The range of the parameter. Raises ------ TypeError If the range is not of type Range. """ self.step_width = step_width if not isinstance(range, Range): raise TypeError("range is not of type Range.") self.range = range def __eq__(self, other: object) -> bool: if isinstance(other, DistributionRange): if ( self.get_attributes() == other.get_attributes() and self.range == other.range ): return True return False @staticmethod def parse(element: ET.Element) -> "DistributionRange": """Parses the XML element of DistributionRange. Parameters ---------- element : xml.etree.ElementTree.Element A DistributionRange element (same as generated by the class itself). Returns ------- DistributionRange A DistributionRange object. """ distribution_range = DistributionRange( element.attrib["stepWidth"], Range.parse(find_mandatory_field(element, "Range")), ) return distribution_range def get_attributes(self) -> dict: """Returns the attributes of the DistributionRange as a dictionary. Returns ------- dict A dictionary containing the attributes of the DistributionRange. """ retdict = {"stepWidth": str(self.step_width)} return retdict def get_element(self) -> ET.Element: """Returns the ElementTree of the DistributionRange. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the DistributionRange. """ element = ET.Element("DistributionRange", attrib=self.get_attributes()) element.append(self.range.get_element()) return elementCreates the DistributionRange used to define a single parameter distribution.
Parameters
step_width:float- Step size of the distribution.
range:Range- The range of the parameter.
Attributes
step_width:float- Step size of the distribution.
range:Range- The range of the parameter.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class.
Initializes the DistributionRange.
Parameters
step_width:float- Step size of the distribution.
range:Range- The range of the parameter.
Raises
TypeError- If the range is not of type Range.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> DistributionRange-
Expand source code
@staticmethod def parse(element: ET.Element) -> "DistributionRange": """Parses the XML element of DistributionRange. Parameters ---------- element : xml.etree.ElementTree.Element A DistributionRange element (same as generated by the class itself). Returns ------- DistributionRange A DistributionRange object. """ distribution_range = DistributionRange( element.attrib["stepWidth"], Range.parse(find_mandatory_field(element, "Range")), ) return distribution_rangeParses the XML element of DistributionRange.
Parameters
element:xml.etree.ElementTree.Element- A DistributionRange element (same as generated by the class itself).
Returns
DistributionRange- A DistributionRange object.
Methods
def get_attributes(self) ‑> dict-
Expand source code
def get_attributes(self) -> dict: """Returns the attributes of the DistributionRange as a dictionary. Returns ------- dict A dictionary containing the attributes of the DistributionRange. """ retdict = {"stepWidth": str(self.step_width)} return retdictReturns the attributes of the DistributionRange as a dictionary.
Returns
dict- A dictionary containing the attributes of the DistributionRange.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the DistributionRange. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the DistributionRange. """ element = ET.Element("DistributionRange", attrib=self.get_attributes()) element.append(self.range.get_element()) return elementReturns the ElementTree of the DistributionRange.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the DistributionRange.
class DistributionSet-
Expand source code
class DistributionSet(VersionBase): """Creates the DistributionSet used to define a single parameter distribution. Attributes ---------- value_elements : list of str A list of all values. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. add_value(value) Adds a value to the DistributionSet. """ def __init__(self): """Initializes the DistributionSet.""" self.value_elements = [] def __eq__(self, other: object) -> bool: if isinstance(other, DistributionSet): if self.value_elements == other.value_elements: return True return False @staticmethod def parse(element: ET.Element) -> "DistributionSet": """Parses the XML element of DistributionSet. Parameters ---------- element : xml.etree.ElementTree.Element A DistributionSet element (same as generated by the class itself). Returns ------- DistributionSet A DistributionSet object. """ dist = DistributionSet() for v in element.findall("Element"): dist.add_value(v.attrib["value"]) return dist def add_value(self, value: str) -> None: """Adds a value to the DistributionSet. Parameters ---------- value : str The value to be added to the distribution. """ self.value_elements.append(value) return self def get_element(self) -> ET.Element: """Returns the ElementTree of the DistributionSet. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the DistributionSet. Raises ------ NotEnoughInputArguments If no values have been added to the DistributionSet. """ element = ET.Element("DistributionSet") if not self.value_elements: raise NotEnoughInputArguments( "No values have been added to the DistributionSet" ) for value in self.value_elements: ET.SubElement(element, "Element", attrib={"value": value}) return elementCreates the DistributionSet used to define a single parameter distribution.
Attributes
value_elements:listofstr- A list of all values.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. add_value(value) Adds a value to the DistributionSet.
Initializes the DistributionSet.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> DistributionSet-
Expand source code
@staticmethod def parse(element: ET.Element) -> "DistributionSet": """Parses the XML element of DistributionSet. Parameters ---------- element : xml.etree.ElementTree.Element A DistributionSet element (same as generated by the class itself). Returns ------- DistributionSet A DistributionSet object. """ dist = DistributionSet() for v in element.findall("Element"): dist.add_value(v.attrib["value"]) return distParses the XML element of DistributionSet.
Parameters
element:xml.etree.ElementTree.Element- A DistributionSet element (same as generated by the class itself).
Returns
DistributionSet- A DistributionSet object.
Methods
def add_value(self, value: str) ‑> None-
Expand source code
def add_value(self, value: str) -> None: """Adds a value to the DistributionSet. Parameters ---------- value : str The value to be added to the distribution. """ self.value_elements.append(value) return selfAdds a value to the DistributionSet.
Parameters
value:str- The value to be added to the distribution.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the DistributionSet. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the DistributionSet. Raises ------ NotEnoughInputArguments If no values have been added to the DistributionSet. """ element = ET.Element("DistributionSet") if not self.value_elements: raise NotEnoughInputArguments( "No values have been added to the DistributionSet" ) for value in self.value_elements: ET.SubElement(element, "Element", attrib={"value": value}) return elementReturns the ElementTree of the DistributionSet.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the DistributionSet.
Raises
NotEnoughInputArguments- If no values have been added to the DistributionSet.
class Histogram-
Expand source code
class Histogram(_StochasticDistributionType): """The Histogram is a Stochastic distribution. Attributes ---------- bins : list of _HistogramBin The bins defining the histogram. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. add_bin(weight, range) Adds a weight and a range to a bin. """ def __init__(self): """Initializes the Histogram.""" self.bins = [] def __eq__(self, other: object) -> bool: if isinstance(other, Histogram): if self.bins == other.bins: return True return False @staticmethod def parse(element: ET.Element) -> "Histogram": """Parses the XML element of Histogram. Parameters ---------- element : xml.etree.ElementTree.Element A Histogram element (same as generated by the class itself). Returns ------- Histogram A Histogram object. """ hist = Histogram() for h_element in element.findall("Bin"): hist.bins.append(_HistogramBin.parse(h_element)) return hist def add_bin(self, weight: float, range: Range) -> None: """Adds a weight and a range to a bin. Parameters ---------- weight : float The weight of the bin. range : Range The range of the bin. """ self.bins.append(_HistogramBin(weight, range)) return self def get_element(self) -> ET.Element: """Returns the ElementTree of the Histogram. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Histogram. Raises ------ NotEnoughInputArguments If the Histogram has no bins. """ element = ET.Element("Histogram") if not self.bins: raise NotEnoughInputArguments( "The Histogram has no bins, please use add_bin to add at least one." ) for _bin in self.bins: element.append(_bin.get_element()) return elementThe Histogram is a Stochastic distribution.
Attributes
bins:listof_HistogramBin- The bins defining the histogram.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. add_bin(weight, range) Adds a weight and a range to a bin.
Initializes the Histogram.
Ancestors
- scenariogeneration.xosc.utils._StochasticDistributionType
- VersionBase
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> Histogram-
Expand source code
@staticmethod def parse(element: ET.Element) -> "Histogram": """Parses the XML element of Histogram. Parameters ---------- element : xml.etree.ElementTree.Element A Histogram element (same as generated by the class itself). Returns ------- Histogram A Histogram object. """ hist = Histogram() for h_element in element.findall("Bin"): hist.bins.append(_HistogramBin.parse(h_element)) return histParses the XML element of Histogram.
Parameters
element:xml.etree.ElementTree.Element- A Histogram element (same as generated by the class itself).
Returns
Histogram- A Histogram object.
Methods
def add_bin(self,
weight: float,
range: Range) ‑> None-
Expand source code
def add_bin(self, weight: float, range: Range) -> None: """Adds a weight and a range to a bin. Parameters ---------- weight : float The weight of the bin. range : Range The range of the bin. """ self.bins.append(_HistogramBin(weight, range)) return selfAdds a weight and a range to a bin.
Parameters
weight:float- The weight of the bin.
range:Range- The range of the bin.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the Histogram. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Histogram. Raises ------ NotEnoughInputArguments If the Histogram has no bins. """ element = ET.Element("Histogram") if not self.bins: raise NotEnoughInputArguments( "The Histogram has no bins, please use add_bin to add at least one." ) for _bin in self.bins: element.append(_bin.get_element()) return elementReturns the ElementTree of the Histogram.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the Histogram.
Raises
NotEnoughInputArguments- If the Histogram has no bins.
class LogNormalDistribution (expected_value: float,
variance: float,
range: Range | None = None)-
Expand source code
class LogNormalDistribution(_StochasticDistributionType): """The LogNormalDistribution is a Stochastic distribution. Parameters ---------- expected_value : float The expected value (mean) of the distribution. variance : float Variance of the distribution. range : Range, optional Limit of the values. Default is None. Attributes ---------- expected_value : float The expected value (mean) of the distribution. variance : float Variance of the distribution. range : Range Limit of the values. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class. """ def __init__( self, expected_value: float, variance: float, range: Optional[Range] = None, ): """Initializes the LogNormalDistribution. Parameters ---------- expected_value : float The expected value (mean) of the distribution. variance : float Variance of the distribution. range : Range, optional Limit of the values. Default is None. """ if range and not isinstance(range, Range): raise TypeError("range input is not of type Range") self.expected_value = convert_float(expected_value) if variance < 0: raise ValueError("Variance cannot be negative") self.variance = convert_float(variance) if range is not None and range.lower < 0: raise ValueError( "For lognormal, the lower limit has to be positive." ) self.range = range def __eq__(self, other: object) -> bool: if isinstance(other, LogNormalDistribution): if ( self.get_attributes() == other.get_attributes() and self.range == other.range ): return True return False @staticmethod def parse(element: ET.Element) -> "LogNormalDistribution": """Parses the XML element of LogNormalDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A LogNormalDistribution element (same as generated by the class itself). Returns ------- LogNormalDistribution A LogNormalDistribution object. """ if element.find("Range") is not None: range = Range.parse(find_mandatory_field(element, "Range")) else: range = None nd = LogNormalDistribution( convert_float(element.attrib["expectedValue"]), convert_float(element.attrib["variance"]), range, ) return nd def get_attributes(self) -> dict: """Returns the attributes of the LogNormalDistribution as a dictionary. Returns ------- dict A dictionary containing the attributes of the LogNormalDistribution. """ retdict = { "expectedValue": str(self.expected_value), "variance": str(self.variance), } return retdict def get_element(self) -> ET.Element: """Returns the ElementTree of the LogNormalDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the LogNormalDistribution. """ if self.isVersionEqLess(minor=2): raise OpenSCENARIOVersionError( "LogNormalDistribution was introduced in OSC V1.3" ) element = ET.Element("LogNormalDistribution", self.get_attributes()) if self.range: element.append(self.range.get_element()) return elementThe LogNormalDistribution is a Stochastic distribution.
Parameters
expected_value:float- The expected value (mean) of the distribution.
variance:float- Variance of the distribution.
range:Range, optional- Limit of the values. Default is None.
Attributes
expected_value:float- The expected value (mean) of the distribution.
variance:float- Variance of the distribution.
range:Range- Limit of the values.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class.
Initializes the LogNormalDistribution.
Parameters
expected_value:float- The expected value (mean) of the distribution.
variance:float- Variance of the distribution.
range:Range, optional- Limit of the values. Default is None.
Ancestors
- scenariogeneration.xosc.utils._StochasticDistributionType
- VersionBase
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> LogNormalDistribution-
Expand source code
@staticmethod def parse(element: ET.Element) -> "LogNormalDistribution": """Parses the XML element of LogNormalDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A LogNormalDistribution element (same as generated by the class itself). Returns ------- LogNormalDistribution A LogNormalDistribution object. """ if element.find("Range") is not None: range = Range.parse(find_mandatory_field(element, "Range")) else: range = None nd = LogNormalDistribution( convert_float(element.attrib["expectedValue"]), convert_float(element.attrib["variance"]), range, ) return ndParses the XML element of LogNormalDistribution.
Parameters
element:xml.etree.ElementTree.Element- A LogNormalDistribution element (same as generated by the class itself).
Returns
LogNormalDistribution- A LogNormalDistribution object.
Methods
def get_attributes(self) ‑> dict-
Expand source code
def get_attributes(self) -> dict: """Returns the attributes of the LogNormalDistribution as a dictionary. Returns ------- dict A dictionary containing the attributes of the LogNormalDistribution. """ retdict = { "expectedValue": str(self.expected_value), "variance": str(self.variance), } return retdictReturns the attributes of the LogNormalDistribution as a dictionary.
Returns
dict- A dictionary containing the attributes of the LogNormalDistribution.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the LogNormalDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the LogNormalDistribution. """ if self.isVersionEqLess(minor=2): raise OpenSCENARIOVersionError( "LogNormalDistribution was introduced in OSC V1.3" ) element = ET.Element("LogNormalDistribution", self.get_attributes()) if self.range: element.append(self.range.get_element()) return elementReturns the ElementTree of the LogNormalDistribution.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the LogNormalDistribution.
class NormalDistribution (expected_value: float,
variance: float,
range: Range | None = None)-
Expand source code
class NormalDistribution(_StochasticDistributionType): """The NormalDistribution is a Stochastic distribution. Parameters ---------- expected_value : float The expected value (mean) of the distribution. variance : float Variance of the distribution. range : Range, optional Limit of the values. Default is None. Attributes ---------- expected_value : float The expected value (mean) of the distribution. variance : float Variance of the distribution. range : Range Limit of the values. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class. """ def __init__( self, expected_value: float, variance: float, range: Optional[Range] = None, ): """Initializes the NormalDistribution. Parameters ---------- expected_value : float The expected value (mean) of the distribution. variance : float Variance of the distribution. range : Range, optional Limit of the values. Default is None. """ if range and not isinstance(range, Range): raise TypeError("range input is not of type Range") self.expected_value = convert_float(expected_value) self.variance = convert_float(variance) self.range = range def __eq__(self, other: object) -> bool: if isinstance(other, NormalDistribution): if ( self.get_attributes() == other.get_attributes() and self.range == other.range ): return True return False @staticmethod def parse(element: ET.Element) -> "NormalDistribution": """Parses the XML element of NormalDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A NormalDistribution element (same as generated by the class itself). Returns ------- NormalDistribution A NormalDistribution object. """ if element.find("Range") is not None: range = Range.parse(find_mandatory_field(element, "Range")) else: range = None nd = NormalDistribution( element.attrib["expectedValue"], element.attrib["variance"], range ) return nd def get_attributes(self) -> dict: """Returns the attributes of the NormalDistribution as a dictionary. Returns ------- dict A dictionary containing the attributes of the NormalDistribution. """ retdict = { "expectedValue": str(self.expected_value), "variance": str(self.variance), } return retdict def get_element(self) -> ET.Element: """Returns the ElementTree of the NormalDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the NormalDistribution. """ element = ET.Element("NormalDistribution", self.get_attributes()) if self.range: element.append(self.range.get_element()) return elementThe NormalDistribution is a Stochastic distribution.
Parameters
expected_value:float- The expected value (mean) of the distribution.
variance:float- Variance of the distribution.
range:Range, optional- Limit of the values. Default is None.
Attributes
expected_value:float- The expected value (mean) of the distribution.
variance:float- Variance of the distribution.
range:Range- Limit of the values.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class.
Initializes the NormalDistribution.
Parameters
expected_value:float- The expected value (mean) of the distribution.
variance:float- Variance of the distribution.
range:Range, optional- Limit of the values. Default is None.
Ancestors
- scenariogeneration.xosc.utils._StochasticDistributionType
- VersionBase
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> NormalDistribution-
Expand source code
@staticmethod def parse(element: ET.Element) -> "NormalDistribution": """Parses the XML element of NormalDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A NormalDistribution element (same as generated by the class itself). Returns ------- NormalDistribution A NormalDistribution object. """ if element.find("Range") is not None: range = Range.parse(find_mandatory_field(element, "Range")) else: range = None nd = NormalDistribution( element.attrib["expectedValue"], element.attrib["variance"], range ) return ndParses the XML element of NormalDistribution.
Parameters
element:xml.etree.ElementTree.Element- A NormalDistribution element (same as generated by the class itself).
Returns
NormalDistribution- A NormalDistribution object.
Methods
def get_attributes(self) ‑> dict-
Expand source code
def get_attributes(self) -> dict: """Returns the attributes of the NormalDistribution as a dictionary. Returns ------- dict A dictionary containing the attributes of the NormalDistribution. """ retdict = { "expectedValue": str(self.expected_value), "variance": str(self.variance), } return retdictReturns the attributes of the NormalDistribution as a dictionary.
Returns
dict- A dictionary containing the attributes of the NormalDistribution.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the NormalDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the NormalDistribution. """ element = ET.Element("NormalDistribution", self.get_attributes()) if self.range: element.append(self.range.get_element()) return elementReturns the ElementTree of the NormalDistribution.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the NormalDistribution.
class ParameterValueDistribution (description: str,
author: str,
scenario_file: str,
parameter_distribution: Stochastic | Deterministic,
osc_minor_version: int = 3,
header_properties: Properties = None,
license: License = None)-
Expand source code
class ParameterValueDistribution(VersionBase): """Creates the ParameterValueDistribution file for OpenSCENARIO. Parameters ---------- description : str A description of the parameter set. author : str The author of the file. scenario_file : str Path to the scenario that the parameter distribution should set. parameter_distribution : Stochastic or Deterministic The parameter distribution. osc_minor_version : int, optional The OSI version wanted. Default is _MINOR_VERSION. header_properties : Properties, optional Properties that can be added to the header. Default is None. license : License, optional Optional license to the file header. Default is None. Attributes ---------- header : FileHeader The file header. scenario_file : str Path to the scenario that the parameter distribution should set. parameter_distribution : Stochastic or Deterministic The parameter distribution. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. write_xml(filename, prettyprint, encoding) Writes an OpenSCENARIO XML file. get_element(elementname) Returns the full ElementTree of the class. """ _XMLNS = XMLNS _XSI = XSI def __init__( self, description: str, author: str, scenario_file: str, parameter_distribution: Union[Stochastic, Deterministic], osc_minor_version: int = _MINOR_VERSION, header_properties: Properties = None, license: License = None, ): """Initializes the ParameterValueDistribution. Parameters ---------- description : str A description of the parameter set. author : str The author of the file. scenario_file : str Path to the scenario that the parameter distribution should set. parameter_distribution : Stochastic or Deterministic The parameter distribution. osc_minor_version : int, optional The OSI version wanted. Default is _MINOR_VERSION. header_properties : Properties, optional Properties that can be added to the header. Default is None. license : License, optional Optional license to the file header. Default is None. Raises ------ TypeError If the parameter_distribution is not of type Stochastic or Deterministic. """ self.header = FileHeader( author, description, revMinor=osc_minor_version, properties=header_properties, license=license, ) if not isinstance( parameter_distribution, Stochastic ) and not isinstance(parameter_distribution, Deterministic): raise TypeError( type(parameter_distribution) + "is not of type Stochastic or Deterministic" ) self.scenario_file = scenario_file self.parameter_distribution = parameter_distribution def __eq__(self, other: object) -> bool: if isinstance(other, ParameterValueDistribution): if ( self.header == other.header and self.scenario_file == other.scenario_file and self.parameter_distribution == other.parameter_distribution ): return True return False @staticmethod def parse(element: ET.Element) -> "ParameterValueDistribution": """Parses the XML element of ParameterValueDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A ParameterValueDistribution element (same as generated by the class itself). Returns ------- ParameterValueDistribution A ParameterValueDistribution object. Raises ------ NotAValidElement If the ParameterValueDistribution is missing a distribution. """ pvd_element = find_mandatory_field( element, "ParameterValueDistribution" ) if pvd_element.find("Stochastic") is not None: dist = Stochastic.parse( find_mandatory_field(pvd_element, "Stochastic") ) elif pvd_element.find("Deterministic") is not None: dist = Deterministic.parse( find_mandatory_field(pvd_element, "Deterministic") ) else: raise NotAValidElement( "ParameterValueDistribution is missing a distribution" ) pvd = ParameterValueDistribution( "", "", find_mandatory_field(pvd_element, "ScenarioFile").attrib[ "filepath" ], dist, ) pvd.header = FileHeader.parse( find_mandatory_field(element, "FileHeader") ) return pvd def get_element(self) -> ET.Element: """Returns the ElementTree of the ParameterValueDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the ParameterValueDistribution. Raises ------ OpenSCENARIOVersionError If the version is not supported. """ if self.isVersion(minor=0): raise OpenSCENARIOVersionError( "Everything related to ParameterValueDistribution was " "introduced in OpenSCENARIO V1.1" ) element = ET.Element( "OpenSCENARIO", attrib={ "xmlns:xsi": self._XMLNS, "xsi:noNamespaceSchemaLocation": self._XSI, }, ) element.append(self.header.get_element()) parameterdist = ET.SubElement(element, "ParameterValueDistribution") ET.SubElement( parameterdist, "ScenarioFile", attrib={"filepath": self.scenario_file}, ) parameterdist.append(self.parameter_distribution.get_element()) return element def write_xml( self, filename: str, prettyprint: bool = True, encoding: str = "utf-8" ): """Writes the ParameterValueDistribution XML file. Parameters ---------- filename : str Path and filename of the desired XML file. prettyprint : bool, optional Whether to pretty print the XML file. Default is True. encoding : str, optional Encoding of the file. Default is "utf-8". """ printToFile(self.get_element(), filename, prettyprint, encoding)Creates the ParameterValueDistribution file for OpenSCENARIO.
Parameters
description:str- A description of the parameter set.
author:str- The author of the file.
scenario_file:str- Path to the scenario that the parameter distribution should set.
parameter_distribution:StochasticorDeterministic- The parameter distribution.
osc_minor_version:int, optional- The OSI version wanted. Default is _MINOR_VERSION.
header_properties:Properties, optional- Properties that can be added to the header. Default is None.
license:License, optional- Optional license to the file header. Default is None.
Attributes
header:FileHeader- The file header.
scenario_file:str- Path to the scenario that the parameter distribution should set.
parameter_distribution:StochasticorDeterministic- The parameter distribution.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. write_xml(filename, prettyprint, encoding) Writes an OpenSCENARIO XML file. get_element(elementname) Returns the full ElementTree of the class.
Initializes the ParameterValueDistribution.
Parameters
description:str- A description of the parameter set.
author:str- The author of the file.
scenario_file:str- Path to the scenario that the parameter distribution should set.
parameter_distribution:StochasticorDeterministic- The parameter distribution.
osc_minor_version:int, optional- The OSI version wanted. Default is _MINOR_VERSION.
header_properties:Properties, optional- Properties that can be added to the header. Default is None.
license:License, optional- Optional license to the file header. Default is None.
Raises
TypeError- If the parameter_distribution is not of type Stochastic or Deterministic.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> ParameterValueDistribution-
Expand source code
@staticmethod def parse(element: ET.Element) -> "ParameterValueDistribution": """Parses the XML element of ParameterValueDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A ParameterValueDistribution element (same as generated by the class itself). Returns ------- ParameterValueDistribution A ParameterValueDistribution object. Raises ------ NotAValidElement If the ParameterValueDistribution is missing a distribution. """ pvd_element = find_mandatory_field( element, "ParameterValueDistribution" ) if pvd_element.find("Stochastic") is not None: dist = Stochastic.parse( find_mandatory_field(pvd_element, "Stochastic") ) elif pvd_element.find("Deterministic") is not None: dist = Deterministic.parse( find_mandatory_field(pvd_element, "Deterministic") ) else: raise NotAValidElement( "ParameterValueDistribution is missing a distribution" ) pvd = ParameterValueDistribution( "", "", find_mandatory_field(pvd_element, "ScenarioFile").attrib[ "filepath" ], dist, ) pvd.header = FileHeader.parse( find_mandatory_field(element, "FileHeader") ) return pvdParses the XML element of ParameterValueDistribution.
Parameters
element:xml.etree.ElementTree.Element- A ParameterValueDistribution element (same as generated by the class itself).
Returns
ParameterValueDistribution- A ParameterValueDistribution object.
Raises
NotAValidElement- If the ParameterValueDistribution is missing a distribution.
Methods
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the ParameterValueDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the ParameterValueDistribution. Raises ------ OpenSCENARIOVersionError If the version is not supported. """ if self.isVersion(minor=0): raise OpenSCENARIOVersionError( "Everything related to ParameterValueDistribution was " "introduced in OpenSCENARIO V1.1" ) element = ET.Element( "OpenSCENARIO", attrib={ "xmlns:xsi": self._XMLNS, "xsi:noNamespaceSchemaLocation": self._XSI, }, ) element.append(self.header.get_element()) parameterdist = ET.SubElement(element, "ParameterValueDistribution") ET.SubElement( parameterdist, "ScenarioFile", attrib={"filepath": self.scenario_file}, ) parameterdist.append(self.parameter_distribution.get_element()) return elementReturns the ElementTree of the ParameterValueDistribution.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the ParameterValueDistribution.
Raises
OpenSCENARIOVersionError- If the version is not supported.
def write_xml(self, filename: str, prettyprint: bool = True, encoding: str = 'utf-8')-
Expand source code
def write_xml( self, filename: str, prettyprint: bool = True, encoding: str = "utf-8" ): """Writes the ParameterValueDistribution XML file. Parameters ---------- filename : str Path and filename of the desired XML file. prettyprint : bool, optional Whether to pretty print the XML file. Default is True. encoding : str, optional Encoding of the file. Default is "utf-8". """ printToFile(self.get_element(), filename, prettyprint, encoding)Writes the ParameterValueDistribution XML file.
Parameters
filename:str- Path and filename of the desired XML file.
prettyprint:bool, optional- Whether to pretty print the XML file. Default is True.
encoding:str, optional- Encoding of the file. Default is "utf-8".
class ParameterValueSet-
Expand source code
class ParameterValueSet(VersionBase): """Creates the ParameterValueSet used by the DeterministicMultiParameterDistribution. Attributes ---------- assignments : list of ParameterAssignment The assignments for the ValueSet. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_parameter(parameterref, value) Adds a parameter value pair. get_element(elementname) Returns the full ElementTree of the class. """ def __init__(self): """Initializes the ParameterValueSet.""" self.sets = [] def __eq__(self, other: object) -> bool: if isinstance(other, ParameterValueSet): if self.sets == other.sets: return True return False @staticmethod def parse(element: ET.Element) -> "ParameterValueSet": """Parses the XML element of ParameterValueSet. Parameters ---------- element : xml.etree.ElementTree.Element A ParameterValueSet element (same as generated by the class itself). Returns ------- ParameterValueSet A ParameterValueSet object. """ parameter_value_set = ParameterValueSet() for pa in element.findall("ParameterAssignment"): parameter_value_set.sets.append(ParameterAssignment.parse(pa)) return parameter_value_set def add_parameter(self, parameterref: str, value: str) -> None: """Adds a parameter and a value. Parameters ---------- parameterref : str Name of the parameter. value : str Value of the parameter. """ self.sets.append(ParameterAssignment(parameterref, value)) return self def get_element(self) -> ET.Element: """Returns the ElementTree of the ParameterValueSet. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the ParameterValueSet. Raises ------ NotEnoughInputArguments If no sets have been added. """ element = ET.Element("ParameterValueSet") if not self.sets: raise NotEnoughInputArguments("No sets have been added") for s in self.sets: element.append(s.get_element()) return elementCreates the ParameterValueSet used by the DeterministicMultiParameterDistribution.
Attributes
assignments:listofParameterAssignment- The assignments for the ValueSet.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_parameter(parameterref, value) Adds a parameter value pair. get_element(elementname) Returns the full ElementTree of the class.
Initializes the ParameterValueSet.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> ParameterValueSet-
Expand source code
@staticmethod def parse(element: ET.Element) -> "ParameterValueSet": """Parses the XML element of ParameterValueSet. Parameters ---------- element : xml.etree.ElementTree.Element A ParameterValueSet element (same as generated by the class itself). Returns ------- ParameterValueSet A ParameterValueSet object. """ parameter_value_set = ParameterValueSet() for pa in element.findall("ParameterAssignment"): parameter_value_set.sets.append(ParameterAssignment.parse(pa)) return parameter_value_setParses the XML element of ParameterValueSet.
Parameters
element:xml.etree.ElementTree.Element- A ParameterValueSet element (same as generated by the class itself).
Returns
ParameterValueSet- A ParameterValueSet object.
Methods
def add_parameter(self, parameterref: str, value: str) ‑> None-
Expand source code
def add_parameter(self, parameterref: str, value: str) -> None: """Adds a parameter and a value. Parameters ---------- parameterref : str Name of the parameter. value : str Value of the parameter. """ self.sets.append(ParameterAssignment(parameterref, value)) return selfAdds a parameter and a value.
Parameters
parameterref:str- Name of the parameter.
value:str- Value of the parameter.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the ParameterValueSet. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the ParameterValueSet. Raises ------ NotEnoughInputArguments If no sets have been added. """ element = ET.Element("ParameterValueSet") if not self.sets: raise NotEnoughInputArguments("No sets have been added") for s in self.sets: element.append(s.get_element()) return elementReturns the ElementTree of the ParameterValueSet.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the ParameterValueSet.
Raises
NotEnoughInputArguments- If no sets have been added.
class PoissonDistribution (expected_value: float,
range: Range | None = None)-
Expand source code
class PoissonDistribution(_StochasticDistributionType): """The PoissonDistribution is a Stochastic distribution. Parameters ---------- expected_value : float The expected value of the distribution. range : Range, optional Limit of the values. Default is None. Attributes ---------- expected_value : float The expected value of the distribution. range : Range Limit of the values. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class. """ def __init__(self, expected_value: float, range: Optional[Range] = None): """Initializes the PoissonDistribution. Parameters ---------- expected_value : float The expected value of the distribution. range : Range, optional Limit of the values. Default is None. """ if range and not isinstance(range, Range): raise TypeError("range input is not of type Range") self.expected_value = expected_value self.range = range def __eq__(self, other: object) -> bool: if isinstance(other, PoissonDistribution): if ( self.get_attributes() == other.get_attributes() and self.range == other.range ): return True return False @staticmethod def parse(element: ET.Element) -> "PoissonDistribution": """Parses the XML element of PoissonDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A PoissonDistribution element (same as generated by the class itself). Returns ------- PoissonDistribution A PoissonDistribution object. """ if element.find("Range") is not None: range = Range.parse(find_mandatory_field(element, "Range")) else: range = None pd = PoissonDistribution(element.attrib["expectedValue"], range) return pd def get_attributes(self) -> dict: """Returns the attributes of the PoissonDistribution as a dictionary. Returns ------- dict A dictionary containing the attributes of the PoissonDistribution. """ retdict = {"expectedValue": str(self.expected_value)} return retdict def get_element(self) -> ET.Element: """Returns the ElementTree of the PoissonDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the PoissonDistribution. """ element = ET.Element("PoissonDistribution", self.get_attributes()) if self.range: element.append(self.range.get_element()) return elementThe PoissonDistribution is a Stochastic distribution.
Parameters
expected_value:float- The expected value of the distribution.
range:Range, optional- Limit of the values. Default is None.
Attributes
expected_value:float- The expected value of the distribution.
range:Range- Limit of the values.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class.
Initializes the PoissonDistribution.
Parameters
expected_value:float- The expected value of the distribution.
range:Range, optional- Limit of the values. Default is None.
Ancestors
- scenariogeneration.xosc.utils._StochasticDistributionType
- VersionBase
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> PoissonDistribution-
Expand source code
@staticmethod def parse(element: ET.Element) -> "PoissonDistribution": """Parses the XML element of PoissonDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A PoissonDistribution element (same as generated by the class itself). Returns ------- PoissonDistribution A PoissonDistribution object. """ if element.find("Range") is not None: range = Range.parse(find_mandatory_field(element, "Range")) else: range = None pd = PoissonDistribution(element.attrib["expectedValue"], range) return pdParses the XML element of PoissonDistribution.
Parameters
element:xml.etree.ElementTree.Element- A PoissonDistribution element (same as generated by the class itself).
Returns
PoissonDistribution- A PoissonDistribution object.
Methods
def get_attributes(self) ‑> dict-
Expand source code
def get_attributes(self) -> dict: """Returns the attributes of the PoissonDistribution as a dictionary. Returns ------- dict A dictionary containing the attributes of the PoissonDistribution. """ retdict = {"expectedValue": str(self.expected_value)} return retdictReturns the attributes of the PoissonDistribution as a dictionary.
Returns
dict- A dictionary containing the attributes of the PoissonDistribution.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the PoissonDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the PoissonDistribution. """ element = ET.Element("PoissonDistribution", self.get_attributes()) if self.range: element.append(self.range.get_element()) return elementReturns the ElementTree of the PoissonDistribution.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the PoissonDistribution.
class ProbabilityDistributionSet-
Expand source code
class ProbabilityDistributionSet(_StochasticDistributionType): """The ProbabilityDistributionSet is a Stochastic distribution. Attributes ---------- sets : list of _ProbabilityDistributionSetElement The sets defining the ProbabilityDistributionSet. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. add_set(value, weight) Adds a value and a weight to a set. """ def __init__(self): """Initializes the ProbabilityDistributionSet.""" self.sets = [] def __eq__(self, other: object) -> bool: if isinstance(other, ProbabilityDistributionSet): if self.sets == other.sets: return True return False @staticmethod def parse(element: ET.Element) -> "ProbabilityDistributionSet": """Parses the XML element of ProbabilityDistributionSet. Parameters ---------- element : xml.etree.ElementTree.Element A ProbabilityDistributionSet element (same as generated by the class itself). Returns ------- ProbabilityDistributionSet A ProbabilityDistributionSet object. """ pds = ProbabilityDistributionSet() for e in element.findall("Element"): pds.sets.append(_ProbabilityDistributionSetElement.parse(e)) return pds def add_set(self, value: str, weight: float) -> None: """Adds a value and a weight to a set. Parameters ---------- value : str Possible value. weight : float Weight of the value. """ self.sets.append(_ProbabilityDistributionSetElement(value, weight)) return self def get_element(self) -> ET.Element: """Returns the ElementTree of the ProbabilityDistributionSet. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the ProbabilityDistributionSet. Raises ------ NotEnoughInputArguments If no sets were added to the ProbabilityDistributionSet. """ element = ET.Element("ProbabilityDistributionSet") if not self.sets: raise NotEnoughInputArguments( "No sets were added to the ProbabilityDistributionSet, please use add_set" ) for bin in self.sets: element.append(bin.get_element()) return elementThe ProbabilityDistributionSet is a Stochastic distribution.
Attributes
sets:listof_ProbabilityDistributionSetElement- The sets defining the ProbabilityDistributionSet.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. add_set(value, weight) Adds a value and a weight to a set.
Initializes the ProbabilityDistributionSet.
Ancestors
- scenariogeneration.xosc.utils._StochasticDistributionType
- VersionBase
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> ProbabilityDistributionSet-
Expand source code
@staticmethod def parse(element: ET.Element) -> "ProbabilityDistributionSet": """Parses the XML element of ProbabilityDistributionSet. Parameters ---------- element : xml.etree.ElementTree.Element A ProbabilityDistributionSet element (same as generated by the class itself). Returns ------- ProbabilityDistributionSet A ProbabilityDistributionSet object. """ pds = ProbabilityDistributionSet() for e in element.findall("Element"): pds.sets.append(_ProbabilityDistributionSetElement.parse(e)) return pdsParses the XML element of ProbabilityDistributionSet.
Parameters
element:xml.etree.ElementTree.Element- A ProbabilityDistributionSet element (same as generated by the class itself).
Returns
ProbabilityDistributionSet- A ProbabilityDistributionSet object.
Methods
def add_set(self, value: str, weight: float) ‑> None-
Expand source code
def add_set(self, value: str, weight: float) -> None: """Adds a value and a weight to a set. Parameters ---------- value : str Possible value. weight : float Weight of the value. """ self.sets.append(_ProbabilityDistributionSetElement(value, weight)) return selfAdds a value and a weight to a set.
Parameters
value:str- Possible value.
weight:float- Weight of the value.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the ProbabilityDistributionSet. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the ProbabilityDistributionSet. Raises ------ NotEnoughInputArguments If no sets were added to the ProbabilityDistributionSet. """ element = ET.Element("ProbabilityDistributionSet") if not self.sets: raise NotEnoughInputArguments( "No sets were added to the ProbabilityDistributionSet, please use add_set" ) for bin in self.sets: element.append(bin.get_element()) return elementReturns the ElementTree of the ProbabilityDistributionSet.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the ProbabilityDistributionSet.
Raises
NotEnoughInputArguments- If no sets were added to the ProbabilityDistributionSet.
class Range (lower: float, upper: float)-
Expand source code
class Range(VersionBase): """The Range creates a Range used by parameter distributions. Parameters ---------- lower : float Lower limit of the range. upper : float Upper limit of the range. Attributes ---------- lower : float Lower limit of the range. upper : float Upper limit of the range. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class. """ def __init__(self, lower: float, upper: float): """Initializes the Range. Parameters ---------- lower : float Lower limit of the range. upper : float Upper limit of the range. """ self.lower = convert_float(lower) self.upper = convert_float(upper) def __eq__(self, other: object) -> bool: if isinstance(other, Range): if self.get_attributes() == other.get_attributes(): return True return False @staticmethod def parse(element: ET.Element) -> "Range": """Parses the XML element of Range. Parameters ---------- element : xml.etree.ElementTree.Element A Range element (same as generated by the class itself). Returns ------- Range A Range object. """ return Range( convert_float(element.attrib["lowerLimit"]), convert_float(element.attrib["upperLimit"]), ) def get_attributes(self) -> dict: """Returns the attributes of the Range as a dictionary. Returns ------- dict A dictionary containing the attributes of the Range. """ retdict = { "lowerLimit": str(self.lower), "upperLimit": str(self.upper), } return retdict def get_element(self, elementname: str = "Range") -> ET.Element: """Returns the ElementTree of the Range. Parameters ---------- elementname : str, optional The name of the element. Default is "Range". Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Range. """ element = ET.Element(elementname, self.get_attributes()) return elementThe Range creates a Range used by parameter distributions.
Parameters
lower:float- Lower limit of the range.
upper:float- Upper limit of the range.
Attributes
lower:float- Lower limit of the range.
upper:float- Upper limit of the range.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class.
Initializes the Range.
Parameters
lower:float- Lower limit of the range.
upper:float- Upper limit of the range.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> Range-
Expand source code
@staticmethod def parse(element: ET.Element) -> "Range": """Parses the XML element of Range. Parameters ---------- element : xml.etree.ElementTree.Element A Range element (same as generated by the class itself). Returns ------- Range A Range object. """ return Range( convert_float(element.attrib["lowerLimit"]), convert_float(element.attrib["upperLimit"]), )Parses the XML element of Range.
Parameters
element:xml.etree.ElementTree.Element- A Range element (same as generated by the class itself).
Returns
Range- A Range object.
Methods
def get_attributes(self) ‑> dict-
Expand source code
def get_attributes(self) -> dict: """Returns the attributes of the Range as a dictionary. Returns ------- dict A dictionary containing the attributes of the Range. """ retdict = { "lowerLimit": str(self.lower), "upperLimit": str(self.upper), } return retdictReturns the attributes of the Range as a dictionary.
Returns
dict- A dictionary containing the attributes of the Range.
def get_element(self, elementname: str = 'Range') ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self, elementname: str = "Range") -> ET.Element: """Returns the ElementTree of the Range. Parameters ---------- elementname : str, optional The name of the element. Default is "Range". Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Range. """ element = ET.Element(elementname, self.get_attributes()) return elementReturns the ElementTree of the Range.
Parameters
elementname:str, optional- The name of the element. Default is "Range".
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the Range.
class Stochastic (number_of_tests: int, random_seed: float = None)-
Expand source code
class Stochastic(VersionBase): """Creates the Stochastic type of parameter distributions. Parameters ---------- number_of_tests : int Number of tests to run. random_seed : float, optional The seed for the run. Default is None. Attributes ---------- number_of_tests : int Number of tests to run. random_seed : float The seed for the run. distributions : dict of *StochasticDistribution The distributions for the parameters. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_distribution(parametername, distribution) Adds a parameter/distribution pair. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class. """ def __init__(self, number_of_tests: int, random_seed: float = None): """Initializes the Stochastic. Parameters ---------- number_of_tests : int Number of tests to run. random_seed : float, optional The seed for the run. Default is None. """ self.number_of_tests = number_of_tests self.random_seed = random_seed self.distributions = {} def __eq__(self, other: object) -> bool: if isinstance(other, Stochastic): if ( self.get_attributes() == other.get_attributes() and self.distributions == other.distributions ): return True return False @staticmethod def parse(element: ET.Element) -> "Stochastic": """Parses the XML element of Stochastic. Parameters ---------- element : xml.etree.ElementTree.Element A Stochastic element (same as generated by the class itself). Returns ------- Stochastic A Stochastic object. """ if "randomSeed" in element.attrib: seed = element.attrib["randomSeed"] else: seed = None stoc = Stochastic(element.attrib["numberOfTestRuns"], seed) for dist_element in element.findall("StochasticDistribution"): name = dist_element.attrib["parameterName"] dist = _StochasticFactory.parse_distribution(dist_element) stoc.add_distribution(name, dist) return stoc def add_distribution( self, parametername: str, distribution: _StochasticDistributionType ) -> None: """Adds a parameter and a related distribution to it. Parameters ---------- parametername : str Name of the parameter. distribution : StochasticDistribution The distribution of that parameter. Raises ------ TypeError If the distribution is not a valid StochasticDistribution. """ if not isinstance(distribution, _StochasticDistributionType): raise TypeError( "distribution input is not a valid StochasticDistribution" ) self.distributions[parametername] = distribution return self def get_attributes(self) -> dict: """Returns the attributes of the Stochastic as a dictionary. Returns ------- dict A dictionary containing the attributes of the Stochastic. """ retdict = {} retdict["numberOfTestRuns"] = str(self.number_of_tests) if self.random_seed is not None: retdict["randomSeed"] = str(self.random_seed) return retdict def get_element(self) -> ET.Element: """Returns the ElementTree of the Stochastic. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Stochastic. Raises ------ NotEnoughInputArguments If no distribution has been added. """ element = ET.Element("Stochastic", self.get_attributes()) if not self.distributions: raise NotEnoughInputArguments("No distribution has been added") for key, value in self.distributions.items(): dist = ET.SubElement( element, "StochasticDistribution", attrib={"parameterName": key}, ) dist.append(value.get_element()) return elementCreates the Stochastic type of parameter distributions.
Parameters
number_of_tests:int- Number of tests to run.
random_seed:float, optional- The seed for the run. Default is None.
Attributes
number_of_tests:int- Number of tests to run.
random_seed:float- The seed for the run.
distributions:dictof*StochasticDistribution- The distributions for the parameters.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. add_distribution(parametername, distribution) Adds a parameter/distribution pair. get_element(elementname) Returns the full ElementTree of the class. get_attributes() Returns a dictionary of all attributes of the class.
Initializes the Stochastic.
Parameters
number_of_tests:int- Number of tests to run.
random_seed:float, optional- The seed for the run. Default is None.
Ancestors
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> Stochastic-
Expand source code
@staticmethod def parse(element: ET.Element) -> "Stochastic": """Parses the XML element of Stochastic. Parameters ---------- element : xml.etree.ElementTree.Element A Stochastic element (same as generated by the class itself). Returns ------- Stochastic A Stochastic object. """ if "randomSeed" in element.attrib: seed = element.attrib["randomSeed"] else: seed = None stoc = Stochastic(element.attrib["numberOfTestRuns"], seed) for dist_element in element.findall("StochasticDistribution"): name = dist_element.attrib["parameterName"] dist = _StochasticFactory.parse_distribution(dist_element) stoc.add_distribution(name, dist) return stocParses the XML element of Stochastic.
Parameters
element:xml.etree.ElementTree.Element- A Stochastic element (same as generated by the class itself).
Returns
Stochastic- A Stochastic object.
Methods
def add_distribution(self,
parametername: str,
distribution: scenariogeneration.xosc.utils._StochasticDistributionType) ‑> None-
Expand source code
def add_distribution( self, parametername: str, distribution: _StochasticDistributionType ) -> None: """Adds a parameter and a related distribution to it. Parameters ---------- parametername : str Name of the parameter. distribution : StochasticDistribution The distribution of that parameter. Raises ------ TypeError If the distribution is not a valid StochasticDistribution. """ if not isinstance(distribution, _StochasticDistributionType): raise TypeError( "distribution input is not a valid StochasticDistribution" ) self.distributions[parametername] = distribution return selfAdds a parameter and a related distribution to it.
Parameters
parametername:str- Name of the parameter.
distribution:StochasticDistribution- The distribution of that parameter.
Raises
TypeError- If the distribution is not a valid StochasticDistribution.
def get_attributes(self) ‑> dict-
Expand source code
def get_attributes(self) -> dict: """Returns the attributes of the Stochastic as a dictionary. Returns ------- dict A dictionary containing the attributes of the Stochastic. """ retdict = {} retdict["numberOfTestRuns"] = str(self.number_of_tests) if self.random_seed is not None: retdict["randomSeed"] = str(self.random_seed) return retdictReturns the attributes of the Stochastic as a dictionary.
Returns
dict- A dictionary containing the attributes of the Stochastic.
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the Stochastic. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the Stochastic. Raises ------ NotEnoughInputArguments If no distribution has been added. """ element = ET.Element("Stochastic", self.get_attributes()) if not self.distributions: raise NotEnoughInputArguments("No distribution has been added") for key, value in self.distributions.items(): dist = ET.SubElement( element, "StochasticDistribution", attrib={"parameterName": key}, ) dist.append(value.get_element()) return elementReturns the ElementTree of the Stochastic.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the Stochastic.
Raises
NotEnoughInputArguments- If no distribution has been added.
class UniformDistribution (range: Range)-
Expand source code
class UniformDistribution(_StochasticDistributionType): """The UniformDistribution is a Stochastic distribution. Parameters ---------- range : Range Limit of the values. Attributes ---------- range : Range Limit of the values. Methods ------- parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class. """ def __init__(self, range: Range): """Initializes the UniformDistribution. Parameters ---------- range : Range Limit of the values. """ if not isinstance(range, Range): raise TypeError("range input is not of type Range") self.range = range def __eq__(self, other: object) -> bool: if isinstance(other, UniformDistribution): if self.range == other.range: return True return False @staticmethod def parse(element: ET.Element) -> "UniformDistribution": """Parses the XML element of UniformDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A UniformDistribution element (same as generated by the class itself). Returns ------- UniformDistribution A UniformDistribution object. """ return UniformDistribution( Range.parse(find_mandatory_field(element, "Range")) ) def get_element(self) -> ET.Element: """Returns the ElementTree of the UniformDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the UniformDistribution. """ element = ET.Element("UniformDistribution") element.append(self.range.get_element()) return elementThe UniformDistribution is a Stochastic distribution.
Parameters
range:Range- Limit of the values.
Attributes
range:Range- Limit of the values.
Methods
parse(element) Parses an ElementTree created by the class and returns an instance of the class. get_element(elementname) Returns the full ElementTree of the class.
Initializes the UniformDistribution.
Parameters
range:Range- Limit of the values.
Ancestors
- scenariogeneration.xosc.utils._StochasticDistributionType
- VersionBase
Static methods
def parse(element: xml.etree.ElementTree.Element) ‑> UniformDistribution-
Expand source code
@staticmethod def parse(element: ET.Element) -> "UniformDistribution": """Parses the XML element of UniformDistribution. Parameters ---------- element : xml.etree.ElementTree.Element A UniformDistribution element (same as generated by the class itself). Returns ------- UniformDistribution A UniformDistribution object. """ return UniformDistribution( Range.parse(find_mandatory_field(element, "Range")) )Parses the XML element of UniformDistribution.
Parameters
element:xml.etree.ElementTree.Element- A UniformDistribution element (same as generated by the class itself).
Returns
UniformDistribution- A UniformDistribution object.
Methods
def get_element(self) ‑> xml.etree.ElementTree.Element-
Expand source code
def get_element(self) -> ET.Element: """Returns the ElementTree of the UniformDistribution. Returns ------- xml.etree.ElementTree.Element The ElementTree representation of the UniformDistribution. """ element = ET.Element("UniformDistribution") element.append(self.range.get_element()) return elementReturns the ElementTree of the UniformDistribution.
Returns
xml.etree.ElementTree.Element- The ElementTree representation of the UniformDistribution.