Metadata-Version: 2.4
Name: lcq-persistence
Version: 0.1.0
Summary: Persistent lower central series quotients (LCQ barcodes) of filtered 2-dimensional CW complexes, in pure Python
Author: Gonzalo Ortega Carpintero
License-Expression: GPL-3.0-or-later
Project-URL: Repository, https://github.com/Gonzalo-Ortega/lcq-persistence
Project-URL: Issues, https://github.com/Gonzalo-Ortega/lcq-persistence/issues
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: matplotlib
Requires-Dist: gudhi
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# lcq-persistence

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24474871.svg)](https://doi.org/10.5281/zenodo.24474871)

Persistent lower central series quotients (LCQ barcodes) of filtered
2-dimensional CW complexes, in pure Python.

A filtered complex $X_0 \subseteq X_1 \subseteq \dots \subseteq X_N$ has a
filtration of fundamental groups $G_t = \pi_1(X_t)$. For every $k \geq 1$,
the layers $\gamma_k(G_t) / \gamma_{k+1}(G_t) \otimes \mathbb{Q}$ of their
lower central series, with the maps induced by the inclusions, form a
persistence module, and this package computes its barcode. Layer 1 is
$H_1$, so its barcode is the $H_1$ barcode. The higher layers can tell apart
spaces that homology cannot: a torus from $S^2 \vee S^1 \vee S^1$, or the
complement of the Borromean rings from that of the unlink.

## Installation

Python 3.10 or later:

```bash
pip install lcq-persistence
```

or, to work on the code:

```bash
git clone https://github.com/Gonzalo-Ortega/lcq-persistence.git
cd lcq-persistence
pip install -e .
```

This also installs GUDHI, NumPy, SciPy and Matplotlib, which the examples,
the point cloud filtrations and the plots use. The computation of the
barcodes itself needs nothing beyond Python.

## Quick start

```python
import gudhi as gd
from lcq_persistence import C2CWComplex

# Two loops at level 0, filled in by one triangle at level 1 and another at
# level 2: pi_1 goes F_2 -> Z -> 1.
st = gd.SimplexTree()
for edge in ([0, 1], [1, 2], [0, 2], [1, 3], [0, 3]):
    st.insert(edge, filtration = 0)
st.insert([0, 1, 2], filtration = 1)
st.insert([0, 1, 3], filtration = 2)

X = C2CWComplex.from_simplex_tree(st)
barcodes = X.lcs_barcodes(max_layer = 3)
# {1: [(1, (0, 2)), (1, (0, 1))],
#  2: [(2, (0, 1))],
#  3: [(3, (0, 1)), (3, (0, 1))]}
```

`lcs_barcodes` returns `{layer: barcode}`. Each barcode is in GUDHI's format,
`[(layer, (birth, death)), ...]`, so GUDHI's plotting functions take it
directly, e.g. `gd.plot_persistence_barcode(barcodes[2])`. The time of each
stage is kept in `X.timings`.

## Building filtered complexes

Filtration levels are the integers $0, 1, \dots, N$.

- **From a GUDHI simplex tree** whose filtration values are those integers:
  `C2CWComplex.from_simplex_tree(st)`. Only the 2-skeleton is used.
  `examples.data_sets.rips_simplex_tree` builds such a tree from a point
  cloud, as a Rips filtration either by scale or grown from a base point.
- **By hand**: vertices, edges and 2-cells, each with its level. An edge
  `((i, j), level)` goes from vertex `i` to vertex `j`. A 2-cell
  `(((edge, sign), ...), level)` is attached along the word in the edges, with
  sign `1` or `-1`. For example, a torus built in two steps:

  ```python
  X = C2CWComplex(
      cells_0 = [0],                                  # one vertex, level 0
      attaching_maps_1 = [((0, 0), 0), ((0, 0), 0)],  # loops a, b, level 0
      attaching_maps_2 = [(((0, 1), (1, 1), (0, -1), (1, -1)), 1)],  # a b a^-1 b^-1, level 1
  )
  X.lcs_barcodes(max_layer = 3)
  # {1: [(1, (0, inf)), (1, (0, inf))], 2: [(2, (0, 1))], 3: [(3, (0, 1)), (3, (0, 1))]}
  ```

- **Complements of unions of balls** around a point cloud in $\mathbb{R}^3$, as
  the radius shrinks:
  `C2CWComplex(*examples.data_sets.alpha_complement_filtration(points, max_radius, min_radius, n_levels))`.
- **From a filtered group presentation**, with all the generators at level 0
  and each relator entering at its own level:
  `C2CWComplex.from_presentation(num_generators, relators, caps)`.
  `examples/complexes.py` builds link-complement filtrations this way, from
  Wirtinger presentations.

## How it works

1. **Discrete Morse matching.** The matching only pairs cells that enter at
   the same level. The fundamental groups of the levels then have nested
   presentations: each one is the previous one plus some generators and
   relators.
2. **Persistent nilpotent quotients.** Nickel's nilpotent quotient
   algorithm runs on the first level. Its consistent polycyclic
   presentation is then updated level by level: by normal closures of the
   new relators, by Tietze moves, and by free extensions for new free
   generators. The layers of every level and the maps between them come
   from this single sweep, for all layers up to `max_layer` at once. All
   the arithmetic is exact.
3. **Barcodes.** Each layer becomes a graded $\mathbb{Q}[t]$-module
   presentation, whose barcode is computed by a sparse column reduction.

The results agree with GAP's nilpotent quotient program `nq`, run from
scratch at every level, on random presentations and filtrations and on the
example complexes of the tests: see
[validation/](https://github.com/Gonzalo-Ortega/lcq-persistence/tree/main/validation).

The cost grows quickly with the first Betti number $b_1$ of the early
levels. By Witt's formula, layer $k$ has about $b_1^k / k$ generators.
Start the filtration later (for example `min_edge_length` in
`rips_simplex_tree`), or ask for fewer layers, when $b_1$ is large.

### From group presentations

If you have a nested filtration of group presentations rather than a
complex, `persistent_lcs` computes its LCS layers, the maps between them and
their barcodes over $\mathbb{Q}$ or $\mathbb{F}_p$. Its levels count from 1:

```python
from lcq_persistence.algorithms.persistent_nilpotent_quotients import persistent_lcs

# Level 1: the free group on x, y. Level 2: add the relator [x, y].
commutator = [('x', -1), ('y', -1), ('x', 1), ('y', 1)]
result = persistent_lcs([['x', 'y'], []], [[], [commutator]], 3)
result.barcode(2)     # [(1, 2)]: [x, y] dies when Z^2 is reached
result.barcode(2, 2)  # the same over F_2
```

## Examples

```bash
python -m lcq_persistence.examples.cw_examples            # handmade complexes, link complements
python -m lcq_persistence.examples.point_cloud_examples   # lists the point cloud examples
python -m lcq_persistence.examples.point_cloud_examples torus_vs_wedge
```

The point cloud examples include tori versus sphere wedges, grid cell
recordings, molecular Borromean rings and catenanes, and a protein catenane.
The real data sets are downloaded the first time they are used (about 350
MB in total) to `~/.cache/lcq_persistence`. To keep them somewhere else, set
the environment variable `LCQ_PERSISTENCE_DATA` to another directory.

## Tests

```bash
python tests/test_persistent_nilpotent_quotients.py
python tests/test_pipeline.py
```

or `pytest`. The tests are pure Python. They check the algorithm on worked
examples, check random filtrations against independent computations, and
compare the barcodes of the example complexes with stored ones, which agree
with GAP's `nq`. The checks against GAP itself are in
[validation/](https://github.com/Gonzalo-Ortega/lcq-persistence/tree/main/validation),
which needs SageMath.

## Layout

```
src/lcq_persistence/
  c2cw_complex.py       filtered 2-dimensional CW complexes (C2CWComplex)
  lcs_pipelines.py      the LCS persistence pipeline, complex -> barcodes
  algorithms/           Morse matching, nested presentations of pi_1,
                        persistent nilpotent quotients, graded barcodes
  plotting.py           plots of point clouds, filtrations and barcodes
  examples/             example complexes, point clouds and scripts
tests/                  pure Python tests
validation/             checks and timings against GAP's nq (needs SageMath)
```

## Use of AI tools

Parts of the code, tests and documentation in this repository were written with
the help of an AI coding assistant, under the author's direction and review.
The author is responsible for the code and its results,
which are checked by the tests in `tests/` and against GAP's nilpotent
quotient program `nq` in `validation/`.

## Citing

If you use this package in your work, please cite it. GitHub's "Cite this
repository" button gives the reference, from
[CITATION.cff](https://github.com/Gonzalo-Ortega/lcq-persistence/blob/main/CITATION.cff).
The DOI [10.5281/zenodo.24474871](https://doi.org/10.5281/zenodo.24474871)
cites all versions, and each release also has its own DOI on Zenodo:
version 0.1.0 is [10.5281/zenodo.24474872](https://doi.org/10.5281/zenodo.24474872).

## License

GNU General Public License, version 3 or later: see
[LICENSE](https://github.com/Gonzalo-Ortega/lcq-persistence/blob/main/LICENSE).
