Bits & Flames bitsandflames/fyron

DICOMDownloader

DICOMDownloader is Fyron's DICOMweb acquisition helper. It downloads individual series or full studies, writes DICOM or NIfTI outputs, and returns result metadata that can be stored beside a cohort table.

When To Use It

  • download a known StudyInstanceUID / SeriesInstanceUID,
  • batch-download imaging rows from a CSV/DataFrame,
  • fetch a full study when the relevant series is not known yet,
  • create reproducible manifests before local image processing.

Use Imaging after files are already local. Use DICOM SEG for segmentation objects and Synthetic Export for generated intensity volumes.

Core Concepts

ConceptMeaning
StudyInstanceUIDDICOM study identifier. Required for study and series downloads.
SeriesInstanceUIDDICOM series identifier. Required when downloading one known series.
download_idDeterministic SHA-256 identifier derived from the study UID alone or from study_uid_series_uid.
output_format"dicom" preserves source slices; "nifti" converts each series into a NIfTI volume.
reference DICOMOptional single source .dcm retained beside a NIfTI output for metadata/provenance.
study_layoutFull-study output style: hierarchical uses per-series folders; flat stores all series in one study folder.
result tableDataFrame with UIDs, download_id, output path, format, and error text.

API Contract

TopicContract
Input shapeStudy/series UID strings for single downloads, or a DataFrame/CSV manifest for batch downloads.
Required columns/filesBatch manifests need study UID and, for series downloads, series UID columns; output directories must be writable.
Return shapeSingle downloads return result metadata; batch helpers return a DataFrame with paths, identifiers, status, and errors.
Saved artifactsDICOM folders, NIfTI files, optional reference DICOM files, manifests, and results CSV files.
Failure modesMissing UIDs, DICOMweb authentication/server errors, conversion failures, existing-file conflicts, or invalid output layout.

The download_id can be reproduced without contacting the server:

python
from fyron.dicom import DICOMDownloader

download_id = DICOMDownloader.download_id(
    study_uid="1.2.3",
    series_uid="1.2.3.4",
)

Required Inputs

InputNeeded forNotes
DICOMweb URLall downloadsMay come from DICOM_WEB_URL.
study UIDsingle study/seriesStudyInstanceUID.
series UIDseries downloadOptional for full-study mode.
output directoryall downloadsDestination folder.

Optional Inputs

InputUse
credentials/sessionauthenticated PACS/DICOMweb
output_format"dicom" or "nifti"
save_reference_dicomkeep one source DICOM slice beside each NIfTI output
reference_dicom_namecustom file name for the retained metadata DICOM
skip_existingresumable jobs
write_manifestreproducibility metadata
results_csvbatch job audit output

Minimal Example

python
from fyron import DICOMDownloader

downloader = DICOMDownloader(
    dicom_web_url="https://pacs.example.org/dicomweb",
    output_format="dicom",
)

result = downloader.download_series(
    study_uid="1.2.3",
    series_uid="1.2.3.4",
    output_dir="dicom_out",
)

result.download_id
result.path
result.error

NIfTI With One Metadata DICOM

When output_format="nifti", Fyron normally converts through a temporary DICOM folder and keeps only the NIfTI output. If you also need a source DICOM header for later metadata checks, set save_reference_dicom=True:

python
from fyron import DICOMDownloader

downloader = DICOMDownloader(
    dicom_web_url="https://pacs.example.org/dicomweb",
    output_format="nifti",
    save_reference_dicom=True,
    reference_dicom_name="reference.dcm",
)

result = downloader.download_series(
    study_uid="1.2.3",
    series_uid="1.2.3.4",
    output_dir="downloads",
)

The series folder then contains:

text
downloads/{download_id}/
  1.2.3.4.nii.gz
  reference.dcm

Use this when downstream tools work with NIfTI but you still want one original DICOM file for PatientID, acquisition metadata, frame of reference, or audit checks. If an existing NIfTI is skipped with skip_existing=True, Fyron does not contact the DICOMweb server and cannot backfill the reference file; rerun with skip_existing=False to create it.

For repeated download jobs, prefer constructor-level configuration:

python
downloader = DICOMDownloader(
    dicom_web_url="https://pacs.example.org/dicomweb",
    output_format="nifti",
    save_reference_dicom=True,
    reference_dicom_name="meta.dcm",
)

For one-off DataFrame downloads, you can override the reference-DICOM behavior directly on the batch call without mutating the downloader:

python
results = downloader.download_from_dataframe(
    df=imaging_df,
    output_dir="downloads",
    save_reference_dicom=True,
    reference_dicom_name="meta.dcm",
)

download_from_df(...) accepts the same arguments. Older scripts that use keep_reference_dicom=True still run with a DeprecationWarning, but new code should use save_reference_dicom=True.

Batch Example

python
results = downloader.download_from_df(
    imaging_df,
    output_dir="dicom_out",
    study_uid_col="study_instance_uid",
    series_uid_col="series_instance_uid",
    save_reference_dicom=True,
    reference_dicom_name="meta.dcm",
    results_csv="dicom_results.csv",
)

The input DataFrame should contain one row per requested series unless download_full_study=True.

ColumnMeaning
study_instance_uidStudy UID used for every request.
series_instance_uidSeries UID used for per-series downloads.
optional label columnCan be used as study_folder_name_col for human-readable full-study folders.

Study Versus Series Downloads

ModeUse whenFolder naming
download_seriesYou already know the relevant series.{output_dir}/{download_id}
download_studyYou need all series in a study or do not know the target series.{output_dir}/{study_download_id} or custom study_folder_name
download_from_dfA cohort CSV/DataFrame contains UIDs.One row per result in the returned result table.

Use output_format="dicom" when later writing DICOM SEG because SEG export needs source-referenced DICOM slices. Use output_format="nifti" when the next step is local image processing, BOA-like processing, or array-based analysis. Use output_format="nifti", save_reference_dicom=True when the analysis should run on NIfTI but metadata provenance still needs one original source DICOM.

Research Decision: DICOM Or NIfTI

ChoiceUse whenArtifact to save
output_format="dicom"You need source slices, DICOM SEG, or detailed acquisition metadata.DICOM folder and result CSV
output_format="nifti"Downstream image processing works on arrays/volumes.NIfTI and result CSV
save_reference_dicom=TrueYou want NIfTI processing plus one original DICOM header for provenance.reference.dcm beside the NIfTI

Always keep the returned result table. It is the bridge between cohort rows, download_id, study/series UIDs, and local files.

CLI Workflow

bash
fyron dicom-download \
  --csv imaging.csv \
  --study-col study_instance_uid \
  --series-col series_instance_uid \
  --output-dir dicom_out \
  --save-reference-dicom \
  --results-csv dicom_results.csv

Function Table

FunctionRequiredOptionalReturns
DICOMDownloader(...)URL or env varauth, output format, compression, workers, reference DICOMdownloader
download_series(...)study UID, series UID, output dirskip existingDicomResult
download_study(...)study UID, output dirlayout, manifests, folder namelist of DicomResult
download_from_df(...)DataFrame, output dirUID columns, full-study mode, CSV output, one-call reference DICOM overrideDataFrame/result table

Return Values

DicomResult contains:

FieldMeaning
study_uidRequested study UID.
series_uidRequested or discovered series UID; None only for study-level failures.
download_idDeterministic identifier used for result matching.
pathOutput folder path.
formatdicom or nifti.
errorNone on success or a formatted error string.

Common Pitfalls

  • Use output_format="dicom" when later writing DICOM SEG; SEG needs original source images.
  • Keep result manifests with the study cohort.
  • Confirm UID columns before a batch download; UID mixups are expensive.
  • Start with one test series before scaling to many studies.
  • For BOA-style downstream extraction, keep a stable mapping between download_id, patient ID, study UID, and series UID.