Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
8f7c35c
improvements to the solver suggested by copilot and temp files
MaxBalmus Oct 22, 2025
ce6adfe
finished recommended changes
MaxBalmus Oct 22, 2025
b7b6f65
Fix NumPy deprecation and runtime warnings
MaxBalmus Oct 22, 2025
084708b
Optimize performance: restore ~25% speed improvement
MaxBalmus Oct 22, 2025
1105085
Fix NumPy 2.0 compatibility: replace np.NaN with np.nan
MaxBalmus Oct 22, 2025
e3bd3dd
removed pandera
MaxBalmus Oct 22, 2025
d17cedb
pre allocating arrays in solver
MaxBalmus Oct 22, 2025
e5cea69
Optimize Solver performance with multiple improvements
MaxBalmus Oct 22, 2025
0b0a635
removed redundant variables
MaxBalmus Oct 22, 2025
f151679
remove redundat imports from solver
MaxBalmus Oct 22, 2025
bf55c73
perf: optimize hot path with pre-computed function-index pairs
MaxBalmus Oct 22, 2025
b0edf60
refactor: eliminate redundant operations and improve code consistency
MaxBalmus Oct 22, 2025
58a5e5d
update solver
MaxBalmus Oct 22, 2025
4e66bac
refactor: Major Components redesign for consistency and efficiency
MaxBalmus Oct 22, 2025
db1182f
perf: Add Numba JIT compilation to HelperRoutines functions
MaxBalmus Oct 22, 2025
b7ae56c
perf: optimize HelperRoutines functions by removing redundant parameters
MaxBalmus Oct 22, 2025
a3fcab9
small changes
MaxBalmus Oct 23, 2025
d129138
added signatures to njit in helperroutines
MaxBalmus Oct 23, 2025
0e723e1
small chages
MaxBalmus Oct 23, 2025
1c964d8
added profiling
MaxBalmus Oct 23, 2025
3ae92e9
added .prof files to .gitignore
MaxBalmus Oct 23, 2025
bf14ed0
perf: optimize ComponentFunctionFactory with functools.partial and pr…
MaxBalmus Oct 23, 2025
4304639
perf: optimize derivative computation in Solver with vectorized input…
MaxBalmus Oct 23, 2025
6d20d6d
refactor: optimize ComponentFactories to use partial and eliminate co…
MaxBalmus Oct 23, 2025
f1ae4ff
feat: add numba-optimized elastance functions to HelperRoutines
MaxBalmus Oct 23, 2025
87973fe
refactor: simplify heart chamber components to use consolidated facto…
MaxBalmus Oct 23, 2025
a7b2743
chore: reorganize benchmark files and add factory performance tests
MaxBalmus Oct 23, 2025
c4bddaf
perf: optimize and deduplicate activation functions with numba JIT
MaxBalmus Oct 23, 2025
67f90fe
refactor: remove redundant chamber law functions
MaxBalmus Oct 23, 2025
c8fa3a7
refactor: clean up unnecessary function imports
MaxBalmus Oct 23, 2025
52e0a09
rename some of the solver variables so it is more readable
MaxBalmus Oct 23, 2025
40295ce
added njit dec to activation function generator
MaxBalmus Oct 24, 2025
277cc2d
work in progress, need to figure out the valves
MaxBalmus Oct 24, 2025
6f12185
update test for the accuracy to be more consistent with the simulatio…
MaxBalmus Oct 27, 2025
1cdec22
update func description simple_bernoulli_diode_flow
MaxBalmus Oct 27, 2025
50d20ec
updated helper and factory libs
MaxBalmus Oct 27, 2025
1bb05c1
feat: add Cython build infrastructure for performance optimization
MaxBalmus Oct 27, 2025
9177a6f
perf: add optimized component factories for Cython backend
MaxBalmus Oct 27, 2025
7015174
refactor: migrate components to use auto-selected factories
MaxBalmus Oct 27, 2025
648ba02
docs: add Cython installation guide and verification script
MaxBalmus Oct 27, 2025
50c4bc7
fix(Solver): prevent array mutation in batch derivative computation
MaxBalmus Oct 27, 2025
a55d347
chore: remove old HelperRoutines.py from root
MaxBalmus Oct 27, 2025
494532d
feat: add Cython detection to main package init
MaxBalmus Oct 27, 2025
ee9ec3f
added cython compilation outputs
MaxBalmus Oct 27, 2025
946f916
added option for cythonic gen_total_dpdt_fixed to be used
MaxBalmus Oct 27, 2025
f110170
fixed bug for cython time_shift function
MaxBalmus Oct 27, 2025
35a82be
new function for comparing the helperroutines
MaxBalmus Oct 27, 2025
89fdecf
added time shifter class to generate the time shift more efficiently
MaxBalmus Oct 28, 2025
01375c3
made the code a bit more efficient with a function iterator
MaxBalmus Oct 28, 2025
5ddec32
settled on a working version
MaxBalmus Oct 28, 2025
5411f2d
update workflows
MaxBalmus Oct 28, 2025
d9c7f8b
update workflows 2nd attempt
MaxBalmus Oct 28, 2025
9ef428a
update workflows 3rd attempt
MaxBalmus Oct 28, 2025
c838113
attempting to fix address issue
MaxBalmus Oct 28, 2025
4bd1690
update workflows 4th attempt
MaxBalmus Oct 28, 2025
93e2721
update workflows 5th attempt
MaxBalmus Oct 28, 2025
af92bc9
Update src/ModularCirc/Components/_ComponentFactoriesOptimized.py
MaxBalmus Oct 28, 2025
d4610db
Update sandbox/profiling/quick_profile.py
MaxBalmus Oct 28, 2025
cce37eb
Update sandbox/profiling/benchmark_factories.py
MaxBalmus Oct 28, 2025
55897c4
Update sandbox/profiling/benchmark_numba.py
MaxBalmus Oct 28, 2025
0f1bd40
update workflows 6th attempt
MaxBalmus Oct 28, 2025
95f9020
Merge branch '10-list-comprehension-bottleneck-sandbox' of https://gi…
MaxBalmus Oct 28, 2025
a2df2ea
update workflows 7th attempt
MaxBalmus Oct 28, 2025
881b8fa
temp check
MaxBalmus Oct 28, 2025
166c4e6
Changes Made:
MaxBalmus Oct 28, 2025
7a2c28c
Fix CI: Automate Cython build and improve portability
MaxBalmus Oct 28, 2025
cc6d7f2
Consolidate setup configuration into pyproject.toml
MaxBalmus Oct 28, 2025
48de1fe
temp checks
MaxBalmus Oct 28, 2025
87fb12c
temp
MaxBalmus Oct 28, 2025
d9376a0
temp
MaxBalmus Oct 28, 2025
582f55f
temp
MaxBalmus Oct 28, 2025
3c1c10a
temp change ci
MaxBalmus Oct 28, 2025
7f3eff0
small changes
MaxBalmus Oct 28, 2025
e0c21e6
temp
MaxBalmus Oct 28, 2025
f6af5a8
temp
MaxBalmus Oct 28, 2025
fffa69d
revert
MaxBalmus Oct 28, 2025
fd3086d
temp
MaxBalmus Oct 28, 2025
134ba7f
change test to lv
MaxBalmus Oct 28, 2025
6c66625
inspect activation function again
MaxBalmus Oct 28, 2025
5a63ddb
test
MaxBalmus Oct 28, 2025
cf50261
corrected
MaxBalmus Oct 28, 2025
cc63a2b
more loggs
MaxBalmus Oct 28, 2025
6867264
found fix
MaxBalmus Oct 28, 2025
930b2ba
revert to normal tests
MaxBalmus Oct 29, 2025
94e571a
Update src/ModularCirc/HelperRoutines/__init__.py
MaxBalmus Oct 29, 2025
9e695f4
remove redundant `ls` comand
MaxBalmus Oct 29, 2025
6912fb3
simplify build_cython.sh
MaxBalmus Oct 29, 2025
2bdc586
Merge branch '10-list-comprehension-bottleneck-sandbox' of https://gi…
MaxBalmus Oct 29, 2025
7a6facc
update CYTHON_README.md
MaxBalmus Oct 29, 2025
9b87961
feat: add flexible Cython/Numba installation with runtime control
MaxBalmus Oct 29, 2025
f177dc4
Merge dev branch into 10-list-comprehension-bottleneck-sandbox
MaxBalmus Oct 30, 2025
23c6e07
Merge branch '10-list-comprehension-bottleneck-sandbox' of https://gi…
MaxBalmus Oct 30, 2025
c833bde
Added `setuptools` to the `pyproject.toml` list of dependencies.
MaxBalmus Nov 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install setuptools --upgrade
pip install ./
pip install cython numpy # Install build dependencies first
pip install -e .

- name: Verify installation
run: |
python verify_installation.py

- name: Run tests
run: |
Expand Down Expand Up @@ -77,9 +82,15 @@ jobs:
run: |
source venv/bin/activate
python -m pip install --upgrade pip
pip install ./
pip install cython numpy # Install build dependencies first
pip install -e .

- name: Verify installation
run: |
source venv/bin/activate
python verify_installation.py

- name: Run tests
run: |
source venv/bin/activate
python -m unittest discover -s tests
python -m unittest discover -s tests
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,12 @@ cython_debug/

# Ignore vscode settings
.vscode/

*prof

# Add to .gitignore
*.c
*.html
*.so
*.pyd
build/
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ pre-commit install # this will install a pre-commit hook into the git repo
You can also/alternatively run `pre-commit run` (changes only) or
`pre-commit run --all-files` to check even without installing the hook.

## Building Cython Extensions (Optional but Recommended)

For optimal performance during development, build the Cython extensions:

```bash
# Install Cython if not already installed
pip install cython

# Build the extensions
./build_cython.sh

# Or manually
python setup_cython.py build_ext --inplace
```

**When to rebuild:**
- After modifying any `.pyx` files (e.g., `src/ModularCirc/HelperRoutines.pyx`)
- After pulling changes that include `.pyx` modifications
- When switching between branches with different Cython code

# Testing

This repo uses `unittest` for testing. You can run locally the tests by running the following command:
Expand Down
112 changes: 112 additions & 0 deletions CYTHON_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Cythonizing ModularCirc HelperRoutines

This directory contains a Cythonized version of the `HelperRoutines` module for improved performance.

## Quick Start (Automatic Build)

**The Cython extension is now built automatically during package installation!**

```bash
# Install with Cython support (requires cython and numpy)
pip install cython numpy
pip install -e .
```

The setup will automatically detect Cython and build the extension. If Cython is not available or the build fails, the package will fall back to the Numba implementation.

## Manual Build (Development)

For quick rebuilds during development without reinstalling the entire package:

```bash
bash build_cython.sh
Copy link
Collaborator

Choose a reason for hiding this comment

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

this requires setuptools, which don't get installed.

Add setuptools to pyproject.toml in dependencies.

```

Or manually:

```bash
python setup_cython.py build_ext --inplace
Copy link
Collaborator

Choose a reason for hiding this comment

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

can't find setup_cython.py. Am I right? Or am I blind?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's no longer relevant, everything was moved to setup.py.

```

## Prerequisites

- Python >= 3.10
- Cython >= 3.0
- NumPy >= 1.20
- C compiler (gcc, clang, or MSVC)

## Using the Cython Module

### Option 1: Automatic (Recommended)

Add this at the top of the script:
```python
# Try to import Cython version, fall back to Numba if unavailable
try:
from ModularCirc.HelperRoutines import resistor_model_flow
print("Using Cythonized HelperRoutines")
except ImportError:
# Fall back to current Numba implementation
pass
```

### Option 2: Manual Import (where function hasn't been cythonised)

In any module that uses HelperRoutines:

```python
try:
from ModularCirc.HelperRoutines.HelperRoutinesCython import resistor_model_flow, chamber_volume_rate_change
except ImportError:
from ModularCirc.HelperRoutines.HelperRoutines import resistor_model_flow, chamber_volume_rate_change
```

## Performance Benefits

Cython provides:
- **No JIT compilation overhead**: Functions are pre-compiled to machine code
- **Faster setup time**: No Numba compilation delays on first run
- **C-level performance**: Direct C math library calls (`sqrt`, `exp`, `log`, etc.)
- **Type safety**: Compile-time type checking
- **GIL release**: Many functions use `nogil` for better multi-threading potential

Expected improvements:
- **Setup time**: Near-instant (no JIT compilation)
- **Runtime**: Comparable to or faster than Numba (0-15% improvement typical)
- **Memory**: Slightly lower memory footprint

## Debugging

If compilation fails, check:

1. **Compiler availability**: Ensure you have a C compiler (gcc, clang, or MSVC)
2. **NumPy headers**: Make sure NumPy is installed: `pip install numpy`
3. **Cython version**: Use Cython >= 0.29: `pip install --upgrade cython`

View detailed annotation (optimization opportunities):

```bash
# After building, check the generated HTML file
open src/ModularCirc/HelperRoutines.html
Copy link
Collaborator

Choose a reason for hiding this comment

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

open src/ModularCirc/HelperRoutines/HelperRoutines.html

```

## Cleanup

To remove compiled artifacts:

```bash
# Remove compiled extensions
rm -f src/ModularCirc/HelperRoutines/HelperRoutinesCython*.so
rm -f src/ModularCirc/HelperRoutines/HelperRoutinesCython*.pyd
rm -f src/ModularCirc/HelperRoutines/HelperRoutines.c

# Remove build artifacts
rm -rf build/
```

## Notes

- The `.pyx` file maintains API compatibility with the original `.py` file
- All Numba `@nb.njit` decorators are replaced with Cython equivalents
- Type annotations use Cython's static typing for maximum performance
- Functions marked `nogil` can run without the Python GIL, enabling true parallelism
132 changes: 132 additions & 0 deletions INSTALLATION_OPTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# ModularCirc Installation Options

ModularCirc supports two performance backends for the `HelperRoutines` module:

1. **Cython** (C-compiled, faster startup, recommended for production)
2. **Numba** (JIT-compiled, slower startup, easier development)

## Installation Methods

### Option 1: Install with Cython (Recommended)

For best performance with faster startup times:

```bash
# Install with Cython dependencies
pip install -e .[performance]

# Build the Cython extension
python setup.py build_ext --inplace
```

### Option 2: Install with Numba only

For development or if you encounter Cython build issues:

```bash
# Set environment variable to skip Cython build
export MODULARCIRC_USE_CYTHON=0

# Install package
pip install -e .
```

Or install normally - if Cython is not available, it will automatically fall back to Numba.

## Runtime Configuration

You can control which implementation is used at runtime with environment variables:

### Force Numba implementation

Even if Cython is installed, you can force the use of Numba:

```bash
export MODULARCIRC_FORCE_NUMBA=1
python your_script.py
```

Or in Python:

```python
import os
os.environ['MODULARCIRC_FORCE_NUMBA'] = '1'

import ModularCirc # Will use Numba
```

### Enable verbose output

To see which implementation is being used:

```bash
export MODULARCIRC_VERBOSE=1
python your_script.py
```

Or:

```python
import os
os.environ['MODULARCIRC_VERBOSE'] = '1'

import ModularCirc
# Will print: "✓ Using Cythonized HelperRoutines" or "⚠ Using Numba HelperRoutines"
```

### Check which implementation is active

In your Python code:

```python
from ModularCirc.HelperRoutines import USING_CYTHON

if USING_CYTHON:
print("Using fast Cython implementation")
else:
print("Using Numba implementation")
```

## Environment Variables Summary

| Variable | Values | Default | Description |
|----------|--------|---------|-------------|
| `MODULARCIRC_USE_CYTHON` | 0 or 1 | 1 | Controls whether to build Cython extension during installation |
| `MODULARCIRC_FORCE_NUMBA` | 0 or 1 | 0 | Forces use of Numba implementation at runtime |
| `MODULARCIRC_VERBOSE` | 0 or 1 | 0 | Enables verbose output about which implementation is used |

## Performance Comparison

- **Cython**: No JIT compilation overhead, faster startup (~2-3x faster first run)
- **Numba**: JIT compilation on first use, slightly slower startup but similar runtime performance

For production use or when running many small simulations, Cython is recommended.
For development or when C compiler is not available, Numba is a good fallback.

## Troubleshooting

### Cython build fails

If you encounter errors building the Cython extension:

```bash
# Disable Cython and use Numba
export MODULARCIRC_USE_CYTHON=0
pip install -e .
```

### Want to rebuild Cython extension

```bash
# Clean and rebuild
python setup.py clean --all
python setup.py build_ext --inplace
```

### Verify installation

```bash
python verify_installation.py
```

This will show which implementation is active and available.
7 changes: 7 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include README.md
include LICENSE
include CONTRIBUTING.md
include pyproject.toml
include setup.py
recursive-include src/ModularCirc *.pyx *.pxd
recursive-include src/ModularCirc *.py
Loading
Loading