sugikey 0.0.0__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.
sugikey-0.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 spectalizer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
sugikey-0.0.0/PKG-INFO ADDED
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.3
2
+ Name: sugikey
3
+ Version: 0.0.0
4
+ Summary: Drawing Sankey diagrams in Python
5
+ Author: Spectalizer
6
+ Author-email: spectalizer@protonmail.com
7
+ Requires-Python: >=3.10,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Requires-Dist: bokeh (>=3.4.0,<4.0.0)
14
+ Requires-Dist: matplotlib (>=3.8.4,<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 (>=2.2.1,<3.0.0)
18
+ Requires-Dist: pulp (>=2.8.0,<3.0.0)
19
+ Requires-Dist: sphinx-material (>=0.0.36,<0.0.37)
20
+ Project-URL: Repository, https://github.com/spectalizer/Sugikey/
21
+ Description-Content-Type: text/markdown
22
+
23
+ # Sugikey
24
+
25
+
26
+ ## Description
27
+ This package aims at providing a reasonably simple and flexible way to draw Sankey diagrams and related flow diagrams in Python.
28
+ 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_.
29
+
30
+ Some key facts:
31
+ * Consumes [pandas](https://pandas.pydata.org/) dataframes or [networkx](https://networkx.org/) directed graphs as inputs.
32
+ * Uses [NetworkX](https://networkx.org/) for processing of the underlying graph structure.
33
+ * Produces visual output with [Matplotlib](https://matplotlib.org/).
34
+ * 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).
35
+
36
+ Have a look at the [documentation](https://spectalizer.github.io/Sugikey/) for more information.
37
+
38
+ ## Visuals
39
+ ![image info](docs/imgs/balanced_with_cross_edge.svg)
40
+
41
+ ## Installation
42
+ The simplest way to use Sugikey is to install the [pypi package](https://pypi.org/project/sugikey/): `pip install sugikey`.
43
+
44
+ You can also use the Python code from the repository, provided you have installed the required packages. We use [Poetry](https://python-poetry.org/) for dependency management.
45
+
46
+ You will need Python >= 3.10, 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.
47
+ The dependencies are managed with [Poetry](https://python-poetry.org/), which you can use to install the package (see _poetry.lock_ and _pyproject.toml_).
48
+
49
+ ## Usage
50
+ 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.
51
+
52
+ ```python
53
+ import pandas as pd
54
+ from sugikey import sankey
55
+
56
+ # Sankey diagram from a pandas dataframe
57
+ flow_df = pd.read_csv(csv_path)
58
+ sankey.sankey_from_df(flow_df)
59
+ ```
60
+
61
+ ```python
62
+ from sugikey import sankey
63
+
64
+ # Sankey diagram from a networkx directed graph
65
+ dig = examples.balanced_tree_with_cross_edge()
66
+ sankey.sankey_from_dig(dig)
67
+ ```
68
+
69
+ Have a look at the [documentation](https://spectalizer.github.io/Sugikey/) for more information.
70
+
71
+ ## More on Sankey diagrams
72
+
73
+ * [Why Sankey diagrams are so great for understanding energy data](https://medium.com/@spectalizer/why-sankey-diagrams-are-so-great-for-understanding-energy-data-b14649d40890)
74
+
75
+ * [Sankey diagrams now have the package they deserve](https://medium.com/@spectalizer/sankey-diagrams-now-have-the-new-python-package-they-deserved-68754a0830d3)
76
+
77
+ ## Support
78
+
79
+ Write an issue if there is an issue.
80
+
81
+
82
+ ## Authors and acknowledgment
83
+ Zlatan B.
84
+
85
+
86
+ ## Project status
87
+ This is a personal project without any guarantee.
88
+ 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.
89
+
@@ -0,0 +1,66 @@
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
+ Have a look at the [documentation](https://spectalizer.github.io/Sugikey/) for more information.
15
+
16
+ ## Visuals
17
+ ![image info](docs/imgs/balanced_with_cross_edge.svg)
18
+
19
+ ## Installation
20
+ The simplest way to use Sugikey is to install the [pypi package](https://pypi.org/project/sugikey/): `pip install sugikey`.
21
+
22
+ You can also use the Python code from the repository, provided you have installed the required packages. We use [Poetry](https://python-poetry.org/) for dependency management.
23
+
24
+ You will need Python >= 3.10, 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.
25
+ The dependencies are managed with [Poetry](https://python-poetry.org/), which you can use to install the package (see _poetry.lock_ and _pyproject.toml_).
26
+
27
+ ## Usage
28
+ 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.
29
+
30
+ ```python
31
+ import pandas as pd
32
+ from sugikey import sankey
33
+
34
+ # Sankey diagram from a pandas dataframe
35
+ flow_df = pd.read_csv(csv_path)
36
+ sankey.sankey_from_df(flow_df)
37
+ ```
38
+
39
+ ```python
40
+ from sugikey import sankey
41
+
42
+ # Sankey diagram from a networkx directed graph
43
+ dig = examples.balanced_tree_with_cross_edge()
44
+ sankey.sankey_from_dig(dig)
45
+ ```
46
+
47
+ Have a look at the [documentation](https://spectalizer.github.io/Sugikey/) for more information.
48
+
49
+ ## More on Sankey diagrams
50
+
51
+ * [Why Sankey diagrams are so great for understanding energy data](https://medium.com/@spectalizer/why-sankey-diagrams-are-so-great-for-understanding-energy-data-b14649d40890)
52
+
53
+ * [Sankey diagrams now have the package they deserve](https://medium.com/@spectalizer/sankey-diagrams-now-have-the-new-python-package-they-deserved-68754a0830d3)
54
+
55
+ ## Support
56
+
57
+ Write an issue if there is an issue.
58
+
59
+
60
+ ## Authors and acknowledgment
61
+ Zlatan B.
62
+
63
+
64
+ ## Project status
65
+ This is a personal project without any guarantee.
66
+ 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,46 @@
1
+ [project]
2
+ name = "sugikey"
3
+ description = "Drawing Sankey diagrams in Python"
4
+ authors = [
5
+ { name = "Spectalizer", email = "spectalizer@protonmail.com" },
6
+ ]
7
+ readme = "README.md"
8
+ urls = {repository = "https://github.com/spectalizer/Sugikey/" }
9
+
10
+ [tool.poetry]
11
+ version = "0.0.0"
12
+ readme = "README.md"
13
+
14
+ [tool.poetry.dependencies]
15
+ python = "^3.10"
16
+ pandas = "^2.2.1"
17
+ matplotlib = "^3.8.4"
18
+ networkx = "^3.0"
19
+ pulp = "^2.8.0"
20
+ openpyxl = "^3.1.2"
21
+ bokeh = "^3.4.0"
22
+ sphinx-material = "^0.0.36"
23
+
24
+ [tool.poetry.group.dev.dependencies]
25
+ pylint = "^2.17.1"
26
+ pytest = "^7.2.2"
27
+ jupyterlab = "^3.6.3"
28
+ black = "^23.3.0"
29
+ isort = "^5.12.0"
30
+ mypy = "^1.1.1"
31
+ python-semantic-release = "^9.21.1"
32
+ mkdocs-material = "^9.6.12"
33
+
34
+ [build-system]
35
+ requires = ["poetry-core"]
36
+ build-backend = "poetry.core.masonry.api"
37
+
38
+ [tool.semantic_release]
39
+ version_toml = ["pyproject.toml:tool.poetry.version"]
40
+ branch = "main"
41
+ upload_to_repository = true
42
+ hvcs = "gitlab"
43
+ build_command = "pip install poetry && poetry build"
44
+
45
+ [tool.mypy]
46
+ ignore_missing_imports = true
@@ -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