From d734c47b9145101dd79e92a21a7d724a383ccb17 Mon Sep 17 00:00:00 2001 From: heinwol Date: Tue, 5 Sep 2023 14:41:04 +0300 Subject: [PATCH] release 0.1.1 - minor additions - convenient sponge network builder - method to write simulation to xlsx - ability to disable sinks - started doc - updated gitignore --- .gitignore | 4 ++ jupyter/doc.ipynb | 65 ++++++++++++++++++++++++++++++ jupyter/network_main.ipynb | 49 ++++++++++++++++------ pyproject.toml | 2 +- sponge_networks/__init__.py | 1 + sponge_networks/sponge_networks.py | 52 +++++++++++++++++++++++- sponge_networks/utils/utils.py | 6 ++- 7 files changed, 162 insertions(+), 17 deletions(-) create mode 100644 jupyter/doc.ipynb diff --git a/.gitignore b/.gitignore index 5178fcc..1c1f19f 100644 --- a/.gitignore +++ b/.gitignore @@ -27,12 +27,16 @@ **/__pypackages__ +# poetry builds +**/dist + ## custom .vscode .direnv temp +**/*.xlsx # temp files **/.~* diff --git a/jupyter/doc.ipynb b/jupyter/doc.ipynb new file mode 100644 index 0000000..d0c4e2a --- /dev/null +++ b/jupyter/doc.ipynb @@ -0,0 +1,65 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "199b356a-4c3b-48d8-95c0-b7ea956c1083", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "sys.path.append('..')" + ] + }, + { + "cell_type": "markdown", + "id": "b1470e9a-d6f3-4eea-89fb-94aab99d9ebd", + "metadata": {}, + "source": [ + "Для того, чтобы обновить версию программы, " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "664a713c-de13-44d2-9f41-016a7efcaad3", + "metadata": {}, + "outputs": [], + "source": [ + "import sponge_networks as sn\n", + "import numpy as np\n", + "import networkx as nx\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "193ab449-2bc6-49fb-9dc1-c169fb52ea1d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/jupyter/network_main.ipynb b/jupyter/network_main.ipynb index 1f55f55..b48f2e6 100644 --- a/jupyter/network_main.ipynb +++ b/jupyter/network_main.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "8122536b-12dd-4ed2-bb86-784fc55d5051", "metadata": {}, "outputs": [], @@ -18,12 +18,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "d2e6c76f-f590-4fee-897e-c0ede2a2367b", "metadata": {}, "outputs": [], "source": [ - "import sponge_networks as rn\n", + "import sponge_networks as sn\n", "\n", "import matplotlib.pyplot as plt\n", "plt.rcParams[\"figure.figsize\"] = (10, 7)\n", @@ -58,14 +58,34 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 7, + "id": "d8d29daf-9181-420f-a3c6-01fec02b531c", + "metadata": {}, + "outputs": [], + "source": [ + "nw = sn.build_sponge_network(\n", + " sn_type=\"hexagonal\", n_cols=3, n_rows=4,\n", + " layout={\n", + " \"weights_sink_edge\": 1,\n", + " \"weights_loop\": 1,\n", + " \"weights_horizontal\": 4,\n", + " \"weights_up_down\": 5,\n", + " \"weights_down_up\": 1,\n", + " },\n", + " generate_sinks=False,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "id": "8b91dcb0-fb07-4922-999a-6589be0ace63", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "41a5c2c68e9b437e88bc46be4176e331", + "model_id": "8959c604d3d246a0a234fa140ccc2061", "version_major": 2, "version_minor": 0 }, @@ -78,17 +98,20 @@ } ], "source": [ - "nw = rn.SpongeNetwork(rn.SpongeNetworkHexagonalBuilder(n_cols=4, n_rows=3, layout=rn.LayoutTriangular(\n", - " weights_sink_edge=1,\n", - " weights_loop=1,\n", - " weights_horizontal=4,\n", - " weights_up_down=5,\n", - " weights_down_up=1\n", - ")))\n", - "sim = nw.run_sponge_simulation([10, 20, 0, 30], n_iters=100)\n", + "sim = nw.run_sponge_simulation([50, 20], n_iters=100)\n", "nw.plot_simulation(sim, scale=1.)" ] }, + { + "cell_type": "code", + "execution_count": 5, + "id": "6f47ed1c-7564-4c34-8c71-b4b5b1a03868", + "metadata": {}, + "outputs": [], + "source": [ + "sim.to_excel(\"lala.xlsx\")" + ] + }, { "cell_type": "code", "execution_count": 90, diff --git a/pyproject.toml b/pyproject.toml index ccd29fd..8a1d311 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sponge-networks" -version = "0.1.0" +version = "0.1.1" description = "a generalization of the resource network model with greedy vertices" authors = ["heinwol "] license = "MIT" diff --git a/sponge_networks/__init__.py b/sponge_networks/__init__.py index 4a4e623..b2d70b7 100644 --- a/sponge_networks/__init__.py +++ b/sponge_networks/__init__.py @@ -17,4 +17,5 @@ SpongeNetworkLayout, SpongeNetworkTriangularBuilder, grid_with_positions, + build_sponge_network, ) diff --git a/sponge_networks/sponge_networks.py b/sponge_networks/sponge_networks.py index 0d77128..c36f41b 100644 --- a/sponge_networks/sponge_networks.py +++ b/sponge_networks/sponge_networks.py @@ -188,11 +188,14 @@ def final_grid_hook(self, grid: nx.DiGraph) -> nx.DiGraph: class SpongeNetwork: - def __init__(self, builder: AbstractSpongeNetworkBuilder) -> None: + def __init__( + self, builder: AbstractSpongeNetworkBuilder, generate_sinks: bool = True + ) -> None: grid = builder.generate_initial_grid() grid = builder.generate_weights_from_layout(grid) grid = builder.generate_loops(grid) - grid = builder.generate_sinks(grid) + if generate_sinks: + grid = builder.generate_sinks(grid) grid = builder.final_grid_hook(grid) self.resource_network = ResourceNetworkGreedy(grid) @@ -350,3 +353,48 @@ def generate_weights_from_layout(self, grid: nx.DiGraph) -> nx.DiGraph: f"some strange edge encountered while building sponge network: {u} -> {v}" ) return grid + + +class _LayoutDict(TypedDict): + weights_sink_edge: float + weights_loop: float + weights_horizontal: float + weights_up_down: float + weights_down_up: float + + +def build_sponge_network( + sn_type: GridType, + n_cols: int, + n_rows: int, + layout: _LayoutDict, + generate_sinks: bool = True, +) -> SpongeNetwork: + match sn_type: + case "grid_2d": + return SpongeNetwork( + SpongeNetwork2dBuilder( + n_cols=n_cols, + n_rows=n_rows, + layout=Layout2d(**layout), + ), + generate_sinks=generate_sinks, + ) + case "hexagonal": + return SpongeNetwork( + SpongeNetworkHexagonalBuilder( + n_cols=n_cols, + n_rows=n_rows, + layout=LayoutHexagonal(**layout), + ), + generate_sinks=generate_sinks, + ) + case "triangular": + return SpongeNetwork( + SpongeNetworkTriangularBuilder( + n_cols=n_cols, + n_rows=n_rows, + layout=LayoutTriangular(**layout), + ), + generate_sinks=generate_sinks, + ) diff --git a/sponge_networks/utils/utils.py b/sponge_networks/utils/utils.py index 35cf01c..4d43c03 100644 --- a/sponge_networks/utils/utils.py +++ b/sponge_networks/utils/utils.py @@ -205,7 +205,7 @@ def __getitem__(self, time: int) -> StateArraySlice[Node]: ), } - def simple_protocol(self): + def simple_protocol(self) -> pd.DataFrame: n_iters = len(self) vertices = lmap(get(self.idx_descriptor), range(len(self.idx_descriptor))) cols = ["t"] + vertices @@ -215,6 +215,10 @@ def simple_protocol(self): df = pd.DataFrame(columns=cols, data=data) return df.set_index("t") + def to_excel(self, filename: str) -> None: + df = self.simple_protocol() + df.to_excel(filename) + def parallel_plot( G: nx.DiGraph, states: StateArray[Node], rng: Sequence[int]