__init__.py 3.66 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 7 8 9 10 11 12 13 14
import os
# Set environment variables to disable multithreading as users will probably
# want to set the number of cores to the max of their computer.
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"

15
import argparse
16 17 18
import multiprocessing as mp
import sys

19
from .session.configuration import Configuration
20
from .analysis_modules import get_module
21
from .managers.parameters import ParametersManager
22

23
__version__ = "0.10.0"
Médéric Boquien's avatar
Médéric Boquien committed
24

25 26

def init(config):
27 28
    """Create a blank configuration file.
    """
29 30 31 32 33 34
    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):
35 36
    """Generate the full configuration.
    """
37 38
    config.generate_conf()
    print("The configuration file has been updated. Please complete the "
39
          "various module parameters and the data file columns to use in "
40 41 42 43
          "the analysis.")


def check(config):
44 45
    """Check the configuration.
    """
46
    # TODO: Check if all the parameters that don't have default values are
47
    # given for each module.
48
    configuration = config.configuration
49 50

    if configuration:
51 52
        print(f"With this configuration cigale will compute "
              f"{ParametersManager(configuration).size} models.")
53 54 55


def run(config):
56 57
    """Run the analysis.
    """
58
    configuration = config.configuration
59 60 61 62

    if configuration:
        analysis_module = get_module(configuration['analysis_method'])
        analysis_module.process(configuration)
63 64 65


def main():
66
    if sys.version_info[:2] < (3, 6):
67 68
        raise Exception(f"Python {sys.version_info[0]}.{sys.version_info[1]} is"
                        f" unsupported. Please upgrade to Python 3.6 or later.")
69

70 71 72
    # 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
73 74 75 76 77 78
    # 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')
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

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

99 100
    if len(sys.argv) == 1:
        parser.print_usage()
101
    else:
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        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)