Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

ENH: Switch recommonmark to MyST-parser #787

Merged
merged 25 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
267d4e8
📝 Create basic for ML API
peterhessey Aug 5, 2022
806f261
📝 Add ML/configs base doc files
peterhessey Aug 5, 2022
0cbd7c7
📝 Finish ML/configs API
peterhessey Aug 8, 2022
f5c82cf
📝 Update augmentations
peterhessey Aug 8, 2022
ac0171a
📝 Add ML/dataset API docs
peterhessey Aug 8, 2022
e9fff6a
📝 Add rst skeleton for ML/models
peterhessey Aug 8, 2022
4fd8de4
📝 Fix docstring missing newlines
peterhessey Aug 8, 2022
6371548
Remove script
peterhessey Aug 8, 2022
27e7784
📝 Finish ML/models API docs
peterhessey Aug 8, 2022
6a1273f
📝 Start ML/SSL API. Fix some formatting issues
peterhessey Aug 9, 2022
562173b
📝 Correct whitespace issues in `:param`
peterhessey Aug 9, 2022
416e907
📝 Fix whitespace errors on `:return` statements
peterhessey Aug 9, 2022
a778dac
📝 Fix :return: statements
peterhessey Aug 9, 2022
33b557c
📝 Finish ML/SSL API
peterhessey Aug 9, 2022
7d4f466
📝 Add ML/utils API docs
peterhessey Aug 9, 2022
19ab5b2
📝 Add visualizer docs, fix `:raise` indents
peterhessey Aug 9, 2022
67169af
📝 Fix more issues with the `:raises:` formatting
peterhessey Aug 9, 2022
7619004
♻️ Restructuring folders
peterhessey Aug 9, 2022
bdc2a51
📝 Limit API `toctree` depth
peterhessey Aug 9, 2022
56c3a52
📝 Add primary InnerEye/ML files API to docs
peterhessey Aug 9, 2022
e9e5ee8
📝 Fix and add `InnerEye/ML/*.py` docs
peterhessey Aug 9, 2022
c1d84a8
⚰️ Remove weird `settings.json` change
peterhessey Aug 9, 2022
933bc7b
📌 Switch recommonmark to MyST-parser
peterhessey Aug 15, 2022
6e610b0
📌 Add myst-parser to `environment.yml`, lock env
peterhessey Aug 16, 2022
a9a5278
Fix conflicts merging main
peterhessey Aug 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
📝 Fix :return: statements
  • Loading branch information
peterhessey committed Aug 9, 2022
commit a778dacd84f7b42d3f2023c949a2fbe6442c5583
2 changes: 1 addition & 1 deletion InnerEye/Common/Statistics/mann_whitney_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def compose_distribution_comparisons(file_contents: List[List[List[str]]]) -> Li

:param file_contents: two or more lists of rows, where each "rows" is returned by read_csv_file on
(typically) a statistics.csv file
:return a list of lines to print
:return: a list of lines to print
"""
value_lists: List[Dict[str, List[float]]] = [parse_values(rows) for rows in file_contents]
return compose_distribution_comparisons_on_lists(value_lists)
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/SSL/lightning_containers/ssl_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _create_ssl_data_modules(self, is_ssl_encoder_module: bool) -> InnerEyeVisio
Returns torch lightning data module for encoder or linear head

:param is_ssl_encoder_module: whether to return the data module for SSL training or for linear head. If true,
:return transforms with two views per sample (batch like (img_v1, img_v2, label)). If False, return only one
:return: transforms with two views per sample (batch like (img_v1, img_v2, label)). If False, return only one
view per sample but also return the index of the sample in the dataset (to make sure we don't use twice the same
batch in one training epoch (batch like (index, img_v1, label), as classifier dataloader expected to be shorter
than SSL training, hence CombinedDataloader might loop over data several times per epoch).
Expand Down
6 changes: 3 additions & 3 deletions InnerEye/ML/SSL/lightning_containers/ssl_image_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class SSLClassifierContainer(SSLContainer):
"""
This module is used to train a linear classifier on top of a frozen (or not) encoder.

If you are running on AML, you can specify the SSL training run id via the --pretraining_run_recovery_id flag. This
will automatically download the checkpoints for you and take the latest one as the starting weights of your
If you are running on AML, you can specify the SSL training run id via the ``--pretraining_run_recovery_id`` flag.
This will automatically download the checkpoints for you and take the latest one as the starting weights of your
classifier.

If you are running locally, you can specify the path to your SSL weights via the --local_ssl_weights_path parameter.
If you are running locally, you can specify the path to your SSL weights via the ``--local_ssl_weights_path`` flag.

See docs/self_supervised_models.md for more details.
"""
Expand Down
5 changes: 4 additions & 1 deletion InnerEye/ML/SSL/lightning_modules/byol/byol_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ def setup(self, *args: Any, **kwargs: Any) -> None:
self.train_iters_per_epoch = self.hparams.num_samples // global_batch_size # type: ignore

def configure_optimizers(self) -> Any:
# exclude certain parameters
"""Testing this out

:return: _description_
"""
parameters = self.exclude_from_wt_decay(self.online_network.named_parameters(),
weight_decay=self.hparams.weight_decay) # type: ignore
optimizer = Adam(parameters,
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/lightning_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _get_metrics_at_optimal_cutoff(self) -> Tuple[torch.Tensor, torch.Tensor, to
difference between true positive rate and false positive rate is smallest. Then, computes
the false positive rate, false negative rate and accuracy at this threshold (i.e. when the
predicted probability is higher than the threshold the predicted label is 1 otherwise 0).
:returns: Tuple(optimal_threshold, false positive rate, false negative rate, accuracy)
:return: Tuple(optimal_threshold, false positive rate, false negative rate, accuracy)
"""
preds, targets = self._get_preds_and_targets()
if torch.unique(targets).numel() == 1:
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def compute_dice_across_patches(segmentation: torch.Tensor,
:param ground_truth: One-hot encoded torch tensor containing ground-truth label ids.
:param allow_multiple_classes_for_each_pixel: If set to False, ground-truth tensor has
to contain only one foreground label for each pixel.
:return A torch tensor of size (Patches, Classes) with the Dice scores. Dice scores are computed for
:return: A torch tensor of size (Patches, Classes) with the Dice scores. Dice scores are computed for
all classes including the background class at index 0.
"""
check_size_matches(segmentation, ground_truth, 4, 5, [0, -3, -2, -1],
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/metrics_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def get_metrics_at_optimal_cutoff(self, hue: str = DEFAULT_HUE_KEY) -> Tuple:
predicted probability is higher than the threshold the predicted label is 1 otherwise 0).

:param hue: The hue to restrict the values used for computation, otherwise all values will be used.
:returns: Tuple(optimal_threshold, false positive rate, false negative rate, accuracy)
:return: Tuple(optimal_threshold, false positive rate, false negative rate, accuracy)
"""
fpr, tpr, thresholds = roc_curve(self.get_labels(hue=hue), self.get_predictions(hue=hue))
optimal_idx = MetricsDict.get_optimal_idx(fpr=fpr, tpr=tpr)
Expand Down
6 changes: 3 additions & 3 deletions InnerEye/ML/model_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def segmentation_model_test_epoch(config: SegmentationModelBase,
:param epoch_and_split: A string that should uniquely identify the epoch and the data split (train/val/test).
:raises TypeError: If the arguments are of the wrong type.
:raises ValueError: When there are issues loading the model.
:return A list with the mean dice score (across all structures apart from background) for each image.
:return: A list with the mean dice score (across all structures apart from background) for each image.
"""
ml_util.set_random_seed(config.get_effective_random_seed(), "Model testing")
results_folder.mkdir(exist_ok=True)
Expand Down Expand Up @@ -212,7 +212,7 @@ def evaluate_model_predictions(process_id: int,
:param config: Segmentation model config object
:param dataset: Dataset object, it is used to load intensity image, labels, and patient metadata.
:param results_folder: Path to results folder
:returns [PatientMetadata, list[list]]: Patient metadata and list of computed metrics for each image.
:return: [PatientMetadata, list[list]]: Patient metadata and list of computed metrics for each image.
"""
sample = dataset.get_samples_at_index(index=process_id)[0]
logging.info(f"Evaluating predictions for patient {sample.patient_id}")
Expand Down Expand Up @@ -244,7 +244,7 @@ def populate_metrics_writer(
from evaluate_model_predictions

:param config: The SegmentationModelBase config from which we read the ground_truth_ids
:returns: A new MetricsPerPatientWriter and a list of foreground DICE score averages
:return: A new MetricsPerPatientWriter and a list of foreground DICE score averages
"""
average_dice: List[FloatOrInt] = []
metrics_writer = MetricsPerPatientWriter()
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/pipelines/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def predict_whole_image(self, image_channels: np.ndarray,
:param voxel_spacing_mm: Voxel spacing to use for each dimension in (Z x Y x X) order
:param mask: A binary image used to ignore results outside it in format: Z x Y x X.
:param patient_id: The identifier of the patient this image belongs to.
:return InferenceResult: that contains Segmentation for each of the classes and their posterior
:return: InferenceResult: that contains Segmentation for each of the classes and their posterior
probabilities.
"""
logging.info(f"Ensembling inference pipelines ({self._get_pipeline_ids()}) "
Expand Down
4 changes: 2 additions & 2 deletions InnerEye/ML/pipelines/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def create_from_checkpoint(path_to_checkpoint: Path,
:param model_config: Model related configurations.
:param pipeline_id: Numeric identifier for the pipeline (useful for logging when ensembling)

:returns InferencePipeline: an instantiated inference pipeline instance, or None if there was no checkpoint
:return: InferencePipeline: an instantiated inference pipeline instance, or None if there was no checkpoint
file for this epoch.
"""
if not path_to_checkpoint.is_file():
Expand Down Expand Up @@ -247,7 +247,7 @@ def predict_whole_image(self, image_channels: np.ndarray,
:param voxel_spacing_mm: Voxel spacing to use for each dimension in (Z x Y x X) order
:param mask: A binary image used to ignore results outside it in format: Z x Y x X.
:param patient_id: The identifier of the patient this image belongs to (defaults to 0 if None provided).
:return InferenceResult: that contains Segmentation for each of the classes and their posterior probabilities.
:return: InferenceResult: that contains Segmentation for each of the classes and their posterior probabilities.
"""
torch.cuda.empty_cache()
if image_channels is None:
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/run_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def register_model(self,

:param checkpoint_paths: Checkpoint paths to register.
:param model_proc: whether it's a single or ensemble model.
:returns Tuple element 1: AML model object, or None if no model could be registered.
:return: Tuple element 1: AML model object, or None if no model could be registered.
Tuple element 2: The result of running the model_deployment_hook, or None if no hook was supplied.
"""
if self.is_offline_run:
Expand Down
4 changes: 2 additions & 2 deletions InnerEye/ML/utils/dataset_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def encode(self, x: Dict[str, List[str]]) -> torch.Tensor:

def get_supported_dataset_column_names(self) -> List[str]:
"""
:returns list of categorical columns that are supported by this encoder
:return: list of categorical columns that are supported by this encoder
"""
return list(self._columns_and_possible_categories.keys())

Expand All @@ -87,7 +87,7 @@ def get_feature_length(self, feature_name: str) -> int:

:param feature_name: the name of the column for which to compute the feature
length.
:returns the feature length i.e. number of possible values for this feature.
:return: the feature length i.e. number of possible values for this feature.
"""
return self._feature_length[feature_name]

Expand Down
6 changes: 3 additions & 3 deletions InnerEye/ML/utils/device_aware_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ def __init__(self) -> None:

def get_devices(self) -> List[torch.device]:
"""
:return a list of device ids on which this module
:return: a list of device ids on which this module
is deployed.
"""
return list({x.device for x in self.parameters()})

def get_number_trainable_parameters(self) -> int:
"""
:return the number of trainable parameters in the module.
:return: the number of trainable parameters in the module.
"""
return sum(p.numel() for p in self.parameters() if p.requires_grad)

def is_model_on_gpu(self) -> bool:
"""
Checks if the model is cuda activated or not
:return True if the model is running on the GPU.
:return: True if the model is running on the GPU.
"""
try:
cuda_activated = next(self.parameters()).is_cuda
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/utils/features_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def standardize(self, sources: List[ScalarDataSource]) -> List[ScalarDataSource]
All features that have zero standard deviation (constant features) are left untouched.

:param sources: list of datasources.
:return list of data sources where all non-imaging features are standardized.
:return: list of data sources where all non-imaging features are standardized.
"""

def apply_source(source: ScalarDataSource) -> ScalarDataSource:
Expand Down
6 changes: 3 additions & 3 deletions InnerEye/ML/utils/image_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def apply_mask_to_posteriors(posteriors: NumpyOrTorch, mask: NumpyOrTorch) -> Nu

:param posteriors: image tensors in shape: Batches (optional) x Classes x Z x Y x X
:param mask: image tensor in shape: Batches (optional) x Z x Y x X
:return posteriors with mask applied
:return: posteriors with mask applied
"""
ml_util.check_size_matches(posteriors, mask, matching_dimensions=[-1, -2, -3])

Expand Down Expand Up @@ -211,7 +211,7 @@ def posteriors_to_segmentation(posteriors: NumpyOrTorch) -> NumpyOrTorch:

:param posteriors: Confidence maps [0,1] for each patch per class in format: Batches x Class x Z x Y x X
or Class x Z x Y x X for non-batched input
:returns segmentation: argmaxed posteriors with each voxel belonging to a single class: Batches x Z x Y x X
:return: segmentation: argmaxed posteriors with each voxel belonging to a single class: Batches x Z x Y x X
or Z x Y x X for non-batched input
"""

Expand Down Expand Up @@ -374,7 +374,7 @@ def get_center_crop(image: NumpyOrTorch, crop_shape: TupleInt3) -> NumpyOrTorch:

:param image: The original image to extract crop from
:param crop_shape: The shape of the center crop to extract
:return the center region as specified by the crop_shape argument.
:return: the center region as specified by the crop_shape argument.
"""
if image is None or crop_shape is None:
raise Exception("image and crop_shape must not be None")
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/utils/layer_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_padding_from_kernel_size(padding: PaddingMode,

:param num_dimensions: The number of dimensions that the returned padding tuple should have, if both
kernel_size and dilation are scalars.
:return padding value required for convolution layers based on input kernel size and dilation.
:return: padding value required for convolution layers based on input kernel size and dilation.
"""
if isinstance(kernel_size, Sized):
num_dimensions = len(kernel_size)
Expand Down
4 changes: 2 additions & 2 deletions InnerEye/ML/utils/metrics_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def convert_input_and_label(model_output: Union[torch.Tensor, np.ndarray],
label: Union[torch.Tensor, np.ndarray]) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Ensures that both model_output and label are tensors of dtype float32.
:return a Tuple with model_output, label as float tensors.
:return: a Tuple with model_output, label as float tensors.
"""
if not torch.is_tensor(model_output):
model_output = torch.tensor(model_output)
Expand All @@ -273,6 +273,6 @@ def is_missing_ground_truth(ground_truth: np.ndarray) -> bool:

:param ground_truth: ground truth binary array with dimensions: [Z x Y x X].
:param label_id: Integer index of the label to check.
:returns: True if the label is missing (signified by NaN), False otherwise.
:return: True if the label is missing (signified by NaN), False otherwise.
"""
return np.isnan(ground_truth[0, 0, 0])
2 changes: 1 addition & 1 deletion InnerEye/ML/utils/ml_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def is_test_from_execution_mode(execution_mode: ModelExecutionMode) -> bool:
"""
Returns a boolean by checking the execution type. The output is used to determine the properties
of the forward pass, e.g. model gradient updates or metric computation.
:return True if execution mode is VAL or TEST, False if TRAIN
:return: True if execution mode is VAL or TEST, False if TRAIN
:raise ValueError if the execution mode is invalid
"""
if execution_mode == ModelExecutionMode.TRAIN:
Expand Down
4 changes: 2 additions & 2 deletions InnerEye/ML/utils/surface_distance_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_run_output_dir(azure_config: AzureConfig, model_config: SegmentationMode

:param azure_config:
:param model_config:
:return output_dir: directory that all artifact paths use as a prefix
:return: output_dir: directory that all artifact paths use as a prefix
"""
if not azure_config.run_recovery_id:
raise ValueError("azure_config.run_recovery_id is not provided")
Expand Down Expand Up @@ -150,7 +150,7 @@ def get_subject_prefix(model_config: SegmentationModelBase, train_mode: ModelExe
:param model_config: Config
:param train_mode: Model execution mode -i.e. train, test or val
:param subject_id: ID of the subject
:return prefix: the filepath prefix within the container from which to download all artifacts
:return: prefix: the filepath prefix within the container from which to download all artifacts
"""
prefix = model_config.outputs_folder / BEST_EPOCH_FOLDER_NAME / train_mode.value / "{0:03d}".format(subject_id)
return prefix
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/utils/temperature_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def set_temperature(self,
:param labels: Labels to use to learn the temperature parameter
:param criterion_fn: A criterion function s.t: (logits, labels) => (loss, ECE)
:param use_gpu: If True then GPU will be used otherwise CPU will be used.
:return Optimal temperature value
:return: Optimal temperature value
"""
if use_gpu:
logits = logits.cuda()
Expand Down
2 changes: 1 addition & 1 deletion InnerEye/ML/visualizers/model_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def forward_preserve_state(module: DeviceAwareModule, inputs: List[torch.Tensor]

:param module: Callable torch module
:param inputs: List of input torch tensors
:return output: Output torch tensors
:return: output: Output torch tensors
"""
if not isinstance(inputs, list):
raise RuntimeError("Inputs object has to be a list of torch tensors")
Expand Down
2 changes: 1 addition & 1 deletion Tests/ML/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def csv_column_contains_value(
:param column_name: The name of the column in which we look for the value
:param value: The value to look for
:param contains_only_value: Check that this is the only value in the column (default True)
:returns: Boolean, whether the CSV column contains the value (and perhaps only the value)
:return: Boolean, whether the CSV column contains the value (and perhaps only the value)
"""
result = True
if not csv_file_path.exists:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation fora
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'furo'
Expand Down