__init__.py 3.32 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
__version__ = "0.10.0"
Médéric Boquien's avatar
Médéric Boquien committed
15

16 17

def init(config):
18 19
    """Create a blank configuration file.
    """
20 21 22 23 24 25
    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):
26 27
    """Generate the full configuration.
    """
28 29
    config.generate_conf()
    print("The configuration file has been updated. Please complete the "
Yannick Roehlly's avatar
Yannick Roehlly committed
30
          "various module parameters and the data file columns to use in "
31 32 33 34
          "the analysis.")


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

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


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

    if configuration:
        analysis_module = get_module(configuration['analysis_method'])
        analysis_module.process(configuration)
54 55 56


def main():
57
    if sys.version_info[:2] < (3, 6):
58 59
        raise Exception("Python {}.{} is unsupported. Please upgrade to "
                        "Python 3.6 or later.".format(*sys.version_info[:2]))
60

61 62 63
    # 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
64 65 66 67 68 69
    # 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')
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    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')

90 91
    if len(sys.argv) == 1:
        parser.print_usage()
92
    else:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        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)