Skip to content

Commit

Permalink
feat: minor grade refactoring (#158)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Siraj <[email protected]>
  • Loading branch information
jjjermiah and JoshuaSiraj authored Jan 2, 2025
1 parent 41a7c88 commit 0dff0f4
Show file tree
Hide file tree
Showing 46 changed files with 5,539 additions and 4,098 deletions.
3 changes: 2 additions & 1 deletion config/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
files =
src/imgtools/logging/**/*.py,
src/imgtools/dicom/**/*.py,
src/imgtools/cli/**/*.py
src/imgtools/cli/**/*.py,
src/imgtools/modules/**/*.py,

# Exclude files from analysis
exclude = tests,
Expand Down
29 changes: 21 additions & 8 deletions config/ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# slowly fix everything

include = [
"src/imgtools/logging/**/*.py",
# "src/imgtools/cli/**/*.py",
"src/imgtools/logging/**/*.py",
"src/imgtools/modules/*.py",
"src/imgtools/dicom/**/*.py",
# "src/imgtools/utils/crawl.py",
]
Expand All @@ -15,16 +15,13 @@ extend-exclude = [
"tests/**/*.py",
"src/imgtools/ops/ops.py",
"src/imgtools/io/**/*.py",
"src/imgtools/modules/**/*.py",
"src/imgtools/transforms/**/*.py",
"src/imgtools/autopipeline.py",
"src/imgtools/pipeline.py",
"src/imgtools/image.py",
]

extend-include = [
"src/imgtools/ops/functional.py",
]
extend-include = ["src/imgtools/ops/functional.py"]


line-length = 100
Expand Down Expand Up @@ -94,6 +91,11 @@ select = [
# Pydocstyle
# https://docs.astral.sh/ruff/rules/#pydocstyle-d
# "D",

###########################################################################
# MIGRATION TO NUMPY2
# https://numpy.org/doc/stable/numpy_2_0_migration_guide.html
"NPY201",
]

ignore = [
Expand All @@ -105,14 +107,25 @@ ignore = [
# Ignored because https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"COM812", # https://docs.astral.sh/ruff/rules/missing-trailing-comma/#missing-trailing-comma-com812
"D206",
"N813",
"N813",
"EM101",
]

[lint.mccabe]
max-complexity = 10

[lint.pydocstyle]
convention = "numpy"

[lint.isort]
known-first-party = ["imgtools", "readii"]
force-wrap-aliases = true
combine-as-imports = true
relative-imports-order = "closest-to-furthest"

[format]

quote-style = "single"
indent-style = "tab"
indent-style = "space"
docstring-code-format = true
docstring-code-line-length = 71
84 changes: 40 additions & 44 deletions pixi.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ serve.cmd = "mkdocs serve -f mkdocs.yml"
############################################## QUALITY ###############################################
# Quality includes linting, type checking, and formatting
[feature.quality.dependencies]
ruff = ">=0.4.4"
ruff = ">=0.4.8"
mypy = ">=1.13.0,<2"
types-pytz = ">=2024.2.0.20241003,<2025"
types-tqdm = ">=4.66.0.20240417,<5"
Expand All @@ -115,7 +115,6 @@ _ruff-check.description = "Run ruff check"

_ruff-format.cmd = ["ruff", "--config", "config/ruff.toml", "format", "src"]
_ruff-format.inputs = ["config/ruff.toml", "src"]
_ruff-format.depends_on = ["_ruff-check"]
_ruff-format.description = "Run ruff format, run check first"

_type-check.cmd = ["mypy", "--config-file", "config/mypy.ini"]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ requires-python = ">=3.10,<4"
dependencies = [
"h5py>=3.11.0,<4",
"joblib>=1.4.2,<2",
"numpy>=1.26.4,<2",
"numpy>=2.0.0,<3",
"matplotlib>=3.8.4,<4",
"pandas>=2.2.2,<3",
"pydicom>=2.4.4",
Expand Down
8 changes: 4 additions & 4 deletions src/imgtools/dicom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from imgtools.dicom.utils import find_dicoms, lookup_tag, similar_tags, tag_exists

__all__ = [
'find_dicoms',
'lookup_tag',
'similar_tags',
'tag_exists',
'find_dicoms',
'lookup_tag',
'similar_tags',
'tag_exists',
]
118 changes: 59 additions & 59 deletions src/imgtools/dicom/index/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,85 @@
@click.command()
@set_log_verbosity()
@click.option(
'--directory',
'-d',
type=click.Path(
exists=True,
file_okay=False,
dir_okay=True,
readable=True,
resolve_path=True,
path_type=Path,
),
help='Directory to search for DICOM files',
required=True,
'--directory',
'-d',
type=click.Path(
exists=True,
file_okay=False,
dir_okay=True,
readable=True,
resolve_path=True,
path_type=Path,
),
help='Directory to search for DICOM files',
required=True,
)
@click.option(
'--update-db',
'-u',
is_flag=True,
help='Force update the database',
default=False,
'--update-db',
'-u',
is_flag=True,
help='Force update the database',
default=False,
)
@click.option(
'--limit',
'-l',
type=int,
default=0,
help='Number of files to index, 0 for all',
'--limit',
'-l',
type=int,
default=0,
help='Number of files to index, 0 for all',
)
def index(
directory: Path, update_db: bool = False, verbose: int = 0, quiet: bool = False, limit: int = 0
directory: Path, update_db: bool = False, verbose: int = 0, quiet: bool = False, limit: int = 0
) -> None:
"""Index DICOM files in a directory.
"""Index DICOM files in a directory.
For now, creates a database in the current directory at .imgtools/imgtools.db.
For now, creates a database in the current directory at .imgtools/imgtools.db.
"""
extension = 'dcm'
check_header = False
logger.info('Searching for DICOM files.', args=locals())
"""
extension = 'dcm'
check_header = False
logger.info('Searching for DICOM files.', args=locals())

dicom_files = find_dicoms(
directory=directory,
check_header=check_header,
recursive=True,
extension=extension,
)
if not dicom_files:
warningmsg = f'No DICOM files found in {directory}.'
logger.warning(
warningmsg,
directory=directory,
check_header=check_header,
recursive=True,
extension=extension,
)
return
dicom_files = find_dicoms(
directory=directory,
check_header=check_header,
recursive=True,
extension=extension,
)
if not dicom_files:
warningmsg = f'No DICOM files found in {directory}.'
logger.warning(
warningmsg,
directory=directory,
check_header=check_header,
recursive=True,
extension=extension,
)
return

logger.info('DICOM find successful.', count=len(dicom_files))
logger.info('DICOM find successful.', count=len(dicom_files))

db_path = DEFAULT_DB_DIR / DEFAULT_DB_NAME
db_path = DEFAULT_DB_DIR / DEFAULT_DB_NAME

db_path.parent.mkdir(parents=True, exist_ok=True)
db_path.parent.mkdir(parents=True, exist_ok=True)

db_handler = DatabaseHandler(db_path=db_path, force_delete=update_db)
db_handler = DatabaseHandler(db_path=db_path, force_delete=update_db)

indexer = DICOMIndexer(db_handler=db_handler)
indexer = DICOMIndexer(db_handler=db_handler)

if limit:
dicom_files = dicom_files[:limit]
if limit:
dicom_files = dicom_files[:limit]

logger.debug('Building index.', count=len(dicom_files), limit=limit)
# Build index
indexer.build_index_from_files(dicom_files)
logger.debug('Building index.', count=len(dicom_files), limit=limit)
# Build index
indexer.build_index_from_files(dicom_files)

_ = DICOMDatabaseInterface(db_handler=db_handler)
_ = DICOMDatabaseInterface(db_handler=db_handler)

logger.info('Indexing complete.')
logger.info('Indexing complete.')

click.echo(f'Indexed {len(dicom_files)} files to {db_path}')
click.echo(f'Indexed {len(dicom_files)} files to {db_path}')


if __name__ == '__main__':
index()
index()
Loading

0 comments on commit 0dff0f4

Please sign in to comment.