openpytea.io¶
- openpytea.io.load_equipment_config(filepath)[source]¶
Load equipment configuration from a JSON file. Parses a JSON file containing equipment specifications and returns a list of Equipment objects. The JSON file must have a top-level ‘equipment’ key containing a list of equipment entries. Args:
filepath (str): Path to the JSON file containing equipment data.
- Returns:
list[Equipment]: A list of Equipment objects constructed from the JSON data.
- Raises:
ValueError: If the JSON file is missing the ‘equipment’ key. ValueError: If ‘equipment’ is not a list. ValueError: If any equipment entry is not a dictionary. ValueError: If any equipment entry is missing required keys
(‘name’, ‘process_type’, ‘category’).
ValueError: If any equipment entry defines neither ‘param’ nor ‘purchased_cost’.
- Notes:
Required keys per equipment entry: ‘name’, ‘process_type’, ‘category’
Each entry must specify either ‘param’ or ‘purchased_cost’
Default values: material=’Carbon steel’, target_year=2024
Optional keys: ‘type’, ‘material’, ‘num_units’, ‘purchased_cost’, ‘cost_year’, ‘cost_func’, ‘target_year’
- openpytea.io.load_plant_config(filepath, equipment_list)[source]¶
Load a plant configuration from a JSON file and create a Plant instance. This function reads a JSON configuration file, validates its structure, and combines it with a provided equipment list to instantiate a Plant object. Args:
filepath (str): Path to the JSON configuration file containing plant data.
The file must contain a top-level ‘plant’ key with the plant configuration.
equipment_list (list): A list of equipment objects to be associated with the plant.
- Returns:
Plant: A Plant instance initialized with the configuration data and equipment list.
- Raises:
ValueError: If the JSON file does not contain a top-level ‘plant’ key. FileNotFoundError: If the specified filepath does not exist. json.JSONDecodeError: If the file is not valid JSON.
- Example:
>>> equipment = [Equipment(...), Equipment(...)] >>> plant = load_plant_config('plant_config.json', equipment)
- openpytea.io.load_analysis_config(filepath)[source]¶
Load analysis configuration from a JSON file. This function reads a JSON file from the specified filepath and validates that it contains the required ‘analysis’ key. Args:
filepath (str): The path to the JSON configuration file to load.
- Returns:
dict: The parsed JSON data containing the analysis configuration.
- Raises:
ValueError: If the JSON file does not contain an ‘analysis’ key. FileNotFoundError: If the specified filepath does not exist. json.JSONDecodeError: If the file is not valid JSON.
- Example:
>>> config = load_analysis_config('analysis.json') >>> print(config['analysis'])
- openpytea.io.load_results(filepath)[source]¶
Load results from a JSON file. Args:
filepath (str): The path to the JSON file containing results.
- Returns:
list or dict: The results data extracted from the JSON file’s ‘results’ key.
- Raises:
ValueError: If the JSON file does not contain a ‘results’ key.
- Examples:
>>> results = load_results('results.json') >>> print(results)
- openpytea.io.export_equipment_strings(equipment_list, filepath)[source]¶
Export a list of equipment objects to a text file. Each equipment object is converted to a string representation and written to a separate line in the output file. Args:
equipment_list (list): A list of equipment objects to export. filepath (str or Path): The file path where the equipment strings will
be written. Can be a string or a Path object.
- Returns:
None
- Raises:
IOError: If the file cannot be opened or written to. TypeError: If equipment_list is not iterable.
- Example:
>>> equipment_list = [Equipment("pump"), Equipment("motor")] >>> export_equipment_strings(equipment_list, "equipment.txt")
- openpytea.io.export_equipment_results(equipment_list, filepath)[source]¶
Export equipment results to a JSON file. Converts a list of equipment objects to a dictionary format and writes them to a JSON file along with metadata and cost totals. :param equipment_list: A list of equipment objects that have a to_dict() method for
serialization.
- Parameters:
filepath (
strorPath) – The file path where the JSON output will be written. Can be a string or a pathlib.Path object.- Return type:
Notes
The output JSON file contains: - metadata: Information about the export including OpenPyTEA version,
generation timestamp (UTC), and number of equipment items.
equipment: List of equipment dictionaries.
totals: Aggregated cost totals including purchased cost and direct cost.
The JSON file is written with 4-space indentation for readability.
Examples
>>> equipment_list = [eq1, eq2, eq3] >>> export_equipment_results(equipment_list, "equipment_export.json")
- openpytea.io.export_plant_results(plant, filepath)[source]¶
Export plant results to a JSON file. Serializes the plant object and metadata to a JSON file at the specified filepath. The output includes version information and the timestamp of when the export was generated. :param plant: The plant object containing results to be exported. :type plant:
Plant:param filepath: The destination file path where the JSON file will be written.Can be a string or pathlib.Path object.
- Return type:
Notes
The exported JSON file contains: - metadata: Generated by information and UTC timestamp - plant data: All plant object attributes via to_dict() method The file is created with UTF-8 encoding and indented JSON formatting (4 spaces).
Examples
>>> from openpytea.io import export_plant_results >>> plant = Plant(...) >>> export_plant_results(plant, "output/plant_results.json")
- openpytea.io.run_equipment(input_path, output_path)[source]¶
Load equipment configuration from file and export results to a specified output path.
This function reads equipment configuration data from an input file, processes it, and writes the results to an output file.
- Parameters:
- Returns:
A list of equipment objects loaded from the input configuration file.
- Return type:
Examples
>>> equipment_list = run_equipment('config/equipment.json', 'output/results.json') >>> print(len(equipment_list))
- openpytea.io.run_plant(plant_input_path, plant_output_path, equipment_input_path=None, equipment_list=None)[source]¶
Load a plant configuration, calculate all plant metrics, and export results. This function orchestrates the complete workflow for plant analysis by loading the plant configuration, computing all relevant calculations, and saving the results to a specified output path. :param plant_input_path: File path to the plant configuration input file. :type plant_input_path:
str:param plant_output_path: File path where the plant results will be exported. :type plant_output_path:str:param equipment_input_path: File path to the equipment configuration input file. Required ifequipment_list is not provided. Default is None.
- Parameters:
equipment_list (
list, optional) – List of equipment configurations. If not provided, will be loaded from equipment_input_path. Default is None.- Returns:
The calculated plant object containing all computed metrics and results.
- Return type:
Plant- Raises:
ValueError – If neither equipment_input_path nor equipment_list is provided.
Examples
>>> plant = run_plant('plant_config.yaml', 'results.csv', ... equipment_input_path='equipment_config.yaml')
- openpytea.io.run_tea(equipment_input_path, plant_input_path, analysis_input_path, output_dir='results')[source]¶
Execute a complete Techno-Economic Analysis (TEA) workflow. This function orchestrates the entire TEA pipeline by loading configuration files, performing calculations, running specified analyses, and exporting results as JSON files and/or plots. :param equipment_input_path: Path to the equipment configuration file. :type equipment_input_path:
strorPath:param plant_input_path: Path to the plant configuration file. :type plant_input_path:strorPath:param analysis_input_path: Path to the analysis configuration file specifying which analyses torun and their parameters.
- Parameters:
output_dir (
strorPath, optional) – Directory where results will be saved. If None, uses the directory specified in the analysis configuration file. Defaults to “results”.- Returns:
Dictionary containing analysis results with keys corresponding to the analyses that were run: - “direct_costs”: Direct cost breakdown by equipment - “fixed_capital”: Fixed capital cost breakdown - “fixed_opex”: Fixed operating expenditure breakdown - “variable_opex”: Variable operating expenditure breakdown - “tornado”: Tornado/sensitivity analysis results - “monte_carlo”: Monte Carlo simulation results with metrics - “sensitivity”: Dictionary of sensitivity analysis cases
- Return type:
- Raises:
ValueError – If a requested Monte Carlo metric is not found in the results.
Notes
Creates output directory if it does not exist when saving results
Automatically clears figures after saving to free memory
- JSON exports include equipment results, plant results, and
analysis results with metadata
- Plot format and resolution (DPI) are configurable via the
analysis configuration file