sugikey 0.1.12__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.1
2
+ Name: sugikey
3
+ Version: 0.1.12
4
+ Summary: Drawing Sankey diagrams in Python
5
+ Author: Your Name
6
+ Author-email: you@example.com
7
+ Requires-Python: >=3.8,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.8
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Requires-Dist: bokeh (>=3.1.0,<4.0.0)
14
+ Requires-Dist: matplotlib (>=3.7.1,<4.0.0)
15
+ Requires-Dist: networkx (>=3.0,<4.0)
16
+ Requires-Dist: openpyxl (>=3.1.2,<4.0.0)
17
+ Requires-Dist: pandas (>=1.5.3,<2.0.0)
18
+ Requires-Dist: pulp (>=2.7.0,<3.0.0)
19
+ Description-Content-Type: text/markdown
20
+
21
+ # Sugikey
22
+
23
+
24
+ ## Description
25
+ This package aims at providing a reasonably simple and flexible way to draw Sankey diagrams and related flow diagrams in Python.
26
+ Sankey diagrams are flow diagrams composed of arrows of width proportional to the flow of e.g. energy or mass. We use the Sugiyama method to derive [layered graph layouts](https://en.wikipedia.org/wiki/Layered_graph_drawing), hence the portmanteau _Sugi(yama)(San)key_.
27
+
28
+ Some key facts:
29
+ * Consumes [pandas](https://pandas.pydata.org/) dataframes or [networkx](https://networkx.org/) directed graphs as inputs.
30
+ * Uses [NetworkX](https://networkx.org/) for processing of the underlying graph structure.
31
+ * Produces visual output with [Matplotlib](https://matplotlib.org/).
32
+ * Limitation: the created diagrams are not interactive, i.e. the arrows cannot be moved. Interactive Sankey diagrams can be created with a variety of other tools, including [d3-Sankey](https://github.com/d3/d3-sankey).
33
+
34
+
35
+ ## Visuals
36
+ ![image info](docs/imgs/balanced_with_cross_edge.svg)
37
+
38
+ ## Installation
39
+ You will need Python >= 3.9, as well as [NetworkX](https://networkx.org/), [pandas](https://pandas.pydata.org/), [Matplotlib](https://matplotlib.org/) and [PuLP](https://coin-or.github.io/pulp/) for layout methods using mathematical optimization.
40
+ The dependencies are managed with [Poetry](https://python-poetry.org/), which you can use to install the package (see _poetry.lock_ and _pyproject.toml_).
41
+
42
+ ## Usage
43
+ The simplest way to use the package are the high-level functions _sankey_from_dig_ and _sankey_from_df_, which take as input a networkx directed graph and a pandas dataframe, respectively.
44
+
45
+ ```python
46
+ # Sankey diagram from a networkx directed graph
47
+ dig = examples.balanced_tree_with_cross_edge()
48
+ sankey.sankey_from_dig(dig)
49
+
50
+ # Sankey diagram from a pandas dataframe
51
+ flow_df = pd.read_csv(csv_path)
52
+ sankey.sankey_from_df(flow_df)
53
+ ```
54
+
55
+ Have a look at the [documentation](https://gitabab.gitlab.io/sugikey/) for more information.
56
+
57
+ ## Support
58
+
59
+ Write an issue if there is an issue.
60
+
61
+
62
+ ## Authors and acknowledgment
63
+ Zlatan B.
64
+
65
+
66
+ ## Project status
67
+ This is a personal project without any guarantee.
68
+ If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
69
+
@@ -0,0 +1,48 @@
1
+ # Sugikey
2
+
3
+
4
+ ## Description
5
+ This package aims at providing a reasonably simple and flexible way to draw Sankey diagrams and related flow diagrams in Python.
6
+ Sankey diagrams are flow diagrams composed of arrows of width proportional to the flow of e.g. energy or mass. We use the Sugiyama method to derive [layered graph layouts](https://en.wikipedia.org/wiki/Layered_graph_drawing), hence the portmanteau _Sugi(yama)(San)key_.
7
+
8
+ Some key facts:
9
+ * Consumes [pandas](https://pandas.pydata.org/) dataframes or [networkx](https://networkx.org/) directed graphs as inputs.
10
+ * Uses [NetworkX](https://networkx.org/) for processing of the underlying graph structure.
11
+ * Produces visual output with [Matplotlib](https://matplotlib.org/).
12
+ * Limitation: the created diagrams are not interactive, i.e. the arrows cannot be moved. Interactive Sankey diagrams can be created with a variety of other tools, including [d3-Sankey](https://github.com/d3/d3-sankey).
13
+
14
+
15
+ ## Visuals
16
+ ![image info](docs/imgs/balanced_with_cross_edge.svg)
17
+
18
+ ## Installation
19
+ You will need Python >= 3.9, as well as [NetworkX](https://networkx.org/), [pandas](https://pandas.pydata.org/), [Matplotlib](https://matplotlib.org/) and [PuLP](https://coin-or.github.io/pulp/) for layout methods using mathematical optimization.
20
+ The dependencies are managed with [Poetry](https://python-poetry.org/), which you can use to install the package (see _poetry.lock_ and _pyproject.toml_).
21
+
22
+ ## Usage
23
+ The simplest way to use the package are the high-level functions _sankey_from_dig_ and _sankey_from_df_, which take as input a networkx directed graph and a pandas dataframe, respectively.
24
+
25
+ ```python
26
+ # Sankey diagram from a networkx directed graph
27
+ dig = examples.balanced_tree_with_cross_edge()
28
+ sankey.sankey_from_dig(dig)
29
+
30
+ # Sankey diagram from a pandas dataframe
31
+ flow_df = pd.read_csv(csv_path)
32
+ sankey.sankey_from_df(flow_df)
33
+ ```
34
+
35
+ Have a look at the [documentation](https://gitabab.gitlab.io/sugikey/) for more information.
36
+
37
+ ## Support
38
+
39
+ Write an issue if there is an issue.
40
+
41
+
42
+ ## Authors and acknowledgment
43
+ Zlatan B.
44
+
45
+
46
+ ## Project status
47
+ This is a personal project without any guarantee.
48
+ If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
@@ -0,0 +1,38 @@
1
+ [tool.poetry]
2
+ name = "sugikey"
3
+ version = "0.1.12"
4
+ description = "Drawing Sankey diagrams in Python"
5
+ authors = ["Your Name <you@example.com>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.8"
10
+ pandas = "^1.5.3"
11
+ matplotlib = "^3.7.1"
12
+ networkx = "^3.0"
13
+ pulp = "^2.7.0"
14
+ openpyxl = "^3.1.2"
15
+ bokeh = "^3.1.0"
16
+
17
+ [tool.poetry.group.dev.dependencies]
18
+ pylint = "^2.17.1"
19
+ pytest = "^7.2.2"
20
+ jupyterlab = "^3.6.3"
21
+ black = "^23.3.0"
22
+ isort = "^5.12.0"
23
+ mypy = "^1.1.1"
24
+ python-semantic-release = "^7.33.2"
25
+ sphinx = "^6.1.3"
26
+ sphinx-gallery = "^0.13.0"
27
+ myst-parser = "^2.0.0"
28
+
29
+ [build-system]
30
+ requires = ["poetry-core"]
31
+ build-backend = "poetry.core.masonry.api"
32
+
33
+ [tool.semantic_release]
34
+ version_toml = "pyproject.toml:tool.poetry.version"
35
+ branch = "main"
36
+ upload_to_repository = true
37
+ hvcs = "gitlab"
38
+ build_command = "pip install poetry && poetry build"
@@ -0,0 +1,3 @@
1
+ # This file marks the directory as Python package directories.
2
+ # It can be left empty.
3
+ # For more informatio see: https://pythontips.com/2013/07/28/what-is-__init__-py/
@@ -0,0 +1,116 @@
1
+ """Drawing Sankey diagrams with Bokeh
2
+
3
+ Bokeh allows interactions to be included in plots: zooming, panning, showing things on hover etc.
4
+ """
5
+
6
+ from typing import Any, Dict, Optional
7
+
8
+ import networkx as nx
9
+ import pandas as pd
10
+ from bokeh.models import ColumnDataSource, HoverTool
11
+ from bokeh.plotting import figure
12
+
13
+ from . import draw
14
+
15
+
16
+ def get_bokeh_kwargs(pyplot_kwargs: Dict[str, Any]) -> dict:
17
+ """Translate pyplot keyword arguments in bokeh keyword arguments
18
+
19
+ Args:
20
+ pyplot_kwargs (Dict[str, Any]): A dictionary of pyplot keyword arguments.
21
+
22
+ Returns:
23
+ bk_kwargs: A dictionary of bokeh keyword arguments.
24
+ """
25
+ bk_kwargs = {}
26
+ kw_translate_dict = {"lw": "line_width"}
27
+ for plt_kw, kw_val in pyplot_kwargs.items():
28
+ bk_kw = kw_translate_dict.get(plt_kw, plt_kw)
29
+ bk_kwargs[bk_kw] = kw_val
30
+ return bk_kwargs
31
+
32
+
33
+ def draw_sankey_bokeh(dig: nx.DiGraph, draw_config: Optional[draw.DrawConfig] = None):
34
+ """The actual drawing of a Sankey plot in bokeh
35
+
36
+ Args:
37
+ dig
38
+ draw_config
39
+
40
+ Returns:
41
+ figure
42
+ """
43
+
44
+ if draw_config is None:
45
+ draw_config = draw.DrawConfig()
46
+
47
+ diagram = draw.get_sankey_diagram(dig, draw_config)
48
+
49
+ x_range, y_range = diagram.coordinate_range
50
+
51
+ plot = figure(
52
+ width=800,
53
+ height=300,
54
+ title="",
55
+ tools="",
56
+ toolbar_location=None,
57
+ match_aspect=True,
58
+ x_range=x_range,
59
+ y_range=y_range,
60
+ )
61
+
62
+ for pline in diagram.lines:
63
+ plot.line(
64
+ pline.x_list, pline.y_list, **get_bokeh_kwargs(draw_config.edge_edge_kw)
65
+ )
66
+
67
+ filled_df = pd.DataFrame(
68
+ {"x": pline.x_list, "y": pline.y_list, "name": pline.name}
69
+ for pline in diagram.filled
70
+ )
71
+ filled_source = ColumnDataSource(filled_df)
72
+ fill_renderer = plot.patches(
73
+ source=filled_source,
74
+ xs="x",
75
+ ys="y",
76
+ **get_bokeh_kwargs(draw_config.edge_fill_kw)
77
+ )
78
+ fill_hover = HoverTool(renderers=[fill_renderer], tooltips=[("", "@name")])
79
+ plot.add_tools(fill_hover)
80
+
81
+ node_label_df = pd.DataFrame(
82
+ [
83
+ {"x": label.x_txt, "y": label.y_txt, "text": label.text}
84
+ for label in diagram.labels
85
+ if label.category == "node_label"
86
+ ]
87
+ )
88
+ node_label_source = ColumnDataSource(node_label_df)
89
+ plot.text(
90
+ source=node_label_source,
91
+ x="x",
92
+ y="y",
93
+ text="text",
94
+ **draw_config.node_label_kwargs
95
+ )
96
+
97
+ edge_label_df = pd.DataFrame(
98
+ [
99
+ {"x": label.x_txt, "y": label.y_txt, "text": label.text}
100
+ for label in diagram.labels
101
+ if label.category == "edge_label"
102
+ ]
103
+ )
104
+ edge_label_source = ColumnDataSource(edge_label_df)
105
+ plot.text(
106
+ source=edge_label_source,
107
+ x="x",
108
+ y="y",
109
+ text="text",
110
+ **draw_config.edge_label_kwargs
111
+ )
112
+
113
+ plot.xgrid.grid_line_color = None
114
+ plot.ygrid.grid_line_color = None
115
+ plot.axis.visible = False
116
+ return plot