deformetrica.py 4.47 KB
Newer Older
1 2 3
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

4
import os
5
import argparse
6

Benoit Martin's avatar
Benoit Martin committed
7 8 9 10
from in_out.xml_parameters import XmlParameters
from launch.compute_parallel_transport import compute_parallel_transport
from launch.estimate_bayesian_atlas import estimate_bayesian_atlas
from launch.estimate_deterministic_atlas import estimate_deterministic_atlas
11
from launch.estimate_rigid_atlas import estimate_rigid_atlas
Benoit Martin's avatar
Benoit Martin committed
12 13 14 15 16
from launch.estimate_geodesic_regression import estimate_geodesic_regression
from launch.estimate_longitudinal_atlas import estimate_longitudinal_atlas
from launch.estimate_longitudinal_metric_model import estimate_longitudinal_metric_model
from launch.estimate_longitudinal_metric_registration import estimate_longitudinal_metric_registration
from launch.estimate_longitudinal_registration import estimate_longitudinal_registration
17
from launch.compute_shooting import run_shooting
Benoit Martin's avatar
Benoit Martin committed
18
from support.utilities.general_settings import Settings
19 20


21
def main():
Benoit Martin's avatar
Benoit Martin committed
22 23
    import logging
    logger = logging.getLogger(__name__)
Benoit Martin's avatar
Benoit Martin committed
24
    logger_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
Benoit Martin's avatar
Benoit Martin committed
25

26 27 28 29 30
    # parse arguments
    parser = argparse.ArgumentParser(description='Deformetrica')
    parser.add_argument('model', type=str, help='model xml file')
    parser.add_argument('dataset', type=str, help='data-set xml file')
    parser.add_argument('optimization', type=str, help='optimization parameters xml file')
31

32 33
    # optional arguments
    parser.add_argument('-o', '--output', type=str, help='output folder')
34 35 36 37 38 39
    # logging levels: https://docs.python.org/2/library/logging.html#logging-levels
    parser.add_argument('--verbosity', '-v',
                        type=str,
                        default='WARNING',
                        choices=['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
                        help='set output verbosity')
40

41
    args = parser.parse_args()
42

43 44 45
    # set logging level
    try:
        log_level = logging.getLevelName(args.verbosity)
Benoit Martin's avatar
Benoit Martin committed
46
        logging.basicConfig(level=log_level, format=logger_format)
47 48 49 50 51
    except ValueError:
        logger.warning('Logging level was not recognized. Using INFO.')
        log_level = logging.INFO

    logger.debug('Using verbosity level: ' + args.verbosity)
Benoit Martin's avatar
Benoit Martin committed
52
    logging.basicConfig(level=log_level, format=logger_format)
53

54 55 56 57 58 59 60 61 62 63 64 65 66
    """
    Read xml files, set general settings, and call the adapted function.
    """
    try:
        if args.output is None:
            logger.info('Creating the output directory: ' + Settings().output_dir)
            os.makedirs(Settings().output_dir)
        else:
            logger.info('Setting output directory to: ' + args.output)
            Settings().set_output_dir(args.output)
    except FileExistsError:
        pass

LOUIS Maxime's avatar
LOUIS Maxime committed
67 68 69
    file_handler = logging.FileHandler(os.path.join(Settings().output_dir, 'log.txt'), mode='w')
    logger.addHandler(file_handler)

70
    logger.info('[ read_all_xmls function ]')
71
    xml_parameters = XmlParameters()
72
    xml_parameters.read_all_xmls(args.model, args.dataset, args.optimization)
73

74 75
    if xml_parameters.model_type == 'DeterministicAtlas'.lower() \
            or xml_parameters.model_type == 'Registration'.lower():
76
        estimate_deterministic_atlas(xml_parameters)
77

78 79
    elif xml_parameters.model_type == 'BayesianAtlas'.lower():
        estimate_bayesian_atlas(xml_parameters)
80

81 82 83
    elif xml_parameters.model_type == 'RigidAtlas'.lower():
        estimate_rigid_atlas(xml_parameters)

84 85
    elif xml_parameters.model_type == 'Regression'.lower():
        estimate_geodesic_regression(xml_parameters)
86

87
    elif xml_parameters.model_type == 'LongitudinalAtlas'.lower():
88 89
        estimate_longitudinal_atlas(xml_parameters)

90 91 92
    elif xml_parameters.model_type == 'LongitudinalRegistration'.lower():
        estimate_longitudinal_registration(xml_parameters)

93 94
    elif xml_parameters.model_type == 'Shooting'.lower():
        run_shooting(xml_parameters)
95

96 97
    elif xml_parameters.model_type == 'ParallelTransport'.lower():
        compute_parallel_transport(xml_parameters)
98

99 100 101
    elif xml_parameters.model_type == 'LongitudinalMetricLearning'.lower():
        estimate_longitudinal_metric_model(xml_parameters)

102 103 104
    elif xml_parameters.model_type == 'LongitudinalMetricRegistration'.lower():
        estimate_longitudinal_metric_registration(xml_parameters)

105 106 107
    else:
        raise RuntimeError('Unrecognized model-type: "' + xml_parameters.model_type
                           + '". Check the corresponding field in the model.xml input file.')
108 109 110 111 112


if __name__ == "__main__":
    # execute only if run as a script
    main()