Communitygithub.com

pydicom

使用 pydicom 讀取、檢查、寫入、轉換,並安全地預檢本機的 DICOM 資料集和像素資料。適用於 DICOM 元資料、傳輸語法、壓縮外掛程式、影格、私有元素、JSON 及有界限的去識別化審查。

pydicom 是什麼?

此技能提供一個基於 pydicom 3.0.2 的完整且安全的 DICOM 工作流程,專門為 AI Agent 設計。它能處理 DICOM 資料集的讀寫、像素處理、元資料檢查及去識別化,並內建嚴格的安全邊界,防止在未經授權的情況下暴露受保護的健康資訊 (PHI)。此技能支援主要 DICOM 操作,如安全的資料集讀取、傳輸語法檢查、像素影格規劃、壓縮/解壓縮及偽匿名化,並提供多個無依賴的 CLI 工具作為輔助。所有操作皆針對本機檔案,無網路存取,使 AI Agent 能在隔離環境中安全地處理醫療影像資料。

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

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

pydicom 是做什麼的?

使用 pydicom 進行 DICOM 資料集的輸入/輸出 (I/O) 和像素處理。此處審查的當前穩定版本為 3.0.2。它修復了 CVE-2026-32711,這是一個精心製作的 DICOMDIR 路徑遍歷問題。pydicom 3.0.2 聲明需要 Python >=3.10;其內建的 DICOM 字典版本為 2024c,而現行的 DICOM 標準可能更新。

強制性安全邊界

  • 僅限於處理使用者有權存取的本機資料。
  • DICOM 元資料、檔案名稱、私有元素、覆蓋圖、結構化內容和像素可能包含受保護的健康資訊 (PHI)。
  • 預設情況下,絕不列印 Dataset、匯出完整元資料/JSON,或將元素值記錄到日誌中。應使用已記錄的允許清單和彙總輸出。
  • pydicom 是一個通用的 DICOM 框架,而非診斷檢視器。像素輸出、驗證、轉換和外掛程式的可用性不構成診斷聲明。
  • 去識別化是針對特定設定檔、目的、接收者、管轄區域和威脅情境而定。需要隱私/DICOM 專家的驗證。
  • 絕不聲稱標籤移除腳本符合 DICOM PS3.15、HIPAA、GDPR 或其他法規。應保留原始檔案並稽核衍生輸出。
  • 將確定性的假名化金鑰和 UID 映射視為重新識別機密:採用最小權限原則和加密/受管機密儲存,絕不將其提交、同步、記錄或與衍生檔共用,並定義備份、輪換、撤銷和銷毀程序。洩露的金鑰會使預期的隔離失效;輪換也會改變確定性的映射關係。
  • 在解析不受信任或異常大的資料集之前,設置明確的輸入檔案、檔案數量、影格數量、解碼位元組和輸出限制。

安裝

建立或啟用一個隔離環境,然後安裝經過審查的確定版本:

uv pip install "pydicom==3.0.2"

未壓縮的像素陣列和影像渲染:

uv pip install "pydicom==3.0.2" "numpy==2.5.1" "Pillow==12.3.0"

僅安裝部署所需的傳輸語法外掛程式:

# 透過 pylibjpeg 支援 JPEG/JPEG-LS、JPEG 2000/HTJ2K 和更快的 RLE
uv pip install "numpy==2.5.1" "pylibjpeg==2.1.0" \
  "pylibjpeg-libjpeg==2.4.0" "pylibjpeg-openjpeg==2.5.0" \
  "pylibjpeg-rle==2.2.0"

# JPEG-LS 編碼器/解碼器
uv pip install "numpy==2.5.1" "pyjpegls==1.5.1"

# 備用解碼器,提供特定平台的 wheel 套件
uv pip install "python-gdcm==3.2.6"

外掛程式的授權條款和 wheel 套件因套件/平台而異;請在部署前進行審查。Pillow 存在已知的解碼限制,且 pydicom 提醒必須獨立檢查外掛程式的輸出。

原生編解碼器的 wheel 套件會擴大供應鏈和記憶體安全的邊界。對於受控部署,請在可信的建置主機上解析這些確定的版本鎖定,鎖定並驗證 wheel 套件的雜湊/來源,將經批准的工件鏡像到內部環境,對其進行掃描,並透過雜湊強制執行來安裝,而不是在執行時期從公開索引解析。

選擇工作流程

  1. 需要彙總概覽:執行 scripts/extract_metadata.py。
  2. 需要有限制的技術檢查:執行 scripts/dicom_inventory.py。
  3. 需要編解碼器部署預檢:執行 scripts/transfer_syntax_inspector.py。
  4. 需要影格/記憶體規劃:執行 scripts/pixel_frame_planner.py。
  5. 需要一個非診斷的渲染影格:執行 scripts/dicom_to_image.py。
  6. 需要一個假名化的衍生檔:閱讀去識別化章節,建立一個經站點審查的行動設定檔,然後依序執行 scripts/anonymize_dicom.py 和 scripts/deidentification_audit.py。
  7. 需要檢查一個敏感的 UID 映射:執行 scripts/uid_mapping_validator.py。

安全地讀取資料集

dcmread() 會回傳一個 FileDataset,它是 Dataset 的子類別,包含檔案格式的狀態,例如 file_meta、前導碼和原始編碼。

from pathlib import Path
import pydicom

path = Path("authorized/input.dcm")
ds = pydicom.dcmread(
    path,
    stop_before_pixels=True,
    specific_tags=[
        "SOPClassUID",
        "Modality",
        "Rows",
        "Columns",
        "NumberOfFrames",
    ],
)

technical = {
    "sop_class": ds.get("SOPClassUID"),
    "modality": ds.get("Modality"),
    "rows": ds.get("Rows"),
    "columns": ds.get("Columns"),
}

使用:

  • stop_before_pixels=True 用於僅處理元資料。
  • specific_tags=[...] 用於最小允許清單。
  • defer_size="1 MiB" 用於後續寫入時必須保留的大數值。
  • force=False (預設值)。force=True 僅繞過檔案格式標頭檢查;它並不證明位元組是有效的 DICOM。

不要對臨床資料呼叫 print(ds)、repr(ds) 或將值迭代到日誌中。

Dataset、DataElement 和序列

透過關鍵字存取標準元素並檢查是否存在:

modality = ds.get("Modality", "UNSPECIFIED")
if "ReferencedImageSequence" in ds:
    for item in ds.ReferencedImageSequence:
        referenced_class = item.get("ReferencedSOPClassUID")

透過標籤存取,例如 ds[0x0010, 0x0010],會回傳一個 DataElement;其 .value 是分開的。Sequence 的行為類似於一個巢狀 Dataset 項目的列表。隱私操作必須遞迴到每個序列項目中,而不僅是頂層。

建立檔案時,使用 FileMetaDataset 處理群組 0002,保持資料集和檔案元資料的 SOP UID 一致,設定傳輸語法 UID,並以強制檔案格式寫入:

from pydicom import dcmwrite
from pydicom.dataset import FileDataset, FileMetaDataset
from pydicom.uid import CTImageStorage, ExplicitVRLittleEndian, generate_uid

meta = FileMetaDataset()
meta.MediaStorageSOPClassUID = CTImageStorage
meta.MediaStorageSOPInstanceUID = generate_uid()
meta.TransferSyntaxUID = ExplicitVRLittleEndian

ds = FileDataset(None, {}, file_meta=meta, preamble=b"\0" * 128)
ds.SOPClassUID = meta.MediaStorageSOPClassUID
ds.SOPInstanceUID = meta.MediaStorageSOPInstanceUID
# 在寫入前,新增所選 IOD 要求的所有屬性。
dcmwrite("new.dcm", ds, enforce_file_format=True, overwrite=False)

write_like_original 在 pydicom 3.0 中已被棄用;請使用 enforce_file_format。成功的寫入不等於完整的 PS3.3 IOD 符合性。

UID 和傳輸語法

檔案元資訊傳輸語法 UID 控制資料集編碼和像素壓縮:

ts = ds.file_meta.TransferSyntaxUID
summary = {
    "uid": str(ts),
    "name": ts.name,
    "compressed": ts.is_compressed,
    "implicit_vr": ts.is_implicit_VR,
    "little_endian": ts.is_little_endian,
}

pydicom 3.0 在傳統資料集標誌之前,從傳輸語法 UID 選擇寫入編碼。在假名化過程中,不要取代結構性 UID(傳輸語法、SOP 類別或編碼方案 UID)。實例/參考 UID 的替換必須是一對一的,並且在完整的宣告範圍內保持一致。

在進行壓縮、解壓縮或封裝之前,請閱讀 references/transfer_syntaxes.md。

像素資料和影格

穩定的 pydicom.pixels API 支援基於路徑、特定影格解碼:

from pydicom.pixels import pixel_array

# 僅讀取來源允許的選定影格。
frame = pixel_array("authorized/image.dcm", index=0, raw=False)

形狀語意:

  • 灰階單影格:(rows, columns)
  • 灰階多影格:(frames, rows, columns)
  • 彩色單影格:(rows, columns, samples)
  • 彩色多影格:(frames, rows, columns, samples)

raw=False 會在可能的情況下將 YCbCr 像素資料轉換為 RGB;raw=True 則在執行必要的極簡處理後保留解碼後的色彩空間。使用 iter_pixels(path, indices=[...]) 進行有界限的多影格迭代。

對於灰階顯示,請按此順序套用轉換:

from pydicom.pixels import apply_modality_lut, apply_voi_lut

modality_values = apply_modality_lut(frame, ds)
display_values = apply_voi_lut(modality_values, ds, index=0)

模態 LUT/重新縮放和 VOI/窗寬窗位會改變顯示/數值語意。MONOCHROME1 可能需要反轉顯示。調色盤色彩需要 apply_color_lut()。表現狀態和 ICC 行為可能需要經過驗證的檢視器。絕不要使用逐影格的最小/最大正規化進行定量分析。

壓縮、解壓縮和封裝

  • 存取 pixel_array 會視需要進行解碼,但不會改變資料集。
  • Dataset.decompress() 會就地更改像素資料,設定 Explicit VR Little Endian,更新影像元資料,並預設產生一個新的 SOP 實例 UID。
  • Dataset.compress(uid) 會就地更改像素資料和傳輸語法,並預設產生一個新的 SOP 實例 UID。
  • pydicom 3.0 內建/找到的編碼器涵蓋穩定的外掛程式矩陣中記錄的 RLE Lossless、JPEG-LS 和 JPEG 2000 組合。
  • 每個壓縮影格會個別編碼,然後進行封裝。對於外部編碼的影格,請使用 encapsulate() 或 encapsulate_extended()。
  • 使用當前的 pydicom.encaps.generate_frames() 或 get_frame() 讀取影格;舊的封裝產生器名稱已在 pydicom 4 中棄用。

始終先檢查能力,限制解碼的位元組/影格,並獨立驗證像素的正確性。有損壓縮的可接受性不在 pydicom 和 DICOM 編碼規範的範圍內。

DICOM JSON 和私有元素

Dataset.to_json()、to_json_dict() 和 Dataset.from_json() 實作了 DICOM JSON 模型,但 pydicom 文件指出 JSON 支援仍處於測試階段。完整的 JSON 可能會內聯二進位資料並暴露每個識別碼和像素酬載。不要將其作為元資料報告發出。BulkDataURI 處理程式會引入單獨的儲存、授權和檢索義務。

私有元素未標準化,可能包含 PHI:

# 遞迴移除,但本身不足以去識別化。
ds.remove_private_tags()

僅在經過審查的明確安全私有政策下保留私有元素。請閱讀 references/common_tags.md 以了解標籤存取、隱私類別和標準指標。

去識別化工作流程

DICOM PS3.15 Annex E 明確指出,保密設定檔無法保證移除所有識別資訊,也無法取代完整的去識別化過程。

  1. 定義目的、接收者、連結需求、法規、威脅模型和可接受的重新識別風險。
  2. 選擇基本應用層級保密設定檔和所需的選項(像素、可識別的視覺特徵、圖形、結構化內容、描述子、時間資訊、患者特徵、設備、機構、UID 和安全私有資料)。
  3. 在受控儲存中保持原始物件不變。
  4. 遞迴套用每個動作,包括巢狀序列。
  5. 在完整範圍內一致地替換實例/參考 UID;保留結構性 UID。
  6. 明確決定日期/時間的處理方式。固定偏移可以保留間隔,但部分日期、時區、獨立時間、閏日、縱向連結和外部事件都需要經過審查的政策。
  7. 檢查像素、覆蓋圖、圖形、結構化內容和可識別的視覺特徵。不要因為缺少元資料就推斷像素乾淨,也不要在未經驗證的情況下設定 BurnedInAnnotation=NO。
  8. 重建檔案元資訊和前導碼以防止資訊洩漏。
  9. 執行技術驗證和去識別化稽核,然後進行專家驗證和記錄的風險審查。

內建腳本會刻意將 PatientIdentityRemoved 設定為 NO,因為它無法確定去識別化是否成功。

輔助 CLI 工具

所有 --help 路徑都是無依賴的。這些工具不執行任何網路存取,也不會發出超出狹義技術允許清單的 DICOM 值。

內建內容包含兩個連結的參考文件、記錄的輔助腳本和合成測試。pydicom 執行時期依賴項是從鎖定的 PyPI 版本安裝的。

# 修訂後的彙總元資料
python scripts/extract_metadata.py authorized/ --recursive

# 僅限元資料的技術清單
python scripts/dicom_inventory.py authorized/ --recursive

# 已安裝的編解碼器/外掛程式能力
python scripts/transfer_syntax_inspector.py --input authorized/image.dcm

# 影格形狀、位元組和轉換計畫
python scripts/pixel_frame_planner.py authorized/image.dcm --frames 0,2-4

# 一個非診斷影格
python scripts/dicom_to_image.py authorized/image.dcm frame.png \
  --acknowledge-pixel-phi

# 建立一個密鑰,然後建立一個有範圍的假名化衍生檔及其稽核報告
python scripts/anonymize_dicom.py --generate-uid-key project.key
python scripts/anonymize_dicom.py authorized/in.dcm derived/out.dcm \
  --uid-key-file project.key --uid-scope export-v1 \
  --audit-report derived/out.audit.json

# 稽核候選元資料;不解壓像素
python scripts/deidentification_audit.py derived/out.dcm

# 驗證一個明確請求的敏感 UID 映射
python scripts/uid_mapping_validator.py derived/uid-map.json \
  --uid-key-file project.key --uid-scope export-v1

產生的原始金鑰檔案是一種受控的本機便利功能,並以僅擁有者可讀取的權限建立。對於生產環境,請從批准的機密管理器中將金鑰位元組具體化到一個鎖定的臨時檔案中,限制只有去識別化服務可以存取,並在使用後安全地移除。將任何可選的 UID 映射與衍生檔分開儲存;它直接連結了原始和替代的識別碼。

pydicom 3.0 遷移注意事項

  • read_file() 和 write_file() 已被移除;請使用 dcmread() 和 dcmwrite()。
  • write_like_original 已被棄用;請使用 enforce_file_format。
  • pydicom.pixel_data_handlers 已被棄用,將在 v4 中移除;請使用 pydicom.pixels。
  • Dataset.pixel_array 預設使用新的像素後端,並在可能的情況下將 YCbCr 轉換為 RGB。
  • JPEGLossless 現在表示 UID 1.2.840.10008.1.2.4.57;JPEGLosslessSV1 表示 .70。
  • Dataset.is_little_endian 和 is_implicit_VR 已在 v4 中棄用。

來源 (驗證日期:2026-07-23)

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.

相關技能