medigan.contribute_model package
Submodules
medigan.contribute_model.base_model_uploader module
Base Model uploader class that uploads models to medigan associated data storage services.
- class medigan.contribute_model.base_model_uploader.BaseModelUploader(model_id: str, metadata: dict)[source]
Bases:
object
BaseModelUploader class: Uploads a user’s model and metadata to third party storage to allow its inclusion into the medigan library.
- Parameters
model_id (str) – The generative model’s unique id
metadata (dict) – The model’s corresponding metadata
- model_id
The generative model’s unique id
- Type
str
- metadata
The model’s corresponding metadata
- Type
dict
medigan.contribute_model.github_model_uploader module
Github Model uploader class that uploads the metadata of a new model to the medigan github repository.
- class medigan.contribute_model.github_model_uploader.GithubModelUploader(model_id: str, access_token: str)[source]
Bases:
medigan.contribute_model.base_model_uploader.BaseModelUploader
GithubModelUploader class: Pushes the metadata of a user’s model to the medigan repo, where it creates a dedicated github issue.
- Parameters
model_id (str) – The generative model’s unique id
access_token (str) – a personal access token linked to your github user account, used as means of authentication
- model_id
The generative model’s unique id
- Type
str
- access_token
a personal access token linked to your github user account, used as means of authentication
- Type
str
- add_package_link_to_metadata(metadata: dict, package_link: Optional[str] = None, is_update_forced: bool = False) dict [source]
Update package_link in the model’s metadata if current package_link does not containing a valid url.
- Parameters
metadata (dict) – The model’s corresponding metadata
package_link (str) – the new package link to used to replace the old one
is_update_forced (bool) – flag to update metadata even though metadata already contains a valid url in its package_link
- Returns
Returns the updated metadata dict with replaced package_link if replacement was applicable.
- Return type
dict
- push(metadata: dict, package_link: Optional[str] = None, creator_name: str = 'n.a.', creator_affiliation: str = 'n.a.', model_description: str = 'n.a.')[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
metadata (dict) – The model’s corresponding metadata
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
medigan.contribute_model.model_contributor module
Model contributor class that tests models, creates metadata entries, uploads and contributes them to medigan.
- class medigan.contribute_model.model_contributor.ModelContributor(model_id: str, init_py_path: str)[source]
Bases:
object
ModelContributor class: Contributes a user’s local model to the public medigan library
- 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.
- model_id
The generative model’s unique id
- Type
str
- init_py_path
The path to the local model’s __init__.py file needed for importing and running this model.
- Type
str
- package_path
Path as string to the generative model’s python package
- Type
str
- package_name
Name of the model’s python package i.e. the name of the model’s zip file and unzipped package folder
- Type
str
- metadata_file_path
Path as string to the generative model’s metadata file e.g. default is relative path to package root.
- Type
str
- zenodo_model_uploader
An instance of the ZenodoModelUploader class
- Type
str
- github_model_uploader
An instance of the GithubModelUploader class.
- Type
str
- add_metadata_from_file(metadata_file_path) 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_weights_name: Optional[str] = None, model_weights_extension: Optional[str] = None, generate_method_name: Optional[str] = None, dependencies: list = [], fill_more_fields_interactively: bool = True, output_path: str = 'config')[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
- is_value_for_key_already_set(key: str, metadata: dict, nested_key) bool [source]
Check if the value of a key in a metadata dictionary is already set and e.g. not an empty string, dict or list.
- Parameters
key (str) – The key in the currently traversed part of the model’s metadata dictionary
metadata (dict) – The currently traversed part of the model’s metadata dictionary
nested_key (str) – the nested_key indicates which subpart of the model’s metadata we are currently traversing
- Returns
Flag indicating whether a value exists for the key in the dict
- Return type
bool
- load_metadata_template() dict [source]
Loads and parses (json to dict) a default medigan metadata template.
- Returns
Returns the metadata template as dict
- Return type
dict
- push_to_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
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(access_token: str, creator_name: str, creator_affiliation: str, model_description: 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
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
- validate_and_update_model_weights_path() dict [source]
Check if the model files can be found in the package_path or based on the path_to_metadata.
Ideally, the user provided package_path and the path_to_metadata should both point to the same model package containing weights, config, license, etc. Here we check both of these paths to find the model weights.
- Returns
Returns the metadata after updating the path to the model’s checkpoint’s weights
- Return type
dict
- validate_init_py_path(init_py_path) bool [source]
Asserts whether the init_py_path exists and points to a valid __init__.py correct file.
- Parameters
init_py_path (str) – The path to the local model’s __init__.py file needed for importing and running this model.
- validate_local_model_import()[source]
Check if the model package in the package_path can be imported as python library using importlib.
- validate_model_id(model_id: str, max_chars: int = 30, min_chars: int = 13) bool [source]
Asserts if the model_id is in the correct format and has a valid length
- Parameters
model_id (str) – The generative model’s unique id
max_chars (int) – the maximum of chars allowed in the model_id
min_chars (int) – the minimum of chars allowed in the model_id
- Returns
Returns flag indicating whether the model_id is correctly formatted.
- Return type
bool
medigan.contribute_model.zenodo_model_uploader module
Zenodo Model uploader class that uploads models to medigan associated data storage on Zenodo.
- class medigan.contribute_model.zenodo_model_uploader.ZenodoModelUploader(model_id, access_token)[source]
Bases:
medigan.contribute_model.base_model_uploader.BaseModelUploader
ZenodoModelUploader class: Uploads a user’s model via API to Zenodo, here it is permanently stored with DOI.
- Parameters
model_id (str) – The generative model’s unique id
access_token (str) – a personal access token in Zenodo linked to a user account for authentication
- model_id
The generative model’s unique id
- Type
str
- access_token
a personal access token in Zenodo linked to a user account for authentication
- Type
str
- create_upload_description(metadata: dict, model_description: str = '') str [source]
Create a string containing the textual description that will accompany the upload model files.
The string contains tags and a text retrieved from the description subsection of the model metadata.
- Parameters
metadata (dict) – The model’s corresponding metadata
model_description (str) – the model_description that will appear on the corresponding Zenodo model upload homepage
- Returns
Returns the textual description of the model upload
- Return type
str
- create_upload_json_data(creator_name: str, creator_affiliation: str, description: str = '') dict [source]
Create some descriptive data in dict format to be uploaded and stored alongside the model files.
- Parameters
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
description (str) – the model_description that will appear on the corresponding Zenodo model upload homepage
- Returns
Returns the descriptive data in dictionary structure describing the model upload.
- Return type
dict
- empty_upload() dict [source]
Upload an empty placeholder entry to Zenodo as is required to retrieve a deposition_id and bucket_url.
deposition_id and bucket_url aare needed for file upload and publishing in the subsequent upload steps.
- Returns
Returns the response retrieved via the Zenodo API
- Return type
dict
- locate_or_create_model_zip_file(package_path: str, package_name: str) -> (<class 'str'>, <class 'str'>)[source]
If not possible to locate, create a zipped python package of the model.
- Parameters
package_path (str) – Path as string to the generative model’s python package containing an __init__.py file
package_name (str) – Name of the model’s python package i.e. the name of the model’s zip file and unzipped package folder
- Returns
Returns a tuple containing two strings: The filename and the file_path of and to the zipped python package
- Return type
tuple
- publish(deposition_id: str) dict [source]
Publish a zenodo upload.
This makes the upload official, as it will then be publicly accessible and persistently stored on Zenodo with associated DOI.
- Parameters
deposition_id (str) – The deposition id assigned by Zenodo to the uploaded model file
- Returns
Returns the response retrieved via the Zenodo API
- Return type
dict
- push(metadata: dict, package_path: str, package_name: str, creator_name: str, creator_affiliation: str, model_description: 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
metadata (dict) – The model’s corresponding metadata
package_path (dict) – The path to the packaged model files
package_name (dict) – The name of the packaged model files
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
- upload(file_path: str, filename: str, bucket_url: str) dict [source]
Upload a file to Zenodo entry of the uploaded model files.
- Parameters
file_path (str) – The path of the file that is uploaded to Zenodo
filename (str) – The name of the file that is uploaded to Zenodo
bucket_url (str) – The bucket url used in the PUT request to upload the data file.
- Returns
Returns the response retrieved via the Zenodo API
- Return type
dict
- upload_descriptive_data(deposition_id: str, data: dict) dict [source]
Upload textual descriptive data to be associated and added to the Zenodo entry of the uploaded model files.
- Parameters
deposition_id (str) – The deposition id assigned by Zenodo to the uploaded model file
data (dict) – The descriptive information that will to be uploaded to Zenodo and associated with the desposition_id
- Returns
Returns the response retrieved via the Zenodo API
- Return type
dict