__init__.py 3.48 KB
Newer Older
Yannick Roehlly's avatar
Yannick Roehlly committed
1
# -*- coding: utf-8 -*-
2
3
# Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
Yannick Roehlly's avatar
Yannick Roehlly committed
4
# Author: Yannick Roehlly
Yannick Roehlly's avatar
Yannick Roehlly committed
5

6
import argparse
7
8
9
import multiprocessing as mp
import sys

10
from .session.configuration import Configuration
11
from .analysis_modules import get_module
12
from .managers.parameters import ParametersManager
13
14
15


def init(config):
16
17
    """Create a blank configuration file.
    """
18
19
20
21
22
23
    config.create_blank_conf()
    print("The initial configuration file was created. Please complete it "
          "with the data file name and the pcigale modules to use.")


def genconf(config):
24
25
    """Generate the full configuration.
    """
26
27
    config.generate_conf()
    print("The configuration file has been updated. Please complete the "
Yannick Roehlly's avatar
Yannick Roehlly committed
28
          "various module parameters and the data file columns to use in "
29
30
31
32
          "the analysis.")


def check(config):
33
34
    """Check the configuration.
    """
35
    # TODO: Check if all the parameters that don't have default values are
36
    # given for each module.
37
    configuration = config.configuration
38
39
40

    if configuration:
        print("With this configuration cigale will compute {} "
41
              "models.".format(ParametersManager(configuration).size))
42
43
44


def run(config):
45
46
    """Run the analysis.
    """
47
    configuration = config.configuration
48
49
50
51

    if configuration:
        analysis_module = get_module(configuration['analysis_method'])
        analysis_module.process(configuration)
52
53
54


def main():
55
56
57
    if sys.version_info[:2] < (3, 5):
        raise Exception("Python {}.{} is unsupported. Please upgrade to "
                        "Python 3.5 or later.".format(*sys.version_info[:2]))
58
    if sys.version_info[:2] < (3, 6):
59
60
61
        print("Python {}.{} detected. For better performance we recommend "
              "Python 3.6 or later.".format(*sys.version_info[:2]))

62
63
64
    # We set the sub processes start method to spawn because it solves
    # deadlocks when a library cannot handle being used on two sides of a
    # forked process. This happens on modern Macs with the Accelerate library
65
66
67
68
69
70
    # for instance. On Linux we should be pretty safe with a fork, which allows
    # to start processes much more rapidly.
    if sys.platform.startswith('linux'):
        mp.set_start_method('fork')
    else:
        mp.set_start_method('spawn')
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    parser = argparse.ArgumentParser()

    parser.add_argument('-c', '--conf-file', dest='config_file',
                        help="Alternative configuration file to use.")

    subparsers = parser.add_subparsers(help="List of commands")

    init_parser = subparsers.add_parser('init', help=init.__doc__)
    init_parser.set_defaults(parser='init')

    genconf_parser = subparsers.add_parser('genconf', help=genconf.__doc__)
    genconf_parser.set_defaults(parser='genconf')

    check_parser = subparsers.add_parser('check', help=check.__doc__)
    check_parser.set_defaults(parser='check')

    run_parser = subparsers.add_parser('run', help=run.__doc__)
    run_parser.set_defaults(parser='run')

91
92
    if len(sys.argv) == 1:
        parser.print_usage()
93
    else:
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        args = parser.parse_args()

        if args.config_file:
            config = Configuration(args.config_file)
        else:
            config = Configuration()

        if args.parser == 'init':
            init(config)
        elif args.parser == 'genconf':
            genconf(config)
        elif args.parser == 'check':
            check(config)
        elif args.parser == 'run':
            run(config)