Skip to content

Commit

Permalink
Merge 4f570f0 into 1292ce4
Browse files Browse the repository at this point in the history
  • Loading branch information
willu47 committed Oct 30, 2019
2 parents 1292ce4 + 4f570f0 commit 5837d71
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

Version 0.4
===========
- Tidy up the command line interface
- Convert to/from SQLite database from/to datapackage
- Remove rotten pygraphviz dependency

Version 0.3
===========

Expand Down
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ which can be difficult to install on Windows.

The easiest way to install the dependencies is to use miniconda.

1. Obtain the .. _miniconda package: https://docs.conda.io/en/latest/miniconda.html
1. Obtain the `miniconda package: <https://docs.conda.io/en/latest/miniconda.html>`_
2. Add the **conda-forge** channel ``conda config --add channels conda-forge``
3. Create a new Python environment ``conda create -n myenv python=3.7 networkx datapackage pandas pulp pygraphviz xlrd``
3. Create a new Python environment
``conda create -n myenv python=3.7 networkx datapackage
pandas pulp graphviz xlrd``
4. Activate the new environment ``conda activate myenv``
5. Use pip to install otoole ``pip install otoole``

Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ Check the version installed::

Download an OSeMOSYS datapackage and convert it to a modelfile::

otoole prep datafile https://zenodo.org/record/3479823/files/KTH-dESA/simplicity-v0.1a0.zip ./simplicity.txt
otoole convert datapackage datafile https://zenodo.org/record/3479823/files/KTH-dESA/simplicity-v0.1a0.zip simplicity.txt

Visualise the Reference Energy System::

otoole viz res https://zenodo.org/record/3479823/files/KTH-dESA/simplicity-v0.1a0.zip res.png && open res.png

Alternatively, convert an OSeMOSYS datafile to a datapackage::

otoole convert --convert_from datafile --convert_to datapackage --from_file simplicity.txt --to_file simplicity
otoole convert datafile datapackage simplicity.txt simplicity


Contents
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ importlib_resources; python_version<'3.7'
networkx
pandas
pulp
pygraphviz
pydot
pyyaml
xlrd
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup_requires = pyscaffold>=3.2a0,<3.3a0
install_requires =
xlrd
pyyaml
pygraphviz
pydot
importlib_resources; python_version<'3.7'
datapackage
pandas
Expand Down
35 changes: 23 additions & 12 deletions src/otoole/visualise/res.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Visualise the reference energy system
"""
import logging
import os
import sys
from typing import Dict, List, Tuple

import networkx as nx
import networkx as nx # mypy: ignore
from datapackage import Package

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +26,7 @@ def create_res(path_to_datapackage: str, path_to_resfile: str):
path_to_datapackage : str
The path to the ``datapackage.json``
path_to_resfile : str
The path to the PNG file to be created
The path to the image file to be created
"""
logger.debug(path_to_resfile, path_to_resfile)
package = load_datapackage(path_to_datapackage)
Expand All @@ -36,8 +37,8 @@ def create_res(path_to_datapackage: str, path_to_resfile: str):
emission = package.get_resource('EMISSION').read()

def extract_nodes(package_rows: List[List], node_type='technology',
color='red', shape='circle') -> List:
nodes = [(x[0], {'type': node_type, 'name': x,
color='red', shape='circle') -> List[Tuple[str, Dict]]:
nodes = [(x[0], {'type': node_type,
'fillcolor': color, 'shape': shape,
'style': 'filled'}
)
Expand Down Expand Up @@ -101,7 +102,7 @@ def extract_edges(package_rows: List[Dict], from_column: str,
nodes += extract_nodes(emission, node_type='emission', color='grey')
nodes += [('AnnualDemand',
{'type': 'demand', 'fillcolor': 'green',
'name': 'AccumulatedAnnualDemand',
'label': 'AccumulatedAnnualDemand',
'style': 'filled'}
)
]
Expand Down Expand Up @@ -146,17 +147,27 @@ def draw_graph(graph, path_to_resfile):
path_to_resfile : str
The file path of the PNG image file that will be created
"""
for node, attributes in graph.nodes.data():
logger.debug("%s: %s", node, attributes)
for source, sink, attributes in graph.edges.data():
logger.debug("%s-%s: %s", source, sink, attributes)

pygraph = nx.nx_agraph.to_agraph(graph)
pygraph.graph_attr['rankdir'] = 'LR'
pygraph.graph_attr['splines'] = 'ortho'
pygraph.graph_attr['concentrate'] = 'true'
filename, ext = os.path.splitext(path_to_resfile)
nx.write_graphml(graph, filename + '.graphml')
dot_graph = nx.nx_pydot.to_pydot(graph)

pygraph.layout(prog='dot')
pygraph.draw(path_to_resfile)
dot_graph.set('rankdir', 'LR')
dot_graph.set('splines', 'ortho')
dot_graph.set('concentrate', 'true')

image_format = ext.strip(".")

def build_graph(nodes: List[Tuple], edges: List[Tuple[str, str, Dict]]):
image = dot_graph.create(prog='dot', format=image_format)
with open(path_to_resfile, 'wb') as image_file:
image_file.write(image)


def build_graph(nodes: List[Tuple[str, Dict]], edges: List[Tuple[str, str, Dict]]) -> nx.DiGraph:
"""Builds the graph using networkx
Arguments
Expand Down
10 changes: 10 additions & 0 deletions tests/test_visualise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import tempfile

from otoole.visualise import create_res


def test_create_res():

url = 'https://raw.githubusercontent.com/OSeMOSYS/simplicity/master/datapackage.json'
_, path_to_resfile = tempfile.mkstemp(suffix='.pdf')
create_res(url, path_to_resfile)
3 changes: 2 additions & 1 deletion tests/travis_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ if [[ "$DISTRIB" == "conda" ]]; then
# Configure the conda environment and put it in the path using the
# provided versions
# (prefer local venv, since the miniconda folder is cached)
conda create -p ./.venv --yes python=${PYTHON_VERSION} pip virtualenv pandas xlrd pyyaml
conda config --add channels conda-forge
conda create -p ./.venv --yes python=${PYTHON_VERSION} pip virtualenv pandas xlrd pyyaml graphviz networkx pulp
source activate ./.venv
fi

Expand Down

0 comments on commit 5837d71

Please sign in to comment.