pyafv 0.3.3__cp39-cp39-macosx_10_9_x86_64.whl

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,117 @@
1
+ import numpy as np
2
+ from scipy.optimize import minimize
3
+ from dataclasses import dataclass, replace
4
+
5
+
6
+ def sigmoid(x):
7
+ # stable sigmoid that handles large |x|
8
+ if x >= 0:
9
+ z = np.exp(-x)
10
+ return 1 / (1 + z)
11
+ else:
12
+ z = np.exp(x)
13
+ return z / (1 + z)
14
+
15
+
16
+ @dataclass(frozen=True)
17
+ class PhysicalParams:
18
+ # Radius (maximal) of the Voronoi cells
19
+ r: float = 1.0
20
+ A0: float = np.pi # Preferred area of the Voronoi cells
21
+ P0: float = 4.8 # Preferred perimeter of the Voronoi cells
22
+ KA: float = 1.0 # Area elasticity
23
+ KP: float = 1.0 # Perimeter elasticity
24
+ lambda_tension: float = 0.2 # Tension difference
25
+ delta: float = 0. # Small offset to avoid singularities
26
+
27
+ def get_steady_state(self):
28
+ # compute steady-state (l,d) given physical params
29
+ params = [self.KA, self.KP, self.A0, self.P0, self.lambda_tension]
30
+ result = self._minimize_energy(params, restarts=10)
31
+ l, d = result[0]
32
+ return l, d
33
+
34
+ def with_optimal_radius(self):
35
+ """Returns a new instance with the radius updated to steady state."""
36
+ l, d = self.get_steady_state()
37
+ new_params = replace(self, r=l)
38
+ return new_params
39
+
40
+ def with_delta(self, delta_new: float):
41
+ """Returns a new instance with the specified delta."""
42
+ return replace(self, delta=delta_new)
43
+
44
+ def _energy_unconstrained(self, z, params):
45
+ v, u = float(z[0]), float(z[1])
46
+ l = np.exp(v) # l > 0
47
+ phi = 0.5*np.pi * sigmoid(u) # phi in (0, pi/2)
48
+ s, c = np.sin(phi), np.cos(phi)
49
+ theta = np.pi + 2.0*phi
50
+ A = 0.5*l*l*(theta + np.sin(2.0*phi))
51
+ P = 2.0*l*c + l*theta
52
+ ln = l*theta
53
+
54
+ KA, KP, A0, P0, Lambda = params
55
+ return KA * (A - A0)**2 + KP * (P - P0)**2 + Lambda * ln
56
+
57
+ def _minimize_energy(self, params, restarts=10, seed=None):
58
+ rng = np.random.default_rng(seed)
59
+ best = None
60
+ for _ in range(restarts):
61
+ z0 = rng.normal(size=2)
62
+
63
+ res = minimize(lambda z: self._energy_unconstrained(z, params), z0, method="BFGS",
64
+ options={"gtol": 1e-8, "maxiter": 1e4})
65
+ val = res.fun
66
+ z = res.x
67
+
68
+ if (best is None) or (val < best[0]):
69
+ best = (val, z)
70
+
71
+ # map back to (l,d)
72
+ val, z = best
73
+ v, u = float(z[0]), float(z[1])
74
+ l = np.exp(v)
75
+ phi = 0.5*np.pi * sigmoid(u)
76
+ d = l*np.sin(phi)
77
+
78
+ return [l, d], val
79
+
80
+
81
+ def target_delta(params: PhysicalParams, target_force: float) -> float:
82
+ """
83
+ Given physical parameters and a target detachment force, compute the corresponding delta.
84
+ """
85
+ KP, A0, P0, Lambda = params.KP, params.A0, params.P0, params.lambda_tension
86
+ l = params.r
87
+
88
+ distances = np.linspace(1e-6, 2*l-(1e-6), 10_000)
89
+ detachment_forces = []
90
+ for distance in distances:
91
+ epsilon = l - (distance/2.)
92
+
93
+ theta = 2 * np.pi - 2 * np.arctan2(np.sqrt(l**2 - (l - epsilon)**2), l - epsilon)
94
+ A = (l - epsilon) * np.sqrt(l**2 -
95
+ (l - epsilon)**2) + 0.5 * (l**2 * theta)
96
+ P = 2 * np.sqrt(l**2 - (l - epsilon)**2) + l * theta
97
+
98
+ f = 4. * np.sqrt((2-epsilon) * epsilon) * (A - A0 + KP * ((P - P0)/(2 - epsilon))
99
+ + (Lambda/2) * (1./((2-epsilon)*epsilon)))
100
+ detachment_forces.append(f)
101
+
102
+ # print(detachment_forces)
103
+
104
+ detachment_forces = np.array(detachment_forces)
105
+
106
+ idx = np.abs(detachment_forces[None, :] - target_force).argmin()
107
+ target_distances = distances[idx]
108
+
109
+ delta = np.sqrt(4*(l**2) - target_distances**2)
110
+
111
+ return delta
112
+
113
+
114
+ __all__ = [
115
+ "PhysicalParams",
116
+ "target_delta",
117
+ ]
pyafv/simulator.py ADDED
@@ -0,0 +1,37 @@
1
+ # API-facing wrapper
2
+
3
+ from .backend import backend_simulator, _BACKEND_NAME
4
+
5
+
6
+ class FiniteVoronoiSimulator(backend_simulator):
7
+ """Finite Voronoi Simulator.
8
+
9
+ This class provides an interface to simulate finite Voronoi models.
10
+ It wraps around the backend simulator implementation, which may be
11
+ either a Cython-accelerated version or a pure Python fallback.
12
+
13
+ Attributes:
14
+ See `backend_simulator` for details.
15
+ """
16
+ # Define as a class attribute
17
+ _BACKEND = _BACKEND_NAME
18
+
19
+ def __init__(self, *args, **kwargs):
20
+ # Ensure you call the parent constructor
21
+ super().__init__(*args, **kwargs)
22
+
23
+ if self._BACKEND not in {"cython", "numba"}: # pragma: no cover
24
+ # raise warning to inform user about fallback
25
+ import warnings
26
+ warnings.warn(
27
+ "Could not import the Cython-built extension module. "
28
+ "Falling back to the pure Python implementation, which may be slower. "
29
+ "To enable the accelerated version, ensure that all dependencies are installed.",
30
+ RuntimeWarning,
31
+ stacklevel=2,
32
+ )
33
+
34
+
35
+ __all__ = [
36
+ "FiniteVoronoiSimulator",
37
+ ]
@@ -0,0 +1,110 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyafv
3
+ Version: 0.3.3
4
+ Summary: Python implementation of the active-finite-Voronoi (AFV) model
5
+ Author-email: "Wei Wang (汪巍)" <ww000721@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Download, https://pypi.org/project/pyafv/#files
8
+ Project-URL: Source Code, https://github.com/wwang721/pyafv
9
+ Keywords: voronoi-model,cellular-patterns,biological-modeling
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Operating System :: OS Independent
21
+ Classifier: Topic :: Scientific/Engineering
22
+ Requires-Python: <3.15,>=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy>=1.26.4
26
+ Requires-Dist: scipy>=1.13.1
27
+ Requires-Dist: matplotlib>=3.8.4
28
+ Provides-Extra: examples
29
+ Requires-Dist: ipywidgets>=8.1.5; extra == "examples"
30
+ Requires-Dist: jupyter>=1.1.0; extra == "examples"
31
+ Requires-Dist: tqdm>=4.67.1; extra == "examples"
32
+ Dynamic: license-file
33
+
34
+ [![PyPi](https://img.shields.io/pypi/v/pyafv)](https://pypi.org/project/pyafv/)
35
+ [![Downloads](https://img.shields.io/pypi/dm/pyafv.svg)](https://pypi.org/project/pyafv/)
36
+ [![Zenodo](https://zenodo.org/badge/1124385738.svg)](https://doi.org/10.5281/zenodo.18091659)
37
+ <!--[![pytest](https://github.com/wwang721/pyafv/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/wwang721/pyafv/actions/workflows/tests.yml?query=branch:main)-->
38
+ [![pytest](https://github.com/wwang721/pyafv/actions/workflows/tests.yml/badge.svg)](https://github.com/wwang721/pyafv/actions/workflows/tests.yml)
39
+ [![codecov](https://codecov.io/github/wwang721/pyafv/branch/main/graph/badge.svg?token=VSXSOX8HVS)](https://codecov.io/github/wwang721/pyafv/tree/main)
40
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
41
+
42
+
43
+ # PyAFV
44
+
45
+ Python code that implements the **active-finite-Voronoi (AFV) model** in 2D.
46
+ The AFV framework was introduced and developed in, for example, Refs. [[1](#huang2023bridging)&ndash;[3](#wang2026divergence)].
47
+
48
+
49
+ ## Installation
50
+
51
+ Install **PyAFV** with:
52
+ ```bash
53
+ pip install pyafv
54
+ ```
55
+ This package requires Python ≥ 3.9 and < 3.15.
56
+
57
+
58
+ ## Usage
59
+
60
+ <!--Using `uv run python`, you should be able to import `pyafv` from anywhere within the repository directory.-->
61
+ The following example demonstrates how to construct a finite-Voronoi diagram:
62
+ ```python
63
+ import numpy as np
64
+ import pyafv as afv
65
+
66
+ N = 100 # number of cells
67
+ pts = np.random.rand(N, 2) * 10 # initial positions
68
+ params = afv.PhysicalParams() # use default parameter values
69
+ sim = afv.FiniteVoronoiSimulator(pts, params) # initialize the simulator
70
+ sim.plot_2d(show=True) # visualize the Voronoi diagram
71
+ ```
72
+ To compute the conservative forces and extract detailed geometric information (e.g., cell areas, vertices, and edges), call:
73
+ ```python
74
+ diag = sim.build()
75
+ ```
76
+ The returned object `diag` is a Python `dict` containing these quantities.
77
+
78
+
79
+ ## More information
80
+
81
+ GitHub: [https://github.com/wwang721/pyafv](https://github.com/wwang721/pyafv)
82
+
83
+ See important [**issues**](https://github.com/wwang721/pyafv/issues?q=is%3Aissue%20state%3Aclosed) for additional context, such as:
84
+ * [QhullError when 3+ points are collinear #1](https://github.com/wwang721/pyafv/issues/1) [Closed - see [comments](https://github.com/wwang721/pyafv/issues/1#issuecomment-3701355742)]
85
+ * [Add customized plotting to examples illustrating access to vertices and edges #5](https://github.com/wwang721/pyafv/issues/5) [Completed in PR [#7](https://github.com/wwang721/pyafv/pull/7)]
86
+ * [Time step dependence of intercellular adhesion in simulations #8](https://github.com/wwang721/pyafv/issues/8) [Closed in PR [#9](https://github.com/wwang721/pyafv/pull/9)]
87
+
88
+
89
+ ## References
90
+
91
+ <table>
92
+ <tr>
93
+ <td id="huang2023bridging" valign="top">[1]</td>
94
+ <td>
95
+ J. Huang, H. Levine, and D. Bi, <em>Bridging the gap between collective motility and epithelial-mesenchymal transitions through the active finite Voronoi model</em>, <a href="https://doi.org/10.1039/D3SM00327B">Soft Matter <strong>19</strong>, 9389 (2023)</a>.
96
+ </td>
97
+ </tr>
98
+ <tr>
99
+ <td id="teomy2018confluent" valign="top">[2]</td>
100
+ <td>
101
+ E. Teomy, D. A. Kessler, and H. Levine, <em>Confluent and nonconfluent phases in a model of cell tissue</em>, <a href="https://doi.org/10.1103/PhysRevE.98.042418">Phys. Rev. E <strong>98</strong>, 042418 (2018)</a>.
102
+ </td>
103
+ </tr>
104
+ <tr>
105
+ <td id="wang2026divergence" valign="top">[3]</td>
106
+ <td>
107
+ W. Wang (汪巍) and B. A. Camley, <em>Divergence of detachment forces in the finite-Voronoi model</em>, manuscript in preparation (2026).
108
+ </td>
109
+ </tr>
110
+ </table>
@@ -0,0 +1,12 @@
1
+ pyafv-0.3.3.dist-info/RECORD,,
2
+ pyafv-0.3.3.dist-info/WHEEL,sha256=Prymy4iewnr2C3GLPmIomJyLIRtmCgaf6MSAdd7b500,135
3
+ pyafv-0.3.3.dist-info/top_level.txt,sha256=mrKQNqc4GQxuZ7hd5UrKxbA_AJsuSqiJyMxL7Nu7va0,6
4
+ pyafv-0.3.3.dist-info/METADATA,sha256=kgjVhTWWgdBpTa-dOvL96pIhMeML5_iLYHBjPEj3QbE,5128
5
+ pyafv-0.3.3.dist-info/licenses/LICENSE,sha256=TNjxBxdiOzyewn2FP7g1FjW3CJoxiGCY6ShPVFv02G8,1065
6
+ pyafv/finite_voronoi_fast.py,sha256=tjzzPpCCNlQ_5cnglTsdBV2IV6Zw-GKpGiS_x81dBNY,36095
7
+ pyafv/backend.py,sha256=p0gXsoZdNxytiBhg-txa9nD1HLzSZUwKPFE8BdWAHAY,395
8
+ pyafv/__init__.py,sha256=A42X5OaACYmVnWGnSDHxg9h-63NnadocQbOuZ6yRnq0,379
9
+ pyafv/physical_params.py,sha256=QPM1CCLx_dlof1WsykHwMLfRMctr5ALlMLVmrpiSp_c,3901
10
+ pyafv/cell_geom.cpython-39-darwin.so,sha256=BpbdoqjSnS7nILpkugR4vUaw0DtdVbXs9RzEPEhMVe8,319456
11
+ pyafv/simulator.py,sha256=foVqh3vKp7-IFqcd3gd6FUIbw-xcXAxVAeUwosHVHS4,1218
12
+ pyafv/finite_voronoi_fallback.py,sha256=NiguvSeLUbDQ2wOH3bnx0rxzQWBEbuP5-sjkWruQigg,42827
@@ -0,0 +1,6 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp39-cp39-macosx_10_9_x86_64
5
+ Generator: delocate 0.13.0
6
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Wei Wang
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.
@@ -0,0 +1 @@
1
+ pyafv