@topolab/tda 0.0.0-stage → 0.3.0
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.
- package/CHANGELOG.md +82 -0
- package/CITATION.cff +72 -0
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/SECURITY.md +34 -0
- package/THIRD_PARTY_LICENSES.md +696 -0
- package/dist/build_info.json +12 -0
- package/dist/check.d.ts +118 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +298 -0
- package/dist/check.js.map +1 -0
- package/dist/complex.d.ts +543 -0
- package/dist/complex.d.ts.map +1 -0
- package/dist/complex.js +712 -0
- package/dist/complex.js.map +1 -0
- package/dist/diagram.d.ts +116 -0
- package/dist/diagram.d.ts.map +1 -0
- package/dist/diagram.js +238 -0
- package/dist/diagram.js.map +1 -0
- package/dist/diagram_of.d.ts +17 -0
- package/dist/diagram_of.d.ts.map +1 -0
- package/dist/diagram_of.js +40 -0
- package/dist/diagram_of.js.map +1 -0
- package/dist/experimental.d.ts +14 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +13 -0
- package/dist/experimental.js.map +1 -0
- package/dist/flat.d.ts +64 -0
- package/dist/flat.d.ts.map +1 -0
- package/dist/flat.js +158 -0
- package/dist/flat.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/ladder/tda_ladder.d.ts +223 -0
- package/dist/ladder/tda_ladder.js +1360 -0
- package/dist/ladder/tda_ladder_bg.wasm +0 -0
- package/dist/ladder/tda_ladder_bg.wasm.d.ts +43 -0
- package/dist/ladder.d.ts +306 -0
- package/dist/ladder.d.ts.map +1 -0
- package/dist/ladder.js +607 -0
- package/dist/ladder.js.map +1 -0
- package/dist/load.d.ts +53 -0
- package/dist/load.d.ts.map +1 -0
- package/dist/load.js +173 -0
- package/dist/load.js.map +1 -0
- package/dist/memory.d.ts +58 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +2 -0
- package/dist/memory.js.map +1 -0
- package/dist/persistence.d.ts +349 -0
- package/dist/persistence.d.ts.map +1 -0
- package/dist/persistence.js +478 -0
- package/dist/persistence.js.map +1 -0
- package/dist/pkg/tda_wasm.d.ts +13 -0
- package/dist/pkg/tda_wasm.js +1900 -0
- package/dist/prepared.d.ts +62 -0
- package/dist/prepared.d.ts.map +1 -0
- package/dist/prepared.js +81 -0
- package/dist/prepared.js.map +1 -0
- package/dist/source.d.ts +43 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +99 -0
- package/dist/source.js.map +1 -0
- package/dist/tda.wasm +0 -0
- package/dist/types.d.ts +146 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vineyard.d.ts +278 -0
- package/dist/vineyard.d.ts.map +1 -0
- package/dist/vineyard.js +236 -0
- package/dist/vineyard.js.map +1 -0
- package/package.json +77 -4
- package/src/check.ts +338 -0
- package/src/complex.ts +1179 -0
- package/src/diagram.ts +299 -0
- package/src/diagram_of.ts +40 -0
- package/src/experimental.ts +13 -0
- package/src/flat.ts +204 -0
- package/src/index.ts +155 -0
- package/src/ladder.ts +903 -0
- package/src/load.ts +201 -0
- package/src/memory.ts +58 -0
- package/src/persistence.ts +752 -0
- package/src/prepared.ts +111 -0
- package/src/source.ts +132 -0
- package/src/types.ts +163 -0
- package/src/vineyard.ts +463 -0
package/src/vineyard.ts
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistence vineyards: the classes of a filtered complex followed through
|
|
3
|
+
* a homotopy of its values (internal module; the package root re-exports
|
|
4
|
+
* the API).
|
|
5
|
+
*/
|
|
6
|
+
import { call, module } from './load.js';
|
|
7
|
+
import { coeff as checkCoeff, f64, int, kind, minPersistence as checkMinPersistence, nonNegative, options, points } from './check.js';
|
|
8
|
+
import type { Keys, PointCloud, Vec } from './check.js';
|
|
9
|
+
import { gridArgs } from './complex.js';
|
|
10
|
+
import { complexArgs } from './persistence.js';
|
|
11
|
+
import type { Complex, ComplexLike, Grid, Scale } from './types.js';
|
|
12
|
+
|
|
13
|
+
/** Which representative cycles a vineyard function returns. */
|
|
14
|
+
export interface VineyardRepresentativesOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Times at which to extract representative cycles, each a finite number in
|
|
17
|
+
* `[0, number of frames − 1]`. Representatives are returned in this order.
|
|
18
|
+
* Every transposition with crossing time `<= t` is applied before the
|
|
19
|
+
* snapshot at `t` is taken (in particular, at `t = 0` on a multi-frame
|
|
20
|
+
* input, tied cells whose order reverses in frame 1 transpose before the
|
|
21
|
+
* `t = 0` snapshot, whereas a 1-frame input has no transpositions).
|
|
22
|
+
*/
|
|
23
|
+
times: Vec;
|
|
24
|
+
/**
|
|
25
|
+
* Homology dimensions to return representatives for. Default: all
|
|
26
|
+
* dimensions present in the vineyard; `[]` returns none.
|
|
27
|
+
*/
|
|
28
|
+
dims?: number[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const VINEYARD_REPRESENTATIVES_KEYS: Keys<VineyardRepresentativesOptions> = { times: true, dims: true };
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Representative cycles of a vineyard at requested times, as typed arrays.
|
|
35
|
+
* Representative `r` stands for vine `vines[r]` at time `times[r]` (with
|
|
36
|
+
* homology dimension `dims[r]`, birth `births[r]` and death `deaths[r]` at
|
|
37
|
+
* that time); its cycle is the linear combination of
|
|
38
|
+
* `cells[offsets[r] .. offsets[r + 1]]` with coefficients
|
|
39
|
+
* `coefficients[offsets[r] .. offsets[r + 1]]` in `(−coeff/2, coeff/2]`
|
|
40
|
+
* (all 1 over `Z/2`).
|
|
41
|
+
*
|
|
42
|
+
* Ordered by requested time, then by vine index.
|
|
43
|
+
*/
|
|
44
|
+
export interface VineyardRepresentatives {
|
|
45
|
+
/** Requested time of each representative. */
|
|
46
|
+
times: Float64Array;
|
|
47
|
+
/** Vine index (`0 .. dims.length − 1` of the enclosing vineyard) of each representative. */
|
|
48
|
+
vines: Uint32Array;
|
|
49
|
+
/** Homology dimension of each representative. */
|
|
50
|
+
dims: Uint8Array;
|
|
51
|
+
/** Birth of the vine at `times[r]`. */
|
|
52
|
+
births: Float64Array;
|
|
53
|
+
/** Death of the vine at `times[r]`, `Infinity` for an essential class. */
|
|
54
|
+
deaths: Float64Array;
|
|
55
|
+
/** Length `count + 1`: representative `r` owns `offsets[r] .. offsets[r + 1]` of `cells` and `coefficients`. */
|
|
56
|
+
offsets: Uint32Array;
|
|
57
|
+
/**
|
|
58
|
+
* Cell indices of each cycle: simplex indices into the input complex for
|
|
59
|
+
* {@link vineyard}, bitmap positions for {@link cubicalVineyard}, simplex
|
|
60
|
+
* indices into the returned `complex` for {@link ripsVineyard}.
|
|
61
|
+
*/
|
|
62
|
+
cells: Uint32Array;
|
|
63
|
+
/** Coefficient of each cell in `(−coeff/2, coeff/2]`; all 1 over `Z/2`. */
|
|
64
|
+
coefficients: Int32Array;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const EMPTY_F64 = new Float64Array(0);
|
|
68
|
+
const EMPTY_U32 = new Uint32Array(0);
|
|
69
|
+
|
|
70
|
+
/** Checks a vineyard `representatives` option against `[0, maxTime]` and `0 .. maxDim`. */
|
|
71
|
+
function repRequest(
|
|
72
|
+
v: unknown,
|
|
73
|
+
fn: string,
|
|
74
|
+
maxTime: number,
|
|
75
|
+
maxDim: number
|
|
76
|
+
): { withReps: boolean; times: Float64Array; filterDims: boolean; dims: Uint32Array } {
|
|
77
|
+
if (v === undefined) return { withReps: false, times: EMPTY_F64, filterDims: false, dims: EMPTY_U32 };
|
|
78
|
+
const o = options<VineyardRepresentativesOptions>(v, fn, VINEYARD_REPRESENTATIVES_KEYS, 'representatives', 'representatives.');
|
|
79
|
+
const times = f64(o.times, 'representatives.times');
|
|
80
|
+
for (let i = 0; i < times.length; i++) {
|
|
81
|
+
const t = times[i]!;
|
|
82
|
+
if (!Number.isFinite(t) || t < 0 || t > maxTime) {
|
|
83
|
+
throw new RangeError(`representatives.times[${i}] must be between 0 and ${maxTime}, got ${t}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (o.dims === undefined) return { withReps: true, times, filterDims: false, dims: EMPTY_U32 };
|
|
87
|
+
if (!Array.isArray(o.dims)) throw new TypeError(`representatives.dims must be an array of integers, got ${kind(o.dims)}`);
|
|
88
|
+
const dims = new Uint32Array(o.dims.length);
|
|
89
|
+
for (let i = 0; i < o.dims.length; i++) dims[i] = int(o.dims[i], `representatives.dims[${i}]`, 0, maxDim);
|
|
90
|
+
return { withReps: true, times, filterDims: true, dims };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Options of {@link vineyard} and {@link cubicalVineyard}. */
|
|
94
|
+
export interface VineyardOptions {
|
|
95
|
+
/** The prime `p` of the coefficient field `Z/p`, from 2 to `2^31 − 1`. Default 2. */
|
|
96
|
+
coeff?: number;
|
|
97
|
+
/**
|
|
98
|
+
* Leave out the vines whose `death − birth` is at most `minPersistence` at
|
|
99
|
+
* every knot; essential vines are always kept. A finite number `>= 0`.
|
|
100
|
+
* Default 0: only the vines that never leave the diagonal are left out.
|
|
101
|
+
*/
|
|
102
|
+
minPersistence?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Also return one representative cycle per kept vine persisting at each
|
|
105
|
+
* requested time (`death > birth` at that time, or essential) in the
|
|
106
|
+
* requested dimensions. Every transposition at crossing time `<= t` is
|
|
107
|
+
* applied before the snapshot at `t` is taken.
|
|
108
|
+
*/
|
|
109
|
+
representatives?: VineyardRepresentativesOptions;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const VINEYARD_KEYS: Keys<VineyardOptions> = { coeff: true, minPersistence: true, representatives: true };
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The vines of a homotopy, as typed arrays. Vine `v` is one class followed
|
|
116
|
+
* through time: its knots are the entries `offsets[v] .. offsets[v + 1]` of
|
|
117
|
+
* `times`, `births`, `deaths`, `birthSimplices` and `deathSimplices`, in
|
|
118
|
+
* time order: its state at time 0, at every switch of the pairing that
|
|
119
|
+
* involves it, and at every frame. Between two consecutive knots its birth
|
|
120
|
+
* and death are linear in time, so the knots are the whole vine. The times
|
|
121
|
+
* of a vine's knots are non-decreasing, not strictly increasing: two knots
|
|
122
|
+
* share a time when a switch happens at time 0 or at a frame (where values
|
|
123
|
+
* are tied), and when two crossings are equal within rounding.
|
|
124
|
+
*
|
|
125
|
+
* The vines come by dimension, and within a dimension in the order of
|
|
126
|
+
* `persistence(complex)` at time 0 (the classes with a death by their death
|
|
127
|
+
* simplex, then the essential classes by their birth simplex).
|
|
128
|
+
*/
|
|
129
|
+
export interface Vineyard {
|
|
130
|
+
/** The homology dimension of each vine (constant along it). */
|
|
131
|
+
dims: Uint8Array;
|
|
132
|
+
/** Length `count + 1`; vine `v` owns the knots `offsets[v] .. offsets[v + 1]`. */
|
|
133
|
+
offsets: Uint32Array;
|
|
134
|
+
/** The time of each knot: frame `m` is at time `m`. Non-decreasing along a vine; two knots may share a time. */
|
|
135
|
+
times: Float64Array;
|
|
136
|
+
/** The birth at each knot. */
|
|
137
|
+
births: Float64Array;
|
|
138
|
+
/** The death at each knot, `Infinity` for an essential class. */
|
|
139
|
+
deaths: Float64Array;
|
|
140
|
+
/** The simplex (an index into the input complex) that creates the class from each knot to the next. */
|
|
141
|
+
birthSimplices: Uint32Array;
|
|
142
|
+
/** The simplex that destroys it from each knot to the next, `-1` for an essential class. */
|
|
143
|
+
deathSimplices: Int32Array;
|
|
144
|
+
/** The number of transpositions of adjacent simplices the homotopy made. */
|
|
145
|
+
transpositions: number;
|
|
146
|
+
/** How many of them switched the pairing (each adds a knot to two vines). */
|
|
147
|
+
switches: number;
|
|
148
|
+
/** The quantity the values measure (the complex's `scale`, `'value'` when it has none). */
|
|
149
|
+
scale: Scale;
|
|
150
|
+
/** The prime `p` of the coefficient field `Z/p`. */
|
|
151
|
+
coeff: number;
|
|
152
|
+
/** Present only when `representatives` was requested. */
|
|
153
|
+
representatives?: VineyardRepresentatives;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The persistence vineyard of a filtered simplicial complex whose values
|
|
158
|
+
* change: `complex.filtrations` are the values at time 0, and `frames[m]`
|
|
159
|
+
* (one value per simplex, in the complex's order) those at time `m + 1`.
|
|
160
|
+
* Between two frames every value moves on a straight line, and the result
|
|
161
|
+
* follows every class of the persistence diagram through that homotopy over
|
|
162
|
+
* `Z/coeff`.
|
|
163
|
+
*
|
|
164
|
+
* The algorithm is the linear-time transposition update ([reference](https://topolab.ai/tda/docs/references/#cohen-steiner-edelsbrunner-and-morozov-2006)): the
|
|
165
|
+
* matrix decomposition behind `persistence` is computed once, at time 0,
|
|
166
|
+
* and repaired in time linear in the number of simplices each time two
|
|
167
|
+
* simplices adjacent in the filtration order exchange their places, instead
|
|
168
|
+
* of being recomputed.
|
|
169
|
+
*
|
|
170
|
+
* The complex must be closed, and monotone at time 0 and at every frame (no
|
|
171
|
+
* face valued above a coface); every value must be finite. Anything else is
|
|
172
|
+
* a `RangeError` naming the simplex. Simplices of equal value keep their
|
|
173
|
+
* order, which at time 0 is the documented order of {@link Complex}: at a
|
|
174
|
+
* frame the vines' points with `death > birth` are the pairs of
|
|
175
|
+
* `persistence` of the complex with that frame's values (over the same
|
|
176
|
+
* `coeff`), and at any time in between those of the interpolated values.
|
|
177
|
+
*
|
|
178
|
+
* Memory bounds a vineyard before its size limits do: the decomposition
|
|
179
|
+
* keeps U, every column addition of the reduction at time 0 (on tied or
|
|
180
|
+
* evenly spread values many times the number of simplices), and
|
|
181
|
+
* transpositions fill it in further. A vineyard that does not fit throws
|
|
182
|
+
* `TOO_LARGE` (phase `'boundary matrix'`). The same holds for
|
|
183
|
+
* {@link cubicalVineyard} and {@link ripsVineyard}.
|
|
184
|
+
*/
|
|
185
|
+
export function vineyard(complex: ComplexLike, frames: ArrayLike<Vec>, opts?: VineyardOptions): Vineyard {
|
|
186
|
+
module();
|
|
187
|
+
const o = options<VineyardOptions>(opts, 'vineyard', VINEYARD_KEYS);
|
|
188
|
+
const c = complexArgs(complex, 'complex');
|
|
189
|
+
const coeff = checkCoeff(o.coeff);
|
|
190
|
+
const minPersistence = checkMinPersistence(o.minPersistence);
|
|
191
|
+
if (!isList(frames)) {
|
|
192
|
+
throw new TypeError(`frames must be an array of value arrays (Float64Array or arrays of numbers), got ${kind(frames)}`);
|
|
193
|
+
}
|
|
194
|
+
const n = c.filtrations.length;
|
|
195
|
+
const count = frames.length + 1;
|
|
196
|
+
const rep = repRequest(o.representatives, 'vineyard', count - 1, 255);
|
|
197
|
+
const all = new Float64Array(n * count);
|
|
198
|
+
all.set(c.filtrations);
|
|
199
|
+
for (let m = 0; m < frames.length; m++) {
|
|
200
|
+
const frame = f64(frames[m], `frames[${m}]`);
|
|
201
|
+
if (frame.length !== n) {
|
|
202
|
+
throw new RangeError(`frames[${m}] must have one value per simplex (${n}), got ${frame.length}`);
|
|
203
|
+
}
|
|
204
|
+
all.set(frame, n * (m + 1));
|
|
205
|
+
}
|
|
206
|
+
return call('vineyard', (m) =>
|
|
207
|
+
vinesOf(m.vineyardFlat(c.vertices, c.offsets, all, count, minPersistence, coeff, rep.times, rep.withReps, rep.dims, rep.filterDims), c.scale, coeff)
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** What the module's vineyard functions return. */
|
|
212
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
|
+
type Raw = any;
|
|
214
|
+
|
|
215
|
+
/** The vines of a module result. */
|
|
216
|
+
function vinesOf(raw: Raw, scale: Scale, coeff: number): Vineyard {
|
|
217
|
+
const out: Vineyard = {
|
|
218
|
+
dims: raw.dims,
|
|
219
|
+
offsets: raw.offsets,
|
|
220
|
+
times: raw.times,
|
|
221
|
+
births: raw.births,
|
|
222
|
+
deaths: raw.deaths,
|
|
223
|
+
birthSimplices: raw.birthSimplices,
|
|
224
|
+
deathSimplices: raw.deathSimplices,
|
|
225
|
+
transpositions: raw.transpositions,
|
|
226
|
+
switches: raw.switches,
|
|
227
|
+
scale,
|
|
228
|
+
coeff
|
|
229
|
+
};
|
|
230
|
+
if (raw.representatives !== undefined) out.representatives = raw.representatives;
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** An array or array-like object (not a typed array) whose length is a non-negative integer. */
|
|
235
|
+
function isList(v: unknown): v is ArrayLike<unknown> {
|
|
236
|
+
if (v === null || typeof v !== 'object' || ArrayBuffer.isView(v)) return false;
|
|
237
|
+
const length = (v as ArrayLike<unknown>).length;
|
|
238
|
+
return Number.isSafeInteger(length) && length >= 0;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** An array of inputs (not a typed array), at least one; returns its length. */
|
|
242
|
+
function frameList(v: unknown, name: string, plural: string, one: string): number {
|
|
243
|
+
if (!isList(v)) {
|
|
244
|
+
throw new TypeError(`${name} must be an array of ${plural}, got ${kind(v)}`);
|
|
245
|
+
}
|
|
246
|
+
const count = v.length;
|
|
247
|
+
if (count === 0) throw new RangeError(`${name} must hold at least one ${one}, got none`);
|
|
248
|
+
return count;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
// Images and volumes
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Persistence vineyard of a sequence of images or volumes
|
|
257
|
+
* ({@link cubicalVineyard}), stored as typed arrays. Has the fields of
|
|
258
|
+
* {@link Vineyard}, with cubical complex cells in place of simplices.
|
|
259
|
+
*/
|
|
260
|
+
export interface CubicalVineyard {
|
|
261
|
+
/** Homology dimension of each vine (constant along the vine). */
|
|
262
|
+
dims: Uint8Array;
|
|
263
|
+
/**
|
|
264
|
+
* Offsets into the knot arrays, of length `count + 1`; vine `v` owns knots
|
|
265
|
+
* `offsets[v] .. offsets[v + 1]`.
|
|
266
|
+
*/
|
|
267
|
+
offsets: Uint32Array;
|
|
268
|
+
/** Time coordinate of each knot; image `m` is at time `m`. Non-decreasing along a vine; two knots may share a time (see {@link Vineyard}). */
|
|
269
|
+
times: Float64Array;
|
|
270
|
+
/** The `birth` coordinate at each knot. */
|
|
271
|
+
births: Float64Array;
|
|
272
|
+
/**
|
|
273
|
+
* The `death` coordinate at each knot, or `Infinity` for an essential
|
|
274
|
+
* class.
|
|
275
|
+
*/
|
|
276
|
+
deaths: Float64Array;
|
|
277
|
+
/**
|
|
278
|
+
* Cell that creates the class from each knot to the next, given as a
|
|
279
|
+
* bitmap position (numbered as in {@link CubicalComplex}`.cells`:
|
|
280
|
+
* `2 · shape[i] + 1` positions along axis `i`, row-major, so the pixel at
|
|
281
|
+
* `(p0, p1, …)` is the cell at `(2 p0 + 1, 2 p1 + 1, …)`).
|
|
282
|
+
*/
|
|
283
|
+
birthCells: Uint32Array;
|
|
284
|
+
/**
|
|
285
|
+
* Cell that destroys the class from each knot to the next, given as a
|
|
286
|
+
* bitmap position, or `-1` for an essential class.
|
|
287
|
+
*/
|
|
288
|
+
deathCells: Int32Array;
|
|
289
|
+
/** Number of adjacent-cell transpositions performed along the homotopy. */
|
|
290
|
+
transpositions: number;
|
|
291
|
+
/**
|
|
292
|
+
* Number of transpositions that switched the persistence pairing (each
|
|
293
|
+
* switch adds a knot to two vines).
|
|
294
|
+
*/
|
|
295
|
+
switches: number;
|
|
296
|
+
/** Common shape of the input images, slowest axis first. */
|
|
297
|
+
shape: number[];
|
|
298
|
+
/** Filtration scale; always `'value'` (the images' own values). */
|
|
299
|
+
scale: 'value';
|
|
300
|
+
/** The prime `p` of the coefficient field `Z/p`. */
|
|
301
|
+
coeff: number;
|
|
302
|
+
/** Present only when `representatives` was requested. */
|
|
303
|
+
representatives?: VineyardRepresentatives;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Compute the persistence vineyard of a sequence of images or volumes of the
|
|
308
|
+
* same shape, following the classes of the sublevel-set persistence diagram
|
|
309
|
+
* (T-construction, as in {@link cubicalPersistence}) across frames over
|
|
310
|
+
* `Z/coeff`.
|
|
311
|
+
*
|
|
312
|
+
* Each image assigns values to the cells of one shared cubical complex, the
|
|
313
|
+
* T-construction complex of the grid: each top-dimensional cell (pixel or
|
|
314
|
+
* voxel) takes its own value, and each lower-dimensional cell takes the
|
|
315
|
+
* minimum value among the top-dimensional cells containing it. Between
|
|
316
|
+
* consecutive images, each cell's value moves linearly, and the vines follow
|
|
317
|
+
* that homotopy using the transposition updates of {@link vineyard}. At
|
|
318
|
+
* image `m`, the vine points with `death > birth` are the pairs of
|
|
319
|
+
* `cubicalPersistence(images[m], { coeff })`. Cell values are interpolated
|
|
320
|
+
* between images rather than pixel values: a lower-dimensional cell takes the
|
|
321
|
+
* linear interpolation of its two cell values, which can lie below the
|
|
322
|
+
* T-construction value of the interpolated image.
|
|
323
|
+
*
|
|
324
|
+
* Every image must be a {@link Grid} with the same shape as `images[0]` and
|
|
325
|
+
* finite values.
|
|
326
|
+
*/
|
|
327
|
+
export function cubicalVineyard(images: ArrayLike<Grid>, opts?: VineyardOptions): CubicalVineyard {
|
|
328
|
+
module();
|
|
329
|
+
const o = options<VineyardOptions>(opts, 'cubicalVineyard', VINEYARD_KEYS);
|
|
330
|
+
const coeff = checkCoeff(o.coeff);
|
|
331
|
+
const minPersistence = checkMinPersistence(o.minPersistence);
|
|
332
|
+
const count = frameList(images, 'images', 'images { values: Float64Array, shape: number[] }', 'image');
|
|
333
|
+
const rep = repRequest(o.representatives, 'cubicalVineyard', count - 1, 255);
|
|
334
|
+
const extents = gridArgs(images[0], 'images[0]');
|
|
335
|
+
const size = images[0]!.values.length;
|
|
336
|
+
const shape = [...images[0]!.shape];
|
|
337
|
+
const all = new Float64Array(size * count);
|
|
338
|
+
for (let m = 0; m < count; m++) {
|
|
339
|
+
const image = images[m];
|
|
340
|
+
if (m > 0) {
|
|
341
|
+
gridArgs(image, `images[${m}]`);
|
|
342
|
+
const other = image!.shape;
|
|
343
|
+
if (other.length !== shape.length || other.some((x, i) => x !== shape[i])) {
|
|
344
|
+
throw new RangeError(`images[${m}].shape must be the shape of images[0] ([${shape.join(', ')}]), got [${other.join(', ')}]`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
all.set(image!.values, size * m);
|
|
348
|
+
}
|
|
349
|
+
return call('cubicalVineyard', (m) => {
|
|
350
|
+
const raw = m.cubicalVineyardFlat(extents, all, count, minPersistence, coeff, rep.times, rep.withReps, rep.dims, rep.filterDims);
|
|
351
|
+
const { birthSimplices, deathSimplices, ...rest } = vinesOf(raw, 'value', coeff);
|
|
352
|
+
return { ...rest, birthCells: birthSimplices, deathCells: deathSimplices, shape, scale: 'value' as const };
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ---------------------------------------------------------------------------
|
|
357
|
+
// Moving point clouds
|
|
358
|
+
// ---------------------------------------------------------------------------
|
|
359
|
+
|
|
360
|
+
/** Options for {@link ripsVineyard}. */
|
|
361
|
+
export interface RipsVineyardOptions {
|
|
362
|
+
/** Maximum homology dimension, an integer from 0 to 9. Default 1. */
|
|
363
|
+
maxDim?: number;
|
|
364
|
+
/**
|
|
365
|
+
* Non-negative edge-length threshold. Keep only simplices whose edges each
|
|
366
|
+
* have length at most `maxEdgeLength` in at least one cloud (the cloud may
|
|
367
|
+
* differ from edge to edge). Default `Infinity`: include every simplex up
|
|
368
|
+
* to dimension `maxDim + 1`, so every vine is exact.
|
|
369
|
+
*/
|
|
370
|
+
maxEdgeLength?: number;
|
|
371
|
+
/** The prime `p` of the coefficient field `Z/p`, from 2 to `2^31 − 1`. Default 2. */
|
|
372
|
+
coeff?: number;
|
|
373
|
+
/**
|
|
374
|
+
* Minimum persistence threshold, a finite number `>= 0`. Omit vines whose
|
|
375
|
+
* `death − birth` is at most `minPersistence` at every knot; essential
|
|
376
|
+
* vines are always kept. Default 0: omit only vines that never leave the
|
|
377
|
+
* diagonal.
|
|
378
|
+
*/
|
|
379
|
+
minPersistence?: number;
|
|
380
|
+
/**
|
|
381
|
+
* Also return one representative cycle per kept vine persisting at each
|
|
382
|
+
* requested time (`death > birth` at that time, or essential) in the
|
|
383
|
+
* requested dimensions (up to `maxDim`). Every transposition at crossing
|
|
384
|
+
* time `<= t` is applied before the snapshot at `t` is taken.
|
|
385
|
+
*/
|
|
386
|
+
representatives?: VineyardRepresentativesOptions;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const RIPS_VINEYARD_KEYS: Keys<RipsVineyardOptions> = {
|
|
390
|
+
maxDim: true,
|
|
391
|
+
maxEdgeLength: true,
|
|
392
|
+
coeff: true,
|
|
393
|
+
minPersistence: true,
|
|
394
|
+
representatives: true
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/** Result of {@link ripsVineyard}: the vineyard and its shared complex. */
|
|
398
|
+
export interface RipsVineyard extends Vineyard {
|
|
399
|
+
/**
|
|
400
|
+
* Shared complex across all clouds, carrying its values from `clouds[0]`
|
|
401
|
+
* in the documented order of a {@link Complex}. Indexed by `birthSimplices`
|
|
402
|
+
* and `deathSimplices`; its vertices are point indices.
|
|
403
|
+
*/
|
|
404
|
+
complex: Complex;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Compute the persistence vineyard of a moving point cloud, following the
|
|
409
|
+
* classes of the Vietoris–Rips persistence diagram across frames over
|
|
410
|
+
* `Z/coeff`. `clouds[m]` holds the same points in the same order at time `m`.
|
|
411
|
+
*
|
|
412
|
+
* One shared complex serves all clouds: every simplex up to dimension
|
|
413
|
+
* `maxDim + 1` whose edges each have length at most `maxEdgeLength` in at
|
|
414
|
+
* least one cloud (all such simplices by default). Cloud `m` assigns each
|
|
415
|
+
* simplex the length of its longest edge there, as {@link ripsComplex} does,
|
|
416
|
+
* and between consecutive clouds each simplex's value moves linearly; the
|
|
417
|
+
* vines follow that homotopy using the transposition updates of
|
|
418
|
+
* {@link vineyard}. Simplex values are interpolated rather than point
|
|
419
|
+
* coordinates: between two clouds, a simplex takes the linear interpolation
|
|
420
|
+
* of its two lengths, not its length in the interpolated cloud.
|
|
421
|
+
*
|
|
422
|
+
* At cloud `m`, the vine points with `death > birth` are the pairs of
|
|
423
|
+
* `ripsPersistence(clouds[m], { maxDim, coeff })`. With a finite
|
|
424
|
+
* `maxEdgeLength`, every vine is exact while `death` is below
|
|
425
|
+
* `maxEdgeLength`: at each cloud and at every time between consecutive
|
|
426
|
+
* clouds, the complex contains every simplex of value at most
|
|
427
|
+
* `maxEdgeLength` at its exact value. A death at or above `maxEdgeLength`
|
|
428
|
+
* indicates only that the class persists until at least `maxEdgeLength`.
|
|
429
|
+
*
|
|
430
|
+
* Every cloud must have the same number of points and `d` as `clouds[0]`,
|
|
431
|
+
* with finite coordinates.
|
|
432
|
+
*/
|
|
433
|
+
export function ripsVineyard(clouds: ArrayLike<PointCloud>, opts?: RipsVineyardOptions): RipsVineyard {
|
|
434
|
+
module();
|
|
435
|
+
const o = options<RipsVineyardOptions>(opts, 'ripsVineyard', RIPS_VINEYARD_KEYS);
|
|
436
|
+
const maxDim = o.maxDim === undefined ? 1 : int(o.maxDim, 'maxDim', 0, 9);
|
|
437
|
+
const maxEdgeLength = o.maxEdgeLength === undefined ? Infinity : nonNegative(o.maxEdgeLength, 'maxEdgeLength');
|
|
438
|
+
const coeff = checkCoeff(o.coeff);
|
|
439
|
+
const minPersistence = checkMinPersistence(o.minPersistence);
|
|
440
|
+
const count = frameList(clouds, 'clouds', 'point clouds { coords: Float64Array, d: number }', 'point cloud');
|
|
441
|
+
const rep = repRequest(o.representatives, 'ripsVineyard', count - 1, maxDim);
|
|
442
|
+
const n = points(clouds[0], 'clouds[0]', { squaredDistances: true });
|
|
443
|
+
const d = clouds[0]!.d;
|
|
444
|
+
const size = clouds[0]!.coords.length;
|
|
445
|
+
const all = new Float64Array(size * count);
|
|
446
|
+
for (let m = 0; m < count; m++) {
|
|
447
|
+
const c = clouds[m];
|
|
448
|
+
if (m > 0) {
|
|
449
|
+
const k = points(c, `clouds[${m}]`, { squaredDistances: true });
|
|
450
|
+
if (c!.d !== d || k !== n) {
|
|
451
|
+
throw new RangeError(`clouds[${m}] must have the ${n} points of ${d} coordinates of clouds[0], got ${k} points of ${c!.d}`);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
all.set(c!.coords, size * m);
|
|
455
|
+
}
|
|
456
|
+
return call('ripsVineyard', (m) => {
|
|
457
|
+
const raw = m.ripsVineyardFlat(all, d, count, maxEdgeLength, maxDim, minPersistence, coeff, rep.times, rep.withReps, rep.dims, rep.filterDims);
|
|
458
|
+
return {
|
|
459
|
+
...vinesOf(raw, 'edgeLength', coeff),
|
|
460
|
+
complex: { vertices: raw.vertices, offsets: raw.complexOffsets, filtrations: raw.filtrations, scale: 'edgeLength' as const }
|
|
461
|
+
};
|
|
462
|
+
});
|
|
463
|
+
}
|