mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
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:
+217
-186
@@ -1,268 +1,299 @@
|
||||
---
|
||||
description: Comprehensive best practices and coding standards for using Dask in Python, focusing on performance, code organization, and common pitfalls. Provides actionable guidance for developers using Dask for parallel and distributed computing.
|
||||
globs: *.py,*.ipynb
|
||||
description: This guide provides opinionated, actionable best practices for writing performant, maintainable, and idiomatic Dask code, focusing on real-world scenarios and modern patterns.
|
||||
globs: **/*.py
|
||||
---
|
||||
# Dask Best Practices and Coding Standards
|
||||
# Dask Best Practices
|
||||
|
||||
This document outlines best practices and coding standards for developing with Dask, focusing on code organization, performance optimization, security considerations, testing strategies, and common pitfalls to avoid. Following these guidelines will help ensure efficient, maintainable, and robust Dask applications.
|
||||
Dask is a powerful library for scaling Python, but it introduces new paradigms. Writing effective Dask code requires understanding its lazy execution model and distributed computing overhead. This guide outlines critical best practices to ensure your Dask pipelines are reliable, performant, and easy to maintain.
|
||||
|
||||
## Library Information:
|
||||
- Name: dask
|
||||
- Tags: ai, ml, data-science, python, parallel-computing, big-data
|
||||
## Critical Guidelines
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
### 1. Start Small and Profile First
|
||||
|
||||
A well-organized codebase is crucial for maintainability and collaboration. Here are best practices for structuring Dask projects:
|
||||
Before reaching for Dask, ensure your problem genuinely requires distributed computing. Optimize your serial code first. Dask adds overhead; it's not a magic bullet for inefficient algorithms.
|
||||
|
||||
### 1.1 Directory Structure
|
||||
**✅ GOOD:** Profile your Pandas/NumPy code to identify bottlenecks. Only then consider Dask for the specific slow parts.
|
||||
|
||||
Adopt a logical directory structure that reflects the project's functionality. A common structure might look like this:
|
||||
```python
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import time
|
||||
|
||||
# Simulate a large dataset operation
|
||||
data = np.random.rand(10_000_000, 10)
|
||||
df_small = pd.DataFrame(data)
|
||||
|
||||
project_root/
|
||||
├── data/ # Input datasets, sample data, etc.
|
||||
├── src/ # Source code
|
||||
│ ├── __init__.py
|
||||
│ ├── data_loading/ # Modules for data input and output
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── loaders.py # Functions to load data from different formats (CSV, Parquet, etc.)
|
||||
│ │ └── writers.py # Functions to write data
|
||||
│ ├── processing/ # Modules for data processing pipelines
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── transformations.py # Dask DataFrame transformations
|
||||
│ │ ├── aggregations.py # Dask DataFrame aggregations and reductions
|
||||
│ │ └── utils.py # Utility functions for processing
|
||||
│ ├── models/ # Modules for machine learning models and related code
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── train.py # Training scripts
|
||||
│ │ ├── predict.py # Prediction scripts
|
||||
│ │ └── evaluate.py # Evaluation scripts
|
||||
│ ├── utils/ # General utility functions
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── helpers.py # Various helper functions
|
||||
│ └── visualization/ # Modules for visualization
|
||||
│ ├── __init__.py
|
||||
│ └── plots.py # Plotting functions
|
||||
├── tests/ # Unit and integration tests
|
||||
│ ├── __init__.py
|
||||
│ ├── data_loading/ # Tests for data loading modules
|
||||
│ ├── processing/ # Tests for processing modules
|
||||
│ ├── models/ # Tests for machine learning models
|
||||
│ └── utils/ # Tests for utility modules
|
||||
├── notebooks/ # Jupyter notebooks for exploration and experimentation
|
||||
├── docs/ # Project documentation
|
||||
├── requirements.txt # Python dependencies
|
||||
├── pyproject.toml # Project configuration
|
||||
└── dask.yaml # Dask configuration
|
||||
start_time = time.time()
|
||||
result_small = df_small[df_small[0] > 0.5].groupby(1).mean()
|
||||
print(f"Pandas execution time: {time.time() - start_time:.2f}s")
|
||||
|
||||
# If this is too slow, then consider Dask for the bottleneck.
|
||||
# Don't start with Dask if Pandas is fast enough.
|
||||
```
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
### 2. Explicit Client Management
|
||||
|
||||
* **Python files:** Use lowercase with underscores (e.g., `data_loader.py`, `processing_utils.py`).
|
||||
* **Jupyter Notebooks:** Descriptive names that clearly indicate the notebook's purpose (e.g., `exploratory_data_analysis.ipynb`, `model_training.ipynb`).
|
||||
* **Configuration Files:** Use standard names like `dask.yaml` for Dask configuration, and `requirements.txt` or `pyproject.toml` for dependencies.
|
||||
Always create an explicit `dask.distributed.Client` at the top of your script or notebook. Use it as a context manager to ensure proper shutdown and pass it explicitly to any Dask-aware libraries. Avoid relying on implicit global state.
|
||||
|
||||
### 1.3 Module Organization
|
||||
**❌ BAD:** Implicit client usage or manual `client.close()`.
|
||||
|
||||
* **Keep modules focused:** Each module should have a clear and specific purpose. Avoid creating monolithic modules with unrelated functionality.
|
||||
* **Use `__init__.py`:** Include `__init__.py` files in subdirectories to make them importable as packages.
|
||||
* **Relative imports:** Use relative imports within packages to avoid ambiguity and improve maintainability (e.g., `from . import utils` instead of `from project.src.utils import utils`).
|
||||
```python
|
||||
# Relying on implicit client or forgetting to close
|
||||
from dask.distributed import Client
|
||||
client = Client() # Client might not be closed on error
|
||||
# ... Dask operations ...
|
||||
# client.close() # Easily forgotten
|
||||
```
|
||||
|
||||
### 1.4 Component Architecture
|
||||
**✅ GOOD:** Use `Client` as a context manager and pass it explicitly.
|
||||
|
||||
* **Layered architecture:** Separate concerns into distinct layers (e.g., data loading, processing, modeling, visualization). This promotes modularity and testability.
|
||||
* **Microservices (optional):** For larger applications, consider breaking down the application into smaller, independent microservices that communicate with each other. Dask can be used within each microservice for parallel processing.
|
||||
* **Dask Delayed:** Use Dask Delayed to create a graph of computations, enabling parallel execution. Define functions using `@dask.delayed` and build up a workflow.
|
||||
```python
|
||||
from dask.distributed import Client, LocalCluster
|
||||
|
||||
### 1.5 Code Splitting
|
||||
# For local development, use LocalCluster
|
||||
with LocalCluster(n_workers=4, threads_per_worker=1, memory_limit='2GB') as cluster:
|
||||
with Client(cluster) as client:
|
||||
print(f"Dask Dashboard: {client.dashboard_link}")
|
||||
# Your Dask operations here, passing 'client' if needed
|
||||
import dask.dataframe as dd
|
||||
df = dd.read_csv("s3://bucket/data/*.csv", blocksize="64MB")
|
||||
# ...
|
||||
```
|
||||
|
||||
* **Functional decomposition:** Break down complex tasks into smaller, well-defined functions.
|
||||
* **Lazy evaluation:** Leverage Dask's lazy evaluation to defer computation until necessary. This allows you to build complex workflows without immediately executing them.
|
||||
* **Chunking:** Divide large datasets into smaller chunks that can be processed independently. Dask automatically handles the parallel processing of these chunks.
|
||||
### 3. Chunk Data Wisely
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
Choose chunk (partition) sizes that fit comfortably in worker memory (typically 100 MiB to 1 GiB) and align with your underlying storage layout (e.g., Parquet row groups). Too small chunks lead to high task overhead; too large chunks lead to memory pressure.
|
||||
|
||||
### 2.1 Design Patterns
|
||||
**❌ BAD:** Default chunking on large files or arbitrary small chunks.
|
||||
|
||||
* **MapReduce:** Dask excels at implementing the MapReduce pattern. Use `dask.dataframe.map_partitions` and `dask.array.map_blocks` to apply functions to individual partitions or blocks, followed by aggregations.
|
||||
* **Data pipelines:** Design data processing workflows as pipelines of Dask operations. This allows for efficient execution and easy modification.
|
||||
* **Scatter/Gather:** Distribute data to workers using `client.scatter` and then collect the results using `client.gather`. This pattern is useful for distributing data that is needed by multiple tasks.
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
# Default blocksize might be too large or too small for your data/cluster
|
||||
df = dd.read_csv("large_file.csv")
|
||||
|
||||
### 2.2 Recommended Approaches
|
||||
# Explicitly small chunks lead to massive task graphs
|
||||
df = dd.read_csv("large_file.csv", blocksize="1MB")
|
||||
```
|
||||
|
||||
* **Start with Pandas/NumPy:** Prototype your code using Pandas and NumPy to ensure it works correctly before scaling up with Dask.
|
||||
* **Use Dask collections:** Prefer using Dask DataFrames and Arrays over Dask Delayed when working with tabular or numerical data. Dask collections provide optimized implementations for common operations.
|
||||
* **Leverage the Dask Dashboard:** Use the Dask Dashboard to monitor task execution, resource usage, and identify performance bottlenecks. The dashboard provides valuable insights into the behavior of your Dask application.
|
||||
**✅ GOOD:** Tune `blocksize` based on data size, worker memory, and file format.
|
||||
|
||||
### 2.3 Anti-patterns
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
# Aim for 100MB-1GB chunks. Adjust based on your cluster's memory profile.
|
||||
# For Parquet, align with row group size if possible.
|
||||
df = dd.read_parquet("s3://bucket/data/*.parquet", blocksize="256MB")
|
||||
print(f"Number of partitions: {df.npartitions}")
|
||||
```
|
||||
|
||||
* **Creating large Python objects outside of Dask:** Avoid creating large Pandas DataFrames or NumPy arrays outside of Dask and then passing them to Dask. Instead, let Dask load the data directly. Use `dd.read_csv` or `da.from_array` instead of loading data with Pandas/NumPy and then converting to Dask.
|
||||
* **Repeatedly calling `compute()`:** Calling `compute()` multiple times on independent parts of a Dask graph forces Dask to recompute shared dependencies. Instead, use `dask.compute()` to compute multiple results in a single call.
|
||||
* **Over-partitioning:** Creating too many small partitions can lead to excessive overhead. Choose chunk sizes that are large enough to minimize overhead, but small enough to fit in memory.
|
||||
* **Ignoring the Index:** If your DataFrame will be queried often on specific columns, setting those columns as the index can drastically improve performance.
|
||||
### 4. Persist Intermediate Results
|
||||
|
||||
### 2.4 State Management
|
||||
If a Dask collection is reused across multiple steps, `persist()` it to keep its computed results in worker memory. This prevents redundant re-computation of the same task graph. Call `.compute()` only once at the very end of your pipeline.
|
||||
|
||||
* **Minimize global state:** Avoid using global variables within Dask tasks. Instead, pass data explicitly as arguments to the tasks.
|
||||
* **Immutable data:** Treat data as immutable whenever possible. This simplifies reasoning about the code and avoids unexpected side effects.
|
||||
* **Dask futures for stateful computations:** For stateful computations, use Dask futures to manage the state of individual tasks. This allows you to track the progress of each task and access its results.
|
||||
**❌ BAD:** Re-computing the same graph multiple times.
|
||||
|
||||
### 2.5 Error Handling
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
df = dd.read_parquet("data/*.parquet")
|
||||
filtered_df = df[df['value'] > 0.5]
|
||||
|
||||
* **Try-except blocks:** Use `try-except` blocks to handle exceptions that may occur within Dask tasks. Log the exceptions and consider retrying the task.
|
||||
* **Dask futures for exception handling:** Dask futures will propagate exceptions from tasks to the main thread. Handle these exceptions appropriately.
|
||||
* **Task retries:** Use the `retries` argument in `client.submit` to automatically retry failed tasks. This can be useful for handling transient errors.
|
||||
# This will re-compute 'filtered_df' twice
|
||||
mean_val = filtered_df['column_A'].mean().compute()
|
||||
std_val = filtered_df['column_B'].std().compute()
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
**✅ GOOD:** `persist()` intermediate results that are reused.
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
df = dd.read_parquet("data/*.parquet")
|
||||
filtered_df = df[df['value'] > 0.5].persist() # Keep in memory
|
||||
|
||||
* **Chunk Size Selection:** Choosing appropriate chunk sizes for your Dask arrays or DataFrames. Aim for sizes that allow multiple chunks to fit in memory without causing excessive overhead. A good rule of thumb is to keep chunks around 100 MB, which balances memory use and processing efficiency.
|
||||
* **Persisting Data:** For large datasets, persist intermediate results in memory after initial processing steps to speed up subsequent computations. This reduces the need for repeated data loading and processing.
|
||||
* **Minimizing Task Graph Size:** Minimize the number of tasks by fusing operations where possible and avoiding excessive partitioning. Large task graphs can lead to significant overhead that impacts performance.
|
||||
* **Fusing operations:** Fuse multiple operations into a single task using `dask.delayed` or `dask.dataframe.map_partitions`. This reduces overhead and improves performance.
|
||||
* **Data locality:** Try to keep data close to the workers that are processing it. This minimizes data transfer and improves performance. Use `client.scatter` to distribute data to specific workers.
|
||||
* **Using the right scheduler:** The threaded scheduler works well for computations that are mostly CPU-bound. The process scheduler works well for computations that are I/O-bound or involve GIL-releasing operations. The distributed scheduler is best for large-scale deployments.
|
||||
* **Avoid Oversubscribing Threads:** Explicitly set the number of threads used by NumPy's linear algebra routines to 1. Export the following environment variables before starting your python process: `export OMP_NUM_THREADS=1`, `export MKL_NUM_THREADS=1`, `export OPENBLAS_NUM_THREADS=1`
|
||||
# Now 'filtered_df' is computed once and reused
|
||||
mean_val = filtered_df['column_A'].mean().compute()
|
||||
std_val = filtered_df['column_B'].std().compute()
|
||||
```
|
||||
|
||||
### 3.2 Memory Management
|
||||
### 5. Leverage Dask-Joblib for Scikit-learn
|
||||
|
||||
* **Monitor memory usage:** Use the Dask Dashboard to monitor memory usage and identify memory leaks.
|
||||
* **Garbage collection:** Explicitly call `gc.collect()` to free up memory that is no longer being used.
|
||||
* **Avoid creating unnecessary copies of data:** Use in-place operations whenever possible to avoid creating unnecessary copies of data.
|
||||
* **Use `dask.cache` for caching intermediate results:** Cache intermediate results to avoid recomputing them. This can significantly improve performance for iterative algorithms.
|
||||
For scikit-learn models, use the Dask-Joblib backend to distribute training, grid search, or cross-validation across your Dask cluster. Wrap your scikit-learn code in `with joblib.parallel_backend('dask'):`.
|
||||
|
||||
### 3.3 Rendering Optimization
|
||||
**❌ BAD:** Running scikit-learn serially or with `n_jobs=-1` on a single machine when a cluster is available.
|
||||
|
||||
* **Optimize data transfer:** Reduce the amount of data that needs to be transferred for visualization. This can be done by downsampling the data or by aggregating it before transferring it.
|
||||
* **Use efficient plotting libraries:** Use plotting libraries that are optimized for large datasets, such as Datashader or HoloViews.
|
||||
```python
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
# ... data loading ...
|
||||
model = RandomForestClassifier()
|
||||
grid_search = GridSearchCV(model, param_grid, n_jobs=-1) # Only local parallelism
|
||||
grid_search.fit(X, y)
|
||||
```
|
||||
|
||||
### 3.4 Bundle Size Optimization
|
||||
**✅ GOOD:** Distribute scikit-learn workloads with Dask-Joblib.
|
||||
|
||||
* **Only import necessary Dask modules:** Only import the Dask modules that are needed by your application. This reduces the size of the application's dependencies.
|
||||
* **Use tree shaking:** Use a build tool that supports tree shaking to remove unused code from the application's bundle.
|
||||
```python
|
||||
from dask.distributed import Client
|
||||
import joblib
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
# ... data loading (can be Dask DataFrames/Arrays) ...
|
||||
|
||||
### 3.5 Lazy Loading
|
||||
with Client() as client: # Or connect to a remote cluster
|
||||
print(f"Dask Dashboard: {client.dashboard_link}")
|
||||
model = RandomForestClassifier()
|
||||
param_grid = {'n_estimators': [10, 50, 100], 'max_depth': [5, 10]}
|
||||
grid_search = GridSearchCV(model, param_grid, cv=3, n_jobs=-1) # n_jobs=-1 is fine here
|
||||
|
||||
* **Dask Delayed:** Dask Delayed allows lazy evaluation of computations by building a graph of tasks that are executed only when the result is needed.
|
||||
* **Dask DataFrames and Arrays:** These collections are lazily evaluated. Operations are not performed until `compute()` is called.
|
||||
with joblib.parallel_backend('dask'):
|
||||
grid_search.fit(X, y) # This will now use the Dask cluster
|
||||
print(f"Best parameters: {grid_search.best_params_}")
|
||||
```
|
||||
|
||||
## 4. Security Best Practices
|
||||
### 6. Avoid Mixing Eager Pandas/NumPy with Lazy Dask
|
||||
|
||||
### 4.1 Common Vulnerabilities
|
||||
Do not mix eager Pandas/NumPy objects with lazy Dask collections inside loops or frequently. This forces Dask to compute repeatedly, negating its benefits. Keep the entire workflow in Dask until the final result is needed.
|
||||
|
||||
* **Arbitrary code execution:** If Dask workers are exposed to untrusted networks, attackers may be able to execute arbitrary code on the workers.
|
||||
* **Data leakage:** Sensitive data may be leaked if Dask workers are not properly secured.
|
||||
**❌ BAD:** Iterating over Dask partitions and converting to Pandas.
|
||||
|
||||
### 4.2 Input Validation
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
df = dd.read_parquet("data/*.parquet")
|
||||
results = []
|
||||
for partition in df.partitions: # This forces computation of each partition
|
||||
pandas_df = partition.compute()
|
||||
results.append(pandas_df['col'].mean())
|
||||
final_result = sum(results)
|
||||
```
|
||||
|
||||
* **Validate data types:** Ensure that input data conforms to the expected types. Use schema validation libraries to enforce data quality.
|
||||
* **Sanitize input data:** Sanitize input data to prevent injection attacks.
|
||||
**✅ GOOD:** Use Dask's built-in methods or `map_partitions`/`map_blocks`.
|
||||
|
||||
### 4.3 Authentication and Authorization
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
df = dd.read_parquet("data/*.parquet")
|
||||
|
||||
* **Use Dask's authentication mechanisms:** Use Dask's authentication mechanisms to secure access to the Dask cluster. Dask supports various authentication methods, including password-based authentication and Kerberos authentication.
|
||||
* **Implement authorization policies:** Implement authorization policies to restrict access to sensitive data and resources.
|
||||
# Use Dask's native operations
|
||||
mean_col = df['col'].mean().compute()
|
||||
|
||||
### 4.4 Data Protection
|
||||
# Or map a function over partitions if custom logic is needed
|
||||
def process_partition(part):
|
||||
return part['col'].mean()
|
||||
|
||||
* **Encrypt sensitive data:** Encrypt sensitive data at rest and in transit.
|
||||
* **Use secure storage:** Store data in secure storage systems with appropriate access controls.
|
||||
* **Regularly back up data:** Regularly back up data to protect against data loss.
|
||||
mean_col_mapped = df.map_partitions(process_partition, meta=('col', 'float64')).compute()
|
||||
```
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
### 7. Use Efficient Binary Formats
|
||||
|
||||
* **Use HTTPS:** Use HTTPS to encrypt communication between clients and the Dask cluster.
|
||||
* **Use strong TLS ciphers:** Configure the Dask cluster to use strong TLS ciphers.
|
||||
Prefer binary data formats like Parquet or Zarr over CSV or JSON. These formats are column-oriented, support schema evolution, and enable efficient filtering and projection, reducing I/O and serialization overhead.
|
||||
|
||||
## 5. Testing Approaches
|
||||
**❌ BAD:** Reading large CSV files.
|
||||
|
||||
### 5.1 Unit Testing
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
# CSV is slow to read, especially with many columns or complex types
|
||||
df = dd.read_csv("s3://bucket/large_data.csv")
|
||||
```
|
||||
|
||||
* **Test individual functions and classes:** Write unit tests for individual functions and classes to ensure they behave as expected.
|
||||
* **Use a testing framework:** Use a testing framework such as pytest or unittest to organize and run tests.
|
||||
* **Mock external dependencies:** Mock external dependencies to isolate the code being tested.
|
||||
**✅ GOOD:** Use Parquet or Zarr.
|
||||
|
||||
### 5.2 Integration Testing
|
||||
```python
|
||||
import dask.dataframe as dd
|
||||
# Parquet is highly optimized for Dask DataFrames
|
||||
df = dd.read_parquet("s3://bucket/large_data.parquet")
|
||||
|
||||
* **Test interactions between components:** Write integration tests to verify that different components of the Dask application work together correctly.
|
||||
* **Test end-to-end workflows:** Write end-to-end tests to verify that the entire Dask application works as expected.
|
||||
# Zarr is excellent for Dask Arrays
|
||||
import dask.array as da
|
||||
arr = da.from_zarr("s3://bucket/large_array.zarr")
|
||||
```
|
||||
|
||||
### 5.3 End-to-End Testing
|
||||
## Advanced Considerations
|
||||
|
||||
* **Simulate real-world scenarios:** Design end-to-end tests that simulate real-world scenarios to ensure the Dask application can handle complex data and workloads.
|
||||
* **Test performance:** Include performance tests to verify that the Dask application meets performance requirements.
|
||||
### 8. Monitor with the Dask Dashboard
|
||||
|
||||
### 5.4 Test Organization
|
||||
The Dask dashboard (`client.dashboard_link`) is your most valuable tool for debugging and optimizing Dask workloads. Use it to identify bottlenecks, visualize task graphs, spot memory issues, and understand worker activity.
|
||||
|
||||
* **Separate test directory:** Create a separate `tests` directory to store all tests.
|
||||
* **Mirror the source code structure:** Organize tests in a way that mirrors the source code structure. This makes it easier to find the tests for a particular module.
|
||||
**✅ GOOD:** Always have the dashboard open when developing Dask code.
|
||||
|
||||
### 5.5 Mocking and Stubbing
|
||||
```python
|
||||
from dask.distributed import Client, LocalCluster
|
||||
with LocalCluster() as cluster:
|
||||
with Client(cluster) as client:
|
||||
print(f"Dask Dashboard: {client.dashboard_link}")
|
||||
# Run your Dask code and observe the dashboard
|
||||
import dask.array as da
|
||||
x = da.random.random((10000, 10000), chunks=(1000, 1000))
|
||||
y = x.mean(axis=1)
|
||||
y.compute() # Watch the dashboard during computation
|
||||
```
|
||||
|
||||
* **Use the `unittest.mock` module:** Use the `unittest.mock` module to create mock objects for testing.
|
||||
* **Mock Dask collections:** Mock Dask DataFrames and Arrays to isolate the code being tested.
|
||||
* **Use dependency injection:** Use dependency injection to make it easier to mock dependencies.
|
||||
### 9. Manage Graph Size
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
Very large task graphs (millions of tasks) can overwhelm the scheduler. If your graph becomes too large, consider:
|
||||
* **Increasing chunk size:** Reduces the number of partitions/tasks.
|
||||
* **Fusing operations:** Combine multiple small operations into a single `map_partitions`/`map_blocks` function.
|
||||
* **Breaking up computation:** For extremely large workloads, compute and save intermediate results in stages.
|
||||
|
||||
### 6.1 Frequent Mistakes
|
||||
**❌ BAD:** Chaining many fine-grained operations on tiny chunks.
|
||||
|
||||
* **Not understanding lazy evaluation:** Dask operations are lazy, meaning they are not executed until `compute()` is called. This can lead to unexpected behavior if you are not aware of it.
|
||||
* **Incorrect chunk size selection:** Choosing the wrong chunk size can significantly impact performance. Experiment with different chunk sizes to find the optimal value for your workload.
|
||||
* **Not persisting intermediate results:** Not persisting intermediate results can lead to repeated computations and poor performance.
|
||||
* **Not using the Dask Dashboard:** The Dask Dashboard provides valuable insights into the behavior of your Dask application. Use it to monitor task execution, resource usage, and identify performance bottlenecks.
|
||||
```python
|
||||
# This creates a very deep graph with many small tasks
|
||||
df = dd.read_parquet("data/*.parquet", blocksize="1MB") # Too small chunks
|
||||
for col in df.columns:
|
||||
df[col] = df[col] * 2 + 1 # Many small operations
|
||||
df.compute()
|
||||
```
|
||||
|
||||
### 6.2 Edge Cases
|
||||
**✅ GOOD:** Use `map_partitions` for complex, per-partition logic.
|
||||
|
||||
* **Empty DataFrames:** Be aware of how Dask handles empty DataFrames. Operations on empty DataFrames may return unexpected results.
|
||||
* **Missing data:** Be aware of how Dask handles missing data (NaN values). Operations on DataFrames and Arrays with missing data may return unexpected results.
|
||||
```python
|
||||
def complex_transform(partition):
|
||||
# Perform multiple operations within a single Pandas function call
|
||||
for col in partition.columns:
|
||||
partition[col] = partition[col] * 2 + 1
|
||||
return partition
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
df = dd.read_parquet("data/*.parquet", blocksize="256MB") # Appropriate chunk size
|
||||
df_transformed = df.map_partitions(complex_transform, meta=df).compute()
|
||||
```
|
||||
|
||||
* **API changes:** Dask's API may change between versions. Be sure to consult the Dask documentation for the version you are using.
|
||||
* **Bug fixes:** New versions of Dask may include bug fixes that address issues you are experiencing. Consider upgrading to the latest version of Dask.
|
||||
### 10. Type Hints
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
Use type hints consistently in your Dask-related functions, especially for `map_partitions` and `map_blocks` callbacks. This improves readability, maintainability, and enables static analysis tools to catch errors early.
|
||||
|
||||
* **Pandas and NumPy versions:** Dask is compatible with specific versions of Pandas and NumPy. Be sure to use compatible versions of these libraries.
|
||||
* **Other libraries:** Dask may have compatibility issues with other libraries. Consult the Dask documentation for compatibility information.
|
||||
**✅ GOOD:** Type-hinting functions passed to Dask.
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
```python
|
||||
import pandas as pd
|
||||
import dask.dataframe as dd
|
||||
|
||||
* **Use the Dask Dashboard:** Use the Dask Dashboard to monitor task execution and identify errors.
|
||||
* **Set breakpoints:** Set breakpoints in your code to debug it using a debugger such as pdb or ipdb.
|
||||
* **Log messages:** Add log messages to your code to track the execution flow and identify errors.
|
||||
* **Use `dask.visualize()`:** Use `dask.visualize()` to visualize the Dask task graph. This can help you understand how Dask is executing your code and identify potential problems.
|
||||
def process_chunk(df: pd.DataFrame) -> pd.DataFrame:
|
||||
"""Applies a transformation to a Pandas DataFrame chunk."""
|
||||
return df[df['value'] > 0.5] * 2
|
||||
|
||||
## 7. Tooling and Environment
|
||||
ddf = dd.read_parquet("data/*.parquet")
|
||||
result_ddf = ddf.map_partitions(process_chunk, meta=ddf)
|
||||
```
|
||||
|
||||
### 7.1 Recommended Development Tools
|
||||
### 11. Testing Approaches
|
||||
|
||||
* **IDE:** VS Code, PyCharm, or other Python IDE.
|
||||
* **Dask Dashboard:** The Dask Dashboard is an essential tool for monitoring Dask applications.
|
||||
* **Debugging tools:** pdb, ipdb, or other Python debuggers.
|
||||
* **Profiling tools:** cProfile or other Python profiling tools.
|
||||
When testing Dask collections, use `dask.array.utils.assert_eq` for Dask Arrays and `dask.dataframe.utils.assert_eq` for Dask DataFrames. These functions compare Dask collections against their NumPy/Pandas equivalents, ensuring correctness of lazy computations.
|
||||
|
||||
### 7.2 Build Configuration
|
||||
**✅ GOOD:** Using Dask's assertion utilities.
|
||||
|
||||
* **Use `pyproject.toml`:** Use `pyproject.toml` to specify the project's build dependencies and configuration.
|
||||
* **Use a virtual environment:** Use a virtual environment to isolate the project's dependencies.
|
||||
```python
|
||||
import numpy as np
|
||||
import dask.array as da
|
||||
from dask.array.utils import assert_eq
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
def test_array_sum():
|
||||
np_arr = np.array([1, 2, 3, 4, 5])
|
||||
da_arr = da.from_array(np_arr, chunks=2)
|
||||
assert_eq(np_arr.sum(), da_arr.sum())
|
||||
|
||||
* **Use a linter:** Use a linter such as pylint or flake8 to check the code for style errors and potential problems.
|
||||
* **Use a formatter:** Use a formatter such as black or autopep8 to automatically format the code.
|
||||
import pandas as pd
|
||||
import dask.dataframe as dd
|
||||
from dask.dataframe.utils import assert_eq
|
||||
|
||||
### 7.4 Deployment Best Practices
|
||||
|
||||
* **Choose the right Dask scheduler:** Choose the appropriate Dask scheduler for your deployment environment. The distributed scheduler is best for large-scale deployments.
|
||||
* **Configure the Dask cluster:** Configure the Dask cluster to meet the needs of your application. Consider factors such as the number of workers, the amount of memory per worker, and the network bandwidth.
|
||||
* **Monitor the Dask cluster:** Monitor the Dask cluster to ensure it is running correctly and meeting performance requirements.
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
|
||||
* **Automate testing:** Automate the testing process using a CI/CD system such as Jenkins or GitLab CI.
|
||||
* **Automate deployment:** Automate the deployment process using a CI/CD system.
|
||||
|
||||
By adhering to these comprehensive guidelines, you can develop robust, efficient, and maintainable Dask applications that leverage the full power of parallel and distributed computing.
|
||||
def test_dataframe_mean():
|
||||
pd_df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
|
||||
dd_df = dd.from_pandas(pd_df, npartitions=2)
|
||||
assert_eq(pd_df.mean(), dd_df.mean())
|
||||
```
|
||||
Reference in New Issue
Block a user