Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding crossing minimisation and straightness model #136

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 116 additions & 18 deletions docs/cookbook/layout-optimisation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stratus85 not a big problem, but you can see that there are lots of changes in the notebook JSON file here which makes it hard to see the thing you actually changed. Please clear the notebook output and save before committing it to git.

"id": "6c86326f-4c4b-4468-a583-049f503f1af7",
"metadata": {
"editable": true,
Expand All @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "83d56948-5c13-4254-bf85-0c4a009627ea",
"metadata": {},
"outputs": [],
Expand All @@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "f490b5b0-ad19-48fc-bef3-0456e154e864",
"metadata": {},
"outputs": [],
Expand All @@ -61,7 +61,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "9664a9bb-f21b-411d-8571-315cb58b42b2",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -101,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "0c5cfcd8-9f3a-407f-8edf-00c9822e42de",
"metadata": {},
"outputs": [],
Expand All @@ -124,7 +124,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "a26aa4f7-e57e-48c6-b1f1-6f80ec294755",
"metadata": {},
"outputs": [],
Expand All @@ -144,10 +144,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "d112fb8e-73d6-4b27-a15e-df481b4d4860",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "eb50567887e346688e066a44518bbaca",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"SankeyWidget(groups=[{'id': 'sources', 'type': 'process', 'title': 'Sources', 'nodes': ['sources^Solar', 'sour…"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sankey_data.to_widget(width=700, height=450, margins=dict(left=100, right=120))"
]
Expand All @@ -162,20 +178,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "4e7a3d95-6167-4175-9b34-17bd3c79ced8",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<generator object optimise_node_order_model.<locals>.<genexpr> at 0x000001887C534900>\n"
]
}
],
"source": [
"sankey_data_evolved = optimise_node_order(sankey_data, group_nodes=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "93987910-ac87-49b3-a976-a4f132586901",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f1c2abb25a224f249ed40458d7a4590f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(SankeyWidget(groups=[{'id': 'sources', 'type': 'process', 'title': 'Sources', 'nodes': ['source…"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sankey_data_evolved.to_widget(width=700, height=450, margins=dict(left=100, right=120), debugging=True)"
]
Expand All @@ -190,20 +230,78 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "28392d80-9ace-47a5-ad96-5b2f43b5f2e8",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f3c1e8d7998743a7a0691c36c565e3cc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"SankeyWidget(groups=[{'id': 'sources', 'type': 'process', 'title': 'Sources', 'nodes': ['sources^Solar', 'sour…"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sankey_data_evolved.to_widget(layout=optimise_node_positions(sankey_data_evolved, scale=1.5))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "00dff780-210c-4ca4-9007-db8c54aa75c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('sources^Solar', 'electricity^*'), ('sources^Solar', 'direct_use^'), ('sources^Nuclear', 'electricity^*'), ('sources^Hydro', 'electricity^*'), ('sources^Wind', 'electricity^*'), ('sources^Geothermal', 'electricity^*'), ('sources^Geothermal', 'direct_use^ '), ('sources^Natural_Gas', 'electricity^*'), ('sources^Natural_Gas', 'direct_use^ '), ('sources^Coal', 'electricity^*'), ('sources^Coal', 'direct_use^ '), ('sources^Biomass', 'electricity^*'), ('sources^Biomass', 'direct_use^ '), ('sources^Petroleum', 'electricity^*'), ('sources^Petroleum', 'direct_use^ '), ('imports^*', 'uses^Residential'), ('electricity^*', 'uses^Commercial'), ('electricity^*', 'uses^Industrial'), ('electricity^*', 'uses^Residential'), ('electricity^*', 'uses^Transportation'), ('electricity^*', '__electricity_rejected_2^*'), ('direct_use^', 'uses^Commercial'), ('direct_use^', 'uses^Industrial'), ('direct_use^', 'uses^Residential'), ('direct_use^ ', 'uses^Commercial'), ('direct_use^ ', 'uses^Residential'), ('direct_use^ ', 'uses^Commercial'), ('direct_use^ ', 'uses^Industrial'), ('direct_use^ ', 'uses^Residential'), ('direct_use^ ', 'uses^Transportation'), ('direct_use^ ', 'uses^Commercial'), ('direct_use^ ', 'uses^Industrial'), ('direct_use^ ', 'uses^Commercial'), ('direct_use^ ', 'uses^Industrial'), ('direct_use^ ', 'uses^Residential'), ('direct_use^ ', 'uses^Transportation'), ('direct_use^ ', 'uses^Commercial'), ('direct_use^ ', 'uses^Industrial'), ('direct_use^ ', 'uses^Residential'), ('direct_use^ ', 'uses^Transportation'), ('__electricity_rejected_2^*', 'rejected^*'), ('uses^Residential', 'energy_services^*'), ('uses^Residential', 'rejected^*'), ('uses^Commercial', 'energy_services^*'), ('uses^Commercial', 'rejected^*'), ('uses^Industrial', 'energy_services^*'), ('uses^Industrial', 'rejected^*'), ('uses^Transportation', 'energy_services^*'), ('uses^Transportation', 'rejected^*')]\n"
]
}
],
"source": [
"edges = []\n",
"for flow in sankey_data_evolved.links:\n",
" edges.append((flow.source,flow.target))\n",
"print(edges)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b8c12e3b-b130-4ac2-9f7b-9ae110cc9905",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ordering( ; sources^Petroleum, sources^Biomass, sources^Natural_Gas, sources^Solar, sources^Geothermal, sources^Coal, sources^Wind, sources^Hydro, sources^Nuclear; | imports^*; direct_use^ , direct_use^ , direct_use^ , direct_use^, direct_use^ , direct_use^ , electricity^*; | ; uses^Transportation, uses^Industrial, uses^Commercial, uses^Residential, __electricity_rejected_2^*; | ; energy_services^*, rejected^*; )"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sankey_data_evolved.to_widget(layout=optimise_node_positions(sankey_data_evolved, scale=1))\n"
"sankey_data_evolved.ordering"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "venvsankey3.10.9",
"language": "python",
"name": "python3"
"name": "venvsankey3.10.9"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -215,7 +313,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions test/test_node_position_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def test_node_positions_no_overlap():
assert layout.node_positions['a2'][1] >= 20 + dy_a1 + minimum_gap


@pytest.mark.xfail(reason='need to account for scale when calculating node positions')
def test_node_positions_no_overlap_with_ccale():
#@pytest.mark.xfail(reason='need to account for scale when calculating node positions')
def test_node_positions_no_overlap_with_scale():
# Check y positions do not overlap
scale = 2
dy_a1 = 3 * scale
Expand All @@ -50,7 +50,7 @@ def test_node_positions_no_overlap_with_ccale():
assert layout.node_positions['a2'][1] >= 20 + dy_a1 + minimum_gap


#@pytest.mark.xfail(reason='need to account for offset between node position and link position')
@pytest.mark.xfail(reason='need to account for offset between node position and link position')
def test_node_positions_target_in_between_sources():
layout = optimise_node_positions(TEST_DATA_SIMPLE_MERGE, scale=1)
y = lambda k: layout.node_positions[k][1]
Expand Down