Skip to content

Commit

Permalink
Merge pull request #83 from SPF-OST/82-create-scenario-csvs-from-ini-…
Browse files Browse the repository at this point in the history
…file

82 create scenario csvs from ini file
  • Loading branch information
ahobeost authored Jul 15, 2024
2 parents 32b623e + c37f57f commit 9410df4
Show file tree
Hide file tree
Showing 27 changed files with 390 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main
- '80-adjust-energynetworkcreate_nodes_data-to-allow-for-multiple-entry-points'
- '82-create-scenario-csvs-from-ini-file'
pull_request:
branches:
- '*'
Expand Down
12 changes: 8 additions & 4 deletions data/examples/run_example_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pandas as pd
import os
import pathlib as pl

from optihood.IO.writers import ScenarioFileWriterExcel
from optihood.energy_network import EnergyNetworkGroup as EnergyNetwork

if __name__ == '__main__':
Expand All @@ -13,7 +15,7 @@
timePeriod = pd.date_range("2018-01-01 00:00:00", "2018-01-31 23:00:00", freq="60min")

# define paths for input and result files
inputFilePath = f"..\\configs\\basic_example_config"
inputFilePath = pl.Path(f"..\\configs\\basic_example_config")
configFileName = f"scenario_{scenario}_group.ini"
inputfileName = f"scenario_{scenario}.xls" # excel file which would be created by createScenarioFile()

Expand Down Expand Up @@ -49,11 +51,13 @@
# "cbc": {"tee": False}
}

# create scenario file from config file
scenarioFileWriter = ScenarioFileWriterExcel(inputFilePath / configFileName, version='grouped',
nr_of_buildings=numberOfBuildings)
scenarioFileWriter.write(inputFilePath / inputfileName)

# create an energy network and set the network parameters from an excel file
network = EnergyNetwork(timePeriod)
# create scenario file from config file
network.createScenarioFile(os.path.join(inputFilePath, configFileName), os.path.join(inputFilePath, inputfileName),
numberOfBuildings)
network.setFromExcel(os.path.join(inputFilePath, inputfileName), numberOfBuildings, opt=optimizationType,
mergeLinkBuses=mergeLinkBuses, mergeBuses=merge_buses, dispatchMode=dispatchMode)

Expand Down
13 changes: 2 additions & 11 deletions optihood/IO/groupScenarioWriter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from configparser import ConfigParser
import numpy as np
import pandas as pd

import optihood.IO.readers as _rd
import optihood.IO.writers as _sw


def create_scenario_file(configFilePath, excel_file_path, numberOfBuildings, writeToFileOrReturnData='file'):
def create_scenario_file(configFilePath, numberOfBuildings):
"""
function to create the input excel file from a config file
saves the generated excel file at the path given by excel_file_path
Expand Down Expand Up @@ -110,11 +108,7 @@ def create_scenario_file(configFilePath, excel_file_path, numberOfBuildings, wri

add_buildings_to_excel_data(excel_data, numberOfBuildings)

if writeToFileOrReturnData == 'file':
_sw.write_prepared_data_and_sheets_to_excel(excel_file_path, excel_data)
return
elif writeToFileOrReturnData == 'data':
return excel_data
return excel_data


def get_excel_variable_names(isGroup=False):
Expand Down Expand Up @@ -221,6 +215,3 @@ def add_buildings_to_excel_data(excel_data, numberOfBuildings):
buildingNo = [i for i in range(1, numberOfBuildings + 1)] * len(excel_data[sheet].index)
excel_data[sheet] = pd.DataFrame(np.repeat(data.values, numberOfBuildings, axis=0), columns=data.columns)
excel_data[sheet]['building'] = buildingNo



17 changes: 3 additions & 14 deletions optihood/IO/individualScenarioWriter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from configparser import ConfigParser
import numpy as np
import pandas as pd

import optihood.IO.readers as _rd
import optihood.IO.writers as _sw
import optihood.IO.groupScenarioWriter as _gsw
import optihood.IO.readers as _rd


def create_scenario_file(configFilePath, excel_file_path, building, numberOfBuildings=1, writeToFileOrReturnData='file'):
def create_scenario_file(configFilePath, building, numberOfBuildings=1):
"""
function to create the input excel file from a config file
saves the generated excel file at the path given by excel_file_path
Expand Down Expand Up @@ -109,12 +106,4 @@ def create_scenario_file(configFilePath, excel_file_path, building, numberOfBuil

_gsw.add_buildings_to_excel_data(excel_data, numberOfBuildings)

if writeToFileOrReturnData == 'file':
_sw.write_prepared_data_and_sheets_to_excel(excel_file_path, excel_data)
return

elif writeToFileOrReturnData == 'data':
return excel_data



return excel_data
19 changes: 10 additions & 9 deletions optihood/IO/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ class CsvScenarioReader(CsvReader):

def __post_init__(self):
super().__post_init__()
paths = _ent.CsvInputFilePathsRelative
self.relative_file_paths = {
_ent.NodeKeys.buses: "buses.csv",
_ent.NodeKeys.grid_connection: "grid_connection.csv",
_ent.NodeKeys.commodity_sources: "commodity_sources.csv",
_ent.NodeKeys.solar: "solar.csv",
_ent.NodeKeys.transformers: "transformers.csv",
_ent.NodeKeys.demand: "demand.csv",
_ent.NodeKeys.storages: "storages.csv",
_ent.NodeKeys.stratified_storage: "stratified_storage.csv",
_ent.NodeKeys.profiles: "profiles.csv",
_ent.NodeKeys.buses: paths.buses,
_ent.NodeKeys.grid_connection: paths.grid_connection,
_ent.NodeKeys.commodity_sources: paths.commodity_sources,
_ent.NodeKeys.solar: paths.solar,
_ent.NodeKeys.transformers: paths.transformers,
_ent.NodeKeys.demand: paths.demand,
_ent.NodeKeys.storages: paths.storages,
_ent.NodeKeys.stratified_storage: paths.stratified_storage,
_ent.NodeKeys.profiles: paths.profiles,
}

def read_scenario(self) -> dict[str, _pd.DataFrame]:
Expand Down
78 changes: 76 additions & 2 deletions optihood/IO/writers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
import abc as _abc
import dataclasses as _dc
import pathlib as _pl
import typing as _tp

import pandas as _pd

import optihood.IO.groupScenarioWriter as _gsw
import optihood.IO.individualScenarioWriter as _isw
import optihood.entities as _ent


@_dc.dataclass()
class ScenarioCreator:
""" Can be used to get data directly without writing to file using 'ScenarioCreator.get_scenario()'.
The building_nrs are only used for the individual scenarios.
"""
config_file_path: _pl.Path
version: _tp.Literal['grouped', 'individual']
nr_of_buildings: int = 1
building_nrs: int = 0
data: dict[str, _pd.DataFrame] = _dc.field(init=False)

def __post_init__(self):
if self.version == 'grouped':
self.get_scenario = self._get_grouped_scenario
elif self.version == 'individual':
self.get_scenario = self._get_individual_scenario

def _get_grouped_scenario(self):
data = _gsw.create_scenario_file(self.config_file_path, self.nr_of_buildings)
return data

def _get_individual_scenario(self):
data = _isw.create_scenario_file(self.config_file_path, self.building_nrs, self.nr_of_buildings)
return data

@_abc.abstractmethod
def _write_scenario_to_file(self, file_path: _pl.Path) -> None:
pass

def write(self, file_path: _pl.Path) -> None:
self.data = self.get_scenario()
self._write_scenario_to_file(file_path)

def write_prepared_data_and_sheets_to_excel(excel_file_path: str, excel_data: dict):
# better to use pathlib paths.

class ScenarioFileWriterExcel(ScenarioCreator):
def _write_scenario_to_file(self, file_path: _pl.Path) -> None:
write_prepared_data_and_sheets_to_excel(file_path, self.data)


class ScenarioFileWriterCSV(ScenarioCreator):
def __post_init__(self):
super().__post_init__()
paths = _ent.CsvInputFilePathsRelative
self.relative_file_paths = {
_ent.NodeKeys.buses: paths.buses,
_ent.NodeKeys.grid_connection: paths.grid_connection,
_ent.NodeKeys.commodity_sources: paths.commodity_sources,
_ent.NodeKeys.solar: paths.solar,
_ent.NodeKeys.transformers: paths.transformers,
_ent.NodeKeys.demand: paths.demand,
_ent.NodeKeys.storages: paths.storages,
_ent.NodeKeys.stratified_storage: paths.stratified_storage,
_ent.NodeKeys.profiles: paths.profiles,
}

def _write_scenario_to_file(self, folder_path: _pl.Path) -> None:
for key, path in self.relative_file_paths.items():
file_path = folder_path / path
write_to_csv(file_path, self.data[key])


def write_prepared_data_and_sheets_to_excel(excel_file_path: _pl.Path, excel_data: dict):
# maybe this can be inlined?
with _pd.ExcelWriter(excel_file_path, engine='openpyxl') as writer:
for sheet, data in excel_data.items():
data.to_excel(writer, sheet_name=sheet, index=False)


def write_to_csv(file_path: _pl.Path, data_sheet: _pd.DataFrame) -> None:
data_sheet.to_csv(file_path)
18 changes: 13 additions & 5 deletions optihood/energy_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import pandas as pd
from oemof.tools import logger

import optihood.IO.writers as _wr
from optihood.IO.writers import ScenarioFileWriterExcel, ScenarioFileWriterCSV
from optihood.entities import NodeKeys

try:
Expand Down Expand Up @@ -1275,18 +1277,24 @@ def __init__(self, timestamp):

@staticmethod
def createScenarioFile(configFilePath, excelFilePath, building, numberOfBuildings):
warnings.warn(f'"EnergyNetworkIndiv.createScenarioFile" will be sunsetted. Please use {_gsw.__name__}.{_gsw.create_scenario_file.__name__} instead.')
warnings.warn(f'"EnergyNetworkIndiv.createScenarioFile" will be sunsetted. Please use '
f'{_wr.__name__}.{_wr.ScenarioFileWriterExcel.__name__} or '
f'{_wr.__name__}.{_wr.ScenarioFileWriterCSV} instead.')

_isw.create_scenario_file(configFilePath, excelFilePath, building, numberOfBuildings)
scenarioFileWriter = _wr.ScenarioFileWriterExcel(configFilePath, building_nrs=building, version="individual")
scenarioFileWriter.write(excelFilePath)


class EnergyNetworkGroup(EnergyNetworkClass):
@staticmethod
def createScenarioFile(configFilePath, excelFilePath, numberOfBuildings):
warnings.warn(f'"EnergyNetworkGroup.createScenarioFile" will be sunsetted. Please use '
f'{_wr.__name__}.{_wr.ScenarioFileWriterExcel.__name__} or '
f'{_wr.__name__}.{_wr.ScenarioFileWriterCSV} instead.')

warnings.warn(f'"EnergyNetworkGroup.createScenarioFile" will be sunsetted. Please use {_gsw.__name__}.{_gsw.create_scenario_file.__name__} instead.')

_gsw.create_scenario_file(configFilePath, excelFilePath, numberOfBuildings)
scenarioFileWriter = _wr.ScenarioFileWriterExcel(configFilePath, nr_of_buildings=numberOfBuildings,
version="grouped")
scenarioFileWriter.write(excelFilePath)

def setFromExcel(self, filePath, numberOfBuildings, clusterSize={}, opt="costs", mergeLinkBuses=False, mergeBuses=None, mergeHeatSourceSink=False, dispatchMode=False, includeCarbonBenefits=False):
# does Excel file exist?
Expand Down
12 changes: 12 additions & 0 deletions optihood/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ class NodeKeys(_enum.StrEnum):
storages = "storages"
stratified_storage = "stratified_storage"
profiles = "profiles"


class CsvInputFilePathsRelative(_enum.StrEnum):
buses = "buses.csv"
grid_connection = "grid_connection.csv"
commodity_sources = "commodity_sources.csv"
solar = "solar.csv"
transformers = "transformers.csv"
demand = "demand.csv"
storages = "storages.csv"
stratified_storage = "stratified_storage.csv"
profiles = "profiles.csv"
41 changes: 41 additions & 0 deletions tests/test_writers/expected_files/group_csvs/buses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
,label,building,excess,excess costs,active
0,gridBus,1,0,,1
1,gridBus,2,0,,1
2,gridBus,3,0,,1
3,gridBus,4,0,,1
4,electricityProdBus,1,0,,1
5,electricityProdBus,2,0,,1
6,electricityProdBus,3,0,,1
7,electricityProdBus,4,0,,1
8,electricityInBus,1,0,,1
9,electricityInBus,2,0,,1
10,electricityInBus,3,0,,1
11,electricityInBus,4,0,,1
12,shDemandBus,1,0,,1
13,shDemandBus,2,0,,1
14,shDemandBus,3,0,,1
15,shDemandBus,4,0,,1
16,dhwDemandBus,1,0,,1
17,dhwDemandBus,2,0,,1
18,dhwDemandBus,3,0,,1
19,dhwDemandBus,4,0,,1
20,shSourceBus,1,0,,1
21,shSourceBus,2,0,,1
22,shSourceBus,3,0,,1
23,shSourceBus,4,0,,1
24,dhwStorageBus,1,0,,1
25,dhwStorageBus,2,0,,1
26,dhwStorageBus,3,0,,1
27,dhwStorageBus,4,0,,1
28,electricityBus,1,1,-0.09,1
29,electricityBus,2,1,-0.09,1
30,electricityBus,3,1,-0.09,1
31,electricityBus,4,1,-0.09,1
32,spaceHeatingBus,1,0,,1
33,spaceHeatingBus,2,0,,1
34,spaceHeatingBus,3,0,,1
35,spaceHeatingBus,4,0,,1
36,domesticHotWaterBus,1,0,,1
37,domesticHotWaterBus,2,0,,1
38,domesticHotWaterBus,3,0,,1
39,domesticHotWaterBus,4,0,,1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,label,building,to,variable costs,CO2 impact,active
0,electricityResource,1,gridBus,0.204,..\excels\basic_example\electricity_impact.csv,1
1,electricityResource,2,gridBus,0.204,..\excels\basic_example\electricity_impact.csv,1
2,electricityResource,3,gridBus,0.204,..\excels\basic_example\electricity_impact.csv,1
3,electricityResource,4,gridBus,0.204,..\excels\basic_example\electricity_impact.csv,1
13 changes: 13 additions & 0 deletions tests/test_writers/expected_files/group_csvs/demand.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
,label,building,active,from,fixed,nominal value,building model
0,electricityDemand,1,1,electricityInBus,1,1,
1,electricityDemand,2,1,electricityInBus,1,1,
2,electricityDemand,3,1,electricityInBus,1,1,
3,electricityDemand,4,1,electricityInBus,1,1,
4,spaceHeatingDemand,1,1,shDemandBus,1,1,
5,spaceHeatingDemand,2,1,shDemandBus,1,1,
6,spaceHeatingDemand,3,1,shDemandBus,1,1,
7,spaceHeatingDemand,4,1,shDemandBus,1,1,
8,domesticHotWaterDemand,1,1,dhwDemandBus,1,1,
9,domesticHotWaterDemand,2,1,dhwDemandBus,1,1,
10,domesticHotWaterDemand,3,1,dhwDemandBus,1,1,
11,domesticHotWaterDemand,4,1,dhwDemandBus,1,1,
25 changes: 25 additions & 0 deletions tests/test_writers/expected_files/group_csvs/grid_connection.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
,label,building,from,to,efficiency
0,gridElectricity,1,gridBus,electricityInBus,1
1,gridElectricity,2,gridBus,electricityInBus,1
2,gridElectricity,3,gridBus,electricityInBus,1
3,gridElectricity,4,gridBus,electricityInBus,1
4,electricitySource,1,electricityProdBus,electricityBus,1
5,electricitySource,2,electricityProdBus,electricityBus,1
6,electricitySource,3,electricityProdBus,electricityBus,1
7,electricitySource,4,electricityProdBus,electricityBus,1
8,producedElectricity,1,electricityBus,electricityInBus,1
9,producedElectricity,2,electricityBus,electricityInBus,1
10,producedElectricity,3,electricityBus,electricityInBus,1
11,producedElectricity,4,electricityBus,electricityInBus,1
12,shSource,1,shSourceBus,spaceHeatingBus,1
13,shSource,2,shSourceBus,spaceHeatingBus,1
14,shSource,3,shSourceBus,spaceHeatingBus,1
15,shSource,4,shSourceBus,spaceHeatingBus,1
16,spaceHeating,1,spaceHeatingBus,shDemandBus,1
17,spaceHeating,2,spaceHeatingBus,shDemandBus,1
18,spaceHeating,3,spaceHeatingBus,shDemandBus,1
19,spaceHeating,4,spaceHeatingBus,shDemandBus,1
20,domesticHotWater,1,domesticHotWaterBus,dhwDemandBus,1
21,domesticHotWater,2,domesticHotWaterBus,dhwDemandBus,1
22,domesticHotWater,3,domesticHotWaterBus,dhwDemandBus,1
23,domesticHotWater,4,domesticHotWaterBus,dhwDemandBus,1
3 changes: 3 additions & 0 deletions tests/test_writers/expected_files/group_csvs/profiles.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,name,path
0,weather_data,..\excels\basic_example\weather.csv
0,demand_profiles,..\excels\basic_example\demand_profiles
5 changes: 5 additions & 0 deletions tests/test_writers/expected_files/group_csvs/solar.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,label,building,from,to,connect,electrical_consumption,peripheral_losses,latitude,longitude,tilt,azimuth,eta_0,a_1,a_2,temp_collector_inlet,delta_temp_n,capacity_max,capacity_min,lifetime,maintenance,installation,planification,invest_base,invest_cap,heat_impact,elec_impact,impact_cap,active
0,pv,1,,electricityProdBus,,,0.05,47.49,7.59,30,180,,,,,40,500,0.4,30,0.02,0,0,17950,1103,0,0,1131,1.0
1,pv,2,,electricityProdBus,,,0.05,47.49,7.59,30,180,,,,,40,500,0.4,30,0.02,0,0,17950,1103,0,0,1131,1.0
2,pv,3,,electricityProdBus,,,0.05,47.49,7.59,30,180,,,,,40,500,0.4,30,0.02,0,0,17950,1103,0,0,1131,1.0
3,pv,4,,electricityProdBus,,,0.05,47.49,7.59,30,180,,,,,40,500,0.4,30,0.02,0,0,17950,1103,0,0,1131,1.0
13 changes: 13 additions & 0 deletions tests/test_writers/expected_files/group_csvs/storages.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
,label,building,active,from,to,efficiency inflow,efficiency outflow,initial capacity,capacity min,capacity max,capacity loss,lifetime,maintenance,installation,planification,invest_base,invest_cap,heat_impact,elec_impact,impact_cap
0,electricalStorage,1,1,electricityProdBus,electricityBus,0.9,0.86,0,0,1000000,0,15,0,0,0,5138,981,0,0,28.66
1,electricalStorage,2,1,electricityProdBus,electricityBus,0.9,0.86,0,0,1000000,0,15,0,0,0,5138,981,0,0,28.66
2,electricalStorage,3,1,electricityProdBus,electricityBus,0.9,0.86,0,0,1000000,0,15,0,0,0,5138,981,0,0,28.66
3,electricalStorage,4,1,electricityProdBus,electricityBus,0.9,0.86,0,0,1000000,0,15,0,0,0,5138,981,0,0,28.66
4,shStorage,1,1,shSourceBus,spaceHeatingBus,,,0,0,1000000,,20,0,0,0,1092,1.41,0,0,0.49
5,shStorage,2,1,shSourceBus,spaceHeatingBus,,,0,0,1000000,,20,0,0,0,1092,1.41,0,0,0.49
6,shStorage,3,1,shSourceBus,spaceHeatingBus,,,0,0,1000000,,20,0,0,0,1092,1.41,0,0,0.49
7,shStorage,4,1,shSourceBus,spaceHeatingBus,,,0,0,1000000,,20,0,0,0,1092,1.41,0,0,0.49
8,dhwStorage,1,1,dhwStorageBus,domesticHotWaterBus,,,0,0,1000000,,20,0,0,0,2132,6.88,0,0,0.49
9,dhwStorage,2,1,dhwStorageBus,domesticHotWaterBus,,,0,0,1000000,,20,0,0,0,2132,6.88,0,0,0.49
10,dhwStorage,3,1,dhwStorageBus,domesticHotWaterBus,,,0,0,1000000,,20,0,0,0,2132,6.88,0,0,0.49
11,dhwStorage,4,1,dhwStorageBus,domesticHotWaterBus,,,0,0,1000000,,20,0,0,0,2132,6.88,0,0,0.49
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,label,diameter,temp_h,temp_c,temp_env,inflow_conversion_factor,outflow_conversion_factor,s_iso,lamb_iso,alpha_inside,alpha_outside
0,shStorage,1,35,15,15,1,1,100,0.03,100,10
1,dhwStorage,1,60,15,15,1,1,100,0.03,100,10
Loading

0 comments on commit 9410df4

Please sign in to comment.