Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+364 -54
View File
@@ -1,56 +1,366 @@
---
description: This rule outlines coding standards, best practices, and common pitfalls for developing scientific computing applications using the SciPy library. It emphasizes clarity, maintainability, performance, and security for efficient SciPy development.
globs: *.py
description: This guide provides opinionated, actionable best practices for writing robust, performant, and maintainable Python code using SciPy, aligning with modern scientific computing standards.
globs: **/*.py
---
- Adhere to PEP 8 style guidelines for Python code. This includes consistent indentation (4 spaces), line length (79 characters for code, 72 for docstrings), and naming conventions (e.g., `lower_case_with_underscores` for functions and variables, `CamelCase` for classes).
- Write comprehensive docstrings for all functions, classes, and modules. Docstrings should follow the NumPy/SciPy docstring standard, detailing parameters, return values, and examples.
- Use meaningful and descriptive variable names to enhance code readability. Avoid single-character variable names (except for loop counters) and abbreviations that are not widely understood.
- Break down complex tasks into smaller, modular functions. Each function should have a single, well-defined purpose.
- Employ object-oriented programming principles (classes, inheritance, polymorphism) when appropriate to structure and organize your code.
- Implement unit tests for all critical functions and classes. Use the `unittest` or `pytest` framework to ensure code correctness and prevent regressions.
- Utilize version control (e.g., Git) to track changes, collaborate effectively, and manage different versions of your code.
- Comment your code to explain complex logic, algorithms, or design decisions. Comments should be concise and up-to-date.
- Employ virtual environments (e.g., `venv` or `conda`) to manage project dependencies and ensure reproducibility.
- Use a linter (e.g., `flake8` or `pylint`) to automatically check your code for style violations, errors, and potential bugs.
- Format your code using a formatter (e.g., `black` or `autopep8`) to ensure consistent code style.
- Consider using type hints (using `typing` module) to improve code readability and catch type-related errors early on.
- Be mindful of performance considerations when using SciPy functions. Vectorize operations whenever possible to avoid explicit loops.
- Choose appropriate SciPy functions and algorithms based on the specific problem and data characteristics.
- Avoid unnecessary data copies to minimize memory usage and improve performance.
- Utilize SciPy's sparse matrix functionality when dealing with large, sparse datasets.
- Profile your code using tools like `cProfile` to identify performance bottlenecks.
- Consider using Numba or Cython to accelerate computationally intensive SciPy code.
- Implement error handling using `try...except` blocks to gracefully handle exceptions and prevent program crashes.
- Log errors and warnings using the `logging` module to facilitate debugging and monitoring.
- Validate user inputs to prevent security vulnerabilities, such as code injection or data corruption.
- Store sensitive data securely using appropriate encryption and access control mechanisms.
- Keep your SciPy library up-to-date to benefit from bug fixes, performance improvements, and new features.
- Be aware of the compatibility between different versions of SciPy and other libraries in your project.
- Refer to the SciPy documentation and community resources for guidance and best practices.
- Avoid modifying SciPy arrays in place when it can lead to unexpected side effects. Instead, create copies of the arrays and perform the modifications on the copies.
- When working with random numbers, use `numpy.random.default_rng()` for more modern and controllable random number generation.
- When writing custom functions that operate on NumPy arrays, make sure to handle different data types correctly.
- When possible, avoid creating intermediate arrays, by chaining operations and making the maximum use of SciPy functions features. This can decrease memory consumption especially on large arrays.
- Use the `optimize` module functions whenever possible to avoid manual implementation of optimization algorithms.
- Use sparse matrices when working with large matrices that have mostly zero values. This will save memory and improve performance.
- Use the `fft` module for fast Fourier transforms when working with signals and images.
- Use the `signal` module for signal processing tasks such as filtering, windowing, and spectral analysis.
- Use the `ndimage` module for image processing tasks such as filtering, segmentation, and feature extraction.
- Use the `integrate` module for numerical integration tasks such as quadrature and differential equation solving.
- Use the `interpolate` module for interpolation tasks such as spline interpolation and polynomial interpolation.
- Use the `stats` module for statistical analysis tasks such as hypothesis testing, probability distributions, and regression analysis.
- When selecting a statistical test, ensure it's appropriate for your data type (continuous vs. discrete) and number of samples, and take care to interpret p-values correctly.
- Do not assume that optimized SciPy functions automatically make your overall code efficient; always profile your code.
- Be cautious when combining SciPy functions with external C/C++ code, ensuring data type consistency and memory management.
- Document and share your SciPy-based code using appropriate version control and licensing to facilitate collaboration.
- If you encounter performance issues with SciPy functions, consider trying alternative algorithms or implementations. Different problems may require different solutions.
- Remember that NumPy is a dependency of SciPy. SciPy builds upon NumPy; leverage the strengths of both libraries in your code. Master basic NumPy array manipulation before diving deeply into SciPy.
- Make sure to always use `pip install -U scipy` when installing or upgrading scipy package.
- If possible try to use conda to install dependencies. The management of binary dependencies is much simpler to solve.
- When collaborating with others, define common interfaces and expectations on what can be changed and what cannot. Do not change interfaces without notifying the other developers.
- Separate the code from data. Do not include hardcoded data or configuration into the code. Use files, databases, or environment variables.
- Use code reviews whenever is possible. Code reviews are useful not only to detect errors and bugs, but also to share knowledge and best practices.
- When generating documentation, if possible add badges that show the test coverage and the status of continuous integration.
- Test numerical code with randomized inputs to ensure code stability. Numerical algorithms are difficult to be completely covered only with fixed test cases.
- Try to avoid global shared mutable state, specially if your application needs to run in parallel.
- Try to use parallelization to improve your code efficiency, but be careful with shared mutable state. Use message passing to synchronize data between processes or threads.
# SciPy Best Practices
This document outlines the definitive best practices for developing with SciPy. Adhering to these guidelines ensures your code is clean, efficient, and compatible with the broader scientific Python ecosystem.
## 1. Code Organization and Style
Strict adherence to style guides is non-negotiable for maintainability.
### 1.1. Line Length and Docstrings
Follow PEP 8 with a strict 88-character line limit. Docstrings must conform to PEP 257 and the NumPy docstring standard, using ASCII characters primarily, with a small whitelist of Unicode symbols.
❌ **BAD**
```python
def calculate_complex_metric(data_array, method='default', tolerance=1e-6, max_iterations=1000, verbose_output=False):
"""Calculates a very complex metric using various parameters and returns a tuple of results."""
# ... implementation ...
return result1, result2, result3
```
✅ **GOOD**
```python
def calculate_complex_metric(
data_array: np.ndarray,
*,
method: str = 'default',
tolerance: float = 1e-6,
max_iterations: int = 1000,
verbose_output: bool = False,
) -> MyResultObject:
"""Calculate a complex metric from `data_array`.
Parameters
----------
data_array : np.ndarray
Input data array.
method : {'default', 'optimized'}, optional
Algorithm method to use. Default is 'default'.
tolerance : float, optional
Convergence tolerance. Default is 1e-6.
max_iterations : int, optional
Maximum number of iterations. Default is 1000.
verbose_output : bool, optional
If True, print detailed progress. Default is False.
Returns
-------
res : MyResultObject
An object with attributes:
statistic : float
The primary calculated statistic.
pvalue : float
The associated p-value.
converged : bool
True if the algorithm converged.
"""
# ... implementation ...
return MyResultObject(statistic=..., pvalue=..., converged=...)
@dataclass
class MyResultObject:
statistic: float
pvalue: float
converged: bool
```
### 1.2. Keyword-Only Arguments
For functions with more than a few arguments, enforce keyword-only arguments after the initial "obvious" ones using `*`. This improves readability and allows for backward-compatible additions of new parameters.
❌ **BAD**
```python
def process_data(data, 0.1, 'linear', True): # What do these mean?
pass
```
✅ **GOOD**
```python
def process_data(data: np.ndarray, *, threshold: float, method: str, normalize: bool):
"""Process data with explicit keyword arguments."""
# ...
pass
process_data(my_array, threshold=0.1, method='linear', normalize=True)
```
### 1.3. Return Non-Iterable Objects
When returning two or more conceptually distinct elements, always use a custom class or an existing SciPy return class (e.g., `OptimizeResult`). Avoid `tuple` or `namedtuple` to force explicit attribute access and enable future extensions.
❌ **BAD**
```python
def analyze_signal(signal: np.ndarray) -> tuple[float, float]:
# ...
return peak_freq, amplitude
```
✅ **GOOD**
```python
from dataclasses import dataclass
@dataclass
class SignalAnalysisResult:
peak_frequency: float
amplitude: float
def analyze_signal(signal: np.ndarray) -> SignalAnalysisResult:
# ...
return SignalAnalysisResult(peak_frequency=peak_freq, amplitude=amplitude)
result = analyze_signal(my_signal)
print(result.peak_frequency)
```
## 2. Common Patterns and Anti-patterns
Leverage modern SciPy features and avoid deprecated interfaces.
### 2.1. Array API Standard Support
Enable `SCIPY_ARRAY_API=1` for seamless interoperability with array libraries like PyTorch or JAX. This enforces stricter input validation and ensures `array type in equals array type out`.
❌ **BAD**
```python
# Relying on implicit NumPy conversion, potentially slow or incompatible
import numpy as np
from scipy.cluster.vq import vq
features = np.asmatrix([[1.9, 2.3, 1.7], [1.5, 2.5, 2.2]])
code_book = np.array([[1., 1., 1.], [2., 2., 2.]])
code, dist = vq(features, code_book) # Works without SCIPY_ARRAY_API=1, but matrix is deprecated
```
✅ **GOOD**
```python
# Set environment variable before import for Array API support
# export SCIPY_ARRAY_API=1
import numpy as np
import torch
from scipy.cluster.vq import vq
# Use standard arrays (NumPy or compatible tensors)
features_np = np.array([[1.9, 2.3, 1.7], [1.5, 2.5, 2.2]])
code_book_np = np.array([[1., 1., 1.], [2., 2., 2.]])
code_np, dist_np = vq(features_np, code_book_np)
features_torch = torch.tensor([[1.9, 2.3, 1.7], [1.5, 2.5, 2.2]])
code_book_torch = torch.tensor([[1., 1., 1.], [2., 2., 2.]])
code_torch, dist_torch = vq(features_torch, code_book_torch)
# Avoid np.matrix, np.ma.MaskedArray, or object dtypes when Array API is enabled
# This will raise TypeError:
# vq(np.asmatrix(features_np), code_book_np)
```
## 3. Performance Considerations
Optimize strategically: "Make it work → make it reliable → make it fast."
### 3.1. Profiling
Never optimize without measuring. Use `timeit` for micro-benchmarks and `cProfile` (or `line_profiler`) for identifying bottlenecks in larger applications.
```python
import numpy as np
import timeit
# Micro-benchmark with timeit
setup_code = "import numpy as np; a = np.arange(1000)"
stmt1 = "a ** 2"
stmt2 = "a * a"
print(f"a ** 2: {timeit.timeit(stmt1, setup=setup_code, number=100000):.3f}s")
print(f"a * a: {timeit.timeit(stmt2, setup=setup_code, number=100000):.3f}s")
# For larger scripts, use cProfile:
# python -m cProfile -o my_profile.prof your_script.py
# Then analyze with `snakeviz my_profile.prof`
```
### 3.2. Algorithmic Optimization
Prioritize choosing the right SciPy algorithm over low-level code tweaks. SciPy's routines are highly optimized (often in C/Fortran).
❌ **BAD**
```python
# Manually implementing a simple optimization loop in Python
import numpy as np
def my_custom_optimizer(func, x0, lr=0.01, max_iter=100):
x = x0
for _ in range(max_iter):
grad = (func(x + 1e-6) - func(x - 1e-6)) / (2 * 1e-6) # Numerical gradient
x -= lr * grad
return x
```
✅ **GOOD**
```python
# Leverage SciPy's robust and optimized optimization routines
from scipy.optimize import minimize
import numpy as np
def objective_function(x):
return (x[0] - 2)**2 + (x[1] - 3)**2
x0 = np.array([0.0, 0.0])
result = minimize(objective_function, x0, method='BFGS')
print(f"Optimized x: {result.x}")
```
## 4. Common Pitfalls and Gotchas
Avoid common mistakes that lead to subtle bugs or incorrect results.
### 4.1. Floating Point Comparisons
Always use `np.testing.assert_allclose` for floating-point array comparisons in tests. Avoid `assert_almost_equal` or `assert_approx_equal`.
❌ **BAD**
```python
import numpy as np
from numpy.testing import assert_almost_equal
arr1 = np.array([0.1 + 0.2, 0.3])
arr2 = np.array([0.3, 0.3])
assert_almost_equal(arr1, arr2) # Can fail due to precision issues
```
✅ **GOOD**
```python
import numpy as np
from numpy.testing import assert_allclose
arr1 = np.array([0.1 + 0.2, 0.3])
arr2 = np.array([0.3, 0.3])
assert_allclose(arr1, arr2, rtol=1e-7, atol=0) # Robust floating point comparison
```
### 4.2. Testing Exceptions and Warnings
Use `pytest.raises` and `pytest.warns` as context managers with the `match` argument to precisely test expected exceptions or warnings.
❌ **BAD**
```python
import numpy as np
from scipy import stats
from numpy.testing import assert_raises
def test_zmap_nan_policy_raise_bad():
scores = np.array([1, 2, 3])
compare = np.array([-8, -3, 2, 7, 12, np.nan])
assert_raises(ValueError, stats.zmap, scores, compare, nan_policy='raise')
```
✅ **GOOD**
```python
import numpy as np
from scipy import stats
import pytest
def test_zmap_nan_policy_raise_good():
scores = np.array([1, 2, 3])
compare = np.array([-8, -3, 2, 7, 12, np.nan])
with pytest.raises(ValueError, match='input contains nan'):
stats.zmap(scores, compare, nan_policy='raise')
```
## 5. Type Hints
Embrace PEP 484-style type hints for improved code clarity, static analysis, and maintainability.
❌ **BAD**
```python
def process(data, config):
# What are data and config?
return data * config['scale']
```
✅ **GOOD**
```python
import numpy as np
from typing import Dict, Any
def process(data: np.ndarray, config: Dict[str, Any]) -> np.ndarray:
"""Process numerical data using a configuration dictionary."""
return data * config['scale']
```
## 6. Virtual Environments
Always use a virtual environment (`venv` or `conda`) to manage dependencies. This isolates your project's dependencies and prevents conflicts.
```bash
# Create a virtual environment
python -m venv .venv
# Activate it
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install SciPy and other dependencies
pip install scipy numpy pandas matplotlib
```
## 7. Packaging
For any project beyond a simple script, structure it as a proper Python package using `pyproject.toml`.
❌ **BAD**
```
my_script.py
data.csv
```
✅ **GOOD**
```
my_project/
├── pyproject.toml
├── README.md
├── my_project/
│ ├── __init__.py
│ ├── analysis.py
│ └── utils.py
└── tests/
├── __init__.py
└── test_analysis.py
```
## 8. Testing Approaches
Comprehensive testing is crucial for reliable scientific code.
### 8.1. Pytest and Pre-commit Hooks
Use `pytest` as your testing framework. Integrate `pre-commit` hooks (e.g., `ruff`, `isort`, `black`) to automate style and linting checks before every commit.
```bash
# Install pre-commit
pip install pre-commit
# In your project root, create a .pre-commit-config.yaml
# Example:
# repos:
# - repo: https://github.com/astral-sh/ruff-pre-commit
# rev: v0.1.9
# hooks:
# - id: ruff
# args: [--fix, --exit-non-zero-on-fix]
# - repo: https://github.com/psf/black
# rev: 23.12.1
# hooks:
# - id: black
# - repo: https://github.com/PyCQA/isort
# rev: 5.12.0
# hooks:
# - id: isort
# Install the hooks
pre-commit install
# Run tests
pytest
```