Skip to content

Commit

Permalink
release 0.1.1
Browse files Browse the repository at this point in the history
- minor additions
  - convenient sponge network builder
  - method to write simulation to xlsx
  - ability to disable sinks
- started doc
- updated gitignore
  • Loading branch information
heinwol committed Sep 5, 2023
1 parent 594cf7f commit d734c47
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@

**/__pypackages__

# poetry builds
**/dist


## custom
.vscode
.direnv

temp
**/*.xlsx

# temp files
**/.~*
Expand Down
65 changes: 65 additions & 0 deletions jupyter/doc.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 36 additions & 13 deletions jupyter/network_main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "8122536b-12dd-4ed2-bb86-784fc55d5051",
"metadata": {},
"outputs": [],
Expand All @@ -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",
Expand Down Expand Up @@ -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
},
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <koreshkovhw@yandex.ru>"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions sponge_networks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
SpongeNetworkLayout,
SpongeNetworkTriangularBuilder,
grid_with_positions,
build_sponge_network,
)
52 changes: 50 additions & 2 deletions sponge_networks/sponge_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
6 changes: 5 additions & 1 deletion sponge_networks/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down

0 comments on commit d734c47

Please sign in to comment.