Skip to content

Commit

Permalink
Apply black
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaraBuettner committed Aug 8, 2024
1 parent dbf0921 commit 2873420
Showing 1 changed file with 84 additions and 63 deletions.
147 changes: 84 additions & 63 deletions etrago/analyze/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,9 @@ def calc_storage_expansion_per_bus(
[(idx, "battery") for idx in battery_distribution.index]
)

dist.loc[
dist.index.get_level_values("carrier") == "battery"
] = battery_distribution
dist.loc[dist.index.get_level_values("carrier") == "battery"] = (
battery_distribution
)
if "H2_overground" in carriers:
h2_overground = network.stores[
network.stores.carrier == "H2_overground"
Expand All @@ -930,9 +930,9 @@ def calc_storage_expansion_per_bus(
[(idx, "H2_overground") for idx in h2_over_distribution.index]
)

dist.loc[
dist.index.get_level_values("carrier") == "H2_overground"
] = h2_over_distribution
dist.loc[dist.index.get_level_values("carrier") == "H2_overground"] = (
h2_over_distribution
)

if "H2_overground" in carriers:
h2_underground = network.stores[
Expand Down Expand Up @@ -1819,7 +1819,9 @@ def plot_background_grid(network, ax, geographical_boundaries, osm):
)


def demand_side_management(self, buses, snapshots, agg="5h", used=False, apply_on="grid_model"):
def demand_side_management(
self, buses, snapshots, agg="5h", used=False, apply_on="grid_model"
):
"""Calculate shifting potential of demand side management
Parameters
Expand Down Expand Up @@ -1854,13 +1856,13 @@ def demand_side_management(self, buses, snapshots, agg="5h", used=False, apply_o
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

df = pd.DataFrame(index=network.snapshots[snapshots])

link = network.links[
(network.links.carrier == "dsm")
& (network.links.bus0.isin(buses))
(network.links.carrier == "dsm") & (network.links.bus0.isin(buses))
]
s = network.stores[
(network.stores.carrier == "dsm")
Expand Down Expand Up @@ -1952,15 +1954,14 @@ def bev_flexibility_potential(
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

# Initialize DataFrame
df = pd.DataFrame(index=network.snapshots[snapshots])

# Select BEV buses and links
bev_buses = network.buses[
network.buses.carrier.str.contains("Li ion")
]
bev_buses = network.buses[network.buses.carrier.str.contains("Li ion")]
bev_links = network.links[
(network.links.bus1.isin(bev_buses.index.values))
& (network.links.bus0.isin(buses))
Expand Down Expand Up @@ -2008,9 +2009,7 @@ def bev_flexibility_potential(
)

if used:
bev_links_t_used = network.links_t.p0[bev_links.index].iloc[
snapshots
]
bev_links_t_used = network.links_t.p0[bev_links.index].iloc[snapshots]

bev_links_t_used.columns = bev_links_t_used.columns.map(bev_links.bus1)

Expand Down Expand Up @@ -2076,7 +2075,8 @@ def heat_stores(
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

df = pd.DataFrame(index=network.snapshots[snapshots])

Expand All @@ -2088,9 +2088,7 @@ def heat_stores(
].index
)
& network.links.bus1.isin(
network.buses[
network.buses.carrier.str.contains("heat")
].index
network.buses[network.buses.carrier.str.contains("heat")].index
)
].bus1.unique()

Expand Down Expand Up @@ -2174,7 +2172,8 @@ def hydrogen_stores(
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

df = pd.DataFrame(index=network.snapshots[snapshots])

Expand All @@ -2186,9 +2185,7 @@ def hydrogen_stores(
].index
)
& network.links.bus1.isin(
network.buses[
network.buses.carrier.str.contains("H2")
].index
network.buses[network.buses.carrier.str.contains("H2")].index
)
].bus1.unique()

Expand All @@ -2208,7 +2205,13 @@ def hydrogen_stores(


def flexibility_usage(
self, flexibility, agg="5h", snapshots=[], buses=[], pre_path=None, apply_on="grid_model",
self,
flexibility,
agg="5h",
snapshots=[],
buses=[],
pre_path=None,
apply_on="grid_model",
):
"""Plots temporal distribution of potential and usage for flexibilities
Expand Down Expand Up @@ -2246,7 +2249,8 @@ def flexibility_usage(
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

colors = coloring()
colors["dlr"] = "orange"
Expand Down Expand Up @@ -2300,9 +2304,7 @@ def flexibility_usage(
df["p_min"] = su.p_nom_opt.sum() * (-1)
df["p_max"] = su.p_nom_opt.sum()
df["p"] = (
network.storage_units_t.p[su.index]
.sum(axis=1)
.iloc[snapshots]
network.storage_units_t.p[su.index].sum(axis=1).iloc[snapshots]
)

df["e_min"] = 0
Expand Down Expand Up @@ -2344,7 +2346,9 @@ def flexibility_usage(
fig_e.savefig(pre_path + f"stored_e_{flexibility}")


def plot_carrier(etrago, carrier_links=["AC"], carrier_buses=["AC"], apply_on="grid_model"):
def plot_carrier(
etrago, carrier_links=["AC"], carrier_buses=["AC"], apply_on="grid_model"
):
"""
Parameters
----------
Expand Down Expand Up @@ -2374,7 +2378,8 @@ def plot_carrier(etrago, carrier_links=["AC"], carrier_buses=["AC"], apply_on="g
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

colors = coloring()
line_colors = "lightblue"
Expand Down Expand Up @@ -2483,7 +2488,7 @@ def plot_grid(
* 'ramp_down': re-dispatch down per carrier in selected timesteps
* 'PowerToH2': location and sizes of electrolyzers
* 'flexibility_usage': use of DSM and BEV charger
* 'PowerToH2_correlation': indication of degree of correlation to
* 'PowerToH2_correlation': indication of degree of correlation to
market or nodal price of electrolyzers
timesteps : array, optional
Expand Down Expand Up @@ -2538,7 +2543,8 @@ def plot_grid(
else:
logger.warning(
"""Parameter apply_on must be one of ['grid_model', 'market_model'
'pre_market_model', 'disaggregated_network'.""")
'pre_market_model', 'disaggregated_network'."""
)

# Set colors for plotting
plotting_colors(network)
Expand Down Expand Up @@ -2851,31 +2857,40 @@ def plot_grid(
bus_colors = coloring()["power_to_H2"]
bus_legend = "PowerToH2"
bus_unit = "TW"



market_bus_de = self.market_model.buses[(self.market_model.buses.country=="DE") & (self.market_model.buses.carrier=="AC")].index
market_bus_de = self.market_model.buses[
(self.market_model.buses.country == "DE")
& (self.market_model.buses.carrier == "AC")
].index
market_price = self.market_model.buses_t.marginal_price[market_bus_de]

bus_colors = pd.Series(index=network.buses.index, data=0)
for bus in network.links[(network.links.carrier == "power_to_H2")].bus0:

for bus in network.links[
(network.links.carrier == "power_to_H2")
].bus0:

nodal_price = network.buses_t.marginal_price[bus]

ely = network.links_t.p0[network.links[(network.links.carrier == "power_to_H2")
& (network.links.bus0==bus)].index]

ely = network.links_t.p0[
network.links[
(network.links.carrier == "power_to_H2")
& (network.links.bus0 == bus)
].index
]
df_corr = pd.DataFrame()

df_corr["ely"] = ely
df_corr["market"] = market_price
df_corr["nodal_price"] = nodal_price

bus_colors[bus] = (df_corr.corr(method = 'spearman').loc["nodal_price", "ely"]/ (
df_corr.corr(method = 'spearman').loc["nodal_price", "ely"])+
df_corr.corr(method = 'spearman').loc["market", "ely"])


bus_colors[bus] = (
df_corr.corr(method="spearman").loc["nodal_price", "ely"]
/ (df_corr.corr(method="spearman").loc["nodal_price", "ely"])
+ df_corr.corr(method="spearman").loc["market", "ely"]
)

bus_colors = bus_colors.abs()

# ely.corr
# ely_corr_market = ely.corrwith(
# market_price, method = 'spearman', axis=1)
Expand All @@ -2900,11 +2915,11 @@ def plot_grid(
link_cmap=plt.cm.viridis,
bus_sizes=bus_sizes,
bus_colors=bus_colors,
bus_cmap = plt.cm.viridis,
bus_cmap=plt.cm.viridis,
line_widths=line_widths,
link_widths=link_widths,
flow=flow,
#title=title,
# title=title,
geomap=False,
projection=ccrs.PlateCarree(),
color_geomap=True,
Expand Down Expand Up @@ -3043,18 +3058,19 @@ def plot_grid(
ll[1],
values=v,
ticks=v[0:101:10],
#fraction=0.028,
# fraction=0.028,
pad=0.04,
orientation="horizontal"
orientation="horizontal",
)
# Set legend label
cb.set_label(label)

elif type(bus_colors) != str:
#import pdb; pdb.set_trace()
ll[0].set_clim([0, bus_colors.max()])
plt.colorbar(ll[0], fraction=0.04, pad=0.004, label="correlation factor", ax=ax)

elif type(bus_colors) != str:
# import pdb; pdb.set_trace()
ll[0].set_clim([0, bus_colors.max()])
plt.colorbar(
ll[0], fraction=0.04, pad=0.004, label="correlation factor", ax=ax
)

# Show plot or save to file
if filename is None:
Expand Down Expand Up @@ -3209,10 +3225,15 @@ def plot_clusters(
& lines["bus1"].isin(map_buses.index)
]
lines["geom"] = lines.apply(
lambda x: x["geom"]
if not pd.isna(x["geom"])
else LineString(
[map_buses["geom"][x["bus0"]], map_buses["geom"][x["bus1"]]]
lambda x: (
x["geom"]
if not pd.isna(x["geom"])
else LineString(
[
map_buses["geom"][x["bus0"]],
map_buses["geom"][x["bus1"]],
]
)
),
axis=1,
)
Expand Down

0 comments on commit 2873420

Please sign in to comment.