lagrangian-gradient-regression 0.1.0__tar.gz
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.
- lagrangian_gradient_regression-0.1.0/.gitignore +19 -0
- lagrangian_gradient_regression-0.1.0/CITATION.cff +26 -0
- lagrangian_gradient_regression-0.1.0/LICENSE +21 -0
- lagrangian_gradient_regression-0.1.0/PKG-INFO +197 -0
- lagrangian_gradient_regression-0.1.0/README.md +155 -0
- lagrangian_gradient_regression-0.1.0/pyproject.toml +97 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/__init__.py +213 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/array_api.py +866 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/backends/__init__.py +2 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/backends/jax_lgr.py +245 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/core/__init__.py +13 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/core/dataframe.py +174 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/core/particle.py +214 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/core/pipeline.py +528 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/data/__init__.py +28 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/data/base.py +45 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/data/erddap.py +243 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/data/glad.py +209 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/data/gridded.py +665 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/data/ingestion.py +180 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experimental/__init__.py +43 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experimental/bootstrap.py +262 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experimental/regression.py +2651 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/__init__.py +43 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/artifacts.py +165 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/datasets.py +1053 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/double_gyre_benchmark.py +2657 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/evaluate.py +829 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/figures.py +739 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/flow_kernels.py +51 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/nonlinear_benchmark.py +2058 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/experiments/notebooks.py +39 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/__init__.py +19 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/abc_flow.py +135 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/base.py +242 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/bickley_jet.py +192 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/gyre.py +117 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/integration.py +2155 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/jax_backend.py +209 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/ocean.py +193 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/flows/registry.py +58 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/interpolation/__init__.py +11 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/interpolation/base.py +70 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/interpolation/griddata_interp.py +61 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/interpolation/rbf.py +106 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/__init__.py +21 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/base.py +107 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/cauchy_green.py +91 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/divergence.py +158 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/dra.py +85 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/ftle.py +91 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/lavd.py +92 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/uncertainty.py +274 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/metrics/vorticity.py +165 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/plotting/__init__.py +11 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/plotting/colormaps.py +154 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/plotting/fields.py +125 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/plotting/paper.py +344 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/plotting/trajectories.py +55 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/preprocessing/__init__.py +48 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/preprocessing/jacobians.py +266 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/preprocessing/trajectories.py +273 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/py.typed +0 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/__init__.py +74 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/base.py +116 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/batched.py +1173 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/epanechnikov.py +81 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/gaussian.py +76 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/gpr.py +160 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/jets.py +275 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/lowess.py +86 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/pseudoinverse.py +54 -0
- lagrangian_gradient_regression-0.1.0/src/lgr/regression/weights.py +364 -0
- lagrangian_gradient_regression-0.1.0/tests/__init__.py +0 -0
- lagrangian_gradient_regression-0.1.0/tests/conftest.py +109 -0
- lagrangian_gradient_regression-0.1.0/tests/test_array_api.py +87 -0
- lagrangian_gradient_regression-0.1.0/tests/test_array_api_phase2.py +138 -0
- lagrangian_gradient_regression-0.1.0/tests/test_compose_periodic.py +65 -0
- lagrangian_gradient_regression-0.1.0/tests/test_dataframe.py +52 -0
- lagrangian_gradient_regression-0.1.0/tests/test_experiment_framework.py +1085 -0
- lagrangian_gradient_regression-0.1.0/tests/test_experiment_metric_references.py +98 -0
- lagrangian_gradient_regression-0.1.0/tests/test_experimental_regression.py +376 -0
- lagrangian_gradient_regression-0.1.0/tests/test_extensions.py +571 -0
- lagrangian_gradient_regression-0.1.0/tests/test_flows.py +197 -0
- lagrangian_gradient_regression-0.1.0/tests/test_glad_drifters.py +139 -0
- lagrangian_gradient_regression-0.1.0/tests/test_gp_prior_means.py +177 -0
- lagrangian_gradient_regression-0.1.0/tests/test_interpolation.py +60 -0
- lagrangian_gradient_regression-0.1.0/tests/test_jet_composition.py +724 -0
- lagrangian_gradient_regression-0.1.0/tests/test_lavd_from_trajectories.py +581 -0
- lagrangian_gradient_regression-0.1.0/tests/test_lavd_vortices.py +789 -0
- lagrangian_gradient_regression-0.1.0/tests/test_math_code_alignment.py +160 -0
- lagrangian_gradient_regression-0.1.0/tests/test_metrics.py +100 -0
- lagrangian_gradient_regression-0.1.0/tests/test_particle.py +62 -0
- lagrangian_gradient_regression-0.1.0/tests/test_pipeline_e2e.py +555 -0
- lagrangian_gradient_regression-0.1.0/tests/test_preprocessing.py +224 -0
- lagrangian_gradient_regression-0.1.0/tests/test_psd_weight_matrices.py +238 -0
- lagrangian_gradient_regression-0.1.0/tests/test_real_ocean_sources.py +122 -0
- lagrangian_gradient_regression-0.1.0/tests/test_regression.py +69 -0
- lagrangian_gradient_regression-0.1.0/tests/test_review_fixes_phase1.py +370 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
dist/
|
|
4
|
+
build/
|
|
5
|
+
*.egg-info/
|
|
6
|
+
.venv/
|
|
7
|
+
venv/
|
|
8
|
+
.vscode/
|
|
9
|
+
.idea/
|
|
10
|
+
.ipynb_checkpoints/
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.ruff_cache/
|
|
13
|
+
htmlcov/
|
|
14
|
+
.coverage
|
|
15
|
+
.DS_Store
|
|
16
|
+
Thumbs.db
|
|
17
|
+
|
|
18
|
+
# Downloads and experiment outputs (regenerable)
|
|
19
|
+
artifacts/
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use this software, please cite the paper below."
|
|
3
|
+
title: "Lagrangian Gradient Regression (LGR)"
|
|
4
|
+
type: software
|
|
5
|
+
authors:
|
|
6
|
+
- family-names: Harms
|
|
7
|
+
given-names: Tanner D.
|
|
8
|
+
repository-code: "https://github.com/TannerHarms/Lagrangian-Gradient-Regression"
|
|
9
|
+
license: MIT
|
|
10
|
+
version: 0.1.0
|
|
11
|
+
preferred-citation:
|
|
12
|
+
type: article
|
|
13
|
+
title: "Lagrangian gradient regression for the detection of coherent structures from sparse trajectory data"
|
|
14
|
+
authors:
|
|
15
|
+
- family-names: Harms
|
|
16
|
+
given-names: Tanner D.
|
|
17
|
+
- family-names: Brunton
|
|
18
|
+
given-names: Steven L.
|
|
19
|
+
- family-names: McKeon
|
|
20
|
+
given-names: Beverley J.
|
|
21
|
+
journal: "Royal Society Open Science"
|
|
22
|
+
volume: 11
|
|
23
|
+
issue: 10
|
|
24
|
+
start: 240586
|
|
25
|
+
year: 2024
|
|
26
|
+
doi: "10.1098/rsos.240586"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Tanner Harms
|
|
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,197 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lagrangian-gradient-regression
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lagrangian Gradient Regression for the detection of Lagrangian Coherent Structures from sparse trajectory data.
|
|
5
|
+
Project-URL: Repository, https://github.com/TannerHarms/Lagrangian-Gradient-Regression
|
|
6
|
+
Project-URL: Paper, https://doi.org/10.1098/rsos.240586
|
|
7
|
+
Project-URL: Issues, https://github.com/TannerHarms/Lagrangian-Gradient-Regression/issues
|
|
8
|
+
Author: Tanner Harms
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: FTLE,coherent structures,fluid dynamics,gradient regression,lagrangian
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Requires-Dist: matplotlib>=3.6
|
|
20
|
+
Requires-Dist: numpy>=1.23
|
|
21
|
+
Requires-Dist: pandas>=1.5
|
|
22
|
+
Requires-Dist: scikit-learn>=1.1
|
|
23
|
+
Requires-Dist: scipy>=1.9
|
|
24
|
+
Requires-Dist: sympy>=1.11
|
|
25
|
+
Provides-Extra: cuda
|
|
26
|
+
Requires-Dist: jax[cuda12]>=0.4; (platform_system == 'Linux') and extra == 'cuda'
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
30
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
31
|
+
Provides-Extra: gpu
|
|
32
|
+
Requires-Dist: jax[cuda12]>=0.4; (platform_system == 'Linux') and extra == 'gpu'
|
|
33
|
+
Provides-Extra: jax
|
|
34
|
+
Requires-Dist: jax>=0.4; extra == 'jax'
|
|
35
|
+
Provides-Extra: ocean
|
|
36
|
+
Requires-Dist: netcdf4>=1.6; extra == 'ocean'
|
|
37
|
+
Requires-Dist: requests>=2.28; extra == 'ocean'
|
|
38
|
+
Requires-Dist: xarray>=2022.9; extra == 'ocean'
|
|
39
|
+
Provides-Extra: rocm
|
|
40
|
+
Requires-Dist: jax[rocm]>=0.4; (platform_system == 'Linux') and extra == 'rocm'
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
|
|
43
|
+
# Lagrangian Gradient Regression (LGR)
|
|
44
|
+
|
|
45
|
+
A modular Python package for estimating flow-map Jacobians from sparse Lagrangian trajectory data and computing Lagrangian Coherent Structure (LCS) diagnostics.
|
|
46
|
+
|
|
47
|
+
## Reference
|
|
48
|
+
|
|
49
|
+
> Harms, T. D., Brunton, S. L., & McKeon, B. J. (2024). [Lagrangian gradient regression for the detection of coherent structures from sparse trajectory data](https://doi.org/10.1098/rsos.240586). *R. Soc. Open Sci.* **11**, 240586.
|
|
50
|
+
|
|
51
|
+
The nonlinear estimators (higher-order polynomial libraries, Gaussian-process local models and the
|
|
52
|
+
composed higher derivatives of the flow map) are described in *Nonlinear Lagrangian Gradient
|
|
53
|
+
Regression* (in preparation). The code and data that reproduce that paper's figures are in
|
|
54
|
+
[Nonlinear-LGR-Paper](https://github.com/TannerHarms/Nonlinear-LGR-Paper).
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install lagrangian-gradient-regression
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The package is imported as `lgr`. It is distributed under its full name because the name `lgr`
|
|
63
|
+
on PyPI belongs to an unrelated project; do not `pip install lgr`. Python 3.12 or newer.
|
|
64
|
+
|
|
65
|
+
Optional extras: `[ocean]` for the gridded ocean-data readers and `[jax]` for the JAX backend.
|
|
66
|
+
|
|
67
|
+
To work on the library itself, install from a clone:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
git clone https://github.com/TannerHarms/Lagrangian-Gradient-Regression.git
|
|
71
|
+
cd Lagrangian-Gradient-Regression
|
|
72
|
+
pip install -e ".[dev]"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
LGR works on a plain `(n_particles, n_times, dim)` array of positions. Nothing
|
|
78
|
+
else is required — no wrapper objects, no DataFrame.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
import numpy as np
|
|
82
|
+
from lgr import (
|
|
83
|
+
fit_library_deformation, compute_lcs_metrics, estimate_error,
|
|
84
|
+
)
|
|
85
|
+
from lgr.flows import DoubleGyre
|
|
86
|
+
|
|
87
|
+
# 1. Trajectories: any (n_particles, n_times, dim) array will do.
|
|
88
|
+
flow = DoubleGyre()
|
|
89
|
+
ics = np.random.rand(500, 2) * [2, 1] # random seeds in [0,2]x[0,1]
|
|
90
|
+
times = np.linspace(0, 10, 101)
|
|
91
|
+
positions = flow.integrate_trajectories(ics, times)
|
|
92
|
+
|
|
93
|
+
# 2. Fit local deformation gradients. `order=1` is standard LGR; raising it
|
|
94
|
+
# fits a richer local flow map and differentiates that at the tracer,
|
|
95
|
+
# which is what reduces finite-neighbourhood bias on sparse data.
|
|
96
|
+
fit = fit_library_deformation(positions, times=times, order=2, k_neighbors=20)
|
|
97
|
+
|
|
98
|
+
fit.F # (n_particles, n_intervals, dim, dim)
|
|
99
|
+
fit.valid # which fits passed the rank/conditioning gates
|
|
100
|
+
fit.normalized_dof_residual # residual, comparable across polynomial orders
|
|
101
|
+
fit.hessian # the second derivative, free once order >= 2
|
|
102
|
+
|
|
103
|
+
# 3. LCS diagnostics from the deformation gradients.
|
|
104
|
+
metrics = compute_lcs_metrics(fit.to_deformation_result(), metrics=("ftle",))
|
|
105
|
+
metrics.ftle # (n_particles, n_intervals)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Missing observations are supported directly — leave them as `NaN` and they are
|
|
109
|
+
excluded from every neighbourhood:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
positions[10, 40:50] = np.nan # this tracer is simply not seen for a while
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Kernel weighting, a Gaussian-process local model, and uncertainty are all
|
|
116
|
+
one argument away:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from lgr import fit_anchored_gp_deformation, WEIGHT_KERNELS
|
|
120
|
+
|
|
121
|
+
fit_library_deformation(positions, times=times, order=2, k_neighbors=20,
|
|
122
|
+
weights="gaussian", bandwidth=0.7)
|
|
123
|
+
gp = fit_anchored_gp_deformation(positions, times=times, k_neighbors=20)
|
|
124
|
+
gp.F_variance # posterior variance of the gradient
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Package Architecture
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
src/lgr/
|
|
131
|
+
├── array_api.py # TrajectoryData, neighbour search, fit_deformation, metrics
|
|
132
|
+
├── regression/ # local estimators: batched kernels, weights, libraries, GP
|
|
133
|
+
├── metrics/ # LCS diagnostics (FTLE, LAVD, DRA, vorticity, uncertainty)
|
|
134
|
+
├── flows/ # analytical flows + batched integration and composition
|
|
135
|
+
├── interpolation/ # scattered-to-grid interpolation
|
|
136
|
+
├── plotting/ # colormaps, fields, trajectories, paper figure style
|
|
137
|
+
├── preprocessing/ # trajectory and Jacobian cleaning
|
|
138
|
+
└── core/ # legacy Particle/DataFrame pipeline (see below)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The array-first API above is the supported path. The `Particle` /
|
|
142
|
+
`generate_dataframe` / `compute_jacobians` pipeline in `lgr.core` predates it
|
|
143
|
+
and is retained for existing scripts; it is slower, holds the whole history in
|
|
144
|
+
a pandas frame, and does not expose the nonlinear estimators. New code should
|
|
145
|
+
use `lgr.PRIMARY_API`.
|
|
146
|
+
|
|
147
|
+
### Extension Points
|
|
148
|
+
|
|
149
|
+
Every major subsystem is built around an **abstract base class** so new
|
|
150
|
+
components can be added without modifying existing code:
|
|
151
|
+
|
|
152
|
+
| Extension Point | ABC | Example Subclasses |
|
|
153
|
+
|----------------|-----|-------------------|
|
|
154
|
+
| Jacobian estimation | `RegressionKernel` | `PseudoinverseRegression`, `GaussianKernelRegression` |
|
|
155
|
+
| Local libraries | `LocalBasis` | `PolynomialBasis` |
|
|
156
|
+
| LCS metrics | `LagrangianMetric` | `FTLEMetric`, `LAVDMetric`, `DRAMetric`, `VorticityMetric` |
|
|
157
|
+
| Flow models | `FlowBase` | `DoubleGyre`, `BickleyJet`, `ABCFlow` |
|
|
158
|
+
| Interpolation | `Interpolator` | `GridDataInterpolator`, `RBFInterpolator` |
|
|
159
|
+
|
|
160
|
+
## Examples
|
|
161
|
+
|
|
162
|
+
Jupyter notebook demonstrations are in the `examples/` directory:
|
|
163
|
+
|
|
164
|
+
- **`LCS_from_velocity_gradients.ipynb`** — FTLE from known analytical velocity gradients
|
|
165
|
+
- **`LGR_on_Random_Sparse_Data.ipynb`** — Full LGR pipeline on sparse/random particle data
|
|
166
|
+
- **`nonlinear_order_benchmark_paper_example.ipynb`** — Paper figure workflow for nonlinear polynomial and GP regression comparisons
|
|
167
|
+
|
|
168
|
+
## Experiment Artifacts
|
|
169
|
+
|
|
170
|
+
The experiment scripts generate NPZ/JSON artifacts, run the array-first LGR
|
|
171
|
+
workflow, and create static figures without changing package source code:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
python scripts/experiments/generate_dataset.py --config configs/experiments/datasets/linear3d_random_tiny.json --output-dir artifacts/datasets
|
|
175
|
+
python scripts/experiments/run_lgr_experiment.py --config configs/experiments/models/array_lgr_default.json --dataset artifacts/datasets/analytic3d_linear3d_random_tiny_seed0.npz --output-dir artifacts/results
|
|
176
|
+
python scripts/experiments/make_figures.py --config configs/experiments/figures/default_report.json --results artifacts/results/analytic3d_linear3d_random_tiny_seed0_array_lgr_default.npz --output-dir artifacts/figures
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Synthetic LPT artifacts and nonlinear model configs are included:
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
python scripts/experiments/generate_dataset.py --config configs/experiments/datasets/synthetic_lpt_double_gyre_tiny.json --output-dir artifacts/datasets
|
|
183
|
+
python scripts/experiments/run_lgr_experiment.py --config configs/experiments/models/array_lgr_polynomial_order2.json --dataset artifacts/datasets/synthetic_lpt_double_gyre_random_tiny_seed0.npz --output-dir artifacts/results
|
|
184
|
+
python scripts/experiments/run_lgr_experiment.py --config configs/experiments/models/array_lgr_model_selection.json --dataset artifacts/datasets/synthetic_lpt_double_gyre_random_tiny_seed0.npz --output-dir artifacts/results
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Slurm and Flux launcher templates are in `configs/experiments/launchers/`.
|
|
188
|
+
|
|
189
|
+
## Running Tests
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
pytest
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT License — see [LICENSE](https://github.com/TannerHarms/Lagrangian-Gradient-Regression/blob/main/LICENSE) for details.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Lagrangian Gradient Regression (LGR)
|
|
2
|
+
|
|
3
|
+
A modular Python package for estimating flow-map Jacobians from sparse Lagrangian trajectory data and computing Lagrangian Coherent Structure (LCS) diagnostics.
|
|
4
|
+
|
|
5
|
+
## Reference
|
|
6
|
+
|
|
7
|
+
> Harms, T. D., Brunton, S. L., & McKeon, B. J. (2024). [Lagrangian gradient regression for the detection of coherent structures from sparse trajectory data](https://doi.org/10.1098/rsos.240586). *R. Soc. Open Sci.* **11**, 240586.
|
|
8
|
+
|
|
9
|
+
The nonlinear estimators (higher-order polynomial libraries, Gaussian-process local models and the
|
|
10
|
+
composed higher derivatives of the flow map) are described in *Nonlinear Lagrangian Gradient
|
|
11
|
+
Regression* (in preparation). The code and data that reproduce that paper's figures are in
|
|
12
|
+
[Nonlinear-LGR-Paper](https://github.com/TannerHarms/Nonlinear-LGR-Paper).
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install lagrangian-gradient-regression
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The package is imported as `lgr`. It is distributed under its full name because the name `lgr`
|
|
21
|
+
on PyPI belongs to an unrelated project; do not `pip install lgr`. Python 3.12 or newer.
|
|
22
|
+
|
|
23
|
+
Optional extras: `[ocean]` for the gridded ocean-data readers and `[jax]` for the JAX backend.
|
|
24
|
+
|
|
25
|
+
To work on the library itself, install from a clone:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
git clone https://github.com/TannerHarms/Lagrangian-Gradient-Regression.git
|
|
29
|
+
cd Lagrangian-Gradient-Regression
|
|
30
|
+
pip install -e ".[dev]"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
LGR works on a plain `(n_particles, n_times, dim)` array of positions. Nothing
|
|
36
|
+
else is required — no wrapper objects, no DataFrame.
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import numpy as np
|
|
40
|
+
from lgr import (
|
|
41
|
+
fit_library_deformation, compute_lcs_metrics, estimate_error,
|
|
42
|
+
)
|
|
43
|
+
from lgr.flows import DoubleGyre
|
|
44
|
+
|
|
45
|
+
# 1. Trajectories: any (n_particles, n_times, dim) array will do.
|
|
46
|
+
flow = DoubleGyre()
|
|
47
|
+
ics = np.random.rand(500, 2) * [2, 1] # random seeds in [0,2]x[0,1]
|
|
48
|
+
times = np.linspace(0, 10, 101)
|
|
49
|
+
positions = flow.integrate_trajectories(ics, times)
|
|
50
|
+
|
|
51
|
+
# 2. Fit local deformation gradients. `order=1` is standard LGR; raising it
|
|
52
|
+
# fits a richer local flow map and differentiates that at the tracer,
|
|
53
|
+
# which is what reduces finite-neighbourhood bias on sparse data.
|
|
54
|
+
fit = fit_library_deformation(positions, times=times, order=2, k_neighbors=20)
|
|
55
|
+
|
|
56
|
+
fit.F # (n_particles, n_intervals, dim, dim)
|
|
57
|
+
fit.valid # which fits passed the rank/conditioning gates
|
|
58
|
+
fit.normalized_dof_residual # residual, comparable across polynomial orders
|
|
59
|
+
fit.hessian # the second derivative, free once order >= 2
|
|
60
|
+
|
|
61
|
+
# 3. LCS diagnostics from the deformation gradients.
|
|
62
|
+
metrics = compute_lcs_metrics(fit.to_deformation_result(), metrics=("ftle",))
|
|
63
|
+
metrics.ftle # (n_particles, n_intervals)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Missing observations are supported directly — leave them as `NaN` and they are
|
|
67
|
+
excluded from every neighbourhood:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
positions[10, 40:50] = np.nan # this tracer is simply not seen for a while
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Kernel weighting, a Gaussian-process local model, and uncertainty are all
|
|
74
|
+
one argument away:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from lgr import fit_anchored_gp_deformation, WEIGHT_KERNELS
|
|
78
|
+
|
|
79
|
+
fit_library_deformation(positions, times=times, order=2, k_neighbors=20,
|
|
80
|
+
weights="gaussian", bandwidth=0.7)
|
|
81
|
+
gp = fit_anchored_gp_deformation(positions, times=times, k_neighbors=20)
|
|
82
|
+
gp.F_variance # posterior variance of the gradient
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Package Architecture
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
src/lgr/
|
|
89
|
+
├── array_api.py # TrajectoryData, neighbour search, fit_deformation, metrics
|
|
90
|
+
├── regression/ # local estimators: batched kernels, weights, libraries, GP
|
|
91
|
+
├── metrics/ # LCS diagnostics (FTLE, LAVD, DRA, vorticity, uncertainty)
|
|
92
|
+
├── flows/ # analytical flows + batched integration and composition
|
|
93
|
+
├── interpolation/ # scattered-to-grid interpolation
|
|
94
|
+
├── plotting/ # colormaps, fields, trajectories, paper figure style
|
|
95
|
+
├── preprocessing/ # trajectory and Jacobian cleaning
|
|
96
|
+
└── core/ # legacy Particle/DataFrame pipeline (see below)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The array-first API above is the supported path. The `Particle` /
|
|
100
|
+
`generate_dataframe` / `compute_jacobians` pipeline in `lgr.core` predates it
|
|
101
|
+
and is retained for existing scripts; it is slower, holds the whole history in
|
|
102
|
+
a pandas frame, and does not expose the nonlinear estimators. New code should
|
|
103
|
+
use `lgr.PRIMARY_API`.
|
|
104
|
+
|
|
105
|
+
### Extension Points
|
|
106
|
+
|
|
107
|
+
Every major subsystem is built around an **abstract base class** so new
|
|
108
|
+
components can be added without modifying existing code:
|
|
109
|
+
|
|
110
|
+
| Extension Point | ABC | Example Subclasses |
|
|
111
|
+
|----------------|-----|-------------------|
|
|
112
|
+
| Jacobian estimation | `RegressionKernel` | `PseudoinverseRegression`, `GaussianKernelRegression` |
|
|
113
|
+
| Local libraries | `LocalBasis` | `PolynomialBasis` |
|
|
114
|
+
| LCS metrics | `LagrangianMetric` | `FTLEMetric`, `LAVDMetric`, `DRAMetric`, `VorticityMetric` |
|
|
115
|
+
| Flow models | `FlowBase` | `DoubleGyre`, `BickleyJet`, `ABCFlow` |
|
|
116
|
+
| Interpolation | `Interpolator` | `GridDataInterpolator`, `RBFInterpolator` |
|
|
117
|
+
|
|
118
|
+
## Examples
|
|
119
|
+
|
|
120
|
+
Jupyter notebook demonstrations are in the `examples/` directory:
|
|
121
|
+
|
|
122
|
+
- **`LCS_from_velocity_gradients.ipynb`** — FTLE from known analytical velocity gradients
|
|
123
|
+
- **`LGR_on_Random_Sparse_Data.ipynb`** — Full LGR pipeline on sparse/random particle data
|
|
124
|
+
- **`nonlinear_order_benchmark_paper_example.ipynb`** — Paper figure workflow for nonlinear polynomial and GP regression comparisons
|
|
125
|
+
|
|
126
|
+
## Experiment Artifacts
|
|
127
|
+
|
|
128
|
+
The experiment scripts generate NPZ/JSON artifacts, run the array-first LGR
|
|
129
|
+
workflow, and create static figures without changing package source code:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
python scripts/experiments/generate_dataset.py --config configs/experiments/datasets/linear3d_random_tiny.json --output-dir artifacts/datasets
|
|
133
|
+
python scripts/experiments/run_lgr_experiment.py --config configs/experiments/models/array_lgr_default.json --dataset artifacts/datasets/analytic3d_linear3d_random_tiny_seed0.npz --output-dir artifacts/results
|
|
134
|
+
python scripts/experiments/make_figures.py --config configs/experiments/figures/default_report.json --results artifacts/results/analytic3d_linear3d_random_tiny_seed0_array_lgr_default.npz --output-dir artifacts/figures
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Synthetic LPT artifacts and nonlinear model configs are included:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
python scripts/experiments/generate_dataset.py --config configs/experiments/datasets/synthetic_lpt_double_gyre_tiny.json --output-dir artifacts/datasets
|
|
141
|
+
python scripts/experiments/run_lgr_experiment.py --config configs/experiments/models/array_lgr_polynomial_order2.json --dataset artifacts/datasets/synthetic_lpt_double_gyre_random_tiny_seed0.npz --output-dir artifacts/results
|
|
142
|
+
python scripts/experiments/run_lgr_experiment.py --config configs/experiments/models/array_lgr_model_selection.json --dataset artifacts/datasets/synthetic_lpt_double_gyre_random_tiny_seed0.npz --output-dir artifacts/results
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Slurm and Flux launcher templates are in `configs/experiments/launchers/`.
|
|
146
|
+
|
|
147
|
+
## Running Tests
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
pytest
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
MIT License — see [LICENSE](https://github.com/TannerHarms/Lagrangian-Gradient-Regression/blob/main/LICENSE) for details.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
# The distribution is named in full because `lgr` on PyPI is an unrelated project.
|
|
7
|
+
# The import name is `lgr`.
|
|
8
|
+
name = "lagrangian-gradient-regression"
|
|
9
|
+
version = "0.1.0"
|
|
10
|
+
description = "Lagrangian Gradient Regression for the detection of Lagrangian Coherent Structures from sparse trajectory data."
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
license = { text = "MIT" }
|
|
13
|
+
requires-python = ">=3.12"
|
|
14
|
+
authors = [
|
|
15
|
+
{ name = "Tanner Harms" },
|
|
16
|
+
]
|
|
17
|
+
keywords = [
|
|
18
|
+
"lagrangian",
|
|
19
|
+
"coherent structures",
|
|
20
|
+
"FTLE",
|
|
21
|
+
"gradient regression",
|
|
22
|
+
"fluid dynamics",
|
|
23
|
+
]
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Development Status :: 3 - Alpha",
|
|
26
|
+
"Intended Audience :: Science/Research",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
"Topic :: Scientific/Engineering :: Physics",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
dependencies = [
|
|
34
|
+
"numpy>=1.23",
|
|
35
|
+
"pandas>=1.5",
|
|
36
|
+
"scipy>=1.9",
|
|
37
|
+
"scikit-learn>=1.1",
|
|
38
|
+
"matplotlib>=3.6",
|
|
39
|
+
"sympy>=1.11",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
dev = [
|
|
44
|
+
"pytest>=7",
|
|
45
|
+
"pytest-cov",
|
|
46
|
+
"ruff",
|
|
47
|
+
]
|
|
48
|
+
ocean = [
|
|
49
|
+
"requests>=2.28",
|
|
50
|
+
"xarray>=2022.9",
|
|
51
|
+
"netcdf4>=1.6",
|
|
52
|
+
]
|
|
53
|
+
jax = [
|
|
54
|
+
"jax>=0.4",
|
|
55
|
+
]
|
|
56
|
+
cuda = [
|
|
57
|
+
"jax[cuda12]>=0.4; platform_system == 'Linux'",
|
|
58
|
+
]
|
|
59
|
+
gpu = [
|
|
60
|
+
"jax[cuda12]>=0.4; platform_system == 'Linux'",
|
|
61
|
+
]
|
|
62
|
+
rocm = [
|
|
63
|
+
"jax[rocm]>=0.4; platform_system == 'Linux'",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[project.urls]
|
|
67
|
+
Repository = "https://github.com/TannerHarms/Lagrangian-Gradient-Regression"
|
|
68
|
+
Paper = "https://doi.org/10.1098/rsos.240586"
|
|
69
|
+
Issues = "https://github.com/TannerHarms/Lagrangian-Gradient-Regression/issues"
|
|
70
|
+
|
|
71
|
+
# ---------------------------------------------------------------------------
|
|
72
|
+
# Hatch — src layout
|
|
73
|
+
# ---------------------------------------------------------------------------
|
|
74
|
+
[tool.hatch.build.targets.wheel]
|
|
75
|
+
packages = ["src/lgr"]
|
|
76
|
+
|
|
77
|
+
# The source distribution carries the library and its tests, not the notebooks.
|
|
78
|
+
[tool.hatch.build.targets.sdist]
|
|
79
|
+
include = ["src/lgr", "tests", "README.md", "LICENSE", "CITATION.cff", "pyproject.toml"]
|
|
80
|
+
|
|
81
|
+
# ---------------------------------------------------------------------------
|
|
82
|
+
# Pytest
|
|
83
|
+
# ---------------------------------------------------------------------------
|
|
84
|
+
[tool.pytest.ini_options]
|
|
85
|
+
testpaths = ["tests"]
|
|
86
|
+
addopts = "-v --tb=short"
|
|
87
|
+
|
|
88
|
+
# ---------------------------------------------------------------------------
|
|
89
|
+
# Ruff (linting / formatting)
|
|
90
|
+
# ---------------------------------------------------------------------------
|
|
91
|
+
[tool.ruff]
|
|
92
|
+
line-length = 88
|
|
93
|
+
target-version = "py310"
|
|
94
|
+
|
|
95
|
+
[tool.ruff.lint]
|
|
96
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM"]
|
|
97
|
+
ignore = ["E501"]
|