From d6e9e06edc555fdf73316b7bd222067fc8399828 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 4 Dec 2019 12:30:19 +0000 Subject: Remove 'with nogil' everywhere Don't think it's needed --- gensvm/cython_wrapper/wrapper.pyx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'gensvm') diff --git a/gensvm/cython_wrapper/wrapper.pyx b/gensvm/cython_wrapper/wrapper.pyx index 009e70b..3d7b87d 100644 --- a/gensvm/cython_wrapper/wrapper.pyx +++ b/gensvm/cython_wrapper/wrapper.pyx @@ -88,8 +88,7 @@ def train_wrap( raise ValueError(error_repl) # Do the actual training - with nogil: - gensvm_train(model, data, seed_model) + gensvm_train(model, data, seed_model) # update the number of variables (this may have changed due to kernel) n_var = get_m(model) @@ -137,9 +136,8 @@ def predict_wrap( predictions = np.empty((n_test_obs, ), dtype=np.int) # do the prediction - with nogil: - gensvm_predict(X.data, V.data, n_test_obs, n_var, n_class, - predictions.data) + gensvm_predict(X.data, V.data, n_test_obs, n_var, n_class, + predictions.data) return predictions @@ -174,10 +172,9 @@ def predict_kernels_wrap( cdef np.ndarray[np.int_t, ndim=1, mode='c'] predictions predictions = np.empty((n_obs_test, ), dtype=np.int) - with nogil: - gensvm_predict_kernels(Xtest.data, Xtrain.data, V.data, V_rows, - V_cols, n_obs_train, n_obs_test, n_var, n_class, kernel_idx, - gamma, coef, degree, kernel_eigen_cutoff, predictions.data) + gensvm_predict_kernels(Xtest.data, Xtrain.data, V.data, V_rows, V_cols, + n_obs_train, n_obs_test, n_var, n_class, kernel_idx, gamma, coef, + degree, kernel_eigen_cutoff, predictions.data) return predictions @@ -243,9 +240,7 @@ def grid_wrap( set_queue(queue, n_tasks, tasks) - with nogil: - gensvm_train_q_helper(queue, cv_idx.data, store_predictions, - verbosity) + gensvm_train_q_helper(queue, cv_idx.data, store_predictions, verbosity) cdef np.ndarray[np.int_t, ndim=1, mode='c'] pred cdef np.ndarray[np.double_t, ndim=1, mode='c'] dur -- cgit v1.2.3 From 8a3846d155f90acf850a0de2f02202a6064e0e81 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 4 Dec 2019 12:30:51 +0000 Subject: Use np.int_, since we expect longs --- gensvm/cython_wrapper/wrapper.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gensvm') diff --git a/gensvm/cython_wrapper/wrapper.pyx b/gensvm/cython_wrapper/wrapper.pyx index 3d7b87d..cdc54f2 100644 --- a/gensvm/cython_wrapper/wrapper.pyx +++ b/gensvm/cython_wrapper/wrapper.pyx @@ -133,7 +133,7 @@ def predict_wrap( # output vector cdef np.ndarray[np.int_t, ndim=1, mode='c'] predictions - predictions = np.empty((n_test_obs, ), dtype=np.int) + predictions = np.empty((n_test_obs, ), dtype=np.int_) # do the prediction gensvm_predict(X.data, V.data, n_test_obs, n_var, n_class, @@ -259,7 +259,7 @@ def grid_wrap( results['params'].append(candidate_params[ID]) results['scores'].append(get_task_performance(tasks[ID])) if store_predictions: - pred = np.zeros((n_obs, ), dtype=np.int) + pred = np.zeros((n_obs, ), dtype=np.int_) copy_task_predictions(tasks[ID], pred.data, n_obs) results['predictions'].append(pred.copy()) dur = np.zeros((n_folds, ), dtype=np.double) -- cgit v1.2.3 From c4e3edeaaebe68cd47eb9561f0880a44455e3023 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 4 Dec 2019 12:31:07 +0000 Subject: No need to copy, we own the memory --- gensvm/cython_wrapper/wrapper.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gensvm') diff --git a/gensvm/cython_wrapper/wrapper.pyx b/gensvm/cython_wrapper/wrapper.pyx index cdc54f2..3a85e92 100644 --- a/gensvm/cython_wrapper/wrapper.pyx +++ b/gensvm/cython_wrapper/wrapper.pyx @@ -264,7 +264,7 @@ def grid_wrap( results['predictions'].append(pred.copy()) dur = np.zeros((n_folds, ), dtype=np.double) copy_task_durations(tasks[ID], dur.data, n_folds) - results['durations'].append(dur.copy()) + results['durations'].append(dur) gensvm_free_queue(queue) free_data(data) -- cgit v1.2.3 From eec0096153472ad016a0a2d6e1a956c607a4a24f Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 4 Dec 2019 12:45:57 +0000 Subject: Remove is_multimetric flag Needed for scikit-learn 0.22.0 --- gensvm/gridsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gensvm') diff --git a/gensvm/gridsearch.py b/gensvm/gridsearch.py index b27a347..22125a4 100644 --- a/gensvm/gridsearch.py +++ b/gensvm/gridsearch.py @@ -116,7 +116,7 @@ def _wrap_score(y_pred, y_true, scorers, is_multimetric): results["score"] = np.nan else: estimator = _MockEstimator(y_pred) - results = _score(estimator, None, y_true, scorers, is_multimetric) + results = _score(estimator, None, y_true, scorers) score_time = time.time() - start_time return results, score_time -- cgit v1.2.3 From 0361b805330cbf72263af025a2267b196456f715 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 4 Dec 2019 13:08:42 +0000 Subject: Deal with changing behavior of check_is_fitted This is going to change in version 0.23, so we might as well inline it now. --- gensvm/core.py | 2 +- gensvm/sklearn_util.py | 7 ++++++- gensvm/util.py | 13 +++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) (limited to 'gensvm') diff --git a/gensvm/core.py b/gensvm/core.py index bfd5d9a..45d59ad 100644 --- a/gensvm/core.py +++ b/gensvm/core.py @@ -16,9 +16,9 @@ from sklearn.exceptions import ConvergenceWarning, FitFailedWarning from sklearn.preprocessing import LabelEncoder from sklearn.utils import check_array, check_X_y, check_random_state from sklearn.utils.multiclass import type_of_target -from sklearn.utils.validation import check_is_fitted from .cython_wrapper import wrapper +from .util import check_is_fitted def _fit_gensvm( diff --git a/gensvm/sklearn_util.py b/gensvm/sklearn_util.py index 182f257..e23921b 100644 --- a/gensvm/sklearn_util.py +++ b/gensvm/sklearn_util.py @@ -207,7 +207,12 @@ def _skl_check_is_fitted(estimator, method_name, refit): "attribute" % (type(estimator).__name__, method_name) ) else: - check_is_fitted(estimator, "best_estimator_") + if not hasattr(estimator, "best_estimator_"): + raise NotFittedError( + "This %s instance is not fitted yet. Call " + "'fit' with appropriate arguments before using this " + "estimator." % type(estimator).__name__ + ) def _skl_grid_score(X, y, scorer_, best_estimator_, refit, multimetric_): diff --git a/gensvm/util.py b/gensvm/util.py index 046f3be..40d0eb1 100644 --- a/gensvm/util.py +++ b/gensvm/util.py @@ -8,6 +8,7 @@ Utility functions for GenSVM import numpy as np +from sklearn.exceptions import NotFittedError def get_ranks(a): """ @@ -37,3 +38,15 @@ def get_ranks(a): ranks[~np.isnan(orig)] = count[dense - 1] + 1 ranks[np.isnan(orig)] = np.max(ranks) + 1 return list(ranks) + + +def check_is_fitted(estimator, attribute): + msg = ( + "This %(name)s instance is not fitted yet. Call 'fit' " + "with appropriate arguments before using this estimator." + ) + if not hasattr(estimator, "fit"): + raise TypeError("%s is not an estimator instance" % (estimator)) + + if not hasattr(estimator, attribute): + raise NotFittedError(msg % {"name": type(estimator).__name__}) -- cgit v1.2.3 From 51fd39469920122f21fcb89f44f7977b2ac323cb Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 4 Dec 2019 13:09:12 +0000 Subject: Formatting --- gensvm/sklearn_util.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'gensvm') diff --git a/gensvm/sklearn_util.py b/gensvm/sklearn_util.py index e23921b..eb8ceb6 100644 --- a/gensvm/sklearn_util.py +++ b/gensvm/sklearn_util.py @@ -89,7 +89,9 @@ def _skl_format_cv_results( score_time, ) = zip(*out) else: - (test_score_dicts, test_sample_counts, fit_time, score_time) = zip(*out) + (test_score_dicts, test_sample_counts, fit_time, score_time) = zip( + *out + ) # test_score_dicts and train_score dicts are lists of dictionaries and # we make them into dict of lists @@ -160,7 +162,9 @@ def _skl_format_cv_results( ) if return_train_score: _store( - "train_%s" % scorer_name, train_scores[scorer_name], splits=True + "train_%s" % scorer_name, + train_scores[scorer_name], + splits=True, ) return results -- cgit v1.2.3