def load_config(cfg_file): cfg_file = qc.forward_slashify(cfg_file) if not (os.path.exists(cfg_file) and os.path.isfile(cfg_file)): logger.error('%s cannot be loaded.' % os.path.realpath(cfg_file)) raise IOError return imp.load_source(cfg_file, cfg_file)
def load_cfg(cfg_file): critical_vars = { 'COMMON': [ 'tdef', 'TRIGGER_DEF', 'EPOCH', 'DATADIR', 'PSD', 'CHANNEL_PICKS', 'SP_FILTER', 'TP_FILTER', 'NOTCH_FILTER', 'FEATURES', 'CLASSIFIER', 'CV_PERFORM' ], 'RF': ['trees', 'max_depth', 'seed'], 'GB': ['trees', 'learning_rate', 'max_depth', 'seed'] } # optional variables with default values optional_vars = { 'LOAD_PSD': False, 'MULTIPLIER': 1, 'EXPORT_GOOD_FEATURES': False, 'FEAT_TOPN': 20, 'EXPORT_CLS': False, 'USE_LOG': False, 'USE_CVA': False, 'REF_CH': None, 'N_JOBS': None, 'EXCLUDES': None, 'CV_IGNORE_THRES': None, 'CV_DECISION_THRES': None, 'BALANCE_SAMPLES': False } cfg_file = qc.forward_slashify(cfg_file) cfg = imp.load_source(cfg_file, cfg_file) for v in critical_vars['COMMON']: if not hasattr(cfg, v): raise RuntimeError('%s not defined in config.' % v) for key in optional_vars: if not hasattr(cfg, key): setattr(cfg, key, optional_vars[key]) qc.print_c( 'load_cfg(): Setting undefined parameter %s=%s' % (key, getattr(cfg, key)), 'Y') # classifier parameters check if cfg.CLASSIFIER == 'RF': if not hasattr(cfg, 'RF'): raise RuntimeError('"RF" not defined in config.') for v in critical_vars['RF']: if v not in cfg.RF: raise RuntimeError('%s not defined in config.' % v) elif cfg.CLASSIFIER == 'GB' or cfg.CLASSIFIER == 'XGB': if not hasattr(cfg, 'GB'): raise RuntimeError('"GB" not defined in config.') for v in critical_vars['GB']: if v not in cfg.GB: raise RuntimeError('%s not defined in config.' % v) elif cfg.CLASSIFIER == 'rLDA' and not hasattr(cfg, 'RLDA_REGULARIZE_COEFF'): raise RuntimeError('"RLDA_REGULARIZE_COEFF" not defined in config.') if cfg.CV_PERFORM is not None: if not hasattr(cfg, 'CV_RANDOM_SEED'): cfg.CV_RANDOM_SEED = None qc.print_c( 'load_cfg(): Setting undefined parameter CV_RANDOM_SEED=%s' % (cfg.CV_RANDOM_SEED), 'Y') if not hasattr(cfg, 'CV_FOLDS'): raise RuntimeError('"CV_FOLDS" not defined in config.') if cfg.CV_PERFORM == 'StratifiedShuffleSplit' and not hasattr( cfg, 'CV_TEST_RATIO'): raise RuntimeError('"CV_TEST_RATIO" not defined in config.') if cfg.N_JOBS is None: cfg.N_JOBS = mp.cpu_count() return cfg