Skip to content

Commit

Permalink
plots: Add more graph plots
Browse files Browse the repository at this point in the history
  • Loading branch information
stroitzsch committed Nov 1, 2023
1 parent 3cfbdea commit 45e2b28
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 21 deletions.
4 changes: 3 additions & 1 deletion examples/development/entrypoint_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def main():
plots.plot_to_file(plots.der_aggregated_apparent_power_time_series, results=results, results_path=results_path)
plots.plot_to_file(plots.node_voltage_per_unit_time_series, results=results, results_path=results_path)
plots.plot_to_file(plots.node_aggregated_voltage_per_unit_time_series, results=results, results_path=results_path)
plots.plot_to_file(plots.electric_grid_asset_layout, results=results, results_path=results_path)
plots.plot_to_file(plots.electric_grid_assets, results=results, results_path=results_path)
plots.plot_to_file(plots.electric_grid_node_voltage_nominal, results=results, results_path=results_path)
plots.plot_to_file(plots.electric_grid_node_voltage_magnitude_min, results=results, results_path=results_path)

# Sample JSON return
print(plots.plot_to_json(plots.der_active_power_time_series, results=results))
Expand Down
2 changes: 1 addition & 1 deletion mesmo/plots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Plotting function collection."""

from .graph import electric_grid_asset_layout
from .graph import electric_grid_assets, electric_grid_node_voltage_magnitude_min, electric_grid_node_voltage_nominal
from .plots import plot_to_figure, plot_to_file, plot_to_json
from .time_series import (
der_active_power_time_series,
Expand Down
133 changes: 114 additions & 19 deletions mesmo/plots/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from netgraph import Graph

import mesmo.data_interface
import mesmo.utils
from mesmo import data_models
from mesmo.plots import constants


class ElectricGridGraph(nx.DiGraph):
"""Electric grid graph object."""

data: mesmo.data_interface.ElectricGridData
line_edges: dict[str, tuple[str, str]]
transformer_edges: dict[str, tuple[str, str]]
node_positions: dict[str, tuple[float, float]]
Expand All @@ -27,6 +29,8 @@ def __init__(self, scenario_name: str):

@multimethod
def __init__(self, electric_grid_data: mesmo.data_interface.ElectricGridData):
self.data = electric_grid_data

# Create graph
super().__init__()
self.add_nodes_from(electric_grid_data.electric_grid_nodes.loc[:, "node_name"].tolist(), layer=0)
Expand Down Expand Up @@ -56,10 +60,11 @@ def __init__(self, electric_grid_data: mesmo.data_interface.ElectricGridData):
self.node_positions = graph_layout.node_positions


def electric_grid_asset_layout(figure: go.Figure, results: data_models.RunResults) -> go.Figure:
graph = ElectricGridGraph(results.scenario_name)
def _get_electric_grid_graph(results: data_models.RunResults):
return ElectricGridGraph(results.scenario_name)

# Plot lines

def _plot_electric_line_layout(figure: go.Figure, results: data_models.RunResults, graph: ElectricGridGraph):
line_edge_x = []
line_edge_y = []
line_edge_x_label = []
Expand All @@ -77,15 +82,13 @@ def electric_grid_asset_layout(figure: go.Figure, results: data_models.RunResult
line_edge_x_label.append((x0 + x1) / 2)
line_edge_y_label.append((y0 + y1) / 2)
line_name.append(name)
# Plot lines
figure.add_trace(
go.Scatter(
x=line_edge_x,
y=line_edge_y,
line=go.scatter.Line(width=2, color="grey"),
mode="lines",
name="lines",
x=line_edge_x, y=line_edge_y, line=go.scatter.Line(width=2, color="grey"), mode="lines", name="lines"
)
)
# Plot line labels
figure.add_trace(
go.Scatter(
x=line_edge_x_label,
Expand All @@ -98,7 +101,8 @@ def electric_grid_asset_layout(figure: go.Figure, results: data_models.RunResult
)
)

# Plot transformers

def _plot_electric_transformer_layout(figure: go.Figure, results: data_models.RunResults, graph: ElectricGridGraph):
transformer_edge_x = []
transformer_edge_y = []
transformer_edge_x_label = []
Expand Down Expand Up @@ -137,6 +141,8 @@ def electric_grid_asset_layout(figure: go.Figure, results: data_models.RunResult
)
)


def _plot_electric_nodes_layout(figure: go.Figure, results: data_models.RunResults, graph: ElectricGridGraph):
node_x = []
node_y = []
node_name = []
Expand All @@ -145,6 +151,62 @@ def electric_grid_asset_layout(figure: go.Figure, results: data_models.RunResult
node_x.append(x)
node_y.append(y)
node_name.append(node)
figure.add_trace(
go.Scatter(
x=node_x,
y=node_y,
mode="markers",
hoverinfo="text",
hovertext=node_name,
marker=go.scatter.Marker(color="teal", size=10),
name="nodes",
)
)


def _plot_electric_nodes_nominal_voltage(figure: go.Figure, results: data_models.RunResults, graph: ElectricGridGraph):
node_x = []
node_y = []
node_value = []
node_name = []
for node in graph.nodes():
x, y = graph.node_positions[node]
node_x.append(x)
node_y.append(y)
node_value.append(graph.data.electric_grid_nodes.at[node, "voltage"])
node_name.append(f"{node}: {node_value[-1]}")
figure.add_trace(
go.Scatter(
x=node_x,
y=node_y,
mode="markers",
hoverinfo="text",
hovertext=node_name,
marker=go.scatter.Marker(
showscale=True,
color=node_value,
size=10,
colorbar=go.scatter.marker.ColorBar(title="Node voltage", titleside="right"),
),
name="nodes",
)
)


def _plot_electric_nodes_min_voltage(figure: go.Figure, results: data_models.RunResults, graph: ElectricGridGraph):
result_values = results.electric_grid_operation_results.node_voltage_magnitude_vector.min()

node_x = []
node_y = []
node_value = []
node_name = []
for node in graph.nodes():
x, y = graph.node_positions[node]
node_x.append(x)
node_y.append(y)
node_index = mesmo.utils.get_index(results.electric_grid_model_index.nodes, node_name=node)
node_value.append(result_values.iloc[node_index].mean() * (3**0.5))
node_name.append(f"{node}: {node_value[-1]}")
figure.add_trace(
go.Scatter(
x=node_x,
Expand All @@ -153,25 +215,58 @@ def electric_grid_asset_layout(figure: go.Figure, results: data_models.RunResult
hoverinfo="text",
hovertext=node_name,
marker=go.scatter.Marker(
# showscale=True,
# colorscale options
#'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
#'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
#'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
# colorscale="YlGnBu",
# reversescale=True,
color="teal",
showscale=True,
color=node_value,
size=10,
# colorbar=dict(thickness=15, title="Node Connections", xanchor="left", titleside="right"),
# line_width=2,
colorbar=go.scatter.marker.ColorBar(title="Node voltage", titleside="right"),
),
name="nodes",
)
)


def electric_grid_assets(figure: go.Figure, results: data_models.RunResults) -> go.Figure:
graph = _get_electric_grid_graph(results)
_plot_electric_line_layout(figure, results, graph)
_plot_electric_transformer_layout(figure, results, graph)
_plot_electric_nodes_layout(figure, results, graph)

title = "Electric grid asset layout"
legend_title = constants.ValueLabels.ASSETS
figure.update_layout(
title=title,
xaxis=go.layout.XAxis(showgrid=False, visible=False),
yaxis=go.layout.YAxis(showgrid=False, visible=False),
legend=go.layout.Legend(title=legend_title, x=0.99, xanchor="auto", y=0.01, yanchor="auto"),
)
return figure


def electric_grid_node_voltage_nominal(figure: go.Figure, results: data_models.RunResults) -> go.Figure:
graph = _get_electric_grid_graph(results)
_plot_electric_line_layout(figure, results, graph)
_plot_electric_transformer_layout(figure, results, graph)
_plot_electric_nodes_nominal_voltage(figure, results, graph)

title = "Electric grid asset layout"
legend_title = constants.ValueLabels.ASSETS
figure.update_layout(
title=title,
xaxis=go.layout.XAxis(showgrid=False, visible=False),
yaxis=go.layout.YAxis(showgrid=False, visible=False),
legend=go.layout.Legend(title=legend_title, x=0.99, xanchor="auto", y=0.01, yanchor="auto"),
)
return figure


def electric_grid_node_voltage_magnitude_min(figure: go.Figure, results: data_models.RunResults) -> go.Figure:
graph = _get_electric_grid_graph(results)
_plot_electric_line_layout(figure, results, graph)
_plot_electric_transformer_layout(figure, results, graph)
_plot_electric_nodes_min_voltage(figure, results, graph)

title = "Electric grid asset layout"
legend_title = constants.ValueLabels.ASSETS
figure.update_layout(
title=title,
xaxis=go.layout.XAxis(showgrid=False, visible=False),
Expand Down

0 comments on commit 45e2b28

Please sign in to comment.