Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions datamint/apihandler/root_api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ async def __upload_single_resource(file_path, segfiles: dict[str, list | dict],
names = _infinite_gen(names)
frame_indices = segfiles.get('frame_index', _infinite_gen(None))
for f, name, frame_index in tqdm(zip(fpaths, names, frame_indices),
desc=f"Uploading segmentations for {file_path}",
total=len(fpaths)):
desc=f"Uploading segmentations for {file_path}",
total=len(fpaths)):
if f is not None:
await self._upload_segmentations_async(rid,
file_path=f,
name=name,
frame_index=frame_index,
transpose_segmentation=transpose_segmentation)
file_path=f,
name=name,
frame_index=frame_index,
transpose_segmentation=transpose_segmentation)
return rid

tasks = [__upload_single_resource(f, segfiles, metadata_file)
Expand Down Expand Up @@ -368,25 +368,26 @@ def upload_resource(self,
def _is_dicom_report(file_path: str | IO) -> bool:
"""
Check if a DICOM file is a report (e.g., Structured Report).

Args:
file_path: Path to the DICOM file or file-like object.

Returns:
bool: True if the DICOM file is a report, False otherwise.
"""
try:
if not is_dicom(file_path):
return False

ds = pydicom.dcmread(file_path, stop_before_pixels=True)
if hasattr(file_path, 'seek'):
file_path.seek(0)
modality = getattr(ds, 'Modality', None)

# Common report modalities
report_modalities = {'SR', 'DOC', 'KO', 'PR', 'ESR'} # SR=Structured Report, DOC=Document, KO=Key Object, PR=Presentation State

# SR=Structured Report, DOC=Document, KO=Key Object, PR=Presentation State
report_modalities = {'SR', 'DOC', 'KO', 'PR', 'ESR'}

return modality in report_modalities
except Exception as e:
_LOGGER.warning(f"Error checking if DICOM is a report: {e}")
Expand Down Expand Up @@ -445,21 +446,18 @@ def upload_resources(self,
list[str | Exception]: A list of resource IDs or errors.
"""

if discard_dicom_reports:
if isinstance(files_path, (str, Path)):
files_path = [files_path]
elif isinstance(files_path, pydicom.dataset.Dataset):
files_path = [files_path]

old_size = len(files_path)
files_path = [f for f in files_path if not RootAPIHandler._is_dicom_report(f)]
if old_size != len(files_path):
_LOGGER.info(f"Discarded {old_size - len(files_path)} DICOM report files from upload.")

if on_error not in ['raise', 'skip']:
raise ValueError("on_error must be either 'raise' or 'skip'")

files_path, is_multiple_resources = RootAPIHandler.__process_files_parameter(files_path)

### Discard DICOM reports
if discard_dicom_reports:
files_path = [f for f in files_path if not RootAPIHandler._is_dicom_report(f)]
old_size = len(files_path)
if old_size is not None and old_size != len(files_path):
_LOGGER.info(f"Discarded {old_size - len(files_path)} DICOM report files from upload.")

if isinstance(metadata, (str, dict)):
_LOGGER.debug("Converting metadatas to a list")
metadata = [metadata]
Expand Down Expand Up @@ -905,7 +903,7 @@ def download_multiple_resources(self,
) -> None:
"""
Download multiple resources and save them to the specified paths.

Args:
resource_ids (list[str]): A list of resource unique ids.
save_path (list[str] | str): A list of paths to save the files or a directory path.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "datamint"
description = "A library for interacting with the Datamint API, designed for efficient data management, processing and Deep Learning workflows."
version = "1.7.2"
version = "1.7.3"
dynamic = ["dependencies"]
requires-python = ">=3.10"
readme = "README.md"
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import json
from aiohttp import FormData
from typing import IO
import requests
import os
import numpy as np
from copy import deepcopy
import logging

# pytest tests --log-cli-level=INFO

Expand Down