# apasto/decorrelate-grids architecture

> Architecture diagram of the GitHub repository apasto/decorrelate-grids, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apasto/decorrelate-grids
- Repository: https://github.com/apasto/decorrelate-grids

## Overview

1. **Repository role**
   - This is a small Python package, not an application server or service.
   - Its purpose is to compute **windowed linear decorrelation** between two same-shaped 2-D grids, producing fitted coefficient grids and residuals.

2. **Top-level package boundary**
   - The actual runtime code lives in the `decorrelategrids/` package.
   - `readme_figures/` is documentation-only.
   - `pyproject.toml` and `environment.yml` define packaging and dependencies; they matter because the project is intended to run in a Python scientific stack, likely with `xarray` and netCDF support.

3. **Main subsystems**
   - `decorrelategrids/rollinglinreg.py`: the numerical core for rolling ordinary-least-squares regression.
   - `decorrelategrids/cliwrapper.py`: the command-line entry point and I/O orchestration.
   - `decorrelategrids/__init__.py`: package marker and likely exports/version surface.

4. **Primary data flow**
   - Input: two aligned 2-D arrays/grids, referred to in the README as `A` and `B`.
   - The core algorithm slides a local window over each valid interior pixel.
   - For each window, it fits `A = c0 + c1 * B`.
   - Output: coefficient grids for `c0` and `c1`, plus a residual grid `epsilon = A - fitted(A)`.

5. **Computation boundary**
   - The math layer is intended to be **format-agnostic** in principle.
   - In practice, the current implementation is tied to **xarray/netCDF** at the edges, because the README says the script reads and writes netCDF files.
   - That means the architecture is split between a reusable algorithm core and a file-format wrapper.

6. **Rolling regression behavior**
   - The window is boxcar-shaped and centered on each pixel, with a configurable half-width in rows and columns.
   - No fit is performed on edge pixels where the full window would exceed array bounds.
   - No fit is performed when the target pixel or any required input is `NaN`.
   - The only implemented model is **ordinary least squares**; there is no generic model-plug-in system yet.

7. **Output semantics**
   - The package does not just return a single decorrelated grid.
   - It also returns the spatially varying fitted coefficients, which are first-class results.
   - This matters for architecture diagrams because the output is a small product family: intercept grid, slope grid, and residual grid.

8. **CLI subsystem**
   - `cliwrapper.py` is the user-facing entry point exposed as the `decorrelategrids` command.
   - Its job is to parse user options, load grid data, call the regression core, and persist results.
   - So the CLI is an orchestration layer rather than a computational layer.

9. **Runtime and tooling**
   - The project targets standard Python packaging and local installation.
   - The docs explicitly mention `pip`, editable installs, `uv`, and Conda, so it is meant to run in common scientific Python environments.
   - Lint/config files (`.flake8`, `.isort.cfg`, `.markdownlint.yaml`) indicate a lightweight repository focused on code quality, not on a larger build pipeline.

10. **Domain-specific design constraint**
   - The code was motivated by InSAR tropospheric-delay mitigation, but the implementation is intentionally domain-neutral.
   - That means the architecture should be drawn as a general grid-regression tool with an optional geoscience use case, not as an InSAR application.

11. **Current maturity boundary**
   - The README labels the project as a **concept / early-stage prototype**.
   - Architecturally, that means the core shape is present, but extensibility points are limited and the format dependence is still part of the design.

12. **What an architecture diagram should show**
   - Two input grids enter a CLI/IO layer.
   - The CLI hands them to a rolling regression engine.
   - The engine performs local-window OLS over valid pixels only.
   - The outputs are coefficient grids and a residual grid, which are written back out through the same I/O layer.

Main entry points: [`README.md`](https://github.com/apasto/decorrelate-grids/blob/main/README.md) (README docs), [`decorrelategrids/rollinglinreg.py`](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py) (Rolling OLS), [`decorrelategrids/__init__.py`](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/__init__.py) (Package API), [`decorrelategrids/cliwrapper.py`](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/cliwrapper.py) (CLI entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_package["Runtime package"]
  node_node_entry["CLI entry<br/>command entrypoint<br/>[cliwrapper.py]"]
  node_node_io["Grid I/O<br/>load-save adapter<br/>[cliwrapper.py]"]
  node_node_core["Rolling OLS<br/>numerical core<br/>[rollinglinreg.py]"]
  node_node_window{{"Window scan<br/>local windowing<br/>[rollinglinreg.py]"}}
  node_node_fit["OLS fit<br/>regression step<br/>[rollinglinreg.py]"]
  node_node_mask(("Valid pixels<br/>nan handling<br/>[rollinglinreg.py]"))
  node_node_coeffs["Coeff grids<br/>model output<br/>[rollinglinreg.py]"]
  node_node_resid["Residual grid<br/>model output<br/>[rollinglinreg.py]"]
  node_node_pkginit["Package API<br/>package surface<br/>[__init__.py]"]
end

subgraph group_grp_docs["Docs and examples"]
  node_node_docs["README docs<br/>documentation<br/>[README.md]"]
  node_node_figs["Figures<br/>doc assets"]
  node_node_issues["Issue template<br/>docs template<br/>[bug_report.md]"]
end

subgraph group_grp_tooling["Packaging and config"]
  node_node_python[("Python stack<br/>runtime env<br/>[pyproject.toml]")]
  node_node_deps["Dependencies<br/>packaging config<br/>[pyproject.toml]"]
  node_node_conda["Conda env<br/>env spec<br/>[environment.yml]"]
  node_node_lint["Lint config<br/>repo tooling<br/>[.flake8]"]
end

node_node_entry -->|"orchestrates"| node_node_io
node_node_io -->|"calls"| node_node_core
node_node_core -->|"slides"| node_node_window
node_node_window -->|"feeds"| node_node_fit
node_node_mask -->|"filters"| node_node_fit
node_node_fit -->|"produces"| node_node_coeffs
node_node_fit -->|"produces"| node_node_resid
node_node_core -->|"returns"| node_node_coeffs
node_node_core -->|"returns"| node_node_resid
node_node_pkginit -->|"exposes"| node_node_core
node_node_pkginit -->|"exposes"| node_node_entry
node_node_docs -->|"describes"| node_node_entry
node_node_docs -->|"describes"| node_node_core
node_node_docs -->|"documents"| node_node_coeffs
node_node_docs -->|"documents"| node_node_resid
node_node_figs -->|"supports"| node_node_docs
node_node_deps -->|"targets"| node_node_python
node_node_conda -->|"mirrors"| node_node_python
node_node_lint -->|"governs"| node_node_pkginit
node_node_issues -->|"extends"| node_node_docs

click node_node_entry "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/cliwrapper.py"
click node_node_io "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/cliwrapper.py"
click node_node_core "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py"
click node_node_window "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py"
click node_node_fit "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py"
click node_node_mask "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py"
click node_node_coeffs "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py"
click node_node_resid "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py"
click node_node_pkginit "https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/__init__.py"
click node_node_python "https://github.com/apasto/decorrelate-grids/blob/main/pyproject.toml"
click node_node_deps "https://github.com/apasto/decorrelate-grids/blob/main/pyproject.toml"
click node_node_conda "https://github.com/apasto/decorrelate-grids/blob/main/environment.yml"
click node_node_lint "https://github.com/apasto/decorrelate-grids/blob/main/.flake8"
click node_node_docs "https://github.com/apasto/decorrelate-grids/blob/main/README.md"
click node_node_figs "https://github.com/apasto/decorrelate-grids/tree/main/readme_figures"
click node_node_issues "https://github.com/apasto/decorrelate-grids/blob/main/.github/ISSUE_TEMPLATE/bug_report.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_entry,node_node_io,node_node_core,node_node_window,node_node_fit,node_node_mask,node_node_coeffs,node_node_resid,node_node_pkginit toneBlue
class node_node_docs,node_node_figs,node_node_issues toneAmber
class node_node_python,node_node_deps,node_node_conda,node_node_lint toneMint
```

## Components

### Runtime package

Installable Python package containing the CLI and numerical core.

- [CLI entry](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/cliwrapper.py): command entrypoint · User-facing `decorrelategrids` command exposed by the package. · `decorrelategrids/cliwrapper.py`
- [Grid I/O](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/cliwrapper.py): load-save adapter · Reads aligned grid inputs and writes coefficient/residual outputs, likely through netCDF/xarray. · `decorrelategrids/cliwrapper.py`
- [Rolling OLS](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py): numerical core · Sliding-window ordinary least squares over two same-shaped 2-D grids. · `decorrelategrids/rollinglinreg.py`
- [Window scan](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py): local windowing · Centers a boxcar window on each interior pixel and skips edge pixels without a full window. · `decorrelategrids/rollinglinreg.py`
- [OLS fit](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py): regression step · Fits `A = c0 + c1 * B` within each valid window. · `decorrelategrids/rollinglinreg.py`
- [Valid pixels](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py): nan handling · Excludes pixels or windows containing required missing values from fitting. · `decorrelategrids/rollinglinreg.py`
- [Coeff grids](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py): model output · Spatial grids for intercept `c0` and slope `c1`. · `decorrelategrids/rollinglinreg.py`
- [Residual grid](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/rollinglinreg.py): model output · Residual field `epsilon = A - fitted(A)` returned as a first-class result. · `decorrelategrids/rollinglinreg.py`
- [Package API](https://github.com/apasto/decorrelate-grids/blob/main/decorrelategrids/__init__.py): package surface · Package marker and export surface for the installable module. · `decorrelategrids/__init__.py`

### Docs and examples

Repository content for README visuals and issue templates.

- [README docs](https://github.com/apasto/decorrelate-grids/blob/main/README.md): documentation · Project narrative, usage, and conceptual figures. · `README.md`
- [Figures](https://github.com/apasto/decorrelate-grids/tree/main/readme_figures): doc assets · Static assets used by the README and documentation. · `readme_figures`
- [Issue template](https://github.com/apasto/decorrelate-grids/blob/main/.github/ISSUE_TEMPLATE/bug_report.md): docs template · GitHub issue template for bug reports. · `.github/ISSUE_TEMPLATE/bug_report.md`

### Packaging and config

Project metadata, dependency definitions, and lightweight repo tooling.

- [Python stack](https://github.com/apasto/decorrelate-grids/blob/main/pyproject.toml): runtime env · Scientific Python runtime used by the package. · `pyproject.toml`
- [Dependencies](https://github.com/apasto/decorrelate-grids/blob/main/pyproject.toml): packaging config · Declares project dependencies and installation metadata. · `pyproject.toml`
- [Conda env](https://github.com/apasto/decorrelate-grids/blob/main/environment.yml): env spec · Alternative scientific environment definition. · `environment.yml`
- [Lint config](https://github.com/apasto/decorrelate-grids/blob/main/.flake8): repo tooling · Lightweight code-quality settings for formatting and linting. · `.flake8`

## Connections

- CLI entry → Grid I/O: orchestrates · CLI coordinates file loading and result writing.
- Grid I/O → Rolling OLS: calls · I/O layer hands arrays to the numerical engine.
- Rolling OLS → Window scan: slides · Core iterates a centered boxcar window over the grid.
- Window scan → OLS fit: feeds · Each valid window is passed to the regression step.
- Valid pixels → OLS fit: filters · Missing data and invalid pixels suppress a fit.
- OLS fit → Coeff grids: produces · Regression emits intercept and slope grids.
- OLS fit → Residual grid: produces · Regression emits a residual grid alongside coefficients.
- Rolling OLS → Coeff grids: returns · Core returns fitted coefficient products.
- Rolling OLS → Residual grid: returns · Core returns residuals as part of the output family.
- Package API → Rolling OLS: exposes · Package surface can expose the main algorithm entry points.
- Package API → CLI entry: exposes · Package surface can expose CLI-facing symbols.
- README docs → CLI entry: describes · README explains the command-line workflow.
- README docs → Rolling OLS: describes · README documents the rolling decorrelation algorithm.
- README docs → Coeff grids: documents · Docs treat coefficient grids as primary outputs.
- README docs → Residual grid: documents · Docs describe the residual decorrelation product.
- Figures → README docs: supports · Figures are used as documentation assets.
- Dependencies → Python stack: targets · Packaging metadata defines the Python runtime stack.
- Conda env → Python stack: mirrors · Conda environment mirrors the scientific Python setup.
- Lint config → Package API: governs · Repo tooling enforces lightweight code quality on the package.
- Issue template → README docs: extends · Issue template supports the repository documentation workflow.
