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
+417 -258
View File
@@ -1,271 +1,430 @@
---
description: This rule provides best practices for using NumPy in Python, covering coding standards, performance optimization, security, and testing strategies to enhance code quality and maintainability.
globs: *.py
description: This guide provides definitive, actionable best practices for writing high-performance, maintainable, and correct NumPy code, emphasizing vectorization, explicit dtypes, and modern GPU acceleration.
globs: **/*.py
---
# NumPy Library Best Practices and Coding Standards
# numpy Best Practices
NumPy is the bedrock of numerical computing in Python. To fully leverage its power for AI/ML and data science in 2025, we must write code that is not just functional but also fast, readable, and robust. This guide outlines our team's definitive best practices for NumPy.
## Core Principles
1. **Vectorize Everything**: Eliminate Python loops over arrays. NumPy operations are implemented in C and are orders of magnitude faster.
2. **Be Explicit with `dtype`**: Always specify array data types to prevent unexpected casting and optimize memory/performance.
3. **Embrace GPU Acceleration**: Utilize CuPy-compatible calls for critical, large-scale computations.
## Code Organization and Structure
Organize NumPy-heavy logic into dedicated, well-named functions and modules.
### 1. Modular Design
Isolate numerical operations into functions that accept and return NumPy arrays. This improves testability and reusability.
❌ BAD: Monolithic script
```python
import numpy as np
def process_data_script(data_path):
data = np.loadtxt(data_path)
# ... many lines of processing ...
mean_val = np.mean(data)
std_val = np.std(data)
normalized_data = (data - mean_val) / std_val
# ... more processing ...
return normalized_data
```
✅ GOOD: Modular functions
```python
import numpy as np
def load_numerical_data(file_path: str) -> np.ndarray:
"""Loads numerical data from a file."""
return np.loadtxt(file_path)
def normalize_array(arr: np.ndarray) -> np.ndarray:
"""Normalizes a NumPy array to have zero mean and unit variance."""
if arr.size == 0:
return arr
mean_val = np.mean(arr)
std_val = np.std(arr)
# Avoid division by zero for constant arrays
if std_val == 0:
return np.zeros_like(arr)
return (arr - mean_val) / std_val
This document outlines best practices for using the NumPy library in Python for scientific computing, data science, machine learning, and AI development. Adhering to these guidelines will improve code readability, maintainability, performance, security, and overall project success.
def process_pipeline(data_path: str) -> np.ndarray:
"""Orchestrates data loading and processing."""
data = load_numerical_data(data_path)
processed_data = normalize_array(data)
return processed_data
```
### 2. Clear Naming and Docstrings
Adhere to PEP 8 for variable and function names (`snake_case`). Use comprehensive docstrings (PEP 257) for all public functions, classes, and modules, especially for complex numerical logic.
❌ BAD: Ambiguous names, no docs
```python
def calc(a, b):
# What does 'a' mean? What does 'b' mean?
# What does this function do?
return np.dot(a, b.T)
```
✅ GOOD: Descriptive names, clear docstrings
```python
import numpy as np
def calculate_covariance_matrix(data_matrix: np.ndarray) -> np.ndarray:
"""
Calculates the covariance matrix for a given data matrix.
## 1. Code Organization and Structure
Args:
data_matrix: A 2D NumPy array where rows are observations
and columns are features.
Returns:
A 2D NumPy array representing the covariance matrix.
"""
if data_matrix.ndim != 2:
raise ValueError("Input data_matrix must be 2-dimensional.")
# Center the data by subtracting the mean of each feature
centered_data = data_matrix - np.mean(data_matrix, axis=0)
# Calculate covariance matrix: (X^T * X) / (n - 1)
covariance_matrix = np.cov(data_matrix, rowvar=False)
return covariance_matrix
```
## Common Patterns and Anti-patterns
### 1. Vectorization over Python Loops
**Always** prefer NumPy's vectorized operations and broadcasting over explicit Python `for` loops when working with arrays.
❌ BAD: Element-wise loop
```python
import numpy as np
data = np.random.rand(1_000_000)
result = np.empty_like(data)
for i in range(len(data)):
result[i] = data[i] * 2 + 5 # Slow for large arrays
```
✅ GOOD: Vectorized operation
```python
import numpy as np
data = np.random.rand(1_000_000)
result = data * 2 + 5 # Fast and concise
```
### 2. Broadcasting
Leverage broadcasting for operations between arrays of different shapes. It's efficient and avoids unnecessary memory allocation.
❌ BAD: Manual tiling or explicit loops for scalar/vector operations
```python
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 10
result = np.empty_like(matrix)
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
result[i, j] = matrix[i, j] + scalar # Avoid
```
✅ GOOD: Broadcasting
```python
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 10
result = matrix + scalar # NumPy handles this efficiently
```
## Performance Considerations
### 1. Explicit `dtype` Specification
**Always** specify `dtype` when creating arrays or performing operations that might change types. This prevents unexpected memory usage and performance penalties from implicit type conversions.
❌ BAD: Implicit `dtype`
```python
import numpy as np
# Python integers become int64 by default, even if int32 is sufficient
arr = np.array([1, 2, 3])
# float operations might implicitly upcast, consuming more memory
result = arr / 2.0
```
✅ GOOD: Explicit `dtype`
```python
import numpy as np
# Use the smallest appropriate dtype
arr_int = np.array([1, 2, 3], dtype=np.int32)
arr_float = np.array([1.0, 2.0, 3.0], dtype=np.float32)
# Ensure consistent dtypes in operations
result = arr_int.astype(np.float32) / 2.0
```
### 2. Leverage GPU Acceleration (CuPy-compatible)
For computationally intensive tasks on large arrays, especially in AI/ML, integrate GPU-accelerated libraries like CuPy. Write functions that can seamlessly switch between NumPy and CuPy.
```python
import numpy as np
try:
import cupy as cp
_has_cupy = True
except ImportError:
_has_cupy = False
def matrix_multiply(a: np.ndarray, b: np.ndarray, use_gpu: bool = False) -> np.ndarray:
"""
Performs matrix multiplication, optionally using GPU if available.
"""
xp = cp if use_gpu and _has_cupy else np
a_xp = xp.asarray(a)
b_xp = xp.asarray(b)
result = xp.dot(a_xp, b_xp)
return result.get() if use_gpu and _has_cupy else result
### 1.1. Directory Structure
* **`root_directory/`**: The project's root directory.
* **`data/`**: Stores datasets (e.g., CSV files, NumPy arrays in `.npy` format). Consider using subdirectories within `data/` to categorize datasets (e.g., `raw/`, `processed/`). Use version control (e.g., DVC) for larger datasets.
* **`notebooks/`**: Jupyter notebooks for exploration, prototyping, and EDA. Number notebooks sequentially (e.g., `01_data_loading.ipynb`, `02_feature_engineering.ipynb`). Keep notebooks clean and well-documented; move reusable code to modules.
* **`src/`** (or `package_name/`): Contains the Python source code.
* **`package_name/`**: The main package directory. Use your project's name as the package name.
* **`__init__.py`**: Marks the directory as a Python package. Can be empty or contain package-level initialization code.
* **`utils/`**: Utility functions and helper classes. Split into submodules if necessary (e.g., `utils/data_handling.py`, `utils/math_functions.py`).
* **`models/`**: Model definitions and training scripts (if applicable). Subdirectories can organize models further (e.g., `models/regression/`, `models/classification/`).
* **`preprocessing/`**: Data preprocessing functions and classes.
* **`visualization/`**: Functions for creating plots and visualizations.
* **`config.py`**: Configuration settings (e.g., file paths, hyperparameters). Use a library like `python-box` or `dynaconf` to manage configurations.
* **`main.py`**: The main entry point for the application (if applicable). Use `if __name__ == "__main__":` to control execution.
* **`tests/`**: Unit and integration tests. Mirror the `src/` structure. Use `pytest` or `unittest`.
* **`tests/utils/`**: Tests for the `utils/` module.
* **`tests/models/`**: Tests for the `models/` module.
* **`conftest.py`**: Configuration file for `pytest`. Can contain fixtures and hooks.
* **`docs/`**: Documentation for the project (e.g., using Sphinx).
* **`scripts/`**: Scripts for data downloading, processing, or model deployment.
* **`requirements.txt`**: Lists Python dependencies. Use `pip freeze > requirements.txt` to generate.
* **`.gitignore`**: Specifies files and directories to ignore in Git (e.g., `data/`, `notebooks/`, `__pycache__/`).
* **`README.md`**: Provides a high-level overview of the project.
* **`LICENSE`**: Specifies the license for the project.
* **`setup.py`** or **`pyproject.toml`**: Used for packaging and distribution.
# Example usage
matrix_a = np.random.rand(1000, 500)
matrix_b = np.random.rand(500, 2000)
### 1.2. File Naming Conventions
# CPU computation
cpu_result = matrix_multiply(matrix_a, matrix_b, use_gpu=False)
# GPU computation (if CuPy installed)
if _has_cupy:
gpu_result = matrix_multiply(matrix_a, matrix_b, use_gpu=True)
assert np.allclose(cpu_result, gpu_result)
```
* **Python files**: Use lowercase with underscores (snake_case): `data_loader.py`, `feature_engineering.py`.
* **Class names**: Use CamelCase: `DataLoader`, `FeatureEngineer`.
* **Function and variable names**: Use snake_case: `load_data()`, `feature_names`.
* **Constants**: Use uppercase with underscores: `MAX_ITERATIONS`, `DEFAULT_LEARNING_RATE`.
### 3. Pre-allocate Arrays
When filling an array iteratively (e.g., in a loop where vectorization isn't fully possible), pre-allocate the array with `np.empty` or `np.zeros` to avoid costly reallocations.
❌ BAD: Appending to a list then converting
```python
import numpy as np
results = []
for i in range(1000):
results.append(np.random.rand(10))
final_array = np.array(results) # Inefficient for many appends
```
✅ GOOD: Pre-allocation
```python
import numpy as np
num_iterations = 1000
array_shape = (10,)
final_array = np.empty((num_iterations,) + array_shape, dtype=np.float32)
for i in range(num_iterations):
final_array[i] = np.random.rand(*array_shape) # Efficient
```
### 1.3. Module Organization
## Common Pitfalls and Gotchas
### 1. Shape Mismatches
NumPy operations are strict about array shapes. Understand broadcasting rules and use `reshape`, `transpose`, `newaxis`, or `squeeze` explicitly.
❌ BAD: Implicit shape assumptions
```python
import numpy as np
vec = np.array([1, 2, 3]) # (3,)
matrix = np.array([[10, 20, 30], [40, 50, 60]]) # (2, 3)
# This will raise a ValueError if not careful, e.g., matrix + vec.T
# Or produce unexpected results due to broadcasting rules
```
✅ GOOD: Explicit shape handling
```python
import numpy as np
vec_row = np.array([1, 2, 3]) # (3,)
vec_col = np.array([1, 2, 3])[:, np.newaxis] # (3, 1)
matrix = np.array([[10, 20, 30], [40, 50, 60]]) # (2, 3)
# Correctly add a row vector to each row of a matrix
result = matrix + vec_row # (2,3) + (3,) -> (2,3)
# Correctly add a column vector to each column of a matrix
result = matrix + vec_col # (2,3) + (3,1) -> (2,3)
```
### 2. View vs. Copy
Understand when NumPy returns a view (modifying it changes the original array) versus a copy. Operations like slicing often return views. Use `.copy()` explicitly when you need an independent array.
❌ BAD: Unintended modification
```python
import numpy as np
original_data = np.arange(10)
subset = original_data[2:5] # This is a view!
subset[:] = 99 # Modifies original_data
print(original_data) # Output: [ 0 1 99 99 99 5 6 7 8 9] - DANGER!
```
✅ GOOD: Explicitly create a copy when needed
```python
import numpy as np
original_data = np.arange(10)
subset_copy = original_data[2:5].copy() # This is a copy!
subset_copy[:] = 99 # Only modifies subset_copy
print(original_data) # Output: [0 1 2 3 4 5 6 7 8 9] - SAFE!
```
## Type Hints
* **Separate concerns**: Each module should have a single, well-defined purpose.
* **Avoid circular dependencies**: Design modules to minimize dependencies on each other.
* **Use relative imports**: Within a package, use relative imports to refer to other modules: `from . import utils`, `from ..models import BaseModel`.
* **Expose a clear API**: Each module should have a well-defined API (Application Programming Interface) of functions and classes that are intended for external use. Hide internal implementation details using leading underscores (e.g., `_helper_function()`).
**Always** use type hints for function signatures involving NumPy arrays. This improves code readability, enables static analysis, and catches errors early. Use `np.ndarray` and specify `dtype` where type precision is critical.
### 1.4. Component Architecture
```python
from typing import Tuple
import numpy as np
* **Layered architecture**: Divide the application into layers (e.g., data access, business logic, presentation) to promote separation of concerns.
* **Microservices**: For larger applications, consider breaking them down into smaller, independent microservices.
* **Design patterns**: Implement relevant design patterns to enhance flexibility and maintainability.
def process_sensor_data(
readings: np.ndarray,
threshold: float = 0.5
) -> Tuple[np.ndarray, np.ndarray]:
"""
Processes sensor readings, separating values above/below a threshold.
### 1.5. Code Splitting
Args:
readings: A 1D NumPy array of sensor readings (expected float32).
threshold: The threshold value.
* **Functions**: Break down large functions into smaller, more manageable functions.
* **Classes**: Use classes to encapsulate related data and behavior.
* **Modules**: Split code into multiple modules based on functionality.
* **Packages**: Organize modules into packages to create a hierarchical structure.
## 2. Common Patterns and Anti-patterns
### 2.1. Design Patterns
* **Strategy**: Use a Strategy pattern to encapsulate different algorithms or approaches for a specific task.
* **Factory**: Employ a Factory pattern to create objects without specifying their concrete classes.
* **Observer**: Use an Observer pattern to notify dependent objects when the state of an object changes.
### 2.2. Recommended Approaches
* **Vectorization**: Leverage NumPy's vectorized operations whenever possible to avoid explicit loops. Vectorization significantly improves performance.
* **Broadcasting**: Understand and utilize NumPy's broadcasting rules to perform operations on arrays with different shapes.
* **Ufuncs (Universal Functions)**: Use NumPy's built-in ufuncs (e.g., `np.sin`, `np.exp`, `np.add`) for element-wise operations. Ufuncs are highly optimized.
* **Masking**: Use boolean masks to select and modify specific elements in arrays.
* **Views vs. Copies**: Be aware of the difference between views and copies when slicing and manipulating arrays. Modifying a view affects the original array.
### 2.3. Anti-patterns and Code Smells
* **Explicit loops**: Avoid explicit loops for element-wise operations. Use vectorized operations instead.
* **Unnecessary copies**: Avoid creating unnecessary copies of arrays. Use views when possible.
* **Modifying arrays in place**: Be careful when modifying arrays in place, as it can lead to unexpected side effects.
* **Ignoring broadcasting rules**: Not understanding broadcasting rules can lead to incorrect results or errors.
* **Hardcoding array shapes**: Avoid hardcoding array shapes. Use `array.shape` to get the shape dynamically.
* **Relying on mutable default arguments**: Avoid using mutable default arguments (e.g., lists, dictionaries) in function definitions.
### 2.4. State Management
* **Immutable data structures**: When appropriate, use immutable data structures to prevent accidental modification of data. Consider libraries like `namedtuple` or `dataclasses` (with `frozen=True`).
* **Encapsulation**: Encapsulate state within classes to control access and modification.
* **Dependency injection**: Use dependency injection to decouple components and make them more testable.
### 2.5. Error Handling
* **Exceptions**: Use exceptions to handle errors and unexpected conditions. Raise specific exceptions (e.g., `ValueError`, `TypeError`) when appropriate.
* **Assertions**: Use assertions to check for conditions that should always be true. Assertions are helpful for debugging and validating data.
* **Logging**: Use logging to record errors, warnings, and informational messages. Configure logging to write to a file or stream.
* **`np.errstate`**: Use `np.errstate` context manager to handle floating-point exceptions (e.g., division by zero, overflow). You can configure how NumPy handles these exceptions (e.g., ignore, warn, raise).
## 3. Performance Considerations
### 3.1. Optimization Techniques
* **Vectorization**: As mentioned earlier, prioritize vectorized operations over explicit loops.
* **Memory Alignment**: NumPy arrays are typically aligned in memory, which can improve performance. Ensure that your data is aligned correctly.
* **Data Types**: Choose the smallest possible data type that can represent your data. Using smaller data types reduces memory usage and improves performance. For example, use `np.int8` instead of `np.int64` if the values are within the range of `np.int8`.
* **`np.einsum`**: Use `np.einsum` (Einstein summation) for complex array operations. `np.einsum` can often be more efficient than explicit loops or other NumPy functions.
* **Numba**: Use Numba to JIT (Just-In-Time) compile NumPy code. Numba can significantly improve the performance of computationally intensive code.
* **Cython**: Use Cython to write NumPy code in C. Cython allows you to take advantage of C's performance while still using Python's syntax.
* **BLAS/LAPACK**: NumPy relies on BLAS (Basic Linear Algebra Subprograms) and LAPACK (Linear Algebra Package) libraries for linear algebra operations. Ensure that you are using an optimized BLAS/LAPACK implementation (e.g., OpenBLAS, MKL).
* **`np.fft`**: For FFT (Fast Fourier Transform) operations, use the functions provided in NumPy's `np.fft` module. They're usually highly optimized.
### 3.2. Memory Management
* **Avoid creating large temporary arrays**: Minimize the creation of large temporary arrays, especially within loops. Use in-place operations when possible.
* **`np.empty`**: Use `np.empty` to create uninitialized arrays when you don't need to initialize the values immediately. This can be faster than `np.zeros` or `np.ones`.
* **`np.memmap`**: Use `np.memmap` to create memory-mapped arrays. Memory-mapped arrays allow you to work with large datasets that don't fit in memory.
* **Garbage collection**: Be mindful of Python's garbage collection. Explicitly delete large arrays when they are no longer needed to free up memory: `del my_array`.
### 3.3. Rendering Optimization (If Applicable)
* This is mainly relevant when NumPy arrays are used for image processing or other visualization tasks. Libraries like Matplotlib, Seaborn, or specialized rendering engines may offer specific optimizations for handling NumPy arrays.
### 3.4. Bundle Size Optimization (If Applicable)
* If you are deploying a web application or other application that includes NumPy, consider using tree shaking to remove unused code. Tools like Webpack or Parcel can help with tree shaking.
### 3.5. Lazy Loading
* If you are working with very large datasets, use lazy loading to load data only when it is needed. Libraries like Dask or Apache Arrow can help with lazy loading.
## 4. Security Best Practices
### 4.1. Common Vulnerabilities
* **Arbitrary code execution**: Avoid using `np.fromstring` or `np.frombuffer` with untrusted input, as this can lead to arbitrary code execution.
* **Denial of service**: Be careful when using NumPy functions with user-supplied input, as this can lead to denial-of-service attacks. Validate input to prevent excessively large array allocations or computationally intensive operations.
* **Integer overflow**: Be aware of integer overflow issues when performing arithmetic operations on large arrays. Use appropriate data types to prevent overflow.
### 4.2. Input Validation
* **Data type validation**: Ensure that the input data has the expected data type.
* **Shape validation**: Ensure that the input data has the expected shape.
* **Range validation**: Ensure that the input data falls within the expected range.
* **Sanitize input**: Sanitize input data to prevent injection attacks.
* **Use `np.asarray`**: When receiving data from external sources, use `np.asarray` to convert it to a NumPy array. This ensures that you are working with a NumPy array and not some other type of object that might have unexpected behavior.
### 4.3. Authentication and Authorization (If Applicable)
* NumPy itself doesn't handle authentication or authorization. If your application requires these features, use appropriate authentication and authorization mechanisms (e.g., OAuth, JWT).
### 4.4. Data Protection
* **Encryption**: Encrypt sensitive data at rest and in transit.
* **Access control**: Restrict access to data based on user roles and permissions.
* **Data masking**: Mask sensitive data to protect it from unauthorized access.
* **Regular Security Audits**: Conduct regular security audits to identify and address potential vulnerabilities.
### 4.5. Secure API Communication (If Applicable)
* Use HTTPS for all API communication.
* Validate all API requests.
* Use rate limiting to prevent denial-of-service attacks.
## 5. Testing Approaches
### 5.1. Unit Testing
* **Test individual functions and classes**: Unit tests should focus on testing individual functions and classes in isolation.
* **Use `pytest` or `unittest`**: Use a testing framework like `pytest` or `unittest` to write and run unit tests.
* **Test edge cases**: Test edge cases and boundary conditions to ensure that your code handles them correctly.
* **Test for exceptions**: Test that your code raises the correct exceptions when errors occur.
* **Use parametrization**: Use parametrization to run the same test with multiple sets of inputs.
* **Assert almost equal**: Use `np.testing.assert_allclose` instead of `assert a == b` when comparing floating-point numbers. This accounts for potential floating-point precision errors.
### 5.2. Integration Testing
* **Test interactions between components**: Integration tests should focus on testing the interactions between different components of your application.
* **Use mock objects**: Use mock objects to isolate components during integration testing.
### 5.3. End-to-End Testing (If Applicable)
* **Test the entire application flow**: End-to-end tests should focus on testing the entire application flow from start to finish.
### 5.4. Test Organization
* **Mirror the source code structure**: Organize your tests in a directory structure that mirrors the source code structure.
* **Use descriptive test names**: Use descriptive test names that clearly indicate what the test is testing.
* **Keep tests small and focused**: Keep tests small and focused to make them easier to understand and maintain.
### 5.5. Mocking and Stubbing
* **Use mock objects to isolate components**: Use mock objects to isolate components during testing.
* **Use `unittest.mock` or `pytest-mock`**: Use a mocking library like `unittest.mock` or `pytest-mock` to create mock objects.
* **Mock external dependencies**: Mock external dependencies (e.g., databases, APIs) to avoid relying on them during testing.
## 6. Common Pitfalls and Gotchas
### 6.1. Frequent Mistakes
* **Incorrect array indexing**: NumPy uses 0-based indexing, which can be confusing for developers who are used to other languages.
* **Incorrect array slicing**: Be careful when slicing arrays, as this can create views or copies depending on the slicing operation.
* **Incorrect broadcasting**: Not understanding broadcasting rules can lead to incorrect results or errors.
* **Modifying views**: Modifying a view affects the original array, which can lead to unexpected side effects.
* **Ignoring data types**: Not specifying the correct data type can lead to integer overflow or other issues.
### 6.2. Edge Cases
* **Empty arrays**: Be careful when working with empty arrays, as they can have unexpected behavior.
* **Arrays with NaN or Inf values**: Be aware that arrays can contain NaN (Not a Number) or Inf (Infinity) values, which can affect calculations.
* **Arrays with mixed data types**: NumPy arrays should typically have a single data type. Be cautious when working with arrays that have mixed data types (e.g., object arrays).
### 6.3. Version-Specific Issues
* Consult the NumPy release notes for information on version-specific issues and changes.
### 6.4. Compatibility Concerns
* **Python version**: Ensure that your code is compatible with the Python version you are using.
* **Other libraries**: Be aware of compatibility issues between NumPy and other libraries.
* **Operating system**: Be aware of potential differences in behavior across different operating systems.
### 6.5. Debugging Strategies
* **Print statements**: Use print statements to inspect the values of variables and arrays.
* **Debuggers**: Use a debugger (e.g., pdb, ipdb) to step through your code and inspect the state of the program.
* **Logging**: Use logging to record errors, warnings, and informational messages.
* **`np.seterr`**: Use `np.seterr` to configure how NumPy handles floating-point exceptions.
* **`np.info`**: Use `np.info` to get information about NumPy objects.
* **Visual Inspection**: Use visualization tools (Matplotlib, Seaborn, etc.) to visually inspect data and identify potential problems.
## 7. Tooling and Environment
### 7.1. Recommended Development Tools
* **IDE**: Use an IDE (Integrated Development Environment) like VS Code, PyCharm, or Spyder.
* **Jupyter Notebook**: Use Jupyter Notebook for exploration, prototyping, and EDA.
* **IPython**: Use IPython for interactive computing.
* **Debugging Tools**: Utilize debuggers like pdb or IDE-integrated debuggers for stepping through code and inspecting variables.
### 7.2. Build Configuration
* **`setup.py` or `pyproject.toml`**: Use `setup.py` or `pyproject.toml` to configure the build process.
* **`requirements.txt`**: Use `requirements.txt` to specify dependencies.
* **Virtual environments**: Use virtual environments to isolate dependencies.
* **Conda**: Consider using Conda for environment and package management, particularly for scientific computing workflows.
### 7.3. Linting and Formatting
* **PEP 8**: Follow PEP 8 style guidelines for Python code.
* **Linters**: Use a linter (e.g., pylint, flake8) to check for style violations and potential errors.
* **Formatters**: Use a formatter (e.g., black, autopep8) to automatically format your code.
* **Pre-commit hooks**: Use pre-commit hooks to run linters and formatters before committing code.
### 7.4. Deployment
* **Containerization (Docker)**: Use Docker to create a containerized environment for your application. This helps ensure consistency across different environments.
* **Cloud platforms**: Deploy your application to a cloud platform (e.g., AWS, Azure, GCP).
* **Serverless functions**: Consider using serverless functions (e.g., AWS Lambda, Azure Functions, Google Cloud Functions) for simple applications.
* **Model serving frameworks**: If deploying machine learning models, use a model serving framework like TensorFlow Serving or TorchServe.
### 7.5. CI/CD Integration
* **Continuous Integration (CI)**: Use a CI system (e.g., Jenkins, Travis CI, CircleCI, GitHub Actions) to automatically build and test your code when changes are made.
* **Continuous Delivery (CD)**: Use a CD system to automatically deploy your code to production after it has been tested.
* **Automated Testing**: Integrate automated tests into your CI/CD pipeline to ensure code quality and prevent regressions.
* **Infrastructure as Code (IaC)**: Define your infrastructure using code (e.g., Terraform, CloudFormation) to automate the provisioning and management of your environment.
By following these best practices, you can write high-quality, maintainable, and performant NumPy code.
Returns:
A tuple containing two 1D NumPy arrays: (above_threshold, below_threshold).
"""
if readings.dtype != np.float32:
raise TypeError("Input 'readings' must be of dtype np.float32")
above = readings[readings >= threshold]
below = readings[readings < threshold]
return above, below
# Example usage
data = np.random.rand(100).astype(np.float32)
high_values, low_values = process_sensor_data(data, threshold=0.7)
```
## Virtual Environments
**Always** use virtual environments (`venv` or `conda`) for every project. This isolates dependencies and prevents version conflicts.
❌ BAD: Installing packages globally
```bash
pip install numpy scipy pandas # Pollutes global Python environment
```
✅ GOOD: Using a virtual environment
```bash
# For venv
python -m venv .venv
source .venv/bin/activate
pip install numpy scipy pandas
# For conda
conda create -n my_project_env python=3.10
conda activate my_project_env
conda install numpy scipy pandas
```
## Packaging
For any reusable NumPy code, package it as a standard Python library. Use `pyproject.toml` for modern packaging.
```toml
# pyproject.toml
[project]
name = "my_numpy_utils"
version = "0.1.0"
dependencies = [
"numpy>=1.26.0",
"scipy>=1.12.0",
]
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
```
## Testing Approaches
**Always** write unit tests for your NumPy code. Numerical stability and correctness are paramount.
### 1. `numpy.testing` for Numerical Comparisons
Use `numpy.testing` functions (e.g., `assert_allclose`, `assert_array_equal`) for comparing arrays, as standard equality checks can fail due to floating-point inaccuracies.
❌ BAD: Direct equality for floats
```python
import numpy as np
a = np.array([0.1 + 0.2])
b = np.array([0.3])
assert a == b # Fails due to floating point precision
```
✅ GOOD: `numpy.testing.assert_allclose`
```python
import numpy as np
from numpy.testing import assert_allclose
a = np.array([0.1 + 0.2])
b = np.array([0.3])
assert_allclose(a, b, rtol=1e-5, atol=1e-8) # Passes
```
### 2. `pytest` Integration
Integrate `numpy.testing` with `pytest` for a robust testing framework.
```python
# tests/test_my_module.py
import numpy as np
from numpy.testing import assert_allclose, assert_array_equal
from my_package.my_module import normalize_array, calculate_covariance_matrix
def test_normalize_array_basic():
arr = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
expected = np.array([-1.34164079, -0.4472136 , 0.4472136 , 1.34164079], dtype=np.float32)
result = normalize_array(arr)
assert_allclose(result, expected, rtol=1e-6)
def test_normalize_array_constant():
arr = np.array([5.0, 5.0, 5.0], dtype=np.float32)
expected = np.array([0.0, 0.0, 0.0], dtype=np.float32)
result = normalize_array(arr)
assert_allclose(result, expected)
def test_calculate_covariance_matrix():
data = np.array([[1, 2], [2, 3], [3, 4]], dtype=np.float64)
expected_cov = np.array([[1.0, 1.0], [1.0, 1.0]], dtype=np.float64)
result_cov = calculate_covariance_matrix(data)
assert_allclose(result_cov, expected_cov, rtol=1e-9)
def test_calculate_covariance_matrix_raises_error_for_1d():
data = np.array([1, 2, 3], dtype=np.float64)
with np.testing.assert_raises(ValueError):
calculate_covariance_matrix(data)
```