Changes for .kilo/plan.md: 0 added lines, 106 removed lines.
Original line number
Diff line number
Diff line
# Refactoring Plan for ``plot_models.py``
## 1️⃣ Goal
Split the monolithic **`PyAETNA/reporting/plot_models.py`** into a set of focused, smaller thematic sub‑modules while:
- Removing *ghost variables* (imports or locals that are never used).
- Eliminating duplicated code blocks.
- Simplifying complex logic where possible.
The end result should be efficient, JIT compliant, easier to read, test and maintain, with each file having a single responsibility. Also `PyAETNA/reporting/plot_models.py` must be smaller after moving parts of the monolithic code to delegated submodules.
---
## 2️⃣ Identify Functional Areas (themes)
The current implementation can be grouped into the following logical sections:
| Theme | Typical responsibilities |
|------|---------------------------|
| **Model component computation** | Functions that build model pieces (`compute_model_components`, `build_mean_function`, `detrend_data`, etc.). |
* Common colour palettes (`_get_distinct_colors`).
* Helper to add reference uncertainty bars (`_add_unc_ref_bars`).
***`overlay_plots.py`**
* Functions that receive the prepared model arrays and draw the four component lines (normalisation, planet, trend, GP) with their confidence bands.
* Separate implementations for RV vs LC where necessary.
***`detrended_residuals.py`**
*`detrend_data` usage to produce the detrended series.
* Residual calculation and plotting, including the KDE inset logic.
***`plot_models.py` (facade)**
* Import the above modules.
* Validate arguments, decide which panels to draw, orchestrate the workflow.
* Keep only high‑level orchestration – no heavy numerical code.
---
## 4️⃣ Refactoring Steps
1.**Create the new files** with minimal stub content and import statements. ✅ Completed
2.**Move functions** and code blocks from `plot_models.py` to `model_components.py` according to the logic stated in sections 2 and 3 (copy‑paste, then delete from the original). Keep the public API identical and update the imports in `plot_models.py`.
3.**Move functions** and code blocks from `plot_models.py` to `confidence_bands.py` according to the logic stated in sections 2 and 3 (copy‑paste, then delete from the original). Keep the public API identical and update the imports in `plot_models.py`.
4.**Run the test** executing a quick end‑to‑end run of `runPyAETNA.py` to ensure nothing broke.
5.**Clean**`plot_models.py` by deleting the code moved to the submodules and adding the calls to the delegated functions.
6.**Search for ghost variables**:
* Unused imports (`jax`, `warnings`, etc.).
* Locally defined variables that are never read after assignment (e.g., temporary caches).
7.**Detect duplicate code blocks** – many sections repeat the same error‑bar plotting logic for different panels; extract a helper like `plot_errorbars(ax, t, y, dy, style)` into `plot_utils.py`.
8.**Simplify complex conditionals**:
* Replace nested `if … else …` chains that only toggle a boolean with a single expression (`show_gp = show_gp and np.any(gp != 0)`).
* Use list comprehensions for building colour maps instead of explicit loops where possible.
9.**Add docstrings** to the newly created functions, describing inputs/outputs – this will aid future maintenance.
10.**Remove** test scripts, stubs and code for backward compatibility produced during the refactoring.
11. **Run** final test by executing `runPyAETNA.py` to catch regressions. If the run aborts, debug the code and rerun the script iteratively.
---
## 5️⃣ Checklist after Refactor
- [ ] All original functionality works (visual output unchanged).
- [ ] No `ImportError` when running the package.
- [ ] Linter (`flake8`/`ruff`) reports zero unused imports or variables.
- [ ] Duplicate code sections have been replaced by shared helpers.
- [ ] Unit tests (if present) pass; otherwise, manually verify a few plots.
---
## 6️⃣ Next Actions for the User
1.**Run the refactor** using the steps above or let me generate the skeleton files automatically.
2. After moving code, run `runPyAETNA.py` to catch regressions. If the run aborts, debug the code and rerun the script iteratively.
3. If you encounter any ambiguous sections (e.g., a variable whose purpose is unclear), ask for clarification – I can help decide whether it should stay or be removed.
---
*This plan is now stored in `/.kilo/plan.md` and can be referenced later while performing the actual code moves.*
Changes for .kilo/planThunk.md: 0 added lines, 219 removed lines.
Original line number
Diff line number
Diff line
# Fix JAX Segmentation Fault in PyAETNA `relabel_array` — Action Plan
## Problem Statement
**User Context:**
G (a researcher at INAF Catania) recently updated JAX, JAXlib, and NumPyro (originally installed March 2025) on the ExoREC-Ganymede H100 server. A previously working PyAETNA analysis script (`runFit.py` in TOI5572 directory) now crashes with a segmentation fault at the JAX/XLA layer.
**Crash Signature:**
```
Fatal Python error: Segmentation fault
File ".../jax/_src/numpy/setops.py", line 856 in unique
File ".../PyAETNA/sampler.py", line 1749 in relabel_array
```
**Root Cause:**
JAX 0.4.31+ flipped the default XLA CPU backend from the legacy StreamExecutor to the new **Thunk runtime** (IFRT/PjRt). The Thunk runtime has a known bug when handling `jnp.unique` operations with **dynamic output shapes** — it crashes at the compiled kernel dispatch level. The `relabel_array` function in `sampler.py` calls `jnp.unique` without specifying an output size, forcing XLA to infer the size dynamically.
**Workaround (Not Sustainable):**
Setting `XLA_FLAGS="--xla_cpu_use_thunk_runtime=false"` reverts to the old backend and the script runs. However, this flag is deprecated and will be removed in a future JAX release.
---
## Solution Overview
Replace all calls to `jnp.unique()` in the `relabel_array` function with **static-size**`jnp.unique(..., size=...)` calls. This forces XLA to compile with a fixed output shape, eliminating the dynamic shape inference that triggers the Thunk runtime bug.
**Benefits:**
- ✅ Removes dependency on deprecated `XLA_FLAGS` workaround
- ✅ Future-proof (works with all XLA backends, current and future)