Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:openego/eDisGo into features/#276-im…
Browse files Browse the repository at this point in the history
…prove-simple-example
  • Loading branch information
maike93he committed Aug 2, 2022
2 parents fc985af + 1f05023 commit 7979973
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 87 deletions.
1 change: 0 additions & 1 deletion doc/whatsnew/v0-2-0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ Changes
standing times, charging demand, etc. per vehicle, whereas TracBEV provides potential charging point locations)
`#174 <https://github.com/openego/eDisGo/issues/174>`_ and
`#191 <https://github.com/openego/eDisGo/pull/191>`_

27 changes: 19 additions & 8 deletions edisgo/flex_opt/charging_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def charging_strategy(
# Delete possible old time series as these influence "residual" charging
edisgo_obj.timeseries.drop_component_time_series(
"loads_active_power",
edisgo_obj.electromobility.integrated_charging_parks_df.edisgo_id.values
edisgo_obj.electromobility.integrated_charging_parks_df.edisgo_id.values,
)

edisgo_obj.timeseries.drop_component_time_series(
"loads_reactive_power",
edisgo_obj.electromobility.integrated_charging_parks_df.edisgo_id.values,
)

eta_cp = edisgo_obj.electromobility.eta_charging_points
Expand Down Expand Up @@ -130,7 +135,7 @@ def charging_strategy(

edisgo_obj.timeseries.add_component_time_series(
"loads_active_power",
pd.DataFrame(data={cp.edisgo_id: dummy_ts}, index=timeindex)
pd.DataFrame(data={cp.edisgo_id: dummy_ts}, index=timeindex),
)

elif strategy == "reduced":
Expand Down Expand Up @@ -168,7 +173,7 @@ def charging_strategy(

edisgo_obj.timeseries.add_component_time_series(
"loads_active_power",
pd.DataFrame(data={cp.edisgo_id: dummy_ts}, index=timeindex)
pd.DataFrame(data={cp.edisgo_id: dummy_ts}, index=timeindex),
)

elif strategy == "residual":
Expand Down Expand Up @@ -277,22 +282,28 @@ def charging_strategy(
dummy_ts.rename(
columns={
cp_id: edisgo_obj.electromobility.integrated_charging_parks_df.at[
cp_id, "edisgo_id"] for cp_id in dummy_ts.columns}
)
cp_id, "edisgo_id"
]
for cp_id in dummy_ts.columns
}
),
)

else:
raise ValueError(f"Strategy {strategy} has not yet been implemented.")

# set reactive power time series to 0 Mvar
# fmt: off
edisgo_obj.timeseries.add_component_time_series(
"loads_reactive_power",
pd.DataFrame(
data=0.,
data=0.0,
index=edisgo_obj.timeseries.timeindex,
columns=edisgo_obj.electromobility.integrated_charging_parks_df.
edisgo_id.values)
columns=edisgo_obj.electromobility.integrated_charging_parks_df
.edisgo_id.values,
),
)
# fmt: on

logging.info(f"Charging strategy {strategy} completed.")

Expand Down
2 changes: 1 addition & 1 deletion edisgo/io/ding0_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pypsa import Network as PyPSANetwork

from edisgo.network.grids import LVGrid, MVGrid
from edisgo.network.grids import MVGrid

if "READTHEDOCS" not in os.environ:
from shapely.wkt import loads as wkt_loads
Expand Down
10 changes: 8 additions & 2 deletions edisgo/io/electromobility_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ def get_weights_df(edisgo_obj, potential_charging_park_indices, **kwargs):
DataFrame with numeric weights
"""

def _get_lv_grid_weights():

"""
Expand Down Expand Up @@ -592,8 +593,13 @@ def _get_lv_grid_weights():

lv_grids_df = pd.DataFrame(
index=[_._id for _ in lv_grids],
columns=["peak_generation_capacity", "substation_capacity",
"generators_weight", "p_set", "loads_weight"]
columns=[
"peak_generation_capacity",
"substation_capacity",
"generators_weight",
"p_set",
"loads_weight",
],
)

lv_grids_df.peak_generation_capacity = [
Expand Down
25 changes: 18 additions & 7 deletions edisgo/io/generators_import.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import os
import numpy as np
import random

import numpy as np
import pandas as pd

from sqlalchemy import func
Expand Down Expand Up @@ -234,13 +234,21 @@ def _validate_sample_geno_location():
# get geom of 1 random MV and 1 random LV generator and transform
sample_mv_geno_geom_shp = transform(
projection,
wkt_loads(generators_res_mv["geom"].dropna().sample(
n=1, random_state=42).values[0]),
wkt_loads(
generators_res_mv["geom"]
.dropna()
.sample(n=1, random_state=42)
.values[0]
),
)
sample_lv_geno_geom_shp = transform(
projection,
wkt_loads(generators_res_lv["geom"].dropna().sample(
n=1, random_state=42).values[0]),
wkt_loads(
generators_res_lv["geom"]
.dropna()
.sample(n=1, random_state=42)
.values[0]
),
)

# get geom of MV grid district
Expand Down Expand Up @@ -652,8 +660,11 @@ def drop_generators(generator_list, gen_type, total_capacity):
if not imported_generators_lv.empty:
grid_ids = edisgo_object.topology._lv_grid_ids
if not any(
[int(_) in grid_ids for _ in list(imported_generators_lv["mvlv_subst_id"])
if not np.isnan(_)]
[
int(_) in grid_ids
for _ in list(imported_generators_lv["mvlv_subst_id"])
if not np.isnan(_)
]
):
logger.warning(
"None of the imported LV generators can be allocated "
Expand Down
1 change: 0 additions & 1 deletion edisgo/network/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os

from abc import ABC, abstractmethod
from math import acos, tan

if "READTHEDOCS" not in os.environ:
from shapely.geometry import Point
Expand Down
10 changes: 4 additions & 6 deletions edisgo/network/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,7 @@ def _set_manual(
df_name=df_name, comp_names=ts_storage_units.columns
)
# set (re)active power
self.add_component_time_series(
df_name=df_name, ts_new=ts_storage_units
)
self.add_component_time_series(df_name=df_name, ts_new=ts_storage_units)

def set_worst_case(
self,
Expand Down Expand Up @@ -624,8 +622,7 @@ def _overwrite_time_series(p, q, comp_type):
ts_dict = {f"{comp_type}_active_power": p, f"{comp_type}_reactive_power": q}
for k, v in ts_dict.items():
# drop previously set time series
self.drop_component_time_series(
df_name=k, comp_names=v.columns)
self.drop_component_time_series(df_name=k, comp_names=v.columns)
# set time series
self.add_component_time_series(
df_name=k,
Expand Down Expand Up @@ -2113,7 +2110,8 @@ def add_component_time_series(self, df_name, ts_new):
)

def _check_if_components_exist(
self, edisgo_object, component_names, component_type):
self, edisgo_object, component_names, component_type
):
"""
Checks if all provided components exist in the network.
Expand Down
38 changes: 5 additions & 33 deletions edisgo/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,9 @@ def _grids_repr(self):
and underlying LV grids.
"""
return ([f"LVGrid_{id}" for id in self._lv_grid_ids] +
[f"MVGrid_{int(self.mv_grid.id)}"])
return [f"LVGrid_{id}" for id in self._lv_grid_ids] + [
f"MVGrid_{int(self.mv_grid.id)}"
]

def get_lv_grid(self, name):
"""
Expand Down Expand Up @@ -2043,7 +2044,8 @@ def _choose_random_substation_id():
logger.error(f"Component type {comp_type} is not a valid option.")

if comp_data["mvlv_subst_id"] is not None and not np.isnan(
comp_data["mvlv_subst_id"]):
comp_data["mvlv_subst_id"]
):

# if substation ID (= LV grid ID) is given and it matches an
# existing LV grid ID (i.e. it is no aggregated LV grid), set grid
Expand Down Expand Up @@ -2408,36 +2410,6 @@ def to_graph(self):
self.transformers_df,
)

def to_geopandas(self, mode: str = "mv"):
"""
Returns components as :geopandas:`GeoDataFrame`\\ s
Returns container with :geopandas:`GeoDataFrame`\\ s containing all
georeferenced components within the grid.
Parameters
----------
mode : str
Return mode. If mode is "mv" the mv components are returned. If mode is "lv"
a generator with a container per lv grid is returned. Default: "mv"
Returns
-------
:class:`~.tools.geopandas_helper.GeoPandasGridContainer` or \
list(:class:`~.tools.geopandas_helper.GeoPandasGridContainer`)
Data container with GeoDataFrames containing all georeferenced components
within the grid(s).
"""
if mode == "mv":
return self.mv_grid.geopandas
elif mode == "lv":
raise NotImplementedError("LV Grids are not georeferenced yet.")
# for lv_grid in self.mv_grid.lv_grids:
# yield lv_grid.geopandas
else:
raise ValueError(f"{mode} is not valid. See docstring for more info.")

def to_geopandas(self, mode: str = "mv"):
"""
Returns components as :geopandas:`GeoDataFrame`\\ s.
Expand Down
1 change: 0 additions & 1 deletion edisgo/tools/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from sqlalchemy import func

from edisgo.flex_opt import check_tech_constraints, exceptions
from edisgo.network.grids import LVGrid
from edisgo.tools import session_scope

if "READTHEDOCS" not in os.environ:
Expand Down
2 changes: 1 addition & 1 deletion tests/io/test_ding0_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shapely

from edisgo.io import ding0_import
from edisgo.network.grids import LVGrid, MVGrid
from edisgo.network.grids import MVGrid
from edisgo.network.topology import Topology


Expand Down
4 changes: 2 additions & 2 deletions tests/io/test_generators_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_update_grids(self):
"subtype": ["solar", "solar", "roof"],
"weather_cell_id": [1122075, 1122075, 1122074],
"voltage_level": [6, 6, 6],
"mvlv_subst_id": [None, None, 6.],
"mvlv_subst_id": [None, None, 6.0],
},
index=[13, 14, 456],
)
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_update_grids_target_capacity(self):
"subtype": ["solar", "solar", "hydro", "wind"],
"weather_cell_id": [1122075, 1122075, 1122074, 1122074],
"voltage_level": [6, 6, 6, 7],
"mvlv_subst_id": [None, None, 6., 2],
"mvlv_subst_id": [None, None, 6.0, 2],
},
index=[13, 145, 456, 654],
)
Expand Down
3 changes: 2 additions & 1 deletion tests/io/test_pypsa_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_to_pypsa(self):
# test mode "lv" and single time step
lv_grid = self.edisgo.topology.get_lv_grid(1)
pypsa_network = pypsa_io.to_pypsa(
self.edisgo, timesteps=timeindex[0], mode="lv", lv_grid_id=lv_grid.id)
self.edisgo, timesteps=timeindex[0], mode="lv", lv_grid_id=lv_grid.id
)
slack_df = pypsa_network.generators[pypsa_network.generators.control == "Slack"]
assert len(slack_df) == 1
assert slack_df.bus.values[0] == lv_grid.station.index[0]
Expand Down
2 changes: 0 additions & 2 deletions tests/network/test_electromobility.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ def test_integrate_charging_parks(self):

electromobility = self.edisgo_obj.electromobility

ts = self.edisgo_obj.timeseries

topology = self.edisgo_obj.topology

designated_charging_parks_with_charging_points = len(
Expand Down
12 changes: 3 additions & 9 deletions tests/network/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,9 +2229,7 @@ def test_drop_component_time_series(self):
time_series_obj = timeseries.TimeSeries()

# check that no error is raised in case of empty dataframe
time_series_obj.drop_component_time_series(
"loads_active_power", "Load1"
)
time_series_obj.drop_component_time_series("loads_active_power", "Load1")

# add dummy time series
time_series_obj.timeindex = pd.date_range("1/1/2018", periods=4, freq="H")
Expand All @@ -2252,9 +2250,7 @@ def test_drop_component_time_series(self):
assert "load_1" not in time_series_obj.loads_active_power.columns

# check with dropping all existing loads
time_series_obj.drop_component_time_series(
"loads_active_power", ["load_2"]
)
time_series_obj.drop_component_time_series("loads_active_power", ["load_2"])
assert time_series_obj.loads_active_power.empty

def test_add_component_time_series(self):
Expand Down Expand Up @@ -2283,9 +2279,7 @@ def test_add_component_time_series(self):
},
index=time_series_obj.timeindex[0:2],
)
time_series_obj.add_component_time_series(
"loads_active_power", df.iloc[:2]
)
time_series_obj.add_component_time_series("loads_active_power", df.iloc[:2])
assert time_series_obj.loads_active_power.shape == (4, 4)
assert "load_3" in time_series_obj.loads_active_power.columns

Expand Down
8 changes: 4 additions & 4 deletions tests/network/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def test_get_lv_grid(self, caplog):
assert str(lv_grid) == name

# test invalid input
name = 1.
name = 1.0
lv_grid = self.topology.get_lv_grid(name)
assert lv_grid is None
assert ("`name` must be integer or string." in caplog.text)
assert "`name` must be integer or string." in caplog.text

def test_rings(self):
"""Test rings getter."""
Expand Down Expand Up @@ -1116,7 +1116,7 @@ def test_connect_to_lv(self):
"GeneratorFluctuating_2", "weather_cell_id"
],
"voltage_level": 6,
"mvlv_subst_id": 10.,
"mvlv_subst_id": 10.0,
}

comp_name = self.edisgo.topology.connect_to_lv(self.edisgo, test_gen)
Expand Down Expand Up @@ -1348,7 +1348,7 @@ def test_connect_to_lv(self):
"geom": geom,
"sector": "home",
"voltage_level": 7,
"mvlv_subst_id": 3.,
"mvlv_subst_id": 3.0,
}

comp_name = self.edisgo.topology.connect_to_lv(
Expand Down
12 changes: 8 additions & 4 deletions tests/test_edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,14 @@ def test_analyze(self, caplog):
self.edisgo.analyze(troubleshooting_mode="iteration", range_start=5)

caplog.clear()
self.edisgo.analyze(troubleshooting_mode="iteration", range_start=5,
range_num=2, raise_not_converged=False)
assert ("Current fraction in iterative process: 5.0." in caplog.text)
assert ("Current fraction in iterative process: 1.0." in caplog.text)
self.edisgo.analyze(
troubleshooting_mode="iteration",
range_start=5,
range_num=2,
raise_not_converged=False,
)
assert "Current fraction in iterative process: 5.0." in caplog.text
assert "Current fraction in iterative process: 1.0." in caplog.text

def test_reinforce(self):
self.setup_worst_case_time_series()
Expand Down
Loading

0 comments on commit 7979973

Please sign in to comment.