Skip to content

Commit

Permalink
chore: Refactor code formatting and improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Feb 3, 2024
1 parent 3dc1996 commit 8bcdfa4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
5 changes: 3 additions & 2 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ def getBodyPartCounts(
)
return bodyparts

def getStudies(self, Collection : str, PatientID : str = "", StudyInstanceUID : str = ""
) -> Union[list[dict[str, str]], None]:
def getStudies(
self, Collection: str, PatientID: str = "", StudyInstanceUID: str = ""
) -> Union[list[dict[str, str]], None]:
PARAMS = self.parsePARAMS(locals())

response = self.query_api(endpoint=NBIA_ENDPOINTS.GET_STUDIES, params=PARAMS)
Expand Down
97 changes: 74 additions & 23 deletions src/nbiatoolkit/nbia_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import io
from .nbia import NBIAClient, __version__

Expand Down Expand Up @@ -27,7 +26,13 @@ def version():
print("\nAvailable CLI tools: \n")

# run each command with -h to see the available options
commands = ["getCollections", "getPatients", "getBodyPartCounts", "getSeries", "downloadSingleSeries"]
commands = [
"getCollections",
"getPatients",
"getBodyPartCounts",
"getSeries",
"downloadSingleSeries",
]
for command in commands:
result = subprocess.run([command, "-h"], capture_output=True, text=True)
output_lines = result.stdout.splitlines()
Expand All @@ -39,9 +44,15 @@ def version():

return


def general_parser(parser: argparse.ArgumentParser) -> argparse.Namespace:
parser.add_argument("-o", "--output", dest="outputfile",
action="store", type=argparse.FileType('w', encoding='UTF-8'), help="Output file (tsv works best)",
parser.add_argument(
"-o",
"--output",
dest="outputfile",
action="store",
type=argparse.FileType("w", encoding="UTF-8"),
help="Output file (tsv works best)",
)

parser.add_argument(
Expand All @@ -59,6 +70,7 @@ def general_parser(parser: argparse.ArgumentParser) -> argparse.Namespace:

return args


# An abstraction of the getCollections and getPatients functions
# to generalize an interface for the CLI
def getResults_cli(func, **kwargs) -> None:
Expand Down Expand Up @@ -98,6 +110,7 @@ def getResults_cli(func, **kwargs) -> None:
print(result)
return


# create a helper function that will be used if the user ever uses --output <FILE>.tsv
# output should be a io.TextIOWrapper object
def writeResultsToFile(results: List, output: io.TextIOWrapper) -> None:
Expand Down Expand Up @@ -125,6 +138,7 @@ def writeResultsToFile(results: List, output: io.TextIOWrapper) -> None:
output.write(str(result) + "\n")
return


def cli_wrapper(func, **kwargs) -> List[str] | None:
"""
Wraps a function call with a loading animation.
Expand Down Expand Up @@ -156,13 +170,19 @@ def cli_wrapper(func, **kwargs) -> List[str] | None:

return result


def getPatients_cli() -> None:
global query
query = "patients"
p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")

p.add_argument("-c", "--collection", action="store",
required=True,type=str,)
p.add_argument(
"-c",
"--collection",
action="store",
required=True,
type=str,
)

args = general_parser(p)

Expand All @@ -174,8 +194,12 @@ def getCollections_cli() -> None:
query = "collections"
p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")

p.add_argument("-p", "--prefix",
action="store", default="", type=str,
p.add_argument(
"-p",
"--prefix",
action="store",
default="",
type=str,
help="The prefix to filter collections by, i.e 'TCGA', 'LIDC', 'NSCLC'",
)

Expand All @@ -191,13 +215,20 @@ def getBodyPartCounts_cli() -> None:

p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")

p.add_argument("-c", "--collection", dest="collection",
action="store", default="", type=str,)
p.add_argument(
"-c",
"--collection",
dest="collection",
action="store",
default="",
type=str,
)

args = general_parser(p)


return getResults_cli(func=NBIAClient().getBodyPartCounts, Collection=args.collection)
return getResults_cli(
func=NBIAClient().getBodyPartCounts, Collection=args.collection
)


def getSeries_cli() -> None:
Expand All @@ -207,19 +238,41 @@ def getSeries_cli() -> None:

p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")

p.add_argument("-c", "--collection", dest="collection", action="store",
default="", type=str,
p.add_argument(
"-c",
"--collection",
dest="collection",
action="store",
default="",
type=str,
)

p.add_argument("-p", "--patientID", dest="patientID",
action="store", default="", type=str,)

p.add_argument("-m", "--modality", dest="modality",
action="store", default="", type=str,)
p.add_argument(
"-p",
"--patientID",
dest="patientID",
action="store",
default="",
type=str,
)

p.add_argument("-study", "--studyInstanceUID", dest="studyInstanceUID",
action="store", default="", type=str,)
p.add_argument(
"-m",
"--modality",
dest="modality",
action="store",
default="",
type=str,
)

p.add_argument(
"-study",
"--studyInstanceUID",
dest="studyInstanceUID",
action="store",
default="",
type=str,
)

p.add_argument(
"--seriesInstanceUID",
Expand Down Expand Up @@ -316,5 +369,3 @@ def downloadSingleSeries_cli() -> None:
filePattern=args.filePattern,
overwrite=args.overwrite,
)


0 comments on commit 8bcdfa4

Please sign in to comment.