Skip to content

Commit

Permalink
Merge pull request #799 from scap3yvt/798-standardize-commenting-style
Browse files Browse the repository at this point in the history
Standardizing commenting style for major functions
  • Loading branch information
Geeks-Sid authored Feb 16, 2024
2 parents 55bf00a + 51bcb82 commit 3af7fff
Show file tree
Hide file tree
Showing 64 changed files with 1,668 additions and 1,201 deletions.
5 changes: 1 addition & 4 deletions GANDLF/anonymize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def run_anonymizer(
input_path: str, output_path: str, parameters: Union[str, list, int], modality: str
):
) -> None:
"""
This function performs anonymization of a single image or a collection of images.
Expand All @@ -17,9 +17,6 @@ def run_anonymizer(
output_path (str): The output file or folder.
parameters (Union[str, list, int]): The parameters for anonymization; for DICOM scans, the only optional argument is "delete_private_tags", which defaults to True.
output_path (str): The modality type to process.
Returns:
torch.Tensor: The output image after morphological operations.
"""
if parameters is None:
parameters = {}
Expand Down
2 changes: 1 addition & 1 deletion GANDLF/anonymize/convert_to_nifti.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SimpleITK as sitk


def convert_to_nifti(input_dicom_directory, output_file):
def convert_to_nifti(input_dicom_directory: str, output_file: str) -> None:
"""
This function performs NIfTI conversion of a DICOM image series.
Expand Down
20 changes: 13 additions & 7 deletions GANDLF/cli/config_generator.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import yaml
from typing import List, Optional, Union
from pathlib import Path
from copy import deepcopy


def generate_new_configs_from_key_and_value(
base_config, key, value, upper_level_key=None
):
base_config: dict,
key: str,
value: Union[str, list, int],
upper_level_key: Optional[str] = None,
) -> List[dict]:
"""
Generate new configs based on a base config and a strategy.
Args:
base_config (dict): The base configuration to generate new configs from.
key (str): The key to change in the base config.
value (Union[str, list, int]): The value to change the key to.
upper_level_key (str, optional): The upper level key to change in the base config; useful for dict. Defaults to None.
upper_level_key (Optional[str]): The upper level key in the base config; useful for dict. Defaults to None.
Returns:
list: A list of new configs.
List[dict]: A list of new configs.
"""
configs_to_return = []
if key == "patch_size":
Expand Down Expand Up @@ -59,15 +63,15 @@ def generate_new_configs_from_key_and_value(
return configs_to_return


def remove_duplicates(configs_list):
def remove_duplicates(configs_list: List[dict]) -> List[dict]:
"""
Remove duplicate configs from a list of configs.
Args:
configs_list (list): A list of configs.
Returns:
list: A list of configs with duplicates removed.
List[dict]: A list of configs with duplicates removed.
"""
configs_to_return = []
for config in configs_list:
Expand All @@ -76,7 +80,9 @@ def remove_duplicates(configs_list):
return configs_to_return


def config_generator(base_config_path, strategy_path, output_dir):
def config_generator(
base_config_path: str, strategy_path: str, output_dir: str
) -> None:
"""
Main function that runs the training and inference.
Expand Down
94 changes: 51 additions & 43 deletions GANDLF/cli/deploy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import os
import shutil
import yaml
import docker
import tarfile
import io
import sysconfig
import os, shutil, yaml, docker, tarfile, io, sysconfig
from typing import Optional

# import copy

Expand All @@ -18,30 +13,30 @@


def run_deployment(
mlcubedir,
outputdir,
target,
mlcube_type,
entrypoint_script=None,
configfile=None,
modeldir=None,
requires_gpu=None,
):
mlcubedir: str,
outputdir: str,
target: str,
mlcube_type: str,
entrypoint_script: Optional[str] = None,
configfile: Optional[str] = None,
modeldir: Optional[str] = None,
requires_gpu: Optional[bool] = None,
) -> bool:
"""
Run the deployment of the model.
This function runs the deployment of the mlcube.
Args:
mlcubedir (str): The path to the mlcube directory.
outputdir (str): The path to the output directory.
target (str): The target to deploy to.
mlcube_type (str): Either 'model' or 'metrics'
entrypoint_script (str): The path of entrypoint script. Only used for metrics and inference
configfile (str, Optional): The path to the configuration file. Required for models
modeldir (str, Optional): The path to the model directory. Required for models
requires_gpu (str, Optional): Whether the model requires GPU. Required for models
target (str): The deployment target.
mlcube_type (str): The type of mlcube.
entrypoint_script (str, optional): The path of entrypoint script; only used for metrics and inference. Defaults to None.
configfile (str, optional): The path of the configuration file; required for models. Defaults to None.
modeldir (str, optional): The path of the model directory; required for models. Defaults to None.
requires_gpu (bool, optional): Whether the model requires GPU; required for models. Defaults to None.
Returns:
bool: True if the deployment was successful, False otherwise.
bool: True if the deployment is successful.
"""
assert (
target in deploy_targets
Expand Down Expand Up @@ -86,23 +81,26 @@ def run_deployment(


def deploy_docker_mlcube(
mlcubedir,
outputdir,
entrypoint_script=None,
config=None,
modeldir=None,
requires_gpu=None,
):
mlcubedir: str,
outputdir: str,
entrypoint_script: Optional[str] = None,
config: Optional[str] = None,
modeldir: Optional[str] = None,
requires_gpu: Optional[bool] = None,
) -> bool:
"""
Deploy the docker mlcube of the model or metrics calculator.
This function deploys the mlcube as a docker container.
Args:
mlcubedir (str): The path to the mlcube directory.
outputdir (str): The path to the output directory.
entrypoint_script (str): The path of entrypoint script. Only used for metrics and inference
config (str, Optional): The path to the configuration file. Required for models
modeldir (str, Optional): The path to the model directory. Required for models
requires_gpu (str, Optional): Whether the model requires GPU. Required for models
entrypoint_script (str, optional): The path of the entrypoint script; only used for metrics and inference. Defaults to None.
config (str, optional): The path of the configuration file; required for models. Defaults to None.
modeldir (str, optional): The path of the model directory; required for models. Defaults to None.
requires_gpu (bool, optional): Whether the model requires GPU; required for models. Defaults to None.
Returns:
bool: True if the deployment is successful.
"""
mlcube_config_file = os.path.join(mlcubedir, "mlcube.yaml")
assert os.path.exists(mlcubedir) and os.path.exists(
Expand Down Expand Up @@ -238,13 +236,18 @@ def deploy_docker_mlcube(
return True


def get_metrics_mlcube_config(mlcube_config_file, entrypoint_script):
def get_metrics_mlcube_config(
mlcube_config_file: str, entrypoint_script: Optional[str] = None
) -> dict:
"""
This function is used to get the metrics from mlcube config file.
This function returns the mlcube config for the metrics.
Args:
mlcube_config_file (str): The path of mlcube config file.
entrypoint_script (str): The path of entrypoint script.
mlcube_config_file (str): Path to mlcube config file.
entrypoint_script (str, optional): The path of entrypoint script; only used for metrics. Defaults to None.
Returns:
dict: The mlcube config for the metrics.
"""
mlcube_config = None
with open(mlcube_config_file, "r") as f:
Expand All @@ -256,14 +259,19 @@ def get_metrics_mlcube_config(mlcube_config_file, entrypoint_script):
return mlcube_config


def get_model_mlcube_config(mlcube_config_file, requires_gpu, entrypoint_script):
def get_model_mlcube_config(
mlcube_config_file: str, requires_gpu: bool, entrypoint_script: Optional[str] = None
) -> dict:
"""
This function returns the mlcube config for the model.
Args:
mlcube_config_file (str): Path to mlcube config file.
requires_gpu (bool): Whether the model requires GPU.
entrypoint_script (str): The path of entrypoint script. Only used for infer task
entrypoint_script (str, optional): The path of entrypoint script; only used for models. Defaults to None.
Returns:
dict: The mlcube config for the model.
"""
mlcube_config = None
with open(mlcube_config_file, "r") as f:
Expand Down Expand Up @@ -333,7 +341,7 @@ def get_model_mlcube_config(mlcube_config_file, requires_gpu, entrypoint_script)
# )


def embed_asset(asset, container, asset_name):
def embed_asset(asset: str, container: object, asset_name: str) -> None:
"""
This function embeds an asset into a container.
Expand Down
79 changes: 41 additions & 38 deletions GANDLF/cli/generate_metrics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import yaml
from typing import Optional
from pprint import pprint
import pandas as pd
from tqdm import tqdm
Expand Down Expand Up @@ -30,7 +31,9 @@
)


def generate_metrics_dict(input_csv: str, config: str, outputfile: str = None) -> dict:
def generate_metrics_dict(
input_csv: str, config: str, outputfile: Optional[str] = None
) -> dict:
"""
This function generates metrics from the input csv and the config.
Expand Down Expand Up @@ -195,18 +198,18 @@ def __fix_2d_tensor(input_tensor):
return input_tensor

def __percentile_clip(
input_tensor,
reference_tensor=None,
p_min=0.5,
p_max=99.5,
strictlyPositive=True,
input_tensor: torch.Tensor,
reference_tensor: torch.Tensor = None,
p_min: Optional[float] = 0.5,
p_max: Optional[float] = 99.5,
strictlyPositive: Optional[bool] = True,
):
"""Normalizes a tensor based on percentiles. Clips values below and above the percentile.
"""
Normalizes a tensor based on percentiles. Clips values below and above the percentile.
Percentiles for normalization can come from another tensor.
Args:
input_tensor (torch.Tensor): Tensor to be normalized based on the data from the reference_tensor.
If reference_tensor is None, the percentiles from this tensor will be used.
input_tensor (torch.Tensor): Tensor to be normalized based on the data from the reference_tensor. If reference_tensor is None, the percentiles from this tensor will be used.
reference_tensor (torch.Tensor, optional): The tensor used for obtaining the percentiles.
p_min (float, optional): Lower end percentile. Defaults to 0.5.
p_max (float, optional): Upper end percentile. Defaults to 99.5.
Expand Down Expand Up @@ -276,24 +279,24 @@ def __percentile_clip(
strictlyPositive=True,
)

overall_stats_dict[current_subject_id][
"ssim"
] = structural_similarity_index(gt_image_infill, output_infill, mask).item()
overall_stats_dict[current_subject_id]["ssim"] = (
structural_similarity_index(output_infill, gt_image_infill, mask).item()
)

# ncc metrics
compute_ncc = parameters.get("compute_ncc", True)
if compute_ncc:
overall_stats_dict[current_subject_id]["ncc_mean"] = ncc_mean(
gt_image_infill, output_infill
output_infill, gt_image_infill
)
overall_stats_dict[current_subject_id]["ncc_std"] = ncc_std(
gt_image_infill, output_infill
output_infill, gt_image_infill
)
overall_stats_dict[current_subject_id]["ncc_max"] = ncc_max(
gt_image_infill, output_infill
output_infill, gt_image_infill
)
overall_stats_dict[current_subject_id]["ncc_min"] = ncc_min(
gt_image_infill, output_infill
output_infill, gt_image_infill
)

# only voxels that are to be inferred (-> flat array)
Expand All @@ -302,47 +305,47 @@ def __percentile_clip(
output_infill = output_infill[mask]

overall_stats_dict[current_subject_id]["mse"] = mean_squared_error(
gt_image_infill, output_infill
output_infill, gt_image_infill
).item()

overall_stats_dict[current_subject_id]["msle"] = mean_squared_log_error(
gt_image_infill, output_infill
output_infill, gt_image_infill
).item()

overall_stats_dict[current_subject_id]["mae"] = mean_absolute_error(
gt_image_infill, output_infill
output_infill, gt_image_infill
).item()

# torchmetrics PSNR using "max"
overall_stats_dict[current_subject_id]["psnr"] = peak_signal_noise_ratio(
gt_image_infill, output_infill
output_infill, gt_image_infill
).item()

# same as above but with epsilon for robustness
overall_stats_dict[current_subject_id][
"psnr_eps"
] = peak_signal_noise_ratio(
gt_image_infill, output_infill, epsilon=sys.float_info.epsilon
).item()
overall_stats_dict[current_subject_id]["psnr_eps"] = (
peak_signal_noise_ratio(
output_infill, gt_image_infill, epsilon=sys.float_info.epsilon
).item()
)

# only use fix data range to [0;1] if the data was normalized before
if normalize:
# torchmetrics PSNR but with fixed data range of 0 to 1
overall_stats_dict[current_subject_id][
"psnr_01"
] = peak_signal_noise_ratio(
gt_image_infill, output_infill, data_range=(0, 1)
).item()
overall_stats_dict[current_subject_id]["psnr_01"] = (
peak_signal_noise_ratio(
output_infill, gt_image_infill, data_range=(0, 1)
).item()
)

# same as above but with epsilon for robustness
overall_stats_dict[current_subject_id][
"psnr_01_eps"
] = peak_signal_noise_ratio(
gt_image_infill,
output_infill,
data_range=(0, 1),
epsilon=sys.float_info.epsilon,
).item()
overall_stats_dict[current_subject_id]["psnr_01_eps"] = (
peak_signal_noise_ratio(
output_infill,
gt_image_infill,
data_range=(0, 1),
epsilon=sys.float_info.epsilon,
).item()
)

pprint(overall_stats_dict)
if outputfile is not None:
Expand Down
Loading

0 comments on commit 3af7fff

Please sign in to comment.