harmonization.scripts

 1import os
 2import sys
 3
 4import numpy as np
 5
 6from harmonization.config import read_config, write_config, get_filenames
 7from harmonization.dictionary import get_global_D, harmonize_my_data
 8
 9
10def main_get_global_D():
11    if len(sys.argv) == 1:
12        usage = 'Everything is set in config.yaml, that is the only input.\nTo create a default config, pass "write /path/to/file/config.yaml" to create a template example'
13        print(usage)
14        sys.exit(0)
15
16    config = sys.argv[1]
17
18    if len(sys.argv) == 3:
19        path = sys.argv[2]
20        if config == 'write':
21            write_config(path)
22            print(f'Default config written at {os.path.abspath(path)}')
23            sys.exit(0)
24        else:
25            error = f'You need to pass the keyword "write" as an argument followed by a filename to write the default config, but you passed {sys.argv[1:]}'
26            raise ValueError(error)
27
28    if len(sys.argv) != 2:
29        error = f'The only accepted argument is a config.yaml file, but you passed {sys.argv[1:]}'
30        raise ValueError(error)
31
32    kwargs = read_config(config)
33
34    # we need to do a few special things for some args
35    path = kwargs.pop('path')
36    use_glob = kwargs.pop('glob')
37    outfilename = kwargs.pop('outfilename')
38    kwargs['positivity'] = kwargs.pop('positivity_D')
39    kwargs.pop('positivity_recon')
40
41    datasets = get_filenames(path, use_glob, kwargs)
42
43    D = get_global_D(datasets, outfilename, **kwargs)
44    np.save(outfilename, D)
45
46
47def main_harmonize_my_data():
48    if len(sys.argv) == 1:
49        usage = 'Everything is set in "config.yaml" and is the only accepted input.'
50        print(usage)
51        sys.exit(0)
52
53    if len(sys.argv) != 2:
54        error = f'The only accepted argument is a "config.yaml" file, but you passed {sys.argv[1:]}'
55        raise ValueError(error)
56
57    config = sys.argv[1]
58    kwargs = read_config(config)
59
60    path = kwargs.pop('path')
61    use_glob = kwargs.pop('glob')
62    kwargs.pop('positivity_D')
63    kwargs['positivity'] = kwargs.pop('positivity_recon')
64
65    datasets = get_filenames(path, use_glob, kwargs)
66
67    # Actually harmonize each dataset by itself
68    for dataset in datasets:
69        harmonize_my_data(dataset, kwargs)
def main_get_global_D():
11def main_get_global_D():
12    if len(sys.argv) == 1:
13        usage = 'Everything is set in config.yaml, that is the only input.\nTo create a default config, pass "write /path/to/file/config.yaml" to create a template example'
14        print(usage)
15        sys.exit(0)
16
17    config = sys.argv[1]
18
19    if len(sys.argv) == 3:
20        path = sys.argv[2]
21        if config == 'write':
22            write_config(path)
23            print(f'Default config written at {os.path.abspath(path)}')
24            sys.exit(0)
25        else:
26            error = f'You need to pass the keyword "write" as an argument followed by a filename to write the default config, but you passed {sys.argv[1:]}'
27            raise ValueError(error)
28
29    if len(sys.argv) != 2:
30        error = f'The only accepted argument is a config.yaml file, but you passed {sys.argv[1:]}'
31        raise ValueError(error)
32
33    kwargs = read_config(config)
34
35    # we need to do a few special things for some args
36    path = kwargs.pop('path')
37    use_glob = kwargs.pop('glob')
38    outfilename = kwargs.pop('outfilename')
39    kwargs['positivity'] = kwargs.pop('positivity_D')
40    kwargs.pop('positivity_recon')
41
42    datasets = get_filenames(path, use_glob, kwargs)
43
44    D = get_global_D(datasets, outfilename, **kwargs)
45    np.save(outfilename, D)
def main_harmonize_my_data():
48def main_harmonize_my_data():
49    if len(sys.argv) == 1:
50        usage = 'Everything is set in "config.yaml" and is the only accepted input.'
51        print(usage)
52        sys.exit(0)
53
54    if len(sys.argv) != 2:
55        error = f'The only accepted argument is a "config.yaml" file, but you passed {sys.argv[1:]}'
56        raise ValueError(error)
57
58    config = sys.argv[1]
59    kwargs = read_config(config)
60
61    path = kwargs.pop('path')
62    use_glob = kwargs.pop('glob')
63    kwargs.pop('positivity_D')
64    kwargs['positivity'] = kwargs.pop('positivity_recon')
65
66    datasets = get_filenames(path, use_glob, kwargs)
67
68    # Actually harmonize each dataset by itself
69    for dataset in datasets:
70        harmonize_my_data(dataset, kwargs)