|
a |
|
b/config/serde.py |
|
|
1 |
""" |
|
|
2 |
Created on November 10, 2019 |
|
|
3 |
functions for writing/reading data to/from disk |
|
|
4 |
|
|
|
5 |
@modified_by: Soroosh Tayebi Arasteh <soroosh.arasteh@fau.de> |
|
|
6 |
""" |
|
|
7 |
import yaml |
|
|
8 |
import numpy as np |
|
|
9 |
import os |
|
|
10 |
import warnings |
|
|
11 |
import shutil |
|
|
12 |
from skimage import io |
|
|
13 |
import pdb |
|
|
14 |
import SimpleITK as sitk |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
def read_config(config_path): |
|
|
20 |
"""Reads config file in yaml format into a dictionary |
|
|
21 |
|
|
|
22 |
Parameters |
|
|
23 |
---------- |
|
|
24 |
config_path: str |
|
|
25 |
Path to the config file in yaml format |
|
|
26 |
|
|
|
27 |
Returns |
|
|
28 |
------- |
|
|
29 |
config dictionary |
|
|
30 |
""" |
|
|
31 |
|
|
|
32 |
with open(config_path, 'rb') as yaml_file: |
|
|
33 |
return yaml.safe_load(yaml_file) |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
def write_config(params, cfg_path, sort_keys=False): |
|
|
37 |
with open(cfg_path, 'w') as f: |
|
|
38 |
yaml.dump(params, f) |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
def create_experiment(experiment_name, global_config_path): |
|
|
42 |
params = read_config(global_config_path) |
|
|
43 |
params['experiment_name'] = experiment_name |
|
|
44 |
create_experiment_folders(params) |
|
|
45 |
cfg_file_name = params['experiment_name'] + '_config.yaml' |
|
|
46 |
cfg_path = os.path.join(os.path.join(params['target_dir'], params['network_output_path']), cfg_file_name) |
|
|
47 |
params['cfg_path'] = cfg_path |
|
|
48 |
write_config(params, cfg_path) |
|
|
49 |
return params |
|
|
50 |
|
|
|
51 |
|
|
|
52 |
def create_experiment_folders(params): |
|
|
53 |
try: |
|
|
54 |
path_keynames = ["network_output_path", "tb_logs_path", "stat_log_path", "output_data_path"] |
|
|
55 |
for key in path_keynames: |
|
|
56 |
params[key] = os.path.join(params['experiment_name'], params[key]) |
|
|
57 |
os.makedirs(os.path.join(params['target_dir'], params[key])) |
|
|
58 |
except: |
|
|
59 |
raise Exception("Experiment already exist. Please try a different experiment name") |
|
|
60 |
|
|
|
61 |
|
|
|
62 |
def open_experiment(experiment_name, global_config_path): |
|
|
63 |
"""Open Existing Experiments |
|
|
64 |
""" |
|
|
65 |
default_params = read_config(global_config_path) |
|
|
66 |
cfg_file_name = experiment_name + '_config.yaml' |
|
|
67 |
cfg_path = os.path.join(os.path.join(default_params['target_dir'], experiment_name, default_params['network_output_path']), cfg_file_name) |
|
|
68 |
params = read_config(cfg_path) |
|
|
69 |
return params |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
def delete_experiment(experiment_name, global_config_path): |
|
|
73 |
"""Delete Existing Experiment folder |
|
|
74 |
""" |
|
|
75 |
default_params = read_config(global_config_path) |
|
|
76 |
cfg_file_name = experiment_name + '_config.yaml' |
|
|
77 |
cfg_path = os.path.join(os.path.join(default_params['target_dir'], experiment_name, default_params['network_output_path']), cfg_file_name) |
|
|
78 |
params = read_config(cfg_path) |
|
|
79 |
shutil.rmtree(os.path.join(params['target_dir'], experiment_name)) |