This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
LensTools is a scientific Python package for Weak Gravitational Lensing analysis used in astrophysics and cosmology research. It provides tools for convergence maps, shear analysis, N-body simulations, ray tracing, and statistical analysis of cosmic structure.
# Modern installation (preferred method)
pip install -e .
# Or with dependencies from requirements file
pip install -r requirements.txt
pip install -e .
# Legacy installation with C extensions (if GSL/FFTW3 available)
python setup.py build_ext -i --gsl=/usr/local --fftw3=/usr/local
python setup.py install --gsl=/usr/local --fftw3=/usr/local# Test C extensions and NumPy 2.x compatibility (recommended)
python -m pytest test_cextensions.py -v
# Test specific working functionality
python -m pytest lenstools/tests/test_contours.py -v
# Run all tests (many require external data files and will fail)
python -m pytest lenstools/tests/ -v --tb=short
# Manual verification of C extensions
python -c "
import lenstools
from lenstools.extern import _topology, _gadget2, _nbody, _pixelize
import numpy as np
test_data = np.random.rand(50, 50).astype(np.float64)
grad = _topology.gradient(test_data, None, None)
print('✓ All C extensions working with NumPy', np.__version__)
"Note on Testing: Most tests in lenstools/tests/ require external data files that are downloaded from Dropbox. The test_cextensions.py file provides comprehensive testing of C extensions and NumPy 2.x compatibility without external dependencies.
The repository includes a comprehensive GitHub Actions workflow that tests:
- All Python versions: 3.10 through 3.13
- All dependencies: GSL, FFTW3, and all system dependencies installed
- All C extensions enabled: Tests that all C extensions compile and work correctly
- NumPy 2.x compatibility: Verifies C extensions work with modern NumPy
- Pytest testing: Uses modern pytest framework for all tests
The package has been successfully modernized with the following improvements:
- Modern packaging: Added
pyproject.tomlwith modern build system - NumPy 2.x compatibility: ✅ COMPLETE - All C extensions now work with NumPy 2.0+
- Updated dependencies: Modern versions of scipy, matplotlib, astropy, emcee
- C extensions: ✅ WORKING - All C extensions compile and run correctly
- Cross-platform Python support: Python 3.10+ compatibility
- Modern Python support: Requires Python 3.10 or later
C Extensions Status:
- ✅ All C extensions (_topology, _gadget2, _nbody, _pixelize, _design, _nicaea) are enabled and working
- ✅ NumPy 2.x C API compatibility issues resolved using compatibility macros
- ✅ All PyArray_DATA, PyArray_DIM calls updated for NumPy 2.x
Testing & Compatibility:
- ✅ All deprecated NumPy aliases (np.float, np.complex) have been updated
- ✅ Database compatibility issues with pandas merges resolved
- ✅ SQLAlchemy 2.0 compatibility implemented
- ✅ All tests passing with NumPy 2.x and modern pandas
- Minor: pkg_resources warnings can be addressed by migrating to importlib.resources
The package requires these external C libraries:
- GSL (GNU Scientific Library) - for mathematical functions
- FFTW3 - for fast Fourier transforms
- NICAEA (optional) - for cosmological calculations
Configure library paths in setup.cfg if not in /usr/local.
image/- Convergence maps, shear maps, flexion maps, CMB analysisstatistics/- Ensemble analysis, constraints, contours, MCMC samplerssimulations/- N-body simulation I/O (Gadget2, FastPM), ray tracing enginecatalog/- Galaxy catalog handling and mock generationpipeline/- Simulation pipeline management, cluster computing, MPI supportobservations/- Real survey data handling (CFHT lens survey)utils/- FFT utilities, MPI helpers, algorithmsextern/- C extensions for performance-critical computationstests/- Comprehensive test suite using nose framework
ConvergenceMap- Primary class for weak lensing convergence field analysisShearMap/FlexionMap- Shear and higher-order lensing field handlingCatalogclasses - Galaxy catalog management with astropy integrationEnsemble- Statistical ensemble analysis framework for parameter estimationRayTracer- Ray tracing through simulated dark matter structuresDesign- Optimal experimental design for cosmological surveys
- N-body simulations: Gadget2, FastPM format support
- Cosmological codes: CAMB, NICAEA parameter calculations
- Observational data: CFHT lens survey, DES-like mock catalogs
- Parallel computing: MPI support for cluster environments
- C extensions in
extern/for computationally intensive operations (topology analysis, Gadget I/O, pixelization) - MPI parallelization throughout the codebase
- Efficient FFT implementations via FFTW3
- NumPy/SciPy vectorized operations
Uses nose testing framework with coverage reporting. Tests are organized by functionality and located in lenstools/tests/. The CI system (Travis) runs tests on Python 3.6/3.7 with external library dependencies.