Generators

class medigan.generators.Generators(config_manager: Optional[medigan.config_manager.ConfigManager] = None, model_selector: Optional[medigan.select_model.model_selector.ModelSelector] = None, model_executors: Optional[list] = None, model_contributors: Optional[list] = None, initialize_all_models: bool = False)[source]

Bases: object

Generators class: Contains medigan’s public methods to facilitate users’ automated sample generation workflows.

Parameters
  • config_manager (ConfigManager) – Provides the config dictionary, based on which model_ids are retrieved and models are selected and executed

  • model_selector (ModelSelector) – Provides model comparison, search, and selection based on keys/values in the selection part of the config dict

  • model_executors (list) – List of initialized ModelExecutor instances that handle model package download, init, and sample generation

  • initialize_all_models (bool) – Flag indicating, if True, that one ModelExecutor for each model_id in the config dict should be initialized triggered by creation of Generators class instance. Note that, if False, the Generators class will only initialize a ModelExecutor on the fly when need be i.e. when the generate method for the respective model is called.

config_manager

Provides the config dictionary, based on which model_ids are retrieved and models are selected and executed

Type

ConfigManager

model_selector

Provides model comparison, search, and selection based on keys/values in the selection part of the config dict

Type

ModelSelector

model_executors

List of initialized ModelExecutor instances that handle model package download, init, and sample generation

Type

list

Methods Summary

add_all_model_executors()

Add ModelExecutor class instances for all models available in the config.

add_metadata_from_file(model_id, ...)

Read and parse the metadata of a local model, identified by model_id, from a metadata file in json format.

add_metadata_from_input(model_id, ...[, ...])

Create a metadata dict for a local model, identified by model_id, given the necessary minimum metadata contents.

add_model_contributor(model_id[, init_py_path])

Add a ModelContributor instance of this model_id to the self.model_contributors list.

add_model_executor(model_id[, ...])

Add one ModelExecutor class instance corresponding to the specified model_id.

add_model_to_config(model_id, metadata[, ...])

Adding or updating a model entry in the global metadata.

contribute(model_id, init_py_path, ...[, ...])

Implements the full model contribution workflow including model metadata generation, model test, model Zenodo upload, and medigan github issue creation.

find_matching_models_by_values(values[, ...])

Search for values (and keys) in model configs and return a list of each matching ModelMatchCandidate.

find_model_and_generate(values[, ...])

Search for values (and keys) in model configs to generate samples with the found model.

find_model_executor_by_id(model_id)

Find and return the ModelExecutor instance of this model_id in the self.model_executors list.

find_models_and_rank(values[, ...])

Search for values (and keys) in model configs, rank results and return sorted list of model dicts.

find_models_rank_and_generate(values[, ...])

Search for values (and keys) in model configs, rank results to generate samples with highest ranked model.

generate(model_id[, num_samples, ...])

Generate samples with the model corresponding to the model_id or return the model's generate function.

get_as_torch_dataloader([dataset, model_id, ...])

Get torch Dataloader sampling synthetic data from medigan model.

get_as_torch_dataset(model_id[, ...])

Get synthetic data in a torch Dataset for specified medigan model.

get_config_by_id(model_id[, config_key])

Get and return the part of the config below a config_key for a specific model_id.

get_generate_function(model_id[, ...])

Return the model's generate function.

get_model_contributor_by_id(model_id)

Find and return the ModelContributor instance of this model_id in the self.model_contributors list.

get_model_executor(model_id[, ...])

Add and return the ModelExecutor instance of this model_id from the self.model_executors list.

get_models_by_key_value_pair(key1, value1[, ...])

Get and return a list of model_id dicts that contain the specified key value pair in their selection config.

get_selection_criteria_by_id(model_id[, ...])

Get and return the selection config dict for a specific model_id.

get_selection_criteria_by_ids([model_ids, ...])

Get and return a list of selection config dicts for each of the specified model_ids.

get_selection_keys([model_id])

Get and return all first level keys from the selection config dict for a specific model_id.

get_selection_values_for_key(key[, model_id])

Get and return the value of a specified key of the selection dict in the config for a specific model_id.

is_model_executor_already_added(model_id)

Check whether the ModelExecutor instance of this model_id is already in self.model_executors list.

is_model_metadata_valid(model_id, metadata)

Checking if a model's corresponding metadata is valid.

list_models()

Return the list of model_ids as strings based on config.

push_to_github(model_id, github_access_token)

Upload the model's metadata inside a github issue to the medigan github repository.

push_to_zenodo(model_id, zenodo_access_token)

Upload the model files as zip archive to a public Zenodo repository where the model will be persistently stored.

rank_models_by_performance([model_ids, ...])

Rank model based on a provided metric and return sorted list of model dicts.

test_model(model_id[, is_local_model, ...])

Test if a model generates and returns a specific number of samples in the correct format

visualize(model_id[, slider_grouper, ...])

Initialize and run ModelVisualizer of this model_id if it is available.

Methods Documentation

add_all_model_executors()[source]

Add ModelExecutor class instances for all models available in the config.

Return type

None

add_metadata_from_file(model_id: str, metadata_file_path: str) dict[source]

Read and parse the metadata of a local model, identified by model_id, from a metadata file in json format.

Parameters
  • model_id (str) – The generative model’s unique id

  • metadata_file_path (str) – the path pointing to the metadata file

Returns

Returns a dict containing the contents of parsed metadata json file.

Return type

dict

add_metadata_from_input(model_id: str, model_weights_name: str, model_weights_extension: str, generate_method_name: str, dependencies: list, fill_more_fields_interactively: bool = True, output_path: str = 'config') dict[source]

Create a metadata dict for a local model, identified by model_id, given the necessary minimum metadata contents.

Parameters
  • model_id (str) – The generative model’s unique id

  • model_weights_name (str) – the name of the checkpoint file containing the model’s weights

  • model_weights_extension (str) – the extension (e.g. .pt) of the checkpoint file containing the model’s weights

  • generate_method_name (str) – the name of the sample generation method inside the models __init__.py file

  • dependencies (list) – the list of dependencies that need to be installed via pip to run the model

  • fill_more_fields_interactively (bool) – flag indicating whether a user will be interactively asked via command line for further input to fill out missing metadata content

  • output_path (str) – the path where the created metadata json file will be stored

Returns

Returns a dict containing the contents of the metadata json file.

Return type

dict

add_model_contributor(model_id: str, init_py_path: Optional[str] = None) medigan.contribute_model.model_contributor.ModelContributor[source]

Add a ModelContributor instance of this model_id to the self.model_contributors list.

Parameters
  • model_id (str) – The generative model’s unique id

  • init_py_path (str) – The path to the local model’s __init__.py file needed for importing and running this model.

Returns

ModelContributor class instance corresponding to the model_id

Return type

ModelContributor

add_model_executor(model_id: str, install_dependencies: bool = False)[source]

Add one ModelExecutor class instance corresponding to the specified model_id.

Parameters
  • model_id (str) – The generative model’s unique id

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

Return type

None

add_model_to_config(model_id: str, metadata: dict, is_local_model: Optional[bool] = None, overwrite_existing_metadata: bool = False, store_new_config: bool = True) bool[source]

Adding or updating a model entry in the global metadata.

Parameters
  • model_id (str) – The generative model’s unique id

  • metadata (dict) – The model’s corresponding metadata

  • is_local_model (bool) – flag indicating whether the tested model is a new local user model i.e not yet part of medigan’s official models

  • overwrite_existing_metadata (bool) – in case of is_local_model, flag indicating whether existing metadata for this model in medigan’s config/global.json should be overwritten.

  • store_new_config (bool) – flag indicating whether the current model metadata should be stored on disk i.e. in config/

Returns

Flag indicating whether model metadata update was successfully concluded

Return type

bool

contribute(model_id: str, init_py_path: str, github_access_token: str, zenodo_access_token: str, metadata_file_path: Optional[str] = None, model_weights_name: Optional[str] = None, model_weights_extension: Optional[str] = None, generate_method_name: Optional[str] = None, dependencies: Optional[list] = None, fill_more_fields_interactively: bool = True, overwrite_existing_metadata: bool = False, output_path: str = 'config', creator_name: str = 'unknown name', creator_affiliation: str = 'unknown affiliation', model_description: str = '', install_dependencies: bool = False)[source]

Implements the full model contribution workflow including model metadata generation, model test, model Zenodo upload, and medigan github issue creation.

Parameters
  • model_id (str) – The generative model’s unique id

  • init_py_path (str) – The path to the local model’s __init__.py file needed for importing and running this model.

  • github_access_token (str) – a personal access token linked to your github user account, used as means of authentication

  • zenodo_access_token (str) – a personal access token in Zenodo linked to a user account for authentication

  • metadata_file_path (str) – the path pointing to the metadata file

  • model_weights_name (str) – the name of the checkpoint file containing the model’s weights

  • model_weights_extension (str) – the extension (e.g. .pt) of the checkpoint file containing the model’s weights

  • generate_method_name (str) – the name of the sample generation method inside the models __init__.py file

  • dependencies (list) – the list of dependencies that need to be installed via pip to run the model

  • fill_more_fields_interactively (bool) – flag indicating whether a user will be interactively asked via command line for further input to fill out missing metadata content

  • overwrite_existing_metadata (bool) – flag indicating whether existing metadata for this model in medigan’s config/global.json should be overwritten.

  • output_path (str) – the path where the created metadata json file will be stored

  • creator_name (str) – the creator name that will appear on the corresponding github issue

  • creator_affiliation (str) – the creator affiliation that will appear on the corresponding github issue

  • model_description (list) – the model_description that will appear on the corresponding github issue

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

Returns

Returns the url pointing to the corresponding issue on github

Return type

str

find_matching_models_by_values(values: list, target_values_operator: str = 'AND', are_keys_also_matched: bool = False, is_case_sensitive: bool = False) list[source]

Search for values (and keys) in model configs and return a list of each matching ModelMatchCandidate.

This function calls an identically named function in a ModelSelector instance.

Parameters
  • values (list) – list of values used to search and find models corresponding to these values

  • target_values_operator (str) – the operator indicating the relationship between values in the evaluation of model search results. Should be either “AND”, “OR”, or “XOR”.

  • are_keys_also_matched (bool) – flag indicating whether, apart from values, the keys in the model config should also be searchable

  • is_case_sensitive (bool) – flag indicating whether the search for values (and) keys in the model config should be case-sensitive.

Returns

a list of ModelMatchCandidate class instances each of which was successfully matched against the search values.

Return type

list

find_model_and_generate(values: list, target_values_operator: str = 'AND', are_keys_also_matched: bool = False, is_case_sensitive: bool = False, num_samples: int = 30, output_path: Optional[str] = None, is_gen_function_returned: bool = False, install_dependencies: bool = False, **kwargs)[source]

Search for values (and keys) in model configs to generate samples with the found model.

Note that the number of found models should be ==1. Else no samples will be generated and a error is logged to console.

Parameters
  • values (list) – list of values used to search and find models corresponding to these values

  • target_values_operator (str) – the operator indicating the relationship between values in the evaluation of model search results. Should be either “AND”, “OR”, or “XOR”.

  • are_keys_also_matched (bool) – flag indicating whether, apart from values, the keys in the model config should also be searchable

  • is_case_sensitive (bool) – flag indicating whether the search for values (and) keys in the model config should be case-sensitive.

  • num_samples (int) – the number of samples that will be generated

  • output_path (str) – the path as str to the output folder where the generated samples will be stored

  • is_gen_function_returned (bool) – flag indicating whether, instead of generating samples, the sample generation function will be returned

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

  • **kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function

Returns

However, if is_gen_function_returned is True, it returns the internal generate function of the model.

Return type

None

find_model_executor_by_id(model_id: str) medigan.execute_model.model_executor.ModelExecutor[source]

Find and return the ModelExecutor instance of this model_id in the self.model_executors list.

Parameters

model_id (str) – The generative model’s unique id

Returns

ModelExecutor class instance corresponding to the model_id

Return type

ModelExecutor

find_models_and_rank(values: list, target_values_operator: str = 'AND', are_keys_also_matched: bool = False, is_case_sensitive: bool = False, metric: str = 'SSIM', order: str = 'asc') list[source]

Search for values (and keys) in model configs, rank results and return sorted list of model dicts.

This function calls an identically named function in a ModelSelector instance.

Parameters
  • values (list`) – list of values used to search and find models corresponding to these values

  • target_values_operator (str) – the operator indicating the relationship between values in the evaluation of model search results. Should be either “AND”, “OR”, or “XOR”.

  • are_keys_also_matched (bool) – flag indicating whether, apart from values, the keys in the model config should also be searchable

  • is_case_sensitive (bool) – flag indicating whether the search for values (and) keys in the model config should be case-sensitive.

  • metric (str) – The key in the selection dict that corresponds to the metric of interest

  • order (str) – the sorting order of the ranked results. Should be either “asc” (ascending) or “desc” (descending)

Returns

a list of the searched and matched model dictionaries containing metric and model_id, sorted by metric.

Return type

list

find_models_rank_and_generate(values: list, target_values_operator: str = 'AND', are_keys_also_matched: bool = False, is_case_sensitive: bool = False, metric: str = 'SSIM', order: str = 'asc', num_samples: int = 30, output_path: Optional[str] = None, is_gen_function_returned: bool = False, install_dependencies: bool = False, **kwargs)[source]

Search for values (and keys) in model configs, rank results to generate samples with highest ranked model.

Parameters
  • values (list) – list of values used to search and find models corresponding to these values

  • target_values_operator (str) – the operator indicating the relationship between values in the evaluation of model search results. Should be either “AND”, “OR”, or “XOR”.

  • are_keys_also_matched (bool) – flag indicating whether, apart from values, the keys in the model config should also be searchable

  • is_case_sensitive (bool) – flag indicating whether the search for values (and) keys in the model config should be case-sensitive.

  • metric (str) – The key in the selection dict that corresponds to the metric of interest

  • order (str) – the sorting order of the ranked results. Should be either “asc” (ascending) or “desc” (descending)

  • num_samples (int) – the number of samples that will be generated

  • output_path (str) – the path as str to the output folder where the generated samples will be stored

  • is_gen_function_returned (bool) – flag indicating whether, instead of generating samples, the sample generation function will be returned

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

  • **kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function

Returns

However, if is_gen_function_returned is True, it returns the internal generate function of the model.

Return type

None

generate(model_id: str, num_samples: int = 30, output_path: Optional[str] = None, save_images: bool = True, is_gen_function_returned: bool = False, install_dependencies: bool = False, **kwargs)[source]

Generate samples with the model corresponding to the model_id or return the model’s generate function.

Parameters
  • model_id (str) – The generative model’s unique id

  • num_samples (int) – the number of samples that will be generated

  • output_path (str) – the path as str to the output folder where the generated samples will be stored

  • save_images (bool) – flag indicating whether generated samples are returned (i.e. as list of numpy arrays) or rather stored in file system (i.e in output_path)

  • is_gen_function_returned (bool) – flag indicating whether, instead of generating samples, the sample generation function will be returned

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

  • **kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function

Returns

Returns images as list of numpy arrays if save_images is False. However, if is_gen_function_returned is True, it returns the internal generate function of the model.

Return type

list

get_as_torch_dataloader(dataset=None, model_id: Optional[str] = None, num_samples: int = 1000, install_dependencies: bool = False, transform=None, batch_size=None, shuffle=None, sampler=None, batch_sampler=None, num_workers=None, collate_fn=None, pin_memory=None, drop_last=None, timeout=None, worker_init_fn=None, prefetch_factor: Optional[int] = None, persistent_workers: Optional[bool] = None, pin_memory_device: Optional[str] = None, **kwargs) torch.utils.data.dataloader.DataLoader[source]

Get torch Dataloader sampling synthetic data from medigan model.

Dataloader combines a dataset and a sampler, and provides an iterable over the given torch dataset. Dataloader is created for synthetic data for the specified medigan model. Pytorch native parameters are set to None per default. Only those params are are passed to the Dataloader() initialization function that are not None.

Parameters
  • dataset (Dataset) – dataset from which to load the data.

  • model_id – str The generative model’s unique id

  • num_samples – int the number of samples that will be generated

  • install_dependencies – bool flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

  • **kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function (e.g. the input path for image-to-image translation models in medigan).

  • transform – the torch data transformation functions to be applied to the data in the dataset.

  • batch_size (int, optional) – how many samples per batch to load (default: None).

  • shuffle (bool, optional) – set to True to have the data reshuffled at every epoch (default: None).

  • sampler (Sampler or Iterable, optional) – defines the strategy to draw samples from the dataset. Can be any Iterable with __len__ implemented. If specified, shuffle must not be specified. (default: None)

  • batch_sampler (Sampler or Iterable, optional) – like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_size, shuffle, sampler, and drop_last. (default: None)

  • num_workers (int, optional) – how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: None)

  • collate_fn (callable, optional) – merges a list of samples to form a mini-batch of Tensor(s). Used when using batched loading from a map-style dataset. (default: None)

  • pin_memory (bool, optional) – If True, the data loader will copy Tensors into CUDA pinned memory before returning them. If your data elements are a custom type, or your collate_fn returns a batch that is a custom type, see the example below. (default: None)

  • drop_last (bool, optional) – set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: None)

  • timeout (numeric, optional) – if positive, the timeout value for collecting a batch from workers. Should always be non-negative. (default: None)

  • worker_init_fn (callable, optional) – If not None, this will be called on each worker subprocess with the worker id (an int in [0, num_workers - 1]) as input, after seeding and before data loading. (default: None)

  • prefetch_factor (int, optional, keyword-only arg) – Number of batches loaded in advance by each worker. 2 means there will be a total of 2 * num_workers batches prefetched across all workers. (default: None).

  • persistent_workers (bool, optional) – If True, the data loader will not shutdown the worker processes after a dataset has been consumed once. This allows to maintain the workers Dataset instances alive. (default: None)

  • pin_memory_device (str, optional) – the device to pin memory to if pin_memory is True (default: None).

Returns

a torch.utils.data.DataLoader object with data generated by model corresponding to inputted Dataset or model_id.

Return type

DataLoader

get_as_torch_dataset(model_id: str, num_samples: int = 100, install_dependencies: bool = False, transform=None, **kwargs) torch.utils.data.dataset.Dataset[source]

Get synthetic data in a torch Dataset for specified medigan model.

The dataset returns a dict with keys sample (== image), labels (== condition), and mask (== segmentation mask). While key ‘sample’ is mandatory, the other key value pairs are only returned if applicable to generative model.

Parameters
  • model_id – str The generative model’s unique id

  • num_samples – int the number of samples that will be generated

  • install_dependencies

    bool

    flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

    transform

    the torch data transformation functions to be applied to the data in the dataset.

  • **kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function (e.g. the input path for image-to-image translation models in medigan).

Returns

a torch.utils.data.Dataset object with data generated by model corresponding to model_id.

Return type

Dataset

get_config_by_id(model_id: str, config_key: Optional[str] = None) dict[source]

Get and return the part of the config below a config_key for a specific model_id.

The config_key parameters can be separated by a ‘.’ (dot) to allow for retrieval of nested config keys, e.g, ‘execution.generator.name’

This function calls an identically named function in a ConfigManager instance.

Parameters
  • model_id (str) – The generative model’s unique id

  • config_key (str) – A key of interest present in the config dict

Returns

a dictionary from the part of the config file corresponding to model_id and config_key.

Return type

dict

get_generate_function(model_id: str, num_samples: int = 30, output_path: Optional[str] = None, install_dependencies: bool = False, **kwargs)[source]

Return the model’s generate function.

Relies on the self.generate function.

Parameters
  • model_id (str) – The generative model’s unique id

  • num_samples (int) – the number of samples that will be generated

  • output_path (str) – the path as str to the output folder where the generated samples will be stored

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

  • **kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function

Returns

The internal reusable generate function of the generative model.

Return type

function

get_model_contributor_by_id(model_id: str) medigan.contribute_model.model_contributor.ModelContributor[source]

Find and return the ModelContributor instance of this model_id in the self.model_contributors list.

Parameters

model_id (str) – The generative model’s unique id

Returns

ModelContributor class instance corresponding to the model_id

Return type

ModelContributor

get_model_executor(model_id: str, install_dependencies: bool = False) medigan.execute_model.model_executor.ModelExecutor[source]

Add and return the ModelExecutor instance of this model_id from the self.model_executors list.

Relies on self.add_model_executor and self.find_model_executor_by_id functions.

Parameters
  • model_id (str) – The generative model’s unique id

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

Returns

ModelExecutor class instance corresponding to the model_id

Return type

ModelExecutor

get_models_by_key_value_pair(key1: str, value1: str, is_case_sensitive: bool = False) list[source]

Get and return a list of model_id dicts that contain the specified key value pair in their selection config.

The key param can contain ‘.’ (dot) separations to allow for retrieval of nested config keys such as ‘execution.generator.name’

This function calls an identically named function in a ModelSelector instance.

Parameters
  • key1 (str) – The key in the selection dict

  • value1 (str) – The value in the selection dict that corresponds to key1

  • is_case_sensitive (bool) – flag to evaluate keys and values with case sensitivity if set to True

Returns

a list of the dictionaries each containing a models id and the found key-value pair in the models config

Return type

list

get_selection_criteria_by_id(model_id: str, is_model_id_removed: bool = True) dict[source]

Get and return the selection config dict for a specific model_id.

This function calls an identically named function in a ModelSelector instance.

Parameters
  • model_id (str) – The generative model’s unique id

  • is_model_id_removed (bool) – flag to to remove the model_ids from first level of dictionary.

Returns

a dictionary corresponding to the selection config of a model

Return type

dict

get_selection_criteria_by_ids(model_ids: Optional[list] = None, are_model_ids_removed: bool = True) list[source]

Get and return a list of selection config dicts for each of the specified model_ids.

This function calls an identically named function in a ModelSelector instance.

Parameters
  • model_ids (list) – A list of generative models’ unique ids or ids abbreviated as integers (e.g. 1, 2, .. 21)

  • are_model_ids_removed (bool) – flag to remove the model_ids from first level of dictionary.

Returns

a list of dictionaries each corresponding to the selection config of a model

Return type

list

get_selection_keys(model_id: Optional[str] = None) list[source]

Get and return all first level keys from the selection config dict for a specific model_id.

This function calls an identically named function in a ModelSelector instance.

Parameters

model_id (str) – The generative model’s unique id

Returns

a list containing the keys as strings of the selection config of the model_id.

Return type

list

get_selection_values_for_key(key: str, model_id: Optional[str] = None) list[source]

Get and return the value of a specified key of the selection dict in the config for a specific model_id.

The key param can contain ‘.’ (dot) separations to allow for retrieval of nested config keys such as ‘execution.generator.name’

This function calls an identically named function in a ModelSelector instance.

Parameters
  • key (str) – The key in the selection dict

  • model_id (str) – The generative model’s unique id

Returns

a list of the values that correspond to the key in the selection config of the model_id.

Return type

list

is_model_executor_already_added(model_id) bool[source]

Check whether the ModelExecutor instance of this model_id is already in self.model_executors list.

Parameters

model_id (str) – The generative model’s unique id

Returns

indicating whether this ModelExecutor had been already previously added to self.model_executors

Return type

bool

is_model_metadata_valid(model_id: str, metadata: dict, is_local_model: bool = True) bool[source]

Checking if a model’s corresponding metadata is valid.

Specific fields in the model’s metadata are mandatory. It is asserted if these key value pairs are present.

Parameters
  • model_id (str) – The generative model’s unique id

  • metadata (dict) – The model’s corresponding metadata

  • is_local_model (bool) – flag indicating whether the tested model is a new local user model i.e not yet part of medigan’s official models

Returns

Flag indicating whether the specific model’s metadata format and fields are valid

Return type

bool

list_models() list[source]

Return the list of model_ids as strings based on config.

Return type

list

push_to_github(model_id: str, github_access_token: str, package_link: Optional[str] = None, creator_name: str = '', creator_affiliation: str = '', model_description: str = '')[source]

Upload the model’s metadata inside a github issue to the medigan github repository.

To add your model to medigan, your metadata will be reviewed on Github and added to medigan’s official model metadata

The medigan repository issues page: https://github.com/RichardObi/medigan/issues

Get your Github access token here: https://github.com/settings/tokens

Parameters
  • model_id (str) – The generative model’s unique id

  • github_access_token (str) – a personal access token linked to your github user account, used as means of authentication

  • package_link – a package link

  • creator_name (str) – the creator name that will appear on the corresponding github issue

  • creator_affiliation (str) – the creator affiliation that will appear on the corresponding github issue

  • model_description (list) – the model_description that will appear on the corresponding github issue

Returns

Returns the url pointing to the corresponding issue on github

Return type

str

push_to_zenodo(model_id: str, zenodo_access_token: str, creator_name: str = 'unknown name', creator_affiliation: str = 'unknown affiliation', model_description: str = '') str[source]

Upload the model files as zip archive to a public Zenodo repository where the model will be persistently stored.

Get your Zenodo access token here: https://zenodo.org/account/settings/applications/tokens/new/ (Enable scopes deposit:actions and deposit:write)

Parameters
  • model_id (str) – The generative model’s unique id

  • zenodo_access_token (str) – a personal access token in Zenodo linked to a user account for authentication

  • creator_name (str) – the creator name that will appear on the corresponding Zenodo model upload homepage

  • creator_affiliation (str) – the creator affiliation that will appear on the corresponding Zenodo model upload homepage

  • model_description (list) – the model_description that will appear on the corresponding Zenodo model upload homepage

Returns

Returns the url pointing to the corresponding Zenodo model upload homepage

Return type

str

rank_models_by_performance(model_ids: Optional[list] = None, metric: str = 'SSIM', order: str = 'asc') list[source]

Rank model based on a provided metric and return sorted list of model dicts.

The metric param can contain ‘.’ (dot) separations to allow for retrieval of nested metric config keys such as ‘downstream_task.CLF.accuracy’

This function calls an identically named function in a ModelSelector instance.

Parameters
  • model_ids (list) – only evaluate the model_ids in this list. If none, evaluate all available model_ids

  • metric (str) – The key in the selection dict that corresponds to the metric of interest

  • order (str) – the sorting order of the ranked results. Should be either “asc” (ascending) or “desc” (descending)

Returns

a list of model dictionaries containing metric and model_id, sorted by metric.

Return type

list

test_model(model_id: str, is_local_model: bool = True, overwrite_existing_metadata: bool = False, store_new_config: bool = True, num_samples: int = 3, install_dependencies: bool = False)[source]

Test if a model generates and returns a specific number of samples in the correct format

Parameters
  • model_id (str) – The generative model’s unique id

  • is_local_model (bool) – flag indicating whether the tested model is a new local user model i.e not yet part of medigan’s official models

  • overwrite_existing_metadata (bool) – in case of is_local_model, flag indicating whether existing metadata for this model in medigan’s config/global.json should be overwritten.

  • store_new_config (bool) – flag indicating whether the current model metadata should be stored on disk i.e. in config/

  • num_samples (int) – the number of samples that will be generated

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.

visualize(model_id: str, slider_grouper: int = 10, auto_close: bool = False, install_dependencies: bool = False) None[source]

Initialize and run ModelVisualizer of this model_id if it is available. It allows to visualize a sample from the model’s output. UI window will pop up allowing the user to control the generation parameters (conditional and unconditional ones).

Parameters
  • model_id (str) – The generative model’s unique id to visualize.

  • slider_grouper (int) – Number of input parameters to group together within one slider.

  • auto_close (bool) – Flag for closing the user interface automatically after time. Used while testing.

  • install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.