Skip to content

Commit

Permalink
Read network structure from selection
Browse files Browse the repository at this point in the history
  • Loading branch information
henhuy committed Apr 10, 2024
1 parent 9451277 commit ed0ada4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project tries to adhere to [Semantic Versioning](https://semver.org/spe

## [Unreleased]
### Fixed
- read network structure from selection
- structure and collection in URL query

## [0.5.0] - 2024-03-22
Expand Down
30 changes: 10 additions & 20 deletions django_energysystem_viewer/network_graph.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import igraph as ig
import pandas as pd
import plotly.graph_objects as go
from django.conf import settings


# generate the trace for the selected sector and algorithm
def generate_trace(sector, algorithm, seperate_commodities):
def generate_trace(process_set, sector, algorithm, seperate_commodities):
"""Generates the trace for the selected sector and algorithm.
Parameters
Expand All @@ -26,11 +24,8 @@ def generate_trace(sector, algorithm, seperate_commodities):
The combined trace of nodes and edges, including their colours and shapes.
"""

# load the process set, change the path if necessary
updated_process_set = pd.read_excel(settings.MEDIA_ROOT + "/" + settings.MODEL_STRUCTURE_FILE, "Process_Set")

# filter aggregations as first step
updated_process_set = updated_process_set[~updated_process_set["process"].str.endswith("_ag")]
updated_process_set = process_set[~process_set["process"].str.endswith("_ag")]

# initialize the lists for the inputs, outputs and processes
inputs = []
Expand Down Expand Up @@ -204,7 +199,7 @@ def generate_trace(sector, algorithm, seperate_commodities):
return data


def generate_trace_process_specific(process_name):
def generate_trace_process_specific(process_set, process_name):
"""Generates the trace for the selected process.
Parameters
Expand All @@ -219,17 +214,14 @@ def generate_trace_process_specific(process_name):
list
The combined trace of nodes and edges, including their colours and shapes."""

# load the process set, change the path if necessary
updated_process_set = pd.read_excel(settings.MEDIA_ROOT + "/" + settings.MODEL_STRUCTURE_FILE, "Process_Set")

# initialize the lists for the inputs, outputs and processes
inputs = []
outputs = []
processes = []

# filter the process set for the selected process such that only the selected process and its inputs and outputs
# are included
filtered_process_set = updated_process_set[updated_process_set["process"].str.startswith(process_name)]
filtered_process_set = process_set[process_set["process"].str.startswith(process_name)]

inputs = filtered_process_set["input"].tolist()
outputs = filtered_process_set["output"].tolist()
Expand Down Expand Up @@ -378,7 +370,7 @@ def generate_trace_process_specific(process_name):
return data


def generate_trace_commodity_specific(commodity_name, selected_sectors):
def generate_trace_commodity_specific(process_set, commodity_name, selected_sectors):
"""
Generates the trace for the selected commodity. All processes that produce the selected commodity are displayed to
the left of the commodity, all processes that consume the selected commodity are displayed to the right of the
Expand All @@ -398,9 +390,6 @@ def generate_trace_commodity_specific(commodity_name, selected_sectors):
list
The combined trace of nodes and edges, including their colours and shapes."""

# load the process set, change the path if necessary
process_set = pd.read_excel(settings.MEDIA_ROOT + "/" + settings.MODEL_STRUCTURE_FILE, "Process_Set")

# initialize the lists for the inputs, outputs and processes
inputs = []
outputs = []
Expand Down Expand Up @@ -647,6 +636,7 @@ def calculate_offset(sector, algorithm):

# generate the graph
def generate_Graph(
process_set,
selected_sectors,
algorithm,
seperate_commodities,
Expand Down Expand Up @@ -708,23 +698,23 @@ def generate_Graph(
# add traces for each selected sector and depending on whether the commodities are seperated or aggregated
if seperate_commodities == "sep":
for sector in selected_sectors:
traces = generate_trace(sector, algorithm, "sep")
traces = generate_trace(process_set, sector, algorithm, "sep")
fig.add_trace(traces[0])
fig.add_trace(traces[1])
elif seperate_commodities == "agg":
# if commodities are aggregated, the selected sectors are combined to one string to use them in the
# generate_trace function
traces = generate_trace("".join(selected_sectors), algorithm, "agg")
traces = generate_trace(process_set, "".join(selected_sectors), algorithm, "agg")
fig.add_trace(traces[0])
fig.add_trace(traces[1])

elif process_specific:
traces = generate_trace_process_specific(process_specific)
traces = generate_trace_process_specific(process_set, process_specific)
fig.add_trace(traces[0])
fig.add_trace(traces[1])

elif commodity_specific:
traces = generate_trace_commodity_specific(commodity_specific, selected_sectors)
traces = generate_trace_commodity_specific(process_set, commodity_specific, selected_sectors)
fig.add_trace(traces[0])
fig.add_trace(traces[1])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1>Network Graph</h1>
<form hx-get="/energysystem/network_graph"
hx-target="#network_graph"
hx-trigger="change">
<input name="structure" value="{{ structure_name }}" hidden>
<label for="mapping">Mapping:</label>
<select id="mapping" name="mapping">
<option label="dav" value="dav"></option>
Expand Down
4 changes: 3 additions & 1 deletion django_energysystem_viewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ def network(request):


def network_graph(request):
structure_name = request.GET.get("structure")
sectors = request.GET.getlist("sectors")
mapping = request.GET["mapping"]
sep_agg = request.GET.get("seperate_join")
process = request.GET.get("process")
commodity = request.GET.get("commodity")
return HttpResponse(ng.generate_Graph(sectors, mapping, sep_agg, process, commodity).to_html())
process_set = get_excel_data(structure_name, "Process_Set")
return HttpResponse(ng.generate_Graph(process_set, sectors, mapping, sep_agg, process, commodity).to_html())


def abbreviations(request):
Expand Down

0 comments on commit ed0ada4

Please sign in to comment.