|
a |
|
b/tests/model_contribution_test_manual.py |
|
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
# ! /usr/bin/env python |
|
|
3 |
""" script for quick local testing if a new model can be added and works inside medigan.""" |
|
|
4 |
# run with python -m tests.model_contribution_test_manual |
|
|
5 |
|
|
|
6 |
import glob |
|
|
7 |
import logging |
|
|
8 |
import shutil |
|
|
9 |
import sys |
|
|
10 |
import unittest |
|
|
11 |
|
|
|
12 |
try: |
|
|
13 |
from src.medigan.generators import Generators |
|
|
14 |
|
|
|
15 |
LOGGING_LEVEL = "INFO" |
|
|
16 |
logger = logging.getLogger() # (__name__) |
|
|
17 |
logger.setLevel(LOGGING_LEVEL) |
|
|
18 |
stream_handler = logging.StreamHandler(sys.stdout) |
|
|
19 |
stream_handler.setLevel(LOGGING_LEVEL) |
|
|
20 |
formatter = logging.Formatter( |
|
|
21 |
"%(asctime)s - %(name)s - %(levelname)s - %(message)s" |
|
|
22 |
) |
|
|
23 |
stream_handler.setFormatter(formatter) |
|
|
24 |
logger.addHandler(stream_handler) |
|
|
25 |
|
|
|
26 |
generators = Generators() |
|
|
27 |
|
|
|
28 |
# Testing init of contributor with correct params |
|
|
29 |
init_py_path = "../models/00012_C-DCGAN_MMG_MASSES/__init__.py" |
|
|
30 |
metadata_file_path = "../models/00012_C-DCGAN_MMG_MASSES/metadata.json" |
|
|
31 |
model_id = "00012_C-DCGAN_MMG_MASSES" |
|
|
32 |
|
|
|
33 |
zenodo_access_token = "ACCESS_TOKEN" |
|
|
34 |
github_access_token = "ACCESS_TOKEN" |
|
|
35 |
|
|
|
36 |
creator_name = "John Doe" |
|
|
37 |
creator_affiliation = "University of Barcelona" |
|
|
38 |
|
|
|
39 |
# Testing full model contribution workflow. |
|
|
40 |
generators.contribute( |
|
|
41 |
model_id=model_id, |
|
|
42 |
init_py_path=init_py_path, |
|
|
43 |
zenodo_access_token=zenodo_access_token, |
|
|
44 |
github_access_token=github_access_token, |
|
|
45 |
metadata_file_path=metadata_file_path, |
|
|
46 |
creator_name=creator_name, |
|
|
47 |
creator_affiliation=creator_affiliation, |
|
|
48 |
) |
|
|
49 |
|
|
|
50 |
# Testing init of contributor with erroneous params |
|
|
51 |
# contributor = generators.add_model_contributor(model_id ='Some model id', init_py_path="somePath") |
|
|
52 |
# contributor = generators.add_model_contributor(model_id ='00008_WGANGP_MMG_MASS_ROI', init_py_path="somePath") |
|
|
53 |
# contributor = generators.add_model_contributor(model_id ='Some model id', init_py_path="init_py_path") |
|
|
54 |
|
|
|
55 |
# Creating the model contributor |
|
|
56 |
# generators.add_model_contributor(model_id=model_id, init_py_path=init_py_path) |
|
|
57 |
|
|
|
58 |
# Adding the metadata of the model from input |
|
|
59 |
# generators.add_metadata_from_file( |
|
|
60 |
# model_id=model_id, metadata_file_path=metadata_file_path |
|
|
61 |
# ) |
|
|
62 |
|
|
|
63 |
# Alternatively, Adding the metadata of the model from file |
|
|
64 |
# metadata = contributor.add_metadata_from_input( |
|
|
65 |
# model_weights_name = "10000", |
|
|
66 |
# model_weights_extension=".pt", |
|
|
67 |
# generate_method_name = "generate", |
|
|
68 |
# dependencies=["numpy", "torch", "opencv-contrib-python-headless"]) |
|
|
69 |
|
|
|
70 |
# Add metadata to global.json config |
|
|
71 |
# generators.test_model(model_id=model_id) |
|
|
72 |
|
|
|
73 |
# Alternatively, explicitely providing model metadata to add the metadata to config |
|
|
74 |
# generators._add_model_to_config(model_id=model_id, metadata=metadata, metadata_file_path=metadata_file_path, |
|
|
75 |
# overwrite_existing_metadata=True) |
|
|
76 |
|
|
|
77 |
# Zenodo upload test |
|
|
78 |
# generators.push_to_zenodo( |
|
|
79 |
# model_id=model_id, |
|
|
80 |
# access_token=zenodo_access_token, |
|
|
81 |
# creator_name="test", |
|
|
82 |
# creator_affiliation="test affiliation", |
|
|
83 |
# ) |
|
|
84 |
|
|
|
85 |
# Manual Zenodo Test 1 |
|
|
86 |
# import requests |
|
|
87 |
# r = requests.get('https://zenodo.org/api/deposit/depositions', params = {'access_token': zenodo_access_token}) |
|
|
88 |
# print(r.status_code) |
|
|
89 |
# print(r.json()) |
|
|
90 |
|
|
|
91 |
# Manual Zenodo Test 2 |
|
|
92 |
# headers = {"Content-Type": "application/json"} |
|
|
93 |
# params = {"access_token": zenodo_access_token} |
|
|
94 |
# r = requests.post( |
|
|
95 |
# "https://zenodo.org/api/deposit/depositions", |
|
|
96 |
# params=params, |
|
|
97 |
# json={}, |
|
|
98 |
# headers=headers, |
|
|
99 |
# ) |
|
|
100 |
# print(r.json()) |
|
|
101 |
# print(r.status_code) |
|
|
102 |
|
|
|
103 |
# Github upload test |
|
|
104 |
# generators.push_to_github( |
|
|
105 |
# model_id=model_id, |
|
|
106 |
# github_access_token=github_access_token, |
|
|
107 |
# package_link=None, |
|
|
108 |
# creator_name="test", |
|
|
109 |
# creator_affiliation="test affiliation", |
|
|
110 |
# model_description="test description", |
|
|
111 |
# ) |
|
|
112 |
|
|
|
113 |
except Exception as e: |
|
|
114 |
logging.error(f"test_init_generators error: {e}") |
|
|
115 |
raise e |