Communitygithub.com

Dask

用于超内存 pandas/NumPy 工作负载的分布式计算工具。当需要将现有 pandas/NumPy 代码扩展到内存限制以上或在集群上运行时使用。适合并行文件处理、分布式机器学习以及与现有 pandas 代码集成。对于单机核外分析可使用 vaex;对于内存内高速处理可使用 polars。

Dask 是什么?

Dask 是一个 Python 并行和分布式计算库,使数据科学家和工程师能够将 pandas 和 NumPy 工作负载扩展到单机 100 GiB 以上,以及跨集群数千 TiB 级别。它提供五个核心组件:DataFrames(并行化 pandas 操作)、Arrays(并行化 NumPy 操作)、Bags(非结构化文本/JSON 处理)、Futures(自定义并行任务)和 Schedulers(控制执行后端:线程、进程、分布式)。关键优势包括:惰性执行以构建优化的任务图、与现有 pandas/NumPy 代码无缝集成、内置监控仪表板(分布式调度器)以及通过简单的 API 调用将计算从单机扩展到集群的能力。最佳实践强调使用适当的数据格式(Parquet、Zarr)、选择 ~100 MB 的块大小、避免重复的 compute() 调用,以及使用仪表板进行性能分析。Dask 支持 Python 3.10+,需要 PyArrow 16+ 用于 DataFrame I/O,并可与 XArray 和 Dask-ML 等库集成。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/skills/dask

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Dask 是做什么的?

概述

Dask 是一个用于并行和分布式计算的 Python 库,提供三个关键能力:

  • 超内存执行:在单机上处理超出可用 RAM 的数据
  • 并行处理:跨多核提高计算速度
  • 分布式计算:跨多台机器支持 TB 级数据集

Dask 可从笔记本电脑(处理约 100 GiB)扩展到集群(处理约 100 TiB),同时保持熟悉的 Python API。

当前上游版本: dask 2026.3.0(PyPI,2026 年 3 月)。文档:docs.dask.org。自 2025.1.0 起,基于表达式的 DataFrame API 和查询计划是唯一实现 — 不要单独安装 dask-expr 或设置 dataframe.query-planning: False。

快速开始

安装

uv pip install "dask>=2025.1"

对于典型的 pandas/NumPy 工作负载,包含分布式调度器和仪表板:

uv pip install "dask[complete]"

远程对象存储(S3、GCS、Azure):

uv pip install s3fs    # s3:// 路径
uv pip install gcsfs   # gs:// 路径

需要 Python 3.10+(2024.12 版本已放弃 3.9 支持)。DataFrame I/O 需要 PyArrow 16+(自 dask 2026.1.2 起)。

何时使用此技能

当以下情况时应使用此技能:

  • 处理超出可用 RAM 的数据集
  • 将 pandas 或 NumPy 操作扩展到更大的数据集
  • 并行化计算以提高性能
  • 高效处理多个文件(CSV、Parquet、JSON、文本日志)
  • 构建具有任务依赖关系的自定义并行工作流
  • 将工作负载分布到多个核心或机器上

核心能力

Dask 提供五个主要组件,每个适用于不同的用例:

1. DataFrames - 并行 Pandas 操作

目的:通过并行处理将 pandas 操作扩展到更大的数据集。

何时使用:

  • 表格数据超出可用 RAM
  • 需要一起处理多个 CSV/Parquet 文件
  • pandas 操作缓慢需要并行化
  • 从 pandas 原型扩展到生产环境

参考文档:关于 Dask DataFrames 的全面指南,请参阅 references/dataframes.md,其中包括:

  • 读取数据(单个文件、多个文件、通配符模式)
  • 常见操作(过滤、分组、连接、聚合)
  • 使用 map_partitions 的自定义操作
  • 性能优化技巧
  • 常见模式(ETL、时间序列、多文件处理)

快速示例:

import dask.dataframe as dd

# 将多个文件读取为单个 DataFrame
ddf = dd.read_csv('data/2024-*.csv')

# 操作在调用 compute() 之前是惰性的
filtered = ddf[ddf['value'] > 100]
result = filtered.groupby('category').mean().compute()

关键点:

  • 操作在调用 .compute() 之前是惰性的(构建任务图)
  • 对于高效的自定义操作使用 map_partitions
  • 在处理来自其他来源的结构化数据时,尽早转换为 DataFrame

2. Arrays - 并行 NumPy 操作

目的:使用分块算法将 NumPy 能力扩展到超出内存的数据集。

何时使用:

  • 数组超出可用 RAM
  • NumPy 操作需要并行化
  • 处理科学数据集(HDF5、Zarr、NetCDF)
  • 需要并行线性代数或数组操作

参考文档:关于 Dask Arrays 的全面指南,请参阅 references/arrays.md,其中包括:

  • 创建数组(从 NumPy、随机、从磁盘)
  • 分块策略和优化
  • 常见操作(算术、归约、线性代数)
  • 使用 map_blocks 的自定义操作
  • 与 HDF5、Zarr 和 XArray 的集成

快速示例:

import dask.array as da

# 创建带分块的大型数组
x = da.random.random((100000, 100000), chunks=(10000, 10000))

# 操作是惰性的
y = x + 100
z = y.mean(axis=0)

# 计算结果
result = z.compute()

关键点:

  • 块大小至关重要(目标每块约 100 MB)
  • 操作在块上并行执行
  • 在需要时对数据进行重新分块以实现高效操作
  • 对于 Dask 中没有的操作使用 map_blocks

3. Bags - 非结构化数据的并行处理

目的:使用函数式操作处理非结构化或半结构化数据(文本、JSON、日志)。

何时使用:

  • 处理文本文件、日志或 JSON 记录
  • 在结构化分析之前进行数据清洗和 ETL
  • 处理不适合数组/DataFrame 格式的 Python 对象
  • 需要内存高效的流式处理

参考文档:关于 Dask Bags 的全面指南,请参阅 references/bags.md,其中包括:

  • 读取文本和 JSON 文件
  • 函数式操作(map、filter、fold、groupby)
  • 转换为 DataFrames
  • 常见模式(日志分析、JSON 处理、文本处理)
  • 性能考虑

快速示例:

import dask.bag as db
import json

# 读取并解析 JSON 文件
bag = db.read_text('logs/*.json').map(json.loads)

# 过滤和转换
valid = bag.filter(lambda x: x['status'] == 'valid')
processed = valid.map(lambda x: {'id': x['id'], 'value': x['value']})

# 转换为 DataFrame 进行分析
ddf = processed.to_dataframe()

关键点:

  • 用于初始数据清洗,然后转换为 DataFrame/Array
  • 使用 foldby 代替 groupby 以获得更好的性能
  • 操作是流式且内存高效的
  • 为复杂操作转换为结构化格式(DataFrame)

4. Futures - 基于任务的并行化

目的:构建自定义并行工作流,对任务执行和依赖关系进行精细控制。

何时使用:

  • 构建动态、演化的工作流
  • 需要立即执行任务(非惰性)
  • 计算依赖于运行时条件
  • 实现自定义并行算法
  • 需要带状态的计算

参考文档:关于 Dask Futures 的全面指南,请参阅 references/futures.md,其中包括:

  • 设置分布式客户端
  • 提交任务并操作 futures
  • 任务依赖关系和数据移动
  • 高级协调(队列、锁、事件、actors)
  • 常见模式(参数扫描、动态任务、迭代算法)

快速示例:

from dask.distributed import Client

client = Client()  # 创建本地集群

# 提交任务(立即执行)
def process(x):
    return x ** 2

futures = client.map(process, range(100))

# 收集结果
results = client.gather(futures)

client.close()

关键点:

  • 需要分布式客户端(即使是单机)
  • 提交时任务立即执行
  • 预先分散大型数据以避免重复传输
  • 每个任务约 1ms 开销(不适合数百万个微小任务)
  • 对有状态工作流使用 actors

5. Schedulers - 执行后端

目的:控制 Dask 任务的执行位置和方式(线程、进程、分布式)。

何时选择调度器:

  • 线程(默认):NumPy/Pandas 操作、释放 GIL 的库、共享内存有利
  • 进程:纯 Python 代码、文本处理、GIL 绑定操作
  • 同步:使用 pdb 调试、性能分析、理解错误
  • 分布式:需要仪表板、多机集群、高级功能

参考文档:关于 Dask Schedulers 的全面指南,请参阅 references/schedulers.md,其中包括:

  • 详细的调度器描述和特性
  • 配置方法(全局、上下文管理器、每次计算)
  • 性能考虑和开销
  • 常见模式和故障排除
  • 线程配置以获得最佳性能

快速示例:

import dask
import dask.dataframe as dd

# 对 DataFrame 使用线程(默认,适用于数值计算)
ddf = dd.read_csv('data.csv')
result1 = ddf.mean().compute()  # 使用线程

# 对 Python 密集型工作使用进程
import dask.bag as db
bag = db.read_text('logs/*.txt')
result2 = bag.map(python_function).compute(scheduler='processes')

# 调试时使用同步
import dask.config as config
dask.config.set(scheduler='synchronous')
result3 = problematic_computation.compute()  # 可以使用 pdb

# 用于监控和扩展的分布式
from dask.distributed import Client
client = Client()
result4 = computation.compute()  # 使用带仪表板的分布式

关键点:

  • 线程:开销最低(每个任务约 10 µs),最适合数值计算
  • 进程:避免 GIL(每个任务约 10 ms),最适合 Python 工作
  • 分布式:监控仪表板(每个任务约 1 ms),可扩展到集群
  • 可以在每次计算或全局范围内切换调度器

最佳实践

有关全面的性能优化指南、内存管理策略和要避免的常见陷阱,请参阅 references/best-practices.md。关键原则包括:

从更简单的解决方案开始

在使用 Dask 之前,请探索:

  • 更好的算法
  • 高效的文件格式(Parquet 代替 CSV)
  • 编译代码(Numba、Cython)
  • 数据采样

关键性能规则

1. 不要先在本地加载数据再交给 Dask

# 错误:先将所有数据加载到内存中
import pandas as pd
df = pd.read_csv('large.csv')
ddf = dd.from_pandas(df, npartitions=10)

# 正确:让 Dask 处理加载
import dask.dataframe as dd
ddf = dd.read_csv('large.csv')

2. 避免重复调用 compute()

# 错误:每次 compute 都是独立的
for item in items:
    result = dask_computation(item).compute()

# 正确:一次 compute 完成所有计算
computations = [dask_computation(item) for item in items]
results = dask.compute(*computations)

3. 不要构建过大的任务图

  • 如果有数百万个任务,则增加块大小
  • 使用 map_partitions/map_blocks 融合操作
  • 检查任务图大小:len(ddf.__dask_graph__())

4. 选择合适的块大小

  • 目标:每个块约 100 MB(或每个核心在工作者内存中包含 10 个块)
  • 太大:内存溢出
  • 太小:调度开销

5. 使用仪表板

from dask.distributed import Client
client = Client()
print(client.dashboard_link)  # 监控性能,识别瓶颈

常见工作流模式

ETL 流水线

import dask.dataframe as dd

# Extract:读取数据
ddf = dd.read_csv('raw_data/*.csv')

# Transform:清洗和处理
ddf = ddf[ddf['status'] == 'valid']
ddf['amount'] = ddf['amount'].astype('float64')
ddf = ddf.dropna(subset=['important_col'])

# Load:聚合和保存
summary = ddf.groupby('category').agg({'amount': ['sum', 'mean']})
summary.to_parquet('output/summary.parquet')

非结构化到结构化流水线

import dask.bag as db
import json

# 从 Bag 开始处理非结构化数据
bag = db.read_text('logs/*.json').map(json.loads)
bag = bag.filter(lambda x: x['status'] == 'valid')

# 转换为 DataFrame 进行结构化分析
ddf = bag.to_dataframe()
result = ddf.groupby('category').mean().compute()

大规模数组计算

import dask.array as da

# 加载或创建大型数组
x = da.from_zarr('large_dataset.zarr')

# 按块处理
normalized = (x - x.mean()) / x.std()

# 保存结果(使用 mode= 覆盖;zarr_array_kwargs 用于压缩)
da.to_zarr(normalized, 'normalized.zarr', mode='w')

自定义并行工作流

from dask.distributed import Client

client = Client()

# 提前分散大型数据集一次
data = client.scatter(large_dataset)

# 带依赖关系的并行处理
futures = []
for param in parameters:
    future = client.submit(process, data, param)
    futures.append(future)

# 收集结果
results = client.gather(futures)

选择合适的组件

使用此决策指南来选择适当的 Dask 组件:

数据类型:

  • 表格数据 → DataFrames
  • 数值数组 → Arrays
  • 文本/JSON/日志 → Bags(然后转换为 DataFrame)
  • 自定义 Python 对象 → Bags 或 Futures

操作类型:

  • 标准 pandas 操作 → DataFrames
  • 标准 NumPy 操作 → Arrays
  • 自定义并行任务 → Futures
  • 文本处理/ETL → Bags

控制级别:

  • 高级、自动 → DataFrames/Arrays
  • 低级、手动 → Futures

工作流类型:

  • 静态计算图 → DataFrames/Arrays/Bags
  • 动态、演化 → Futures

集成注意事项

文件格式

  • 高效:Parquet、HDF5、Zarr(列式、压缩、并行友好)
  • 兼容但较慢:CSV(仅用于初始导入)
  • 用于 Arrays:HDF5、Zarr、NetCDF

集合之间的转换

# Bag → DataFrame
ddf = bag.to_dataframe()

# DataFrame → Array(用于数值数据)
arr = ddf.to_dask_array(lengths=True)

# Array → DataFrame
ddf = dd.from_dask_array(arr, columns=['col1', 'col2'])

与其他库一起使用

  • XArray:使用带标签维度的 Dask 数组封装(地理空间、成像)
  • Dask-ML:具有 scikit-learn 兼容 API 的机器学习
  • Distributed:高级集群管理和监控

调试与开发

迭代开发工作流

  1. 使用同步调度器在小数据上测试:
dask.config.set(scheduler='synchronous')
result = computation.compute()  # 可以使用 pdb,易于调试
  1. 在样本上使用线程验证:
sample = ddf.head(1000)  # 小样本
# 测试逻辑,然后扩展到完整数据集
  1. 使用分布式进行监控扩展:
from dask.distributed import Client
client = Client()
print(client.dashboard_link)  # 监控性能
result = computation.compute()

常见问题

内存错误:

  • 减小块大小
  • 有策略地使用 persist() 并在完成后删除
  • 检查自定义函数中的内存泄漏

启动缓慢:

  • 任务图太大(增加块大小)
  • 使用 map_partitions 或 map_blocks 减少任务数量

并行化不佳:

  • 块太大(增加分区数)
  • 对 Python 代码使用线程(切换到进程)
  • 数据依赖项阻止并行执行

仪表板不显示:

  • 确保未禁用仪表板
  • 检查 client.dashboard_link 是否正确
  • 可能需要打开防火墙端口

参考文档

有关 Dask 生态系统的更多信息,请访问 docs.dask.org。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

adaptyv

How to use the Adaptyv Bio Foundry API and Python SDK for protein experiment design, submission, and results retrieval. Use this skill whenever the user mentions Adaptyv, Foundry API, protein binding assays, protein screening experiments, BLI/SPR assays, thermostability assays, or wants to submit protein sequences for experimental characterization. Also trigger when code imports `adaptyv`, `adaptyv_sdk`, or `FoundryClient`, or references `foundry-api-public.adaptyvbio.com`.

aeon

This skill should be used for time series machine learning tasks including classification, regression, clustering, forecasting, anomaly detection, segmentation, and similarity search. Use when working with temporal data, sequential patterns, or time-indexed observations requiring specialized algorithms beyond standard ML approaches. Particularly suited for univariate and multivariate time series analysis with scikit-learn compatible APIs.

alphagenome

Look up precomputed AlphaGenome Atlas effects for any GRCh38 single-nucleotide variant (AVI score with Phred and 18 SHAP feature attributions, plus raw and quantile scores for RNA-seq, DNase, ATAC, ChIP-TF, ChIP-histone, CAGE, PRO-cap, splicing, polyadenylation and contact-map tracks), score variants or scan windows on demand with the AlphaGenome model for human and mouse (variant scoring, in silico mutagenesis, REF-versus-ALT track prediction), and build Atlas website deep links. Use when the user mentions AlphaGenome, AlphaGenome Atlas, AVI or AlphaGenome Variant Impact, DeepMind variant effect prediction, or wants to prioritise or mechanistically interpret non-coding, regulatory, splicing, enhancer, promoter, or chromatin-accessibility effects of SNVs from a VCF, credible set, or region. Research use only; not a clinical tool.

analytical-method-validation

Plan, execute, and document validation, verification, and transfer of analytical procedures under the governing framework - ICH Q2(R2) and Q14, USP <1220>/<1225>/<1226>, ICH M10 bioanalytical, CLSI EP, or ISO/IEC 17025. Use for HPLC, LC-MS/MS, GC, CE, ICP-MS, dissolution, qNMR, qPCR, NIR, and ligand binding or cell-based assays whenever the question is whether a procedure is fit for its intended purpose. Triggers include

anndata

Data structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use cellxgene-census.

arbor

Autonomously improve a real artifact (code, training recipe, agent harness, data pipeline, prompt) against an objective and an evaluator, using Hypothesis Tree Refinement (HTR) from the Arbor paper. Use this whenever someone wants to iteratively optimize something over many experiments without overfitting — e.g.

arboreto

Infer gene regulatory networks (GRNs) from gene expression data using scalable algorithms (GRNBoost2, GENIE3). Use when analyzing transcriptomics data (bulk RNA-seq, single-cell RNA-seq) to identify transcription factor-target gene relationships and regulatory interactions. Supports distributed computation for large-scale datasets.

astropy

Core Python library for astronomy and astrophysics workflows that need Astropy APIs, including units/quantities, coordinates, FITS I/O, tables, time systems, WCS, and cosmology. Use when implementing or debugging astronomical data analysis code with Astropy.

autoskill

Observe the user

benchling-integration

Benchling Python SDK and REST API integration for registry entities, inventory, ELN entries, workflows, Benchling Apps, and Data Warehouse queries. Use when automating lab data with benchling-sdk or the v2 API.

bgpt-paper-search

Search scientific papers and retrieve structured experimental data extracted from full-text studies via the BGPT MCP server. Returns 25+ fields per paper including methods, results, sample sizes, quality scores, and conclusions. Use for literature reviews, evidence synthesis, and finding experimental details not available in abstracts alone.

bids

>

biopython

Comprehensive molecular biology toolkit. Use for sequence manipulation, file parsing (FASTA/GenBank/PDB), phylogenetics, and programmatic NCBI/PubMed access (Bio.Entrez). Best for batch processing, custom bioinformatics pipelines, BLAST automation. For quick lookups use gget; for multi-service integration use bioservices.

bioservices

Unified Python interface to 40+ bioinformatics services. Use when querying multiple databases (UniProt, KEGG, ChEMBL, Reactome) in a single workflow with consistent API. Best for cross-database analysis, ID mapping across services. For quick single-database lookups use gget; for sequence/file manipulation use biopython.

bulk-rnaseq

End-to-end bulk RNA-seq orchestrator — takes raw FASTQ reads through QC and trimming (FastQC, fastp/Trim Galore), alignment and quantification (STAR, Salmon, featureCounts), assembles a gene-level counts matrix, then hands off to differential expression (pydeseq2), pathway/GSEA enrichment (pathway-enrichment), and publication figures (scientific-visualization). Use whenever the user has bulk RNA-seq reads or quant output and wants a complete, reproducible differential-expression workflow — e.g.

cellxgene-census

Query the CZ CELLxGENE Census programmatically for versioned public single-cell and spatial transcriptomics data. Use when you need population-scale cell metadata, gene expression slices, Census summary counts, source H5AD URIs/downloads, embeddings, spatial Census data, or reference atlas comparisons across organisms, tissues, diseases, assays, and cell types. For analyzing your own local single-cell data use scanpy, anndata, or scvi-tools.

cirq

Google quantum computing framework. Use when targeting Google Quantum AI hardware, designing noise-aware circuits, or running quantum characterization experiments. Best for Google hardware, noise modeling, and low-level circuit design. For IBM hardware use qiskit; for quantum ML with autodiff use pennylane; for physics simulations use qutip.

citation-management

Comprehensive citation management for academic research. Search OpenAlex, PubMed, and Google Scholar for papers, extract accurate metadata, validate citations, and generate properly formatted BibTeX entries. This skill should be used when you need to find papers, verify citation information, convert DOIs to BibTeX, or ensure reference accuracy in scientific writing.

clinical-decision-support

Prepare and validate research-only clinical decision-support evaluation, evidence-profile, cohort, survival, biomarker/model, privacy, and governance artifacts. Use for aggregate or synthetic research documentation and traceability—not patient care or live clinical operation.

clinical-reports

Create safety-bounded draft structures and run local deterministic checks for clinical case, diagnostic, trial, safety, and aggregate research reports. Use only with synthetic, de-identified, or aggregate inputs and verified source-fact manifests; every output requires qualified review.

相关技能