Skip to content

feat/fitting#6

Merged
Ameyanagi merged 8 commits into
mainfrom
feat/fitting
Feb 10, 2026
Merged

feat/fitting#6
Ameyanagi merged 8 commits into
mainfrom
feat/fitting

Conversation

@Ameyanagi
Copy link
Copy Markdown
Owner

  • Add FEFF85L fitting APIs with Rust runner and uv fixture workflow
  • Redesign FEFF fitting API and add xraylarch comparison outputs
  • feat(fitting): add FEFF batch parallel API and benchmark
  • refactor(fitting): simplify to joint/independent APIs with explicit batch strategy

Copilot AI review requested due to automatic review settings February 10, 2026 08:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new Rust-core FEFF85L fitting subsystem to xraytsubaki, including expression-driven parameterization, a consuming-self builder API, multi-dataset (joint) fitting, batch-parallel fitting utilities, and documentation/fixtures/scripts for parity comparisons against xraylarch.

Changes:

  • Introduces xafs::fitting module with FEFF85L path parsing (feffNNNN.dat), path2chi/ff2chi, expression evaluation + symbol extraction, transforms, and LM-based solver with joint + independent batch execution.
  • Adds a new ergonomic builder API (FeffFit, FeffFitDataset, Param) and re-exports via prelude, plus examples/benchmarks for batch fitting and larch comparison exports.
  • Adds xraylarch-generated fixtures and plot-generation scripts/workflow assets to validate/visualize parity.

Reviewed changes

Copilot reviewed 43 out of 100 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
py-xraytsubaki/src/lib.rs Adds fitting error categorization for Python bindings.
plans/fitting-api-redesign.md Design plan for the new builder API, expression engine, and multi-dataset fitting.
doc/scripts/generate_feff_vs_larch_png.py New script to render PNG comparisons from CSV parity outputs.
doc/scripts/generate_feff_vs_larch_plots.py New script to generate SVG plots + an index from CSV parity outputs.
doc/plots/feff_vs_larch_index.md Generated index of comparison plot outputs.
crates/xraytsubaki/tests/testfiles/feffcu01.dat Adds FEFF fixture path file for tests/parity.
crates/xraytsubaki/tests/testfiles/feff_path_chi_larch_ref.txt Adds larch reference output for path2chi parity.
crates/xraytsubaki/tests/testfiles/feff_fit_target_larch.txt Adds larch reference target curve for fitting parity.
crates/xraytsubaki/tests/testfiles/feff_ff2chi_larch_ref.txt Adds larch reference output for ff2chi parity.
crates/xraytsubaki/tests/testfiles/feff0002.dat Adds second FEFF fixture path file for multi-path tests/parity.
crates/xraytsubaki/tests/pythonscript/pyproject.toml Adds uv-managed Python project for regenerating larch fixtures.
crates/xraytsubaki/tests/pythonscript/generate_test.py Extends fixture generator to produce FEFF fitting parity references.
crates/xraytsubaki/tests/pythonscript/README.md Documents how to regenerate fixtures via uv.
crates/xraytsubaki/tests/pythonscript/.gitignore Ignores uv venv and Python cache.
crates/xraytsubaki/tests/feff_builder_examples.rs Adds integration-style tests demonstrating builder workflows and larch parity checks.
crates/xraytsubaki/src/xafs/mod.rs Registers xafs::fitting and wires FittingError into the top-level XAFSError.
crates/xraytsubaki/src/xafs/fitting/variables.rs Adds variable graph resolution + expression symbol extraction wrappers.
crates/xraytsubaki/src/xafs/fitting/transform.rs Adds R-space transform application, residual construction, and n_idp computation.
crates/xraytsubaki/src/xafs/fitting/solver.rs Implements joint solver + independent batch execution and associated tests.
crates/xraytsubaki/src/xafs/fitting/path_model.rs Implements FEFF path modeling (path2chi/ff2chi) and interpolation utilities.
crates/xraytsubaki/src/xafs/fitting/mod.rs Registers fitting submodules and provides public API façade functions/re-exports.
crates/xraytsubaki/src/xafs/fitting/feffdat.rs Implements FEFF85L feffNNNN.dat parsing + tests.
crates/xraytsubaki/src/xafs/fitting/expression.rs Adds pest-based expression parsing/evaluation + function/constant support + caching.
crates/xraytsubaki/src/xafs/fitting/expression.pest Defines the expression grammar (operators, calls, constants, identifiers).
crates/xraytsubaki/src/xafs/fitting/errors.rs Introduces FittingError enum for fitting/runner/expression/parser errors.
crates/xraytsubaki/src/xafs/fitting/builder.rs Adds FeffFit consuming builder, auto-discovery, and builder tests.
crates/xraytsubaki/src/prelude.rs Re-exports fitting APIs/types to the crate prelude.
crates/xraytsubaki/examples/feff_compare_export.rs Adds an example exporting parity CSVs for plotting and comparisons.
crates/xraytsubaki/doc/feff-fitting-mvp.md Documents MVP scope, boundaries, and migration guidance for fitting.
crates/xraytsubaki/benches/feff_fitting_batch_benchmark.rs Adds criterion benchmark for batch independent fitting performance.
crates/xraytsubaki/README.md Updates project README with FEFF fitting MVP feature bullets and scope notes.
crates/xraytsubaki/Cargo.toml Adds pest_derive dependency and registers new benchmark target.
Cargo.toml Adds pest_derive to workspace dependencies.
Cargo.lock Lockfile updates for new dependencies pulled in by expression parsing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/xraytsubaki/src/xafs/fitting/path_model.rs
Comment thread crates/xraytsubaki/src/xafs/fitting/variables.rs Outdated
Comment thread crates/xraytsubaki/src/xafs/fitting/transform.rs
Comment thread doc/scripts/generate_feff_vs_larch_png.py
Comment thread crates/xraytsubaki/src/xafs/fitting/builder.rs
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

Overview

This PR introduces a new FEFF fitting surface area, including a Rust-based FEFF runner, a redesigned fitting API (joint/independent), batch-parallel evaluation support with a benchmark, and fixtures/scripts for comparing outputs against xraylarch.

How it fits the codebase

The bulk of the change lands under crates/xraytsubaki/src/xafs/fitting/, adding/reshaping types, parsing (Pest grammar), builders, solvers, and the FEFF runner integration. Tests and golden reference artifacts in crates/xraytsubaki/tests/ validate core behavior and provide a Python/uv workflow for generating reference data. Documentation and plots under doc/ record comparison results and the intended API direction.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk.
  • Reviewed the new fitting/runner surface area and test fixtures; no definite functional or safety issues were identified that would fail at runtime or break public API behavior as implemented. Remaining observations were either already covered by tests or are non-blocking edge cases not exercised by current usage.
  • No files require special attention

Important Files Changed

Filename Overview
Cargo.lock Adds pest_derive and hashing/crypto transitive deps to lockfile; no issues found.
Cargo.toml Adds pest_derive to workspace dependencies; no issues found.
crates/xraytsubaki/Cargo.toml Adds pest_derive dependency and registers new fitting batch benchmark; no issues found.
crates/xraytsubaki/src/xafs/fitting/builder.rs Adds FEFF fitting builder API supporting joint/independent and batch strategies; no issues found.
crates/xraytsubaki/src/xafs/fitting/expression.pest Adds pest grammar for expression parsing; no issues found (minor latent ambiguity for identifier 'e' not exercised).
crates/xraytsubaki/src/xafs/fitting/runner.rs Added FEFF85L runner integration (exec, discovery, timeout); no issues found.
crates/xraytsubaki/src/xafs/fitting/solver.rs Added LM solver with joint/independent modes and batch strategies; no issues found.
crates/xraytsubaki/src/xafs/fitting/transform.rs Added FFT transform/windowing and residual computations for fitting; no issues found.
crates/xraytsubaki/src/xafs/fitting/types.rs Added core fitting types (datasets, strategies, results, serde); no issues found.
crates/xraytsubaki/tests/feff_builder_examples.rs Added integration tests covering builder, transforms, fitting workflows; no issues found.
py-xraytsubaki/src/lib.rs Exposed fitting module through Python bindings; no issues found.

Sequence Diagram

sequenceDiagram
    participant User as Caller
    participant Builder as FittingBuilder
    participant Model as PathModel/FitProblem
    participant Vars as FitVariables
    participant Runner as FeffRunner
    participant Solver as Solver (joint/independent)

    User->>Builder: configure dataset(s), paths, variables, transform
    Builder->>Model: build FitProblem (datasets + models)
    Model->>Vars: compile expressions / parameter mapping
    alt needs FEFF data
        Model->>Runner: run FEFF85L (batch/parallel)
        Runner-->>Model: feff.dat / chi(k) outputs
    end
    User->>Solver: solve(problem, strategy)
    Solver->>Model: evaluate residuals (k/R space)
    Model->>Vars: apply parameter vector (clamp bounds)
    Model-->>Solver: residuals + jacobian/metrics
    Solver-->>User: FitResult (best params, stats, exports)
Loading

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

This PR introduces a new FEFF fitting subsystem under crates/xraytsubaki/src/xafs/fitting/, including:

  • Parsing FEFF85L feffNNNN.dat files into FeffDat / FeffPathModel.
  • Modeling χ(k) via path2chi/ff2chi with expression-based parameters.
  • R-space transforms/residuals and a Levenberg–Marquardt joint-fit solver, plus an independent batch API with configurable rayon execution strategy.
  • A FEFF85L module runner that resolves sibling/PATH executables, stages feff.inp, captures logs, and discovers generated path files.

Key issues to address before merge are concentrated in the modeling/solver edge-cases: the interpolation helper can panic when fitting explores parameter regions that produce NaN in the computed q-grid, and expression evaluation currently can silently replace invalid (non-finite) values with defaults, masking real modeling errors. There is also a correctness concern in per-dataset reduced-χ² diagnostics for joint fits (using a global n_vary for every dataset) and a numerical-stability concern in the unscaled finite-difference Jacobian step that feeds covariance estimation.

Confidence Score: 3/5

  • This PR is not safe to merge until a crash-on-invalid-parameter-region bug in interpolation/expression handling is addressed.
  • Confidence reduced due to a demonstrated panic path (partial_cmp(...).unwrap()) reachable when fitting explores parameter regions that yield NaN q-values, plus silent non-finite expression fallbacks that can hide invalid models. Remaining concerns are diagnostics/numerical robustness rather than immediate runtime failures.
  • crates/xraytsubaki/src/xafs/fitting/path_model.rs, crates/xraytsubaki/src/xafs/fitting/variables.rs, crates/xraytsubaki/src/xafs/fitting/solver.rs

Important Files Changed

Filename Overview
crates/xraytsubaki/src/xafs/fitting/solver.rs Implements joint/independent FEFF fitting with LM solver; issues: interp_linear_clamped panic risk via ff2chi (see path_model), per-dataset DOF uses global n_vary, Jacobian step is unscaled (numerical stability).
crates/xraytsubaki/src/xafs/fitting/path_model.rs Adds FEFF path modeling (feffpath/path2chi/ff2chi) and interpolation; bug: partial_cmp(...).unwrap() can panic if computed q contains NaN (e.g., during fitting when parameters produce invalid energies).
crates/xraytsubaki/src/xafs/fitting/variables.rs Adds variable resolution and expression evaluation; bug: resolve_path_param silently replaces non-finite expression results with default values, hiding errors and corrupting fits.
crates/xraytsubaki/src/xafs/fitting/transform.rs Implements R-space transform and residual calculation with masking; appears consistent, no merge-blocking issues found.
crates/xraytsubaki/src/xafs/fitting/runner.rs Implements FEFF85L module runner with command resolution, staging, logs, and tests using mock executables; no merge-blocking issues found.
crates/xraytsubaki/src/xafs/fitting/types.rs Introduces core data types for FEFF fitting/run/batch options and result structs; no merge-blocking issues found.
crates/xraytsubaki/src/xafs/fitting/feffdat.rs Adds FEFF85L feffNNNN.dat parser with validation and tests; no merge-blocking issues found.
crates/xraytsubaki/src/xafs/fitting/mod.rs Wires fitting module exports and public API wrappers to xafs::Result/XAFSError; no merge-blocking issues found.

Sequence Diagram

sequenceDiagram
  autonumber
  actor Caller
  participant API as xafs::fitting (mod.rs)
  participant Solver as solver::feffit_joint
  participant Vars as FitVariables/expressions
  participant Model as path_model::ff2chi
  participant FFT as transform::apply_r_transform
  participant LM as LevenbergMarquardt

  Caller->>API: feffit_joint(datasets, vars)
  API->>Solver: feffit_joint(...)
  Solver->>Solver: validate_dataset (k/chi, paths, transform)
  Solver->>FFT: apply_r_transform(data chi)
  Solver->>LM: minimize(problem)
  loop iterations
    LM->>Solver: set_params(params)
    Solver->>Vars: apply_parameter_vector
    Solver->>Model: ff2chi(paths, vars, k)
    Model->>Vars: resolve_values / resolve_path_param
    Model-->>Solver: model chi + per-path chi
    Solver->>FFT: apply_r_transform(model chi)
    Solver-->>LM: residuals + jacobian
  end
  LM-->>Solver: solved problem
  Solver->>Model: ff2chi(paths, solved vars, k)
  Solver->>FFT: apply_r_transform(model + path chis)
  Solver-->>API: FeffFitResult (metrics + datasets)
  API-->>Caller: Result<FeffFitResult>
Loading

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

Comment thread crates/xraytsubaki/src/xafs/fitting/path_model.rs Outdated
Comment thread crates/xraytsubaki/src/xafs/fitting/variables.rs Outdated
Comment thread crates/xraytsubaki/src/xafs/fitting/solver.rs
Comment thread crates/xraytsubaki/src/xafs/fitting/solver.rs
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

This PR introduces a new FEFF85L fitting stack in crates/xraytsubaki/src/xafs/fitting/ (path parsing, path→χ computation, R-space transform/residual, and a Levenberg–Marquardt solver), along with batch execution strategies, benchmarks, examples, and comparison artifacts.

The core data flow is: parse FEFF .dat → build FeffPathModel → compute model χ(k) via ff2chi → transform to χ(R) via apply_r_transform → assemble residuals in R-space → solve joint or independent fits.

Must-fix before merge: the FEFF .dat parser currently uses byte-indexed slicing on &str (line[..64], line[64..], line[2..]), which can panic at runtime on non-ASCII/UTF-8 input instead of returning a FittingError. Additionally, interp_linear_clamped still contains an unwrap-on-NaN compare path (previously discussed) that can crash interpolation if a NaN reaches the search comparator.

Confidence Score: 3/5

  • This PR adds substantial new fitting functionality but contains a couple of confirmed panic paths in core parsing/interpolation that should be fixed before merging.
  • Score reduced due to verified runtime panics in feffdat.rs from unsafe UTF-8 string slicing, plus an interpolation comparator unwrap that can crash on NaN inputs; remaining changes appear broadly consistent with the intended fitting pipeline.
  • crates/xraytsubaki/src/xafs/fitting/feffdat.rs, crates/xraytsubaki/src/xafs/fitting/path_model.rs

Important Files Changed

Filename Overview
crates/xraytsubaki/src/xafs/fitting/feffdat.rs Implements FEFF85L .dat parser; current header detection uses unsafe UTF-8 string slicing that can panic on non-ASCII input.
crates/xraytsubaki/src/xafs/fitting/path_model.rs Adds FEFF path-to-chi computation and interpolation; interpolation uses partial_cmp(...).unwrap() which can panic on NaN inputs (previously reported) and should be guarded.
crates/xraytsubaki/src/xafs/fitting/solver.rs Introduces joint/independent Levenberg–Marquardt solver and batch strategies; main flow looks consistent, though per-dataset DOF and Jacobian step sizing concerns exist (already discussed in prior threads).
crates/xraytsubaki/src/xafs/fitting/transform.rs Adds R-space transform and residual computation with input validation; residual masking logic is guarded and errors on empty residual.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Solver as solver::feffit_joint
    participant Problem as FeffFitMultiProblem
    participant PathModel as path_model::ff2chi
    participant Transform as transform::apply_r_transform
    participant LM as LevenbergMarquardt

    Caller->>Solver: "feffit_joint(datasets, variables)"
    Solver->>Solver: "validate_dataset() for each"
    Solver->>Problem: "FeffFitMultiProblem::new(datasets, variables, varying_names)"
    loop "init data transforms"
        Problem->>Transform: "apply_r_transform(k, data_chi, transform)"
        Transform-->>Problem: "TransformOutput (data)"
    end
    Solver->>LM: "minimize(problem)"
    loop "LM iterations"
        LM->>Problem: "set_params(x)"
        LM->>Problem: "residuals() / jacobian()"
        Problem->>PathModel: "ff2chi(paths, vars, k)"
        PathModel-->>Problem: "model chi + per-path chi"
        Problem->>Transform: "apply_r_transform(k, model_chi, transform)"
        Transform-->>Problem: "TransformOutput (model)"
        Problem-->>LM: "residual vector"
    end
    LM-->>Solver: "solved problem + report"
    Solver->>Problem: "evaluate_models(solved.vars)"
    loop "per dataset output"
        Solver->>Transform: "residual_in_r_space(data_transform, model_transform)"
    end
    Solver-->>Caller: "FeffFitResult"
Loading

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment thread crates/xraytsubaki/src/xafs/fitting/feffdat.rs Outdated
Comment thread crates/xraytsubaki/src/xafs/fitting/feffdat.rs Outdated
Comment thread crates/xraytsubaki/src/xafs/fitting/path_model.rs Outdated
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

This PR introduces a new FEFF85L fitting stack: a FEFF runner that resolves and executes module binaries, a FEFF .dat parser, path-modeling (path2chi/ff2chi), R-space transform and residual computation, and a Levenberg–Marquardt solver supporting joint and independent batch fitting. The new APIs are wired under crates/xraytsubaki/src/xafs/fitting/* and include tests, examples, and documentation/plot outputs for comparison against Larch.

Overall, the core modeling/solver flow is coherent: datasets are validated, variables are resolved/applied, models are generated via ff2chi, transformed into R-space, and residuals are fed into LM; results are then post-processed into per-dataset diagnostics and optional covariance-derived stderr fields.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk.
  • Core fitting/runner pipeline is internally consistent, includes input validation for common failure modes (non-finite values, monotonic grids, empty masks), and has good unit/integration test coverage for modeling, solver behavior, and runner command resolution. Candidate panic/logic issues reviewed were not reachable given the current validation and data construction paths.
  • No files require special attention

Important Files Changed

Filename Overview
crates/xraytsubaki/src/xafs/fitting/path_model.rs Adds FEFF path modeling APIs (feffpath/path2chi/ff2chi), parameter resolution, and linear interpolation with input validation + tests.
crates/xraytsubaki/src/xafs/fitting/transform.rs Implements R-space transform pipeline, residual computation, N_idp calculation, and transform validation with unit tests.
crates/xraytsubaki/examples/feff_compare_export.rs New example exporting FEFF vs Larch comparison outputs; large but self-contained.
crates/xraytsubaki/src/xafs/fitting/builder.rs Introduces/updates fluent builders for datasets/paths/variables; integrates with solver types.
crates/xraytsubaki/src/xafs/fitting/errors.rs Defines fitting/runner error types used across modules; consistent error mapping.
crates/xraytsubaki/src/xafs/fitting/expression.rs Implements expression parsing/evaluation and symbol extraction; used by variables/path params.
crates/xraytsubaki/src/xafs/fitting/feffdat.rs Implements FEFF85L .dat parser with monotonic/finite validation; includes string slicing on title/header lines but likely safe for ASCII FEFF output.
crates/xraytsubaki/src/xafs/fitting/runner.rs Adds FEFF module runner, command resolution, workspace staging, and path discovery with tests.
crates/xraytsubaki/src/xafs/fitting/solver.rs Adds LM-based joint and independent batch solver, per-dataset diagnostics, and covariance stderr computation.
crates/xraytsubaki/src/xafs/fitting/types.rs Defines core fitting/runner data types and defaults used across modules.
crates/xraytsubaki/src/xafs/fitting/variables.rs Implements variable resolution (including expression dependencies) and parameter vector mapping for solver.
crates/xraytsubaki/tests/feff_builder_examples.rs Adds integration-style tests demonstrating builder usage and end-to-end fitting.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Solver as feffit_joint (solver.rs)
    participant Vars as FitVariables (variables.rs)
    participant Model as ff2chi/path2chi (path_model.rs)
    participant Xform as apply_r_transform (transform.rs)
    participant LM as LevenbergMarquardt

    Client->>Solver: feffit_joint(datasets, variables)
    Solver->>Solver: validate_dataset() + validate_transform()
    Solver->>Vars: varying_names()
    Solver->>LM: minimize(FeffFitMultiProblem)
    loop per iteration
        LM->>Solver: residuals()/jacobian()
        Solver->>Vars: apply_parameter_vector()
        Solver->>Model: ff2chi(paths, vars, k)
        Model->>Vars: resolve_values() + resolve_params()
        Model-->>Solver: model chi + per-path chi
        Solver->>Xform: apply_r_transform(k, chi, transform)
        Xform-->>Solver: TransformOutput
        Solver->>Solver: residual_in_r_space(data, model, epsilon_k)
        Solver-->>LM: residual vector
    end
    LM-->>Solver: solved problem
    Solver-->>Client: FeffFitResult (variables + datasets)
Loading

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

This PR adds a new xafs::fitting module implementing FEFF85L path parsing/modeling (feffdat, path_model), a pest-based expression system for variable/path parameter expressions, an LM-based joint solver with independent batch execution, plus a builder-style API (FeffFit) and a Rust-based FEFF module runner that stages feff.inp, resolves module executables, and collects feffNNNN.dat outputs.

The new APIs are wired into the crate’s public surface via crates/xraytsubaki/src/xafs/fitting/mod.rs and crates/xraytsubaki/src/xafs/mod.rs (including XAFSError::Fitting). Tests/fixtures and documentation/plots are added to validate comparisons vs. Larch and to support a UV-based Python fixture workflow.

One blocking issue was found in the FEFF runner: it waits for module exit before draining stdout/stderr, which can deadlock on pipe backpressure for verbose modules.

Confidence Score: 3/5

  • This PR is close to mergeable but has a runner correctness issue that can hang executions for verbose FEFF modules.
  • Core fitting/modeling logic is well-validated with extensive tests and explicit finite/monotonic checks. However, the FEFF runner currently risks a stdout/stderr pipe deadlock by waiting for child exit before draining output, which can block real executions.
  • crates/xraytsubaki/src/xafs/fitting/runner.rs

Important Files Changed

Filename Overview
crates/xraytsubaki/src/xafs/fitting/builder.rs Adds builder-style FeffFit API with dataset defaults and auto-discovery of variables from path expressions; no blocking issues found.
crates/xraytsubaki/src/xafs/fitting/runner.rs Adds Rust FEFF runner that stages feff.inp, resolves sibling/PATH modules, executes them with optional timeout, and collects feffNNNN.dat outputs; contains a likely deadlock risk when reading stdout/stderr after wait.
crates/xraytsubaki/src/xafs/fitting/solver.rs Adds LM-based joint solver plus independent batch runner, per-dataset diagnostics, and Jacobian/covariance estimation; overall sound but see runner-style residual penalty handling and potential silent penalties.
crates/xraytsubaki/src/xafs/fitting/expression.rs Implements cached pest-based expression parser/evaluator with symbol extraction and finite-result enforcement; no definite issues found.
crates/xraytsubaki/src/xafs/fitting/path_model.rs Implements FEFF path modeling (ff2chi/path2chi), parameter resolution, and safe interpolation with finite checks; previous NaN/UTF-8 panic paths appear addressed.
crates/xraytsubaki/src/xafs/fitting/feffdat.rs Adds FEFF85L .dat parser with UTF-8-safe splitting and monotonic/finite validation; no definite issues found.
crates/xraytsubaki/src/xafs/fitting/transform.rs Adds R-space transform utilities and residual computation with masking and configuration validation; no definite issues found.
crates/xraytsubaki/src/xafs/fitting/types.rs Adds core fitting types (datasets, variables, params, results, batch options, runner request/result) used across the new fitting module; no definite issues found.
crates/xraytsubaki/src/xafs/fitting/variables.rs Adds FitVariables parameter vector mapping, expression-graph resolution with cycle detection, and strict non-finite rejection for resolved path params; no definite issues found.
crates/xraytsubaki/src/xafs/fitting/mod.rs Wires the new fitting module and re-exports APIs; no definite issues found.
crates/xraytsubaki/src/xafs/mod.rs Exposes fitting module and integrates FittingError into the top-level XAFSError enum; no definite issues found.

Sequence Diagram

sequenceDiagram
    autonumber
    participant U as User/Caller
    participant B as FeffFit (builder)
    participant S as solver::feffit_joint
    participant P as path_model::ff2chi
    participant T as transform::apply_r_transform
    participant LM as LevenbergMarquardt

    U->>B: FeffFit::new()
    U->>B: data()/add_path()/set_inits()/...
    U->>B: fit()
    B->>B: collect_default_dataset()
    B->>B: auto_discover_variables()
    B->>S: feffit_joint(datasets, vars)
    S->>S: validate_dataset() for each dataset
    S->>T: apply_r_transform(data chi)
    S->>LM: minimize(FeffFitMultiProblem)
    loop iterations
        LM->>S: residuals()/jacobian()
        S->>P: ff2chi(paths, vars, k)
        P-->>S: model chi + per-path chi
        S->>T: apply_r_transform(model chi)
        S-->>LM: residual vector
    end
    S-->>B: FeffFitResult + per-dataset diagnostics
    B-->>U: FeffFitResult (with warnings)
Loading

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

11 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +100 to +103
(Ok(result), Ok(())) => Ok(result),
(Err(run_err), Ok(())) => Err(run_err),
(Ok(_), Err(restore_err)) => Err(restore_err),
(Err(run_err), Err(_restore_err)) => Err(run_err),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Child process I/O deadlock
run_single_module waits for the child to exit (wait_for_exit) before draining stdout/stderr. If a FEFF module writes enough output to fill the pipe buffer, the child can block on write and never exit, and the parent will spin until timeout (or forever if no timeout), even though the process is still running. This is reproducible when a module is chatty.

Fix by reading stdout/stderr concurrently while the child runs (e.g., spawn threads to read_to_end and then wait, or use wait_with_output).

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/xraytsubaki/src/xafs/fitting/runner.rs
Line: 100:103

Comment:
**Child process I/O deadlock**
`run_single_module` waits for the child to exit (`wait_for_exit`) *before* draining `stdout`/`stderr`. If a FEFF module writes enough output to fill the pipe buffer, the child can block on write and never exit, and the parent will spin until timeout (or forever if no timeout), even though the process is still running. This is reproducible when a module is chatty.

Fix by reading stdout/stderr concurrently while the child runs (e.g., spawn threads to `read_to_end` and then `wait`, or use `wait_with_output`).

How can I resolve this? If you propose a fix, please make it concise.

@Ameyanagi Ameyanagi merged commit 2d60f95 into main Feb 10, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants