Skip to content

Commit

Permalink
refactor: refactor find-dicom logic + docstring formatting, closes #149
Browse files Browse the repository at this point in the history
… (#186)

- **Documentation**
	- Improved docstring formatting for better readability in DICOM-related utility functions
	- Enhanced parameter descriptions in documentation

- **Chores**
	- Reformatted code for improved readability
	- Slight modifications to code structure without changing core functionality
  • Loading branch information
jjjermiah authored Jan 20, 2025
1 parent 093eab0 commit da363d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/imgtools/dicom/sort/sorter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def resolve_path(
format_str : str
The format string for the resolved path.
check_existing : bool, optional
If True, check if the resolved path already exists (default is True).
If True, check if the resolved path already exists (default is True).
truncate : bool, optional
If True, truncate long values in the resolved path (default is True).
If True, truncate long values in the resolved path (default is True).
sanitize : bool, optional
If True, sanitize the resolved path (default is True).
If True, sanitize the resolved path (default is True).
Returns
-------
Expand All @@ -88,7 +88,7 @@ def resolve_path(
resolved_path = Path(format_str % tags, path.name)
if check_existing and not resolved_path.exists():
resolved_path = resolved_path.resolve()
else:
elif check_existing:
errmsg = f"Path {resolved_path} already exists."
logger.error(errmsg, source_path=path, resolved_path=resolved_path)
raise FileExistsError(errmsg)
Expand Down
35 changes: 18 additions & 17 deletions src/imgtools/dicom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,30 @@ def find_dicoms(
Parameters
----------
directory : Path
The directory in which to search for DICOM files.
The directory in which to search for DICOM files.
recursive : bool
Whether to include subdirectories in the search.
- If `True`, recursively search all subdirectories.
- If `False`, search only the specified directory.
Whether to include subdirectories in the search
check_header : bool
Whether to validate files by checking for a valid DICOM header.
- If `True`, perform DICOM header validation (slower but more accurate).
- If `False`, skip header validation and rely on extension.
Whether to validate files by checking for a valid DICOM header.
- If `True`, perform DICOM header validation (slower but more accurate).
- If `False`, skip header validation and rely on extension.
extension : str, optional
File extension to search for (e.g., "dcm"). If `None`, consider all files
regardless of extension.
File extension to search for (e.g., "dcm"). If `None`, consider all files
regardless of extension.
limit : int, optional
Maximum number of DICOM files to return. If `None`, return all found files.
Maximum number of DICOM files to return. If `None`, return all found files.
Returns
-------
List[Path]
A list of valid DICOM file paths found in the directory.
A list of valid DICOM file paths found in the directory.
Notes
-----
- If `check_header` is enabled, the function checks each file for a valid
DICOM header, which may slow down the search process.
DICOM header, which may slow down the search process.
Examples
--------
Expand Down Expand Up @@ -175,8 +171,11 @@ def find_dicoms(
files = (
file.absolute()
for file in glob_method(pattern)
if _is_valid_dicom(file, check_header)
and (not search_input or all(term in str(file.as_posix()) for term in search_input))
if (
not search_input
or all(term in str(file.as_posix()) for term in search_input)
)
and _is_valid_dicom(file, check_header)
)

return list(islice(files, limit)) if limit else list(files)
Expand All @@ -186,7 +185,9 @@ def find_dicoms(
# DICOM TAG UTILITIES
###############################################################################

ALL_DICOM_TAGS: FrozenSet[str] = frozenset(value[4] for value in DicomDictionary.values())
ALL_DICOM_TAGS: FrozenSet[str] = frozenset(
value[4] for value in DicomDictionary.values()
)


@functools.lru_cache(maxsize=1024)
Expand Down

0 comments on commit da363d6

Please sign in to comment.