# Plan: Extract Experimental Kinetics into a Standalone Crate

## Goal

Copy the `experimental_kinetics` (domain logic) and `experimental_kinetics_gui` (GUI) modules from the KiThe monorepo into a new standalone Rust crate that compiles and runs independently. The new crate's main window will be the Experimental Kinetics GUI (no longer embedded as a sub-window of KiThe).

**Critical constraint:** The original KiThe source code must NOT be modified.

---

## 1. Dependency Analysis

### 1.1 External Crates (from KiThe's `Cargo.toml`)

All of these are needed by the extracted modules:

| Crate | Version | Used By |
|-------|---------|---------|
| `eframe` | 0.32.3 | GUI (main window framework) |
| `egui` | 0.32.3 | GUI (UI components) |
| `egui_plot` | 0.33.0 | GUI (plot rendering) |
| `egui_extras` | 0.32.3 | GUI (table builder, images) |
| `egui_kittest` | 0.32.3 | GUI (testing) |
| `polars` | 0.53.0 | Domain (DataFrame, CSV I/O) |
| `ndarray` | 0.17.2 | Domain (numerical arrays) |
| `ndarray-stats` | 0.7.0 | Domain (statistics) |
| `nalgebra` | 0.35.0 | Domain (matrices, IVP solver) |
| `RustedSciThe` | 0.4.4 | Domain (splines, ODE solvers, SavGol, Lowess) |
| `serde` / `serde_json` | 1.0 | Settings serialization |
| `log` / `simplelog` | 0.4 / 0.12 | Logging |
| `tabled` | 0.21.0 | Settings table display |
| `rand` / `rand_distr` | 0.10 / 0.6 | Synthetic data generation |
| `tempfile` | 3.26.0 | Synthetic data temp files |
| `chrono` | 0.4.41 | Timestamps |
| `rfd` | 0.17.2 | File dialogs |
| `strum` / `strum_macros` | 0.28.0 | Enum iteration |
| `thiserror` | 2.0.18 | Error types |
| `regex` | 1.12.4 | String processing |
| `approx` | 0.5.1 | Floating point comparisons |
| `fastLowess` | 1.1.2 | LOWESS smoothing |
| `KiThePlot` | 0.5.0 | Plot editor window |
| `splines` | 5.0.0 | Spline interpolation |
| `bspline` | 1.1.0 | B-spline interpolation |
| `lstsq` | 0.7.0 | Least squares |
| `rayon` | 1.11.0 | Parallelism |
| `polars-ops` | (polars feature) | DataFrame operations |

### 1.2 Internal KiThe Module Dependencies (MUST be copied)

The extracted modules reference these KiThe-internal modules. They must all be copied into the new crate:

#### From `src/Kinetics/experimental_kinetics/` (domain logic):

| File | Purpose |
|------|---------|
| `one_experiment_dataset.rs` | Core TGA dataset types (`TGADataset`, `TGASchema`, `TGADomainError`, etc.) |
| `exp_engine_api.rs` | Plot data extraction, golden pipeline config |
| `exp_kinetics_column_manipulation.rs` | Column manipulation utilities |
| `exp_kinetics_smooth_filter.rs` | Smoothing/filtering (Hampel, SavGol) |
| `LSQSplines2.rs` | LSQ spline implementation |
| `ndarray_statistics.rs` | ndarray-based statistics |
| `kinetic_methods.rs` | Kinetic method infrastructure |
| `kinetic_methods2.rs` | Kinetic method simulations |
| `kinetic_methods_tests.rs` | Tests |
| `filters_smoothing_test.rs` | Tests |
| `one_experiment_dataset_test.rs` | Tests |
| `testing_mod.rs` | Synthetic data generation |
| `experiment_series/experiment_series_main.rs` | `TGASeries`, `TGAExperiment` |
| `experiment_series/experiment_series_core.rs` | Core series logic |
| `experiment_series/experiment_series_io.rs` | I/O operations |
| `experiment_series/experiment_series_ops.rs` | Operations |
| `experiment_series/experiment_series_analysis.rs` | Analysis |
| `experiment_series/experiment_series2.rs` | `UnitedDataset`, `SampledColumns` |
| `experiment_series/experiment_series3.rs` | I/O for kinetic methods |
| `experiment_series/experiment_series_main_test.rs` | Tests |
| `kinetic_methods/combined.rs` | Combined kinetic analysis |
| `kinetic_methods/Criado.rs` | Criado master curve |
| `kinetic_methods/Criado2.rs` | Criado variant |
| `kinetic_methods/Friedman.rs` | Friedman method |
| `kinetic_methods/integral_isoconversion.rs` | OFW, KAS, Starink |
| `kinetic_methods/integral_isoconversion_tests.rs` | Tests |
| `kinetic_methods/is_this_a_sublimation.rs` | Sublimation check |
| `kinetic_methods/isoconversion.rs` | Isoconversional dispatcher |
| `kinetic_methods/kinetic_regression.rs` | OLS regression |
| `kinetic_methods/Kissinger.rs` | Kissinger method |
| `kinetic_methods/Vyazovkin.rs` | Vyazovkin method |

#### From `src/gui/experimental_kinetics_gui/` (GUI):

| File | Purpose |
|------|---------|
| `experimental_kinetics_gui_main.rs` | Main `PlotApp` struct (the app window) |
| `model.rs` | `PlotModel`, `PlotCurve` |
| `view.rs` | `PlotView` (rendering) |
| `controller.rs` | `PlotController` (interactions) |
| `controller_buttons_and_panels.rs` | Top menus, panels |
| `controller_filters.rs` | Filter dialogs |
| `controller_golden_pipeline.rs` | Golden pipeline dialog |
| `controller_kinetics.rs` | Direct problem dialog |
| `controller_methods.rs` | Kinetic methods dialogs |
| `controller_table.rs` | Column manager |
| `interaction.rs` | Selection, pan, zoom state |
| `kitheplot_wrapper.rs` | KiThePlot integration |
| `model_handlers.rs` | Model ↔ domain bridge |
| `settings.rs` | Settings persistence |
| `test_options.rs` | Test/synthetic data UI |
| `help_view.rs` | Help viewer |
| `gui_test.rs` | Test helpers |
| `main.rs` | (currently empty TODO) |

#### Additional KiThe modules referenced by the GUI:

| Module | File | Used By |
|--------|------|---------|
| `gui::ivp_common` | `src/gui/ivp_common.rs` | `controller_kinetics.rs` (IVP task state) |
| `Kinetics::solid_state_kinetics_IVP` | `src/Kinetics/solid_state_kinetics_IVP.rs` | `ivp_common`, `kinetic_methods2` |

These two additional modules must also be copied.

---

## 2. New Crate Structure

```
experimental_kinetics_standalone/
├── Cargo.toml
├── src/
│   ├── main.rs                    # New entry point
│   ├── lib.rs                     # Re-exports
│   │
│   ├── domain/                    # (was src/Kinetics/experimental_kinetics/)
│   │   ├── mod.rs                 # Module declarations
│   │   ├── one_experiment_dataset.rs
│   │   ├── exp_engine_api.rs
│   │   ├── exp_kinetics_column_manipulation.rs
│   │   ├── exp_kinetics_smooth_filter.rs
│   │   ├── LSQSplines2.rs
│   │   ├── ndarray_statistics.rs
│   │   ├── kinetic_methods.rs
│   │   ├── kinetic_methods2.rs
│   │   ├── kinetic_methods_tests.rs
│   │   ├── filters_smoothing_test.rs
│   │   ├── one_experiment_dataset_test.rs
│   │   ├── testing_mod.rs
│   │   ├── experiment_series/
│   │   │   ├── experiment_series_main.rs
│   │   │   ├── experiment_series_core.rs
│   │   │   ├── experiment_series_io.rs
│   │   │   ├── experiment_series_ops.rs
│   │   │   ├── experiment_series_analysis.rs
│   │   │   ├── experiment_series2.rs
│   │   │   ├── experiment_series3.rs
│   │   │   └── experiment_series_main_test.rs
│   │   └── kinetic_methods/
│   │       ├── mod.rs
│   │       ├── combined.rs
│   │       ├── Criado.rs
│   │       ├── Criado2.rs
│   │       ├── Friedman.rs
│   │       ├── integral_isoconversion.rs
│   │       ├── integral_isoconversion_tests.rs
│   │       ├── is_this_a_sublimation.rs
│   │       ├── isoconversion.rs
│   │       ├── kinetic_regression.rs
│   │       ├── Kissinger.rs
│   │       └── Vyazovkin.rs
│   │
│   ├── gui/                       # (was src/gui/experimental_kinetics_gui/)
│   │   ├── mod.rs                 # Module declarations
│   │   ├── experimental_kinetics_gui_main.rs
│   │   ├── model.rs
│   │   ├── view.rs
│   │   ├── controller.rs
│   │   ├── controller_buttons_and_panels.rs
│   │   ├── controller_filters.rs
│   │   ├── controller_golden_pipeline.rs
│   │   ├── controller_kinetics.rs
│   │   ├── controller_methods.rs
│   │   ├── controller_table.rs
│   │   ├── interaction.rs
│   │   ├── kitheplot_wrapper.rs
│   │   ├── model_handlers.rs
│   │   ├── settings.rs
│   │   ├── test_options.rs
│   │   ├── help_view.rs
│   │   └── gui_test.rs
│   │
│   ├── ivp_common.rs              # (was src/gui/ivp_common.rs)
│   └── solid_state_kinetics_IVP.rs # (was src/Kinetics/solid_state_kinetics_IVP.rs)
│
└── assets/                        # Copy from src/assets/
    ├── TGAexample.txt
    ├── help_eng.md
    ├── help_rus.md
    └── icon.png
```

---

## 3. Path Rewriting Rules

All `use crate::` paths referencing KiThe's internal structure must be rewritten:

| Original Path | New Path |
|---------------|----------|
| `crate::Kinetics::experimental_kinetics::...` | `crate::domain::...` |
| `crate::gui::experimental_kinetics_gui::...` | `crate::gui::...` |
| `crate::gui::ivp_common::...` | `crate::ivp_common::...` |
| `crate::Kinetics::solid_state_kinetics_IVP::...` | `crate::solid_state_kinetics_IVP::...` |

---

## 4. main.rs Design

The new `main.rs` will:

1. Use `eframe::run_native` to launch the `PlotApp` directly (no longer embedded in KiThe's main window)
2. Configure native options (window title, size, icon)
3. Initialize logging

```rust
fn main() -> Result<(), eframe::Error> {
    let native_options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default()
            .with_inner_size([1200.0, 800.0])
            .with_min_inner_size([600.0, 400.0])
            .with_icon(/* load icon */),
        ..Default::default()
    };
    
    eframe::run_native(
        "Experimental Kinetics - TGA Analysis",
        native_options,
        Box::new(|_cc| Ok(Box::new(PlotApp::new()))),
    )
}
```

---

## 5. Implementation Steps (in order)

### Step 1: Create directory structure
- Create all directories as outlined in section 2

### Step 2: Create Cargo.toml
- Copy all relevant dependencies from KiThe's Cargo.toml
- Set `name = "experimental_kinetics"`, `edition = "2024"`

### Step 3: Copy domain module files
- Copy all files from `src/Kinetics/experimental_kinetics/` to `src/domain/`
- Create `src/domain/mod.rs` with module declarations
- Create `src/domain/kinetic_methods/mod.rs` with sub-module declarations

### Step 4: Copy GUI module files
- Copy all files from `src/gui/experimental_kinetics_gui/` to `src/gui/`
- Create `src/gui/mod.rs` with module declarations

### Step 5: Copy additional dependencies
- Copy `src/gui/ivp_common.rs` to `src/ivp_common.rs`
- Copy `src/Kinetics/solid_state_kinetics_IVP.rs` to `src/solid_state_kinetics_IVP.rs`

### Step 6: Rewrite all `use crate::` paths
- Systematically replace all internal paths per the table in section 3

### Step 7: Create main.rs
- Implement the standalone eframe application entry point

### Step 8: Copy assets
- Copy `TGAexample.txt`, `help_eng.md`, `help_rus.md`, `icon.png` to `assets/`

### Step 9: Build and fix compilation errors
- Run `cargo build` and iteratively fix any issues

---

## 6. Potential Issues & Mitigations

| Issue | Mitigation |
|-------|------------|
| `#[path = "..."]` attributes in module declarations | Rewrite to use standard `mod` declarations with correct relative paths |
| `super::` references crossing module boundaries | Adjust to match new module hierarchy |
| `experiment_series_main.rs` uses `#[path]` for sub-modules | These sub-modules are already in the `experiment_series/` directory, so `mod experiment_series_core;` will work naturally |
| `controller_kinetics.rs` depends on `ivp_common` which depends on `solid_state_kinetics_IVP` | Both must be copied |
| `kitheplot_wrapper.rs` depends on external `kithe_plot` crate | Add `KiThePlot` (already in KiThe's deps as `kithe_plot = "0.5.0"`) — verify the exact crate name |
| Help viewer references `src/assets/help_*.md` | Update path to `assets/help_*.md` |
| `gui_test.rs` references `src/assets/TGAexample.txt` | Update path to `assets/TGAexample.txt` |