Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from sklearn.model_selection._search import (
check_scoring, indexable,
Parallel, delayed, defaultdict, rankdata
)
from sklearn.model_selection._validation import (
_score, _num_samples, _index_param_value, _safe_split,
logger)
from ... import logging
from builtins import object, zip
try:
from sklearn.utils.fixes import MaskedArray
except ImportError:
from numpy.ma import MaskedArray
LOG = logging.getLogger('mriqc.classifier')
class RobustGridSearchCV(GridSearchCV):
def _fit(self, X, y, groups, parameter_iterable):
"""Actual fitting, performing the search over parameters."""
cv = check_cv(self.cv, y, classifier=is_classifier(self.estimator))
self.scorer_ = check_scoring(self.estimator, scoring=self.scoring)
X, y, groups = indexable(X, y, groups)
n_splits = cv.get_n_splits(X, y, groups)
if self.verbose > 0 and isinstance(parameter_iterable, Sized):
n_candidates = len(parameter_iterable)
LOG.log(19, "Fitting %d folds for each of %d candidates, totalling"
" %d fits", n_splits, n_candidates, n_candidates * n_splits)
pre_dispatch = self.pre_dispatch
g_input.add_argument('--log-level', action='store', default='INFO',
choices=['CRITICAL', 'ERROR', 'WARN', 'INFO', 'DEBUG'])
g_input.add_argument('-o', '--output-file', action='store', default='cv_inner_loop.csv',
help='the output table with cross validated scores')
g_input.add_argument('-O', '--output-outer-cv', action='store', default='cv_outer_loop.csv',
help='the output table with cross validated scores')
g_input.add_argument('--njobs', action='store', default=-1, type=int,
help='number of jobs')
g_input.add_argument('--task-id', action='store')
opts = parser.parse_args()
logger = logging.getLogger()
if opts.log_file is not None:
fhl = logging.FileHandler(opts.log_file)
fhl.setFormatter(fmt=logging.Formatter(LOG_FORMAT))
logger.addHandler(fhl)
logger.setLevel(opts.log_level)
parameters = None
if opts.parameters is not None:
with open(opts.parameters) as paramfile:
parameters = yaml.load(paramfile)
cvhelper = NestedCVHelper(opts.training_data, opts.training_labels,
n_jobs=opts.njobs, param=parameters,
task_id=opts.task_id)
cvhelper.cv_inner = read_cv(opts.cv_inner)
import pandas as pd
from .sklearn.cv_nested import nested_fit_and_score, ModelAndGridSearchCV
from .sklearn._split import RobustLeavePGroupsOut as LeavePGroupsOut
from sklearn.base import is_classifier, clone
from sklearn.metrics.scorer import check_scoring
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection._split import check_cv
from .helper import CVHelperBase
from .. import logging
from builtins import str
LOG = logging.getLogger('mriqc.classifier')
LOG.setLevel(logging.INFO)
DEFAULT_TEST_PARAMETERS = {
'svc_linear': [{'C': [0.1, 1]}],
}
EXCLUDE_COLUMNS = [
'size_x', 'size_y', 'size_z',
'spacing_x', 'spacing_y', 'spacing_z',
'qi_1', 'qi_2',
'tpm_overlap_csf', 'tpm_overlap_gm', 'tpm_overlap_wm',
]
FEATURE_NAMES = [
'cjv', 'cnr', 'efc', 'fber',
'fwhm_avg', 'fwhm_x', 'fwhm_y', 'fwhm_z',
def warn_redirect(message, category, filename, lineno, file=None, line=None):
from mriqc import logging
LOG = logging.getLogger('mriqc.warnings')
if category not in cached_warnings:
LOG.debug('captured warning (%s): %s', category, message)
cached_warnings.append(category)
from .. import logging
from builtins import object, zip
NUMPY_MA = False
try:
NUMPY_MA = np_version < (1, 12, 0)
except TypeError:
pass
if NUMPY_MA:
from sklearn.utils.fixes import MaskedArray
else:
from numpy.ma import MaskedArray
LOG = logging.getLogger('mriqc.classifier')
def _len(indict):
product = partial(reduce, operator.mul)
return sum(product(len(v) for v in p.values()) if p else 1
for p in indict)
class ModelParameterGrid(object):
"""
Grid of models and parameters with a discrete number of values for each.
Can be used to iterate over parameter value combinations with the
Python built-in function iter.
Read more in the :ref:`User Guide `.
Parameters
----------
param_grid : dict of string to sequence, or sequence of such
The parameter grid to explore, as a dictionary mapping estimator
g_input.add_argument('-o', '--output-file', action='store', default='cv_inner_loop.csv',
help='the output table with cross validated scores')
g_input.add_argument('-O', '--output-outer-cv', action='store', default='cv_outer_loop.csv',
help='the output table with cross validated scores')
g_input.add_argument('--njobs', action='store', default=-1, type=int,
help='number of jobs')
g_input.add_argument('--task-id', action='store')
opts = parser.parse_args()
logger = logging.getLogger()
if opts.log_file is not None:
fhl = logging.FileHandler(opts.log_file)
fhl.setFormatter(fmt=logging.Formatter(LOG_FORMAT))
logger.addHandler(fhl)
logger.setLevel(opts.log_level)
parameters = None
if opts.parameters is not None:
with open(opts.parameters) as paramfile:
parameters = yaml.load(paramfile)
cvhelper = NestedCVHelper(opts.training_data, opts.training_labels,
n_jobs=opts.njobs, param=parameters,
task_id=opts.task_id)
cvhelper.cv_inner = read_cv(opts.cv_inner)
cvhelper.cv_outer = read_cv(opts.cv_outer)
# Run inner loop before setting held-out data, for hygene
g_input.add_argument('-o', '--output-file', action='store', default='cv_inner_loop.csv',
help='the output table with cross validated scores')
g_input.add_argument('-O', '--output-outer-cv', action='store', default='cv_outer_loop.csv',
help='the output table with cross validated scores')
g_input.add_argument('--njobs', action='store', default=-1, type=int,
help='number of jobs')
g_input.add_argument('--task-id', action='store')
opts = parser.parse_args()
logger = logging.getLogger()
if opts.log_file is not None:
fhl = logging.FileHandler(opts.log_file)
fhl.setFormatter(fmt=logging.Formatter(LOG_FORMAT))
logger.addHandler(fhl)
logger.setLevel(opts.log_level)
parameters = None
if opts.parameters is not None:
with open(opts.parameters) as paramfile:
parameters = yaml.load(paramfile)
cvhelper = NestedCVHelper(opts.training_data, opts.training_labels,
n_jobs=opts.njobs, param=parameters,
task_id=opts.task_id)
cvhelper.cv_inner = read_cv(opts.cv_inner)
cvhelper.cv_outer = read_cv(opts.cv_outer)
from .sklearn.cv_nested import nested_fit_and_score, ModelAndGridSearchCV
from .sklearn._split import RobustLeavePGroupsOut as LeavePGroupsOut
from sklearn.base import is_classifier, clone
from sklearn.metrics.scorer import check_scoring
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection._split import check_cv
from .helper import CVHelperBase
from .. import logging
from builtins import str
LOG = logging.getLogger('mriqc.classifier')
LOG.setLevel(logging.INFO)
DEFAULT_TEST_PARAMETERS = {
'svc_linear': [{'C': [0.1, 1]}],
}
EXCLUDE_COLUMNS = [
'size_x', 'size_y', 'size_z',
'spacing_x', 'spacing_y', 'spacing_z',
'qi_1', 'qi_2',
'tpm_overlap_csf', 'tpm_overlap_gm', 'tpm_overlap_wm',
]
FEATURE_NAMES = [
'cjv', 'cnr', 'efc', 'fber',
'fwhm_avg', 'fwhm_x', 'fwhm_y', 'fwhm_z',
'icvs_csf', 'icvs_gm', 'icvs_wm',