Esempio n. 1
0
def reset_eskapade(skip_config=False):
    """Reset Eskapade objects.

    :param bool skip_config: skip reset of configuration object
    """
    settings = process_manager.service(ConfigObject)
    process_manager.reset()
    if skip_config:
        process_manager.service(settings)
Esempio n. 2
0
def io_dir(io_type, io_conf=None):
    """Construct directory path.

    :param str io_type: type of result to store, e.g. data, macro, results.
    :param io_conf: IO configuration object
    :return: directory path
    :rtype: str
    """
    if not io_conf:
        io_conf = process_manager.service(ConfigObject).io_conf()
    # check inputs
    if io_type not in IO_LOCS:
        logger.fatal('Unknown IO type: "{type!s}".', type=io_type)
        raise RuntimeError('IO directory found for specified IO type.')
    if IO_LOCS[io_type] not in io_conf:
        logger.fatal('Directory for io_type ({type}->{path}) not in io_conf.', type=io_type, path=IO_LOCS[io_type])
        raise RuntimeError('io_dir: directory for specified IO type not found in specified IO configuration.')

    # construct directory path
    base_dir = io_conf[IO_LOCS[io_type]]
    sub_dir = IO_SUB_DIRS[io_type].format(ana_name=repl_whites(io_conf['analysis_name']),
                                          ana_version=repl_whites(str(io_conf['analysis_version'])))
    dir_path = base_dir + ('/' if base_dir[-1] != '/' else '') + sub_dir

    # create and return directory path
    create_dir(dir_path)
    return dir_path
Esempio n. 3
0
    def test_service(self, mock_create):
        pm = process_manager

        # register service by specifying type
        ps = ProcessServiceMock()
        mock_create.return_value = ps
        ps_ = process_manager.service(ProcessServiceMock)
        self.assertIn(ProcessServiceMock, pm._services)
        self.assertIs(ps_, ps)
        self.assertIs(pm._services[ProcessServiceMock], ps)
        pm.reset()

        # register service by specifying instance
        ps = ProcessServiceMock()
        ps_ = process_manager.service(ps)
        self.assertIn(ProcessServiceMock, pm._services)
        self.assertIs(ps_, ps)
        self.assertIs(pm._services[ProcessServiceMock], ps)
        pm.reset()

        # register service with wrong type
        with self.assertRaises(TypeError):
            process_manager.service(object)
        pm.reset()
Esempio n. 4
0
def record_file_number(file_name_base, file_name_ext, io_conf=None):
    """Get next prediction-record file number.

    :param str file_name_base: base file name
    :param str file_name_ext: file name extension
    :param io_conf: I/O configuration object
    :return: next prediction-record file number
    :rtype: int
    """
    if not io_conf:
        io_conf = process_manager.service(ConfigObject).io_conf()
    file_name_base = repl_whites(file_name_base)
    file_name_ext = repl_whites(file_name_ext)
    records_dir = io_dir('records', io_conf)
    max_num = -1
    regex = re.compile('.*/{}_(\d*?).{}'.format(file_name_base, file_name_ext))
    for file_path in glob.glob('{}/{}_*.{}'.format(records_dir, file_name_base, file_name_ext)):
        digit = regex.search(file_path)
        if digit:
            max_num = max(max_num, int(digit.group(1)))

    return max_num + 1
Esempio n. 5
0
def io_path(io_type, sub_path, io_conf=None):
    """Construct directory path with sub path.

    :param str io_type: type of result to store, e.g. data, macro, results.
    :param str sub_path: sub path to be included in io path
    :param io_conf: IO configuration object
    :return: full path to directory
    :rtype: str
    """
    if not io_conf:
        io_conf = process_manager.service(ConfigObject).io_conf()
    # check inputs
    if not isinstance(sub_path, str):
        logger.fatal('Specified sub path/file name must be a string, but has type "{type!s}"',
                     type=type(sub_path).__name__)
        raise TypeError('The sub path/file name in the io_path function must be a string')
    sub_path = repl_whites(sub_path).strip('/')

    # construct path
    full_path = io_dir(io_type, io_conf) + '/' + sub_path
    if os.path.dirname(full_path):
        create_dir(os.path.dirname(full_path))

    return full_path
Esempio n. 6
0
def eskapade_run(settings=None):
    """Run Eskapade.

    This function is called in the script eskapade_run when run
    from the cmd line.  The working principle of Eskapade is to run chains of
    custom code chunks (so-called links).

    Each chain should have a specific purpose, for example pre-processing
    incoming data, booking and/or training predictive algorithms, validating
    these predictive algorithms, evaluating the algorithms.

    By using this principle, links can be easily reused in future projects.

    :param ConfigObject settings: analysis settings
    :return: status of the execution
    :rtype: StatusCode
    """
    # get config object from process manager
    if settings:
        # use supplied settings as config service in process manager
        process_manager.remove_service(ConfigObject, silent=True)
        process_manager.service(settings)
    settings = process_manager.service(ConfigObject)

    def message(msg):
        width = 80
        fence = '*'
        logger.info(fence * width)
        logger.info('{begin}{msg:>{fill}}{end:>{e_fill}}'.format(
            begin=fence,
            msg=msg,
            fill=(width + len(msg)) // 2,
            end=fence,
            e_fill=(width - len(msg) - 1) // 2))
        logger.info(fence * width)

    message('Welcome to Eskapade!')

    # check for batch mode
    if settings.get('batchMode'):
        # set non-interactive Matplotlib backend before plotting tools are imported
        eskapade.utils.set_matplotlib_backend(batch=True, silent=False)

    # execute configuration macro, this sets up the order of the chains and links.
    if not settings['macro']:
        raise RuntimeError('macro is not set')
    process_manager.execute_macro(settings['macro'])

    if 'ROOT.RooFit' in sys.modules:
        # initialize logging for RooFit
        from eskapade.root_analysis.roofit_utils import set_rf_log_level
        set_rf_log_level(settings['logLevel'])

    # check analysis name
    if not settings['analysisName']:
        raise RuntimeError('analysis name is not set')

    # standard execution from now on
    status = process_manager.run()

    message('Leaving Eskapade. Bye!')

    return status