/
main.py
86 lines (66 loc) · 2.92 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import diameter_synthesis.utils as utils
from diameter_synthesis.build_diameters import build_diameters
from diameter_synthesis.build_models import build_models
import os
import glob
import shutil
import click
import json
from tqdm import tqdm
import numpy as np
# diable warnings
import morphio
morphio.set_maximum_warnings(0)
# hack to convert numpy types to python types for json compatibility
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
@click.group()
def cli():
pass
@cli.command('run_models')
@click.argument('config_file', type=click.Path(exists=True))
def run_models(config_file):
""" Run the model extraction from config file"""
# load the config file
with open(config_file, 'r') as f:
config = json.load(f)
print('Loading morphologies...')
# load all the morphologies
morphologies_tmp = utils.load_morphologies(
config['morph_path'], n_morphs_max=config['n_morphs_max'], mtypes_sort=config['mtypes_sort'], n_mtypes_max=config['n_mtypes_max'])
#remove empty mtypes if missing data (not clean...)
morphologies = morphologies_tmp.copy()
for m in morphologies_tmp:
if len(morphologies_tmp[m]) == 0:
del morphologies[m]
print('Extracting models parameters...')
# compute the model
models_params = build_models(config['models'], morphologies, config['neurite_types'],
config['extra_params'], fig_folder=config['fig_folder'], ext=config['ext'], plot=config['plot'])
# save the models parameters in a json file
with open(config['models_params_file'], 'w') as json_file:
json.dump(models_params, json_file, sort_keys=True, indent=4, cls=NpEncoder)
@cli.command('run_diameters')
@click.argument('config_file', type=click.Path(exists=True))
def run_diameters(config_file):
""" Build new diameters from config file and diameter model"""
# load the config file
with open(config_file, 'r') as f:
config = json.load(f)
print('Loading morphologies...')
# load all the morphologies
morphologies_dict = utils.create_morphologies_dict(
config['morph_path'], n_morphs_max=config['n_morphs_max'], mtypes_sort=config['mtypes_sort'], n_mtypes_max=config['n_mtypes_max'])
print('Generate diameters...')
with open(config['models_params_file'], 'r') as f:
models_params = json.load(f)
# generate diameters
models_params = build_diameters(config['models'], models_params, morphologies_dict, config['neurite_types'], config['new_morph_path'], config['extra_params'], config['morph_path'], plot=config['plot'], n_cpu=config['n_cpu'], n_samples=config['n_samples'], ext = config['ext'])