bw2io.extractors.simapro_csv
#
Module Contents#
Classes#
Extract datasets from SimaPro CSV export files. |
Functions#
|
Replace all occurrences of elements of |
|
Convert a string to a number. |
Attributes#
- exception bw2io.extractors.simapro_csv.EndOfDatasets[source]#
Bases:
Exception
Raise exception when there are no more datasets to iterate.
Initialize self. See help(type(self)) for accurate signature.
- class bw2io.extractors.simapro_csv.SimaProCSVExtractor[source]#
Bases:
object
Extract datasets from SimaPro CSV export files.
The CSV file should be in a specific format, with row 1 containing either the string “SimaPro” or “CSV separator.”
Parameters#
- filepathstr
The path to the SimaPro CSV export file.
- delimiterstr, optional
The delimiter in the CSV file. Default is “;”.
- namestr, optional
The name of the project. If the name is not provided, it is extracted from the CSV file.
- encoding: str, optional
The character encoding in the SimaPro CSV file. Defaults to “cp1252”.
Returns#
- datasetslist
The list of extracted datasets from the CSV file.
- global_parametersdict
The dictionary of global parameters for the CSV file.
- project_metadatadict
The dictionary of project metadata.
Raises#
- AssertionError:
If the CSV file is not a valid Simapro export file.
- classmethod create_distribution(amount, kind, field1, field2, field3)[source]#
Create a distribution based on the given uncertainty data.
Parameters#
- amountstr
The amount of uncertainty.
- kindstr
The kind of uncertainty.
- field1str
The first field of uncertainty data.
- field2str
The second field of uncertainty data.
- field3str
The third field of uncertainty data.
Returns#
- dict
A dictionary representing the distribution.
Raises#
- ValueError
If the given uncertainty type is unknown.
Notes#
This method creates a distribution based on the given uncertainty data. The distribution is returned as a dictionary with the following keys: - “uncertainty type”: the ID of the uncertainty type - “loc”: the location parameter of the distribution - “amount”: the amount of uncertainty Depending on the kind of uncertainty, other keys may be included: - “scale”: the scale parameter of the distribution (for “Lognormal” and “Normal” uncertainties) - “minimum”: the minimum value of the distribution (for “Triangle” and “Uniform” uncertainties) - “maximum”: the maximum value of the distribution (for “Triangle” and “Uniform” uncertainties) - “negative”: True if the amount of uncertainty is negative, False otherwise. If the kind of uncertainty is “Undefined”, an undefined uncertainty distribution is created. If the kind of uncertainty is “Lognormal”, a lognormal uncertainty distribution is created. If the kind of uncertainty is “Normal”, a normal uncertainty distribution is created. If the kind of uncertainty is “Triangle”, a triangular uncertainty distribution is created. If the kind of uncertainty is “Uniform”, a uniform uncertainty distribution is created. If the kind of uncertainty is unknown, a ValueError is raised.
- classmethod extract(filepath, delimiter=';', name=None, encoding='cp1252')[source]#
Extract data from a SimaPro export file (.csv) and returns a list of datasets, global parameters, and project metadata.
Parameters:#
- filepathstr
The file path of the SimaPro export file to extract data from.
- delimiterstr, optional
The delimiter used in the SimaPro export file. Defaults to “;”.
- namestr, optional
The name of the project. If not provided, the method will attempt to infer it from the SimaPro export file.
- encodingstr, optional
The character encoding of the SimaPro export file. Defaults to “cp1252”.
Returns:#
- Tuple[List[Dict], Dict, Dict]
- A tuple containing:
a list of dictionaries representing each dataset extracted from the SimaPro export file,
a dictionary containing global parameters extracted from the SimaPro export file, and
a dictionary containing project metadata extracted from the SimaPro export file.
- classmethod get_global_parameters(data, pm)[source]#
Extract and return global parameters from a SimaPro export file.
- Args:
data (List[List[str]]): A list of lists containing the data read from the SimaPro export file. pm (Dict[str, str]): A dictionary containing project metadata extracted from the SimaPro export file.
- Returns:
- A tuple containing:
parameters (Dict[str, Dict[str, Any]]): A dictionary containing global parameters extracted from the SimaPro export file. Each parameter is represented as a dictionary with keys ‘name’, ‘unit’, ‘formula’, and ‘amount’.
global_precompiled (Dict[str, Pattern]): A dictionary containing compiled regular expression patterns used to search for parameter names in the SimaPro export file.
- Raises:
ValueError: If an invalid parameter is encountered in the SimaPro export file.
- classmethod get_next_process_index(data, index)[source]#
Get the index of the next process in the given data.
Parameters:#
- dataList[List[str]]
The data to search for the next process.
- indexint
The index to start the search from.
Returns:#
- int
The index of the next process in the data.
- classmethod get_project_metadata(data)[source]#
Parse metadata from a list of strings and returns a dictionary of metadata key-value pairs.
Parameters#
- datalist
A list of strings containing metadata in the format “{key}: {value}”.
Returns#
- dict
A dictionary of metadata key-value pairs extracted from the input data list.
Raises#
- ValueError
If a line of metadata does not contain a colon : character, or if it contains multiple colons.
- AssertionError
If a line of metadata does not start and end with curly braces {}.
Notes#
This method assumes that each line in the input data list contains only one metadata key-value pair, and that the key and value are separated by a single colon : character.
Examples#
>>> data = ["{name}: John Smith", "{age}: 25", "", "{country: UK}"] >>> meta = get_project_metadata(data) >>> print(meta) {"name": "John Smith", "age": "25", "country": "UK"}
- classmethod get_project_name(data)[source]#
Extract the project name from the given data.
Parameters#
- datalist
A list of data, where each item is a list of strings representing a row of the data.
Returns#
- str
The project name.
Notes#
This method searches for a row in the data where the first item starts with “{Project:” or “{Projet:”. If such a row is found, the project name is extracted from that row and returned. Otherwise, None is returned.
- classmethod invalid_uncertainty_data(amount, kind, field1, field2, field3)[source]#
Determine if the uncertainty data is invalid.
Parameters#
- amountstr
The amount of uncertainty.
- kindstr
The kind of uncertainty.
- field1str
The first field of uncertainty data.
- field2str
The second field of uncertainty data.
- field3str
The third field of uncertainty data.
Returns#
- bool
True if the uncertainty data is invalid, False otherwise.
Notes#
This method checks if the given uncertainty data is invalid based on the kind of uncertainty. If the kind is “Lognormal” and amount is empty or field1 is “0” or “1”, the uncertainty data is considered invalid.
- classmethod parse_biosphere_flow(line, category, pm)[source]#
Parse biosphere flow line.
name
subcategory
unit
value or formula
uncertainty type
uncert. param.
uncert. param.
uncert. param.
comment
However, sometimes the value is in index 2, and the unit in index 3. Because why not! We assume default ordering unless we find a number in index 2.
- classmethod parse_calculated_parameter(line, pm)[source]#
Parse a line in the ‘Calculated parameters’ section of a SimaPro file and return a dictionary of its components.
Parameters#
- lineList[str]
The line to be parsed, with the first string being the name, the second string the formula, and subsequent strings comments associated with the parameter.
- pmDict[str, float]
A dictionary mapping variable names to their values in the context of the parameter.
Returns#
parsed_parameter : Dict[str, Union[str, List[str]]] A dictionary with the following keys: - ‘name’ : str
The name of the parameter.
- ‘formula’str
The formula used in the parameter, with variables replaced by their values according to pm.
- ‘comment’List[str]
A list of comments on the parameter.
Examples#
#TODO
- classmethod parse_final_waste_flow(line, pm)[source]#
Parse final wate flow line.
0: name 1: subcategory? 2: unit 3. value or formula 4. uncertainty type 5. uncert. param. 6. uncert. param. 7. uncert. param.
However, sometimes the value is in index 2, and the unit in index 3. Because why not! We assume default ordering unless we find a number in index 2.
- classmethod parse_input_line(line, category, pm)[source]#
Parse technosphere input line.
name
unit
value or formula
uncertainty type
uncert. param.
uncert. param.
uncert. param.
comment
However, sometimes the value is in index 1, and the unit in index 2. Because why not! We assume default ordering unless we find a number in index 1.
- classmethod parse_input_parameter(line)[source]#
Parse input parameters section of a SimaPro file.
name
value (not formula)
uncertainty type
uncert. param.
uncert. param.
uncert. param.
hidden (“Yes” or “No” - we ignore)
comment
Returns#
#TODO Examples ——– #TODO
- classmethod parse_reference_product(line, pm)[source]#
Parse reference product line.
name
unit
value or formula
allocation
waste type
category (separated by )
comment
However, sometimes the value is in index 1, and the unit in index 2. Because why not! We assume default ordering unless we find a number in index 1.
- bw2io.extractors.simapro_csv.replace_with_uppercase(string, names, precompiled)[source]#
Replace all occurrences of elements of
names
instring
with their uppercase equivalents.Parameters#
- stringstr
String to be modified.
- nameslist
List of variable name strings that should already all be uppercase.
- precompileddict
Dictionary #TODO.
Returns#
The modified string.
- bw2io.extractors.simapro_csv.to_number(obj)[source]#
Convert a string to a number.
Parameters#
- objstr
The string to be converted to a number
Returns#
- float or str
converted number as float, or the unchanged string if not successfully converted.