Skip to content

Solver and penalties update#22

Merged
JR-1991 merged 8 commits into
masterfrom
solver-and-penalities-update
Aug 20, 2025
Merged

Solver and penalties update#22
JR-1991 merged 8 commits into
masterfrom
solver-and-penalities-update

Conversation

@JR-1991

@JR-1991 JR-1991 commented Aug 20, 2025

Copy link
Copy Markdown
Owner

This pull request introduces several improvements and new features to the neural modeling codebase, focusing on solver flexibility, penalty extensions, and model configuration. The most significant changes are the refactoring of solver handling to allow dynamic selection and configuration, the addition of a null space penalty for stoichiometric matrices, and enhancements to model initialization and plotting.

Solver Flexibility and API Improvements

  • Refactored all neural model classes (NeuralBase, NeuralODE, RateFlowODE, UniversalODE) to allow passing the solver type and its configuration (rtol, atol, dt0) dynamically, both in __call__ and predict methods. This enables users to select and configure ODE solvers at runtime, improving flexibility and usability. Helper methods _create_controller and _instantiate_solver were added for controller and solver instantiation. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

Penalty Extensions for Stoichiometric Matrices

  • Added a new penalty function penalize_null_space to encourage the stoichiometric matrix to have a null space of zero, which can be used to enforce physical constraints (e.g., conservation laws). Integrated this penalty into the for_rateflow penalty collection, allowing it to be configured via null_space_alpha. [1] [2] [3] [4]

Model Initialization and Configuration

  • Improved RateFlowODE initialization to accept and validate a mass_constraint matrix, ensuring it matches the species count and enforcing shape requirements. This supports more robust modeling of conservation laws. Also, model fields now use equinox field annotations for better static/dynamic parameter handling. [1] [2] [3]

Plotting Enhancements

  • Enhanced rate plotting in plot_learned_rates by improving grid display (major and minor ticks) and updating the right panel to overlay model predictions with measurements for clearer visualization.

Miscellaneous

  • Added a new test script test.py demonstrating model creation, neural ODE instantiation, and parameter flattening for debugging or analysis.

Updated NeuralBase and derived classes to allow passing custom solver types and stepsize controllers, improving flexibility for ODE integration. Added support for runtime selection of solver, rtol, atol, and dt0 parameters. Refactored __call__ methods and internal helper functions to instantiate solvers and controllers as needed.
Introduces a new penalty function, penalize_null_space, to encourage the stoichiometric matrix to have a null space of zero. Integrates this penalty into the Penalties class for NeuralRDE models, allowing configuration via null_space_alpha.
Enhanced grid styling for the rate magnitude subplot by enabling both major and minor grids with custom appearance. Updated the model fit plotting to include predicted measurements for better comparison.
@JR-1991 JR-1991 requested a review from Copilot August 20, 2025 20:56
@JR-1991 JR-1991 self-assigned this Aug 20, 2025
@JR-1991 JR-1991 added the enhancement New feature or request label Aug 20, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces significant improvements to the neural modeling framework by adding solver flexibility, penalty extensions, and model configuration enhancements. The main focus is on enabling dynamic solver selection and configuration at runtime, along with new penalty functions for stoichiometric matrices.

  • Refactored all neural model classes to support dynamic solver configuration (type, rtol, atol, dt0) in both __call__ and predict methods
  • Added null space penalty for stoichiometric matrices to enforce conservation laws
  • Enhanced RateFlowODE initialization with mass constraint validation and equinox field annotations

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test.py Simple test script demonstrating model creation and parameter flattening
catalax/neural/neuralbase.py Core refactoring for solver flexibility with helper methods and abstract interface
catalax/neural/neuralode.py Updated to use new dynamic solver configuration system
catalax/neural/rateflow.py Enhanced with mass constraint validation, equinox fields, and solver flexibility
catalax/neural/universalode.py Updated to support dynamic solver configuration
catalax/neural/penalties/stoich_mat.py Added new null space penalty function
catalax/neural/penalties/penalties.py Integrated null space penalty into penalty collection
catalax/neural/plots/rateflow.py Improved grid display and model prediction overlay

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread catalax/neural/penalties/stoich_mat.py
Comment thread catalax/neural/rateflow.py
Comment thread catalax/neural/neuralbase.py Outdated
Comment thread catalax/neural/rateflow.py Outdated
@JR-1991 JR-1991 changed the title Solver and penalities update Solver and penalties update Aug 20, 2025
JR-1991 and others added 5 commits August 20, 2025 23:01
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
The default value for the reaction_size field in RateFlowODE was removed, making it a required argument. This change clarifies the initialization requirements for the class.
@JR-1991 JR-1991 merged commit f6161ee into master Aug 20, 2025
4 checks passed
@JR-1991 JR-1991 deleted the solver-and-penalities-update branch August 20, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants