Skip to content

Commit

Permalink
style: black
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Feb 17, 2024
1 parent e012b2b commit 4c5ba94
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 54 deletions.
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
repos:
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-json
- id: check-toml
- id: pretty-format-json

- repo: https://gitlab.com/smop/pre-commit-hooks
rev: v1.0.0
hooks:
- id: check-poetry

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.0
hooks:
- id: lintr
args: [--warn_only, --key=value]
- id: roxygenize
- id: no-print-statement
- id: no-debug-statement
- id: deps-in-desc
- id: use-tidy-description
- id: parsable-R
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[![PyPI - Downloads](https://img.shields.io/pypi/dm/nbiatoolkit.svg?label=pypi%20downloads)](https://pypi.org/project/nbiatoolkit/)
![GitHub repo size](https://img.shields.io/github/repo-size/jjjermiah/nbia-toolkit)
[![Docker Pulls](https://img.shields.io/docker/pulls/jjjermiah/nbiatoolkit)](https://hub.docker.com/r/jjjermiah/nbiatoolkit)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)



Expand Down
6 changes: 3 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
sys.path.insert(0, os.path.abspath("../src"))
# -- Project information -----------------------------------------------------

project = u"nbiatoolkit"
copyright = u"2023, Jermiah Joseph"
author = u"Jermiah Joseph"
project = "nbiatoolkit"
copyright = "2023, Jermiah Joseph"
author = "Jermiah Joseph"

# -- General configuration ---------------------------------------------------

Expand Down
106 changes: 105 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions src/nbiatoolkit/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from cryptography.fernet import Fernet


def encrypt_credentials(
key: bytes, username: str, password: str
) -> Tuple[str, str]:
def encrypt_credentials(key: bytes, username: str, password: str) -> Tuple[str, str]:
"""
Encrypts the given username and password using the provided key.
Expand Down Expand Up @@ -141,7 +139,9 @@ def __init__(
self.username, self.password = encrypt_credentials(
key=self.fernet_key, username=username, password=password
)
self.username, self.password = encrypt_credentials(key=self._fernet_key, username=username, password=password)
self.username, self.password = encrypt_credentials(
key=self._fernet_key, username=username, password=password
)

if isinstance(base_url, NBIA_ENDPOINTS):
self.base_url = base_url.value
Expand All @@ -154,7 +154,6 @@ def __init__(
self.refresh_token = "" # Fix: Assign an empty string instead of None
self.scope = None


@property
def fernet_key(self) -> bytes:
return self._fernet_key
Expand Down
24 changes: 14 additions & 10 deletions src/nbiatoolkit/dicomsort/dicomsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,25 @@ def sortDICOMFiles(
return all(result)



def _get_dicom_files(sourceDir) -> "list[str]":
dicom_file_paths = []
# Iterate over all files in the source directory
for root, dirs, files in os.walk(sourceDir):
for f in files:
dicom_file_paths.append(os.path.join(root, f)) if f.endswith(
".dcm"
) else None
return dicom_file_paths
dicom_file_paths = []
# Iterate over all files in the source directory
for root, dirs, files in os.walk(sourceDir):
for f in files:
dicom_file_paths.append(os.path.join(root, f)) if f.endswith(
".dcm"
) else None
return dicom_file_paths


# parallel function to read in each file using dcmread only


def _read_in_dicom_file(filePath: str) -> Optional[pydicom.FileDataset]:
try:
dataset: pydicom.FileDataset = pydicom.dcmread(filePath, stop_before_pixels=True)
dataset: pydicom.FileDataset = pydicom.dcmread(
filePath, stop_before_pixels=True
)
return dataset
except InvalidDicomError as e:
print(f"Error reading file {filePath}: {e}")
Expand All @@ -139,6 +142,7 @@ def _read_in_dicom_file(filePath: str) -> Optional[pydicom.FileDataset]:
print(f"Error reading file {filePath}: is ``None`` or of an unsupported type.")
return None


# sourceDir = "/Users/bhklab/Documents/GitHub/NBIA-toolkit/NBIA-Download"
# files = _get_dicom_files(sourceDir=sourceDir)
# dcm_list = []
Expand Down
6 changes: 3 additions & 3 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,9 @@ def getDICOMTags(
self,
SeriesInstanceUID: str,
) -> Union[list[dict], None]:
assert SeriesInstanceUID is not None and isinstance(SeriesInstanceUID, str), \
"SeriesInstanceUID must be a string"
assert SeriesInstanceUID is not None and isinstance(
SeriesInstanceUID, str
), "SeriesInstanceUID must be a string"

PARAMS = self.parsePARAMS({"SeriesUID": SeriesInstanceUID})

Expand All @@ -415,7 +416,6 @@ def getDICOMTags(

return response


def downloadSeries(
self,
SeriesInstanceUID: Union[str, list],
Expand Down
2 changes: 0 additions & 2 deletions src/nbiatoolkit/utils/nbia_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class NBIA_ENDPOINTS(Enum):
DOWNLOAD_SERIES = "v2/getImageWithMD5Hash"
GET_DICOM_TAGS = "getDicomTags"



# curl -H "Authorization:Bearer YOUR_ACCESS_TOKEN" -k "https://services.cancerimagingarchive.net/nbia-api/services/v2/getSeriesMetaData"
# curl -H "Authorization:Bearer YOUR_ACCESS_TOKEN" -k "https://services.cancerimagingarchive.net/nbia-api/services/v2/getSeriesSize"

Expand Down
8 changes: 8 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import time
import requests


@pytest.fixture
def oauth() -> OAuth2:
return OAuth2()


def test_oauth2(oauth: OAuth2) -> None:
assert oauth.client_id == "NBIA"
assert oauth.username != "nbia_guest"
Expand All @@ -23,18 +25,21 @@ def test_oauth2(oauth: OAuth2) -> None:
assert oauth.refresh_expiry is not None
assert oauth.scope is not None


def test_is_token_expired(oauth: OAuth2) -> None:
assert oauth.is_token_expired() == False
oauth.expiry_time = time.time() - 100
assert oauth.is_token_expired() == True


def test_refresh_token_or_request_new(oauth: OAuth2) -> None:
oauth.refresh_token_or_request_new()
assert oauth.access_token is not None
assert oauth.refresh_token is not None
assert oauth.refresh_expiry is not None
assert oauth.expiry_time is not None


def test_refresh_after_expiry(oauth: OAuth2) -> None:
access_token_before = oauth.access_token

Expand All @@ -48,6 +53,7 @@ def test_refresh_after_expiry(oauth: OAuth2) -> None:
assert oauth.expiry_time is not None
assert oauth.is_token_expired() == False


def test_failed_refresh(oauth: OAuth2) -> None:
oauth.refresh_token = ""
with pytest.raises(AssertionError):
Expand All @@ -58,6 +64,7 @@ def test_failed_refresh(oauth: OAuth2) -> None:
with pytest.raises(requests.exceptions.HTTPError):
oauth._refresh_access_token()


def test_request_new_access_token(oauth: OAuth2) -> None:
oauth.refresh_token = ""
oauth.request_new_access_token()
Expand All @@ -67,6 +74,7 @@ def test_request_new_access_token(oauth: OAuth2) -> None:
assert oauth.expiry_time is not None
assert oauth.is_token_expired() == False


def test_logout(oauth: OAuth2) -> None:
oauth.logout()
assert oauth.access_token is None
Expand Down

0 comments on commit 4c5ba94

Please sign in to comment.