repo_name
stringlengths
7
60
path
stringlengths
6
134
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
1.04k
149k
license
stringclasses
12 values
pkruskal/scikit-learn
sklearn/covariance/graph_lasso_.py
127
25626
"""GraphLasso: sparse inverse covariance estimation with an l1-penalized estimator. """ # Author: Gael Varoquaux <gael.varoquaux@normalesup.org> # License: BSD 3 clause # Copyright: INRIA import warnings import operator import sys import time import numpy as np from scipy import linalg from .empirical_covariance_ import (empirical_covariance, EmpiricalCovariance, log_likelihood) from ..utils import ConvergenceWarning from ..utils.extmath import pinvh from ..utils.validation import check_random_state, check_array from ..linear_model import lars_path from ..linear_model import cd_fast from ..cross_validation import check_cv, cross_val_score from ..externals.joblib import Parallel, delayed import collections # Helper functions to compute the objective and dual objective functions # of the l1-penalized estimator def _objective(mle, precision_, alpha): """Evaluation of the graph-lasso objective function the objective function is made of a shifted scaled version of the normalized log-likelihood (i.e. its empirical mean over the samples) and a penalisation term to promote sparsity """ p = precision_.shape[0] cost = - 2. * log_likelihood(mle, precision_) + p * np.log(2 * np.pi) cost += alpha * (np.abs(precision_).sum() - np.abs(np.diag(precision_)).sum()) return cost def _dual_gap(emp_cov, precision_, alpha): """Expression of the dual gap convergence criterion The specific definition is given in Duchi "Projected Subgradient Methods for Learning Sparse Gaussians". """ gap = np.sum(emp_cov * precision_) gap -= precision_.shape[0] gap += alpha * (np.abs(precision_).sum() - np.abs(np.diag(precision_)).sum()) return gap def alpha_max(emp_cov): """Find the maximum alpha for which there are some non-zeros off-diagonal. Parameters ---------- emp_cov : 2D array, (n_features, n_features) The sample covariance matrix Notes ----- This results from the bound for the all the Lasso that are solved in GraphLasso: each time, the row of cov corresponds to Xy. As the bound for alpha is given by `max(abs(Xy))`, the result follows. """ A = np.copy(emp_cov) A.flat[::A.shape[0] + 1] = 0 return np.max(np.abs(A)) # The g-lasso algorithm def graph_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, enet_tol=1e-4, max_iter=100, verbose=False, return_costs=False, eps=np.finfo(np.float64).eps, return_n_iter=False): """l1-penalized covariance estimator Read more in the :ref:`User Guide <sparse_inverse_covariance>`. Parameters ---------- emp_cov : 2D ndarray, shape (n_features, n_features) Empirical covariance from which to compute the covariance estimate. alpha : positive float The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. cov_init : 2D array (n_features, n_features), optional The initial guess for the covariance. mode : {'cd', 'lars'} The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. tol : positive float, optional The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. enet_tol : positive float, optional The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. max_iter : integer, optional The maximum number of iterations. verbose : boolean, optional If verbose is True, the objective function and dual gap are printed at each iteration. return_costs : boolean, optional If return_costs is True, the objective function and dual gap at each iteration are returned. eps : float, optional The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. return_n_iter : bool, optional Whether or not to return the number of iterations. Returns ------- covariance : 2D ndarray, shape (n_features, n_features) The estimated covariance matrix. precision : 2D ndarray, shape (n_features, n_features) The estimated (sparse) precision matrix. costs : list of (objective, dual_gap) pairs The list of values of the objective function and the dual gap at each iteration. Returned only if return_costs is True. n_iter : int Number of iterations. Returned only if `return_n_iter` is set to True. See Also -------- GraphLasso, GraphLassoCV Notes ----- The algorithm employed to solve this problem is the GLasso algorithm, from the Friedman 2008 Biostatistics paper. It is the same algorithm as in the R `glasso` package. One possible difference with the `glasso` R package is that the diagonal coefficients are not penalized. """ _, n_features = emp_cov.shape if alpha == 0: if return_costs: precision_ = linalg.inv(emp_cov) cost = - 2. * log_likelihood(emp_cov, precision_) cost += n_features * np.log(2 * np.pi) d_gap = np.sum(emp_cov * precision_) - n_features if return_n_iter: return emp_cov, precision_, (cost, d_gap), 0 else: return emp_cov, precision_, (cost, d_gap) else: if return_n_iter: return emp_cov, linalg.inv(emp_cov), 0 else: return emp_cov, linalg.inv(emp_cov) if cov_init is None: covariance_ = emp_cov.copy() else: covariance_ = cov_init.copy() # As a trivial regularization (Tikhonov like), we scale down the # off-diagonal coefficients of our starting point: This is needed, as # in the cross-validation the cov_init can easily be # ill-conditioned, and the CV loop blows. Beside, this takes # conservative stand-point on the initial conditions, and it tends to # make the convergence go faster. covariance_ *= 0.95 diagonal = emp_cov.flat[::n_features + 1] covariance_.flat[::n_features + 1] = diagonal precision_ = pinvh(covariance_) indices = np.arange(n_features) costs = list() # The different l1 regression solver have different numerical errors if mode == 'cd': errors = dict(over='raise', invalid='ignore') else: errors = dict(invalid='raise') try: # be robust to the max_iter=0 edge case, see: # https://github.com/scikit-learn/scikit-learn/issues/4134 d_gap = np.inf for i in range(max_iter): for idx in range(n_features): sub_covariance = covariance_[indices != idx].T[indices != idx] row = emp_cov[idx, indices != idx] with np.errstate(**errors): if mode == 'cd': # Use coordinate descent coefs = -(precision_[indices != idx, idx] / (precision_[idx, idx] + 1000 * eps)) coefs, _, _, _ = cd_fast.enet_coordinate_descent_gram( coefs, alpha, 0, sub_covariance, row, row, max_iter, enet_tol, check_random_state(None), False) else: # Use LARS _, _, coefs = lars_path( sub_covariance, row, Xy=row, Gram=sub_covariance, alpha_min=alpha / (n_features - 1), copy_Gram=True, method='lars', return_path=False) # Update the precision matrix precision_[idx, idx] = ( 1. / (covariance_[idx, idx] - np.dot(covariance_[indices != idx, idx], coefs))) precision_[indices != idx, idx] = (- precision_[idx, idx] * coefs) precision_[idx, indices != idx] = (- precision_[idx, idx] * coefs) coefs = np.dot(sub_covariance, coefs) covariance_[idx, indices != idx] = coefs covariance_[indices != idx, idx] = coefs d_gap = _dual_gap(emp_cov, precision_, alpha) cost = _objective(emp_cov, precision_, alpha) if verbose: print( '[graph_lasso] Iteration % 3i, cost % 3.2e, dual gap %.3e' % (i, cost, d_gap)) if return_costs: costs.append((cost, d_gap)) if np.abs(d_gap) < tol: break if not np.isfinite(cost) and i > 0: raise FloatingPointError('Non SPD result: the system is ' 'too ill-conditioned for this solver') else: warnings.warn('graph_lasso: did not converge after %i iteration:' ' dual gap: %.3e' % (max_iter, d_gap), ConvergenceWarning) except FloatingPointError as e: e.args = (e.args[0] + '. The system is too ill-conditioned for this solver',) raise e if return_costs: if return_n_iter: return covariance_, precision_, costs, i + 1 else: return covariance_, precision_, costs else: if return_n_iter: return covariance_, precision_, i + 1 else: return covariance_, precision_ class GraphLasso(EmpiricalCovariance): """Sparse inverse covariance estimation with an l1-penalized estimator. Read more in the :ref:`User Guide <sparse_inverse_covariance>`. Parameters ---------- alpha : positive float, default 0.01 The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. mode : {'cd', 'lars'}, default 'cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. tol : positive float, default 1e-4 The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. enet_tol : positive float, optional The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. max_iter : integer, default 100 The maximum number of iterations. verbose : boolean, default False If verbose is True, the objective function and dual gap are plotted at each iteration. assume_centered : boolean, default False If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data are centered before computation. Attributes ---------- covariance_ : array-like, shape (n_features, n_features) Estimated covariance matrix precision_ : array-like, shape (n_features, n_features) Estimated pseudo inverse matrix. n_iter_ : int Number of iterations run. See Also -------- graph_lasso, GraphLassoCV """ def __init__(self, alpha=.01, mode='cd', tol=1e-4, enet_tol=1e-4, max_iter=100, verbose=False, assume_centered=False): self.alpha = alpha self.mode = mode self.tol = tol self.enet_tol = enet_tol self.max_iter = max_iter self.verbose = verbose self.assume_centered = assume_centered # The base class needs this for the score method self.store_precision = True def fit(self, X, y=None): X = check_array(X) if self.assume_centered: self.location_ = np.zeros(X.shape[1]) else: self.location_ = X.mean(0) emp_cov = empirical_covariance( X, assume_centered=self.assume_centered) self.covariance_, self.precision_, self.n_iter_ = graph_lasso( emp_cov, alpha=self.alpha, mode=self.mode, tol=self.tol, enet_tol=self.enet_tol, max_iter=self.max_iter, verbose=self.verbose, return_n_iter=True) return self # Cross-validation with GraphLasso def graph_lasso_path(X, alphas, cov_init=None, X_test=None, mode='cd', tol=1e-4, enet_tol=1e-4, max_iter=100, verbose=False): """l1-penalized covariance estimator along a path of decreasing alphas Read more in the :ref:`User Guide <sparse_inverse_covariance>`. Parameters ---------- X : 2D ndarray, shape (n_samples, n_features) Data from which to compute the covariance estimate. alphas : list of positive floats The list of regularization parameters, decreasing order. X_test : 2D array, shape (n_test_samples, n_features), optional Optional test matrix to measure generalisation error. mode : {'cd', 'lars'} The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. tol : positive float, optional The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. enet_tol : positive float, optional The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. max_iter : integer, optional The maximum number of iterations. verbose : integer, optional The higher the verbosity flag, the more information is printed during the fitting. Returns ------- covariances_ : List of 2D ndarray, shape (n_features, n_features) The estimated covariance matrices. precisions_ : List of 2D ndarray, shape (n_features, n_features) The estimated (sparse) precision matrices. scores_ : List of float The generalisation error (log-likelihood) on the test data. Returned only if test data is passed. """ inner_verbose = max(0, verbose - 1) emp_cov = empirical_covariance(X) if cov_init is None: covariance_ = emp_cov.copy() else: covariance_ = cov_init covariances_ = list() precisions_ = list() scores_ = list() if X_test is not None: test_emp_cov = empirical_covariance(X_test) for alpha in alphas: try: # Capture the errors, and move on covariance_, precision_ = graph_lasso( emp_cov, alpha=alpha, cov_init=covariance_, mode=mode, tol=tol, enet_tol=enet_tol, max_iter=max_iter, verbose=inner_verbose) covariances_.append(covariance_) precisions_.append(precision_) if X_test is not None: this_score = log_likelihood(test_emp_cov, precision_) except FloatingPointError: this_score = -np.inf covariances_.append(np.nan) precisions_.append(np.nan) if X_test is not None: if not np.isfinite(this_score): this_score = -np.inf scores_.append(this_score) if verbose == 1: sys.stderr.write('.') elif verbose > 1: if X_test is not None: print('[graph_lasso_path] alpha: %.2e, score: %.2e' % (alpha, this_score)) else: print('[graph_lasso_path] alpha: %.2e' % alpha) if X_test is not None: return covariances_, precisions_, scores_ return covariances_, precisions_ class GraphLassoCV(GraphLasso): """Sparse inverse covariance w/ cross-validated choice of the l1 penalty Read more in the :ref:`User Guide <sparse_inverse_covariance>`. Parameters ---------- alphas : integer, or list positive float, optional If an integer is given, it fixes the number of points on the grids of alpha to be used. If a list is given, it gives the grid to be used. See the notes in the class docstring for more details. n_refinements: strictly positive integer The number of times the grid is refined. Not used if explicit values of alphas are passed. cv : cross-validation generator, optional see sklearn.cross_validation module. If None is passed, defaults to a 3-fold strategy tol: positive float, optional The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. enet_tol : positive float, optional The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. max_iter: integer, optional Maximum number of iterations. mode: {'cd', 'lars'} The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where number of features is greater than number of samples. Elsewhere prefer cd which is more numerically stable. n_jobs: int, optional number of jobs to run in parallel (default 1). verbose: boolean, optional If verbose is True, the objective function and duality gap are printed at each iteration. assume_centered : Boolean If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data are centered before computation. Attributes ---------- covariance_ : numpy.ndarray, shape (n_features, n_features) Estimated covariance matrix. precision_ : numpy.ndarray, shape (n_features, n_features) Estimated precision matrix (inverse covariance). alpha_ : float Penalization parameter selected. cv_alphas_ : list of float All penalization parameters explored. `grid_scores`: 2D numpy.ndarray (n_alphas, n_folds) Log-likelihood score on left-out data across folds. n_iter_ : int Number of iterations run for the optimal alpha. See Also -------- graph_lasso, GraphLasso Notes ----- The search for the optimal penalization parameter (alpha) is done on an iteratively refined grid: first the cross-validated scores on a grid are computed, then a new refined grid is centered around the maximum, and so on. One of the challenges which is faced here is that the solvers can fail to converge to a well-conditioned estimate. The corresponding values of alpha then come out as missing values, but the optimum may be close to these missing values. """ def __init__(self, alphas=4, n_refinements=4, cv=None, tol=1e-4, enet_tol=1e-4, max_iter=100, mode='cd', n_jobs=1, verbose=False, assume_centered=False): self.alphas = alphas self.n_refinements = n_refinements self.mode = mode self.tol = tol self.enet_tol = enet_tol self.max_iter = max_iter self.verbose = verbose self.cv = cv self.n_jobs = n_jobs self.assume_centered = assume_centered # The base class needs this for the score method self.store_precision = True def fit(self, X, y=None): """Fits the GraphLasso covariance model to X. Parameters ---------- X : ndarray, shape (n_samples, n_features) Data from which to compute the covariance estimate """ X = check_array(X) if self.assume_centered: self.location_ = np.zeros(X.shape[1]) else: self.location_ = X.mean(0) emp_cov = empirical_covariance( X, assume_centered=self.assume_centered) cv = check_cv(self.cv, X, y, classifier=False) # List of (alpha, scores, covs) path = list() n_alphas = self.alphas inner_verbose = max(0, self.verbose - 1) if isinstance(n_alphas, collections.Sequence): alphas = self.alphas n_refinements = 1 else: n_refinements = self.n_refinements alpha_1 = alpha_max(emp_cov) alpha_0 = 1e-2 * alpha_1 alphas = np.logspace(np.log10(alpha_0), np.log10(alpha_1), n_alphas)[::-1] t0 = time.time() for i in range(n_refinements): with warnings.catch_warnings(): # No need to see the convergence warnings on this grid: # they will always be points that will not converge # during the cross-validation warnings.simplefilter('ignore', ConvergenceWarning) # Compute the cross-validated loss on the current grid # NOTE: Warm-restarting graph_lasso_path has been tried, and # this did not allow to gain anything (same execution time with # or without). this_path = Parallel( n_jobs=self.n_jobs, verbose=self.verbose )( delayed(graph_lasso_path)( X[train], alphas=alphas, X_test=X[test], mode=self.mode, tol=self.tol, enet_tol=self.enet_tol, max_iter=int(.1 * self.max_iter), verbose=inner_verbose) for train, test in cv) # Little danse to transform the list in what we need covs, _, scores = zip(*this_path) covs = zip(*covs) scores = zip(*scores) path.extend(zip(alphas, scores, covs)) path = sorted(path, key=operator.itemgetter(0), reverse=True) # Find the maximum (avoid using built in 'max' function to # have a fully-reproducible selection of the smallest alpha # in case of equality) best_score = -np.inf last_finite_idx = 0 for index, (alpha, scores, _) in enumerate(path): this_score = np.mean(scores) if this_score >= .1 / np.finfo(np.float64).eps: this_score = np.nan if np.isfinite(this_score): last_finite_idx = index if this_score >= best_score: best_score = this_score best_index = index # Refine the grid if best_index == 0: # We do not need to go back: we have chosen # the highest value of alpha for which there are # non-zero coefficients alpha_1 = path[0][0] alpha_0 = path[1][0] elif (best_index == last_finite_idx and not best_index == len(path) - 1): # We have non-converged models on the upper bound of the # grid, we need to refine the grid there alpha_1 = path[best_index][0] alpha_0 = path[best_index + 1][0] elif best_index == len(path) - 1: alpha_1 = path[best_index][0] alpha_0 = 0.01 * path[best_index][0] else: alpha_1 = path[best_index - 1][0] alpha_0 = path[best_index + 1][0] if not isinstance(n_alphas, collections.Sequence): alphas = np.logspace(np.log10(alpha_1), np.log10(alpha_0), n_alphas + 2) alphas = alphas[1:-1] if self.verbose and n_refinements > 1: print('[GraphLassoCV] Done refinement % 2i out of %i: % 3is' % (i + 1, n_refinements, time.time() - t0)) path = list(zip(*path)) grid_scores = list(path[1]) alphas = list(path[0]) # Finally, compute the score with alpha = 0 alphas.append(0) grid_scores.append(cross_val_score(EmpiricalCovariance(), X, cv=cv, n_jobs=self.n_jobs, verbose=inner_verbose)) self.grid_scores = np.array(grid_scores) best_alpha = alphas[best_index] self.alpha_ = best_alpha self.cv_alphas_ = alphas # Finally fit the model with the selected alpha self.covariance_, self.precision_, self.n_iter_ = graph_lasso( emp_cov, alpha=best_alpha, mode=self.mode, tol=self.tol, enet_tol=self.enet_tol, max_iter=self.max_iter, verbose=inner_verbose, return_n_iter=True) return self
bsd-3-clause
mikekestemont/ruzicka
code/04latin_test_o2.py
1
3340
from __future__ import print_function import os import time import json import pickle import sys from itertools import product, combinations import matplotlib import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import numpy as np from sklearn.preprocessing import LabelEncoder from ruzicka.utilities import binarize from ruzicka.vectorization import Vectorizer from ruzicka.utilities import load_pan_dataset, train_dev_split, get_vocab_size from sklearn.cross_validation import train_test_split from ruzicka.score_shifting import ScoreShifter from ruzicka.evaluation import pan_metrics from ruzicka.Order2Verifier import Order2Verifier as Verifier import ruzicka.art as art # run script for top-5 metrics ngram_type = 'word' ngram_size = 1 base = 'profile' vector_space = 'tf_std' metric = 'cosine' nb_bootstrap_iter = 100 rnd_prop = 0.5 nb_imposters = 30 mfi = sys.maxint min_df = 2 # get imposter data: train_data, _ = load_pan_dataset('../data/latin/dev') # ignore unknown documents train_labels, train_documents = zip(*train_data) # get test data: test_data, _ = load_pan_dataset('../data/latin/test') # ignore unknown documents test_labels, test_documents = zip(*test_data) # fit encoder for author labels: label_encoder = LabelEncoder() label_encoder.fit(train_labels+test_labels) train_ints = label_encoder.transform(train_labels) test_ints = label_encoder.transform(test_labels) # fit vectorizer: vectorizer = Vectorizer(mfi = mfi, vector_space = vector_space, ngram_type = ngram_type, ngram_size = ngram_size) vectorizer.fit(train_documents+test_documents) train_X = vectorizer.transform(train_documents).toarray() test_X = vectorizer.transform(test_documents).toarray() cols = ['label'] for test_author in sorted(set(test_ints)): auth_label = label_encoder.inverse_transform([test_author])[0] cols.append(auth_label) proba_df = pd.DataFrame(columns=cols) for idx in range(len(test_documents)): target_auth = test_ints[idx] target_docu = test_X[idx] non_target_test_ints = np.array([test_ints[i] for i in range(len(test_ints)) if i != idx]) non_target_test_X = np.array([test_X[i] for i in range(len(test_ints)) if i != idx]) tmp_train_X = np.vstack((train_X, non_target_test_X)) tmp_train_y = np.hstack((train_ints, non_target_test_ints)) tmp_test_X, tmp_test_y = [], [] for t_auth in sorted(set(test_ints)): tmp_test_X.append(target_docu) tmp_test_y.append(t_auth) # fit the verifier: verifier = Verifier(metric = metric, base = base, nb_bootstrap_iter = nb_bootstrap_iter, rnd_prop = rnd_prop) verifier.fit(tmp_train_X, tmp_train_y) probas = verifier.predict_proba(test_X = tmp_test_X, test_y = tmp_test_y, nb_imposters = nb_imposters) row = [label_encoder.inverse_transform([target_auth])[0]] # author label row += list(probas) print(row) proba_df.loc[len(proba_df)] = row proba_df = proba_df.set_index('label') # write away score tables: table_dir = '../output/tables/' if not os.path.isdir(table_dir): os.mkdir(table_dir) proba_df.to_csv(table_dir+'lat_proba_'+metric+'_'+vector_space+'.csv')
mit
MartinDelzant/scikit-learn
benchmarks/bench_tree.py
297
3617
""" To run this, you'll need to have installed. * scikit-learn Does two benchmarks First, we fix a training set, increase the number of samples to classify and plot number of classified samples as a function of time. In the second benchmark, we increase the number of dimensions of the training set, classify a sample and plot the time taken as a function of the number of dimensions. """ import numpy as np import pylab as pl import gc from datetime import datetime # to store the results scikit_classifier_results = [] scikit_regressor_results = [] mu_second = 0.0 + 10 ** 6 # number of microseconds in a second def bench_scikit_tree_classifier(X, Y): """Benchmark with scikit-learn decision tree classifier""" from sklearn.tree import DecisionTreeClassifier gc.collect() # start time tstart = datetime.now() clf = DecisionTreeClassifier() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_classifier_results.append( delta.seconds + delta.microseconds / mu_second) def bench_scikit_tree_regressor(X, Y): """Benchmark with scikit-learn decision tree regressor""" from sklearn.tree import DecisionTreeRegressor gc.collect() # start time tstart = datetime.now() clf = DecisionTreeRegressor() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_regressor_results.append( delta.seconds + delta.microseconds / mu_second) if __name__ == '__main__': print('============================================') print('Warning: this is going to take a looong time') print('============================================') n = 10 step = 10000 n_samples = 10000 dim = 10 n_classes = 10 for i in range(n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') n_samples += step X = np.random.randn(n_samples, dim) Y = np.random.randint(0, n_classes, (n_samples,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(n_samples) bench_scikit_tree_regressor(X, Y) xx = range(0, n * step, step) pl.figure('scikit-learn tree benchmark results') pl.subplot(211) pl.title('Learning with varying number of samples') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of samples') pl.ylabel('Time (s)') scikit_classifier_results = [] scikit_regressor_results = [] n = 10 step = 500 start_dim = 500 n_classes = 10 dim = start_dim for i in range(0, n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') dim += step X = np.random.randn(100, dim) Y = np.random.randint(0, n_classes, (100,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(100) bench_scikit_tree_regressor(X, Y) xx = np.arange(start_dim, start_dim + n * step, step) pl.subplot(212) pl.title('Learning in high dimensional spaces') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of dimensions') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
ipashchenko/emcee-x
document/plots/oned.py
16
2164
import os import sys import time import numpy as np import matplotlib.pyplot as pl import h5py from multiprocessing import Pool sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..", ".."))) import emcee # import acor def lnprobfn(p, icov): return -0.5 * np.dot(p, np.dot(icov, p)) def random_cov(ndim, dof=1): v = np.random.randn(ndim * (ndim + dof)).reshape((ndim + dof, ndim)) return (sum([np.outer(v[i], v[i]) for i in range(ndim + dof)]) / (ndim + dof)) _rngs = {} def _worker(args): i, outfn, nsteps = args pid = os.getpid() _random = _rngs.get(pid, np.random.RandomState(int(int(pid) + time.time()))) _rngs[pid] = _random ndim = int(np.ceil(2 ** (7 * _random.rand()))) nwalkers = 2 * ndim + 2 # nwalkers += nwalkers % 2 print ndim, nwalkers cov = random_cov(ndim) icov = np.linalg.inv(cov) ens_samp = emcee.EnsembleSampler(nwalkers, ndim, lnprobfn, args=[icov]) ens_samp.random_state = _random.get_state() pos, lnprob, state = ens_samp.run_mcmc(np.random.randn(nwalkers * ndim) .reshape([nwalkers, ndim]), nsteps) proposal = np.diag(cov.diagonal()) mh_samp = emcee.MHSampler(proposal, ndim, lnprobfn, args=[icov]) mh_samp.random_state = state mh_samp.run_mcmc(np.random.randn(ndim), nsteps) f = h5py.File(outfn) f["data"][i, :] = np.array([ndim, np.mean(ens_samp.acor), np.mean(mh_samp.acor)]) f.close() def oned(): nsteps = 10000 niter = 10 nthreads = 2 outfn = os.path.join(os.path.split(__file__)[0], "gauss_scaling.h5") print outfn f = h5py.File(outfn, "w") f.create_dataset("data", (niter, 3), "f") f.close() pool = Pool(nthreads) pool.map(_worker, [(i, outfn, nsteps) for i in range(niter)]) f = h5py.File(outfn) data = f["data"][...] f.close() pl.clf() pl.plot(data[:, 0], data[:, 1], "ks", alpha=0.5) pl.plot(data[:, 0], data[:, 2], ".k", alpha=0.5) pl.savefig(os.path.join(os.path.split(__file__)[0], "gauss_scaling.png")) if __name__ == "__main__": oned()
mit
gdl-civestav-localization/cinvestav_location_fingerprinting
experimentation/__init__.py
1
1691
import os import cPickle import matplotlib.pyplot as plt from datasets import DatasetManager def plot_cost(results, data_name, plot_label): plt.figure(plot_label) plt.ylabel('Accuracy (m)', fontsize=30) plt.xlabel('Epoch', fontsize=30) plt.yscale('symlog') plt.tick_params(axis='both', which='major', labelsize=20) plt.grid(True) for i in range(1, 2, 1): y, x = zip(*results[i][data_name]) name = results[i]['Name'] plt.plot(x, y, label=name, linewidth=5.0) plt.legend(fontsize='xx-large') def get_metrics(test_set_y, predicted_values, model_name): for i in xrange(len(predicted_values)): print predicted_values[i][1] if __name__ == '__main__': """ seed = 50 with open(os.path.join('experimentation', 'cinvestav_testbed_experiment_results_' + str(seed)), 'rb') as f: results = cPickle.load(f) plot_cost( results=results, data_name='cost_train', plot_label='Cost on train phase') plot_cost( results=results, data_name='cost_valid', plot_label='Cost on valid phase') plot_cost( results=results, data_name='cost_test', plot_label='Cost on test phase') plt.show() """ seed = 50 dataset, result = DatasetManager.read_dataset2('test_cleaned_dataset.csv', shared=True, seed=seed) with open(os.path.join('trained_models', 'Logistic Regressionbrandeis_university.save'), 'rb') as f: model = cPickle.load(f) predicted_values = model.predict(dataset) get_metrics( test_set_y=result, predicted_values=predicted_values, model_name='Logistic Regression' )
gpl-3.0
bachiraoun/fullrmc
Constraints/StructureFactorConstraints.py
1
64342
""" StructureFactorConstraints contains classes for all constraints related experimental static structure factor functions. .. inheritance-diagram:: fullrmc.Constraints.StructureFactorConstraints :parts: 1 """ # standard libraries imports from __future__ import print_function import itertools, re # external libraries imports import numpy as np from pdbparser.Utilities.Database import is_element_property, get_element_property from pdbparser.Utilities.Collection import get_normalized_weighting # fullrmc imports from ..Globals import INT_TYPE, FLOAT_TYPE, PI, PRECISION, LOGGER from ..Globals import str, long, unicode, bytes, basestring, range, xrange, maxint from ..Core.Collection import is_number, is_integer, get_path from ..Core.Collection import reset_if_collected_out_of_date, get_real_elements_weight from ..Core.Collection import get_caller_frames from ..Core.Constraint import Constraint, ExperimentalConstraint from ..Core.pairs_histograms import multiple_pairs_histograms_coords, full_pairs_histograms_coords class StructureFactorConstraint(ExperimentalConstraint): """ Controls the Structure Factor noted as S(Q) and also called total-scattering structure function or Static Structure Factor. S(Q) is a dimensionless quantity and normalized such as the average value :math:`<S(Q)>=1`. It is worth mentioning that S(Q) is nothing other than the normalized and corrected diffraction pattern if all experimental artefacts powder. The computation of S(Q) is done through an inverse Sine Fourier transform of the computed pair distribution function G(r). .. math:: S(Q) = 1+ \\frac{1}{Q} \\int_{0}^{\\infty} G(r) sin(Qr) dr From an atomistic model and histogram point of view, G(r) is computed as the following: .. math:: G(r) = 4 \\pi r (\\rho_{r} - \\rho_{0}) = 4 \\pi \\rho_{0} r (g(r)-1) = \\frac{R(r)}{r} - 4 \\pi \\rho_{0} g(r) is calculated after binning all pair atomic distances into a weighted histograms as the following: .. math:: g(r) = \\sum \\limits_{i,j}^{N} w_{i,j} \\frac{\\rho_{i,j}(r)}{\\rho_{0}} = \\sum \\limits_{i,j}^{N} w_{i,j} \\frac{n_{i,j}(r) / v(r)}{N_{i,j} / V} Where:\n :math:`Q` is the momentum transfer. \n :math:`r` is the distance between two atoms. \n :math:`\\rho_{i,j}(r)` is the pair density function of atoms i and j. \n :math:`\\rho_{0}` is the average number density of the system. \n :math:`w_{i,j}` is the relative weighting of atom types i and j. \n :math:`R(r)` is the radial distribution function (rdf). \n :math:`N` is the total number of atoms. \n :math:`V` is the volume of the system. \n :math:`n_{i,j}(r)` is the number of atoms i neighbouring j at a distance r. \n :math:`v(r)` is the annulus volume at distance r and of thickness dr. \n :math:`N_{i,j}` is the total number of atoms i and j in the system. \n +----------------------------------------------------------------------+ |.. figure:: reduced_structure_factor_constraint_plot_method.png | | :width: 530px | | :height: 400px | | :align: left | | | | Reduced structure factor of memory shape Nickel-Titanium alloy. | +----------------------------------------------------------------------+ :Parameters: #. experimentalData (numpy.ndarray, string): Experimental data as numpy.ndarray or string path to load data using numpy.loadtxt method. #. dataWeights (None, numpy.ndarray): Weights array of the same number of points of experimentalData used in the constraint's standard error computation. Therefore particular fitting emphasis can be put on different data points that might be considered as more or less important in order to get a reasonable and plausible modal.\n If None is given, all data points are considered of the same importance in the computation of the constraint's standard error.\n If numpy.ndarray is given, all weights must be positive and all zeros weighted data points won't contribute to the total constraint's standard error. At least a single weight point is required to be non-zeros and the weights array will be automatically scaled upon setting such as the the sum of all the weights is equal to the number of data points. #. weighting (string): The elements weighting scheme. It must be any atomic attribute (atomicNumber, neutronCohb, neutronIncohb, neutronCohXs, neutronIncohXs, atomicWeight, covalentRadius) defined in pdbparser database. In case of xrays or neutrons experimental weights, one can simply set weighting to 'xrays' or 'neutrons' and the value will be automatically adjusted to respectively 'atomicNumber' and 'neutronCohb'. If attribute values are missing in the pdbparser database, atomic weights must be given in atomsWeight dictionary argument. #. atomsWeight (None, dict): Atoms weight dictionary where keys are atoms element and values are custom weights. If None is given or partially given, missing elements weighting will be fully set given weighting scheme. #. rmin (None, number): The minimum distance value to compute G(r) histogram. If None is given, rmin is computed as :math:`2 \\pi / Q_{max}`. #. rmax (None, number): The maximum distance value to compute G(r) histogram. If None is given, rmax is computed as :math:`2 \\pi / dQ`. #. dr (None, number): The distance bin value to compute G(r) histogram. If None is given, bin is computed as :math:`2 \\pi / (Q_{max}-Q_{min})`. #. scaleFactor (number): A normalization scale factor used to normalize the computed data to the experimental ones. #. adjustScaleFactor (list, tuple): Used to adjust fit or guess the best scale factor during stochastic engine runtime. It must be a list of exactly three entries.\n #. The frequency in number of generated moves of finding the best scale factor. If 0 frequency is given, it means that the scale factor is fixed. #. The minimum allowed scale factor value. #. The maximum allowed scale factor value. #. windowFunction (None, numpy.ndarray): The window function to convolute with the computed pair distribution function of the system prior to comparing it with the experimental data. In general, the experimental pair distribution function G(r) shows artificial wrinkles, among others the main reason is because G(r) is computed by applying a sine Fourier transform to the experimental structure factor S(q). Therefore window function is used to best imitate the numerical artefacts in the experimental data. #. limits (None, tuple, list): The distance limits to compute the histograms. If None is given, the limits will be automatically set the the min and max distance of the experimental data. Otherwise, a tuple of exactly two items where the first is the minimum distance or None and the second is the maximum distance or None. **NB**: If adjustScaleFactor first item (frequency) is 0, the scale factor will remain untouched and the limits minimum and maximum won't be checked. .. code-block:: python # import fullrmc modules from fullrmc.Engine import Engine from fullrmc.Constraints.StructureFactorConstraints import StructureFactorConstraint # create engine ENGINE = Engine(path='my_engine.rmc') # set pdb file ENGINE.set_pdb('system.pdb') # create and add constraint SFC = StructureFactorConstraint(experimentalData="sq.dat", weighting="atomicNumber") ENGINE.add_constraints(SFC) """ def __init__(self, experimentalData, dataWeights=None, weighting="atomicNumber", atomsWeight=None, rmin=None, rmax=None, dr=None, scaleFactor=1.0, adjustScaleFactor=(0, 0.8, 1.2), windowFunction=None, limits=None): # initialize variables self.__experimentalQValues = None self.__experimentalSF = None self.__rmin = None self.__rmax = None self.__dr = None self.__minimumDistance = None self.__maximumDistance = None self.__bin = None self.__shellCenters = None self.__histogramSize = None self.__shellVolumes = None self.__Gr2SqMatrix = None # initialize constraint super(StructureFactorConstraint, self).__init__( experimentalData=experimentalData, dataWeights=dataWeights, scaleFactor=scaleFactor, adjustScaleFactor=adjustScaleFactor) # set atomsWeight self.set_atoms_weight(atomsWeight) # set elements weighting self.set_weighting(weighting) self.__set_weighting_scheme() # set window function self.set_window_function(windowFunction) # set r parameters self.set_rmin(rmin) self.set_rmax(rmax) self.set_dr(dr) # set frame data FRAME_DATA = [d for d in self.FRAME_DATA] FRAME_DATA.extend(['_StructureFactorConstraint__experimentalQValues', '_StructureFactorConstraint__experimentalSF', '_StructureFactorConstraint__elementsPairs', '_StructureFactorConstraint__weightingScheme', '_StructureFactorConstraint__atomsWeight', '_StructureFactorConstraint__qmin', '_StructureFactorConstraint__qmax', '_StructureFactorConstraint__rmin', '_StructureFactorConstraint__rmax', '_StructureFactorConstraint__dr', '_StructureFactorConstraint__minimumDistance', '_StructureFactorConstraint__maximumDistance', '_StructureFactorConstraint__bin', '_StructureFactorConstraint__shellCenters', '_StructureFactorConstraint__histogramSize', '_StructureFactorConstraint__shellVolumes', '_StructureFactorConstraint__Gr2SqMatrix', '_StructureFactorConstraint__windowFunction', '_elementsWeight',] ) RUNTIME_DATA = [d for d in self.RUNTIME_DATA] RUNTIME_DATA.extend( [] ) object.__setattr__(self, 'FRAME_DATA', tuple(FRAME_DATA) ) object.__setattr__(self, 'RUNTIME_DATA', tuple(RUNTIME_DATA) ) def _codify_update__(self, name='constraint', addDependencies=True): dependencies = [] code = [] if addDependencies: code.extend(dependencies) dw = self.dataWeights if dw is not None: dw = list(dw) code.append("dw = {dw}".format(dw=dw)) wf = self.windowFunction if isinstance(wf, np.ndarray): code.append("wf = np.array({wf})".format(wf=list(wf))) else: code.append("wf = {wf}".format(wf=wf)) code.append("{name}.set_used({val})".format(name=name, val=self.used)) code.append("{name}.set_scale_factor({val})".format(name=name, val=self.scaleFactor)) code.append("{name}.set_adjust_scale_factor({val})".format(name=name, val=self.adjustScaleFactor)) code.append("{name}.set_data_weights(dw)".format(name=name)) code.append("{name}.set_atoms_weight({val})".format(name=name, val=self.atomsWeight)) code.append("{name}.set_window_function(wf)".format(name=name)) code.append("{name}.set_rmin({val})".format(name=name, val=self.rmin)) code.append("{name}.set_rmax({val})".format(name=name, val=self.rmax)) code.append("{name}.set_dr({val})".format(name=name, val=self.dr)) code.append("{name}.set_limits({val})".format(name=name, val=self.limits)) # return return dependencies, '\n'.join(code) def _codify__(self, engine, name='constraint', addDependencies=True): assert isinstance(name, basestring), LOGGER.error("name must be a string") assert re.match('[a-zA-Z_][a-zA-Z0-9_]*$', name) is not None, LOGGER.error("given name '%s' can't be used as a variable name"%name) klass = self.__class__.__name__ dependencies = ['import numpy as np','from fullrmc.Constraints import StructureFactorConstraints'] code = [] if addDependencies: code.extend(dependencies) x = list(self.experimentalData[:,0]) y = list(self.experimentalData[:,1]) code.append("x = {x}".format(x=x)) code.append("y = {y}".format(y=y)) code.append("d = np.transpose([x,y]).astype(np.float32)") dw = self.dataWeights if dw is not None: dw = list(dw) code.append("dw = {dw}".format(dw=dw)) wf = self.windowFunction if isinstance(wf, np.ndarray): code.append("wf = np.array({wf})".format(wf=list(wf))) else: code.append("wf = {wf}".format(wf=wf)) code.append("{name} = {klass}s.{klass}\ (experimentalData=d, dataWeights=dw, weighting='{weighting}', atomsWeight={atomsWeight}, \ rmin={rmin}, rmax={rmax}, dr={dr}, scaleFactor={scaleFactor}, adjustScaleFactor={adjustScaleFactor}, \ shapeFuncParams=sfp, windowFunction=wf, limits={limits})".format(name=name, klass=klass, weighting=self.weighting, atomsWeight=self.atomsWeight, rmin=self.rmin, rmax=self.rmax, dr=self.dr, scaleFactor=self.scaleFactor, adjustScaleFactor=self.adjustScaleFactor, limits=self.limits)) code.append("{engine}.add_constraints([{name}])".format(engine=engine, name=name)) # return return dependencies, '\n'.join(code) #def __getstate__(self): # # make sure that __Gr2SqMatrix is not pickled but saved to the disk as None # state = super(StructureFactorConstraint, self).__getstate__() # state["_StructureFactorConstraint__Gr2SqMatrix"] = None # return state # #def __setstate__(self, state): # # make sure to regenerate G(r) to S(q) matrix at loading time # self.__dict__.update( state ) # self.__set_Gr_2_Sq_matrix() # def __set_Gr_2_Sq_matrix(self): if self.__experimentalQValues is None or self.__shellCenters is None: self.__Gr2SqMatrix = None else: Qs = self.__experimentalQValues Rs = self.__shellCenters dr = self.__shellCenters[1]-self.__shellCenters[0] qr = Rs.reshape((-1,1))*(np.ones((len(Rs),1), dtype=FLOAT_TYPE)*Qs) sinqr = np.sin(qr) sinqr_q = sinqr/Qs self.__Gr2SqMatrix = dr*sinqr_q def __set_weighting_scheme(self): if self.engine is not None: self.__elementsPairs = sorted(itertools.combinations_with_replacement(self.engine.elements,2)) #elementsWeight = dict([(el,float(get_element_property(el,self.__weighting))) for el in self.engine.elements]) #self._elementsWeight = dict([(el,self.__atomsWeight.get(el, float(get_element_property(el,self.__weighting)))) for el in self.engine.elements]) self._elementsWeight = get_real_elements_weight(elements=self.engine.elements, weightsDict=self.__atomsWeight, weighting=self.__weighting) self.__weightingScheme = get_normalized_weighting(numbers=self.engine.numberOfAtomsPerElement, weights=self._elementsWeight) for k in self.__weightingScheme: self.__weightingScheme[k] = FLOAT_TYPE(self.__weightingScheme[k]) else: self.__elementsPairs = None self.__weightingScheme = None # dump to repository self._dump_to_repository({'_StructureFactorConstraint__elementsPairs' : self.__elementsPairs, '_StructureFactorConstraint__weightingScheme': self.__weightingScheme}) def __set_histogram(self): if self.__minimumDistance is None or self.__maximumDistance is None or self.__bin is None: self.__shellCenters = None self.__histogramSize = None self.__shellVolumes = None else: # compute edges if self.engine is not None and self.rmax is None: minHalfBox = np.min( [np.linalg.norm(v)/2. for v in self.engine.basisVectors]) self.__edges = np.arange(self.__minimumDistance,minHalfBox, self.__bin).astype(FLOAT_TYPE) else: self.__edges = np.arange(self.__minimumDistance, self.__maximumDistance+self.__bin, self.__bin).astype(FLOAT_TYPE) # adjust rmin and rmax self.__minimumDistance = self.__edges[0] self.__maximumDistance = self.__edges[-1] # compute shellCenters self.__shellCenters = (self.__edges[0:-1]+self.__edges[1:])/FLOAT_TYPE(2.) # set histogram size self.__histogramSize = INT_TYPE( len(self.__edges)-1 ) # set shell centers and volumes self.__shellVolumes = FLOAT_TYPE(4.0/3.)*PI*((self.__edges[1:])**3 - self.__edges[0:-1]**3) # dump to repository self._dump_to_repository({'_StructureFactorConstraint__minimumDistance': self.__minimumDistance, '_StructureFactorConstraint__maximumDistance': self.__maximumDistance, '_StructureFactorConstraint__shellCenters' : self.__shellCenters, '_StructureFactorConstraint__histogramSize' : self.__histogramSize, '_StructureFactorConstraint__shellVolumes' : self.__shellVolumes}) # reset constraint self.reset_constraint() # reset sq matrix self.__set_Gr_2_Sq_matrix() def _on_collector_reset(self): pass @property def rmin(self): """ Histogram minimum distance. """ return self.__rmin @property def rmax(self): """ Histogram maximum distance. """ return self.__rmax @property def dr(self): """ Histogram bin size.""" return self.__dr @property def bin(self): """ Computed histogram distance bin size.""" return self.__bin @property def minimumDistance(self): """ Computed histogram minimum distance. """ return self.__minimumDistance @property def maximumDistance(self): """ Computed histogram maximum distance. """ return self.__maximumDistance @property def qmin(self): """ Experimental data reciprocal distances minimum. """ return self.__qmin @property def qmax(self): """ Experimental data reciprocal distances maximum. """ return self.__qmax @property def dq(self): """ Experimental data reciprocal distances bin size. """ return self.__experimentalQValues[1]-self.__experimentalQValues[0] @property def experimentalQValues(self): """ Experimental data used q values. """ return self.__experimentalQValues @property def histogramSize(self): """ Histogram size""" return self.__histogramSize @property def shellCenters(self): """ Shells center array""" return self.__shellCenters @property def shellVolumes(self): """ Shells volume array""" return self.__shellVolumes @property def experimentalSF(self): """ Experimental Structure Factor or S(q)""" return self.__experimentalSF @property def elementsPairs(self): """ Elements pairs """ return self.__elementsPairs @property def atomsWeight(self): """Custom atoms weight""" return self.__atomsWeight @property def weighting(self): """ Elements weighting definition. """ return self.__weighting @property def weightingScheme(self): """ Elements weighting scheme. """ return self.__weightingScheme @property def windowFunction(self): """ Convolution window function. """ return self.__windowFunction @property def Gr2SqMatrix(self): """ G(r) to S(q) transformation matrix.""" return self.__Gr2SqMatrix @property def _experimentalX(self): """For internal use only to interface ExperimentalConstraint.get_constraints_properties""" return self.__experimentalQValues @property def _experimentalY(self): """For internal use only to interface ExperimentalConstraint.get_constraints_properties""" return self.__experimentalSF @property def _modelX(self): """For internal use only to interface ExperimentalConstraint.get_constraints_properties""" return self.__experimentalQValues def listen(self, message, argument=None): """ Listens to any message sent from the Broadcaster. :Parameters: #. message (object): Any python object to send to constraint's listen method. #. argument (object): Any type of argument to pass to the listeners. """ if message in ("engine set","update pdb","update molecules indexes","update elements indexes","update names indexes"): self.__set_weighting_scheme() # reset histogram if self.engine is not None: self.__set_histogram() self.reset_constraint() # ADDED 2017-JAN-08 elif message in("update boundary conditions",): self.reset_constraint() def set_rmin(self, rmin): """ Set rmin value. :parameters: #. rmin (None, number): The minimum distance value to compute G(r) histogram. If None is given, rmin is computed as :math:`2 \\pi / Q_{max}`. """ if rmin is None: minimumDistance = FLOAT_TYPE( 2.*PI/self.__qmax ) else: assert is_number(rmin), LOGGER.error("rmin must be None or a number") minimumDistance = FLOAT_TYPE(rmin) if self.__maximumDistance is not None: assert minimumDistance<self.__maximumDistance, LOGGER.error("rmin must be smaller than rmax %s"%self.__maximumDistance) self.__rmin = rmin self.__minimumDistance = minimumDistance # dump to repository self._dump_to_repository({'_StructureFactorConstraint__rmin': self.__rmin, '_StructureFactorConstraint__minimumDistance': self.__minimumDistance}) # reset histogram self.__set_histogram() def set_rmax(self, rmax): """ Set rmax value. :Parameters: #. rmax (None, number): The maximum distance value to compute G(r) histogram. If None is given, rmax is computed as :math:`2 \\pi / dQ`. """ if rmax is None: dq = self.__experimentalQValues[1]-self.__experimentalQValues[0] maximumDistance = FLOAT_TYPE( 2.*PI/dq ) else: assert is_number(rmax), LOGGER.error("rmax must be None or a number") maximumDistance = FLOAT_TYPE(rmax) if self.__minimumDistance is not None: assert maximumDistance>self.__minimumDistance, LOGGER.error("rmax must be bigger than rmin %s"%self.__minimumDistance) self.__rmax = rmax self.__maximumDistance = maximumDistance # dump to repository self._dump_to_repository({'_StructureFactorConstraint__rmax': self.__rmax, '_StructureFactorConstraint__maximumDistance': self.__maximumDistance}) # reset histogram self.__set_histogram() def set_dr(self, dr): """ Set dr value. :Parameters: #. dr (None, number): The distance bin value to compute G(r) histogram. If None is given, bin is computed as :math:`2 \\pi / (Q_{max}-Q_{min})`. """ if dr is None: bin = 2.*PI/self.__qmax rbin = round(bin,1) if rbin>bin: rbin -= 0.1 bin = FLOAT_TYPE( rbin ) else: assert is_number(dr), LOGGER.error("dr must be None or a number") bin = FLOAT_TYPE(dr) self.__dr = dr self.__bin = bin # dump to repository self._dump_to_repository({'_StructureFactorConstraint__dr': self.__dr, '_StructureFactorConstraint__bin': self.__bin}) # reset histogram self.__set_histogram() def set_weighting(self, weighting): """ Set elements weighting. It must be a valid entry of pdbparser atom's database. :Parameters: #. weighting (string): The elements weighting scheme. It must be any atomic attribute (atomicNumber, neutronCohb, neutronIncohb, neutronCohXs, neutronIncohXs, atomicWeight, covalentRadius) defined in pdbparser database. In case of xrays or neutrons experimental weights, one can simply set weighting to 'xrays' or 'neutrons' and the value will be automatically adjusted to respectively 'atomicNumber' and 'neutronCohb'. If attribute values are missing in the pdbparser database, atomic weights must be given in atomsWeight dictionary argument. """ if weighting.lower() in ["xrays","x-rays","xray","x-ray"]: LOGGER.fixed("'%s' weighting is set to atomicNumber"%weighting) weighting = "atomicNumber" elif weighting.lower() in ["neutron","neutrons"]: LOGGER.fixed("'%s' weighting is set to neutronCohb"%weighting) weighting = "neutronCohb" assert is_element_property(weighting),LOGGER.error( "weighting is not a valid pdbparser atoms database entry") assert weighting != "atomicFormFactor", LOGGER.error("atomicFormFactor weighting is not allowed") self.__weighting = weighting # dump to repository self._dump_to_repository({'_StructureFactorConstraint__weighting': self.__weighting}) def set_atoms_weight(self, atomsWeight): """ Custom set atoms weight. This is the way to setting a atoms weights different than the given weighting scheme. :Parameters: #. atomsWeight (None, dict): Atoms weight dictionary where keys are atoms element and values are custom weights. If None is given or partially given, missing elements weighting will be fully set given weighting scheme. """ if atomsWeight is None: AW = {} else: assert isinstance(atomsWeight, dict),LOGGER.error("atomsWeight must be None or a dictionary") AW = {} for k in atomsWeight: assert isinstance(k, basestring),LOGGER.error("atomsWeight keys must be strings") try: val = float(atomsWeight[k]) except: raise LOGGER.error( "atomsWeight values must be numerical") AW[k]=val # set atomsWeight self.__atomsWeight = AW # dump to repository self._dump_to_repository({'_StructureFactorConstraint__atomsWeight': self.__atomsWeight}) def set_window_function(self, windowFunction): """ Set convolution window function. :Parameters: #. windowFunction (None, numpy.ndarray): The window function to convolute with the computed pair distribution function of the system prior to comparing it with the experimental data. In general, the experimental pair distribution function G(r) shows artificial wrinkles, among others the main reason is because G(r) is computed by applying a sine Fourier transform to the experimental structure factor S(q). Therefore window function is used to best imitate the numerical artefacts in the experimental data. """ if windowFunction is not None: assert isinstance(windowFunction, np.ndarray), LOGGER.error("windowFunction must be a numpy.ndarray") assert windowFunction.dtype.type is FLOAT_TYPE, LOGGER.error("windowFunction type must be %s"%FLOAT_TYPE) assert len(windowFunction.shape) == 1, LOGGER.error("windowFunction must be of dimension 1") assert len(windowFunction) <= self.experimentalData.shape[0], LOGGER.error("windowFunction length must be smaller than experimental data") # normalize window function windowFunction /= np.sum(windowFunction) # check window size # set windowFunction self.__windowFunction = windowFunction # dump to repository self._dump_to_repository({'_StructureFactorConstraint__windowFunction': self.__windowFunction}) def set_experimental_data(self, experimentalData): """ Set constraint's experimental data. :Parameters: #. experimentalData (numpy.ndarray, string): The experimental data as numpy.ndarray or string path to load data using numpy.loadtxt function. """ # get experimental data super(StructureFactorConstraint, self).set_experimental_data(experimentalData=experimentalData) # set limits self.set_limits(self.limits) def set_limits(self, limits): """ Set the reciprocal distance limits (qmin, qmax). :Parameters: #. limits (None, tuple, list): Distance limits to bound experimental data and compute histograms. If None is given, the limits will be automatically set to min and max reciprocal distance recorded in experimental data. If given, a tuple of minimum reciprocal distance (qmin) or None and maximum reciprocal distance (qmax) or None should be given. """ self._ExperimentalConstraint__set_limits(limits) # set qvalues self.__experimentalQValues = self.experimentalData[self.limitsIndexStart:self.limitsIndexEnd+1,0].astype(FLOAT_TYPE) self.__experimentalSF = self.experimentalData[self.limitsIndexStart:self.limitsIndexEnd+1,1].astype(FLOAT_TYPE) # set qmin and qmax self.__qmin = self.__experimentalQValues[0] self.__qmax = self.__experimentalQValues[-1] assert self.__qmin>0, LOGGER.error("qmin must be bigger than 0. Experimental null q values are ambigous. Try setting limits.") # dump to repository self._dump_to_repository({'_StructureFactorConstraint__experimentalQValues': self.__experimentalQValues, '_StructureFactorConstraint__experimentalSF' : self.__experimentalSF, '_StructureFactorConstraint__qmin' : self.__qmin, '_StructureFactorConstraint__qmax' : self.__qmax}) # set used dataWeights self._set_used_data_weights(limitsIndexStart=self.limitsIndexStart, limitsIndexEnd=self.limitsIndexEnd) # reset constraint self.reset_constraint() # reset sq matrix self.__set_Gr_2_Sq_matrix() def update_standard_error(self): """ Compute and set constraint's standardError.""" # set standardError totalSQ = self.get_constraint_value()["total_no_window"] self.set_standard_error(self.compute_standard_error(modelData = totalSQ)) def check_experimental_data(self, experimentalData): """ Check whether experimental data is correct. :Parameters: #. experimentalData (object): The experimental data to check. :Returns: #. result (boolean): Whether it is correct or not. #. message (str): Checking message that explains whats's wrong with the given data """ if not isinstance(experimentalData, np.ndarray): return False, "experimentalData must be a numpy.ndarray" if experimentalData.dtype.type is not FLOAT_TYPE: return False, "experimentalData type must be %s"%FLOAT_TYPE if len(experimentalData.shape) !=2: return False, "experimentalData must be of dimension 2" if experimentalData.shape[1] !=2: return False, "experimentalData must have only 2 columns" # check distances order inOrder = (np.array(sorted(experimentalData[:,0]), dtype=FLOAT_TYPE)-experimentalData[:,0])<=PRECISION if not np.all(inOrder): return False, "experimentalData distances are not sorted in order" if experimentalData[0][0]<0: return False, "experimentalData distances min value is found negative" # data format is correct return True, "" def compute_standard_error(self, modelData): """ Compute the standard error (StdErr) as the squared deviations between model computed data and the experimental ones. .. math:: StdErr = \\sum \\limits_{i}^{N} W_{i}(Y(X_{i})-F(X_{i}))^{2} Where:\n :math:`N` is the total number of experimental data points. \n :math:`W_{i}` is the data point weight. It becomes equivalent to 1 when dataWeights is set to None. \n :math:`Y(X_{i})` is the experimental data point :math:`X_{i}`. \n :math:`F(X_{i})` is the computed from the model data :math:`X_{i}`. \n :Parameters: #. modelData (numpy.ndarray): The data to compare with the experimental one and compute the squared deviation. :Returns: #. standardError (number): The calculated constraint's standardError. """ # compute difference diff = self.__experimentalSF-modelData # return standard error if self._usedDataWeights is None: return np.add.reduce((diff)**2) else: return np.add.reduce(self._usedDataWeights*((diff)**2)) def _get_Sq_from_Gr(self, Gr): return np.sum(Gr.reshape((-1,1))*self.__Gr2SqMatrix, axis=0)+1 def _apply_scale_factor(self, Sq, scaleFactor): if scaleFactor != 1: Sq = scaleFactor*(Sq-1) + 1 return Sq def __get_total_Sq(self, data, rho0): """This method is created just to speed up the computation of the total Sq upon fitting.""" Gr = np.zeros(self.__histogramSize, dtype=FLOAT_TYPE) for pair in self.__elementsPairs: # get weighting scheme wij = self.__weightingScheme.get(pair[0]+"-"+pair[1], None) if wij is None: wij = self.__weightingScheme[pair[1]+"-"+pair[0]] # get number of atoms per element ni = self.engine.numberOfAtomsPerElement[pair[0]] nj = self.engine.numberOfAtomsPerElement[pair[1]] # get index of element idi = self.engine.elements.index(pair[0]) idj = self.engine.elements.index(pair[1]) # get Nij if idi == idj: Nij = ni*(ni-1)/2.0 Dij = Nij/self.engine.volume nij = data["intra"][idi,idj,:]+data["inter"][idi,idj,:] Gr += wij*nij/Dij else: Nij = ni*nj Dij = Nij/self.engine.volume nij = data["intra"][idi,idj,:]+data["intra"][idj,idi,:] + data["inter"][idi,idj,:]+data["inter"][idj,idi,:] Gr += wij*nij/Dij # Devide by shells volume Gr /= self.shellVolumes # compute total G(r) #rho0 = (self.engine.numberOfAtoms/self.engine.volume).astype(FLOAT_TYPE) Gr = (FLOAT_TYPE(4.)*PI*self.__shellCenters*rho0)*(Gr-1) # Compute S(q) from G(r) Sq = self._get_Sq_from_Gr(Gr) # Multiply by scale factor self._fittedScaleFactor = self.get_adjusted_scale_factor(self.__experimentalSF, Sq, self._usedDataWeights) # apply scale factor Sq = self._apply_scale_factor(Sq, self._fittedScaleFactor) # apply multiframe prior and weight Sq = self._apply_multiframe_prior(Sq) # convolve total with window function if self.__windowFunction is not None: Sq = np.convolve(Sq, self.__windowFunction, 'same') return Sq def get_adjusted_scale_factor(self, experimentalData, modelData, dataWeights): """Overload to reduce S(q) prior to fitting scale factor. S(q) -> 1 at high q and this will create a wrong scale factor. Overloading can be avoided but it's better to for performance reasons """ SF = self.scaleFactor # check to update scaleFactor if self.adjustScaleFactorFrequency: if not self.engine.accepted%self.adjustScaleFactorFrequency: SF = self.fit_scale_factor(experimentalData-1, modelData-1, dataWeights) return SF def _get_constraint_value(self, data, applyMultiframePrior=True): # http://erice2011.docking.org/upload/Other/Billinge_PDF/03-ReadingMaterial/BillingePDF2011.pdf page 6 #import time #startTime = time.clock() output = {} for pair in self.__elementsPairs: output["sf_intra_%s-%s" % pair] = np.zeros(self.__histogramSize, dtype=FLOAT_TYPE) output["sf_inter_%s-%s" % pair] = np.zeros(self.__histogramSize, dtype=FLOAT_TYPE) output["sf_total_%s-%s" % pair] = np.zeros(self.__histogramSize, dtype=FLOAT_TYPE) gr = np.zeros(self.__histogramSize, dtype=FLOAT_TYPE) for pair in self.__elementsPairs: # get weighting scheme wij = self.__weightingScheme.get(pair[0]+"-"+pair[1], None) if wij is None: wij = self.__weightingScheme[pair[1]+"-"+pair[0]] # get number of atoms per element ni = self.engine.numberOfAtomsPerElement[pair[0]] nj = self.engine.numberOfAtomsPerElement[pair[1]] # get index of element idi = self.engine.elements.index(pair[0]) idj = self.engine.elements.index(pair[1]) # get Nij if idi == idj: Nij = ni*(ni-1)/2.0 output["sf_intra_%s-%s" % pair] += data["intra"][idi,idj,:] output["sf_inter_%s-%s" % pair] += data["inter"][idi,idj,:] else: Nij = ni*nj output["sf_intra_%s-%s" % pair] += data["intra"][idi,idj,:] + data["intra"][idj,idi,:] output["sf_inter_%s-%s" % pair] += data["inter"][idi,idj,:] + data["inter"][idj,idi,:] # compute g(r) nij = output["sf_intra_%s-%s" % pair] + output["sf_inter_%s-%s" % pair] dij = nij/self.__shellVolumes Dij = Nij/self.engine.volume gr += wij*dij/Dij # calculate intensityFactor intensityFactor = (self.engine.volume*wij)/(Nij*self.__shellVolumes) # divide by factor output["sf_intra_%s-%s" % pair] *= intensityFactor output["sf_inter_%s-%s" % pair] *= intensityFactor output["sf_total_%s-%s" % pair] = output["sf_intra_%s-%s" % pair] + output["sf_inter_%s-%s" % pair] # Compute S(q) from G(r) output["sf_intra_%s-%s" % pair] = self._get_Sq_from_Gr(output["sf_intra_%s-%s" % pair]) output["sf_inter_%s-%s" % pair] = self._get_Sq_from_Gr(output["sf_inter_%s-%s" % pair]) output["sf_total_%s-%s" % pair] = self._get_Sq_from_Gr(output["sf_total_%s-%s" % pair]) # compute total G(r) rho0 = (self.engine.numberOfAtoms/self.engine.volume).astype(FLOAT_TYPE) Gr = (FLOAT_TYPE(4.)*PI*self.__shellCenters*rho0) * (gr-1) # Compute S(q) from G(r) Sq = self._get_Sq_from_Gr(Gr) # multiply by scale factor output["total_no_window"] = self._apply_scale_factor(Sq, self._fittedScaleFactor) # apply multiframe prior and weight if applyMultiframePrior: output["total_no_window"] = self._apply_multiframe_prior(output["total_no_window"]) # convolve total with window function if self.__windowFunction is not None: output["total"] = np.convolve(output["total_no_window"], self.__windowFunction, 'same').astype(FLOAT_TYPE) else: output["total"] = output["total_no_window"] return output def get_constraint_value(self, applyMultiframePrior=True): """ Compute all partial Structure Factor (SQs). :Parameters: #. applyMultiframePrior (boolean): Whether to apply subframe weight and prior to the total. This will only have an effect when used frame is a subframe and in case subframe weight and prior is defined. :Returns: #. SQs (dictionary): The SQs dictionnary, where keys are the element wise intra and inter molecular SQs and values are the computed SQs. """ if self.data is None: LOGGER.warn("data must be computed first using 'compute_data' method.") return {} return self._get_constraint_value(self.data, applyMultiframePrior=applyMultiframePrior) def get_constraint_original_value(self): """ Compute all partial Pair Distribution Functions (PDFs). :Returns: #. PDFs (dictionary): The PDFs dictionnary, where keys are the element wise intra and inter molecular PDFs and values are the computed PDFs. """ if self.originalData is None: LOGGER.warn("originalData must be computed first using 'compute_data' method.") return {} return self._get_constraint_value(self.originalData) @reset_if_collected_out_of_date def compute_data(self, update=True): """ Compute constraint's data. :Parameters: #. update (boolean): whether to update constraint data and standard error with new computation. If data is computed and updated by another thread or process while the stochastic engine is running, this might lead to a state alteration of the constraint which will lead to a no additional accepted moves in the run :Returns: #. data (dict): constraint data dictionary #. standardError (float): constraint standard error """ intra,inter = full_pairs_histograms_coords( boxCoords = self.engine.boxCoordinates, basis = self.engine.basisVectors, isPBC = self.engine.isPBC, moleculeIndex = self.engine.moleculesIndex, elementIndex = self.engine.elementsIndex, numberOfElements = self.engine.numberOfElements, minDistance = self.__minimumDistance, maxDistance = self.__maximumDistance, histSize = self.__histogramSize, bin = self.__bin, ncores = self.engine._runtime_ncores ) # create data and compute standard error data = {"intra":intra, "inter":inter} totalSQ = self.__get_total_Sq(data, rho0=self.engine.numberDensity) stdError = self.compute_standard_error(modelData = totalSQ) # update if update: self.set_data(data) self.set_active_atoms_data_before_move(None) self.set_active_atoms_data_after_move(None) self.set_standard_error(stdError) # set original data if self.originalData is None: self._set_original_data(self.data) # return return data, stdError def compute_before_move(self, realIndexes, relativeIndexes): """ Compute constraint before move is executed :Parameters: #. realIndexes (numpy.ndarray): Not used here. #. relativeIndexes (numpy.ndarray): Group atoms relative index the move will be applied to. """ intraM,interM = multiple_pairs_histograms_coords( indexes = relativeIndexes, boxCoords = self.engine.boxCoordinates, basis = self.engine.basisVectors, isPBC = self.engine.isPBC, moleculeIndex = self.engine.moleculesIndex, elementIndex = self.engine.elementsIndex, numberOfElements = self.engine.numberOfElements, minDistance = self.__minimumDistance, maxDistance = self.__maximumDistance, histSize = self.__histogramSize, bin = self.__bin, allAtoms = True, ncores = self.engine._runtime_ncores ) intraF,interF = full_pairs_histograms_coords( boxCoords = self.engine.boxCoordinates[relativeIndexes], basis = self.engine.basisVectors, isPBC = self.engine.isPBC, moleculeIndex = self.engine.moleculesIndex[relativeIndexes], elementIndex = self.engine.elementsIndex[relativeIndexes], numberOfElements = self.engine.numberOfElements, minDistance = self.__minimumDistance, maxDistance = self.__maximumDistance, histSize = self.__histogramSize, bin = self.__bin, ncores = self.engine._runtime_ncores ) self.set_active_atoms_data_before_move( {"intra":intraM-intraF, "inter":interM-interF} ) self.set_active_atoms_data_after_move(None) def compute_after_move(self, realIndexes, relativeIndexes, movedBoxCoordinates): """ Compute constraint after move is executed :Parameters: #. realIndexes (numpy.ndarray): Not used here. #. relativeIndexes (numpy.ndarray): Group atoms relative index the move will be applied to. #. movedBoxCoordinates (numpy.ndarray): The moved atoms new coordinates. """ # change coordinates temporarily boxData = np.array(self.engine.boxCoordinates[relativeIndexes], dtype=FLOAT_TYPE) self.engine.boxCoordinates[relativeIndexes] = movedBoxCoordinates # calculate pair distribution function intraM,interM = multiple_pairs_histograms_coords( indexes = relativeIndexes, boxCoords = self.engine.boxCoordinates, basis = self.engine.basisVectors, isPBC = self.engine.isPBC, moleculeIndex = self.engine.moleculesIndex, elementIndex = self.engine.elementsIndex, numberOfElements = self.engine.numberOfElements, minDistance = self.__minimumDistance, maxDistance = self.__maximumDistance, histSize = self.__histogramSize, bin = self.__bin, allAtoms = True, ncores = self.engine._runtime_ncores ) intraF,interF = full_pairs_histograms_coords( boxCoords = self.engine.boxCoordinates[relativeIndexes], basis = self.engine.basisVectors, isPBC = self.engine.isPBC, moleculeIndex = self.engine.moleculesIndex[relativeIndexes], elementIndex = self.engine.elementsIndex[relativeIndexes], numberOfElements = self.engine.numberOfElements, minDistance = self.__minimumDistance, maxDistance = self.__maximumDistance, histSize = self.__histogramSize, bin = self.__bin, ncores = self.engine._runtime_ncores ) # set active atoms data self.set_active_atoms_data_after_move( {"intra":intraM-intraF, "inter":interM-interF} ) # reset coordinates self.engine.boxCoordinates[relativeIndexes] = boxData # compute standardError after move dataIntra = self.data["intra"]-self.activeAtomsDataBeforeMove["intra"]+self.activeAtomsDataAfterMove["intra"] dataInter = self.data["inter"]-self.activeAtomsDataBeforeMove["inter"]+self.activeAtomsDataAfterMove["inter"] totalSQ = self.__get_total_Sq({"intra":dataIntra, "inter":dataInter}, rho0=self.engine.numberDensity) self.set_after_move_standard_error( self.compute_standard_error(modelData = totalSQ) ) # increment tried self.increment_tried() def accept_move(self, realIndexes, relativeIndexes): """ Accept move :Parameters: #. realIndexes (numpy.ndarray): Not used here. #. relativeIndexes (numpy.ndarray): Not used here. """ dataIntra = self.data["intra"]-self.activeAtomsDataBeforeMove["intra"]+self.activeAtomsDataAfterMove["intra"] dataInter = self.data["inter"]-self.activeAtomsDataBeforeMove["inter"]+self.activeAtomsDataAfterMove["inter"] # change permanently _data self.set_data( {"intra":dataIntra, "inter":dataInter} ) # reset activeAtoms data self.set_active_atoms_data_before_move(None) self.set_active_atoms_data_after_move(None) # update standardError self.set_standard_error( self.afterMoveStandardError ) self.set_after_move_standard_error( None ) # set new scale factor self._set_fitted_scale_factor_value(self._fittedScaleFactor) # increment accepted self.increment_accepted() def reject_move(self, realIndexes, relativeIndexes): """ Reject move :Parameters: #. realIndexes (numpy.ndarray): Not used here. #. relativeIndexes (numpy.ndarray): Not used here. """ # reset activeAtoms data self.set_active_atoms_data_before_move(None) self.set_active_atoms_data_after_move(None) # update standardError self.set_after_move_standard_error( None ) def compute_as_if_amputated(self, realIndex, relativeIndex): """ Compute and return constraint's data and standard error as if given atom is amputated. :Parameters: #. realIndex (numpy.ndarray): Atom's index as a numpy array of a single element. #. relativeIndex (numpy.ndarray): Atom's relative index as a numpy array of a single element. """ # compute data self.compute_before_move(realIndexes=realIndex, relativeIndexes=relativeIndex) dataIntra = self.data["intra"]-self.activeAtomsDataBeforeMove["intra"] dataInter = self.data["inter"]-self.activeAtomsDataBeforeMove["inter"] data = {"intra":dataIntra, "inter":dataInter} # temporarily adjust self.__weightingScheme weightingScheme = self.__weightingScheme relativeIndex = relativeIndex[0] selectedElement = self.engine.allElements[relativeIndex] self.engine.numberOfAtomsPerElement[selectedElement] -= 1 self.__weightingScheme = get_normalized_weighting(numbers=self.engine.numberOfAtomsPerElement, weights=self._elementsWeight ) for k in self.__weightingScheme: self.__weightingScheme[k] = FLOAT_TYPE(self.__weightingScheme[k]) ## END OF ADDED 08 FEB 2017 # compute standard error if not self.engine._RT_moveGenerator.allowFittingScaleFactor: SF = self.adjustScaleFactorFrequency self._set_adjust_scale_factor_frequency(0) rho0 = ((self.engine.numberOfAtoms-1)/self.engine.volume).astype(FLOAT_TYPE) totalSQ = self.__get_total_Sq(data, rho0=rho0) standardError = self.compute_standard_error(modelData = totalSQ) if not self.engine._RT_moveGenerator.allowFittingScaleFactor: self._set_adjust_scale_factor_frequency(SF) # reset activeAtoms data self.set_active_atoms_data_before_move(None) # set amputation self.set_amputation_data( {'data':data, 'weightingScheme':self.__weightingScheme} ) # compute standard error self.set_amputation_standard_error( standardError ) # reset weightingScheme and number of atoms per element self.__weightingScheme = weightingScheme self.engine.numberOfAtomsPerElement[selectedElement] += 1 def accept_amputation(self, realIndex, relativeIndex): """ Accept amputated atom and sets constraints data and standard error accordingly. :Parameters: #. realIndex (numpy.ndarray): Not used here. #. relativeIndex (numpy.ndarray): Not used here. """ #self.set_data( self.amputationData ) ## COMMENTED 08 FEB 2017 self.set_data( self.amputationData['data'] ) self.__weightingScheme = self.amputationData['weightingScheme'] self.set_standard_error( self.amputationStandardError ) self.set_amputation_data( None ) self.set_amputation_standard_error( None ) # set new scale factor self._set_fitted_scale_factor_value(self._fittedScaleFactor) def reject_amputation(self, realIndex, relativeIndex): """ Reject amputated atom and set constraint's data and standard error accordingly. :Parameters: #. realIndex (numpy.ndarray): Not used here. #. relativeIndex (numpy.ndarray): Not used here. """ self.set_amputation_data( None ) self.set_amputation_standard_error( None ) def _on_collector_collect_atom(self, realIndex): pass def _on_collector_release_atom(self, realIndex): pass def _constraint_copy_needs_lut(self): return {'_StructureFactorConstraint__elementsPairs' :'_StructureFactorConstraint__elementsPairs', '_StructureFactorConstraint__histogramSize' :'_StructureFactorConstraint__histogramSize', '_StructureFactorConstraint__weightingScheme' :'_StructureFactorConstraint__weightingScheme', '_StructureFactorConstraint__shellVolumes' :'_StructureFactorConstraint__shellVolumes', '_StructureFactorConstraint__shellCenters' :'_StructureFactorConstraint__shellCenters', '_StructureFactorConstraint__windowFunction' :'_StructureFactorConstraint__windowFunction', '_StructureFactorConstraint__experimentalQValues' :'_StructureFactorConstraint__experimentalQValues', '_StructureFactorConstraint__experimentalSF' :'_StructureFactorConstraint__experimentalSF', '_StructureFactorConstraint__Gr2SqMatrix' :'_StructureFactorConstraint__Gr2SqMatrix', '_StructureFactorConstraint__minimumDistance' :'_StructureFactorConstraint__minimumDistance', '_StructureFactorConstraint__maximumDistance' :'_StructureFactorConstraint__maximumDistance', '_StructureFactorConstraint__bin' :'_StructureFactorConstraint__bin', '_ExperimentalConstraint__scaleFactor' :'_ExperimentalConstraint__scaleFactor', '_ExperimentalConstraint__dataWeights' :'_ExperimentalConstraint__dataWeights', '_ExperimentalConstraint__multiframePrior' :'_ExperimentalConstraint__multiframePrior', '_ExperimentalConstraint__multiframeWeight' :'_ExperimentalConstraint__multiframeWeight', '_ExperimentalConstraint__limits' :'_ExperimentalConstraint__limits', '_ExperimentalConstraint__limitsIndexStart' :'_ExperimentalConstraint__limitsIndexStart', '_ExperimentalConstraint__limitsIndexEnd' :'_ExperimentalConstraint__limitsIndexEnd', '_Constraint__used' :'_Constraint__used', '_Constraint__data' :'_Constraint__data', '_Constraint__state' :'_Constraint__state', '_Constraint__standardError' :'_Constraint__standardError', '_fittedScaleFactor' :'_fittedScaleFactor', '_usedDataWeights' :'_usedDataWeights', '_Engine__state' :'_Engine__state', '_Engine__boxCoordinates' :'_Engine__boxCoordinates', '_Engine__basisVectors' :'_Engine__basisVectors', '_Engine__isPBC' :'_Engine__isPBC', '_Engine__moleculesIndex' :'_Engine__moleculesIndex', '_Engine__elementsIndex' :'_Engine__elementsIndex', '_Engine__numberOfAtomsPerElement' :'_Engine__numberOfAtomsPerElement', '_Engine__elements' :'_Engine__elements', '_Engine__numberDensity' :'_Engine__numberDensity', '_Engine__volume' :'_Engine__volume', '_Engine__realCoordinates' :'_Engine__realCoordinates', '_atomsCollector' :'_atomsCollector', ('engine','_atomsCollector') :'_atomsCollector', } def plot(self, xlabelParams={'xlabel':'$Q(\\AA^{-1})$', 'size':10}, ylabelParams={'ylabel':'$S(Q)$', 'size':10}, **kwargs): """ Alias to ExperimentalConstraint.plot with additional parameters :Additional/Adjusted Parameters: #. xlabelParams (None, dict): modified matplotlib.axes.Axes.set_xlabel parameters. #. ylabelParams (None, dict): modified matplotlib.axes.Axes.set_ylabel parameters. """ return super(StructureFactorConstraint, self).plot(xlabelParams= xlabelParams, ylabelParams= ylabelParams, **kwargs) class ReducedStructureFactorConstraint(StructureFactorConstraint): """ The Reduced Structure Factor that we will also note S(Q) is exactly the same quantity as the Structure Factor but with the slight difference that it is normalized to 0 rather than 1 and therefore :math:`<S(Q)>=0`. The computation of S(Q) is done through a Sine inverse Fourier transform of the computed pair distribution function noted as G(r). .. math:: S(Q) = \\frac{1}{Q} \\int_{0}^{\\infty} G(r) sin(Qr) dr The only reason why the Reduced Structure Factor is implemented, is because many experimental data are treated in this form. And it is just convenient not to manipulate the experimental data every time. """ def _get_Sq_from_Gr(self, Gr): return np.sum(Gr.reshape((-1,1))*self.Gr2SqMatrix, axis=0) def _apply_scale_factor(self, Sq, scaleFactor): if scaleFactor != 1: Sq = scaleFactor*Sq return Sq def get_adjusted_scale_factor(self, experimentalData, modelData, dataWeights): """ dummy overload that does exactly the same thing """ SF = self.scaleFactor # check to update scaleFactor if self.adjustScaleFactorFrequency: if not self.engine.accepted%self.adjustScaleFactorFrequency: SF = self.fit_scale_factor(experimentalData, modelData, dataWeights) return SF def plot(self, xlabelParams={'xlabel':'$Q(\\AA^{-1})$', 'size':10}, ylabelParams={'ylabel':'$S(Q)-1$', 'size':10}, **kwargs): """ Alias to ExperimentalConstraint.plot with additional parameters :Additional/Adjusted Parameters: #. xlabelParams (None, dict): modified matplotlib.axes.Axes.set_xlabel parameters. #. ylabelParams (None, dict): modified matplotlib.axes.Axes.set_ylabel parameters. """ return super(StructureFactorConstraint, self).plot(xlabelParams= xlabelParams, ylabelParams= ylabelParams, **kwargs)
agpl-3.0
q1ang/scikit-learn
examples/ensemble/plot_forest_importances_faces.py
403
1519
""" ================================================= Pixel importances with a parallel forest of trees ================================================= This example shows the use of forests of trees to evaluate the importance of the pixels in an image classification task (faces). The hotter the pixel, the more important. The code below also illustrates how the construction and the computation of the predictions can be parallelized within multiple jobs. """ print(__doc__) from time import time import matplotlib.pyplot as plt from sklearn.datasets import fetch_olivetti_faces from sklearn.ensemble import ExtraTreesClassifier # Number of cores to use to perform parallel fitting of the forest model n_jobs = 1 # Load the faces dataset data = fetch_olivetti_faces() X = data.images.reshape((len(data.images), -1)) y = data.target mask = y < 5 # Limit to 5 classes X = X[mask] y = y[mask] # Build a forest and compute the pixel importances print("Fitting ExtraTreesClassifier on faces data with %d cores..." % n_jobs) t0 = time() forest = ExtraTreesClassifier(n_estimators=1000, max_features=128, n_jobs=n_jobs, random_state=0) forest.fit(X, y) print("done in %0.3fs" % (time() - t0)) importances = forest.feature_importances_ importances = importances.reshape(data.images[0].shape) # Plot pixel importances plt.matshow(importances, cmap=plt.cm.hot) plt.title("Pixel importances with forests of trees") plt.show()
bsd-3-clause
Ttl/scikit-rf
skrf/io/general.py
3
22567
''' .. module:: skrf.io.general ======================================== general (:mod:`skrf.io.general`) ======================================== General io functions for reading and writing skrf objects .. autosummary:: :toctree: generated/ read read_all read_all_networks write write_all save_sesh Writing output to spreadsheet .. autosummary:: :toctree: generated/ network_2_spreadsheet networkset_2_spreadsheet ''' import sys import six.moves.cPickle as pickle from six.moves.cPickle import UnpicklingError import inspect import os import zipfile import warnings import sys from ..util import get_extn, get_fid from ..network import Network from ..frequency import Frequency from ..media import Media from ..networkSet import NetworkSet from ..calibration.calibration import Calibration from copy import copy dir_ = copy(dir) # delayed import: from pandas import DataFrame, Series for ntwk_2_spreadsheet # file extension conventions for skrf objects. global OBJ_EXTN OBJ_EXTN = [ [Frequency, 'freq'], [Network, 'ntwk'], [NetworkSet, 'ns'], [Calibration, 'cal'], [Media, 'med'], [object, 'p'], ] def read(file, *args, **kwargs): ''' Read skrf object[s] from a pickle file Reads a skrf object that is written with :func:`write`, which uses the :mod:`pickle` module. Parameters ------------ file : str or file-object name of file, or a file-object \*args, \*\*kwargs : arguments and keyword arguments passed through to pickle.load Examples ------------- >>> n = rf.Network(f=[1,2,3],s=[1,1,1],z0=50) >>> n.write('my_ntwk.ntwk') >>> n_2 = rf.read('my_ntwk.ntwk') See Also ---------- read : read a skrf object write : write skrf object[s] read_all : read all skrf objects in a directory write_all : write dictionary of skrf objects to a directory Notes ------- if `file` is a file-object it is left open, if it is a filename then a file-object is opened and closed. If file is a file-object and reading fails, then the position is reset back to 0 using seek if possible. ''' fid = get_fid(file, mode='rb') try: obj = pickle.load(fid, *args, **kwargs) except (UnpicklingError, UnicodeDecodeError) as e: # if fid is seekable then reset to beginning of file fid.seek(0) if isinstance(file, str): # we created the fid so close it fid.close() raise if isinstance(file, str): # we created the fid so close it fid.close() return obj def write(file, obj, overwrite = True): ''' Write skrf object[s] to a file This uses the :mod:`pickle` module to write skrf objects to a file. Note that you can write any pickl-able python object. For example, you can write a list or dictionary of :class:`~skrf.network.Network` objects or :class:`~skrf.calibration.calibration.Calibration` objects. This will write out a single file. If you would like to write out a seperate file for each object, use :func:`write_all`. Parameters ------------ file : file or string File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string, an appropriate extension will be appended to the file name if it does not already have an extension. obj : an object, or list/dict of objects object or list/dict of objects to write to disk overwrite : Boolean if file exists, should it be overwritten? Notes ------- If `file` is a str, but doesnt contain a suffix, one is chosen automatically. Here are the extensions ==================================================== =============== skrf object extension ==================================================== =============== :class:`~skrf.frequency.Frequency` '.freq' :class:`~skrf.network.Network` '.ntwk' :class:`~skrf.networkSet.NetworkSet` '.ns' :class:`~skrf.calibration.calibration.Calibration` '.cal' :class:`~skrf.media.media.Media` '.med' other '.p' ==================================================== =============== To make the file written by this method cross-platform, the pickling protocol 2 is used. See :mod:`pickle` for more info. Examples ------------- Convert a touchstone file to a pickled Network, >>> n = rf.Network('my_ntwk.s2p') >>> rf.write('my_ntwk',n) >>> n_red = rf.read('my_ntwk.ntwk') Writing a list of different objects >>> n = rf.Network('my_ntwk.s2p') >>> ns = rf.NetworkSet([n,n,n]) >>> rf.write('out',[n,ns]) >>> n_red = rf.read('out.p') See Also ------------ read : read a skrf object write : write skrf object[s] read_all : read all skrf objects in a directory write_all : write dictionary of skrf objects to a directory skrf.network.Network.write : write method of Network skrf.calibration.calibration.Calibration.write : write method of Calibration ''' if isinstance(file, str): extn = get_extn(file) if extn is None: # if there is not extension add one for obj_extn in OBJ_EXTN: if isinstance(obj, obj_extn[0]): extn = obj_extn[1] break file = file + '.' + extn if os.path.exists(file): if not overwrite: warnings.warn('file exists, and overwrite option is False. Not writing.') return with open(file, 'wb') as fid: pickle.dump(obj, fid, protocol=2) else: fid = file pickle.dump(obj, fid, protocol=2) fid.close() def read_all(dir='.', contains = None, f_unit = None, obj_type=None): ''' Read all skrf objects in a directory Attempts to load all files in `dir`, using :func:`read`. Any file that is not readable by skrf is skipped. Optionally, simple filtering can be achieved through the use of `contains` argument. Parameters -------------- dir : str, optional the directory to load from, default \'.\' contains : str, optional if not None, only files containing this substring will be loaded f_unit : ['hz','khz','mhz','ghz','thz'] for all :class:`~skrf.network.Network` objects, set their frequencies's :attr:`~skrf.frequency.Frequency.f_unit` obj_type : str Name of skrf object types to read (ie 'Network') Returns --------- out : dictionary dictionary containing all loaded skrf objects. keys are the filenames without extensions, and the values are the objects Examples ---------- >>> rf.read_all('skrf/data/') {'delay_short': 1-Port Network: 'delay_short', 75-110 GHz, 201 pts, z0=[ 50.+0.j], 'line': 2-Port Network: 'line', 75-110 GHz, 201 pts, z0=[ 50.+0.j 50.+0.j], 'ntwk1': 2-Port Network: 'ntwk1', 1-10 GHz, 91 pts, z0=[ 50.+0.j 50.+0.j], 'one_port': one port Calibration: 'one_port', 500-750 GHz, 201 pts, 4-ideals/4-measured, ... >>> rf.read_all('skrf/data/', obj_type = 'Network') {'delay_short': 1-Port Network: 'delay_short', 75-110 GHz, 201 pts, z0=[ 50.+0.j], 'line': 2-Port Network: 'line', 75-110 GHz, 201 pts, z0=[ 50.+0.j 50.+0.j], 'ntwk1': 2-Port Network: 'ntwk1', 1-10 GHz, 91 pts, z0=[ 50.+0.j 50.+0.j], ... See Also ---------- read : read a skrf object write : write skrf object[s] read_all : read all skrf objects in a directory write_all : write dictionary of skrf objects to a directory ''' out={} for filename in os.listdir(dir): if contains is not None and contains not in filename: continue fullname = os.path.join(dir,filename) keyname = os.path.splitext(filename)[0] try: out[keyname] = read(fullname) continue except: pass try: out[keyname] = Network(fullname) continue except: pass if f_unit is not None: for keyname in out: try: out[keyname].frequency.unit = f_unit except: pass if obj_type is not None: out = dict([(k, out[k]) for k in out if isinstance(out[k],sys.modules[__name__].__dict__[obj_type])]) return out def read_all_networks(*args, **kwargs): ''' Read all networks in a directory. This is a convenience function. It just calls:: read_all(*args,obj_type='Network', **kwargs) See Also ---------- read_all ''' if 'f_unit' not in kwargs: kwargs.update({'f_unit':'ghz'}) return read_all(*args,obj_type='Network', **kwargs) ran = read_all_networks def write_all(dict_objs, dir='.', *args, **kwargs): ''' Write a dictionary of skrf objects individual files in `dir`. Each object is written to its own file. The filename used for each object is taken from its key in the dictionary. If no extension exists in the key, then one is added. See :func:`write` for a list of extensions. If you would like to write the dictionary to a single output file use :func:`write`. Notes ------- Any object in dict_objs that is pickl-able will be written. Parameters ------------ dict_objs : dict dictionary of skrf objects dir : str directory to save skrf objects into \*args, \*\*kwargs : passed through to :func:`~skrf.io.general.write`. `overwrite` option may be of use. See Also ----------- read : read a skrf object write : write skrf object[s] read_all : read all skrf objects in a directory write_all : write dictionary of skrf objects to a directory Examples ---------- Writing a diction of different skrf objects >>> from skrf.data import line, short >>> d = {'ring_slot':ring_slot, 'one_port_cal':one_port_cal} >>> rf.write_all(d) ''' if not os.path.exists('.'): raise OSError('No such directory: %s'%dir) for k in dict_objs: filename = k obj = dict_objs[k] extn = get_extn(filename) if extn is None: # if there is not extension add one for obj_extn in OBJ_EXTN: if isinstance(obj, obj_extn[0]): extn = obj_extn[1] break filename = filename + '.' + extn try: with open(os.path.join(dir+'/', filename), 'wb') as fid: write(fid, obj,*args, **kwargs) except Exception as inst: print(inst) warnings.warn('couldnt write %s: %s'%(k,str(inst))) pass def save_sesh(dict_objs, file='skrfSesh.p', module='skrf', exclude_prefix='_'): ''' Save all `skrf` objects in the local namespace. This is used to save current workspace in a hurry, by passing it the output of :func:`locals` (see Examples). Note this can be used for other modules as well by passing a different `module` name. Parameters ------------ dict_objs : dict dictionary containing `skrf` objects. See the Example. file : str or file-object, optional the file to save all objects to module : str, optional the module name to grep for. exclude_prefix: str, optional dont save objects which have this as a prefix. See Also ---------- read : read a skrf object write : write skrf object[s] read_all : read all skrf objects in a directory write_all : write dictionary of skrf objects to a directory Examples --------- Write out all skrf objects in current namespace. >>> rf.write_all(locals(), 'mysesh.p') ''' objects = {} print('pickling: ') for k in dict_objs: try: if module in inspect.getmodule(dict_objs[k]).__name__: try: pickle.dumps(dict_objs[k]) if k[0] != '_': objects[k] = dict_objs[k] print(k+', ') finally: pass except(AttributeError, TypeError): pass if len (objects ) == 0: print('nothing') write(file, objects) def load_all_touchstones(dir = '.', contains=None, f_unit=None): ''' Loads all touchtone files in a given dir into a dictionary. Notes ------- Alternatively you can use the :func:`read_all` function. Parameters ----------- dir : string the path contains : string a string the filenames must contain to be loaded. f_unit : ['hz','mhz','ghz'] the frequency unit to assign all loaded networks. see :attr:`frequency.Frequency.unit`. Returns --------- ntwkDict : a dictonary with keys equal to the file name (without a suffix), and values equal to the corresponding ntwk types Examples ---------- >>> ntwk_dict = rf.load_all_touchstones('.', contains ='20v') See Also ----------- read_all ''' ntwkDict = {} for f in os.listdir (dir): if contains is not None and contains not in f: continue fullname = os.path.join(dir,f) keyname,extn = os.path.splitext(f) extn = extn.lower() try: if extn[1]== 's' and extn[-1]=='p': ntwkDict[keyname]=(Network(dir +'/'+f)) if f_unit is not None: ntwkDict[keyname].frequency.unit=f_unit except: pass return ntwkDict def write_dict_of_networks(ntwkDict, dir='.'): ''' Saves a dictionary of networks touchstone files in a given directory The filenames assigned to the touchstone files are taken from the keys of the dictionary. Parameters ----------- ntwkDict : dictionary dictionary of :class:`Network` objects dir : string directory to write touchstone file to ''' warnings.warn('Deprecated. use write_all.', DeprecationWarning) for ntwkKey in ntwkDict: ntwkDict[ntwkKey].write_touchstone(filename = dir+'/'+ntwkKey) def read_csv(filename): ''' Read a 2-port s-parameter data from a csv file. Specifically, this reads a two-port csv file saved from a Rohde Shcwarz ZVA-40, and possibly other network analyzers. It returns into a :class:`Network` object. Parameters ------------ filename : str name of file Returns -------- ntwk : :class:`Network` object the network representing data in the csv file ''' ntwk = Network(name=filename[:-4]) try: data = npy.loadtxt(filename, skiprows=3,delimiter=',',\ usecols=range(9)) s11 = data[:,1] +1j*data[:,2] s21 = data[:,3] +1j*data[:,4] s12 = data[:,5] +1j*data[:,6] s22 = data[:,7] +1j*data[:,8] ntwk.s = npy.array([[s11, s21],[s12,s22]]).transpose().reshape(-1,2,2) except(IndexError): data = npy.loadtxt(filename, skiprows=3,delimiter=',',\ usecols=range(3)) ntwk.s = data[:,1] +1j*data[:,2] ntwk.frequency.f = data[:,0] ntwk.frequency.unit='ghz' return ntwk ## file conversion def statistical_2_touchstone(file_name, new_file_name=None,\ header_string='# GHz S RI R 50.0'): ''' Converts Statistical file to a touchstone file. Converts the file format used by Statistical and other Dylan Williams software to standard touchstone format. Parameters ------------ file_name : string name of file to convert new_file_name : string name of new file to write out (including extension) header_string : string touchstone header written to first beginning of file ''' if new_file_name is None: new_file_name = 'tmp-'+file_name remove_tmp_file = True # This breaks compatibility with python 2.6 and older with file(file_name, 'r') as old_file, open(new_file_name, 'w') as new_file: new_file.write('%s\n'%header_string) for line in old_file: new_file.write(line) if remove_tmp_file is True: os.rename(new_file_name,file_name) def network_2_spreadsheet(ntwk, file_name =None, file_type= 'excel', form='db', *args, **kwargs): ''' Write a Network object to a spreadsheet, for your boss Write the s-parameters of a network to a spreadsheet, in a variety of forms. This functions makes use of the pandas module, which in turn makes use of the xlrd module. These are imported during this function call. For more details about the file-writing functions see the pandas.DataFrom.to_?? functions. Notes ------ The frequency unit used in the spreadsheet is take from `ntwk.frequency.unit` Parameters ----------- ntwk : :class:`~skrf.network.Network` object the network to write file_name : str, None the file_name to write. if None, ntwk.name is used. file_type : ['csv','excel','html'] the type of file to write. See pandas.DataFrame.to_??? functions. form : 'db','ma','ri' format to write data, * db = db, deg * ma = mag, deg * ri = real, imag \*args, \*\*kwargs : passed to pandas.DataFrame.to_??? functions. See Also --------- networkset_2_spreadsheet : writes a spreadsheet for many networks ''' from pandas import DataFrame, Series # delayed because its not a requirement file_extns = {'csv':'csv','excel':'xls','html':'html'} form = form.lower() if form not in ['db','ri','ma']: raise ValueError('`form` must be either `db`,`ma`,`ri`') file_type = file_type.lower() if file_type not in file_extns.keys(): raise ValueError('file_type must be `csv`,`html`,`excel` ') if ntwk.name is None and file_name is None: raise ValueError('Either ntwk must have name or give a file_name') if file_name is None and 'excel_writer' not in kwargs.keys(): file_name = ntwk.name + '.'+file_extns[file_type] d = {} index =ntwk.frequency.f_scaled if form =='db': for m,n in ntwk.port_tuples: d['S%i%i Log Mag(dB)'%(m+1,n+1)] = \ Series(ntwk.s_db[:,m,n], index = index) d[u'S%i%i Phase(deg)'%(m+1,n+1)] = \ Series(ntwk.s_deg[:,m,n], index = index) elif form =='ma': for m,n in ntwk.port_tuples: d['S%i%i Mag(lin)'%(m+1,n+1)] = \ Series(ntwk.s_mag[:,m,n], index = index) d[u'S%i%i Phase(deg)'%(m+1,n+1)] = \ Series(ntwk.s_deg[:,m,n], index = index) elif form =='ri': for m,n in ntwk.port_tuples: d['S%i%i Real'%(m+1,n+1)] = \ Series(ntwk.s_re[:,m,n], index = index) d[u'S%i%i Imag'%(m+1,n+1)] = \ Series(ntwk.s_im[:,m,n], index = index) df = DataFrame(d) df.__getattribute__('to_%s'%file_type)(file_name, index_label='Freq(%s)'%ntwk.frequency.unit, *args, **kwargs) def network_2_dataframe(ntwk, attrs=['s_db'], ports = None): ''' Convert one or more attributes of a network to a pandas DataFrame Parameters -------------- ntwk : :class:`~skrf.network.Network` object the network to write attrs : list Network attributes like ['s_db','s_deg'] ports : list of tuples list of port pairs to write. defaults to ntwk.port_tuples (like [[0,0]]) Returns ---------- df : pandas DataFrame Object ''' from pandas import DataFrame, Series # delayed because its not a requirement d = {} index =ntwk.frequency.f_scaled if ports is None: ports = ntwk.port_tuples for attr in attrs: for m,n in ports: d['%s %i%i'%(attr, m+1,n+1)] = \ Series(ntwk.__getattribute__(attr)[:,m,n], index = index) return DataFrame(d) def networkset_2_spreadsheet(ntwkset, file_name=None, file_type= 'excel', *args, **kwargs): ''' Write a NetworkSet object to a spreadsheet, for your boss Write the s-parameters of a each network in the networkset to a spreadsheet. If the `excel` file_type is used, then each network, is written to its own sheet, with the sheetname taken from the network `name` attribute. This functions makes use of the pandas module, which in turn makes use of the xlrd module. These are imported during this function Notes ------ The frequency unit used in the spreadsheet is take from `ntwk.frequency.unit` Parameters ----------- ntwkset : :class:`~skrf.networkSet.NetworkSet` object the network to write file_name : str, None the file_name to write. if None, ntwk.name is used. file_type : ['csv','excel','html'] the type of file to write. See pandas.DataFrame.to_??? functions. form : 'db','ma','ri' format to write data, * db = db, deg * ma = mag, deg * ri = real, imag \*args, \*\*kwargs : passed to pandas.DataFrame.to_??? functions. See Also --------- networkset_2_spreadsheet : writes a spreadsheet for many networks ''' from pandas import DataFrame, Series, ExcelWriter # delayed because its not a requirement if ntwkset.name is None and file_name is None: raise(ValueError('Either ntwkset must have name or give a file_name')) if file_type == 'excel': writer = ExcelWriter(file_name) [network_2_spreadsheet(k, writer, sheet_name =k.name, *args, **kwargs) for k in ntwkset] writer.save() else: [network_2_spreadsheet(k,*args, **kwargs) for k in ntwkset] # Provide a StringBuffer that let's me work with Python2 strings and Python3 unicode strings without thinking if sys.version_info < (3, 0): import StringIO class StringBuffer(StringIO.StringIO): def __enter__(self): return self def __exit__(self, *args): self.close() else: import io StringBuffer = io.StringIO
bsd-3-clause
MobleyLab/SAMPL6
host_guest/Analysis/Scripts/analyze_sampling.py
1
116143
#!/usr/bin/env python # ============================================================================= # GLOBAL IMPORTS # ============================================================================= import collections import copy import itertools import json import math import os import numpy as np import pandas as pd import scipy as sp import seaborn as sns from matplotlib import pyplot as plt from pkganalysis.stats import mean_confidence_interval from pkganalysis.sampling import (SamplingSubmission, YankSamplingAnalysis, YANK_N_ITERATIONS, DG_KEY, DDG_KEY, export_dictionary) from pkganalysis.submission import (load_submissions) # ============================================================================= # CONSTANTS # ============================================================================= YANK_METHOD_PAPER_NAME = 'OpenMM/HREX' # Paths to input data. SAMPLING_SUBMISSIONS_DIR_PATH = '../SubmissionsDoNotUpload/975/' YANK_ANALYSIS_DIR_PATH = 'YankAnalysis/Sampling/' SAMPLING_ANALYSIS_DIR_PATH = '../SAMPLing/' SAMPLING_DATA_DIR_PATH = os.path.join(SAMPLING_ANALYSIS_DIR_PATH, 'Data') SAMPLING_PLOT_DIR_PATH = os.path.join(SAMPLING_ANALYSIS_DIR_PATH, 'Plots') SAMPLING_PAPER_DIR_PATH = os.path.join(SAMPLING_ANALYSIS_DIR_PATH, 'PaperImages') # All system ids. SYSTEM_IDS = [ 'CB8-G3-0', 'CB8-G3-1', 'CB8-G3-2', 'CB8-G3-3', 'CB8-G3-4', 'OA-G3-0', 'OA-G3-1', 'OA-G3-2', 'OA-G3-3', 'OA-G3-4', 'OA-G6-0', 'OA-G6-1', 'OA-G6-2', 'OA-G6-3', 'OA-G6-4' ] # Kelly's colors for maximum contrast. # "gray95", "gray13", "gold2", "plum4", "darkorange1", "lightskyblue2", "firebrick", "burlywood3", "gray51", "springgreen4", "lightpink2", "deepskyblue4", "lightsalmon2", "mediumpurple4", "orange", "maroon", "yellow3", "brown4", "yellow4", "sienna4", "chocolate", "gray19" KELLY_COLORS = ['#F2F3F4', '#222222', '#F3C300', '#875692', '#F38400', '#A1CAF1', '#BE0032', '#C2B280', '#848482', '#008856', '#E68FAC', '#0067A5', '#F99379', '#604E97', '#F6A600', '#B3446C', '#DCD300', '#882D17', '#8DB600', '#654522', '#E25822', '#2B3D26'] TAB10_COLORS = sns.color_palette('tab10') # Index of Kelly's colors associated to each submission. SUBMISSION_COLORS = { 'AMBER/APR': 'dodgerblue',#KELLY_COLORS[11], 'OpenMM/REVO': 'gold', #KELLY_COLORS[7], 'OpenMM/SOMD': KELLY_COLORS[4], 'GROMACS/EE': 'darkviolet', #KELLY_COLORS[3], 'GROMACS/EE-fullequil': 'hotpink', #KELLY_COLORS[10], YANK_METHOD_PAPER_NAME: '#4ECC41', #'limegreen', #KELLY_COLORS[9], 'GROMACS/NS-DS/SB-long': KELLY_COLORS[6], 'GROMACS/NS-DS/SB': KELLY_COLORS[1], 'GROMACS/NS-Jarz-F': TAB10_COLORS[0], 'GROMACS/NS-Jarz-R': TAB10_COLORS[1], 'GROMACS/NS-Gauss-F': TAB10_COLORS[2], 'GROMACS/NS-Gauss-R': TAB10_COLORS[4], 'NAMD/BAR': 'saddlebrown' } SUBMISSION_LINE_STYLES = { 'AMBER/APR': '--', 'OpenMM/REVO': '-', 'OpenMM/SOMD': '-', 'GROMACS/EE': '-', 'GROMACS/EE-fullequil': '-', YANK_METHOD_PAPER_NAME: '-', 'GROMACS/NS-DS/SB-long': '-', 'GROMACS/NS-DS/SB': '-', 'GROMACS/NS-Jarz-F': '-', 'GROMACS/NS-Jarz-R': '-', 'GROMACS/NS-Gauss-F': '-', 'GROMACS/NS-Gauss-R': '-', 'NAMD/BAR': '--', } N_ENERGY_EVALUATIONS_SCALE = 1e6 # ============================================================================= # UTILITY FUNCTIONS # ============================================================================= def reduce_to_first_significant_digit(quantity, uncertainty): """Truncate a quantity to the first significant digit of its uncertainty.""" first_significant_digit = math.floor(math.log10(abs(uncertainty))) quantity = round(quantity, -first_significant_digit) uncertainty = round(uncertainty, -first_significant_digit) return quantity, uncertainty def load_yank_analysis(): """Load the YANK analysis in a single dataframe.""" yank_free_energies = {} for system_id in SYSTEM_IDS: file_path = os.path.join(YANK_ANALYSIS_DIR_PATH, 'yank-{}.json'.format(system_id)) with open(file_path, 'r') as f: yank_free_energies[system_id] = json.load(f) return yank_free_energies def fit_efficiency(mean_data, find_best_fit=True): """Compute the efficiency by fitting the model and using only the asymptotic data. We fit using the simulation percentage as the independent value because it is less prone to overflowing during fitting. We then return the efficiency in units of (kcal/mol)**2/n_energy_evaluations. """ from scipy.optimize import curve_fit def model(x, log_efficiency): return np.exp(log_efficiency) / x vars = mean_data['std'].values**2 cost = mean_data['Simulation percentage'].values # cost = mean_data['N energy evaluations'].values / 1e7 if find_best_fit: # Find fit with best error up to discarding 70% of calculation. max_discarded = math.floor(0.5*len(cost)) else: # Use all the data. max_discarded = 1 # Fit. fits = [] for n_discarded in range(max_discarded): cost_fit = cost[n_discarded:] vars_fit = vars[n_discarded:] fit = curve_fit(model, cost_fit, vars_fit, p0=[0.0]) fits.append((np.exp(fit[0]), fit[1])) # Find the fit with the minimum error. n_discarded = fits.index(min(fits, key=lambda x: x[1])) # Convert efficiency / simulation_percentage to efficiency / n_energy_evaluations efficiency = fits[n_discarded][0][0] / 100 * mean_data['N energy evaluations'].values[-1] # efficiency = fits[n_discarded][0][0] * 1e7 return efficiency, n_discarded def export_submissions(submissions, reference_free_energies): """Export the submission data to CSV and JSON format.""" for submission in submissions: exported_data = {} # Export data of the 5 independent replicates. for system_id in sorted(submission.data['System ID'].unique()): system_id_data = submission.data[submission.data['System ID'] == system_id] exported_data[system_id] = collections.OrderedDict([ ('DG', system_id_data[DG_KEY].values.tolist()), ('dDG', system_id_data[DDG_KEY].values.tolist()), ('cpu_times', system_id_data['CPU time [s]'].values.tolist()), ('n_energy_evaluations', system_id_data['N energy evaluations'].values.tolist()), ]) # Export data of mean trajectory and confidence intervals. mean_free_energies = submission.mean_free_energies() for system_name in mean_free_energies['System name'].unique(): system_name_data = mean_free_energies[mean_free_energies['System name'] == system_name] # Obtain free energies and bias. free_energies = system_name_data[DG_KEY].values free_energies_ci = system_name_data['$\Delta$G CI'].values reference_diff = free_energies - reference_free_energies.loc[system_name, '$\Delta$G [kcal/mol]'] exported_data[system_name + '-mean'] = collections.OrderedDict([ ('DG', free_energies.tolist()), ('DG_CI', free_energies_ci.tolist()), ('reference_difference', reference_diff.tolist()), ('n_energy_evaluations', system_name_data['N energy evaluations'].values.tolist()), ]) # Export. file_base_path = os.path.join(SAMPLING_DATA_DIR_PATH, submission.receipt_id) export_dictionary(exported_data, file_base_path) # ============================================================================= # PLOTTING FUNCTIONS # ============================================================================= def plot_mean_free_energy(mean_data, ax, x='Simulation percentage', color_mean=None, color_ci=None, zorder=None, start=None, stride=1, scale_n_energy_evaluations=True, plot_ci=True, **plot_kwargs): """Plot mean trajectory with confidence intervals.""" ci_key = '$\Delta$G CI' if start is None: # Discard the first datapoint which are 0.0 (i.e. no estimate). start = np.nonzero(mean_data[DG_KEY].values)[0][0] if x == 'N energy evaluations' and scale_n_energy_evaluations: # Plot in millions of energy evaluations. scale = N_ENERGY_EVALUATIONS_SCALE else: scale = 1 x = mean_data[x].values[start::stride] / scale mean_dg = mean_data[DG_KEY].values[start::stride] sem_dg = mean_data[ci_key].values[start::stride] # Plot mean trajectory confidence intervals. if plot_ci: ax.fill_between(x, mean_dg + sem_dg, mean_dg - sem_dg, alpha=0.15, color=color_ci, zorder=zorder) # Plot the mean free energy trajectory. if zorder is not None: # Push the CI shaded area in the background so that the trajectories are always visible. zorder += 20 ax.plot(x, mean_dg, color=color_mean, alpha=1.0, zorder=zorder, **plot_kwargs) return ax def plot_mean_data(mean_data, axes, color=None, ls=None, label=None, x='N energy evaluations', zorder=None, plot_std=True, plot_bias=True, plot_ci=True): """Plot free energy, variance and bias as a function of the cost in three different axes.""" # Do not plot the part of data without index. first_nonzero_idx = np.nonzero(mean_data[DG_KEY].values)[0][0] # If the x-axis is the number of energy/force evaluations, plot it in units of millions. if x == 'N energy evaluations': scale = N_ENERGY_EVALUATIONS_SCALE else: scale = 1 # Plot the submission mean trajectory with CI. plot_mean_free_energy(mean_data, x=x, ax=axes[0], color_mean=color, color_ci=color, ls=ls, zorder=zorder, start=first_nonzero_idx, label=label, plot_ci=plot_ci) # Plot standard deviation of the trajectories. if plot_std: axes[1].plot(mean_data[x].values[first_nonzero_idx:] / scale, mean_data['std'].values[first_nonzero_idx:], color=color, alpha=0.8, ls=ls, zorder=zorder, label=label) if plot_bias: axes[2].plot(mean_data[x].values[first_nonzero_idx:] / scale, mean_data['bias'].values[first_nonzero_idx:], color=color, alpha=0.8, ls=ls, zorder=zorder, label=label) def align_yaxis(ax1, v1, ax2, v2): """Adjust ax2 ylimit so that v2 in in the twin ax2 is aligned to v1 in ax1. From https://stackoverflow.com/questions/10481990/matplotlib-axis-with-two-scales-shared-origin . """ _, y1 = ax1.transData.transform((0, v1)) _, y2 = ax2.transData.transform((0, v2)) inv = ax2.transData.inverted() _, dy = inv.transform((0, 0)) - inv.transform((0, y1-y2)) miny, maxy = ax2.get_ylim() ax2.set_ylim(miny+dy, maxy+dy) # ============================================================================= # FIGURE 1 - SAMPLING CHALLENGE OVERVIEW # ============================================================================= def plot_example_bias_variance(yank_analysis, type='mixed', cost='generic', max_n_eval_percentage=1.0, mixed_proportion=0.5, model_free_energy=None, plot_experimental_value=False): """Free energy trajectories used to visualize bias and variance on the plots. This is used to illustrate how bias and uncertainty are intended in the paper. Parameters ---------- type : str, optional Can be 'single' (plot only CB8-G3-1), 'all' (plot all system IDs of CB8-G3), 'mean' (plot mean trajectory and uncertainties), and 'mixed (first part is all system IDs and second part is mean trajectory and uncertainties). cost : str, optional Can be 'generic' (no label on x-axis) or 'neval' (x-axis in number of energy evaluations). mixed_proportion : float, optional The proportion of all System IDs and mean trajectories in mixed-type plots. """ # sns.set_context('paper', font_scale=1.6) sns.set_style('white') sns.set_context('paper', font_scale=1.0) # Load the data n_iterations = 40000 cb8_data = yank_analysis.get_free_energies_from_iteration(n_iterations, system_name='CB8-G3', mean_trajectory=False) cb8_data_mean = yank_analysis.get_free_energies_from_iteration(n_iterations, system_name='CB8-G3', mean_trajectory=True) max_n_eval = max(cb8_data_mean['N energy evaluations']) max_n_eval_scaled = int(max_n_eval / N_ENERGY_EVALUATIONS_SCALE) max_displayed_n_eval = next(x for x in cb8_data_mean['N energy evaluations'] if x >= max_n_eval * max_n_eval_percentage) max_displayed_n_eval_scaled = int(max_displayed_n_eval / N_ENERGY_EVALUATIONS_SCALE) # Determine the asymptotic free energy if not given. if model_free_energy is None: model_free_energy = cb8_data_mean[DG_KEY].values[-1] # Scale the number of energy evaluations. cb8_data.loc[:,'N energy evaluations'] /= N_ENERGY_EVALUATIONS_SCALE fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(2.5, 1.8)) if type == 'single': # Plot only CB8-G3-1. cb8_data_1 = cb8_data[cb8_data['System ID'] == 'CB8-G3-1'] sns.lineplot(data=cb8_data_1, x='N energy evaluations', y=DG_KEY, hue='System ID', palette='bright', ax=ax, alpha=0.6) elif type == 'all': # Plot the 5 replicates individual trajectories. sns.lineplot(data=cb8_data, x='N energy evaluations', y=DG_KEY, hue='System ID', palette='bright', ax=ax, alpha=0.6) elif type == 'mean': # Plot the submission mean trajectory with CI. plot_mean_free_energy(cb8_data_mean, x='N energy evaluations', ax=ax, color_mean='black', plot_ci=True, color_ci='black', scale_n_energy_evaluations=True) elif type == 'mixed': # Plot all System IDs for the first half and mean/uncertainty in second half. half_n_eval = max_displayed_n_eval_scaled * mixed_proportion cb8_data_first_half = cb8_data[cb8_data['N energy evaluations'] <= half_n_eval + max_n_eval_scaled / 100] sns.lineplot(data=cb8_data_first_half, x='N energy evaluations', y=DG_KEY, hue='System ID', palette='bright', ax=ax, alpha=0.6) cb8_data_second_half = cb8_data_mean[cb8_data_mean['N energy evaluations'] >= half_n_eval * N_ENERGY_EVALUATIONS_SCALE] plot_mean_free_energy(cb8_data_second_half, x='N energy evaluations', ax=ax, color_mean='black', plot_ci=True, color_ci=(0.3, 0.3, 0.3), scale_n_energy_evaluations=True, ls='--') try: ax.get_legend().remove() except AttributeError: pass # Set limits x_lim = (0, max_displayed_n_eval_scaled) ax.set_xlim(x_lim) y_lim = (-12.5, -10.5) ax.set_ylim(y_lim) # Plot model and experiment indication. Both values are not real data, just an example. model_free_energy = -10.75 final_prediction = cb8_data_mean[cb8_data_mean['N energy evaluations'] == max_displayed_n_eval][DG_KEY].values[0] ax.plot(x_lim, [model_free_energy]*2, color='gray', ls='--') ax.text(x_lim[-1]+(max_n_eval_scaled*max_n_eval_percentage)/100, model_free_energy, r'$\Delta$G$_{\theta}$') ax.text(x_lim[-1]+(max_n_eval_scaled*max_n_eval_percentage)/100, final_prediction - 0.13, r'$\overline{\Delta G}$') # Plot experimental value horizontal line only for generic plot. if plot_experimental_value: experiment_dg = -11.75 plt.plot(x_lim, [experiment_dg]*2, color='black') if cost == 'neval': ax.set_xlabel('N force/energy evaluations') else: ax.set_xlabel('Computational cost', labelpad=-5) ax.set_ylabel('$\Delta$G', labelpad=-5) ax.set_yticklabels([]) ax.set_xticklabels([]) plt.tight_layout(pad=0.1, rect=[0.0, 0.0, 0.90, 1.0]) # Save file. figure_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'Figure 1 - host-guest') os.makedirs(figure_dir_path, exist_ok=True) output_base_path = os.path.join(figure_dir_path, 'example_trajectories') plt.savefig(output_base_path + '.pdf') # ============================================================================= # FIGURE 2 - MEAN ERROR AND RELATIVE EFFICIENCY CARTOON # ============================================================================= def plot_mean_error_cartoon(): """Plot the cartoon used to explain mean error and relative efficiency. This is used as an example to clarify some gotchas with the difinition of efficiency. """ from mpl_toolkits.axes_grid1.inset_locator import inset_axes sns.set_context('paper') sns.set_style('white') def err_decay_func_square(decay_coeff, c): return decay_coeff / np.sqrt(c) def mean_error_square(decay_coeff, c_min, c_max): return 2 * decay_coeff * (np.sqrt(c_max) - np.sqrt(c_min)) / (c_max - c_min) def err_decay_func_B(decay_coeff, c): return decay_coeff / c**(5/6) def mean_error_B(decay_coeff, c_min, c_max): return 6 * decay_coeff * (c_max**(1/6) - c_min**(1/6)) / (c_max - c_min) decay_coeffs = { 'A': 1.0, 'B': 2.5, 'Z': 1.5, } c_ranges = collections.OrderedDict([ ("A'", np.arange(1, 4.5, 0.1)), ("A''", np.arange(3, 6, 0.1)), ("B", np.arange(2, 6.5, 0.1)), ("Z", np.arange(1, 6.5, 0.1)), ]) # Determine colors colors. colors = {m: 'C'+str(i) for i, m in enumerate(sorted(c_ranges))} # Plot the error trajectories. fig, ax = plt.subplots(figsize=(3.5, 2.6)) # method_names = ["B", "Z", "A'", "A''"] method_names = ["Z", "A'", "A''"] for method_name in method_names: color = colors[method_name] c_range = c_ranges[method_name] decay_coeff = decay_coeffs[method_name[0]] if method_name == 'B': err_decay_func = err_decay_func_B else: err_decay_func = err_decay_func_square err = err_decay_func(decay_coeff, c_range) # Plot error area. ax.plot(c_range, err, color=color, label=method_name, zorder=1) ax.fill_between(c_range, err, 0, color=color, alpha=0.5, zorder=0) # Add method label. c_method_label_idx = int(len(c_range) / 8) ax.text(c_range[c_method_label_idx], err[c_method_label_idx]+0.01, method_name, fontsize=12) if method_name[0] == 'A': # Plot mean error. c_min, c_max = min(c_range), max(c_range) mean_err = mean_error_square(decay_coeff, c_min, c_max) # Start mean error horizontal line from the error curve. c_mean = (decay_coeff / mean_err)**2 ax.plot([0, c_mean], [mean_err, mean_err], color='black', ls='--', alpha=0.8, zorder=1) # Add label mean error. # ax.text(1.05, mean_err+0.025, '$\mathbb{E}[RMSE_{' + method_name + '}]$', fontsize=9) ax.text(-0.3, mean_err+0.025, '$\mathbb{E}[RMSE_{' + method_name + '}]$', fontsize=9) # Add c_min/max labels. ax.text(c_min-0.4, -0.1, 'c$_{min,' + method_name + '}$', fontsize=9) ax.text(c_max-0.4, -0.1, 'c$_{max,' + method_name + '}$', fontsize=9) # Configure axes. ax.set_xlim(1, 6.4) ax.set_ylim(0, 2) ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_ylabel('$RMSE(\Delta G)$') ax.set_xlabel('computational cost') # Pull axes labels closest to axes. ax.tick_params(axis='x', which='major', pad=2.0) ax.yaxis.set_label_coords(0.0, 0.65) # Plot the relative efficiencies in an inset plot. ax_ins = inset_axes(ax, width='100%', height='100%', bbox_to_anchor=[145, 115, 90, 50]) # Compute relative efficiencies with respect to Z. relative_efficiencies = collections.OrderedDict() for method_name in [name for name in method_names if name != 'Z']: c_min, c_max = min(c_ranges[method_name]), max(c_ranges[method_name]) if method_name == 'B': mean_error_func = mean_error_B else: mean_error_func = mean_error_square mean_err_method = mean_error_func(decay_coeffs[method_name[0]], c_min, c_max) mean_err_Z = mean_error_square(decay_coeffs['Z'], c_min, c_max) relative_efficiencies[method_name] = -np.log(mean_err_method/mean_err_Z) # Plot horizontal bar plot with all efficiencies. labels, rel_effs = zip(*relative_efficiencies.items()) bar_colors = [colors[m] for m in labels] labels = [l + '/Z' for l in labels] # labels = ['$e_{err,' + str(l) + '/Z}$' for l in labels] ax_ins.barh(y=labels, width=rel_effs, color=bar_colors, alpha=0.85) ax_ins.set_title('relative efficiency', pad=2.5) # plt.tight_layout(rect=[0.0, 0.0, 1.0, 1.0]) plt.tight_layout(rect=[0.1, 0.0, 1.0, 1.0]) # Pull axes labels closest to axes. ax_ins.set_xticks([0.0]) ax_ins.grid(axis='x') ax_ins.tick_params(axis='x', which='major', pad=0.0) ax_ins.tick_params(axis='y', which='major', pad=0.0) output_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'Figure2-efficiency_cartoon') os.makedirs(output_dir_path, exist_ok=True) plt.savefig(os.path.join(output_dir_path, 'error_trajectories.pdf')) # ============================================================================= # FIGURE 3 - FREE ENERGY TRAJECTORIES # ============================================================================= def plot_submissions_trajectory(submissions, yank_analysis, axes, y_limits=None, plot_std=True, plot_bias=True, plot_bias_to_reference=False, system_names=None): """Plot free energy trajectories, std, and bias of the given submissions.""" if system_names is None: system_names = ['CB8-G3', 'OA-G3', 'OA-G6'] n_systems = len(system_names) max_n_energy_evaluations = {system_name: 0 for system_name in system_names} min_n_energy_evaluations = {system_name: np.inf for system_name in system_names} # Handle default arguments. if y_limits is None: # 3 by 3 matrix of y limits for the plots. y_limits = [[None for _ in range(n_systems)] for _ in range(n_systems)] # We need a 2D array of axes for the code to work even if we're not plotting std or bias. try: axes_shape = len(axes.shape) except AttributeError: axes = np.array([[axes]]) else: if axes_shape == 1: axes = np.array([axes]) # Build a dictionary mapping submissions and system names to their mean data. all_mean_data = {} for submission in submissions: # We always want to print in order all_mean_data[submission.paper_name] = {} mean_free_energies = submission.mean_free_energies() for system_name in system_names: # CB8-G3 calculations for GROMACS/EE did not converge. if submission.name == 'Expanded-ensemble/MBAR' and system_name == 'CB8-G3': continue # Add mean free energies for this system. system_mean_data = mean_free_energies[mean_free_energies['System name'] == system_name] n_energy_evaluations = system_mean_data['N energy evaluations'].values[-1] all_mean_data[submission.paper_name][system_name] = system_mean_data # Keep track of the maximum and minimum number of energy evaluations, # which will be used to determine how to truncate the plotted reference # data and determine the zorder of the trajectories respectively. max_n_energy_evaluations[system_name] = max(max_n_energy_evaluations[system_name], n_energy_evaluations) min_n_energy_evaluations[system_name] = min(min_n_energy_evaluations[system_name], n_energy_evaluations) # Add also reference YANK calculations if provided. if yank_analysis is not None: all_mean_data[YANK_METHOD_PAPER_NAME] = {} for system_name in system_names: system_mean_data = yank_analysis.get_free_energies_from_energy_evaluations( max_n_energy_evaluations[system_name], system_name=system_name, mean_trajectory=True) all_mean_data[YANK_METHOD_PAPER_NAME][system_name] = system_mean_data # Create a table mapping submissions and system name to the zorder used # to plot the free energy trajectory so that smaller shaded areas are on # top of bigger ones. # First find the average CI for all methods up to min_n_energy_evaluations. methods_cis = {name: {} for name in system_names} for method_name, method_mean_data in all_mean_data.items(): for system_name, system_mean_data in method_mean_data.items(): # Find index of all energy evaluations < min_n_energy_evaluations. n_energy_evaluations = system_mean_data['N energy evaluations'].values last_idx = np.searchsorted(n_energy_evaluations, min_n_energy_evaluations[system_name], side='right') cis = system_mean_data['$\Delta$G CI'].values[:last_idx] methods_cis[system_name][method_name] = np.mean(cis) # For each system, order methods from smallest CI (plot on top) to greatest CI (background). zorders = {name: {} for name in system_names} for system_name, system_cis in methods_cis.items(): ordered_methods = sorted(system_cis.keys(), key=lambda method_name: system_cis[method_name]) for zorder, method_name in enumerate(ordered_methods): zorders[system_name][method_name] = zorder # The columns are in order CB8-G3, OA-G3, and OA-G6. system_columns = {'CB8-G3': 0, 'OA-G3': 1, 'OA-G6': 2} # Plot submissions in alphabetical order to order he legend labels. for method_name in sorted(all_mean_data.keys()): submission_mean_data = all_mean_data[method_name] submission_color = SUBMISSION_COLORS[method_name] submission_ls = SUBMISSION_LINE_STYLES[method_name] # Plot free energy trajectories. for system_name, mean_data in submission_mean_data.items(): ax_idx = system_columns[system_name] # The OA prediction of the NS short protocol are the same of the long protocol submission file. if method_name == 'GROMACS/NS-DS/SB-long' and system_name != 'CB8-G3': # Just add the label. axes[0][ax_idx].plot([], color=submission_color, ls=submission_ls, label=method_name) continue # Update maximum number of energy evaluations. n_energy_evaluations = mean_data['N energy evaluations'].values[-1] max_n_energy_evaluations[system_name] = max(max_n_energy_evaluations[system_name], n_energy_evaluations) # Determine zorder and plot. zorder = zorders[system_name][method_name] plot_mean_data(mean_data, axes[:,ax_idx], color=submission_color, ls=submission_ls, zorder=zorder, label=method_name, plot_std=plot_std, plot_bias=plot_bias) # Fix labels. axes[0][0].set_ylabel('$\Delta$G [kcal/mol]') if plot_std: axes[1][0].set_ylabel('std($\Delta$G) [kcal/mol]') if plot_bias: axes[2][0].set_ylabel('bias [kcal/mol]') central_column_idx = int(len(axes[0])/2) axes[-1][central_column_idx].set_xlabel('number of energy/force evaluations [10$^6$]') # Fix axes limits. for ax_idx, system_name in enumerate(system_names): for row_idx in range(len(axes)): ax = axes[row_idx][ax_idx] # Set the x-axis limits. ax.set_xlim((0, max_n_energy_evaluations[system_name]/N_ENERGY_EVALUATIONS_SCALE)) # Keep the x-axis label only at the bottom row. if row_idx != len(axes)-1: ax.xaxis.set_ticklabels([]) y_lim = y_limits[row_idx][ax_idx] if y_lim is not None: ax.set_ylim(y_lim) # Set the system name in the title. axes[0][ax_idx].set_title(system_name) # Create a bias axis AFTER the ylim has been set. if yank_analysis is not None and plot_bias_to_reference: for ax_idx, (system_name, ax) in enumerate(zip(system_names, axes[0])): yank_full_mean_data = yank_analysis.get_system_free_energies(system_name, mean_trajectory=True) ref_free_energy = yank_full_mean_data[DG_KEY].values[-1] with sns.axes_style('white'): ax2 = ax.twinx() # Plot a vertical line to fix the scale. vertical_line = np.linspace(*ax.get_ylim()) - ref_free_energy ax2.plot([50] * len(vertical_line), vertical_line, alpha=0.0001) ax2.grid(alpha=0.5, linestyle='dashed', zorder=0) # We add the bias y-label only on the rightmost Axis. if ax_idx == n_systems - 1: ax2.set_ylabel('Bias to reference [kcal/mol]') # Set the 0 of the twin axis to the YANK reference free energy. align_yaxis(ax, ref_free_energy, ax2, 0.0) def plot_all_entries_trajectory(submissions, yank_analysis, zoomed=False): """Plot free energy trajectories, std, and bias of the challenge entries.""" sns.set_style('whitegrid') sns.set_context('paper') # Create a figure with 3 columns (one for each system) and 2 rows. # The first row contains the free energy trajectory and CI, the second # a plot of the estimator variance, and the third the bias to the # asymptotic value. if zoomed: figsize = (7.25, 7.0) # Without REVO else: figsize = (7.25, 7.0) # With REVO fig, axes = plt.subplots(nrows=3, ncols=3, figsize=figsize) # Optionally, remove REVO. if zoomed: submissions = [s for s in submissions if s.name not in ['WExploreRateRatio']] if zoomed: # Y-axis limits when REVO calculations are excluded. y_limits = [ [(-15, -10), (-9, -4), (-9, -4)], [(0, 2), (0, 0.8), (0, 0.8)], [(-3, 1), (-0.6, 0.6), (-0.6, 0.6)], ] else: # Y-axis limits when REVO calculations are included. y_limits = [ [(-17, -9), (-13, -5), (-13, -5)], [(0, 2), (0, 1.75), (0, 1.75)], [(-4, 4), (-0.6, 0.6), (-0.6, 0.6)], ] plot_submissions_trajectory(submissions, yank_analysis, axes, y_limits=y_limits) # Show/save figure. if zoomed: plt.tight_layout(h_pad=0.2, rect=[0.0, 0.00, 1.0, 0.92], w_pad=0.0) # Without REVO else: plt.tight_layout(h_pad=0.2, rect=[0.0, 0.00, 1.0, 0.92]) # With REVO # Plot legend. if zoomed: # bbox_to_anchor = (2.52, 1.55) # Without REVO. bbox_to_anchor = (2.4, 1.48) else: bbox_to_anchor = (2.4, 1.48) # With REVO. axes[0][1].legend(loc='upper right', bbox_to_anchor=bbox_to_anchor, fancybox=True, ncol=4) plt.subplots_adjust(wspace=0.35) # plt.show() if zoomed: file_name = 'Figure3-free_energy_trajectories_zoomed' else: file_name = 'Figure3-free_energy_trajectories' figure_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'Figure3-free_energy_trajectories') os.makedirs(figure_dir_path, exist_ok=True) output_base_path = os.path.join(figure_dir_path, file_name) plt.savefig(output_base_path + '.pdf') # plt.savefig(output_base_path + '.png', dpi=500) # ============================================================================= # FIGURE 4 - NONEQUILIBRIUM SWITCHING ESTIMATOR COMPARISON # ============================================================================= def plot_all_nonequilibrium_switching(submissions): """Plot free energy trajectories, std, and bias of the nonequilibrium-switching calculations.""" # Create a figure with 3 columns (one for each system) and 2 rows. # The first row contains the free energy trajectory and CI, the second # a plot of the estimator variance, and the third the bias to the # asymptotic value. figsize = (7.25, 3.5) fig, axes = plt.subplots(nrows=1, ncols=3, figsize=figsize) # Select nonequilibrium-switching calculations with estimators. submissions = [s for s in submissions if 'NS' in s.paper_name] # Y-axis limits. y_limits = [ [(-20, 5), (-40, 0), (-40, 0)] ] plot_submissions_trajectory(submissions, yank_analysis=None, axes=axes, y_limits=y_limits, plot_std=False, plot_bias=False) # Show/save figure. plt.tight_layout(pad=0.0, rect=[0.0, 0.00, 1.0, 0.85]) # Plot legend. legend = axes[0].legend(loc='upper left', bbox_to_anchor=(0.6, 1.3), fancybox=True, ncol=3) # Change legend labels to refer to estimator used rather than overall method ID. legend_labels_map = { 'GROMACS/NS-DS/SB-long': 'BAR-long', 'GROMACS/NS-DS/SB': 'BAR', 'GROMACS/NS-Jarz-F': 'Jarzynski-Forward', 'GROMACS/NS-Jarz-R': 'Jarzynski-Reverse', 'GROMACS/NS-Gauss-F': 'Gaussian-Forward', 'GROMACS/NS-Gauss-R': 'Gaussian-Reverse', } for text in legend.get_texts(): text.set_text(legend_labels_map[text.get_text()]) plt.subplots_adjust(wspace=0.35) # plt.show() figure_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'Figure4-nonequilibrium_comparison') os.makedirs(figure_dir_path, exist_ok=True) output_base_path = os.path.join(figure_dir_path, 'Figure4-nonequilibrium_comparison') plt.savefig(output_base_path + '.pdf') # plt.savefig(output_base_path + '.png', dpi=500) # ============================================================================= # FIGURE 5 - BAROSTAT AND RESTRAINT # ============================================================================= # Directories containing the volume information of YANK and GROMACS/EE. BAROSTAT_DATA_DIR_PATH = os.path.join('..', 'SAMPLing', 'Data', 'BarostatData') YANK_VOLUMES_DIR_PATH = os.path.join(BAROSTAT_DATA_DIR_PATH, 'YankVolumes') EE_VOLUMES_DIR_PATH = os.path.join(BAROSTAT_DATA_DIR_PATH, 'EEVolumes') def plot_volume_distributions(axes, plot_predicted=False): """Plot the volume distributions obtained with Monte Carlo and Berendsen barostat.""" import scipy.stats import scipy.integrate from simtk import unit # Load data. mc_volumes = collections.OrderedDict([ (1, np.load(os.path.join(YANK_VOLUMES_DIR_PATH, 'volumes_pressure100.npy'))), (100, np.load(os.path.join(YANK_VOLUMES_DIR_PATH, 'volumes_pressure10000.npy'))), ]) mc_volumes_hrex = collections.OrderedDict([ (1, np.load(os.path.join(YANK_VOLUMES_DIR_PATH, 'hrex_state_volumes_state0.npy'))), (58, np.load(os.path.join(YANK_VOLUMES_DIR_PATH, 'hrex_state_volumes_state58.npy'))), ]) b_volumes = collections.OrderedDict([ (1, np.load(os.path.join(EE_VOLUMES_DIR_PATH, '1atm_vanilla.npy'))), (100, np.load(os.path.join(EE_VOLUMES_DIR_PATH, '100atm_vanilla.npy'))), ]) b_volumes_ee = collections.OrderedDict([ (1, np.load(os.path.join(EE_VOLUMES_DIR_PATH, '1atm_expanded.npy'))), (100, np.load(os.path.join(EE_VOLUMES_DIR_PATH, '100atm_expanded.npy'))), ]) # Print some statistics for each distribution. for volume_trajectories, label in [(mc_volumes, 'MC-MD '), (mc_volumes_hrex, 'MC-HREX'), (b_volumes, 'BB-MD '), (b_volumes_ee, 'BB-EE ')]: for pressure, trajectory in volume_trajectories.items(): n = len(trajectory) t_stat = 2.326 # 98% CI mean = np.mean(trajectory) sem = scipy.stats.sem(trajectory) mean_ci = t_stat * sem var = np.var(trajectory, ddof=1) # Standard error of variance if volume is gaussianly distributed sev = var * np.sqrt(2 / (n-1)) var_ci = t_stat * sev skew = scipy.stats.skew(trajectory) # Standard error of skewness if volume is gaussianly distributed ses = np.sqrt( 6*n*(n-1) / ((n-2)*(n+1)*(n+3)) ) skew_ci = t_stat * ses print('{}-{} (n={}): mean={:.3f} +- {:.3f}nm^3\t\tvar={:.3f} +- {:.3f}\tskew={:.3f} +- {:.3f}'.format( pressure, label, n, mean, mean_ci, var, var_ci, skew, skew_ci)) # Plot the 1atm vs 100atm comparison. barostats = ['B', 'MC'] for ax, volume_trajectories, barostat in zip(axes, [b_volumes, mc_volumes], barostats): barostat += ',MD' barostat = 'MD' for pressure, trajectory in volume_trajectories.items(): label = '$\\rho_{{\mathrm{{{}}}}}$(V|{}atm)'.format(barostat, pressure) ax = sns.distplot(trajectory, label=label, hist=False, ax=ax) if plot_predicted: # Plot predicted distribution. beta = 1.0 / (unit.BOLTZMANN_CONSTANT_kB * 298.15*unit.kelvin) p1 = 1.0 * unit.atmosphere p2 = 100.0 * unit.atmosphere volumes = np.linspace(78.0, 82.0, num=200) fit = scipy.stats.norm # Fit the original distribution. original_pressure, new_pressure = list(volume_trajectories.keys()) original_trajectory = list(volume_trajectories.values())[0] fit_parameters = fit.fit(original_trajectory) # Find normalizing constant predicted distribution. predicted_distribution = lambda v: np.exp(-beta*(p2 - p1)*v*unit.nanometer**3) * fit.pdf([v], *fit_parameters) normalizing_factor = scipy.integrate.quad(predicted_distribution, volumes[0], volumes[-1])[0] predicted = np.array([predicted_distribution(v) / normalizing_factor for v in volumes]) # Set the scale. label = '$\\rho_{{\mathrm{{{}}}}}$(V|{}atm)$\cdot e^{{\\beta ({}atm - {}atm) V}}$'.format(barostat, original_pressure, new_pressure, original_pressure) ax.plot(volumes, predicted, ls='--', label=label) # ax.plot(volumes, [fit.pdf([v], *fit_parameters) for v in volumes], label='original') # Plot comparison MD vs expanded ensemble and HREX volumes. for ax_idx, (trajectory, label) in enumerate([ (b_volumes_ee[1], 'B,EE'), (mc_volumes_hrex[1], 'MC,HREX') ]): label = 'E' ax = axes[ax_idx] label = '$\\rho_{{\mathrm{{{}}}}}$(V|1atm)'.format(label) sns.distplot(trajectory, label=label, hist=False, ax=ax) # Set titles and configure axes. axes[0].set_title('Berendsen barostat volume distribution', pad=2.0) axes[1].set_title('Monte Carlo barostat volume distribution', pad=2.0) for ax_idx in range(len(axes)): axes[ax_idx].set_xlim((78.8, 81.2)) axes[ax_idx].set_ylim((0.0, 6.0)) axes[ax_idx].set_ylabel('density') axes[0].set_xlabel('', labelpad=0.3) axes[1].set_xlabel('Volume [nm^3]', labelpad=0.3) # Create single legend for both MC and B barostat axes. bbox_to_anchor = (-0.1, -0.15) axes[0].legend(fontsize='xx-small', loc='upper left', bbox_to_anchor=bbox_to_anchor, ncol=4, fancybox=True, labelspacing=0.7, handletextpad=0.4, columnspacing=1.1,) # axes[0].get_legend().remove() axes[1].get_legend().remove() plt.tight_layout(pad=0, rect=[0.0, 0.0, 1.0, 1.0]) # Directory with the restraint information. RESTRAINT_DATA_DIR_PATH = os.path.join('YankAnalysis', 'RestraintAnalysis') # The state index of the discharged state with LJ interactions intact. DISCHARGED_STATE = { 'CB8-G3': 25, 'OA-G3': 32, 'OA-G6': 29 } # The final free energy predictions without restraint unbiasing. BIASED_FREE_ENERGIES = { 'CB8-G3-0': -10.643, 'CB8-G3-1': -10.533, 'CB8-G3-2': -10.463, 'CB8-G3-3': None, # TODO: Run the biased analysis 'CB8-G3-4': -10.324, 'OA-G3-0': -5.476, 'OA-G3-1': -5.588, 'OA-G3-2': -5.486, 'OA-G3-3': -5.510, 'OA-G3-4': -5.497, 'OA-G6-0': -5.669, 'OA-G6-1': -5.665, 'OA-G6-2': -5.767, 'OA-G6-3': -5.737, 'OA-G6-4': -5.788, } def plot_restraint_distance_distribution(system_id, ax, kde=True, iteration_set=None): """Plot the distribution of restraint distances at bound, discharged, and decoupled states. Return the 99.99-percentile restraint radius that was used as a cutoff during analysis. """ n_iterations = YANK_N_ITERATIONS + 1 # Count also iteration 0. system_name = system_id[:-2] discharged_state_idx = DISCHARGED_STATE[system_name] # Load all distances cached during the analysis. cache_dir_path = os.path.join('pkganalysis', 'cache', system_id.replace('-', '')) cached_distances_file_path = os.path.join(cache_dir_path, 'restraint_distances_cache.npz') distances_kn = np.load(cached_distances_file_path)['arr_0'] # Distances are in nm but we plot in Angstrom. distances_kn *= 10 n_states = int(len(distances_kn) / n_iterations) # Use the same colors that are used in the water analysis figures. color_palette = sns.color_palette('viridis', n_colors=n_states) color_palette = [color_palette[i] for i in (0, discharged_state_idx, -1)] # Isolate distances in the bound, discharged (only LJ), and decoupled state. distances_kn_bound = distances_kn[:n_iterations] distances_kn_discharged = distances_kn[(discharged_state_idx-1)*n_iterations:discharged_state_idx*n_iterations] distances_kn_decoupled = distances_kn[(n_states-1)*n_iterations:] # Filter iterations. if iteration_set is not None: distances_kn_bound = distances_kn_bound[iteration_set] distances_kn_discharged = distances_kn_discharged[iteration_set] distances_kn_decoupled = distances_kn_decoupled[iteration_set] assert len(distances_kn_bound) == len(distances_kn_decoupled) # Plot the distributions. # sns.distplot(distances_kn, ax=ax, kde=True, label='all states') sns.distplot(distances_kn_bound, ax=ax, kde=kde, label='bound', color=color_palette[0]) sns.distplot(distances_kn_discharged, ax=ax, kde=kde, label='discharged', color=color_palette[1]) sns.distplot(distances_kn_decoupled, ax=ax, kde=kde, label='decoupled', color=color_palette[2]) # Plot the threshold used for analysis, computed as the # 99.99-percentile of all distances in the bound state. distance_cutoff = np.percentile(a=distances_kn_bound, q=99.99) limits = ax.get_ylim() ax.plot([distance_cutoff for _ in range(100)], np.linspace(limits[0], limits[1]/2, num=100), color='black') return distance_cutoff def plot_restraint_profile(system_id, ax, restraint_cutoff): """Plot the free energy as a function of the restraint cutoff.""" # Load the free energy profile for this system. restraint_profile_file_path = os.path.join(RESTRAINT_DATA_DIR_PATH, system_id.replace('-', '') + '.json') with open(restraint_profile_file_path, 'r') as f: free_energies_profile = json.load(f) # Reorder the free energies by increasing cutoff and convert str keys to floats. free_energies_profile = [(float(d), f) for d, f in free_energies_profile.items()] free_energies_profile = sorted(free_energies_profile, key=lambda x: x[0]) distance_cutoffs, free_energies = list(zip(*free_energies_profile)) f, df = list(zip(*free_energies)) # Convert string to floats. distance_cutoffs = [float(c) for c in distance_cutoffs] # Plot profile. ax.errorbar(x=distance_cutoffs, y=f, yerr=df, label='after reweighting') # Plot biased free energy biased_f = BIASED_FREE_ENERGIES[system_id] x = np.linspace(*ax.get_xlim()) ax.plot(x, [biased_f for _ in x], label='before reweighting') # Plot restraint distance cutoff. limits = ax.get_ylim() x = [restraint_cutoff for _ in range(100)] y = np.linspace(limits[0], limits[1], num=100) ax.plot(x, y, color='black') def plot_restraint_analysis(system_id, axes): """Plot distribution of restraint distances and free energy profile on two axes.""" # Histograms of restraint distances/energies. ax = axes[0] kde = True restraint_cutoff = plot_restraint_distance_distribution(system_id, ax, kde=kde) # Set restraint distance distribution lables and titles. ax.set_title('Restrained ligand-receptor distance', pad=2.0) if kde is False: ax.set_ylabel('Number of samples') else: ax.set_ylabel('density') ax.legend(loc='upper right', fontsize='x-small') ax.set_xlabel('Restrained distance [$\mathrm{\AA}$]', labelpad=0.3) # Free energy as a function of restraint distance. ax = axes[1] ax.set_title('$\Delta G$ as a function of restraint radius cutoff', pad=2.0 ) plot_restraint_profile(system_id, ax, restraint_cutoff) # Labels and legend. ax.set_xlabel('Restraint radius cutoff [$\mathrm{\AA}$]', labelpad=0.3) ax.set_ylabel('$\Delta G$ [kcal/mol]') ax.legend(fontsize='x-small') def plot_restraint_and_barostat_analysis(): """Plot the Figure showing info for the restraint and barostat analysis.""" import seaborn as sns from matplotlib import pyplot as plt sns.set_style('whitegrid') sns.set_context('paper', font_scale=1.0) # Create two columns, each of them share the x-axis. fig = plt.figure(figsize=(7.25, 4)) # Restraint distribution axes. ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(223, sharex=ax1) barostat_axes = [ax1, ax2] # Volume distribution axes. ax3 = fig.add_subplot(222) ax4 = fig.add_subplot(224, sharex=ax3) restraint_axes = [ax3, ax4] # Plot barostat analysis. plot_volume_distributions(barostat_axes, plot_predicted=True) # Plot restraint analysis. system_id = 'OA-G3-0' plot_restraint_analysis(system_id, restraint_axes) # Configure axes. restraint_axes[0].set_xlim((0, 10.045)) restraint_axes[1].set_ylim((-7, -3.9)) for ax in restraint_axes + barostat_axes: ax.tick_params(axis='x', which='major', pad=0.1) ax.tick_params(axis='y', which='major', pad=0.1) plt.tight_layout(pad=0.3) # plt.show() output_file_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'Figure5-restraint_barostat', 'restraint_barostat.pdf') os.makedirs(os.path.dirname(output_file_path), exist_ok=True) plt.savefig(output_file_path) # ============================================================================= # FIGURE 6 - HREX INITIAL BIAS # ============================================================================= def plot_yank_system_bias(system_name, data_dir_paths, axes, shift_to_origin=True, plot_std=True): """Plot the YANK free energy trajectoies when discarding initial samples for a single system.""" color_palette = sns.color_palette('viridis', n_colors=len(data_dir_paths)+1) # Plot trajectories with truncated data. all_iterations = set() for data_idx, data_dir_path in enumerate(data_dir_paths): yank_analysis = YankSamplingAnalysis(data_dir_path) # In the YankAnalysis folder, each analysis starting from # iteration N is in the folder "iterN/". last_dir_name = os.path.basename(os.path.normpath(data_dir_path)) label = last_dir_name[4:] # First color is for the full data. color = color_palette[data_idx+1] # Collect all iterations that we'll plot for the full data. mean_data = yank_analysis.get_system_free_energies(system_name, mean_trajectory=True) all_iterations.update(mean_data['HREX iteration'].values.tolist()) # Simulate plotting starting from the origin. if shift_to_origin: mean_data['HREX iteration'] -= mean_data['HREX iteration'].values[0] plot_mean_data(mean_data, axes, x='HREX iteration', color=color, label=label, plot_std=plot_std, plot_bias=False, plot_ci=False) # Plot trajectory with full data. color = color_palette[0] # Plot an early iteration and all the iterations analyzed for the bias. yank_analysis = YankSamplingAnalysis(YANK_ANALYSIS_DIR_PATH) system_ids = [system_name + '-' + str(i) for i in range(5)] first_iteration = yank_analysis.get_system_iterations(system_ids[0])[2] iterations = [first_iteration] + sorted(all_iterations) mean_data = yank_analysis._get_free_energies_from_iterations( iterations, system_ids, mean_trajectory=True) # Simulate plotting starting from the origin. if shift_to_origin: mean_data['HREX iteration'] -= mean_data['HREX iteration'].values[0] # Simulate ploatting starting from the origin. plot_mean_data(mean_data, axes, x='HREX iteration', color=color, label='0', plot_std=plot_std, plot_bias=False, plot_ci=False) axes[0].set_title(system_name) def plot_yank_bias(plot_std=True, figure_dir_path=None): """Plot YANK free energy trajectories when discarding initial samples.""" # In the first column, plot the "unshifted" trajectory of CB8-G3, # with all sub-trajectories shifted to the origin. In the second # and third columns, plot the trajectories of CB8-G3 and OA-G3 # with all sub-trajectories shifted to the origin. what_to_plot = [ ('CB8-G3', False), # ('CB8-G3', True), ('OA-G3', False), # ('OA-G3', False), ('OA-G6', False), ] if plot_std: n_rows = 2 else: n_rows = 1 n_cols = len(what_to_plot) fig, axes = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(7.25, 4.0)) # The loops are based on a two dimensional array of axes. if n_rows == 1: axes = np.array([axes]) # Sort paths by how many samples they have. data_dir_paths = ['YankAnalysis/BiasAnalysis/iter{}/'.format(i) for i in [1000, 2000, 4000, 8000, 16000, 24000]] for column_idx, (system_name, shift_to_origin) in enumerate(what_to_plot): plot_yank_system_bias(system_name, data_dir_paths, axes[:,column_idx], shift_to_origin=shift_to_origin, plot_std=plot_std) title = system_name + ' (shifted)' if shift_to_origin else system_name axes[0,column_idx].set_title(title) # Fix axes limits and labels. ylimits = { 'CB8-G3': (-12.5, -10.5), 'OA-G3': (-8, -6), 'OA-G6': (-8, -6) } for column_idx, (system_name, _) in enumerate(what_to_plot): axes[0][column_idx].set_ylim(ylimits[system_name]) if plot_std: axes[1][column_idx].set_ylim((0, 0.6)) for row_idx, ax_idx in itertools.product(range(n_rows), range(n_cols)): # Control the number of ticks for the x axis. axes[row_idx][ax_idx].locator_params(axis='x', nbins=4) # Set x limits for number of iterations. axes[row_idx][ax_idx].set_xlim((0, YANK_N_ITERATIONS)) # Remove ticks labels that are shared with the last row. for row_idx, ax_idx in itertools.product(range(n_rows-1), range(n_cols)): axes[row_idx][ax_idx].set_xticklabels([]) # Set axes labels. axes[0][0].set_ylabel('$\Delta$G [kcal/mol]') if plot_std: axes[1][0].set_ylabel('std($\Delta$G) [kcal/mol]') # If there is an odd number of columns print x label only on the central one. if n_cols % 2 == 1: axes[-1][1].set_xlabel('HREX iteration') else: for ax in axes[-1]: ax.set_xlabel('HREX iteration') plt.tight_layout(h_pad=0.1, rect=[0.0, 0.00, 1.0, 0.91]) handles, labels = axes[0][0].get_legend_handles_labels() handles = [handles[-1]] + handles[:-1] labels = [labels[-1]] + labels[:-1] bbox_to_anchor = (0.4, 1.53) axes[0][0].legend(handles, labels, loc='upper left', bbox_to_anchor=bbox_to_anchor, title='number of discarded initial iterations', ncol=len(data_dir_paths)+1, fancybox=True, labelspacing=0.8, handletextpad=0.5, columnspacing=1.2, fontsize='small') # plt.show() if figure_dir_path is None: figure_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'Figure6-bias_hrex') os.makedirs(figure_dir_path, exist_ok=True) output_file_path = os.path.join(figure_dir_path, 'Figure6-bias_hrex') plt.savefig(output_file_path + '.pdf') # plt.savefig(output_file_path + '.png', dpi=600) # ============================================================================= # SUPPORTING INFORMATION - EXAMPLE OF HREX BIAS # ============================================================================= def simulate_correlation_samples(): """Simulation of bias from same initial configuration. There are 3 states as different harmonic oscillators, but all or almost all the samples come from the first (bound) state to simulate what happens when they don't decorrelate fast enough. The hypothesis is that most is that starting from the bound state causes the initial free energy to be artificially negative if the correlation times are long. The second (discharged) state is just a shifted harmonic oscillator (same free energy as bound state). The third (unbound) is shifted and has much higher entropy. """ from numpy.random import normal from pymbar import MBAR def harmonic_oscillator_free_energy(sigma): """Analytical expression for the free energy of a harmonic oscillator.""" #return - np.log(2 * np.pi * sigma**2) * 3.0 / 2.0 # 3D oscillator return - np.log(np.sqrt(2 * np.pi) * sigma) def harmonic_oscillator_potential(x, loc, std): """Compute potential of the given positions given location and standard deviation of the Gaussian distribution. Potentials are returned in units of kT. """ spring_constant = 1 / std**2 return spring_constant / 2.0 * (x - loc)**2 def print_free_energies(Deltaf_ij, dDeltaf_ij): mbar_str = ', '.join(['{:.4f} +- {:.4f}'.format(f, df) for f, df in zip(Deltaf_ij[:,0], dDeltaf_ij[:,0])]) print('MBAR :', mbar_str) analytical_str = ', '.join(['{:.4f} '.format(f) for f in analytical_Deltaf]) print('Analytical:', analytical_str) def compute_mbar_free_energy(all_samples, shifts, stds, analytical_f): n_states = len(all_samples) # u_kn[k,n] is the reduced potential energy n-th sample evaluated at state k. u_kn = np.empty(shape=(n_states, n_states*n_samples)) # Convert samples to potentials. for k in range(n_states): for sampled_k, samples in enumerate(all_samples): start = sampled_k * n_samples end = (sampled_k + 1) * n_samples u_kn[k,start:end] = harmonic_oscillator_potential(samples, loc=shifts[k], std=stds[k]) # Compute MBAR free energy. N_k = np.array([n_samples] * n_states) mbar = MBAR(u_kn, N_k=N_k, initial_f_k=analytical_f) Deltaf_ij, dDeltaf_ij, _ = mbar.getFreeEnergyDifferences() return Deltaf_ij, dDeltaf_ij # Determine standard deviation and shift of the harmonic distributions. n_samples = 5000000 stds = np.array([2.0, 2.0, 5.0]) shifts = np.array([0.0, 2.0, 2.0]) print('\nspring constants:', 1 / stds**2) # Compute analytical free energy. analytical_f = np.array([harmonic_oscillator_free_energy(s) for s in stds]) analytical_Deltaf = np.array([analytical_f[0] - analytical_f[i] for i in range(len(stds))]) # FIRST TEST. # Sample from all states and verify that MBAR free energy is correct. # ------------------------------------------------------------------- all_samples = [normal(loc=l, scale=s, size=n_samples) for l, s in zip(shifts, stds)] Deltaf_ij, dDeltaf_ij = compute_mbar_free_energy(all_samples, shifts, stds, analytical_f) print() print_free_energies(Deltaf_ij, dDeltaf_ij) # SECOND TEST. # Check if the bias is not due to lack of overlap. If we sample only the end states the estimate should be correct. # ----------------------------------------------------------------------------------------------------------------- for i in range(1, len(all_samples)): all_samples_bar = [all_samples[0], all_samples[i]] shifts_bar = [shifts[0], shifts[i]] stds_bar = [stds[0], stds[i]] analytical_f_bar = [analytical_f[0], analytical_f[i]] Deltaf_ij, dDeltaf_ij = compute_mbar_free_energy(all_samples_bar, shifts_bar, stds_bar, analytical_f_bar) print('\nBAR_{}0'.format(i)) print_free_energies(Deltaf_ij, dDeltaf_ij) # THIRD TEST. # Now sample from only the bound state to see how the free energy changes. # ------------------------------------------------------------------------ all_samples[1:] = [normal(loc=shifts[0], scale=stds[0], size=n_samples) for _ in range(len(stds)-1)] Deltaf_ij, dDeltaf_ij = compute_mbar_free_energy(all_samples, shifts, stds, analytical_f) print() print_free_energies(Deltaf_ij, dDeltaf_ij) # FOURTH TEST. # Now let the unbound state decorrelate fast (i.e. sample from its own distribution). # ----------------------------------------------------------------------------------- all_samples[-1] = normal(loc=shifts[-1], scale=stds[-1], size=n_samples) Deltaf_ij, dDeltaf_ij = compute_mbar_free_energy(all_samples, shifts, stds, analytical_f) print() print_free_energies(Deltaf_ij, dDeltaf_ij) # RESULT: SUCCESS!!! # ============================================================================= # SUPPORTING INFORMATION - COMPLEX/SOLVENT and ENTROPY/ENTHALPY DECOMPOSITION # ============================================================================= def _mean_data_decomposition(data): # Convert into a numpy array to take the mean. # Convert None (not supported by numpy) into nans. try: # This may fail if we have computed different iterations for each. data = np.array(data, dtype=np.float) except ValueError: data_lengths = [len(x) for x in data] print('Warning: Truncating data of shape {}'.format(data_lengths)) min_length = min(data_lengths) data = [x[:min_length] for x in data] data = np.array(data, dtype=np.float) # Compute std and mean along the trajectory ignoring NaNs. return np.nanmean(data, axis=0), np.nanstd(data, axis=0) def _plot_phase_decomposition(ax, phase_free_energies): # Shortcuts. data = phase_free_energies label = '$\Delta$G' # Plot each phase data on a separate axis to make the comparison on different order of magnitudes easier. # Receipt with three axes: https://matplotlib.org/3.1.0/gallery/ticks_and_spines/multiple_yaxis_with_spines.html phase_axes = { 'complex': ax.twinx(), 'solvent': ax.twinx() } phase_colors = { 'complex': 'C1', 'solvent': 'C0', } for ax_name in sorted(phase_axes): phase_axes[ax_name].set_ylabel(label + ' ' + ax_name + ' [kcal/mol]', color=phase_colors[ax_name]) phase_axes[ax_name].spines["right"].set_position(("axes", 1.2)) # Compute total free energy summing complex and solvent for all replicates. total_mean = [np.array(data['solvent'][i]) + np.array(data['complex'][i]) for i in range(5)] total_mean, total_std = _mean_data_decomposition(total_mean) # Compute and plot the phase free energy. for phase_name in ['complex', 'solvent']: color = phase_colors[phase_name] # Convert into a numpy array to take the mean. # Convert None (not supported by numpy) into nans. data[phase_name], std = _mean_data_decomposition(data[phase_name]) # Plot each phase data on a separate axis to make the comparison easier. phase_axes[phase_name].plot(data[phase_name], ls='-', color=color, label=label + ' ' + phase_name) # Plot uncertainties. phase_axes[phase_name].fill_between(x=list(range(len(std))), y1=data[phase_name]-std, y2=data[phase_name]+std, color=color, alpha=0.7) # Plot total free energy. # total = data['solvent'] + data['complex'] # ax.plot(total, color='black', label=label+' total') ax.plot(total_mean, color='black', label=label+' total') ax.fill_between(x=list(range(len(total_std))), y1=total_mean-total_std, y2=total_mean+total_std, color='black', alpha=0.7) ax.set_ylabel(label + ' total [kcal/mol]') ax.set_xlabel('simulation percentage') # Make the range of all y axes the same. ax.set_ylim((-21, -18)) phase_axes['complex'].set_ylim((-151.0, -148.0)) phase_axes['solvent'].set_ylim((129.0, 132.0)) def _plot_entropy_enthalpy_decomposition(ax, phase_free_energies, phase_enthalpy): # Analyze only the complex. phase_name = 'complex' # Plot each phase data on a separate axis to make the comparison on different order of magnitudes easier. # Receipt with three axes: https://matplotlib.org/3.1.0/gallery/ticks_and_spines/multiple_yaxis_with_spines.html axes = { '$\Delta$G': ax, '$\Delta$H': ax.twinx(), '-T$\Delta$S': ax.twinx(), } colors = { '$\Delta$G': 'black', '$\Delta$H': 'C1', '-T$\Delta$S': 'C0', } for ax_name in sorted(axes): axes[ax_name].set_ylabel(ax_name + ' ' + phase_name + ' [kcal/mol]', color=colors[ax_name]) axes[ax_name].spines["right"].set_position(("axes", 1.2)) # Variable used to propagate entropy decomposition. entropy_std = [] # Plot the total average free energy and enthalpy and for each phase. for data, label in [(phase_free_energies, '$\Delta$G'), (phase_enthalpy, '$\Delta$H')]: color = colors[label] # Convert into a numpy array to take the mean. # Convert None (not supported by numpy) into nans. data[phase_name], std = _mean_data_decomposition(data[phase_name]) ns_replica = np.arange(0.0, 40.0, 40/len(std)) # Plot each phase data on a separate axis to make the comparison easier. axes[label].plot(ns_replica, data[phase_name], ls='-', color=color, label=label+' '+phase_name) # Plot uncertainties. axes[label].fill_between(x=ns_replica, y1=data[phase_name]-std, y2=data[phase_name]+std, color=color, alpha=0.7) # Propagate uncertainty. if len(entropy_std) == 0: entropy_std = std**2 else: entropy_std += std**2 entropy_std = np.sqrt(entropy_std) # Plot also entropies. label = '-T$\Delta$S' color = colors[label] entropy = phase_free_energies[phase_name] - phase_enthalpy[phase_name] axes[label].plot(ns_replica, entropy, ls='-', color=color, label=label+' '+phase_name) # Plot uncertainties. axes[label].fill_between(x=ns_replica, y1=entropy-entropy_std, y2=entropy+entropy_std, color=color, alpha=0.7) ax.set_xlabel('ns/replica') def plot_decomposition(system_name, starting_iteration, type, output_file_path): """ Decomposition of the free energy trajectory in complex/solvent phase or entropy/enthalpy. Parameters ---------- type : str Can be 'entropy-enthalpy' or 'phase'. """ data_file_pattern = 'YankAnalysis/BiasAnalysis/iter{}/fe-decomposition-{}-{{}}.json'.format( starting_iteration, system_name) n_replicates = 5 phase_free_energies = {'complex': [[] for _ in range(n_replicates)], 'solvent': [[] for _ in range(n_replicates)]} phase_enthalpy = copy.deepcopy(phase_free_energies) for replicate_idx in range(n_replicates): # Read decomposition data. decomposition_data_file_path = data_file_pattern.format(replicate_idx) with open(decomposition_data_file_path, 'r') as f: decomposition_data = json.load(f) # Read free energy and enthalpy at each iteration. sorted_decomposition_data = sorted(decomposition_data, key=lambda x: int(x.split('-')[1])) for phase_iter in sorted_decomposition_data: decomposition = decomposition_data[phase_iter] phase_name, iteration = phase_iter.split('-') # Correct sign consistent with thermodynamic cycle. if phase_name == 'complex': sign = -1 else: sign = 1 corrected_free_energy = sign * (decomposition['DeltaF'] + decomposition['DeltaF_standard_state_correction']) phase_free_energies[phase_name][replicate_idx].append(corrected_free_energy) # Multiplication works only if enthalpy is not None. if decomposition['DeltaH'] is not None: decomposition['DeltaH'] *= sign phase_enthalpy[phase_name][replicate_idx].append(decomposition['DeltaH']) # Create figure. fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(7.25, 4.6)) if type == 'entropy-enthalpy': _plot_entropy_enthalpy_decomposition(ax, phase_free_energies, phase_enthalpy) else: _plot_phase_decomposition(ax, phase_free_energies) # # Plot total free energy. # total = data['solvent'] + data['complex'] # ax.plot(total, color=color, label=label) # totals.append(total) # Plot also entropies. # ax.plot(totals[0] - totals[1], color='blue', label='-T$\Delta$S') # ax.set_ylim((-20, -18)) # phase_axes['complex'].set_ylim((-153, -148)) # phase_axes['solvent'].set_ylim((128, 133)) # ax.set_ylim((-23, -18)) # phase_axes['complex'].set_ylim((30, 45)) # phase_axes['solvent'].set_ylim((-55, -40)) # ax.legend() plt.tight_layout() if output_file_path is not None: os.makedirs(os.path.dirname(output_file_path), exist_ok=True) plt.savefig(output_file_path) else: plt.show() # ============================================================================= # RELATIVE EFFICIENCY ANALYSIS # ============================================================================= def get_relative_efficiency_input(submission, yank_analysis, system_name): """Prepare the data to compute the mean relative efficiencies for this system.""" # For GROMACS/EE-fullquil we need to account for the extra equilibration # cost and shift all energy evaluation to the right. if submission.paper_name == 'GROMACS/EE-fullequil': mean_free_energies = submission.mean_free_energies() mean_data = mean_free_energies[mean_free_energies['System name'] == system_name] first_shifted = mean_data['N energy evaluations'].values[0] last_shifted = mean_data['N energy evaluations'].values[-1] calibration_cost = first_shifted*100/99 - last_shifted/99 else: calibration_cost = 0 # Isolate the data for the system. data_sub = submission.data[submission.data['System name'] == system_name] n_energy_evaluations = max(data_sub['N energy evaluations']) data_ref = yank_analysis.get_free_energies_from_energy_evaluations( n_energy_evaluations, system_name=system_name, mean_trajectory=False, start=calibration_cost) # Obtain the free energies for the submission. n_replicates = 5 free_energy_sub = np.empty(shape=(n_replicates, 100)) free_energy_ref = np.empty(shape=(n_replicates, 100)) for data, free_energy in [ (data_sub, free_energy_sub), (data_ref, free_energy_ref), ]: for i in range(n_replicates): system_id = system_name + '-' + str(i) system_id_data = data[data['System ID'] == system_id] free_energy[i] = system_id_data[DG_KEY].values # Discard the initial frames of REVO and GROMACS/EE that don't have predictions. from pkganalysis.efficiency import discard_initial_zeros free_energy_ref, free_energy_sub = discard_initial_zeros(free_energy_ref, free_energy_sub) # Determine the actual asymptotic free energy of YANK. asymptotic_free_energy_ref = yank_analysis.get_reference_free_energies()[system_name] return free_energy_ref, free_energy_sub, asymptotic_free_energy_ref def compute_all_relative_efficiencies( free_energy_A, free_energy_B, ci, n_bootstrap_samples, asymptotic_free_energy_A=None, asymptotic_free_energy_B=None ): from pkganalysis.efficiency import EfficiencyAnalysis analysis = EfficiencyAnalysis(free_energy_A, free_energy_B, asymptotic_free_energy_A, asymptotic_free_energy_B) std_rel_eff = analysis.compute_std_relative_efficiency( confidence_interval=ci, n_bootstrap_samples=n_bootstrap_samples) abs_bias_rel_eff = analysis.compute_abs_bias_relative_efficiency( confidence_interval=ci, n_bootstrap_samples=n_bootstrap_samples) rmse_rel_eff = analysis.compute_rmse_relative_efficiency( confidence_interval=ci, n_bootstrap_samples=n_bootstrap_samples) if ci is None: rel_eff = [std_rel_eff, abs_bias_rel_eff, rmse_rel_eff] return rel_eff else: rel_eff = [std_rel_eff[0], abs_bias_rel_eff[0], rmse_rel_eff[0]] cis = [std_rel_eff[1], abs_bias_rel_eff[1], rmse_rel_eff[1]] return rel_eff, cis def plot_relative_efficiencies(submissions, yank_analysis, ci=0.95, n_bootstrap_samples=1000, same_plot=False, step_cumulative=2): sns.set_style('whitegrid') sns.set_context('paper') statistic_names = ['std', 'absolute bias', 'RMSE'] # Create output directory. figure_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'SI_Figure-efficiencies') os.makedirs(figure_dir_path, exist_ok=True) # Check if we need all the efficiencies in the same plot or not. if same_plot: fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(7.25, 8)) # Keep track of data range by statistic. statistic_ranges = {name: [np.inf, 0] for name in statistic_names} # Keep track of n_energy_evaluations by column. max_n_energy_evaluations = [0 for _ in range(3)] for submission in submissions: if submission.paper_name in {'OpenMM/REVO'}: continue # if submission.paper_name in {'AMBER/APR', 'GROMACS/NS-DS/SB', 'GROMACS/NS-DS/SB-long', # 'NAMD/BAR', 'GROMACS/EE', 'GROMACS/EE-fullequil', 'OpenMM/SOMD'}: # continue print(submission.paper_name) system_names = submission.data['System name'].unique() # Create figure. if not same_plot: # For GROMACS/EE, there are no submissions for CB8-G3. if 'GROMACS/EE' in submission.paper_name: system_names = system_names[~(system_names == 'CB8-G3')] fig, axes = plt.subplots(nrows=3, ncols=len(system_names), figsize=(7.25, 8)) statistic_ranges = {name: [np.inf, 0] for name in statistic_names} for col_idx, system_name in enumerate(system_names): color = SUBMISSION_COLORS[submission.paper_name] # For GROMACS/EE, there are no submissions for CB8-G3. if 'GROMACS/EE' in submission.paper_name and system_name == 'CB8-G3': continue # For GROMACS/NS-DS/SB-long there are no new submissions for OAs. if 'GROMACS/NS-DS/SB-long' in submission.paper_name and system_name != 'CB8-G3': # Just add the label. axes[0][col_idx].plot([], color=color, label=submission.paper_name) continue # Get input for EfficiencyAnalysis. free_energy_ref, free_energy_sub, asymptotic_free_energy_ref = get_relative_efficiency_input( submission, yank_analysis, system_name) # Get the relative efficiencies. rel_eff = compute_all_relative_efficiencies( free_energy_ref, free_energy_sub, ci, n_bootstrap_samples, asymptotic_free_energy_A=asymptotic_free_energy_ref ) if ci is not None: rel_eff, cis = rel_eff # Unpack confidence intervals. # Use the same asymptotic free energies to compute the absolute bias # relative efficiency as a function of the simulation length. asymptotic_free_energy_sub = free_energy_sub.mean(axis=0)[-1] # # Print relative efficiencies. # print(system_name, ci) # if ci is not None: # for rel_eff, bounds in zip(rel_eff, cis): # print('\t', rel_eff, bounds.tolist()) # else: # for rel_eff in rel_eff: # print('\t', rel_eff) # Compute mean efficiencies as a function of the length of the simulation. n_costs = free_energy_ref.shape[1] n_rel_eff = int(n_costs / step_cumulative) relative_efficiencies = np.empty(shape=(3, n_rel_eff)) low_bounds = np.empty(shape=(3, n_rel_eff)) high_bounds = np.empty(shape=(3, n_rel_eff)) for i, c in enumerate(range(step_cumulative-1, n_costs, step_cumulative)): c1 = c + 1 rel_eff = compute_all_relative_efficiencies( free_energy_ref[:,:c1], free_energy_sub[:,:c1], ci, n_bootstrap_samples, asymptotic_free_energy_A=asymptotic_free_energy_ref, asymptotic_free_energy_B=asymptotic_free_energy_sub ) if ci is not None: rel_eff, cis = rel_eff # Unpack confidence intervals. # Update CI lower and upper bound. relative_efficiencies[:,i] = rel_eff if ci is not None: low_bounds[:,i] = [x[0] for x in cis] high_bounds[:,i] = [x[1] for x in cis] # Get number of energy evaluations. mean_data = submission.mean_free_energies(system_name=system_name) # Check how many initial iteration have been discarded. discarded_iterations = 100 - n_costs n_energy_evaluations = mean_data['N energy evaluations'].values[ discarded_iterations+1::step_cumulative] / 1e6 for row_idx, rel_eff in enumerate(relative_efficiencies): ax = axes[row_idx][col_idx] ax.plot(n_energy_evaluations, rel_eff, color=color, label=submission.paper_name) # Plot back line at 0. ax.plot(n_energy_evaluations, [0 for _ in n_energy_evaluations], color='black', ls='--') # Update data range. statistic_range = statistic_ranges[statistic_names[row_idx]] # if ci is None: # min_rel_eff = min(rel_eff) # max_rel_eff = max(rel_eff) # else: # min_rel_eff = min(*rel_eff, *low_bounds[row_idx]) # max_rel_eff = max(*rel_eff, *high_bounds[row_idx]) statistic_range[0] = min(statistic_range[0], min(rel_eff)) statistic_range[1] = max(statistic_range[1], max(rel_eff)) # Update x-axis range. if same_plot: max_n_energy_evaluations[col_idx] = max(max_n_energy_evaluations[col_idx], n_energy_evaluations[-1]) else: for row_idx in range(len(statistic_names)): axes[row_idx][col_idx].set_xlim((0, n_energy_evaluations[-1])) if ci is not None: # Plot confidence intervals. for row_idx, (low_bound_c, high_bound_c) in enumerate(zip(low_bounds, high_bounds)): ax = axes[row_idx][col_idx] ax.fill_between(n_energy_evaluations, low_bound_c, high_bound_c, alpha=0.35, color='gray') # We do this multiple times unnecessarily if same_plot is True, but the code is simpler. for col_idx, system_name in enumerate(system_names): axes[0][col_idx].set_title(system_name) for row_idx, statistic_name in enumerate(statistic_names): axes[row_idx][0].set_ylabel(statistic_name + ' rel eff') for col_idx in range(len(system_names)): if same_plot: extra_space = 0.1 else: # Make space for confidence intervals. extra_space = 1 ylimits = (statistic_ranges[statistic_name][0] - extra_space, statistic_ranges[statistic_name][1] + extra_space) axes[row_idx][col_idx].set_ylim(ylimits) axes[row_idx][col_idx].tick_params(axis='y', which='major', pad=0.1) axes[-1][1].set_xlabel('Number of force/energy evaluations [10$^6$]') # Set labels and axes limits. if not same_plot: fig.suptitle(submission.paper_name) output_file_base_name = 'releff-{}-{}'.format(submission.file_name, submission.receipt_id) output_file_base_path = os.path.join(figure_dir_path, output_file_base_name) plt.savefig(output_file_base_path + '.pdf') # plt.savefig(output_file_base_path + '.png', dpi=600) # plt.show() if same_plot: for row_idx in range(len(statistic_names)): for col_idx in range(len(system_names)): axes[row_idx][col_idx].set_xlim((0, max_n_energy_evaluations[col_idx])) axes[0][1].legend(loc='upper right', bbox_to_anchor=(2.0, 1.48), fancybox=True, ncol=3) output_file_base_path = os.path.join(figure_dir_path, 'relative-efficiencies') plt.savefig(output_file_base_path + '.pdf') # plt.savefig(output_file_base_path + '.png', dpi=600) # plt.show() def plot_absolute_efficiencies(submissions, yank_analysis, ci=0.95, n_bootstrap_samples=1000): sns.set_style('whitegrid') sns.set_context('paper') # Keep track of data range by statistic. statistic_names = ['std', 'absolute bias', 'RMSE'] # Keep track of maximum number of energy evaluations # to determine plotting range for YANK. system_names = ['CB8-G3', 'OA-G3', 'OA-G6'] max_n_energy_eval = {name: 0 for name in system_names} # Create figure. fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(7.25, 8)) for submission in submissions + [yank_analysis]: if 'REVO' in submission.paper_name: continue print(submission.paper_name) # Obtain std, bias, and RMSE of the 5 trajectories. # If this is a YANK analysis, we get it later specifically for the system. if not isinstance(submission, YankSamplingAnalysis): mean_free_energies = submission.mean_free_energies() color = SUBMISSION_COLORS[submission.paper_name] for col_idx, system_name in enumerate(system_names): # GROMACS/EE doesn't have submissions for CB8-G3. if 'GROMACS/EE' in submission.paper_name and system_name == 'CB8-G3': continue # For GROMACS/NS-DS/SB-long there are no new submissions for OAs. if 'GROMACS/NS-DS/SB-long' in submission.paper_name and 'OA' in system_name: # Just add the label. axes[0][col_idx].plot([], color=color, label=submission.paper_name) continue # Select the submission data for only this host-guest system. if isinstance(submission, YankSamplingAnalysis): line_style = '--' mean_data = submission.get_free_energies_from_energy_evaluations( max_n_energy_eval[system_name], system_name=system_name, mean_trajectory=True) else: line_style = '-' mean_data = mean_free_energies[mean_free_energies['System name'] == system_name] # Update maximum number of energy evaluations. n_energy_evaluations = mean_data['N energy evaluations'].values max_n_energy_eval[system_name] = max(max_n_energy_eval[system_name], n_energy_evaluations[-1]) # Discard initial computational costs for which there's no data. first_nonzero_idx = np.nonzero(mean_data[DG_KEY])[0][0] n_energy_evaluations = n_energy_evaluations[first_nonzero_idx:] # Compute cumulative total std, abs_bias, and RMSE. scale_energy_evaluations = 1e6 norm_factor = (n_energy_evaluations - n_energy_evaluations[0])[1:] / scale_energy_evaluations avg_std = sp.integrate.cumtrapz(mean_data['std'].values[first_nonzero_idx:]) / norm_factor avg_abs_bias = sp.integrate.cumtrapz(np.abs(mean_data['bias'].values[first_nonzero_idx:])) / norm_factor avg_rmse = sp.integrate.cumtrapz(mean_data['RMSE'].values[first_nonzero_idx:]) / norm_factor # Plot total statistics as a function of the energy evaluations. # Discard first energy evaluation as cumtrapz doesn't return a result for it. for row_idx, avg_stats in enumerate([avg_std, avg_abs_bias, avg_rmse]): ax = axes[row_idx, col_idx] ax.plot(n_energy_evaluations[1:] / scale_energy_evaluations, avg_stats, color=color, label=submission.paper_name, ls=line_style) # Set x axis. ax.set_xlim((0, n_energy_evaluations[-1] / scale_energy_evaluations)) # Set labels and axes limits. y_limits = { 'std': (0, 0.4), 'absolute bias': (0, 0.3), 'RMSE': (0, 0.4) } for col_idx, system_name in enumerate(system_names): axes[0][col_idx].set_title(system_name) # Set y limits (shared for each row). for row_idx, statistic_name in enumerate(statistic_names): axes[row_idx][col_idx].set_ylim(y_limits[statistic_name]) axes[row_idx][col_idx].tick_params(axis='y', which='major', pad=0.1) # # Remove shared ticks. # for row_idx in range(len(statistic_names)): # for col_idx in range(len(system_names)): # if col_idx > 0: # axes[row_idx][col_idx].set_yticklabels([]) # if row_idx < len(statistic_names)-1: # axes[row_idx][col_idx].set_xticklabels([]) for row_idx, statistic_name in enumerate(statistic_names): axes[row_idx][0].set_ylabel('mean ' + statistic_name + ' [kcal/mol]') axes[-1][1].set_xlabel('N energy evaluations [M]') axes[0][1].legend(loc='upper right', bbox_to_anchor=(2.0, 1.48), fancybox=True, ncol=3) figure_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'SI_Figure-efficiencies') os.makedirs(figure_dir_path, exist_ok=True) output_file_base_path = os.path.join(figure_dir_path, 'absolute-efficiencies') plt.savefig(output_file_base_path + '.pdf') # plt.savefig(output_file_base_path + '.png', dpi=600) # plt.show() def print_relative_efficiency_table( submissions, yank_analysis, ci=0.95, n_bootstrap_samples=100, print_bias_corrected=False ): """Create a table with standard deviation, absolute bias, and RMSE relative efficiency.""" methods = [] # Initialize the table to be converted into a Pandas dataframe. system_names = ['CB8-G3', 'OA-G3', 'OA-G6'] statistic_names = [r'$e_{\mathrm{std}}$', r'$e_{|\mathrm{bias}|}$', r'$e_{\mathrm{RMSD}}$'] column_names = ['\\makecell{$\Delta$ G \\\\ $[$kcal/mol$]$}', '\\makecell{n eval \\\\ $[$M$]$}'] + statistic_names # Add columns. efficiency_table = collections.OrderedDict() for system_name, column_name in itertools.product(system_names, column_names): efficiency_table[(system_name, column_name)] = [] for submission in submissions: # Collect method's names in the given order. methods.append(submission.paper_name) mean_free_energies = submission.mean_free_energies() for system_name in system_names: # CB8-G3 calculations for GROMACS/EE did not converge yet, and the # long protocol in CS-NS calculations have been run only on CB8-G3. if ((submission.name == 'Expanded-ensemble/MBAR' and system_name == 'CB8-G3') or (submission.paper_name == 'GROMACS/NS-DS/SB-long' and system_name != 'CB8-G3')): relative_efficiencies, relative_efficiencies_corrected = np.full((2, 3), fill_value=np.nan) dg = '' n_force_eval = '' else: # Get input for EfficiencyAnalysis. free_energy_ref, free_energy_sub, asymptotic_free_energy_ref = get_relative_efficiency_input( submission, yank_analysis, system_name) # Get the relative efficiencies. relative_efficiencies, cis = compute_all_relative_efficiencies( free_energy_ref, free_energy_sub, ci, n_bootstrap_samples, asymptotic_free_energy_A=asymptotic_free_energy_ref ) # Recompute relative efficiencies assuming that YANK converged. if print_bias_corrected: relative_efficiencies_corrected, cis_corrected = compute_all_relative_efficiencies( free_energy_ref, free_energy_sub, ci, n_bootstrap_samples) # Select the data for only this host-guest system. mean_data_sub = mean_free_energies[mean_free_energies['System name'] == system_name] # Get the final free energy and number of energy/force evaluations. dg = mean_data_sub[DG_KEY].values[-1] dg_CI = mean_data_sub['$\Delta$G CI'].values[-1] # Confidence interval. dg, dg_CI = reduce_to_first_significant_digit(dg, dg_CI) n_force_eval = mean_data_sub['N energy evaluations'].values[-1] # Convert to string format. dg = '{} $\\pm$ {}'.format(dg, dg_CI) n_force_eval = str(int(round(n_force_eval / 1e6))) # Add free energy and cost entries. efficiency_table[(system_name, column_names[0])].append(dg) efficiency_table[(system_name, column_names[1])].append(n_force_eval) # Add efficiency entries for the table. for statistic_idx, statistic_name in enumerate(statistic_names): # Gather the format arguments. rel_effs = [relative_efficiencies[statistic_idx], cis[statistic_idx][0], cis[statistic_idx][1]] if print_bias_corrected: rel_effs.append(relative_efficiencies_corrected[statistic_idx]) # Comment this if we don't want to print CIs for the corrected estimate. rel_effs.extend([cis_corrected[statistic_idx][0], cis_corrected[statistic_idx][1]]) # Print significant digits. efficiencies_format = [] for e_idx in range(0, len(rel_effs), 3): rel_eff, low_bound, high_bound = rel_effs[e_idx:e_idx+3] if high_bound - rel_eff < 0.1 or rel_eff - low_bound < 0.1: fmt = '{:2.2f}' else: fmt = '{:2.1f}' # Print lower and higher bound as sub and superscripts of the estimate. efficiencies_format.append(fmt + '$_{{\raisem{{2pt}}{{' + fmt + '}}}}^{{\mathstrut ' + fmt + '}}$') if np.isnan(rel_effs[0]): data_entry = '' # Standard deviation efficiency is not affected by the bias. elif print_bias_corrected and ('std' not in statistic_name): data_entry = efficiencies_format[0] + ' (' + efficiencies_format[1] + ')' data_entry = data_entry.format(*rel_effs) else: data_entry = efficiencies_format[0].format(*rel_effs[:3]) # Remove the minus sign from "-0". data_entry = data_entry.replace('-0.0', '0.0') data_entry = data_entry.replace('-0.00', '0.00') efficiency_table[(system_name, statistic_name)].append(data_entry) # Add row for reference calculation. methods.append(YANK_METHOD_PAPER_NAME) # Add free energy and cost entries. for system_name in system_names: yank_mean_data = yank_analysis.get_free_energies_from_iteration( YANK_N_ITERATIONS, system_name=system_name, mean_trajectory=True) dg = yank_mean_data[DG_KEY].values[-1] dg_CI = yank_mean_data['$\Delta$G CI'].values[-1] # Confidence interval. dg, dg_CI = reduce_to_first_significant_digit(dg, dg_CI) n_force_eval = yank_mean_data['N energy evaluations'].values[-1] n_force_eval = str(int(round(n_force_eval / 1e6))) efficiency_table[(system_name, column_names[0])].append('{} $\\pm$ {}'.format(dg, dg_CI)) efficiency_table[(system_name, column_names[1])].append(n_force_eval) # All efficiencies are relative to YANK so they're all 1. for system_name, statistic_name in itertools.product(system_names, statistic_names): efficiency_table[(system_name, statistic_name)].append('0.0') # Convert to Pandas Dataframe. efficiency_table = pd.DataFrame(efficiency_table) # Set the method's names as index column. efficiency_table = efficiency_table.assign(Method=methods) efficiency_table.set_index(keys='Method', inplace=True) # Print table. column_format = 'lccccc|ccccc|ccccc' efficiency_table_latex = efficiency_table.to_latex(column_format=column_format, multicolumn_format='c', escape=False) # Make header and reference method bold. textbf = lambda s: '\\textbf{' + s + '}' efficiency_table_latex = efficiency_table_latex.replace(YANK_METHOD_PAPER_NAME, textbf(YANK_METHOD_PAPER_NAME)) efficiency_table_latex = efficiency_table_latex.replace('Method', textbf('Method')) for system_name in system_names: efficiency_table_latex = efficiency_table_latex.replace(system_name, textbf(system_name)) for column_name in column_names: efficiency_table_latex = efficiency_table_latex.replace(column_name, textbf(column_name)) print(efficiency_table_latex) def print_nonequilibrium_relative_efficiencies(nonequilibrium_submissions): """Print relative efficiencies w.r.t. for the nonequilibrium estimators table.""" system_names = ['CB8-G3', 'OA-G3', 'OA-G6'] def _get_free_energy_array(submission, system_name, step=1, max_c=100, get_asymptotic=False): n_replicates = 5 system_data = submission.data[submission.data['System name'] == system_name] free_energy_array = np.empty(shape=(n_replicates, int(max_c/step))) for i in range(n_replicates): system_id = system_name + '-' + str(i) system_id_data = system_data[system_data['System ID'] == system_id] free_energy_array[i] = system_id_data[DG_KEY].values[:max_c:step] if get_asymptotic: mean_free_energies = submission.mean_free_energies() asymptotic = mean_free_energies[mean_free_energies['System name'] == system_name][DG_KEY].values[-1] return free_energy_array, asymptotic return free_energy_array # Use GROMACS/NS-DS/SB-long as reference method. reference_submission = [s for s in nonequilibrium_submissions if s.paper_name == 'GROMACS/NS-DS/SB-long'][0] # Also remove the other BAR submission. nonequilibrium_submissions = [s for s in nonequilibrium_submissions if 'GROMACS/NS-DS/SB' not in s.paper_name] # Get only the first 50 as the 1-directional estimators only have half the cost. free_energy_ref = {} asymptotic_ref = {} for system_name in system_names: DG, asympt = _get_free_energy_array(reference_submission, system_name, max_c=50, get_asymptotic=True) free_energy_ref[system_name] = DG asymptotic_ref[system_name] = asympt for submission in nonequilibrium_submissions: print(submission.paper_name, end='') for system_name in system_names: free_energy_sub = _get_free_energy_array(submission, system_name, step=2) rel_eff, cis = compute_all_relative_efficiencies( free_energy_ref[system_name], free_energy_sub, ci=0.95, n_bootstrap_samples=1000, asymptotic_free_energy_A=asymptotic_ref[system_name], asymptotic_free_energy_B=asymptotic_ref[system_name] ) for i, stat_name in enumerate(['std', 'bias', 'RMSE']): print(r' & {:.1f}$_{{\raisem{{2pt}}{{{:.1f}}}}}^{{\mathstrut {:.1f}}}$'.format(rel_eff[i], cis[i][0], cis[i][1]), end='') print(r' \\') def print_final_prediction_table(submissions, yank_analysis): """Plot the table containing the fina binding free energy predictions for all replicates.""" for submission in submissions + [yank_analysis]: # GROMACS/EE-fullequil predictions are identical to GROMACS/EE if submission.paper_name == 'GROMACS/EE-fullequil': continue if isinstance(submission, YankSamplingAnalysis): submission_data = yank_analysis.get_free_energies_from_iteration(final_iteration=YANK_N_ITERATIONS) else: submission_data = submission.data submission_data = submission_data[submission_data['Simulation percentage'] == 100] row_str = submission.paper_name + ' & ' submission_final_DGs = [] for system_id in submission_data['System ID'].unique(): # GROMACS/EE doesn't have predictions for CB8-G3, and the # GROMACS/NS-DS/SB-long protocol was applied only to CB8-G3. if (('GROMACS/EE' in submission.paper_name and 'CB8-G3' in system_id) or (submission.paper_name == 'GROMACS/NS-DS/SB-long' and 'OA' in system_id)): submission_final_DGs.append('') continue dg = submission_data.loc[submission_data['System ID'] == system_id, DG_KEY].values[0] ddg = submission_data.loc[submission_data['System ID'] == system_id, DDG_KEY].values[0] dg, ddg = reduce_to_first_significant_digit(dg, ddg) submission_final_DGs.append(r'{} $\pm$ {}'.format(dg, ddg)) row_str += ' & '.join(submission_final_DGs) + r' \\' print(row_str) # ============================================================================= # SUPPORTING INFORMATION - SINGLE TRAJECTORIES # ============================================================================= def plot_single_trajectories_figures(axes, system_data, system_mean_data, reference_system_mean_data=None, plot_errors=True, plot_methods_uncertainties=True): """Plot individual free energy trajectories and standard deviations for a single method and system.""" system_name = system_data['System name'].unique()[0] palette_mean = sns.color_palette('pastel') submission_mean_color = 'black' reference_mean_color = palette_mean[9] # Plot the method uncertainties of the single replicate trajectories. # First scale the number of energy evaluations. system_data.loc[:,'N energy evaluations'] /= N_ENERGY_EVALUATIONS_SCALE # Plot the 5 replicates individual trajectories. # First remove the initial predictions that are 0.0 (i.e. there is no estimate). ax = axes[0] system_data = system_data[system_data[DG_KEY] != 0.0] sns.lineplot(data=system_data, x='N energy evaluations', y=DG_KEY, hue='System ID', palette='bright', ax=ax, alpha=0.6) # Plot the submission mean trajectory with CI. plot_mean_free_energy(system_mean_data, x='N energy evaluations', ax=ax, color_mean=submission_mean_color, plot_ci=False, color_ci=submission_mean_color, label='Best estimate', scale_n_energy_evaluations=True) # Plot YANK mean trajectory with CI. if reference_system_mean_data is not None: plot_mean_free_energy(reference_system_mean_data, x='N energy evaluations', ax=ax, color_mean=reference_mean_color, plot_ci=False, color_ci=reference_mean_color, label='Reference estimate', scale_n_energy_evaluations=True) ax.set_title(system_name) # Add the y-label only on the leftmost Axis. if system_name != 'CB8-G3': ax.set_ylabel('') # Remove the legend for now, which will be added at the end after tighting up the plot. ax.get_legend().remove() # Create a bias axis. if reference_system_mean_data is not None: ref_free_energy = reference_free_energies.loc[system_name, DG_KEY] with sns.axes_style('white'): ax2 = ax.twinx() # Plot a vertical line to make the scale. vertical_line = np.linspace(*ax.get_ylim()) - ref_free_energy ax2.plot([50] * len(vertical_line), vertical_line, alpha=0.0001) ax2.grid(alpha=0.5, linestyle='dashed', zorder=0) # We add the bias y-label only on the rightmost Axis. if system_name == 'OA-G6': ax2.set_ylabel('Bias to reference [kcal/mol]') # Set the 0 of the twin axis to the YANK reference free energy. align_yaxis(ax, ref_free_energy, ax2, 0.0) if plot_errors: # The x-axis is shared between the 2 rows so we can plot the ticks only in the bottom one. ax.xaxis.set_ticklabels([]) ax.set_xlabel('') ax = axes[1] # REVO uses the mean of the 5 replicates to estimate the # uncertainty so it doesn't add information. if plot_methods_uncertainties: sns.lineplot(data=system_data, x='N energy evaluations', y=DDG_KEY, hue='System ID', palette='bright', ax=ax, alpha=0.6) # The legend is added later at the top. ax.get_legend().remove() # Plot the standard deviation of the free energy trajectories. # submission_std = system_mean_data['std'] submission_std = system_mean_data['unbiased_std'] # cost = system_mean_data['Simulation percentage'].values cost = system_mean_data['N energy evaluations'].values / N_ENERGY_EVALUATIONS_SCALE ax.plot(cost, submission_std, color=submission_mean_color) # Plot confidence interval around standard deviation. submission_std_low_ci = system_mean_data['unbiased_std_low_CI'].values submission_std_up_ci = system_mean_data['unbiased_std_up_CI'].values ax.fill_between(cost, submission_std_low_ci, submission_std_up_ci, alpha=0.35, color='gray') if reference_system_mean_data is not None: # reference_std = reference_system_mean_data['std'] reference_std = reference_system_mean_data['unbiased_std'] ax.plot(cost, reference_std, color=reference_mean_color) # Only the central plot shows the x-label. ax.set_xlabel('') # Add the y-label only on the leftmost Axis. if system_name != 'CB8-G3': ax.set_ylabel('') else: ax.set_ylabel('std($\Delta$G) [kcal/mol]') # Set x limits. for ax in axes: ax.set_xlim((0, max(system_data['N energy evaluations']))) def plot_all_single_trajectories_figures(submissions, yank_analysis, plot_errors=True, output_path_dir=None): """Individual plots for each method with the 5 individual free energy and uncertainty trajectories.""" sns.set_style('whitegrid') sns.set_context('paper') if output_path_dir is None: output_path_dir = os.path.join(SAMPLING_PAPER_DIR_PATH, 'SI_Figure-individual-trajectories/') os.makedirs(output_path_dir, exist_ok=True) # -------------------- # # Plot submission data # # -------------------- # # Remove nonequilibrium-switching calculations with single-direction estimators. submissions = [s for s in submissions if ('Jarz' not in s.paper_name and 'Gauss' not in s.paper_name)] for submission in submissions + [yank_analysis]: # CB8-G3 calculations for GROMACS/EE did not converge yet. if submission.name == 'Expanded-ensemble/MBAR': submission.data = submission.data[submission.data['System name'] != 'CB8-G3'] # REVO uses the mean of the 5 replicates to estimate the # uncertainty so it doesn't add information. if 'REVO' in submission.paper_name: plot_methods_uncertainties = False else: plot_methods_uncertainties = True if not isinstance(submission, YankSamplingAnalysis): mean_free_energies = submission.mean_free_energies() unique_system_names = submission.data['System name'].unique() else: unique_system_names = sorted(submission.system_names) # Create a figure with 3 axes (one for each system). n_systems = len(unique_system_names) if plot_errors: # The second row will plot the errors. fig, axes = plt.subplots(nrows=2, ncols=n_systems, figsize=(7.25, 4.8)) trajectory_axes = axes[0] else: fig, axes = plt.subplots(nrows=1, ncols=n_systems, figsize=(7.25, 2.4)) trajectory_axes = axes # Set figure title. fig.suptitle(submission.paper_name) # Determine range of data across systems. min_DG = np.inf max_DG = -np.inf min_dDG = np.inf max_dDG = -np.inf # for system_name in unique_system_names: for ax_idx, system_name in enumerate(unique_system_names): if isinstance(submission, YankSamplingAnalysis): data = submission.get_free_energies_from_iteration(final_iteration=YANK_N_ITERATIONS, system_name=system_name) mean_data = submission.get_free_energies_from_iteration(final_iteration=YANK_N_ITERATIONS, system_name=system_name, mean_trajectory=True) else: # Select the data for only this host-guest system. data = submission.data[submission.data['System name'] == system_name] mean_data = mean_free_energies[mean_free_energies['System name'] == system_name] plot_single_trajectories_figures(axes[:,ax_idx], data, mean_data, plot_errors=plot_errors, reference_system_mean_data=None, plot_methods_uncertainties=plot_methods_uncertainties) # Collect max and min data to determine axes range. min_DG = min(min_DG, min(data[DG_KEY]), min(mean_data[DG_KEY])) max_DG = max(max_DG, max(data[DG_KEY]), max(mean_data[DG_KEY])) min_dDG = min(min_dDG, min(data[DDG_KEY]), min(mean_data['std'])) max_dDG = max(max_dDG, max(data[DDG_KEY]), max(mean_data['std'])) # Set limits. for i in range(len(unique_system_names)): axes[0][i].set_ylim((min_DG, max_DG)) axes[1][i].set_ylim((min_dDG, max_dDG)) # Keep ticks only in external plots. axes[0][i].set_xticklabels([]) for i in range(1, len(unique_system_names)): axes[0][i].set_yticklabels([]) axes[1][i].set_yticklabels([]) # The x-label is shown only in the central plot. axes[-1][1].set_xlabel('N energy evaluations [10$^6$]') plt.tight_layout(pad=0.2, rect=[0.0, 0.0, 1.0, 0.85]) # Create legend. # The first handle/label is the legend title "System ID" so we get rid of it. handles, labels = trajectory_axes[0].get_legend_handles_labels() labels = ['replicate ' + str(i) for i in range(5)] + labels[6:] bbox_to_anchor = (-0.1, 1.35) trajectory_axes[0].legend(handles=handles[1:], labels=labels, loc='upper left', bbox_to_anchor=bbox_to_anchor, ncol=6, fancybox=True, labelspacing=0.8, handletextpad=0.5, columnspacing=1.2) # Save figure. output_file_name = 'replicates-{}-{}'.format(submission.file_name, submission.receipt_id) plt.savefig(os.path.join(output_path_dir, output_file_name + '.pdf')) # plt.savefig(os.path.join(output_path_dir, output_file_name + '.png'), dpi=300) # plt.show() # ============================================================================= # SUPPORTING INFORMATION - HREX/MBAR STATISTICAL INEFFICIENCY ANALYSIS # ============================================================================= def plot_hrex_stat_ineff_trajectories(): """Individual plots for HREX with the 5 individual free energy and uncertainty trajectories as a function of the statistical inefficiency.""" sns.set_context('paper') # Limits of y-axis (free energies, uncertainties) by system. y_limits = { 'CB8-G3': [(-14, -10), (0, 2)], 'OA-G3': [(-9, -5), (0, 1.5)], 'OA-G6': [(-9, -5), (0, 1.5)], } # Create output dir. output_path_dir = os.path.join(SAMPLING_PAPER_DIR_PATH, 'SI_Figure-statistical-inefficiency') os.makedirs(output_path_dir, exist_ok=True) # Read the data, which is organized by statistical inefficiency. # We'll then plot by system. yank_analysis_by_statineff = collections.OrderedDict() for stat_ineff in ['5', '10', '20', '50', '100', '200']: data_dir_path = os.path.join('YankAnalysis', 'CorrelationAnalysis', 'statineff-{}'.format(stat_ineff)) yank_analysis = YankSamplingAnalysis(data_dir_path) yank_analysis_by_statineff[stat_ineff] = yank_analysis # Plot by system. for system_name in ['CB8-G3', 'OA-G3', 'OA-G6']: fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(7.25, 9.8)) # Set figure title. fig.suptitle('HREX uncertainty predictions as a function of\n' 'statistical inefficiency for {}'.format(system_name)) # for system_name in unique_system_names: for stat_ineff_idx, stat_ineff in enumerate(yank_analysis_by_statineff): yank_analysis = yank_analysis_by_statineff[stat_ineff] data = yank_analysis.get_free_energies_from_iteration(final_iteration=YANK_N_ITERATIONS, system_name=system_name) mean_data = yank_analysis.get_free_energies_from_iteration(final_iteration=YANK_N_ITERATIONS, system_name=system_name, mean_trajectory=True) # Plot on the correct axis. DG_row = 2*int(stat_ineff_idx / 3) col = stat_ineff_idx % 3 stat_ineff_axes = axes[DG_row:DG_row+2, col] plot_single_trajectories_figures(stat_ineff_axes, data, mean_data, plot_errors=True, reference_system_mean_data=None, plot_methods_uncertainties=True) # Set titles and limits. title = 'Statistical inefficiency: {} ps'.format(stat_ineff) if DG_row > 0: title = '\n' + title stat_ineff_axes[0].set_title(title, fontweight='bold') stat_ineff_axes[0].set_ylim(y_limits[system_name][0]) stat_ineff_axes[1].set_ylim(y_limits[system_name][1]) stat_ineff_axes[0].set_ylabel('$\Delta$G [kcal/mol]') stat_ineff_axes[1].set_ylabel('std($\Delta$G) [kcal/mol]') # Keep ticks only in external plots. for row_idx in range(axes.shape[0]): for col_idx in range(axes.shape[1]): if row_idx != len(axes[0]) - 1: axes[row_idx][col_idx].set_xticklabels([]) if col_idx != 0: axes[row_idx][col_idx].set_ylabel('') axes[row_idx][col_idx].set_yticklabels([]) # Set x label. axes[-1][1].set_xlabel('N energy evaluations [10$^6$]') plt.tight_layout(pad=0.0, rect=[0.0, 0.0, 1.0, 0.88]) # Create legend. # The first handle/label is the legend title "System ID" so we get rid of it. handles, labels = axes[0][0].get_legend_handles_labels() labels = ['replicate ' + str(i) for i in range(5)] + labels[6:] bbox_to_anchor = (0.05, 1.35) axes[0][0].legend(handles=handles[1:], labels=labels, loc='upper left', bbox_to_anchor=bbox_to_anchor, ncol=6, fancybox=True, labelspacing=0.8, handletextpad=0.5, columnspacing=1.2) # Save figure. output_file_name = 'statineff-{}'.format(system_name) plt.savefig(os.path.join(output_path_dir, output_file_name + '.pdf')) # plt.savefig(os.path.join(output_path_dir, output_file_name + '.png'), dpi=300) # plt.show() # ============================================================================= # MAIN # ============================================================================= if __name__ == '__main__': sns.set_style('whitegrid') sns.set_context('paper') # Read reference values. yank_analysis = YankSamplingAnalysis(YANK_ANALYSIS_DIR_PATH) # Obtain free energies and final reference values. mean_reference_free_energies = yank_analysis.get_free_energies_from_iteration(YANK_N_ITERATIONS, mean_trajectory=True) reference_free_energies = mean_reference_free_energies[mean_reference_free_energies['Simulation percentage'] == 100] reference_free_energies.set_index('System name', inplace=True) # Compute efficiency of reference. reference_efficiencies = {} for system_name in mean_reference_free_energies['System name'].unique(): mean_data = mean_reference_free_energies[mean_reference_free_energies ['System name'] == system_name] reference_efficiencies[system_name], n_discarded = fit_efficiency(mean_data) # Import user map. with open('../SubmissionsDoNotUpload/SAMPL6_user_map.csv', 'r') as f: user_map = pd.read_csv(f) # Load submissions data. We do OA and TEMOA together. all_submissions = load_submissions(SamplingSubmission, SAMPLING_SUBMISSIONS_DIR_PATH, user_map) # Remove AMBER/TI. all_submissions = [s for s in all_submissions if s.name not in ['Langevin/Virtual Bond/TI']] # Create an extra submission for GROMACS/EE where the full cost of equilibration has been taken into account. gromacs_ee_submission = copy.deepcopy([s for s in all_submissions if s.paper_name == 'GROMACS/EE'][0]) gromacs_ee_submission.paper_name = 'GROMACS/EE-fullequil' gromacs_ee_submission.file_name = 'EENVT-fullequil' data = gromacs_ee_submission.data # Shortcut. mean_free_energies = gromacs_ee_submission.mean_free_energies() for system_name in ['OA-G3', 'OA-G6']: mean_data = mean_free_energies[mean_free_energies['System name'] == system_name] first_nonzero_idx = np.nonzero(mean_data[DG_KEY].values)[0][0] full_equilibration_cost = mean_data['N energy evaluations'].values[first_nonzero_idx] * 4 for i in data[data['System name'] == system_name].index: data.at[i, 'N energy evaluations'] += full_equilibration_cost all_submissions.append(gromacs_ee_submission) # Sort the submissions to have all pot and tables in the same order. all_submissions = sorted(all_submissions, key=lambda s: s.paper_name) # Separate the main submissions from the data about nonequilibrium estimators. main_submissions = [s for s in all_submissions if not ('Jarz' in s.paper_name or 'Gauss' in s.paper_name)] noneq_submissions = [s for s in all_submissions if 'NS' in s.paper_name] # Export YANK analysis and submissions to CSV/JSON tables. yank_analysis.export(os.path.join(SAMPLING_DATA_DIR_PATH, 'reference_free_energies')) for s in main_submissions: file_base_path = os.path.join(SAMPLING_DATA_DIR_PATH, s.receipt_id + '-reference') yank_analysis.export_by_submission(file_base_path, s) export_submissions(all_submissions, reference_free_energies) # Create example trajectory for the figure describing the challenge process. plot_example_bias_variance(yank_analysis, max_n_eval_percentage=0.4, mixed_proportion=0.3) # Cartoon explaining mean error and relative efficiency. plot_mean_error_cartoon() # Create figure with free energy, standard deviation, and bias as a function of computational cost. plot_all_entries_trajectory(main_submissions, yank_analysis, zoomed=False) plot_all_entries_trajectory(main_submissions, yank_analysis, zoomed=True) # Create results and efficiency table. print_relative_efficiency_table(main_submissions, yank_analysis, print_bias_corrected=False) # Plot nonequilibrium-switching single-direction estimator. plot_all_nonequilibrium_switching(noneq_submissions) # Plot sensitivity analysis figure. plot_restraint_and_barostat_analysis() # Plot figure for HREX bias analysis. plot_yank_bias() # Supporting information # ---------------------- # Absolute/relative efficiency as a function of the computational cost. plot_relative_efficiencies(main_submissions, yank_analysis) plot_relative_efficiencies(main_submissions, yank_analysis, ci=None, same_plot=True) plot_absolute_efficiencies(main_submissions, yank_analysis) # Relative efficiency for uni/bi-directional estimators. print_nonequilibrium_relative_efficiencies(noneq_submissions) # Plot replicate predictions table. print_final_prediction_table(all_submissions, yank_analysis) # Plot individual trajectories. plot_all_single_trajectories_figures(all_submissions, yank_analysis) # Plot statistical inefficiency analysis. plot_hrex_stat_ineff_trajectories() # Supporting information for bias section. output_dir_path = os.path.join(SAMPLING_PAPER_DIR_PATH, 'SI_Figure-bias_hrex') plot_decomposition('CB8-G3', starting_iteration=5, type='phase', output_file_path=output_dir_path + '/free-energy-phase-decomposition.pdf')) plot_decomposition('CB8-G3', starting_iteration=5, type='entropy-enthalpy', output_file_path=output_dir_path + '/free-energy-entropy-decomposition.pdf')
mit
felipemontefuscolo/bitme
get_bitmex_candles.py
1
4122
#!/usr/bin/env python import sys import time import swagger_client from swagger_client.rest import ApiException from utils.utils import smart_open import argparse import pandas as pd MAX_NUM_CANDLES_BITMEX = 500 def print_file(file_or_stdout, api_instance, bin_size, partial, symbol, reverse, start_time, end_time): chunks = split_in_chunks(start_time, end_time, MAX_NUM_CANDLES_BITMEX, bin_size) with smart_open(file_or_stdout) as fh: print("time,open,high,low,close,volume", file=fh) num_pages = len(chunks) for i in range(num_pages): chunk = chunks[i] s = chunk[0] e = chunk[1] count = (e - s) / pd.Timedelta(bin_size) page = api_instance.trade_get_bucketed( bin_size=bin_size, partial=partial, symbol=symbol, count=count, start=0.0, reverse=reverse, start_time=s, end_time=e) print("from {} to {}: {} candles downloaded".format(s, e, len(page))) # TODO: bitmex has a bug where the high is not the highest value !!!!! for line in reversed(page): print(','.join([line.timestamp.strftime('%Y-%m-%dT%H:%M:%S'), str(line.open), str(max(line.high, line.open)), str(min(line.low, line.open)), str(line.close), str(line.volume)]), file=fh) sys.stdout.write( "progress: completed %d out of %d pages (%.2f%%) \r" % (i + 1, num_pages, 100 * float(i + 1) / num_pages)) sys.stdout.flush() time.sleep(1.001) print("") def split_in_chunks(start: pd.Timedelta, end: pd.Timedelta, chunk_size: int, bucket_size: str): i = start r = [] dt = chunk_size * pd.Timedelta(bucket_size) while i <= end: r += [(i, min(end, i + dt))] i += dt return r def get_args(args=None, namespace=None): parser = argparse.ArgumentParser(description="Get bitmex data") parser.add_argument('-b', '--begin-time', type=pd.Timestamp, required=True, help="Example: '2018-04-01T00:00:01'") parser.add_argument('-e', '--end-time', type=pd.Timestamp, required=True, help="Example: '2018-04-02T00:00:01'") parser.add_argument('-s', '--symbol', type=str, default='XBTUSD', help='Instrument symbol. Send a bare series (e.g. XBU) to get data for the nearest expiring' 'contract in that series. You can also send a timeframe, e.g. `XBU:monthly`. ' 'Timeframes are `daily`, `weekly`, `monthly`, `quarterly`, and `biquarterly`. (optional)') parser.add_argument('-z', '--bin-size', choices=('1m', '5m', '1h', '1d'), default='1m', type=str, help='Time interval to bucket by') parser.add_argument('-o', '--file-or-stdout', type=str, required=True, help='Output filename or "-" for stdout') parser.add_argument('--partial', action='store_true', default=False, ) args = parser.parse_args(args, namespace) return args def main(): args = get_args() # create an instance of the API class configuration = swagger_client.Configuration() configuration.host = 'https://www.bitmex.com/api/v1' api_instance = swagger_client.TradeApi(swagger_client.ApiClient(configuration)) print("print to file " + (args.file_or_stdout if args.file_or_stdout is not '-' else 'std output')) try: print_file(file_or_stdout=args.file_or_stdout, api_instance=api_instance, bin_size=args.bin_size, partial=args.partial, symbol=args.symbol, reverse=False, start_time=args.begin_time, end_time=args.end_time) except ApiException as e: print("Exception when calling TradeApi->trade_get_bucketed: %s\n" % e) return 0 if __name__ == "__main__": sys.exit(main())
mpl-2.0
rsignell-usgs/PySeidon
pyseidon/tidegaugeClass/plotsTidegauge.py
2
1096
#!/usr/bin/python2.7 # encoding: utf-8 from __future__ import division import numpy as np import matplotlib.pyplot as plt import matplotlib.tri as Tri import matplotlib.ticker as ticker import seaborn class PlotsTidegauge: """'Plots' subset of Tidegauge class gathers plotting functions""" def __init__(self, variable, debug=False): self._var = variable def plot_xy(self, x, y, title=' ', xLabel=' ', yLabel=' '): """ Simple X vs Y plot Inputs: ------ - x = 1D array - y = 1D array """ fig = plt.figure(figsize=(18,10)) plt.rc('font',size='22') self._fig = plt.plot(x, y, label=title) scale = 1 ticks = ticker.FuncFormatter(lambda lon, pos: '{0:g}'.format(lon/scale)) plt.ylabel(yLabel) plt.xlabel(xLabel) #plt.legend() plt.show() #TR_comments: templates # def whatever(self, debug=False): # if debug or self._debug: # print 'Start whatever...' # # if debug or self._debug: # print '...Passed'
agpl-3.0
mxjl620/scikit-learn
examples/manifold/plot_mds.py
261
2616
""" ========================= Multi-dimensional scaling ========================= An illustration of the metric and non-metric MDS on generated noisy data. The reconstructed points using the metric MDS and non metric MDS are slightly shifted to avoid overlapping. """ # Author: Nelle Varoquaux <nelle.varoquaux@gmail.com> # Licence: BSD print(__doc__) import numpy as np from matplotlib import pyplot as plt from matplotlib.collections import LineCollection from sklearn import manifold from sklearn.metrics import euclidean_distances from sklearn.decomposition import PCA n_samples = 20 seed = np.random.RandomState(seed=3) X_true = seed.randint(0, 20, 2 * n_samples).astype(np.float) X_true = X_true.reshape((n_samples, 2)) # Center the data X_true -= X_true.mean() similarities = euclidean_distances(X_true) # Add noise to the similarities noise = np.random.rand(n_samples, n_samples) noise = noise + noise.T noise[np.arange(noise.shape[0]), np.arange(noise.shape[0])] = 0 similarities += noise mds = manifold.MDS(n_components=2, max_iter=3000, eps=1e-9, random_state=seed, dissimilarity="precomputed", n_jobs=1) pos = mds.fit(similarities).embedding_ nmds = manifold.MDS(n_components=2, metric=False, max_iter=3000, eps=1e-12, dissimilarity="precomputed", random_state=seed, n_jobs=1, n_init=1) npos = nmds.fit_transform(similarities, init=pos) # Rescale the data pos *= np.sqrt((X_true ** 2).sum()) / np.sqrt((pos ** 2).sum()) npos *= np.sqrt((X_true ** 2).sum()) / np.sqrt((npos ** 2).sum()) # Rotate the data clf = PCA(n_components=2) X_true = clf.fit_transform(X_true) pos = clf.fit_transform(pos) npos = clf.fit_transform(npos) fig = plt.figure(1) ax = plt.axes([0., 0., 1., 1.]) plt.scatter(X_true[:, 0], X_true[:, 1], c='r', s=20) plt.scatter(pos[:, 0], pos[:, 1], s=20, c='g') plt.scatter(npos[:, 0], npos[:, 1], s=20, c='b') plt.legend(('True position', 'MDS', 'NMDS'), loc='best') similarities = similarities.max() / similarities * 100 similarities[np.isinf(similarities)] = 0 # Plot the edges start_idx, end_idx = np.where(pos) #a sequence of (*line0*, *line1*, *line2*), where:: # linen = (x0, y0), (x1, y1), ... (xm, ym) segments = [[X_true[i, :], X_true[j, :]] for i in range(len(pos)) for j in range(len(pos))] values = np.abs(similarities) lc = LineCollection(segments, zorder=0, cmap=plt.cm.hot_r, norm=plt.Normalize(0, values.max())) lc.set_array(similarities.flatten()) lc.set_linewidths(0.5 * np.ones(len(segments))) ax.add_collection(lc) plt.show()
bsd-3-clause
RuthAngus/LSST-max
code/GP_periodogram.py
1
1066
from __future__ import print_function import numpy as np import matplotlib.pyplot as plt from GProtation import make_plot, lnprob, neglnlike import emcee import time import george from george.kernels import ExpSquaredKernel, ExpSine2Kernel import scipy.optimize as spo def GP_periodogram(x, y, yerr, p_init, plims, N): """ This function takes a light curves and attempts to produce a GP periodogram. It returns the value of the highest peak. The kernel hyperparameters are optimised over a grid of periods. This is also a "profile likelihood". x, y, yerr: the light curve. p_init: the initial guess for the period. plims: the (log) boundaries for the grid. N: the number of grid points. """ # create the grid periods = np.linspace(np.exp(plims[0], np.exp(plims[1], 10) # initial hyperparameters if __name__ == "__main__": # fake data x = np.arange(0, 10, 100) p = 2 err = .1 y = np.sin(2*np.pi*(1./p)*x) + np.random.randn(100)*err yerr = np.ones_like(y) * err p_init, plims = 2, np.log(.1, 5) GP_periodogram(x, y, yerr, p_init, plims, 10)
mit
equialgo/scikit-learn
examples/hetero_feature_union.py
81
6241
""" ============================================= Feature Union with Heterogeneous Data Sources ============================================= Datasets can often contain components of that require different feature extraction and processing pipelines. This scenario might occur when: 1. Your dataset consists of heterogeneous data types (e.g. raster images and text captions) 2. Your dataset is stored in a Pandas DataFrame and different columns require different processing pipelines. This example demonstrates how to use :class:`sklearn.feature_extraction.FeatureUnion` on a dataset containing different types of features. We use the 20-newsgroups dataset and compute standard bag-of-words features for the subject line and body in separate pipelines as well as ad hoc features on the body. We combine them (with weights) using a FeatureUnion and finally train a classifier on the combined set of features. The choice of features is not particularly helpful, but serves to illustrate the technique. """ # Author: Matt Terry <matt.terry@gmail.com> # # License: BSD 3 clause from __future__ import print_function import numpy as np from sklearn.base import BaseEstimator, TransformerMixin from sklearn.datasets import fetch_20newsgroups from sklearn.datasets.twenty_newsgroups import strip_newsgroup_footer from sklearn.datasets.twenty_newsgroups import strip_newsgroup_quoting from sklearn.decomposition import TruncatedSVD from sklearn.feature_extraction import DictVectorizer from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics import classification_report from sklearn.pipeline import FeatureUnion from sklearn.pipeline import Pipeline from sklearn.svm import SVC class ItemSelector(BaseEstimator, TransformerMixin): """For data grouped by feature, select subset of data at a provided key. The data is expected to be stored in a 2D data structure, where the first index is over features and the second is over samples. i.e. >> len(data[key]) == n_samples Please note that this is the opposite convention to scikit-learn feature matrixes (where the first index corresponds to sample). ItemSelector only requires that the collection implement getitem (data[key]). Examples include: a dict of lists, 2D numpy array, Pandas DataFrame, numpy record array, etc. >> data = {'a': [1, 5, 2, 5, 2, 8], 'b': [9, 4, 1, 4, 1, 3]} >> ds = ItemSelector(key='a') >> data['a'] == ds.transform(data) ItemSelector is not designed to handle data grouped by sample. (e.g. a list of dicts). If your data is structured this way, consider a transformer along the lines of `sklearn.feature_extraction.DictVectorizer`. Parameters ---------- key : hashable, required The key corresponding to the desired value in a mappable. """ def __init__(self, key): self.key = key def fit(self, x, y=None): return self def transform(self, data_dict): return data_dict[self.key] class TextStats(BaseEstimator, TransformerMixin): """Extract features from each document for DictVectorizer""" def fit(self, x, y=None): return self def transform(self, posts): return [{'length': len(text), 'num_sentences': text.count('.')} for text in posts] class SubjectBodyExtractor(BaseEstimator, TransformerMixin): """Extract the subject & body from a usenet post in a single pass. Takes a sequence of strings and produces a dict of sequences. Keys are `subject` and `body`. """ def fit(self, x, y=None): return self def transform(self, posts): features = np.recarray(shape=(len(posts),), dtype=[('subject', object), ('body', object)]) for i, text in enumerate(posts): headers, _, bod = text.partition('\n\n') bod = strip_newsgroup_footer(bod) bod = strip_newsgroup_quoting(bod) features['body'][i] = bod prefix = 'Subject:' sub = '' for line in headers.split('\n'): if line.startswith(prefix): sub = line[len(prefix):] break features['subject'][i] = sub return features pipeline = Pipeline([ # Extract the subject & body ('subjectbody', SubjectBodyExtractor()), # Use FeatureUnion to combine the features from subject and body ('union', FeatureUnion( transformer_list=[ # Pipeline for pulling features from the post's subject line ('subject', Pipeline([ ('selector', ItemSelector(key='subject')), ('tfidf', TfidfVectorizer(min_df=50)), ])), # Pipeline for standard bag-of-words model for body ('body_bow', Pipeline([ ('selector', ItemSelector(key='body')), ('tfidf', TfidfVectorizer()), ('best', TruncatedSVD(n_components=50)), ])), # Pipeline for pulling ad hoc features from post's body ('body_stats', Pipeline([ ('selector', ItemSelector(key='body')), ('stats', TextStats()), # returns a list of dicts ('vect', DictVectorizer()), # list of dicts -> feature matrix ])), ], # weight components in FeatureUnion transformer_weights={ 'subject': 0.8, 'body_bow': 0.5, 'body_stats': 1.0, }, )), # Use a SVC classifier on the combined features ('svc', SVC(kernel='linear')), ]) # limit the list of categories to make running this example faster. categories = ['alt.atheism', 'talk.religion.misc'] train = fetch_20newsgroups(random_state=1, subset='train', categories=categories, ) test = fetch_20newsgroups(random_state=1, subset='test', categories=categories, ) pipeline.fit(train.data, train.target) y = pipeline.predict(test.data) print(classification_report(y, test.target))
bsd-3-clause
sodafree/backend
build/ipython/docs/examples/parallel/dagdeps.py
6
3566
"""Example for generating an arbitrary DAG as a dependency map. This demo uses networkx to generate the graph. Authors ------- * MinRK """ import networkx as nx from random import randint, random from IPython import parallel def randomwait(): import time from random import random time.sleep(random()) return time.time() def random_dag(nodes, edges): """Generate a random Directed Acyclic Graph (DAG) with a given number of nodes and edges.""" G = nx.DiGraph() for i in range(nodes): G.add_node(i) while edges > 0: a = randint(0,nodes-1) b=a while b==a: b = randint(0,nodes-1) G.add_edge(a,b) if nx.is_directed_acyclic_graph(G): edges -= 1 else: # we closed a loop! G.remove_edge(a,b) return G def add_children(G, parent, level, n=2): """Add children recursively to a binary tree.""" if level == 0: return for i in range(n): child = parent+str(i) G.add_node(child) G.add_edge(parent,child) add_children(G, child, level-1, n) def make_bintree(levels): """Make a symmetrical binary tree with @levels""" G = nx.DiGraph() root = '0' G.add_node(root) add_children(G, root, levels, 2) return G def submit_jobs(view, G, jobs): """Submit jobs via client where G describes the time dependencies.""" results = {} for node in nx.topological_sort(G): with view.temp_flags(after=[ results[n] for n in G.predecessors(node) ]): results[node] = view.apply(jobs[node]) return results def validate_tree(G, results): """Validate that jobs executed after their dependencies.""" for node in G: started = results[node].metadata.started for parent in G.predecessors(node): finished = results[parent].metadata.completed assert started > finished, "%s should have happened after %s"%(node, parent) def main(nodes, edges): """Generate a random graph, submit jobs, then validate that the dependency order was enforced. Finally, plot the graph, with time on the x-axis, and in-degree on the y (just for spread). All arrows must point at least slightly to the right if the graph is valid. """ from matplotlib import pyplot as plt from matplotlib.dates import date2num from matplotlib.cm import gist_rainbow print("building DAG") G = random_dag(nodes, edges) jobs = {} pos = {} colors = {} for node in G: jobs[node] = randomwait client = parallel.Client() view = client.load_balanced_view() print("submitting %i tasks with %i dependencies"%(nodes,edges)) results = submit_jobs(view, G, jobs) print("waiting for results") view.wait() print("done") for node in G: md = results[node].metadata start = date2num(md.started) runtime = date2num(md.completed) - start pos[node] = (start, runtime) colors[node] = md.engine_id validate_tree(G, results) nx.draw(G, pos, node_list=colors.keys(), node_color=colors.values(), cmap=gist_rainbow, with_labels=False) x,y = zip(*pos.values()) xmin,ymin = map(min, (x,y)) xmax,ymax = map(max, (x,y)) xscale = xmax-xmin yscale = ymax-ymin plt.xlim(xmin-xscale*.1,xmax+xscale*.1) plt.ylim(ymin-yscale*.1,ymax+yscale*.1) return G,results if __name__ == '__main__': from matplotlib import pyplot as plt # main(5,10) main(32,96) plt.show()
bsd-3-clause
dhhagan/PAM
Python/PAM.py
1
5037
#PAM.py import re import glob, os, time from numpy import * from pylab import * def analyzeFile(fileName,delim): cols = {} indexToName = {} lineNum = 0 goodLines = 0 shortLines = 0 FILE = open(fileName,'r') for line in FILE: line = line.strip() if lineNum < 1: lineNum += 1 continue elif lineNum == 1: headings = line.split(delim) i = 0 for heading in headings: heading = heading.strip() cols[heading] = [] indexToName[i] = heading i += 1 lineNum += 1 lineLength = len(cols) else: data = line.split(delim) if len(data) == lineLength: goodLines += 1 i = 0 for point in data: point = point.strip() cols[indexToName[i]] += [point] i += 1 lineNum += 1 else: shortLines += 1 lineNum += 1 continue FILE.close return cols, indexToName, lineNum, shortLines def numericalSort(value): numbers = re.compile(r'(\d+)') parts = numbers.split(value) parts[1::2] = map(int, parts[1::2]) return parts def popDate(fileName): run = fileName.split('.')[0] runNo = run.split('_')[-1] return runNo def getFile(date,regex):#Works files = [] files = sorted((glob.glob('*'+regex+'*')),key=numericalSort,reverse=False) if date.lower() == 'last': files = files.pop() else: files = [item for item in files if re.search(date,item)] return files def plotConc(data,ozone,times): # This function plots data versus time import datetime as dt from matplotlib import pyplot as plt from matplotlib.dates import date2num #time = [dt.datetime.strptime(time,"%m/%d/%Y %I:%M:%S %p") for time in times] time = [dt.datetime.strptime(time,"%m/%d/%Y %I:%M:%S %p") for time in times] x = date2num(time) legend1 = [] legend2 = [] fig = plt.figure('Gas Concentration Readings for East St.Louis') ax1 = fig.add_subplot(111) ax2 = twinx() for key,value in data.items(): ax1.plot_date(x,data[key],'-',xdate=True) legend1.append(key) for key, value in ozone.items(): ax2.plot_date(x,ozone[key],'-.',xdate=True) legend2.append(key) title('Gas Concentrations for East St. Louis', fontsize = 12) ax1.set_ylabel(r'$Concentration(ppb)$', fontsize = 12) ax2.set_ylabel(r'$Concentration(ppb)$', fontsize = 12) xlabel(r"$Time \, Stamp$", fontsize = 12) ax1.legend(legend1,loc='upper right') ax2.legend(legend2,loc='lower right') grid(True) return def plotBankRelays(data,relays,times): # This function plots data versus time import datetime as dt from matplotlib import pyplot as plt from matplotlib.dates import date2num time = [dt.datetime.strptime(time,"%m/%d/%Y %I:%M:%S %p") for time in times] x = date2num(time) #x1 = [date.strftime("%m-%d %H:%M:%S") for date in time] legend1 = [] legend2 = [] #plt.locator_params(axis='x', nbins=4) fig = plt.figure('VAPS Thermocouple Readings: Chart 2') ax1 = fig.add_subplot(111) ax2 = twinx() for key,value in data.items(): ax1.plot_date(x,data[key],'-',xdate=True) legend1.append(key) for key,value in relays.items(): ax2.plot_date(x,relays[key],'--',xdate=True) legend2.append(key) title('VAPS Temperatures: Chart 2', fontsize = 12) ax1.set_ylabel(r'$Temperature(^oC)$', fontsize = 12) ax2.set_ylabel(r'$Relay \, States$', fontsize = 12) ax1.set_xlabel(r"$Time \, Stamp$", fontsize = 12) #print [num2date(item) for item in ax1.get_xticks()] #ax1.set_xticks(x) #ax1.set_xticklabels([date.strftime("%m-%d %H:%M %p") for date in time]) #ax1.legend(bbox_to_anchor=(0.,1.02,1.,.102),loc=3,ncol=2,mode="expand",borderaxespad=0.) ax1.legend(legend1,loc='upper right') ax2.legend(legend2,loc='lower right') #ax1.xaxis.set_major_formatter(FormatStrFormatter(date.strftime("%m-%d %H:%M:%S"))) plt.subplots_adjust(bottom=0.15) grid(True) return def goodFiles(files,goodHeaders,delim): # Good irregFiles = 0 goodFiles = [] for file in files: lineNo = 0 falseCount = 0 FILE = open(file,'r') for line in FILE: line = line.strip() if lineNo == 5: # Check all the headings to make sure the file is good head = line.split(delim) for item in head: if item in goodHeaders: continue else: falseCount += 1 if falseCount == 0: goodFiles.append(file) else: irregFiles += 1 lineNo += 1 else: lineNo += 1 continue FILE.close return goodFiles, irregFiles
mit
RachitKansal/scikit-learn
sklearn/neighbors/tests/test_approximate.py
71
18815
""" Testing for the approximate neighbor search using Locality Sensitive Hashing Forest module (sklearn.neighbors.LSHForest). """ # Author: Maheshakya Wijewardena, Joel Nothman import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_array_less from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_not_equal from sklearn.utils.testing import assert_warns_message from sklearn.utils.testing import ignore_warnings from sklearn.metrics.pairwise import pairwise_distances from sklearn.neighbors import LSHForest from sklearn.neighbors import NearestNeighbors def test_neighbors_accuracy_with_n_candidates(): # Checks whether accuracy increases as `n_candidates` increases. n_candidates_values = np.array([.1, 50, 500]) n_samples = 100 n_features = 10 n_iter = 10 n_points = 5 rng = np.random.RandomState(42) accuracies = np.zeros(n_candidates_values.shape[0], dtype=float) X = rng.rand(n_samples, n_features) for i, n_candidates in enumerate(n_candidates_values): lshf = LSHForest(n_candidates=n_candidates) lshf.fit(X) for j in range(n_iter): query = X[rng.randint(0, n_samples)].reshape(1, -1) neighbors = lshf.kneighbors(query, n_neighbors=n_points, return_distance=False) distances = pairwise_distances(query, X, metric='cosine') ranks = np.argsort(distances)[0, :n_points] intersection = np.intersect1d(ranks, neighbors).shape[0] ratio = intersection / float(n_points) accuracies[i] = accuracies[i] + ratio accuracies[i] = accuracies[i] / float(n_iter) # Sorted accuracies should be equal to original accuracies assert_true(np.all(np.diff(accuracies) >= 0), msg="Accuracies are not non-decreasing.") # Highest accuracy should be strictly greater than the lowest assert_true(np.ptp(accuracies) > 0, msg="Highest accuracy is not strictly greater than lowest.") def test_neighbors_accuracy_with_n_estimators(): # Checks whether accuracy increases as `n_estimators` increases. n_estimators = np.array([1, 10, 100]) n_samples = 100 n_features = 10 n_iter = 10 n_points = 5 rng = np.random.RandomState(42) accuracies = np.zeros(n_estimators.shape[0], dtype=float) X = rng.rand(n_samples, n_features) for i, t in enumerate(n_estimators): lshf = LSHForest(n_candidates=500, n_estimators=t) lshf.fit(X) for j in range(n_iter): query = X[rng.randint(0, n_samples)].reshape(1, -1) neighbors = lshf.kneighbors(query, n_neighbors=n_points, return_distance=False) distances = pairwise_distances(query, X, metric='cosine') ranks = np.argsort(distances)[0, :n_points] intersection = np.intersect1d(ranks, neighbors).shape[0] ratio = intersection / float(n_points) accuracies[i] = accuracies[i] + ratio accuracies[i] = accuracies[i] / float(n_iter) # Sorted accuracies should be equal to original accuracies assert_true(np.all(np.diff(accuracies) >= 0), msg="Accuracies are not non-decreasing.") # Highest accuracy should be strictly greater than the lowest assert_true(np.ptp(accuracies) > 0, msg="Highest accuracy is not strictly greater than lowest.") @ignore_warnings def test_kneighbors(): # Checks whether desired number of neighbors are returned. # It is guaranteed to return the requested number of neighbors # if `min_hash_match` is set to 0. Returned distances should be # in ascending order. n_samples = 12 n_features = 2 n_iter = 10 rng = np.random.RandomState(42) X = rng.rand(n_samples, n_features) lshf = LSHForest(min_hash_match=0) # Test unfitted estimator assert_raises(ValueError, lshf.kneighbors, X[0]) lshf.fit(X) for i in range(n_iter): n_neighbors = rng.randint(0, n_samples) query = X[rng.randint(0, n_samples)].reshape(1, -1) neighbors = lshf.kneighbors(query, n_neighbors=n_neighbors, return_distance=False) # Desired number of neighbors should be returned. assert_equal(neighbors.shape[1], n_neighbors) # Multiple points n_queries = 5 queries = X[rng.randint(0, n_samples, n_queries)] distances, neighbors = lshf.kneighbors(queries, n_neighbors=1, return_distance=True) assert_equal(neighbors.shape[0], n_queries) assert_equal(distances.shape[0], n_queries) # Test only neighbors neighbors = lshf.kneighbors(queries, n_neighbors=1, return_distance=False) assert_equal(neighbors.shape[0], n_queries) # Test random point(not in the data set) query = rng.randn(n_features).reshape(1, -1) lshf.kneighbors(query, n_neighbors=1, return_distance=False) # Test n_neighbors at initialization neighbors = lshf.kneighbors(query, return_distance=False) assert_equal(neighbors.shape[1], 5) # Test `neighbors` has an integer dtype assert_true(neighbors.dtype.kind == 'i', msg="neighbors are not in integer dtype.") def test_radius_neighbors(): # Checks whether Returned distances are less than `radius` # At least one point should be returned when the `radius` is set # to mean distance from the considering point to other points in # the database. # Moreover, this test compares the radius neighbors of LSHForest # with the `sklearn.neighbors.NearestNeighbors`. n_samples = 12 n_features = 2 n_iter = 10 rng = np.random.RandomState(42) X = rng.rand(n_samples, n_features) lshf = LSHForest() # Test unfitted estimator assert_raises(ValueError, lshf.radius_neighbors, X[0]) lshf.fit(X) for i in range(n_iter): # Select a random point in the dataset as the query query = X[rng.randint(0, n_samples)].reshape(1, -1) # At least one neighbor should be returned when the radius is the # mean distance from the query to the points of the dataset. mean_dist = np.mean(pairwise_distances(query, X, metric='cosine')) neighbors = lshf.radius_neighbors(query, radius=mean_dist, return_distance=False) assert_equal(neighbors.shape, (1,)) assert_equal(neighbors.dtype, object) assert_greater(neighbors[0].shape[0], 0) # All distances to points in the results of the radius query should # be less than mean_dist distances, neighbors = lshf.radius_neighbors(query, radius=mean_dist, return_distance=True) assert_array_less(distances[0], mean_dist) # Multiple points n_queries = 5 queries = X[rng.randint(0, n_samples, n_queries)] distances, neighbors = lshf.radius_neighbors(queries, return_distance=True) # dists and inds should not be 1D arrays or arrays of variable lengths # hence the use of the object dtype. assert_equal(distances.shape, (n_queries,)) assert_equal(distances.dtype, object) assert_equal(neighbors.shape, (n_queries,)) assert_equal(neighbors.dtype, object) # Compare with exact neighbor search query = X[rng.randint(0, n_samples)].reshape(1, -1) mean_dist = np.mean(pairwise_distances(query, X, metric='cosine')) nbrs = NearestNeighbors(algorithm='brute', metric='cosine').fit(X) distances_exact, _ = nbrs.radius_neighbors(query, radius=mean_dist) distances_approx, _ = lshf.radius_neighbors(query, radius=mean_dist) # Radius-based queries do not sort the result points and the order # depends on the method, the random_state and the dataset order. Therefore # we need to sort the results ourselves before performing any comparison. sorted_dists_exact = np.sort(distances_exact[0]) sorted_dists_approx = np.sort(distances_approx[0]) # Distances to exact neighbors are less than or equal to approximate # counterparts as the approximate radius query might have missed some # closer neighbors. assert_true(np.all(np.less_equal(sorted_dists_exact, sorted_dists_approx))) def test_radius_neighbors_boundary_handling(): X = [[0.999, 0.001], [0.5, 0.5], [0, 1.], [-1., 0.001]] n_points = len(X) # Build an exact nearest neighbors model as reference model to ensure # consistency between exact and approximate methods nnbrs = NearestNeighbors(algorithm='brute', metric='cosine').fit(X) # Build a LSHForest model with hyperparameter values that always guarantee # exact results on this toy dataset. lsfh = LSHForest(min_hash_match=0, n_candidates=n_points).fit(X) # define a query aligned with the first axis query = [[1., 0.]] # Compute the exact cosine distances of the query to the four points of # the dataset dists = pairwise_distances(query, X, metric='cosine').ravel() # The first point is almost aligned with the query (very small angle), # the cosine distance should therefore be almost null: assert_almost_equal(dists[0], 0, decimal=5) # The second point form an angle of 45 degrees to the query vector assert_almost_equal(dists[1], 1 - np.cos(np.pi / 4)) # The third point is orthogonal from the query vector hence at a distance # exactly one: assert_almost_equal(dists[2], 1) # The last point is almost colinear but with opposite sign to the query # therefore it has a cosine 'distance' very close to the maximum possible # value of 2. assert_almost_equal(dists[3], 2, decimal=5) # If we query with a radius of one, all the samples except the last sample # should be included in the results. This means that the third sample # is lying on the boundary of the radius query: exact_dists, exact_idx = nnbrs.radius_neighbors(query, radius=1) approx_dists, approx_idx = lsfh.radius_neighbors(query, radius=1) assert_array_equal(np.sort(exact_idx[0]), [0, 1, 2]) assert_array_equal(np.sort(approx_idx[0]), [0, 1, 2]) assert_array_almost_equal(np.sort(exact_dists[0]), dists[:-1]) assert_array_almost_equal(np.sort(approx_dists[0]), dists[:-1]) # If we perform the same query with a slighltly lower radius, the third # point of the dataset that lay on the boundary of the previous query # is now rejected: eps = np.finfo(np.float64).eps exact_dists, exact_idx = nnbrs.radius_neighbors(query, radius=1 - eps) approx_dists, approx_idx = lsfh.radius_neighbors(query, radius=1 - eps) assert_array_equal(np.sort(exact_idx[0]), [0, 1]) assert_array_equal(np.sort(approx_idx[0]), [0, 1]) assert_array_almost_equal(np.sort(exact_dists[0]), dists[:-2]) assert_array_almost_equal(np.sort(approx_dists[0]), dists[:-2]) def test_distances(): # Checks whether returned neighbors are from closest to farthest. n_samples = 12 n_features = 2 n_iter = 10 rng = np.random.RandomState(42) X = rng.rand(n_samples, n_features) lshf = LSHForest() lshf.fit(X) for i in range(n_iter): n_neighbors = rng.randint(0, n_samples) query = X[rng.randint(0, n_samples)].reshape(1, -1) distances, neighbors = lshf.kneighbors(query, n_neighbors=n_neighbors, return_distance=True) # Returned neighbors should be from closest to farthest, that is # increasing distance values. assert_true(np.all(np.diff(distances[0]) >= 0)) # Note: the radius_neighbors method does not guarantee the order of # the results. def test_fit(): # Checks whether `fit` method sets all attribute values correctly. n_samples = 12 n_features = 2 n_estimators = 5 rng = np.random.RandomState(42) X = rng.rand(n_samples, n_features) lshf = LSHForest(n_estimators=n_estimators) lshf.fit(X) # _input_array = X assert_array_equal(X, lshf._fit_X) # A hash function g(p) for each tree assert_equal(n_estimators, len(lshf.hash_functions_)) # Hash length = 32 assert_equal(32, lshf.hash_functions_[0].components_.shape[0]) # Number of trees_ in the forest assert_equal(n_estimators, len(lshf.trees_)) # Each tree has entries for every data point assert_equal(n_samples, len(lshf.trees_[0])) # Original indices after sorting the hashes assert_equal(n_estimators, len(lshf.original_indices_)) # Each set of original indices in a tree has entries for every data point assert_equal(n_samples, len(lshf.original_indices_[0])) def test_partial_fit(): # Checks whether inserting array is consitent with fitted data. # `partial_fit` method should set all attribute values correctly. n_samples = 12 n_samples_partial_fit = 3 n_features = 2 rng = np.random.RandomState(42) X = rng.rand(n_samples, n_features) X_partial_fit = rng.rand(n_samples_partial_fit, n_features) lshf = LSHForest() # Test unfitted estimator lshf.partial_fit(X) assert_array_equal(X, lshf._fit_X) lshf.fit(X) # Insert wrong dimension assert_raises(ValueError, lshf.partial_fit, np.random.randn(n_samples_partial_fit, n_features - 1)) lshf.partial_fit(X_partial_fit) # size of _input_array = samples + 1 after insertion assert_equal(lshf._fit_X.shape[0], n_samples + n_samples_partial_fit) # size of original_indices_[1] = samples + 1 assert_equal(len(lshf.original_indices_[0]), n_samples + n_samples_partial_fit) # size of trees_[1] = samples + 1 assert_equal(len(lshf.trees_[1]), n_samples + n_samples_partial_fit) def test_hash_functions(): # Checks randomness of hash functions. # Variance and mean of each hash function (projection vector) # should be different from flattened array of hash functions. # If hash functions are not randomly built (seeded with # same value), variances and means of all functions are equal. n_samples = 12 n_features = 2 n_estimators = 5 rng = np.random.RandomState(42) X = rng.rand(n_samples, n_features) lshf = LSHForest(n_estimators=n_estimators, random_state=rng.randint(0, np.iinfo(np.int32).max)) lshf.fit(X) hash_functions = [] for i in range(n_estimators): hash_functions.append(lshf.hash_functions_[i].components_) for i in range(n_estimators): assert_not_equal(np.var(hash_functions), np.var(lshf.hash_functions_[i].components_)) for i in range(n_estimators): assert_not_equal(np.mean(hash_functions), np.mean(lshf.hash_functions_[i].components_)) def test_candidates(): # Checks whether candidates are sufficient. # This should handle the cases when number of candidates is 0. # User should be warned when number of candidates is less than # requested number of neighbors. X_train = np.array([[5, 5, 2], [21, 5, 5], [1, 1, 1], [8, 9, 1], [6, 10, 2]], dtype=np.float32) X_test = np.array([7, 10, 3], dtype=np.float32).reshape(1, -1) # For zero candidates lshf = LSHForest(min_hash_match=32) lshf.fit(X_train) message = ("Number of candidates is not sufficient to retrieve" " %i neighbors with" " min_hash_match = %i. Candidates are filled up" " uniformly from unselected" " indices." % (3, 32)) assert_warns_message(UserWarning, message, lshf.kneighbors, X_test, n_neighbors=3) distances, neighbors = lshf.kneighbors(X_test, n_neighbors=3) assert_equal(distances.shape[1], 3) # For candidates less than n_neighbors lshf = LSHForest(min_hash_match=31) lshf.fit(X_train) message = ("Number of candidates is not sufficient to retrieve" " %i neighbors with" " min_hash_match = %i. Candidates are filled up" " uniformly from unselected" " indices." % (5, 31)) assert_warns_message(UserWarning, message, lshf.kneighbors, X_test, n_neighbors=5) distances, neighbors = lshf.kneighbors(X_test, n_neighbors=5) assert_equal(distances.shape[1], 5) def test_graphs(): # Smoke tests for graph methods. n_samples_sizes = [5, 10, 20] n_features = 3 rng = np.random.RandomState(42) for n_samples in n_samples_sizes: X = rng.rand(n_samples, n_features) lshf = LSHForest(min_hash_match=0) lshf.fit(X) kneighbors_graph = lshf.kneighbors_graph(X) radius_neighbors_graph = lshf.radius_neighbors_graph(X) assert_equal(kneighbors_graph.shape[0], n_samples) assert_equal(kneighbors_graph.shape[1], n_samples) assert_equal(radius_neighbors_graph.shape[0], n_samples) assert_equal(radius_neighbors_graph.shape[1], n_samples) def test_sparse_input(): # note: Fixed random state in sp.rand is not supported in older scipy. # The test should succeed regardless. X1 = sp.rand(50, 100) X2 = sp.rand(10, 100) forest_sparse = LSHForest(radius=1, random_state=0).fit(X1) forest_dense = LSHForest(radius=1, random_state=0).fit(X1.A) d_sparse, i_sparse = forest_sparse.kneighbors(X2, return_distance=True) d_dense, i_dense = forest_dense.kneighbors(X2.A, return_distance=True) assert_almost_equal(d_sparse, d_dense) assert_almost_equal(i_sparse, i_dense) d_sparse, i_sparse = forest_sparse.radius_neighbors(X2, return_distance=True) d_dense, i_dense = forest_dense.radius_neighbors(X2.A, return_distance=True) assert_equal(d_sparse.shape, d_dense.shape) for a, b in zip(d_sparse, d_dense): assert_almost_equal(a, b) for a, b in zip(i_sparse, i_dense): assert_almost_equal(a, b)
bsd-3-clause
ChanChiChoi/scikit-learn
sklearn/linear_model/ransac.py
191
14261
# coding: utf-8 # Author: Johannes Schönberger # # License: BSD 3 clause import numpy as np from ..base import BaseEstimator, MetaEstimatorMixin, RegressorMixin, clone from ..utils import check_random_state, check_array, check_consistent_length from ..utils.random import sample_without_replacement from ..utils.validation import check_is_fitted from .base import LinearRegression _EPSILON = np.spacing(1) def _dynamic_max_trials(n_inliers, n_samples, min_samples, probability): """Determine number trials such that at least one outlier-free subset is sampled for the given inlier/outlier ratio. Parameters ---------- n_inliers : int Number of inliers in the data. n_samples : int Total number of samples in the data. min_samples : int Minimum number of samples chosen randomly from original data. probability : float Probability (confidence) that one outlier-free sample is generated. Returns ------- trials : int Number of trials. """ inlier_ratio = n_inliers / float(n_samples) nom = max(_EPSILON, 1 - probability) denom = max(_EPSILON, 1 - inlier_ratio ** min_samples) if nom == 1: return 0 if denom == 1: return float('inf') return abs(float(np.ceil(np.log(nom) / np.log(denom)))) class RANSACRegressor(BaseEstimator, MetaEstimatorMixin, RegressorMixin): """RANSAC (RANdom SAmple Consensus) algorithm. RANSAC is an iterative algorithm for the robust estimation of parameters from a subset of inliers from the complete data set. More information can be found in the general documentation of linear models. A detailed description of the algorithm can be found in the documentation of the ``linear_model`` sub-package. Read more in the :ref:`User Guide <RansacRegression>`. Parameters ---------- base_estimator : object, optional Base estimator object which implements the following methods: * `fit(X, y)`: Fit model to given training data and target values. * `score(X, y)`: Returns the mean accuracy on the given test data, which is used for the stop criterion defined by `stop_score`. Additionally, the score is used to decide which of two equally large consensus sets is chosen as the better one. If `base_estimator` is None, then ``base_estimator=sklearn.linear_model.LinearRegression()`` is used for target values of dtype float. Note that the current implementation only supports regression estimators. min_samples : int (>= 1) or float ([0, 1]), optional Minimum number of samples chosen randomly from original data. Treated as an absolute number of samples for `min_samples >= 1`, treated as a relative number `ceil(min_samples * X.shape[0]`) for `min_samples < 1`. This is typically chosen as the minimal number of samples necessary to estimate the given `base_estimator`. By default a ``sklearn.linear_model.LinearRegression()`` estimator is assumed and `min_samples` is chosen as ``X.shape[1] + 1``. residual_threshold : float, optional Maximum residual for a data sample to be classified as an inlier. By default the threshold is chosen as the MAD (median absolute deviation) of the target values `y`. is_data_valid : callable, optional This function is called with the randomly selected data before the model is fitted to it: `is_data_valid(X, y)`. If its return value is False the current randomly chosen sub-sample is skipped. is_model_valid : callable, optional This function is called with the estimated model and the randomly selected data: `is_model_valid(model, X, y)`. If its return value is False the current randomly chosen sub-sample is skipped. Rejecting samples with this function is computationally costlier than with `is_data_valid`. `is_model_valid` should therefore only be used if the estimated model is needed for making the rejection decision. max_trials : int, optional Maximum number of iterations for random sample selection. stop_n_inliers : int, optional Stop iteration if at least this number of inliers are found. stop_score : float, optional Stop iteration if score is greater equal than this threshold. stop_probability : float in range [0, 1], optional RANSAC iteration stops if at least one outlier-free set of the training data is sampled in RANSAC. This requires to generate at least N samples (iterations):: N >= log(1 - probability) / log(1 - e**m) where the probability (confidence) is typically set to high value such as 0.99 (the default) and e is the current fraction of inliers w.r.t. the total number of samples. residual_metric : callable, optional Metric to reduce the dimensionality of the residuals to 1 for multi-dimensional target values ``y.shape[1] > 1``. By default the sum of absolute differences is used:: lambda dy: np.sum(np.abs(dy), axis=1) random_state : integer or numpy.RandomState, optional The generator used to initialize the centers. If an integer is given, it fixes the seed. Defaults to the global numpy random number generator. Attributes ---------- estimator_ : object Best fitted model (copy of the `base_estimator` object). n_trials_ : int Number of random selection trials until one of the stop criteria is met. It is always ``<= max_trials``. inlier_mask_ : bool array of shape [n_samples] Boolean mask of inliers classified as ``True``. References ---------- .. [1] http://en.wikipedia.org/wiki/RANSAC .. [2] http://www.cs.columbia.edu/~belhumeur/courses/compPhoto/ransac.pdf .. [3] http://www.bmva.org/bmvc/2009/Papers/Paper355/Paper355.pdf """ def __init__(self, base_estimator=None, min_samples=None, residual_threshold=None, is_data_valid=None, is_model_valid=None, max_trials=100, stop_n_inliers=np.inf, stop_score=np.inf, stop_probability=0.99, residual_metric=None, random_state=None): self.base_estimator = base_estimator self.min_samples = min_samples self.residual_threshold = residual_threshold self.is_data_valid = is_data_valid self.is_model_valid = is_model_valid self.max_trials = max_trials self.stop_n_inliers = stop_n_inliers self.stop_score = stop_score self.stop_probability = stop_probability self.residual_metric = residual_metric self.random_state = random_state def fit(self, X, y): """Fit estimator using RANSAC algorithm. Parameters ---------- X : array-like or sparse matrix, shape [n_samples, n_features] Training data. y : array-like, shape = [n_samples] or [n_samples, n_targets] Target values. Raises ------ ValueError If no valid consensus set could be found. This occurs if `is_data_valid` and `is_model_valid` return False for all `max_trials` randomly chosen sub-samples. """ X = check_array(X, accept_sparse='csr') y = check_array(y, ensure_2d=False) check_consistent_length(X, y) if self.base_estimator is not None: base_estimator = clone(self.base_estimator) else: base_estimator = LinearRegression() if self.min_samples is None: # assume linear model by default min_samples = X.shape[1] + 1 elif 0 < self.min_samples < 1: min_samples = np.ceil(self.min_samples * X.shape[0]) elif self.min_samples >= 1: if self.min_samples % 1 != 0: raise ValueError("Absolute number of samples must be an " "integer value.") min_samples = self.min_samples else: raise ValueError("Value for `min_samples` must be scalar and " "positive.") if min_samples > X.shape[0]: raise ValueError("`min_samples` may not be larger than number " "of samples ``X.shape[0]``.") if self.stop_probability < 0 or self.stop_probability > 1: raise ValueError("`stop_probability` must be in range [0, 1].") if self.residual_threshold is None: # MAD (median absolute deviation) residual_threshold = np.median(np.abs(y - np.median(y))) else: residual_threshold = self.residual_threshold if self.residual_metric is None: residual_metric = lambda dy: np.sum(np.abs(dy), axis=1) else: residual_metric = self.residual_metric random_state = check_random_state(self.random_state) try: # Not all estimator accept a random_state base_estimator.set_params(random_state=random_state) except ValueError: pass n_inliers_best = 0 score_best = np.inf inlier_mask_best = None X_inlier_best = None y_inlier_best = None # number of data samples n_samples = X.shape[0] sample_idxs = np.arange(n_samples) n_samples, _ = X.shape for self.n_trials_ in range(1, self.max_trials + 1): # choose random sample set subset_idxs = sample_without_replacement(n_samples, min_samples, random_state=random_state) X_subset = X[subset_idxs] y_subset = y[subset_idxs] # check if random sample set is valid if (self.is_data_valid is not None and not self.is_data_valid(X_subset, y_subset)): continue # fit model for current random sample set base_estimator.fit(X_subset, y_subset) # check if estimated model is valid if (self.is_model_valid is not None and not self.is_model_valid(base_estimator, X_subset, y_subset)): continue # residuals of all data for current random sample model y_pred = base_estimator.predict(X) diff = y_pred - y if diff.ndim == 1: diff = diff.reshape(-1, 1) residuals_subset = residual_metric(diff) # classify data into inliers and outliers inlier_mask_subset = residuals_subset < residual_threshold n_inliers_subset = np.sum(inlier_mask_subset) # less inliers -> skip current random sample if n_inliers_subset < n_inliers_best: continue if n_inliers_subset == 0: raise ValueError("No inliers found, possible cause is " "setting residual_threshold ({0}) too low.".format( self.residual_threshold)) # extract inlier data set inlier_idxs_subset = sample_idxs[inlier_mask_subset] X_inlier_subset = X[inlier_idxs_subset] y_inlier_subset = y[inlier_idxs_subset] # score of inlier data set score_subset = base_estimator.score(X_inlier_subset, y_inlier_subset) # same number of inliers but worse score -> skip current random # sample if (n_inliers_subset == n_inliers_best and score_subset < score_best): continue # save current random sample as best sample n_inliers_best = n_inliers_subset score_best = score_subset inlier_mask_best = inlier_mask_subset X_inlier_best = X_inlier_subset y_inlier_best = y_inlier_subset # break if sufficient number of inliers or score is reached if (n_inliers_best >= self.stop_n_inliers or score_best >= self.stop_score or self.n_trials_ >= _dynamic_max_trials(n_inliers_best, n_samples, min_samples, self.stop_probability)): break # if none of the iterations met the required criteria if inlier_mask_best is None: raise ValueError( "RANSAC could not find valid consensus set, because" " either the `residual_threshold` rejected all the samples or" " `is_data_valid` and `is_model_valid` returned False for all" " `max_trials` randomly ""chosen sub-samples. Consider " "relaxing the ""constraints.") # estimate final model using all inliers base_estimator.fit(X_inlier_best, y_inlier_best) self.estimator_ = base_estimator self.inlier_mask_ = inlier_mask_best return self def predict(self, X): """Predict using the estimated model. This is a wrapper for `estimator_.predict(X)`. Parameters ---------- X : numpy array of shape [n_samples, n_features] Returns ------- y : array, shape = [n_samples] or [n_samples, n_targets] Returns predicted values. """ check_is_fitted(self, 'estimator_') return self.estimator_.predict(X) def score(self, X, y): """Returns the score of the prediction. This is a wrapper for `estimator_.score(X, y)`. Parameters ---------- X : numpy array or sparse matrix of shape [n_samples, n_features] Training data. y : array, shape = [n_samples] or [n_samples, n_targets] Target values. Returns ------- z : float Score of the prediction. """ check_is_fitted(self, 'estimator_') return self.estimator_.score(X, y)
bsd-3-clause
jakobworldpeace/scikit-learn
doc/tutorial/text_analytics/solutions/exercise_02_sentiment.py
104
3139
"""Build a sentiment analysis / polarity model Sentiment analysis can be casted as a binary text classification problem, that is fitting a linear classifier on features extracted from the text of the user messages so as to guess wether the opinion of the author is positive or negative. In this examples we will use a movie review dataset. """ # Author: Olivier Grisel <olivier.grisel@ensta.org> # License: Simplified BSD import sys from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV from sklearn.datasets import load_files from sklearn.model_selection import train_test_split from sklearn import metrics if __name__ == "__main__": # NOTE: we put the following in a 'if __name__ == "__main__"' protected # block to be able to use a multi-core grid search that also works under # Windows, see: http://docs.python.org/library/multiprocessing.html#windows # The multiprocessing module is used as the backend of joblib.Parallel # that is used when n_jobs != 1 in GridSearchCV # the training data folder must be passed as first argument movie_reviews_data_folder = sys.argv[1] dataset = load_files(movie_reviews_data_folder, shuffle=False) print("n_samples: %d" % len(dataset.data)) # split the dataset in training and test set: docs_train, docs_test, y_train, y_test = train_test_split( dataset.data, dataset.target, test_size=0.25, random_state=None) # TASK: Build a vectorizer / classifier pipeline that filters out tokens # that are too rare or too frequent pipeline = Pipeline([ ('vect', TfidfVectorizer(min_df=3, max_df=0.95)), ('clf', LinearSVC(C=1000)), ]) # TASK: Build a grid search to find out whether unigrams or bigrams are # more useful. # Fit the pipeline on the training set using grid search for the parameters parameters = { 'vect__ngram_range': [(1, 1), (1, 2)], } grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1) grid_search.fit(docs_train, y_train) # TASK: print the mean and std for each candidate along with the parameter # settings for all the candidates explored by grid search. n_candidates = len(grid_search.cv_results_['params']) for i in range(n_candidates): print(i, 'params - %s; mean - %0.2f; std - %0.2f' % (grid_search.cv_results_['params'][i], grid_search.cv_results_['mean_test_score'][i], grid_search.cv_results_['std_test_score'][i])) # TASK: Predict the outcome on the testing set and store it in a variable # named y_predicted y_predicted = grid_search.predict(docs_test) # Print the classification report print(metrics.classification_report(y_test, y_predicted, target_names=dataset.target_names)) # Print and plot the confusion matrix cm = metrics.confusion_matrix(y_test, y_predicted) print(cm) # import matplotlib.pyplot as plt # plt.matshow(cm) # plt.show()
bsd-3-clause
duthchao/kaggle-galaxies
predict_augmented_npy_maxout2048_pysex.py
7
9584
""" Load an analysis file and redo the predictions on the validation set / test set, this time with augmented data and averaging. Store them as numpy files. """ import numpy as np # import pandas as pd import theano import theano.tensor as T import layers import cc_layers import custom import load_data import realtime_augmentation as ra import time import csv import os import cPickle as pickle BATCH_SIZE = 32 # 16 NUM_INPUT_FEATURES = 3 CHUNK_SIZE = 8000 # 10000 # this should be a multiple of the batch size # ANALYSIS_PATH = "analysis/try_convnet_cc_multirot_3x69r45_untied_bias.pkl" ANALYSIS_PATH = "analysis/final/try_convnet_cc_multirotflip_3x69r45_maxout2048_pysex.pkl" DO_VALID = True # disable this to not bother with the validation set evaluation DO_TEST = True # disable this to not generate predictions on the testset target_filename = os.path.basename(ANALYSIS_PATH).replace(".pkl", ".npy.gz") target_path_valid = os.path.join("predictions/final/augmented/valid", target_filename) target_path_test = os.path.join("predictions/final/augmented/test", target_filename) print "Loading model data etc." analysis = np.load(ANALYSIS_PATH) input_sizes = [(69, 69), (69, 69)] ds_transforms = [ ra.build_ds_transform(3.0, target_size=input_sizes[0]), ra.build_ds_transform(3.0, target_size=input_sizes[1]) + ra.build_augmentation_transform(rotation=45)] num_input_representations = len(ds_transforms) # split training data into training + a small validation set num_train = load_data.num_train num_valid = num_train // 10 # integer division num_train -= num_valid num_test = load_data.num_test valid_ids = load_data.train_ids[num_train:] train_ids = load_data.train_ids[:num_train] test_ids = load_data.test_ids train_indices = np.arange(num_train) valid_indices = np.arange(num_train, num_train+num_valid) test_indices = np.arange(num_test) y_valid = np.load("data/solutions_train.npy")[num_train:] print "Build model" l0 = layers.Input2DLayer(BATCH_SIZE, NUM_INPUT_FEATURES, input_sizes[0][0], input_sizes[0][1]) l0_45 = layers.Input2DLayer(BATCH_SIZE, NUM_INPUT_FEATURES, input_sizes[1][0], input_sizes[1][1]) l0r = layers.MultiRotSliceLayer([l0, l0_45], part_size=45, include_flip=True) l0s = cc_layers.ShuffleBC01ToC01BLayer(l0r) l1a = cc_layers.CudaConvnetConv2DLayer(l0s, n_filters=32, filter_size=6, weights_std=0.01, init_bias_value=0.1, dropout=0.0, partial_sum=1, untie_biases=True) l1 = cc_layers.CudaConvnetPooling2DLayer(l1a, pool_size=2) l2a = cc_layers.CudaConvnetConv2DLayer(l1, n_filters=64, filter_size=5, weights_std=0.01, init_bias_value=0.1, dropout=0.0, partial_sum=1, untie_biases=True) l2 = cc_layers.CudaConvnetPooling2DLayer(l2a, pool_size=2) l3a = cc_layers.CudaConvnetConv2DLayer(l2, n_filters=128, filter_size=3, weights_std=0.01, init_bias_value=0.1, dropout=0.0, partial_sum=1, untie_biases=True) l3b = cc_layers.CudaConvnetConv2DLayer(l3a, n_filters=128, filter_size=3, pad=0, weights_std=0.1, init_bias_value=0.1, dropout=0.0, partial_sum=1, untie_biases=True) l3 = cc_layers.CudaConvnetPooling2DLayer(l3b, pool_size=2) l3s = cc_layers.ShuffleC01BToBC01Layer(l3) j3 = layers.MultiRotMergeLayer(l3s, num_views=4) # 2) # merge convolutional parts # l4 = layers.DenseLayer(j3, n_outputs=4096, weights_std=0.001, init_bias_value=0.01, dropout=0.5) l4a = layers.DenseLayer(j3, n_outputs=4096, weights_std=0.001, init_bias_value=0.01, dropout=0.5, nonlinearity=layers.identity) l4 = layers.FeatureMaxPoolingLayer(l4a, pool_size=2, feature_dim=1, implementation='reshape') # l5 = layers.DenseLayer(l4, n_outputs=37, weights_std=0.01, init_bias_value=0.0, dropout=0.5, nonlinearity=custom.clip_01) # nonlinearity=layers.identity) l5 = layers.DenseLayer(l4, n_outputs=37, weights_std=0.01, init_bias_value=0.1, dropout=0.5, nonlinearity=layers.identity) # l6 = layers.OutputLayer(l5, error_measure='mse') l6 = custom.OptimisedDivGalaxyOutputLayer(l5) # this incorporates the constraints on the output (probabilities sum to one, weighting, etc.) xs_shared = [theano.shared(np.zeros((1,1,1,1), dtype=theano.config.floatX)) for _ in xrange(num_input_representations)] idx = T.lscalar('idx') givens = { l0.input_var: xs_shared[0][idx*BATCH_SIZE:(idx+1)*BATCH_SIZE], l0_45.input_var: xs_shared[1][idx*BATCH_SIZE:(idx+1)*BATCH_SIZE], } compute_output = theano.function([idx], l6.predictions(dropout_active=False), givens=givens) print "Load model parameters" layers.set_param_values(l6, analysis['param_values']) print "Create generators" # set here which transforms to use to make predictions augmentation_transforms = [] for zoom in [1 / 1.2, 1.0, 1.2]: for angle in np.linspace(0, 360, 10, endpoint=False): augmentation_transforms.append(ra.build_augmentation_transform(rotation=angle, zoom=zoom)) augmentation_transforms.append(ra.build_augmentation_transform(rotation=(angle + 180), zoom=zoom, shear=180)) # flipped print " %d augmentation transforms." % len(augmentation_transforms) augmented_data_gen_valid = ra.realtime_fixed_augmented_data_gen(valid_indices, 'train', augmentation_transforms=augmentation_transforms, chunk_size=CHUNK_SIZE, target_sizes=input_sizes, ds_transforms=ds_transforms, processor_class=ra.LoadAndProcessFixedPysexCenteringRescaling) valid_gen = load_data.buffered_gen_mp(augmented_data_gen_valid, buffer_size=1) augmented_data_gen_test = ra.realtime_fixed_augmented_data_gen(test_indices, 'test', augmentation_transforms=augmentation_transforms, chunk_size=CHUNK_SIZE, target_sizes=input_sizes, ds_transforms=ds_transforms, processor_class=ra.LoadAndProcessFixedPysexCenteringRescaling) test_gen = load_data.buffered_gen_mp(augmented_data_gen_test, buffer_size=1) approx_num_chunks_valid = int(np.ceil(num_valid * len(augmentation_transforms) / float(CHUNK_SIZE))) approx_num_chunks_test = int(np.ceil(num_test * len(augmentation_transforms) / float(CHUNK_SIZE))) print "Approximately %d chunks for the validation set" % approx_num_chunks_valid print "Approximately %d chunks for the test set" % approx_num_chunks_test if DO_VALID: print print "VALIDATION SET" print "Compute predictions" predictions_list = [] start_time = time.time() for e, (chunk_data, chunk_length) in enumerate(valid_gen): print "Chunk %d" % (e + 1) xs_chunk = chunk_data # need to transpose the chunks to move the 'channels' dimension up xs_chunk = [x_chunk.transpose(0, 3, 1, 2) for x_chunk in xs_chunk] print " load data onto GPU" for x_shared, x_chunk in zip(xs_shared, xs_chunk): x_shared.set_value(x_chunk) num_batches_chunk = int(np.ceil(chunk_length / float(BATCH_SIZE))) # make predictions, don't forget to cute off the zeros at the end predictions_chunk_list = [] for b in xrange(num_batches_chunk): if b % 1000 == 0: print " batch %d/%d" % (b + 1, num_batches_chunk) predictions = compute_output(b) predictions_chunk_list.append(predictions) predictions_chunk = np.vstack(predictions_chunk_list) predictions_chunk = predictions_chunk[:chunk_length] # cut off zeros / padding print " compute average over transforms" predictions_chunk_avg = predictions_chunk.reshape(-1, len(augmentation_transforms), 37).mean(1) predictions_list.append(predictions_chunk_avg) time_since_start = time.time() - start_time print " %s since start" % load_data.hms(time_since_start) all_predictions = np.vstack(predictions_list) print "Write predictions to %s" % target_path_valid load_data.save_gz(target_path_valid, all_predictions) print "Evaluate" rmse_valid = analysis['losses_valid'][-1] rmse_augmented = np.sqrt(np.mean((y_valid - all_predictions)**2)) print " MSE (last iteration):\t%.6f" % rmse_valid print " MSE (augmented):\t%.6f" % rmse_augmented if DO_TEST: print print "TEST SET" print "Compute predictions" predictions_list = [] start_time = time.time() for e, (chunk_data, chunk_length) in enumerate(test_gen): print "Chunk %d" % (e + 1) xs_chunk = chunk_data # need to transpose the chunks to move the 'channels' dimension up xs_chunk = [x_chunk.transpose(0, 3, 1, 2) for x_chunk in xs_chunk] print " load data onto GPU" for x_shared, x_chunk in zip(xs_shared, xs_chunk): x_shared.set_value(x_chunk) num_batches_chunk = int(np.ceil(chunk_length / float(BATCH_SIZE))) # make predictions, don't forget to cute off the zeros at the end predictions_chunk_list = [] for b in xrange(num_batches_chunk): if b % 1000 == 0: print " batch %d/%d" % (b + 1, num_batches_chunk) predictions = compute_output(b) predictions_chunk_list.append(predictions) predictions_chunk = np.vstack(predictions_chunk_list) predictions_chunk = predictions_chunk[:chunk_length] # cut off zeros / padding print " compute average over transforms" predictions_chunk_avg = predictions_chunk.reshape(-1, len(augmentation_transforms), 37).mean(1) predictions_list.append(predictions_chunk_avg) time_since_start = time.time() - start_time print " %s since start" % load_data.hms(time_since_start) all_predictions = np.vstack(predictions_list) print "Write predictions to %s" % target_path_test load_data.save_gz(target_path_test, all_predictions) print "Done!"
bsd-3-clause
chrismattmann/tika-similarity
sk_kmeans.py
2
4409
#!/usr/bin/env python2.7 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # from tika import parser import pandas as pd from vector import Vector from sklearn.cluster import KMeans import argparse, os, json def filterFiles(inputDir, acceptTypes): filename_list = [] for root, dirnames, files in os.walk(inputDir): dirnames[:] = [d for d in dirnames if not d.startswith('.')] for filename in files: if not filename.startswith('.'): filename_list.append(os.path.join(root, filename)) filename_list = (filename for filename in filename_list if "metadata" in parser.from_file(filename)) if acceptTypes: filename_list = (filename for filename in filename_list if str(parser.from_file(filename)['metadata']['Content-Type'].encode('utf-8').decode('utf-8')).split('/')[-1] in acceptTypes) else: print("Accepting all MIME Types.....") return filename_list if __name__ == "__main__": argParser = argparse.ArgumentParser('k-means Clustering of documents based on metadata values') argParser.add_argument('--inputDir', required=True, help='path to directory containing files') argParser.add_argument('--outJSON', required=True, help='/path/to/clusters.json containing k-means cluster assignments') argParser.add_argument('--Kvalue', help='number of clusters to find') #argParser.add_argument('--findK', action='store_true', help='find the optimal value of K') argParser.add_argument('--accept', nargs='+', type=str, help='Optional: compute similarity only on specified IANA MIME Type(s)') args = argParser.parse_args() # cluster for a particular value of K # if args.inputDir and args.outJSON and args.findK: if args.inputDir and args.outJSON and args.Kvalue: list_of_points = [] for eachFile in filterFiles(args.inputDir, args.accept): list_of_points.append(Vector(eachFile, parser.from_file(eachFile)["metadata"])) list_of_Dicts = (point.features for point in list_of_points) df = pd.DataFrame(list_of_Dicts) df = df.fillna(0) print(df.shape) kmeans = KMeans(n_clusters=int(args.Kvalue), init='k-means++', max_iter=300, # k-means convergence n_init=10, # find global minima n_jobs=-2, # parallelize ) labels = kmeans.fit_predict(df) # unsupervised (X, y=None) print(labels) # kmeans.labels_ clusters = {} for i in range(0, len(labels)): node = { "metadata": json.dumps(list_of_points[i].features), "name": list_of_points[i].filename.split('/')[-1], "path": list_of_points[i].filename } try: clusters[str(labels[i])].append(node) except KeyError: clusters[str(labels[i])] = [] clusters[str(labels[i])].append(node) # generate clusters.JSON with open(args.outJSON, "w") as jsonF: json_data = {"name": "clusters"} children = [] for key in clusters: cluster_children = {"name": "cluster"+key, "children": clusters[key]} children.append(cluster_children) json_data["children"] = children json.dump(json_data, jsonF) # print matplotlib # user chooses k => generates k # find elbow #kmeans.transform() # String Length Of Course # df.to_csv("bashhshs.csv", sep=',')
apache-2.0
xiaoxiamii/scikit-learn
examples/preprocessing/plot_function_transformer.py
161
1949
""" ========================================================= Using FunctionTransformer to select columns ========================================================= Shows how to use a function transformer in a pipeline. If you know your dataset's first principle component is irrelevant for a classification task, you can use the FunctionTransformer to select all but the first column of the PCA transformed data. """ import matplotlib.pyplot as plt import numpy as np from sklearn.cross_validation import train_test_split from sklearn.decomposition import PCA from sklearn.pipeline import make_pipeline from sklearn.preprocessing import FunctionTransformer def _generate_vector(shift=0.5, noise=15): return np.arange(1000) + (np.random.rand(1000) - shift) * noise def generate_dataset(): """ This dataset is two lines with a slope ~ 1, where one has a y offset of ~100 """ return np.vstack(( np.vstack(( _generate_vector(), _generate_vector() + 100, )).T, np.vstack(( _generate_vector(), _generate_vector(), )).T, )), np.hstack((np.zeros(1000), np.ones(1000))) def all_but_first_column(X): return X[:, 1:] def drop_first_component(X, y): """ Create a pipeline with PCA and the column selector and use it to transform the dataset. """ pipeline = make_pipeline( PCA(), FunctionTransformer(all_but_first_column), ) X_train, X_test, y_train, y_test = train_test_split(X, y) pipeline.fit(X_train, y_train) return pipeline.transform(X_test), y_test if __name__ == '__main__': X, y = generate_dataset() plt.scatter(X[:, 0], X[:, 1], c=y, s=50) plt.show() X_transformed, y_transformed = drop_first_component(*generate_dataset()) plt.scatter( X_transformed[:, 0], np.zeros(len(X_transformed)), c=y_transformed, s=50, ) plt.show()
bsd-3-clause
cl4rke/scikit-learn
sklearn/svm/tests/test_sparse.py
95
12156
from nose.tools import assert_raises, assert_true, assert_false import numpy as np from scipy import sparse from numpy.testing import (assert_array_almost_equal, assert_array_equal, assert_equal) from sklearn import datasets, svm, linear_model, base from sklearn.datasets import make_classification, load_digits, make_blobs from sklearn.svm.tests import test_svm from sklearn.utils import ConvergenceWarning from sklearn.utils.extmath import safe_sparse_dot from sklearn.utils.testing import assert_warns, assert_raise_message # test sample 1 X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) X_sp = sparse.lil_matrix(X) Y = [1, 1, 1, 2, 2, 2] T = np.array([[-1, -1], [2, 2], [3, 2]]) true_result = [1, 2, 2] # test sample 2 X2 = np.array([[0, 0, 0], [1, 1, 1], [2, 0, 0, ], [0, 0, 2], [3, 3, 3]]) X2_sp = sparse.dok_matrix(X2) Y2 = [1, 2, 2, 2, 3] T2 = np.array([[-1, -1, -1], [1, 1, 1], [2, 2, 2]]) true_result2 = [1, 2, 3] iris = datasets.load_iris() # permute rng = np.random.RandomState(0) perm = rng.permutation(iris.target.size) iris.data = iris.data[perm] iris.target = iris.target[perm] # sparsify iris.data = sparse.csr_matrix(iris.data) def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test): dense_svm.fit(X_train.toarray(), y_train) if sparse.isspmatrix(X_test): X_test_dense = X_test.toarray() else: X_test_dense = X_test sparse_svm.fit(X_train, y_train) assert_true(sparse.issparse(sparse_svm.support_vectors_)) assert_true(sparse.issparse(sparse_svm.dual_coef_)) assert_array_almost_equal(dense_svm.support_vectors_, sparse_svm.support_vectors_.toarray()) assert_array_almost_equal(dense_svm.dual_coef_, sparse_svm.dual_coef_.toarray()) if dense_svm.kernel == "linear": assert_true(sparse.issparse(sparse_svm.coef_)) assert_array_almost_equal(dense_svm.coef_, sparse_svm.coef_.toarray()) assert_array_almost_equal(dense_svm.support_, sparse_svm.support_) assert_array_almost_equal(dense_svm.predict(X_test_dense), sparse_svm.predict(X_test)) assert_array_almost_equal(dense_svm.decision_function(X_test_dense), sparse_svm.decision_function(X_test)) assert_array_almost_equal(dense_svm.decision_function(X_test_dense), sparse_svm.decision_function(X_test_dense)) assert_array_almost_equal(dense_svm.predict_proba(X_test_dense), sparse_svm.predict_proba(X_test), 4) msg = "cannot use sparse input in 'SVC' trained on dense data" if sparse.isspmatrix(X_test): assert_raise_message(ValueError, msg, dense_svm.predict, X_test) def test_svc(): """Check that sparse SVC gives the same result as SVC""" # many class dataset: X_blobs, y_blobs = make_blobs(n_samples=100, centers=10, random_state=0) X_blobs = sparse.csr_matrix(X_blobs) datasets = [[X_sp, Y, T], [X2_sp, Y2, T2], [X_blobs[:80], y_blobs[:80], X_blobs[80:]], [iris.data, iris.target, iris.data]] kernels = ["linear", "poly", "rbf", "sigmoid"] for dataset in datasets: for kernel in kernels: clf = svm.SVC(kernel=kernel, probability=True, random_state=0) sp_clf = svm.SVC(kernel=kernel, probability=True, random_state=0) check_svm_model_equal(clf, sp_clf, *dataset) def test_unsorted_indices(): # test that the result with sorted and unsorted indices in csr is the same # we use a subset of digits as iris, blobs or make_classification didn't # show the problem digits = load_digits() X, y = digits.data[:50], digits.target[:50] X_test = sparse.csr_matrix(digits.data[50:100]) X_sparse = sparse.csr_matrix(X) coef_dense = svm.SVC(kernel='linear', probability=True, random_state=0).fit(X, y).coef_ sparse_svc = svm.SVC(kernel='linear', probability=True, random_state=0).fit(X_sparse, y) coef_sorted = sparse_svc.coef_ # make sure dense and sparse SVM give the same result assert_array_almost_equal(coef_dense, coef_sorted.toarray()) X_sparse_unsorted = X_sparse[np.arange(X.shape[0])] X_test_unsorted = X_test[np.arange(X_test.shape[0])] # make sure we scramble the indices assert_false(X_sparse_unsorted.has_sorted_indices) assert_false(X_test_unsorted.has_sorted_indices) unsorted_svc = svm.SVC(kernel='linear', probability=True, random_state=0).fit(X_sparse_unsorted, y) coef_unsorted = unsorted_svc.coef_ # make sure unsorted indices give same result assert_array_almost_equal(coef_unsorted.toarray(), coef_sorted.toarray()) assert_array_almost_equal(sparse_svc.predict_proba(X_test_unsorted), sparse_svc.predict_proba(X_test)) def test_svc_with_custom_kernel(): kfunc = lambda x, y: safe_sparse_dot(x, y.T) clf_lin = svm.SVC(kernel='linear').fit(X_sp, Y) clf_mylin = svm.SVC(kernel=kfunc).fit(X_sp, Y) assert_array_equal(clf_lin.predict(X_sp), clf_mylin.predict(X_sp)) def test_svc_iris(): # Test the sparse SVC with the iris dataset for k in ('linear', 'poly', 'rbf'): sp_clf = svm.SVC(kernel=k).fit(iris.data, iris.target) clf = svm.SVC(kernel=k).fit(iris.data.toarray(), iris.target) assert_array_almost_equal(clf.support_vectors_, sp_clf.support_vectors_.toarray()) assert_array_almost_equal(clf.dual_coef_, sp_clf.dual_coef_.toarray()) assert_array_almost_equal( clf.predict(iris.data.toarray()), sp_clf.predict(iris.data)) if k == 'linear': assert_array_almost_equal(clf.coef_, sp_clf.coef_.toarray()) def test_sparse_decision_function(): #Test decision_function #Sanity check, test that decision_function implemented in python #returns the same as the one in libsvm # multi class: clf = svm.SVC(kernel='linear', C=0.1).fit(iris.data, iris.target) dec = safe_sparse_dot(iris.data, clf.coef_.T) + clf.intercept_ assert_array_almost_equal(dec, clf.decision_function(iris.data)) # binary: clf.fit(X, Y) dec = np.dot(X, clf.coef_.T) + clf.intercept_ prediction = clf.predict(X) assert_array_almost_equal(dec.ravel(), clf.decision_function(X)) assert_array_almost_equal( prediction, clf.classes_[(clf.decision_function(X) > 0).astype(np.int).ravel()]) expected = np.array([-1., -0.66, -1., 0.66, 1., 1.]) assert_array_almost_equal(clf.decision_function(X), expected, 2) def test_error(): # Test that it gives proper exception on deficient input # impossible value of C assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y) # impossible value of nu clf = svm.NuSVC(nu=0.0) assert_raises(ValueError, clf.fit, X_sp, Y) Y2 = Y[:-1] # wrong dimensions for labels assert_raises(ValueError, clf.fit, X_sp, Y2) clf = svm.SVC() clf.fit(X_sp, Y) assert_array_equal(clf.predict(T), true_result) def test_linearsvc(): # Similar to test_SVC clf = svm.LinearSVC(random_state=0).fit(X, Y) sp_clf = svm.LinearSVC(random_state=0).fit(X_sp, Y) assert_true(sp_clf.fit_intercept) assert_array_almost_equal(clf.coef_, sp_clf.coef_, decimal=4) assert_array_almost_equal(clf.intercept_, sp_clf.intercept_, decimal=4) assert_array_almost_equal(clf.predict(X), sp_clf.predict(X_sp)) clf.fit(X2, Y2) sp_clf.fit(X2_sp, Y2) assert_array_almost_equal(clf.coef_, sp_clf.coef_, decimal=4) assert_array_almost_equal(clf.intercept_, sp_clf.intercept_, decimal=4) def test_linearsvc_iris(): # Test the sparse LinearSVC with the iris dataset sp_clf = svm.LinearSVC(random_state=0).fit(iris.data, iris.target) clf = svm.LinearSVC(random_state=0).fit(iris.data.toarray(), iris.target) assert_equal(clf.fit_intercept, sp_clf.fit_intercept) assert_array_almost_equal(clf.coef_, sp_clf.coef_, decimal=1) assert_array_almost_equal(clf.intercept_, sp_clf.intercept_, decimal=1) assert_array_almost_equal( clf.predict(iris.data.toarray()), sp_clf.predict(iris.data)) # check decision_function pred = np.argmax(sp_clf.decision_function(iris.data), 1) assert_array_almost_equal(pred, clf.predict(iris.data.toarray())) # sparsify the coefficients on both models and check that they still # produce the same results clf.sparsify() assert_array_equal(pred, clf.predict(iris.data)) sp_clf.sparsify() assert_array_equal(pred, sp_clf.predict(iris.data)) def test_weight(): # Test class weights X_, y_ = make_classification(n_samples=200, n_features=100, weights=[0.833, 0.167], random_state=0) X_ = sparse.csr_matrix(X_) for clf in (linear_model.LogisticRegression(), svm.LinearSVC(random_state=0), svm.SVC()): clf.set_params(class_weight={0: 5}) clf.fit(X_[:180], y_[:180]) y_pred = clf.predict(X_[180:]) assert_true(np.sum(y_pred == y_[180:]) >= 11) def test_sample_weights(): # Test weights on individual samples clf = svm.SVC() clf.fit(X_sp, Y) assert_array_equal(clf.predict(X[2]), [1.]) sample_weight = [.1] * 3 + [10] * 3 clf.fit(X_sp, Y, sample_weight=sample_weight) assert_array_equal(clf.predict(X[2]), [2.]) def test_sparse_liblinear_intercept_handling(): # Test that sparse liblinear honours intercept_scaling param test_svm.test_dense_liblinear_intercept_handling(svm.LinearSVC) def test_sparse_realdata(): # Test on a subset from the 20newsgroups dataset. # This catchs some bugs if input is not correctly converted into # sparse format or weights are not correctly initialized. data = np.array([0.03771744, 0.1003567, 0.01174647, 0.027069]) indices = np.array([6, 5, 35, 31]) indptr = np.array( [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4]) X = sparse.csr_matrix((data, indices, indptr)) y = np.array( [1., 0., 2., 2., 1., 1., 1., 2., 2., 0., 1., 2., 2., 0., 2., 0., 3., 0., 3., 0., 1., 1., 3., 2., 3., 2., 0., 3., 1., 0., 2., 1., 2., 0., 1., 0., 2., 3., 1., 3., 0., 1., 0., 0., 2., 0., 1., 2., 2., 2., 3., 2., 0., 3., 2., 1., 2., 3., 2., 2., 0., 1., 0., 1., 2., 3., 0., 0., 2., 2., 1., 3., 1., 1., 0., 1., 2., 1., 1., 3.]) clf = svm.SVC(kernel='linear').fit(X.toarray(), y) sp_clf = svm.SVC(kernel='linear').fit(sparse.coo_matrix(X), y) assert_array_equal(clf.support_vectors_, sp_clf.support_vectors_.toarray()) assert_array_equal(clf.dual_coef_, sp_clf.dual_coef_.toarray()) def test_sparse_svc_clone_with_callable_kernel(): # Test that the "dense_fit" is called even though we use sparse input # meaning that everything works fine. a = svm.SVC(C=1, kernel=lambda x, y: x * y.T, probability=True, random_state=0) b = base.clone(a) b.fit(X_sp, Y) pred = b.predict(X_sp) b.predict_proba(X_sp) dense_svm = svm.SVC(C=1, kernel=lambda x, y: np.dot(x, y.T), probability=True, random_state=0) pred_dense = dense_svm.fit(X, Y).predict(X) assert_array_equal(pred_dense, pred) # b.decision_function(X_sp) # XXX : should be supported def test_timeout(): sp = svm.SVC(C=1, kernel=lambda x, y: x * y.T, probability=True, random_state=0, max_iter=1) assert_warns(ConvergenceWarning, sp.fit, X_sp, Y) def test_consistent_proba(): a = svm.SVC(probability=True, max_iter=1, random_state=0) proba_1 = a.fit(X, Y).predict_proba(X) a = svm.SVC(probability=True, max_iter=1, random_state=0) proba_2 = a.fit(X, Y).predict_proba(X) assert_array_almost_equal(proba_1, proba_2)
bsd-3-clause
Kongsea/tensorflow
tensorflow/examples/learn/hdf5_classification.py
75
2899
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Example of DNNClassifier for Iris plant dataset, hdf5 format.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from sklearn import datasets from sklearn import metrics from sklearn import model_selection import tensorflow as tf import h5py # pylint: disable=g-bad-import-order X_FEATURE = 'x' # Name of the input feature. def main(unused_argv): # Load dataset. iris = datasets.load_iris() x_train, x_test, y_train, y_test = model_selection.train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) # Note that we are saving and load iris data as h5 format as a simple # demonstration here. h5f = h5py.File('/tmp/test_hdf5.h5', 'w') h5f.create_dataset('X_train', data=x_train) h5f.create_dataset('X_test', data=x_test) h5f.create_dataset('y_train', data=y_train) h5f.create_dataset('y_test', data=y_test) h5f.close() h5f = h5py.File('/tmp/test_hdf5.h5', 'r') x_train = np.array(h5f['X_train']) x_test = np.array(h5f['X_test']) y_train = np.array(h5f['y_train']) y_test = np.array(h5f['y_test']) # Build 3 layer DNN with 10, 20, 10 units respectively. feature_columns = [ tf.feature_column.numeric_column( X_FEATURE, shape=np.array(x_train).shape[1:])] classifier = tf.estimator.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3) # Train. train_input_fn = tf.estimator.inputs.numpy_input_fn( x={X_FEATURE: x_train}, y=y_train, num_epochs=None, shuffle=True) classifier.train(input_fn=train_input_fn, steps=200) # Predict. test_input_fn = tf.estimator.inputs.numpy_input_fn( x={X_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False) predictions = classifier.predict(input_fn=test_input_fn) y_predicted = np.array(list(p['class_ids'] for p in predictions)) y_predicted = y_predicted.reshape(np.array(y_test).shape) # Score with sklearn. score = metrics.accuracy_score(y_test, y_predicted) print('Accuracy (sklearn): {0:f}'.format(score)) # Score with tensorflow. scores = classifier.evaluate(input_fn=test_input_fn) print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy'])) if __name__ == '__main__': tf.app.run()
apache-2.0
pmediano/ComputationalNeurodynamics
Fall2016/Exercise_1/Solutions/IzNeuronRK4.py
1
1897
""" Computational Neurodynamics Exercise 1 Simulates Izhikevich's neuron model using the Runge-Kutta 4 method. Parameters for regular spiking, fast spiking and bursting neurons extracted from: http://www.izhikevich.org/publications/spikes.htm (C) Murray Shanahan et al, 2016 """ import numpy as np import matplotlib.pyplot as plt # Create time points Tmin = 0 Tmax = 200 # Simulation time dt = 0.01 # Step size T = np.arange(Tmin, Tmax+dt, dt) # Base current I = 10 ## Parameters of Izhikevich's model (regular spiking) a = 0.02 b = 0.2 c = -65 d = 8 ## Parameters of Izhikevich's model (fast spiking) # a = 0.02 # b = 0.25 # c = -65 # d = 2 ## Parameters of Izhikevich's model (bursting) # a = 0.02 # b = 0.2 # c = -50 # d = 2 ## Make a state vector that has a (v, u) pair for each timestep s = np.zeros((len(T), 2)) ## Initial values s[0, 0] = -65 s[0, 1] = -1 # Note that s1[0] is v, s1[1] is u. This is Izhikevich equation in vector form def s_dt(s1, I): v_dt = 0.04*(s1[0]**2) + 5*s1[0] + 140 - s1[1] + I u_dt = a*(b*s1[0] - s1[1]) return np.array([v_dt, u_dt]) ## SIMULATE for t in range(len(T)-1): # Calculate the four constants of Runge-Kutta method k_1 = s_dt(s[t], I) k_2 = s_dt(s[t] + 0.5*dt*k_1, I) k_3 = s_dt(s[t] + 0.5*dt*k_2, I) k_4 = s_dt(s[t] + dt*k_3, I) s[t+1] = s[t] + (1.0/6)*dt*(k_1 + 2*k_2 + 2*k_3 + k_4) # Reset the neuron if it has spiked if s[t+1, 0] >= 30: s[t, 0] = 30 # Add a Dirac pulse for visualisation s[t+1, 0] = c # Reset to resting potential s[t+1, 1] += d # Update recovery variable v = s[:, 0] u = s[:, 1] ## Plot the membrane potential plt.subplot(211) plt.plot(T, v) plt.xlabel('Time (ms)') plt.ylabel('Membrane potential v (mV)') plt.title('Izhikevich Neuron') # Plot the reset variable plt.subplot(212) plt.plot(T, u) plt.xlabel('Time (ms)') plt.ylabel('Reset variable u') plt.show()
gpl-3.0
craigcitro/pydatalab
tests/bigquery/schema_tests.py
6
4284
# Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except # in compliance with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing permissions and limitations under # the License. from __future__ import absolute_import from __future__ import unicode_literals import collections import pandas import sys import unittest import google.datalab.bigquery import google.datalab.utils class TestCases(unittest.TestCase): def test_schema_from_dataframe(self): df = TestCases._create_data_frame() result = google.datalab.bigquery.Schema.from_data(df) self.assertEqual(google.datalab.bigquery.Schema.from_data(TestCases._create_inferred_schema()), result) def test_schema_from_data(self): variant1 = [ 3, 2.0, True, ['cow', 'horse', [0, []]] ] variant2 = collections.OrderedDict() variant2['Column1'] = 3 variant2['Column2'] = 2.0 variant2['Column3'] = True variant2['Column4'] = collections.OrderedDict() variant2['Column4']['Column1'] = 'cow' variant2['Column4']['Column2'] = 'horse' variant2['Column4']['Column3'] = collections.OrderedDict() variant2['Column4']['Column3']['Column1'] = 0 variant2['Column4']['Column3']['Column2'] = collections.OrderedDict() master = [ {'name': 'Column1', 'type': 'INTEGER'}, {'name': 'Column2', 'type': 'FLOAT'}, {'name': 'Column3', 'type': 'BOOLEAN'}, {'name': 'Column4', 'type': 'RECORD', 'fields': [ {'name': 'Column1', 'type': 'STRING'}, {'name': 'Column2', 'type': 'STRING'}, {'name': 'Column3', 'type': 'RECORD', 'fields': [ {'name': 'Column1', 'type': 'INTEGER'}, {'name': 'Column2', 'type': 'RECORD', 'fields': []} ]} ]} ] schema_master = google.datalab.bigquery.Schema(master) with self.assertRaises(Exception) as error1: google.datalab.bigquery.Schema.from_data(variant1) if sys.version_info[0] == 3: self.assertEquals('Cannot create a schema from heterogeneous list [3, 2.0, True, ' + '[\'cow\', \'horse\', [0, []]]]; perhaps you meant to use ' + 'Schema.from_record?', str(error1.exception)) else: self.assertEquals('Cannot create a schema from heterogeneous list [3, 2.0, True, ' + '[u\'cow\', u\'horse\', [0, []]]]; perhaps you meant to use ' + 'Schema.from_record?', str(error1.exception)) schema3 = google.datalab.bigquery.Schema.from_data([variant1]) schema4 = google.datalab.bigquery.Schema.from_data([variant2]) schema5 = google.datalab.bigquery.Schema.from_data(master) schema6 = google.datalab.bigquery.Schema.from_record(variant1) schema7 = google.datalab.bigquery.Schema.from_record(variant2) self.assertEquals(schema_master, schema3, 'schema inferred from list of lists with from_data') self.assertEquals(schema_master, schema4, 'schema inferred from list of dicts with from_data') self.assertEquals(schema_master, schema5, 'schema inferred from BQ schema list with from_data') self.assertEquals(schema_master, schema6, 'schema inferred from list with from_record') self.assertEquals(schema_master, schema7, 'schema inferred from dict with from_record') @staticmethod def _create_data_frame(): data = { 'some': [ 0, 1, 2, 3 ], 'column': [ 'r0', 'r1', 'r2', 'r3' ], 'headers': [ 10.0, 10.0, 10.0, 10.0 ] } return pandas.DataFrame(data) @staticmethod def _create_inferred_schema(extra_field=None): schema = [ {'name': 'some', 'type': 'INTEGER'}, {'name': 'column', 'type': 'STRING'}, {'name': 'headers', 'type': 'FLOAT'}, ] if extra_field: schema.append({'name': extra_field, 'type': 'INTEGER'}) return schema
apache-2.0
joelfrederico/SciSalt
scisalt/qt/mplwidget.py
1
13557
from PyQt4 import QtGui from PyQt4 import QtCore from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as _FigureCanvas from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as _NavigationToolbar import matplotlib as _mpl import numpy as _np from .Rectangle import Rectangle import pdb import traceback import logging loggerlevel = logging.DEBUG logger = logging.getLogger(__name__) try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Slider_and_Text(QtGui.QWidget): valueChanged = QtCore.pyqtSignal(int) sliderReleased = QtCore.pyqtSignal(int) def __init__(self, parent=None): QtGui.QWidget.__init__(self) self.setMaximumHeight(40) # Enable tracking by default self._tracking = True self.hLayout = QtGui.QHBoxLayout() self.slider = QtGui.QSlider() self.leftbutton = QtGui.QPushButton() self.leftbutton.setText("<") sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.leftbutton.sizePolicy().hasHeightForWidth()) # self.leftbutton.setSizePolicy(sizePolicy) self.leftbutton.clicked.connect(self._subone) self.rightbutton = QtGui.QPushButton() self.rightbutton.setText(">") sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.rightbutton.sizePolicy().hasHeightForWidth()) # self.rightbutton.setSizePolicy(sizePolicy) self.rightbutton.clicked.connect(self._addone) self.v = QtGui.QIntValidator() self.box = QtGui.QLineEdit() self.box.setValidator(self.v) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.box.sizePolicy().hasHeightForWidth()) # self.box.setSizePolicy(sizePolicy) self.hLayout.addWidget(self.leftbutton) self.hLayout.addWidget(self.slider) self.hLayout.addWidget(self.box) self.hLayout.addWidget(self.rightbutton) self.setLayout(self.hLayout) self.slider.valueChanged.connect(self._sliderChanged) self.box.editingFinished.connect(self._textChanged) self.setOrientation(QtCore.Qt.Horizontal) # Connect release so tracking works as expected self.slider.sliderReleased.connect(self._sliderReleased) def _addone(self): self.value = self.value + 1 self.valueChanged.emit(self.value) def _subone(self): self.value = self.value - 1 self.valueChanged.emit(self.value) def _sliderReleased(self): print('Released') self.sliderReleased.emit(self.slider.value) def setTracking(self, val): print('Tracking set to {}'.format(val)) self._tracking = val def setMaximum(self, val): self.slider.setMaximum(val) self.v.setRange(self.slider.minimum(), self.slider.maximum()) self.box.setValidator(self.v) def setMinimum(self, val): self.slider.setMinimum(val) self.v.setRange(self.slider.minimum(), self.slider.maximum()) self.box.setValidator(self.v) def _sliderChanged(self, val): self.box.setText(str(val)) if self._tracking: try: self.slider.sliderReleased.disconnect() except: pass self.valueChanged.emit(val) else: try: self.slider.sliderReleased.disconnect() except: pass self.slider.sliderReleased.connect(self._sliderChanged_notracking) def _sliderChanged_notracking(self): val = self.slider.value() # print('Value to be emitted is {}'.format(val)) self.valueChanged.emit(val) def _textChanged(self): val = self.box.text() self.slider.setValue(int(val)) self._sliderChanged_notracking() def setOrientation(self, *args, **kwargs): self.slider.setOrientation(*args, **kwargs) def _getValue(self): return self.slider.value() def _setValue(self, val): self.slider.setValue(val) self.box.setText(str(val)) value = property(_getValue, _setValue) def setValue(self, val): self.slider.setValue(val) self.box.setText(str(val)) # self.valueChanged.emit(val) class Mpl_Plot(_FigureCanvas): def __init__(self, parent=None): # Initialize things self.fig = _mpl.figure.Figure() _FigureCanvas.__init__(self, self.fig) _FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) _FigureCanvas.updateGeometry(self) # Create axes self.ax = self.fig.add_subplot(111) def plot(self, *args, **kwargs): self.ax.clear() self.ax.plot(*args, **kwargs) self.ax.ticklabel_format(style='sci', scilimits=(0, 0), axis='y') self.ax.figure.canvas.draw() class Mpl_Image(QtGui.QWidget): # Signal for when the rectangle is changed rectChanged = QtCore.pyqtSignal(Rectangle) def __init__(self, parent=None, rectbool = True, toolbarbool=False, image=None): # Initialize things QtGui.QWidget.__init__(self) self.rectbool = rectbool self._clim_min = 0 self._clim_max = 3600 self._pressed = False # Add a vertical layout self.vLayout = QtGui.QVBoxLayout() # Add a figure self.fig = _mpl.figure.Figure() # Add a canvas containing the fig self.canvas = _FigureCanvas(self.fig) _FigureCanvas.setSizePolicy(self.canvas, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) _FigureCanvas.updateGeometry(self.canvas) # Setup the layout if toolbarbool: self.toolbar = _NavigationToolbar(self.canvas, self) self.toolbar.setMaximumHeight(20) self.vLayout.addWidget(self.toolbar) self.vLayout.addWidget(self.canvas) self.setLayout(self.vLayout) # Create axes self.ax = self.fig.add_subplot(111) # Include rectangle functionality if rectbool: self.fig.canvas.mpl_connect('button_press_event', self.on_press) self.fig.canvas.mpl_connect('button_release_event', self.on_release) self.Rectangle = Rectangle( x = -10 , y = 0 , width = 0 , height = 3 , axes = self.ax ) # Add image self.image = image def _get_img(self): return self._image def _set_img(self, image): self.ax.clear() self._image = image if image is not None: self._imgplot = self.ax.imshow(image, interpolation='none') if self.rectbool: self.ax.add_patch(self.Rectangle.get_rect()) # imagemax = _np.max(_np.max(image)) self.set_clim(self._clim_min, self._clim_max) image = property(_get_img, _set_img) def set_clim(self, clim_min, clim_max): if self.image is not None: self._clim_min = clim_min self._clim_max = clim_max self._imgplot.set_clim(clim_min, clim_max) self.ax.figure.canvas.draw() def on_press(self, event): if self.toolbar._active is None: self._pressed = True self.x0 = event.xdata self.y0 = event.ydata logger.log(level=loggerlevel, msg='Pressed: x0: {}, y0: {}'.format(self.x0, self.y0)) def on_release(self, event): if self._pressed: self._pressed = False print('release') self.x1 = event.xdata self.y1 = event.ydata width = self.x1 - self.x0 height = self.y1 - self.y0 logger.log(level=loggerlevel, msg='Released: x0: {}, y0: {}, x1: {}, y1: {}, width: {}, height: {}'.format( self.x0 , self.y0 , self.x1 , self.y1 , width , height ) ) self.Rectangle.set_xy((self.x0, self.y0)) self.Rectangle.set_width(width) self.Rectangle.set_height(height) self.ax.figure.canvas.draw() self.rectChanged.emit(self.Rectangle) # print(self.rect) def zoom_rect(self, border=None, border_px=None): # ====================================== # Get x coordinates # ====================================== x0 = self.Rectangle.get_x() width = self.Rectangle.get_width() x1 = x0+width # ====================================== # Get y coordinates # ====================================== y0 = self.Rectangle.get_y() height = self.Rectangle.get_height() y1 = y0+height # ====================================== # Validate borders # ====================================== if (border_px is None) and (border is not None): xborder = border[0]*width yborder = border[1]*height elif (border_px is not None) and (border is None): xborder = border_px[0] yborder = border_px[1] elif (border_px is None) and (border is None): raise IOError('No border info specified!') elif (border_px is not None) and (border is not None): raise IOError('Too much border info specified, both border_px and border!') else: raise IOError('End of the line!') # ====================================== # Add borders # ====================================== x0 = x0 - xborder x1 = x1 + xborder y0 = y0 - yborder y1 = y1 + yborder # ====================================== # Validate coordinates to prevent # unPythonic crash # ====================================== if not ((0 <= x0 and x0 <= self.image.shape[1]) and (0 <= x1 and x1 <= self.image.shape[1])): print('X issue') print('Requested: x=({}, {})'.format(x0, x1)) x0 = 0 x1 = self.image.shape[1] if not ((0 <= y0 and y0 <= self.image.shape[0]) and (0 <= y1 and y1 <= self.image.shape[0])): print('y issue') print('Requested: y=({}, {})'.format(y0, y1)) y0 = 0 y1 = self.image.shape[0] # ====================================== # Set viewable area # ====================================== self.ax.set_xlim(x0, x1) self.ax.set_ylim(y0, y1) # ====================================== # Redraw canvas to show updates # ====================================== self.ax.figure.canvas.draw() class Mpl_Image_Plus_Slider(QtGui.QWidget): # def __init__(self, parent=None, **kwargs): def __init__(self, parent=None, **kwargs): # Initialize self as a widget QtGui.QWidget.__init__(self, parent) # Add a vertical layout with parent self self.vLayout = QtGui.QVBoxLayout(self) self.vLayout.setObjectName(_fromUtf8("vLayout")) # Add an Mpl_Image widget to vLayout, # save it to self._img # Pass arguments through to Mpl_Image. self._img = Mpl_Image(parent=parent, toolbarbool=True, **kwargs) self._img.setObjectName(_fromUtf8("_img")) self.vLayout.addWidget(self._img) # Add a slider to vLayout, # save it to self.max_slider # self.max_slider = QtGui.QSlider(self) self.max_slider = Slider_and_Text(self) self.max_slider.setObjectName(_fromUtf8("max_slider")) self.max_slider.setOrientation(QtCore.Qt.Horizontal) self.vLayout.addWidget(self.max_slider) # Setup slider to work with _img's clims self.max_slider.valueChanged.connect(lambda val: self.set_clim(0, val)) def _get_image(self): return self._img.image def _set_image(self, image): self._img.image = image maximage = _np.max(_np.max(image)) self.max_slider.setMaximum(maximage) image = property(_get_image, _set_image) def _get_ax(self): return self._img.ax ax = property(_get_ax) def _get_Rectangle(self): return self._img.Rectangle # def _set_rect(self, rect): # self._img.rect(rect) Rectangle = property(_get_Rectangle) def zoom_rect(self, border=None, border_px=None): self._img.zoom_rect(border, border_px) def set_clim(self, *args, **kwargs): self._img.set_clim(*args, **kwargs) def setSliderValue(self, val): self.max_slider.setValue(val)
mit
jonyroda97/redbot-amigosprovaveis
lib/matplotlib/units.py
2
6084
""" The classes here provide support for using custom classes with matplotlib, e.g., those that do not expose the array interface but know how to convert themselves to arrays. It also supports classes with units and units conversion. Use cases include converters for custom objects, e.g., a list of datetime objects, as well as for objects that are unit aware. We don't assume any particular units implementation; rather a units implementation must provide the register with the Registry converter dictionary and a ConversionInterface. For example, here is a complete implementation which supports plotting with native datetime objects:: import matplotlib.units as units import matplotlib.dates as dates import matplotlib.ticker as ticker import datetime class DateConverter(units.ConversionInterface): @staticmethod def convert(value, unit, axis): 'convert value to a scalar or array' return dates.date2num(value) @staticmethod def axisinfo(unit, axis): 'return major and minor tick locators and formatters' if unit!='date': return None majloc = dates.AutoDateLocator() majfmt = dates.AutoDateFormatter(majloc) return AxisInfo(majloc=majloc, majfmt=majfmt, label='date') @staticmethod def default_units(x, axis): 'return the default unit for x or None' return 'date' # finally we register our object type with a converter units.registry[datetime.date] = DateConverter() """ from __future__ import (absolute_import, division, print_function, unicode_literals) import six from matplotlib.cbook import iterable, is_numlike, safe_first_element import numpy as np class AxisInfo(object): """information to support default axis labeling and tick labeling, and default limits""" def __init__(self, majloc=None, minloc=None, majfmt=None, minfmt=None, label=None, default_limits=None): """ majloc and minloc: TickLocators for the major and minor ticks majfmt and minfmt: TickFormatters for the major and minor ticks label: the default axis label default_limits: the default min, max of the axis if no data is present If any of the above are None, the axis will simply use the default """ self.majloc = majloc self.minloc = minloc self.majfmt = majfmt self.minfmt = minfmt self.label = label self.default_limits = default_limits class ConversionInterface(object): """ The minimal interface for a converter to take custom instances (or sequences) and convert them to values mpl can use """ @staticmethod def axisinfo(unit, axis): 'return an units.AxisInfo instance for axis with the specified units' return None @staticmethod def default_units(x, axis): 'return the default unit for x or None for the given axis' return None @staticmethod def convert(obj, unit, axis): """ convert obj using unit for the specified axis. If obj is a sequence, return the converted sequence. The output must be a sequence of scalars that can be used by the numpy array layer """ return obj @staticmethod def is_numlike(x): """ The matplotlib datalim, autoscaling, locators etc work with scalars which are the units converted to floats given the current unit. The converter may be passed these floats, or arrays of them, even when units are set. Derived conversion interfaces may opt to pass plain-ol unitless numbers through the conversion interface and this is a helper function for them. """ if iterable(x): for thisx in x: return is_numlike(thisx) else: return is_numlike(x) class Registry(dict): """ register types with conversion interface """ def __init__(self): dict.__init__(self) self._cached = {} def get_converter(self, x): 'get the converter interface instance for x, or None' if not len(self): return None # nothing registered # DISABLED idx = id(x) # DISABLED cached = self._cached.get(idx) # DISABLED if cached is not None: return cached converter = None classx = getattr(x, '__class__', None) if classx is not None: converter = self.get(classx) if isinstance(x, np.ndarray) and x.size: xravel = x.ravel() try: # pass the first value of x that is not masked back to # get_converter if not np.all(xravel.mask): # some elements are not masked converter = self.get_converter( xravel[np.argmin(xravel.mask)]) return converter except AttributeError: # not a masked_array # Make sure we don't recurse forever -- it's possible for # ndarray subclasses to continue to return subclasses and # not ever return a non-subclass for a single element. next_item = xravel[0] if (not isinstance(next_item, np.ndarray) or next_item.shape != x.shape): converter = self.get_converter(next_item) return converter if converter is None: try: thisx = safe_first_element(x) except (TypeError, StopIteration): pass else: if classx and classx != getattr(thisx, '__class__', None): converter = self.get_converter(thisx) return converter # DISABLED self._cached[idx] = converter return converter registry = Registry()
gpl-3.0
preprocessed-connectomes-project/quality-assessment-protocol
scripts/qap_check_output_csv.py
1
1302
#!/usr/bin/env python def main(): import os import argparse from qap.script_utils import check_csv_missing_subs, csv_to_pandas_df, \ write_inputs_dict_to_yaml_file, read_yml_file from qap.qap_utils import raise_smart_exception parser = argparse.ArgumentParser() parser.add_argument("output_csv", type=str, help="the main output directory of the QAP run " "which contains the participant directories") parser.add_argument("data_config", type=str, help="the main output directory of the QAP run " "which contains the participant directories") parser.add_argument("data_type", type=str, help="the main output directory of the QAP run " "which contains the participant directories") args = parser.parse_args() csv_df = csv_to_pandas_df(args.output_csv) data_dict = read_yml_file(args.data_config) new_dict = check_csv_missing_subs(csv_df, data_dict, args.data_type) if new_dict: out_file = os.path.join(os.getcwd(), "missing_%s_data.yml" % args.data_type) write_inputs_dict_to_yaml_file(new_dict, out_file) if __name__ == "__main__": main()
bsd-3-clause
xyguo/scikit-learn
examples/cross_decomposition/plot_compare_cross_decomposition.py
55
4761
""" =================================== Compare cross decomposition methods =================================== Simple usage of various cross decomposition algorithms: - PLSCanonical - PLSRegression, with multivariate response, a.k.a. PLS2 - PLSRegression, with univariate response, a.k.a. PLS1 - CCA Given 2 multivariate covarying two-dimensional datasets, X, and Y, PLS extracts the 'directions of covariance', i.e. the components of each datasets that explain the most shared variance between both datasets. This is apparent on the **scatterplot matrix** display: components 1 in dataset X and dataset Y are maximally correlated (points lie around the first diagonal). This is also true for components 2 in both dataset, however, the correlation across datasets for different components is weak: the point cloud is very spherical. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.cross_decomposition import PLSCanonical, PLSRegression, CCA ############################################################################### # Dataset based latent variables model n = 500 # 2 latents vars: l1 = np.random.normal(size=n) l2 = np.random.normal(size=n) latents = np.array([l1, l1, l2, l2]).T X = latents + np.random.normal(size=4 * n).reshape((n, 4)) Y = latents + np.random.normal(size=4 * n).reshape((n, 4)) X_train = X[:n / 2] Y_train = Y[:n / 2] X_test = X[n / 2:] Y_test = Y[n / 2:] print("Corr(X)") print(np.round(np.corrcoef(X.T), 2)) print("Corr(Y)") print(np.round(np.corrcoef(Y.T), 2)) ############################################################################### # Canonical (symmetric) PLS # Transform data # ~~~~~~~~~~~~~~ plsca = PLSCanonical(n_components=2) plsca.fit(X_train, Y_train) X_train_r, Y_train_r = plsca.transform(X_train, Y_train) X_test_r, Y_test_r = plsca.transform(X_test, Y_test) # Scatter plot of scores # ~~~~~~~~~~~~~~~~~~~~~~ # 1) On diagonal plot X vs Y scores on each components plt.figure(figsize=(12, 8)) plt.subplot(221) plt.plot(X_train_r[:, 0], Y_train_r[:, 0], "ob", label="train") plt.plot(X_test_r[:, 0], Y_test_r[:, 0], "or", label="test") plt.xlabel("x scores") plt.ylabel("y scores") plt.title('Comp. 1: X vs Y (test corr = %.2f)' % np.corrcoef(X_test_r[:, 0], Y_test_r[:, 0])[0, 1]) plt.xticks(()) plt.yticks(()) plt.legend(loc="best") plt.subplot(224) plt.plot(X_train_r[:, 1], Y_train_r[:, 1], "ob", label="train") plt.plot(X_test_r[:, 1], Y_test_r[:, 1], "or", label="test") plt.xlabel("x scores") plt.ylabel("y scores") plt.title('Comp. 2: X vs Y (test corr = %.2f)' % np.corrcoef(X_test_r[:, 1], Y_test_r[:, 1])[0, 1]) plt.xticks(()) plt.yticks(()) plt.legend(loc="best") # 2) Off diagonal plot components 1 vs 2 for X and Y plt.subplot(222) plt.plot(X_train_r[:, 0], X_train_r[:, 1], "*b", label="train") plt.plot(X_test_r[:, 0], X_test_r[:, 1], "*r", label="test") plt.xlabel("X comp. 1") plt.ylabel("X comp. 2") plt.title('X comp. 1 vs X comp. 2 (test corr = %.2f)' % np.corrcoef(X_test_r[:, 0], X_test_r[:, 1])[0, 1]) plt.legend(loc="best") plt.xticks(()) plt.yticks(()) plt.subplot(223) plt.plot(Y_train_r[:, 0], Y_train_r[:, 1], "*b", label="train") plt.plot(Y_test_r[:, 0], Y_test_r[:, 1], "*r", label="test") plt.xlabel("Y comp. 1") plt.ylabel("Y comp. 2") plt.title('Y comp. 1 vs Y comp. 2 , (test corr = %.2f)' % np.corrcoef(Y_test_r[:, 0], Y_test_r[:, 1])[0, 1]) plt.legend(loc="best") plt.xticks(()) plt.yticks(()) plt.show() ############################################################################### # PLS regression, with multivariate response, a.k.a. PLS2 n = 1000 q = 3 p = 10 X = np.random.normal(size=n * p).reshape((n, p)) B = np.array([[1, 2] + [0] * (p - 2)] * q).T # each Yj = 1*X1 + 2*X2 + noize Y = np.dot(X, B) + np.random.normal(size=n * q).reshape((n, q)) + 5 pls2 = PLSRegression(n_components=3) pls2.fit(X, Y) print("True B (such that: Y = XB + Err)") print(B) # compare pls2.coef_ with B print("Estimated B") print(np.round(pls2.coef_, 1)) pls2.predict(X) ############################################################################### # PLS regression, with univariate response, a.k.a. PLS1 n = 1000 p = 10 X = np.random.normal(size=n * p).reshape((n, p)) y = X[:, 0] + 2 * X[:, 1] + np.random.normal(size=n * 1) + 5 pls1 = PLSRegression(n_components=3) pls1.fit(X, y) # note that the number of components exceeds 1 (the dimension of y) print("Estimated betas") print(np.round(pls1.coef_, 1)) ############################################################################### # CCA (PLS mode B with symmetric deflation) cca = CCA(n_components=2) cca.fit(X_train, Y_train) X_train_r, Y_train_r = plsca.transform(X_train, Y_train) X_test_r, Y_test_r = plsca.transform(X_test, Y_test)
bsd-3-clause
colin2328/asciiclass
lectures/lec6/match-loop.py
3
2094
import csv from sklearn import tree import editdist import re def string_match_score(p1,p2,field): s1 = p1[field] s2 = p2[field] return editdist.distance(s1.lower(),s2.lower())/float(len(s1)) def jaccard_score(p1,p2,field): name1 = p1[field] name2 = p2[field] set1 = set(name1.lower().split()) set2 = set(name2.lower().split()) c = set1.intersection(set2) return float(len(c)) / (len(set1) + len(set2) - len(c)) def price_score(p1,p2,field): price1 = p1[field] if (len(price1) == 0): return 10000 price2 = p2[field] if (len(price2) == 0): return 10000 price1 = re.sub('[\$,]', '', price1) price2 = re.sub('[\$,]', '', price2) price1 = float(price1) price2 = float(price2) return abs(price1 - price2) print "Loading Data" abtReader = csv.DictReader(open("Abt.csv","rU")) buyReader = csv.DictReader(open("Buy.csv","rU")) gtLines = csv.DictReader(open("abt_buy_perfectMapping.csv","rU")) gtBuyMap = {} gtAbtMap = {} abtAr = [] buyAr = [] for r in abtReader: abtAr.append(r) for r in buyReader: buyAr.append(r) for r in gtLines: gtAbtMap[r["idAbt"]] = r["idBuy"] gtBuyMap[r["idBuy"]] = r["idAbt"] for loop in range(0,10,1): falsePos = 0 truePos = 0 falseNeg = 0 trueNeg = 0 thresh = float(loop)/10.0 for r1 in buyAr: bestMatch = 0 bestVal = [] j = 0 for r2 in abtAr: s = jaccard_score(r1,r2,"name") if (s > bestMatch): bestMatch = s bestVal = r2 if (bestMatch > thresh): # print "Best match: ",r1["name"],bestVal["name"],"score=",bestMatch if (gtBuyMap[r1["id"]] == bestVal["id"]): truePos = truePos + 1 else: falsePos = falsePos + 1 precision = truePos / float(truePos + falsePos) recall = truePos / float(len(buyAr)) fmeas = (2.0 * precision * recall) / (precision + recall) print "THRESH = ",thresh,"TP = ",truePos,"FP = ",falsePos,"PREC = ",precision,"RECALL = ",recall,"F = ",fmeas
mit
Obus/scikit-learn
doc/datasets/mldata_fixture.py
367
1183
"""Fixture module to skip the datasets loading when offline Mock urllib2 access to mldata.org and create a temporary data folder. """ from os import makedirs from os.path import join import numpy as np import tempfile import shutil from sklearn import datasets from sklearn.utils.testing import install_mldata_mock from sklearn.utils.testing import uninstall_mldata_mock def globs(globs): # Create a temporary folder for the data fetcher global custom_data_home custom_data_home = tempfile.mkdtemp() makedirs(join(custom_data_home, 'mldata')) globs['custom_data_home'] = custom_data_home return globs def setup_module(): # setup mock urllib2 module to avoid downloading from mldata.org install_mldata_mock({ 'mnist-original': { 'data': np.empty((70000, 784)), 'label': np.repeat(np.arange(10, dtype='d'), 7000), }, 'iris': { 'data': np.empty((150, 4)), }, 'datasets-uci-iris': { 'double0': np.empty((150, 4)), 'class': np.empty((150,)), }, }) def teardown_module(): uninstall_mldata_mock() shutil.rmtree(custom_data_home)
bsd-3-clause
amozie/amozie
studzie/keras_gym/mountain_car_v0.py
1
2577
import numpy as np import matplotlib.pyplot as plt import gym import time import copy from keras.models import Sequential, Model from keras.layers import Dense, Activation, Flatten, Lambda, Input, Reshape, concatenate, Merge from keras.optimizers import Adam, RMSprop from keras.callbacks import History from keras import backend as K import tensorflow as tf from gym import Env, Space, spaces from gym.utils import seeding from rl.agents.dqn import DQNAgent from rl.policy import BoltzmannQPolicy, EpsGreedyQPolicy from rl.memory import SequentialMemory, EpisodeParameterMemory from rl.agents.cem import CEMAgent from rl.agents import SARSAAgent from rl.callbacks import TrainEpisodeLogger, CallbackList class MountainCarEnv(Env): metadata = {'render.modes': ['human', 'rgb_array']} def __init__(self) -> None: self.env = gym.make('MountainCar-v0') self.action_space = self.env.action_space self.observation_space = self.env.observation_space def _step(self, action): step = self.env.step(action) step = list(step) step[1] = np.abs(step[0][1]) - 0.05 return tuple(step) def _reset(self): return self.env.reset() def _seed(self, seed=None): return self.env.seed(seed) def _render(self, mode='human', close=False): return self.env.render(mode, close) def _close(self): return self.env.close() env = MountainCarEnv() env.seed() nb_actions = env.action_space.n x = Input((1,) + env.observation_space.shape) y = Flatten()(x) y = Dense(16)(y) y = Activation('relu')(y) y = Dense(16)(y) y = Activation('relu')(y) y = Dense(16)(y) y = Activation('relu')(y) y = Dense(nb_actions)(y) y = Activation('linear')(y) model = Model(x, y) memory = SequentialMemory(limit=10000, window_length=1) # policy = BoltzmannQPolicy() policy = EpsGreedyQPolicy() dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=1000, gamma=.9, batch_size=32, enable_dueling_network=False, dueling_type='avg', target_model_update=.1, policy=policy) dqn.compile(Adam(), metrics=['mae']) hist = dqn.fit(env, nb_steps=10000, visualize=False, verbose=2, callbacks=None) state = env.reset() action = env.action_space.sample() print(action) state_list= [] for i in range(500): action = np.argmax(dqn.model.predict(np.expand_dims(np.expand_dims(state, 0), 0))[0]) state, reward, done, _ = env.step(2) state_list.append(reward) env.render() env.render(close=True) dqn.test(env, nb_episodes=5, visualize=True) env.render(close=True)
apache-2.0
nolanliou/tensorflow
tensorflow/examples/get_started/regression/imports85.py
24
6638
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """A dataset loader for imports85.data.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import numpy as np import tensorflow as tf try: import pandas as pd # pylint: disable=g-import-not-at-top except ImportError: pass URL = "https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data" # Order is important for the csv-readers, so we use an OrderedDict here. defaults = collections.OrderedDict([ ("symboling", [0]), ("normalized-losses", [0.0]), ("make", [""]), ("fuel-type", [""]), ("aspiration", [""]), ("num-of-doors", [""]), ("body-style", [""]), ("drive-wheels", [""]), ("engine-location", [""]), ("wheel-base", [0.0]), ("length", [0.0]), ("width", [0.0]), ("height", [0.0]), ("curb-weight", [0.0]), ("engine-type", [""]), ("num-of-cylinders", [""]), ("engine-size", [0.0]), ("fuel-system", [""]), ("bore", [0.0]), ("stroke", [0.0]), ("compression-ratio", [0.0]), ("horsepower", [0.0]), ("peak-rpm", [0.0]), ("city-mpg", [0.0]), ("highway-mpg", [0.0]), ("price", [0.0]) ]) # pyformat: disable types = collections.OrderedDict((key, type(value[0])) for key, value in defaults.items()) def _get_imports85(): path = tf.contrib.keras.utils.get_file(URL.split("/")[-1], URL) return path def dataset(y_name="price", train_fraction=0.7): """Load the imports85 data as a (train,test) pair of `Dataset`. Each dataset generates (features_dict, label) pairs. Args: y_name: The name of the column to use as the label. train_fraction: A float, the fraction of data to use for training. The remainder will be used for evaluation. Returns: A (train,test) pair of `Datasets` """ # Download and cache the data path = _get_imports85() # Define how the lines of the file should be parsed def decode_line(line): """Convert a csv line into a (features_dict,label) pair.""" # Decode the line to a tuple of items based on the types of # csv_header.values(). items = tf.decode_csv(line, list(defaults.values())) # Convert the keys and items to a dict. pairs = zip(defaults.keys(), items) features_dict = dict(pairs) # Remove the label from the features_dict label = features_dict.pop(y_name) return features_dict, label def has_no_question_marks(line): """Returns True if the line of text has no question marks.""" # split the line into an array of characters chars = tf.string_split(line[tf.newaxis], "").values # for each character check if it is a question mark is_question = tf.equal(chars, "?") any_question = tf.reduce_any(is_question) no_question = ~any_question return no_question def in_training_set(line): """Returns a boolean tensor, true if the line is in the training set.""" # If you randomly split the dataset you won't get the same split in both # sessions if you stop and restart training later. Also a simple # random split won't work with a dataset that's too big to `.cache()` as # we are doing here. num_buckets = 1000000 bucket_id = tf.string_to_hash_bucket_fast(line, num_buckets) # Use the hash bucket id as a random number that's deterministic per example return bucket_id < int(train_fraction * num_buckets) def in_test_set(line): """Returns a boolean tensor, true if the line is in the training set.""" # Items not in the training set are in the test set. # This line must use `~` instead of `not` because `not` only works on python # booleans but we are dealing with symbolic tensors. return ~in_training_set(line) base_dataset = (tf.contrib.data # Get the lines from the file. .TextLineDataset(path) # drop lines with question marks. .filter(has_no_question_marks)) train = (base_dataset # Take only the training-set lines. .filter(in_training_set) # Decode each line into a (features_dict, label) pair. .map(decode_line) # Cache data so you only decode the file once. .cache()) # Do the same for the test-set. test = (base_dataset.filter(in_test_set).cache().map(decode_line)) return train, test def raw_dataframe(): """Load the imports85 data as a pd.DataFrame.""" # Download and cache the data path = _get_imports85() # Load it into a pandas dataframe df = pd.read_csv(path, names=types.keys(), dtype=types, na_values="?") return df def load_data(y_name="price", train_fraction=0.7, seed=None): """Get the imports85 data set. A description of the data is available at: https://archive.ics.uci.edu/ml/datasets/automobile The data itself can be found at: https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data Args: y_name: the column to return as the label. train_fraction: the fraction of the dataset to use for training. seed: The random seed to use when shuffling the data. `None` generates a unique shuffle every run. Returns: a pair of pairs where the first pair is the training data, and the second is the test data: `(x_train, y_train), (x_test, y_test) = get_imports85_dataset(...)` `x` contains a pandas DataFrame of features, while `y` contains the label array. """ # Load the raw data columns. data = raw_dataframe() # Delete rows with unknowns data = data.dropna() # Shuffle the data np.random.seed(seed) # Split the data into train/test subsets. x_train = data.sample(frac=train_fraction, random_state=seed) x_test = data.drop(x_train.index) # Extract the label from the features dataframe. y_train = x_train.pop(y_name) y_test = x_test.pop(y_name) return (x_train, y_train), (x_test, y_test)
apache-2.0
waynenilsen/statsmodels
statsmodels/sandbox/examples/ex_kaplan_meier.py
33
2838
#An example for the Kaplan-Meier estimator from __future__ import print_function from statsmodels.compat.python import lrange import statsmodels.api as sm import matplotlib.pyplot as plt import numpy as np from statsmodels.sandbox.survival2 import KaplanMeier #Getting the strike data as an array dta = sm.datasets.strikes.load() print('basic data') print('\n') dta = list(dta.values()[-1]) print(dta[lrange(5),:]) print('\n') #Create the KaplanMeier object and fit the model km = KaplanMeier(dta,0) km.fit() #show the results km.plot() print('basic model') print('\n') km.summary() print('\n') #Mutiple survival curves km2 = KaplanMeier(dta,0,exog=1) km2.fit() print('more than one curve') print('\n') km2.summary() print('\n') km2.plot() #with censoring censoring = np.ones_like(dta[:,0]) censoring[dta[:,0] > 80] = 0 dta = np.c_[dta,censoring] print('with censoring') print('\n') print(dta[lrange(5),:]) print('\n') km3 = KaplanMeier(dta,0,exog=1,censoring=2) km3.fit() km3.summary() print('\n') km3.plot() #Test for difference of survival curves log_rank = km3.test_diff([0.0645,-0.03957]) print('log rank test') print('\n') print(log_rank) print('\n') #The zeroth element of log_rank is the chi-square test statistic #for the difference between the survival curves for exog = 0.0645 #and exog = -0.03957, the index one element is the degrees of freedom for #the test, and the index two element is the p-value for the test wilcoxon = km3.test_diff([0.0645,-0.03957], rho=1) print('Wilcoxon') print('\n') print(wilcoxon) print('\n') #Same info as log_rank, but for Peto and Peto modification to the #Gehan-Wilcoxon test #User specified functions for tests #A wider range of rates can be accessed by using the 'weight' parameter #for the test_diff method #For example, if the desire weights are S(t)*(1-S(t)), where S(t) is a pooled #estimate for the survival function, this could be computed by doing def weights(t): #must accept one arguement, even though it is not used here s = KaplanMeier(dta,0,censoring=2) s.fit() s = s.results[0][0] s = s * (1 - s) return s #KaplanMeier provides an array of times to the weighting function #internally, so the weighting function must accept one arguement test = km3.test_diff([0.0645,-0.03957], weight=weights) print('user specified weights') print('\n') print(test) print('\n') #Groups with nan names #These can be handled by passing the data to KaplanMeier as an array of strings groups = np.ones_like(dta[:,1]) groups = groups.astype('S4') groups[dta[:,1] > 0] = 'high' groups[dta[:,1] <= 0] = 'low' dta = dta.astype('S4') dta[:,1] = groups print('with nan group names') print('\n') print(dta[lrange(5),:]) print('\n') km4 = KaplanMeier(dta,0,exog=1,censoring=2) km4.fit() km4.summary() print('\n') km4.plot() #show all the plots plt.show()
bsd-3-clause
INCF/BIDS2ISATab
setup.py
1
2176
from setuptools import setup import os here = os.path.abspath(os.path.dirname(__file__)) setup( name="BIDS2ISATab", # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # http://packaging.python.org/en/latest/tutorial.html#version version='0.1.0', description="Command line tool generating ISA-Tab compatible description from a Brain Imaging Data Structure " "compatible dataset.", long_description="Command line tool generating ISA-Tab compatible description from a Brain Imaging Data Structure " "compatible dataset.", # The project URL. url='https://github.com/INCF/BIDS2ISATab', # Choose your license license='BSD', classifiers=[ # How mature is this project? Common values are # 3 - Alpha # 4 - Beta # 5 - Production/Stable 'Development Status :: 4 - Beta', # Pick your license as you wish (should match "license" above) 'License :: OSI Approved :: BSD License', # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.5', ], # What does your project relate to? keywords='bids isatab', # You can just specify the packages manually here if your project is # simple. Or you can use find_packages. packages=["bids2isatab"], # List run-time dependencies here. These will be installed by pip when your # project is installed. install_requires = ["future", "pandas", 'nibabel'], include_package_data=True, # To provide executable scripts, use entry points in preference to the # "scripts" keyword. Entry points provide cross-platform support and allow # pip to create the appropriate form of executable for the target platform. entry_points={ 'console_scripts': [ 'bids2isatab=bids2isatab.main:main', ], }, )
apache-2.0
MichaelAquilina/numpy
numpy/lib/npyio.py
42
71218
from __future__ import division, absolute_import, print_function import sys import os import re import itertools import warnings import weakref from operator import itemgetter import numpy as np from . import format from ._datasource import DataSource from numpy.core.multiarray import packbits, unpackbits from ._iotools import ( LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, has_nested_fields, flatten_dtype, easy_dtype, _bytes_to_name ) from numpy.compat import ( asbytes, asstr, asbytes_nested, bytes, basestring, unicode ) if sys.version_info[0] >= 3: import pickle else: import cPickle as pickle from future_builtins import map loads = pickle.loads __all__ = [ 'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource' ] class BagObj(object): """ BagObj(obj) Convert attribute look-ups to getitems on the object passed in. Parameters ---------- obj : class instance Object on which attribute look-up is performed. Examples -------- >>> from numpy.lib.npyio import BagObj as BO >>> class BagDemo(object): ... def __getitem__(self, key): # An instance of BagObj(BagDemo) ... # will call this method when any ... # attribute look-up is required ... result = "Doesn't matter what you want, " ... return result + "you're gonna get this" ... >>> demo_obj = BagDemo() >>> bagobj = BO(demo_obj) >>> bagobj.hello_there "Doesn't matter what you want, you're gonna get this" >>> bagobj.I_can_be_anything "Doesn't matter what you want, you're gonna get this" """ def __init__(self, obj): # Use weakref to make NpzFile objects collectable by refcount self._obj = weakref.proxy(obj) def __getattribute__(self, key): try: return object.__getattribute__(self, '_obj')[key] except KeyError: raise AttributeError(key) def __dir__(self): """ Enables dir(bagobj) to list the files in an NpzFile. This also enables tab-completion in an interpreter or IPython. """ return object.__getattribute__(self, '_obj').keys() def zipfile_factory(*args, **kwargs): import zipfile kwargs['allowZip64'] = True return zipfile.ZipFile(*args, **kwargs) class NpzFile(object): """ NpzFile(fid) A dictionary-like object with lazy-loading of files in the zipped archive provided on construction. `NpzFile` is used to load files in the NumPy ``.npz`` data archive format. It assumes that files in the archive have a ``.npy`` extension, other files are ignored. The arrays and file strings are lazily loaded on either getitem access using ``obj['key']`` or attribute lookup using ``obj.f.key``. A list of all files (without ``.npy`` extensions) can be obtained with ``obj.files`` and the ZipFile object itself using ``obj.zip``. Attributes ---------- files : list of str List of all files in the archive with a ``.npy`` extension. zip : ZipFile instance The ZipFile object initialized with the zipped archive. f : BagObj instance An object on which attribute can be performed as an alternative to getitem access on the `NpzFile` instance itself. allow_pickle : bool, optional Allow loading pickled data. Default: True pickle_kwargs : dict, optional Additional keyword arguments to pass on to pickle.load. These are only useful when loading object arrays saved on Python 2 when using Python 3. Parameters ---------- fid : file or str The zipped archive to open. This is either a file-like object or a string containing the path to the archive. own_fid : bool, optional Whether NpzFile should close the file handle. Requires that `fid` is a file-like object. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npz = np.load(outfile) >>> isinstance(npz, np.lib.io.NpzFile) True >>> npz.files ['y', 'x'] >>> npz['x'] # getitem access array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> npz.f.x # attribute lookup array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ def __init__(self, fid, own_fid=False, allow_pickle=True, pickle_kwargs=None): # Import is postponed to here since zipfile depends on gzip, an # optional component of the so-called standard library. _zip = zipfile_factory(fid) self._files = _zip.namelist() self.files = [] self.allow_pickle = allow_pickle self.pickle_kwargs = pickle_kwargs for x in self._files: if x.endswith('.npy'): self.files.append(x[:-4]) else: self.files.append(x) self.zip = _zip self.f = BagObj(self) if own_fid: self.fid = fid else: self.fid = None def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): """ Close the file. """ if self.zip is not None: self.zip.close() self.zip = None if self.fid is not None: self.fid.close() self.fid = None self.f = None # break reference cycle def __del__(self): self.close() def __getitem__(self, key): # FIXME: This seems like it will copy strings around # more than is strictly necessary. The zipfile # will read the string and then # the format.read_array will copy the string # to another place in memory. # It would be better if the zipfile could read # (or at least uncompress) the data # directly into the array memory. member = 0 if key in self._files: member = 1 elif key in self.files: member = 1 key += '.npy' if member: bytes = self.zip.open(key) magic = bytes.read(len(format.MAGIC_PREFIX)) bytes.close() if magic == format.MAGIC_PREFIX: bytes = self.zip.open(key) return format.read_array(bytes, allow_pickle=self.allow_pickle, pickle_kwargs=self.pickle_kwargs) else: return self.zip.read(key) else: raise KeyError("%s is not a file in the archive" % key) def __iter__(self): return iter(self.files) def items(self): """ Return a list of tuples, with each tuple (filename, array in file). """ return [(f, self[f]) for f in self.files] def iteritems(self): """Generator that returns tuples (filename, array in file).""" for f in self.files: yield (f, self[f]) def keys(self): """Return files in the archive with a ``.npy`` extension.""" return self.files def iterkeys(self): """Return an iterator over the files in the archive.""" return self.__iter__() def __contains__(self, key): return self.files.__contains__(key) def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII'): """ Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. Parameters ---------- file : file-like object or string The file to read. File-like objects must support the ``seek()`` and ``read()`` methods. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see `numpy.memmap` for a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. allow_pickle : bool, optional Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: True fix_imports : bool, optional Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. If `fix_imports` is True, pickle will try to map the old Python 2 names to the new names used in Python 3. encoding : str, optional What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than 'latin1', 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical data. Default: 'ASCII' Returns ------- result : array, tuple, dict, etc. Data stored in the file. For ``.npz`` files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors. Raises ------ IOError If the input file does not exist or cannot be read. ValueError The file contains an object array, but allow_pickle=False given. See Also -------- save, savez, savez_compressed, loadtxt memmap : Create a memory-map to an array stored in a file on disk. Notes ----- - If the file contains pickle data, then whatever object is stored in the pickle is returned. - If the file is a ``.npy`` file, then a single array is returned. - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. - If the file is a ``.npz`` file, the returned value supports the context manager protocol in a similar fashion to the open function:: with load('foo.npz') as data: a = data['a'] The underlying file descriptor is closed when exiting the 'with' block. Examples -------- Store data to disk, and load it again: >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) >>> np.load('/tmp/123.npy') array([[1, 2, 3], [4, 5, 6]]) Store compressed data to disk, and load it again: >>> a=np.array([[1, 2, 3], [4, 5, 6]]) >>> b=np.array([1, 2]) >>> np.savez('/tmp/123.npz', a=a, b=b) >>> data = np.load('/tmp/123.npz') >>> data['a'] array([[1, 2, 3], [4, 5, 6]]) >>> data['b'] array([1, 2]) >>> data.close() Mem-map the stored array, and then access the second row directly from disk: >>> X = np.load('/tmp/123.npy', mmap_mode='r') >>> X[1, :] memmap([4, 5, 6]) """ import gzip own_fid = False if isinstance(file, basestring): fid = open(file, "rb") own_fid = True else: fid = file if encoding not in ('ASCII', 'latin1', 'bytes'): # The 'encoding' value for pickle also affects what encoding # the serialized binary data of Numpy arrays is loaded # in. Pickle does not pass on the encoding information to # Numpy. The unpickling code in numpy.core.multiarray is # written to assume that unicode data appearing where binary # should be is in 'latin1'. 'bytes' is also safe, as is 'ASCII'. # # Other encoding values can corrupt binary data, and we # purposefully disallow them. For the same reason, the errors= # argument is not exposed, as values other than 'strict' # result can similarly silently corrupt numerical data. raise ValueError("encoding must be 'ASCII', 'latin1', or 'bytes'") if sys.version_info[0] >= 3: pickle_kwargs = dict(encoding=encoding, fix_imports=fix_imports) else: # Nothing to do on Python 2 pickle_kwargs = {} try: # Code to distinguish from NumPy binary files and pickles. _ZIP_PREFIX = asbytes('PK\x03\x04') N = len(format.MAGIC_PREFIX) magic = fid.read(N) fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) # Transfer file ownership to NpzFile tmp = own_fid own_fid = False return NpzFile(fid, own_fid=tmp, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) else: return format.read_array(fid, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) else: # Try a pickle if not allow_pickle: raise ValueError("allow_pickle=False, but file does not contain " "non-pickled data") try: return pickle.load(fid, **pickle_kwargs) except: raise IOError( "Failed to interpret file %s as a pickle" % repr(file)) finally: if own_fid: fid.close() def save(file, arr, allow_pickle=True, fix_imports=True): """ Save an array to a binary file in NumPy ``.npy`` format. Parameters ---------- file : file or str File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string, a ``.npy`` extension will be appended to the file name if it does not already have one. allow_pickle : bool, optional Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example if the stored objects require libraries that are not available, and not all pickled data is compatible between Python 2 and Python 3). Default: True fix_imports : bool, optional Only useful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way. If `fix_imports` is True, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2. arr : array_like Array data to be saved. See Also -------- savez : Save several arrays into a ``.npz`` archive savetxt, load Notes ----- For a description of the ``.npy`` format, see the module docstring of `numpy.lib.format` or the Numpy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ own_fid = False if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") own_fid = True else: fid = file if sys.version_info[0] >= 3: pickle_kwargs = dict(fix_imports=fix_imports) else: # Nothing to do on Python 2 pickle_kwargs = None try: arr = np.asanyarray(arr) format.write_array(fid, arr, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) finally: if own_fid: fid.close() def savez(file, *args, **kwds): """ Save several arrays into a single file in uncompressed ``.npz`` format. If arguments are passed in with no keywords, the corresponding variable names, in the ``.npz`` file, are 'arr_0', 'arr_1', etc. If keyword arguments are given, the corresponding variable names, in the ``.npz`` file will match the keyword names. Parameters ---------- file : str or file Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the ``.npz`` extension will be appended to the file name if it is not already there. args : Arguments, optional Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside `savez`, the arrays will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments, optional Arrays to save to the file. Arrays will be saved in the file with the keyword names. Returns ------- None See Also -------- save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in ``.npy`` format. For a description of the ``.npy`` format, see `numpy.lib.format` or the Numpy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the ``.files`` attribute), and for the arrays themselves. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) Using `savez` with \\*args, the arrays are saved with default names. >>> np.savez(outfile, x, y) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_1', 'arr_0'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Using `savez` with \\**kwds, the arrays are saved with the keyword names. >>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npzfile = np.load(outfile) >>> npzfile.files ['y', 'x'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ _savez(file, args, kwds, False) def savez_compressed(file, *args, **kwds): """ Save several arrays into a single file in compressed ``.npz`` format. If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : str File name of ``.npz`` file. args : Arguments Function arguments. kwds : Keyword arguments Keywords. See Also -------- numpy.savez : Save several arrays into an uncompressed ``.npz`` file format numpy.load : Load the files created by savez_compressed. """ _savez(file, args, kwds, True) def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None): # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile # Import deferred for startup time improvement import tempfile if isinstance(file, basestring): if not file.endswith('.npz'): file = file + '.npz' namedict = kwds for i, val in enumerate(args): key = 'arr_%d' % i if key in namedict.keys(): raise ValueError( "Cannot use un-named variables and keyword %s" % key) namedict[key] = val if compress: compression = zipfile.ZIP_DEFLATED else: compression = zipfile.ZIP_STORED zipf = zipfile_factory(file, mode="w", compression=compression) # Stage arrays in a temporary file on disk, before writing to zip. fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy') os.close(fd) try: for key, val in namedict.items(): fname = key + '.npy' fid = open(tmpfile, 'wb') try: format.write_array(fid, np.asanyarray(val), allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) fid.close() fid = None zipf.write(tmpfile, arcname=fname) finally: if fid: fid.close() finally: os.remove(tmpfile) zipf.close() def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ def floatconv(x): x.lower() if b'0x' in x: return float.fromhex(asstr(x)) return float(x) typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) if issubclass(typ, np.uint64): return np.uint64 if issubclass(typ, np.int64): return np.int64 if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.floating): return floatconv elif issubclass(typ, np.complex): return lambda x: complex(asstr(x)) elif issubclass(typ, np.bytes_): return bytes else: return str def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0): """ Load data from a text file. Each row in the text file must have the same number of values. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings for Python 3k. dtype : data-type, optional Data-type of the resulting array; default: float. If this is a structured data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type. comments : str or sequence, optional The characters or list of characters used to indicate the start of a comment; default: '#'. delimiter : str, optional The string used to separate values. By default, this is any whitespace. converters : dict, optional A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None. skiprows : int, optional Skip the first `skiprows` lines; default: 0. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)``. When used with a structured data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 Returns ------- out : ndarray Data read from the text file. See Also -------- load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files Notes ----- This function aims to be a fast reader for simply formatted files. The `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. .. versionadded:: 1.10.0 The strings produced by the Python float.hex method can be used as input for floats. Examples -------- >>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\\n2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72\\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([('M', 21, 72.0), ('F', 35, 58.0)], dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')]) >>> c = StringIO("1,0,2\\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([ 1., 3.]) >>> y array([ 2., 4.]) """ # Type conversions for Py3 convenience if comments is not None: if isinstance(comments, (basestring, bytes)): comments = [asbytes(comments)] else: comments = [asbytes(comment) for comment in comments] # Compile regex for comments beforehand comments = (re.escape(comment) for comment in comments) regex_comments = re.compile(asbytes('|').join(comments)) user_converters = converters if delimiter is not None: delimiter = asbytes(delimiter) if usecols is not None: usecols = list(usecols) fown = False try: if _is_string_like(fname): fown = True if fname.endswith('.gz'): import gzip fh = iter(gzip.GzipFile(fname)) elif fname.endswith('.bz2'): import bz2 fh = iter(bz2.BZ2File(fname)) elif sys.version_info[0] == 2: fh = iter(open(fname, 'U')) else: fh = iter(open(fname)) else: fh = iter(fname) except TypeError: raise ValueError('fname must be a string, file handle, or generator') X = [] def flatten_dtype(dt): """Unpack a structured data-type, and produce re-packing info.""" if dt.names is None: # If the dtype is flattened, return. # If the dtype has a shape, the dtype occurs # in the list more than once. shape = dt.shape if len(shape) == 0: return ([dt.base], None) else: packing = [(shape[-1], list)] if len(shape) > 1: for dim in dt.shape[-2::-1]: packing = [(dim*packing[0][0], packing*dim)] return ([dt.base] * int(np.prod(dt.shape)), packing) else: types = [] packing = [] for field in dt.names: tp, bytes = dt.fields[field] flat_dt, flat_packing = flatten_dtype(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays if len(tp.shape) > 0: packing.extend(flat_packing) else: packing.append((len(flat_dt), flat_packing)) return (types, packing) def pack_items(items, packing): """Pack items into nested lists based on re-packing info.""" if packing is None: return items[0] elif packing is tuple: return tuple(items) elif packing is list: return list(items) else: start = 0 ret = [] for length, subpacking in packing: ret.append(pack_items(items[start:start+length], subpacking)) start += length return tuple(ret) def split_line(line): """Chop off comments, strip, and split at delimiter. Note that although the file is opened as text, this function returns bytes. """ line = asbytes(line) if comments is not None: line = regex_comments.split(asbytes(line), maxsplit=1)[0] line = line.strip(asbytes('\r\n')) if line: return line.split(delimiter) else: return [] try: # Make sure we're dealing with a proper dtype dtype = np.dtype(dtype) defconv = _getconv(dtype) # Skip the first `skiprows` lines for i in range(skiprows): next(fh) # Read until we find a line with some values, and use # it to estimate the number of columns, N. first_vals = None try: while not first_vals: first_line = next(fh) first_vals = split_line(first_line) except StopIteration: # End of lines reached first_line = '' first_vals = [] warnings.warn('loadtxt: Empty input file: "%s"' % fname) N = len(usecols or first_vals) dtype_types, packing = flatten_dtype(dtype) if len(dtype_types) > 1: # We're dealing with a structured array, each field of # the dtype matches a column converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype converters = [defconv for i in range(N)] if N > 1: packing = [(N, tuple)] # By preference, use the converters specified by the user for i, conv in (user_converters or {}).items(): if usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue converters[i] = conv # Parse each line, including the first for i, line in enumerate(itertools.chain([first_line], fh)): vals = split_line(line) if len(vals) == 0: continue if usecols: vals = [vals[i] for i in usecols] if len(vals) != N: line_num = i + skiprows + 1 raise ValueError("Wrong number of columns at line %d" % line_num) # Convert each value according to its column and store items = [conv(val) for (conv, val) in zip(converters, vals)] # Then pack it according to the dtype's nesting items = pack_items(items, packing) X.append(items) finally: if fown: fh.close() X = np.array(X, dtype) # Multicolumn data are returned with shape (1, N, M), i.e. # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) # Verify that the array has at least dimensions `ndmin`. # Check correctness of the values of `ndmin` if ndmin not in [0, 1, 2]: raise ValueError('Illegal value of ndmin keyword: %s' % ndmin) # Tweak the size and shape of the arrays - remove extraneous dimensions if X.ndim > ndmin: X = np.squeeze(X) # and ensure we have the minimum number of dimensions asked for # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 if X.ndim < ndmin: if ndmin == 1: X = np.atleast_1d(X) elif ndmin == 2: X = np.atleast_2d(X).T if unpack: if len(dtype_types) > 1: # For structured arrays, return an array for each field. return [X[field] for field in dtype.names] else: return X.T else: return X def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '): """ Save an array to a text file. Parameters ---------- fname : filename or file handle If the filename ends in ``.gz``, the file is automatically saved in compressed gzip format. `loadtxt` understands gzipped files transparently. X : array_like Data to be saved to a text file. fmt : str or sequence of strs, optional A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. For complex `X`, the legal options for `fmt` are: a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted like `' (%s+%sj)' % (fmt, fmt)` b) a full string specifying every real and imaginary part, e.g. `' %.4e %+.4j %.4e %+.4j %.4e %+.4j'` for 3 columns c) a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns delimiter : str, optional String or character separating columns. newline : str, optional String or character separating lines. .. versionadded:: 1.5.0 header : str, optional String that will be written at the beginning of the file. .. versionadded:: 1.7.0 footer : str, optional String that will be written at the end of the file. .. versionadded:: 1.7.0 comments : str, optional String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# ', as expected by e.g. ``numpy.loadtxt``. .. versionadded:: 1.7.0 See Also -------- save : Save an array to a binary file in NumPy ``.npy`` format savez : Save several arrays into an uncompressed ``.npz`` archive savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- Further explanation of the `fmt` parameter (``%[flag]width[.precision]specifier``): flags: ``-`` : left justify ``+`` : Forces to precede result with + or -. ``0`` : Left pad the number with zeros instead of space (see width). width: Minimum number of characters to be printed. The value is not truncated if it has more characters. precision: - For integer specifiers (eg. ``d,i,o,x``), the minimum number of digits. - For ``e, E`` and ``f`` specifiers, the number of digits to print after the decimal point. - For ``g`` and ``G``, the maximum number of significant digits. - For ``s``, the maximum number of characters. specifiers: ``c`` : character ``d`` or ``i`` : signed decimal integer ``e`` or ``E`` : scientific notation with ``e`` or ``E``. ``f`` : decimal floating point ``g,G`` : use the shorter of ``e,E`` or ``f`` ``o`` : signed octal ``s`` : string of characters ``u`` : unsigned decimal integer ``x,X`` : unsigned hexadecimal integer This explanation of ``fmt`` is not complete, for an exhaustive specification see [1]_. References ---------- .. [1] `Format Specification Mini-Language <http://docs.python.org/library/string.html# format-specification-mini-language>`_, Python Documentation. Examples -------- >>> x = y = z = np.arange(0.0,5.0,1.0) >>> np.savetxt('test.out', x, delimiter=',') # X is an array >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation """ # Py3 conversions first if isinstance(fmt, bytes): fmt = asstr(fmt) delimiter = asstr(delimiter) own_fh = False if _is_string_like(fname): own_fh = True if fname.endswith('.gz'): import gzip fh = gzip.open(fname, 'wb') else: if sys.version_info[0] >= 3: fh = open(fname, 'wb') else: fh = open(fname, 'w') elif hasattr(fname, 'write'): fh = fname else: raise ValueError('fname must be a string or file handle') try: X = np.asarray(X) # Handle 1-dimensional arrays if X.ndim == 1: # Common case -- 1d array of numbers if X.dtype.names is None: X = np.atleast_2d(X).T ncol = 1 # Complex dtype -- each field indicates a separate column else: ncol = len(X.dtype.descr) else: ncol = X.shape[1] iscomplex_X = np.iscomplexobj(X) # `fmt` can be a string with multiple insertion points or a # list of formats. E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') if type(fmt) in (list, tuple): if len(fmt) != ncol: raise AttributeError('fmt has wrong shape. %s' % str(fmt)) format = asstr(delimiter).join(map(asstr, fmt)) elif isinstance(fmt, str): n_fmt_chars = fmt.count('%') error = ValueError('fmt has wrong number of %% formats: %s' % fmt) if n_fmt_chars == 1: if iscomplex_X: fmt = [' (%s+%sj)' % (fmt, fmt), ] * ncol else: fmt = [fmt, ] * ncol format = delimiter.join(fmt) elif iscomplex_X and n_fmt_chars != (2 * ncol): raise error elif ((not iscomplex_X) and n_fmt_chars != ncol): raise error else: format = fmt else: raise ValueError('invalid fmt: %r' % (fmt,)) if len(header) > 0: header = header.replace('\n', '\n' + comments) fh.write(asbytes(comments + header + newline)) if iscomplex_X: for row in X: row2 = [] for number in row: row2.append(number.real) row2.append(number.imag) fh.write(asbytes(format % tuple(row2) + newline)) else: for row in X: try: fh.write(asbytes(format % tuple(row) + newline)) except TypeError: raise TypeError("Mismatch between array dtype ('%s') and " "format specifier ('%s')" % (str(X.dtype), format)) if len(footer) > 0: footer = footer.replace('\n', '\n' + comments) fh.write(asbytes(comments + footer + newline)) finally: if own_fh: fh.close() def fromregex(file, regexp, dtype): """ Construct an array from a text file, using regular expression parsing. The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array. Parameters ---------- file : str or file File name or file object to read. regexp : str or regexp Regular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype. dtype : dtype or list of dtypes Dtype for the structured array. Returns ------- output : ndarray The output array, containing the part of the content of `file` that was matched by `regexp`. `output` is always a structured array. Raises ------ TypeError When `dtype` is not a valid dtype for a structured array. See Also -------- fromstring, loadtxt Notes ----- Dtypes for structured arrays can be specified in several forms, but all forms specify at least the data type and field name. For details see `doc.structured_arrays`. Examples -------- >>> f = open('test.dat', 'w') >>> f.write("1312 foo\\n1534 bar\\n444 qux") >>> f.close() >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex('test.dat', regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], dtype=[('num', '<i8'), ('key', '|S3')]) >>> output['num'] array([1312, 1534, 444], dtype=int64) """ own_fh = False if not hasattr(file, "read"): file = open(file, 'rb') own_fh = True try: if not hasattr(regexp, 'match'): regexp = re.compile(asbytes(regexp)) if not isinstance(dtype, np.dtype): dtype = np.dtype(dtype) seq = regexp.findall(file.read()) if seq and not isinstance(seq[0], tuple): # Only one group is in the regexp. # Create the new array as a single data-type and then # re-interpret as a single-field structured array. newdtype = np.dtype(dtype[dtype.names[0]]) output = np.array(seq, dtype=newdtype) output.dtype = dtype else: output = np.array(seq, dtype=dtype) return output finally: if own_fh: file.close() #####-------------------------------------------------------------------------- #---- --- ASCII functions --- #####-------------------------------------------------------------------------- def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None): """ Load data from a text file, with missing values handled as specified. Each line past the first `skip_header` lines is split at the `delimiter` character, and characters following the `comments` character are discarded. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is `.gz` or `.bz2`, the file is first decompressed. Note that generators must return byte strings in Python 3k. dtype : dtype, optional Data type of the resulting array. If None, the dtypes will be determined by the contents of each column, individually. comments : str, optional The character used to indicate the start of a comment. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive whitespaces act as delimiter. An integer or sequence of integers can also be provided as width(s) of each field. skiprows : int, optional `skiprows` was removed in numpy 1.10. Please use `skip_header` instead. skip_header : int, optional The number of lines to skip at the beginning of the file. skip_footer : int, optional The number of lines to skip at the end of the file. converters : variable, optional The set of functions that convert the data of a column to a value. The converters can also be used to provide a default value for missing data: ``converters = {3: lambda s: float(s or 0)}``. missing : variable, optional `missing` was removed in numpy 1.10. Please use `missing_values` instead. missing_values : variable, optional The set of strings corresponding to missing data. filling_values : variable, optional The set of values to be used as default when the data are missing. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. names : {None, True, str, sequence}, optional If `names` is True, the field names are read from the first valid line after the first `skip_header` lines. If `names` is a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. If `names` is None, the names of the dtype fields will be used, if any. excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended an underscore: for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional Whether to automatically strip white spaces from the variables. replace_space : char, optional Character(s) used in replacement of white spaces in the variables names. By default, use a '_'. case_sensitive : {True, False, 'upper', 'lower'}, optional If True, field names are case sensitive. If False or 'upper', field names are converted to upper case. If 'lower', field names are converted to lower case. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)`` usemask : bool, optional If True, return a masked array. If False, return a regular array. loose : bool, optional If True, do not raise errors for invalid values. invalid_raise : bool, optional If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped. max_rows : int, optional The maximum number of rows to read. Must not be used with skip_footer at the same time. If given, the value must be at least 1. Default is to read the entire file. .. versionadded:: 1.10.0 Returns ------- out : ndarray Data read from the text file. If `usemask` is True, this is a masked array. See Also -------- numpy.loadtxt : equivalent function when no data is missing. Notes ----- * When spaces are used as delimiters, or when no delimiter has been given as input, there should not be any missing data between two fields. * When the variables are named (either by a flexible dtype or with `names`, there must not be any header in the file (else a ValueError exception is raised). * Individual values are not stripped of spaces by default. When using a custom converter, make sure the function does remove spaces. References ---------- .. [1] Numpy User Guide, section `I/O with Numpy <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_. Examples --------- >>> from io import StringIO >>> import numpy as np Comma delimited file with mixed dtype >>> s = StringIO("1,1.3,abcde") >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ... ('mystring','S5')], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Using dtype = None >>> s.seek(0) # needed for StringIO example only >>> data = np.genfromtxt(s, dtype=None, ... names = ['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Specifying dtype and names >>> s.seek(0) >>> data = np.genfromtxt(s, dtype="i8,f8,S5", ... names=['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) An example with fixed-width columns >>> s = StringIO("11.3abcde") >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], ... delimiter=[1,3,5]) >>> data array((1, 1.3, 'abcde'), dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')]) """ if max_rows is not None: if skip_footer: raise ValueError( "The keywords 'skip_footer' and 'max_rows' can not be " "specified at the same time.") if max_rows < 1: raise ValueError("'max_rows' must be at least 1.") # Py3 data conversions to bytes, for convenience if comments is not None: comments = asbytes(comments) if isinstance(delimiter, unicode): delimiter = asbytes(delimiter) if isinstance(missing_values, (unicode, list, tuple)): missing_values = asbytes_nested(missing_values) # if usemask: from numpy.ma import MaskedArray, make_mask_descr # Check the input dictionary of converters user_converters = converters or {} if not isinstance(user_converters, dict): raise TypeError( "The input argument 'converter' should be a valid dictionary " "(got '%s' instead)" % type(user_converters)) # Initialize the filehandle, the LineSplitter and the NameValidator own_fhd = False try: if isinstance(fname, basestring): if sys.version_info[0] == 2: fhd = iter(np.lib._datasource.open(fname, 'rbU')) else: fhd = iter(np.lib._datasource.open(fname, 'rb')) own_fhd = True else: fhd = iter(fname) except TypeError: raise TypeError( "fname must be a string, filehandle, or generator. " "(got %s instead)" % type(fname)) split_line = LineSplitter(delimiter=delimiter, comments=comments, autostrip=autostrip)._handyman validate_names = NameValidator(excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Skip the first `skip_header` rows for i in range(skip_header): next(fhd) # Keep on until we find the first valid values first_values = None try: while not first_values: first_line = next(fhd) if names is True: if comments in first_line: first_line = ( asbytes('').join(first_line.split(comments)[1:])) first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty first_line = asbytes('') first_values = [] warnings.warn('genfromtxt: Empty input file: "%s"' % fname) # Should we take the first values as names ? if names is True: fval = first_values[0].strip() if fval in comments: del first_values[0] # Check the columns to use: make sure `usecols` is a list if usecols is not None: try: usecols = [_.strip() for _ in usecols.split(",")] except AttributeError: try: usecols = list(usecols) except TypeError: usecols = [usecols, ] nbcols = len(usecols or first_values) # Check the names and overwrite the dtype.names if needed if names is True: names = validate_names([_bytes_to_name(_.strip()) for _ in first_values]) first_line = asbytes('') elif _is_string_like(names): names = validate_names([_.strip() for _ in names.split(',')]) elif names: names = validate_names(names) # Get the dtype if dtype is not None: dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names, excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Make sure the names is a list (for 2.5) if names is not None: names = list(names) if usecols: for (i, current) in enumerate(usecols): # if usecols is a list of names, convert to a list of indices if _is_string_like(current): usecols[i] = names.index(current) elif current < 0: usecols[i] = current + len(first_values) # If the dtype is not None, make sure we update it if (dtype is not None) and (len(dtype) > nbcols): descr = dtype.descr dtype = np.dtype([descr[_] for _ in usecols]) names = list(dtype.names) # If `names` is not None, update the names elif (names is not None) and (len(names) > nbcols): names = [names[_] for _ in usecols] elif (names is not None) and (dtype is not None): names = list(dtype.names) # Process the missing values ............................... # Rename missing_values for convenience user_missing_values = missing_values or () # Define the list of missing_values (one column: one list) missing_values = [list([asbytes('')]) for _ in range(nbcols)] # We have a dictionary: process it field by field if isinstance(user_missing_values, dict): # Loop on the items for (key, val) in user_missing_values.items(): # Is the key a string ? if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped continue # Redefine the key as needed if it's a column number if usecols: try: key = usecols.index(key) except ValueError: pass # Transform the value as a list of string if isinstance(val, (list, tuple)): val = [str(_) for _ in val] else: val = [str(val), ] # Add the value(s) to the current list of missing if key is None: # None acts as default for miss in missing_values: miss.extend(val) else: missing_values[key].extend(val) # We have a sequence : each item matches a column elif isinstance(user_missing_values, (list, tuple)): for (value, entry) in zip(user_missing_values, missing_values): value = str(value) if value not in entry: entry.append(value) # We have a string : apply it to all entries elif isinstance(user_missing_values, bytes): user_value = user_missing_values.split(asbytes(",")) for entry in missing_values: entry.extend(user_value) # We have something else: apply it to all entries else: for entry in missing_values: entry.extend([str(user_missing_values)]) # Process the filling_values ............................... # Rename the input for convenience user_filling_values = filling_values if user_filling_values is None: user_filling_values = [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually if isinstance(user_filling_values, dict): for (key, val) in user_filling_values.items(): if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped, continue # Redefine the key if it's a column number and usecols is defined if usecols: try: key = usecols.index(key) except ValueError: pass # Add the value to the list filling_values[key] = val # We have a sequence : update on a one-to-one basis elif isinstance(user_filling_values, (list, tuple)): n = len(user_filling_values) if (n <= nbcols): filling_values[:n] = user_filling_values else: filling_values = user_filling_values[:nbcols] # We have something else : use it for all entries else: filling_values = [user_filling_values] * nbcols # Initialize the converters ................................ if dtype is None: # Note: we can't use a [...]*nbcols, as we would have 3 times the same # ... converter, instead of 3 different converters. converters = [StringConverter(None, missing_values=miss, default=fill) for (miss, fill) in zip(missing_values, filling_values)] else: dtype_flat = flatten_dtype(dtype, flatten_base=True) # Initialize the converters if len(dtype_flat) > 1: # Flexible type : get a converter from each dtype zipit = zip(dtype_flat, missing_values, filling_values) converters = [StringConverter(dt, locked=True, missing_values=miss, default=fill) for (dt, miss, fill) in zipit] else: # Set to a default converter (but w/ different missing values) zipit = zip(missing_values, filling_values) converters = [StringConverter(dtype, locked=True, missing_values=miss, default=fill) for (miss, fill) in zipit] # Update the converters to use the user-defined ones uc_update = [] for (j, conv) in user_converters.items(): # If the converter is specified by column names, use the index instead if _is_string_like(j): try: j = names.index(j) i = j except ValueError: continue elif usecols: try: i = usecols.index(j) except ValueError: # Unused converter specified continue else: i = j # Find the value to test - first_line is not filtered by usecols: if len(first_line): testing_value = first_values[j] else: testing_value = None converters[i].update(conv, locked=True, testing_value=testing_value, default=filling_values[i], missing_values=missing_values[i],) uc_update.append((i, conv)) # Make sure we have the corrected keys in user_converters... user_converters.update(uc_update) # Fixme: possible error as following variable never used. #miss_chars = [_.missing_values for _ in converters] # Initialize the output lists ... # ... rows rows = [] append_to_rows = rows.append # ... masks if usemask: masks = [] append_to_masks = masks.append # ... invalid invalid = [] append_to_invalid = invalid.append # Parse each line for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): values = split_line(line) nbvalues = len(values) # Skip an empty line if nbvalues == 0: continue if usecols: # Select only the columns we need try: values = [values[_] for _ in usecols] except IndexError: append_to_invalid((i + skip_header + 1, nbvalues)) continue elif nbvalues != nbcols: append_to_invalid((i + skip_header + 1, nbvalues)) continue # Store the values append_to_rows(tuple(values)) if usemask: append_to_masks(tuple([v.strip() in m for (v, m) in zip(values, missing_values)])) if len(rows) == max_rows: break if own_fhd: fhd.close() # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): current_column = [itemgetter(i)(_m) for _m in rows] try: converter.iterupgrade(current_column) except ConverterLockError: errmsg = "Converter #%i is locked and cannot be upgraded: " % i current_column = map(itemgetter(i), rows) for (j, value) in enumerate(current_column): try: converter.upgrade(value) except (ConverterError, ValueError): errmsg += "(occurred line #%i for value '%s')" errmsg %= (j + 1 + skip_header, value) raise ConverterError(errmsg) # Check that we don't have invalid values nbinvalid = len(invalid) if nbinvalid > 0: nbrows = len(rows) + nbinvalid - skip_footer # Construct the error message template = " Line #%%i (got %%i columns instead of %i)" % nbcols if skip_footer > 0: nbinvalid_skipped = len([_ for _ in invalid if _[0] > nbrows + skip_header]) invalid = invalid[:nbinvalid - nbinvalid_skipped] skip_footer -= nbinvalid_skipped # # nbrows -= skip_footer # errmsg = [template % (i, nb) # for (i, nb) in invalid if i < nbrows] # else: errmsg = [template % (i, nb) for (i, nb) in invalid] if len(errmsg): errmsg.insert(0, "Some errors were detected !") errmsg = "\n".join(errmsg) # Raise an exception ? if invalid_raise: raise ValueError(errmsg) # Issue a warning ? else: warnings.warn(errmsg, ConversionWarning) # Strip the last skip_footer data if skip_footer > 0: rows = rows[:-skip_footer] if usemask: masks = masks[:-skip_footer] # Convert each value according to the converter: # We want to modify the list in place to avoid creating a new one... if loose: rows = list( zip(*[[conv._loose_call(_r) for _r in map(itemgetter(i), rows)] for (i, conv) in enumerate(converters)])) else: rows = list( zip(*[[conv._strict_call(_r) for _r in map(itemgetter(i), rows)] for (i, conv) in enumerate(converters)])) # Reset the dtype data = rows if dtype is None: # Get the dtypes from the types of the converters column_types = [conv.type for conv in converters] # Find the columns with strings... strcolidx = [i for (i, v) in enumerate(column_types) if v in (type('S'), np.string_)] # ... and take the largest number of chars. for i in strcolidx: column_types[i] = "|S%i" % max(len(row[i]) for row in data) # if names is None: # If the dtype is uniform, don't define names, else use '' base = set([c.type for c in converters if c._checked]) if len(base) == 1: (ddtype, mdtype) = (list(base)[0], np.bool) else: ddtype = [(defaultfmt % i, dt) for (i, dt) in enumerate(column_types)] if usemask: mdtype = [(defaultfmt % i, np.bool) for (i, dt) in enumerate(column_types)] else: ddtype = list(zip(names, column_types)) mdtype = list(zip(names, [np.bool] * len(column_types))) output = np.array(data, dtype=ddtype) if usemask: outputmask = np.array(masks, dtype=mdtype) else: # Overwrite the initial dtype names if needed if names and dtype.names: dtype.names = names # Case 1. We have a structured type if len(dtype_flat) > 1: # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] # First, create the array using a flattened dtype: # [('a', int), ('b1', int), ('b2', float)] # Then, view the array using the specified dtype. if 'O' in (_.char for _ in dtype_flat): if has_nested_fields(dtype): raise NotImplementedError( "Nested fields involving objects are not supported...") else: output = np.array(data, dtype=dtype) else: rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) output = rows.view(dtype) # Now, process the rowmasks the same way if usemask: rowmasks = np.array( masks, dtype=np.dtype([('', np.bool) for t in dtype_flat])) # Construct the new dtype mdtype = make_mask_descr(dtype) outputmask = rowmasks.view(mdtype) # Case #2. We have a basic dtype else: # We used some user-defined converters if user_converters: ishomogeneous = True descr = [] for i, ttype in enumerate([conv.type for conv in converters]): # Keep the dtype of the current converter if i in user_converters: ishomogeneous &= (ttype == dtype.type) if ttype == np.string_: ttype = "|S%i" % max(len(row[i]) for row in data) descr.append(('', ttype)) else: descr.append(('', dtype)) # So we changed the dtype ? if not ishomogeneous: # We have more than one field if len(descr) > 1: dtype = np.dtype(descr) # We have only one field: drop the name if not needed. else: dtype = np.dtype(ttype) # output = np.array(data, dtype) if usemask: if dtype.names: mdtype = [(_, np.bool) for _ in dtype.names] else: mdtype = np.bool outputmask = np.array(masks, dtype=mdtype) # Try to take care of the missing data we missed names = output.dtype.names if usemask and names: for (name, conv) in zip(names or (), converters): missing_values = [conv(_) for _ in conv.missing_values if _ != asbytes('')] for mval in missing_values: outputmask[name] |= (output[name] == mval) # Construct the final array if usemask: output = output.view(MaskedArray) output._mask = outputmask if unpack: return output.squeeze().T return output.squeeze() def ndfromtxt(fname, **kwargs): """ Load ASCII data stored in a file and return it as a single array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function. """ kwargs['usemask'] = False return genfromtxt(fname, **kwargs) def mafromtxt(fname, **kwargs): """ Load ASCII data stored in a text file and return a masked array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ kwargs['usemask'] = True return genfromtxt(fname, **kwargs) def recfromtxt(fname, **kwargs): """ Load ASCII data from a file and return it in a record array. If ``usemask=False`` a standard `recarray` is returned, if ``usemask=True`` a MaskedRecords array is returned. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ kwargs.setdefault("dtype", None) usemask = kwargs.get('usemask', False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output def recfromcsv(fname, **kwargs): """ Load ASCII data stored in a comma-separated file. The returned array is a record array (if ``usemask=False``, see `recarray`) or a masked record array (if ``usemask=True``, see `ma.mrecords.MaskedRecords`). Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ # Set default kwargs for genfromtxt as relevant to csv import. kwargs.setdefault("case_sensitive", "lower") kwargs.setdefault("names", True) kwargs.setdefault("delimiter", ",") kwargs.setdefault("dtype", None) output = genfromtxt(fname, **kwargs) usemask = kwargs.get("usemask", False) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output
bsd-3-clause
banesullivan/ParaViewGeophysics
PVGeo/ubc/tensor.py
1
21910
__all__ = [ 'TensorMeshReader', 'TensorMeshAppender', 'TopoMeshAppender', ] __displayname__ = 'Tensor Mesh' import os import sys import numpy as np import pandas as pd import vtk from .. import _helpers, interface from ..base import AlgorithmBase from .two_file_base import ModelAppenderBase, ubcMeshReaderBase if sys.version_info < (3,): from StringIO import StringIO else: from io import StringIO class TensorMeshReader(ubcMeshReaderBase): """UBC Mesh 2D/3D models are defined using a 2-file format. The "mesh" file describes how the data is discretized. The "model" file lists the physical property values for all cells in a mesh. A model file is meaningless without an associated mesh file. The reader will automatically detect if the mesh is 2D or 3D and read the remainder of the data with that dimensionality assumption. If the mesh file is 2D, then then model file must also be in the 2D format (same for 3D). Note: Model File is optional. Reader will still construct ``vtkRectilinearGrid`` safely. """ __displayname__ = 'UBC Tensor Mesh Reader' __category__ = 'reader' description = 'PVGeo: UBC Mesh 2D/3D Two-File Format' def __init__(self, nOutputPorts=1, outputType='vtkRectilinearGrid', **kwargs): ubcMeshReaderBase.__init__( self, nOutputPorts=nOutputPorts, outputType=outputType, **kwargs ) self.__mesh = vtk.vtkRectilinearGrid() self.__models = [] @staticmethod def place_model_on_mesh(mesh, model, data_name='Data'): """Places model data onto a mesh. This is for the UBC Grid data reaers to associate model data with the mesh grid. Args: mesh (vtkRectilinearGrid): The ``vtkRectilinearGrid`` that is the mesh to place the model data upon. model (np.array): A NumPy float array that holds all of the data to place inside of the mesh's cells. data_name (str) : The name of the model data array once placed on the ``vtkRectilinearGrid``. Return: vtkRectilinearGrid : Returns the input ``vtkRectilinearGrid`` with model data appended. """ if isinstance(model, dict): for key in model.keys(): TensorMeshReader.place_model_on_mesh(mesh, model[key], data_name=key) return mesh # model.GetNumberOfValues() if model is vtkDataArray # Make sure this model file fits the dimensions of the mesh ext = mesh.GetExtent() n1, n2, n3 = ext[1], ext[3], ext[5] if n1 * n2 * n3 < len(model): raise _helpers.PVGeoError( 'Model `%s` has more data than the given mesh has cells to hold.' % data_name ) elif n1 * n2 * n3 > len(model): raise _helpers.PVGeoError( 'Model `%s` does not have enough data to fill the given mesh\'s cells.' % data_name ) # Swap axes because VTK structures the coordinates a bit differently # - This is absolutely crucial! # - Do not play with unless you know what you are doing! if model.ndim > 1 and model.ndim < 3: ncomp = model.shape[1] model = np.reshape(model, (n1, n2, n3, ncomp)) model = np.swapaxes(model, 0, 1) model = np.swapaxes(model, 0, 2) # Now reverse Z axis model = model[::-1, :, :, :] # Note it is in Fortran ordering model = np.reshape(model, (n1 * n2 * n3, ncomp)) else: model = np.reshape(model, (n1, n2, n3)) model = np.swapaxes(model, 0, 1) model = np.swapaxes(model, 0, 2) # Now reverse Z axis model = model[::-1, :, :] # Note it is in Fortran ordering model = model.flatten() # Convert data to VTK data structure and append to output c = interface.convert_array(model, name=data_name, deep=True) # THIS IS CELL DATA! Add the model data to CELL data: mesh.GetCellData().AddArray(c) return mesh # ------------------------------------------------------------------# # ---------------------- UBC MESH 2D ------------------------# # ------------------------------------------------------------------# @staticmethod def ubc_mesh_2d(FileName, output): """This method reads a UBC 2D Mesh file and builds an empty ``vtkRectilinearGrid`` for data to be inserted into. `Format Specs`_. .. _Format Specs: http://giftoolscookbook.readthedocs.io/en/latest/content/fileFormats/mesh2Dfile.html Args: FileName (str) : The mesh filename as an absolute path for the input mesh file in UBC 3D Mesh Format. output (vtkRectilinearGrid) : The output data object Return: vtkRectilinearGrid : a ``vtkRectilinearGrid`` generated from the UBC 3D Mesh grid. Mesh is defined by the input mesh file. No data attributes here, simply an empty mesh. Use the ``place_model_on_mesh()`` method to associate with model data. """ # Read in data from file xpts, xdisc, zpts, zdisc = ubcMeshReaderBase._ubc_mesh_2d_part(FileName) nx = np.sum(np.array(xdisc, dtype=int)) + 1 nz = np.sum(np.array(zdisc, dtype=int)) + 1 # Now generate the vtkRectilinear Grid def _genCoords(pts, disc, z=False): c = [float(pts[0])] for i in range(len(pts) - 1): start = float(pts[i]) stop = float(pts[i + 1]) num = int(disc[i]) w = (stop - start) / num for j in range(1, num): c.append(start + (j) * w) c.append(stop) c = np.array(c, dtype=float) if z: c = -c[::-1] return interface.convert_array(c, deep=True) xcoords = _genCoords(xpts, xdisc) zcoords = _genCoords(zpts, zdisc, z=True) ycoords = interface.convert_array(np.zeros(1), deep=True) output.SetDimensions(nx, 2, nz) # note this subtracts 1 output.SetXCoordinates(xcoords) output.SetYCoordinates(ycoords) output.SetZCoordinates(zcoords) return output @staticmethod def ubc_model_2d(FileName): """Reads a 2D model file and returns a 1D NumPy float array. Use the ``place_model_on_mesh()`` method to associate with a grid. Note: Only supports single component data Args: FileName (str) : The model filename as an absolute path for the input model file in UBCMesh Model Format. Also accepts a list of string file names. Return: np.array : a NumPy float array that holds the model data read from the file. Use the ``place_model_on_mesh()`` method to associate with a grid. If a list of file names is given then it will return a dictionary of NumPy float array with keys as the basenames of the files. """ if isinstance(FileName, (list, tuple)): out = {} for f in FileName: out[os.path.basename(f)] = TensorMeshReader.ubc_model_2d(f) return out dim = np.genfromtxt( FileName, dtype=int, delimiter=None, comments='!', max_rows=1 ) names = ['col%d' % i for i in range(dim[0])] df = pd.read_csv( FileName, names=names, delim_whitespace=True, skiprows=1, comment='!' ) data = df.values if np.shape(data)[0] != dim[1] and np.shape(data)[1] != dim[0]: raise _helpers.PVGeoError('Mode file `%s` improperly formatted.' % FileName) return data.flatten(order='F') def __ubc_mesh_data_2d(self, filename_mesh, filename_models, output): """Helper method to read a 2D mesh""" # Construct/read the mesh if self.need_to_readMesh(): TensorMeshReader.ubc_mesh_2d(filename_mesh, self.__mesh) self.need_to_readMesh(flag=False) output.DeepCopy(self.__mesh) if self.need_to_readModels() and self.this_has_models(): self.__models = [] for f in filename_models: # Read the model data self.__models.append(TensorMeshReader.ubc_model_2d(f)) self.need_to_readModels(flag=False) return output # ------------------------------------------------------------------# # ---------------------- UBC MESH 3D ------------------------# # ------------------------------------------------------------------# @staticmethod def ubc_mesh_3d(FileName, output): """This method reads a UBC 3D Mesh file and builds an empty ``vtkRectilinearGrid`` for data to be inserted into. Args: FileName (str) : The mesh filename as an absolute path for the input mesh file in UBC 3D Mesh Format. output (vtkRectilinearGrid) : The output data object Return: vtkRectilinearGrid : a ``vtkRectilinearGrid`` generated from the UBC 3D Mesh grid. Mesh is defined by the input mesh file. No data attributes here, simply an empty mesh. Use the ``place_model_on_mesh()`` method to associate with model data. """ # --- Read in the mesh ---# fileLines = np.genfromtxt(FileName, dtype=str, delimiter='\n', comments='!') # Get mesh dimensions dim = np.array(fileLines[0].split('!')[0].split(), dtype=int) dim = (dim[0] + 1, dim[1] + 1, dim[2] + 1) # The origin corner (Southwest-top) # - Remember UBC format specifies down as the positive Z # - Easting, Northing, Altitude oo = np.array(fileLines[1].split('!')[0].split(), dtype=float) ox, oy, oz = oo[0], oo[1], oo[2] # Read cell sizes for each line in the UBC mesh files def _readCellLine(line): line_list = [] for seg in line.split(): if '*' in seg: sp = seg.split('*') seg_arr = np.ones((int(sp[0]),), dtype=float) * float(sp[1]) else: seg_arr = np.array([float(seg)], dtype=float) line_list.append(seg_arr) return np.concatenate(line_list) # Read the cell sizes cx = _readCellLine(fileLines[2].split('!')[0]) cy = _readCellLine(fileLines[3].split('!')[0]) cz = _readCellLine(fileLines[4].split('!')[0]) # Invert the indexing of the vector to start from the bottom. cz = cz[::-1] # Adjust the reference point to the bottom south west corner oz = oz - np.sum(cz) # Now generate the coordinates for from cell width and origin cox = ox + np.cumsum(cx) cox = np.insert(cox, 0, ox) coy = oy + np.cumsum(cy) coy = np.insert(coy, 0, oy) coz = oz + np.cumsum(cz) coz = np.insert(coz, 0, oz) # Set the dims and coordinates for the output output.SetDimensions(dim[0], dim[1], dim[2]) # Convert to VTK array for setting coordinates output.SetXCoordinates(interface.convert_array(cox, deep=True)) output.SetYCoordinates(interface.convert_array(coy, deep=True)) output.SetZCoordinates(interface.convert_array(coz, deep=True)) return output def __ubc_mesh_data_3d(self, filename_mesh, filename_models, output): """Helper method to read a 3D mesh""" # Construct/read the mesh if self.need_to_readMesh(): TensorMeshReader.ubc_mesh_3d(filename_mesh, self.__mesh) self.need_to_readMesh(flag=False) output.DeepCopy(self.__mesh) if self.need_to_readModels() and self.this_has_models(): self.__models = [] for f in filename_models: # Read the model data self.__models.append(TensorMeshReader.ubc_model_3d(f)) self.need_to_readModels(flag=False) return output def __ubc_tensor_mesh(self, filename_mesh, filename_models, output): """Wrapper to Read UBC GIF 2D and 3D meshes. UBC Mesh 2D/3D models are defined using a 2-file format. The "mesh" file describes how the data is descritized. The "model" file lists the physical property values for all cells in a mesh. A model file is meaningless without an associated mesh file. If the mesh file is 2D, then then model file must also be in the 2D format (same for 3D). Args: filename_mesh (str) : The mesh filename as an absolute path for the input mesh file in UBC 2D/3D Mesh Format filename_models (str or list(str)) : The model filename(s) as an absolute path for the input model file in UBC 2D/3D Model Format. output (vtkRectilinearGrid) : The output data object Return: vtkRectilinearGrid : a ``vtkRectilinearGrid`` generated from the UBC 2D/3D Mesh grid. Mesh is defined by the input mesh file. Cell data is defined by the input model file. """ # Check if the mesh is a UBC 2D mesh if self.is_2d(): self.__ubc_mesh_data_2d(filename_mesh, filename_models, output) # Check if the mesh is a UBC 3D mesh elif self.is_3d(): self.__ubc_mesh_data_3d(filename_mesh, filename_models, output) else: raise _helpers.PVGeoError('File format not recognized') return output def RequestData(self, request, inInfo, outInfo): """Handles data request by the pipeline.""" # Get output: output = self.GetOutputData(outInfo, 0) # Get requested time index i = _helpers.get_requested_time(self, outInfo) self.__ubc_tensor_mesh( self.get_mesh_filename(), self.get_model_filenames(), output ) # Place the model data for given timestep onto the mesh if len(self.__models) > i: TensorMeshReader.place_model_on_mesh( output, self.__models[i], self.get_data_name() ) return 1 def RequestInformation(self, request, inInfo, outInfo): """Handles info request by pipeline about timesteps and grid extents.""" # Call parent to handle time stuff ubcMeshReaderBase.RequestInformation(self, request, inInfo, outInfo) # Now set whole output extent if self.need_to_readMesh(): ext = self._read_extent() info = outInfo.GetInformationObject(0) # Set WHOLE_EXTENT: This is absolutely necessary info.Set(vtk.vtkStreamingDemandDrivenPipeline.WHOLE_EXTENT(), ext, 6) return 1 def clear_mesh(self): """Use to clean/rebuild the mesh""" self.__mesh = vtk.vtkRectilinearGrid() ubcMeshReaderBase.clear_models(self) def clear_models(self): """Use to clean the models and reread""" self.__models = [] ubcMeshReaderBase.clear_models(self) ############################################################################### class TensorMeshAppender(ModelAppenderBase): """This filter reads a timeseries of models and appends it to an input ``vtkRectilinearGrid`` """ __displayname__ = 'UBC Tensor Mesh Appender' __category__ = 'filter' def __init__(self, **kwargs): ModelAppenderBase.__init__( self, inputType='vtkRectilinearGrid', outputType='vtkRectilinearGrid', **kwargs ) def _read_up_front(self): """Internal helepr to read data at start""" reader = ubcMeshReaderBase.ubc_model_3d if not self._is_3D: # Note how in UBC format, 2D grids are specified on an XZ plane (no Y component) # This will only work prior to rotations to account for real spatial reference reader = TensorMeshReader.ubc_model_2d self._models = [] for f in self._model_filenames: # Read the model data self._models.append(reader(f)) self.need_to_read(flag=False) return def _place_on_mesh(self, output, idx=0): """Internal helepr to place a model on the mesh for a given index""" TensorMeshReader.place_model_on_mesh( output, self._models[idx], self.get_data_name() ) return ############################################################################### class TopoMeshAppender(AlgorithmBase): """This filter reads a single discrete topography file and appends it as a boolean data array. """ __displayname__ = 'Append UBC Discrete Topography' __category__ = 'filter' def __init__( self, inputType='vtkRectilinearGrid', outputType='vtkRectilinearGrid', **kwargs ): AlgorithmBase.__init__( self, nInputPorts=1, inputType=inputType, nOutputPorts=1, outputType=outputType, ) self._topoFileName = kwargs.get('filename', None) self.__indices = None self.__need_to_read = True self.__ne, self.__nn = None, None def need_to_read(self, flag=None): """Ask self if the reader needs to read the files again Args: flag (bool): if the flag is set then this method will set the read status Return: bool: The status of the reader aspect of the filter. """ if flag is not None and isinstance(flag, (bool, int)): self.__need_to_read = flag return self.__need_to_read def Modified(self, read_again=True): """Call modified if the files needs to be read again again.""" if read_again: self.__need_to_read = read_again AlgorithmBase.Modified(self) def modified(self, read_again=True): """Call modified if the files needs to be read again again.""" return self.Modified(read_again=read_again) def _read_up_front(self): """Internal helepr to read data at start""" # Read the file content = np.genfromtxt( self._topoFileName, dtype=str, delimiter='\n', comments='!' ) dim = content[0].split() self.__ne, self.__nn = int(dim[0]), int(dim[1]) self.__indices = pd.read_csv( StringIO("\n".join(content[1::])), names=['i', 'j', 'k'], delim_whitespace=True, ) # NOTE: K indices are inverted self.need_to_read(flag=False) return def _place_on_mesh(self, output): """Internal helepr to place an active cells model on the mesh""" # Check mesh extents to math topography nx, ny, nz = output.GetDimensions() nx, ny, nz = nx - 1, ny - 1, nz - 1 # because GetDimensions counts the nodes topz = np.max(self.__indices['k']) + 1 if nx != self.__nn or ny != self.__ne or topz > nz: raise _helpers.PVGeoError( 'Dimension mismatch between input grid and topo file.' ) # # Adjust the k indices to be in caarteian system # self.__indices['k'] = nz - self.__indices['k'] # Fill out the topo and add it as model as it will be in UBC format # Create a 3D array of 1s and zeros (1 means beneath topo or active) topo = np.empty((ny, nx, nz), dtype=float) topo[:] = np.nan for row in self.__indices.values: i, j, k = row topo[i, j, k + 1 :] = 0 topo[i, j, : k + 1] = 1 # Add as model... ``place_model_on_mesh`` handles the rest TensorMeshReader.place_model_on_mesh( output, topo.flatten(), 'Active Topography' ) return def RequestData(self, request, inInfo, outInfo): """Used by pipeline to generate output""" # Get input/output of Proxy pdi = self.GetInputData(inInfo, 0, 0) output = self.GetOutputData(outInfo, 0) output.DeepCopy(pdi) # ShallowCopy if you want changes to propagate upstream # Perfrom task: if self.__need_to_read: self._read_up_front() # Place the model data for given timestep onto the mesh self._place_on_mesh(output) return 1 #### Setters and Getters #### def clear_topo_file(self): """Use to clear data file name.""" self._topoFileName = None self.Modified(read_again=True) def set_topo_filename(self, filename): """Use to set the file names for the reader. Handles single strings only""" if filename is None: return # do nothing if None is passed by a constructor on accident elif isinstance(filename, str) and self._topoFileName != filename: self._topoFileName = filename self.Modified() return 1 ############################################################################### # # import numpy as np # indices = np.array([[0,0,1], # [0,1,1], # [0,2,1], # [1,0,1], # [1,1,1], # [1,2,1], # [2,0,1], # [2,1,1], # [2,2,1], # ]) # # topo = np.empty((3,3,3), dtype=float) # topo[:] = np.nan # # for row in indices: # i, j, k = row # topo[i, j, k:] = 0 # topo[i, j, :k] = 1 # topo
bsd-3-clause
Healthcast/RSV
python/all_year_predict/methods.py
2
3879
#!/usr/bin/pyhton import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, neighbors, linear_model from sklearn import svm from sklearn import metrics from sklearn.cross_validation import train_test_split from sklearn.ensemble import RandomForestClassifier def apply_algorithm(paras, X, y): if paras['clf'] == 'svm': clf = svm.SVC(kernel=paras['svm'][1], C=paras['svm'][0], probability=True) elif paras['clf'] == 'knn': clf = neighbors.KNeighborsClassifier(paras['knn'][0],\ weights=paras['knn'][1]) elif paras['clf'] == 'rf': clf = RandomForestClassifier(max_depth=paras['rf'][0], \ n_estimators=paras['rf'][1],\ max_features=paras['rf'][2]) else: print str("unknown classifier") sys.exit(2) return clf def apply_evaluation(paras, X, y, clf, data): X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3, \ random_state=0) clf.fit(X_train, y_train) r = clf.predict(X_test) d = clf.decision_function(X) p = clf.predict_proba(X).T[1]*3 h = data["hospital"].T[data["city"].index(paras["city"])] h1 = h.astype(float) m = max(h1) h1=h1/m*4 plt.figure() # plt.plot(d) plt.plot(y) plt.plot(h1) plt.plot(p) # height = 4 # bottom = -2 # ss = data["season_start"] # date=data["date1"] # c_id = data["city"].index(paras["city"]) # ylabel = data["ylabels"] # for m in ss: # plt.plot([m, m],[bottom, height], 'y--', linewidth=1) # # for m in range(1, len(ss)-1): # a = ss[m] # plt.text(a-5,height, date[a].split('-')[0]) # # #plot the start week # up=1 # for j in range(len(ylabel.T[c_id])-1): # if ylabel.T[c_id,j] == 1 : # plt.plot([j, j],[bottom, height], 'k-', linewidth=2) # if up==1: # plt.text(j-10, height-1, date[j]) # up=0 # else: # plt.text(j-10, height-2, date[j]) # up=1 # plt.show() #plot the results # x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1 # y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1 # # xx, yy = np.meshgrid(np.arange(x_min, x_max, 1), np.arange(y_min, y_max, 1)) # Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Z = Z.reshape(xx.shape) # # plt.figure() # plt.pcolormesh(xx, yy, Z) # plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train) # plt.xlim(xx.min(), xx.max()) # plt.ylim(yy.min(), yy.max()) # plt.title("binary classification classification") # plt.show() # if paras['eva'] == 'accuracy': print "The accuracy:" print metrics.accuracy_score(y_test, r) elif paras['eva'] == 'precision': print "The precision:" print metrics.precision_score(y_test, r) elif paras['eva'] == 'recall': print "The recall:" print metrics.recall_score(y_test, r) elif paras['eva'] == 'confusion': print "The confusion matrix:" print metrics.confusion_matrix(y_test, r) elif paras['eva'] == 'report': print "The report:" print metrics.classification_report(y_test, r) elif paras['eva'] == 'roc' and paras['clf'] == 'svm': scores = clf.decision_function(X_test) print "The auc:" fpr, tpr, thresholds = metrics.roc_curve(y_test, scores) roc_auc = metrics.auc(fpr, tpr) print str(roc_auc) plt.figure() plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc) plt.plot([0, 1], [0, 1], 'k--') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.show()
gpl-2.0
CIFASIS/pylearn2
pylearn2/packaged_dependencies/theano_linear/unshared_conv/localdot.py
39
5044
""" WRITEME """ import logging from ..linear import LinearTransform from .unshared_conv import FilterActs, ImgActs from theano.compat.six.moves import xrange from theano.sandbox import cuda if cuda.cuda_available: import gpu_unshared_conv # register optimizations import numpy as np import warnings try: import matplotlib.pyplot as plt except (RuntimeError, ImportError, TypeError) as matplotlib_exception: warnings.warn("Unable to import matplotlib. Some features unavailable. " "Original exception: " + str(matplotlib_exception)) logger = logging.getLogger(__name__) class LocalDot(LinearTransform): """ LocalDot is an linear operation computationally similar to convolution in the spatial domain, except that whereas convolution applying a single filter or set of filters across an image, the LocalDot has different filterbanks for different points in the image. Mathematically, this is a general linear transform except for a restriction that filters are 0 outside of a spatially localized patch within the image. Image shape is 5-tuple: color_groups colors_per_group rows cols images Filterbank shape is 7-tuple (!) 0 row_positions 1 col_positions 2 colors_per_group 3 height 4 width 5 color_groups 6 filters_per_group The result of left-multiplication a 5-tuple with shape: filter_groups filters_per_group row_positions col_positions images Parameters ---------- filters : WRITEME irows : WRITEME Image rows icols : WRITEME Image columns subsample : WRITEME padding_start : WRITEME filters_shape : WRITEME message : WRITEME """ def __init__(self, filters, irows, icols=None, subsample=(1, 1), padding_start=None, filters_shape=None, message=""): LinearTransform.__init__(self, [filters]) self._filters = filters if filters_shape is None: self._filters_shape = tuple(filters.get_value(borrow=True).shape) else: self._filters_shape = tuple(filters_shape) self._irows = irows if icols is None: self._icols = irows else: self._icols = icols if self._icols != self._irows: raise NotImplementedError('GPU code at least needs square imgs') self._subsample = tuple(subsample) self._padding_start = padding_start if len(self._filters_shape) != 7: raise TypeError('need 7-tuple filter shape', self._filters_shape) if self._subsample[0] != self._subsample[1]: raise ValueError('subsampling must be same in rows and cols') self._filter_acts = FilterActs(self._subsample[0]) self._img_acts = ImgActs(module_stride=self._subsample[0]) if message: self._message = message else: self._message = filters.name def rmul(self, x): """ .. todo:: WRITEME """ assert x.ndim == 5 return self._filter_acts(x, self._filters) def rmul_T(self, x): """ .. todo:: WRITEME """ return self._img_acts(self._filters, x, self._irows, self._icols) def col_shape(self): """ .. todo:: WRITEME """ ishape = self.row_shape() + (-99,) fshape = self._filters_shape hshape, = self._filter_acts.infer_shape(None, (ishape, fshape)) assert hshape[-1] == -99 return hshape[:-1] def row_shape(self): """ .. todo:: WRITEME """ fshape = self._filters_shape fmodulesR, fmodulesC, fcolors, frows, fcols = fshape[:-2] fgroups, filters_per_group = fshape[-2:] return fgroups, fcolors, self._irows, self._icols def print_status(self): """ .. todo:: WRITEME """ raise NotImplementedError("TODO: fix dependence on non-existent " "ndarray_status function") """print ndarray_status( self._filters.get_value(borrow=True), msg='%s{%s}'% (self.__class__.__name__, self._message)) """ def imshow_gray(self): """ .. todo:: WRITEME """ filters = self._filters.get_value() modR, modC, colors, rows, cols, grps, fs_per_grp = filters.shape logger.info(filters.shape) rval = np.zeros(( modR * (rows + 1) - 1, modC * (cols + 1) - 1, )) for rr, modr in enumerate(xrange(0, rval.shape[0], rows + 1)): for cc, modc in enumerate(xrange(0, rval.shape[1], cols + 1)): rval[modr:modr + rows, modc:modc + cols] = filters[rr, cc, 0, :, :, 0, 0] plt.imshow(rval, cmap='gray') return rval
bsd-3-clause
kcompher/thunder
thunder/extraction/source.py
6
31847
from numpy import asarray, mean, sqrt, ndarray, amin, amax, concatenate, sum, zeros, maximum, \ argmin, newaxis, ones, delete, NaN, inf, isnan, clip, logical_or, unique, where, all from thunder.utils.serializable import Serializable from thunder.utils.common import checkParams, aslist from thunder.rdds.images import Images from thunder.rdds.series import Series class Source(Serializable, object): """ A single source, represented as a list of coordinates and other optional specifications. A source also has a set of lazily computed attributes useful for representing and comparing its geometry, such as center, bounding box, and bounding polygon. These properties will be computed lazily and made available as attributes when requested. Parameters ---------- coordinates : array-like List of 2D or 3D coordinates, can be a list of lists or array of shape (n,2) or (n,3) values : list or array-like Value (or weight) associated with each coordiante id : int or string Arbitrary specification per source, typically an index or string label Attributes ---------- center : list or array-like The coordinates of the center of the source polygon : list or array-like The coordinates of a polygon bounding the region (a convex hull) bbox : list or array-like Boundaries of the source (with the lowest values for all axes followed by the highest values) area : scalar The area of the region """ from zope.cachedescriptors import property def __init__(self, coordinates, values=None, id=None): self.coordinates = asarray(coordinates) if self.coordinates.ndim == 1 and len(self.coordinates) > 0: self.coordinates = asarray([self.coordinates]) if values is not None: self.values = asarray(values) if self.values.ndim == 0: self.values = asarray([self.values]) if not (len(self.coordinates) == len(self.values)): raise ValueError("Lengths of coordinates %g and values %g do not match" % (len(self.coordinates), len(self.values))) if id is not None: self.id = id @property.Lazy def center(self): """ Find the region center using a mean. """ # TODO Add option to use weights return mean(self.coordinates, axis=0) @property.Lazy def polygon(self): """ Find the bounding polygon as a convex hull """ # TODO Add option for simplification from scipy.spatial import ConvexHull if len(self.coordinates) >= 4: inds = ConvexHull(self.coordinates).vertices return self.coordinates[inds] else: return self.coordinates @property.Lazy def bbox(self): """ Find the bounding box. """ mn = amin(self.coordinates, axis=0) mx = amax(self.coordinates, axis=0) return concatenate((mn, mx)) @property.Lazy def area(self): """ Find the region area. """ return len(self.coordinates) def restore(self, skip=None): """ Remove all lazy properties, will force recomputation """ if skip is None: skip = [] elif isinstance(skip, str): skip = [skip] for prop in LAZY_ATTRIBUTES: if prop in self.__dict__.keys() and prop not in skip: del self.__dict__[prop] return self def distance(self, other, method='euclidean'): """ Distance between the center of this source and another. Parameters ---------- other : Source, or array-like Either another source, or the center coordinates of another source method : str Specify a distance measure to used for spatial distance between source centers. Current options include Euclidean distance ('euclidean') and L1-norm ('l1'). """ from numpy.linalg import norm checkParams(method, ['euclidean', 'l1']) if method == 'l1': order = 1 else: order = 2 if isinstance(other, Source): return norm(self.center - other.center, ord=order) elif isinstance(other, list) or isinstance(other, ndarray): return norm(self.center - asarray(other), ord=order) def overlap(self, other, method='fraction'): """ Compute the overlap between this source and other. Options are a symmetric measure of overlap based on the fraction of intersecting pixels relative to the union ('fraction'), an assymmetric measure of overlap that expresses detected intersecting pixels (relative to this source) using precision and recall rates ('rates'), or a correlation coefficient of the weights within the intersection (not defined for binary weights) ('correlation') Parameters ---------- other : Source The source to compute overlap with. method : str Which estimate of overlap to compute, options are 'fraction' (symmetric) 'rates' (asymmetric) or 'correlation' """ checkParams(method, ['fraction', 'rates', 'correlation']) coordsSelf = aslist(self.coordinates) coordsOther = aslist(other.coordinates) intersection = [a for a in coordsSelf if a in coordsOther] nhit = float(len(intersection)) ntotal = float(len(set([tuple(x) for x in coordsSelf] + [tuple(x) for x in coordsOther]))) if method == 'rates': recall = nhit / len(coordsSelf) precision = nhit / len(coordsOther) return recall, precision if method == 'fraction': return nhit / float(ntotal) if method == 'correlation': from scipy.stats import spearmanr if not (hasattr(self, 'values') and hasattr(other, 'values')): raise ValueError('Sources must have values to compute correlation') else: valuesSelf = aslist(self.values) valuesOther = aslist(other.values) if len(intersection) > 0: left = [v for v, c in zip(valuesSelf, coordsSelf) if c in coordsOther] right = [v for v, c in zip(valuesOther, coordsOther) if c in coordsSelf] rho, _ = spearmanr(left, right) else: rho = 0.0 return rho def merge(self, other): """ Combine this source with other """ self.coordinates = concatenate((self.coordinates, other.coordinates)) if hasattr(self, 'values'): self.values = concatenate((self.values, other.values)) return self def tolist(self): """ Convert array-like attributes to list """ import copy new = copy.copy(self) for prop in ["coordinates", "values", "center", "bbox", "polygon"]: if prop in self.__dict__.keys(): val = new.__getattribute__(prop) if val is not None and not isinstance(val, list): setattr(new, prop, val.tolist()) return new def toarray(self): """ Convert array-like attributes to ndarray """ import copy new = copy.copy(self) for prop in ["coordinates", "values", "center", "bbox", "polygon"]: if prop in self.__dict__.keys(): val = new.__getattribute__(prop) if val is not None and not isinstance(val, ndarray): setattr(new, prop, asarray(val)) return new def crop(self, minBound, maxBound): """ Crop a source by removing coordinates outside bounds. Follows normal slice indexing conventions. Parameters ---------- minBound : tuple Minimum or starting bounds for each axis maxBound : tuple Maximum or ending bounds for each axis """ coords = self.coordinates newid = self.id if hasattr(self, 'id') else None if hasattr(self, 'values') and self.values is not None: values = self.values inside = [(c, v) for c, v in zip(coords, values) if c not in coords] newcoords, newvalues = zip(*inside) return Source(coordinates=newcoords, values=newvalues, id=newid) else: newcoords = [c for c in coords if all(c >= minBound) and all(c < maxBound)] return Source(coordinates=newcoords, id=newid) def dilate(self, size): """ Dilate a source using morphological operators. Parameters ---------- size : int Size of dilation in pixels """ if size == 0: newcoords = self.coordinates else: size = (size * 2) + 1 if hasattr(self, 'values') and self.values is not None: raise AttributeError('Cannot dilate sources with values') from skimage.morphology import binary_dilation coords = self.coordinates extent = self.bbox[len(self.center):] - self.bbox[0:len(self.center)] + 1 + size * 2 m = zeros(extent) coords = (coords - self.bbox[0:len(self.center)] + size) m[coords.T.tolist()] = 1 m = binary_dilation(m, ones((size, size))) newcoords = asarray(where(m)).T + self.bbox[0:len(self.center)] - size newcoords = [c for c in newcoords if all(c >= 0)] newid = self.id if hasattr(self, 'id') else None return Source(coordinates=newcoords, id=newid) def exclude(self, other): """ Remove coordinates derived from another Source or an array. If other is an array, will remove coordinates of all non-zero elements from this source. If other is a source, will remove any matching coordinates. Parameters ---------- other : ndarray or Source Source to remove """ if isinstance(other, ndarray): coordsOther = asarray(where(other)).T else: coordsOther = aslist(other.coordinates) coordsSelf = aslist(self.coordinates) newid = self.id if hasattr(self, 'id') else None if hasattr(self, 'values') and self.values is not None: valuesSelf = self.values complement = [(c, v) for c, v in zip(coordsSelf, valuesSelf) if c not in coordsOther] newcoords, newvalues = zip(*complement) return Source(coordinates=newcoords, values=newvalues, id=newid) else: complement = [a for a in coordsSelf if a not in coordsOther] return Source(coordinates=complement, id=newid) def outline(self, inner, outer): """ Compute source outline by differencing two dilations Parameters ---------- inner : int Size of inner outline boundary (in pixels) outer : int Size of outer outline boundary (in pixels) """ return self.dilate(outer).exclude(self.dilate(inner)) def transform(self, data, collect=True): """ Extract series from data using a list of sources. Currently only supports averaging over coordinates. Params ------ data : Images or Series object The data from which to extract collect : boolean, optional, default = True Whether to collect to local array or keep as a Series """ if not (isinstance(data, Images) or isinstance(data, Series)): raise Exception("Input must either be Images or Series (or a subclass)") # TODO add support for weighting if isinstance(data, Images): output = data.meanByRegions([self.coordinates]).toSeries() else: output = data.meanOfRegion(self.coordinates) if collect: return output.collectValuesAsArray() else: return output def mask(self, dims=None, binary=True, outline=False, color=None): """ Construct a mask from a source, either locally or within a larger image. Parameters ---------- dims : list or tuple, optional, default = None Dimensions of large image in which to draw mask. If none, will restrict to the bounding box of the region. binary : boolean, optional, deafult = True Whether to incoporate values or only show a binary mask outline : boolean, optional, deafult = False Whether to only show outlines (derived using binary dilation) color : str or array-like RGB triplet (from 0 to 1) or named color (e.g. 'red', 'blue') """ from thunder import Colorize coords = self.coordinates if dims is None: extent = self.bbox[len(self.center):] - self.bbox[0:len(self.center)] + 1 m = zeros(extent) coords = (coords - self.bbox[0:len(self.center)]) else: m = zeros(dims) if hasattr(self, 'values') and self.values is not None and binary is False: m[coords.T.tolist()] = self.values else: m[coords.T.tolist()] = 1 if outline: from skimage.morphology import binary_dilation m = binary_dilation(m, ones((3, 3))) - m if color is not None: m = Colorize(cmap='indexed', colors=[color]).transform([m]) return m def inbounds(self, minBound, maxBound): """ Check what fraction of coordinates are inside given bounds Parameters ---------- minBound : list or tuple Minimum bounds maxBounds : list or tuple Maximum bounds """ minCheck = sum(self.coordinates < minBound, axis=1) > 0 maxCheck = sum(self.coordinates > maxBound, axis=1) > 0 fraction = 1 - sum(logical_or(minCheck, maxCheck)) / float(len(self.coordinates)) return fraction @staticmethod def fromMask(mask, id=None): """ Genearte a source from a mask. Assumes that the mask is an image where all non-zero elements are part of the source. If all non-zero elements are 1, then values will be ignored as the source is assumed to be binary. Parameters ---------- mask : array-like An array (typically 2D or 3D) containing the image mask id : int or string Arbitrary identifier for the source, typically an int or string """ mask = asarray(mask) u = unique(mask) if len(u) == 2 and u[0] == 0 and u[1] == 1: inds = where(mask) return Source(coordinates=asarray(zip(*inds)), id=id) else: inds = where(mask) values = mask[inds] coords = asarray(zip(*inds)) return Source(coordinates=coords, values=values, id=id) @staticmethod def fromCoordinates(coordinates, values=None, id=None): """ Generate a source from a list of coordinates and values. Parameters ---------- coordinates : array-like List coordinates as a list of lists or array of shape (n,2) or (n,3) values : list or array-like Value (or weight) associated with each coordiante id : int or string Arbitrary specification per source, typically an index or string label """ return Source(coordinates, values, id) def __repr__(self): s = self.__class__.__name__ for opt in ["id", "center", "bbox"]: if hasattr(self, opt): o = self.__getattribute__(opt) os = o.tolist() if isinstance(o, ndarray) else o s += '\n%s: %s' % (opt, repr(os)) return s class SourceModel(Serializable, object): """ A source model as a collection of extracted sources. Parameters ---------- sources : list or Sources or a single Source The identified sources See also -------- Source """ def __init__(self, sources): if isinstance(sources, Source): self.sources = [sources] elif isinstance(sources, list) and isinstance(sources[0], Source): self.sources = sources elif isinstance(sources, list): self.sources = [] for ss in sources: self.sources.append(Source(ss)) else: raise Exception("Input type not recognized, must be Source, list of Sources, " "or list of coordinates, got %s" % type(sources)) def __getitem__(self, entry): if not isinstance(entry, int): raise IndexError("Selection not recognized, must be Int, got %s" % type(entry)) return self.sources[entry] def combiner(self, prop, tolist=True): combined = [] for s in self.sources: p = getattr(s, prop) if tolist: p = p.tolist() combined.append(p) return combined @property def coordinates(self): """ List of coordinates combined across sources """ return self.combiner('coordinates') @property def values(self): """ List of coordinates combined across sources """ return self.combiner('values') @property def centers(self): """ Array of centers combined across sources """ return asarray(self.combiner('center')) @property def polygons(self): """ List of polygons combined across sources """ return self.combiner('polygon') @property def areas(self): """ List of areas combined across sources """ return self.combiner('area', tolist=False) @property def count(self): """ Number of sources """ return len(self.sources) def masks(self, dims=None, binary=True, outline=False, base=None, color=None, inds=None): """ Composite masks combined across sources as an image. Parameters ---------- dims : list or tuple, optional, default = None Dimensions of image in which to create masks, must either provide these or provide a base image binary : boolean, optional, deafult = True Whether to incoporate values or only show a binary mask outline : boolean, optional, deafult = False Whether to only show outlines (derived using binary dilation) base : SourceModel or array-like, optional, deafult = None Base background image on which to put masks, or another set of sources (usually for comparisons). color : str, optional, deafult = None Color to assign regions, will assign randomly if 'random' inds : array-like, optional, deafult = None List of indices if only showing a subset """ from thunder import Colorize from matplotlib.cm import get_cmap if inds is None: inds = range(0, self.count) if dims is None and base is None: raise Exception("Must provide image dimensions for composite masks " "or provide a base image.") if base is not None and isinstance(base, SourceModel): outline = True if dims is None and base is not None: dims = asarray(base).shape if isinstance(base, SourceModel): base = base.masks(dims, color='silver') elif isinstance(base, ndarray): base = Colorize(cmap='indexed', colors=['white']).transform([base]) if base is not None and color is None: color = 'deeppink' if color == 'random': combined = zeros(list(dims) + [3]) ncolors = min(self.count, 20) colors = get_cmap('rainbow', ncolors)(range(0, ncolors, 1))[:, 0:3] for i in inds: combined = maximum(self.sources[i].mask(dims, binary, outline, colors[i % len(colors)]), combined) else: combined = zeros(dims) for i in inds: combined = maximum(self.sources[i].mask(dims, binary, outline), combined) if color is not None and color != 'random': combined = Colorize(cmap='indexed', colors=[color]).transform([combined]) if base is not None: combined = maximum(base, combined) return combined def match(self, other, unique=False, minDistance=inf): """ For each source in self, find the index of the closest source in other. Uses euclidean distances between centers to determine distances. Can select nearest matches with or without enforcing uniqueness; if unique is False, will return the closest source in other for each source in self, possibly repeating sources multiple times if unique is True, will only allow each source in other to be matched with a single source in self, as determined by a greedy selection procedure. The minDistance parameter can be used to prevent far-away sources from being chosen during greedy selection. Params ------ other : SourceModel The source model to match sources to unique : boolean, optional, deafult = True Whether to only return unique matches minDistance : scalar, optiona, default = inf Minimum distance to use when selecting matches """ from scipy.spatial.distance import cdist targets = other.centers targetInds = range(0, len(targets)) matches = [] for s in self.sources: update = 1 # skip if no targets left, otherwise update if len(targets) == 0: update = 0 else: dists = cdist(targets, s.center[newaxis]) if dists.min() < minDistance: ind = argmin(dists) else: update = 0 # apply updates, otherwise add a nan if update == 1: matches.append(targetInds[ind]) if unique is True: targets = delete(targets, ind, axis=0) targetInds = delete(targetInds, ind) else: matches.append(NaN) return matches def distance(self, other, minDistance=inf): """ Compute the distance between each source in self and other. First estimates a matching source from other for each source in self, then computes the distance between the two sources. The matches are unique, using a greedy procedure, and minDistance can be used to prevent outliers during matching. Parameters ---------- other : SourceModel The sources to compute distances to minDistance : scalar, optiona, default = inf Minimum distance to use when matching indices """ inds = self.match(other, unique=True, minDistance=minDistance) d = [] for jj, ii in enumerate(inds): if ii is not NaN: d.append(self[jj].distance(other[ii])) else: d.append(NaN) return asarray(d) def overlap(self, other, method='fraction', minDistance=inf): """ Estimate overlap between sources in self and other. Will compute the similarity of sources in self that are found in other, based on either source pixel overlap or correlation. Parameters ---------- other : SourceModel The sources to compare to method : str, optional, default = 'fraction" Method to use when computing overlap between sources ('fraction', 'rates', or 'correlation') minDistance : scalar, optional, default = inf Minimum distance to use when matching indices """ inds = self.match(other, unique=True, minDistance=minDistance) d = [] for jj, ii in enumerate(inds): if ii is not NaN: d.append(self[jj].overlap(other[ii], method=method)) else: if method == 'rates': d.append((NaN, NaN)) else: d.append(NaN) return asarray(d) def similarity(self, other, metric='distance', thresh=5, minDistance=inf): """ Estimate similarity to another set of sources using recall and precision. Will compute the number of sources in self that are also in other, based on a given distance metric and a threshold. The recall rate is the number of matches divided by the number in self, and the precision rate is the number of matches divided by the number in other. Typically self is ground truth and other is an estimate. The F score is defined as 2 * (recall * precision) / (recall + precision) Before computing metrics, all sources in self are matched to other, and a minimum distance can be set to control matching. Parameters ---------- other : SourceModel The sources to compare to. metric : str, optional, default = 'distance' Metric to use when computing distances, options include 'distance' and 'overlap' thresh : scalar, optional, default = 5 The distance below which a source is considered found. minDistance : scalar, optional, default = inf Minimum distance to use when matching indices. """ checkParams(metric, ['distance', 'overlap']) if metric == 'distance': # when evaluating distances, # minimum distance should be the threshold if minDistance == inf: minDistance = thresh vals = self.distance(other, minDistance=minDistance) vals[isnan(vals)] = inf compare = lambda x: x < thresh elif metric == 'overlap': vals = self.overlap(other, method='fraction', minDistance=minDistance) vals[isnan(vals)] = 0 compare = lambda x: x > thresh else: raise Exception("Metric not recognized") recall = sum(map(compare, vals)) / float(self.count) precision = sum(map(compare, vals)) / float(other.count) score = 2 * (recall * precision) / (recall + precision) return recall, precision, score def transform(self, data, collect=True): """ Extract series from data using a list of sources. Currently only supports simple averaging over coordinates. Params ------ data : Images or Series object The data from which to extract signals collect : boolean, optional, default = True Whether to collect to local array or keep as a Series """ if not (isinstance(data, Images) or isinstance(data, Series)): raise Exception("Input must either be Images or Series (or a subclass)") # TODO add support for weighting if isinstance(data, Images): output = data.meanByRegions(self.coordinates).toSeries() else: output = data.meanByRegions(self.coordinates) if collect: return output.collectValuesAsArray() else: return output def clean(self, cleaners=None): """ Apply one or more cleaners to sources, returning filtered sources Parameters ---------- cleaners : Cleaner or list of Cleaners, optional, default = None Which cleaners to apply, if None, will apply BasicCleaner with defaults """ from thunder.extraction.cleaners import Cleaner, BasicCleaner from copy import copy if isinstance(cleaners, list): for c in cleaners: if not isinstance(c, Cleaner): raise Exception("List must only contain Cleaners") elif isinstance(cleaners, Cleaner): cleaners = [cleaners] elif cleaners is None: cleaners = [BasicCleaner()] else: raise Exception("Must provide Cleaner or list of Cleaners, got %s" % type(cleaners)) newmodel = copy(self) for c in cleaners: newmodel = c.clean(newmodel) return newmodel def dilate(self, size): """ Dilate all sources using morphological operators Parameters ---------- size : int Size of dilation in pixels """ return SourceModel([s.dilate(size) for s in self.sources]) def outline(self, inner, outer): """ Outline all sources inner : int Size of inner outline boundary (in pixels) outer : int Size of outer outline boundary (in pixels) """ return SourceModel([s.outline(inner, outer) for s in self.sources]) def crop(self, minBound, maxBound): """ Crop all sources by removing coordinates outside of bounds Parameters ---------- minBound : tuple Minimum or starting bounds for each axis maxBound : tuple Maximum or ending bounds for each axis """ return SourceModel([s.crop(minBound, maxBound) for s in self.sources]) def save(self, f, include=None, overwrite=False, **kwargs): """ Custom save to file with simplified, human-readable output, and selection of lazy attributes. """ import copy output = copy.deepcopy(self) if isinstance(include, str): include = [include] if include is not None: for prop in include: map(lambda s: getattr(s, prop), output.sources) output.sources = map(lambda s: s.restore(include).tolist(), output.sources) simplify = lambda d: d['sources']['py/homogeneousList']['data'] super(SourceModel, output).save(f, simplify=simplify, overwrite=overwrite, **kwargs) @classmethod def load(cls, f, **kwargs): """ Custom load from file to handle simplified, human-readable output """ unsimplify = lambda d: {'sources': { 'py/homogeneousList': {'data': d, 'module': 'thunder.extraction.source', 'type': 'Source'}}} output = super(SourceModel, cls).load(f, unsimplify=unsimplify) output.sources = map(lambda s: s.toarray(), output.sources) return output @classmethod def deserialize(cls, d, **kwargs): """ Custom load from JSON to handle simplified, human-readable output """ unsimplify = lambda d: {'sources': { 'py/homogeneousList': {'data': d, 'module': 'thunder.extraction.source', 'type': 'Source'}}} output = super(SourceModel, cls).deserialize(d, unsimplify=unsimplify) output.sources = map(lambda s: s.toarray(), output.sources) return output def __repr__(self): s = self.__class__.__name__ s += '\n%g sources' % (len(self.sources)) return s LAZY_ATTRIBUTES = ["center", "polygon", "bbox", "area"]
apache-2.0
danviv/trading-with-python
cookbook/reconstructVXX/reconstructVXX.py
77
3574
# -*- coding: utf-8 -*- """ Reconstructing VXX from futures data author: Jev Kuznetsov License : BSD """ from __future__ import division from pandas import * import numpy as np import os class Future(object): """ vix future class, used to keep data structures simple """ def __init__(self,series,code=None): """ code is optional, example '2010_01' """ self.series = series.dropna() # price data self.settleDate = self.series.index[-1] self.dt = len(self.series) # roll period (this is default, should be recalculated) self.code = code # string code 'YYYY_MM' def monthNr(self): """ get month nr from the future code """ return int(self.code.split('_')[1]) def dr(self,date): """ days remaining before settlement, on a given date """ return(sum(self.series.index>date)) def price(self,date): """ price on a date """ return self.series.get_value(date) def returns(df): """ daily return """ return (df/df.shift(1)-1) def recounstructVXX(): """ calculate VXX returns needs a previously preprocessed file vix_futures.csv """ dataDir = os.path.expanduser('~')+'/twpData' X = DataFrame.from_csv(dataDir+'/vix_futures.csv') # raw data table # build end dates list & futures classes futures = [] codes = X.columns endDates = [] for code in codes: f = Future(X[code],code=code) print code,':', f.settleDate endDates.append(f.settleDate) futures.append(f) endDates = np.array(endDates) # set roll period of each future for i in range(1,len(futures)): futures[i].dt = futures[i].dr(futures[i-1].settleDate) # Y is the result table idx = X.index Y = DataFrame(index=idx, columns=['first','second','days_left','w1','w2', 'ret','30days_avg']) # W is the weight matrix W = DataFrame(data = np.zeros(X.values.shape),index=idx,columns = X.columns) # for VXX calculation see http://www.ipathetn.com/static/pdf/vix-prospectus.pdf # page PS-20 for date in idx: i =np.nonzero(endDates>=date)[0][0] # find first not exprired future first = futures[i] # first month futures class second = futures[i+1] # second month futures class dr = first.dr(date) # number of remaining dates in the first futures contract dt = first.dt #number of business days in roll period W.set_value(date,codes[i],100*dr/dt) W.set_value(date,codes[i+1],100*(dt-dr)/dt) # this is all just debug info p1 = first.price(date) p2 = second.price(date) w1 = 100*dr/dt w2 = 100*(dt-dr)/dt Y.set_value(date,'first',p1) Y.set_value(date,'second',p2) Y.set_value(date,'days_left',first.dr(date)) Y.set_value(date,'w1',w1) Y.set_value(date,'w2',w2) Y.set_value(date,'30days_avg',(p1*w1+p2*w2)/100) valCurr = (X*W.shift(1)).sum(axis=1) # value on day N valYest = (X.shift(1)*W.shift(1)).sum(axis=1) # value on day N-1 Y['ret'] = valCurr/valYest-1 # index return on day N return Y ##-------------------Main script--------------------------- if __name__=="__main__": Y = recounstructVXX() print Y.head(30)# Y.to_csv('reconstructedVXX.csv')
bsd-3-clause
einarhuseby/arctic
arctic/_util.py
3
1846
from pandas import DataFrame from pandas.util.testing import assert_frame_equal from pymongo.errors import OperationFailure import string import logging logger = logging.getLogger(__name__) def indent(s, num_spaces): s = string.split(s, '\n') s = [(num_spaces * ' ') + line for line in s] s = string.join(s, '\n') return s def are_equals(o1, o2, **kwargs): try: if isinstance(o1, DataFrame): assert_frame_equal(o1, o2, kwargs) return True return o1 == o2 except Exception: return False def enable_sharding(arctic, library_name, hashed=False): c = arctic._conn lib = arctic[library_name]._arctic_lib dbname = lib._db.name library_name = lib.get_top_level_collection().name try: c.admin.command('enablesharding', dbname) except OperationFailure, e: if not 'failed: already enabled' in str(e): raise if not hashed: logger.info("Range sharding 'symbol' on: " + dbname + '.' + library_name) c.admin.command('shardCollection', dbname + '.' + library_name, key={'symbol': 1}) else: logger.info("Hash sharding 'symbol' on: " + dbname + '.' + library_name) c.admin.command('shardCollection', dbname + '.' + library_name, key={'symbol': 'hashed'}) def enable_powerof2sizes(arctic, library_name): lib = arctic[library_name]._arctic_lib collection = lib.get_top_level_collection() lib._db.command({"collMod": collection.name, 'usePowerOf2Sizes': "true"}) logger.info("usePowerOf2Sizes enabled for %s", collection.name) for coll in collection.database.collection_names(): if coll.startswith("%s." % collection.name): lib._db.command({"collMod": coll, 'usePowerOf2Sizes': "true"}) logger.info("usePowerOf2Sizes enabled for %s", coll)
lgpl-2.1
BillyLiggins/fitting
first.py
1
7031
import copy import echidna import echidna.output.plot as plot import echidna.core.spectra as spectra from echidna.output import store import matplotlib.pyplot as plt import argparse import glob import numpy as np import os def convertor(path): flist=np.array(glob.glob(path)) for ntuple in flist: os.system("python ~/echidna/echidna/scripts/dump_spectra_ntuple.py -c ~/workspace/PhD/fitting/config.yml -f "+ str(ntuple)+" -s hdf5/") def combinerNtuple(path,filename): flist=np.array(glob.glob(path)) print flist first = True for hdf5 in flist: print hdf5 if first: spectrum1 = store.fill_from_ntuple(hdf5) first = False else: spectrum2 = store.fill_from_ntuple(hdf5) spectrum1.add(spectrum2) store.dump(filename, spectrum1) def combiner(path,filename): flist=np.array(glob.glob(path)) print flist first = True for hdf5 in flist: print hdf5 if first: spectrum1 = store.load(hdf5) first = False else: spectrum2 = store.load(hdf5) spectrum1.add(spectrum2) store.dump(filename, spectrum1) """The way you should do it is to define a lot of spectra and then plot them. You don't really know how to normlise the histrogram or indeed weather that is of any uses in the first place. """ def slicer(spectrumPath,filler,nslice): for i in range(nslice): spectrum=store.load(spectrumPath) print spectrum.sum() shrink_dict = {"energy_reco_low": 0., "energy_reco_high": 0.6, "radial_reco_low": i*6000.0/nslice, "radial_reco_high": (i+1)*6000/nslice} spectrum.cut(**shrink_dict) spectrum.scale(1) spec2=copy.copy(spectrum) spec2._name=str(i*1000)+"mm to "+str((i+1)*1000)+"mm" print type(spec2) filler.append(spec2) def slicerMC(spectrumPath,filler,nslice): for i in range(nslice): spectrum=store.load(spectrumPath) print spectrum.sum() shrink_dict = {"energy_mc_low": 0., "energy_mc_high": 1, "radial_mc_low": i*6000.0/nslice, "radial_mc_high": (i+1)*6000/nslice} spectrum.cut(**shrink_dict) spectrum.scale(1) spec2=copy.copy(spectrum) spec2._name="MC" print type(spec2) print "This gives the number os events in each window:" print "mc : "+str(i*6000.0/nslice)+"mm to "+str((i+1)*6000.0/nslice)+"mm : "+str(spec2.sum()) filler.append(spec2) def slicerReco(spectrumPath,filler,nslice): for i in range(nslice): spectrum=store.load(spectrumPath) print spectrum.sum() shrink_dict = {"energy_reco_low": 0., "energy_reco_high": 1., "radial_reco_low": i*6000.0/nslice, "radial_reco_high": (i+1)*6000/nslice} spectrum.cut(**shrink_dict) spectrum.scale(1) spec2=copy.copy(spectrum) spec2._name="Reco" print type(spec2) print "This gives the number os events in each window:" print "reco : "+str(i*6000.0/nslice)+"mm to "+str((i+1)*6000.0/nslice)+"mm : "+str(spec2.sum()) filler.append(spec2) def signalPlotter(spectra,dim,name): i=0 for spec in spectra: fig = plt.figure() ax= fig.add_subplot(1,1,1) par = spec.get_config().get_par(dim) width = par.get_width() bins = np.linspace(par._low,par._high, par._bins+1) x = bins[:-1] + 0.5*width plt.xlabel(str(dim)+ " [" + par.get_unit() + "]") plt.ylabel("Events per " + str(width) + " " + par.get_unit() + " bin") ax.set(title="Normalised energy spectrum in "+str(i*1000)+"mm to "+str((i+1)*1000)+"mm ",ylabel="Events per " + str(width) + " " + par.get_unit() + " bin", xlabel=str(dim)+" [" + par.get_unit() + "]") ax.hist(x,bins,weights=spec.project(dim),histtype="stepfilled", color="RoyalBlue",label=spec._name) fig.savefig("slice_"+str(name)+"_"+str(i*1000)+"_"+str((i+1)*1000)+".png") i=1+i def combiPlotter(spectra,dim,name): i=0 fig = plt.figure() ax= fig.add_subplot(1,1,1) for spec in spectra: par = spec.get_config().get_par(dim) width = par.get_width() bins = np.linspace(par._low,par._high, par._bins+1) x = bins[:-1] + 0.5*width plt.xlabel(str(dim)+ " [" + par.get_unit() + "]") plt.ylabel("Events per " + str(width) + " " + par.get_unit() + " bin") ax.set(title="Normalised energy spectrum in 1000mm slices",ylabel="Events per " + str(width) + " " + par.get_unit() + " bin", xlabel="energy_reco"+ " [" + par.get_unit() + "]") ax.hist(x,bins,weights=spec.project("energy_reco"),label=spec._name,histtype='step') ax.set_ylim([0,0.03]) ax.set_xlim([0.2,0.7]) ax.legend(loc="best") fig.savefig("combined_"+str(name)+".png") def func(path,nslice,name): spectra=[] slicer(path,spectra,nslice) signalPlotter(spectra,"energy_reco",name) combiPlotter(spectra,"energy_reco",name) def po210(): convertor("po210_ntuple/*") combiner("hdf5/SolarPo**ntuple*","hdf5/SolarPo210_combined.hdf5") plotpath="plots/" func("hdf5/SolarPo210_combined.hdf5",6,"po210") def bi210(): convertor("bi210_ntuple/*") combiner("hdf5/SolarBi**ntuple*","hdf5/SolarBi210_combined.hdf5") plotpath="plots/" func("hdf5/SolarBi210_combined.hdf5",6,"bi210") def compair(spectrumPathReco,spectrumPathMC,name): spectraReco=[] spectraMC=[] slicerReco(spectrumPathReco,spectraReco,6) slicerMC(spectrumPathMC,spectraMC,6) for i in range(0,len(spectraReco)): fig = plt.figure() ax= fig.add_subplot(1,1,1) par = spectraReco[i].get_config().get_par("energy_reco") width = par.get_width() bins = np.linspace(par._low,par._high, par._bins+1) x = bins[:-1] + 0.5*width ax.set(title="Normalised energy spectrum in "+str(i*1000)+"mm to "+str((i+1)*1000)+"mm ",ylabel="Events per " + str(width) + " " + par.get_unit() + " bin", xlabel="Energy [" + par.get_unit() + "]") ax.hist(x,bins,weights=spectraReco[i].project("energy_reco"),histtype="stepfilled",label=spectraReco[i]._name) par = spectraMC[i].get_config().get_par("energy_mc") width = par.get_width() bins = np.linspace(par._low,par._high, par._bins+1) x = bins[:-1] + 0.5*width ax.hist(x,bins,weights=spectraMC[i].project("energy_mc"),histtype="stepfilled",label=spectraMC[i]._name,alpha=0.75) ax.legend(loc=2) fig.savefig("compare_"+str(name)+"_"+str(i*1000)+"_"+str((i+1)*1000)+".png") if __name__=="__main__": print "You need to compare the recon against the mc" print "You should bin in bigger bins becuase you could then bin in 4d" """You need to plot the standard spectra"""
mit
rexshihaoren/scikit-learn
sklearn/feature_extraction/tests/test_feature_hasher.py
258
2861
from __future__ import unicode_literals import numpy as np from sklearn.feature_extraction import FeatureHasher from nose.tools import assert_raises, assert_true from numpy.testing import assert_array_equal, assert_equal def test_feature_hasher_dicts(): h = FeatureHasher(n_features=16) assert_equal("dict", h.input_type) raw_X = [{"dada": 42, "tzara": 37}, {"gaga": 17}] X1 = FeatureHasher(n_features=16).transform(raw_X) gen = (iter(d.items()) for d in raw_X) X2 = FeatureHasher(n_features=16, input_type="pair").transform(gen) assert_array_equal(X1.toarray(), X2.toarray()) def test_feature_hasher_strings(): # mix byte and Unicode strings; note that "foo" is a duplicate in row 0 raw_X = [["foo", "bar", "baz", "foo".encode("ascii")], ["bar".encode("ascii"), "baz", "quux"]] for lg_n_features in (7, 9, 11, 16, 22): n_features = 2 ** lg_n_features it = (x for x in raw_X) # iterable h = FeatureHasher(n_features, non_negative=True, input_type="string") X = h.transform(it) assert_equal(X.shape[0], len(raw_X)) assert_equal(X.shape[1], n_features) assert_true(np.all(X.data > 0)) assert_equal(X[0].sum(), 4) assert_equal(X[1].sum(), 3) assert_equal(X.nnz, 6) def test_feature_hasher_pairs(): raw_X = (iter(d.items()) for d in [{"foo": 1, "bar": 2}, {"baz": 3, "quux": 4, "foo": -1}]) h = FeatureHasher(n_features=16, input_type="pair") x1, x2 = h.transform(raw_X).toarray() x1_nz = sorted(np.abs(x1[x1 != 0])) x2_nz = sorted(np.abs(x2[x2 != 0])) assert_equal([1, 2], x1_nz) assert_equal([1, 3, 4], x2_nz) def test_hash_empty_input(): n_features = 16 raw_X = [[], (), iter(range(0))] h = FeatureHasher(n_features=n_features, input_type="string") X = h.transform(raw_X) assert_array_equal(X.A, np.zeros((len(raw_X), n_features))) def test_hasher_invalid_input(): assert_raises(ValueError, FeatureHasher, input_type="gobbledygook") assert_raises(ValueError, FeatureHasher, n_features=-1) assert_raises(ValueError, FeatureHasher, n_features=0) assert_raises(TypeError, FeatureHasher, n_features='ham') h = FeatureHasher(n_features=np.uint16(2 ** 6)) assert_raises(ValueError, h.transform, []) assert_raises(Exception, h.transform, [[5.5]]) assert_raises(Exception, h.transform, [[None]]) def test_hasher_set_params(): # Test delayed input validation in fit (useful for grid search). hasher = FeatureHasher() hasher.set_params(n_features=np.inf) assert_raises(TypeError, hasher.fit) def test_hasher_zeros(): # Assert that no zeros are materialized in the output. X = FeatureHasher().transform([{'foo': 0}]) assert_equal(X.data.shape, (0,))
bsd-3-clause
tmhm/scikit-learn
examples/plot_kernel_approximation.py
262
8004
""" ================================================== Explicit feature map approximation for RBF kernels ================================================== An example illustrating the approximation of the feature map of an RBF kernel. .. currentmodule:: sklearn.kernel_approximation It shows how to use :class:`RBFSampler` and :class:`Nystroem` to approximate the feature map of an RBF kernel for classification with an SVM on the digits dataset. Results using a linear SVM in the original space, a linear SVM using the approximate mappings and using a kernelized SVM are compared. Timings and accuracy for varying amounts of Monte Carlo samplings (in the case of :class:`RBFSampler`, which uses random Fourier features) and different sized subsets of the training set (for :class:`Nystroem`) for the approximate mapping are shown. Please note that the dataset here is not large enough to show the benefits of kernel approximation, as the exact SVM is still reasonably fast. Sampling more dimensions clearly leads to better classification results, but comes at a greater cost. This means there is a tradeoff between runtime and accuracy, given by the parameter n_components. Note that solving the Linear SVM and also the approximate kernel SVM could be greatly accelerated by using stochastic gradient descent via :class:`sklearn.linear_model.SGDClassifier`. This is not easily possible for the case of the kernelized SVM. The second plot visualized the decision surfaces of the RBF kernel SVM and the linear SVM with approximate kernel maps. The plot shows decision surfaces of the classifiers projected onto the first two principal components of the data. This visualization should be taken with a grain of salt since it is just an interesting slice through the decision surface in 64 dimensions. In particular note that a datapoint (represented as a dot) does not necessarily be classified into the region it is lying in, since it will not lie on the plane that the first two principal components span. The usage of :class:`RBFSampler` and :class:`Nystroem` is described in detail in :ref:`kernel_approximation`. """ print(__doc__) # Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org> # Andreas Mueller <amueller@ais.uni-bonn.de> # License: BSD 3 clause # Standard scientific Python imports import matplotlib.pyplot as plt import numpy as np from time import time # Import datasets, classifiers and performance metrics from sklearn import datasets, svm, pipeline from sklearn.kernel_approximation import (RBFSampler, Nystroem) from sklearn.decomposition import PCA # The digits dataset digits = datasets.load_digits(n_class=9) # To apply an classifier on this data, we need to flatten the image, to # turn the data in a (samples, feature) matrix: n_samples = len(digits.data) data = digits.data / 16. data -= data.mean(axis=0) # We learn the digits on the first half of the digits data_train, targets_train = data[:n_samples / 2], digits.target[:n_samples / 2] # Now predict the value of the digit on the second half: data_test, targets_test = data[n_samples / 2:], digits.target[n_samples / 2:] #data_test = scaler.transform(data_test) # Create a classifier: a support vector classifier kernel_svm = svm.SVC(gamma=.2) linear_svm = svm.LinearSVC() # create pipeline from kernel approximation # and linear svm feature_map_fourier = RBFSampler(gamma=.2, random_state=1) feature_map_nystroem = Nystroem(gamma=.2, random_state=1) fourier_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fourier), ("svm", svm.LinearSVC())]) nystroem_approx_svm = pipeline.Pipeline([("feature_map", feature_map_nystroem), ("svm", svm.LinearSVC())]) # fit and predict using linear and kernel svm: kernel_svm_time = time() kernel_svm.fit(data_train, targets_train) kernel_svm_score = kernel_svm.score(data_test, targets_test) kernel_svm_time = time() - kernel_svm_time linear_svm_time = time() linear_svm.fit(data_train, targets_train) linear_svm_score = linear_svm.score(data_test, targets_test) linear_svm_time = time() - linear_svm_time sample_sizes = 30 * np.arange(1, 10) fourier_scores = [] nystroem_scores = [] fourier_times = [] nystroem_times = [] for D in sample_sizes: fourier_approx_svm.set_params(feature_map__n_components=D) nystroem_approx_svm.set_params(feature_map__n_components=D) start = time() nystroem_approx_svm.fit(data_train, targets_train) nystroem_times.append(time() - start) start = time() fourier_approx_svm.fit(data_train, targets_train) fourier_times.append(time() - start) fourier_score = fourier_approx_svm.score(data_test, targets_test) nystroem_score = nystroem_approx_svm.score(data_test, targets_test) nystroem_scores.append(nystroem_score) fourier_scores.append(fourier_score) # plot the results: plt.figure(figsize=(8, 8)) accuracy = plt.subplot(211) # second y axis for timeings timescale = plt.subplot(212) accuracy.plot(sample_sizes, nystroem_scores, label="Nystroem approx. kernel") timescale.plot(sample_sizes, nystroem_times, '--', label='Nystroem approx. kernel') accuracy.plot(sample_sizes, fourier_scores, label="Fourier approx. kernel") timescale.plot(sample_sizes, fourier_times, '--', label='Fourier approx. kernel') # horizontal lines for exact rbf and linear kernels: accuracy.plot([sample_sizes[0], sample_sizes[-1]], [linear_svm_score, linear_svm_score], label="linear svm") timescale.plot([sample_sizes[0], sample_sizes[-1]], [linear_svm_time, linear_svm_time], '--', label='linear svm') accuracy.plot([sample_sizes[0], sample_sizes[-1]], [kernel_svm_score, kernel_svm_score], label="rbf svm") timescale.plot([sample_sizes[0], sample_sizes[-1]], [kernel_svm_time, kernel_svm_time], '--', label='rbf svm') # vertical line for dataset dimensionality = 64 accuracy.plot([64, 64], [0.7, 1], label="n_features") # legends and labels accuracy.set_title("Classification accuracy") timescale.set_title("Training times") accuracy.set_xlim(sample_sizes[0], sample_sizes[-1]) accuracy.set_xticks(()) accuracy.set_ylim(np.min(fourier_scores), 1) timescale.set_xlabel("Sampling steps = transformed feature dimension") accuracy.set_ylabel("Classification accuracy") timescale.set_ylabel("Training time in seconds") accuracy.legend(loc='best') timescale.legend(loc='best') # visualize the decision surface, projected down to the first # two principal components of the dataset pca = PCA(n_components=8).fit(data_train) X = pca.transform(data_train) # Gemerate grid along first two principal components multiples = np.arange(-2, 2, 0.1) # steps along first component first = multiples[:, np.newaxis] * pca.components_[0, :] # steps along second component second = multiples[:, np.newaxis] * pca.components_[1, :] # combine grid = first[np.newaxis, :, :] + second[:, np.newaxis, :] flat_grid = grid.reshape(-1, data.shape[1]) # title for the plots titles = ['SVC with rbf kernel', 'SVC (linear kernel)\n with Fourier rbf feature map\n' 'n_components=100', 'SVC (linear kernel)\n with Nystroem rbf feature map\n' 'n_components=100'] plt.tight_layout() plt.figure(figsize=(12, 5)) # predict and plot for i, clf in enumerate((kernel_svm, nystroem_approx_svm, fourier_approx_svm)): # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. plt.subplot(1, 3, i + 1) Z = clf.predict(flat_grid) # Put the result into a color plot Z = Z.reshape(grid.shape[:-1]) plt.contourf(multiples, multiples, Z, cmap=plt.cm.Paired) plt.axis('off') # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=targets_train, cmap=plt.cm.Paired) plt.title(titles[i]) plt.tight_layout() plt.show()
bsd-3-clause
harisbal/pandas
pandas/core/tools/datetimes.py
4
30680
from functools import partial from datetime import datetime, time from collections import MutableMapping import numpy as np from pandas._libs import tslib, tslibs from pandas._libs.tslibs.strptime import array_strptime from pandas._libs.tslibs import parsing, conversion, Timestamp from pandas._libs.tslibs.parsing import ( # noqa parse_time_string, DateParseError, _format_is_iso, _guess_datetime_format) from pandas.core.dtypes.common import ( ensure_object, is_datetime64_ns_dtype, is_datetime64_dtype, is_datetime64tz_dtype, is_integer_dtype, is_integer, is_float, is_list_like, is_scalar, is_numeric_dtype, is_object_dtype) from pandas.core.dtypes.generic import ( ABCIndexClass, ABCSeries, ABCDataFrame) from pandas.core.dtypes.missing import notna from pandas.core import algorithms from pandas.compat import zip def _guess_datetime_format_for_array(arr, **kwargs): # Try to guess the format based on the first non-NaN element non_nan_elements = notna(arr).nonzero()[0] if len(non_nan_elements): return _guess_datetime_format(arr[non_nan_elements[0]], **kwargs) def _maybe_cache(arg, format, cache, convert_listlike): """ Create a cache of unique dates from an array of dates Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series format : string Strftime format to parse time cache : boolean True attempts to create a cache of converted values convert_listlike : function Conversion function to apply on dates Returns ------- cache_array : Series Cache of converted, unique dates. Can be empty """ from pandas import Series cache_array = Series() if cache: # Perform a quicker unique check from pandas import Index if not Index(arg).is_unique: unique_dates = algorithms.unique(arg) cache_dates = convert_listlike(unique_dates, True, format) cache_array = Series(cache_dates, index=unique_dates) return cache_array def _convert_and_box_cache(arg, cache_array, box, errors, name=None): """ Convert array of dates with a cache and box the result Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series cache_array : Series Cache of converted, unique dates box : boolean True boxes result as an Index-like, False returns an ndarray errors : string 'ignore' plus box=True will convert result to Index name : string, default None Name for a DatetimeIndex Returns ------- result : datetime of converted dates Returns: - Index-like if box=True - ndarray if box=False """ from pandas import Series, DatetimeIndex, Index result = Series(arg).map(cache_array) if box: if errors == 'ignore': return Index(result, name=name) else: return DatetimeIndex(result, name=name) return result.values def _return_parsed_timezone_results(result, timezones, box, tz, name): """ Return results from array_strptime if a %z or %Z directive was passed. Parameters ---------- result : ndarray int64 date representations of the dates timezones : ndarray pytz timezone objects box : boolean True boxes result as an Index-like, False returns an ndarray tz : object None or pytz timezone object name : string, default None Name for a DatetimeIndex Returns ------- tz_result : ndarray of parsed dates with timezone Returns: - Index-like if box=True - ndarray of Timestamps if box=False """ if tz is not None: raise ValueError("Cannot pass a tz argument when " "parsing strings with timezone " "information.") tz_results = np.array([Timestamp(res).tz_localize(zone) for res, zone in zip(result, timezones)]) if box: from pandas import Index return Index(tz_results, name=name) return tz_results def _convert_listlike_datetimes(arg, box, format, name=None, tz=None, unit=None, errors=None, infer_datetime_format=None, dayfirst=None, yearfirst=None, exact=None): """ Helper function for to_datetime. Performs the conversions of 1D listlike of dates Parameters ---------- arg : list, tuple, ndarray, Series, Index date to be parced box : boolean True boxes result as an Index-like, False returns an ndarray name : object None or string for the Index name tz : object None or 'utc' unit : string None or string of the frequency of the passed data errors : string error handing behaviors from to_datetime, 'raise', 'coerce', 'ignore' infer_datetime_format : boolean inferring format behavior from to_datetime dayfirst : boolean dayfirst parsing behavior from to_datetime yearfirst : boolean yearfirst parsing behavior from to_datetime exact : boolean exact format matching behavior from to_datetime Returns ------- ndarray of parsed dates Returns: - Index-like if box=True - ndarray of Timestamps if box=False """ from pandas import DatetimeIndex if isinstance(arg, (list, tuple)): arg = np.array(arg, dtype='O') # these are shortcutable if is_datetime64tz_dtype(arg): if not isinstance(arg, DatetimeIndex): return DatetimeIndex(arg, tz=tz, name=name) if tz == 'utc': arg = arg.tz_convert(None).tz_localize(tz) return arg elif is_datetime64_ns_dtype(arg): if box and not isinstance(arg, DatetimeIndex): try: return DatetimeIndex(arg, tz=tz, name=name) except ValueError: pass return arg elif unit is not None: if format is not None: raise ValueError("cannot specify both format and unit") arg = getattr(arg, 'values', arg) result = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) if box: if errors == 'ignore': from pandas import Index return Index(result, name=name) return DatetimeIndex(result, tz=tz, name=name) return result elif getattr(arg, 'ndim', 1) > 1: raise TypeError('arg must be a string, datetime, list, tuple, ' '1-d array, or Series') arg = ensure_object(arg) require_iso8601 = False if infer_datetime_format and format is None: format = _guess_datetime_format_for_array(arg, dayfirst=dayfirst) if format is not None: # There is a special fast-path for iso8601 formatted # datetime strings, so in those cases don't use the inferred # format because this path makes process slower in this # special case format_is_iso8601 = _format_is_iso(format) if format_is_iso8601: require_iso8601 = not infer_datetime_format format = None try: result = None if format is not None: # shortcut formatting here if format == '%Y%m%d': try: result = _attempt_YYYYMMDD(arg, errors=errors) except (ValueError, TypeError, tslibs.OutOfBoundsDatetime): raise ValueError("cannot convert the input to " "'%Y%m%d' date format") # fallback if result is None: try: result, timezones = array_strptime( arg, format, exact=exact, errors=errors) if '%Z' in format or '%z' in format: return _return_parsed_timezone_results( result, timezones, box, tz, name) except tslibs.OutOfBoundsDatetime: if errors == 'raise': raise result = arg except ValueError: # if format was inferred, try falling back # to array_to_datetime - terminate here # for specified formats if not infer_datetime_format: if errors == 'raise': raise result = arg if result is None and (format is None or infer_datetime_format): result, tz_parsed = tslib.array_to_datetime( arg, errors=errors, utc=tz == 'utc', dayfirst=dayfirst, yearfirst=yearfirst, require_iso8601=require_iso8601 ) if tz_parsed is not None: if box: # We can take a shortcut since the datetime64 numpy array # is in UTC return DatetimeIndex._simple_new(result, name=name, tz=tz_parsed) else: # Convert the datetime64 numpy array to an numpy array # of datetime objects result = [Timestamp(ts, tz=tz_parsed).to_pydatetime() for ts in result] return np.array(result, dtype=object) if box: # Ensure we return an Index in all cases where box=True if is_datetime64_dtype(result): return DatetimeIndex(result, tz=tz, name=name) elif is_object_dtype(result): # e.g. an Index of datetime objects from pandas import Index return Index(result, name=name) return result except ValueError as e: try: values, tz = conversion.datetime_to_datetime64(arg) return DatetimeIndex._simple_new(values, name=name, tz=tz) except (ValueError, TypeError): raise e def _adjust_to_origin(arg, origin, unit): """ Helper function for to_datetime. Adjust input argument to the specified origin Parameters ---------- arg : list, tuple, ndarray, Series, Index date to be adjusted origin : 'julian' or Timestamp origin offset for the arg unit : string passed unit from to_datetime, must be 'D' Returns ------- ndarray or scalar of adjusted date(s) """ if origin == 'julian': original = arg j0 = Timestamp(0).to_julian_date() if unit != 'D': raise ValueError("unit must be 'D' for origin='julian'") try: arg = arg - j0 except TypeError: raise ValueError("incompatible 'arg' type for given " "'origin'='julian'") # premptively check this for a nice range j_max = Timestamp.max.to_julian_date() - j0 j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): raise tslibs.OutOfBoundsDatetime( "{original} is Out of Bounds for " "origin='julian'".format(original=original)) else: # arg must be numeric if not ((is_scalar(arg) and (is_integer(arg) or is_float(arg))) or is_numeric_dtype(np.asarray(arg))): raise ValueError( "'{arg}' is not compatible with origin='{origin}'; " "it must be numeric with a unit specified ".format( arg=arg, origin=origin)) # we are going to offset back to unix / epoch time try: offset = Timestamp(origin) except tslibs.OutOfBoundsDatetime: raise tslibs.OutOfBoundsDatetime( "origin {origin} is Out of Bounds".format(origin=origin)) except ValueError: raise ValueError("origin {origin} cannot be converted " "to a Timestamp".format(origin=origin)) if offset.tz is not None: raise ValueError( "origin offset {} must be tz-naive".format(offset)) offset -= Timestamp(0) # convert the offset to the unit of the arg # this should be lossless in terms of precision offset = offset // tslibs.Timedelta(1, unit=unit) # scalars & ndarray-like can handle the addition if is_list_like(arg) and not isinstance( arg, (ABCSeries, ABCIndexClass, np.ndarray)): arg = np.asarray(arg) arg = arg + offset return arg def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=False): """ Convert argument to datetime. Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series .. versionadded:: 0.18.1 or DataFrame/dict-like errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as NaT - If 'ignore', then invalid parsing will return the input dayfirst : boolean, default False Specify a date parse order if `arg` is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug, based on dateutil behavior). yearfirst : boolean, default False Specify a date parse order if `arg` is str or its list-likes. - If True parses dates with the year first, eg 10/11/12 is parsed as 2010-11-12. - If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil). Warning: yearfirst=True is not strict, but will prefer to parse with year first (this is a known bug, based on dateutil behavior). .. versionadded:: 0.16.1 utc : boolean, default None Return UTC DatetimeIndex if True (converting any tz-aware datetime.datetime objects as well). box : boolean, default True - If True returns a DatetimeIndex or Index-like object - If False returns ndarray of values. format : string, default None strftime to parse time, eg "%d/%m/%Y", note that "%f" will parse all the way up to nanoseconds. exact : boolean, True by default - If True, require an exact format match. - If False, allow the format to match anywhere in the target string. unit : string, default 'ns' unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit='ms' and origin='unix' (the default), this would calculate the number of milliseconds to the unix epoch start. infer_datetime_format : boolean, default False If True and no `format` is given, attempt to infer the format of the datetime strings, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by ~5-10x. origin : scalar, default is 'unix' Define the reference date. The numeric values would be parsed as number of units (defined by `unit`) since this reference date. - If 'unix' (or POSIX) time; origin is set to 1970-01-01. - If 'julian', unit must be 'D', and origin is set to beginning of Julian Calendar. Julian day number 0 is assigned to the day starting at noon on January 1, 4713 BC. - If Timestamp convertible, origin is set to Timestamp identified by origin. .. versionadded:: 0.20.0 cache : boolean, default False If True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. .. versionadded:: 0.23.0 Returns ------- ret : datetime if parsing succeeded. Return type depends on input: - list-like: DatetimeIndex - Series: Series of datetime64 dtype - scalar: Timestamp In case when it is not possible to return designated types (e.g. when any element of input is before Timestamp.min or after Timestamp.max) return will have datetime.datetime type (or corresponding array/Series). Examples -------- Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like ['year', 'month', 'day', 'minute', 'second', 'ms', 'us', 'ns']) or plurals of the same >>> df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3], 'day': [4, 5]}) >>> pd.to_datetime(df) 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns] If a date does not meet the `timestamp limitations <http://pandas.pydata.org/pandas-docs/stable/timeseries.html #timeseries-timestamp-limits>`_, passing errors='ignore' will return the original input instead of raising any exception. Passing errors='coerce' will force an out-of-bounds date to NaT, in addition to forcing non-dates (or non-parseable dates) to NaT. >>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT Passing infer_datetime_format=True can often-times speedup a parsing if its not an ISO8601 format exactly, but in a regular format. >>> s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000']*1000) >>> s.head() 0 3/11/2000 1 3/12/2000 2 3/13/2000 3 3/11/2000 4 3/12/2000 dtype: object >>> %timeit pd.to_datetime(s,infer_datetime_format=True) 100 loops, best of 3: 10.4 ms per loop >>> %timeit pd.to_datetime(s,infer_datetime_format=False) 1 loop, best of 3: 471 ms per loop Using a unix epoch time >>> pd.to_datetime(1490195805, unit='s') Timestamp('2017-03-22 15:16:45') >>> pd.to_datetime(1490195805433502912, unit='ns') Timestamp('2017-03-22 15:16:45.433502912') .. warning:: For float arg, precision rounding might happen. To prevent unexpected behavior use a fixed-width exact type. Using a non-unix epoch origin >>> pd.to_datetime([1, 2, 3], unit='D', origin=pd.Timestamp('1960-01-01')) 0 1960-01-02 1 1960-01-03 2 1960-01-04 See also -------- pandas.DataFrame.astype : Cast argument to a specified dtype. pandas.to_timedelta : Convert argument to timedelta. """ if arg is None: return None if origin != 'unix': arg = _adjust_to_origin(arg, origin, unit) tz = 'utc' if utc else None convert_listlike = partial(_convert_listlike_datetimes, tz=tz, unit=unit, dayfirst=dayfirst, yearfirst=yearfirst, errors=errors, exact=exact, infer_datetime_format=infer_datetime_format) if isinstance(arg, Timestamp): result = arg elif isinstance(arg, ABCSeries): cache_array = _maybe_cache(arg, format, cache, convert_listlike) if not cache_array.empty: result = arg.map(cache_array) else: from pandas import Series values = convert_listlike(arg._values, True, format) result = Series(values, index=arg.index, name=arg.name) elif isinstance(arg, (ABCDataFrame, MutableMapping)): result = _assemble_from_unit_mappings(arg, errors=errors) elif isinstance(arg, ABCIndexClass): cache_array = _maybe_cache(arg, format, cache, convert_listlike) if not cache_array.empty: result = _convert_and_box_cache(arg, cache_array, box, errors, name=arg.name) else: convert_listlike = partial(convert_listlike, name=arg.name) result = convert_listlike(arg, box, format) elif is_list_like(arg): cache_array = _maybe_cache(arg, format, cache, convert_listlike) if not cache_array.empty: result = _convert_and_box_cache(arg, cache_array, box, errors) else: result = convert_listlike(arg, box, format) else: result = convert_listlike(np.array([arg]), box, format)[0] return result # mappings for assembling units _unit_map = {'year': 'year', 'years': 'year', 'month': 'month', 'months': 'month', 'day': 'day', 'days': 'day', 'hour': 'h', 'hours': 'h', 'minute': 'm', 'minutes': 'm', 'second': 's', 'seconds': 's', 'ms': 'ms', 'millisecond': 'ms', 'milliseconds': 'ms', 'us': 'us', 'microsecond': 'us', 'microseconds': 'us', 'ns': 'ns', 'nanosecond': 'ns', 'nanoseconds': 'ns' } def _assemble_from_unit_mappings(arg, errors): """ assemble the unit specified fields from the arg (DataFrame) Return a Series for actual parsing Parameters ---------- arg : DataFrame errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as NaT - If 'ignore', then invalid parsing will return the input Returns ------- Series """ from pandas import to_timedelta, to_numeric, DataFrame arg = DataFrame(arg) if not arg.columns.is_unique: raise ValueError("cannot assemble with duplicate keys") # replace passed unit with _unit_map def f(value): if value in _unit_map: return _unit_map[value] # m is case significant if value.lower() in _unit_map: return _unit_map[value.lower()] return value unit = {k: f(k) for k in arg.keys()} unit_rev = {v: k for k, v in unit.items()} # we require at least Ymd required = ['year', 'month', 'day'] req = sorted(list(set(required) - set(unit_rev.keys()))) if len(req): raise ValueError("to assemble mappings requires at least that " "[year, month, day] be specified: [{required}] " "is missing".format(required=','.join(req))) # keys we don't recognize excess = sorted(list(set(unit_rev.keys()) - set(_unit_map.values()))) if len(excess): raise ValueError("extra keys have been passed " "to the datetime assemblage: " "[{excess}]".format(excess=','.join(excess))) def coerce(values): # we allow coercion to if errors allows values = to_numeric(values, errors=errors) # prevent overflow in case of int8 or int16 if is_integer_dtype(values): values = values.astype('int64', copy=False) return values values = (coerce(arg[unit_rev['year']]) * 10000 + coerce(arg[unit_rev['month']]) * 100 + coerce(arg[unit_rev['day']])) try: values = to_datetime(values, format='%Y%m%d', errors=errors) except (TypeError, ValueError) as e: raise ValueError("cannot assemble the " "datetimes: {error}".format(error=e)) for u in ['h', 'm', 's', 'ms', 'us', 'ns']: value = unit_rev.get(u) if value is not None and value in arg: try: values += to_timedelta(coerce(arg[value]), unit=u, errors=errors) except (TypeError, ValueError) as e: raise ValueError("cannot assemble the datetimes [{value}]: " "{error}".format(value=value, error=e)) return values def _attempt_YYYYMMDD(arg, errors): """ try to parse the YYYYMMDD/%Y%m%d format, try to deal with NaT-like, arg is a passed in as an object dtype, but could really be ints/strings with nan-like/or floats (e.g. with nan) Parameters ---------- arg : passed value errors : 'raise','ignore','coerce' """ def calc(carg): # calculate the actual result carg = carg.astype(object) parsed = parsing.try_parse_year_month_day(carg / 10000, carg / 100 % 100, carg % 100) return tslib.array_to_datetime(parsed, errors=errors)[0] def calc_with_mask(carg, mask): result = np.empty(carg.shape, dtype='M8[ns]') iresult = result.view('i8') iresult[~mask] = tslibs.iNaT masked_result = calc(carg[mask].astype(np.float64).astype(np.int64)) result[mask] = masked_result.astype('M8[ns]') return result # try intlike / strings that are ints try: return calc(arg.astype(np.int64)) except ValueError: pass # a float with actual np.nan try: carg = arg.astype(np.float64) return calc_with_mask(carg, notna(carg)) except ValueError: pass # string with NaN-like try: mask = ~algorithms.isin(arg, list(tslib.nat_strings)) return calc_with_mask(arg, mask) except ValueError: pass return None # Fixed time formats for time parsing _time_formats = ["%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p"] def _guess_time_format_for_array(arr): # Try to guess the format based on the first non-NaN element non_nan_elements = notna(arr).nonzero()[0] if len(non_nan_elements): element = arr[non_nan_elements[0]] for time_format in _time_formats: try: datetime.strptime(element, time_format) return time_format except ValueError: pass return None def to_time(arg, format=None, infer_time_format=False, errors='raise'): """ Parse time strings to time objects using fixed strptime formats ("%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p") Use infer_time_format if all the strings are in the same format to speed up conversion. Parameters ---------- arg : string in time format, datetime.time, list, tuple, 1-d array, Series format : str, default None Format used to convert arg into a time object. If None, fixed formats are used. infer_time_format: bool, default False Infer the time format based on the first non-NaN element. If all strings are in the same format, this will speed up conversion. errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as None - If 'ignore', then invalid parsing will return the input Returns ------- datetime.time """ from pandas.core.series import Series def _convert_listlike(arg, format): if isinstance(arg, (list, tuple)): arg = np.array(arg, dtype='O') elif getattr(arg, 'ndim', 1) > 1: raise TypeError('arg must be a string, datetime, list, tuple, ' '1-d array, or Series') arg = ensure_object(arg) if infer_time_format and format is None: format = _guess_time_format_for_array(arg) times = [] if format is not None: for element in arg: try: times.append(datetime.strptime(element, format).time()) except (ValueError, TypeError): if errors == 'raise': msg = ("Cannot convert {element} to a time with given " "format {format}").format(element=element, format=format) raise ValueError(msg) elif errors == 'ignore': return arg else: times.append(None) else: formats = _time_formats[:] format_found = False for element in arg: time_object = None for time_format in formats: try: time_object = datetime.strptime(element, time_format).time() if not format_found: # Put the found format in front fmt = formats.pop(formats.index(time_format)) formats.insert(0, fmt) format_found = True break except (ValueError, TypeError): continue if time_object is not None: times.append(time_object) elif errors == 'raise': raise ValueError("Cannot convert arg {arg} to " "a time".format(arg=arg)) elif errors == 'ignore': return arg else: times.append(None) return times if arg is None: return arg elif isinstance(arg, time): return arg elif isinstance(arg, Series): values = _convert_listlike(arg._values, format) return Series(values, index=arg.index, name=arg.name) elif isinstance(arg, ABCIndexClass): return _convert_listlike(arg, format) elif is_list_like(arg): return _convert_listlike(arg, format) return _convert_listlike(np.array([arg]), format)[0]
bsd-3-clause
guziy/basemap
setup.py
1
6013
from __future__ import (absolute_import, division, print_function) import glob import io import os import sys from setuptools.dist import Distribution if sys.version_info < (2, 6): raise SystemExit("""matplotlib and the basemap toolkit require Python 2.6 or later.""") # Do not require numpy for just querying the package # Taken from the netcdf-python setup file (which took it from h5py setup file). inc_dirs = [] if any('--' + opt in sys.argv for opt in Distribution.display_option_names + ['help-commands', 'help']) or sys.argv[1] == 'egg_info': from setuptools import setup, Extension else: import numpy # Use numpy versions if they are available. from numpy.distutils.core import setup, Extension # append numpy include dir. inc_dirs.append(numpy.get_include()) def get_install_requirements(path): path = os.path.join(os.path.dirname(__file__), path) with io.open(path, encoding='utf-8') as fp: content = fp.read() return [req for req in content.split("\n") if req != '' and not req.startswith('#')] def checkversion(GEOS_dir): """check geos C-API header file (geos_c.h)""" try: f = open(os.path.join(GEOS_dir, 'include', 'geos_c.h')) except IOError: return None geos_version = None for line in f: if line.startswith('#define GEOS_VERSION'): geos_version = line.split()[2] return geos_version # get location of geos lib from environment variable if it is set. if 'GEOS_DIR' in os.environ: GEOS_dir = os.environ.get('GEOS_DIR') else: # set GEOS_dir manually here if automatic detection fails. GEOS_dir = None user_home = os.path.expanduser('~') geos_search_locations = [user_home, os.path.join(user_home, 'local'), '/usr', '/usr/local', '/sw', '/opt', '/opt/local'] if GEOS_dir is None: # if GEOS_dir not set, check a few standard locations. GEOS_dirs = geos_search_locations for direc in GEOS_dirs: geos_version = checkversion(direc) sys.stdout.write('checking for GEOS lib in %s ....\n' % direc) if geos_version is None or geos_version < '"3.1.1"': continue else: sys.stdout.write('GEOS lib (version %s) found in %s\n' %\ (geos_version[1:-1],direc)) GEOS_dir = direc break else: geos_version = checkversion(GEOS_dir) if GEOS_dir is None: raise SystemExit(""" Can't find geos library in standard locations ('%s'). Please install the corresponding packages using your systems software management system (e.g. for Debian Linux do: 'apt-get install libgeos-3.3.3 libgeos-c1 libgeos-dev' and/or set the environment variable GEOS_DIR to point to the location where geos is installed (for example, if geos_c.h is in /usr/local/include, and libgeos_c is in /usr/local/lib, set GEOS_DIR to /usr/local), or edit the setup.py script manually and set the variable GEOS_dir (right after the line that says "set GEOS_dir manually here".""" % "', '".join(geos_search_locations)) else: geos_include_dirs=[os.path.join(GEOS_dir,'include')] + inc_dirs geos_library_dirs=[os.path.join(GEOS_dir,'lib'),os.path.join(GEOS_dir,'lib64')] packages = ['mpl_toolkits','mpl_toolkits.basemap'] namespace_packages = ['mpl_toolkits'] package_dirs = {'':'lib'} # can't install _geoslib in mpl_toolkits.basemap namespace, # or Basemap objects won't be pickleable. # don't use runtime_library_dirs on windows (workaround # for a distutils bug - http://bugs.python.org/issue2437). if sys.platform == 'win32': runtime_lib_dirs = [] else: runtime_lib_dirs = geos_library_dirs extensions = [ Extension("_geoslib",['src/_geoslib.c'], library_dirs=geos_library_dirs, runtime_library_dirs=runtime_lib_dirs, include_dirs=geos_include_dirs, libraries=['geos_c']) ] # Specify all the required mpl data pathout =\ os.path.join('lib',os.path.join('mpl_toolkits',os.path.join('basemap','data'))) datafiles = glob.glob(os.path.join(pathout,'*')) datafiles = [os.path.join('data',os.path.basename(f)) for f in datafiles] package_data = {'mpl_toolkits.basemap':datafiles} install_requires = get_install_requirements("requirements.txt") __version__ = "1.2.1" setup( name = "basemap", version = __version__, description = "Plot data on map projections with matplotlib", long_description = """ An add-on toolkit for matplotlib that lets you plot data on map projections with coastlines, lakes, rivers and political boundaries. See http://matplotlib.org/basemap/users/examples.html for examples of what it can do.""", url = "https://matplotlib.org/basemap/", download_url = "https://github.com/matplotlib/basemap/archive/v{0}rel.tar.gz".format(__version__), author = "Jeff Whitaker", author_email = "jeffrey.s.whitaker@noaa.gov", maintainer = "Ben Root", maintainer_email = "ben.v.root@gmail.com", install_requires = install_requires, platforms = ["any"], license = "OSI Approved", keywords = ["python","plotting","plots","graphs","charts","GIS","mapping","map projections","maps"], classifiers = ["Development Status :: 5 - Production/Stable", "Intended Audience :: Science/Research", "License :: OSI Approved", "Programming Language :: Python", "Programming Language :: Python :: 3", "Topic :: Scientific/Engineering :: Visualization", "Topic :: Software Development :: Libraries :: Python Modules", "Operating System :: OS Independent"], packages = packages, namespace_packages = namespace_packages, package_dir = package_dirs, ext_modules = extensions, package_data = package_data )
gpl-2.0
jmetzen/scikit-learn
examples/svm/plot_oneclass.py
80
2338
""" ========================================== One-class SVM with non-linear kernel (RBF) ========================================== An example using a one-class SVM for novelty detection. :ref:`One-class SVM <svm_outlier_detection>` is an unsupervised algorithm that learns a decision function for novelty detection: classifying new data as similar or different to the training set. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) # Generate train data X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations X = 0.3 * np.random.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate some abnormal novel observations X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) # fit the model clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) n_error_train = y_pred_train[y_pred_train == -1].size n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size # plot the line, the points, and the nearest vectors to the plane Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.title("Novelty Detection") plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu) a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='darkred') plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred') s = 40 b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white', s=s) b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='blueviolet', s=s) c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='gold', s=s) plt.axis('tight') plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.legend([a.collections[0], b1, b2, c], ["learned frontier", "training observations", "new regular observations", "new abnormal observations"], loc="upper left", prop=matplotlib.font_manager.FontProperties(size=11)) plt.xlabel( "error train: %d/200 ; errors novel regular: %d/40 ; " "errors novel abnormal: %d/40" % (n_error_train, n_error_test, n_error_outliers)) plt.show()
bsd-3-clause
olologin/scikit-learn
examples/svm/plot_iris.py
225
3252
""" ================================================== Plot different SVM classifiers in the iris dataset ================================================== Comparison of different linear SVM classifiers on a 2D projection of the iris dataset. We only consider the first 2 features of this dataset: - Sepal length - Sepal width This example shows how to plot the decision surface for four SVM classifiers with different kernels. The linear models ``LinearSVC()`` and ``SVC(kernel='linear')`` yield slightly different decision boundaries. This can be a consequence of the following differences: - ``LinearSVC`` minimizes the squared hinge loss while ``SVC`` minimizes the regular hinge loss. - ``LinearSVC`` uses the One-vs-All (also known as One-vs-Rest) multiclass reduction while ``SVC`` uses the One-vs-One multiclass reduction. Both linear models have linear decision boundaries (intersecting hyperplanes) while the non-linear kernel models (polynomial or Gaussian RBF) have more flexible non-linear decision boundaries with shapes that depend on the kind of kernel and its parameters. .. NOTE:: while plotting the decision function of classifiers for toy 2D datasets can help get an intuitive understanding of their respective expressive power, be aware that those intuitions don't always generalize to more realistic high-dimensional problems. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target h = .02 # step size in the mesh # we create an instance of SVM and fit out data. We do not scale our # data since we want to plot the support vectors C = 1.0 # SVM regularization parameter svc = svm.SVC(kernel='linear', C=C).fit(X, y) rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y) poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y) lin_svc = svm.LinearSVC(C=C).fit(X, y) # create a mesh to plot in x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # title for the plots titles = ['SVC with linear kernel', 'LinearSVC (linear kernel)', 'SVC with RBF kernel', 'SVC with polynomial (degree 3) kernel'] for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)): # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. plt.subplot(2, 2, i + 1) plt.subplots_adjust(wspace=0.4, hspace=0.4) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8) # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired) plt.xlabel('Sepal length') plt.ylabel('Sepal width') plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.xticks(()) plt.yticks(()) plt.title(titles[i]) plt.show()
bsd-3-clause
nlhepler/freetype-py3
examples/glyph-vector-2.py
1
3414
#!/usr/bin/env python # -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # # FreeType high-level python API - Copyright 2011 Nicolas P. Rougier # Distributed under the terms of the new BSD license. # # ----------------------------------------------------------------------------- ''' Show how to access glyph outline description. ''' from freetype import * if __name__ == '__main__': import numpy import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches face = Face(b'./Vera.ttf') face.set_char_size( 32*64 ) face.load_char('g') slot = face.glyph bitmap = face.glyph.bitmap width = face.glyph.bitmap.width rows = face.glyph.bitmap.rows pitch = face.glyph.bitmap.pitch data = [] for i in range(rows): data.extend(bitmap.buffer[i*pitch:i*pitch+width]) Z = numpy.array(data,dtype=numpy.ubyte).reshape(rows, width) outline = slot.outline points = numpy.array(outline.points, dtype=[('x',float), ('y',float)]) x, y = points['x'], points['y'] figure = plt.figure(figsize=(8,10)) axis = figure.add_subplot(111) #axis.scatter(points['x'], points['y'], alpha=.25) start, end = 0, 0 VERTS, CODES = [], [] # Iterate over each contour for i in range(len(outline.contours)): end = outline.contours[i] points = outline.points[start:end+1] points.append(points[0]) tags = outline.tags[start:end+1] tags.append(tags[0]) segments = [ [points[0],], ] for j in range(1, len(points) ): segments[-1].append(points[j]) if tags[j] & (1 << 0) and j < (len(points)-1): segments.append( [points[j],] ) verts = [points[0], ] codes = [Path.MOVETO,] for segment in segments: if len(segment) == 2: verts.extend(segment[1:]) codes.extend([Path.LINETO]) elif len(segment) == 3: verts.extend(segment[1:]) codes.extend([Path.CURVE3, Path.CURVE3]) else: verts.append(segment[1]) codes.append(Path.CURVE3) for i in range(1,len(segment)-2): A,B = segment[i], segment[i+1] C = ((A[0]+B[0])/2.0, (A[1]+B[1])/2.0) verts.extend([ C, B ]) codes.extend([ Path.CURVE3, Path.CURVE3]) verts.append(segment[-1]) codes.append(Path.CURVE3) VERTS.extend(verts) CODES.extend(codes) start = end+1 # Draw glyph path = Path(VERTS, CODES) glyph = patches.PathPatch(path, fill = True, facecolor=(0.8,0.5,0.8), alpha=.25, lw=0) glyph_outline = patches.PathPatch(path, fill = False, edgecolor='black', lw=3) plt.imshow(Z, extent=[x.min(), x.max(),y.min(), y.max()], interpolation='nearest', cmap = plt.cm.gray_r, vmin=0, vmax=400) plt.xticks(numpy.linspace(x.min(), x.max(), Z.shape[1]+1), ()) plt.yticks(numpy.linspace(y.min(), y.max(), Z.shape[0]+1), ()) plt.grid(color='k', linewidth=1, linestyle='-') axis.add_patch(glyph) axis.add_patch(glyph_outline) axis.set_xlim(x.min(), x.max()) axis.set_ylim(y.min(), y.max()) plt.savefig('test.pdf') plt.show()
bsd-3-clause
ssaeger/scikit-learn
sklearn/feature_selection/tests/test_base.py
143
3670
import numpy as np from scipy import sparse as sp from nose.tools import assert_raises, assert_equal from numpy.testing import assert_array_equal from sklearn.base import BaseEstimator from sklearn.feature_selection.base import SelectorMixin from sklearn.utils import check_array class StepSelector(SelectorMixin, BaseEstimator): """Retain every `step` features (beginning with 0)""" def __init__(self, step=2): self.step = step def fit(self, X, y=None): X = check_array(X, 'csc') self.n_input_feats = X.shape[1] return self def _get_support_mask(self): mask = np.zeros(self.n_input_feats, dtype=bool) mask[::self.step] = True return mask support = [True, False] * 5 support_inds = [0, 2, 4, 6, 8] X = np.arange(20).reshape(2, 10) Xt = np.arange(0, 20, 2).reshape(2, 5) Xinv = X.copy() Xinv[:, 1::2] = 0 y = [0, 1] feature_names = list('ABCDEFGHIJ') feature_names_t = feature_names[::2] feature_names_inv = np.array(feature_names) feature_names_inv[1::2] = '' def test_transform_dense(): sel = StepSelector() Xt_actual = sel.fit(X, y).transform(X) Xt_actual2 = StepSelector().fit_transform(X, y) assert_array_equal(Xt, Xt_actual) assert_array_equal(Xt, Xt_actual2) # Check dtype matches assert_equal(np.int32, sel.transform(X.astype(np.int32)).dtype) assert_equal(np.float32, sel.transform(X.astype(np.float32)).dtype) # Check 1d list and other dtype: names_t_actual = sel.transform([feature_names]) assert_array_equal(feature_names_t, names_t_actual.ravel()) # Check wrong shape raises error assert_raises(ValueError, sel.transform, np.array([[1], [2]])) def test_transform_sparse(): sparse = sp.csc_matrix sel = StepSelector() Xt_actual = sel.fit(sparse(X)).transform(sparse(X)) Xt_actual2 = sel.fit_transform(sparse(X)) assert_array_equal(Xt, Xt_actual.toarray()) assert_array_equal(Xt, Xt_actual2.toarray()) # Check dtype matches assert_equal(np.int32, sel.transform(sparse(X).astype(np.int32)).dtype) assert_equal(np.float32, sel.transform(sparse(X).astype(np.float32)).dtype) # Check wrong shape raises error assert_raises(ValueError, sel.transform, np.array([[1], [2]])) def test_inverse_transform_dense(): sel = StepSelector() Xinv_actual = sel.fit(X, y).inverse_transform(Xt) assert_array_equal(Xinv, Xinv_actual) # Check dtype matches assert_equal(np.int32, sel.inverse_transform(Xt.astype(np.int32)).dtype) assert_equal(np.float32, sel.inverse_transform(Xt.astype(np.float32)).dtype) # Check 1d list and other dtype: names_inv_actual = sel.inverse_transform([feature_names_t]) assert_array_equal(feature_names_inv, names_inv_actual.ravel()) # Check wrong shape raises error assert_raises(ValueError, sel.inverse_transform, np.array([[1], [2]])) def test_inverse_transform_sparse(): sparse = sp.csc_matrix sel = StepSelector() Xinv_actual = sel.fit(sparse(X)).inverse_transform(sparse(Xt)) assert_array_equal(Xinv, Xinv_actual.toarray()) # Check dtype matches assert_equal(np.int32, sel.inverse_transform(sparse(Xt).astype(np.int32)).dtype) assert_equal(np.float32, sel.inverse_transform(sparse(Xt).astype(np.float32)).dtype) # Check wrong shape raises error assert_raises(ValueError, sel.inverse_transform, np.array([[1], [2]])) def test_get_support(): sel = StepSelector() sel.fit(X, y) assert_array_equal(support, sel.get_support()) assert_array_equal(support_inds, sel.get_support(indices=True))
bsd-3-clause
costypetrisor/scikit-learn
sklearn/gaussian_process/tests/test_gaussian_process.py
267
6813
""" Testing for Gaussian Process module (sklearn.gaussian_process) """ # Author: Vincent Dubourg <vincent.dubourg@gmail.com> # Licence: BSD 3 clause from nose.tools import raises from nose.tools import assert_true import numpy as np from sklearn.gaussian_process import GaussianProcess from sklearn.gaussian_process import regression_models as regression from sklearn.gaussian_process import correlation_models as correlation from sklearn.datasets import make_regression from sklearn.utils.testing import assert_greater f = lambda x: x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T X2 = np.atleast_2d([2., 4., 5.5, 6.5, 7.5]).T y = f(X).ravel() def test_1d(regr=regression.constant, corr=correlation.squared_exponential, random_start=10, beta0=None): # MLE estimation of a one-dimensional Gaussian Process model. # Check random start optimization. # Test the interpolating property. gp = GaussianProcess(regr=regr, corr=corr, beta0=beta0, theta0=1e-2, thetaL=1e-4, thetaU=1e-1, random_start=random_start, verbose=False).fit(X, y) y_pred, MSE = gp.predict(X, eval_MSE=True) y2_pred, MSE2 = gp.predict(X2, eval_MSE=True) assert_true(np.allclose(y_pred, y) and np.allclose(MSE, 0.) and np.allclose(MSE2, 0., atol=10)) def test_2d(regr=regression.constant, corr=correlation.squared_exponential, random_start=10, beta0=None): # MLE estimation of a two-dimensional Gaussian Process model accounting for # anisotropy. Check random start optimization. # Test the interpolating property. b, kappa, e = 5., .5, .1 g = lambda x: b - x[:, 1] - kappa * (x[:, 0] - e) ** 2. X = np.array([[-4.61611719, -6.00099547], [4.10469096, 5.32782448], [0.00000000, -0.50000000], [-6.17289014, -4.6984743], [1.3109306, -6.93271427], [-5.03823144, 3.10584743], [-2.87600388, 6.74310541], [5.21301203, 4.26386883]]) y = g(X).ravel() thetaL = [1e-4] * 2 thetaU = [1e-1] * 2 gp = GaussianProcess(regr=regr, corr=corr, beta0=beta0, theta0=[1e-2] * 2, thetaL=thetaL, thetaU=thetaU, random_start=random_start, verbose=False) gp.fit(X, y) y_pred, MSE = gp.predict(X, eval_MSE=True) assert_true(np.allclose(y_pred, y) and np.allclose(MSE, 0.)) eps = np.finfo(gp.theta_.dtype).eps assert_true(np.all(gp.theta_ >= thetaL - eps)) # Lower bounds of hyperparameters assert_true(np.all(gp.theta_ <= thetaU + eps)) # Upper bounds of hyperparameters def test_2d_2d(regr=regression.constant, corr=correlation.squared_exponential, random_start=10, beta0=None): # MLE estimation of a two-dimensional Gaussian Process model accounting for # anisotropy. Check random start optimization. # Test the GP interpolation for 2D output b, kappa, e = 5., .5, .1 g = lambda x: b - x[:, 1] - kappa * (x[:, 0] - e) ** 2. f = lambda x: np.vstack((g(x), g(x))).T X = np.array([[-4.61611719, -6.00099547], [4.10469096, 5.32782448], [0.00000000, -0.50000000], [-6.17289014, -4.6984743], [1.3109306, -6.93271427], [-5.03823144, 3.10584743], [-2.87600388, 6.74310541], [5.21301203, 4.26386883]]) y = f(X) gp = GaussianProcess(regr=regr, corr=corr, beta0=beta0, theta0=[1e-2] * 2, thetaL=[1e-4] * 2, thetaU=[1e-1] * 2, random_start=random_start, verbose=False) gp.fit(X, y) y_pred, MSE = gp.predict(X, eval_MSE=True) assert_true(np.allclose(y_pred, y) and np.allclose(MSE, 0.)) @raises(ValueError) def test_wrong_number_of_outputs(): gp = GaussianProcess() gp.fit([[1, 2, 3], [4, 5, 6]], [1, 2, 3]) def test_more_builtin_correlation_models(random_start=1): # Repeat test_1d and test_2d for several built-in correlation # models specified as strings. all_corr = ['absolute_exponential', 'squared_exponential', 'cubic', 'linear'] for corr in all_corr: test_1d(regr='constant', corr=corr, random_start=random_start) test_2d(regr='constant', corr=corr, random_start=random_start) test_2d_2d(regr='constant', corr=corr, random_start=random_start) def test_ordinary_kriging(): # Repeat test_1d and test_2d with given regression weights (beta0) for # different regression models (Ordinary Kriging). test_1d(regr='linear', beta0=[0., 0.5]) test_1d(regr='quadratic', beta0=[0., 0.5, 0.5]) test_2d(regr='linear', beta0=[0., 0.5, 0.5]) test_2d(regr='quadratic', beta0=[0., 0.5, 0.5, 0.5, 0.5, 0.5]) test_2d_2d(regr='linear', beta0=[0., 0.5, 0.5]) test_2d_2d(regr='quadratic', beta0=[0., 0.5, 0.5, 0.5, 0.5, 0.5]) def test_no_normalize(): gp = GaussianProcess(normalize=False).fit(X, y) y_pred = gp.predict(X) assert_true(np.allclose(y_pred, y)) def test_random_starts(): # Test that an increasing number of random-starts of GP fitting only # increases the reduced likelihood function of the optimal theta. n_samples, n_features = 50, 3 np.random.seed(0) rng = np.random.RandomState(0) X = rng.randn(n_samples, n_features) * 2 - 1 y = np.sin(X).sum(axis=1) + np.sin(3 * X).sum(axis=1) best_likelihood = -np.inf for random_start in range(1, 5): gp = GaussianProcess(regr="constant", corr="squared_exponential", theta0=[1e-0] * n_features, thetaL=[1e-4] * n_features, thetaU=[1e+1] * n_features, random_start=random_start, random_state=0, verbose=False).fit(X, y) rlf = gp.reduced_likelihood_function()[0] assert_greater(rlf, best_likelihood - np.finfo(np.float32).eps) best_likelihood = rlf def test_mse_solving(): # test the MSE estimate to be sane. # non-regression test for ignoring off-diagonals of feature covariance, # testing with nugget that renders covariance useless, only # using the mean function, with low effective rank of data gp = GaussianProcess(corr='absolute_exponential', theta0=1e-4, thetaL=1e-12, thetaU=1e-2, nugget=1e-2, optimizer='Welch', regr="linear", random_state=0) X, y = make_regression(n_informative=3, n_features=60, noise=50, random_state=0, effective_rank=1) gp.fit(X, y) assert_greater(1000, gp.predict(X, eval_MSE=True)[1].mean())
bsd-3-clause
alvarofierroclavero/scikit-learn
sklearn/kernel_ridge.py
155
6545
"""Module :mod:`sklearn.kernel_ridge` implements kernel ridge regression.""" # Authors: Mathieu Blondel <mathieu@mblondel.org> # Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # License: BSD 3 clause import numpy as np from .base import BaseEstimator, RegressorMixin from .metrics.pairwise import pairwise_kernels from .linear_model.ridge import _solve_cholesky_kernel from .utils import check_X_y from .utils.validation import check_is_fitted class KernelRidge(BaseEstimator, RegressorMixin): """Kernel ridge regression. Kernel ridge regression (KRR) combines ridge regression (linear least squares with l2-norm regularization) with the kernel trick. It thus learns a linear function in the space induced by the respective kernel and the data. For non-linear kernels, this corresponds to a non-linear function in the original space. The form of the model learned by KRR is identical to support vector regression (SVR). However, different loss functions are used: KRR uses squared error loss while support vector regression uses epsilon-insensitive loss, both combined with l2 regularization. In contrast to SVR, fitting a KRR model can be done in closed-form and is typically faster for medium-sized datasets. On the other hand, the learned model is non-sparse and thus slower than SVR, which learns a sparse model for epsilon > 0, at prediction-time. This estimator has built-in support for multi-variate regression (i.e., when y is a 2d-array of shape [n_samples, n_targets]). Read more in the :ref:`User Guide <kernel_ridge>`. Parameters ---------- alpha : {float, array-like}, shape = [n_targets] Small positive values of alpha improve the conditioning of the problem and reduce the variance of the estimates. Alpha corresponds to ``(2*C)^-1`` in other linear models such as LogisticRegression or LinearSVC. If an array is passed, penalties are assumed to be specific to the targets. Hence they must correspond in number. kernel : string or callable, default="linear" Kernel mapping used internally. A callable should accept two arguments and the keyword arguments passed to this object as kernel_params, and should return a floating point number. gamma : float, default=None Gamma parameter for the RBF, polynomial, exponential chi2 and sigmoid kernels. Interpretation of the default value is left to the kernel; see the documentation for sklearn.metrics.pairwise. Ignored by other kernels. degree : float, default=3 Degree of the polynomial kernel. Ignored by other kernels. coef0 : float, default=1 Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels. kernel_params : mapping of string to any, optional Additional parameters (keyword arguments) for kernel function passed as callable object. Attributes ---------- dual_coef_ : array, shape = [n_features] or [n_targets, n_features] Weight vector(s) in kernel space X_fit_ : {array-like, sparse matrix}, shape = [n_samples, n_features] Training data, which is also required for prediction References ---------- * Kevin P. Murphy "Machine Learning: A Probabilistic Perspective", The MIT Press chapter 14.4.3, pp. 492-493 See also -------- Ridge Linear ridge regression. SVR Support Vector Regression implemented using libsvm. Examples -------- >>> from sklearn.kernel_ridge import KernelRidge >>> import numpy as np >>> n_samples, n_features = 10, 5 >>> rng = np.random.RandomState(0) >>> y = rng.randn(n_samples) >>> X = rng.randn(n_samples, n_features) >>> clf = KernelRidge(alpha=1.0) >>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE KernelRidge(alpha=1.0, coef0=1, degree=3, gamma=None, kernel='linear', kernel_params=None) """ def __init__(self, alpha=1, kernel="linear", gamma=None, degree=3, coef0=1, kernel_params=None): self.alpha = alpha self.kernel = kernel self.gamma = gamma self.degree = degree self.coef0 = coef0 self.kernel_params = kernel_params def _get_kernel(self, X, Y=None): if callable(self.kernel): params = self.kernel_params or {} else: params = {"gamma": self.gamma, "degree": self.degree, "coef0": self.coef0} return pairwise_kernels(X, Y, metric=self.kernel, filter_params=True, **params) @property def _pairwise(self): return self.kernel == "precomputed" def fit(self, X, y=None, sample_weight=None): """Fit Kernel Ridge regression model Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training data y : array-like, shape = [n_samples] or [n_samples, n_targets] Target values sample_weight : float or numpy array of shape [n_samples] Individual weights for each sample, ignored if None is passed. Returns ------- self : returns an instance of self. """ # Convert data X, y = check_X_y(X, y, accept_sparse=("csr", "csc"), multi_output=True, y_numeric=True) K = self._get_kernel(X) alpha = np.atleast_1d(self.alpha) ravel = False if len(y.shape) == 1: y = y.reshape(-1, 1) ravel = True copy = self.kernel == "precomputed" self.dual_coef_ = _solve_cholesky_kernel(K, y, alpha, sample_weight, copy) if ravel: self.dual_coef_ = self.dual_coef_.ravel() self.X_fit_ = X return self def predict(self, X): """Predict using the the kernel ridge model Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Samples. Returns ------- C : array, shape = [n_samples] or [n_samples, n_targets] Returns predicted values. """ check_is_fitted(self, ["X_fit_", "dual_coef_"]) K = self._get_kernel(X, self.X_fit_) return np.dot(K, self.dual_coef_)
bsd-3-clause
jcasner/nupic
external/linux32/lib/python2.6/site-packages/matplotlib/path.py
69
20263
""" Contains a class for managing paths (polylines). """ import math from weakref import WeakValueDictionary import numpy as np from numpy import ma from matplotlib._path import point_in_path, get_path_extents, \ point_in_path_collection, get_path_collection_extents, \ path_in_path, path_intersects_path, convert_path_to_polygons from matplotlib.cbook import simple_linear_interpolation class Path(object): """ :class:`Path` represents a series of possibly disconnected, possibly closed, line and curve segments. The underlying storage is made up of two parallel numpy arrays: - *vertices*: an Nx2 float array of vertices - *codes*: an N-length uint8 array of vertex types These two arrays always have the same length in the first dimension. For example, to represent a cubic curve, you must provide three vertices as well as three codes ``CURVE3``. The code types are: - ``STOP`` : 1 vertex (ignored) A marker for the end of the entire path (currently not required and ignored) - ``MOVETO`` : 1 vertex Pick up the pen and move to the given vertex. - ``LINETO`` : 1 vertex Draw a line from the current position to the given vertex. - ``CURVE3`` : 1 control point, 1 endpoint Draw a quadratic Bezier curve from the current position, with the given control point, to the given end point. - ``CURVE4`` : 2 control points, 1 endpoint Draw a cubic Bezier curve from the current position, with the given control points, to the given end point. - ``CLOSEPOLY`` : 1 vertex (ignored) Draw a line segment to the start point of the current polyline. Users of Path objects should not access the vertices and codes arrays directly. Instead, they should use :meth:`iter_segments` to get the vertex/code pairs. This is important, since many :class:`Path` objects, as an optimization, do not store a *codes* at all, but have a default one provided for them by :meth:`iter_segments`. Note also that the vertices and codes arrays should be treated as immutable -- there are a number of optimizations and assumptions made up front in the constructor that will not change when the data changes. """ # Path codes STOP = 0 # 1 vertex MOVETO = 1 # 1 vertex LINETO = 2 # 1 vertex CURVE3 = 3 # 2 vertices CURVE4 = 4 # 3 vertices CLOSEPOLY = 5 # 1 vertex NUM_VERTICES = [1, 1, 1, 2, 3, 1] code_type = np.uint8 def __init__(self, vertices, codes=None): """ Create a new path with the given vertices and codes. *vertices* is an Nx2 numpy float array, masked array or Python sequence. *codes* is an N-length numpy array or Python sequence of type :attr:`matplotlib.path.Path.code_type`. These two arrays must have the same length in the first dimension. If *codes* is None, *vertices* will be treated as a series of line segments. If *vertices* contains masked values, they will be converted to NaNs which are then handled correctly by the Agg PathIterator and other consumers of path data, such as :meth:`iter_segments`. """ if ma.isMaskedArray(vertices): vertices = vertices.astype(np.float_).filled(np.nan) else: vertices = np.asarray(vertices, np.float_) if codes is not None: codes = np.asarray(codes, self.code_type) assert codes.ndim == 1 assert len(codes) == len(vertices) assert vertices.ndim == 2 assert vertices.shape[1] == 2 self.should_simplify = (len(vertices) >= 128 and (codes is None or np.all(codes <= Path.LINETO))) self.has_nonfinite = not np.isfinite(vertices).all() self.codes = codes self.vertices = vertices #@staticmethod def make_compound_path(*args): """ (staticmethod) Make a compound path from a list of Path objects. Only polygons (not curves) are supported. """ for p in args: assert p.codes is None lengths = [len(x) for x in args] total_length = sum(lengths) vertices = np.vstack([x.vertices for x in args]) vertices.reshape((total_length, 2)) codes = Path.LINETO * np.ones(total_length) i = 0 for length in lengths: codes[i] = Path.MOVETO i += length return Path(vertices, codes) make_compound_path = staticmethod(make_compound_path) def __repr__(self): return "Path(%s, %s)" % (self.vertices, self.codes) def __len__(self): return len(self.vertices) def iter_segments(self, simplify=None): """ Iterates over all of the curve segments in the path. Each iteration returns a 2-tuple (*vertices*, *code*), where *vertices* is a sequence of 1 - 3 coordinate pairs, and *code* is one of the :class:`Path` codes. If *simplify* is provided, it must be a tuple (*width*, *height*) defining the size of the figure, in native units (e.g. pixels or points). Simplification implies both removing adjacent line segments that are very close to parallel, and removing line segments outside of the figure. The path will be simplified *only* if :attr:`should_simplify` is True, which is determined in the constructor by this criteria: - No curves - More than 128 vertices """ vertices = self.vertices if not len(vertices): return codes = self.codes len_vertices = len(vertices) isfinite = np.isfinite NUM_VERTICES = self.NUM_VERTICES MOVETO = self.MOVETO LINETO = self.LINETO CLOSEPOLY = self.CLOSEPOLY STOP = self.STOP if simplify is not None and self.should_simplify: polygons = self.to_polygons(None, *simplify) for vertices in polygons: yield vertices[0], MOVETO for v in vertices[1:]: yield v, LINETO elif codes is None: if self.has_nonfinite: next_code = MOVETO for v in vertices: if np.isfinite(v).all(): yield v, next_code next_code = LINETO else: next_code = MOVETO else: yield vertices[0], MOVETO for v in vertices[1:]: yield v, LINETO else: i = 0 was_nan = False while i < len_vertices: code = codes[i] if code == CLOSEPOLY: yield [], code i += 1 elif code == STOP: return else: num_vertices = NUM_VERTICES[int(code)] curr_vertices = vertices[i:i+num_vertices].flatten() if not isfinite(curr_vertices).all(): was_nan = True elif was_nan: yield curr_vertices[-2:], MOVETO was_nan = False else: yield curr_vertices, code i += num_vertices def transformed(self, transform): """ Return a transformed copy of the path. .. seealso:: :class:`matplotlib.transforms.TransformedPath`: A specialized path class that will cache the transformed result and automatically update when the transform changes. """ return Path(transform.transform(self.vertices), self.codes) def contains_point(self, point, transform=None): """ Returns *True* if the path contains the given point. If *transform* is not *None*, the path will be transformed before performing the test. """ if transform is not None: transform = transform.frozen() return point_in_path(point[0], point[1], self, transform) def contains_path(self, path, transform=None): """ Returns *True* if this path completely contains the given path. If *transform* is not *None*, the path will be transformed before performing the test. """ if transform is not None: transform = transform.frozen() return path_in_path(self, None, path, transform) def get_extents(self, transform=None): """ Returns the extents (*xmin*, *ymin*, *xmax*, *ymax*) of the path. Unlike computing the extents on the *vertices* alone, this algorithm will take into account the curves and deal with control points appropriately. """ from transforms import Bbox if transform is not None: transform = transform.frozen() return Bbox(get_path_extents(self, transform)) def intersects_path(self, other, filled=True): """ Returns *True* if this path intersects another given path. *filled*, when True, treats the paths as if they were filled. That is, if one path completely encloses the other, :meth:`intersects_path` will return True. """ return path_intersects_path(self, other, filled) def intersects_bbox(self, bbox, filled=True): """ Returns *True* if this path intersects a given :class:`~matplotlib.transforms.Bbox`. *filled*, when True, treats the path as if it was filled. That is, if one path completely encloses the other, :meth:`intersects_path` will return True. """ from transforms import BboxTransformTo rectangle = self.unit_rectangle().transformed( BboxTransformTo(bbox)) result = self.intersects_path(rectangle, filled) return result def interpolated(self, steps): """ Returns a new path resampled to length N x steps. Does not currently handle interpolating curves. """ vertices = simple_linear_interpolation(self.vertices, steps) codes = self.codes if codes is not None: new_codes = Path.LINETO * np.ones(((len(codes) - 1) * steps + 1, )) new_codes[0::steps] = codes else: new_codes = None return Path(vertices, new_codes) def to_polygons(self, transform=None, width=0, height=0): """ Convert this path to a list of polygons. Each polygon is an Nx2 array of vertices. In other words, each polygon has no ``MOVETO`` instructions or curves. This is useful for displaying in backends that do not support compound paths or Bezier curves, such as GDK. If *width* and *height* are both non-zero then the lines will be simplified so that vertices outside of (0, 0), (width, height) will be clipped. """ if len(self.vertices) == 0: return [] if transform is not None: transform = transform.frozen() if self.codes is None and (width == 0 or height == 0): if transform is None: return [self.vertices] else: return [transform.transform(self.vertices)] # Deal with the case where there are curves and/or multiple # subpaths (using extension code) return convert_path_to_polygons(self, transform, width, height) _unit_rectangle = None #@classmethod def unit_rectangle(cls): """ (staticmethod) Returns a :class:`Path` of the unit rectangle from (0, 0) to (1, 1). """ if cls._unit_rectangle is None: cls._unit_rectangle = \ Path([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]) return cls._unit_rectangle unit_rectangle = classmethod(unit_rectangle) _unit_regular_polygons = WeakValueDictionary() #@classmethod def unit_regular_polygon(cls, numVertices): """ (staticmethod) Returns a :class:`Path` for a unit regular polygon with the given *numVertices* and radius of 1.0, centered at (0, 0). """ if numVertices <= 16: path = cls._unit_regular_polygons.get(numVertices) else: path = None if path is None: theta = (2*np.pi/numVertices * np.arange(numVertices + 1).reshape((numVertices + 1, 1))) # This initial rotation is to make sure the polygon always # "points-up" theta += np.pi / 2.0 verts = np.concatenate((np.cos(theta), np.sin(theta)), 1) path = Path(verts) cls._unit_regular_polygons[numVertices] = path return path unit_regular_polygon = classmethod(unit_regular_polygon) _unit_regular_stars = WeakValueDictionary() #@classmethod def unit_regular_star(cls, numVertices, innerCircle=0.5): """ (staticmethod) Returns a :class:`Path` for a unit regular star with the given numVertices and radius of 1.0, centered at (0, 0). """ if numVertices <= 16: path = cls._unit_regular_stars.get((numVertices, innerCircle)) else: path = None if path is None: ns2 = numVertices * 2 theta = (2*np.pi/ns2 * np.arange(ns2 + 1)) # This initial rotation is to make sure the polygon always # "points-up" theta += np.pi / 2.0 r = np.ones(ns2 + 1) r[1::2] = innerCircle verts = np.vstack((r*np.cos(theta), r*np.sin(theta))).transpose() path = Path(verts) cls._unit_regular_polygons[(numVertices, innerCircle)] = path return path unit_regular_star = classmethod(unit_regular_star) #@classmethod def unit_regular_asterisk(cls, numVertices): """ (staticmethod) Returns a :class:`Path` for a unit regular asterisk with the given numVertices and radius of 1.0, centered at (0, 0). """ return cls.unit_regular_star(numVertices, 0.0) unit_regular_asterisk = classmethod(unit_regular_asterisk) _unit_circle = None #@classmethod def unit_circle(cls): """ (staticmethod) Returns a :class:`Path` of the unit circle. The circle is approximated using cubic Bezier curves. This uses 8 splines around the circle using the approach presented here: Lancaster, Don. `Approximating a Circle or an Ellipse Using Four Bezier Cubic Splines <http://www.tinaja.com/glib/ellipse4.pdf>`_. """ if cls._unit_circle is None: MAGIC = 0.2652031 SQRTHALF = np.sqrt(0.5) MAGIC45 = np.sqrt((MAGIC*MAGIC) / 2.0) vertices = np.array( [[0.0, -1.0], [MAGIC, -1.0], [SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45], [SQRTHALF, -SQRTHALF], [SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45], [1.0, -MAGIC], [1.0, 0.0], [1.0, MAGIC], [SQRTHALF+MAGIC45, SQRTHALF-MAGIC45], [SQRTHALF, SQRTHALF], [SQRTHALF-MAGIC45, SQRTHALF+MAGIC45], [MAGIC, 1.0], [0.0, 1.0], [-MAGIC, 1.0], [-SQRTHALF+MAGIC45, SQRTHALF+MAGIC45], [-SQRTHALF, SQRTHALF], [-SQRTHALF-MAGIC45, SQRTHALF-MAGIC45], [-1.0, MAGIC], [-1.0, 0.0], [-1.0, -MAGIC], [-SQRTHALF-MAGIC45, -SQRTHALF+MAGIC45], [-SQRTHALF, -SQRTHALF], [-SQRTHALF+MAGIC45, -SQRTHALF-MAGIC45], [-MAGIC, -1.0], [0.0, -1.0], [0.0, -1.0]], np.float_) codes = cls.CURVE4 * np.ones(26) codes[0] = cls.MOVETO codes[-1] = cls.CLOSEPOLY cls._unit_circle = Path(vertices, codes) return cls._unit_circle unit_circle = classmethod(unit_circle) #@classmethod def arc(cls, theta1, theta2, n=None, is_wedge=False): """ (staticmethod) Returns an arc on the unit circle from angle *theta1* to angle *theta2* (in degrees). If *n* is provided, it is the number of spline segments to make. If *n* is not provided, the number of spline segments is determined based on the delta between *theta1* and *theta2*. Masionobe, L. 2003. `Drawing an elliptical arc using polylines, quadratic or cubic Bezier curves <http://www.spaceroots.org/documents/ellipse/index.html>`_. """ # degrees to radians theta1 *= np.pi / 180.0 theta2 *= np.pi / 180.0 twopi = np.pi * 2.0 halfpi = np.pi * 0.5 eta1 = np.arctan2(np.sin(theta1), np.cos(theta1)) eta2 = np.arctan2(np.sin(theta2), np.cos(theta2)) eta2 -= twopi * np.floor((eta2 - eta1) / twopi) if (theta2 - theta1 > np.pi) and (eta2 - eta1 < np.pi): eta2 += twopi # number of curve segments to make if n is None: n = int(2 ** np.ceil((eta2 - eta1) / halfpi)) if n < 1: raise ValueError("n must be >= 1 or None") deta = (eta2 - eta1) / n t = np.tan(0.5 * deta) alpha = np.sin(deta) * (np.sqrt(4.0 + 3.0 * t * t) - 1) / 3.0 steps = np.linspace(eta1, eta2, n + 1, True) cos_eta = np.cos(steps) sin_eta = np.sin(steps) xA = cos_eta[:-1] yA = sin_eta[:-1] xA_dot = -yA yA_dot = xA xB = cos_eta[1:] yB = sin_eta[1:] xB_dot = -yB yB_dot = xB if is_wedge: length = n * 3 + 4 vertices = np.zeros((length, 2), np.float_) codes = Path.CURVE4 * np.ones((length, ), Path.code_type) vertices[1] = [xA[0], yA[0]] codes[0:2] = [Path.MOVETO, Path.LINETO] codes[-2:] = [Path.LINETO, Path.CLOSEPOLY] vertex_offset = 2 end = length - 2 else: length = n * 3 + 1 vertices = np.zeros((length, 2), np.float_) codes = Path.CURVE4 * np.ones((length, ), Path.code_type) vertices[0] = [xA[0], yA[0]] codes[0] = Path.MOVETO vertex_offset = 1 end = length vertices[vertex_offset :end:3, 0] = xA + alpha * xA_dot vertices[vertex_offset :end:3, 1] = yA + alpha * yA_dot vertices[vertex_offset+1:end:3, 0] = xB - alpha * xB_dot vertices[vertex_offset+1:end:3, 1] = yB - alpha * yB_dot vertices[vertex_offset+2:end:3, 0] = xB vertices[vertex_offset+2:end:3, 1] = yB return Path(vertices, codes) arc = classmethod(arc) #@classmethod def wedge(cls, theta1, theta2, n=None): """ (staticmethod) Returns a wedge of the unit circle from angle *theta1* to angle *theta2* (in degrees). If *n* is provided, it is the number of spline segments to make. If *n* is not provided, the number of spline segments is determined based on the delta between *theta1* and *theta2*. """ return cls.arc(theta1, theta2, n, True) wedge = classmethod(wedge) _get_path_collection_extents = get_path_collection_extents def get_path_collection_extents(*args): """ Given a sequence of :class:`Path` objects, returns the bounding box that encapsulates all of them. """ from transforms import Bbox if len(args[1]) == 0: raise ValueError("No paths provided") return Bbox.from_extents(*_get_path_collection_extents(*args))
agpl-3.0
Loisel/tmr3
tmr.py
1
15096
#!/usr/bin/python """ A module to calculate the current, the conductance and the TMR from a set of rate arrays. The rate arrays are supposed to be stored in a h5 file in the job directory. The result is stored in a h5 file. The name of the dataset contains all parameters. They are also stored as attributes in the dataset. The conductance in the two lead configurations (parallel/anti-parallel) are stored in arrays in the dataset. Usage: ./tmr.py <jobname> """ import numpy as np from numpy import linalg import time import sys import getopt import h5py import os # We are picky about possible floating point overflows # to avoid calculating NaNs np.seterr(divide="raise") np.seterr(invalid="raise") # A helper module to calculate the populations. import pop # The configuration module import cfg # path to the dat directory datpath = "dat/" # name of the temporary file where the rates are stored ratefile = "running_calc.h5" # name of the h5 file to store the conductance for the two configuration # and the configuraion parameters. hdffile = "simdata_new.h5" def save_hdf5(fname,G_P,G_AP): """ Store the conductance and the configuration to the h5 file. Args: fname: filename of the h5 file G_P: the conductance for leads with parallel magnetization G_AP: the conductance for leads with anti-parallel magnetization """ print "Shape of GP {}".format(G_P.shape) fileh = h5py.File(fname,"a") # Note that the selection of parameters to construct the name of the # dataset should be chosen such that this string is unique! # That is, it should contain all running parameters. dset_name = "G={}_kbT={}_Ec={}_E0={}_Pol={}_PolOrb={}_SO={}_tau={}_DS={}_B_P={}_B_AP={}_B_ORB_P={}_B_ORB_AP={}_W_e={}_W_0={}".format(cfg.conf['G_scale'],cfg.conf['kBT'],cfg.conf['E_C'],cfg.conf['E_0'],cfg.conf['Pol'],cfg.conf['OrbPol'],cfg.conf['SO'],cfg.conf['tau_r'],cfg.conf['D_S_factor'],cfg.conf['B_P'],cfg.conf['B_AP'],cfg.conf['B_ORB_P'],cfg.conf['B_ORB_AP'],cfg.conf['W_E'],cfg.conf['W_0']) try: # we create the dataset dset = fileh.create_dataset(dset_name,data=np.vstack((G_P,G_AP))) # and store the config attributes dset.attrs['alpha'] = cfg.conf['ALPHA'] dset.attrs['temperature'] = cfg.conf['kBT'] dset.attrs['coupling'] = cfg.conf['G_scale'] dset.attrs['electron_number'] = cfg.conf['N_0'] dset.attrs['charging_energy'] = cfg.conf['E_C'] dset.attrs['level_spacing'] = cfg.conf['E_0'] dset.attrs['polarization_spin'] = cfg.conf['Pol'] dset.attrs['polarization_orbit'] = cfg.conf['OrbPol'] dset.attrs['spinorbit'] = cfg.conf['SO'] dset.attrs['stonershift'] = cfg.conf['D_S_factor'] dset.attrs['tau_r'] = cfg.conf['tau_r'] dset.attrs['vg_min'] = cfg.conf['V_g_min'] dset.attrs['vg_max'] = cfg.conf['V_g_max'] dset.attrs['b_p'] = cfg.conf['B_P'] dset.attrs['b_ap'] = cfg.conf['B_AP'] dset.attrs['b_orb_p'] = cfg.conf['B_ORB_P'] dset.attrs['b_orb_ap'] = cfg.conf['B_ORB_AP'] dset.attrs['w_0'] = cfg.conf['W_0'] dset.attrs['w_e'] = cfg.conf['W_E'] dset.attrs['timestamp'] = time.time() except KeyError: # If the choice was not unique we complain but continue. print "Dataset exists." fileh.close() def eval_DENKER(GM,GP,configuration): """ Evaluate the density matrix kernel using the in- and out-tunneling rates. Args: GM,GP: numpy arrays containing in- and out-tunneling rates in the order of cfg.TLIST. configuration: integer determining parallel (0) or anti-parallel(1) configuration Returns: the density matrix as a square 2-d numpy array that is NP**2 in size, where NP is the number of states in the groundstatespace. """ # we get a view on the transition list and, for simplicity, its transpose TLIST = cfg.TLIST[configuration] TLIST_T = np.transpose(TLIST) # from all transitions we extract all groundstates in the statespace # this is probably a complicated way to do it PLIST = list(set(TLIST_T[0]).union(TLIST_T[1])) # ... and sort it by index PLIST.sort() # the number of groundstates NP = len(PLIST) # let's create an empty density matrix ME = np.zeros((NP,NP)) # we create a version of the transition list that does not contain # the indices in terms of the energy array (see cfg.py), but # in terms of the number in the state list (plist) # (the transition list can then be used to denote non-zero matrix elements) TMP = np.copy(TLIST) for idx,val in enumerate(PLIST): TMP[TLIST == val] = idx # We calculate diagonal elements of the density matrix: # TLIST_T[1] == num selects the correct in-tunneling rates for the # state with label num # have a look at numpy.where to understand this line for idx,num in enumerate(PLIST): ME[idx,idx] = -np.sum(np.where(TLIST_T[1] == num,GP,0.)) - np.sum(np.where(TLIST_T[0] == num,GM,0.)) # for the off diagonal elements we can directly use the generated TMP # transition list for k,tup in enumerate(TMP): ME[tup[0],tup[1]] = GP[k] ME[tup[1],tup[0]] = GM[k] # print "tup: {} and matrix element {}".format(tup,ME[tuple(tup)]) return ME def eval_CURKER(GM,GP,configuration): """ Evaluate the current kernel using the in- and out-tunneling rates. Args: GM,GP: numpy arrays containing in- and out-tunneling rates in the order of cfg.TLIST. configuration: integer determining parallel (0) or anti-parallel(1) configuration Returns: the current kernel as a 1-d numpy array. """ # We get a view on the transition list and its transpose TLIST = cfg.TLIST[configuration] TLIST_T = np.transpose(TLIST) # ... and extract the list of groundstates (see also eval_DENKER) PLIST = list(set(TLIST_T[0]).union(TLIST_T[1])) PLIST.sort() # this determines the size of the statespace NP = len(PLIST) CUR = np.zeros(NP) # Note that the current kernel can be calculated by summing the diagonal elements # of the density matrix with opposite sign # compare eval_DENKER for idx,num in enumerate(PLIST): CUR[idx] = np.sum(np.where(TLIST_T[1] == num,GP,0.)) - np.sum(np.where(TLIST_T[0] == num,GM,0.)) return CUR def current(GP,GM,POP,configuration): """ Calculate the current using the rates and populations. Args: GP, GM: np-arrays containing in- and out-tunneling rates. POP: np-array for the populations configuration: integer determining parallel (0) or anti-parallel(1) configuration Returns: current as a float. """ # We calculate the current kernel CURKER = eval_CURKER(GM,GP,configuration) # and vector-multiply it with the population vector I = -np.sum(cfg.conf["ELE"]*np.dot( CURKER, POP)) return I def eval_tmr(fname,plotname,pop): """ Calculates the TMR by evaluating conductance through parallel and anti-parallel polarized contacts. Args: fname: the h5 file to load the rates from. plotname: A name for the pdf output to produce. pop: If True, we plot the populations, too. """ # We prepare the current and conductance vectors for different # values of gate and bias voltage C_p = np.zeros((cfg.conf['NV'],cfg.conf['NVb'])) C_ap = np.zeros((cfg.conf['NV'],cfg.conf['NVb'])) G_p = np.zeros((cfg.conf['NV'],cfg.conf['NVb']-1)) G_ap = np.zeros((cfg.conf['NV'],cfg.conf['NVb']-1)) dVb = cfg.conf['Vb_range'][1]- cfg.conf['Vb_range'][0] # the population vectors, for all values of gate and bias POP_p = np.zeros((cfg.conf['NVb'],cfg.conf['NV'],cfg.N_GS[0])) POP_ap = np.zeros((cfg.conf['NVb'],cfg.conf['NV'],cfg.N_GS[1])) # We iterate over two bias values first for nV,Vb in enumerate(cfg.conf["Vb_range"]): # now the rates are loaded from the h5 file # note that the label of the specific rate arrays are fixed with h5py.File(fname) as file: GP0_p = np.array(file['par_P0_V{}'.format(Vb)]) GP0_ap = np.array(file['apa_P0_V{}'.format(Vb)]) GP1_p = np.array(file['par_P1_V{}'.format(Vb)]) GP1_ap = np.array(file['apa_P1_V{}'.format(Vb)]) GM0_p = np.array(file['par_M0_V{}'.format(Vb)]) GM0_ap = np.array(file['apa_M0_V{}'.format(Vb)]) GM1_p = np.array(file['par_M1_V{}'.format(Vb)]) GM1_ap = np.array(file['apa_M1_V{}'.format(Vb)]) # for the density kernel, we sum all rates over both leads DENKER_p = np.array([eval_DENKER(GM0_p[n]+GM1_p[n],GP0_p[n]+GP1_p[n],0)for n in range(cfg.conf["NV"])]) DENKER_ap = np.array([eval_DENKER(GM0_ap[n]+GM1_ap[n],GP0_ap[n]+GP1_ap[n],1)for n in range(cfg.conf["NV"])]) # the populations are calculated from the density kernel by an asymptotic # approximation scheme POP_ap[nV] = np.array([pop.asymptotic_ssp(DENKER_ap[n]) for n in range(cfg.conf["NV"])]) POP_p[nV] = np.array([pop.asymptotic_ssp(DENKER_p[n]) for n in range(cfg.conf["NV"])]) # note that the current is calculated from the rates in one of the leads only C_p[:,nV] = np.array([ current(GP0_p[n],GM0_p[n],POP_p[nV,n],0) for n in np.arange(cfg.conf["NV"]) ]) C_ap[:,nV] = np.array([ current(GP0_ap[n],GM0_ap[n],POP_ap[nV,n],1) for n in np.arange(cfg.conf["NV"]) ]) # the numerical derivative gives the conductance G_p = np.diff(C_p).flatten()/dVb G_ap = np.diff(C_ap).flatten()/dVb # we save the conductance traces to a h5 file specified as a global variable # hdffile in the path datpath # It is possible that the dataset already exists. In this case, we issue a warning. try: save_hdf5("{}{}".format(datpath,hdffile),G_p,G_ap) except RuntimeError: print "Unable to save to {}, maybe there is already a dataset with similar parameters...".format(hdffile) # the tmr and conductance graphs are plotted to a pdf file for review. plot_tmr_pdf(G_p,G_ap,plotname) # if the pop flag is set, we also plot the population for one bias value if pop: plot_population([POP_p[0],POP_ap[0]],os.path.splitext(plotname)[0]+"_POP.pdf") def plot_tmr_pdf(C_p,C_ap,fname): """ A helper routine to plot the conductance and TMR to a pdf file in the datpath. Args: C_p, C_ap: the parallel and anti-parallel conductance. fname: the filename to plot to """ import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt # we plot the conductance graph on top, p and ap with different colors Axes1 = plt.subplot(2,1,1) Axes1.set_xticklabels([]) plt.ylabel("Conductance (e^2/h)") plt.title("Conductance at zero bias") # parallel is plotted in red, and anti-parallel as blue dashed line plt.plot( cfg.conf["V_g"],C_p,'r',cfg.conf["V_g"], C_ap, 'b--') # on the second panel, the TMR is plotted Axes2 = plt.subplot(2,1,2) plt.xlabel("gate voltage (V)") plt.ylabel("TMR") plt.title("TMR") plt.ylim((-0.3,1.5)) TMR = np.zeros(cfg.conf["NV"]) for i in range(cfg.conf["NV"]): try: TMR[i] = C_p[i]/C_ap[i]-1. except ZeroDivisionError: print "Zero Division, returning null." TMR[i] = 0. plt.plot( cfg.conf["V_g"], TMR) plt.savefig(fname, bbox_inches='tight') def plot_population(POP, fname): """ Calculates and plots selected populations of the quantum dot with gate voltage. The edge states N=-1 and 5 are neglected. Args: POP: a list with the two population vectors for parallel and anti-parallel configurations fname: the filename to plot to """ import matplotlib.pyplot as plt NV = cfg.conf["NV"] print "Calculating populations..." # We plot the populations for both configurations # the parallel populations on top # the anti-parallel on bottom Ax = [plt.subplot(2,1,1),plt.subplot(2,1,2)] cm = plt.get_cmap('gist_rainbow') PopPlots = [1,4,8,12,17,18] NP = len(PopPlots) for gamidx in range(2): TLIST = cfg.TLIST[gamidx] TLIST_T = np.transpose(TLIST) PLIST = list(set(TLIST_T[0]).union(TLIST_T[1])) PLIST.sort() # we cycle through the linecolors to distinguish the different # groundstates Ax[gamidx].set_color_cycle([cm(1.*k/NP) for k in range(NP)]) for i in PopPlots: color = cm(1.*i/NP) LABEL = "P_{}".format(cfg.int_to_state(PLIST[i])) Ax[gamidx].plot( cfg.conf["V_g"], POP[gamidx][:,i],label=LABEL) lines =Ax[gamidx].get_lines() labels = [l.get_label() for l in lines] leg = plt.figlegend(lines,labels,loc='upper right') plt.savefig(fname) plt.show() class Usage(Exception): def __init__(self, msg): self.msg = msg def main(argv=None): """ Interface routine to call the tmr module. Example: ./tmr.py <jobname> In principle, there were routines to plot rates, populations, conductances etc. but apart from the population plotting, none of the use cases was needed anymore. """ POP = False # The default config file is called cnt.conf cfile = "cnt.conf" rlist = [0.,] if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "hc:P", ["help","config=","pop"]) except getopt.error, msg: raise Usage(msg) for o,a in opts: if o in ('-h','--help'): usage() exit() elif o in ('-c','--config'): cfile = a elif o in ('-P','--pop'): POP = True else: raise Usage('Invalid argument.') # we parse the config and initialize it cfg.parse_conf("dat/{0}/{1}".format(args[0],cfile)) cfg.init() h5file = "{}{}/{}".format(datpath,args[0],ratefile) pdffile = "{}{}.pdf".format(datpath,args[0]) print "Try to open {}".format(h5file) eval_tmr(h5file,pdffile,POP) except Usage, err: print >>sys.stderr, err.msg print >>sys.stderr, "for help use --help" return 2 def usage(): print "This is a tool to process rate files.\n\ \n\ usage: tmr.py [-hP] [--pop] jobname\n\ \n\ --pop or -P: Plot the populations.\n\ \n\ jobname: The name of the directory for the rate files.\n\ \n\ The script searches for files dat/jobname/running_calc.h5\n\ and dat/jobname/cnt.conf" if __name__ == "__main__": sys.exit(main())
gpl-3.0
mlperf/training_results_v0.7
NVIDIA/benchmarks/minigo/implementations/tensorflow/minigo/oneoffs/training_curve.py
8
5964
# Copyright 2018 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Used to plot the accuracy of the policy and value networks in predicting professional game moves and results over the course of training. Check FLAGS for default values for what models to load and what sgf files to parse. Usage: python training_curve.py Sample 3 positions from each game python training_curve.py --num_positions=3 Only grab games after 2005 (default is 2000) python training_curve.py --min_year=2005 """ import sys sys.path.insert(0, '.') import os.path import numpy as np import matplotlib.pyplot as plt import pandas as pd from absl import app, flags from tqdm import tqdm import coords from rl_loop import fsdb import oneoff_utils flags.DEFINE_string("sgf_dir", None, "sgf database") flags.DEFINE_string("plot_dir", "data", "Where to save the plots.") flags.DEFINE_integer("min_year", "2000", "Only take sgf games with date >= min_year") flags.DEFINE_string("komi", "7.5", "Only take sgf games with given komi") flags.DEFINE_integer("idx_start", 150, "Only take models after given idx") flags.DEFINE_integer("num_positions", 1, "How many positions from each game to sample from.") flags.DEFINE_integer("eval_every", 5, "Eval every k models to generate the curve") flags.mark_flag_as_required('sgf_dir') FLAGS = flags.FLAGS def batch_run_many(player, positions, batch_size=100): """Used to avoid a memory oveflow issue when running the network on too many positions. TODO: This should be a member function of player.network?""" prob_list = [] value_list = [] for idx in range(0, len(positions), batch_size): probs, values = player.network.run_many(positions[idx:idx + batch_size]) prob_list.append(probs) value_list.append(values) return np.concatenate(prob_list, axis=0), np.concatenate(value_list, axis=0) def eval_player(player, positions, moves, results): probs, values = batch_run_many(player, positions) policy_moves = [coords.from_flat(c) for c in np.argmax(probs, axis=1)] top_move_agree = [moves[idx] == policy_moves[idx] for idx in range(len(moves))] square_err = (values - results) ** 2 / 4 return top_move_agree, square_err def sample_positions_from_games(sgf_files, num_positions=1): pos_data = [] move_data = [] result_data = [] move_idxs = [] fail_count = 0 for path in tqdm(sgf_files, desc="loading sgfs", unit="games"): try: positions, moves, results = oneoff_utils.parse_sgf_to_examples(path) except KeyboardInterrupt: raise except Exception as e: print("Parse exception:", e) fail_count += 1 continue # add entire game if num_positions == -1: pos_data.extend(positions) move_data.extend(moves) move_idxs.extend(range(len(positions))) result_data.extend(results) else: for idx in np.random.choice(len(positions), num_positions): pos_data.append(positions[idx]) move_data.append(moves[idx]) result_data.append(results[idx]) move_idxs.append(idx) print("Sampled {} positions, failed to parse {} files".format( len(pos_data), fail_count)) return pos_data, move_data, result_data, move_idxs def get_training_curve_data( model_dir, pos_data, move_data, result_data, idx_start, eval_every): model_paths = oneoff_utils.get_model_paths(model_dir) df = pd.DataFrame() player = None print("Evaluating models {}-{}, eval_every={}".format( idx_start, len(model_paths), eval_every)) for idx in tqdm(range(idx_start, len(model_paths), eval_every)): if player: oneoff_utils.restore_params(model_paths[idx], player) else: player = oneoff_utils.load_player(model_paths[idx]) correct, squared_errors = eval_player( player=player, positions=pos_data, moves=move_data, results=result_data) avg_acc = np.mean(correct) avg_mse = np.mean(squared_errors) print("Model: {}, acc: {:.4f}, mse: {:.4f}".format( model_paths[idx], avg_acc, avg_mse)) df = df.append({"num": idx, "acc": avg_acc, "mse": avg_mse}, ignore_index=True) return df def save_plots(data_dir, df): plt.plot(df["num"], df["acc"]) plt.xlabel("Model idx") plt.ylabel("Accuracy") plt.title("Accuracy in Predicting Professional Moves") plot_path = os.path.join(data_dir, "move_acc.pdf") plt.savefig(plot_path) plt.figure() plt.plot(df["num"], df["mse"]) plt.xlabel("Model idx") plt.ylabel("MSE/4") plt.title("MSE in predicting outcome") plot_path = os.path.join(data_dir, "value_mse.pdf") plt.savefig(plot_path) def main(unusedargv): sgf_files = oneoff_utils.find_and_filter_sgf_files( FLAGS.sgf_dir, FLAGS.min_year, FLAGS.komi) pos_data, move_data, result_data, move_idxs = sample_positions_from_games( sgf_files=sgf_files, num_positions=FLAGS.num_positions) df = get_training_curve_data(fsdb.models_dir(), pos_data, move_data, result_data, FLAGS.idx_start, FLAGS.eval_every) save_plots(FLAGS.plot_dir, df) if __name__ == "__main__": app.run(main)
apache-2.0
rhattersley/cartopy
lib/cartopy/tests/mpl/test_ticker.py
3
8574
# (C) British Crown Copyright 2014 - 2017, Met Office # # This file is part of cartopy. # # cartopy is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the # Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # cartopy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with cartopy. If not, see <https://www.gnu.org/licenses/>. from __future__ import (absolute_import, division, print_function) try: from unittest.mock import Mock except ImportError: from mock import Mock from matplotlib.axes import Axes import pytest import cartopy.crs as ccrs from cartopy.mpl.geoaxes import GeoAxes from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter def test_LatitudeFormatter_bad_axes(): formatter = LatitudeFormatter() formatter.axis = Mock(axes=Mock(Axes, projection=ccrs.PlateCarree())) message = 'This formatter can only be used with cartopy axes.' with pytest.raises(TypeError, message=message): formatter(0) def test_LatitudeFormatter_bad_projection(): formatter = LatitudeFormatter() formatter.axis = Mock(axes=Mock(GeoAxes, projection=ccrs.Orthographic())) message = 'This formatter cannot be used with non-rectangular projections.' with pytest.raises(TypeError, message=message): formatter(0) def test_LongitudeFormatter_bad_axes(): formatter = LongitudeFormatter() formatter.axis = Mock(axes=Mock(Axes, projection=ccrs.PlateCarree())) message = 'This formatter can only be used with cartopy axes.' with pytest.raises(TypeError, message=message): formatter(0) def test_LongitudeFormatter_bad_projection(): formatter = LongitudeFormatter() formatter.axis = Mock(axes=Mock(GeoAxes, projection=ccrs.Orthographic())) message = 'This formatter cannot be used with non-rectangular projections.' with pytest.raises(TypeError, message=message): formatter(0) def test_LatitudeFormatter(): formatter = LatitudeFormatter() p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-90, -60, -30, 0, 30, 60, 90] result = [formatter(tick) for tick in test_ticks] expected = [u'90\u00B0S', u'60\u00B0S', u'30\u00B0S', u'0\u00B0', u'30\u00B0N', u'60\u00B0N', u'90\u00B0N'] assert result == expected def test_LatitudeFormatter_degree_symbol(): formatter = LatitudeFormatter(degree_symbol='') p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-90, -60, -30, 0, 30, 60, 90] result = [formatter(tick) for tick in test_ticks] expected = [u'90S', u'60S', u'30S', u'0', u'30N', u'60N', u'90N'] assert result == expected def test_LatitudeFormatter_number_format(): formatter = LatitudeFormatter(number_format='.2f') p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-90, -60, -30, 0, 30, 60, 90] result = [formatter(tick) for tick in test_ticks] expected = [u'90.00\u00B0S', u'60.00\u00B0S', u'30.00\u00B0S', u'0.00\u00B0', u'30.00\u00B0N', u'60.00\u00B0N', u'90.00\u00B0N'] assert result == expected def test_LatitudeFormatter_mercator(): formatter = LatitudeFormatter() p = ccrs.Mercator() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-15496570.739707904, -8362698.548496634, -3482189.085407435, 0.0, 3482189.085407435, 8362698.548496634, 15496570.739707898] result = [formatter(tick) for tick in test_ticks] expected = [u'80\u00B0S', u'60\u00B0S', u'30\u00B0S', u'0\u00B0', u'30\u00B0N', u'60\u00B0N', u'80\u00B0N'] assert result == expected def test_LatitudeFormatter_small_numbers(): formatter = LatitudeFormatter(number_format='.7f') p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [40.1275150, 40.1275152, 40.1275154] result = [formatter(tick) for tick in test_ticks] expected = [u'40.1275150\u00B0N', u'40.1275152\u00B0N', u'40.1275154\u00B0N'] assert result == expected def test_LongitudeFormatter_central_longitude_0(): formatter = LongitudeFormatter(dateline_direction_label=True) p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-180, -120, -60, 0, 60, 120, 180] result = [formatter(tick) for tick in test_ticks] expected = [u'180\u00B0W', u'120\u00B0W', u'60\u00B0W', u'0\u00B0', u'60\u00B0E', u'120\u00B0E', u'180\u00B0E'] assert result == expected def test_LongitudeFormatter_central_longitude_180(): formatter = LongitudeFormatter(zero_direction_label=True) p = ccrs.PlateCarree(central_longitude=180) formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-180, -120, -60, 0, 60, 120, 180] result = [formatter(tick) for tick in test_ticks] expected = [u'0\u00B0E', u'60\u00B0E', u'120\u00B0E', u'180\u00B0', u'120\u00B0W', u'60\u00B0W', u'0\u00B0W'] assert result == expected def test_LongitudeFormatter_central_longitude_120(): formatter = LongitudeFormatter() p = ccrs.PlateCarree(central_longitude=120) formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-180, -120, -60, 0, 60, 120, 180] result = [formatter(tick) for tick in test_ticks] expected = [u'60\u00B0W', u'0\u00B0', u'60\u00B0E', u'120\u00B0E', u'180\u00B0', u'120\u00B0W', u'60\u00B0W'] assert result == expected def test_LongitudeFormatter_degree_symbol(): formatter = LongitudeFormatter(degree_symbol='', dateline_direction_label=True) p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-180, -120, -60, 0, 60, 120, 180] result = [formatter(tick) for tick in test_ticks] expected = [u'180W', u'120W', u'60W', u'0', u'60E', u'120E', u'180E'] assert result == expected def test_LongitudeFormatter_number_format(): formatter = LongitudeFormatter(number_format='.2f', dateline_direction_label=True) p = ccrs.PlateCarree() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-180, -120, -60, 0, 60, 120, 180] result = [formatter(tick) for tick in test_ticks] expected = [u'180.00\u00B0W', u'120.00\u00B0W', u'60.00\u00B0W', u'0.00\u00B0', u'60.00\u00B0E', u'120.00\u00B0E', u'180.00\u00B0E'] assert result == expected def test_LongitudeFormatter_mercator(): formatter = LongitudeFormatter(dateline_direction_label=True) p = ccrs.Mercator() formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-20037508.342783064, -13358338.895188706, -6679169.447594353, 0.0, 6679169.447594353, 13358338.895188706, 20037508.342783064] result = [formatter(tick) for tick in test_ticks] expected = [u'180\u00B0W', u'120\u00B0W', u'60\u00B0W', u'0\u00B0', u'60\u00B0E', u'120\u00B0E', u'180\u00B0E'] assert result == expected def test_LongitudeFormatter_small_numbers_0(): formatter = LongitudeFormatter(number_format='.7f') p = ccrs.PlateCarree(central_longitude=0) formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-17.1142343, -17.1142340, -17.1142337] result = [formatter(tick) for tick in test_ticks] expected = [u'17.1142343\u00B0W', u'17.1142340\u00B0W', u'17.1142337\u00B0W'] assert result == expected def test_LongitudeFormatter_small_numbers_180(): formatter = LongitudeFormatter(zero_direction_label=True, number_format='.7f') p = ccrs.PlateCarree(central_longitude=180) formatter.axis = Mock(axes=Mock(GeoAxes, projection=p)) test_ticks = [-17.1142343, -17.1142340, -17.1142337] result = [formatter(tick) for tick in test_ticks] expected = [u'162.8857657\u00B0E', u'162.8857660\u00B0E', u'162.8857663\u00B0E'] assert result == expected
lgpl-3.0
nolanliou/tensorflow
tensorflow/contrib/losses/python/metric_learning/metric_loss_ops_test.py
41
20535
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for triplet_semihard_loss.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from tensorflow.contrib.losses.python import metric_learning as metric_loss_ops from tensorflow.python.framework import ops from tensorflow.python.framework import sparse_tensor from tensorflow.python.ops import math_ops from tensorflow.python.ops import nn from tensorflow.python.platform import test try: # pylint: disable=g-import-not-at-top from sklearn import datasets from sklearn import metrics HAS_SKLEARN = True except ImportError: HAS_SKLEARN = False def pairwise_distance_np(feature, squared=False): """Computes the pairwise distance matrix in numpy. Args: feature: 2-D numpy array of size [number of data, feature dimension] squared: Boolean. If true, output is the pairwise squared euclidean distance matrix; else, output is the pairwise euclidean distance matrix. Returns: pairwise_distances: 2-D numpy array of size [number of data, number of data]. """ triu = np.triu_indices(feature.shape[0], 1) upper_tri_pdists = np.linalg.norm(feature[triu[1]] - feature[triu[0]], axis=1) if squared: upper_tri_pdists **= 2. num_data = feature.shape[0] pairwise_distances = np.zeros((num_data, num_data)) pairwise_distances[np.triu_indices(num_data, 1)] = upper_tri_pdists # Make symmetrical. pairwise_distances = pairwise_distances + pairwise_distances.T - np.diag( pairwise_distances.diagonal()) return pairwise_distances class ContrastiveLossTest(test.TestCase): def testContrastive(self): with self.test_session(): num_data = 10 feat_dim = 6 margin = 1.0 embeddings_anchor = np.random.rand(num_data, feat_dim).astype(np.float32) embeddings_positive = np.random.rand(num_data, feat_dim).astype( np.float32) labels = np.random.randint(0, 2, size=(num_data,)).astype(np.float32) # Compute the loss in NP dist = np.sqrt( np.sum(np.square(embeddings_anchor - embeddings_positive), axis=1)) loss_np = np.mean( labels * np.square(dist) + (1.0 - labels) * np.square(np.maximum(margin - dist, 0.0))) # Compute the loss with TF loss_tf = metric_loss_ops.contrastive_loss( labels=ops.convert_to_tensor(labels), embeddings_anchor=ops.convert_to_tensor(embeddings_anchor), embeddings_positive=ops.convert_to_tensor(embeddings_positive), margin=margin) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) class TripletSemiHardLossTest(test.TestCase): def testTripletSemiHard(self): with self.test_session(): num_data = 10 feat_dim = 6 margin = 1.0 num_classes = 4 embedding = np.random.rand(num_data, feat_dim).astype(np.float32) labels = np.random.randint( 0, num_classes, size=(num_data)).astype(np.float32) # Reshape labels to compute adjacency matrix. labels_reshaped = np.reshape(labels, (labels.shape[0], 1)) # Compute the loss in NP. adjacency = np.equal(labels_reshaped, labels_reshaped.T) pdist_matrix = pairwise_distance_np(embedding, squared=True) loss_np = 0.0 num_positives = 0.0 for i in range(num_data): for j in range(num_data): if adjacency[i][j] > 0.0 and i != j: num_positives += 1.0 pos_distance = pdist_matrix[i][j] neg_distances = [] for k in range(num_data): if adjacency[i][k] == 0: neg_distances.append(pdist_matrix[i][k]) # Sort by distance. neg_distances.sort() chosen_neg_distance = neg_distances[0] for l in range(len(neg_distances)): chosen_neg_distance = neg_distances[l] if chosen_neg_distance > pos_distance: break loss_np += np.maximum( 0.0, margin - chosen_neg_distance + pos_distance) loss_np /= num_positives # Compute the loss in TF. loss_tf = metric_loss_ops.triplet_semihard_loss( labels=ops.convert_to_tensor(labels), embeddings=ops.convert_to_tensor(embedding), margin=margin) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) class LiftedStructLossTest(test.TestCase): def testLiftedStruct(self): with self.test_session(): num_data = 10 feat_dim = 6 margin = 1.0 num_classes = 4 embedding = np.random.rand(num_data, feat_dim).astype(np.float32) labels = np.random.randint( 0, num_classes, size=(num_data)).astype(np.float32) # Reshape labels to compute adjacency matrix. labels_reshaped = np.reshape(labels, (labels.shape[0], 1)) # Compute the loss in NP adjacency = np.equal(labels_reshaped, labels_reshaped.T) pdist_matrix = pairwise_distance_np(embedding) loss_np = 0.0 num_constraints = 0.0 for i in range(num_data): for j in range(num_data): if adjacency[i][j] > 0.0 and i != j: d_pos = pdist_matrix[i][j] negs = [] for k in range(num_data): if not adjacency[i][k]: negs.append(margin - pdist_matrix[i][k]) for l in range(num_data): if not adjacency[j][l]: negs.append(margin - pdist_matrix[j][l]) negs = np.array(negs) max_elem = np.max(negs) negs -= max_elem negs = np.exp(negs) soft_maximum = np.log(np.sum(negs)) + max_elem num_constraints += 1.0 this_loss = max(soft_maximum + d_pos, 0) loss_np += this_loss * this_loss loss_np = loss_np / num_constraints / 2.0 # Compute the loss in TF loss_tf = metric_loss_ops.lifted_struct_loss( labels=ops.convert_to_tensor(labels), embeddings=ops.convert_to_tensor(embedding), margin=margin) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) def convert_to_list_of_sparse_tensor(np_matrix): list_of_sparse_tensors = [] nrows, ncols = np_matrix.shape for i in range(nrows): sp_indices = [] for j in range(ncols): if np_matrix[i][j] == 1: sp_indices.append([j]) num_non_zeros = len(sp_indices) list_of_sparse_tensors.append(sparse_tensor.SparseTensor( indices=np.array(sp_indices), values=np.ones((num_non_zeros,)), dense_shape=np.array([ncols,]))) return list_of_sparse_tensors class NpairsLossTest(test.TestCase): def testNpairs(self): with self.test_session(): num_data = 15 feat_dim = 6 num_classes = 5 reg_lambda = 0.02 embeddings_anchor = np.random.rand(num_data, feat_dim).astype(np.float32) embeddings_positive = np.random.rand(num_data, feat_dim).astype( np.float32) labels = np.random.randint( 0, num_classes, size=(num_data)).astype(np.float32) # Reshape labels to compute adjacency matrix. labels_reshaped = np.reshape(labels, (labels.shape[0], 1)) # Compute the loss in NP reg_term = np.mean(np.sum(np.square(embeddings_anchor), 1)) reg_term += np.mean(np.sum(np.square(embeddings_positive), 1)) reg_term *= 0.25 * reg_lambda similarity_matrix = np.matmul(embeddings_anchor, embeddings_positive.T) labels_remapped = np.equal( labels_reshaped, labels_reshaped.T).astype(np.float32) labels_remapped /= np.sum(labels_remapped, axis=1, keepdims=True) xent_loss = math_ops.reduce_mean(nn.softmax_cross_entropy_with_logits( logits=ops.convert_to_tensor(similarity_matrix), labels=ops.convert_to_tensor(labels_remapped))).eval() loss_np = xent_loss + reg_term # Compute the loss in TF loss_tf = metric_loss_ops.npairs_loss( labels=ops.convert_to_tensor(labels), embeddings_anchor=ops.convert_to_tensor(embeddings_anchor), embeddings_positive=ops.convert_to_tensor(embeddings_positive), reg_lambda=reg_lambda) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) class NpairsLossMultiLabelTest(test.TestCase): def testNpairsMultiLabelLossWithSingleLabelEqualsNpairsLoss(self): with self.test_session(): num_data = 15 feat_dim = 6 reg_lambda = 0.02 embeddings_anchor = np.random.rand(num_data, feat_dim).astype(np.float32) embeddings_positive = np.random.rand(num_data, feat_dim).astype( np.float32) labels = np.arange(num_data) labels = np.reshape(labels, -1) # Compute vanila npairs loss. loss_npairs = metric_loss_ops.npairs_loss( labels=ops.convert_to_tensor(labels), embeddings_anchor=ops.convert_to_tensor(embeddings_anchor), embeddings_positive=ops.convert_to_tensor(embeddings_positive), reg_lambda=reg_lambda).eval() # Compute npairs multilabel loss. labels_one_hot = np.identity(num_data) loss_npairs_multilabel = metric_loss_ops.npairs_loss_multilabel( sparse_labels=convert_to_list_of_sparse_tensor(labels_one_hot), embeddings_anchor=ops.convert_to_tensor(embeddings_anchor), embeddings_positive=ops.convert_to_tensor(embeddings_positive), reg_lambda=reg_lambda).eval() self.assertAllClose(loss_npairs, loss_npairs_multilabel) def testNpairsMultiLabel(self): with self.test_session(): num_data = 15 feat_dim = 6 num_classes = 10 reg_lambda = 0.02 embeddings_anchor = np.random.rand(num_data, feat_dim).astype(np.float32) embeddings_positive = np.random.rand(num_data, feat_dim).astype( np.float32) labels = np.random.randint(0, 2, (num_data, num_classes)) # set entire column to one so that each row has at least one bit set. labels[:, -1] = 1 # Compute the loss in NP reg_term = np.mean(np.sum(np.square(embeddings_anchor), 1)) reg_term += np.mean(np.sum(np.square(embeddings_positive), 1)) reg_term *= 0.25 * reg_lambda similarity_matrix = np.matmul(embeddings_anchor, embeddings_positive.T) labels_remapped = np.dot(labels, labels.T).astype(np.float) labels_remapped /= np.sum(labels_remapped, 1, keepdims=True) xent_loss = math_ops.reduce_mean(nn.softmax_cross_entropy_with_logits( logits=ops.convert_to_tensor(similarity_matrix), labels=ops.convert_to_tensor(labels_remapped))).eval() loss_np = xent_loss + reg_term # Compute the loss in TF loss_tf = metric_loss_ops.npairs_loss_multilabel( sparse_labels=convert_to_list_of_sparse_tensor(labels), embeddings_anchor=ops.convert_to_tensor(embeddings_anchor), embeddings_positive=ops.convert_to_tensor(embeddings_positive), reg_lambda=reg_lambda) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) def compute_ground_truth_cluster_score(feat, y): y_unique = np.unique(y) score_gt_np = 0.0 for c in y_unique: feat_subset = feat[y == c, :] pdist_subset = pairwise_distance_np(feat_subset) score_gt_np += -1.0 * np.min(np.sum(pdist_subset, axis=0)) score_gt_np = score_gt_np.astype(np.float32) return score_gt_np def compute_cluster_loss_numpy(feat, y, margin_multiplier=1.0, enable_pam_finetuning=True): if enable_pam_finetuning: facility = ForwardGreedyFacility( n_clusters=np.unique(y).size).pam_augmented_fit(feat, y, margin_multiplier) else: facility = ForwardGreedyFacility( n_clusters=np.unique(y).size).loss_augmented_fit(feat, y, margin_multiplier) score_augmented = facility.score_aug_ score_gt = compute_ground_truth_cluster_score(feat, y) return np.maximum(np.float32(0.0), score_augmented - score_gt) class ForwardGreedyFacility(object): def __init__(self, n_clusters=8): self.n_clusters = n_clusters self.center_ics_ = None def _check_init_args(self): # Check n_clusters. if (self.n_clusters is None or self.n_clusters <= 0 or not isinstance(self.n_clusters, int)): raise ValueError('n_clusters has to be nonnegative integer.') def loss_augmented_fit(self, feat, y, loss_mult): """Fit K-Medoids to the provided data.""" self._check_init_args() # Check that the array is good and attempt to convert it to # Numpy array if possible. feat = self._check_array(feat) # Apply distance metric to get the distance matrix. pdists = pairwise_distance_np(feat) num_data = feat.shape[0] candidate_ids = list(range(num_data)) candidate_scores = np.zeros(num_data,) subset = [] k = 0 while k < self.n_clusters: candidate_scores = [] for i in candidate_ids: # push i to subset. subset.append(i) marginal_cost = -1.0 * np.sum(np.min(pdists[:, subset], axis=1)) loss = 1.0 - metrics.normalized_mutual_info_score( y, self._get_cluster_ics(pdists, subset)) candidate_scores.append(marginal_cost + loss_mult * loss) # remove i from subset. subset.pop() # push i_star to subset. i_star = candidate_ids[np.argmax(candidate_scores)] subset.append(i_star) # remove i_star from candidate indices. candidate_ids.remove(i_star) k += 1 # Expose labels_ which are the assignments of # the training data to clusters. self.labels_ = self._get_cluster_ics(pdists, subset) # Expose cluster centers, i.e. medoids. self.cluster_centers_ = feat.take(subset, axis=0) # Expose indices of chosen cluster centers. self.center_ics_ = subset # Expose the score = -\sum_{i \in V} min_{j \in S} || x_i - x_j || self.score_ = np.float32(-1.0) * self._get_facility_distance(pdists, subset) self.score_aug_ = self.score_ + loss_mult * ( 1.0 - metrics.normalized_mutual_info_score( y, self._get_cluster_ics(pdists, subset))) self.score_aug_ = self.score_aug_.astype(np.float32) # Expose the chosen cluster indices. self.subset_ = subset return self def _augmented_update_medoid_ics_in_place(self, pdists, y_gt, cluster_ics, medoid_ics, loss_mult): for cluster_idx in range(self.n_clusters): # y_pred = self._get_cluster_ics(D, medoid_ics) # Don't prematurely do the assignment step. # Do this after we've updated all cluster medoids. y_pred = cluster_ics if sum(y_pred == cluster_idx) == 0: # Cluster is empty. continue curr_score = ( -1.0 * np.sum( pdists[medoid_ics[cluster_idx], y_pred == cluster_idx]) + loss_mult * (1.0 - metrics.normalized_mutual_info_score( y_gt, y_pred))) pdist_in = pdists[y_pred == cluster_idx, :] pdist_in = pdist_in[:, y_pred == cluster_idx] all_scores_fac = np.sum(-1.0 * pdist_in, axis=1) all_scores_loss = [] for i in range(y_pred.size): if y_pred[i] != cluster_idx: continue # remove this cluster's current centroid medoid_ics_i = medoid_ics[:cluster_idx] + medoid_ics[cluster_idx + 1:] # add this new candidate to the centroid list medoid_ics_i += [i] y_pred_i = self._get_cluster_ics(pdists, medoid_ics_i) all_scores_loss.append(loss_mult * ( 1.0 - metrics.normalized_mutual_info_score(y_gt, y_pred_i))) all_scores = all_scores_fac + all_scores_loss max_score_idx = np.argmax(all_scores) max_score = all_scores[max_score_idx] if max_score > curr_score: medoid_ics[cluster_idx] = np.where( y_pred == cluster_idx)[0][max_score_idx] def pam_augmented_fit(self, feat, y, loss_mult): pam_max_iter = 5 self._check_init_args() feat = self._check_array(feat) pdists = pairwise_distance_np(feat) self.loss_augmented_fit(feat, y, loss_mult) print('PAM -1 (before PAM): score: %f, score_aug: %f' % ( self.score_, self.score_aug_)) # Initialize from loss augmented facility location subset = self.center_ics_ for iter_ in range(pam_max_iter): # update the cluster assignment cluster_ics = self._get_cluster_ics(pdists, subset) # update the medoid for each clusters self._augmented_update_medoid_ics_in_place(pdists, y, cluster_ics, subset, loss_mult) self.score_ = np.float32(-1.0) * self._get_facility_distance( pdists, subset) self.score_aug_ = self.score_ + loss_mult * ( 1.0 - metrics.normalized_mutual_info_score( y, self._get_cluster_ics(pdists, subset))) self.score_aug_ = self.score_aug_.astype(np.float32) print('PAM iter: %d, score: %f, score_aug: %f' % (iter_, self.score_, self.score_aug_)) self.center_ics_ = subset self.labels_ = cluster_ics return self def _check_array(self, feat): # Check that the number of clusters is less than or equal to # the number of samples if self.n_clusters > feat.shape[0]: raise ValueError('The number of medoids ' + '({}) '.format( self.n_clusters) + 'must be larger than the number ' + 'of samples ({})'.format(feat.shape[0])) return feat def _get_cluster_ics(self, pdists, subset): """Returns cluster indices for pdist and current medoid indices.""" # Assign data points to clusters based on # which cluster assignment yields # the smallest distance` cluster_ics = np.argmin(pdists[subset, :], axis=0) return cluster_ics def _get_facility_distance(self, pdists, subset): return np.sum(np.min(pdists[subset, :], axis=0)) class ClusterLossTest(test.TestCase): def _genClusters(self, n_samples, n_clusters): blobs = datasets.make_blobs( n_samples=n_samples, centers=n_clusters) embedding, labels = blobs embedding = (embedding - embedding.mean(axis=0)) / embedding.std(axis=0) embedding = embedding.astype(np.float32) return embedding, labels def testClusteringLossPAMOff(self): if not HAS_SKLEARN: return with self.test_session(): margin_multiplier = 10.0 embeddings, labels = self._genClusters(n_samples=128, n_clusters=64) loss_np = compute_cluster_loss_numpy( embeddings, labels, margin_multiplier, enable_pam_finetuning=False) loss_tf = metric_loss_ops.cluster_loss( labels=ops.convert_to_tensor(labels), embeddings=ops.convert_to_tensor(embeddings), margin_multiplier=margin_multiplier, enable_pam_finetuning=False) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) def testClusteringLossPAMOn(self): if not HAS_SKLEARN: return with self.test_session(): margin_multiplier = 10.0 embeddings, labels = self._genClusters(n_samples=128, n_clusters=64) loss_np = compute_cluster_loss_numpy( embeddings, labels, margin_multiplier, enable_pam_finetuning=True) loss_tf = metric_loss_ops.cluster_loss( labels=ops.convert_to_tensor(labels), embeddings=ops.convert_to_tensor(embeddings), margin_multiplier=margin_multiplier, enable_pam_finetuning=True) loss_tf = loss_tf.eval() self.assertAllClose(loss_np, loss_tf) if __name__ == '__main__': test.main()
apache-2.0
Jay-Jay-D/LeanSTP
Algorithm.Framework/Portfolio/MinimumVariancePortfolioOptimizer.py
3
4622
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. # Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import pandas as pd from scipy.optimize import minimize ### <summary> ### Provides an implementation of a portfolio optimizer that calculate the optimal weights ### with the weight range from -1 to 1 and minimize the portfolio variance with a target return of 2% ### </summary> class MinimumVariancePortfolioOptimizer: '''Provides an implementation of a portfolio optimizer that calculate the optimal weights with the weight range from -1 to 1 and minimize the portfolio variance with a target return of 2%''' def __init__(self, minimum_weight = -1, maximum_weight = 1, target_return = 0.02): '''Initialize the MinimumVariancePortfolioOptimizer Args: minimum_weight(float): The lower bounds on portfolio weights maximum_weight(float): The upper bounds on portfolio weights target_return(float): The target portfolio return''' self.minimum_weight = minimum_weight self.maximum_weight = maximum_weight self.target_return = target_return def Optimize(self, historicalReturns, expectedReturns = None, covariance = None): ''' Perform portfolio optimization for a provided matrix of historical returns and an array of expected returns args: historicalReturns: Matrix of annualized historical returns where each column represents a security and each row returns for the given date/time (size: K x N). expectedReturns: Array of double with the portfolio annualized expected returns (size: K x 1). covariance: Multi-dimensional array of double with the portfolio covariance of annualized returns (size: K x K). Returns: Array of double with the portfolio weights (size: K x 1) ''' if covariance is None: covariance = historicalReturns.cov() if expectedReturns is None: expectedReturns = historicalReturns.mean() size = historicalReturns.columns.size # K x 1 x0 = np.array(size * [1. / size]) constraints = [ {'type': 'eq', 'fun': lambda weights: self.get_budget_constraint(weights)}, {'type': 'eq', 'fun': lambda weights: self.get_target_constraint(weights, expectedReturns)}] opt = minimize(lambda weights: self.portfolio_variance(weights, covariance), # Objective function x0, # Initial guess bounds = self.get_boundary_conditions(size), # Bounds for variables constraints = constraints, # Constraints definition method='SLSQP') # Optimization method: Sequential Least SQuares Programming return opt['x'] def portfolio_variance(self, weights, covariance): '''Computes the portfolio variance Args: weighs: Portfolio weights covariance: Covariance matrix of historical returns''' variance = np.dot(weights.T, np.dot(covariance, weights)) if variance == 0: raise ValueError(f'MinimumVariancePortfolioOptimizer.portfolio_variance: Volatility cannot be zero. Weights: {weights}') return variance def get_boundary_conditions(self, size): '''Creates the boundary condition for the portfolio weights''' return tuple((self.minimum_weight, self.maximum_weight) for x in range(size)) def get_budget_constraint(self, weights): '''Defines a budget constraint: the sum of the weights equals unity''' return np.sum(weights) - 1 def get_target_constraint(self, weights, expectedReturns): '''Ensure that the portfolio return target a given return''' return np.dot(np.matrix(expectedReturns), np.matrix(weights).T).item() - self.target_return
apache-2.0
bgris/ODL_bgris
lib/python3.5/site-packages/matplotlib/patches.py
6
148732
# -*- coding: utf-8 -*- from __future__ import (absolute_import, division, print_function, unicode_literals) import six from six.moves import map, zip import math import matplotlib as mpl import numpy as np import matplotlib.cbook as cbook import matplotlib.artist as artist from matplotlib.artist import allow_rasterization import matplotlib.colors as colors from matplotlib import docstring import matplotlib.transforms as transforms from matplotlib.path import Path import matplotlib.lines as mlines from matplotlib.bezier import split_bezier_intersecting_with_closedpath from matplotlib.bezier import get_intersection, inside_circle, get_parallels from matplotlib.bezier import make_wedged_bezier2 from matplotlib.bezier import split_path_inout, get_cos_sin from matplotlib.bezier import make_path_regular, concatenate_paths # these are not available for the object inspector until after the # class is built so we define an initial set here for the init # function and they will be overridden after object definition docstring.interpd.update(Patch=""" ================= ============================================== Property Description ================= ============================================== alpha float animated [True | False] antialiased or aa [True | False] capstyle ['butt' | 'round' | 'projecting'] clip_box a matplotlib.transform.Bbox instance clip_on [True | False] edgecolor or ec any matplotlib color facecolor or fc any matplotlib color figure a matplotlib.figure.Figure instance fill [True | False] hatch unknown joinstyle ['miter' | 'round' | 'bevel'] label any string linewidth or lw float lod [True | False] transform a matplotlib.transform transformation instance visible [True | False] zorder any number ================= ============================================== """) _patch_alias_map = { 'antialiased': ['aa'], 'edgecolor': ['ec'], 'facecolor': ['fc'], 'linewidth': ['lw'], 'linestyle': ['ls'] } class Patch(artist.Artist): """ A patch is a 2D artist with a face color and an edge color. If any of *edgecolor*, *facecolor*, *linewidth*, or *antialiased* are *None*, they default to their rc params setting. """ zorder = 1 validCap = ('butt', 'round', 'projecting') validJoin = ('miter', 'round', 'bevel') # Whether to draw an edge by default. Set on a # subclass-by-subclass basis. _edge_default = False def __str__(self): return str(self.__class__).split('.')[-1] def __init__(self, edgecolor=None, facecolor=None, color=None, linewidth=None, linestyle=None, antialiased=None, hatch=None, fill=True, capstyle=None, joinstyle=None, **kwargs): """ The following kwarg properties are supported %(Patch)s """ artist.Artist.__init__(self) if linewidth is None: linewidth = mpl.rcParams['patch.linewidth'] if linestyle is None: linestyle = "solid" if capstyle is None: capstyle = 'butt' if joinstyle is None: joinstyle = 'miter' if antialiased is None: antialiased = mpl.rcParams['patch.antialiased'] self._fill = True # needed for set_facecolor call if color is not None: if (edgecolor is not None or facecolor is not None): import warnings warnings.warn("Setting the 'color' property will override" "the edgecolor or facecolor properties. ") self.set_color(color) else: self.set_edgecolor(edgecolor) self.set_facecolor(facecolor) # unscaled dashes. Needed to scale dash patterns by lw self._us_dashes = None self._linewidth = 0 self.set_fill(fill) self.set_linestyle(linestyle) self.set_linewidth(linewidth) self.set_antialiased(antialiased) self.set_hatch(hatch) self.set_capstyle(capstyle) self.set_joinstyle(joinstyle) self._combined_transform = transforms.IdentityTransform() if len(kwargs): self.update(kwargs) def get_verts(self): """ Return a copy of the vertices used in this patch If the patch contains Bezier curves, the curves will be interpolated by line segments. To access the curves as curves, use :meth:`get_path`. """ trans = self.get_transform() path = self.get_path() polygons = path.to_polygons(trans) if len(polygons): return polygons[0] return [] def _process_radius(self, radius): if radius is not None: return radius if cbook.is_numlike(self._picker): _radius = self._picker else: if self.get_edgecolor()[3] == 0: _radius = 0 else: _radius = self.get_linewidth() return _radius def contains(self, mouseevent, radius=None): """Test whether the mouse event occurred in the patch. Returns T/F, {} """ if six.callable(self._contains): return self._contains(self, mouseevent) radius = self._process_radius(radius) inside = self.get_path().contains_point( (mouseevent.x, mouseevent.y), self.get_transform(), radius) return inside, {} def contains_point(self, point, radius=None): """ Returns *True* if the given point is inside the path (transformed with its transform attribute). """ radius = self._process_radius(radius) return self.get_path().contains_point(point, self.get_transform(), radius) def update_from(self, other): """ Updates this :class:`Patch` from the properties of *other*. """ artist.Artist.update_from(self, other) # For some properties we don't need or don't want to go through the # getters/setters, so we just copy them directly. self._edgecolor = other._edgecolor self._facecolor = other._facecolor self._fill = other._fill self._hatch = other._hatch # copy the unscaled dash pattern self._us_dashes = other._us_dashes self.set_linewidth(other._linewidth) # also sets dash properties self.set_transform(other.get_data_transform()) def get_extents(self): """ Return a :class:`~matplotlib.transforms.Bbox` object defining the axis-aligned extents of the :class:`Patch`. """ return self.get_path().get_extents(self.get_transform()) def get_transform(self): """ Return the :class:`~matplotlib.transforms.Transform` applied to the :class:`Patch`. """ return self.get_patch_transform() + artist.Artist.get_transform(self) def get_data_transform(self): """ Return the :class:`~matplotlib.transforms.Transform` instance which maps data coordinates to physical coordinates. """ return artist.Artist.get_transform(self) def get_patch_transform(self): """ Return the :class:`~matplotlib.transforms.Transform` instance which takes patch coordinates to data coordinates. For example, one may define a patch of a circle which represents a radius of 5 by providing coordinates for a unit circle, and a transform which scales the coordinates (the patch coordinate) by 5. """ return transforms.IdentityTransform() def get_antialiased(self): """ Returns True if the :class:`Patch` is to be drawn with antialiasing. """ return self._antialiased get_aa = get_antialiased def get_edgecolor(self): """ Return the edge color of the :class:`Patch`. """ return self._edgecolor get_ec = get_edgecolor def get_facecolor(self): """ Return the face color of the :class:`Patch`. """ return self._facecolor get_fc = get_facecolor def get_linewidth(self): """ Return the line width in points. """ return self._linewidth get_lw = get_linewidth def get_linestyle(self): """ Return the linestyle. Will be one of ['solid' | 'dashed' | 'dashdot' | 'dotted'] """ return self._linestyle get_ls = get_linestyle def set_antialiased(self, aa): """ Set whether to use antialiased rendering ACCEPTS: [True | False] or None for default """ if aa is None: aa = mpl.rcParams['patch.antialiased'] self._antialiased = aa self.stale = True def set_aa(self, aa): """alias for set_antialiased""" return self.set_antialiased(aa) def _set_edgecolor(self, color): if color is None: if (mpl.rcParams['patch.force_edgecolor'] or not self._fill or self._edge_default): color = mpl.rcParams['patch.edgecolor'] else: color = 'none' self._edgecolor = colors.to_rgba(color, self._alpha) self.stale = True def set_edgecolor(self, color): """ Set the patch edge color ACCEPTS: mpl color spec, None, 'none', or 'auto' """ self._original_edgecolor = color self._set_edgecolor(color) def set_ec(self, color): """alias for set_edgecolor""" return self.set_edgecolor(color) def _set_facecolor(self, color): if color is None: color = mpl.rcParams['patch.facecolor'] alpha = self._alpha if self._fill else 0 self._facecolor = colors.to_rgba(color, alpha) self.stale = True def set_facecolor(self, color): """ Set the patch face color ACCEPTS: mpl color spec, or None for default, or 'none' for no color """ self._original_facecolor = color self._set_facecolor(color) def set_fc(self, color): """alias for set_facecolor""" return self.set_facecolor(color) def set_color(self, c): """ Set both the edgecolor and the facecolor. ACCEPTS: matplotlib color spec .. seealso:: :meth:`set_facecolor`, :meth:`set_edgecolor` For setting the edge or face color individually. """ self.set_facecolor(c) self.set_edgecolor(c) def set_alpha(self, alpha): """ Set the alpha tranparency of the patch. ACCEPTS: float or None """ if alpha is not None: try: float(alpha) except TypeError: raise TypeError('alpha must be a float or None') artist.Artist.set_alpha(self, alpha) self._set_facecolor(self._original_facecolor) self._set_edgecolor(self._original_edgecolor) # stale is already True def set_linewidth(self, w): """ Set the patch linewidth in points ACCEPTS: float or None for default """ if w is None: w = mpl.rcParams['patch.linewidth'] if w is None: w = mpl.rcParams['axes.linewidth'] self._linewidth = float(w) # scale the dash pattern by the linewidth offset, ls = self._us_dashes self._dashoffset, self._dashes = mlines._scale_dashes( offset, ls, self._linewidth) self.stale = True def set_lw(self, lw): """alias for set_linewidth""" return self.set_linewidth(lw) def set_linestyle(self, ls): """ Set the patch linestyle =========================== ================= linestyle description =========================== ================= ``'-'`` or ``'solid'`` solid line ``'--'`` or ``'dashed'`` dashed line ``'-.'`` or ``'dashdot'`` dash-dotted line ``':'`` or ``'dotted'`` dotted line =========================== ================= Alternatively a dash tuple of the following form can be provided:: (offset, onoffseq), where ``onoffseq`` is an even length tuple of on and off ink in points. ACCEPTS: ['solid' | 'dashed', 'dashdot', 'dotted' | (offset, on-off-dash-seq) | ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` | ``' '`` | ``''``] Parameters ---------- ls : { '-', '--', '-.', ':'} and more see description The line style. """ if ls is None: ls = "solid" self._linestyle = ls # get the unscalled dash pattern offset, ls = self._us_dashes = mlines._get_dash_pattern(ls) # scale the dash pattern by the linewidth self._dashoffset, self._dashes = mlines._scale_dashes( offset, ls, self._linewidth) self.stale = True def set_ls(self, ls): """alias for set_linestyle""" return self.set_linestyle(ls) def set_fill(self, b): """ Set whether to fill the patch ACCEPTS: [True | False] """ self._fill = bool(b) self._set_facecolor(self._original_facecolor) self._set_edgecolor(self._original_edgecolor) self.stale = True def get_fill(self): 'return whether fill is set' return self._fill # Make fill a property so as to preserve the long-standing # but somewhat inconsistent behavior in which fill was an # attribute. fill = property(get_fill, set_fill) def set_capstyle(self, s): """ Set the patch capstyle ACCEPTS: ['butt' | 'round' | 'projecting'] """ s = s.lower() if s not in self.validCap: raise ValueError('set_capstyle passed "%s";\n' % (s,) + 'valid capstyles are %s' % (self.validCap,)) self._capstyle = s self.stale = True def get_capstyle(self): "Return the current capstyle" return self._capstyle def set_joinstyle(self, s): """ Set the patch joinstyle ACCEPTS: ['miter' | 'round' | 'bevel'] """ s = s.lower() if s not in self.validJoin: raise ValueError('set_joinstyle passed "%s";\n' % (s,) + 'valid joinstyles are %s' % (self.validJoin,)) self._joinstyle = s self.stale = True def get_joinstyle(self): "Return the current joinstyle" return self._joinstyle def set_hatch(self, hatch): """ Set the hatching pattern *hatch* can be one of:: / - diagonal hatching \ - back diagonal | - vertical - - horizontal + - crossed x - crossed diagonal o - small circle O - large circle . - dots * - stars Letters can be combined, in which case all the specified hatchings are done. If same letter repeats, it increases the density of hatching of that pattern. Hatching is supported in the PostScript, PDF, SVG and Agg backends only. ACCEPTS: ['/' | '\\\\' | '|' | '-' | '+' | 'x' | 'o' | 'O' | '.' | '*'] """ self._hatch = hatch self.stale = True def get_hatch(self): 'Return the current hatching pattern' return self._hatch @allow_rasterization def draw(self, renderer): 'Draw the :class:`Patch` to the given *renderer*.' if not self.get_visible(): return renderer.open_group('patch', self.get_gid()) gc = renderer.new_gc() gc.set_foreground(self._edgecolor, isRGBA=True) lw = self._linewidth if self._edgecolor[3] == 0: lw = 0 gc.set_linewidth(lw) gc.set_dashes(0, self._dashes) gc.set_capstyle(self._capstyle) gc.set_joinstyle(self._joinstyle) gc.set_antialiased(self._antialiased) self._set_gc_clip(gc) gc.set_url(self._url) gc.set_snap(self.get_snap()) rgbFace = self._facecolor if rgbFace[3] == 0: rgbFace = None # (some?) renderers expect this as no-fill signal gc.set_alpha(self._alpha) if self._hatch: gc.set_hatch(self._hatch) if self.get_sketch_params() is not None: gc.set_sketch_params(*self.get_sketch_params()) path = self.get_path() transform = self.get_transform() tpath = transform.transform_path_non_affine(path) affine = transform.get_affine() if self.get_path_effects(): from matplotlib.patheffects import PathEffectRenderer renderer = PathEffectRenderer(self.get_path_effects(), renderer) renderer.draw_path(gc, tpath, affine, rgbFace) gc.restore() renderer.close_group('patch') self.stale = False def get_path(self): """ Return the path of this patch """ raise NotImplementedError('Derived must override') def get_window_extent(self, renderer=None): return self.get_path().get_extents(self.get_transform()) patchdoc = artist.kwdoc(Patch) for k in ('Rectangle', 'Circle', 'RegularPolygon', 'Polygon', 'Wedge', 'Arrow', 'FancyArrow', 'YAArrow', 'CirclePolygon', 'Ellipse', 'Arc', 'FancyBboxPatch', 'Patch'): docstring.interpd.update({k: patchdoc}) # define Patch.__init__ docstring after the class has been added to interpd docstring.dedent_interpd(Patch.__init__) class Shadow(Patch): def __str__(self): return "Shadow(%s)" % (str(self.patch)) @docstring.dedent_interpd def __init__(self, patch, ox, oy, props=None, **kwargs): """ Create a shadow of the given *patch* offset by *ox*, *oy*. *props*, if not *None*, is a patch property update dictionary. If *None*, the shadow will have have the same color as the face, but darkened. kwargs are %(Patch)s """ Patch.__init__(self) self.patch = patch self.props = props self._ox, self._oy = ox, oy self._shadow_transform = transforms.Affine2D() self._update() def _update(self): self.update_from(self.patch) if self.props is not None: self.update(self.props) else: r, g, b, a = colors.to_rgba(self.patch.get_facecolor()) rho = 0.3 r = rho * r g = rho * g b = rho * b self.set_facecolor((r, g, b, 0.5)) self.set_edgecolor((r, g, b, 0.5)) self.set_alpha(0.5) def _update_transform(self, renderer): ox = renderer.points_to_pixels(self._ox) oy = renderer.points_to_pixels(self._oy) self._shadow_transform.clear().translate(ox, oy) def _get_ox(self): return self._ox def _set_ox(self, ox): self._ox = ox def _get_oy(self): return self._oy def _set_oy(self, oy): self._oy = oy def get_path(self): return self.patch.get_path() def get_patch_transform(self): return self.patch.get_patch_transform() + self._shadow_transform def draw(self, renderer): self._update_transform(renderer) Patch.draw(self, renderer) class Rectangle(Patch): """ Draw a rectangle with lower left at *xy* = (*x*, *y*) with specified *width* and *height*. """ def __str__(self): return self.__class__.__name__ \ + "(%g,%g;%gx%g)" % (self._x, self._y, self._width, self._height) @docstring.dedent_interpd def __init__(self, xy, width, height, angle=0.0, **kwargs): """ *angle* rotation in degrees (anti-clockwise) *fill* is a boolean indicating whether to fill the rectangle Valid kwargs are: %(Patch)s """ Patch.__init__(self, **kwargs) self._x = float(xy[0]) self._y = float(xy[1]) self._width = float(width) self._height = float(height) self._angle = float(angle) # Note: This cannot be calculated until this is added to an Axes self._rect_transform = transforms.IdentityTransform() def get_path(self): """ Return the vertices of the rectangle """ return Path.unit_rectangle() def _update_patch_transform(self): """NOTE: This cannot be called until after this has been added to an Axes, otherwise unit conversion will fail. This maxes it very important to call the accessor method and not directly access the transformation member variable. """ x = self.convert_xunits(self._x) y = self.convert_yunits(self._y) width = self.convert_xunits(self._width) height = self.convert_yunits(self._height) bbox = transforms.Bbox.from_bounds(x, y, width, height) rot_trans = transforms.Affine2D() rot_trans.rotate_deg_around(x, y, self._angle) self._rect_transform = transforms.BboxTransformTo(bbox) self._rect_transform += rot_trans def get_patch_transform(self): self._update_patch_transform() return self._rect_transform def get_x(self): "Return the left coord of the rectangle" return self._x def get_y(self): "Return the bottom coord of the rectangle" return self._y def get_xy(self): "Return the left and bottom coords of the rectangle" return self._x, self._y def get_width(self): "Return the width of the rectangle" return self._width def get_height(self): "Return the height of the rectangle" return self._height def set_x(self, x): """ Set the left coord of the rectangle ACCEPTS: float """ self._x = x self.stale = True def set_y(self, y): """ Set the bottom coord of the rectangle ACCEPTS: float """ self._y = y self.stale = True def set_xy(self, xy): """ Set the left and bottom coords of the rectangle ACCEPTS: 2-item sequence """ self._x, self._y = xy self.stale = True def set_width(self, w): """ Set the width rectangle ACCEPTS: float """ self._width = w self.stale = True def set_height(self, h): """ Set the width rectangle ACCEPTS: float """ self._height = h self.stale = True def set_bounds(self, *args): """ Set the bounds of the rectangle: l,b,w,h ACCEPTS: (left, bottom, width, height) """ if len(args) == 0: l, b, w, h = args[0] else: l, b, w, h = args self._x = l self._y = b self._width = w self._height = h self.stale = True def get_bbox(self): return transforms.Bbox.from_bounds(self._x, self._y, self._width, self._height) xy = property(get_xy, set_xy) class RegularPolygon(Patch): """ A regular polygon patch. """ def __str__(self): return "Poly%d(%g,%g)" % (self._numVertices, self._xy[0], self._xy[1]) @docstring.dedent_interpd def __init__(self, xy, numVertices, radius=5, orientation=0, **kwargs): """ Constructor arguments: *xy* A length 2 tuple (*x*, *y*) of the center. *numVertices* the number of vertices. *radius* The distance from the center to each of the vertices. *orientation* rotates the polygon (in radians). Valid kwargs are: %(Patch)s """ self._xy = xy self._numVertices = numVertices self._orientation = orientation self._radius = radius self._path = Path.unit_regular_polygon(numVertices) self._poly_transform = transforms.Affine2D() self._update_transform() Patch.__init__(self, **kwargs) def _update_transform(self): self._poly_transform.clear() \ .scale(self.radius) \ .rotate(self.orientation) \ .translate(*self.xy) def _get_xy(self): return self._xy def _set_xy(self, xy): self._xy = xy self._update_transform() xy = property(_get_xy, _set_xy) def _get_orientation(self): return self._orientation def _set_orientation(self, orientation): self._orientation = orientation self._update_transform() orientation = property(_get_orientation, _set_orientation) def _get_radius(self): return self._radius def _set_radius(self, radius): self._radius = radius self._update_transform() radius = property(_get_radius, _set_radius) def _get_numvertices(self): return self._numVertices def _set_numvertices(self, numVertices): self._numVertices = numVertices numvertices = property(_get_numvertices, _set_numvertices) def get_path(self): return self._path def get_patch_transform(self): self._update_transform() return self._poly_transform class PathPatch(Patch): """ A general polycurve path patch. """ _edge_default = True def __str__(self): return "Poly((%g, %g) ...)" % tuple(self._path.vertices[0]) @docstring.dedent_interpd def __init__(self, path, **kwargs): """ *path* is a :class:`matplotlib.path.Path` object. Valid kwargs are: %(Patch)s .. seealso:: :class:`Patch` For additional kwargs """ Patch.__init__(self, **kwargs) self._path = path def get_path(self): return self._path class Polygon(Patch): """ A general polygon patch. """ def __str__(self): return "Poly((%g, %g) ...)" % tuple(self._path.vertices[0]) @docstring.dedent_interpd def __init__(self, xy, closed=True, **kwargs): """ *xy* is a numpy array with shape Nx2. If *closed* is *True*, the polygon will be closed so the starting and ending points are the same. Valid kwargs are: %(Patch)s .. seealso:: :class:`Patch` For additional kwargs """ Patch.__init__(self, **kwargs) self._closed = closed self.set_xy(xy) def get_path(self): """ Get the path of the polygon Returns ------- path : Path The :class:`~matplotlib.path.Path` object for the polygon """ return self._path def get_closed(self): """ Returns if the polygon is closed Returns ------- closed : bool If the path is closed """ return self._closed def set_closed(self, closed): """ Set if the polygon is closed Parameters ---------- closed : bool True if the polygon is closed """ if self._closed == bool(closed): return self._closed = bool(closed) self.set_xy(self.get_xy()) self.stale = True def get_xy(self): """ Get the vertices of the path Returns ------- vertices : numpy array The coordinates of the vertices as a Nx2 ndarray. """ return self._path.vertices def set_xy(self, xy): """ Set the vertices of the polygon Parameters ---------- xy : numpy array or iterable of pairs The coordinates of the vertices as a Nx2 ndarray or iterable of pairs. """ xy = np.asarray(xy) if self._closed: if len(xy) and (xy[0] != xy[-1]).any(): xy = np.concatenate([xy, [xy[0]]]) else: if len(xy) > 2 and (xy[0] == xy[-1]).all(): xy = xy[:-1] self._path = Path(xy, closed=self._closed) self.stale = True _get_xy = get_xy _set_xy = set_xy xy = property( get_xy, set_xy, None, """Set/get the vertices of the polygon. This property is provided for backward compatibility with matplotlib 0.91.x only. New code should use :meth:`~matplotlib.patches.Polygon.get_xy` and :meth:`~matplotlib.patches.Polygon.set_xy` instead.""") class Wedge(Patch): """ Wedge shaped patch. """ def __str__(self): return "Wedge(%g,%g)" % (self.theta1, self.theta2) @docstring.dedent_interpd def __init__(self, center, r, theta1, theta2, width=None, **kwargs): """ Draw a wedge centered at *x*, *y* center with radius *r* that sweeps *theta1* to *theta2* (in degrees). If *width* is given, then a partial wedge is drawn from inner radius *r* - *width* to outer radius *r*. Valid kwargs are: %(Patch)s """ Patch.__init__(self, **kwargs) self.center = center self.r, self.width = r, width self.theta1, self.theta2 = theta1, theta2 self._patch_transform = transforms.IdentityTransform() self._recompute_path() def _recompute_path(self): # Inner and outer rings are connected unless the annulus is complete if abs((self.theta2 - self.theta1) - 360) <= 1e-12: theta1, theta2 = 0, 360 connector = Path.MOVETO else: theta1, theta2 = self.theta1, self.theta2 connector = Path.LINETO # Form the outer ring arc = Path.arc(theta1, theta2) if self.width is not None: # Partial annulus needs to draw the outer ring # followed by a reversed and scaled inner ring v1 = arc.vertices v2 = arc.vertices[::-1] * float(self.r - self.width) / self.r v = np.vstack([v1, v2, v1[0, :], (0, 0)]) c = np.hstack([arc.codes, arc.codes, connector, Path.CLOSEPOLY]) c[len(arc.codes)] = connector else: # Wedge doesn't need an inner ring v = np.vstack([arc.vertices, [(0, 0), arc.vertices[0, :], (0, 0)]]) c = np.hstack([arc.codes, [connector, connector, Path.CLOSEPOLY]]) # Shift and scale the wedge to the final location. v *= self.r v += np.asarray(self.center) self._path = Path(v, c) def set_center(self, center): self._path = None self.center = center self.stale = True def set_radius(self, radius): self._path = None self.r = radius self.stale = True def set_theta1(self, theta1): self._path = None self.theta1 = theta1 self.stale = True def set_theta2(self, theta2): self._path = None self.theta2 = theta2 self.stale = True def set_width(self, width): self._path = None self.width = width self.stale = True def get_path(self): if self._path is None: self._recompute_path() return self._path # COVERAGE NOTE: Not used internally or from examples class Arrow(Patch): """ An arrow patch. """ def __str__(self): return "Arrow()" _path = Path([ [0.0, 0.1], [0.0, -0.1], [0.8, -0.1], [0.8, -0.3], [1.0, 0.0], [0.8, 0.3], [0.8, 0.1], [0.0, 0.1]], closed=True) @docstring.dedent_interpd def __init__(self, x, y, dx, dy, width=1.0, **kwargs): """ Draws an arrow, starting at (*x*, *y*), direction and length given by (*dx*, *dy*) the width of the arrow is scaled by *width*. Valid kwargs are: %(Patch)s """ Patch.__init__(self, **kwargs) L = np.hypot(dx, dy) if L != 0: cx = float(dx) / L sx = float(dy) / L else: # Account for division by zero cx, sx = 0, 1 trans1 = transforms.Affine2D().scale(L, width) trans2 = transforms.Affine2D.from_values(cx, sx, -sx, cx, 0.0, 0.0) trans3 = transforms.Affine2D().translate(x, y) trans = trans1 + trans2 + trans3 self._patch_transform = trans.frozen() def get_path(self): return self._path def get_patch_transform(self): return self._patch_transform class FancyArrow(Polygon): """ Like Arrow, but lets you set head width and head height independently. """ _edge_default = True def __str__(self): return "FancyArrow()" @docstring.dedent_interpd def __init__(self, x, y, dx, dy, width=0.001, length_includes_head=False, head_width=None, head_length=None, shape='full', overhang=0, head_starts_at_zero=False, **kwargs): """ Constructor arguments *width*: float (default: 0.001) width of full arrow tail *length_includes_head*: [True | False] (default: False) True if head is to be counted in calculating the length. *head_width*: float or None (default: 3*width) total width of the full arrow head *head_length*: float or None (default: 1.5 * head_width) length of arrow head *shape*: ['full', 'left', 'right'] (default: 'full') draw the left-half, right-half, or full arrow *overhang*: float (default: 0) fraction that the arrow is swept back (0 overhang means triangular shape). Can be negative or greater than one. *head_starts_at_zero*: [True | False] (default: False) if True, the head starts being drawn at coordinate 0 instead of ending at coordinate 0. Other valid kwargs (inherited from :class:`Patch`) are: %(Patch)s """ if head_width is None: head_width = 3 * width if head_length is None: head_length = 1.5 * head_width distance = np.hypot(dx, dy) if length_includes_head: length = distance else: length = distance + head_length if not length: verts = [] # display nothing if empty else: # start by drawing horizontal arrow, point at (0,0) hw, hl, hs, lw = head_width, head_length, overhang, width left_half_arrow = np.array([ [0.0, 0.0], # tip [-hl, -hw / 2.0], # leftmost [-hl * (1 - hs), -lw / 2.0], # meets stem [-length, -lw / 2.0], # bottom left [-length, 0], ]) # if we're not including the head, shift up by head length if not length_includes_head: left_half_arrow += [head_length, 0] # if the head starts at 0, shift up by another head length if head_starts_at_zero: left_half_arrow += [head_length / 2.0, 0] # figure out the shape, and complete accordingly if shape == 'left': coords = left_half_arrow else: right_half_arrow = left_half_arrow * [1, -1] if shape == 'right': coords = right_half_arrow elif shape == 'full': # The half-arrows contain the midpoint of the stem, # which we can omit from the full arrow. Including it # twice caused a problem with xpdf. coords = np.concatenate([left_half_arrow[:-2], right_half_arrow[-2::-1]]) else: raise ValueError("Got unknown shape: %s" % shape) if distance != 0: cx = float(dx) / distance sx = float(dy) / distance else: #Account for division by zero cx, sx = 0, 1 M = np.array([[cx, sx], [-sx, cx]]) verts = np.dot(coords, M) + (x + dx, y + dy) Polygon.__init__(self, list(map(tuple, verts)), closed=True, **kwargs) docstring.interpd.update({"FancyArrow": FancyArrow.__init__.__doc__}) docstring.interpd.update({"FancyArrow": FancyArrow.__init__.__doc__}) class YAArrow(Patch): """ Yet another arrow class. This is an arrow that is defined in display space and has a tip at *x1*, *y1* and a base at *x2*, *y2*. """ def __str__(self): return "YAArrow()" @docstring.dedent_interpd def __init__(self, figure, xytip, xybase, width=4, frac=0.1, headwidth=12, **kwargs): """ Constructor arguments: *xytip* (*x*, *y*) location of arrow tip *xybase* (*x*, *y*) location the arrow base mid point *figure* The :class:`~matplotlib.figure.Figure` instance (fig.dpi) *width* The width of the arrow in points *frac* The fraction of the arrow length occupied by the head *headwidth* The width of the base of the arrow head in points Valid kwargs are: %(Patch)s """ self.xytip = xytip self.xybase = xybase self.width = width self.frac = frac self.headwidth = headwidth Patch.__init__(self, **kwargs) # Set self.figure after Patch.__init__, since it sets self.figure to # None self.figure = figure def get_path(self): # Since this is dpi dependent, we need to recompute the path # every time. # the base vertices x1, y1 = self.xytip x2, y2 = self.xybase k1 = self.width * self.figure.dpi / 72. / 2. k2 = self.headwidth * self.figure.dpi / 72. / 2. xb1, yb1, xb2, yb2 = self.getpoints(x1, y1, x2, y2, k1) # a point on the segment 20% of the distance from the tip to the base theta = math.atan2(y2 - y1, x2 - x1) r = math.sqrt((y2 - y1) ** 2. + (x2 - x1) ** 2.) xm = x1 + self.frac * r * math.cos(theta) ym = y1 + self.frac * r * math.sin(theta) xc1, yc1, xc2, yc2 = self.getpoints(x1, y1, xm, ym, k1) xd1, yd1, xd2, yd2 = self.getpoints(x1, y1, xm, ym, k2) xs = self.convert_xunits([xb1, xb2, xc2, xd2, x1, xd1, xc1, xb1]) ys = self.convert_yunits([yb1, yb2, yc2, yd2, y1, yd1, yc1, yb1]) return Path(list(zip(xs, ys)), closed=True) def get_patch_transform(self): return transforms.IdentityTransform() def getpoints(self, x1, y1, x2, y2, k): """ For line segment defined by (*x1*, *y1*) and (*x2*, *y2*) return the points on the line that is perpendicular to the line and intersects (*x2*, *y2*) and the distance from (*x2*, *y2*) of the returned points is *k*. """ x1, y1, x2, y2, k = list(map(float, (x1, y1, x2, y2, k))) if y2 - y1 == 0: return x2, y2 + k, x2, y2 - k elif x2 - x1 == 0: return x2 + k, y2, x2 - k, y2 m = (y2 - y1) / (x2 - x1) pm = -1. / m a = 1 b = -2 * y2 c = y2 ** 2. - k ** 2. * pm ** 2. / (1. + pm ** 2.) y3a = (-b + math.sqrt(b ** 2. - 4 * a * c)) / (2. * a) x3a = (y3a - y2) / pm + x2 y3b = (-b - math.sqrt(b ** 2. - 4 * a * c)) / (2. * a) x3b = (y3b - y2) / pm + x2 return x3a, y3a, x3b, y3b class CirclePolygon(RegularPolygon): """ A polygon-approximation of a circle patch. """ def __str__(self): return "CirclePolygon(%d,%d)" % self.center @docstring.dedent_interpd def __init__(self, xy, radius=5, resolution=20, # the number of vertices ** kwargs): """ Create a circle at *xy* = (*x*, *y*) with given *radius*. This circle is approximated by a regular polygon with *resolution* sides. For a smoother circle drawn with splines, see :class:`~matplotlib.patches.Circle`. Valid kwargs are: %(Patch)s """ RegularPolygon.__init__(self, xy, resolution, radius, orientation=0, **kwargs) class Ellipse(Patch): """ A scale-free ellipse. """ def __str__(self): return "Ellipse(%s,%s;%sx%s)" % (self.center[0], self.center[1], self.width, self.height) @docstring.dedent_interpd def __init__(self, xy, width, height, angle=0.0, **kwargs): """ *xy* center of ellipse *width* total length (diameter) of horizontal axis *height* total length (diameter) of vertical axis *angle* rotation in degrees (anti-clockwise) Valid kwargs are: %(Patch)s """ Patch.__init__(self, **kwargs) self.center = xy self.width, self.height = width, height self.angle = angle self._path = Path.unit_circle() # Note: This cannot be calculated until this is added to an Axes self._patch_transform = transforms.IdentityTransform() def _recompute_transform(self): """NOTE: This cannot be called until after this has been added to an Axes, otherwise unit conversion will fail. This maxes it very important to call the accessor method and not directly access the transformation member variable. """ center = (self.convert_xunits(self.center[0]), self.convert_yunits(self.center[1])) width = self.convert_xunits(self.width) height = self.convert_yunits(self.height) self._patch_transform = transforms.Affine2D() \ .scale(width * 0.5, height * 0.5) \ .rotate_deg(self.angle) \ .translate(*center) def get_path(self): """ Return the vertices of the rectangle """ return self._path def get_patch_transform(self): self._recompute_transform() return self._patch_transform class Circle(Ellipse): """ A circle patch. """ def __str__(self): return "Circle((%g,%g),r=%g)" % (self.center[0], self.center[1], self.radius) @docstring.dedent_interpd def __init__(self, xy, radius=5, **kwargs): """ Create true circle at center *xy* = (*x*, *y*) with given *radius*. Unlike :class:`~matplotlib.patches.CirclePolygon` which is a polygonal approximation, this uses Bézier splines and is much closer to a scale-free circle. Valid kwargs are: %(Patch)s """ Ellipse.__init__(self, xy, radius * 2, radius * 2, **kwargs) self.radius = radius def set_radius(self, radius): """ Set the radius of the circle ACCEPTS: float """ self.width = self.height = 2 * radius self.stale = True def get_radius(self): 'return the radius of the circle' return self.width / 2. radius = property(get_radius, set_radius) class Arc(Ellipse): """ An elliptical arc. Because it performs various optimizations, it can not be filled. The arc must be used in an :class:`~matplotlib.axes.Axes` instance---it can not be added directly to a :class:`~matplotlib.figure.Figure`---because it is optimized to only render the segments that are inside the axes bounding box with high resolution. """ def __str__(self): return "Arc(%s,%s;%sx%s)" % (self.center[0], self.center[1], self.width, self.height) @docstring.dedent_interpd def __init__(self, xy, width, height, angle=0.0, theta1=0.0, theta2=360.0, **kwargs): """ The following args are supported: *xy* center of ellipse *width* length of horizontal axis *height* length of vertical axis *angle* rotation in degrees (anti-clockwise) *theta1* starting angle of the arc in degrees *theta2* ending angle of the arc in degrees If *theta1* and *theta2* are not provided, the arc will form a complete ellipse. Valid kwargs are: %(Patch)s """ fill = kwargs.setdefault('fill', False) if fill: raise ValueError("Arc objects can not be filled") Ellipse.__init__(self, xy, width, height, angle, **kwargs) self.theta1 = theta1 self.theta2 = theta2 self._path = Path.arc(self.theta1, self.theta2) @allow_rasterization def draw(self, renderer): """ Ellipses are normally drawn using an approximation that uses eight cubic bezier splines. The error of this approximation is 1.89818e-6, according to this unverified source: Lancaster, Don. Approximating a Circle or an Ellipse Using Four Bezier Cubic Splines. http://www.tinaja.com/glib/ellipse4.pdf There is a use case where very large ellipses must be drawn with very high accuracy, and it is too expensive to render the entire ellipse with enough segments (either splines or line segments). Therefore, in the case where either radius of the ellipse is large enough that the error of the spline approximation will be visible (greater than one pixel offset from the ideal), a different technique is used. In that case, only the visible parts of the ellipse are drawn, with each visible arc using a fixed number of spline segments (8). The algorithm proceeds as follows: 1. The points where the ellipse intersects the axes bounding box are located. (This is done be performing an inverse transformation on the axes bbox such that it is relative to the unit circle -- this makes the intersection calculation much easier than doing rotated ellipse intersection directly). This uses the "line intersecting a circle" algorithm from: Vince, John. Geometry for Computer Graphics: Formulae, Examples & Proofs. London: Springer-Verlag, 2005. 2. The angles of each of the intersection points are calculated. 3. Proceeding counterclockwise starting in the positive x-direction, each of the visible arc-segments between the pairs of vertices are drawn using the bezier arc approximation technique implemented in :meth:`matplotlib.path.Path.arc`. """ if not hasattr(self, 'axes'): raise RuntimeError('Arcs can only be used in Axes instances') self._recompute_transform() # Get the width and height in pixels width = self.convert_xunits(self.width) height = self.convert_yunits(self.height) width, height = self.get_transform().transform_point( (width, height)) inv_error = (1.0 / 1.89818e-6) * 0.5 if width < inv_error and height < inv_error: # self._path = Path.arc(self.theta1, self.theta2) return Patch.draw(self, renderer) def iter_circle_intersect_on_line(x0, y0, x1, y1): dx = x1 - x0 dy = y1 - y0 dr2 = dx * dx + dy * dy D = x0 * y1 - x1 * y0 D2 = D * D discrim = dr2 - D2 # Single (tangential) intersection if discrim == 0.0: x = (D * dy) / dr2 y = (-D * dx) / dr2 yield x, y elif discrim > 0.0: # The definition of "sign" here is different from # np.sign: we never want to get 0.0 if dy < 0.0: sign_dy = -1.0 else: sign_dy = 1.0 sqrt_discrim = np.sqrt(discrim) for sign in (1., -1.): x = (D * dy + sign * sign_dy * dx * sqrt_discrim) / dr2 y = (-D * dx + sign * np.abs(dy) * sqrt_discrim) / dr2 yield x, y def iter_circle_intersect_on_line_seg(x0, y0, x1, y1): epsilon = 1e-9 if x1 < x0: x0e, x1e = x1, x0 else: x0e, x1e = x0, x1 if y1 < y0: y0e, y1e = y1, y0 else: y0e, y1e = y0, y1 x0e -= epsilon y0e -= epsilon x1e += epsilon y1e += epsilon for x, y in iter_circle_intersect_on_line(x0, y0, x1, y1): if x >= x0e and x <= x1e and y >= y0e and y <= y1e: yield x, y # Transforms the axes box_path so that it is relative to the unit # circle in the same way that it is relative to the desired # ellipse. box_path = Path.unit_rectangle() box_path_transform = transforms.BboxTransformTo(self.axes.bbox) + \ self.get_transform().inverted() box_path = box_path.transformed(box_path_transform) PI = np.pi TWOPI = PI * 2.0 RAD2DEG = 180.0 / PI DEG2RAD = PI / 180.0 theta1 = self.theta1 theta2 = self.theta2 thetas = {} # For each of the point pairs, there is a line segment for p0, p1 in zip(box_path.vertices[:-1], box_path.vertices[1:]): x0, y0 = p0 x1, y1 = p1 for x, y in iter_circle_intersect_on_line_seg(x0, y0, x1, y1): theta = np.arccos(x) if y < 0: theta = TWOPI - theta # Convert radians to angles theta *= RAD2DEG if theta > theta1 and theta < theta2: thetas[theta] = None thetas = list(six.iterkeys(thetas)) thetas.sort() thetas.append(theta2) last_theta = theta1 theta1_rad = theta1 * DEG2RAD inside = box_path.contains_point((np.cos(theta1_rad), np.sin(theta1_rad))) # save original path path_original = self._path for theta in thetas: if inside: Path.arc(last_theta, theta, 8) Patch.draw(self, renderer) inside = False else: inside = True last_theta = theta # restore original path self._path = path_original def bbox_artist(artist, renderer, props=None, fill=True): """ This is a debug function to draw a rectangle around the bounding box returned by :meth:`~matplotlib.artist.Artist.get_window_extent` of an artist, to test whether the artist is returning the correct bbox. *props* is a dict of rectangle props with the additional property 'pad' that sets the padding around the bbox in points. """ if props is None: props = {} props = props.copy() # don't want to alter the pad externally pad = props.pop('pad', 4) pad = renderer.points_to_pixels(pad) bbox = artist.get_window_extent(renderer) l, b, w, h = bbox.bounds l -= pad / 2. b -= pad / 2. w += pad h += pad r = Rectangle(xy=(l, b), width=w, height=h, fill=fill, ) r.set_transform(transforms.IdentityTransform()) r.set_clip_on(False) r.update(props) r.draw(renderer) def draw_bbox(bbox, renderer, color='k', trans=None): """ This is a debug function to draw a rectangle around the bounding box returned by :meth:`~matplotlib.artist.Artist.get_window_extent` of an artist, to test whether the artist is returning the correct bbox. """ l, b, w, h = bbox.bounds r = Rectangle(xy=(l, b), width=w, height=h, edgecolor=color, fill=False, ) if trans is not None: r.set_transform(trans) r.set_clip_on(False) r.draw(renderer) def _pprint_table(_table, leadingspace=2): """ Given the list of list of strings, return a string of REST table format. """ if leadingspace: pad = ' ' * leadingspace else: pad = '' columns = [[] for cell in _table[0]] for row in _table: for column, cell in zip(columns, row): column.append(cell) col_len = [max([len(cell) for cell in column]) for column in columns] lines = [] table_formatstr = pad + ' '.join([('=' * cl) for cl in col_len]) lines.append('') lines.append(table_formatstr) lines.append(pad + ' '.join([cell.ljust(cl) for cell, cl in zip(_table[0], col_len)])) lines.append(table_formatstr) lines.extend([(pad + ' '.join([cell.ljust(cl) for cell, cl in zip(row, col_len)])) for row in _table[1:]]) lines.append(table_formatstr) lines.append('') return "\n".join(lines) def _pprint_styles(_styles): """ A helper function for the _Style class. Given the dictionary of (stylename : styleclass), return a formatted string listing all the styles. Used to update the documentation. """ names, attrss, clss = [], [], [] import inspect _table = [["Class", "Name", "Attrs"]] for name, cls in sorted(_styles.items()): if six.PY2: args, varargs, varkw, defaults = inspect.getargspec(cls.__init__) else: (args, varargs, varkw, defaults, kwonlyargs, kwonlydefs, annotations) = inspect.getfullargspec(cls.__init__) if defaults: args = [(argname, argdefault) for argname, argdefault in zip(args[1:], defaults)] else: args = None if args is None: argstr = 'None' else: argstr = ",".join([("%s=%s" % (an, av)) for an, av in args]) # adding ``quotes`` since - and | have special meaning in reST _table.append([cls.__name__, "``%s``" % name, argstr]) return _pprint_table(_table) def _simpleprint_styles(_styles): """ A helper function for the _Style class. Given the dictionary of (stylename : styleclass), return a string rep of the list of keys. Used to update the documentation. """ styles = "[ \'" styles += "\' | \'".join(str(i) for i in sorted(_styles.keys())) styles += "\' ]" return styles class _Style(object): """ A base class for the Styles. It is meant to be a container class, where actual styles are declared as subclass of it, and it provides some helper functions. """ def __new__(self, stylename, **kw): """ return the instance of the subclass with the given style name. """ # the "class" should have the _style_list attribute, which is # a dictionary of stylname, style class paie. _list = stylename.replace(" ", "").split(",") _name = _list[0].lower() try: _cls = self._style_list[_name] except KeyError: raise ValueError("Unknown style : %s" % stylename) try: _args_pair = [cs.split("=") for cs in _list[1:]] _args = dict([(k, float(v)) for k, v in _args_pair]) except ValueError: raise ValueError("Incorrect style argument : %s" % stylename) _args.update(kw) return _cls(**_args) @classmethod def get_styles(klass): """ A class method which returns a dictionary of available styles. """ return klass._style_list @classmethod def pprint_styles(klass): """ A class method which returns a string of the available styles. """ return _pprint_styles(klass._style_list) @classmethod def register(klass, name, style): """ Register a new style. """ if not issubclass(style, klass._Base): raise ValueError("%s must be a subclass of %s" % (style, klass._Base)) klass._style_list[name] = style class BoxStyle(_Style): """ :class:`BoxStyle` is a container class which defines several boxstyle classes, which are used for :class:`FancyBboxPatch`. A style object can be created as:: BoxStyle.Round(pad=0.2) or:: BoxStyle("Round", pad=0.2) or:: BoxStyle("Round, pad=0.2") Following boxstyle classes are defined. %(AvailableBoxstyles)s An instance of any boxstyle class is an callable object, whose call signature is:: __call__(self, x0, y0, width, height, mutation_size, aspect_ratio=1.) and returns a :class:`Path` instance. *x0*, *y0*, *width* and *height* specify the location and size of the box to be drawn. *mutation_scale* determines the overall size of the mutation (by which I mean the transformation of the rectangle to the fancy box). *mutation_aspect* determines the aspect-ratio of the mutation. .. plot:: mpl_examples/pylab_examples/fancybox_demo2.py """ _style_list = {} class _Base(object): """ :class:`BBoxTransmuterBase` and its derivatives are used to make a fancy box around a given rectangle. The :meth:`__call__` method returns the :class:`~matplotlib.path.Path` of the fancy box. This class is not an artist and actual drawing of the fancy box is done by the :class:`FancyBboxPatch` class. """ # The derived classes are required to be able to be initialized # w/o arguments, i.e., all its argument (except self) must have # the default values. def __init__(self): """ initializtion. """ super(BoxStyle._Base, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): """ The transmute method is a very core of the :class:`BboxTransmuter` class and must be overriden in the subclasses. It receives the location and size of the rectangle, and the mutation_size, with which the amount of padding and etc. will be scaled. It returns a :class:`~matplotlib.path.Path` instance. """ raise NotImplementedError('Derived must override') def __call__(self, x0, y0, width, height, mutation_size, aspect_ratio=1.): """ Given the location and size of the box, return the path of the box around it. - *x0*, *y0*, *width*, *height* : location and size of the box - *mutation_size* : a reference scale for the mutation. - *aspect_ratio* : aspect-ration for the mutation. """ # The __call__ method is a thin wrapper around the transmute method # and take care of the aspect. if aspect_ratio is not None: # Squeeze the given height by the aspect_ratio y0, height = y0 / aspect_ratio, height / aspect_ratio # call transmute method with squeezed height. path = self.transmute(x0, y0, width, height, mutation_size) vertices, codes = path.vertices, path.codes # Restore the height vertices[:, 1] = vertices[:, 1] * aspect_ratio return Path(vertices, codes) else: return self.transmute(x0, y0, width, height, mutation_size) def __reduce__(self): # because we have decided to nest thes classes, we need to # add some more information to allow instance pickling. import matplotlib.cbook as cbook return (cbook._NestedClassGetter(), (BoxStyle, self.__class__.__name__), self.__dict__ ) class Square(_Base): """ A simple square box. """ def __init__(self, pad=0.3): """ *pad* amount of padding """ self.pad = pad super(BoxStyle.Square, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): pad = mutation_size * self.pad # width and height with padding added. width, height = width + 2*pad, height + 2*pad # boundary of the padded box x0, y0 = x0 - pad, y0 - pad, x1, y1 = x0 + width, y0 + height vertices = [(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)] codes = [Path.MOVETO] + [Path.LINETO] * 3 + [Path.CLOSEPOLY] return Path(vertices, codes) _style_list["square"] = Square class Circle(_Base): """A simple circle box.""" def __init__(self, pad=0.3): """ Parameters ---------- pad : float The amount of padding around the original box. """ self.pad = pad super(BoxStyle.Circle, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): pad = mutation_size * self.pad width, height = width + 2 * pad, height + 2 * pad # boundary of the padded box x0, y0 = x0 - pad, y0 - pad, return Path.circle((x0 + width/2., y0 + height/2.), (max([width, height]) / 2.)) _style_list["circle"] = Circle class LArrow(_Base): """ (left) Arrow Box """ def __init__(self, pad=0.3): self.pad = pad super(BoxStyle.LArrow, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): # padding pad = mutation_size * self.pad # width and height with padding added. width, height = width + 2. * pad, height + 2. * pad # boundary of the padded box x0, y0 = x0 - pad, y0 - pad, x1, y1 = x0 + width, y0 + height dx = (y1 - y0) / 2. dxx = dx * .5 # adjust x0. 1.4 <- sqrt(2) x0 = x0 + pad / 1.4 cp = [(x0 + dxx, y0), (x1, y0), (x1, y1), (x0 + dxx, y1), (x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx), (x0 + dxx, y0 - dxx), # arrow (x0 + dxx, y0), (x0 + dxx, y0)] com = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] path = Path(cp, com) return path _style_list["larrow"] = LArrow class RArrow(LArrow): """ (right) Arrow Box """ def __init__(self, pad=0.3): super(BoxStyle.RArrow, self).__init__(pad) def transmute(self, x0, y0, width, height, mutation_size): p = BoxStyle.LArrow.transmute(self, x0, y0, width, height, mutation_size) p.vertices[:, 0] = 2 * x0 + width - p.vertices[:, 0] return p _style_list["rarrow"] = RArrow class DArrow(_Base): """ (Double) Arrow Box """ # This source is copied from LArrow, # modified to add a right arrow to the bbox. def __init__(self, pad=0.3): self.pad = pad super(BoxStyle.DArrow, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): # padding pad = mutation_size * self.pad # width and height with padding added. # The width is padded by the arrows, so we don't need to pad it. height = height + 2. * pad # boundary of the padded box x0, y0 = x0 - pad, y0 - pad x1, y1 = x0 + width, y0 + height dx = (y1 - y0)/2. dxx = dx * .5 # adjust x0. 1.4 <- sqrt(2) x0 = x0 + pad / 1.4 cp = [(x0 + dxx, y0), (x1, y0), # bot-segment (x1, y0 - dxx), (x1 + dx + dxx, y0 + dx), (x1, y1 + dxx), # right-arrow (x1, y1), (x0 + dxx, y1), # top-segment (x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx), (x0 + dxx, y0 - dxx), # left-arrow (x0 + dxx, y0), (x0 + dxx, y0)] # close-poly com = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] path = Path(cp, com) return path _style_list['darrow'] = DArrow class Round(_Base): """ A box with round corners. """ def __init__(self, pad=0.3, rounding_size=None): """ *pad* amount of padding *rounding_size* rounding radius of corners. *pad* if None """ self.pad = pad self.rounding_size = rounding_size super(BoxStyle.Round, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): # padding pad = mutation_size * self.pad # size of the roudning corner if self.rounding_size: dr = mutation_size * self.rounding_size else: dr = pad width, height = width + 2. * pad, height + 2. * pad x0, y0 = x0 - pad, y0 - pad, x1, y1 = x0 + width, y0 + height # Round corners are implemented as quadratic bezier. e.g., # [(x0, y0-dr), (x0, y0), (x0+dr, y0)] for lower left corner. cp = [(x0 + dr, y0), (x1 - dr, y0), (x1, y0), (x1, y0 + dr), (x1, y1 - dr), (x1, y1), (x1 - dr, y1), (x0 + dr, y1), (x0, y1), (x0, y1 - dr), (x0, y0 + dr), (x0, y0), (x0 + dr, y0), (x0 + dr, y0)] com = [Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.CLOSEPOLY] path = Path(cp, com) return path _style_list["round"] = Round class Round4(_Base): """ Another box with round edges. """ def __init__(self, pad=0.3, rounding_size=None): """ *pad* amount of padding *rounding_size* rounding size of edges. *pad* if None """ self.pad = pad self.rounding_size = rounding_size super(BoxStyle.Round4, self).__init__() def transmute(self, x0, y0, width, height, mutation_size): # padding pad = mutation_size * self.pad # roudning size. Use a half of the pad if not set. if self.rounding_size: dr = mutation_size * self.rounding_size else: dr = pad / 2. width, height = (width + 2. * pad - 2 * dr, height + 2. * pad - 2 * dr) x0, y0 = x0 - pad + dr, y0 - pad + dr, x1, y1 = x0 + width, y0 + height cp = [(x0, y0), (x0 + dr, y0 - dr), (x1 - dr, y0 - dr), (x1, y0), (x1 + dr, y0 + dr), (x1 + dr, y1 - dr), (x1, y1), (x1 - dr, y1 + dr), (x0 + dr, y1 + dr), (x0, y1), (x0 - dr, y1 - dr), (x0 - dr, y0 + dr), (x0, y0), (x0, y0)] com = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CLOSEPOLY] path = Path(cp, com) return path _style_list["round4"] = Round4 class Sawtooth(_Base): """ A sawtooth box. """ def __init__(self, pad=0.3, tooth_size=None): """ *pad* amount of padding *tooth_size* size of the sawtooth. pad* if None """ self.pad = pad self.tooth_size = tooth_size super(BoxStyle.Sawtooth, self).__init__() def _get_sawtooth_vertices(self, x0, y0, width, height, mutation_size): # padding pad = mutation_size * self.pad # size of sawtooth if self.tooth_size is None: tooth_size = self.pad * .5 * mutation_size else: tooth_size = self.tooth_size * mutation_size tooth_size2 = tooth_size / 2. width, height = (width + 2. * pad - tooth_size, height + 2. * pad - tooth_size) # the sizes of the vertical and horizontal sawtooth are # separately adjusted to fit the given box size. dsx_n = int(np.round((width - tooth_size) / (tooth_size * 2))) * 2 dsx = (width - tooth_size) / dsx_n dsy_n = int(np.round((height - tooth_size) / (tooth_size * 2))) * 2 dsy = (height - tooth_size) / dsy_n x0, y0 = x0 - pad + tooth_size2, y0 - pad + tooth_size2 x1, y1 = x0 + width, y0 + height bottom_saw_x = [x0] + \ [x0 + tooth_size2 + dsx * .5 * i for i in range(dsx_n * 2)] + \ [x1 - tooth_size2] bottom_saw_y = [y0] + \ [y0 - tooth_size2, y0, y0 + tooth_size2, y0] * dsx_n + \ [y0 - tooth_size2] right_saw_x = [x1] + \ [x1 + tooth_size2, x1, x1 - tooth_size2, x1] * dsx_n + \ [x1 + tooth_size2] right_saw_y = [y0] + \ [y0 + tooth_size2 + dsy * .5 * i for i in range(dsy_n * 2)] + \ [y1 - tooth_size2] top_saw_x = [x1] + \ [x1 - tooth_size2 - dsx * .5 * i for i in range(dsx_n * 2)] + \ [x0 + tooth_size2] top_saw_y = [y1] + \ [y1 + tooth_size2, y1, y1 - tooth_size2, y1] * dsx_n + \ [y1 + tooth_size2] left_saw_x = [x0] + \ [x0 - tooth_size2, x0, x0 + tooth_size2, x0] * dsy_n + \ [x0 - tooth_size2] left_saw_y = [y1] + \ [y1 - tooth_size2 - dsy * .5 * i for i in range(dsy_n * 2)] + \ [y0 + tooth_size2] saw_vertices = (list(zip(bottom_saw_x, bottom_saw_y)) + list(zip(right_saw_x, right_saw_y)) + list(zip(top_saw_x, top_saw_y)) + list(zip(left_saw_x, left_saw_y)) + [(bottom_saw_x[0], bottom_saw_y[0])]) return saw_vertices def transmute(self, x0, y0, width, height, mutation_size): saw_vertices = self._get_sawtooth_vertices(x0, y0, width, height, mutation_size) path = Path(saw_vertices, closed=True) return path _style_list["sawtooth"] = Sawtooth class Roundtooth(Sawtooth): """A rounded tooth box.""" def __init__(self, pad=0.3, tooth_size=None): """ *pad* amount of padding *tooth_size* size of the sawtooth. pad* if None """ super(BoxStyle.Roundtooth, self).__init__(pad, tooth_size) def transmute(self, x0, y0, width, height, mutation_size): saw_vertices = self._get_sawtooth_vertices(x0, y0, width, height, mutation_size) # Add a trailing vertex to allow us to close the polygon correctly saw_vertices = np.concatenate([np.array(saw_vertices), [saw_vertices[0]]], axis=0) codes = ([Path.MOVETO] + [Path.CURVE3, Path.CURVE3] * ((len(saw_vertices)-1)//2) + [Path.CLOSEPOLY]) return Path(saw_vertices, codes) _style_list["roundtooth"] = Roundtooth if __doc__: # __doc__ could be None if -OO optimization is enabled __doc__ = cbook.dedent(__doc__) % \ {"AvailableBoxstyles": _pprint_styles(_style_list)} docstring.interpd.update( AvailableBoxstyles=_pprint_styles(BoxStyle._style_list), ListBoxstyles=_simpleprint_styles(BoxStyle._style_list)) class FancyBboxPatch(Patch): """ Draw a fancy box around a rectangle with lower left at *xy*=(*x*, *y*) with specified width and height. :class:`FancyBboxPatch` class is similar to :class:`Rectangle` class, but it draws a fancy box around the rectangle. The transformation of the rectangle box to the fancy box is delegated to the :class:`BoxTransmuterBase` and its derived classes. """ _edge_default = True def __str__(self): return self.__class__.__name__ \ + "(%g,%g;%gx%g)" % (self._x, self._y, self._width, self._height) @docstring.dedent_interpd def __init__(self, xy, width, height, boxstyle="round", bbox_transmuter=None, mutation_scale=1., mutation_aspect=None, **kwargs): """ *xy* = lower left corner *width*, *height* *boxstyle* determines what kind of fancy box will be drawn. It can be a string of the style name with a comma separated attribute, or an instance of :class:`BoxStyle`. Following box styles are available. %(AvailableBoxstyles)s *mutation_scale* : a value with which attributes of boxstyle (e.g., pad) will be scaled. default=1. *mutation_aspect* : The height of the rectangle will be squeezed by this value before the mutation and the mutated box will be stretched by the inverse of it. default=None. Valid kwargs are: %(Patch)s """ Patch.__init__(self, **kwargs) self._x = xy[0] self._y = xy[1] self._width = width self._height = height if boxstyle == "custom": if bbox_transmuter is None: raise ValueError("bbox_transmuter argument is needed with " "custom boxstyle") self._bbox_transmuter = bbox_transmuter else: self.set_boxstyle(boxstyle) self._mutation_scale = mutation_scale self._mutation_aspect = mutation_aspect self.stale = True @docstring.dedent_interpd def set_boxstyle(self, boxstyle=None, **kw): """ Set the box style. *boxstyle* can be a string with boxstyle name with optional comma-separated attributes. Alternatively, the attrs can be provided as keywords:: set_boxstyle("round,pad=0.2") set_boxstyle("round", pad=0.2) Old attrs simply are forgotten. Without argument (or with *boxstyle* = None), it returns available box styles. The following boxstyles are available: %(AvailableBoxstyles)s ACCEPTS: %(ListBoxstyles)s """ if boxstyle is None: return BoxStyle.pprint_styles() if isinstance(boxstyle, BoxStyle._Base): self._bbox_transmuter = boxstyle elif six.callable(boxstyle): self._bbox_transmuter = boxstyle else: self._bbox_transmuter = BoxStyle(boxstyle, **kw) self.stale = True def set_mutation_scale(self, scale): """ Set the mutation scale. ACCEPTS: float """ self._mutation_scale = scale self.stale = True def get_mutation_scale(self): """ Return the mutation scale. """ return self._mutation_scale def set_mutation_aspect(self, aspect): """ Set the aspect ratio of the bbox mutation. ACCEPTS: float """ self._mutation_aspect = aspect self.stale = True def get_mutation_aspect(self): """ Return the aspect ratio of the bbox mutation. """ return self._mutation_aspect def get_boxstyle(self): "Return the boxstyle object" return self._bbox_transmuter def get_path(self): """ Return the mutated path of the rectangle """ _path = self.get_boxstyle()(self._x, self._y, self._width, self._height, self.get_mutation_scale(), self.get_mutation_aspect()) return _path # Following methods are borrowed from the Rectangle class. def get_x(self): "Return the left coord of the rectangle" return self._x def get_y(self): "Return the bottom coord of the rectangle" return self._y def get_width(self): "Return the width of the rectangle" return self._width def get_height(self): "Return the height of the rectangle" return self._height def set_x(self, x): """ Set the left coord of the rectangle ACCEPTS: float """ self._x = x self.stale = True def set_y(self, y): """ Set the bottom coord of the rectangle ACCEPTS: float """ self._y = y self.stale = True def set_width(self, w): """ Set the width rectangle ACCEPTS: float """ self._width = w self.stale = True def set_height(self, h): """ Set the width rectangle ACCEPTS: float """ self._height = h self.stale = True def set_bounds(self, *args): """ Set the bounds of the rectangle: l,b,w,h ACCEPTS: (left, bottom, width, height) """ if len(args) == 0: l, b, w, h = args[0] else: l, b, w, h = args self._x = l self._y = b self._width = w self._height = h self.stale = True def get_bbox(self): return transforms.Bbox.from_bounds(self._x, self._y, self._width, self._height) class ConnectionStyle(_Style): """ :class:`ConnectionStyle` is a container class which defines several connectionstyle classes, which is used to create a path between two points. These are mainly used with :class:`FancyArrowPatch`. A connectionstyle object can be either created as:: ConnectionStyle.Arc3(rad=0.2) or:: ConnectionStyle("Arc3", rad=0.2) or:: ConnectionStyle("Arc3, rad=0.2") The following classes are defined %(AvailableConnectorstyles)s An instance of any connection style class is an callable object, whose call signature is:: __call__(self, posA, posB, patchA=None, patchB=None, shrinkA=2., shrinkB=2.) and it returns a :class:`Path` instance. *posA* and *posB* are tuples of x,y coordinates of the two points to be connected. *patchA* (or *patchB*) is given, the returned path is clipped so that it start (or end) from the boundary of the patch. The path is further shrunk by *shrinkA* (or *shrinkB*) which is given in points. """ _style_list = {} class _Base(object): """ A base class for connectionstyle classes. The subclass needs to implement a *connect* method whose call signature is:: connect(posA, posB) where posA and posB are tuples of x, y coordinates to be connected. The method needs to return a path connecting two points. This base class defines a __call__ method, and a few helper methods. """ class SimpleEvent: def __init__(self, xy): self.x, self.y = xy def _clip(self, path, patchA, patchB): """ Clip the path to the boundary of the patchA and patchB. The starting point of the path needed to be inside of the patchA and the end point inside the patch B. The *contains* methods of each patch object is utilized to test if the point is inside the path. """ if patchA: def insideA(xy_display): xy_event = ConnectionStyle._Base.SimpleEvent(xy_display) return patchA.contains(xy_event)[0] try: left, right = split_path_inout(path, insideA) except ValueError: right = path path = right if patchB: def insideB(xy_display): xy_event = ConnectionStyle._Base.SimpleEvent(xy_display) return patchB.contains(xy_event)[0] try: left, right = split_path_inout(path, insideB) except ValueError: left = path path = left return path def _shrink(self, path, shrinkA, shrinkB): """ Shrink the path by fixed size (in points) with shrinkA and shrinkB """ if shrinkA: x, y = path.vertices[0] insideA = inside_circle(x, y, shrinkA) try: left, right = split_path_inout(path, insideA) path = right except ValueError: pass if shrinkB: x, y = path.vertices[-1] insideB = inside_circle(x, y, shrinkB) try: left, right = split_path_inout(path, insideB) path = left except ValueError: pass return path def __call__(self, posA, posB, shrinkA=2., shrinkB=2., patchA=None, patchB=None): """ Calls the *connect* method to create a path between *posA* and *posB*. The path is clipped and shrunken. """ path = self.connect(posA, posB) clipped_path = self._clip(path, patchA, patchB) shrunk_path = self._shrink(clipped_path, shrinkA, shrinkB) return shrunk_path def __reduce__(self): # because we have decided to nest these classes, we need to # add some more information to allow instance pickling. import matplotlib.cbook as cbook return (cbook._NestedClassGetter(), (ConnectionStyle, self.__class__.__name__), self.__dict__ ) class Arc3(_Base): """ Creates a simple quadratic bezier curve between two points. The curve is created so that the middle contol points (C1) is located at the same distance from the start (C0) and end points(C2) and the distance of the C1 to the line connecting C0-C2 is *rad* times the distance of C0-C2. """ def __init__(self, rad=0.): """ *rad* curvature of the curve. """ self.rad = rad def connect(self, posA, posB): x1, y1 = posA x2, y2 = posB x12, y12 = (x1 + x2) / 2., (y1 + y2) / 2. dx, dy = x2 - x1, y2 - y1 f = self.rad cx, cy = x12 + f * dy, y12 - f * dx vertices = [(x1, y1), (cx, cy), (x2, y2)] codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3] return Path(vertices, codes) _style_list["arc3"] = Arc3 class Angle3(_Base): """ Creates a simple quadratic bezier curve between two points. The middle control points is placed at the intersecting point of two lines which crosses the start (or end) point and has a angle of angleA (or angleB). """ def __init__(self, angleA=90, angleB=0): """ *angleA* starting angle of the path *angleB* ending angle of the path """ self.angleA = angleA self.angleB = angleB def connect(self, posA, posB): x1, y1 = posA x2, y2 = posB cosA, sinA = (math.cos(self.angleA / 180. * math.pi), math.sin(self.angleA / 180. * math.pi)) cosB, sinB = (math.cos(self.angleB / 180. * math.pi), math.sin(self.angleB / 180. * math.pi)) cx, cy = get_intersection(x1, y1, cosA, sinA, x2, y2, cosB, sinB) vertices = [(x1, y1), (cx, cy), (x2, y2)] codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3] return Path(vertices, codes) _style_list["angle3"] = Angle3 class Angle(_Base): """ Creates a picewise continuous quadratic bezier path between two points. The path has a one passing-through point placed at the intersecting point of two lines which crosses the start (or end) point and has a angle of angleA (or angleB). The connecting edges are rounded with *rad*. """ def __init__(self, angleA=90, angleB=0, rad=0.): """ *angleA* starting angle of the path *angleB* ending angle of the path *rad* rounding radius of the edge """ self.angleA = angleA self.angleB = angleB self.rad = rad def connect(self, posA, posB): x1, y1 = posA x2, y2 = posB cosA, sinA = (math.cos(self.angleA / 180. * math.pi), math.sin(self.angleA / 180. * math.pi)) cosB, sinB = (math.cos(self.angleB / 180. * math.pi), math.sin(self.angleB / 180. * math.pi)) cx, cy = get_intersection(x1, y1, cosA, sinA, x2, y2, cosB, sinB) vertices = [(x1, y1)] codes = [Path.MOVETO] if self.rad == 0.: vertices.append((cx, cy)) codes.append(Path.LINETO) else: dx1, dy1 = x1 - cx, y1 - cy d1 = (dx1 ** 2 + dy1 ** 2) ** .5 f1 = self.rad / d1 dx2, dy2 = x2 - cx, y2 - cy d2 = (dx2 ** 2 + dy2 ** 2) ** .5 f2 = self.rad / d2 vertices.extend([(cx + dx1 * f1, cy + dy1 * f1), (cx, cy), (cx + dx2 * f2, cy + dy2 * f2)]) codes.extend([Path.LINETO, Path.CURVE3, Path.CURVE3]) vertices.append((x2, y2)) codes.append(Path.LINETO) return Path(vertices, codes) _style_list["angle"] = Angle class Arc(_Base): """ Creates a picewise continuous quadratic bezier path between two points. The path can have two passing-through points, a point placed at the distance of armA and angle of angleA from point A, another point with respect to point B. The edges are rounded with *rad*. """ def __init__(self, angleA=0, angleB=0, armA=None, armB=None, rad=0.): """ *angleA* : starting angle of the path *angleB* : ending angle of the path *armA* : length of the starting arm *armB* : length of the ending arm *rad* : rounding radius of the edges """ self.angleA = angleA self.angleB = angleB self.armA = armA self.armB = armB self.rad = rad def connect(self, posA, posB): x1, y1 = posA x2, y2 = posB vertices = [(x1, y1)] rounded = [] codes = [Path.MOVETO] if self.armA: cosA = math.cos(self.angleA / 180. * math.pi) sinA = math.sin(self.angleA / 180. * math.pi) # x_armA, y_armB d = self.armA - self.rad rounded.append((x1 + d * cosA, y1 + d * sinA)) d = self.armA rounded.append((x1 + d * cosA, y1 + d * sinA)) if self.armB: cosB = math.cos(self.angleB / 180. * math.pi) sinB = math.sin(self.angleB / 180. * math.pi) x_armB, y_armB = x2 + self.armB * cosB, y2 + self.armB * sinB if rounded: xp, yp = rounded[-1] dx, dy = x_armB - xp, y_armB - yp dd = (dx * dx + dy * dy) ** .5 rounded.append((xp + self.rad * dx / dd, yp + self.rad * dy / dd)) vertices.extend(rounded) codes.extend([Path.LINETO, Path.CURVE3, Path.CURVE3]) else: xp, yp = vertices[-1] dx, dy = x_armB - xp, y_armB - yp dd = (dx * dx + dy * dy) ** .5 d = dd - self.rad rounded = [(xp + d * dx / dd, yp + d * dy / dd), (x_armB, y_armB)] if rounded: xp, yp = rounded[-1] dx, dy = x2 - xp, y2 - yp dd = (dx * dx + dy * dy) ** .5 rounded.append((xp + self.rad * dx / dd, yp + self.rad * dy / dd)) vertices.extend(rounded) codes.extend([Path.LINETO, Path.CURVE3, Path.CURVE3]) vertices.append((x2, y2)) codes.append(Path.LINETO) return Path(vertices, codes) _style_list["arc"] = Arc class Bar(_Base): """ A line with *angle* between A and B with *armA* and *armB*. One of the arms is extended so that they are connected in a right angle. The length of armA is determined by (*armA* + *fraction* x AB distance). Same for armB. """ def __init__(self, armA=0., armB=0., fraction=0.3, angle=None): """ Parameters ---------- armA : float minimum length of armA armB : float minimum length of armB fraction : float a fraction of the distance between two points that will be added to armA and armB. angle : float or None angle of the connecting line (if None, parallel to A and B) """ self.armA = armA self.armB = armB self.fraction = fraction self.angle = angle def connect(self, posA, posB): x1, y1 = posA x20, y20 = x2, y2 = posB x12, y12 = (x1 + x2) / 2., (y1 + y2) / 2. theta1 = math.atan2(y2 - y1, x2 - x1) dx, dy = x2 - x1, y2 - y1 dd = (dx * dx + dy * dy) ** .5 ddx, ddy = dx / dd, dy / dd armA, armB = self.armA, self.armB if self.angle is not None: #angle = self.angle % 180. #if angle < 0. or angle > 180.: # angle #theta0 = (self.angle%180.)/180.*math.pi theta0 = self.angle / 180. * math.pi #theta0 = (((self.angle+90)%180.) - 90.)/180.*math.pi dtheta = theta1 - theta0 dl = dd * math.sin(dtheta) dL = dd * math.cos(dtheta) #x2, y2 = x2 + dl*ddy, y2 - dl*ddx x2, y2 = x1 + dL * math.cos(theta0), y1 + dL * math.sin(theta0) armB = armB - dl # update dx, dy = x2 - x1, y2 - y1 dd2 = (dx * dx + dy * dy) ** .5 ddx, ddy = dx / dd2, dy / dd2 else: dl = 0. #if armA > armB: # armB = armA + dl #else: # armA = armB - dl arm = max(armA, armB) f = self.fraction * dd + arm #fB = self.fraction*dd + armB cx1, cy1 = x1 + f * ddy, y1 - f * ddx cx2, cy2 = x2 + f * ddy, y2 - f * ddx vertices = [(x1, y1), (cx1, cy1), (cx2, cy2), (x20, y20)] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO] return Path(vertices, codes) _style_list["bar"] = Bar if __doc__: __doc__ = cbook.dedent(__doc__) % \ {"AvailableConnectorstyles": _pprint_styles(_style_list)} def _point_along_a_line(x0, y0, x1, y1, d): """ find a point along a line connecting (x0, y0) -- (x1, y1) whose distance from (x0, y0) is d. """ dx, dy = x0 - x1, y0 - y1 ff = d / (dx * dx + dy * dy) ** .5 x2, y2 = x0 - ff * dx, y0 - ff * dy return x2, y2 class ArrowStyle(_Style): """ :class:`ArrowStyle` is a container class which defines several arrowstyle classes, which is used to create an arrow path along a given path. These are mainly used with :class:`FancyArrowPatch`. A arrowstyle object can be either created as:: ArrowStyle.Fancy(head_length=.4, head_width=.4, tail_width=.4) or:: ArrowStyle("Fancy", head_length=.4, head_width=.4, tail_width=.4) or:: ArrowStyle("Fancy, head_length=.4, head_width=.4, tail_width=.4") The following classes are defined %(AvailableArrowstyles)s An instance of any arrow style class is a callable object, whose call signature is:: __call__(self, path, mutation_size, linewidth, aspect_ratio=1.) and it returns a tuple of a :class:`Path` instance and a boolean value. *path* is a :class:`Path` instance along which the arrow will be drawn. *mutation_size* and *aspect_ratio* have the same meaning as in :class:`BoxStyle`. *linewidth* is a line width to be stroked. This is meant to be used to correct the location of the head so that it does not overshoot the destination point, but not all classes support it. .. plot:: mpl_examples/pylab_examples/fancyarrow_demo.py """ _style_list = {} class _Base(object): """ Arrow Transmuter Base class ArrowTransmuterBase and its derivatives are used to make a fancy arrow around a given path. The __call__ method returns a path (which will be used to create a PathPatch instance) and a boolean value indicating the path is open therefore is not fillable. This class is not an artist and actual drawing of the fancy arrow is done by the FancyArrowPatch class. """ # The derived classes are required to be able to be initialized # w/o arguments, i.e., all its argument (except self) must have # the default values. def __init__(self): super(ArrowStyle._Base, self).__init__() @staticmethod def ensure_quadratic_bezier(path): """ Some ArrowStyle class only wokrs with a simple quaratic bezier curve (created with Arc3Connetion or Angle3Connector). This static method is to check if the provided path is a simple quadratic bezier curve and returns its control points if true. """ segments = list(path.iter_segments()) if ((len(segments) != 2) or (segments[0][1] != Path.MOVETO) or (segments[1][1] != Path.CURVE3)): msg = "'path' it's not a valid quadratic bezier curve" raise ValueError(msg) return list(segments[0][0]) + list(segments[1][0]) def transmute(self, path, mutation_size, linewidth): """ The transmute method is the very core of the ArrowStyle class and must be overriden in the subclasses. It receives the path object along which the arrow will be drawn, and the mutation_size, with which the arrow head etc. will be scaled. The linewidth may be used to adjust the path so that it does not pass beyond the given points. It returns a tuple of a Path instance and a boolean. The boolean value indicate whether the path can be filled or not. The return value can also be a list of paths and list of booleans of a same length. """ raise NotImplementedError('Derived must override') def __call__(self, path, mutation_size, linewidth, aspect_ratio=1.): """ The __call__ method is a thin wrapper around the transmute method and take care of the aspect ratio. """ path = make_path_regular(path) if aspect_ratio is not None: # Squeeze the given height by the aspect_ratio vertices, codes = path.vertices[:], path.codes[:] # Squeeze the height vertices[:, 1] = vertices[:, 1] / aspect_ratio path_shrunk = Path(vertices, codes) # call transmute method with squeezed height. path_mutated, fillable = self.transmute(path_shrunk, linewidth, mutation_size) if cbook.iterable(fillable): path_list = [] for p in zip(path_mutated): v, c = p.vertices, p.codes # Restore the height v[:, 1] = v[:, 1] * aspect_ratio path_list.append(Path(v, c)) return path_list, fillable else: return path_mutated, fillable else: return self.transmute(path, mutation_size, linewidth) def __reduce__(self): # because we have decided to nest thes classes, we need to # add some more information to allow instance pickling. import matplotlib.cbook as cbook return (cbook._NestedClassGetter(), (ArrowStyle, self.__class__.__name__), self.__dict__ ) class _Curve(_Base): """ A simple arrow which will work with any path instance. The returned path is simply concatenation of the original path + at most two paths representing the arrow head at the begin point and the at the end point. The arrow heads can be either open or closed. """ def __init__(self, beginarrow=None, endarrow=None, fillbegin=False, fillend=False, head_length=.2, head_width=.1): """ The arrows are drawn if *beginarrow* and/or *endarrow* are true. *head_length* and *head_width* determines the size of the arrow relative to the *mutation scale*. The arrowhead at the begin (or end) is closed if fillbegin (or fillend) is True. """ self.beginarrow, self.endarrow = beginarrow, endarrow self.head_length, self.head_width = head_length, head_width self.fillbegin, self.fillend = fillbegin, fillend super(ArrowStyle._Curve, self).__init__() def _get_arrow_wedge(self, x0, y0, x1, y1, head_dist, cos_t, sin_t, linewidth ): """ Return the paths for arrow heads. Since arrow lines are drawn with capstyle=projected, The arrow goes beyond the desired point. This method also returns the amount of the path to be shrunken so that it does not overshoot. """ # arrow from x0, y0 to x1, y1 dx, dy = x0 - x1, y0 - y1 cp_distance = np.hypot(dx, dy) # pad_projected : amount of pad to account the # overshooting of the projection of the wedge pad_projected = (.5 * linewidth / sin_t) # Account for division by zero if cp_distance == 0: cp_distance = 1 # apply pad for projected edge ddx = pad_projected * dx / cp_distance ddy = pad_projected * dy / cp_distance # offset for arrow wedge dx = dx / cp_distance * head_dist dy = dy / cp_distance * head_dist dx1, dy1 = cos_t * dx + sin_t * dy, -sin_t * dx + cos_t * dy dx2, dy2 = cos_t * dx - sin_t * dy, sin_t * dx + cos_t * dy vertices_arrow = [(x1 + ddx + dx1, y1 + ddy + dy1), (x1 + ddx, y1 + ddy), (x1 + ddx + dx2, y1 + ddy + dy2)] codes_arrow = [Path.MOVETO, Path.LINETO, Path.LINETO] return vertices_arrow, codes_arrow, ddx, ddy def transmute(self, path, mutation_size, linewidth): head_length, head_width = self.head_length * mutation_size, \ self.head_width * mutation_size head_dist = math.sqrt(head_length ** 2 + head_width ** 2) cos_t, sin_t = head_length / head_dist, head_width / head_dist # begin arrow x0, y0 = path.vertices[0] x1, y1 = path.vertices[1] # If there is no room for an arrow and a line, then skip the arrow has_begin_arrow = (self.beginarrow and not ((x0 == x1) and (y0 == y1))) if has_begin_arrow: verticesA, codesA, ddxA, ddyA = \ self._get_arrow_wedge(x1, y1, x0, y0, head_dist, cos_t, sin_t, linewidth) else: verticesA, codesA = [], [] ddxA, ddyA = 0., 0. # end arrow x2, y2 = path.vertices[-2] x3, y3 = path.vertices[-1] # If there is no room for an arrow and a line, then skip the arrow has_end_arrow = (self.endarrow and not ((x2 == x3) and (y2 == y3))) if has_end_arrow: verticesB, codesB, ddxB, ddyB = \ self._get_arrow_wedge(x2, y2, x3, y3, head_dist, cos_t, sin_t, linewidth) else: verticesB, codesB = [], [] ddxB, ddyB = 0., 0. # this simple code will not work if ddx, ddy is greater than # separation bettern vertices. _path = [Path(np.concatenate([[(x0 + ddxA, y0 + ddyA)], path.vertices[1:-1], [(x3 + ddxB, y3 + ddyB)]]), path.codes)] _fillable = [False] if has_begin_arrow: if self.fillbegin: p = np.concatenate([verticesA, [verticesA[0], verticesA[0]], ]) c = np.concatenate([codesA, [Path.LINETO, Path.CLOSEPOLY]]) _path.append(Path(p, c)) _fillable.append(True) else: _path.append(Path(verticesA, codesA)) _fillable.append(False) if has_end_arrow: if self.fillend: _fillable.append(True) p = np.concatenate([verticesB, [verticesB[0], verticesB[0]], ]) c = np.concatenate([codesB, [Path.LINETO, Path.CLOSEPOLY]]) _path.append(Path(p, c)) else: _fillable.append(False) _path.append(Path(verticesB, codesB)) return _path, _fillable class Curve(_Curve): """ A simple curve without any arrow head. """ def __init__(self): super(ArrowStyle.Curve, self).__init__( beginarrow=False, endarrow=False) _style_list["-"] = Curve class CurveA(_Curve): """ An arrow with a head at its begin point. """ def __init__(self, head_length=.4, head_width=.2): """ *head_length* length of the arrow head *head_width* width of the arrow head """ super(ArrowStyle.CurveA, self).__init__( beginarrow=True, endarrow=False, head_length=head_length, head_width=head_width) _style_list["<-"] = CurveA class CurveB(_Curve): """ An arrow with a head at its end point. """ def __init__(self, head_length=.4, head_width=.2): """ *head_length* length of the arrow head *head_width* width of the arrow head """ super(ArrowStyle.CurveB, self).__init__( beginarrow=False, endarrow=True, head_length=head_length, head_width=head_width) _style_list["->"] = CurveB class CurveAB(_Curve): """ An arrow with heads both at the begin and the end point. """ def __init__(self, head_length=.4, head_width=.2): """ *head_length* length of the arrow head *head_width* width of the arrow head """ super(ArrowStyle.CurveAB, self).__init__( beginarrow=True, endarrow=True, head_length=head_length, head_width=head_width) _style_list["<->"] = CurveAB class CurveFilledA(_Curve): """ An arrow with filled triangle head at the begin. """ def __init__(self, head_length=.4, head_width=.2): """ *head_length* length of the arrow head *head_width* width of the arrow head """ super(ArrowStyle.CurveFilledA, self).__init__( beginarrow=True, endarrow=False, fillbegin=True, fillend=False, head_length=head_length, head_width=head_width) _style_list["<|-"] = CurveFilledA class CurveFilledB(_Curve): """ An arrow with filled triangle head at the end. """ def __init__(self, head_length=.4, head_width=.2): """ *head_length* length of the arrow head *head_width* width of the arrow head """ super(ArrowStyle.CurveFilledB, self).__init__( beginarrow=False, endarrow=True, fillbegin=False, fillend=True, head_length=head_length, head_width=head_width) _style_list["-|>"] = CurveFilledB class CurveFilledAB(_Curve): """ An arrow with filled triangle heads both at the begin and the end point. """ def __init__(self, head_length=.4, head_width=.2): """ *head_length* length of the arrow head *head_width* width of the arrow head """ super(ArrowStyle.CurveFilledAB, self).__init__( beginarrow=True, endarrow=True, fillbegin=True, fillend=True, head_length=head_length, head_width=head_width) _style_list["<|-|>"] = CurveFilledAB class _Bracket(_Base): def __init__(self, bracketA=None, bracketB=None, widthA=1., widthB=1., lengthA=0.2, lengthB=0.2, angleA=None, angleB=None, scaleA=None, scaleB=None): self.bracketA, self.bracketB = bracketA, bracketB self.widthA, self.widthB = widthA, widthB self.lengthA, self.lengthB = lengthA, lengthB self.angleA, self.angleB = angleA, angleB self.scaleA, self.scaleB = scaleA, scaleB def _get_bracket(self, x0, y0, cos_t, sin_t, width, length): # arrow from x0, y0 to x1, y1 from matplotlib.bezier import get_normal_points x1, y1, x2, y2 = get_normal_points(x0, y0, cos_t, sin_t, width) dx, dy = length * cos_t, length * sin_t vertices_arrow = [(x1 + dx, y1 + dy), (x1, y1), (x2, y2), (x2 + dx, y2 + dy)] codes_arrow = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO] return vertices_arrow, codes_arrow def transmute(self, path, mutation_size, linewidth): if self.scaleA is None: scaleA = mutation_size else: scaleA = self.scaleA if self.scaleB is None: scaleB = mutation_size else: scaleB = self.scaleB vertices_list, codes_list = [], [] if self.bracketA: x0, y0 = path.vertices[0] x1, y1 = path.vertices[1] cos_t, sin_t = get_cos_sin(x1, y1, x0, y0) verticesA, codesA = self._get_bracket(x0, y0, cos_t, sin_t, self.widthA * scaleA, self.lengthA * scaleA) vertices_list.append(verticesA) codes_list.append(codesA) vertices_list.append(path.vertices) codes_list.append(path.codes) if self.bracketB: x0, y0 = path.vertices[-1] x1, y1 = path.vertices[-2] cos_t, sin_t = get_cos_sin(x1, y1, x0, y0) verticesB, codesB = self._get_bracket(x0, y0, cos_t, sin_t, self.widthB * scaleB, self.lengthB * scaleB) vertices_list.append(verticesB) codes_list.append(codesB) vertices = np.concatenate(vertices_list) codes = np.concatenate(codes_list) p = Path(vertices, codes) return p, False class BracketAB(_Bracket): """ An arrow with a bracket(]) at both ends. """ def __init__(self, widthA=1., lengthA=0.2, angleA=None, widthB=1., lengthB=0.2, angleB=None): """ *widthA* width of the bracket *lengthA* length of the bracket *angleA* angle between the bracket and the line *widthB* width of the bracket *lengthB* length of the bracket *angleB* angle between the bracket and the line """ super(ArrowStyle.BracketAB, self).__init__( True, True, widthA=widthA, lengthA=lengthA, angleA=angleA, widthB=widthB, lengthB=lengthB, angleB=angleB) _style_list["]-["] = BracketAB class BracketA(_Bracket): """ An arrow with a bracket(]) at its end. """ def __init__(self, widthA=1., lengthA=0.2, angleA=None): """ *widthA* width of the bracket *lengthA* length of the bracket *angleA* angle between the bracket and the line """ super(ArrowStyle.BracketA, self).__init__(True, None, widthA=widthA, lengthA=lengthA, angleA=angleA) _style_list["]-"] = BracketA class BracketB(_Bracket): """ An arrow with a bracket([) at its end. """ def __init__(self, widthB=1., lengthB=0.2, angleB=None): """ *widthB* width of the bracket *lengthB* length of the bracket *angleB* angle between the bracket and the line """ super(ArrowStyle.BracketB, self).__init__(None, True, widthB=widthB, lengthB=lengthB, angleB=angleB) _style_list["-["] = BracketB class BarAB(_Bracket): """ An arrow with a bar(|) at both ends. """ def __init__(self, widthA=1., angleA=None, widthB=1., angleB=None): """ *widthA* width of the bracket *lengthA* length of the bracket *angleA* angle between the bracket and the line *widthB* width of the bracket *lengthB* length of the bracket *angleB* angle between the bracket and the line """ super(ArrowStyle.BarAB, self).__init__( True, True, widthA=widthA, lengthA=0, angleA=angleA, widthB=widthB, lengthB=0, angleB=angleB) _style_list["|-|"] = BarAB class Simple(_Base): """ A simple arrow. Only works with a quadratic bezier curve. """ def __init__(self, head_length=.5, head_width=.5, tail_width=.2): """ *head_length* length of the arrow head *head_with* width of the arrow head *tail_width* width of the arrow tail """ self.head_length, self.head_width, self.tail_width = \ head_length, head_width, tail_width super(ArrowStyle.Simple, self).__init__() def transmute(self, path, mutation_size, linewidth): x0, y0, x1, y1, x2, y2 = self.ensure_quadratic_bezier(path) # divide the path into a head and a tail head_length = self.head_length * mutation_size in_f = inside_circle(x2, y2, head_length) arrow_path = [(x0, y0), (x1, y1), (x2, y2)] from .bezier import NonIntersectingPathException try: arrow_out, arrow_in = \ split_bezier_intersecting_with_closedpath(arrow_path, in_f, tolerence=0.01) except NonIntersectingPathException: # if this happens, make a straight line of the head_length # long. x0, y0 = _point_along_a_line(x2, y2, x1, y1, head_length) x1n, y1n = 0.5 * (x0 + x2), 0.5 * (y0 + y2) arrow_in = [(x0, y0), (x1n, y1n), (x2, y2)] arrow_out = None # head head_width = self.head_width * mutation_size head_left, head_right = make_wedged_bezier2(arrow_in, head_width / 2., wm=.5) # tail if arrow_out is not None: tail_width = self.tail_width * mutation_size tail_left, tail_right = get_parallels(arrow_out, tail_width / 2.) patch_path = [(Path.MOVETO, tail_right[0]), (Path.CURVE3, tail_right[1]), (Path.CURVE3, tail_right[2]), (Path.LINETO, head_right[0]), (Path.CURVE3, head_right[1]), (Path.CURVE3, head_right[2]), (Path.CURVE3, head_left[1]), (Path.CURVE3, head_left[0]), (Path.LINETO, tail_left[2]), (Path.CURVE3, tail_left[1]), (Path.CURVE3, tail_left[0]), (Path.LINETO, tail_right[0]), (Path.CLOSEPOLY, tail_right[0]), ] else: patch_path = [(Path.MOVETO, head_right[0]), (Path.CURVE3, head_right[1]), (Path.CURVE3, head_right[2]), (Path.CURVE3, head_left[1]), (Path.CURVE3, head_left[0]), (Path.CLOSEPOLY, head_left[0]), ] path = Path([p for c, p in patch_path], [c for c, p in patch_path]) return path, True _style_list["simple"] = Simple class Fancy(_Base): """ A fancy arrow. Only works with a quadratic bezier curve. """ def __init__(self, head_length=.4, head_width=.4, tail_width=.4): """ *head_length* length of the arrow head *head_with* width of the arrow head *tail_width* width of the arrow tail """ self.head_length, self.head_width, self.tail_width = \ head_length, head_width, tail_width super(ArrowStyle.Fancy, self).__init__() def transmute(self, path, mutation_size, linewidth): x0, y0, x1, y1, x2, y2 = self.ensure_quadratic_bezier(path) # divide the path into a head and a tail head_length = self.head_length * mutation_size arrow_path = [(x0, y0), (x1, y1), (x2, y2)] from .bezier import NonIntersectingPathException # path for head in_f = inside_circle(x2, y2, head_length) try: path_out, path_in = \ split_bezier_intersecting_with_closedpath( arrow_path, in_f, tolerence=0.01) except NonIntersectingPathException: # if this happens, make a straight line of the head_length # long. x0, y0 = _point_along_a_line(x2, y2, x1, y1, head_length) x1n, y1n = 0.5 * (x0 + x2), 0.5 * (y0 + y2) arrow_path = [(x0, y0), (x1n, y1n), (x2, y2)] path_head = arrow_path else: path_head = path_in # path for head in_f = inside_circle(x2, y2, head_length * .8) path_out, path_in = split_bezier_intersecting_with_closedpath( arrow_path, in_f, tolerence=0.01 ) path_tail = path_out # head head_width = self.head_width * mutation_size head_l, head_r = make_wedged_bezier2(path_head, head_width / 2., wm=.6) # tail tail_width = self.tail_width * mutation_size tail_left, tail_right = make_wedged_bezier2(path_tail, tail_width * .5, w1=1., wm=0.6, w2=0.3) # path for head in_f = inside_circle(x0, y0, tail_width * .3) path_in, path_out = split_bezier_intersecting_with_closedpath( arrow_path, in_f, tolerence=0.01 ) tail_start = path_in[-1] head_right, head_left = head_r, head_l patch_path = [(Path.MOVETO, tail_start), (Path.LINETO, tail_right[0]), (Path.CURVE3, tail_right[1]), (Path.CURVE3, tail_right[2]), (Path.LINETO, head_right[0]), (Path.CURVE3, head_right[1]), (Path.CURVE3, head_right[2]), (Path.CURVE3, head_left[1]), (Path.CURVE3, head_left[0]), (Path.LINETO, tail_left[2]), (Path.CURVE3, tail_left[1]), (Path.CURVE3, tail_left[0]), (Path.LINETO, tail_start), (Path.CLOSEPOLY, tail_start), ] path = Path([p for c, p in patch_path], [c for c, p in patch_path]) return path, True _style_list["fancy"] = Fancy class Wedge(_Base): """ Wedge(?) shape. Only works with a quadratic bezier curve. The begin point has a width of the tail_width and the end point has a width of 0. At the middle, the width is shrink_factor*tail_width. """ def __init__(self, tail_width=.3, shrink_factor=0.5): """ *tail_width* width of the tail *shrink_factor* fraction of the arrow width at the middle point """ self.tail_width = tail_width self.shrink_factor = shrink_factor super(ArrowStyle.Wedge, self).__init__() def transmute(self, path, mutation_size, linewidth): x0, y0, x1, y1, x2, y2 = self.ensure_quadratic_bezier(path) arrow_path = [(x0, y0), (x1, y1), (x2, y2)] b_plus, b_minus = make_wedged_bezier2( arrow_path, self.tail_width * mutation_size / 2., wm=self.shrink_factor) patch_path = [(Path.MOVETO, b_plus[0]), (Path.CURVE3, b_plus[1]), (Path.CURVE3, b_plus[2]), (Path.LINETO, b_minus[2]), (Path.CURVE3, b_minus[1]), (Path.CURVE3, b_minus[0]), (Path.CLOSEPOLY, b_minus[0]), ] path = Path([p for c, p in patch_path], [c for c, p in patch_path]) return path, True _style_list["wedge"] = Wedge if __doc__: __doc__ = cbook.dedent(__doc__) % \ {"AvailableArrowstyles": _pprint_styles(_style_list)} docstring.interpd.update( AvailableArrowstyles=_pprint_styles(ArrowStyle._style_list), AvailableConnectorstyles=_pprint_styles(ConnectionStyle._style_list), ) class FancyArrowPatch(Patch): """ A fancy arrow patch. It draws an arrow using the :class:ArrowStyle. """ _edge_default = True def __str__(self): if self._posA_posB is not None: (x1, y1), (x2, y2) = self._posA_posB return self.__class__.__name__ \ + "(%g,%g->%g,%g)" % (x1, y1, x2, y2) else: return self.__class__.__name__ \ + "(%s)" % (str(self._path_original),) @docstring.dedent_interpd def __init__(self, posA=None, posB=None, path=None, arrowstyle="simple", arrow_transmuter=None, connectionstyle="arc3", connector=None, patchA=None, patchB=None, shrinkA=2., shrinkB=2., mutation_scale=1., mutation_aspect=None, dpi_cor=1., **kwargs): """ If *posA* and *posB* is given, a path connecting two point are created according to the connectionstyle. The path will be clipped with *patchA* and *patchB* and further shrunken by *shrinkA* and *shrinkB*. An arrow is drawn along this resulting path using the *arrowstyle* parameter. If *path* provided, an arrow is drawn along this path and *patchA*, *patchB*, *shrinkA*, and *shrinkB* are ignored. The *connectionstyle* describes how *posA* and *posB* are connected. It can be an instance of the ConnectionStyle class (matplotlib.patches.ConnectionStlye) or a string of the connectionstyle name, with optional comma-separated attributes. The following connection styles are available. %(AvailableConnectorstyles)s The *arrowstyle* describes how the fancy arrow will be drawn. It can be string of the available arrowstyle names, with optional comma-separated attributes, or one of the ArrowStyle instance. The optional attributes are meant to be scaled with the *mutation_scale*. The following arrow styles are available. %(AvailableArrowstyles)s *mutation_scale* : a value with which attributes of arrowstyle (e.g., head_length) will be scaled. default=1. *mutation_aspect* : The height of the rectangle will be squeezed by this value before the mutation and the mutated box will be stretched by the inverse of it. default=None. Valid kwargs are: %(Patch)s """ Patch.__init__(self, **kwargs) if posA is not None and posB is not None and path is None: self._posA_posB = [posA, posB] if connectionstyle is None: connectionstyle = "arc3" self.set_connectionstyle(connectionstyle) elif posA is None and posB is None and path is not None: self._posA_posB = None self._connetors = None else: raise ValueError("either posA and posB, or path need to provided") self.patchA = patchA self.patchB = patchB self.shrinkA = shrinkA self.shrinkB = shrinkB self._path_original = path self.set_arrowstyle(arrowstyle) self._mutation_scale = mutation_scale self._mutation_aspect = mutation_aspect self.set_dpi_cor(dpi_cor) #self._draw_in_display_coordinate = True def set_dpi_cor(self, dpi_cor): """ dpi_cor is currently used for linewidth-related things and shrink factor. Mutation scale is affected by this. """ self._dpi_cor = dpi_cor self.stale = True def get_dpi_cor(self): """ dpi_cor is currently used for linewidth-related things and shrink factor. Mutation scale is affected by this. """ return self._dpi_cor def set_positions(self, posA, posB): """ set the begin and end positions of the connecting path. Use current value if None. """ if posA is not None: self._posA_posB[0] = posA if posB is not None: self._posA_posB[1] = posB self.stale = True def set_patchA(self, patchA): """ set the begin patch. """ self.patchA = patchA self.stale = True def set_patchB(self, patchB): """ set the begin patch """ self.patchB = patchB self.stale = True def set_connectionstyle(self, connectionstyle, **kw): """ Set the connection style. *connectionstyle* can be a string with connectionstyle name with optional comma-separated attributes. Alternatively, the attrs can be provided as keywords. set_connectionstyle("arc,angleA=0,armA=30,rad=10") set_connectionstyle("arc", angleA=0,armA=30,rad=10) Old attrs simply are forgotten. Without argument (or with connectionstyle=None), return available styles as a list of strings. """ if connectionstyle is None: return ConnectionStyle.pprint_styles() if isinstance(connectionstyle, ConnectionStyle._Base): self._connector = connectionstyle elif six.callable(connectionstyle): # we may need check the calling convention of the given function self._connector = connectionstyle else: self._connector = ConnectionStyle(connectionstyle, **kw) self.stale = True def get_connectionstyle(self): """ Return the ConnectionStyle instance """ return self._connector def set_arrowstyle(self, arrowstyle=None, **kw): """ Set the arrow style. *arrowstyle* can be a string with arrowstyle name with optional comma-separated attributes. Alternatively, the attrs can be provided as keywords. set_arrowstyle("Fancy,head_length=0.2") set_arrowstyle("fancy", head_length=0.2) Old attrs simply are forgotten. Without argument (or with arrowstyle=None), return available box styles as a list of strings. """ if arrowstyle is None: return ArrowStyle.pprint_styles() if isinstance(arrowstyle, ArrowStyle._Base): self._arrow_transmuter = arrowstyle else: self._arrow_transmuter = ArrowStyle(arrowstyle, **kw) self.stale = True def get_arrowstyle(self): """ Return the arrowstyle object """ return self._arrow_transmuter def set_mutation_scale(self, scale): """ Set the mutation scale. ACCEPTS: float """ self._mutation_scale = scale self.stale = True def get_mutation_scale(self): """ Return the mutation scale. """ return self._mutation_scale def set_mutation_aspect(self, aspect): """ Set the aspect ratio of the bbox mutation. ACCEPTS: float """ self._mutation_aspect = aspect self.stale = True def get_mutation_aspect(self): """ Return the aspect ratio of the bbox mutation. """ return self._mutation_aspect def get_path(self): """ return the path of the arrow in the data coordinate. Use get_path_in_displaycoord() method to retrieve the arrow path in the display coord. """ _path, fillable = self.get_path_in_displaycoord() if cbook.iterable(fillable): _path = concatenate_paths(_path) return self.get_transform().inverted().transform_path(_path) def get_path_in_displaycoord(self): """ Return the mutated path of the arrow in the display coord """ dpi_cor = self.get_dpi_cor() if self._posA_posB is not None: posA = self.get_transform().transform_point(self._posA_posB[0]) posB = self.get_transform().transform_point(self._posA_posB[1]) _path = self.get_connectionstyle()(posA, posB, patchA=self.patchA, patchB=self.patchB, shrinkA=self.shrinkA * dpi_cor, shrinkB=self.shrinkB * dpi_cor ) else: _path = self.get_transform().transform_path(self._path_original) _path, fillable = self.get_arrowstyle()( _path, self.get_mutation_scale() * dpi_cor, self.get_linewidth() * dpi_cor, self.get_mutation_aspect() ) #if not fillable: # self._fill = False return _path, fillable def draw(self, renderer): if not self.get_visible(): return renderer.open_group('patch', self.get_gid()) gc = renderer.new_gc() gc.set_foreground(self._edgecolor, isRGBA=True) lw = self._linewidth if self._edgecolor[3] == 0: lw = 0 gc.set_linewidth(lw) gc.set_dashes(self._dashoffset, self._dashes) gc.set_antialiased(self._antialiased) self._set_gc_clip(gc) gc.set_capstyle('round') gc.set_snap(self.get_snap()) rgbFace = self._facecolor if rgbFace[3] == 0: rgbFace = None # (some?) renderers expect this as no-fill signal gc.set_alpha(self._alpha) if self._hatch: gc.set_hatch(self._hatch) if self.get_sketch_params() is not None: gc.set_sketch_params(*self.get_sketch_params()) # FIXME : dpi_cor is for the dpi-dependecy of the # linewidth. There could be room for improvement. # #dpi_cor = renderer.points_to_pixels(1.) self.set_dpi_cor(renderer.points_to_pixels(1.)) path, fillable = self.get_path_in_displaycoord() if not cbook.iterable(fillable): path = [path] fillable = [fillable] affine = transforms.IdentityTransform() if self.get_path_effects(): from matplotlib.patheffects import PathEffectRenderer renderer = PathEffectRenderer(self.get_path_effects(), renderer) for p, f in zip(path, fillable): if f: renderer.draw_path(gc, p, affine, rgbFace) else: renderer.draw_path(gc, p, affine, None) gc.restore() renderer.close_group('patch') self.stale = False class ConnectionPatch(FancyArrowPatch): """ A :class:`~matplotlib.patches.ConnectionPatch` class is to make connecting lines between two points (possibly in different axes). """ def __str__(self): return "ConnectionPatch((%g,%g),(%g,%g))" % \ (self.xy1[0], self.xy1[1], self.xy2[0], self.xy2[1]) @docstring.dedent_interpd def __init__(self, xyA, xyB, coordsA, coordsB=None, axesA=None, axesB=None, arrowstyle="-", arrow_transmuter=None, connectionstyle="arc3", connector=None, patchA=None, patchB=None, shrinkA=0., shrinkB=0., mutation_scale=10., mutation_aspect=None, clip_on=False, dpi_cor=1., **kwargs): """ Connect point *xyA* in *coordsA* with point *xyB* in *coordsB* Valid keys are =============== ====================================================== Key Description =============== ====================================================== arrowstyle the arrow style connectionstyle the connection style relpos default is (0.5, 0.5) patchA default is bounding box of the text patchB default is None shrinkA default is 2 points shrinkB default is 2 points mutation_scale default is text size (in points) mutation_aspect default is 1. ? any key for :class:`matplotlib.patches.PathPatch` =============== ====================================================== *coordsA* and *coordsB* are strings that indicate the coordinates of *xyA* and *xyB*. ================= =================================================== Property Description ================= =================================================== 'figure points' points from the lower left corner of the figure 'figure pixels' pixels from the lower left corner of the figure 'figure fraction' 0,0 is lower left of figure and 1,1 is upper, right 'axes points' points from lower left corner of axes 'axes pixels' pixels from lower left corner of axes 'axes fraction' 0,1 is lower left of axes and 1,1 is upper right 'data' use the coordinate system of the object being annotated (default) 'offset points' Specify an offset (in points) from the *xy* value 'polar' you can specify *theta*, *r* for the annotation, even in cartesian plots. Note that if you are using a polar axes, you do not need to specify polar for the coordinate system since that is the native "data" coordinate system. ================= =================================================== """ if coordsB is None: coordsB = coordsA # we'll draw ourself after the artist we annotate by default self.xy1 = xyA self.xy2 = xyB self.coords1 = coordsA self.coords2 = coordsB self.axesA = axesA self.axesB = axesB FancyArrowPatch.__init__(self, posA=(0, 0), posB=(1, 1), arrowstyle=arrowstyle, arrow_transmuter=arrow_transmuter, connectionstyle=connectionstyle, connector=connector, patchA=patchA, patchB=patchB, shrinkA=shrinkA, shrinkB=shrinkB, mutation_scale=mutation_scale, mutation_aspect=mutation_aspect, clip_on=clip_on, dpi_cor=dpi_cor, **kwargs) # if True, draw annotation only if self.xy is inside the axes self._annotation_clip = None def _get_xy(self, x, y, s, axes=None): """ caculate the pixel position of given point """ if axes is None: axes = self.axes if s == 'data': trans = axes.transData x = float(self.convert_xunits(x)) y = float(self.convert_yunits(y)) return trans.transform_point((x, y)) elif s == 'offset points': # convert the data point dx, dy = self.xy # prevent recursion if self.xycoords == 'offset points': return self._get_xy(dx, dy, 'data') dx, dy = self._get_xy(dx, dy, self.xycoords) # convert the offset dpi = self.figure.get_dpi() x *= dpi / 72. y *= dpi / 72. # add the offset to the data point x += dx y += dy return x, y elif s == 'polar': theta, r = x, y x = r * np.cos(theta) y = r * np.sin(theta) trans = axes.transData return trans.transform_point((x, y)) elif s == 'figure points': # points from the lower left corner of the figure dpi = self.figure.dpi l, b, w, h = self.figure.bbox.bounds r = l + w t = b + h x *= dpi / 72. y *= dpi / 72. if x < 0: x = r + x if y < 0: y = t + y return x, y elif s == 'figure pixels': # pixels from the lower left corner of the figure l, b, w, h = self.figure.bbox.bounds r = l + w t = b + h if x < 0: x = r + x if y < 0: y = t + y return x, y elif s == 'figure fraction': # (0,0) is lower left, (1,1) is upper right of figure trans = self.figure.transFigure return trans.transform_point((x, y)) elif s == 'axes points': # points from the lower left corner of the axes dpi = self.figure.dpi l, b, w, h = axes.bbox.bounds r = l + w t = b + h if x < 0: x = r + x * dpi / 72. else: x = l + x * dpi / 72. if y < 0: y = t + y * dpi / 72. else: y = b + y * dpi / 72. return x, y elif s == 'axes pixels': #pixels from the lower left corner of the axes l, b, w, h = axes.bbox.bounds r = l + w t = b + h if x < 0: x = r + x else: x = l + x if y < 0: y = t + y else: y = b + y return x, y elif s == 'axes fraction': #(0,0) is lower left, (1,1) is upper right of axes trans = axes.transAxes return trans.transform_point((x, y)) def set_annotation_clip(self, b): """ set *annotation_clip* attribute. * True: the annotation will only be drawn when self.xy is inside the axes. * False: the annotation will always be drawn regardless of its position. * None: the self.xy will be checked only if *xycoords* is "data" """ self._annotation_clip = b self.stale = True def get_annotation_clip(self): """ Return *annotation_clip* attribute. See :meth:`set_annotation_clip` for the meaning of return values. """ return self._annotation_clip def get_path_in_displaycoord(self): """ Return the mutated path of the arrow in the display coord """ dpi_cor = self.get_dpi_cor() x, y = self.xy1 posA = self._get_xy(x, y, self.coords1, self.axesA) x, y = self.xy2 posB = self._get_xy(x, y, self.coords2, self.axesB) _path = self.get_connectionstyle()(posA, posB, patchA=self.patchA, patchB=self.patchB, shrinkA=self.shrinkA * dpi_cor, shrinkB=self.shrinkB * dpi_cor ) _path, fillable = self.get_arrowstyle()( _path, self.get_mutation_scale() * dpi_cor, self.get_linewidth() * dpi_cor, self.get_mutation_aspect() ) return _path, fillable def _check_xy(self, renderer): """ check if the annotation need to be drawn. """ b = self.get_annotation_clip() if b or (b is None and self.coords1 == "data"): x, y = self.xy1 xy_pixel = self._get_xy(x, y, self.coords1, self.axesA) if not self.axes.contains_point(xy_pixel): return False if b or (b is None and self.coords2 == "data"): x, y = self.xy2 xy_pixel = self._get_xy(x, y, self.coords2, self.axesB) if self.axesB is None: axes = self.axes else: axes = self.axesB if not axes.contains_point(xy_pixel): return False return True def draw(self, renderer): """ Draw. """ if renderer is not None: self._renderer = renderer if not self.get_visible(): return if not self._check_xy(renderer): return FancyArrowPatch.draw(self, renderer)
gpl-3.0
emdodds/LCAversions
timing.py
1
3230
#This file will time various versions of LCA from __future__ import division import numpy as np import sklearn.preprocessing as skp from timeit import default_timer as timer from LCAnumpy import lca as lcan from LCAfortran import lca as lcaf from LCAnumbaprog import lca as lcag def main(): """Profiles various versions of LCA.""" nshort = 6 tshort = 2 nmed = 3 tmed = 6 nlong = 1 #Setup variables for inference numDict = int(2048) numBatch = int(128) dataSize = int(256) dictsIn = np.random.randn(numDict,dataSize) # LCA requires that dictionary be unit norm dictsIn = skp.normalize(dictsIn, axis=1) stimuli = np.random.randn(numBatch,dataSize) batchCoeffs = np.random.randn(numBatch,numDict) coeffs = np.zeros((numBatch, numDict)) eta = .01 lamb = .05 nIter = 300 adapt = .99 softThresh = 0 thresh = np.random.randn(numBatch) #LCA params = """Parameters: numDict: """+str(numDict)+""" numBatch: """+str(numBatch)+""" dataSize: """+str(dataSize)+""" nIter: """+str(nIter)+"""\n""" print params start = timer() lcan.infer(dictsIn,stimuli,eta,lamb,nIter,adapt) dt = timer()-start if dt < tshort: n_times = nshort elif dt < tmed: n_times = nmed else: n_times = nlong for ii in xrange(n_times-1): start = timer() lcan.infer(dictsIn,stimuli,eta,lamb,nIter,adapt) dt = dt+timer()-start dt = dt/(n_times) print '---------------Numpy based LCA----------------' print 'Average time over '+str(n_times)+' trials:' print '%f s' % dt dictsIn = np.array(dictsIn,order='F') stimuli = np.array(stimuli,order='F') coeffs = np.array(coeffs,order='F') batchCoeffs = np.array(batchCoeffs,order='F') thresh = np.array(thresh,order='F') start = timer() lcaf.lca(dictsIn,stimuli,eta,lamb,nIter,softThresh,adapt,coeffs,batchCoeffs,thresh,numDict,numBatch,dataSize) dt = timer()-start if dt < tshort: n_times = nshort elif dt < tmed: n_times = nmed else: n_times = nlong for ii in xrange(n_times-1): start = timer() lcaf.lca(dictsIn,stimuli,eta,lamb,nIter,softThresh,adapt,coeffs,batchCoeffs,thresh,numDict,numBatch,dataSize) dt = dt+timer()-start dt = dt/(n_times) print '---------------Fortran based LCA--------------' print 'Average time over '+str(n_times)+' trials:' print '%f s' % dt dictsIn = np.array(dictsIn,dtype=np.float32,order='F') stimuli = np.array(stimuli,dtype=np.float32,order='F') start = timer() lcag.infer(dictsIn,stimuli,eta,lamb,nIter,adapt) dt = timer()-start if dt < tshort: n_times = nshort elif dt < tmed: n_times = nmed else: n_times = nlong for ii in xrange(n_times-1): start = timer() lcag.infer(dictsIn,stimuli,eta,lamb,nIter,adapt) dt = dt+timer()-start dt = dt/(n_times) print '----------------GPU based LCA-----------------' print 'Average time over '+str(n_times)+' trials:' print '%f s' % dt if __name__ == '__main__': main()
mit
otmaneJai/Zipline
zipline/sources/data_frame_source.py
26
5253
# # Copyright 2015 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Tools to generate data sources. """ import numpy as np import pandas as pd from zipline.gens.utils import hash_args from zipline.sources.data_source import DataSource class DataFrameSource(DataSource): """ Data source that yields from a pandas DataFrame. :Axis layout: * columns : sids * index : datetime :Note: Bars where the price is nan are filtered out. """ def __init__(self, data, **kwargs): assert isinstance(data.index, pd.tseries.index.DatetimeIndex) # Only accept integer SIDs as the items of the DataFrame assert isinstance(data.columns, pd.Int64Index) # TODO is ffilling correct/necessary? # Forward fill prices self.data = data.fillna(method='ffill') # Unpack config dictionary with default values. self.start = kwargs.get('start', self.data.index[0]) self.end = kwargs.get('end', self.data.index[-1]) self.sids = self.data.columns # Hash_value for downstream sorting. self.arg_string = hash_args(data, **kwargs) self._raw_data = None self.started_sids = set() @property def mapping(self): return { 'dt': (lambda x: x, 'dt'), 'sid': (lambda x: x, 'sid'), 'price': (float, 'price'), 'volume': (int, 'volume'), } @property def instance_hash(self): return self.arg_string def raw_data_gen(self): for dt, series in self.data.iterrows(): for sid, price in series.iteritems(): # Skip SIDs that can not be forward filled if np.isnan(price) and \ sid not in self.started_sids: continue self.started_sids.add(sid) event = { 'dt': dt, 'sid': sid, 'price': price, # Just chose something large # if no volume available. 'volume': 1e9, } yield event @property def raw_data(self): if not self._raw_data: self._raw_data = self.raw_data_gen() return self._raw_data class DataPanelSource(DataSource): """ Data source that yields from a pandas Panel. :Axis layout: * items : sids * major_axis : datetime * minor_axis : price, volume, ... :Note: Bars where the price is nan are filtered out. """ def __init__(self, data, **kwargs): assert isinstance(data.major_axis, pd.tseries.index.DatetimeIndex) # Only accept integer SIDs as the items of the Panel assert isinstance(data.items, pd.Int64Index) # TODO is ffilling correct/necessary? # forward fill with volumes of 0 self.data = data.fillna(value={'volume': 0}) self.data = self.data.fillna(method='ffill') # Unpack config dictionary with default values. self.start = kwargs.get('start', self.data.major_axis[0]) self.end = kwargs.get('end', self.data.major_axis[-1]) self.sids = self.data.items # Hash_value for downstream sorting. self.arg_string = hash_args(data, **kwargs) self._raw_data = None self.started_sids = set() @property def mapping(self): mapping = { 'dt': (lambda x: x, 'dt'), 'sid': (lambda x: x, 'sid'), 'price': (float, 'price'), 'volume': (int, 'volume'), } # Add additional fields. for field_name in self.data.minor_axis: if field_name in ['price', 'volume', 'dt', 'sid']: continue mapping[field_name] = (lambda x: x, field_name) return mapping @property def instance_hash(self): return self.arg_string def raw_data_gen(self): for dt in self.data.major_axis: df = self.data.major_xs(dt) for sid, series in df.iteritems(): # Skip SIDs that can not be forward filled if np.isnan(series['price']) and \ sid not in self.started_sids: continue self.started_sids.add(sid) event = { 'dt': dt, 'sid': sid, } for field_name, value in series.iteritems(): event[field_name] = value yield event @property def raw_data(self): if not self._raw_data: self._raw_data = self.raw_data_gen() return self._raw_data
apache-2.0
GuessWhoSamFoo/pandas
pandas/tests/tslibs/test_parsing.py
2
5799
# -*- coding: utf-8 -*- """ Tests for Timestamp parsing, aimed at pandas/_libs/tslibs/parsing.pyx """ from datetime import datetime from dateutil.parser import parse import numpy as np import pytest from pandas._libs.tslibs import parsing from pandas._libs.tslibs.parsing import parse_time_string import pandas.util._test_decorators as td from pandas.util import testing as tm def test_parse_time_string(): (date, parsed, reso) = parse_time_string("4Q1984") (date_lower, parsed_lower, reso_lower) = parse_time_string("4q1984") assert date == date_lower assert reso == reso_lower assert parsed == parsed_lower @pytest.mark.parametrize("dashed,normal", [ ("1988-Q2", "1988Q2"), ("2Q-1988", "2Q1988") ]) def test_parse_time_quarter_with_dash(dashed, normal): # see gh-9688 (date_dash, parsed_dash, reso_dash) = parse_time_string(dashed) (date, parsed, reso) = parse_time_string(normal) assert date_dash == date assert parsed_dash == parsed assert reso_dash == reso @pytest.mark.parametrize("dashed", [ "-2Q1992", "2-Q1992", "4-4Q1992" ]) def test_parse_time_quarter_with_dash_error(dashed): msg = ("Unknown datetime string format, " "unable to parse: {dashed}".format(dashed=dashed)) with pytest.raises(parsing.DateParseError, match=msg): parse_time_string(dashed) @pytest.mark.parametrize("date_string,expected", [ ("123.1234", False), ("-50000", False), ("999", False), ("m", False), ("T", False), ("Mon Sep 16, 2013", True), ("2012-01-01", True), ("01/01/2012", True), ("01012012", True), ("0101", True), ("1-1", True) ]) def test_does_not_convert_mixed_integer(date_string, expected): assert parsing._does_string_look_like_datetime(date_string) is expected @pytest.mark.parametrize("date_str,kwargs,msg", [ ("2013Q5", dict(), ("Incorrect quarterly string is given, " "quarter must be between 1 and 4: 2013Q5")), # see gh-5418 ("2013Q1", dict(freq="INVLD-L-DEC-SAT"), ("Unable to retrieve month information " "from given freq: INVLD-L-DEC-SAT")) ]) def test_parsers_quarterly_with_freq_error(date_str, kwargs, msg): with pytest.raises(parsing.DateParseError, match=msg): parsing.parse_time_string(date_str, **kwargs) @pytest.mark.parametrize("date_str,freq,expected", [ ("2013Q2", None, datetime(2013, 4, 1)), ("2013Q2", "A-APR", datetime(2012, 8, 1)), ("2013-Q2", "A-DEC", datetime(2013, 4, 1)) ]) def test_parsers_quarterly_with_freq(date_str, freq, expected): result, _, _ = parsing.parse_time_string(date_str, freq=freq) assert result == expected @pytest.mark.parametrize("date_str", [ "2Q 2005", "2Q-200A", "2Q-200", "22Q2005", "2Q200.", "6Q-20" ]) def test_parsers_quarter_invalid(date_str): if date_str == "6Q-20": msg = ("Incorrect quarterly string is given, quarter " "must be between 1 and 4: {date_str}".format(date_str=date_str)) else: msg = ("Unknown datetime string format, unable " "to parse: {date_str}".format(date_str=date_str)) with pytest.raises(ValueError, match=msg): parsing.parse_time_string(date_str) @pytest.mark.parametrize("date_str,expected", [ ("201101", datetime(2011, 1, 1, 0, 0)), ("200005", datetime(2000, 5, 1, 0, 0)) ]) def test_parsers_month_freq(date_str, expected): result, _, _ = parsing.parse_time_string(date_str, freq="M") assert result == expected @td.skip_if_not_us_locale @pytest.mark.parametrize("string,fmt", [ ("20111230", "%Y%m%d"), ("2011-12-30", "%Y-%m-%d"), ("30-12-2011", "%d-%m-%Y"), ("2011-12-30 00:00:00", "%Y-%m-%d %H:%M:%S"), ("2011-12-30T00:00:00", "%Y-%m-%dT%H:%M:%S"), ("2011-12-30 00:00:00.000000", "%Y-%m-%d %H:%M:%S.%f") ]) def test_guess_datetime_format_with_parseable_formats(string, fmt): result = parsing._guess_datetime_format(string) assert result == fmt @pytest.mark.parametrize("dayfirst,expected", [ (True, "%d/%m/%Y"), (False, "%m/%d/%Y") ]) def test_guess_datetime_format_with_dayfirst(dayfirst, expected): ambiguous_string = "01/01/2011" result = parsing._guess_datetime_format(ambiguous_string, dayfirst=dayfirst) assert result == expected @td.skip_if_has_locale @pytest.mark.parametrize("string,fmt", [ ("30/Dec/2011", "%d/%b/%Y"), ("30/December/2011", "%d/%B/%Y"), ("30/Dec/2011 00:00:00", "%d/%b/%Y %H:%M:%S") ]) def test_guess_datetime_format_with_locale_specific_formats(string, fmt): result = parsing._guess_datetime_format(string) assert result == fmt @pytest.mark.parametrize("invalid_dt", [ "2013", "01/2013", "12:00:00", "1/1/1/1", "this_is_not_a_datetime", "51a", 9, datetime(2011, 1, 1) ]) def test_guess_datetime_format_invalid_inputs(invalid_dt): # A datetime string must include a year, month and a day for it to be # guessable, in addition to being a string that looks like a datetime. assert parsing._guess_datetime_format(invalid_dt) is None @pytest.mark.parametrize("string,fmt", [ ("2011-1-1", "%Y-%m-%d"), ("1/1/2011", "%m/%d/%Y"), ("30-1-2011", "%d-%m-%Y"), ("2011-1-1 0:0:0", "%Y-%m-%d %H:%M:%S"), ("2011-1-3T00:00:0", "%Y-%m-%dT%H:%M:%S"), ("2011-1-1 00:00:00", "%Y-%m-%d %H:%M:%S") ]) def test_guess_datetime_format_no_padding(string, fmt): # see gh-11142 result = parsing._guess_datetime_format(string) assert result == fmt def test_try_parse_dates(): arr = np.array(["5/1/2000", "6/1/2000", "7/1/2000"], dtype=object) result = parsing.try_parse_dates(arr, dayfirst=True) expected = np.array([parse(d, dayfirst=True) for d in arr]) tm.assert_numpy_array_equal(result, expected)
bsd-3-clause
xubenben/data-science-from-scratch
code/clustering.py
60
6438
from __future__ import division from linear_algebra import squared_distance, vector_mean, distance import math, random import matplotlib.image as mpimg import matplotlib.pyplot as plt class KMeans: """performs k-means clustering""" def __init__(self, k): self.k = k # number of clusters self.means = None # means of clusters def classify(self, input): """return the index of the cluster closest to the input""" return min(range(self.k), key=lambda i: squared_distance(input, self.means[i])) def train(self, inputs): self.means = random.sample(inputs, self.k) assignments = None while True: # Find new assignments new_assignments = map(self.classify, inputs) # If no assignments have changed, we're done. if assignments == new_assignments: return # Otherwise keep the new assignments, assignments = new_assignments for i in range(self.k): i_points = [p for p, a in zip(inputs, assignments) if a == i] # avoid divide-by-zero if i_points is empty if i_points: self.means[i] = vector_mean(i_points) def squared_clustering_errors(inputs, k): """finds the total squared error from k-means clustering the inputs""" clusterer = KMeans(k) clusterer.train(inputs) means = clusterer.means assignments = map(clusterer.classify, inputs) return sum(squared_distance(input,means[cluster]) for input, cluster in zip(inputs, assignments)) def plot_squared_clustering_errors(plt): ks = range(1, len(inputs) + 1) errors = [squared_clustering_errors(inputs, k) for k in ks] plt.plot(ks, errors) plt.xticks(ks) plt.xlabel("k") plt.ylabel("total squared error") plt.show() # # using clustering to recolor an image # def recolor_image(input_file, k=5): img = mpimg.imread(path_to_png_file) pixels = [pixel for row in img for pixel in row] clusterer = KMeans(k) clusterer.train(pixels) # this might take a while def recolor(pixel): cluster = clusterer.classify(pixel) # index of the closest cluster return clusterer.means[cluster] # mean of the closest cluster new_img = [[recolor(pixel) for pixel in row] for row in img] plt.imshow(new_img) plt.axis('off') plt.show() # # hierarchical clustering # def is_leaf(cluster): """a cluster is a leaf if it has length 1""" return len(cluster) == 1 def get_children(cluster): """returns the two children of this cluster if it's a merged cluster; raises an exception if this is a leaf cluster""" if is_leaf(cluster): raise TypeError("a leaf cluster has no children") else: return cluster[1] def get_values(cluster): """returns the value in this cluster (if it's a leaf cluster) or all the values in the leaf clusters below it (if it's not)""" if is_leaf(cluster): return cluster # is already a 1-tuple containing value else: return [value for child in get_children(cluster) for value in get_values(child)] def cluster_distance(cluster1, cluster2, distance_agg=min): """finds the aggregate distance between elements of cluster1 and elements of cluster2""" return distance_agg([distance(input1, input2) for input1 in get_values(cluster1) for input2 in get_values(cluster2)]) def get_merge_order(cluster): if is_leaf(cluster): return float('inf') else: return cluster[0] # merge_order is first element of 2-tuple def bottom_up_cluster(inputs, distance_agg=min): # start with every input a leaf cluster / 1-tuple clusters = [(input,) for input in inputs] # as long as we have more than one cluster left... while len(clusters) > 1: # find the two closest clusters c1, c2 = min([(cluster1, cluster2) for i, cluster1 in enumerate(clusters) for cluster2 in clusters[:i]], key=lambda (x, y): cluster_distance(x, y, distance_agg)) # remove them from the list of clusters clusters = [c for c in clusters if c != c1 and c != c2] # merge them, using merge_order = # of clusters left merged_cluster = (len(clusters), [c1, c2]) # and add their merge clusters.append(merged_cluster) # when there's only one cluster left, return it return clusters[0] def generate_clusters(base_cluster, num_clusters): # start with a list with just the base cluster clusters = [base_cluster] # as long as we don't have enough clusters yet... while len(clusters) < num_clusters: # choose the last-merged of our clusters next_cluster = min(clusters, key=get_merge_order) # remove it from the list clusters = [c for c in clusters if c != next_cluster] # and add its children to the list (i.e., unmerge it) clusters.extend(get_children(next_cluster)) # once we have enough clusters... return clusters if __name__ == "__main__": inputs = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]] random.seed(0) # so you get the same results as me clusterer = KMeans(3) clusterer.train(inputs) print "3-means:" print clusterer.means print random.seed(0) clusterer = KMeans(2) clusterer.train(inputs) print "2-means:" print clusterer.means print print "errors as a function of k" for k in range(1, len(inputs) + 1): print k, squared_clustering_errors(inputs, k) print print "bottom up hierarchical clustering" base_cluster = bottom_up_cluster(inputs) print base_cluster print print "three clusters, min:" for cluster in generate_clusters(base_cluster, 3): print get_values(cluster) print print "three clusters, max:" base_cluster = bottom_up_cluster(inputs, max) for cluster in generate_clusters(base_cluster, 3): print get_values(cluster)
unlicense
clairetang6/bokeh
bokeh/charts/builders/bar_builder.py
5
12416
"""This is the Bokeh charts interface. It gives you a high level API to build complex plot is a simple way. This is the Bar class which lets you build your Bar charts just passing the arguments to the Chart class and calling the proper functions. It also add a new chained stacked method. """ # ----------------------------------------------------------------------------- # Copyright (c) 2012 - 2014, Continuum Analytics, Inc. All rights reserved. # # Powered by the Bokeh Development Team. # # The full license is in the file LICENSE.txt, distributed with this software. # ----------------------------------------------------------------------------- # ----------------------------------------------------------------------------- # Imports # ----------------------------------------------------------------------------- from __future__ import absolute_import, print_function, division from ..builder import Builder, create_and_build from ...models import FactorRange, Range1d from ..glyphs import BarGlyph from ...core.properties import Float, Enum, Bool, Override from ..properties import Dimension from ..attributes import ColorAttr, CatAttr from ..operations import Stack, Dodge from ...core.enums import Aggregation from ..stats import stats from ...models.sources import ColumnDataSource from ..utils import help # ----------------------------------------------------------------------------- # Classes and functions # ----------------------------------------------------------------------------- class BarBuilder(Builder): """This is the Bar builder and it is in charge of plotting Bar chart (grouped and stacked) in an easy and intuitive way. Essentially, it utilizes a standardized way to ingest the data, make the proper calculations and generate renderers. The renderers reference the transformed data, which represent the groups of data that were derived from the inputs. We additionally make calculations for the ranges. The x_range is categorical, and is made either from the label argument or from the `pandas.DataFrame.index`. The y_range can be supplied as the parameter continuous_range, or will be calculated as a linear range (Range1d) based on the supplied values. The bar builder is and can be further used as a base class for other builders that might also be performing some aggregation across derived groups of data. """ # ToDo: add label back as a discrete dimension values = Dimension('values') dimensions = ['values'] # req_dimensions = [['values']] default_attributes = {'label': CatAttr(), 'color': ColorAttr(), 'line_color': ColorAttr(default='white'), 'stack': CatAttr(), 'group': CatAttr()} agg = Enum(Aggregation, default='sum') max_height = Float(1.0) min_height = Float(0.0) bar_width = Float(default=0.8) fill_alpha = Float(default=0.8) glyph = BarGlyph comp_glyph_types = Override(default=[BarGlyph]) label_attributes = ['stack', 'group'] label_only = Bool(False) values_only = Bool(False) _perform_stack = False _perform_group = False def setup(self): if self.attributes['color'].columns is None: if self.attributes['stack'].columns is not None: self.attributes['color'].setup(columns=self.attributes['stack'].columns) if self.attributes['group'].columns is not None: self.attributes['color'].setup(columns=self.attributes['group'].columns) if self.attributes['stack'].columns is not None: self._perform_stack = True if self.attributes['group'].columns is not None: self._perform_group = True # ToDo: perform aggregation validation # Not given values kw, so using only categorical data if self.values.dtype.name == 'object' and len(self.attribute_columns) == 0: # agg must be count self.agg = 'count' self.attributes['label'].set_columns(self.values.selection) else: pass self._apply_inferred_index() if self.xlabel is None: if self.attributes['label'].columns is not None: self.xlabel = str( ', '.join(self.attributes['label'].columns).title()).title() else: self.xlabel = self.values.selection if self.ylabel is None: if not self.label_only: self.ylabel = '%s( %s )' % ( self.agg.title(), str(self.values.selection).title()) else: self.ylabel = '%s( %s )' % ( self.agg.title(), ', '.join(self.attributes['label'].columns).title()) def _apply_inferred_index(self): """Configure chart when labels are provided as index instead of as kwarg.""" # try to infer grouping vs stacking labels if (self.attributes['label'].columns is None and self.values.selection is not None): if self.attributes['stack'].columns is not None: special_column = 'unity' else: special_column = 'index' self._data['label'] = special_column self.attributes['label'].setup(data=ColumnDataSource(self._data.df), columns=special_column) self.xlabel = '' def set_ranges(self): """Push the Bar data into the ColumnDataSource and calculate the proper ranges. """ x_items = self.attributes['label'].items if x_items is None: x_items = '' x_labels = [] # Items are identified by tuples. If the tuple has a single value, # we unpack it for item in x_items: item = self._get_label(item) x_labels.append(str(item)) self.x_range = FactorRange(factors=x_labels) y_shift = abs(0.1 * ((self.min_height + self.max_height) / 2)) if self.min_height < 0: start = self.min_height - y_shift else: start = 0.0 if self.max_height > 0: end = self.max_height + y_shift else: end = 0.0 self.y_range = Range1d(start=start, end=end) def get_extra_args(self): if self.__class__ is not BarBuilder: attrs = self.properties(with_bases=False) return {attr: getattr(self, attr) for attr in attrs} else: return {} def yield_renderers(self): """Use the rect glyphs to display the bars. Takes reference points from data loaded at the ColumnDataSource. """ kwargs = self.get_extra_args() attrs = self.collect_attr_kwargs() for group in self._data.groupby(**self.attributes): glyph_kwargs = self.get_group_kwargs(group, attrs) group_kwargs = kwargs.copy() group_kwargs.update(glyph_kwargs) props = self.glyph.properties().difference(set(['label'])) # make sure we always pass the color and line color for k in ['color', 'line_color']: group_kwargs[k] = group[k] # TODO(fpliger): we shouldn't need to do this to ensure we don't # have extra kwargs... this is needed now because # of label, group and stack being "special" for k in set(group_kwargs): if k not in props: group_kwargs.pop(k) bg = self.glyph(label=group.label, x_label=self._get_label(group['label']), values=group.data[self.values.selection].values, agg=stats[self.agg](), width=self.bar_width, fill_alpha=self.fill_alpha, stack_label=self._get_label(group['stack']), dodge_label=self._get_label(group['group']), **group_kwargs) self.add_glyph(group, bg) if self._perform_stack: Stack().apply(self.comp_glyphs) if self._perform_group: Dodge().apply(self.comp_glyphs) # a higher level function of bar chart is to keep track of max height of all bars self.max_height = max([renderer.y_max for renderer in self.comp_glyphs]) self.min_height = min([renderer.y_min for renderer in self.comp_glyphs]) for renderer in self.comp_glyphs: for sub_renderer in renderer.renderers: yield sub_renderer @help(BarBuilder) def Bar(data, label=None, values=None, color=None, stack=None, group=None, agg="sum", xscale="categorical", yscale="linear", xgrid=False, ygrid=True, continuous_range=None, **kw): """ Create a Bar chart using :class:`BarBuilder <bokeh.charts.builders.bar_builder.BarBuilder>` render the geometry from values, cat and stacked. Args: data (:ref:`userguide_charts_data_types`): the data source for the chart. label (list(str) or str, optional): list of string representing the categories. (Defaults to None) values (str, optional): iterable 2d representing the data series values matrix. color (str or list(str) or `~bokeh.charts._attributes.ColorAttr`): string color, string column name, list of string columns or a custom `ColorAttr`, which replaces the default `ColorAttr` for the builder. stack (list(str) or str, optional): columns to use for stacking. (Defaults to False, so grouping is assumed) group (list(str) or str, optional): columns to use for grouping. agg (str): how to aggregate the `values`. (Defaults to 'sum', or only label is provided, then performs a `count`) continuous_range(Range1d, optional): Custom continuous_range to be used. (Defaults to None) In addition to the parameters specific to this chart, :ref:`userguide_charts_defaults` are also accepted as keyword parameters. Returns: :class:`Chart`: includes glyph renderers that generate bars Examples: .. bokeh-plot:: :source-position: above from bokeh.charts import Bar, output_file, show, hplot # best support is with data in a format that is table-like data = { 'sample': ['1st', '2nd', '1st', '2nd', '1st', '2nd'], 'interpreter': ['python', 'python', 'pypy', 'pypy', 'jython', 'jython'], 'timing': [-2, 5, 12, 40, 22, 30] } # x-axis labels pulled from the interpreter column, stacking labels from sample column bar = Bar(data, values='timing', label='interpreter', stack='sample', agg='mean', title="Python Interpreter Sampling", legend='top_right', plot_width=400) # table-like data results in reconfiguration of the chart with no data manipulation bar2 = Bar(data, values='timing', label=['interpreter', 'sample'], agg='mean', title="Python Interpreters", plot_width=400) output_file("stacked_bar.html") show(hplot(bar, bar2)) """ if continuous_range and not isinstance(continuous_range, Range1d): raise ValueError( "continuous_range must be an instance of bokeh.models.ranges.Range1d" ) if label is not None and values is None: kw['label_only'] = True if (agg == 'sum') or (agg == 'mean'): agg = 'count' values = label # The continuous_range is the y_range (until we implement HBar charts) y_range = continuous_range kw['label'] = label kw['values'] = values kw['color'] = color kw['stack'] = stack kw['group'] = group kw['agg'] = agg kw['xscale'] = xscale kw['yscale'] = yscale kw['xgrid'] = xgrid kw['ygrid'] = ygrid kw['y_range'] = y_range chart = create_and_build(BarBuilder, data, **kw) # hide x labels if there is a single value, implying stacking only if len(chart.x_range.factors) == 1 and not label: chart.below[0].visible = False return chart
bsd-3-clause
steven-murray/pydftools
setup.py
1
2408
#!/usr/bin/env python # -*- coding: utf-8 -*- """The setup script.""" from setuptools import setup, find_packages import io import os import re with open("README.rst") as readme_file: readme = readme_file.read() with open("HISTORY.rst") as history_file: history = history_file.read() requirements = [ "scipy", "numpy>=1.6.2", "Click>=6.0", "attrs>=17.0", "cached_property", "chainconsumer", "matplotlib" # TODO: put package requirements here ] def read(*names, **kwargs): with io.open( os.path.join(os.path.dirname(__file__), *names), encoding=kwargs.get("encoding", "utf8"), ) as fp: return fp.read() def find_version(*file_paths): version_file = read(*file_paths) version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M) if version_match: return version_match.group(1) raise RuntimeError("Unable to find version string.") setup_requirements = [ "pytest-runner", # TODO(steven-murray): put setup requirements (distutils extensions, etc.) here ] test_requirements = [ "pytest", # TODO: put package test requirements here ] setup( name="pydftools", version=find_version("pydftools", "__init__.py"), description="A pure-python port of the dftools R package.", long_description=readme + "\n\n" + history, author="Steven Murray", author_email="steven.murray@curtin.edu.au", url="https://github.com/steven-murray/pydftools", packages=find_packages(include=["pydftools"]), entry_points={"console_scripts": ["pydftools=pydftools.cli:main"]}, include_package_data=True, install_requires=requirements, license="MIT license", zip_safe=False, keywords="pydftools", classifiers=[ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", ], test_suite="tests", tests_require=test_requirements, setup_requires=setup_requirements, )
mit
jakobworldpeace/scikit-learn
sklearn/ensemble/forest.py
8
67993
"""Forest of trees-based ensemble methods Those methods include random forests and extremely randomized trees. The module structure is the following: - The ``BaseForest`` base class implements a common ``fit`` method for all the estimators in the module. The ``fit`` method of the base ``Forest`` class calls the ``fit`` method of each sub-estimator on random samples (with replacement, a.k.a. bootstrap) of the training set. The init of the sub-estimator is further delegated to the ``BaseEnsemble`` constructor. - The ``ForestClassifier`` and ``ForestRegressor`` base classes further implement the prediction logic by computing an average of the predicted outcomes of the sub-estimators. - The ``RandomForestClassifier`` and ``RandomForestRegressor`` derived classes provide the user with concrete implementations of the forest ensemble method using classical, deterministic ``DecisionTreeClassifier`` and ``DecisionTreeRegressor`` as sub-estimator implementations. - The ``ExtraTreesClassifier`` and ``ExtraTreesRegressor`` derived classes provide the user with concrete implementations of the forest ensemble method using the extremely randomized trees ``ExtraTreeClassifier`` and ``ExtraTreeRegressor`` as sub-estimator implementations. Single and multi-output problems are both handled. """ # Authors: Gilles Louppe <g.louppe@gmail.com> # Brian Holt <bdholt1@gmail.com> # Joly Arnaud <arnaud.v.joly@gmail.com> # Fares Hedayati <fares.hedayati@gmail.com> # # License: BSD 3 clause from __future__ import division import warnings from warnings import warn from abc import ABCMeta, abstractmethod import numpy as np from scipy.sparse import issparse from scipy.sparse import hstack as sparse_hstack from ..base import ClassifierMixin, RegressorMixin from ..externals.joblib import Parallel, delayed from ..externals import six from ..metrics import r2_score from ..preprocessing import OneHotEncoder from ..tree import (DecisionTreeClassifier, DecisionTreeRegressor, ExtraTreeClassifier, ExtraTreeRegressor) from ..tree._tree import DTYPE, DOUBLE from ..utils import check_random_state, check_array, compute_sample_weight from ..exceptions import DataConversionWarning, NotFittedError from .base import BaseEnsemble, _partition_estimators from ..utils.fixes import bincount, parallel_helper from ..utils.multiclass import check_classification_targets from ..utils.validation import check_is_fitted __all__ = ["RandomForestClassifier", "RandomForestRegressor", "ExtraTreesClassifier", "ExtraTreesRegressor", "RandomTreesEmbedding"] MAX_INT = np.iinfo(np.int32).max def _generate_sample_indices(random_state, n_samples): """Private function used to _parallel_build_trees function.""" random_instance = check_random_state(random_state) sample_indices = random_instance.randint(0, n_samples, n_samples) return sample_indices def _generate_unsampled_indices(random_state, n_samples): """Private function used to forest._set_oob_score function.""" sample_indices = _generate_sample_indices(random_state, n_samples) sample_counts = bincount(sample_indices, minlength=n_samples) unsampled_mask = sample_counts == 0 indices_range = np.arange(n_samples) unsampled_indices = indices_range[unsampled_mask] return unsampled_indices def _parallel_build_trees(tree, forest, X, y, sample_weight, tree_idx, n_trees, verbose=0, class_weight=None): """Private function used to fit a single tree in parallel.""" if verbose > 1: print("building tree %d of %d" % (tree_idx + 1, n_trees)) if forest.bootstrap: n_samples = X.shape[0] if sample_weight is None: curr_sample_weight = np.ones((n_samples,), dtype=np.float64) else: curr_sample_weight = sample_weight.copy() indices = _generate_sample_indices(tree.random_state, n_samples) sample_counts = bincount(indices, minlength=n_samples) curr_sample_weight *= sample_counts if class_weight == 'subsample': with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) curr_sample_weight *= compute_sample_weight('auto', y, indices) elif class_weight == 'balanced_subsample': curr_sample_weight *= compute_sample_weight('balanced', y, indices) tree.fit(X, y, sample_weight=curr_sample_weight, check_input=False) else: tree.fit(X, y, sample_weight=sample_weight, check_input=False) return tree class BaseForest(six.with_metaclass(ABCMeta, BaseEnsemble)): """Base class for forests of trees. Warning: This class should not be used directly. Use derived classes instead. """ @abstractmethod def __init__(self, base_estimator, n_estimators=10, estimator_params=tuple(), bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(BaseForest, self).__init__( base_estimator=base_estimator, n_estimators=n_estimators, estimator_params=estimator_params) self.bootstrap = bootstrap self.oob_score = oob_score self.n_jobs = n_jobs self.random_state = random_state self.verbose = verbose self.warm_start = warm_start self.class_weight = class_weight def apply(self, X): """Apply trees in the forest to X, return leaf indices. Parameters ---------- X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns ------- X_leaves : array_like, shape = [n_samples, n_estimators] For each datapoint x in X and for each tree in the forest, return the index of the leaf x ends up in. """ X = self._validate_X_predict(X) results = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend="threading")( delayed(parallel_helper)(tree, 'apply', X, check_input=False) for tree in self.estimators_) return np.array(results).T def decision_path(self, X): """Return the decision path in the forest .. versionadded:: 0.18 Parameters ---------- X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns ------- indicator : sparse csr array, shape = [n_samples, n_nodes] Return a node indicator matrix where non zero elements indicates that the samples goes through the nodes. n_nodes_ptr : array of size (n_estimators + 1, ) The columns from indicator[n_nodes_ptr[i]:n_nodes_ptr[i+1]] gives the indicator value for the i-th estimator. """ X = self._validate_X_predict(X) indicators = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend="threading")( delayed(parallel_helper)(tree, 'decision_path', X, check_input=False) for tree in self.estimators_) n_nodes = [0] n_nodes.extend([i.shape[1] for i in indicators]) n_nodes_ptr = np.array(n_nodes).cumsum() return sparse_hstack(indicators).tocsr(), n_nodes_ptr def fit(self, X, y, sample_weight=None): """Build a forest of trees from the training set (X, y). Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The training input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csc_matrix``. y : array-like, shape = [n_samples] or [n_samples, n_outputs] The target values (class labels in classification, real numbers in regression). sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. In the case of classification, splits are also ignored if they would result in any single class carrying a negative weight in either child node. Returns ------- self : object Returns self. """ # Validate or convert input data X = check_array(X, accept_sparse="csc", dtype=DTYPE) y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None) if sample_weight is not None: sample_weight = check_array(sample_weight, ensure_2d=False) if issparse(X): # Pre-sort indices to avoid that each individual tree of the # ensemble sorts the indices. X.sort_indices() # Remap output n_samples, self.n_features_ = X.shape y = np.atleast_1d(y) if y.ndim == 2 and y.shape[1] == 1: warn("A column-vector y was passed when a 1d array was" " expected. Please change the shape of y to " "(n_samples,), for example using ravel().", DataConversionWarning, stacklevel=2) if y.ndim == 1: # reshape is necessary to preserve the data contiguity against vs # [:, np.newaxis] that does not. y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] y, expanded_class_weight = self._validate_y_class_weight(y) if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous: y = np.ascontiguousarray(y, dtype=DOUBLE) if expanded_class_weight is not None: if sample_weight is not None: sample_weight = sample_weight * expanded_class_weight else: sample_weight = expanded_class_weight # Check parameters self._validate_estimator() if not self.bootstrap and self.oob_score: raise ValueError("Out of bag estimation only available" " if bootstrap=True") random_state = check_random_state(self.random_state) if not self.warm_start or not hasattr(self, "estimators_"): # Free allocated memory, if any self.estimators_ = [] n_more_estimators = self.n_estimators - len(self.estimators_) if n_more_estimators < 0: raise ValueError('n_estimators=%d must be larger or equal to ' 'len(estimators_)=%d when warm_start==True' % (self.n_estimators, len(self.estimators_))) elif n_more_estimators == 0: warn("Warm-start fitting without increasing n_estimators does not " "fit new trees.") else: if self.warm_start and len(self.estimators_) > 0: # We draw from the random state to get the random state we # would have got if we hadn't used a warm_start. random_state.randint(MAX_INT, size=len(self.estimators_)) trees = [] for i in range(n_more_estimators): tree = self._make_estimator(append=False, random_state=random_state) trees.append(tree) # Parallel loop: we use the threading backend as the Cython code # for fitting the trees is internally releasing the Python GIL # making threading always more efficient than multiprocessing in # that case. trees = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend="threading")( delayed(_parallel_build_trees)( t, self, X, y, sample_weight, i, len(trees), verbose=self.verbose, class_weight=self.class_weight) for i, t in enumerate(trees)) # Collect newly grown trees self.estimators_.extend(trees) if self.oob_score: self._set_oob_score(X, y) # Decapsulate classes_ attributes if hasattr(self, "classes_") and self.n_outputs_ == 1: self.n_classes_ = self.n_classes_[0] self.classes_ = self.classes_[0] return self @abstractmethod def _set_oob_score(self, X, y): """Calculate out of bag predictions and score.""" def _validate_y_class_weight(self, y): # Default implementation return y, None def _validate_X_predict(self, X): """Validate X whenever one tries to predict, apply, predict_proba""" if self.estimators_ is None or len(self.estimators_) == 0: raise NotFittedError("Estimator not fitted, " "call `fit` before exploiting the model.") return self.estimators_[0]._validate_X_predict(X, check_input=True) @property def feature_importances_(self): """Return the feature importances (the higher, the more important the feature). Returns ------- feature_importances_ : array, shape = [n_features] """ check_is_fitted(self, 'estimators_') all_importances = Parallel(n_jobs=self.n_jobs, backend="threading")( delayed(getattr)(tree, 'feature_importances_') for tree in self.estimators_) return sum(all_importances) / len(self.estimators_) class ForestClassifier(six.with_metaclass(ABCMeta, BaseForest, ClassifierMixin)): """Base class for forest of trees-based classifiers. Warning: This class should not be used directly. Use derived classes instead. """ @abstractmethod def __init__(self, base_estimator, n_estimators=10, estimator_params=tuple(), bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(ForestClassifier, self).__init__( base_estimator, n_estimators=n_estimators, estimator_params=estimator_params, bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start, class_weight=class_weight) def _set_oob_score(self, X, y): """Compute out-of-bag score""" X = check_array(X, dtype=DTYPE, accept_sparse='csr') n_classes_ = self.n_classes_ n_samples = y.shape[0] oob_decision_function = [] oob_score = 0.0 predictions = [] for k in range(self.n_outputs_): predictions.append(np.zeros((n_samples, n_classes_[k]))) for estimator in self.estimators_: unsampled_indices = _generate_unsampled_indices( estimator.random_state, n_samples) p_estimator = estimator.predict_proba(X[unsampled_indices, :], check_input=False) if self.n_outputs_ == 1: p_estimator = [p_estimator] for k in range(self.n_outputs_): predictions[k][unsampled_indices, :] += p_estimator[k] for k in range(self.n_outputs_): if (predictions[k].sum(axis=1) == 0).any(): warn("Some inputs do not have OOB scores. " "This probably means too few trees were used " "to compute any reliable oob estimates.") decision = (predictions[k] / predictions[k].sum(axis=1)[:, np.newaxis]) oob_decision_function.append(decision) oob_score += np.mean(y[:, k] == np.argmax(predictions[k], axis=1), axis=0) if self.n_outputs_ == 1: self.oob_decision_function_ = oob_decision_function[0] else: self.oob_decision_function_ = oob_decision_function self.oob_score_ = oob_score / self.n_outputs_ def _validate_y_class_weight(self, y): check_classification_targets(y) y = np.copy(y) expanded_class_weight = None if self.class_weight is not None: y_original = np.copy(y) self.classes_ = [] self.n_classes_ = [] y_store_unique_indices = np.zeros(y.shape, dtype=np.int) for k in range(self.n_outputs_): classes_k, y_store_unique_indices[:, k] = np.unique(y[:, k], return_inverse=True) self.classes_.append(classes_k) self.n_classes_.append(classes_k.shape[0]) y = y_store_unique_indices if self.class_weight is not None: valid_presets = ('balanced', 'balanced_subsample') if isinstance(self.class_weight, six.string_types): if self.class_weight not in valid_presets: raise ValueError('Valid presets for class_weight include ' '"balanced" and "balanced_subsample". Given "%s".' % self.class_weight) if self.warm_start: warn('class_weight presets "balanced" or "balanced_subsample" are ' 'not recommended for warm_start if the fitted data ' 'differs from the full dataset. In order to use ' '"balanced" weights, use compute_class_weight("balanced", ' 'classes, y). In place of y you can use a large ' 'enough sample of the full training set target to ' 'properly estimate the class frequency ' 'distributions. Pass the resulting weights as the ' 'class_weight parameter.') if (self.class_weight != 'balanced_subsample' or not self.bootstrap): if self.class_weight == "balanced_subsample": class_weight = "balanced" else: class_weight = self.class_weight expanded_class_weight = compute_sample_weight(class_weight, y_original) return y, expanded_class_weight def predict(self, X): """Predict class for X. The predicted class of an input sample is a vote by the trees in the forest, weighted by their probability estimates. That is, the predicted class is the one with highest mean probability estimate across the trees. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns ------- y : array of shape = [n_samples] or [n_samples, n_outputs] The predicted classes. """ proba = self.predict_proba(X) if self.n_outputs_ == 1: return self.classes_.take(np.argmax(proba, axis=1), axis=0) else: n_samples = proba[0].shape[0] predictions = np.zeros((n_samples, self.n_outputs_)) for k in range(self.n_outputs_): predictions[:, k] = self.classes_[k].take(np.argmax(proba[k], axis=1), axis=0) return predictions def predict_proba(self, X): """Predict class probabilities for X. The predicted class probabilities of an input sample are computed as the mean predicted class probabilities of the trees in the forest. The class probability of a single tree is the fraction of samples of the same class in a leaf. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns ------- p : array of shape = [n_samples, n_classes], or a list of n_outputs such arrays if n_outputs > 1. The class probabilities of the input samples. The order of the classes corresponds to that in the attribute `classes_`. """ check_is_fitted(self, 'estimators_') # Check data X = self._validate_X_predict(X) # Assign chunk of trees to jobs n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) # Parallel loop all_proba = Parallel(n_jobs=n_jobs, verbose=self.verbose, backend="threading")( delayed(parallel_helper)(e, 'predict_proba', X, check_input=False) for e in self.estimators_) # Reduce proba = all_proba[0] if self.n_outputs_ == 1: for j in range(1, len(all_proba)): proba += all_proba[j] proba /= len(self.estimators_) else: for j in range(1, len(all_proba)): for k in range(self.n_outputs_): proba[k] += all_proba[j][k] for k in range(self.n_outputs_): proba[k] /= self.n_estimators return proba def predict_log_proba(self, X): """Predict class log-probabilities for X. The predicted class log-probabilities of an input sample is computed as the log of the mean predicted class probabilities of the trees in the forest. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns ------- p : array of shape = [n_samples, n_classes], or a list of n_outputs such arrays if n_outputs > 1. The class probabilities of the input samples. The order of the classes corresponds to that in the attribute `classes_`. """ proba = self.predict_proba(X) if self.n_outputs_ == 1: return np.log(proba) else: for k in range(self.n_outputs_): proba[k] = np.log(proba[k]) return proba class ForestRegressor(six.with_metaclass(ABCMeta, BaseForest, RegressorMixin)): """Base class for forest of trees-based regressors. Warning: This class should not be used directly. Use derived classes instead. """ @abstractmethod def __init__(self, base_estimator, n_estimators=10, estimator_params=tuple(), bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(ForestRegressor, self).__init__( base_estimator, n_estimators=n_estimators, estimator_params=estimator_params, bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) def predict(self, X): """Predict regression target for X. The predicted regression target of an input sample is computed as the mean predicted regression targets of the trees in the forest. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns ------- y : array of shape = [n_samples] or [n_samples, n_outputs] The predicted values. """ check_is_fitted(self, 'estimators_') # Check data X = self._validate_X_predict(X) # Assign chunk of trees to jobs n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) # Parallel loop all_y_hat = Parallel(n_jobs=n_jobs, verbose=self.verbose, backend="threading")( delayed(parallel_helper)(e, 'predict', X, check_input=False) for e in self.estimators_) # Reduce y_hat = sum(all_y_hat) / len(self.estimators_) return y_hat def _set_oob_score(self, X, y): """Compute out-of-bag scores""" X = check_array(X, dtype=DTYPE, accept_sparse='csr') n_samples = y.shape[0] predictions = np.zeros((n_samples, self.n_outputs_)) n_predictions = np.zeros((n_samples, self.n_outputs_)) for estimator in self.estimators_: unsampled_indices = _generate_unsampled_indices( estimator.random_state, n_samples) p_estimator = estimator.predict( X[unsampled_indices, :], check_input=False) if self.n_outputs_ == 1: p_estimator = p_estimator[:, np.newaxis] predictions[unsampled_indices, :] += p_estimator n_predictions[unsampled_indices, :] += 1 if (n_predictions == 0).any(): warn("Some inputs do not have OOB scores. " "This probably means too few trees were used " "to compute any reliable oob estimates.") n_predictions[n_predictions == 0] = 1 predictions /= n_predictions self.oob_prediction_ = predictions if self.n_outputs_ == 1: self.oob_prediction_ = \ self.oob_prediction_.reshape((n_samples, )) self.oob_score_ = 0.0 for k in range(self.n_outputs_): self.oob_score_ += r2_score(y[:, k], predictions[:, k]) self.oob_score_ /= self.n_outputs_ class RandomForestClassifier(ForestClassifier): """A random forest classifier. A random forest is a meta estimator that fits a number of decision tree classifiers on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement if `bootstrap=True` (default). Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="gini") The function to measure the quality of a split. Supported criteria are "gini" for the Gini impurity and "entropy" for the information gain. Note: this parameter is tree-specific. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=sqrt(n_features)`. - If "sqrt", then `max_features=sqrt(n_features)` (same as "auto"). - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. min_samples_split : int, float, optional (default=2) The minimum number of samples required to split an internal node: - If int, then consider `min_samples_split` as the minimum number. - If float, then `min_samples_split` is a percentage and `ceil(min_samples_split * n_samples)` are the minimum number of samples for each split. .. versionchanged:: 0.18 Added float values for percentages. min_samples_leaf : int, float, optional (default=1) The minimum number of samples required to be at a leaf node: - If int, then consider `min_samples_leaf` as the minimum number. - If float, then `min_samples_leaf` is a percentage and `ceil(min_samples_leaf * n_samples)` are the minimum number of samples for each node. .. versionchanged:: 0.18 Added float values for percentages. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. min_impurity_split : float, optional (default=1e-7) Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf. .. versionadded:: 0.18 bootstrap : boolean, optional (default=True) Whether bootstrap samples are used when building trees. oob_score : bool (default=False) Whether to use out-of-bag samples to estimate the generalization accuracy. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. class_weight : dict, list of dicts, "balanced", "balanced_subsample" or None, optional (default=None) Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` The "balanced_subsample" mode is the same as "balanced" except that weights are computed based on the bootstrap sample for every tree grown. For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. Attributes ---------- estimators_ : list of DecisionTreeClassifier The collection of fitted sub-estimators. classes_ : array of shape = [n_classes] or a list of such arrays The classes labels (single output problem), or a list of arrays of class labels (multi-output problem). n_classes_ : int or list The number of classes (single output problem), or a list containing the number of classes for each output (multi-output problem). n_features_ : int The number of features when ``fit`` is performed. n_outputs_ : int The number of outputs when ``fit`` is performed. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_decision_function_ : array of shape = [n_samples, n_classes] Decision function computed with out-of-bag estimate on the training set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case, `oob_decision_function_` might contain NaN. Notes ----- The features are always randomly permuted at each split. Therefore, the best found split may vary, even with the same training data, ``max_features=n_features`` and ``bootstrap=False``, if the improvement of the criterion is identical for several splits enumerated during the search of the best split. To obtain a deterministic behaviour during fitting, ``random_state`` has to be fixed. References ---------- .. [1] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32, 2001. See also -------- DecisionTreeClassifier, ExtraTreesClassifier """ def __init__(self, n_estimators=10, criterion="gini", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, min_impurity_split=1e-7, bootstrap=True, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(RandomForestClassifier, self).__init__( base_estimator=DecisionTreeClassifier(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "min_impurity_split", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start, class_weight=class_weight) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes self.min_impurity_split = min_impurity_split class RandomForestRegressor(ForestRegressor): """A random forest regressor. A random forest is a meta estimator that fits a number of classifying decision trees on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement if `bootstrap=True` (default). Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="mse") The function to measure the quality of a split. Supported criteria are "mse" for the mean squared error, which is equal to variance reduction as feature selection criterion, and "mae" for the mean absolute error. .. versionadded:: 0.18 Mean Absolute Error (MAE) criterion. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=n_features`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. min_samples_split : int, float, optional (default=2) The minimum number of samples required to split an internal node: - If int, then consider `min_samples_split` as the minimum number. - If float, then `min_samples_split` is a percentage and `ceil(min_samples_split * n_samples)` are the minimum number of samples for each split. .. versionchanged:: 0.18 Added float values for percentages. min_samples_leaf : int, float, optional (default=1) The minimum number of samples required to be at a leaf node: - If int, then consider `min_samples_leaf` as the minimum number. - If float, then `min_samples_leaf` is a percentage and `ceil(min_samples_leaf * n_samples)` are the minimum number of samples for each node. .. versionchanged:: 0.18 Added float values for percentages. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. min_impurity_split : float, optional (default=1e-7) Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf. .. versionadded:: 0.18 bootstrap : boolean, optional (default=True) Whether bootstrap samples are used when building trees. oob_score : bool, optional (default=False) whether to use out-of-bag samples to estimate the R^2 on unseen data. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. Attributes ---------- estimators_ : list of DecisionTreeRegressor The collection of fitted sub-estimators. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). n_features_ : int The number of features when ``fit`` is performed. n_outputs_ : int The number of outputs when ``fit`` is performed. oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_prediction_ : array of shape = [n_samples] Prediction computed with out-of-bag estimate on the training set. Notes ----- The features are always randomly permuted at each split. Therefore, the best found split may vary, even with the same training data, ``max_features=n_features`` and ``bootstrap=False``, if the improvement of the criterion is identical for several splits enumerated during the search of the best split. To obtain a deterministic behaviour during fitting, ``random_state`` has to be fixed. References ---------- .. [1] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32, 2001. See also -------- DecisionTreeRegressor, ExtraTreesRegressor """ def __init__(self, n_estimators=10, criterion="mse", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, min_impurity_split=1e-7, bootstrap=True, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(RandomForestRegressor, self).__init__( base_estimator=DecisionTreeRegressor(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "min_impurity_split", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes self.min_impurity_split = min_impurity_split class ExtraTreesClassifier(ForestClassifier): """An extra-trees classifier. This class implements a meta estimator that fits a number of randomized decision trees (a.k.a. extra-trees) on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="gini") The function to measure the quality of a split. Supported criteria are "gini" for the Gini impurity and "entropy" for the information gain. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=sqrt(n_features)`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. min_samples_split : int, float, optional (default=2) The minimum number of samples required to split an internal node: - If int, then consider `min_samples_split` as the minimum number. - If float, then `min_samples_split` is a percentage and `ceil(min_samples_split * n_samples)` are the minimum number of samples for each split. .. versionchanged:: 0.18 Added float values for percentages. min_samples_leaf : int, float, optional (default=1) The minimum number of samples required to be at a leaf node: - If int, then consider `min_samples_leaf` as the minimum number. - If float, then `min_samples_leaf` is a percentage and `ceil(min_samples_leaf * n_samples)` are the minimum number of samples for each node. .. versionchanged:: 0.18 Added float values for percentages. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. min_impurity_split : float, optional (default=1e-7) Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf. .. versionadded:: 0.18 bootstrap : boolean, optional (default=False) Whether bootstrap samples are used when building trees. oob_score : bool, optional (default=False) Whether to use out-of-bag samples to estimate the generalization accuracy. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. class_weight : dict, list of dicts, "balanced", "balanced_subsample" or None, optional (default=None) Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` The "balanced_subsample" mode is the same as "balanced" except that weights are computed based on the bootstrap sample for every tree grown. For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. Attributes ---------- estimators_ : list of DecisionTreeClassifier The collection of fitted sub-estimators. classes_ : array of shape = [n_classes] or a list of such arrays The classes labels (single output problem), or a list of arrays of class labels (multi-output problem). n_classes_ : int or list The number of classes (single output problem), or a list containing the number of classes for each output (multi-output problem). feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). n_features_ : int The number of features when ``fit`` is performed. n_outputs_ : int The number of outputs when ``fit`` is performed. oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_decision_function_ : array of shape = [n_samples, n_classes] Decision function computed with out-of-bag estimate on the training set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case, `oob_decision_function_` might contain NaN. References ---------- .. [1] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized trees", Machine Learning, 63(1), 3-42, 2006. See also -------- sklearn.tree.ExtraTreeClassifier : Base classifier for this ensemble. RandomForestClassifier : Ensemble Classifier based on trees with optimal splits. """ def __init__(self, n_estimators=10, criterion="gini", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, min_impurity_split=1e-7, bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(ExtraTreesClassifier, self).__init__( base_estimator=ExtraTreeClassifier(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "min_impurity_split", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start, class_weight=class_weight) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes self.min_impurity_split = min_impurity_split class ExtraTreesRegressor(ForestRegressor): """An extra-trees regressor. This class implements a meta estimator that fits a number of randomized decision trees (a.k.a. extra-trees) on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="mse") The function to measure the quality of a split. Supported criteria are "mse" for the mean squared error, which is equal to variance reduction as feature selection criterion, and "mae" for the mean absolute error. .. versionadded:: 0.18 Mean Absolute Error (MAE) criterion. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=n_features`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. min_samples_split : int, float, optional (default=2) The minimum number of samples required to split an internal node: - If int, then consider `min_samples_split` as the minimum number. - If float, then `min_samples_split` is a percentage and `ceil(min_samples_split * n_samples)` are the minimum number of samples for each split. .. versionchanged:: 0.18 Added float values for percentages. min_samples_leaf : int, float, optional (default=1) The minimum number of samples required to be at a leaf node: - If int, then consider `min_samples_leaf` as the minimum number. - If float, then `min_samples_leaf` is a percentage and `ceil(min_samples_leaf * n_samples)` are the minimum number of samples for each node. .. versionchanged:: 0.18 Added float values for percentages. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. min_impurity_split : float, optional (default=1e-7) Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf. .. versionadded:: 0.18 bootstrap : boolean, optional (default=False) Whether bootstrap samples are used when building trees. oob_score : bool, optional (default=False) Whether to use out-of-bag samples to estimate the R^2 on unseen data. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. Attributes ---------- estimators_ : list of DecisionTreeRegressor The collection of fitted sub-estimators. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). n_features_ : int The number of features. n_outputs_ : int The number of outputs. oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_prediction_ : array of shape = [n_samples] Prediction computed with out-of-bag estimate on the training set. References ---------- .. [1] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized trees", Machine Learning, 63(1), 3-42, 2006. See also -------- sklearn.tree.ExtraTreeRegressor: Base estimator for this ensemble. RandomForestRegressor: Ensemble regressor using trees with optimal splits. """ def __init__(self, n_estimators=10, criterion="mse", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, min_impurity_split=1e-7, bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(ExtraTreesRegressor, self).__init__( base_estimator=ExtraTreeRegressor(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "min_impurity_split", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes self.min_impurity_split = min_impurity_split class RandomTreesEmbedding(BaseForest): """An ensemble of totally random trees. An unsupervised transformation of a dataset to a high-dimensional sparse representation. A datapoint is coded according to which leaf of each tree it is sorted into. Using a one-hot encoding of the leaves, this leads to a binary coding with as many ones as there are trees in the forest. The dimensionality of the resulting representation is ``n_out <= n_estimators * max_leaf_nodes``. If ``max_leaf_nodes == None``, the number of leaf nodes is at most ``n_estimators * 2 ** max_depth``. Read more in the :ref:`User Guide <random_trees_embedding>`. Parameters ---------- n_estimators : integer, optional (default=10) Number of trees in the forest. max_depth : integer, optional (default=5) The maximum depth of each tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. min_samples_split : int, float, optional (default=2) The minimum number of samples required to split an internal node: - If int, then consider `min_samples_split` as the minimum number. - If float, then `min_samples_split` is a percentage and `ceil(min_samples_split * n_samples)` is the minimum number of samples for each split. .. versionchanged:: 0.18 Added float values for percentages. min_samples_leaf : int, float, optional (default=1) The minimum number of samples required to be at a leaf node: - If int, then consider `min_samples_leaf` as the minimum number. - If float, then `min_samples_leaf` is a percentage and `ceil(min_samples_leaf * n_samples)` is the minimum number of samples for each node. .. versionchanged:: 0.18 Added float values for percentages. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. min_impurity_split : float, optional (default=1e-7) Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf. .. versionadded:: 0.18 sparse_output : bool, optional (default=True) Whether or not to return a sparse CSR matrix, as default behavior, or to return a dense array compatible with dense pipeline operators. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. Attributes ---------- estimators_ : list of DecisionTreeClassifier The collection of fitted sub-estimators. References ---------- .. [1] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized trees", Machine Learning, 63(1), 3-42, 2006. .. [2] Moosmann, F. and Triggs, B. and Jurie, F. "Fast discriminative visual codebooks using randomized clustering forests" NIPS 2007 """ def __init__(self, n_estimators=10, max_depth=5, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_leaf_nodes=None, min_impurity_split=1e-7, sparse_output=True, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(RandomTreesEmbedding, self).__init__( base_estimator=ExtraTreeRegressor(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "min_impurity_split", "random_state"), bootstrap=False, oob_score=False, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) self.criterion = 'mse' self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = 1 self.max_leaf_nodes = max_leaf_nodes self.min_impurity_split = min_impurity_split self.sparse_output = sparse_output def _set_oob_score(self, X, y): raise NotImplementedError("OOB score not supported by tree embedding") def fit(self, X, y=None, sample_weight=None): """Fit estimator. Parameters ---------- X : array-like or sparse matrix, shape=(n_samples, n_features) The input samples. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csc_matrix`` for maximum efficiency. Returns ------- self : object Returns self. """ self.fit_transform(X, y, sample_weight=sample_weight) return self def fit_transform(self, X, y=None, sample_weight=None): """Fit estimator and transform dataset. Parameters ---------- X : array-like or sparse matrix, shape=(n_samples, n_features) Input data used to build forests. Use ``dtype=np.float32`` for maximum efficiency. Returns ------- X_transformed : sparse matrix, shape=(n_samples, n_out) Transformed dataset. """ X = check_array(X, accept_sparse=['csc']) if issparse(X): # Pre-sort indices to avoid that each individual tree of the # ensemble sorts the indices. X.sort_indices() rnd = check_random_state(self.random_state) y = rnd.uniform(size=X.shape[0]) super(RandomTreesEmbedding, self).fit(X, y, sample_weight=sample_weight) self.one_hot_encoder_ = OneHotEncoder(sparse=self.sparse_output) return self.one_hot_encoder_.fit_transform(self.apply(X)) def transform(self, X): """Transform dataset. Parameters ---------- X : array-like or sparse matrix, shape=(n_samples, n_features) Input data to be transformed. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csr_matrix`` for maximum efficiency. Returns ------- X_transformed : sparse matrix, shape=(n_samples, n_out) Transformed dataset. """ return self.one_hot_encoder_.transform(self.apply(X))
bsd-3-clause
BigDataforYou/movie_recommendation_workshop_1
big_data_4_you_demo_1/venv/lib/python2.7/site-packages/pandas/stats/tests/test_math.py
9
1836
import nose from datetime import datetime from numpy.random import randn import numpy as np from pandas.core.api import Series, DataFrame, date_range import pandas.util.testing as tm import pandas.stats.math as pmath from pandas import ols N, K = 100, 10 _have_statsmodels = True try: import statsmodels.api as sm except ImportError: try: import scikits.statsmodels.api as sm # noqa except ImportError: _have_statsmodels = False class TestMath(tm.TestCase): _nan_locs = np.arange(20, 40) _inf_locs = np.array([]) def setUp(self): arr = randn(N) arr[self._nan_locs] = np.NaN self.arr = arr self.rng = date_range(datetime(2009, 1, 1), periods=N) self.series = Series(arr.copy(), index=self.rng) self.frame = DataFrame(randn(N, K), index=self.rng, columns=np.arange(K)) def test_rank_1d(self): self.assertEqual(1, pmath.rank(self.series)) self.assertEqual(0, pmath.rank(Series(0, self.series.index))) def test_solve_rect(self): if not _have_statsmodels: raise nose.SkipTest("no statsmodels") b = Series(np.random.randn(N), self.frame.index) result = pmath.solve(self.frame, b) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): expected = ols(y=b, x=self.frame, intercept=False).beta self.assertTrue(np.allclose(result, expected)) def test_inv_illformed(self): singular = DataFrame(np.array([[1, 1], [2, 2]])) rs = pmath.inv(singular) expected = np.array([[0.1, 0.2], [0.1, 0.2]]) self.assertTrue(np.allclose(rs, expected)) if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)
mit
Insight-book/data-science-from-scratch
first-edition/code/gradient_descent.py
53
5895
from __future__ import division from collections import Counter from linear_algebra import distance, vector_subtract, scalar_multiply import math, random def sum_of_squares(v): """computes the sum of squared elements in v""" return sum(v_i ** 2 for v_i in v) def difference_quotient(f, x, h): return (f(x + h) - f(x)) / h def plot_estimated_derivative(): def square(x): return x * x def derivative(x): return 2 * x derivative_estimate = lambda x: difference_quotient(square, x, h=0.00001) # plot to show they're basically the same import matplotlib.pyplot as plt x = range(-10,10) plt.plot(x, map(derivative, x), 'rx') # red x plt.plot(x, map(derivative_estimate, x), 'b+') # blue + plt.show() # purple *, hopefully def partial_difference_quotient(f, v, i, h): # add h to just the i-th element of v w = [v_j + (h if j == i else 0) for j, v_j in enumerate(v)] return (f(w) - f(v)) / h def estimate_gradient(f, v, h=0.00001): return [partial_difference_quotient(f, v, i, h) for i, _ in enumerate(v)] def step(v, direction, step_size): """move step_size in the direction from v""" return [v_i + step_size * direction_i for v_i, direction_i in zip(v, direction)] def sum_of_squares_gradient(v): return [2 * v_i for v_i in v] def safe(f): """define a new function that wraps f and return it""" def safe_f(*args, **kwargs): try: return f(*args, **kwargs) except: return float('inf') # this means "infinity" in Python return safe_f # # # minimize / maximize batch # # def minimize_batch(target_fn, gradient_fn, theta_0, tolerance=0.000001): """use gradient descent to find theta that minimizes target function""" step_sizes = [100, 10, 1, 0.1, 0.01, 0.001, 0.0001, 0.00001] theta = theta_0 # set theta to initial value target_fn = safe(target_fn) # safe version of target_fn value = target_fn(theta) # value we're minimizing while True: gradient = gradient_fn(theta) next_thetas = [step(theta, gradient, -step_size) for step_size in step_sizes] # choose the one that minimizes the error function next_theta = min(next_thetas, key=target_fn) next_value = target_fn(next_theta) # stop if we're "converging" if abs(value - next_value) < tolerance: return theta else: theta, value = next_theta, next_value def negate(f): """return a function that for any input x returns -f(x)""" return lambda *args, **kwargs: -f(*args, **kwargs) def negate_all(f): """the same when f returns a list of numbers""" return lambda *args, **kwargs: [-y for y in f(*args, **kwargs)] def maximize_batch(target_fn, gradient_fn, theta_0, tolerance=0.000001): return minimize_batch(negate(target_fn), negate_all(gradient_fn), theta_0, tolerance) # # minimize / maximize stochastic # def in_random_order(data): """generator that returns the elements of data in random order""" indexes = [i for i, _ in enumerate(data)] # create a list of indexes random.shuffle(indexes) # shuffle them for i in indexes: # return the data in that order yield data[i] def minimize_stochastic(target_fn, gradient_fn, x, y, theta_0, alpha_0=0.01): data = zip(x, y) theta = theta_0 # initial guess alpha = alpha_0 # initial step size min_theta, min_value = None, float("inf") # the minimum so far iterations_with_no_improvement = 0 # if we ever go 100 iterations with no improvement, stop while iterations_with_no_improvement < 100: value = sum( target_fn(x_i, y_i, theta) for x_i, y_i in data ) if value < min_value: # if we've found a new minimum, remember it # and go back to the original step size min_theta, min_value = theta, value iterations_with_no_improvement = 0 alpha = alpha_0 else: # otherwise we're not improving, so try shrinking the step size iterations_with_no_improvement += 1 alpha *= 0.9 # and take a gradient step for each of the data points for x_i, y_i in in_random_order(data): gradient_i = gradient_fn(x_i, y_i, theta) theta = vector_subtract(theta, scalar_multiply(alpha, gradient_i)) return min_theta def maximize_stochastic(target_fn, gradient_fn, x, y, theta_0, alpha_0=0.01): return minimize_stochastic(negate(target_fn), negate_all(gradient_fn), x, y, theta_0, alpha_0) if __name__ == "__main__": print "using the gradient" v = [random.randint(-10,10) for i in range(3)] tolerance = 0.0000001 while True: #print v, sum_of_squares(v) gradient = sum_of_squares_gradient(v) # compute the gradient at v next_v = step(v, gradient, -0.01) # take a negative gradient step if distance(next_v, v) < tolerance: # stop if we're converging break v = next_v # continue if we're not print "minimum v", v print "minimum value", sum_of_squares(v) print print "using minimize_batch" v = [random.randint(-10,10) for i in range(3)] v = minimize_batch(sum_of_squares, sum_of_squares_gradient, v) print "minimum v", v print "minimum value", sum_of_squares(v)
unlicense
macioosch/dynamo-hard-spheres-sim
convergence-plot.py
1
6346
#!/usr/bin/env python2 # encoding=utf-8 from __future__ import division, print_function from glob import glob from itertools import izip from matplotlib import pyplot as plt import numpy as np input_files = glob("csv/convergence-256000-0.*.csv") #input_files = glob("csv/convergence-500000-0.*.csv") #input_files = glob("csv/convergence-1000188-0.*.csv") #plotted_parameter = "msds_diffusion" plotted_parameter = "pressures_collision" #plotted_parameter = "pressures_virial" #plotted_parameter = "msds_val" #plotted_parameter = "times" legend_names = [] tight_layout = False show_legend = False for file_number, file_name in enumerate(sorted(input_files)): data = np.genfromtxt(file_name, delimiter='\t', names=[ "packings","densities","collisions","n_atoms","pressures_virial", "pressures_collision","msds_val","msds_diffusion","times", "std_pressures_virial","std_pressures_collision","std_msds_val", "std_msds_diffusion","std_times"]) n_atoms = data["n_atoms"][0] density = data["densities"][0] equilibrated_collisions = data["collisions"] - 2*data["collisions"][0] \ + data["collisions"][1] """ ### 5 graphs: D(CPS) ### tight_layout = True skip_points = 0 ax = plt.subplot(3, 2, file_number+1) plt.fill_between((equilibrated_collisions / n_atoms)[skip_points:], data[plotted_parameter][skip_points:] - data["std_" + plotted_parameter][skip_points:], data[plotted_parameter][skip_points:] + data["std_" + plotted_parameter][skip_points:], alpha=0.3) plt.plot((equilibrated_collisions / n_atoms)[skip_points:], data[plotted_parameter][skip_points:], lw=2) if plotted_parameter == "msds_diffusion": plt.ylim(0.990*data[plotted_parameter][-1], 1.005*data[plotted_parameter][-1]) plt.xlim([0, 1e5]) plt.legend(["Density {}".format(data["densities"][0])], loc="lower right") ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%.4f')) plt.xlabel("Collisions per sphere") plt.ylabel("D") """ ### 5 graphs: relative D(CPS) ### tight_layout = True skip_points = 0 ax = plt.subplot(3, 2, file_number+1) plt.fill_between((equilibrated_collisions / n_atoms)[skip_points:], -1 + (data[plotted_parameter][skip_points:] - data["std_" + plotted_parameter][skip_points:])/data[plotted_parameter][-1], -1 + (data[plotted_parameter][skip_points:] + data["std_" + plotted_parameter][skip_points:])/data[plotted_parameter][-1], alpha=0.3) plt.plot((equilibrated_collisions / n_atoms)[skip_points:], -1 + data[plotted_parameter][skip_points:]/data[plotted_parameter][-1], lw=2) plt.ylim(data["std_" + plotted_parameter][-1]*20*np.array([-1, 1])/data[plotted_parameter][-1]) #plt.xscale("log") plt.xlim([0, 1e5]) plt.legend(["$\\rho\\sigma^3=\\ {}$".format(data["densities"][0])], loc="lower right") ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%.2e')) plt.xlabel("$C/N$") plt.ylabel("$[Z_{MD}(C) / Z_{MD}(C=10^5 N)] - 1$") """ ### 1 graph: D(t) ### show_legend = True skip_points = 0 plt.title("D(t) for 5 densities") plt.loglog(data["times"][skip_points:], data[plotted_parameter][skip_points:]) legend_names.append(data["densities"][0]) plt.xlabel("Time") plt.ylabel("D") """ """ ### 1 graph: D(t) / Dinf ### show_legend = True skip_points = 0 #plt.fill_between(data["times"][skip_points:], # (data[plotted_parameter] - data["std_" + plotted_parameter]) # / data[plotted_parameter][-1] - 1, # (data[plotted_parameter] + data["std_" + plotted_parameter]) # / data[plotted_parameter][-1] - 1, color="grey", alpha=0.4) plt.plot(data["times"][skip_points:], data[plotted_parameter] / data[plotted_parameter][-1] - 1, lw=1) legend_names.append(data["densities"][0]) #plt.xscale("log") plt.xlabel("Time") plt.ylabel("D / D(t --> inf)") """ """ ### 5 graphs: D(1/CPS) ### tight_layout = True skip_points = 40 ax = plt.subplot(3, 2, file_number+1) plt.fill_between((n_atoms / equilibrated_collisions)[skip_points:], data[plotted_parameter][skip_points:] - data["std_" + plotted_parameter][skip_points:], data[plotted_parameter][skip_points:] + data["std_" + plotted_parameter][skip_points:], alpha=0.3) plt.plot((n_atoms / equilibrated_collisions)[skip_points:], data[plotted_parameter][skip_points:], lw=2) plt.title("Density {}:".format(data["densities"][0])) ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%.7f')) plt.xlim(xmin=0) plt.xlabel("1 / Collisions per sphere") plt.ylabel("D") """ """ ### 1 graph: D(CPS) / Dinf ### show_legend = True plt.fill_between(equilibrated_collisions / n_atoms, (data[plotted_parameter] - data["std_" + plotted_parameter]) / data[plotted_parameter][-1] - 1, (data[plotted_parameter] + data["std_" + plotted_parameter]) / data[plotted_parameter][-1] - 1, color="grey", alpha=0.4) plt.plot(equilibrated_collisions / n_atoms, data[plotted_parameter] / data[plotted_parameter][-1] - 1, lw=2) legend_names.append(data["densities"][0]) plt.xlabel("Collisions per sphere") plt.ylabel("D / D(t --> inf)") """ """ ### 1 graph: D(1/CPS) / Dinf ### show_legend = True plt.fill_between(n_atoms / equilibrated_collisions, (data[plotted_parameter] - data["std_" + plotted_parameter]) / data[plotted_parameter][-1] - 1, (data[plotted_parameter] + data["std_" + plotted_parameter]) / data[plotted_parameter][-1] - 1, color="grey", alpha=0.4) plt.plot( n_atoms / equilibrated_collisions, data[plotted_parameter] / data[plotted_parameter][-1] - 1) legend_names.append(data["densities"][0]) plt.xlabel(" 1 / Collisions per sphere") plt.ylabel(plotted_parameter) """ #if tight_layout: # plt.tight_layout(pad=0.0, w_pad=0.0, h_pad=0.0) if show_legend: plt.legend(legend_names, title="Density:", loc="lower right") plt.show()
gpl-3.0
webmasterraj/FogOrNot
flask/lib/python2.7/site-packages/pandas/stats/tests/test_moments.py
3
89255
import nose import sys import functools import warnings from datetime import datetime from numpy.random import randn from numpy.testing.decorators import slow import numpy as np from distutils.version import LooseVersion from pandas import Series, DataFrame, Panel, bdate_range, isnull, notnull, concat from pandas.util.testing import ( assert_almost_equal, assert_series_equal, assert_frame_equal, assert_panel_equal, assert_index_equal ) import pandas.core.datetools as datetools import pandas.stats.moments as mom import pandas.util.testing as tm from pandas.compat import range, zip, PY3, StringIO N, K = 100, 10 class Base(tm.TestCase): _multiprocess_can_split_ = True _nan_locs = np.arange(20, 40) _inf_locs = np.array([]) def _create_data(self): arr = randn(N) arr[self._nan_locs] = np.NaN self.arr = arr self.rng = bdate_range(datetime(2009, 1, 1), periods=N) self.series = Series(arr.copy(), index=self.rng) self.frame = DataFrame(randn(N, K), index=self.rng, columns=np.arange(K)) class TestMoments(Base): def setUp(self): self._create_data() warnings.simplefilter("ignore", category=FutureWarning) def test_centered_axis_validation(self): # ok mom.rolling_mean(Series(np.ones(10)),3,center=True ,axis=0) # bad axis self.assertRaises(ValueError, mom.rolling_mean,Series(np.ones(10)),3,center=True ,axis=1) # ok ok mom.rolling_mean(DataFrame(np.ones((10,10))),3,center=True ,axis=0) mom.rolling_mean(DataFrame(np.ones((10,10))),3,center=True ,axis=1) # bad axis self.assertRaises(ValueError, mom.rolling_mean,DataFrame(np.ones((10,10))),3,center=True ,axis=2) def test_rolling_sum(self): self._check_moment_func(mom.rolling_sum, np.sum) def test_rolling_count(self): counter = lambda x: np.isfinite(x).astype(float).sum() self._check_moment_func(mom.rolling_count, counter, has_min_periods=False, preserve_nan=False, fill_value=0) def test_rolling_mean(self): self._check_moment_func(mom.rolling_mean, np.mean) def test_cmov_mean(self): # GH 8238 tm._skip_if_no_scipy() vals = np.array([6.95, 15.21, 4.72, 9.12, 13.81, 13.49, 16.68, 9.48, 10.63, 14.48]) xp = np.array([np.nan, np.nan, 9.962, 11.27 , 11.564, 12.516, 12.818, 12.952, np.nan, np.nan]) rs = mom.rolling_mean(vals, 5, center=True) assert_almost_equal(xp, rs) xp = Series(rs) rs = mom.rolling_mean(Series(vals), 5, center=True) assert_series_equal(xp, rs) def test_cmov_window(self): # GH 8238 tm._skip_if_no_scipy() vals = np.array([6.95, 15.21, 4.72, 9.12, 13.81, 13.49, 16.68, 9.48, 10.63, 14.48]) xp = np.array([np.nan, np.nan, 9.962, 11.27 , 11.564, 12.516, 12.818, 12.952, np.nan, np.nan]) rs = mom.rolling_window(vals, 5, 'boxcar', center=True) assert_almost_equal(xp, rs) xp = Series(rs) rs = mom.rolling_window(Series(vals), 5, 'boxcar', center=True) assert_series_equal(xp, rs) def test_cmov_window_corner(self): # GH 8238 tm._skip_if_no_scipy() # all nan vals = np.empty(10, dtype=float) vals.fill(np.nan) rs = mom.rolling_window(vals, 5, 'boxcar', center=True) self.assertTrue(np.isnan(rs).all()) # empty vals = np.array([]) rs = mom.rolling_window(vals, 5, 'boxcar', center=True) self.assertEqual(len(rs), 0) # shorter than window vals = np.random.randn(5) rs = mom.rolling_window(vals, 10, 'boxcar') self.assertTrue(np.isnan(rs).all()) self.assertEqual(len(rs), 5) def test_cmov_window_frame(self): # Gh 8238 tm._skip_if_no_scipy() vals = np.array([[ 12.18, 3.64], [ 10.18, 9.16], [ 13.24, 14.61], [ 4.51, 8.11], [ 6.15, 11.44], [ 9.14, 6.21], [ 11.31, 10.67], [ 2.94, 6.51], [ 9.42, 8.39], [ 12.44, 7.34 ]]) xp = np.array([[ np.nan, np.nan], [ np.nan, np.nan], [ 9.252, 9.392], [ 8.644, 9.906], [ 8.87 , 10.208], [ 6.81 , 8.588], [ 7.792, 8.644], [ 9.05 , 7.824], [ np.nan, np.nan], [ np.nan, np.nan]]) # DataFrame rs = mom.rolling_window(DataFrame(vals), 5, 'boxcar', center=True) assert_frame_equal(DataFrame(xp), rs) def test_cmov_window_na_min_periods(self): tm._skip_if_no_scipy() # min_periods vals = Series(np.random.randn(10)) vals[4] = np.nan vals[8] = np.nan xp = mom.rolling_mean(vals, 5, min_periods=4, center=True) rs = mom.rolling_window(vals, 5, 'boxcar', min_periods=4, center=True) assert_series_equal(xp, rs) def test_cmov_window_regular(self): # GH 8238 tm._skip_if_no_scipy() win_types = ['triang', 'blackman', 'hamming', 'bartlett', 'bohman', 'blackmanharris', 'nuttall', 'barthann'] vals = np.array([6.95, 15.21, 4.72, 9.12, 13.81, 13.49, 16.68, 9.48, 10.63, 14.48]) xps = { 'hamming': [np.nan, np.nan, 8.71384, 9.56348, 12.38009, 14.03687, 13.8567, 11.81473, np.nan, np.nan], 'triang': [np.nan, np.nan, 9.28667, 10.34667, 12.00556, 13.33889, 13.38, 12.33667, np.nan, np.nan], 'barthann': [np.nan, np.nan, 8.4425, 9.1925, 12.5575, 14.3675, 14.0825, 11.5675, np.nan, np.nan], 'bohman': [np.nan, np.nan, 7.61599, 9.1764, 12.83559, 14.17267, 14.65923, 11.10401, np.nan, np.nan], 'blackmanharris': [np.nan, np.nan, 6.97691, 9.16438, 13.05052, 14.02156, 15.10512, 10.74574, np.nan, np.nan], 'nuttall': [np.nan, np.nan, 7.04618, 9.16786, 13.02671, 14.03559, 15.05657, 10.78514, np.nan, np.nan], 'blackman': [np.nan, np.nan, 7.73345, 9.17869, 12.79607, 14.20036, 14.57726, 11.16988, np.nan, np.nan], 'bartlett': [np.nan, np.nan, 8.4425, 9.1925, 12.5575, 14.3675, 14.0825, 11.5675, np.nan, np.nan]} for wt in win_types: xp = Series(xps[wt]) rs = mom.rolling_window(Series(vals), 5, wt, center=True) assert_series_equal(xp, rs) def test_cmov_window_regular_linear_range(self): # GH 8238 tm._skip_if_no_scipy() win_types = ['triang', 'blackman', 'hamming', 'bartlett', 'bohman', 'blackmanharris', 'nuttall', 'barthann'] vals = np.array(range(10), dtype=np.float) xp = vals.copy() xp[:2] = np.nan xp[-2:] = np.nan xp = Series(xp) for wt in win_types: rs = mom.rolling_window(Series(vals), 5, wt, center=True) assert_series_equal(xp, rs) def test_cmov_window_regular_missing_data(self): # GH 8238 tm._skip_if_no_scipy() win_types = ['triang', 'blackman', 'hamming', 'bartlett', 'bohman', 'blackmanharris', 'nuttall', 'barthann'] vals = np.array([6.95, 15.21, 4.72, 9.12, 13.81, 13.49, 16.68, np.nan, 10.63, 14.48]) xps = { 'bartlett': [np.nan, np.nan, 9.70333, 10.5225, 8.4425, 9.1925, 12.5575, 14.3675, 15.61667, 13.655], 'blackman': [np.nan, np.nan, 9.04582, 11.41536, 7.73345, 9.17869, 12.79607, 14.20036, 15.8706, 13.655], 'barthann': [np.nan, np.nan, 9.70333, 10.5225, 8.4425, 9.1925, 12.5575, 14.3675, 15.61667, 13.655], 'bohman': [np.nan, np.nan, 8.9444, 11.56327, 7.61599, 9.1764, 12.83559, 14.17267, 15.90976, 13.655], 'hamming': [np.nan, np.nan, 9.59321, 10.29694, 8.71384, 9.56348, 12.38009, 14.20565, 15.24694, 13.69758], 'nuttall': [np.nan, np.nan, 8.47693, 12.2821, 7.04618, 9.16786, 13.02671, 14.03673, 16.08759, 13.65553], 'triang': [np.nan, np.nan, 9.33167, 9.76125, 9.28667, 10.34667, 12.00556, 13.82125, 14.49429, 13.765], 'blackmanharris': [np.nan, np.nan, 8.42526, 12.36824, 6.97691, 9.16438, 13.05052, 14.02175, 16.1098, 13.65509] } for wt in win_types: xp = Series(xps[wt]) rs = mom.rolling_window(Series(vals), 5, wt, min_periods=3) assert_series_equal(xp, rs) def test_cmov_window_special(self): # GH 8238 tm._skip_if_no_scipy() win_types = ['kaiser', 'gaussian', 'general_gaussian', 'slepian'] kwds = [{'beta': 1.}, {'std': 1.}, {'power': 2., 'width': 2.}, {'width': 0.5}] vals = np.array([6.95, 15.21, 4.72, 9.12, 13.81, 13.49, 16.68, 9.48, 10.63, 14.48]) xps = { 'gaussian': [np.nan, np.nan, 8.97297, 9.76077, 12.24763, 13.89053, 13.65671, 12.01002, np.nan, np.nan], 'general_gaussian': [np.nan, np.nan, 9.85011, 10.71589, 11.73161, 13.08516, 12.95111, 12.74577, np.nan, np.nan], 'slepian': [np.nan, np.nan, 9.81073, 10.89359, 11.70284, 12.88331, 12.96079, 12.77008, np.nan, np.nan], 'kaiser': [np.nan, np.nan, 9.86851, 11.02969, 11.65161, 12.75129, 12.90702, 12.83757, np.nan, np.nan] } for wt, k in zip(win_types, kwds): xp = Series(xps[wt]) rs = mom.rolling_window(Series(vals), 5, wt, center=True, **k) assert_series_equal(xp, rs) def test_cmov_window_special_linear_range(self): # GH 8238 tm._skip_if_no_scipy() win_types = ['kaiser', 'gaussian', 'general_gaussian', 'slepian'] kwds = [{'beta': 1.}, {'std': 1.}, {'power': 2., 'width': 2.}, {'width': 0.5}] vals = np.array(range(10), dtype=np.float) xp = vals.copy() xp[:2] = np.nan xp[-2:] = np.nan xp = Series(xp) for wt, k in zip(win_types, kwds): rs = mom.rolling_window(Series(vals), 5, wt, center=True, **k) assert_series_equal(xp, rs) def test_rolling_median(self): self._check_moment_func(mom.rolling_median, np.median) def test_rolling_min(self): self._check_moment_func(mom.rolling_min, np.min) a = np.array([1, 2, 3, 4, 5]) b = mom.rolling_min(a, window=100, min_periods=1) assert_almost_equal(b, np.ones(len(a))) self.assertRaises(ValueError, mom.rolling_min, np.array([1, 2, 3]), window=3, min_periods=5) def test_rolling_max(self): self._check_moment_func(mom.rolling_max, np.max) a = np.array([1, 2, 3, 4, 5]) b = mom.rolling_max(a, window=100, min_periods=1) assert_almost_equal(a, b) self.assertRaises(ValueError, mom.rolling_max, np.array([1, 2, 3]), window=3, min_periods=5) def test_rolling_quantile(self): qs = [.1, .5, .9] def scoreatpercentile(a, per): values = np.sort(a, axis=0) idx = per / 1. * (values.shape[0] - 1) return values[int(idx)] for q in qs: def f(x, window, min_periods=None, freq=None, center=False): return mom.rolling_quantile(x, window, q, min_periods=min_periods, freq=freq, center=center) def alt(x): return scoreatpercentile(x, q) self._check_moment_func(f, alt) def test_rolling_apply(self): # suppress warnings about empty slices, as we are deliberately testing with a 0-length Series with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning) ser = Series([]) assert_series_equal(ser, mom.rolling_apply(ser, 10, lambda x: x.mean())) def roll_mean(x, window, min_periods=None, freq=None, center=False): return mom.rolling_apply(x, window, lambda x: x[np.isfinite(x)].mean(), min_periods=min_periods, freq=freq, center=center) self._check_moment_func(roll_mean, np.mean) # GH 8080 s = Series([None, None, None]) result = mom.rolling_apply(s, 2, lambda x: len(x), min_periods=0) expected = Series([1., 2., 2.]) assert_series_equal(result, expected) def test_rolling_apply_out_of_bounds(self): # #1850 arr = np.arange(4) # it works! result = mom.rolling_apply(arr, 10, np.sum) self.assertTrue(isnull(result).all()) result = mom.rolling_apply(arr, 10, np.sum, min_periods=1) assert_almost_equal(result, result) def test_rolling_std(self): self._check_moment_func(mom.rolling_std, lambda x: np.std(x, ddof=1)) self._check_moment_func(functools.partial(mom.rolling_std, ddof=0), lambda x: np.std(x, ddof=0)) def test_rolling_std_1obs(self): result = mom.rolling_std(np.array([1., 2., 3., 4., 5.]), 1, min_periods=1) expected = np.array([np.nan] * 5) assert_almost_equal(result, expected) result = mom.rolling_std(np.array([1., 2., 3., 4., 5.]), 1, min_periods=1, ddof=0) expected = np.zeros(5) assert_almost_equal(result, expected) result = mom.rolling_std(np.array([np.nan, np.nan, 3., 4., 5.]), 3, min_periods=2) self.assertTrue(np.isnan(result[2])) def test_rolling_std_neg_sqrt(self): # unit test from Bottleneck # Test move_nanstd for neg sqrt. a = np.array([0.0011448196318903589, 0.00028718669878572767, 0.00028718669878572767, 0.00028718669878572767, 0.00028718669878572767]) b = mom.rolling_std(a, window=3) self.assertTrue(np.isfinite(b[2:]).all()) b = mom.ewmstd(a, span=3) self.assertTrue(np.isfinite(b[2:]).all()) def test_rolling_var(self): self._check_moment_func(mom.rolling_var, lambda x: np.var(x, ddof=1), test_stable=True) self._check_moment_func(functools.partial(mom.rolling_var, ddof=0), lambda x: np.var(x, ddof=0)) def test_rolling_skew(self): try: from scipy.stats import skew except ImportError: raise nose.SkipTest('no scipy') self._check_moment_func(mom.rolling_skew, lambda x: skew(x, bias=False)) def test_rolling_kurt(self): try: from scipy.stats import kurtosis except ImportError: raise nose.SkipTest('no scipy') self._check_moment_func(mom.rolling_kurt, lambda x: kurtosis(x, bias=False)) def test_fperr_robustness(self): # TODO: remove this once python 2.5 out of picture if PY3: raise nose.SkipTest("doesn't work on python 3") # #2114 data = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a@\xaa\xaa\xaa\xaa\xaa\xaa\x02@8\x8e\xe38\x8e\xe3\xe8?z\t\xed%\xb4\x97\xd0?\xa2\x0c<\xdd\x9a\x1f\xb6?\x82\xbb\xfa&y\x7f\x9d?\xac\'\xa7\xc4P\xaa\x83?\x90\xdf\xde\xb0k8j?`\xea\xe9u\xf2zQ?*\xe37\x9d\x98N7?\xe2.\xf5&v\x13\x1f?\xec\xc9\xf8\x19\xa4\xb7\x04?\x90b\xf6w\x85\x9f\xeb>\xb5A\xa4\xfaXj\xd2>F\x02\xdb\xf8\xcb\x8d\xb8>.\xac<\xfb\x87^\xa0>\xe8:\xa6\xf9_\xd3\x85>\xfb?\xe2cUU\xfd?\xfc\x7fA\xed8\x8e\xe3?\xa5\xaa\xac\x91\xf6\x12\xca?n\x1cs\xb6\xf9a\xb1?\xe8%D\xf3L-\x97?5\xddZD\x11\xe7~?#>\xe7\x82\x0b\x9ad?\xd9R4Y\x0fxK?;7x;\nP2?N\xf4JO\xb8j\x18?4\xf81\x8a%G\x00?\x9a\xf5\x97\r2\xb4\xe5>\xcd\x9c\xca\xbcB\xf0\xcc>3\x13\x87(\xd7J\xb3>\x99\x19\xb4\xe0\x1e\xb9\x99>ff\xcd\x95\x14&\x81>\x88\x88\xbc\xc7p\xddf>`\x0b\xa6_\x96|N>@\xb2n\xea\x0eS4>U\x98\x938i\x19\x1b>\x8eeb\xd0\xf0\x10\x02>\xbd\xdc-k\x96\x16\xe8=(\x93\x1e\xf2\x0e\x0f\xd0=\xe0n\xd3Bii\xb5=*\xe9\x19Y\x8c\x8c\x9c=\xc6\xf0\xbb\x90]\x08\x83=]\x96\xfa\xc0|`i=>d\xfc\xd5\xfd\xeaP=R0\xfb\xc7\xa7\x8e6=\xc2\x95\xf9_\x8a\x13\x1e=\xd6c\xa6\xea\x06\r\x04=r\xda\xdd8\t\xbc\xea<\xf6\xe6\x93\xd0\xb0\xd2\xd1<\x9d\xdeok\x96\xc3\xb7<&~\xea9s\xaf\x9f<UUUUUU\x13@q\x1c\xc7q\x1c\xc7\xf9?\xf6\x12\xdaKh/\xe1?\xf2\xc3"e\xe0\xe9\xc6?\xed\xaf\x831+\x8d\xae?\xf3\x1f\xad\xcb\x1c^\x94?\x15\x1e\xdd\xbd>\xb8\x02@\xc6\xd2&\xfd\xa8\xf5\xe8?\xd9\xe1\x19\xfe\xc5\xa3\xd0?v\x82"\xa8\xb2/\xb6?\x9dX\x835\xee\x94\x9d?h\x90W\xce\x9e\xb8\x83?\x8a\xc0th~Kj?\\\x80\xf8\x9a\xa9\x87Q?%\xab\xa0\xce\x8c_7?1\xe4\x80\x13\x11*\x1f? \x98\x00\r\xb6\xc6\x04?\x80u\xabf\x9d\xb3\xeb>UNrD\xbew\xd2>\x1c\x13C[\xa8\x9f\xb8>\x12b\xd7<pj\xa0>m-\x1fQ@\xe3\x85>\xe6\x91)l\x00/m>Da\xc6\xf2\xaatS>\x05\xd7]\xee\xe3\xf09>' arr = np.frombuffer(data, dtype='<f8') if sys.byteorder != "little": arr = arr.byteswap().newbyteorder() result = mom.rolling_sum(arr, 2) self.assertTrue((result[1:] >= 0).all()) result = mom.rolling_mean(arr, 2) self.assertTrue((result[1:] >= 0).all()) result = mom.rolling_var(arr, 2) self.assertTrue((result[1:] >= 0).all()) # #2527, ugh arr = np.array([0.00012456, 0.0003, 0]) result = mom.rolling_mean(arr, 1) self.assertTrue(result[-1] >= 0) result = mom.rolling_mean(-arr, 1) self.assertTrue(result[-1] <= 0) def _check_moment_func(self, func, static_comp, window=50, has_min_periods=True, has_center=True, has_time_rule=True, preserve_nan=True, fill_value=None, test_stable=False): self._check_ndarray(func, static_comp, window=window, has_min_periods=has_min_periods, preserve_nan=preserve_nan, has_center=has_center, fill_value=fill_value, test_stable=test_stable) self._check_structures(func, static_comp, has_min_periods=has_min_periods, has_time_rule=has_time_rule, fill_value=fill_value, has_center=has_center) def _check_ndarray(self, func, static_comp, window=50, has_min_periods=True, preserve_nan=True, has_center=True, fill_value=None, test_stable=False, test_window=True): result = func(self.arr, window) assert_almost_equal(result[-1], static_comp(self.arr[-50:])) if preserve_nan: assert(np.isnan(result[self._nan_locs]).all()) # excluding NaNs correctly arr = randn(50) arr[:10] = np.NaN arr[-10:] = np.NaN if has_min_periods: result = func(arr, 50, min_periods=30) assert_almost_equal(result[-1], static_comp(arr[10:-10])) # min_periods is working correctly result = func(arr, 20, min_periods=15) self.assertTrue(np.isnan(result[23])) self.assertFalse(np.isnan(result[24])) self.assertFalse(np.isnan(result[-6])) self.assertTrue(np.isnan(result[-5])) arr2 = randn(20) result = func(arr2, 10, min_periods=5) self.assertTrue(isnull(result[3])) self.assertTrue(notnull(result[4])) # min_periods=0 result0 = func(arr, 20, min_periods=0) result1 = func(arr, 20, min_periods=1) assert_almost_equal(result0, result1) else: result = func(arr, 50) assert_almost_equal(result[-1], static_comp(arr[10:-10])) # GH 7925 if has_center: if has_min_periods: result = func(arr, 20, min_periods=15, center=True) expected = func(np.concatenate((arr, np.array([np.NaN] * 9))), 20, min_periods=15)[9:] else: result = func(arr, 20, center=True) expected = func(np.concatenate((arr, np.array([np.NaN] * 9))), 20)[9:] self.assert_numpy_array_equivalent(result, expected) if test_stable: result = func(self.arr + 1e9, window) assert_almost_equal(result[-1], static_comp(self.arr[-50:] + 1e9)) # Test window larger than array, #7297 if test_window: if has_min_periods: for minp in (0, len(self.arr)-1, len(self.arr)): result = func(self.arr, len(self.arr)+1, min_periods=minp) expected = func(self.arr, len(self.arr), min_periods=minp) nan_mask = np.isnan(result) self.assertTrue(np.array_equal(nan_mask, np.isnan(expected))) nan_mask = ~nan_mask assert_almost_equal(result[nan_mask], expected[nan_mask]) else: result = func(self.arr, len(self.arr)+1) expected = func(self.arr, len(self.arr)) nan_mask = np.isnan(result) self.assertTrue(np.array_equal(nan_mask, np.isnan(expected))) nan_mask = ~nan_mask assert_almost_equal(result[nan_mask], expected[nan_mask]) def _check_structures(self, func, static_comp, has_min_periods=True, has_time_rule=True, has_center=True, fill_value=None): series_result = func(self.series, 50) tm.assert_isinstance(series_result, Series) frame_result = func(self.frame, 50) self.assertEqual(type(frame_result), DataFrame) # check time_rule works if has_time_rule: win = 25 minp = 10 if has_min_periods: series_result = func(self.series[::2], win, min_periods=minp, freq='B') frame_result = func(self.frame[::2], win, min_periods=minp, freq='B') else: series_result = func(self.series[::2], win, freq='B') frame_result = func(self.frame[::2], win, freq='B') last_date = series_result.index[-1] prev_date = last_date - 24 * datetools.bday trunc_series = self.series[::2].truncate(prev_date, last_date) trunc_frame = self.frame[::2].truncate(prev_date, last_date) assert_almost_equal(series_result[-1], static_comp(trunc_series)) assert_almost_equal(frame_result.xs(last_date), trunc_frame.apply(static_comp)) # GH 7925 if has_center: if has_min_periods: minp = 10 series_xp = func(self.series.reindex(list(self.series.index)+['x%d'%x for x in range(12)]), 25, min_periods=minp).shift(-12).reindex(self.series.index) frame_xp = func(self.frame.reindex(list(self.frame.index)+['x%d'%x for x in range(12)]), 25, min_periods=minp).shift(-12).reindex(self.frame.index) series_rs = func(self.series, 25, min_periods=minp, center=True) frame_rs = func(self.frame, 25, min_periods=minp, center=True) else: series_xp = func(self.series.reindex(list(self.series.index)+['x%d'%x for x in range(12)]), 25).shift(-12).reindex(self.series.index) frame_xp = func(self.frame.reindex(list(self.frame.index)+['x%d'%x for x in range(12)]), 25).shift(-12).reindex(self.frame.index) series_rs = func(self.series, 25, center=True) frame_rs = func(self.frame, 25, center=True) if fill_value is not None: series_xp = series_xp.fillna(fill_value) frame_xp = frame_xp.fillna(fill_value) assert_series_equal(series_xp, series_rs) assert_frame_equal(frame_xp, frame_rs) def test_ewma(self): self._check_ew(mom.ewma) arr = np.zeros(1000) arr[5] = 1 result = mom.ewma(arr, span=100, adjust=False).sum() self.assertTrue(np.abs(result - 1) < 1e-2) s = Series([1.0, 2.0, 4.0, 8.0]) expected = Series([1.0, 1.6, 2.736842, 4.923077]) for f in [lambda s: mom.ewma(s, com=2.0, adjust=True), lambda s: mom.ewma(s, com=2.0, adjust=True, ignore_na=False), lambda s: mom.ewma(s, com=2.0, adjust=True, ignore_na=True), ]: result = f(s) assert_series_equal(result, expected) expected = Series([1.0, 1.333333, 2.222222, 4.148148]) for f in [lambda s: mom.ewma(s, com=2.0, adjust=False), lambda s: mom.ewma(s, com=2.0, adjust=False, ignore_na=False), lambda s: mom.ewma(s, com=2.0, adjust=False, ignore_na=True), ]: result = f(s) assert_series_equal(result, expected) def test_ewma_nan_handling(self): s = Series([1.] + [np.nan] * 5 + [1.]) result = mom.ewma(s, com=5) assert_almost_equal(result, [1.] * len(s)) s = Series([np.nan] * 2 + [1.] + [np.nan] * 2 + [1.]) result = mom.ewma(s, com=5) assert_almost_equal(result, [np.nan] * 2 + [1.] * 4) # GH 7603 s0 = Series([np.nan, 1., 101.]) s1 = Series([1., np.nan, 101.]) s2 = Series([np.nan, 1., np.nan, np.nan, 101., np.nan]) s3 = Series([1., np.nan, 101., 50.]) com = 2. alpha = 1. / (1. + com) def simple_wma(s, w): return (s.multiply(w).cumsum() / w.cumsum()).fillna(method='ffill') for (s, adjust, ignore_na, w) in [ (s0, True, False, [np.nan, (1. - alpha), 1.]), (s0, True, True, [np.nan, (1. - alpha), 1.]), (s0, False, False, [np.nan, (1. - alpha), alpha]), (s0, False, True, [np.nan, (1. - alpha), alpha]), (s1, True, False, [(1. - alpha)**2, np.nan, 1.]), (s1, True, True, [(1. - alpha), np.nan, 1.]), (s1, False, False, [(1. - alpha)**2, np.nan, alpha]), (s1, False, True, [(1. - alpha), np.nan, alpha]), (s2, True, False, [np.nan, (1. - alpha)**3, np.nan, np.nan, 1., np.nan]), (s2, True, True, [np.nan, (1. - alpha), np.nan, np.nan, 1., np.nan]), (s2, False, False, [np.nan, (1. - alpha)**3, np.nan, np.nan, alpha, np.nan]), (s2, False, True, [np.nan, (1. - alpha), np.nan, np.nan, alpha, np.nan]), (s3, True, False, [(1. - alpha)**3, np.nan, (1. - alpha), 1.]), (s3, True, True, [(1. - alpha)**2, np.nan, (1. - alpha), 1.]), (s3, False, False, [(1. - alpha)**3, np.nan, (1. - alpha) * alpha, alpha * ((1. - alpha)**2 + alpha)]), (s3, False, True, [(1. - alpha)**2, np.nan, (1. - alpha) * alpha, alpha]), ]: expected = simple_wma(s, Series(w)) result = mom.ewma(s, com=com, adjust=adjust, ignore_na=ignore_na) assert_series_equal(result, expected) if ignore_na is False: # check that ignore_na defaults to False result = mom.ewma(s, com=com, adjust=adjust) assert_series_equal(result, expected) def test_ewmvar(self): self._check_ew(mom.ewmvar) def test_ewmvol(self): self._check_ew(mom.ewmvol) def test_ewma_span_com_args(self): A = mom.ewma(self.arr, com=9.5) B = mom.ewma(self.arr, span=20) assert_almost_equal(A, B) self.assertRaises(Exception, mom.ewma, self.arr, com=9.5, span=20) self.assertRaises(Exception, mom.ewma, self.arr) def test_ewma_halflife_arg(self): A = mom.ewma(self.arr, com=13.932726172912965) B = mom.ewma(self.arr, halflife=10.0) assert_almost_equal(A, B) self.assertRaises(Exception, mom.ewma, self.arr, span=20, halflife=50) self.assertRaises(Exception, mom.ewma, self.arr, com=9.5, halflife=50) self.assertRaises(Exception, mom.ewma, self.arr, com=9.5, span=20, halflife=50) self.assertRaises(Exception, mom.ewma, self.arr) def test_ew_empty_arrays(self): arr = np.array([], dtype=np.float64) funcs = [mom.ewma, mom.ewmvol, mom.ewmvar] for f in funcs: result = f(arr, 3) assert_almost_equal(result, arr) def _check_ew(self, func): self._check_ew_ndarray(func) self._check_ew_structures(func) def _check_ew_ndarray(self, func, preserve_nan=False): result = func(self.arr, com=10) if preserve_nan: assert(np.isnan(result[self._nan_locs]).all()) # excluding NaNs correctly arr = randn(50) arr[:10] = np.NaN arr[-10:] = np.NaN s = Series(arr) # check min_periods # GH 7898 result = func(s, 50, min_periods=2) self.assertTrue(np.isnan(result.values[:11]).all()) self.assertFalse(np.isnan(result.values[11:]).any()) for min_periods in (0, 1): result = func(s, 50, min_periods=min_periods) if func == mom.ewma: self.assertTrue(np.isnan(result.values[:10]).all()) self.assertFalse(np.isnan(result.values[10:]).any()) else: # ewmstd, ewmvol, ewmvar (with bias=False) require at least two values self.assertTrue(np.isnan(result.values[:11]).all()) self.assertFalse(np.isnan(result.values[11:]).any()) # check series of length 0 result = func(Series([]), 50, min_periods=min_periods) assert_series_equal(result, Series([])) # check series of length 1 result = func(Series([1.]), 50, min_periods=min_periods) if func == mom.ewma: assert_series_equal(result, Series([1.])) else: # ewmstd, ewmvol, ewmvar with bias=False require at least two values assert_series_equal(result, Series([np.NaN])) # pass in ints result2 = func(np.arange(50), span=10) self.assertEqual(result2.dtype, np.float_) def _check_ew_structures(self, func): series_result = func(self.series, com=10) tm.assert_isinstance(series_result, Series) frame_result = func(self.frame, com=10) self.assertEqual(type(frame_result), DataFrame) # create the data only once as we are not setting it def _create_consistency_data(): def create_series(): return [Series(), Series([np.nan]), Series([np.nan, np.nan]), Series([3.]), Series([np.nan, 3.]), Series([3., np.nan]), Series([1., 3.]), Series([2., 2.]), Series([3., 1.]), Series([5., 5., 5., 5., np.nan, np.nan, np.nan, 5., 5., np.nan, np.nan]), Series([np.nan, 5., 5., 5., np.nan, np.nan, np.nan, 5., 5., np.nan, np.nan]), Series([np.nan, np.nan, 5., 5., np.nan, np.nan, np.nan, 5., 5., np.nan, np.nan]), Series([np.nan, 3., np.nan, 3., 4., 5., 6., np.nan, np.nan, 7., 12., 13., 14., 15.]), Series([np.nan, 5., np.nan, 2., 4., 0., 9., np.nan, np.nan, 3., 12., 13., 14., 15.]), Series([2., 3., np.nan, 3., 4., 5., 6., np.nan, np.nan, 7., 12., 13., 14., 15.]), Series([2., 5., np.nan, 2., 4., 0., 9., np.nan, np.nan, 3., 12., 13., 14., 15.]), Series(range(10)), Series(range(20, 0, -2)), ] def create_dataframes(): return [DataFrame(), DataFrame(columns=['a']), DataFrame(columns=['a', 'a']), DataFrame(columns=['a', 'b']), DataFrame(np.arange(10).reshape((5, 2))), DataFrame(np.arange(25).reshape((5, 5))), DataFrame(np.arange(25).reshape((5, 5)), columns=['a', 'b', 99, 'd', 'd']), ] + [DataFrame(s) for s in create_series()] def is_constant(x): values = x.values.ravel() return len(set(values[notnull(values)])) == 1 def no_nans(x): return x.notnull().all().all() # data is a tuple(object, is_contant, no_nans) data = create_series() + create_dataframes() return [ (x, is_constant(x), no_nans(x)) for x in data ] _consistency_data = _create_consistency_data() class TestMomentsConsistency(Base): def _create_data(self): super(TestMomentsConsistency, self)._create_data() self.data = _consistency_data def setUp(self): self._create_data() warnings.simplefilter("ignore", category=FutureWarning) def _test_moments_consistency(self, min_periods, count, mean, mock_mean, corr, var_unbiased=None, std_unbiased=None, cov_unbiased=None, var_biased=None, std_biased=None, cov_biased=None, var_debiasing_factors=None): def _non_null_values(x): values = x.values.ravel() return set(values[notnull(values)].tolist()) for (x, is_constant, no_nans) in self.data: assert_equal = assert_series_equal if isinstance(x, Series) else assert_frame_equal count_x = count(x) mean_x = mean(x) if mock_mean: # check that mean equals mock_mean expected = mock_mean(x) assert_equal(mean_x, expected) # check that correlation of a series with itself is either 1 or NaN corr_x_x = corr(x, x) # self.assertTrue(_non_null_values(corr_x_x).issubset(set([1.]))) # restore once rolling_cov(x, x) is identically equal to var(x) if is_constant: # check mean of constant series expected = x * np.nan expected[count_x >= max(min_periods, 1)] = x.max().max() assert_equal(mean_x, expected) # check correlation of constant series with itself is NaN expected[:] = np.nan assert_equal(corr_x_x, expected) if var_unbiased and var_biased and var_debiasing_factors: # check variance debiasing factors var_unbiased_x = var_unbiased(x) var_biased_x = var_biased(x) var_debiasing_factors_x = var_debiasing_factors(x) assert_equal(var_unbiased_x, var_biased_x * var_debiasing_factors_x) for (std, var, cov) in [(std_biased, var_biased, cov_biased), (std_unbiased, var_unbiased, cov_unbiased)]: # check that var(x), std(x), and cov(x) are all >= 0 var_x = var(x) std_x = std(x) self.assertFalse((var_x < 0).any().any()) self.assertFalse((std_x < 0).any().any()) if cov: cov_x_x = cov(x, x) self.assertFalse((cov_x_x < 0).any().any()) # check that var(x) == cov(x, x) assert_equal(var_x, cov_x_x) # check that var(x) == std(x)^2 assert_equal(var_x, std_x * std_x) if var is var_biased: # check that biased var(x) == mean(x^2) - mean(x)^2 mean_x2 = mean(x * x) assert_equal(var_x, mean_x2 - (mean_x * mean_x)) if is_constant: # check that variance of constant series is identically 0 self.assertFalse((var_x > 0).any().any()) expected = x * np.nan expected[count_x >= max(min_periods, 1)] = 0. if var is var_unbiased: expected[count_x < 2] = np.nan assert_equal(var_x, expected) if isinstance(x, Series): for (y, is_constant, no_nans) in self.data: if not x.isnull().equals(y.isnull()): # can only easily test two Series with similar structure continue # check that cor(x, y) is symmetric corr_x_y = corr(x, y) corr_y_x = corr(y, x) assert_equal(corr_x_y, corr_y_x) if cov: # check that cov(x, y) is symmetric cov_x_y = cov(x, y) cov_y_x = cov(y, x) assert_equal(cov_x_y, cov_y_x) # check that cov(x, y) == (var(x+y) - var(x) - var(y)) / 2 var_x_plus_y = var(x + y) var_y = var(y) assert_equal(cov_x_y, 0.5 * (var_x_plus_y - var_x - var_y)) # check that corr(x, y) == cov(x, y) / (std(x) * std(y)) std_y = std(y) assert_equal(corr_x_y, cov_x_y / (std_x * std_y)) if cov is cov_biased: # check that biased cov(x, y) == mean(x*y) - mean(x)*mean(y) mean_y = mean(y) mean_x_times_y = mean(x * y) assert_equal(cov_x_y, mean_x_times_y - (mean_x * mean_y)) @slow def test_ewm_consistency(self): def _weights(s, com, adjust, ignore_na): if isinstance(s, DataFrame): if not len(s.columns): return DataFrame(index=s.index, columns=s.columns) w = concat([ _weights(s.iloc[:, i], com=com, adjust=adjust, ignore_na=ignore_na) for i, _ in enumerate(s.columns) ], axis=1) w.index=s.index w.columns=s.columns return w w = Series(np.nan, index=s.index) alpha = 1. / (1. + com) if ignore_na: w[s.notnull()] = _weights(s[s.notnull()], com=com, adjust=adjust, ignore_na=False) elif adjust: for i in range(len(s)): if s.iat[i] == s.iat[i]: w.iat[i] = pow(1. / (1. - alpha), i) else: sum_wts = 0. prev_i = -1 for i in range(len(s)): if s.iat[i] == s.iat[i]: if prev_i == -1: w.iat[i] = 1. else: w.iat[i] = alpha * sum_wts / pow(1. - alpha, i - prev_i) sum_wts += w.iat[i] prev_i = i return w def _variance_debiasing_factors(s, com, adjust, ignore_na): weights = _weights(s, com=com, adjust=adjust, ignore_na=ignore_na) cum_sum = weights.cumsum().fillna(method='ffill') cum_sum_sq = (weights * weights).cumsum().fillna(method='ffill') numerator = cum_sum * cum_sum denominator = numerator - cum_sum_sq denominator[denominator <= 0.] = np.nan return numerator / denominator def _ewma(s, com, min_periods, adjust, ignore_na): weights = _weights(s, com=com, adjust=adjust, ignore_na=ignore_na) result = s.multiply(weights).cumsum().divide(weights.cumsum()).fillna(method='ffill') result[mom.expanding_count(s) < (max(min_periods, 1) if min_periods else 1)] = np.nan return result com = 3. for min_periods in [0, 1, 2, 3, 4]: for adjust in [True, False]: for ignore_na in [False, True]: # test consistency between different ewm* moments self._test_moments_consistency( min_periods=min_periods, count=mom.expanding_count, mean=lambda x: mom.ewma(x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na), mock_mean=lambda x: _ewma(x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na), corr=lambda x, y: mom.ewmcorr(x, y, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na), var_unbiased=lambda x: mom.ewmvar(x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na, bias=False), std_unbiased=lambda x: mom.ewmstd(x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na, bias=False), cov_unbiased=lambda x, y: mom.ewmcov(x, y, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na, bias=False), var_biased=lambda x: mom.ewmvar(x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na, bias=True), std_biased=lambda x: mom.ewmstd(x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na, bias=True), cov_biased=lambda x, y: mom.ewmcov(x, y, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na, bias=True), var_debiasing_factors=lambda x: _variance_debiasing_factors(x, com=com, adjust=adjust, ignore_na=ignore_na)) @slow def test_expanding_consistency(self): base_functions = [ (mom.expanding_count, lambda v: Series(v).count(), None), (mom.expanding_max, lambda v: Series(v).max(), None), (mom.expanding_min, lambda v: Series(v).min(), None), (mom.expanding_sum, lambda v: Series(v).sum(), None), (mom.expanding_mean, lambda v: Series(v).mean(), None), (mom.expanding_std, lambda v: Series(v).std(), 1), (mom.expanding_cov, lambda v: Series(v).cov(Series(v)), None), (mom.expanding_corr, lambda v: Series(v).corr(Series(v)), None), (mom.expanding_var, lambda v: Series(v).var(), 1), #(mom.expanding_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed #(mom.expanding_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed #(lambda x, min_periods: mom.expanding_quantile(x, 0.3, min_periods=min_periods), # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed (mom.expanding_median, lambda v: Series(v).median(), None), (mom.expanding_max, np.nanmax, 1), (mom.expanding_min, np.nanmin, 1), (mom.expanding_sum, np.nansum, 1), ] if np.__version__ >= LooseVersion('1.8.0'): base_functions += [ (mom.expanding_mean, np.nanmean, 1), (mom.expanding_std, lambda v: np.nanstd(v, ddof=1), 1), (mom.expanding_var, lambda v: np.nanvar(v, ddof=1), 1), ] if np.__version__ >= LooseVersion('1.9.0'): base_functions += [ (mom.expanding_median, np.nanmedian, 1), ] no_nan_functions = [ (mom.expanding_max, np.max, None), (mom.expanding_min, np.min, None), (mom.expanding_sum, np.sum, None), (mom.expanding_mean, np.mean, None), (mom.expanding_std, lambda v: np.std(v, ddof=1), 1), (mom.expanding_var, lambda v: np.var(v, ddof=1), 1), (mom.expanding_median, np.median, None), ] # suppress warnings about empty slices, as we are deliberately testing with empty/0-length Series/DataFrames with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning) for min_periods in [0, 1, 2, 3, 4]: # test consistency between different expanding_* moments self._test_moments_consistency( min_periods=min_periods, count=mom.expanding_count, mean=lambda x: mom.expanding_mean(x, min_periods=min_periods), mock_mean=lambda x: mom.expanding_sum(x, min_periods=min_periods) / mom.expanding_count(x), corr=lambda x, y: mom.expanding_corr(x, y, min_periods=min_periods), var_unbiased=lambda x: mom.expanding_var(x, min_periods=min_periods), std_unbiased=lambda x: mom.expanding_std(x, min_periods=min_periods), cov_unbiased=lambda x, y: mom.expanding_cov(x, y, min_periods=min_periods), var_biased=lambda x: mom.expanding_var(x, min_periods=min_periods, ddof=0), std_biased=lambda x: mom.expanding_std(x, min_periods=min_periods, ddof=0), cov_biased=lambda x, y: mom.expanding_cov(x, y, min_periods=min_periods, ddof=0), var_debiasing_factors=lambda x: mom.expanding_count(x) / (mom.expanding_count(x) - 1.).replace(0., np.nan) ) # test consistency between expanding_xyz() and either (a) expanding_apply of Series.xyz(), # or (b) expanding_apply of np.nanxyz() for (x, is_constant, no_nans) in self.data: assert_equal = assert_series_equal if isinstance(x, Series) else assert_frame_equal functions = base_functions # GH 8269 if no_nans: functions = base_functions + no_nan_functions for (expanding_f, f, require_min_periods) in functions: if require_min_periods and (min_periods is not None) and (min_periods < require_min_periods): continue if expanding_f is mom.expanding_count: expanding_f_result = expanding_f(x) expanding_apply_f_result = mom.expanding_apply(x, func=f, min_periods=0) else: if expanding_f in [mom.expanding_cov, mom.expanding_corr]: expanding_f_result = expanding_f(x, min_periods=min_periods, pairwise=False) else: expanding_f_result = expanding_f(x, min_periods=min_periods) expanding_apply_f_result = mom.expanding_apply(x, func=f, min_periods=min_periods) assert_equal(expanding_f_result, expanding_apply_f_result) if (expanding_f in [mom.expanding_cov, mom.expanding_corr]) and isinstance(x, DataFrame): # test pairwise=True expanding_f_result = expanding_f(x, x, min_periods=min_periods, pairwise=True) expected = Panel(items=x.index, major_axis=x.columns, minor_axis=x.columns) for i, _ in enumerate(x.columns): for j, _ in enumerate(x.columns): expected.iloc[:, i, j] = expanding_f(x.iloc[:, i], x.iloc[:, j], min_periods=min_periods) assert_panel_equal(expanding_f_result, expected) @slow def test_rolling_consistency(self): base_functions = [ (mom.rolling_count, lambda v: Series(v).count(), None), (mom.rolling_max, lambda v: Series(v).max(), None), (mom.rolling_min, lambda v: Series(v).min(), None), (mom.rolling_sum, lambda v: Series(v).sum(), None), (mom.rolling_mean, lambda v: Series(v).mean(), None), (mom.rolling_std, lambda v: Series(v).std(), 1), (mom.rolling_cov, lambda v: Series(v).cov(Series(v)), None), (mom.rolling_corr, lambda v: Series(v).corr(Series(v)), None), (mom.rolling_var, lambda v: Series(v).var(), 1), #(mom.rolling_skew, lambda v: Series(v).skew(), 3), # restore once GH 8086 is fixed #(mom.rolling_kurt, lambda v: Series(v).kurt(), 4), # restore once GH 8086 is fixed #(lambda x, window, min_periods, center: mom.rolling_quantile(x, window, 0.3, min_periods=min_periods, center=center), # lambda v: Series(v).quantile(0.3), None), # restore once GH 8084 is fixed (mom.rolling_median, lambda v: Series(v).median(), None), (mom.rolling_max, np.nanmax, 1), (mom.rolling_min, np.nanmin, 1), (mom.rolling_sum, np.nansum, 1), ] if np.__version__ >= LooseVersion('1.8.0'): base_functions += [ (mom.rolling_mean, np.nanmean, 1), (mom.rolling_std, lambda v: np.nanstd(v, ddof=1), 1), (mom.rolling_var, lambda v: np.nanvar(v, ddof=1), 1), ] if np.__version__ >= LooseVersion('1.9.0'): base_functions += [ (mom.rolling_median, np.nanmedian, 1), ] no_nan_functions = [ (mom.rolling_max, np.max, None), (mom.rolling_min, np.min, None), (mom.rolling_sum, np.sum, None), (mom.rolling_mean, np.mean, None), (mom.rolling_std, lambda v: np.std(v, ddof=1), 1), (mom.rolling_var, lambda v: np.var(v, ddof=1), 1), (mom.rolling_median, np.median, None), ] for window in [1, 2, 3, 10, 20]: for min_periods in set([0, 1, 2, 3, 4, window]): if min_periods and (min_periods > window): continue for center in [False, True]: # test consistency between different rolling_* moments self._test_moments_consistency( min_periods=min_periods, count=lambda x: mom.rolling_count(x, window=window, center=center), mean=lambda x: mom.rolling_mean(x, window=window, min_periods=min_periods, center=center), mock_mean=lambda x: mom.rolling_sum(x, window=window, min_periods=min_periods, center=center).divide( mom.rolling_count(x, window=window, center=center)), corr=lambda x, y: mom.rolling_corr(x, y, window=window, min_periods=min_periods, center=center), var_unbiased=lambda x: mom.rolling_var(x, window=window, min_periods=min_periods, center=center), std_unbiased=lambda x: mom.rolling_std(x, window=window, min_periods=min_periods, center=center), cov_unbiased=lambda x, y: mom.rolling_cov(x, y, window=window, min_periods=min_periods, center=center), var_biased=lambda x: mom.rolling_var(x, window=window, min_periods=min_periods, center=center, ddof=0), std_biased=lambda x: mom.rolling_std(x, window=window, min_periods=min_periods, center=center, ddof=0), cov_biased=lambda x, y: mom.rolling_cov(x, y, window=window, min_periods=min_periods, center=center, ddof=0), var_debiasing_factors=lambda x: mom.rolling_count(x, window=window, center=center).divide( (mom.rolling_count(x, window=window, center=center) - 1.).replace(0., np.nan)), ) # test consistency between rolling_xyz() and either (a) rolling_apply of Series.xyz(), # or (b) rolling_apply of np.nanxyz() for (x, is_constant, no_nans) in self.data: assert_equal = assert_series_equal if isinstance(x, Series) else assert_frame_equal functions = base_functions # GH 8269 if no_nans: functions = base_functions + no_nan_functions for (rolling_f, f, require_min_periods) in functions: if require_min_periods and (min_periods is not None) and (min_periods < require_min_periods): continue if rolling_f is mom.rolling_count: rolling_f_result = rolling_f(x, window=window, center=center) rolling_apply_f_result = mom.rolling_apply(x, window=window, func=f, min_periods=0, center=center) else: if rolling_f in [mom.rolling_cov, mom.rolling_corr]: rolling_f_result = rolling_f(x, window=window, min_periods=min_periods, center=center, pairwise=False) else: rolling_f_result = rolling_f(x, window=window, min_periods=min_periods, center=center) rolling_apply_f_result = mom.rolling_apply(x, window=window, func=f, min_periods=min_periods, center=center) assert_equal(rolling_f_result, rolling_apply_f_result) if (rolling_f in [mom.rolling_cov, mom.rolling_corr]) and isinstance(x, DataFrame): # test pairwise=True rolling_f_result = rolling_f(x, x, window=window, min_periods=min_periods, center=center, pairwise=True) expected = Panel(items=x.index, major_axis=x.columns, minor_axis=x.columns) for i, _ in enumerate(x.columns): for j, _ in enumerate(x.columns): expected.iloc[:, i, j] = rolling_f(x.iloc[:, i], x.iloc[:, j], window=window, min_periods=min_periods, center=center) assert_panel_equal(rolling_f_result, expected) # binary moments def test_rolling_cov(self): A = self.series B = A + randn(len(A)) result = mom.rolling_cov(A, B, 50, min_periods=25) assert_almost_equal(result[-1], np.cov(A[-50:], B[-50:])[0, 1]) def test_rolling_cov_pairwise(self): self._check_pairwise_moment(mom.rolling_cov, 10, min_periods=5) def test_rolling_corr(self): A = self.series B = A + randn(len(A)) result = mom.rolling_corr(A, B, 50, min_periods=25) assert_almost_equal(result[-1], np.corrcoef(A[-50:], B[-50:])[0, 1]) # test for correct bias correction a = tm.makeTimeSeries() b = tm.makeTimeSeries() a[:5] = np.nan b[:10] = np.nan result = mom.rolling_corr(a, b, len(a), min_periods=1) assert_almost_equal(result[-1], a.corr(b)) def test_rolling_corr_pairwise(self): self._check_pairwise_moment(mom.rolling_corr, 10, min_periods=5) def _check_pairwise_moment(self, func, *args, **kwargs): panel = func(self.frame, *args, **kwargs) actual = panel.ix[:, 1, 5] expected = func(self.frame[1], self.frame[5], *args, **kwargs) tm.assert_series_equal(actual, expected) def test_flex_binary_moment(self): # GH3155 # don't blow the stack self.assertRaises(TypeError, mom._flex_binary_moment,5,6,None) def test_corr_sanity(self): #GH 3155 df = DataFrame( np.array( [[ 0.87024726, 0.18505595], [ 0.64355431, 0.3091617 ], [ 0.92372966, 0.50552513], [ 0.00203756, 0.04520709], [ 0.84780328, 0.33394331], [ 0.78369152, 0.63919667]]) ) res = mom.rolling_corr(df[0],df[1],5,center=True) self.assertTrue(all([np.abs(np.nan_to_num(x)) <=1 for x in res])) # and some fuzzing for i in range(10): df = DataFrame(np.random.rand(30,2)) res = mom.rolling_corr(df[0],df[1],5,center=True) try: self.assertTrue(all([np.abs(np.nan_to_num(x)) <=1 for x in res])) except: print(res) def test_flex_binary_frame(self): def _check(method): series = self.frame[1] res = method(series, self.frame, 10) res2 = method(self.frame, series, 10) exp = self.frame.apply(lambda x: method(series, x, 10)) tm.assert_frame_equal(res, exp) tm.assert_frame_equal(res2, exp) frame2 = self.frame.copy() frame2.values[:] = np.random.randn(*frame2.shape) res3 = method(self.frame, frame2, 10) exp = DataFrame(dict((k, method(self.frame[k], frame2[k], 10)) for k in self.frame)) tm.assert_frame_equal(res3, exp) methods = [mom.rolling_corr, mom.rolling_cov] for meth in methods: _check(meth) def test_ewmcov(self): self._check_binary_ew(mom.ewmcov) def test_ewmcov_pairwise(self): self._check_pairwise_moment(mom.ewmcov, span=10, min_periods=5) def test_ewmcorr(self): self._check_binary_ew(mom.ewmcorr) def test_ewmcorr_pairwise(self): self._check_pairwise_moment(mom.ewmcorr, span=10, min_periods=5) def _check_binary_ew(self, func): A = Series(randn(50), index=np.arange(50)) B = A[2:] + randn(48) A[:10] = np.NaN B[-10:] = np.NaN result = func(A, B, 20, min_periods=5) self.assertTrue(np.isnan(result.values[:14]).all()) self.assertFalse(np.isnan(result.values[14:]).any()) # GH 7898 for min_periods in (0, 1, 2): result = func(A, B, 20, min_periods=min_periods) # binary functions (ewmcov, ewmcorr) with bias=False require at least two values self.assertTrue(np.isnan(result.values[:11]).all()) self.assertFalse(np.isnan(result.values[11:]).any()) # check series of length 0 result = func(Series([]), Series([]), 50, min_periods=min_periods) assert_series_equal(result, Series([])) # check series of length 1 result = func(Series([1.]), Series([1.]), 50, min_periods=min_periods) assert_series_equal(result, Series([np.NaN])) self.assertRaises(Exception, func, A, randn(50), 20, min_periods=5) def test_expanding_apply(self): ser = Series([]) assert_series_equal(ser, mom.expanding_apply(ser, lambda x: x.mean())) def expanding_mean(x, min_periods=1, freq=None): return mom.expanding_apply(x, lambda x: x.mean(), min_periods=min_periods, freq=freq) self._check_expanding(expanding_mean, np.mean) # GH 8080 s = Series([None, None, None]) result = mom.expanding_apply(s, lambda x: len(x), min_periods=0) expected = Series([1., 2., 3.]) assert_series_equal(result, expected) def test_expanding_apply_args_kwargs(self): def mean_w_arg(x, const): return np.mean(x) + const df = DataFrame(np.random.rand(20, 3)) expected = mom.expanding_apply(df, np.mean) + 20. assert_frame_equal(mom.expanding_apply(df, mean_w_arg, args=(20,)), expected) assert_frame_equal(mom.expanding_apply(df, mean_w_arg, kwargs={'const' : 20}), expected) def test_expanding_corr(self): A = self.series.dropna() B = (A + randn(len(A)))[:-5] result = mom.expanding_corr(A, B) rolling_result = mom.rolling_corr(A, B, len(A), min_periods=1) assert_almost_equal(rolling_result, result) def test_expanding_count(self): result = mom.expanding_count(self.series) assert_almost_equal(result, mom.rolling_count(self.series, len(self.series))) def test_expanding_quantile(self): result = mom.expanding_quantile(self.series, 0.5) rolling_result = mom.rolling_quantile(self.series, len(self.series), 0.5, min_periods=1) assert_almost_equal(result, rolling_result) def test_expanding_cov(self): A = self.series B = (A + randn(len(A)))[:-5] result = mom.expanding_cov(A, B) rolling_result = mom.rolling_cov(A, B, len(A), min_periods=1) assert_almost_equal(rolling_result, result) def test_expanding_max(self): self._check_expanding(mom.expanding_max, np.max, preserve_nan=False) def test_expanding_cov_pairwise(self): result = mom.expanding_cov(self.frame) rolling_result = mom.rolling_cov(self.frame, len(self.frame), min_periods=1) for i in result.items: assert_almost_equal(result[i], rolling_result[i]) def test_expanding_corr_pairwise(self): result = mom.expanding_corr(self.frame) rolling_result = mom.rolling_corr(self.frame, len(self.frame), min_periods=1) for i in result.items: assert_almost_equal(result[i], rolling_result[i]) def test_expanding_cov_diff_index(self): # GH 7512 s1 = Series([1, 2, 3], index=[0, 1, 2]) s2 = Series([1, 3], index=[0, 2]) result = mom.expanding_cov(s1, s2) expected = Series([None, None, 2.0]) assert_series_equal(result, expected) s2a = Series([1, None, 3], index=[0, 1, 2]) result = mom.expanding_cov(s1, s2a) assert_series_equal(result, expected) s1 = Series([7, 8, 10], index=[0, 1, 3]) s2 = Series([7, 9, 10], index=[0, 2, 3]) result = mom.expanding_cov(s1, s2) expected = Series([None, None, None, 4.5]) assert_series_equal(result, expected) def test_expanding_corr_diff_index(self): # GH 7512 s1 = Series([1, 2, 3], index=[0, 1, 2]) s2 = Series([1, 3], index=[0, 2]) result = mom.expanding_corr(s1, s2) expected = Series([None, None, 1.0]) assert_series_equal(result, expected) s2a = Series([1, None, 3], index=[0, 1, 2]) result = mom.expanding_corr(s1, s2a) assert_series_equal(result, expected) s1 = Series([7, 8, 10], index=[0, 1, 3]) s2 = Series([7, 9, 10], index=[0, 2, 3]) result = mom.expanding_corr(s1, s2) expected = Series([None, None, None, 1.]) assert_series_equal(result, expected) def test_rolling_cov_diff_length(self): # GH 7512 s1 = Series([1, 2, 3], index=[0, 1, 2]) s2 = Series([1, 3], index=[0, 2]) result = mom.rolling_cov(s1, s2, window=3, min_periods=2) expected = Series([None, None, 2.0]) assert_series_equal(result, expected) s2a = Series([1, None, 3], index=[0, 1, 2]) result = mom.rolling_cov(s1, s2a, window=3, min_periods=2) assert_series_equal(result, expected) def test_rolling_corr_diff_length(self): # GH 7512 s1 = Series([1, 2, 3], index=[0, 1, 2]) s2 = Series([1, 3], index=[0, 2]) result = mom.rolling_corr(s1, s2, window=3, min_periods=2) expected = Series([None, None, 1.0]) assert_series_equal(result, expected) s2a = Series([1, None, 3], index=[0, 1, 2]) result = mom.rolling_corr(s1, s2a, window=3, min_periods=2) assert_series_equal(result, expected) def test_rolling_functions_window_non_shrinkage(self): # GH 7764 s = Series(range(4)) s_expected = Series(np.nan, index=s.index) df = DataFrame([[1,5], [3, 2], [3,9], [-1,0]], columns=['A','B']) df_expected = DataFrame(np.nan, index=df.index, columns=df.columns) df_expected_panel = Panel(items=df.index, major_axis=df.columns, minor_axis=df.columns) functions = [lambda x: mom.rolling_cov(x, x, pairwise=False, window=10, min_periods=5), lambda x: mom.rolling_corr(x, x, pairwise=False, window=10, min_periods=5), lambda x: mom.rolling_max(x, window=10, min_periods=5), lambda x: mom.rolling_min(x, window=10, min_periods=5), lambda x: mom.rolling_sum(x, window=10, min_periods=5), lambda x: mom.rolling_mean(x, window=10, min_periods=5), lambda x: mom.rolling_std(x, window=10, min_periods=5), lambda x: mom.rolling_var(x, window=10, min_periods=5), lambda x: mom.rolling_skew(x, window=10, min_periods=5), lambda x: mom.rolling_kurt(x, window=10, min_periods=5), lambda x: mom.rolling_quantile(x, quantile=0.5, window=10, min_periods=5), lambda x: mom.rolling_median(x, window=10, min_periods=5), lambda x: mom.rolling_apply(x, func=sum, window=10, min_periods=5), lambda x: mom.rolling_window(x, win_type='boxcar', window=10, min_periods=5), ] for f in functions: try: s_result = f(s) assert_series_equal(s_result, s_expected) df_result = f(df) assert_frame_equal(df_result, df_expected) except (ImportError): # scipy needed for rolling_window continue functions = [lambda x: mom.rolling_cov(x, x, pairwise=True, window=10, min_periods=5), lambda x: mom.rolling_corr(x, x, pairwise=True, window=10, min_periods=5), # rolling_corr_pairwise is depracated, so the following line should be deleted # when rolling_corr_pairwise is removed. lambda x: mom.rolling_corr_pairwise(x, x, window=10, min_periods=5), ] for f in functions: df_result_panel = f(df) assert_panel_equal(df_result_panel, df_expected_panel) def test_moment_functions_zero_length(self): # GH 8056 s = Series() s_expected = s df1 = DataFrame() df1_expected = df1 df1_expected_panel = Panel(items=df1.index, major_axis=df1.columns, minor_axis=df1.columns) df2 = DataFrame(columns=['a']) df2_expected = df2 df2_expected_panel = Panel(items=df2.index, major_axis=df2.columns, minor_axis=df2.columns) functions = [lambda x: mom.expanding_count(x), lambda x: mom.expanding_cov(x, x, pairwise=False, min_periods=5), lambda x: mom.expanding_corr(x, x, pairwise=False, min_periods=5), lambda x: mom.expanding_max(x, min_periods=5), lambda x: mom.expanding_min(x, min_periods=5), lambda x: mom.expanding_sum(x, min_periods=5), lambda x: mom.expanding_mean(x, min_periods=5), lambda x: mom.expanding_std(x, min_periods=5), lambda x: mom.expanding_var(x, min_periods=5), lambda x: mom.expanding_skew(x, min_periods=5), lambda x: mom.expanding_kurt(x, min_periods=5), lambda x: mom.expanding_quantile(x, quantile=0.5, min_periods=5), lambda x: mom.expanding_median(x, min_periods=5), lambda x: mom.expanding_apply(x, func=sum, min_periods=5), lambda x: mom.rolling_count(x, window=10), lambda x: mom.rolling_cov(x, x, pairwise=False, window=10, min_periods=5), lambda x: mom.rolling_corr(x, x, pairwise=False, window=10, min_periods=5), lambda x: mom.rolling_max(x, window=10, min_periods=5), lambda x: mom.rolling_min(x, window=10, min_periods=5), lambda x: mom.rolling_sum(x, window=10, min_periods=5), lambda x: mom.rolling_mean(x, window=10, min_periods=5), lambda x: mom.rolling_std(x, window=10, min_periods=5), lambda x: mom.rolling_var(x, window=10, min_periods=5), lambda x: mom.rolling_skew(x, window=10, min_periods=5), lambda x: mom.rolling_kurt(x, window=10, min_periods=5), lambda x: mom.rolling_quantile(x, quantile=0.5, window=10, min_periods=5), lambda x: mom.rolling_median(x, window=10, min_periods=5), lambda x: mom.rolling_apply(x, func=sum, window=10, min_periods=5), lambda x: mom.rolling_window(x, win_type='boxcar', window=10, min_periods=5), ] for f in functions: try: s_result = f(s) assert_series_equal(s_result, s_expected) df1_result = f(df1) assert_frame_equal(df1_result, df1_expected) df2_result = f(df2) assert_frame_equal(df2_result, df2_expected) except (ImportError): # scipy needed for rolling_window continue functions = [lambda x: mom.expanding_cov(x, x, pairwise=True, min_periods=5), lambda x: mom.expanding_corr(x, x, pairwise=True, min_periods=5), lambda x: mom.rolling_cov(x, x, pairwise=True, window=10, min_periods=5), lambda x: mom.rolling_corr(x, x, pairwise=True, window=10, min_periods=5), # rolling_corr_pairwise is depracated, so the following line should be deleted # when rolling_corr_pairwise is removed. lambda x: mom.rolling_corr_pairwise(x, x, window=10, min_periods=5), ] for f in functions: df1_result_panel = f(df1) assert_panel_equal(df1_result_panel, df1_expected_panel) df2_result_panel = f(df2) assert_panel_equal(df2_result_panel, df2_expected_panel) def test_expanding_cov_pairwise_diff_length(self): # GH 7512 df1 = DataFrame([[1,5], [3, 2], [3,9]], columns=['A','B']) df1a = DataFrame([[1,5], [3,9]], index=[0,2], columns=['A','B']) df2 = DataFrame([[5,6], [None,None], [2,1]], columns=['X','Y']) df2a = DataFrame([[5,6], [2,1]], index=[0,2], columns=['X','Y']) result1 = mom.expanding_cov(df1, df2, pairwise=True)[2] result2 = mom.expanding_cov(df1, df2a, pairwise=True)[2] result3 = mom.expanding_cov(df1a, df2, pairwise=True)[2] result4 = mom.expanding_cov(df1a, df2a, pairwise=True)[2] expected = DataFrame([[-3., -5.], [-6., -10.]], index=['A','B'], columns=['X','Y']) assert_frame_equal(result1, expected) assert_frame_equal(result2, expected) assert_frame_equal(result3, expected) assert_frame_equal(result4, expected) def test_expanding_corr_pairwise_diff_length(self): # GH 7512 df1 = DataFrame([[1,2], [3, 2], [3,4]], columns=['A','B']) df1a = DataFrame([[1,2], [3,4]], index=[0,2], columns=['A','B']) df2 = DataFrame([[5,6], [None,None], [2,1]], columns=['X','Y']) df2a = DataFrame([[5,6], [2,1]], index=[0,2], columns=['X','Y']) result1 = mom.expanding_corr(df1, df2, pairwise=True)[2] result2 = mom.expanding_corr(df1, df2a, pairwise=True)[2] result3 = mom.expanding_corr(df1a, df2, pairwise=True)[2] result4 = mom.expanding_corr(df1a, df2a, pairwise=True)[2] expected = DataFrame([[-1.0, -1.0], [-1.0, -1.0]], index=['A','B'], columns=['X','Y']) assert_frame_equal(result1, expected) assert_frame_equal(result2, expected) assert_frame_equal(result3, expected) assert_frame_equal(result4, expected) def test_pairwise_stats_column_names_order(self): # GH 7738 df1s = [DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=[0,1]), DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=[1,0]), DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=[1,1]), DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=['C','C']), DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=[1.,0]), DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=[0.,1]), DataFrame([[2,4],[1,2],[5,2],[8,1]], columns=['C',1]), DataFrame([[2.,4.],[1.,2.],[5.,2.],[8.,1.]], columns=[1,0.]), DataFrame([[2,4.],[1,2.],[5,2.],[8,1.]], columns=[0,1.]), DataFrame([[2,4],[1,2],[5,2],[8,1.]], columns=[1.,'X']), ] df2 = DataFrame([[None,1,1],[None,1,2],[None,3,2],[None,8,1]], columns=['Y','Z','X']) s = Series([1,1,3,8]) # suppress warnings about incomparable objects, as we are deliberately testing with such column labels with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=".*incomparable objects.*", category=RuntimeWarning) # DataFrame methods (which do not call _flex_binary_moment()) for f in [lambda x: x.cov(), lambda x: x.corr(), ]: results = [f(df) for df in df1s] for (df, result) in zip(df1s, results): assert_index_equal(result.index, df.columns) assert_index_equal(result.columns, df.columns) for i, result in enumerate(results): if i > 0: self.assert_numpy_array_equivalent(result, results[0]) # DataFrame with itself, pairwise=True for f in [lambda x: mom.expanding_cov(x, pairwise=True), lambda x: mom.expanding_corr(x, pairwise=True), lambda x: mom.rolling_cov(x, window=3, pairwise=True), lambda x: mom.rolling_corr(x, window=3, pairwise=True), lambda x: mom.ewmcov(x, com=3, pairwise=True), lambda x: mom.ewmcorr(x, com=3, pairwise=True), ]: results = [f(df) for df in df1s] for (df, result) in zip(df1s, results): assert_index_equal(result.items, df.index) assert_index_equal(result.major_axis, df.columns) assert_index_equal(result.minor_axis, df.columns) for i, result in enumerate(results): if i > 0: self.assert_numpy_array_equivalent(result, results[0]) # DataFrame with itself, pairwise=False for f in [lambda x: mom.expanding_cov(x, pairwise=False), lambda x: mom.expanding_corr(x, pairwise=False), lambda x: mom.rolling_cov(x, window=3, pairwise=False), lambda x: mom.rolling_corr(x, window=3, pairwise=False), lambda x: mom.ewmcov(x, com=3, pairwise=False), lambda x: mom.ewmcorr(x, com=3, pairwise=False), ]: results = [f(df) for df in df1s] for (df, result) in zip(df1s, results): assert_index_equal(result.index, df.index) assert_index_equal(result.columns, df.columns) for i, result in enumerate(results): if i > 0: self.assert_numpy_array_equivalent(result, results[0]) # DataFrame with another DataFrame, pairwise=True for f in [lambda x, y: mom.expanding_cov(x, y, pairwise=True), lambda x, y: mom.expanding_corr(x, y, pairwise=True), lambda x, y: mom.rolling_cov(x, y, window=3, pairwise=True), lambda x, y: mom.rolling_corr(x, y, window=3, pairwise=True), lambda x, y: mom.ewmcov(x, y, com=3, pairwise=True), lambda x, y: mom.ewmcorr(x, y, com=3, pairwise=True), ]: results = [f(df, df2) for df in df1s] for (df, result) in zip(df1s, results): assert_index_equal(result.items, df.index) assert_index_equal(result.major_axis, df.columns) assert_index_equal(result.minor_axis, df2.columns) for i, result in enumerate(results): if i > 0: self.assert_numpy_array_equivalent(result, results[0]) # DataFrame with another DataFrame, pairwise=False for f in [lambda x, y: mom.expanding_cov(x, y, pairwise=False), lambda x, y: mom.expanding_corr(x, y, pairwise=False), lambda x, y: mom.rolling_cov(x, y, window=3, pairwise=False), lambda x, y: mom.rolling_corr(x, y, window=3, pairwise=False), lambda x, y: mom.ewmcov(x, y, com=3, pairwise=False), lambda x, y: mom.ewmcorr(x, y, com=3, pairwise=False), ]: results = [f(df, df2) if df.columns.is_unique else None for df in df1s] for (df, result) in zip(df1s, results): if result is not None: expected_index = df.index.union(df2.index) expected_columns = df.columns.union(df2.columns) assert_index_equal(result.index, expected_index) assert_index_equal(result.columns, expected_columns) else: tm.assertRaisesRegexp(ValueError, "'arg1' columns are not unique", f, df, df2) tm.assertRaisesRegexp(ValueError, "'arg2' columns are not unique", f, df2, df) # DataFrame with a Series for f in [lambda x, y: mom.expanding_cov(x, y), lambda x, y: mom.expanding_corr(x, y), lambda x, y: mom.rolling_cov(x, y, window=3), lambda x, y: mom.rolling_corr(x, y, window=3), lambda x, y: mom.ewmcov(x, y, com=3), lambda x, y: mom.ewmcorr(x, y, com=3), ]: results = [f(df, s) for df in df1s] + [f(s, df) for df in df1s] for (df, result) in zip(df1s, results): assert_index_equal(result.index, df.index) assert_index_equal(result.columns, df.columns) for i, result in enumerate(results): if i > 0: self.assert_numpy_array_equivalent(result, results[0]) def test_rolling_skew_edge_cases(self): all_nan = Series([np.NaN] * 5) # yields all NaN (0 variance) d = Series([1] * 5) x = mom.rolling_skew(d, window=5) assert_series_equal(all_nan, x) # yields all NaN (window too small) d = Series(np.random.randn(5)) x = mom.rolling_skew(d, window=2) assert_series_equal(all_nan, x) # yields [NaN, NaN, NaN, 0.177994, 1.548824] d = Series([-1.50837035, -0.1297039 , 0.19501095, 1.73508164, 0.41941401]) expected = Series([np.NaN, np.NaN, np.NaN, 0.177994, 1.548824]) x = mom.rolling_skew(d, window=4) assert_series_equal(expected, x) def test_rolling_kurt_edge_cases(self): all_nan = Series([np.NaN] * 5) # yields all NaN (0 variance) d = Series([1] * 5) x = mom.rolling_kurt(d, window=5) assert_series_equal(all_nan, x) # yields all NaN (window too small) d = Series(np.random.randn(5)) x = mom.rolling_kurt(d, window=3) assert_series_equal(all_nan, x) # yields [NaN, NaN, NaN, 1.224307, 2.671499] d = Series([-1.50837035, -0.1297039 , 0.19501095, 1.73508164, 0.41941401]) expected = Series([np.NaN, np.NaN, np.NaN, 1.224307, 2.671499]) x = mom.rolling_kurt(d, window=4) assert_series_equal(expected, x) def _check_expanding_ndarray(self, func, static_comp, has_min_periods=True, has_time_rule=True, preserve_nan=True): result = func(self.arr) assert_almost_equal(result[10], static_comp(self.arr[:11])) if preserve_nan: assert(np.isnan(result[self._nan_locs]).all()) arr = randn(50) if has_min_periods: result = func(arr, min_periods=30) assert(np.isnan(result[:29]).all()) assert_almost_equal(result[-1], static_comp(arr[:50])) # min_periods is working correctly result = func(arr, min_periods=15) self.assertTrue(np.isnan(result[13])) self.assertFalse(np.isnan(result[14])) arr2 = randn(20) result = func(arr2, min_periods=5) self.assertTrue(isnull(result[3])) self.assertTrue(notnull(result[4])) # min_periods=0 result0 = func(arr, min_periods=0) result1 = func(arr, min_periods=1) assert_almost_equal(result0, result1) else: result = func(arr) assert_almost_equal(result[-1], static_comp(arr[:50])) def _check_expanding_structures(self, func): series_result = func(self.series) tm.assert_isinstance(series_result, Series) frame_result = func(self.frame) self.assertEqual(type(frame_result), DataFrame) def _check_expanding(self, func, static_comp, has_min_periods=True, has_time_rule=True, preserve_nan=True): self._check_expanding_ndarray(func, static_comp, has_min_periods=has_min_periods, has_time_rule=has_time_rule, preserve_nan=preserve_nan) self._check_expanding_structures(func) def test_rolling_max_gh6297(self): """Replicate result expected in GH #6297""" indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 2 datapoints on one of the days indices.append(datetime(1975, 1, 3, 6, 0)) series = Series(range(1, 7), index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() expected = Series([1.0, 2.0, 6.0, 4.0, 5.0], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = mom.rolling_max(series, window=1, freq='D') assert_series_equal(expected, x) def test_rolling_max_how_resample(self): indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 3 datapoints on last day (4, 10, and 20) indices.append(datetime(1975, 1, 5, 1)) indices.append(datetime(1975, 1, 5, 2)) series = Series(list(range(0, 5)) + [10, 20], index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() # Default how should be max expected = Series([0.0, 1.0, 2.0, 3.0, 20.0], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = mom.rolling_max(series, window=1, freq='D') assert_series_equal(expected, x) # Now specify median (10.0) expected = Series([0.0, 1.0, 2.0, 3.0, 10.0], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = mom.rolling_max(series, window=1, freq='D', how='median') assert_series_equal(expected, x) # Now specify mean (4+10+20)/3 v = (4.0+10.0+20.0)/3.0 expected = Series([0.0, 1.0, 2.0, 3.0, v], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = mom.rolling_max(series, window=1, freq='D', how='mean') assert_series_equal(expected, x) def test_rolling_min_how_resample(self): indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 3 datapoints on last day (4, 10, and 20) indices.append(datetime(1975, 1, 5, 1)) indices.append(datetime(1975, 1, 5, 2)) series = Series(list(range(0, 5)) + [10, 20], index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() # Default how should be min expected = Series([0.0, 1.0, 2.0, 3.0, 4.0], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = mom.rolling_min(series, window=1, freq='D') assert_series_equal(expected, x) def test_rolling_median_how_resample(self): indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 3 datapoints on last day (4, 10, and 20) indices.append(datetime(1975, 1, 5, 1)) indices.append(datetime(1975, 1, 5, 2)) series = Series(list(range(0, 5)) + [10, 20], index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() # Default how should be median expected = Series([0.0, 1.0, 2.0, 3.0, 10], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = mom.rolling_median(series, window=1, freq='D') assert_series_equal(expected, x) if __name__ == '__main__': import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)
gpl-2.0
annahs/atmos_research
LEO_2D_histos_from_db.py
1
3992
import sys import os import datetime import pickle import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.colors as colors from pprint import pprint import sqlite3 import calendar from datetime import datetime #id INTEGER PRIMARY KEY AUTOINCREMENT, #sp2b_file TEXT, #file_index INT, #instr TEXT, #instr_locn TEXT, #particle_type TEXT, #particle_dia FLOAT, #unix_ts_utc FLOAT, #actual_scat_amp FLOAT, #actual_peak_pos INT, #FF_scat_amp FLOAT, #FF_peak_pos INT, #FF_gauss_width FLOAT, #zeroX_to_peak FLOAT, #LF_scat_amp FLOAT, #incand_amp FLOAT, #lag_time_fit_to_incand FLOAT, #LF_baseline_pct_diff FLOAT, #rBC_mass_fg FLOAT, #coat_thickness_nm FLOAT, #zero_crossing_posn FLOAT, #UNIQUE (sp2b_file, file_index, instr) #connect to database conn = sqlite3.connect('C:/projects/dbs/SP2_data.db') c = conn.cursor() instrument = 'UBCSP2' instrument_locn = 'WHI' type_particle = 'incand' start_date = datetime.strptime('20120401','%Y%m%d') end_date = datetime.strptime('20120531','%Y%m%d') lookup_file = 'C:/Users/Sarah Hanna/Documents/Data/WHI long term record/coatings/lookup_tables/coating_lookup_table_WHI_2012_UBCSP2.lupckl' rBC_density = 1.8 incand_sat = 3750 LF_max = 45000 #above this is unreasonable lookup = open(lookup_file, 'r') lookup_table = pickle.load(lookup) lookup.close() min_rBC_mass = 1.63#120 2.6-#140 3.86-#160nm 0.25 max_rBC_mass = 2.6#140 3.86-160 5.5-#180nm 10.05 VED_min = 65 VED_max = 220 scat_lim = 100 begin_data = calendar.timegm(start_date.timetuple()) end_data = calendar.timegm(end_date.timetuple()) data = [] particles=0 no_scat=0 no_scat_110 =0 fit_failure=0 early_evap=0 early_evap_110=0 flat_fit=0 LF_high=0 for row in c.execute('''SELECT rBC_mass_fg, coat_thickness_nm, unix_ts_utc, LF_scat_amp, LF_baseline_pct_diff, sp2b_file, file_index, instr,actual_scat_amp FROM SP2_coating_analysis WHERE instr=? and instr_locn=? and particle_type=? and rBC_mass_fg>=? and rBC_mass_fg<? and unix_ts_utc>=? and unix_ts_utc<?''', (instrument,instrument_locn,type_particle, min_rBC_mass, max_rBC_mass, begin_data,end_data)): particles+=1 rBC_mass = row[0] coat_thickness = row[1] event_time = datetime.utcfromtimestamp(row[2]) LEO_amp = row[3] LF_baseline_pctdiff = row[4] file = row[5] index = row[6] instrt = row[7] meas_scat_amp = row[8] rBC_VED = (((rBC_mass/(10**15*rBC_density))*6/3.14159)**(1/3.0))*10**7 #VED in nm with 10^15fg/g and 10^7nm/cm if meas_scat_amp < 6: no_scat +=1 if rBC_VED > scat_lim: no_scat_110+=1 data.append([rBC_VED,coat_thickness]) if LEO_amp == 0.0 and LF_baseline_pctdiff == None and meas_scat_amp >= 6: early_evap +=1 if rBC_VED > scat_lim: early_evap_110 +=1 if LEO_amp == -2: early_evap +=1 if rBC_VED > scat_lim: early_evap_110 +=1 if LEO_amp == -1: fit_failure +=1 if LEO_amp == 0.0 and LF_baseline_pctdiff != None: flat_fit +=1 if LEO_amp > LF_max: LF_high +=1 if LEO_amp > 0: data.append([rBC_VED,coat_thickness]) print '# of particles', particles print 'no_scat', no_scat print 'no_scat_110', no_scat_110 print 'fit_failure', fit_failure print 'early_evap', early_evap print 'early_evap_110', early_evap_110 print 'flat_fit', flat_fit print 'LF_high', LF_high evap_pct = (early_evap)*100.0/particles evap_pct_110 = (early_evap_110)*100.0/particles no_scat_pct = (no_scat)*100.0/particles no_scat_pct_110 = no_scat_110*100./particles print evap_pct, evap_pct_110, no_scat_pct,no_scat_pct_110 rBC_VEDs = [row[0] for row in data] coatings = [row[1] for row in data] median_coat = np.median (coatings) print 'median coating',median_coat #####hexbin coat vs core### fig = plt.figure() ax = fig.add_subplot(111) #x_limits = [0,250] #y_limits = [0,250] #h = plt.hexbin(rBC_VEDs, coatings, cmap=cm.jet,gridsize = 50, mincnt=1) hist = plt.hist(coatings, bins=50) plt.xlabel('frequency') plt.xlabel('Coating Thickness (nm)') #cb = plt.colorbar() #cb.set_label('frequency') plt.show()
mit
harshaneelhg/scikit-learn
sklearn/naive_bayes.py
128
28358
# -*- coding: utf-8 -*- """ The :mod:`sklearn.naive_bayes` module implements Naive Bayes algorithms. These are supervised learning methods based on applying Bayes' theorem with strong (naive) feature independence assumptions. """ # Author: Vincent Michel <vincent.michel@inria.fr> # Minor fixes by Fabian Pedregosa # Amit Aides <amitibo@tx.technion.ac.il> # Yehuda Finkelstein <yehudaf@tx.technion.ac.il> # Lars Buitinck <L.J.Buitinck@uva.nl> # Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # (parts based on earlier work by Mathieu Blondel) # # License: BSD 3 clause from abc import ABCMeta, abstractmethod import numpy as np from scipy.sparse import issparse from .base import BaseEstimator, ClassifierMixin from .preprocessing import binarize from .preprocessing import LabelBinarizer from .preprocessing import label_binarize from .utils import check_X_y, check_array from .utils.extmath import safe_sparse_dot, logsumexp from .utils.multiclass import _check_partial_fit_first_call from .utils.fixes import in1d from .utils.validation import check_is_fitted from .externals import six __all__ = ['BernoulliNB', 'GaussianNB', 'MultinomialNB'] class BaseNB(six.with_metaclass(ABCMeta, BaseEstimator, ClassifierMixin)): """Abstract base class for naive Bayes estimators""" @abstractmethod def _joint_log_likelihood(self, X): """Compute the unnormalized posterior log probability of X I.e. ``log P(c) + log P(x|c)`` for all rows x of X, as an array-like of shape [n_classes, n_samples]. Input is passed to _joint_log_likelihood as-is by predict, predict_proba and predict_log_proba. """ def predict(self, X): """ Perform classification on an array of test vectors X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array, shape = [n_samples] Predicted target values for X """ jll = self._joint_log_likelihood(X) return self.classes_[np.argmax(jll, axis=1)] def predict_log_proba(self, X): """ Return log-probability estimates for the test vector X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array-like, shape = [n_samples, n_classes] Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute `classes_`. """ jll = self._joint_log_likelihood(X) # normalize by P(x) = P(f_1, ..., f_n) log_prob_x = logsumexp(jll, axis=1) return jll - np.atleast_2d(log_prob_x).T def predict_proba(self, X): """ Return probability estimates for the test vector X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array-like, shape = [n_samples, n_classes] Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute `classes_`. """ return np.exp(self.predict_log_proba(X)) class GaussianNB(BaseNB): """ Gaussian Naive Bayes (GaussianNB) Can perform online updates to model parameters via `partial_fit` method. For details on algorithm used to update feature means and variance online, see Stanford CS tech report STAN-CS-79-773 by Chan, Golub, and LeVeque: http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf Read more in the :ref:`User Guide <gaussian_naive_bayes>`. Attributes ---------- class_prior_ : array, shape (n_classes,) probability of each class. class_count_ : array, shape (n_classes,) number of training samples observed in each class. theta_ : array, shape (n_classes, n_features) mean of each feature per class sigma_ : array, shape (n_classes, n_features) variance of each feature per class Examples -------- >>> import numpy as np >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> Y = np.array([1, 1, 1, 2, 2, 2]) >>> from sklearn.naive_bayes import GaussianNB >>> clf = GaussianNB() >>> clf.fit(X, Y) GaussianNB() >>> print(clf.predict([[-0.8, -1]])) [1] >>> clf_pf = GaussianNB() >>> clf_pf.partial_fit(X, Y, np.unique(Y)) GaussianNB() >>> print(clf_pf.predict([[-0.8, -1]])) [1] """ def fit(self, X, y, sample_weight=None): """Fit Gaussian Naive Bayes according to X, y Parameters ---------- X : array-like, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples,) Target values. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X, y = check_X_y(X, y) return self._partial_fit(X, y, np.unique(y), _refit=True, sample_weight=sample_weight) @staticmethod def _update_mean_variance(n_past, mu, var, X, sample_weight=None): """Compute online update of Gaussian mean and variance. Given starting sample count, mean, and variance, a new set of points X, and optionally sample weights, return the updated mean and variance. (NB - each dimension (column) in X is treated as independent -- you get variance, not covariance). Can take scalar mean and variance, or vector mean and variance to simultaneously update a number of independent Gaussians. See Stanford CS tech report STAN-CS-79-773 by Chan, Golub, and LeVeque: http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf Parameters ---------- n_past : int Number of samples represented in old mean and variance. If sample weights were given, this should contain the sum of sample weights represented in old mean and variance. mu : array-like, shape (number of Gaussians,) Means for Gaussians in original set. var : array-like, shape (number of Gaussians,) Variances for Gaussians in original set. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- total_mu : array-like, shape (number of Gaussians,) Updated mean for each Gaussian over the combined set. total_var : array-like, shape (number of Gaussians,) Updated variance for each Gaussian over the combined set. """ if X.shape[0] == 0: return mu, var # Compute (potentially weighted) mean and variance of new datapoints if sample_weight is not None: n_new = float(sample_weight.sum()) new_mu = np.average(X, axis=0, weights=sample_weight / n_new) new_var = np.average((X - new_mu) ** 2, axis=0, weights=sample_weight / n_new) else: n_new = X.shape[0] new_var = np.var(X, axis=0) new_mu = np.mean(X, axis=0) if n_past == 0: return new_mu, new_var n_total = float(n_past + n_new) # Combine mean of old and new data, taking into consideration # (weighted) number of observations total_mu = (n_new * new_mu + n_past * mu) / n_total # Combine variance of old and new data, taking into consideration # (weighted) number of observations. This is achieved by combining # the sum-of-squared-differences (ssd) old_ssd = n_past * var new_ssd = n_new * new_var total_ssd = (old_ssd + new_ssd + (n_past / float(n_new * n_total)) * (n_new * mu - n_new * new_mu) ** 2) total_var = total_ssd / n_total return total_mu, total_var def partial_fit(self, X, y, classes=None, sample_weight=None): """Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance and numerical stability overhead, hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples,) Target values. classes : array-like, shape (n_classes,) List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ return self._partial_fit(X, y, classes, _refit=False, sample_weight=sample_weight) def _partial_fit(self, X, y, classes=None, _refit=False, sample_weight=None): """Actual implementation of Gaussian NB fitting. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples,) Target values. classes : array-like, shape (n_classes,) List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. _refit: bool If true, act as though this were the first time we called _partial_fit (ie, throw away any past fitting and start over). sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X, y = check_X_y(X, y) epsilon = 1e-9 if _refit: self.classes_ = None if _check_partial_fit_first_call(self, classes): # This is the first call to partial_fit: # initialize various cumulative counters n_features = X.shape[1] n_classes = len(self.classes_) self.theta_ = np.zeros((n_classes, n_features)) self.sigma_ = np.zeros((n_classes, n_features)) self.class_prior_ = np.zeros(n_classes) self.class_count_ = np.zeros(n_classes) else: if X.shape[1] != self.theta_.shape[1]: msg = "Number of features %d does not match previous data %d." raise ValueError(msg % (X.shape[1], self.theta_.shape[1])) # Put epsilon back in each time self.sigma_[:, :] -= epsilon classes = self.classes_ unique_y = np.unique(y) unique_y_in_classes = in1d(unique_y, classes) if not np.all(unique_y_in_classes): raise ValueError("The target label(s) %s in y do not exist in the " "initial classes %s" % (y[~unique_y_in_classes], classes)) for y_i in unique_y: i = classes.searchsorted(y_i) X_i = X[y == y_i, :] if sample_weight is not None: sw_i = sample_weight[y == y_i] N_i = sw_i.sum() else: sw_i = None N_i = X_i.shape[0] new_theta, new_sigma = self._update_mean_variance( self.class_count_[i], self.theta_[i, :], self.sigma_[i, :], X_i, sw_i) self.theta_[i, :] = new_theta self.sigma_[i, :] = new_sigma self.class_count_[i] += N_i self.sigma_[:, :] += epsilon self.class_prior_[:] = self.class_count_ / np.sum(self.class_count_) return self def _joint_log_likelihood(self, X): check_is_fitted(self, "classes_") X = check_array(X) joint_log_likelihood = [] for i in range(np.size(self.classes_)): jointi = np.log(self.class_prior_[i]) n_ij = - 0.5 * np.sum(np.log(2. * np.pi * self.sigma_[i, :])) n_ij -= 0.5 * np.sum(((X - self.theta_[i, :]) ** 2) / (self.sigma_[i, :]), 1) joint_log_likelihood.append(jointi + n_ij) joint_log_likelihood = np.array(joint_log_likelihood).T return joint_log_likelihood class BaseDiscreteNB(BaseNB): """Abstract base class for naive Bayes on discrete/categorical data Any estimator based on this class should provide: __init__ _joint_log_likelihood(X) as per BaseNB """ def _update_class_log_prior(self, class_prior=None): n_classes = len(self.classes_) if class_prior is not None: if len(class_prior) != n_classes: raise ValueError("Number of priors must match number of" " classes.") self.class_log_prior_ = np.log(class_prior) elif self.fit_prior: # empirical prior, with sample_weight taken into account self.class_log_prior_ = (np.log(self.class_count_) - np.log(self.class_count_.sum())) else: self.class_log_prior_ = np.zeros(n_classes) - np.log(n_classes) def partial_fit(self, X, y, classes=None, sample_weight=None): """Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance overhead hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead. Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. classes : array-like, shape = [n_classes] List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. sample_weight : array-like, shape = [n_samples], optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X = check_array(X, accept_sparse='csr', dtype=np.float64) _, n_features = X.shape if _check_partial_fit_first_call(self, classes): # This is the first call to partial_fit: # initialize various cumulative counters n_effective_classes = len(classes) if len(classes) > 1 else 2 self.class_count_ = np.zeros(n_effective_classes, dtype=np.float64) self.feature_count_ = np.zeros((n_effective_classes, n_features), dtype=np.float64) elif n_features != self.coef_.shape[1]: msg = "Number of features %d does not match previous data %d." raise ValueError(msg % (n_features, self.coef_.shape[-1])) Y = label_binarize(y, classes=self.classes_) if Y.shape[1] == 1: Y = np.concatenate((1 - Y, Y), axis=1) n_samples, n_classes = Y.shape if X.shape[0] != Y.shape[0]: msg = "X.shape[0]=%d and y.shape[0]=%d are incompatible." raise ValueError(msg % (X.shape[0], y.shape[0])) # label_binarize() returns arrays with dtype=np.int64. # We convert it to np.float64 to support sample_weight consistently Y = Y.astype(np.float64) if sample_weight is not None: Y *= check_array(sample_weight).T class_prior = self.class_prior # Count raw events from data before updating the class log prior # and feature log probas self._count(X, Y) # XXX: OPTIM: we could introduce a public finalization method to # be called by the user explicitly just once after several consecutive # calls to partial_fit and prior any call to predict[_[log_]proba] # to avoid computing the smooth log probas at each call to partial fit self._update_feature_log_prob() self._update_class_log_prior(class_prior=class_prior) return self def fit(self, X, y, sample_weight=None): """Fit Naive Bayes classifier according to X, y Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. sample_weight : array-like, shape = [n_samples], optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X, y = check_X_y(X, y, 'csr') _, n_features = X.shape labelbin = LabelBinarizer() Y = labelbin.fit_transform(y) self.classes_ = labelbin.classes_ if Y.shape[1] == 1: Y = np.concatenate((1 - Y, Y), axis=1) # LabelBinarizer().fit_transform() returns arrays with dtype=np.int64. # We convert it to np.float64 to support sample_weight consistently; # this means we also don't have to cast X to floating point Y = Y.astype(np.float64) if sample_weight is not None: Y *= check_array(sample_weight).T class_prior = self.class_prior # Count raw events from data before updating the class log prior # and feature log probas n_effective_classes = Y.shape[1] self.class_count_ = np.zeros(n_effective_classes, dtype=np.float64) self.feature_count_ = np.zeros((n_effective_classes, n_features), dtype=np.float64) self._count(X, Y) self._update_feature_log_prob() self._update_class_log_prior(class_prior=class_prior) return self # XXX The following is a stopgap measure; we need to set the dimensions # of class_log_prior_ and feature_log_prob_ correctly. def _get_coef(self): return (self.feature_log_prob_[1:] if len(self.classes_) == 2 else self.feature_log_prob_) def _get_intercept(self): return (self.class_log_prior_[1:] if len(self.classes_) == 2 else self.class_log_prior_) coef_ = property(_get_coef) intercept_ = property(_get_intercept) class MultinomialNB(BaseDiscreteNB): """ Naive Bayes classifier for multinomial models The multinomial Naive Bayes classifier is suitable for classification with discrete features (e.g., word counts for text classification). The multinomial distribution normally requires integer feature counts. However, in practice, fractional counts such as tf-idf may also work. Read more in the :ref:`User Guide <multinomial_naive_bayes>`. Parameters ---------- alpha : float, optional (default=1.0) Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). fit_prior : boolean Whether to learn class prior probabilities or not. If false, a uniform prior will be used. class_prior : array-like, size (n_classes,) Prior probabilities of the classes. If specified the priors are not adjusted according to the data. Attributes ---------- class_log_prior_ : array, shape (n_classes, ) Smoothed empirical log probability for each class. intercept_ : property Mirrors ``class_log_prior_`` for interpreting MultinomialNB as a linear model. feature_log_prob_ : array, shape (n_classes, n_features) Empirical log probability of features given a class, ``P(x_i|y)``. coef_ : property Mirrors ``feature_log_prob_`` for interpreting MultinomialNB as a linear model. class_count_ : array, shape (n_classes,) Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided. feature_count_ : array, shape (n_classes, n_features) Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided. Examples -------- >>> import numpy as np >>> X = np.random.randint(5, size=(6, 100)) >>> y = np.array([1, 2, 3, 4, 5, 6]) >>> from sklearn.naive_bayes import MultinomialNB >>> clf = MultinomialNB() >>> clf.fit(X, y) MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True) >>> print(clf.predict(X[2])) [3] Notes ----- For the rationale behind the names `coef_` and `intercept_`, i.e. naive Bayes as a linear classifier, see J. Rennie et al. (2003), Tackling the poor assumptions of naive Bayes text classifiers, ICML. References ---------- C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265. http://nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html """ def __init__(self, alpha=1.0, fit_prior=True, class_prior=None): self.alpha = alpha self.fit_prior = fit_prior self.class_prior = class_prior def _count(self, X, Y): """Count and smooth feature occurrences.""" if np.any((X.data if issparse(X) else X) < 0): raise ValueError("Input X must be non-negative") self.feature_count_ += safe_sparse_dot(Y.T, X) self.class_count_ += Y.sum(axis=0) def _update_feature_log_prob(self): """Apply smoothing to raw counts and recompute log probabilities""" smoothed_fc = self.feature_count_ + self.alpha smoothed_cc = smoothed_fc.sum(axis=1) self.feature_log_prob_ = (np.log(smoothed_fc) - np.log(smoothed_cc.reshape(-1, 1))) def _joint_log_likelihood(self, X): """Calculate the posterior log probability of the samples X""" check_is_fitted(self, "classes_") X = check_array(X, accept_sparse='csr') return (safe_sparse_dot(X, self.feature_log_prob_.T) + self.class_log_prior_) class BernoulliNB(BaseDiscreteNB): """Naive Bayes classifier for multivariate Bernoulli models. Like MultinomialNB, this classifier is suitable for discrete data. The difference is that while MultinomialNB works with occurrence counts, BernoulliNB is designed for binary/boolean features. Read more in the :ref:`User Guide <bernoulli_naive_bayes>`. Parameters ---------- alpha : float, optional (default=1.0) Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). binarize : float or None, optional Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors. fit_prior : boolean Whether to learn class prior probabilities or not. If false, a uniform prior will be used. class_prior : array-like, size=[n_classes,] Prior probabilities of the classes. If specified the priors are not adjusted according to the data. Attributes ---------- class_log_prior_ : array, shape = [n_classes] Log probability of each class (smoothed). feature_log_prob_ : array, shape = [n_classes, n_features] Empirical log probability of features given a class, P(x_i|y). class_count_ : array, shape = [n_classes] Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided. feature_count_ : array, shape = [n_classes, n_features] Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided. Examples -------- >>> import numpy as np >>> X = np.random.randint(2, size=(6, 100)) >>> Y = np.array([1, 2, 3, 4, 4, 5]) >>> from sklearn.naive_bayes import BernoulliNB >>> clf = BernoulliNB() >>> clf.fit(X, Y) BernoulliNB(alpha=1.0, binarize=0.0, class_prior=None, fit_prior=True) >>> print(clf.predict(X[2])) [3] References ---------- C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265. http://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html A. McCallum and K. Nigam (1998). A comparison of event models for naive Bayes text classification. Proc. AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41-48. V. Metsis, I. Androutsopoulos and G. Paliouras (2006). Spam filtering with naive Bayes -- Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS). """ def __init__(self, alpha=1.0, binarize=.0, fit_prior=True, class_prior=None): self.alpha = alpha self.binarize = binarize self.fit_prior = fit_prior self.class_prior = class_prior def _count(self, X, Y): """Count and smooth feature occurrences.""" if self.binarize is not None: X = binarize(X, threshold=self.binarize) self.feature_count_ += safe_sparse_dot(Y.T, X) self.class_count_ += Y.sum(axis=0) def _update_feature_log_prob(self): """Apply smoothing to raw counts and recompute log probabilities""" smoothed_fc = self.feature_count_ + self.alpha smoothed_cc = self.class_count_ + self.alpha * 2 self.feature_log_prob_ = (np.log(smoothed_fc) - np.log(smoothed_cc.reshape(-1, 1))) def _joint_log_likelihood(self, X): """Calculate the posterior log probability of the samples X""" check_is_fitted(self, "classes_") X = check_array(X, accept_sparse='csr') if self.binarize is not None: X = binarize(X, threshold=self.binarize) n_classes, n_features = self.feature_log_prob_.shape n_samples, n_features_X = X.shape if n_features_X != n_features: raise ValueError("Expected input with %d features, got %d instead" % (n_features, n_features_X)) neg_prob = np.log(1 - np.exp(self.feature_log_prob_)) # Compute neg_prob · (1 - X).T as ∑neg_prob - X · neg_prob jll = safe_sparse_dot(X, (self.feature_log_prob_ - neg_prob).T) jll += self.class_log_prior_ + neg_prob.sum(axis=1) return jll
bsd-3-clause
aavanian/bokeh
bokeh/sampledata/tests/test_world_cities.py
2
1963
#----------------------------------------------------------------------------- # Copyright (c) 2012 - 2017, Anaconda, Inc. All rights reserved. # # Powered by the Bokeh Development Team. # # The full license is in the file LICENSE.txt, distributed with this software. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # Boilerplate #----------------------------------------------------------------------------- from __future__ import absolute_import, division, print_function, unicode_literals import pytest ; pytest #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- # Standard library imports # External imports import pandas as pd # Bokeh imports from bokeh.util.testing import verify_all # Module under test #import bokeh.sampledata.world_cities as bsw #----------------------------------------------------------------------------- # Setup #----------------------------------------------------------------------------- ALL = ( 'data', ) #----------------------------------------------------------------------------- # General API #----------------------------------------------------------------------------- Test___all__ = pytest.mark.sampledata(verify_all("bokeh.sampledata.world_cities", ALL)) @pytest.mark.sampledata def test_data(): import bokeh.sampledata.world_cities as bsw assert isinstance(bsw.data, pd.DataFrame) # don't check detail for external data #----------------------------------------------------------------------------- # Dev API #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # Private API #-----------------------------------------------------------------------------
bsd-3-clause
thp44/delphin_6_automation
data_process/2d_1d/archieve/temperature.py
1
18075
__author__ = "Christian Kongsgaard" __license__ = 'MIT' # -------------------------------------------------------------------------------------------------------------------- # # IMPORTS # Modules import matplotlib.pyplot as plt import numpy as np import os import datetime import matplotlib.dates as mdates import pandas as pd # RiBuild Modules from delphin_6_automation.file_parsing import delphin_parser # -------------------------------------------------------------------------------------------------------------------- # # RIBuild # Application colors = {'top': '#FBBA00', 'mid': '#B81A5D', 'bottom': '#79C6C0', '1d_brick': '#000000', '1d_mortar': '#BDCCD4'} project_dict = {'dresden_zp_high_ratio_uninsulated_4a': {'map': {'5ad9e0352e2cb22f2c4f15b4': 'brick_1d', '5ad9e3bf2e2cb22f2c4f166b': 'mortar_1d', '5adb0a102e2cb22f2c4f17e9': '2d'} }, 'dresden_zd_high_ratio_uninsulated_4a': {'map': {'5ad9e0ba2e2cb22f2c4f15f1': 'brick_1d', '5ad9e3bf2e2cb22f2c4f166b': 'mortar_1d', '5adb2dc02e2cb22f2c4f1873': '2d'} }, 'potsdam_high_ratio_uninsulated_4a': {'map': {'5ad9e3462e2cb22f2c4f162e': 'brick_1d', '5ad9e3bf2e2cb22f2c4f166b': 'mortar_1d', '5adcc9702e2cb22f2c4f18fd': '2d'} }, 'dresden_zp_low_ratio_uninsulated_4a': {'map': {'5ad9e6192e2cb22f2c4f175f': 'brick_1d', '5ad9e5812e2cb22f2c4f1722': 'mortar_1d', '5adda7172e2cb20baca57c6e': '2d'} }, 'dresden_zd_low_ratio_uninsulated_4a': {'map': {'5ad9e44f2e2cb22f2c4f16a8': 'brick_1d', '5ad9e5812e2cb22f2c4f1722': 'mortar_1d', '5adcd4402e2cb22f2c4f1987': '2d'} }, 'potsdam_low_ratio_uninsulated_4a': {'map': {'5ad9e4f22e2cb22f2c4f16e5': 'brick_1d', '5ad9e5812e2cb22f2c4f1722': 'mortar_1d', '5add9b902e2cb20baca57be4': '2d'} }, 'dresden_zp_high_ratio_insulated_4a': {'map': {'5ae824252e2cb22d48db5955': 'brick_1d', '5ae82c222e2cb2156000902b': 'mortar_1d', '5ae355cf2e2cb2201055c1a4': '2d'} }, 'dresden_zd_high_ratio_insulated_4a': {'map': {'5ae824d82e2cb22d48db5998': 'brick_1d', '5ae82c222e2cb2156000902b': 'mortar_1d', '5ae398f12e2cb2201055c263': '2d'} }, 'potsdam_high_ratio_insulated_4a': {'map': {'5ae82bac2e2cb21560008fe8': 'brick_1d', '5ae82c222e2cb2156000902b': 'mortar_1d', '5ae6ca982e2cb2201055c322': '2d'} }, 'dresden_zp_low_ratio_insulated_4a': {'map': {'5ae82e5d2e2cb21560009137': 'brick_1d', '5ae82dc02e2cb215600090f4': 'mortar_1d', '5ae6fdbf2e2cb20d5891272f': '2d'} }, 'dresden_zd_low_ratio_insulated_4a': {'map': {'5ae82cb12e2cb2156000906e': 'brick_1d', '5ae82dc02e2cb215600090f4': 'mortar_1d', '5ae6d9bf2e2cb2201055c3e1': '2d'} }, 'potsdam_low_ratio_insulated_4a': {'map': {'5ae82d3b2e2cb215600090b1': 'brick_1d', '5ae82dc02e2cb215600090f4': 'mortar_1d', '5ae6edaf2e2cb20d58912670': '2d'} }, } result_folder = r'U:\RIBuild\2D_1D\Results' files = ['temperature profile.d6o'] # Functions def get_points(result: dict, geo: dict): points = [] for index_ in result['indices']: x_ = geo['element_geometry'][index_][1] y_ = geo['element_geometry'][index_][2] points.append({'cell': index_, 'x': x_, 'y': y_}) return points def add_data_to_points(points: list, results: dict, result_name: str): for cell_ in results['result'].keys(): cell_index = int(cell_.split('_')[1]) for point in points: if point['cell'] == cell_index: point[result_name] = np.array(results['result'][cell_][8760:]) break def main(project_): projects = list(project_dict[project_]['map'].keys()) parsed_dicts = {'brick_1d': {'temp': {}, 'geo': {}}, 'mortar_1d': {'temp': {}, 'geo': {}}, '2d': {'temp': {}, 'geo': {}}, } for p_ in projects: for mp_key in project_dict[project_]['map'].keys(): if p_ == mp_key: key = project_dict[project_]['map'][mp_key] folder = result_folder + f'/{p_}/results' geo_file = [file for file in os.listdir(folder) if file.endswith('.g6a')][0] parsed_dicts[key]['temp'], _ = delphin_parser.d6o_to_dict(folder, files[0]) parsed_dicts[key]['geo'] = delphin_parser.g6a_to_dict(folder, geo_file) x_date = [datetime.datetime(2020, 1, 1) + datetime.timedelta(hours=i) for i in range(len(parsed_dicts['brick_1d']['temp']['result']['cell_0'][8760:]))] # Brick 1D brick_1d = get_points(parsed_dicts['brick_1d']['temp'], parsed_dicts['brick_1d']['geo']) brick_1d.sort(key=lambda point: point['x']) add_data_to_points(brick_1d, parsed_dicts['brick_1d']['temp'], 'temperature') # Mortar 1D mortar_1d = get_points(parsed_dicts['mortar_1d']['temp'], parsed_dicts['mortar_1d']['geo']) mortar_1d.sort(key=lambda point: point['x']) add_data_to_points(mortar_1d, parsed_dicts['mortar_1d']['temp'], 'temperature') # 2D sim_2d = get_points(parsed_dicts['2d']['temp'], parsed_dicts['2d']['geo']) sim_2d.sort(key=lambda point: (point['x'], point['y'])) add_data_to_points(sim_2d, parsed_dicts['2d']['temp'], 'temperature') # Plots def plot_locations(quantity): # Axes 00 plt.figure() plt.title(f"{quantity}\n1D-Location: {brick_1d[0]['x']:.4f} and 2D-Location: {sim_2d[0]['x']:.4f}") plt.plot(x_date, brick_1d[0][quantity], color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_1d[0][quantity], color=colors['1d_mortar'], label=f"1D Mortar") plt.plot(x_date, sim_2d[0][quantity], color=colors['bottom'], label=f"2D Bottom") plt.plot(x_date, sim_2d[1][quantity], color=colors['mid'], label=f"2D Mid") plt.plot(x_date, sim_2d[2][quantity], color=colors['top'], label=f"2D Top") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel(f'{quantity}') # Axes 01 plt.figure() plt.title(f"{quantity}\n1D-Location: {brick_1d[1]['x']:.4f} and 2D-Location: {sim_2d[3]['x']:.4f}") plt.plot(x_date, brick_1d[1][quantity], color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_1d[1][quantity], color=colors['1d_mortar'], label=f"1D Mortar") plt.plot(x_date, sim_2d[3][quantity], color=colors['bottom'], label=f"2D Bottom") plt.plot(x_date, sim_2d[4][quantity], color=colors['mid'], label=f"2D Mid") plt.plot(x_date, sim_2d[5][quantity], color=colors['top'], label=f"2D Top") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel(f'{quantity}') # Axes 10 plt.figure() plt.title(f"{quantity}\n1D-Location: {brick_1d[2]['x']:.4f} and 2D-Location: {sim_2d[6]['x']:.4f}") plt.plot(x_date, brick_1d[2][quantity], color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_1d[2][quantity], color=colors['1d_mortar'], label=f"1D Mortar") plt.plot(x_date, sim_2d[6][quantity], color=colors['bottom'], label=f"2D Bottom") plt.plot(x_date, sim_2d[7][quantity], color=colors['mid'], label=f"2D Mid") plt.plot(x_date, sim_2d[8][quantity], color=colors['top'], label=f"2D Top") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel(f'{quantity}') # Axes 11 plt.figure() plt.title(f"{quantity}\n1D-Location: {brick_1d[3]['x']:.4f} and 2D-Location: {sim_2d[9]['x']:.4f}") plt.plot(x_date, brick_1d[3][quantity], color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_1d[3][quantity], color=colors['1d_mortar'], label=f"1D Mortar") plt.plot(x_date, sim_2d[9][quantity], color=colors['bottom'], label=f"2D Bottom") plt.plot(x_date, sim_2d[10][quantity], color=colors['mid'], label=f"2D Mid") plt.plot(x_date, sim_2d[11][quantity], color=colors['top'], label=f"2D Top") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel(f'{quantity}') # Axes 20 plt.figure() plt.title(f"{quantity}\n1D-Location: {brick_1d[4]['x']:.4f} and 2D-Location: {sim_2d[12]['x']:.4f}") plt.plot(x_date, brick_1d[4][quantity], color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_1d[4][quantity], color=colors['1d_mortar'], label=f"1D Mortar") plt.plot(x_date, sim_2d[12][quantity], color=colors['bottom'], label=f"2D Bottom") plt.plot(x_date, sim_2d[13][quantity], color=colors['mid'], label=f"2D Mid") plt.plot(x_date, sim_2d[14][quantity], color=colors['top'], label=f"2D Top") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel(f'{quantity}') # Axes 21 plt.figure() plt.title(f"{quantity}\n1D-Location: {brick_1d[5]['x']:.4f} and 2D-Location: {sim_2d[15]['x']:.4f}") plt.plot(x_date, brick_1d[5][quantity], color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_1d[5][quantity], color=colors['1d_mortar'], label=f"1D Mortar") plt.plot(x_date, sim_2d[15][quantity], color=colors['bottom'], label=f"2D Bottom") plt.plot(x_date, sim_2d[16][quantity], color=colors['mid'], label=f"2D Mid") plt.plot(x_date, sim_2d[17][quantity], color=colors['top'], label=f"2D Top") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel(f'{quantity}') #plot_locations(quantity='temperature') #plt.show() def abs_diff(x1, x2): return x2 - x1 def rel_diff(x1, x2): return (abs(x2 - x1))/abs(x2) * 100 def differences(i, plots=False): avg_2d = np.mean([sim_2d[i]['temperature'], sim_2d[i+2]['temperature'], sim_2d[i+2]['temperature']], axis=0) brick_abs = abs_diff(brick_1d[i]['temperature'], avg_2d) mortar_abs = abs_diff(mortar_1d[i]['temperature'], avg_2d) brick_rel = rel_diff(brick_1d[i]['temperature'], avg_2d) mortar_rel = rel_diff(mortar_1d[i]['temperature'], avg_2d) if plots: # Plot plt.figure() plt.title(f"Temperature - Absolute Difference\n" f"1D-Location: {brick_1d[i]['x']:.4f} and 2D-Location: {sim_2d[i*3]['x']:.4f}") plt.plot(x_date, brick_abs, color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_abs, color=colors['1d_mortar'], label=f"1D Mortar") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel('C') plt.figure() plt.title(f"Temperature - Relative Difference\n" f"1D-Location: {brick_1d[i]['x']:.4f} and 2D-Location: {sim_2d[i*3]['x']:.4f}") plt.plot(x_date, brick_rel, color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_rel, color=colors['1d_mortar'], label=f"1D Mortar") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel('%') local_df = pd.DataFrame(columns=[f"{brick_1d[i]['x']:.04f}", f"{brick_1d[i]['x']:.04f}", f"{brick_1d[i]['x']:.04f}", f"{brick_1d[i]['x']:.04f}"], index=pd.DatetimeIndex(start=datetime.datetime(2020, 1, 1), freq='h', periods=len(brick_rel)), data=np.vstack([brick_rel, brick_abs, mortar_rel, mortar_abs]).T) local_df.columns = pd.MultiIndex.from_arrays([local_df.columns, ['brick', 'brick', 'mortar', 'mortar'], ['relative', 'absolute', 'relative', 'absolute']], names=['location', 'material', 'type']) return local_df def differences_weighted(i, plots=False): avg_2d = np.average(a=[sim_2d[i]['temperature'], sim_2d[i+2]['temperature'], sim_2d[i+2]['temperature']], axis=0, weights=[56, 24., 56]) brick_abs = abs_diff(brick_1d[i]['temperature'], avg_2d) mortar_abs = abs_diff(mortar_1d[i]['temperature'], avg_2d) brick_rel = rel_diff(brick_1d[i]['temperature'], avg_2d) mortar_rel = rel_diff(mortar_1d[i]['temperature'], avg_2d) if plots: # Plot plt.figure() plt.title(f"Temperature - Weighted Absolute Difference\n" f"1D-Location: {brick_1d[i]['x']:.4f} and 2D-Location: {sim_2d[i*3]['x']:.4f}") plt.plot(x_date, brick_abs, color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_abs, color=colors['1d_mortar'], label=f"1D Mortar") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel('%') plt.figure() plt.title(f"Temperature - Weighted Relative Difference\n" f"1D-Location: {brick_1d[i]['x']:.4f} and 2D-Location: {sim_2d[i*3]['x']:.4f}") plt.plot(x_date, brick_rel, color=colors['1d_brick'], label=f"1D Brick") plt.plot(x_date, mortar_rel, color=colors['1d_mortar'], label=f"1D Mortar") plt.legend() plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%B')) plt.ylabel('%') local_df = pd.DataFrame(columns=[f"{brick_1d[i]['x']:.04f}", f"{brick_1d[i]['x']:.04f}", f"{brick_1d[i]['x']:.04f}", f"{brick_1d[i]['x']:.04f}"], index=pd.DatetimeIndex(start=datetime.datetime(2020, 1, 1), freq='h', periods=len(brick_rel)), data=np.vstack([brick_rel, brick_abs, mortar_rel, mortar_abs]).T) local_df.columns = pd.MultiIndex.from_arrays([local_df.columns, ['brick', 'brick', 'mortar', 'mortar'], ['relative', 'absolute', 'relative', 'absolute']], names=['location', 'material', 'type']) return local_df dataframes = [] weighted_dataframes = [] for index in range(len(brick_1d)): dataframes.append(differences(index)) weighted_dataframes.append(differences_weighted(index)) #plt.show() result_dataframe = pd.concat(dataframes, axis=1) w_result_dataframe = pd.concat(weighted_dataframes, axis=1) absolute_df = result_dataframe.loc[:, pd.IndexSlice[:, :, 'absolute']] absolute_df.columns = absolute_df.columns.droplevel(level=2) relative_df = result_dataframe.loc[:, pd.IndexSlice[:, :, 'relative']] relative_df.columns = relative_df.columns.droplevel(level=2) w_absolute_df = w_result_dataframe.loc[:, pd.IndexSlice[:, :, 'absolute']] w_absolute_df.columns = w_absolute_df.columns.droplevel(level=2) w_relative_df = w_result_dataframe.loc[:, pd.IndexSlice[:, :, 'relative']] w_relative_df.columns = w_relative_df.columns.droplevel(level=2) plt.figure() ax = absolute_df.boxplot() ax.set_ylim(-20, 20) ax.set_ylabel('Temperature - C') ax.set_title('Absolute Differences') #plt.show() out_folder = r'C:\Users\ocni\PycharmProjects\delphin_6_automation\data_process\2d_1d\processed_data' def excel(): writer = pd.ExcelWriter(out_folder + '/temperature.xlsx') relative_df.describe().to_excel(writer, 'relative') absolute_df.describe().to_excel(writer, 'absolute') writer.save() #excel() def save_relative(): hdf_file = out_folder + '/relative_temperature.h5' w_relative_df.to_hdf(hdf_file, project_, append=True) save_relative() for project_key in project_dict.keys(): print(f'Processing {project_key}') main(project_key)
mit
lenovor/scikit-learn
examples/mixture/plot_gmm_selection.py
248
3223
""" ================================= Gaussian Mixture Model Selection ================================= This example shows that model selection can be performed with Gaussian Mixture Models using information-theoretic criteria (BIC). Model selection concerns both the covariance type and the number of components in the model. In that case, AIC also provides the right result (not shown to save time), but BIC is better suited if the problem is to identify the right model. Unlike Bayesian procedures, such inferences are prior-free. In that case, the model with 2 components and full covariance (which corresponds to the true generative model) is selected. """ print(__doc__) import itertools import numpy as np from scipy import linalg import matplotlib.pyplot as plt import matplotlib as mpl from sklearn import mixture # Number of samples per component n_samples = 500 # Generate random sample, two components np.random.seed(0) C = np.array([[0., -0.1], [1.7, .4]]) X = np.r_[np.dot(np.random.randn(n_samples, 2), C), .7 * np.random.randn(n_samples, 2) + np.array([-6, 3])] lowest_bic = np.infty bic = [] n_components_range = range(1, 7) cv_types = ['spherical', 'tied', 'diag', 'full'] for cv_type in cv_types: for n_components in n_components_range: # Fit a mixture of Gaussians with EM gmm = mixture.GMM(n_components=n_components, covariance_type=cv_type) gmm.fit(X) bic.append(gmm.bic(X)) if bic[-1] < lowest_bic: lowest_bic = bic[-1] best_gmm = gmm bic = np.array(bic) color_iter = itertools.cycle(['k', 'r', 'g', 'b', 'c', 'm', 'y']) clf = best_gmm bars = [] # Plot the BIC scores spl = plt.subplot(2, 1, 1) for i, (cv_type, color) in enumerate(zip(cv_types, color_iter)): xpos = np.array(n_components_range) + .2 * (i - 2) bars.append(plt.bar(xpos, bic[i * len(n_components_range): (i + 1) * len(n_components_range)], width=.2, color=color)) plt.xticks(n_components_range) plt.ylim([bic.min() * 1.01 - .01 * bic.max(), bic.max()]) plt.title('BIC score per model') xpos = np.mod(bic.argmin(), len(n_components_range)) + .65 +\ .2 * np.floor(bic.argmin() / len(n_components_range)) plt.text(xpos, bic.min() * 0.97 + .03 * bic.max(), '*', fontsize=14) spl.set_xlabel('Number of components') spl.legend([b[0] for b in bars], cv_types) # Plot the winner splot = plt.subplot(2, 1, 2) Y_ = clf.predict(X) for i, (mean, covar, color) in enumerate(zip(clf.means_, clf.covars_, color_iter)): v, w = linalg.eigh(covar) if not np.any(Y_ == i): continue plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], .8, color=color) # Plot an ellipse to show the Gaussian component angle = np.arctan2(w[0][1], w[0][0]) angle = 180 * angle / np.pi # convert to degrees v *= 4 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180 + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(.5) splot.add_artist(ell) plt.xlim(-10, 10) plt.ylim(-3, 6) plt.xticks(()) plt.yticks(()) plt.title('Selected GMM: full model, 2 components') plt.subplots_adjust(hspace=.35, bottom=.02) plt.show()
bsd-3-clause
notkarol/banjin
experiment/python_word_matching_speed.py
1
4650
#!/usr/bin/python # Takes in a dictionary of words # Verifies that all functions return the same answers # Generates random hands from the probability of getting tiles from the bunch # Then prints out how long each function takes to find all matching words # Generates various hand sizes to see if there's any scaling import matplotlib.pyplot as plt import numpy as np import pickle import os import sys import timeit # Naive list way of matching wordbank def f0_list(hand, wordbank): results = [] for w_i in range(len(wordbank)): match = True for i in range(26): if hand[i] < wordbank[w_i][i]: match = False break if match: results.append(w_i) return results # A for loop and some numpy def f1_list(hand, wordbank): results = [] for w_i in range(len(wordbank)): if min(list(map(lambda x: x[1] - x[0], zip(wordbank[w_i], hand)))) >= 0: results.append(w_i) return results # Naive way using numpy def f0_np(hand, wordbank): results = [] for w_i in range(len(wordbank)): match = True for i in range(26): if hand[i] < wordbank[w_i,i]: match = False break if match: results.append(w_i) return results # A for loop and some numpy def f1_np(hand, wordbank): results = [] for w_i in range(len(wordbank)): if not np.any((hand - wordbank[w_i]) < 0): results.append(w_i) return results # A for loop and some numpy def f2_np(hand, wordbank): results = [] for w_i in range(len(wordbank)): if np.min(hand - wordbank[w_i]) >= 0: results.append(w_i) return results # Vectorized sum and difference def f3_np(hand, wordbank): return np.where(np.sum((wordbank - hand) > 0, axis=1) == 0)[0] # vectorized just using any def f4_np(hand, wordbank): return np.where(np.any(wordbank > hand, axis=1) == 0)[0] # Prepare a 2D list and a 2D np array of letter frequencies with open(sys.argv[1]) as f: words = [x.split()[0] for x in f.readlines()] wordbank_list = [[0] * 26 for _ in range(len(words))] wordbank_np = np.zeros((len(words), 26)) for w_i in range(len(words)): for letter in sorted(words[w_i]): pos = ord(letter) - 65 wordbank_list[w_i][pos] += 1 wordbank_np[w_i][pos] += 1 # Arrays for keeping track of functions and data-specific wordbanks hand_sizes = list(range(2, 9)) functions = {'list' : [f0_list, f1_list], 'numpy': [f0_np, f1_np, f2_np, f3_np, f4_np]} wordbanks = {'list' : wordbank_list, 'numpy': wordbank_np} n_iter = 10 if len(sys.argv) < 3 else int(sys.argv[2]) timings = {} for datatype in functions: timings[datatype] = np.zeros((max(hand_sizes) + 1, n_iter, len(functions[datatype]))) # Verify that our functions give the same answers for datatype in functions: for func in functions[datatype]: print(datatype, func(wordbanks[datatype][len(wordbank_list) // 2], wordbanks[datatype])) # Time each word imports = 'from __main__ import functions, wordbanks' for counter in range(n_iter): for hand_size in hand_sizes: # Get a specific hand size hand = [13,3,3,6,18,3,4,3,12,2,2,5,3,8,11,3,2,9,6,9,6,3,3,2,3,2] while sum(hand) > hand_size: pos = np.random.randint(sum(hand)) for i in range(len(hand)): pos -= hand[i] if pos < 0: hand[i] -= 1 break hand = str(hand) # For this hand go wild for datatype in functions: for f_i in range(len(functions[datatype])): cmd = 'functions["%s"][%i](%s, wordbanks["%s"])' % (datatype, f_i, hand, datatype) timings[datatype][hand_size, counter, f_i] += timeit.timeit(cmd, imports, number=8) print("\rCompleted %.1f%%" % (100 * (counter + 1) / n_iter), end='') print() # Save words and timings in case we're doing a long-lasting operation filename = 'word_matching_timings_%s.pkl' % os.path.basename(sys.argv[1]) with open(filename, 'wb') as f: print("Saving", filename) pickle.dump((words, wordbanks, timings), f) # Show Results for datatype in functions: means = np.mean(timings[datatype], axis=1) for f_i in range(means.shape[1]): plt.semilogy(hand_sizes, means[:, f_i][min(hand_sizes):], label='%s F%i' % (datatype, f_i)) plt.legend(loc='center left', bbox_to_anchor=(0.85, 0.5)) plt.xlabel("Hand Size") plt.ylabel("Execution Time") plt.title("Word Matching") plt.show()
mit
ishanic/scikit-learn
sklearn/feature_extraction/hashing.py
183
6155
# Author: Lars Buitinck <L.J.Buitinck@uva.nl> # License: BSD 3 clause import numbers import numpy as np import scipy.sparse as sp from . import _hashing from ..base import BaseEstimator, TransformerMixin def _iteritems(d): """Like d.iteritems, but accepts any collections.Mapping.""" return d.iteritems() if hasattr(d, "iteritems") else d.items() class FeatureHasher(BaseEstimator, TransformerMixin): """Implements feature hashing, aka the hashing trick. This class turns sequences of symbolic feature names (strings) into scipy.sparse matrices, using a hash function to compute the matrix column corresponding to a name. The hash function employed is the signed 32-bit version of Murmurhash3. Feature names of type byte string are used as-is. Unicode strings are converted to UTF-8 first, but no Unicode normalization is done. Feature values must be (finite) numbers. This class is a low-memory alternative to DictVectorizer and CountVectorizer, intended for large-scale (online) learning and situations where memory is tight, e.g. when running prediction code on embedded devices. Read more in the :ref:`User Guide <feature_hashing>`. Parameters ---------- n_features : integer, optional The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners. dtype : numpy type, optional The type of feature values. Passed to scipy.sparse matrix constructors as the dtype argument. Do not set this to bool, np.boolean or any unsigned integer type. input_type : string, optional Either "dict" (the default) to accept dictionaries over (feature_name, value); "pair" to accept pairs of (feature_name, value); or "string" to accept single strings. feature_name should be a string, while value should be a number. In the case of "string", a value of 1 is implied. The feature_name is hashed to find the appropriate column for the feature. The value's sign might be flipped in the output (but see non_negative, below). non_negative : boolean, optional, default np.float64 Whether output matrices should contain non-negative values only; effectively calls abs on the matrix prior to returning it. When True, output values can be interpreted as frequencies. When False, output values will have expected value zero. Examples -------- >>> from sklearn.feature_extraction import FeatureHasher >>> h = FeatureHasher(n_features=10) >>> D = [{'dog': 1, 'cat':2, 'elephant':4},{'dog': 2, 'run': 5}] >>> f = h.transform(D) >>> f.toarray() array([[ 0., 0., -4., -1., 0., 0., 0., 0., 0., 2.], [ 0., 0., 0., -2., -5., 0., 0., 0., 0., 0.]]) See also -------- DictVectorizer : vectorizes string-valued features using a hash table. sklearn.preprocessing.OneHotEncoder : handles nominal/categorical features encoded as columns of integers. """ def __init__(self, n_features=(2 ** 20), input_type="dict", dtype=np.float64, non_negative=False): self._validate_params(n_features, input_type) self.dtype = dtype self.input_type = input_type self.n_features = n_features self.non_negative = non_negative @staticmethod def _validate_params(n_features, input_type): # strangely, np.int16 instances are not instances of Integral, # while np.int64 instances are... if not isinstance(n_features, (numbers.Integral, np.integer)): raise TypeError("n_features must be integral, got %r (%s)." % (n_features, type(n_features))) elif n_features < 1 or n_features >= 2 ** 31: raise ValueError("Invalid number of features (%d)." % n_features) if input_type not in ("dict", "pair", "string"): raise ValueError("input_type must be 'dict', 'pair' or 'string'," " got %r." % input_type) def fit(self, X=None, y=None): """No-op. This method doesn't do anything. It exists purely for compatibility with the scikit-learn transformer API. Returns ------- self : FeatureHasher """ # repeat input validation for grid search (which calls set_params) self._validate_params(self.n_features, self.input_type) return self def transform(self, raw_X, y=None): """Transform a sequence of instances to a scipy.sparse matrix. Parameters ---------- raw_X : iterable over iterable over raw features, length = n_samples Samples. Each sample must be iterable an (e.g., a list or tuple) containing/generating feature names (and optionally values, see the input_type constructor argument) which will be hashed. raw_X need not support the len function, so it can be the result of a generator; n_samples is determined on the fly. y : (ignored) Returns ------- X : scipy.sparse matrix, shape = (n_samples, self.n_features) Feature matrix, for use with estimators or further transformers. """ raw_X = iter(raw_X) if self.input_type == "dict": raw_X = (_iteritems(d) for d in raw_X) elif self.input_type == "string": raw_X = (((f, 1) for f in x) for x in raw_X) indices, indptr, values = \ _hashing.transform(raw_X, self.n_features, self.dtype) n_samples = indptr.shape[0] - 1 if n_samples == 0: raise ValueError("Cannot vectorize empty sequence.") X = sp.csr_matrix((values, indices, indptr), dtype=self.dtype, shape=(n_samples, self.n_features)) X.sum_duplicates() # also sorts the indices if self.non_negative: np.abs(X.data, X.data) return X
bsd-3-clause
moonbury/notebooks
github/MatplotlibCookbook/Chapter 8/wx-supershape-1.py
3
1121
import wx, numpy from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg from matplotlib.figure import Figure def supershape_radius(phi, a, b, m, n1, n2, n3): theta = .25 * m * phi cos = numpy.fabs(numpy.cos(theta) / a) ** n2 sin = numpy.fabs(numpy.sin(theta) / b) ** n3 r = (cos + sin) ** (-1. / n1) r /= numpy.max(r) return r class SuperShapeFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER, size = (480, 480)) self.fig = Figure((6, 6), dpi = 80) self.panel = wx.Panel(self, -1) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(FigureCanvasWxAgg(self.panel, -1, self.fig), 1) self.panel.SetSizer(sizer) self.draw_figure() def draw_figure(self): phi = numpy.linspace(0, 2 * numpy.pi, 1024) r = supershape_radius(phi, 1, 1, 3, 2, 18, 18) ax = self.fig.add_subplot(111, polar = True) ax.plot(phi, r, lw = 3.) self.fig.canvas.draw() app = wx.App(redirect = True) top = SuperShapeFrame(None, -1, 'SuperShape') top.Show() app.MainLoop()
gpl-3.0
mlee92/Programming
Econ/supply_demand_elasticity/demand_elasticity.py
2
1413
# Elasticity of demand is a measure of how strongly consumers respond to a change in the price of a good # Formally, % change in demand / % change in price # Problem: Graph the histogram of average-elasticity for a linear-demand good with random coefficients (a, b) import random import matplotlib.pyplot as plt import numpy as np SIM = 1000; UNIT_RANGE = range(1, 50) AVGS = list() COEF = [0, 0] def generate_coefficients(): global COEF a = random.randint(1, 25) b = random.randint(a*50, 25*50) COEF = [a, b] def price(unit): return COEF[1] - COEF[0]*unit def graph_price(): x = np.linspace(1,50,50) y = price(x) plt.plot(x, y) plt.show() def elasticity(d1, d2): cPrice = price(d2) - price(d1) cDemand = d2 - d1 pPrice = cPrice / price(d1) pDemand = cDemand / d1 return abs(pDemand / pPrice) def simulate(): global AVGS, COEF, UNIT_RANGE generate_coefficients() elast_list = list() for i in UNIT_RANGE: for j in UNIT_RANGE: if(i != j): elast_list.append(elasticity(i, j)) mu = np.mean(elast_list) print(COEF, mu) AVGS.append(mu) def init(): for i in range(0, SIM): simulate() init() print(SIM) plt.hist(AVGS) plt.show()
gpl-2.0
cython-testbed/pandas
pandas/core/apply.py
4
12744
import warnings import numpy as np from pandas import compat from pandas._libs import reduction from pandas.core.dtypes.generic import ABCSeries from pandas.core.dtypes.common import ( is_extension_type, is_dict_like, is_list_like, is_sequence) from pandas.util._decorators import cache_readonly from pandas.io.formats.printing import pprint_thing def frame_apply(obj, func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, ignore_failures=False, args=None, kwds=None): """ construct and return a row or column based frame apply object """ axis = obj._get_axis_number(axis) if axis == 0: klass = FrameRowApply elif axis == 1: klass = FrameColumnApply return klass(obj, func, broadcast=broadcast, raw=raw, reduce=reduce, result_type=result_type, ignore_failures=ignore_failures, args=args, kwds=kwds) class FrameApply(object): def __init__(self, obj, func, broadcast, raw, reduce, result_type, ignore_failures, args, kwds): self.obj = obj self.raw = raw self.ignore_failures = ignore_failures self.args = args or () self.kwds = kwds or {} if result_type not in [None, 'reduce', 'broadcast', 'expand']: raise ValueError("invalid value for result_type, must be one " "of {None, 'reduce', 'broadcast', 'expand'}") if broadcast is not None: warnings.warn("The broadcast argument is deprecated and will " "be removed in a future version. You can specify " "result_type='broadcast' to broadcast the result " "to the original dimensions", FutureWarning, stacklevel=4) if broadcast: result_type = 'broadcast' if reduce is not None: warnings.warn("The reduce argument is deprecated and will " "be removed in a future version. You can specify " "result_type='reduce' to try to reduce the result " "to the original dimensions", FutureWarning, stacklevel=4) if reduce: if result_type is not None: raise ValueError( "cannot pass both reduce=True and result_type") result_type = 'reduce' self.result_type = result_type # curry if needed if ((kwds or args) and not isinstance(func, (np.ufunc, compat.string_types))): def f(x): return func(x, *args, **kwds) else: f = func self.f = f # results self.result = None self.res_index = None self.res_columns = None @property def columns(self): return self.obj.columns @property def index(self): return self.obj.index @cache_readonly def values(self): return self.obj.values @cache_readonly def dtypes(self): return self.obj.dtypes @property def agg_axis(self): return self.obj._get_agg_axis(self.axis) def get_result(self): """ compute the results """ # dispatch to agg if is_list_like(self.f) or is_dict_like(self.f): return self.obj.aggregate(self.f, axis=self.axis, *self.args, **self.kwds) # all empty if len(self.columns) == 0 and len(self.index) == 0: return self.apply_empty_result() # string dispatch if isinstance(self.f, compat.string_types): # Support for `frame.transform('method')` # Some methods (shift, etc.) require the axis argument, others # don't, so inspect and insert if necessary. func = getattr(self.obj, self.f) sig = compat.signature(func) if 'axis' in sig.args: self.kwds['axis'] = self.axis return func(*self.args, **self.kwds) # ufunc elif isinstance(self.f, np.ufunc): with np.errstate(all='ignore'): results = self.f(self.values) return self.obj._constructor(data=results, index=self.index, columns=self.columns, copy=False) # broadcasting if self.result_type == 'broadcast': return self.apply_broadcast() # one axis empty elif not all(self.obj.shape): return self.apply_empty_result() # raw elif self.raw and not self.obj._is_mixed_type: return self.apply_raw() return self.apply_standard() def apply_empty_result(self): """ we have an empty result; at least 1 axis is 0 we will try to apply the function to an empty series in order to see if this is a reduction function """ # we are not asked to reduce or infer reduction # so just return a copy of the existing object if self.result_type not in ['reduce', None]: return self.obj.copy() # we may need to infer reduce = self.result_type == 'reduce' from pandas import Series if not reduce: EMPTY_SERIES = Series([]) try: r = self.f(EMPTY_SERIES, *self.args, **self.kwds) reduce = not isinstance(r, Series) except Exception: pass if reduce: return self.obj._constructor_sliced(np.nan, index=self.agg_axis) else: return self.obj.copy() def apply_raw(self): """ apply to the values as a numpy array """ try: result = reduction.reduce(self.values, self.f, axis=self.axis) except Exception: result = np.apply_along_axis(self.f, self.axis, self.values) # TODO: mixed type case if result.ndim == 2: return self.obj._constructor(result, index=self.index, columns=self.columns) else: return self.obj._constructor_sliced(result, index=self.agg_axis) def apply_broadcast(self, target): result_values = np.empty_like(target.values) # axis which we want to compare compliance result_compare = target.shape[0] for i, col in enumerate(target.columns): res = self.f(target[col]) ares = np.asarray(res).ndim # must be a scalar or 1d if ares > 1: raise ValueError("too many dims to broadcast") elif ares == 1: # must match return dim if result_compare != len(res): raise ValueError("cannot broadcast result") result_values[:, i] = res # we *always* preserve the original index / columns result = self.obj._constructor(result_values, index=target.index, columns=target.columns) return result def apply_standard(self): # try to reduce first (by default) # this only matters if the reduction in values is of different dtype # e.g. if we want to apply to a SparseFrame, then can't directly reduce # we cannot reduce using non-numpy dtypes, # as demonstrated in gh-12244 if (self.result_type in ['reduce', None] and not self.dtypes.apply(is_extension_type).any()): # Create a dummy Series from an empty array from pandas import Series values = self.values index = self.obj._get_axis(self.axis) labels = self.agg_axis empty_arr = np.empty(len(index), dtype=values.dtype) dummy = Series(empty_arr, index=index, dtype=values.dtype) try: result = reduction.reduce(values, self.f, axis=self.axis, dummy=dummy, labels=labels) return self.obj._constructor_sliced(result, index=labels) except Exception: pass # compute the result using the series generator self.apply_series_generator() # wrap results return self.wrap_results() def apply_series_generator(self): series_gen = self.series_generator res_index = self.result_index i = None keys = [] results = {} if self.ignore_failures: successes = [] for i, v in enumerate(series_gen): try: results[i] = self.f(v) keys.append(v.name) successes.append(i) except Exception: pass # so will work with MultiIndex if len(successes) < len(res_index): res_index = res_index.take(successes) else: try: for i, v in enumerate(series_gen): results[i] = self.f(v) keys.append(v.name) except Exception as e: if hasattr(e, 'args'): # make sure i is defined if i is not None: k = res_index[i] e.args = e.args + ('occurred at index %s' % pprint_thing(k), ) raise self.results = results self.res_index = res_index self.res_columns = self.result_columns def wrap_results(self): results = self.results # see if we can infer the results if len(results) > 0 and is_sequence(results[0]): return self.wrap_results_for_axis() # dict of scalars result = self.obj._constructor_sliced(results) result.index = self.res_index return result class FrameRowApply(FrameApply): axis = 0 def apply_broadcast(self): return super(FrameRowApply, self).apply_broadcast(self.obj) @property def series_generator(self): return (self.obj._ixs(i, axis=1) for i in range(len(self.columns))) @property def result_index(self): return self.columns @property def result_columns(self): return self.index def wrap_results_for_axis(self): """ return the results for the rows """ results = self.results result = self.obj._constructor(data=results) if not isinstance(results[0], ABCSeries): try: result.index = self.res_columns except ValueError: pass try: result.columns = self.res_index except ValueError: pass return result class FrameColumnApply(FrameApply): axis = 1 def apply_broadcast(self): result = super(FrameColumnApply, self).apply_broadcast(self.obj.T) return result.T @property def series_generator(self): constructor = self.obj._constructor_sliced return (constructor(arr, index=self.columns, name=name) for i, (arr, name) in enumerate(zip(self.values, self.index))) @property def result_index(self): return self.index @property def result_columns(self): return self.columns def wrap_results_for_axis(self): """ return the results for the columns """ results = self.results # we have requested to expand if self.result_type == 'expand': result = self.infer_to_same_shape() # we have a non-series and don't want inference elif not isinstance(results[0], ABCSeries): from pandas import Series result = Series(results) result.index = self.res_index # we may want to infer results else: result = self.infer_to_same_shape() return result def infer_to_same_shape(self): """ infer the results to the same shape as the input object """ results = self.results result = self.obj._constructor(data=results) result = result.T # set the index result.index = self.res_index # infer dtypes result = result.infer_objects() return result
bsd-3-clause
jeffery-do/Vizdoombot
doom/lib/python3.5/site-packages/skimage/viewer/utils/core.py
19
6555
import numpy as np from ..qt import QtWidgets, has_qt, FigureManagerQT, FigureCanvasQTAgg from ..._shared.utils import warn import matplotlib as mpl from matplotlib.figure import Figure from matplotlib import _pylab_helpers from matplotlib.colors import LinearSegmentedColormap if has_qt and 'agg' not in mpl.get_backend().lower(): warn("Recommended matplotlib backend is `Agg` for full " "skimage.viewer functionality.") __all__ = ['init_qtapp', 'start_qtapp', 'RequiredAttr', 'figimage', 'LinearColormap', 'ClearColormap', 'FigureCanvas', 'new_plot', 'update_axes_image'] QApp = None def init_qtapp(): """Initialize QAppliction. The QApplication needs to be initialized before creating any QWidgets """ global QApp QApp = QtWidgets.QApplication.instance() if QApp is None: QApp = QtWidgets.QApplication([]) return QApp def is_event_loop_running(app=None): """Return True if event loop is running.""" if app is None: app = init_qtapp() if hasattr(app, '_in_event_loop'): return app._in_event_loop else: return False def start_qtapp(app=None): """Start Qt mainloop""" if app is None: app = init_qtapp() if not is_event_loop_running(app): app._in_event_loop = True app.exec_() app._in_event_loop = False else: app._in_event_loop = True class RequiredAttr(object): """A class attribute that must be set before use.""" instances = dict() def __init__(self, init_val=None): self.instances[self, None] = init_val def __get__(self, obj, objtype): value = self.instances[self, obj] if value is None: raise AttributeError('Required attribute not set') return value def __set__(self, obj, value): self.instances[self, obj] = value class LinearColormap(LinearSegmentedColormap): """LinearSegmentedColormap in which color varies smoothly. This class is a simplification of LinearSegmentedColormap, which doesn't support jumps in color intensities. Parameters ---------- name : str Name of colormap. segmented_data : dict Dictionary of 'red', 'green', 'blue', and (optionally) 'alpha' values. Each color key contains a list of `x`, `y` tuples. `x` must increase monotonically from 0 to 1 and corresponds to input values for a mappable object (e.g. an image). `y` corresponds to the color intensity. """ def __init__(self, name, segmented_data, **kwargs): segmented_data = dict((key, [(x, y, y) for x, y in value]) for key, value in segmented_data.items()) LinearSegmentedColormap.__init__(self, name, segmented_data, **kwargs) class ClearColormap(LinearColormap): """Color map that varies linearly from alpha = 0 to 1 """ def __init__(self, rgb, max_alpha=1, name='clear_color'): r, g, b = rgb cg_speq = {'blue': [(0.0, b), (1.0, b)], 'green': [(0.0, g), (1.0, g)], 'red': [(0.0, r), (1.0, r)], 'alpha': [(0.0, 0.0), (1.0, max_alpha)]} LinearColormap.__init__(self, name, cg_speq) class FigureCanvas(FigureCanvasQTAgg): """Canvas for displaying images.""" def __init__(self, figure, **kwargs): self.fig = figure FigureCanvasQTAgg.__init__(self, self.fig) FigureCanvasQTAgg.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) FigureCanvasQTAgg.updateGeometry(self) def resizeEvent(self, event): FigureCanvasQTAgg.resizeEvent(self, event) # Call to `resize_event` missing in FigureManagerQT. # See https://github.com/matplotlib/matplotlib/pull/1585 self.resize_event() def new_canvas(*args, **kwargs): """Return a new figure canvas.""" allnums = _pylab_helpers.Gcf.figs.keys() num = max(allnums) + 1 if allnums else 1 FigureClass = kwargs.pop('FigureClass', Figure) figure = FigureClass(*args, **kwargs) canvas = FigureCanvas(figure) fig_manager = FigureManagerQT(canvas, num) return fig_manager.canvas def new_plot(parent=None, subplot_kw=None, **fig_kw): """Return new figure and axes. Parameters ---------- parent : QtWidget Qt widget that displays the plot objects. If None, you must manually call ``canvas.setParent`` and pass the parent widget. subplot_kw : dict Keyword arguments passed ``matplotlib.figure.Figure.add_subplot``. fig_kw : dict Keyword arguments passed ``matplotlib.figure.Figure``. """ if subplot_kw is None: subplot_kw = {} canvas = new_canvas(**fig_kw) canvas.setParent(parent) fig = canvas.figure ax = fig.add_subplot(1, 1, 1, **subplot_kw) return fig, ax def figimage(image, scale=1, dpi=None, **kwargs): """Return figure and axes with figure tightly surrounding image. Unlike pyplot.figimage, this actually plots onto an axes object, which fills the figure. Plotting the image onto an axes allows for subsequent overlays of axes artists. Parameters ---------- image : array image to plot scale : float If scale is 1, the figure and axes have the same dimension as the image. Smaller values of `scale` will shrink the figure. dpi : int Dots per inch for figure. If None, use the default rcParam. """ dpi = dpi if dpi is not None else mpl.rcParams['figure.dpi'] kwargs.setdefault('interpolation', 'nearest') kwargs.setdefault('cmap', 'gray') h, w, d = np.atleast_3d(image).shape figsize = np.array((w, h), dtype=float) / dpi * scale fig, ax = new_plot(figsize=figsize, dpi=dpi) fig.subplots_adjust(left=0, bottom=0, right=1, top=1) ax.set_axis_off() ax.imshow(image, **kwargs) ax.figure.canvas.draw() return fig, ax def update_axes_image(image_axes, image): """Update the image displayed by an image plot. This sets the image plot's array and updates its shape appropriately Parameters ---------- image_axes : `matplotlib.image.AxesImage` Image axes to update. image : array Image array. """ image_axes.set_array(image) # Adjust size if new image shape doesn't match the original h, w = image.shape[:2] image_axes.set_extent((0, w, h, 0))
mit
hlin117/scikit-learn
examples/ensemble/plot_forest_iris.py
18
6190
""" ==================================================================== Plot the decision surfaces of ensembles of trees on the iris dataset ==================================================================== Plot the decision surfaces of forests of randomized trees trained on pairs of features of the iris dataset. This plot compares the decision surfaces learned by a decision tree classifier (first column), by a random forest classifier (second column), by an extra- trees classifier (third column) and by an AdaBoost classifier (fourth column). In the first row, the classifiers are built using the sepal width and the sepal length features only, on the second row using the petal length and sepal length only, and on the third row using the petal width and the petal length only. In descending order of quality, when trained (outside of this example) on all 4 features using 30 estimators and scored using 10 fold cross validation, we see:: ExtraTreesClassifier() # 0.95 score RandomForestClassifier() # 0.94 score AdaBoost(DecisionTree(max_depth=3)) # 0.94 score DecisionTree(max_depth=None) # 0.94 score Increasing `max_depth` for AdaBoost lowers the standard deviation of the scores (but the average score does not improve). See the console's output for further details about each model. In this example you might try to: 1) vary the ``max_depth`` for the ``DecisionTreeClassifier`` and ``AdaBoostClassifier``, perhaps try ``max_depth=3`` for the ``DecisionTreeClassifier`` or ``max_depth=None`` for ``AdaBoostClassifier`` 2) vary ``n_estimators`` It is worth noting that RandomForests and ExtraTrees can be fitted in parallel on many cores as each tree is built independently of the others. AdaBoost's samples are built sequentially and so do not use multiple cores. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn import clone from sklearn.datasets import load_iris from sklearn.ensemble import (RandomForestClassifier, ExtraTreesClassifier, AdaBoostClassifier) from sklearn.externals.six.moves import xrange from sklearn.tree import DecisionTreeClassifier # Parameters n_classes = 3 n_estimators = 30 cmap = plt.cm.RdYlBu plot_step = 0.02 # fine step width for decision surface contours plot_step_coarser = 0.5 # step widths for coarse classifier guesses RANDOM_SEED = 13 # fix the seed on each iteration # Load data iris = load_iris() plot_idx = 1 models = [DecisionTreeClassifier(max_depth=None), RandomForestClassifier(n_estimators=n_estimators), ExtraTreesClassifier(n_estimators=n_estimators), AdaBoostClassifier(DecisionTreeClassifier(max_depth=3), n_estimators=n_estimators)] for pair in ([0, 1], [0, 2], [2, 3]): for model in models: # We only take the two corresponding features X = iris.data[:, pair] y = iris.target # Shuffle idx = np.arange(X.shape[0]) np.random.seed(RANDOM_SEED) np.random.shuffle(idx) X = X[idx] y = y[idx] # Standardize mean = X.mean(axis=0) std = X.std(axis=0) X = (X - mean) / std # Train clf = clone(model) clf = model.fit(X, y) scores = clf.score(X, y) # Create a title for each column and the console by using str() and # slicing away useless parts of the string model_title = str(type(model)).split(".")[-1][:-2][:-len("Classifier")] model_details = model_title if hasattr(model, "estimators_"): model_details += " with {} estimators".format(len(model.estimators_)) print( model_details + " with features", pair, "has a score of", scores ) plt.subplot(3, 4, plot_idx) if plot_idx <= len(models): # Add a title at the top of each column plt.title(model_title) # Now plot the decision boundary using a fine mesh as input to a # filled contour plot x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step), np.arange(y_min, y_max, plot_step)) # Plot either a single DecisionTreeClassifier or alpha blend the # decision surfaces of the ensemble of classifiers if isinstance(model, DecisionTreeClassifier): Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) cs = plt.contourf(xx, yy, Z, cmap=cmap) else: # Choose alpha blend level with respect to the number of estimators # that are in use (noting that AdaBoost can use fewer estimators # than its maximum if it achieves a good enough fit early on) estimator_alpha = 1.0 / len(model.estimators_) for tree in model.estimators_: Z = tree.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) cs = plt.contourf(xx, yy, Z, alpha=estimator_alpha, cmap=cmap) # Build a coarser grid to plot a set of ensemble classifications # to show how these are different to what we see in the decision # surfaces. These points are regularly space and do not have a black outline xx_coarser, yy_coarser = np.meshgrid(np.arange(x_min, x_max, plot_step_coarser), np.arange(y_min, y_max, plot_step_coarser)) Z_points_coarser = model.predict(np.c_[xx_coarser.ravel(), yy_coarser.ravel()]).reshape(xx_coarser.shape) cs_points = plt.scatter(xx_coarser, yy_coarser, s=15, c=Z_points_coarser, cmap=cmap, edgecolors="none") # Plot the training points, these are clustered together and have a # black outline plt.scatter(X[:, 0], X[:, 1], c=y, cmap=ListedColormap(['r', 'y', 'b'])) plot_idx += 1 # move on to the next plot in sequence plt.suptitle("Classifiers on feature subsets of the Iris dataset") plt.axis("tight") plt.show()
bsd-3-clause
linsalrob/EdwardsLab
phage_protein_blast_genera/tax_violin_plots.py
1
2239
""" """ import os import sys import argparse import matplotlib #matplotlib.use('Agg') import matplotlib.pyplot as plt if __name__ == '__main__': parser = argparse.ArgumentParser(description="") parser.add_argument('-f', help='Genome average output file (from genera_per_phage_protein.py', default='/home/redwards/Desktop/gav_all_host.out') parser.add_argument('-n', help='taxonomy name one of: kingdom / phylum / genus / species', default='genus') parser.add_argument('-v', help='verbose output', action="store_true") args = parser.parse_args() ynames = {'kingdom' : 'kingdoms', 'phylum' : 'phyla', 'genus' : 'genera', 'species' : 'species'} col = None colkey = {'kingdom' : 3, 'phylum' : 4, 'genus' : 5, 'species' : 6} if args.n not in colkey: sys.stderr.write("Sorry, taxonomy name must be one of {}\n".format("|".join(list(colkey.keys())))) sys.exit(-1) col = colkey[args.n] want = {'Gut', 'Mouth', 'Nose', 'Skin', 'Lungs'} data = {} with open(args.f, 'r') as fin: for l in fin: p=l.strip().split("\t") if p[2] not in want: p[2] = 'All phages' #continue ## comment or uncomment this to include/exclude all data if p[2] not in data: data[p[2]] = [] data[p[2]].append(float(p[col])) labels = sorted(data.keys()) scores = [] count = 1 ticks = [] for l in labels: scores.append(data[l]) ticks.append(count) count += 1 fig = plt.figure() ax = fig.add_subplot(111) # ax.boxplot(alldata) vp = ax.violinplot(scores, showmeans=True) for i, j in enumerate(vp['bodies']): if i == 0: j.set_color('gray') elif i == 1: j.set_color('sandybrown') else: j.set_color('lightpink') ax.set_xlabel("Body Site") ax.set_ylabel("Average number of {}".format(ynames[args.n])) ax.set_xticks(ticks) ax.set_xticklabels(labels, rotation='vertical') ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() fig.set_facecolor('white') plt.tight_layout() #plt.show() fig.savefig("/home/redwards/Desktop/bodysites.png")
mit
rabrahm/ceres
utils/FastRotators/spfr.py
1
18831
from pylab import * import pyfits from PyAstronomy import pyasl import scipy from scipy import interpolate from scipy import ndimage from scipy import signal import pickle from matplotlib.backends.backend_pdf import PdfPages import os #from pyevolve import G1DList #from pyevolve import GSimpleGA from multiprocessing import Pool import time def download_models(webpage='http://svo2.cab.inta-csic.es/theory/models/coelho/high/data/',dest='../../data/'): os.system('mkdir '+dest+'/COELHO2014') cwd = os.getcwd() os.chdir(dest+'/COELHO2014') tf = np.arange(6000,10001,250) gf = np.arange(2.5,4.6,0.5) #gf = np.array([2.5]) zf = np.array([-1.,-0.5,0.0,0.2]) for t in tf: for g in gf: for z in zf: modname = get_modname(t,g,z) if z<0: sz = 'm' else: sz = 'p' sz = sz+str(float(np.absolute(z))).replace('.','')+'p00/' os.system('wget ' + webpage+sz+modname+'.fits') os.system('wget ' + webpage+sz+modname+'plc.fits') os.chdir(cwd) return True def n_Edlen(l): sigma = 1e4 / l sigma2 = sigma*sigma n = 1 + 1e-8 * (8342.13 + 2406030 / (130-sigma2) + 15997/(38.9-sigma2)) return n def n_Morton(l): sigma = 1e4 / l sigma2 = sigma*sigma n = 1 + 6.4328e-5 + 2.94981e-2 / (146.-sigma2) + 2.5540e-4/(41.-sigma2) return n def ToAir(l): return (l / n_Edlen(l)) def ToVacuum(l): cond = 1 l_prev = l.copy() while(cond): l_new = n_Edlen(l_prev) * l if (max(np.absolute(l_new - l_prev)) < 1e-10): cond = 0 l_prev = l_new return l_prev def get_modname(t,g,z): st = str(int(t)) if t<10000: st = '0'+st sg = '+'+str(np.around(g,1)) if z < 0: sz = 'm' else: sz = 'p' z=float(z) sz = sz + str(np.around(np.absolute(z),1)) sz = sz.replace('.','') return 't'+st+'_g'+sg+'_'+sz+'p00_hr' def get_model(t,g,z,model_path='../../data/COELHO2014/'): modname = model_path + get_modname(t,g,z) try: out = pyfits.getdata(modname+'.fits') except: out = pyfits.getdata(modname+'plc.fits') return out def get_near(x,vec): if x == vec[0]: mmin = vec[0] mmax = vec[1] elif x == vec[-1]: mmin = vec[-2] mmax = vec[-1] else: tvec = vec - x In = np.where(tvec < 0)[0] mmin = tvec[In].max() + x Ix = np.where(tvec >= 0)[0] mmax = tvec[Ix].min() + x return mmin,mmax def trilinear_interpolation(t,g,z,model_path='../../data/COELHO2014/'): teffs = np.arange(6000,10001,250) loggs = np.arange(2.5,4.6,0.5) fehs = np.array([-1.,-0.5,0.0,0.2]) x0,x1 = get_near(t,teffs) y0,y1 = get_near(g,loggs) z0,z1 = get_near(z,fehs) xd = (t-x0)/(x1-x0) yd = (g-y0)/(y1-y0) zd = (z-z0)/(z1-z0) try: hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'.fits') except: hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'plc.fits') c000 = get_model(x0,y0,z0,model_path) c001 = get_model(x0,y0,z1,model_path) c010 = get_model(x0,y1,z0,model_path) c100 = get_model(x1,y0,z0,model_path) c110 = get_model(x1,y1,z0,model_path) c101 = get_model(x1,y0,z1,model_path) c011 = get_model(x0,y1,z1,model_path) c111 = get_model(x1,y1,z1,model_path) wav = np.arange(len(c111))*hd['CDELT1'] + hd['CRVAL1'] c00 = c000*(1-xd) + c100*xd c01 = c001*(1-xd) + c101*xd c10 = c010*(1-xd) + c110*xd c11 = c011*(1-xd) + c111*xd c0 = c00*(1-yd) + c10*yd c1 = c01*(1-yd) + c11*yd c = c0*(1-zd) + c1*zd return wav,c def normalize_model(w,f): ow = w.copy() of = f.copy() #plot(w,f) while True: #medflts = scipy.signal.medfilt(f,1001) coef = np.polyfit(w,f,6) fited = np.polyval(coef,w) res = f - fited I = np.where(res > -np.sqrt(np.var(res)))[0] w,f = w[I],f[I] if len(w) < 0.3* len(ow): break #plot(ow,np.polyval(coef,ow)) #show() return coef def spec_ccf(sw,sf,mw,mf,vi,vf,dv): mf = mf -1 mf = -mf #plot(mw,mf) tck = interpolate.splrep(mw,mf,k=1) v = vi retccf = [] vels = [] while v<=vf: swt = sw * (1 + v/299792.458) mft = interpolate.splev(swt,tck) #if v == 0: # plot(swt,mft) # plot(swt,sft) # show() mft -= np.mean(mft) sft = sf - np.mean(sf) #sft = sf.copy() #print np.sum(mft**2),np.sum(sft**2) retccf.append(np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2))) vels.append(v) v+=dv return np.array(vels),np.array(retccf) def ccf_fft(swt,sft,mwt,mft): mf = mft -1 mf = -mf #plot(mw,mf) tck = interpolate.splrep(np.log(mwt),mf,k=1) sw = np.log(swt) tck2 = interpolate.splrep(sw,sft,k=1) nsw = np.linspace(sw[0], sw[-1], 5000) sf = interpolate.splev(nsw,tck2) mf = interpolate.splev(nsw,tck) sf -= np.mean(sf) mf -= np.mean(mf) plot(nsw,sf) plot(nsw,mf) show() retccf = np.fft.ifft(np.conj(np.fft.fft(sf))*np.fft.fft(mf)) retccf = np.hstack((retccf[2500:],retccf[:2500])) retvels = np.arange(len(retccf)) - 0.5*len(retccf) retvels *= (nsw[1]-nsw[0]) retvels = 299792.458*(np.exp(retvels)-1.) return retvels, retccf def ccf_simple(sw,sf,mw,mf,rv): mf = mf -1 mf = -mf #plot(mw,mf) tck = interpolate.splrep(mw,mf,k=1) swt = sw * (1 + rv/299792.458) mft = interpolate.splev(swt,tck) mft -= np.mean(mft) sft = sf - np.mean(sf) return np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2)) def clean_strong_lines(mw,sc,mode=1): if mode==1: #"""" I = np.where((mw>6520)&(mw<6600))[0] sc[I] = 1. I = np.where((mw>5888)&(mw<5897))[0] sc[I] = 1. I = np.where((mw>4310)&(mw<4360))[0] sc[I] = 1. I = np.where((mw>4840)&(mw<4880))[0] sc[I] = 1. I = np.where((mw>4070)&(mw<4130))[0] sc[I] = 1. I = np.where((mw>3875)&(mw<3900))[0] sc[I] = 1. I = np.where((mw>3920)&(mw<3945))[0] sc[I] = 1. I = np.where((mw>3955)&(mw<3980))[0] sc[I] = 1. I = np.where(mw<3850)[0] sc[I] = 1. #""" if mode==2: #"""" I = np.where((mw>6550)&(mw<6570))[0] sc[I] = 1. I = np.where((mw>5888)&(mw<5897))[0] sc[I] = 1. I = np.where((mw>4320)&(mw<4350))[0] sc[I] = 1. I = np.where((mw>4850)&(mw<4870))[0] sc[I] = 1. I = np.where((mw>4090)&(mw<4110))[0] sc[I] = 1. I = np.where((mw>3875)&(mw<3900))[0] sc[I] = 1. I = np.where((mw>3920)&(mw<3945))[0] sc[I] = 1. I = np.where((mw>3955)&(mw<3980))[0] sc[I] = 1. I = np.where(mw<3850)[0] sc[I] = 1. #""" return sc def RVforFR(wavs,flxs,teff=6700,logg=4.0,feh=-1.0,vsini=100.,model_path='../../data/COELHO2014/',vmin=-1000.,vmax=1000.,vstep=10.): def fitfunc(p,x): ret = p[3] + p[0] * np.exp(-.5*((x-p[1])/p[2])**2) return ret errfunc = lambda p,x,y: np.ravel( (fitfunc(p,x)-y) ) #sc = get_model(teff,logg,feh) #hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') #wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] teff = float(teff) try: sc = get_model(teff,logg,feh) hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] except: mw,sc = trilinear_interpolation(teff,logg,feh,model_path) for order in range(len(flxs)): flxs[order] = clean_strong_lines(wavs[order],flxs[order]) sc = clean_strong_lines(mw,sc) II = np.where(sc != 1)[0] JJ = np.where(sc == 1)[0] coef = normalize_model(mw[II],sc[II]) sc /= np.polyval(coef,mw) sc[JJ] = 1. mw = ToVacuum(mw) weis1 = [] ccftot = [] for i in range(wavs.shape[0]): #plot(wavs[i],flxs[i]) scf = flxs[i] scw = wavs[i] J = np.where(scf!=0)[0] scw,scf = scw[J],scf[J] I = np.where((mw>scw[0]-100) & (mw<scw[-1]+100)) tmf = pyasl.fastRotBroad(mw[I], sc[I], 0.5, vsini) #plot(mw[I],tmf) J = np.where(scf!=1)[0] if len(J)>100: ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) #plot(ccv,ccf) #show() #ccf = np.array(ccf) wei1 = len(np.where(scf!=1)[0])**2 weis1.append(wei1) if len(ccftot)==0: ccftot = ccf.copy()*wei1 else: ccftot = np.vstack((ccftot,ccf.copy()*wei1)) #show() weis1 = np.array(weis1) ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) p0 = [ccftot.min(),ccv[np.argmin(ccftot)],vsini,ccftot[0]] p1, success = scipy.optimize.leastsq(errfunc,p0, args=(ccv,ccftot)) return p1,ccv,ccftot,fitfunc(p1,ccv) def calc_bss2(vels,xc,coef, bot_i=0.15, bot_f=0.4, top_i=0.6, top_f=0.9, dt=0.01): try: I1 = np.where((vels>coef[1]-3*coef[2]) & (vels<coef[1]) )[0] I2 = np.where((vels<coef[1]+3*coef[2]) & (vels>coef[1]) )[0] I3 = np.where(vels<coef[1]-4*coef[2])[0] I4 = np.where(vels>coef[1]+4*coef[2])[0] I = np.hstack((I3,I4)) base = np.median(xc[I]) xc = base - xc xc /= xc.max() v1,x1 = vels[I1],xc[I1] v2,x2 = vels[I2],xc[I2] #plot(v1,x1) #plot(v2,x2) #show() dp = top_f vect = [] while dp >= top_i: lb = np.where(x1>dp)[0][0] m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) n = v1[lb] - m*x1[lb] bs1 = m*dp+n lb = np.where(x2>dp)[0][-1] m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) n = v2[lb] - m*x2[lb] bs2 = m*dp+n vect.append(0.5*(bs2+bs1)) dp-=dt vect = np.array(vect) dp = bot_f vecb = [] while dp >= bot_i: lb = np.where(x1>dp)[0][0] m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) n = v1[lb] - m*x1[lb] bs1 = m*dp+n lb = np.where(x2>dp)[0][-1] m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) n = v2[lb] - m*x2[lb] bs2 = m*dp+n vecb.append(0.5*(bs2+bs1)) dp-=dt vecb = np.array(vecb) return np.median(vecb) - np.median(vect) except: return -999.0 """ def lnlike(theta, W, F, Ferr): mw,sc = trilinear_interpolation(int(theta[0]),theta[1],theta[2]) sct = clean_strong_lines(mw,sc.copy()) #plot(mw,sc) #show() coef = normalize_model(mw,sct) sc /= np.polyval(coef,mw) #print gfd mw = ToVacuum(mw) mw *= 1 + theta[3]/299792.458 totD,totM,totE = np.array([]),np.array([]),np.array([]) for i in range(W.shape[0]): scf = F[i] scw = W[i] scfe = Ferr[i] J = np.where(scf!=0)[0] scw,scf,scfe = scw[J],scf[J],scfe[J] I = np.where((mw>scw[0]-10) & (mw<scw[-1]+10)) tmf = pyasl.fastRotBroad(mw[I], sc[I], 0.5, theta[4]) tck = interpolate.splrep(mw[I],tmf,k=1) tmf = interpolate.splev(scw,tck) tmf = clean_strong_lines(scw,tmf.copy()) I = np.where(tmf!=1)[0] #plot(scw,tmf) #plot(scw[I],tmf[I]) #plot(scw[I],scf[I]) #show() #print gfd tmf = tmf[I] scf = scf[I] scfe = scfe[I] tmf /= np.sum(tmf) tsf = scf/np.sum(scf) tse = scfe*(np.sum(scf)**2) totD = np.hstack((totD,tsf)) totM = np.hstack((totM,tmf)) totE = np.hstack((totE,tse)) #plot(scw[I],tsf) #plot(scw[I],tmf) #plot(scw[I],tsf + 1./np.sqrt(tse)) #show() #print fds #print theta #show() #print gvfd #ret = -np.log(2*np.pi) + np.log(np.sum(np.exp(-0.5*((y-model)/yerr)**2)/yerr)) #ret = -0.5*(np.sum(inv_sigma2*(F-model)**2 - np.log(inv_sigma2))) ret = -0.5*(np.sum(totE*(totD-totM)**2 - np.log(totE))) #for i in range(len(F)): # errorbar(Y,F[i],yerr=Ferr[i],fmt='b') #for j in model: # plot(Y,j,'r') #show() #print theta, ret if np.isnan(ret): return -np.inf else: return ret def lnprior(theta): if 6000 < theta[0] < 9000 and 3.0 < theta[1] < 4.5 and -1 < theta[2] < 0.2 and -500 < theta[3] < 500 and 1. < theta[4] < 500.: return 0.0 return -np.inf def lnprob(theta, W,F,Ferr): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf return lp + lnlike(theta,W,F,Ferr) """ def multiccf(pars): teff,logg,feh,vsini=pars[0],pars[1],pars[2],pars[3] vmin=-500 vmax=500. vstep=20. sc = get_model(teff,logg,feh) hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] try: sc = get_model(teff,logg,feh) hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] except: mw,sc = trilinear_interpolation(teff,logg,feh,model_path) sc = clean_strong_lines(mw,sc) II = np.where(sc != 1)[0] JJ = np.where(sc == 1)[0] coef = normalize_model(mw[II],sc[II]) sc /= np.polyval(coef,mw) sc[JJ] = 1. mw = ToVacuum(mw) weis1 = [] ccftot = [] for i in range(wavs.shape[0]): scf = flxs[i].copy() scw = wavs[i].copy() J = np.where(scf!=0)[0] scw,scf = scw[J],scf[J] I = np.where((mw>scw[0]-100) & (mw<scw[-1]+100)) tmf = pyasl.fastRotBroad(mw[I], sc[I], 0.5, vsini) #plot(mw[I],tmf) J = np.where(scf!=1)[0] if len(J)>100: ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) #ccv,ccf = ccf_fft(scw,scf,mw[I],tmf) #plot(ccv,ccf) #show() wei1 = len(np.where(scf!=1)[0])**2 weis1.append(wei1) if len(ccftot)==0: ccftot = ccf.copy()*wei1 else: ccftot = np.vstack((ccftot,ccf.copy()*wei1)) weis1 = np.array(weis1) ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) #print gfds #ccftot = np.mean(ccftot,axis=0) #print pars, ccftot.min() return ccftot.min() def get_pars_fr(wavst,flxst,model_patht='../../data/COELHO2014/',npools=4,fixG=1.0): for order in range(len(flxst)): flxst[order] = clean_strong_lines(wavst[order],flxst[order],mode=1) t0 = time.time() global wavs,flxs global model_path wavs,flxs=wavst.copy(),flxst.copy() model_path=model_patht gt = np.array([6000,7000,8000,9000,10000]) gg = np.array([2.5,3.0,3.5,4.0,4.5]) if fixG != -1: gg = np.array([fixG]) gz = np.array([-1,-0.5,0.0,0.2]) gr = np.array([10.,50.,100.,150.,200.,250.,300.]) #""" tr = np.tile(gr,len(gt)*len(gg)*len(gz)) tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) tot = np.vstack((tt,tg,tz,tr)).T #for pars in tot: # pars = [8000,4.0,-0.5,40.0] # print pars, multiccf(pars) p = Pool(npools) vals = np.array((p.map(multiccf, list(tot)))) p.terminate() I = np.argmin(vals) best_vals = tot[I] bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] #""" t1 = time.time() print bt,bg,bz,br, (t1-t0)/60.,'mins' #bt,bg,bz,br = 7000.,4.5, 0.2, 100.0 gt = np.arange(bt-1000,bt+1001,250) I = np.where((gt>=6000) & (gt<=10000))[0] gt = gt[I] gr = np.arange(br-60.,br+61.,20.) I = np.where(gr>=10)[0] gr = gr[I] tr = np.tile(gr,len(gt)*len(gg)*len(gz)) tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) tot = np.vstack((tt,tg,tz,tr)).T p = Pool(npools) vals = np.array((p.map(multiccf, list(tot)))) p.terminate() I = np.argmin(vals) best_vals = tot[I] bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] t2 = time.time() print bt,bg,bz,br, (t2-t1)/60.,'mins' #np.savetxt('temp_grid.txt',vals) if fixG==-1: grid = np.reshape(vals,(len(gt),len(gg),len(gz),len(gr))) tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) tckg = interpolate.splrep(gg,np.arange(len(gg)),k=1) tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) itckg = interpolate.splrep(np.arange(len(gg)),gg,k=1) itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) st = np.arange(gt[0],gt[-1]+1,10.) sg = np.arange(gg[0],gg[-1]+0.01,0.1) sz = np.arange(gz[0],gz[-1]+0.01,0.1) sr = np.arange(gr[0],gr[-1]+1.,5.) st = interpolate.splev(st,tckt) sg = interpolate.splev(sg,tckg) sz = interpolate.splev(sz,tckz) sr = interpolate.splev(sr,tckr) tr2 = np.tile(sr,len(st)*len(sg)*len(sz)) tg2 = np.repeat(np.tile(sg,len(st)),len(sr)*len(sz)) tz2 = np.repeat(np.tile(sz,len(st)*len(sg)),len(sr)) tt2 = np.repeat(st,len(sg)*len(sr)*len(sz)) tot2 = np.vstack((tt2,tg2,tz2,tr2)) zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') I = np.argmin(zi) minval = tot2[:,I] mint = interpolate.splev(minval[0],itckt) ming = interpolate.splev(minval[1],itckg) minz = interpolate.splev(minval[2],itckz) minr = interpolate.splev(minval[3],itckr) else: grid = np.reshape(vals,(len(gt),len(gz),len(gr))) tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) st = np.arange(gt[0],gt[-1]+1,10.) sz = np.arange(gz[0],gz[-1]+0.01,0.1) sr = np.arange(gr[0],gr[-1]+1.,5.) st = interpolate.splev(st,tckt) sz = interpolate.splev(sz,tckz) sr = interpolate.splev(sr,tckr) tr2 = np.tile(sr,len(st)*len(sz)) tz2 = np.repeat(np.tile(sz,len(st)),len(sr)) tt2 = np.repeat(st,len(sr)*len(sz)) tot2 = np.vstack((tt2,tz2,tr2)) zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') I = np.argmin(zi) minval = tot2[:,I] mint = interpolate.splev(minval[0],itckt) ming = fixG minz = interpolate.splev(minval[1],itckz) minr = interpolate.splev(minval[2],itckr) #d = {'grid':grid, 'zi':zi, 'tot2':tot2, 'gt':gt, 'gg':gg, 'gz':gz, 'gr':gr} #pickle.dump(d,open('temp_dict.pkl')) return float(mint),float(ming),float(minz),float(minr) def plot_CCF_FR(xc_dict,path='XC.pdf'): vels = xc_dict['vels'] xc_av = xc_dict['xc_av'] XCmodelgau = xc_dict['XCmodelgau'] #refvel = xc_dict['refvel'] p1gau = xc_dict['p1gau'] f1 = figure() pp = PdfPages(path) ax1 = f1.add_subplot(111) ax1.plot(vels, xc_av,'b.', label='CCF') ax1.plot(vels, XCmodelgau,'r-',label='Gaussian fit') xlabel('Velocity (km/s)') ylabel('XC') ax1.axvline(p1gau[1],linestyle=':',color='r') ax1.axhline(0.0,linestyle='-') title('Average Cross-Correlation Function + Fit') handles, labels = ax1.get_legend_handles_labels() ax1.legend(handles[::-1], labels[::-1],prop={'size':6}) pp.savefig() pp.close() clf() pass """ def trans_chromosome(chromosome): teff = chromosome[0]*100.+chromosome[1]*10.+chromosome[2] m = (10000.- 6000.)/999. n = 6000. teff = teff*m + n logg = chromosome[3] + chromosome[4]*0.1 m = (4.5 - 3.0)/9.9 n = 3. logg = logg*m + n feh = chromosome[5] + chromosome[6]*0.1 m = (0.2 - -1.)/9.9 n = -1. feh = feh*m + n vsini = chromosome[7]*10. + chromosome[8] m = (300. - 10.)/99. n = 10. vsini = vsini*m + n return teff, logg, feh, vsini global wavs, flxs def find_pars_GA(wavs,flxs,model_path='../../data/COELHO2014/'): def eval_func(chromosome): print list(chromosome) teff, logg, feh, vsini = trans_chromosome(chromosome) print teff, logg, feh, vsini pt,vels,ccf,mod = RVforFR(wavs,flxs,teff=teff,logg=logg,feh=feh,vsini=vsini,model_path=model_path) score = -ccf.min() return score genome = G1DList.G1DList(9) genome.evaluator.set(eval_func) ga = GSimpleGA.GSimpleGA(genome, interactiveMode=True) ga.setGenerations(40) ga.setMutationRate(0.2) ga.setPopulationSize(20) #ga.setCrossoverRate(1.0) genome.setParams(rangemin=0, rangemax=9) #ga.setMultiProcessing(True) ga.evolve(freq_stats=10) print ga.bestIndividual() print trans_chromosome(ga.bestIndividual()) """
mit
HeraclesHX/scikit-learn
sklearn/cluster/tests/test_dbscan.py
114
11393
""" Tests for DBSCAN clustering algorithm """ import pickle import numpy as np from scipy.spatial import distance from scipy import sparse from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_in from sklearn.utils.testing import assert_not_in from sklearn.cluster.dbscan_ import DBSCAN from sklearn.cluster.dbscan_ import dbscan from sklearn.cluster.tests.common import generate_clustered_data from sklearn.metrics.pairwise import pairwise_distances n_clusters = 3 X = generate_clustered_data(n_clusters=n_clusters) def test_dbscan_similarity(): # Tests the DBSCAN algorithm with a similarity array. # Parameters chosen specifically for this task. eps = 0.15 min_samples = 10 # Compute similarities D = distance.squareform(distance.pdist(X)) D /= np.max(D) # Compute DBSCAN core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - (1 if -1 in labels else 0) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric="precomputed", eps=eps, min_samples=min_samples) labels = db.fit(D).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_feature(): # Tests the DBSCAN algorithm with a feature vector array. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 metric = 'euclidean' # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples) labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_sparse(): core_sparse, labels_sparse = dbscan(sparse.lil_matrix(X), eps=.8, min_samples=10) core_dense, labels_dense = dbscan(X, eps=.8, min_samples=10) assert_array_equal(core_dense, core_sparse) assert_array_equal(labels_dense, labels_sparse) def test_dbscan_no_core_samples(): rng = np.random.RandomState(0) X = rng.rand(40, 10) X[X < .8] = 0 for X_ in [X, sparse.csr_matrix(X)]: db = DBSCAN(min_samples=6).fit(X_) assert_array_equal(db.components_, np.empty((0, X_.shape[1]))) assert_array_equal(db.labels_, -1) assert_equal(db.core_sample_indices_.shape, (0,)) def test_dbscan_callable(): # Tests the DBSCAN algorithm with a callable metric. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 # metric is the function reference, not the string key. metric = distance.euclidean # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_balltree(): # Tests the DBSCAN algorithm with balltree for neighbor calculation. eps = 0.8 min_samples = 10 D = pairwise_distances(X) core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='kd_tree') labels = db.fit(X).labels_ n_clusters_3 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_3, n_clusters) db = DBSCAN(p=1.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_4 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_4, n_clusters) db = DBSCAN(leaf_size=20, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_5 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_5, n_clusters) def test_input_validation(): # DBSCAN.fit should accept a list of lists. X = [[1., 2.], [3., 4.]] DBSCAN().fit(X) # must not raise exception def test_dbscan_badargs(): # Test bad argument values: these should all raise ValueErrors assert_raises(ValueError, dbscan, X, eps=-1.0) assert_raises(ValueError, dbscan, X, algorithm='blah') assert_raises(ValueError, dbscan, X, metric='blah') assert_raises(ValueError, dbscan, X, leaf_size=-1) assert_raises(ValueError, dbscan, X, p=-1) def test_pickle(): obj = DBSCAN() s = pickle.dumps(obj) assert_equal(type(pickle.loads(s)), obj.__class__) def test_boundaries(): # ensure min_samples is inclusive of core point core, _ = dbscan([[0], [1]], eps=2, min_samples=2) assert_in(0, core) # ensure eps is inclusive of circumference core, _ = dbscan([[0], [1], [1]], eps=1, min_samples=2) assert_in(0, core) core, _ = dbscan([[0], [1], [1]], eps=.99, min_samples=2) assert_not_in(0, core) def test_weighted_dbscan(): # ensure sample_weight is validated assert_raises(ValueError, dbscan, [[0], [1]], sample_weight=[2]) assert_raises(ValueError, dbscan, [[0], [1]], sample_weight=[2, 3, 4]) # ensure sample_weight has an effect assert_array_equal([], dbscan([[0], [1]], sample_weight=None, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 5], min_samples=6)[0]) assert_array_equal([0], dbscan([[0], [1]], sample_weight=[6, 5], min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 6], min_samples=6)[0]) # points within eps of each other: assert_array_equal([0, 1], dbscan([[0], [1]], eps=1.5, sample_weight=[5, 1], min_samples=6)[0]) # and effect of non-positive and non-integer sample_weight: assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[5.9, 0.1], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[6, -1], eps=1.5, min_samples=6)[0]) # for non-negative sample_weight, cores should be identical to repetition rng = np.random.RandomState(42) sample_weight = rng.randint(0, 5, X.shape[0]) core1, label1 = dbscan(X, sample_weight=sample_weight) assert_equal(len(label1), len(X)) X_repeated = np.repeat(X, sample_weight, axis=0) core_repeated, label_repeated = dbscan(X_repeated) core_repeated_mask = np.zeros(X_repeated.shape[0], dtype=bool) core_repeated_mask[core_repeated] = True core_mask = np.zeros(X.shape[0], dtype=bool) core_mask[core1] = True assert_array_equal(np.repeat(core_mask, sample_weight), core_repeated_mask) # sample_weight should work with precomputed distance matrix D = pairwise_distances(X) core3, label3 = dbscan(D, sample_weight=sample_weight, metric='precomputed') assert_array_equal(core1, core3) assert_array_equal(label1, label3) # sample_weight should work with estimator est = DBSCAN().fit(X, sample_weight=sample_weight) core4 = est.core_sample_indices_ label4 = est.labels_ assert_array_equal(core1, core4) assert_array_equal(label1, label4) est = DBSCAN() label5 = est.fit_predict(X, sample_weight=sample_weight) core5 = est.core_sample_indices_ assert_array_equal(core1, core5) assert_array_equal(label1, label5) assert_array_equal(label1, est.labels_) def test_dbscan_core_samples_toy(): X = [[0], [2], [3], [4], [6], [8], [10]] n_samples = len(X) for algorithm in ['brute', 'kd_tree', 'ball_tree']: # Degenerate case: every sample is a core sample, either with its own # cluster or including other close core samples. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=1) assert_array_equal(core_samples, np.arange(n_samples)) assert_array_equal(labels, [0, 1, 1, 1, 2, 3, 4]) # With eps=1 and min_samples=2 only the 3 samples from the denser area # are core samples. All other points are isolated and considered noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=2) assert_array_equal(core_samples, [1, 2, 3]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # Only the sample in the middle of the dense area is core. Its two # neighbors are edge samples. Remaining samples are noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=3) assert_array_equal(core_samples, [2]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # It's no longer possible to extract core samples with eps=1: # everything is noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=4) assert_array_equal(core_samples, []) assert_array_equal(labels, -np.ones(n_samples)) def test_dbscan_precomputed_metric_with_degenerate_input_arrays(): # see https://github.com/scikit-learn/scikit-learn/issues/4641 for # more details X = np.ones((10, 2)) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert_equal(len(set(labels)), 1) X = np.zeros((10, 2)) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert_equal(len(set(labels)), 1)
bsd-3-clause
micahcochran/geopandas
geopandas/tools/tests/test_sjoin.py
1
10287
from __future__ import absolute_import from distutils.version import LooseVersion import numpy as np import pandas as pd from shapely.geometry import Point, Polygon import geopandas from geopandas import GeoDataFrame, GeoSeries, read_file, base from geopandas import sjoin import pytest from pandas.util.testing import assert_frame_equal pandas_0_18_problem = 'fails under pandas < 0.19 due to pandas issue 15692,'\ 'not problem with sjoin.' @pytest.fixture() def dfs(request): polys1 = GeoSeries( [Polygon([(0, 0), (5, 0), (5, 5), (0, 5)]), Polygon([(5, 5), (6, 5), (6, 6), (5, 6)]), Polygon([(6, 0), (9, 0), (9, 3), (6, 3)])]) polys2 = GeoSeries( [Polygon([(1, 1), (4, 1), (4, 4), (1, 4)]), Polygon([(4, 4), (7, 4), (7, 7), (4, 7)]), Polygon([(7, 7), (10, 7), (10, 10), (7, 10)])]) df1 = GeoDataFrame({'geometry': polys1, 'df1': [0, 1, 2]}) df2 = GeoDataFrame({'geometry': polys2, 'df2': [3, 4, 5]}) if request.param == 'string-index': df1.index = ['a', 'b', 'c'] df2.index = ['d', 'e', 'f'] # construction expected frames expected = {} part1 = df1.copy().reset_index().rename( columns={'index': 'index_left'}) part2 = df2.copy().iloc[[0, 1, 1, 2]].reset_index().rename( columns={'index': 'index_right'}) part1['_merge'] = [0, 1, 2] part2['_merge'] = [0, 0, 1, 3] exp = pd.merge(part1, part2, on='_merge', how='outer') expected['intersects'] = exp.drop('_merge', axis=1).copy() part1 = df1.copy().reset_index().rename( columns={'index': 'index_left'}) part2 = df2.copy().reset_index().rename( columns={'index': 'index_right'}) part1['_merge'] = [0, 1, 2] part2['_merge'] = [0, 3, 3] exp = pd.merge(part1, part2, on='_merge', how='outer') expected['contains'] = exp.drop('_merge', axis=1).copy() part1['_merge'] = [0, 1, 2] part2['_merge'] = [3, 1, 3] exp = pd.merge(part1, part2, on='_merge', how='outer') expected['within'] = exp.drop('_merge', axis=1).copy() return [request.param, df1, df2, expected] @pytest.mark.skipif(not base.HAS_SINDEX, reason='Rtree absent, skipping') class TestSpatialJoin: @pytest.mark.parametrize('dfs', ['default-index', 'string-index'], indirect=True) @pytest.mark.parametrize('op', ['intersects', 'contains', 'within']) def test_inner(self, op, dfs): index, df1, df2, expected = dfs res = sjoin(df1, df2, how='inner', op=op) exp = expected[op].dropna().copy() exp = exp.drop('geometry_y', axis=1).rename( columns={'geometry_x': 'geometry'}) exp[['df1', 'df2']] = exp[['df1', 'df2']].astype('int64') if index == 'default-index': exp[['index_left', 'index_right']] = \ exp[['index_left', 'index_right']].astype('int64') exp = exp.set_index('index_left') exp.index.name = None assert_frame_equal(res, exp) @pytest.mark.parametrize('dfs', ['default-index', 'string-index'], indirect=True) @pytest.mark.parametrize('op', ['intersects', 'contains', 'within']) def test_left(self, op, dfs): index, df1, df2, expected = dfs res = sjoin(df1, df2, how='left', op=op) exp = expected[op].dropna(subset=['index_left']).copy() exp = exp.drop('geometry_y', axis=1).rename( columns={'geometry_x': 'geometry'}) exp['df1'] = exp['df1'].astype('int64') if index == 'default-index': exp['index_left'] = exp['index_left'].astype('int64') # TODO: in result the dtype is object res['index_right'] = res['index_right'].astype(float) exp = exp.set_index('index_left') exp.index.name = None assert_frame_equal(res, exp) @pytest.mark.parametrize('dfs', ['default-index', 'string-index'], indirect=True) @pytest.mark.parametrize('op', ['intersects', 'contains', 'within']) def test_right(self, op, dfs): index, df1, df2, expected = dfs res = sjoin(df1, df2, how='right', op=op) exp = expected[op].dropna(subset=['index_right']).copy() exp = exp.drop('geometry_x', axis=1).rename( columns={'geometry_y': 'geometry'}) exp['df2'] = exp['df2'].astype('int64') if index == 'default-index': exp['index_right'] = exp['index_right'].astype('int64') res['index_left'] = res['index_left'].astype(float) exp = exp.set_index('index_right') exp = exp.reindex(columns=res.columns) assert_frame_equal(res, exp, check_index_type=False) @pytest.mark.skipif(not base.HAS_SINDEX, reason='Rtree absent, skipping') class TestSpatialJoinNYBB: def setup_method(self): nybb_filename = geopandas.datasets.get_path('nybb') self.polydf = read_file(nybb_filename) self.crs = self.polydf.crs N = 20 b = [int(x) for x in self.polydf.total_bounds] self.pointdf = GeoDataFrame( [{'geometry': Point(x, y), 'pointattr1': x + y, 'pointattr2': x - y} for x, y in zip(range(b[0], b[2], int((b[2]-b[0])/N)), range(b[1], b[3], int((b[3]-b[1])/N)))], crs=self.crs) def test_geometry_name(self): # test sjoin is working with other geometry name polydf_original_geom_name = self.polydf.geometry.name self.polydf = (self.polydf.rename(columns={'geometry': 'new_geom'}) .set_geometry('new_geom')) assert polydf_original_geom_name != self.polydf.geometry.name res = sjoin(self.polydf, self.pointdf, how="left") assert self.polydf.geometry.name == res.geometry.name def test_sjoin_left(self): df = sjoin(self.pointdf, self.polydf, how='left') assert df.shape == (21, 8) for i, row in df.iterrows(): assert row.geometry.type == 'Point' assert 'pointattr1' in df.columns assert 'BoroCode' in df.columns def test_sjoin_right(self): # the inverse of left df = sjoin(self.pointdf, self.polydf, how="right") df2 = sjoin(self.polydf, self.pointdf, how="left") assert df.shape == (12, 8) assert df.shape == df2.shape for i, row in df.iterrows(): assert row.geometry.type == 'MultiPolygon' for i, row in df2.iterrows(): assert row.geometry.type == 'MultiPolygon' def test_sjoin_inner(self): df = sjoin(self.pointdf, self.polydf, how="inner") assert df.shape == (11, 8) def test_sjoin_op(self): # points within polygons df = sjoin(self.pointdf, self.polydf, how="left", op="within") assert df.shape == (21, 8) assert df.ix[1]['BoroName'] == 'Staten Island' # points contain polygons? never happens so we should have nulls df = sjoin(self.pointdf, self.polydf, how="left", op="contains") assert df.shape == (21, 8) assert np.isnan(df.ix[1]['Shape_Area']) def test_sjoin_bad_op(self): # AttributeError: 'Point' object has no attribute 'spandex' with pytest.raises(ValueError): sjoin(self.pointdf, self.polydf, how="left", op="spandex") def test_sjoin_duplicate_column_name(self): pointdf2 = self.pointdf.rename(columns={'pointattr1': 'Shape_Area'}) df = sjoin(pointdf2, self.polydf, how="left") assert 'Shape_Area_left' in df.columns assert 'Shape_Area_right' in df.columns def test_sjoin_values(self): # GH190 self.polydf.index = [1, 3, 4, 5, 6] df = sjoin(self.pointdf, self.polydf, how='left') assert df.shape == (21, 8) df = sjoin(self.polydf, self.pointdf, how='left') assert df.shape == (12, 8) @pytest.mark.skipif(str(pd.__version__) < LooseVersion('0.19'), reason=pandas_0_18_problem) @pytest.mark.xfail def test_no_overlapping_geometry(self): # Note: these tests are for correctly returning GeoDataFrame # when result of the join is empty df_inner = sjoin(self.pointdf.iloc[17:], self.polydf, how='inner') df_left = sjoin(self.pointdf.iloc[17:], self.polydf, how='left') df_right = sjoin(self.pointdf.iloc[17:], self.polydf, how='right') # Recent Pandas development has introduced a new way of handling merges # this change has altered the output when no overlapping geometries if str(pd.__version__) > LooseVersion('0.18.1'): right_idxs = pd.Series(range(0, 5), name='index_right', dtype='int64') else: right_idxs = pd.Series(name='index_right', dtype='int64') expected_inner_df = pd.concat( [self.pointdf.iloc[:0], pd.Series(name='index_right', dtype='int64'), self.polydf.drop('geometry', axis=1).iloc[:0]], axis=1) expected_inner = GeoDataFrame( expected_inner_df, crs={'init': 'epsg:4326', 'no_defs': True}) expected_right_df = pd.concat( [self.pointdf.drop('geometry', axis=1).iloc[:0], pd.concat([pd.Series(name='index_left', dtype='int64'), right_idxs], axis=1), self.polydf], axis=1) expected_right = GeoDataFrame( expected_right_df, crs={'init': 'epsg:4326', 'no_defs': True})\ .set_index('index_right') expected_left_df = pd.concat( [self.pointdf.iloc[17:], pd.Series(name='index_right', dtype='int64'), self.polydf.iloc[:0].drop('geometry', axis=1)], axis=1) expected_left = GeoDataFrame( expected_left_df, crs={'init': 'epsg:4326', 'no_defs': True}) assert expected_inner.equals(df_inner) assert expected_right.equals(df_right) assert expected_left.equals(df_left) @pytest.mark.skip("Not implemented") def test_sjoin_outer(self): df = sjoin(self.pointdf, self.polydf, how="outer") assert df.shape == (21, 8)
bsd-3-clause
jrleja/bsfh
misc/timings_pyfsps.py
3
4274
#compare a lookup table of spectra at ages and metallicities to #calls to fsps.sps.get_spectrum() for different metallicities import time, os, subprocess, re, sys import numpy as np #import matplotlib.pyplot as pl import fsps from prospect import sources as sps_basis from prospect.models import sedmodel def run_command(cmd): """ Open a child process, and return its exit status and stdout. """ child = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE) out = [s for s in child.stdout] w = child.wait() return os.WEXITSTATUS(w), out # Check to make sure that the required environment variable is present. try: ev = os.environ["SPS_HOME"] except KeyError: raise ImportError("You need to have the SPS_HOME environment variable") # Check the SVN revision number. cmd = ["svnversion", ev] stat, out = run_command(" ".join(cmd)) fsps_vers = int(re.match("^([0-9])+", out[0]).group(0)) sps = fsps.StellarPopulation(zcontinuous=True) print('FSPS version = {}'.format(fsps_vers)) print('Zs={0}, N_lambda={1}'.format(sps.zlegend, len(sps.wavelengths))) print('single age') def spec_from_fsps(z, t, s): t0 = time.time() sps.params['logzsol'] = z sps.params['sigma_smooth'] = s sps.params['tage'] = t wave, spec = sps.get_spectrum(peraa=True, tage = sps.params['tage']) #print(spec.shape) return time.time()-t0 def mags_from_fsps(z, t, s): t0 = time.time() sps.params['zred']=t sps.params['logzsol'] = z sps.params['sigma_smooth'] = s sps.params['tage'] = t mags = sps.get_mags(tage = sps.params['tage'], redshift=0.0) #print(spec.shape) return time.time()-t0 def spec_from_ztinterp(z, t, s): t0 = time.time() sps.params['logzsol'] = z sps.params['sigma_smooth'] = s sps.params['tage'] = t sps.params['imf3'] = s spec, m, l = sps.ztinterp(sps.params['logzsol'], sps.params['tage'], peraa=True) #print(spec.shape) return time.time()-t0 if sys.argv[1] == 'mags': from_fsps = mags_from_fsps print('timing get_mags') print('nbands = {}'.format(len(sps.get_mags(tage=1.0)))) elif sys.argv[1] == 'spec': from_fsps = spec_from_fsps print('timing get_spectrum') elif sys.argv[1] == 'ztinterp': from_fsps = spec_from_ztinterp print('timing get_spectrum') elif sys.argv[1] == 'sedpy': from sedpy import observate nbands = len(sps.get_mags(tage=1.0)) fnames = nbands * ['sdss_r0'] filters = observate.load_filters(fnames) def mags_from_sedpy(z, t, s): t0 = time.time() sps.params['logzsol'] = z sps.params['sigma_smooth'] = s sps.params['tage'] = t wave, spec = sps.get_spectrum(peraa=True, tage = sps.params['tage']) mags = observate.getSED(wave, spec, filters) return time.time()-t0 from_fsps = mags_from_sedpy sps.params['add_neb_emission'] = False sps.params['smooth_velocity'] = True sps.params['sfh'] = 0 ntry = 30 zz = np.random.uniform(-1,0,ntry) tt = np.random.uniform(0.1,4,ntry) ss = np.random.uniform(1,2.5,ntry) #make sure all z's already compiled _ =[from_fsps(z, 1.0, 0.0) for z in [-1, -0.8, -0.6, -0.4, -0.2, 0.0]] all_dur = [] print('no neb emission:') dur_many = np.zeros(ntry) for i in xrange(ntry): dur_many[i] = from_fsps(zz[i], tt[i], ss[i]) print('<t/call>={0}s, sigma_t={1}s'.format(dur_many.mean(), dur_many.std())) all_dur += [dur_many] print('no neb emission, no smooth:') dur_many = np.zeros(ntry) for i in xrange(ntry): dur_many[i] = from_fsps(zz[i], tt[i], 0.0) print('<t/call>={0}s, sigma_t={1}s'.format(dur_many.mean(), dur_many.std())) all_dur += [dur_many] sps.params['add_neb_emission'] = True print('neb emission:') dur_many = np.zeros(ntry) for i in xrange(ntry): dur_many[i] = from_fsps(zz[i], tt[i], ss[i]) print('<t/call>={0}s, sigma_t={1}s'.format(dur_many.mean(), dur_many.std())) all_dur += [dur_many] print('neb emission, no smooth:') dur_many = np.zeros(ntry) for i in xrange(ntry): dur_many[i] = from_fsps(zz[i], tt[i], 0.0) print('<t/call>={0}s, sigma_t={1}s'.format(dur_many.mean(), dur_many.std())) all_dur += [dur_many]
mit
imaculate/scikit-learn
examples/preprocessing/plot_function_transformer.py
158
1993
""" ========================================================= Using FunctionTransformer to select columns ========================================================= Shows how to use a function transformer in a pipeline. If you know your dataset's first principle component is irrelevant for a classification task, you can use the FunctionTransformer to select all but the first column of the PCA transformed data. """ import matplotlib.pyplot as plt import numpy as np from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA from sklearn.pipeline import make_pipeline from sklearn.preprocessing import FunctionTransformer def _generate_vector(shift=0.5, noise=15): return np.arange(1000) + (np.random.rand(1000) - shift) * noise def generate_dataset(): """ This dataset is two lines with a slope ~ 1, where one has a y offset of ~100 """ return np.vstack(( np.vstack(( _generate_vector(), _generate_vector() + 100, )).T, np.vstack(( _generate_vector(), _generate_vector(), )).T, )), np.hstack((np.zeros(1000), np.ones(1000))) def all_but_first_column(X): return X[:, 1:] def drop_first_component(X, y): """ Create a pipeline with PCA and the column selector and use it to transform the dataset. """ pipeline = make_pipeline( PCA(), FunctionTransformer(all_but_first_column), ) X_train, X_test, y_train, y_test = train_test_split(X, y) pipeline.fit(X_train, y_train) return pipeline.transform(X_test), y_test if __name__ == '__main__': X, y = generate_dataset() lw = 0 plt.figure() plt.scatter(X[:, 0], X[:, 1], c=y, lw=lw) plt.figure() X_transformed, y_transformed = drop_first_component(*generate_dataset()) plt.scatter( X_transformed[:, 0], np.zeros(len(X_transformed)), c=y_transformed, lw=lw, s=60 ) plt.show()
bsd-3-clause
rohanp/scikit-learn
sklearn/model_selection/tests/test_validation.py
20
27961
"""Test the validation module""" from __future__ import division import sys import warnings import numpy as np from scipy.sparse import coo_matrix, csr_matrix from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_false from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_raise_message from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_less from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_warns from sklearn.utils.mocking import CheckingClassifier, MockDataFrame from sklearn.model_selection import cross_val_score from sklearn.model_selection import cross_val_predict from sklearn.model_selection import permutation_test_score from sklearn.model_selection import KFold from sklearn.model_selection import StratifiedKFold from sklearn.model_selection import LeaveOneOut from sklearn.model_selection import LeaveOneLabelOut from sklearn.model_selection import LeavePLabelOut from sklearn.model_selection import LabelKFold from sklearn.model_selection import LabelShuffleSplit from sklearn.model_selection import learning_curve from sklearn.model_selection import validation_curve from sklearn.model_selection._validation import _check_is_permutation from sklearn.datasets import make_regression from sklearn.datasets import load_boston from sklearn.datasets import load_iris from sklearn.metrics import explained_variance_score from sklearn.metrics import make_scorer from sklearn.metrics import precision_score from sklearn.linear_model import Ridge from sklearn.linear_model import PassiveAggressiveClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.cluster import KMeans from sklearn.preprocessing import Imputer from sklearn.pipeline import Pipeline from sklearn.externals.six.moves import cStringIO as StringIO from sklearn.base import BaseEstimator from sklearn.multiclass import OneVsRestClassifier from sklearn.datasets import make_classification from sklearn.datasets import make_multilabel_classification from test_split import MockClassifier class MockImprovingEstimator(BaseEstimator): """Dummy classifier to test the learning curve""" def __init__(self, n_max_train_sizes): self.n_max_train_sizes = n_max_train_sizes self.train_sizes = 0 self.X_subset = None def fit(self, X_subset, y_subset=None): self.X_subset = X_subset self.train_sizes = X_subset.shape[0] return self def predict(self, X): raise NotImplementedError def score(self, X=None, Y=None): # training score becomes worse (2 -> 1), test error better (0 -> 1) if self._is_training_data(X): return 2. - float(self.train_sizes) / self.n_max_train_sizes else: return float(self.train_sizes) / self.n_max_train_sizes def _is_training_data(self, X): return X is self.X_subset class MockIncrementalImprovingEstimator(MockImprovingEstimator): """Dummy classifier that provides partial_fit""" def __init__(self, n_max_train_sizes): super(MockIncrementalImprovingEstimator, self).__init__(n_max_train_sizes) self.x = None def _is_training_data(self, X): return self.x in X def partial_fit(self, X, y=None, **params): self.train_sizes += X.shape[0] self.x = X[0] class MockEstimatorWithParameter(BaseEstimator): """Dummy classifier to test the validation curve""" def __init__(self, param=0.5): self.X_subset = None self.param = param def fit(self, X_subset, y_subset): self.X_subset = X_subset self.train_sizes = X_subset.shape[0] return self def predict(self, X): raise NotImplementedError def score(self, X=None, y=None): return self.param if self._is_training_data(X) else 1 - self.param def _is_training_data(self, X): return X is self.X_subset # XXX: use 2D array, since 1D X is being detected as a single sample in # check_consistent_length X = np.ones((10, 2)) X_sparse = coo_matrix(X) y = np.arange(10) // 2 def test_cross_val_score(): clf = MockClassifier() for a in range(-10, 10): clf.a = a # Smoke test scores = cross_val_score(clf, X, y) assert_array_equal(scores, clf.score(X, y)) # test with multioutput y scores = cross_val_score(clf, X_sparse, X) assert_array_equal(scores, clf.score(X_sparse, X)) scores = cross_val_score(clf, X_sparse, y) assert_array_equal(scores, clf.score(X_sparse, y)) # test with multioutput y scores = cross_val_score(clf, X_sparse, X) assert_array_equal(scores, clf.score(X_sparse, X)) # test with X and y as list list_check = lambda x: isinstance(x, list) clf = CheckingClassifier(check_X=list_check) scores = cross_val_score(clf, X.tolist(), y.tolist()) clf = CheckingClassifier(check_y=list_check) scores = cross_val_score(clf, X, y.tolist()) assert_raises(ValueError, cross_val_score, clf, X, y, scoring="sklearn") # test with 3d X and X_3d = X[:, :, np.newaxis] clf = MockClassifier(allow_nd=True) scores = cross_val_score(clf, X_3d, y) clf = MockClassifier(allow_nd=False) assert_raises(ValueError, cross_val_score, clf, X_3d, y) def test_cross_val_score_predict_labels(): # Check if ValueError (when labels is None) propagates to cross_val_score # and cross_val_predict # And also check if labels is correctly passed to the cv object X, y = make_classification(n_samples=20, n_classes=2, random_state=0) clf = SVC(kernel="linear") label_cvs = [LeaveOneLabelOut(), LeavePLabelOut(2), LabelKFold(), LabelShuffleSplit()] for cv in label_cvs: assert_raise_message(ValueError, "The labels parameter should not be None", cross_val_score, estimator=clf, X=X, y=y, cv=cv) assert_raise_message(ValueError, "The labels parameter should not be None", cross_val_predict, estimator=clf, X=X, y=y, cv=cv) def test_cross_val_score_pandas(): # check cross_val_score doesn't destroy pandas dataframe types = [(MockDataFrame, MockDataFrame)] try: from pandas import Series, DataFrame types.append((Series, DataFrame)) except ImportError: pass for TargetType, InputFeatureType in types: # X dataframe, y series X_df, y_ser = InputFeatureType(X), TargetType(y) check_df = lambda x: isinstance(x, InputFeatureType) check_series = lambda x: isinstance(x, TargetType) clf = CheckingClassifier(check_X=check_df, check_y=check_series) cross_val_score(clf, X_df, y_ser) def test_cross_val_score_mask(): # test that cross_val_score works with boolean masks svm = SVC(kernel="linear") iris = load_iris() X, y = iris.data, iris.target kfold = KFold(5) scores_indices = cross_val_score(svm, X, y, cv=kfold) kfold = KFold(5) cv_masks = [] for train, test in kfold.split(X, y): mask_train = np.zeros(len(y), dtype=np.bool) mask_test = np.zeros(len(y), dtype=np.bool) mask_train[train] = 1 mask_test[test] = 1 cv_masks.append((train, test)) scores_masks = cross_val_score(svm, X, y, cv=cv_masks) assert_array_equal(scores_indices, scores_masks) def test_cross_val_score_precomputed(): # test for svm with precomputed kernel svm = SVC(kernel="precomputed") iris = load_iris() X, y = iris.data, iris.target linear_kernel = np.dot(X, X.T) score_precomputed = cross_val_score(svm, linear_kernel, y) svm = SVC(kernel="linear") score_linear = cross_val_score(svm, X, y) assert_array_equal(score_precomputed, score_linear) # Error raised for non-square X svm = SVC(kernel="precomputed") assert_raises(ValueError, cross_val_score, svm, X, y) # test error is raised when the precomputed kernel is not array-like # or sparse assert_raises(ValueError, cross_val_score, svm, linear_kernel.tolist(), y) def test_cross_val_score_fit_params(): clf = MockClassifier() n_samples = X.shape[0] n_classes = len(np.unique(y)) W_sparse = coo_matrix((np.array([1]), (np.array([1]), np.array([0]))), shape=(10, 1)) P_sparse = coo_matrix(np.eye(5)) DUMMY_INT = 42 DUMMY_STR = '42' DUMMY_OBJ = object() def assert_fit_params(clf): # Function to test that the values are passed correctly to the # classifier arguments for non-array type assert_equal(clf.dummy_int, DUMMY_INT) assert_equal(clf.dummy_str, DUMMY_STR) assert_equal(clf.dummy_obj, DUMMY_OBJ) fit_params = {'sample_weight': np.ones(n_samples), 'class_prior': np.ones(n_classes) / n_classes, 'sparse_sample_weight': W_sparse, 'sparse_param': P_sparse, 'dummy_int': DUMMY_INT, 'dummy_str': DUMMY_STR, 'dummy_obj': DUMMY_OBJ, 'callback': assert_fit_params} cross_val_score(clf, X, y, fit_params=fit_params) def test_cross_val_score_score_func(): clf = MockClassifier() _score_func_args = [] def score_func(y_test, y_predict): _score_func_args.append((y_test, y_predict)) return 1.0 with warnings.catch_warnings(record=True): scoring = make_scorer(score_func) score = cross_val_score(clf, X, y, scoring=scoring) assert_array_equal(score, [1.0, 1.0, 1.0]) assert len(_score_func_args) == 3 def test_cross_val_score_errors(): class BrokenEstimator: pass assert_raises(TypeError, cross_val_score, BrokenEstimator(), X) def test_cross_val_score_with_score_func_classification(): iris = load_iris() clf = SVC(kernel='linear') # Default score (should be the accuracy score) scores = cross_val_score(clf, iris.data, iris.target, cv=5) assert_array_almost_equal(scores, [0.97, 1., 0.97, 0.97, 1.], 2) # Correct classification score (aka. zero / one score) - should be the # same as the default estimator score zo_scores = cross_val_score(clf, iris.data, iris.target, scoring="accuracy", cv=5) assert_array_almost_equal(zo_scores, [0.97, 1., 0.97, 0.97, 1.], 2) # F1 score (class are balanced so f1_score should be equal to zero/one # score f1_scores = cross_val_score(clf, iris.data, iris.target, scoring="f1_weighted", cv=5) assert_array_almost_equal(f1_scores, [0.97, 1., 0.97, 0.97, 1.], 2) def test_cross_val_score_with_score_func_regression(): X, y = make_regression(n_samples=30, n_features=20, n_informative=5, random_state=0) reg = Ridge() # Default score of the Ridge regression estimator scores = cross_val_score(reg, X, y, cv=5) assert_array_almost_equal(scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) # R2 score (aka. determination coefficient) - should be the # same as the default estimator score r2_scores = cross_val_score(reg, X, y, scoring="r2", cv=5) assert_array_almost_equal(r2_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) # Mean squared error; this is a loss function, so "scores" are negative mse_scores = cross_val_score(reg, X, y, cv=5, scoring="mean_squared_error") expected_mse = np.array([-763.07, -553.16, -274.38, -273.26, -1681.99]) assert_array_almost_equal(mse_scores, expected_mse, 2) # Explained variance scoring = make_scorer(explained_variance_score) ev_scores = cross_val_score(reg, X, y, cv=5, scoring=scoring) assert_array_almost_equal(ev_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) def test_permutation_score(): iris = load_iris() X = iris.data X_sparse = coo_matrix(X) y = iris.target svm = SVC(kernel='linear') cv = StratifiedKFold(2) score, scores, pvalue = permutation_test_score( svm, X, y, n_permutations=30, cv=cv, scoring="accuracy") assert_greater(score, 0.9) assert_almost_equal(pvalue, 0.0, 1) score_label, _, pvalue_label = permutation_test_score( svm, X, y, n_permutations=30, cv=cv, scoring="accuracy", labels=np.ones(y.size), random_state=0) assert_true(score_label == score) assert_true(pvalue_label == pvalue) # check that we obtain the same results with a sparse representation svm_sparse = SVC(kernel='linear') cv_sparse = StratifiedKFold(2) score_label, _, pvalue_label = permutation_test_score( svm_sparse, X_sparse, y, n_permutations=30, cv=cv_sparse, scoring="accuracy", labels=np.ones(y.size), random_state=0) assert_true(score_label == score) assert_true(pvalue_label == pvalue) # test with custom scoring object def custom_score(y_true, y_pred): return (((y_true == y_pred).sum() - (y_true != y_pred).sum()) / y_true.shape[0]) scorer = make_scorer(custom_score) score, _, pvalue = permutation_test_score( svm, X, y, n_permutations=100, scoring=scorer, cv=cv, random_state=0) assert_almost_equal(score, .93, 2) assert_almost_equal(pvalue, 0.01, 3) # set random y y = np.mod(np.arange(len(y)), 3) score, scores, pvalue = permutation_test_score( svm, X, y, n_permutations=30, cv=cv, scoring="accuracy") assert_less(score, 0.5) assert_greater(pvalue, 0.2) def test_permutation_test_score_allow_nans(): # Check that permutation_test_score allows input data with NaNs X = np.arange(200, dtype=np.float64).reshape(10, -1) X[2, :] = np.nan y = np.repeat([0, 1], X.shape[0] / 2) p = Pipeline([ ('imputer', Imputer(strategy='mean', missing_values='NaN')), ('classifier', MockClassifier()), ]) permutation_test_score(p, X, y, cv=5) def test_cross_val_score_allow_nans(): # Check that cross_val_score allows input data with NaNs X = np.arange(200, dtype=np.float64).reshape(10, -1) X[2, :] = np.nan y = np.repeat([0, 1], X.shape[0] / 2) p = Pipeline([ ('imputer', Imputer(strategy='mean', missing_values='NaN')), ('classifier', MockClassifier()), ]) cross_val_score(p, X, y, cv=5) def test_cross_val_score_multilabel(): X = np.array([[-3, 4], [2, 4], [3, 3], [0, 2], [-3, 1], [-2, 1], [0, 0], [-2, -1], [-1, -2], [1, -2]]) y = np.array([[1, 1], [0, 1], [0, 1], [0, 1], [1, 1], [0, 1], [1, 0], [1, 1], [1, 0], [0, 0]]) clf = KNeighborsClassifier(n_neighbors=1) scoring_micro = make_scorer(precision_score, average='micro') scoring_macro = make_scorer(precision_score, average='macro') scoring_samples = make_scorer(precision_score, average='samples') score_micro = cross_val_score(clf, X, y, scoring=scoring_micro, cv=5) score_macro = cross_val_score(clf, X, y, scoring=scoring_macro, cv=5) score_samples = cross_val_score(clf, X, y, scoring=scoring_samples, cv=5) assert_almost_equal(score_micro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 3]) assert_almost_equal(score_macro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4]) assert_almost_equal(score_samples, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4]) def test_cross_val_predict(): boston = load_boston() X, y = boston.data, boston.target cv = KFold() est = Ridge() # Naive loop (should be same as cross_val_predict): preds2 = np.zeros_like(y) for train, test in cv.split(X, y): est.fit(X[train], y[train]) preds2[test] = est.predict(X[test]) preds = cross_val_predict(est, X, y, cv=cv) assert_array_almost_equal(preds, preds2) preds = cross_val_predict(est, X, y) assert_equal(len(preds), len(y)) cv = LeaveOneOut() preds = cross_val_predict(est, X, y, cv=cv) assert_equal(len(preds), len(y)) Xsp = X.copy() Xsp *= (Xsp > np.median(Xsp)) Xsp = coo_matrix(Xsp) preds = cross_val_predict(est, Xsp, y) assert_array_almost_equal(len(preds), len(y)) preds = cross_val_predict(KMeans(), X) assert_equal(len(preds), len(y)) class BadCV(): def split(self, X, y=None, labels=None): for i in range(4): yield np.array([0, 1, 2, 3]), np.array([4, 5, 6, 7, 8]) assert_raises(ValueError, cross_val_predict, est, X, y, cv=BadCV()) def test_cross_val_predict_input_types(): clf = Ridge() # Smoke test predictions = cross_val_predict(clf, X, y) assert_equal(predictions.shape, (10,)) # test with multioutput y predictions = cross_val_predict(clf, X_sparse, X) assert_equal(predictions.shape, (10, 2)) predictions = cross_val_predict(clf, X_sparse, y) assert_array_equal(predictions.shape, (10,)) # test with multioutput y predictions = cross_val_predict(clf, X_sparse, X) assert_array_equal(predictions.shape, (10, 2)) # test with X and y as list list_check = lambda x: isinstance(x, list) clf = CheckingClassifier(check_X=list_check) predictions = cross_val_predict(clf, X.tolist(), y.tolist()) clf = CheckingClassifier(check_y=list_check) predictions = cross_val_predict(clf, X, y.tolist()) # test with 3d X and X_3d = X[:, :, np.newaxis] check_3d = lambda x: x.ndim == 3 clf = CheckingClassifier(check_X=check_3d) predictions = cross_val_predict(clf, X_3d, y) assert_array_equal(predictions.shape, (10,)) def test_cross_val_predict_pandas(): # check cross_val_score doesn't destroy pandas dataframe types = [(MockDataFrame, MockDataFrame)] try: from pandas import Series, DataFrame types.append((Series, DataFrame)) except ImportError: pass for TargetType, InputFeatureType in types: # X dataframe, y series X_df, y_ser = InputFeatureType(X), TargetType(y) check_df = lambda x: isinstance(x, InputFeatureType) check_series = lambda x: isinstance(x, TargetType) clf = CheckingClassifier(check_X=check_df, check_y=check_series) cross_val_predict(clf, X_df, y_ser) def test_cross_val_score_sparse_fit_params(): iris = load_iris() X, y = iris.data, iris.target clf = MockClassifier() fit_params = {'sparse_sample_weight': coo_matrix(np.eye(X.shape[0]))} a = cross_val_score(clf, X, y, fit_params=fit_params) assert_array_equal(a, np.ones(3)) def test_learning_curve(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) with warnings.catch_warnings(record=True) as w: train_sizes, train_scores, test_scores = learning_curve( estimator, X, y, cv=3, train_sizes=np.linspace(0.1, 1.0, 10)) if len(w) > 0: raise RuntimeError("Unexpected warning: %r" % w[0].message) assert_equal(train_scores.shape, (10, 3)) assert_equal(test_scores.shape, (10, 3)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_unsupervised(): X, _ = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y=None, cv=3, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_verbose(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) old_stdout = sys.stdout sys.stdout = StringIO() try: train_sizes, train_scores, test_scores = \ learning_curve(estimator, X, y, cv=3, verbose=1) finally: out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = old_stdout assert("[learning_curve]" in out) def test_learning_curve_incremental_learning_not_possible(): X, y = make_classification(n_samples=2, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) # The mockup does not have partial_fit() estimator = MockImprovingEstimator(1) assert_raises(ValueError, learning_curve, estimator, X, y, exploit_incremental_learning=True) def test_learning_curve_incremental_learning(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockIncrementalImprovingEstimator(20) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y, cv=3, exploit_incremental_learning=True, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_incremental_learning_unsupervised(): X, _ = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockIncrementalImprovingEstimator(20) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y=None, cv=3, exploit_incremental_learning=True, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_batch_and_incremental_learning_are_equal(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) train_sizes = np.linspace(0.2, 1.0, 5) estimator = PassiveAggressiveClassifier(n_iter=1, shuffle=False) train_sizes_inc, train_scores_inc, test_scores_inc = \ learning_curve( estimator, X, y, train_sizes=train_sizes, cv=3, exploit_incremental_learning=True) train_sizes_batch, train_scores_batch, test_scores_batch = \ learning_curve( estimator, X, y, cv=3, train_sizes=train_sizes, exploit_incremental_learning=False) assert_array_equal(train_sizes_inc, train_sizes_batch) assert_array_almost_equal(train_scores_inc.mean(axis=1), train_scores_batch.mean(axis=1)) assert_array_almost_equal(test_scores_inc.mean(axis=1), test_scores_batch.mean(axis=1)) def test_learning_curve_n_sample_range_out_of_bounds(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0, 1]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0.0, 1.0]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0.1, 1.1]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0, 20]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[1, 21]) def test_learning_curve_remove_duplicate_sample_sizes(): X, y = make_classification(n_samples=3, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(2) train_sizes, _, _ = assert_warns( RuntimeWarning, learning_curve, estimator, X, y, cv=3, train_sizes=np.linspace(0.33, 1.0, 3)) assert_array_equal(train_sizes, [1, 2]) def test_learning_curve_with_boolean_indices(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) cv = KFold(n_folds=3) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y, cv=cv, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_validation_curve(): X, y = make_classification(n_samples=2, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) param_range = np.linspace(0, 1, 10) with warnings.catch_warnings(record=True) as w: train_scores, test_scores = validation_curve( MockEstimatorWithParameter(), X, y, param_name="param", param_range=param_range, cv=2 ) if len(w) > 0: raise RuntimeError("Unexpected warning: %r" % w[0].message) assert_array_almost_equal(train_scores.mean(axis=1), param_range) assert_array_almost_equal(test_scores.mean(axis=1), 1 - param_range) def test_check_is_permutation(): p = np.arange(100) assert_true(_check_is_permutation(p, 100)) assert_false(_check_is_permutation(np.delete(p, 23), 100)) p[0] = 23 assert_false(_check_is_permutation(p, 100)) def test_cross_val_predict_sparse_prediction(): # check that cross_val_predict gives same result for sparse and dense input X, y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=False, return_indicator=True, random_state=1) X_sparse = csr_matrix(X) y_sparse = csr_matrix(y) classif = OneVsRestClassifier(SVC(kernel='linear')) preds = cross_val_predict(classif, X, y, cv=10) preds_sparse = cross_val_predict(classif, X_sparse, y_sparse, cv=10) preds_sparse = preds_sparse.toarray() assert_array_almost_equal(preds_sparse, preds)
bsd-3-clause
abbeymiles/aima-python
submissions/Blue/myNN.py
10
3071
from sklearn import datasets from sklearn.neural_network import MLPClassifier import traceback from submissions.Blue import music class DataFrame: data = [] feature_names = [] target = [] target_names = [] musicATRB = DataFrame() musicATRB.data = [] targetData = [] ''' Extract data from the CORGIS Music Library. Most 'hit' songs average 48-52 bars and no more than ~3 minutes (180 seconds)... ''' allSongs = music.get_songs() for song in allSongs: try: length = float(song['song']["duration"]) targetData.append(length) genre = song['artist']['terms'] #String title = song['song']['title'] #String # release = float(song['song']['Release']) musicATRB.data.append([genre, title]) except: traceback.print_exc() musicATRB.feature_names = [ 'Genre', 'Title', 'Release', 'Length', ] musicATRB.target = [] def musicTarget(release): if (song['song']['duration'] <= 210 ): #if the song is less that 3.5 minutes (210 seconds) long return 1 return 0 for i in targetData: tt = musicTarget(i) musicATRB.target.append(tt) musicATRB.target_names = [ 'Not a hit song', 'Could be a hit song', ] Examples = { 'Music': musicATRB, } ''' Make a customn classifier, ''' mlpc = MLPClassifier( hidden_layer_sizes = (100,), activation = 'relu', solver='sgd', # 'adam', alpha = 0.0001, # batch_size='auto', learning_rate = 'adaptive', # 'constant', # power_t = 0.5, max_iter = 1000, # 200, shuffle = True, # random_state = None, # tol = 1e-4, # verbose = False, # warm_start = False, # momentum = 0.9, # nesterovs_momentum = True, # early_stopping = False, # validation_fraction = 0.1, # beta_1 = 0.9, # beta_2 = 0.999, # epsilon = 1e-8, ) ''' Try scaling the data. ''' musicScaled = DataFrame() def setupScales(grid): global min, max min = list(grid[0]) max = list(grid[0]) for row in range(1, len(grid)): for col in range(len(grid[row])): cell = grid[row][col] if cell < min[col]: min[col] = cell if cell > max[col]: max[col] = cell def scaleGrid(grid): newGrid = [] for row in range(len(grid)): newRow = [] for col in range(len(grid[row])): try: cell = grid[row][col] scaled = (cell - min[col]) \ / (max[col] - min[col]) newRow.append(scaled) except: pass newGrid.append(newRow) return newGrid setupScales(musicATRB.data) musicScaled.data = scaleGrid(musicATRB.data) musicScaled.feature_names = musicATRB.feature_names musicScaled.target = musicATRB.target musicScaled.target_names = musicATRB.target_names Examples = { 'musicDefault': { 'frame': musicATRB, }, 'MusicSGD': { 'frame': musicATRB, 'mlpc': mlpc }, 'MusisScaled': { 'frame': musicScaled, }, }
mit
manuelli/director
src/python/director/planplayback.py
1
7857
import os import vtkAll as vtk import math import time import re import numpy as np from director.timercallback import TimerCallback from director import objectmodel as om from director.simpletimer import SimpleTimer from director.utime import getUtime from director import robotstate import copy import pickle import scipy.interpolate def asRobotPlan(msg): ''' If the given message is a robot_plan_with_supports_t then this function returns the plan message contained within it. For any other message type, this function just returns its input argument. ''' try: import drc as lcmdrc except ImportError: pass else: if isinstance(msg, lcmdrc.robot_plan_with_supports_t): return msg.plan return msg class PlanPlayback(object): def __init__(self): self.animationCallback = None self.animationTimer = None self.interpolationMethod = 'slinear' self.playbackSpeed = 1.0 self.jointNameRegex = '' @staticmethod def getPlanPoses(msgOrList): if isinstance(msgOrList, list): messages = msgOrList allPoseTimes, allPoses = PlanPlayback.getPlanPoses(messages[0]) for msg in messages[1:]: poseTimes, poses = PlanPlayback.getPlanPoses(msg) poseTimes += allPoseTimes[-1] allPoseTimes = np.hstack((allPoseTimes, poseTimes[1:])) allPoses += poses[1:] return allPoseTimes, allPoses else: msg = asRobotPlan(msgOrList) poses = [] poseTimes = [] for plan in msg.plan: pose = robotstate.convertStateMessageToDrakePose(plan) poseTimes.append(plan.utime / 1e6) poses.append(pose) return np.array(poseTimes), poses @staticmethod def getPlanElapsedTime(msg): msg = asRobotPlan(msg) startTime = msg.plan[0].utime endTime = msg.plan[-1].utime return (endTime - startTime) / 1e6 @staticmethod def mergePlanMessages(plans): msg = copy.deepcopy(plans[0]) for plan in plans[1:]: plan = copy.deepcopy(plan) lastTime = msg.plan[-1].utime for state in plan.plan: state.utime += lastTime msg.plan_info += plan.plan_info msg.plan += plan.plan msg.num_states = len(msg.plan) return msg @staticmethod def isPlanInfoFeasible(info): return 0 <= info < 10 @staticmethod def isPlanFeasible(plan): plan = asRobotPlan(plan) return plan is not None and (max(plan.plan_info) < 10 and min(plan.plan_info) >= 0) def stopAnimation(self): if self.animationTimer: self.animationTimer.stop() def setInterpolationMethod(method): self.interpolationMethod = method def playPlan(self, msg, jointController): self.playPlans([msg], jointController) def playPlans(self, messages, jointController): assert len(messages) poseTimes, poses = self.getPlanPoses(messages) self.playPoses(poseTimes, poses, jointController) def getPoseInterpolatorFromPlan(self, message): poseTimes, poses = self.getPlanPoses(message) return self.getPoseInterpolator(poseTimes, poses) def getPoseInterpolator(self, poseTimes, poses, unwrap_rpy=True): if unwrap_rpy: poses = np.array(poses, copy=True) poses[:,3:6] = np.unwrap(poses[:,3:6],axis=0) if self.interpolationMethod in ['slinear', 'quadratic', 'cubic']: f = scipy.interpolate.interp1d(poseTimes, poses, axis=0, kind=self.interpolationMethod) elif self.interpolationMethod == 'pchip': f = scipy.interpolate.PchipInterpolator(poseTimes, poses, axis=0) return f def getPlanPoseMeshes(self, messages, jointController, robotModel, numberOfSamples): poseTimes, poses = self.getPlanPoses(messages) f = self.getPoseInterpolator(poseTimes, poses) sampleTimes = np.linspace(poseTimes[0], poseTimes[-1], numberOfSamples) meshes = [] for sampleTime in sampleTimes: pose = f(sampleTime) jointController.setPose('plan_playback', pose) polyData = vtk.vtkPolyData() robotModel.model.getModelMesh(polyData) meshes.append(polyData) return meshes def showPoseAtTime(self, time, jointController, poseInterpolator): pose = poseInterpolator(time) jointController.setPose('plan_playback', pose) def playPoses(self, poseTimes, poses, jointController): f = self.getPoseInterpolator(poseTimes, poses) timer = SimpleTimer() def updateAnimation(): tNow = timer.elapsed() * self.playbackSpeed if tNow > poseTimes[-1]: pose = poses[-1] jointController.setPose('plan_playback', pose) if self.animationCallback: self.animationCallback() return False pose = f(tNow) jointController.setPose('plan_playback', pose) if self.animationCallback: self.animationCallback() self.animationTimer = TimerCallback() self.animationTimer.targetFps = 60 self.animationTimer.callback = updateAnimation self.animationTimer.start() updateAnimation() def picklePlan(self, filename, msg): poseTimes, poses = self.getPlanPoses(msg) pickle.dump((poseTimes, poses), open(filename, 'w')) def getMovingJointNames(self, msg): poseTimes, poses = self.getPlanPoses(msg) diffs = np.diff(poses, axis=0) jointIds = np.unique(np.where(diffs != 0.0)[1]) jointNames = [robotstate.getDrakePoseJointNames()[jointId] for jointId in jointIds] return jointNames def plotPlan(self, msg): poseTimes, poses = self.getPlanPoses(msg) self.plotPoses(poseTimes, poses) def plotPoses(self, poseTimes, poses): import matplotlib.pyplot as plt poses = np.array(poses) if self.jointNameRegex: jointIds = range(poses.shape[1]) else: diffs = np.diff(poses, axis=0) jointIds = np.unique(np.where(diffs != 0.0)[1]) jointNames = [robotstate.getDrakePoseJointNames()[jointId] for jointId in jointIds] jointTrajectories = [poses[:,jointId] for jointId in jointIds] seriesNames = [] sampleResolutionInSeconds = 0.01 numberOfSamples = (poseTimes[-1] - poseTimes[0]) / sampleResolutionInSeconds xnew = np.linspace(poseTimes[0], poseTimes[-1], numberOfSamples) fig = plt.figure() ax = fig.add_subplot(111) for jointId, jointName, jointTrajectory in zip(jointIds, jointNames, jointTrajectories): if self.jointNameRegex and not re.match(self.jointNameRegex, jointName): continue x = poseTimes y = jointTrajectory y = np.rad2deg(y) if self.interpolationMethod in ['slinear', 'quadratic', 'cubic']: f = scipy.interpolate.interp1d(x, y, kind=self.interpolationMethod) elif self.interpolationMethod == 'pchip': f = scipy.interpolate.PchipInterpolator(x, y) ax.plot(x, y, 'ko') seriesNames.append(jointName + ' points') ax.plot(xnew, f(xnew), '-') seriesNames.append(jointName + ' ' + self.interpolationMethod) ax.legend(seriesNames, loc='upper right').draggable() ax.set_xlabel('time (s)') ax.set_ylabel('joint angle (deg)') ax.set_title('joint trajectories') plt.show()
bsd-3-clause
pianomania/scikit-learn
sklearn/linear_model/stochastic_gradient.py
16
50617
# Authors: Peter Prettenhofer <peter.prettenhofer@gmail.com> (main author) # Mathieu Blondel (partial_fit support) # # License: BSD 3 clause """Classification and regression using Stochastic Gradient Descent (SGD).""" import numpy as np from abc import ABCMeta, abstractmethod from ..externals.joblib import Parallel, delayed from .base import LinearClassifierMixin, SparseCoefMixin from .base import make_dataset from ..base import BaseEstimator, RegressorMixin from ..utils import check_array, check_random_state, check_X_y from ..utils.extmath import safe_sparse_dot from ..utils.multiclass import _check_partial_fit_first_call from ..utils.validation import check_is_fitted from ..externals import six from .sgd_fast import plain_sgd, average_sgd from ..utils.fixes import astype from ..utils import compute_class_weight from ..utils import deprecated from .sgd_fast import Hinge from .sgd_fast import SquaredHinge from .sgd_fast import Log from .sgd_fast import ModifiedHuber from .sgd_fast import SquaredLoss from .sgd_fast import Huber from .sgd_fast import EpsilonInsensitive from .sgd_fast import SquaredEpsilonInsensitive LEARNING_RATE_TYPES = {"constant": 1, "optimal": 2, "invscaling": 3, "pa1": 4, "pa2": 5} PENALTY_TYPES = {"none": 0, "l2": 2, "l1": 1, "elasticnet": 3} DEFAULT_EPSILON = 0.1 # Default value of ``epsilon`` parameter. class BaseSGD(six.with_metaclass(ABCMeta, BaseEstimator, SparseCoefMixin)): """Base class for SGD classification and regression.""" def __init__(self, loss, penalty='l2', alpha=0.0001, C=1.0, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=0.1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, warm_start=False, average=False): self.loss = loss self.penalty = penalty self.learning_rate = learning_rate self.epsilon = epsilon self.alpha = alpha self.C = C self.l1_ratio = l1_ratio self.fit_intercept = fit_intercept self.n_iter = n_iter self.shuffle = shuffle self.random_state = random_state self.verbose = verbose self.eta0 = eta0 self.power_t = power_t self.warm_start = warm_start self.average = average self._validate_params() def set_params(self, *args, **kwargs): super(BaseSGD, self).set_params(*args, **kwargs) self._validate_params() return self @abstractmethod def fit(self, X, y): """Fit model.""" def _validate_params(self): """Validate input params. """ if not isinstance(self.shuffle, bool): raise ValueError("shuffle must be either True or False") if self.n_iter <= 0: raise ValueError("n_iter must be > zero") if not (0.0 <= self.l1_ratio <= 1.0): raise ValueError("l1_ratio must be in [0, 1]") if self.alpha < 0.0: raise ValueError("alpha must be >= 0") if self.learning_rate in ("constant", "invscaling"): if self.eta0 <= 0.0: raise ValueError("eta0 must be > 0") if self.learning_rate == "optimal" and self.alpha == 0: raise ValueError("alpha must be > 0 since " "learning_rate is 'optimal'. alpha is used " "to compute the optimal learning rate.") # raises ValueError if not registered self._get_penalty_type(self.penalty) self._get_learning_rate_type(self.learning_rate) if self.loss not in self.loss_functions: raise ValueError("The loss %s is not supported. " % self.loss) def _get_loss_function(self, loss): """Get concrete ``LossFunction`` object for str ``loss``. """ try: loss_ = self.loss_functions[loss] loss_class, args = loss_[0], loss_[1:] if loss in ('huber', 'epsilon_insensitive', 'squared_epsilon_insensitive'): args = (self.epsilon, ) return loss_class(*args) except KeyError: raise ValueError("The loss %s is not supported. " % loss) def _get_learning_rate_type(self, learning_rate): try: return LEARNING_RATE_TYPES[learning_rate] except KeyError: raise ValueError("learning rate %s " "is not supported. " % learning_rate) def _get_penalty_type(self, penalty): penalty = str(penalty).lower() try: return PENALTY_TYPES[penalty] except KeyError: raise ValueError("Penalty %s is not supported. " % penalty) def _validate_sample_weight(self, sample_weight, n_samples): """Set the sample weight array.""" if sample_weight is None: # uniform sample weights sample_weight = np.ones(n_samples, dtype=np.float64, order='C') else: # user-provided array sample_weight = np.asarray(sample_weight, dtype=np.float64, order="C") if sample_weight.shape[0] != n_samples: raise ValueError("Shapes of X and sample_weight do not match.") return sample_weight def _allocate_parameter_mem(self, n_classes, n_features, coef_init=None, intercept_init=None): """Allocate mem for parameters; initialize if provided.""" if n_classes > 2: # allocate coef_ for multi-class if coef_init is not None: coef_init = np.asarray(coef_init, order="C") if coef_init.shape != (n_classes, n_features): raise ValueError("Provided ``coef_`` does not match " "dataset. ") self.coef_ = coef_init else: self.coef_ = np.zeros((n_classes, n_features), dtype=np.float64, order="C") # allocate intercept_ for multi-class if intercept_init is not None: intercept_init = np.asarray(intercept_init, order="C") if intercept_init.shape != (n_classes, ): raise ValueError("Provided intercept_init " "does not match dataset.") self.intercept_ = intercept_init else: self.intercept_ = np.zeros(n_classes, dtype=np.float64, order="C") else: # allocate coef_ for binary problem if coef_init is not None: coef_init = np.asarray(coef_init, dtype=np.float64, order="C") coef_init = coef_init.ravel() if coef_init.shape != (n_features,): raise ValueError("Provided coef_init does not " "match dataset.") self.coef_ = coef_init else: self.coef_ = np.zeros(n_features, dtype=np.float64, order="C") # allocate intercept_ for binary problem if intercept_init is not None: intercept_init = np.asarray(intercept_init, dtype=np.float64) if intercept_init.shape != (1,) and intercept_init.shape != (): raise ValueError("Provided intercept_init " "does not match dataset.") self.intercept_ = intercept_init.reshape(1,) else: self.intercept_ = np.zeros(1, dtype=np.float64, order="C") # initialize average parameters if self.average > 0: self.standard_coef_ = self.coef_ self.standard_intercept_ = self.intercept_ self.average_coef_ = np.zeros(self.coef_.shape, dtype=np.float64, order="C") self.average_intercept_ = np.zeros(self.standard_intercept_.shape, dtype=np.float64, order="C") def _prepare_fit_binary(est, y, i): """Initialization for fit_binary. Returns y, coef, intercept. """ y_i = np.ones(y.shape, dtype=np.float64, order="C") y_i[y != est.classes_[i]] = -1.0 average_intercept = 0 average_coef = None if len(est.classes_) == 2: if not est.average: coef = est.coef_.ravel() intercept = est.intercept_[0] else: coef = est.standard_coef_.ravel() intercept = est.standard_intercept_[0] average_coef = est.average_coef_.ravel() average_intercept = est.average_intercept_[0] else: if not est.average: coef = est.coef_[i] intercept = est.intercept_[i] else: coef = est.standard_coef_[i] intercept = est.standard_intercept_[i] average_coef = est.average_coef_[i] average_intercept = est.average_intercept_[i] return y_i, coef, intercept, average_coef, average_intercept def fit_binary(est, i, X, y, alpha, C, learning_rate, n_iter, pos_weight, neg_weight, sample_weight): """Fit a single binary classifier. The i'th class is considered the "positive" class. """ # if average is not true, average_coef, and average_intercept will be # unused y_i, coef, intercept, average_coef, average_intercept = \ _prepare_fit_binary(est, y, i) assert y_i.shape[0] == y.shape[0] == sample_weight.shape[0] dataset, intercept_decay = make_dataset(X, y_i, sample_weight) penalty_type = est._get_penalty_type(est.penalty) learning_rate_type = est._get_learning_rate_type(learning_rate) # XXX should have random_state_! random_state = check_random_state(est.random_state) # numpy mtrand expects a C long which is a signed 32 bit integer under # Windows seed = random_state.randint(0, np.iinfo(np.int32).max) if not est.average: return plain_sgd(coef, intercept, est.loss_function_, penalty_type, alpha, C, est.l1_ratio, dataset, n_iter, int(est.fit_intercept), int(est.verbose), int(est.shuffle), seed, pos_weight, neg_weight, learning_rate_type, est.eta0, est.power_t, est.t_, intercept_decay) else: standard_coef, standard_intercept, average_coef, \ average_intercept = average_sgd(coef, intercept, average_coef, average_intercept, est.loss_function_, penalty_type, alpha, C, est.l1_ratio, dataset, n_iter, int(est.fit_intercept), int(est.verbose), int(est.shuffle), seed, pos_weight, neg_weight, learning_rate_type, est.eta0, est.power_t, est.t_, intercept_decay, est.average) if len(est.classes_) == 2: est.average_intercept_[0] = average_intercept else: est.average_intercept_[i] = average_intercept return standard_coef, standard_intercept class BaseSGDClassifier(six.with_metaclass(ABCMeta, BaseSGD, LinearClassifierMixin)): loss_functions = { "hinge": (Hinge, 1.0), "squared_hinge": (SquaredHinge, 1.0), "perceptron": (Hinge, 0.0), "log": (Log, ), "modified_huber": (ModifiedHuber, ), "squared_loss": (SquaredLoss, ), "huber": (Huber, DEFAULT_EPSILON), "epsilon_insensitive": (EpsilonInsensitive, DEFAULT_EPSILON), "squared_epsilon_insensitive": (SquaredEpsilonInsensitive, DEFAULT_EPSILON), } @abstractmethod def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, class_weight=None, warm_start=False, average=False): super(BaseSGDClassifier, self).__init__(loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, warm_start=warm_start, average=average) self.class_weight = class_weight self.n_jobs = int(n_jobs) @property @deprecated("Attribute loss_function was deprecated in version 0.19 and " "will be removed in 0.21. Use 'loss_function_' instead") def loss_function(self): return self.loss_function_ def _partial_fit(self, X, y, alpha, C, loss, learning_rate, n_iter, classes, sample_weight, coef_init, intercept_init): X, y = check_X_y(X, y, 'csr', dtype=np.float64, order="C") n_samples, n_features = X.shape self._validate_params() _check_partial_fit_first_call(self, classes) n_classes = self.classes_.shape[0] # Allocate datastructures from input arguments self._expanded_class_weight = compute_class_weight(self.class_weight, self.classes_, y) sample_weight = self._validate_sample_weight(sample_weight, n_samples) if getattr(self, "coef_", None) is None or coef_init is not None: self._allocate_parameter_mem(n_classes, n_features, coef_init, intercept_init) elif n_features != self.coef_.shape[-1]: raise ValueError("Number of features %d does not match previous " "data %d." % (n_features, self.coef_.shape[-1])) self.loss_function_ = self._get_loss_function(loss) if not hasattr(self, "t_"): self.t_ = 1.0 # delegate to concrete training procedure if n_classes > 2: self._fit_multiclass(X, y, alpha=alpha, C=C, learning_rate=learning_rate, sample_weight=sample_weight, n_iter=n_iter) elif n_classes == 2: self._fit_binary(X, y, alpha=alpha, C=C, learning_rate=learning_rate, sample_weight=sample_weight, n_iter=n_iter) else: raise ValueError("The number of class labels must be " "greater than one.") return self def _fit(self, X, y, alpha, C, loss, learning_rate, coef_init=None, intercept_init=None, sample_weight=None): if hasattr(self, "classes_"): self.classes_ = None X, y = check_X_y(X, y, 'csr', dtype=np.float64, order="C") n_samples, n_features = X.shape # labels can be encoded as float, int, or string literals # np.unique sorts in asc order; largest class id is positive class classes = np.unique(y) if self.warm_start and hasattr(self, "coef_"): if coef_init is None: coef_init = self.coef_ if intercept_init is None: intercept_init = self.intercept_ else: self.coef_ = None self.intercept_ = None if self.average > 0: self.standard_coef_ = self.coef_ self.standard_intercept_ = self.intercept_ self.average_coef_ = None self.average_intercept_ = None # Clear iteration count for multiple call to fit. self.t_ = 1.0 self._partial_fit(X, y, alpha, C, loss, learning_rate, self.n_iter, classes, sample_weight, coef_init, intercept_init) return self def _fit_binary(self, X, y, alpha, C, sample_weight, learning_rate, n_iter): """Fit a binary classifier on X and y. """ coef, intercept = fit_binary(self, 1, X, y, alpha, C, learning_rate, n_iter, self._expanded_class_weight[1], self._expanded_class_weight[0], sample_weight) self.t_ += n_iter * X.shape[0] # need to be 2d if self.average > 0: if self.average <= self.t_ - 1: self.coef_ = self.average_coef_.reshape(1, -1) self.intercept_ = self.average_intercept_ else: self.coef_ = self.standard_coef_.reshape(1, -1) self.standard_intercept_ = np.atleast_1d(intercept) self.intercept_ = self.standard_intercept_ else: self.coef_ = coef.reshape(1, -1) # intercept is a float, need to convert it to an array of length 1 self.intercept_ = np.atleast_1d(intercept) def _fit_multiclass(self, X, y, alpha, C, learning_rate, sample_weight, n_iter): """Fit a multi-class classifier by combining binary classifiers Each binary classifier predicts one class versus all others. This strategy is called OVA: One Versus All. """ # Use joblib to fit OvA in parallel. result = Parallel(n_jobs=self.n_jobs, backend="threading", verbose=self.verbose)( delayed(fit_binary)(self, i, X, y, alpha, C, learning_rate, n_iter, self._expanded_class_weight[i], 1., sample_weight) for i in range(len(self.classes_))) for i, (_, intercept) in enumerate(result): self.intercept_[i] = intercept self.t_ += n_iter * X.shape[0] if self.average > 0: if self.average <= self.t_ - 1.0: self.coef_ = self.average_coef_ self.intercept_ = self.average_intercept_ else: self.coef_ = self.standard_coef_ self.standard_intercept_ = np.atleast_1d(self.intercept_) self.intercept_ = self.standard_intercept_ def partial_fit(self, X, y, classes=None, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Subset of the training data y : numpy array, shape (n_samples,) Subset of the target values classes : array, shape (n_classes,) Classes across all calls to partial_fit. Can be obtained by via `np.unique(y_all)`, where y_all is the target vector of the entire dataset. This argument is required for the first call to partial_fit and can be omitted in the subsequent calls. Note that y doesn't need to contain all labels in `classes`. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples. If not provided, uniform weights are assumed. Returns ------- self : returns an instance of self. """ if self.class_weight in ['balanced']: raise ValueError("class_weight '{0}' is not supported for " "partial_fit. In order to use 'balanced' weights," " use compute_class_weight('{0}', classes, y). " "In place of y you can us a large enough sample " "of the full training set target to properly " "estimate the class frequency distributions. " "Pass the resulting weights as the class_weight " "parameter.".format(self.class_weight)) return self._partial_fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, n_iter=1, classes=classes, sample_weight=sample_weight, coef_init=None, intercept_init=None) def fit(self, X, y, coef_init=None, intercept_init=None, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data y : numpy array, shape (n_samples,) Target values coef_init : array, shape (n_classes, n_features) The initial coefficients to warm-start the optimization. intercept_init : array, shape (n_classes,) The initial intercept to warm-start the optimization. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples. If not provided, uniform weights are assumed. These weights will be multiplied with class_weight (passed through the constructor) if class_weight is specified Returns ------- self : returns an instance of self. """ return self._fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, coef_init=coef_init, intercept_init=intercept_init, sample_weight=sample_weight) class SGDClassifier(BaseSGDClassifier): """Linear classifiers (SVM, logistic regression, a.o.) with SGD training. This estimator implements regularized linear models with stochastic gradient descent (SGD) learning: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate). SGD allows minibatch (online/out-of-core) learning, see the partial_fit method. For best results using the default learning rate schedule, the data should have zero mean and unit variance. This implementation works with data represented as dense or sparse arrays of floating point values for the features. The model it fits can be controlled with the loss parameter; by default, it fits a linear support vector machine (SVM). The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection. Read more in the :ref:`User Guide <sgd>`. Parameters ---------- loss : str, 'hinge', 'log', 'modified_huber', 'squared_hinge',\ 'perceptron', or a regression loss: 'squared_loss', 'huber',\ 'epsilon_insensitive', or 'squared_epsilon_insensitive' The loss function to be used. Defaults to 'hinge', which gives a linear SVM. The 'log' loss gives logistic regression, a probabilistic classifier. 'modified_huber' is another smooth loss that brings tolerance to outliers as well as probability estimates. 'squared_hinge' is like hinge but is quadratically penalized. 'perceptron' is the linear loss used by the perceptron algorithm. The other losses are designed for regression but can be useful in classification as well; see SGDRegressor for a description. penalty : str, 'none', 'l2', 'l1', or 'elasticnet' The penalty (aka regularization term) to be used. Defaults to 'l2' which is the standard regularizer for linear SVM models. 'l1' and 'elasticnet' might bring sparsity to the model (feature selection) not achievable with 'l2'. alpha : float Constant that multiplies the regularization term. Defaults to 0.0001 Also used to compute learning_rate when set to 'optimal'. l1_ratio : float The Elastic Net mixing parameter, with 0 <= l1_ratio <= 1. l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1. Defaults to 0.15. fit_intercept : bool Whether the intercept should be estimated or not. If False, the data is assumed to be already centered. Defaults to True. n_iter : int, optional The number of passes over the training data (aka epochs). The number of iterations is set to 1 if using partial_fit. Defaults to 5. shuffle : bool, optional Whether or not the training data should be shuffled after each epoch. Defaults to True. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. verbose : integer, optional The verbosity level epsilon : float Epsilon in the epsilon-insensitive loss functions; only if `loss` is 'huber', 'epsilon_insensitive', or 'squared_epsilon_insensitive'. For 'huber', determines the threshold at which it becomes less important to get the prediction exactly right. For epsilon-insensitive, any differences between the current prediction and the correct label are ignored if they are less than this threshold. n_jobs : integer, optional The number of CPUs to use to do the OVA (One Versus All, for multi-class problems) computation. -1 means 'all CPUs'. Defaults to 1. learning_rate : string, optional The learning rate schedule: - 'constant': eta = eta0 - 'optimal': eta = 1.0 / (alpha * (t + t0)) [default] - 'invscaling': eta = eta0 / pow(t, power_t) where t0 is chosen by a heuristic proposed by Leon Bottou. eta0 : double The initial learning rate for the 'constant' or 'invscaling' schedules. The default value is 0.0 as eta0 is not used by the default schedule 'optimal'. power_t : double The exponent for inverse scaling learning rate [default 0.5]. class_weight : dict, {class_label: weight} or "balanced" or None, optional Preset for the class_weight fit parameter. Weights associated with classes. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` warm_start : bool, optional When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. average : bool or int, optional When set to True, computes the averaged SGD weights and stores the result in the ``coef_`` attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So ``average=10`` will begin averaging after seeing 10 samples. Attributes ---------- coef_ : array, shape (1, n_features) if n_classes == 2 else (n_classes,\ n_features) Weights assigned to the features. intercept_ : array, shape (1,) if n_classes == 2 else (n_classes,) Constants in decision function. loss_function_ : concrete ``LossFunction`` Examples -------- >>> import numpy as np >>> from sklearn import linear_model >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) >>> Y = np.array([1, 1, 2, 2]) >>> clf = linear_model.SGDClassifier() >>> clf.fit(X, Y) ... #doctest: +NORMALIZE_WHITESPACE SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1, eta0=0.0, fit_intercept=True, l1_ratio=0.15, learning_rate='optimal', loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5, random_state=None, shuffle=True, verbose=0, warm_start=False) >>> print(clf.predict([[-0.8, -1]])) [1] See also -------- LinearSVC, LogisticRegression, Perceptron """ def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, class_weight=None, warm_start=False, average=False): super(SGDClassifier, self).__init__( loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, n_jobs=n_jobs, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, class_weight=class_weight, warm_start=warm_start, average=average) def _check_proba(self): check_is_fitted(self, "t_") if self.loss not in ("log", "modified_huber"): raise AttributeError("probability estimates are not available for" " loss=%r" % self.loss) @property def predict_proba(self): """Probability estimates. This method is only available for log loss and modified Huber loss. Multiclass probability estimates are derived from binary (one-vs.-rest) estimates by simple normalization, as recommended by Zadrozny and Elkan. Binary probability estimates for loss="modified_huber" are given by (clip(decision_function(X), -1, 1) + 1) / 2. For other loss functions it is necessary to perform proper probability calibration by wrapping the classifier with :class:`sklearn.calibration.CalibratedClassifierCV` instead. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Returns ------- array, shape (n_samples, n_classes) Returns the probability of the sample for each class in the model, where classes are ordered as they are in `self.classes_`. References ---------- Zadrozny and Elkan, "Transforming classifier scores into multiclass probability estimates", SIGKDD'02, http://www.research.ibm.com/people/z/zadrozny/kdd2002-Transf.pdf The justification for the formula in the loss="modified_huber" case is in the appendix B in: http://jmlr.csail.mit.edu/papers/volume2/zhang02c/zhang02c.pdf """ self._check_proba() return self._predict_proba def _predict_proba(self, X): if self.loss == "log": return self._predict_proba_lr(X) elif self.loss == "modified_huber": binary = (len(self.classes_) == 2) scores = self.decision_function(X) if binary: prob2 = np.ones((scores.shape[0], 2)) prob = prob2[:, 1] else: prob = scores np.clip(scores, -1, 1, prob) prob += 1. prob /= 2. if binary: prob2[:, 0] -= prob prob = prob2 else: # the above might assign zero to all classes, which doesn't # normalize neatly; work around this to produce uniform # probabilities prob_sum = prob.sum(axis=1) all_zero = (prob_sum == 0) if np.any(all_zero): prob[all_zero, :] = 1 prob_sum[all_zero] = len(self.classes_) # normalize prob /= prob_sum.reshape((prob.shape[0], -1)) return prob else: raise NotImplementedError("predict_(log_)proba only supported when" " loss='log' or loss='modified_huber' " "(%r given)" % self.loss) @property def predict_log_proba(self): """Log of probability estimates. This method is only available for log loss and modified Huber loss. When loss="modified_huber", probability estimates may be hard zeros and ones, so taking the logarithm is not possible. See ``predict_proba`` for details. Parameters ---------- X : array-like, shape (n_samples, n_features) Returns ------- T : array-like, shape (n_samples, n_classes) Returns the log-probability of the sample for each class in the model, where classes are ordered as they are in `self.classes_`. """ self._check_proba() return self._predict_log_proba def _predict_log_proba(self, X): return np.log(self.predict_proba(X)) class BaseSGDRegressor(BaseSGD, RegressorMixin): loss_functions = { "squared_loss": (SquaredLoss, ), "huber": (Huber, DEFAULT_EPSILON), "epsilon_insensitive": (EpsilonInsensitive, DEFAULT_EPSILON), "squared_epsilon_insensitive": (SquaredEpsilonInsensitive, DEFAULT_EPSILON), } @abstractmethod def __init__(self, loss="squared_loss", penalty="l2", alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, random_state=None, learning_rate="invscaling", eta0=0.01, power_t=0.25, warm_start=False, average=False): super(BaseSGDRegressor, self).__init__(loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, warm_start=warm_start, average=average) def _partial_fit(self, X, y, alpha, C, loss, learning_rate, n_iter, sample_weight, coef_init, intercept_init): X, y = check_X_y(X, y, "csr", copy=False, order='C', dtype=np.float64) y = astype(y, np.float64, copy=False) n_samples, n_features = X.shape self._validate_params() # Allocate datastructures from input arguments sample_weight = self._validate_sample_weight(sample_weight, n_samples) if getattr(self, "coef_", None) is None: self._allocate_parameter_mem(1, n_features, coef_init, intercept_init) elif n_features != self.coef_.shape[-1]: raise ValueError("Number of features %d does not match previous " "data %d." % (n_features, self.coef_.shape[-1])) if self.average > 0 and getattr(self, "average_coef_", None) is None: self.average_coef_ = np.zeros(n_features, dtype=np.float64, order="C") self.average_intercept_ = np.zeros(1, dtype=np.float64, order="C") self._fit_regressor(X, y, alpha, C, loss, learning_rate, sample_weight, n_iter) return self def partial_fit(self, X, y, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Subset of training data y : numpy array of shape (n_samples,) Subset of target values sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples. If not provided, uniform weights are assumed. Returns ------- self : returns an instance of self. """ return self._partial_fit(X, y, self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, n_iter=1, sample_weight=sample_weight, coef_init=None, intercept_init=None) def _fit(self, X, y, alpha, C, loss, learning_rate, coef_init=None, intercept_init=None, sample_weight=None): if self.warm_start and getattr(self, "coef_", None) is not None: if coef_init is None: coef_init = self.coef_ if intercept_init is None: intercept_init = self.intercept_ else: self.coef_ = None self.intercept_ = None if self.average > 0: self.standard_intercept_ = self.intercept_ self.standard_coef_ = self.coef_ self.average_coef_ = None self.average_intercept_ = None # Clear iteration count for multiple call to fit. self.t_ = 1.0 return self._partial_fit(X, y, alpha, C, loss, learning_rate, self.n_iter, sample_weight, coef_init, intercept_init) def fit(self, X, y, coef_init=None, intercept_init=None, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data y : numpy array, shape (n_samples,) Target values coef_init : array, shape (n_features,) The initial coefficients to warm-start the optimization. intercept_init : array, shape (1,) The initial intercept to warm-start the optimization. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- self : returns an instance of self. """ return self._fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, coef_init=coef_init, intercept_init=intercept_init, sample_weight=sample_weight) def _decision_function(self, X): """Predict using the linear model Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Returns ------- array, shape (n_samples,) Predicted target values per element in X. """ check_is_fitted(self, ["t_", "coef_", "intercept_"], all_or_any=all) X = check_array(X, accept_sparse='csr') scores = safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_ return scores.ravel() def predict(self, X): """Predict using the linear model Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Returns ------- array, shape (n_samples,) Predicted target values per element in X. """ return self._decision_function(X) def _fit_regressor(self, X, y, alpha, C, loss, learning_rate, sample_weight, n_iter): dataset, intercept_decay = make_dataset(X, y, sample_weight) loss_function = self._get_loss_function(loss) penalty_type = self._get_penalty_type(self.penalty) learning_rate_type = self._get_learning_rate_type(learning_rate) if not hasattr(self, "t_"): self.t_ = 1.0 random_state = check_random_state(self.random_state) # numpy mtrand expects a C long which is a signed 32 bit integer under # Windows seed = random_state.randint(0, np.iinfo(np.int32).max) if self.average > 0: self.standard_coef_, self.standard_intercept_, \ self.average_coef_, self.average_intercept_ =\ average_sgd(self.standard_coef_, self.standard_intercept_[0], self.average_coef_, self.average_intercept_[0], loss_function, penalty_type, alpha, C, self.l1_ratio, dataset, n_iter, int(self.fit_intercept), int(self.verbose), int(self.shuffle), seed, 1.0, 1.0, learning_rate_type, self.eta0, self.power_t, self.t_, intercept_decay, self.average) self.average_intercept_ = np.atleast_1d(self.average_intercept_) self.standard_intercept_ = np.atleast_1d(self.standard_intercept_) self.t_ += n_iter * X.shape[0] if self.average <= self.t_ - 1.0: self.coef_ = self.average_coef_ self.intercept_ = self.average_intercept_ else: self.coef_ = self.standard_coef_ self.intercept_ = self.standard_intercept_ else: self.coef_, self.intercept_ = \ plain_sgd(self.coef_, self.intercept_[0], loss_function, penalty_type, alpha, C, self.l1_ratio, dataset, n_iter, int(self.fit_intercept), int(self.verbose), int(self.shuffle), seed, 1.0, 1.0, learning_rate_type, self.eta0, self.power_t, self.t_, intercept_decay) self.t_ += n_iter * X.shape[0] self.intercept_ = np.atleast_1d(self.intercept_) class SGDRegressor(BaseSGDRegressor): """Linear model fitted by minimizing a regularized empirical loss with SGD SGD stands for Stochastic Gradient Descent: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate). The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection. This implementation works with data represented as dense numpy arrays of floating point values for the features. Read more in the :ref:`User Guide <sgd>`. Parameters ---------- loss : str, 'squared_loss', 'huber', 'epsilon_insensitive', \ or 'squared_epsilon_insensitive' The loss function to be used. Defaults to 'squared_loss' which refers to the ordinary least squares fit. 'huber' modifies 'squared_loss' to focus less on getting outliers correct by switching from squared to linear loss past a distance of epsilon. 'epsilon_insensitive' ignores errors less than epsilon and is linear past that; this is the loss function used in SVR. 'squared_epsilon_insensitive' is the same but becomes squared loss past a tolerance of epsilon. penalty : str, 'none', 'l2', 'l1', or 'elasticnet' The penalty (aka regularization term) to be used. Defaults to 'l2' which is the standard regularizer for linear SVM models. 'l1' and 'elasticnet' might bring sparsity to the model (feature selection) not achievable with 'l2'. alpha : float Constant that multiplies the regularization term. Defaults to 0.0001 Also used to compute learning_rate when set to 'optimal'. l1_ratio : float The Elastic Net mixing parameter, with 0 <= l1_ratio <= 1. l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1. Defaults to 0.15. fit_intercept : bool Whether the intercept should be estimated or not. If False, the data is assumed to be already centered. Defaults to True. n_iter : int, optional The number of passes over the training data (aka epochs). The number of iterations is set to 1 if using partial_fit. Defaults to 5. shuffle : bool, optional Whether or not the training data should be shuffled after each epoch. Defaults to True. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. verbose : integer, optional The verbosity level. epsilon : float Epsilon in the epsilon-insensitive loss functions; only if `loss` is 'huber', 'epsilon_insensitive', or 'squared_epsilon_insensitive'. For 'huber', determines the threshold at which it becomes less important to get the prediction exactly right. For epsilon-insensitive, any differences between the current prediction and the correct label are ignored if they are less than this threshold. learning_rate : string, optional The learning rate schedule: - 'constant': eta = eta0 - 'optimal': eta = 1.0 / (alpha * (t + t0)) [default] - 'invscaling': eta = eta0 / pow(t, power_t) where t0 is chosen by a heuristic proposed by Leon Bottou. eta0 : double, optional The initial learning rate [default 0.01]. power_t : double, optional The exponent for inverse scaling learning rate [default 0.25]. warm_start : bool, optional When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. average : bool or int, optional When set to True, computes the averaged SGD weights and stores the result in the ``coef_`` attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So ``average=10`` will begin averaging after seeing 10 samples. Attributes ---------- coef_ : array, shape (n_features,) Weights assigned to the features. intercept_ : array, shape (1,) The intercept term. average_coef_ : array, shape (n_features,) Averaged weights assigned to the features. average_intercept_ : array, shape (1,) The averaged intercept term. Examples -------- >>> import numpy as np >>> from sklearn import linear_model >>> n_samples, n_features = 10, 5 >>> np.random.seed(0) >>> y = np.random.randn(n_samples) >>> X = np.random.randn(n_samples, n_features) >>> clf = linear_model.SGDRegressor() >>> clf.fit(X, y) ... #doctest: +NORMALIZE_WHITESPACE SGDRegressor(alpha=0.0001, average=False, epsilon=0.1, eta0=0.01, fit_intercept=True, l1_ratio=0.15, learning_rate='invscaling', loss='squared_loss', n_iter=5, penalty='l2', power_t=0.25, random_state=None, shuffle=True, verbose=0, warm_start=False) See also -------- Ridge, ElasticNet, Lasso, SVR """ def __init__(self, loss="squared_loss", penalty="l2", alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, random_state=None, learning_rate="invscaling", eta0=0.01, power_t=0.25, warm_start=False, average=False): super(SGDRegressor, self).__init__(loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, warm_start=warm_start, average=average)
bsd-3-clause
brodeau/aerobulk
python/plot_tests/plot_station_asf.py
1
9926
#!/usr/bin/env python # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- # Post-diagnostic of STATION_ASF / L. Brodeau, 2019 import sys from os import path as path #from string import replace import math import numpy as nmp from netCDF4 import Dataset,num2date import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt import matplotlib.dates as mdates reload(sys) sys.setdefaultencoding('utf8') cy1 = '2016' ; # First year cy2 = '2018' ; # Last year jt0 = 0 jt0 = 17519 dir_figs='.' size_fig=(13,7) fig_ext='png' clr_red = '#AD0000' clr_blu = '#3749A3' clr_gre = '#548F64' clr_sat = '#ffed00' clr_mod = '#008ab8' rDPI=200. L_ALGOS = [ 'COARE3p6' , 'ECMWF' , 'NCAR' ] l_xtrns = [ '-noskin' , '-noskin' , '' ] ; # string to add to algo name (L_ALGOS) to get version without skin params turned on l_color = [ '#ffed00' , '#008ab8' , '0.4' ] ; # colors to differentiate algos on the plot l_width = [ 3 , 2 , 1 ] ; # line-width to differentiate algos on the plot l_style = [ '-' , '-' , '--' ] ; # line-style L_VNEM = [ 'qla' , 'qsb' , 'qt' , 'qlw' , 'taum' , 'dt_skin' ] L_VARO = [ 'Qlat' , 'Qsen' , 'Qnet' , 'Qlw' , 'Tau' , 'dT_skin' ] ; # name of variable on figure L_VARL = [ r'$Q_{lat}$', r'$Q_{sens}$' , r'$Q_{net}$' , r'$Q_{lw}$' , r'$|\tau|$' , r'$\Delta T_{skin}$' ] ; # name of variable in latex mode L_VUNT = [ r'$W/m^2$' , r'$W/m^2$' , r'$W/m^2$' , r'$W/m^2$' , r'$N/m^2$' , 'K' ] L_VMAX = [ 75. , 75. , 800. , 25. , 1.2 , -0.7 ] L_VMIN = [ -250. , -125. , -400. , -150. , 0. , 0.7 ] L_ANOM = [ True , True , True , True , True , False ] #L_VNEM = [ 'qlw' ] #L_VARO = [ 'Qlw' ] ; # name of variable on figure #L_VARL = [ r'$Q_{lw}$' ] ; # name of variable in latex mode #L_VUNT = [ r'$W/m^2$' ] #L_VMAX = [ 25. ] #L_VMIN = [ -150. ] #L_ANOM = [ True ] nb_algos = len(L_ALGOS) ; print(nb_algos) # Getting arguments: narg = len(sys.argv) if narg != 2: print 'Usage: '+sys.argv[0]+' <DIR_OUT_SASF>'; sys.exit(0) cdir_data = sys.argv[1] # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Populating and checking existence of files to be read # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> def chck4f(cf): cmesg = 'ERROR: File '+cf+' does not exist !!!' if not path.exists(cf): print cmesg ; sys.exit(0) ###cf_in = nmp.empty((), dtype="S10") cf_in = [] ; cf_in_ns = [] for ja in range(nb_algos): cfi = cdir_data+'/output/'+'STATION_ASF-'+L_ALGOS[ja]+'_1h_'+cy1+'0101_'+cy2+'1231_gridT.nc' chck4f(cfi) cf_in.append(cfi) # Same but without skin params: for ja in range(nb_algos): cfi = cdir_data+'/output/'+'STATION_ASF-'+L_ALGOS[ja]+l_xtrns[ja]+'_1h_'+cy1+'0101_'+cy2+'1231_gridT.nc' chck4f(cfi) cf_in_ns.append(cfi) print('Files we are goin to use:') for ja in range(nb_algos): print(cf_in[ja]) print(' --- same without cool-skin/warm-layer:') for ja in range(nb_algos): print(cf_in_ns[ja]) #----------------------------------------------------------------- # Getting time array from the first file: id_in = Dataset(cf_in[0]) vt = id_in.variables['time_counter'][jt0:] cunit_t = id_in.variables['time_counter'].units clndr_t = id_in.variables['time_counter'].calendar id_in.close() Nt = len(vt) print(' "time" => units = '+cunit_t+', calendar = "'+clndr_t+'"') vtime = num2date(vt, units=cunit_t) ; # something understandable! ii=Nt/300 ib=max(ii-ii%10,1) xticks_d=int(30*ib) font_inf = { 'fontname':'Open Sans', 'fontweight':'normal', 'fontsize':14 } nb_var = len(L_VNEM) xF = nmp.zeros((Nt,nb_algos)) xFa = nmp.zeros((Nt,nb_algos)) for ctest in ['skin','noskin']: for jv in range(nb_var): print('\n *** Treating variable: '+L_VARO[jv]+' ('+ctest+') !') for ja in range(nb_algos): # if ctest == 'skin': id_in = Dataset(cf_in[ja]) if ctest == 'noskin': id_in = Dataset(cf_in_ns[ja]) xF[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name id_in.close() fig = plt.figure(num = jv, figsize=size_fig, facecolor='w', edgecolor='k') ax1 = plt.axes([0.07, 0.22, 0.9, 0.75]) ax1.set_xticks(vtime[::xticks_d]) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) plt.xticks(rotation='60') for ja in range(nb_algos): plt.plot(vtime, xF[:,ja], '-', color=l_color[ja], linestyle=l_style[ja], linewidth=l_width[ja], label=L_ALGOS[ja], zorder=10+ja) ax1.set_ylim(L_VMIN[jv], L_VMAX[jv]) ; ax1.set_xlim(vtime[0],vtime[Nt-1]) plt.ylabel(L_VARL[jv]+' ['+L_VUNT[jv]+']') ax1.grid(color='k', linestyle='-', linewidth=0.3) plt.legend(bbox_to_anchor=(0.45, 0.2), ncol=1, shadow=True, fancybox=True) ax1.annotate(cvar_lnm+' ('+ctest+')', xy=(0.3, 0.97), xycoords='axes fraction', bbox={'facecolor':'w', 'alpha':1., 'pad':10}, zorder=50, **font_inf) plt.savefig(L_VARO[jv]+'_'+ctest+'.'+fig_ext, dpi=int(rDPI), transparent=False) plt.close(jv) if L_ANOM[jv]: for ja in range(nb_algos): xFa[:,ja] = xF[:,ja] - nmp.mean(xF,axis=1) if nmp.sum(xFa[:,:]) == 0.0: print(' Well! Seems that for variable '+L_VARO[jv]+', choice of algo has no impact a all!') print(' ==> skipping anomaly plot...') else: # Want a symetric y-range that makes sense for the anomaly we're looking at: rmax = nmp.max(xFa) ; rmin = nmp.min(xFa) rmax = max( abs(rmax) , abs(rmin) ) romagn = math.floor(math.log(rmax, 10)) ; # order of magnitude of the anomaly we're dealing with rmlt = 10.**(int(romagn)) / 2. yrng = math.copysign( math.ceil(abs(rmax)/rmlt)*rmlt , rmax) #print 'yrng = ', yrng ; #sys.exit(0) fig = plt.figure(num = 10+jv, figsize=size_fig, facecolor='w', edgecolor='k') ax1 = plt.axes([0.07, 0.22, 0.9, 0.75]) ax1.set_xticks(vtime[::xticks_d]) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) plt.xticks(rotation='60') for ja in range(nb_algos): plt.plot(vtime, xFa[:,ja], '-', color=l_color[ja], linewidth=l_width[ja], label=L_ALGOS[ja], zorder=10+ja) ax1.set_ylim(-yrng,yrng) ; ax1.set_xlim(vtime[0],vtime[Nt-1]) plt.ylabel(L_VARL[jv]+' ['+L_VUNT[jv]+']') ax1.grid(color='k', linestyle='-', linewidth=0.3) plt.legend(bbox_to_anchor=(0.45, 0.2), ncol=1, shadow=True, fancybox=True) ax1.annotate('Anomaly of '+cvar_lnm+' ('+ctest+')', xy=(0.3, 0.97), xycoords='axes fraction', bbox={'facecolor':'w', 'alpha':1., 'pad':10}, zorder=50, **font_inf) plt.savefig(L_VARO[jv]+'_'+ctest+'_anomaly.'+fig_ext, dpi=int(rDPI), transparent=False) plt.close(10+jv) # Difference skin vs noskin: xFns = nmp.zeros((Nt,nb_algos)) for jv in range(nb_var-1): print('\n *** Treating variable: '+L_VARO[jv]+' ('+ctest+') !') for ja in range(nb_algos-1): id_in = Dataset(cf_in[ja]) xF[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name id_in.close() # id_in = Dataset(cf_in_ns[ja]) xFns[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name id_in.close() xFa[:,ja] = xF[:,ja] - xFns[:,ja] ; # difference! # Want a symetric y-range that makes sense for the anomaly we're looking at: rmax = nmp.max(xFa) ; rmin = nmp.min(xFa) rmax = max( abs(rmax) , abs(rmin) ) romagn = math.floor(math.log(rmax, 10)) ; # order of magnitude of the anomaly we're dealing with rmlt = 10.**(int(romagn)) / 2. yrng = math.copysign( math.ceil(abs(rmax)/rmlt)*rmlt , rmax) print 'yrng = ', yrng ; #sys.exit(0) for ja in range(nb_algos-1): calgo = L_ALGOS[ja] if nmp.sum(xFa[:,ja]) == 0.0: print(' Well! Seems that for variable '+L_VARO[jv]+', and algo '+calgo+', skin param has no impact') print(' ==> skipping difference plot...') else: fig = plt.figure(num = jv, figsize=size_fig, facecolor='w', edgecolor='k') ax1 = plt.axes([0.07, 0.22, 0.9, 0.75]) ax1.set_xticks(vtime[::xticks_d]) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) plt.xticks(rotation='60') plt.plot(vtime, xFa[:,ja], '-', color=l_color[ja], linestyle=l_style[ja], linewidth=l_width[ja], label=None, zorder=10+ja) ax1.set_ylim(-yrng,yrng) ; ax1.set_xlim(vtime[0],vtime[Nt-1]) plt.ylabel(L_VARL[jv]+' ['+L_VUNT[jv]+']') ax1.grid(color='k', linestyle='-', linewidth=0.3) #plt.legend(bbox_to_anchor=(0.45, 0.2), ncol=1, shadow=True, fancybox=True) ax1.annotate(cvar_lnm+' ('+ctest+')', xy=(0.3, 0.97), xycoords='axes fraction', bbox={'facecolor':'w', 'alpha':1., 'pad':10}, zorder=50, **font_inf) plt.savefig('diff_skin-noskin_'+L_VARO[jv]+'_'+calgo+'_'+ctest+'.'+fig_ext, dpi=int(rDPI), transparent=False) plt.close(jv)
gpl-3.0
ZENGXH/scikit-learn
examples/bicluster/plot_spectral_biclustering.py
403
2011
""" ============================================= A demo of the Spectral Biclustering algorithm ============================================= This example demonstrates how to generate a checkerboard dataset and bicluster it using the Spectral Biclustering algorithm. The data is generated with the ``make_checkerboard`` function, then shuffled and passed to the Spectral Biclustering algorithm. The rows and columns of the shuffled matrix are rearranged to show the biclusters found by the algorithm. The outer product of the row and column label vectors shows a representation of the checkerboard structure. """ print(__doc__) # Author: Kemal Eren <kemal@kemaleren.com> # License: BSD 3 clause import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import make_checkerboard from sklearn.datasets import samples_generator as sg from sklearn.cluster.bicluster import SpectralBiclustering from sklearn.metrics import consensus_score n_clusters = (4, 3) data, rows, columns = make_checkerboard( shape=(300, 300), n_clusters=n_clusters, noise=10, shuffle=False, random_state=0) plt.matshow(data, cmap=plt.cm.Blues) plt.title("Original dataset") data, row_idx, col_idx = sg._shuffle(data, random_state=0) plt.matshow(data, cmap=plt.cm.Blues) plt.title("Shuffled dataset") model = SpectralBiclustering(n_clusters=n_clusters, method='log', random_state=0) model.fit(data) score = consensus_score(model.biclusters_, (rows[:, row_idx], columns[:, col_idx])) print("consensus score: {:.1f}".format(score)) fit_data = data[np.argsort(model.row_labels_)] fit_data = fit_data[:, np.argsort(model.column_labels_)] plt.matshow(fit_data, cmap=plt.cm.Blues) plt.title("After biclustering; rearranged to show biclusters") plt.matshow(np.outer(np.sort(model.row_labels_) + 1, np.sort(model.column_labels_) + 1), cmap=plt.cm.Blues) plt.title("Checkerboard structure of rearranged data") plt.show()
bsd-3-clause
cwu2011/scikit-learn
sklearn/preprocessing/__init__.py
14
1184
""" The :mod:`sklearn.preprocessing` module includes scaling, centering, normalization, binarization and imputation methods. """ from .data import Binarizer from .data import KernelCenterer from .data import MinMaxScaler from .data import MaxAbsScaler from .data import Normalizer from .data import RobustScaler from .data import StandardScaler from .data import add_dummy_feature from .data import binarize from .data import normalize from .data import scale from .data import robust_scale from .data import maxabs_scale from .data import OneHotEncoder from .data import PolynomialFeatures from .label import label_binarize from .label import LabelBinarizer from .label import LabelEncoder from .label import MultiLabelBinarizer from .imputation import Imputer __all__ = [ 'Binarizer', 'Imputer', 'KernelCenterer', 'LabelBinarizer', 'LabelEncoder', 'MultiLabelBinarizer', 'MinMaxScaler', 'MaxAbsScaler', 'Normalizer', 'OneHotEncoder', 'RobustScaler', 'StandardScaler', 'add_dummy_feature', 'PolynomialFeatures', 'binarize', 'normalize', 'scale', 'robust_scale', 'maxabs_scale', 'label_binarize', ]
bsd-3-clause
cuttlefishh/emp
code/01-metadata/metadata_template_generator.py
1
4911
#!/usr/bin/env python import click import pandas as pd import re # Hard-coded variables investigation_type = 'metagenome' # Function: return dataframe of environmental package-specific metadata items # A single environmental package (soil) or list can be provided (soil,water). def show_items_of_env_pkg(df_env_pkg, list_of_env_pkg): """Return dataframe of environmental package-specific metadata items""" df_items = df_env_pkg[df_env_pkg['Environmental package'].isin(list_of_env_pkg)] return df_items # Function: return dataframe of metadata template def create_template_for_env_pkg(df_QiitaEBI, df_MIMS, df_env_pkg, list_of_env_pkg, number_of_samples, sample_prefix): """Return dataframe of metadata template""" # get headers/requirement/example of Qiita-EBI/MIMS/env_pkg columns pkg_items = show_items_of_env_pkg(df_env_pkg, list_of_env_pkg) headers_env_pkg = pkg_items['Structured comment name'].values require_env_pkg = pkg_items['Requirement'] example_env_pkg = pkg_items['Value syntax'] headers_all = list(df_QiitaEBI.iloc[0]) + list(df_MIMS.iloc[0]) + list(headers_env_pkg) require_all = pd.concat([df_QiitaEBI.iloc[1], df_MIMS.iloc[1], require_env_pkg]) example_all = pd.concat([df_QiitaEBI.iloc[2], df_MIMS.iloc[2], example_env_pkg]) # populate template dataframe df_template = pd.DataFrame(columns=headers_all, dtype=object) df_template.loc['Requirement'] = require_all.values df_template.loc['Format'] = example_all.values string_of_env_pkg = re.sub(r'\W', '.', '.'.join(list_of_env_pkg)) for i in range(0, number_of_samples): df_template.loc[i+1] = ['' for x in range(len(df_template.columns))] df_template.loc[i+1]['sample_name'] = '%s.%s.%s' % (sample_prefix, string_of_env_pkg, i+1) df_template.loc[i+1]['investigation_type'] = investigation_type df_template.loc[i+1]['env_package'] = ' or '.join(list_of_env_pkg) return df_template @click.command() @click.option('--qiita_ebi_mims_path', required=True, type=click.Path(resolve_path=True, readable=True, exists=True), help='Excel file with Qiita/EBI and MIMS required fields. Example: Qiita_EBI_MIMS_v1.xlsx') @click.option('--migs_mims_path', required=True, type=click.Path(resolve_path=True, readable=True, exists=True), help='Excel file with MIxS standards. Example: MIGS_MIMS_v4.xls') @click.option('--list_of_env_pkg', required=True, type=click.STRING, help="One (recommended) or more (separated by commas) environmental package. Choose from: air, built environment, host-associated, human-associated, human-skin, human-oral, human-gut, human-vaginal, microbial mat/biofilm, misc environment, plant-associated, sediment, soil, wastewater/sludge, water") @click.option('--number_of_samples', required=True, type=click.INT, help='Number of samples (per environmental package) to create rows for in the template') @click.option('--sample_prefix', required=True, type=click.STRING, help='Prefix string to prepend to sample numbers in row indexes. Example: Metcalf40 (EMP500 PI and study number)') # Main function: generate metadata template and readme csv files def generate_metadata_template(qiita_ebi_mims_path, migs_mims_path, list_of_env_pkg, number_of_samples, sample_prefix): """Generate metadata template and readme csv files""" # Qiita/EBI/MIMS Excel file to DataFrames df_QiitaEBI = pd.read_excel(qiita_ebi_mims_path, sheetname='QiitaEBI', header=None) df_MIMS = pd.read_excel(qiita_ebi_mims_path, sheetname='MIMS', header=None) list_of_env_pkg = list_of_env_pkg.split(",") # MIGS/MIMS Excel file to DataFrames df_README = pd.read_excel(migs_mims_path, sheetname='README', header=None) df_MIGS_MIMS = pd.read_excel(migs_mims_path, sheetname='MIGS_MIMS', header=0, index_col=0) df_env_pkg = pd.read_excel(migs_mims_path, sheetname='environmental_packages', header=0) # generate template file df_template = create_template_for_env_pkg(df_QiitaEBI, df_MIMS, df_env_pkg, list_of_env_pkg, number_of_samples, sample_prefix) string_of_env_pkg = re.sub(r'\W', '_', '_'.join(list_of_env_pkg)) df_template.to_csv('%s_%s_%s_samples.csv' % (sample_prefix, string_of_env_pkg, number_of_samples), index_label='index') # generate info file df_MIMS_select = df_MIGS_MIMS[df_MIGS_MIMS.Section.isin(['investigation', 'environment', 'migs/mims/mimarks extension'])] df_MIMS_select.to_csv('README_MIMS_metadata.csv') df_env_pkg_select = show_items_of_env_pkg(df_env_pkg, list_of_env_pkg) del df_env_pkg_select['Environmental package'] df_env_pkg_select.set_index('Structured comment name', inplace=True) string_of_env_pkg = re.sub(r'\W', '_', '_'.join(list_of_env_pkg)) df_env_pkg_select.to_csv('README_%s_metadata.csv' % string_of_env_pkg) # Execute main function if __name__ == '__main__': generate_metadata_template()
bsd-3-clause
NixaSoftware/CVis
venv/lib/python2.7/site-packages/pandas/tests/frame/test_replace.py
15
43479
# -*- coding: utf-8 -*- from __future__ import print_function import pytest from datetime import datetime import re from pandas.compat import (zip, range, lrange, StringIO) from pandas import (DataFrame, Series, Index, date_range, compat, Timestamp) import pandas as pd from numpy import nan import numpy as np from pandas.util.testing import (assert_series_equal, assert_frame_equal) import pandas.util.testing as tm from pandas.tests.frame.common import TestData class TestDataFrameReplace(TestData): def test_replace_inplace(self): self.tsframe['A'][:5] = nan self.tsframe['A'][-5:] = nan tsframe = self.tsframe.copy() tsframe.replace(nan, 0, inplace=True) assert_frame_equal(tsframe, self.tsframe.fillna(0)) pytest.raises(TypeError, self.tsframe.replace, nan, inplace=True) pytest.raises(TypeError, self.tsframe.replace, nan) # mixed type mf = self.mixed_frame mf.iloc[5:20, mf.columns.get_loc('foo')] = nan mf.iloc[-10:, mf.columns.get_loc('A')] = nan result = self.mixed_frame.replace(np.nan, 0) expected = self.mixed_frame.fillna(value=0) assert_frame_equal(result, expected) tsframe = self.tsframe.copy() tsframe.replace([nan], [0], inplace=True) assert_frame_equal(tsframe, self.tsframe.fillna(0)) def test_regex_replace_scalar(self): obj = {'a': list('ab..'), 'b': list('efgh')} dfobj = DataFrame(obj) mix = {'a': lrange(4), 'b': list('ab..')} dfmix = DataFrame(mix) # simplest cases # regex -> value # obj frame res = dfobj.replace(r'\s*\.\s*', nan, regex=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.replace(r'\s*\.\s*', nan, regex=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex # obj frame res = dfobj.replace(r'\s*(\.)\s*', r'\1\1\1', regex=True) objc = obj.copy() objc['a'] = ['a', 'b', '...', '...'] expec = DataFrame(objc) assert_frame_equal(res, expec) # with mixed res = dfmix.replace(r'\s*(\.)\s*', r'\1\1\1', regex=True) mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) # everything with compiled regexs as well res = dfobj.replace(re.compile(r'\s*\.\s*'), nan, regex=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.replace(re.compile(r'\s*\.\s*'), nan, regex=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex # obj frame res = dfobj.replace(re.compile(r'\s*(\.)\s*'), r'\1\1\1') objc = obj.copy() objc['a'] = ['a', 'b', '...', '...'] expec = DataFrame(objc) assert_frame_equal(res, expec) # with mixed res = dfmix.replace(re.compile(r'\s*(\.)\s*'), r'\1\1\1') mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) res = dfmix.replace(regex=re.compile(r'\s*(\.)\s*'), value=r'\1\1\1') mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) res = dfmix.replace(regex=r'\s*(\.)\s*', value=r'\1\1\1') mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) def test_regex_replace_scalar_inplace(self): obj = {'a': list('ab..'), 'b': list('efgh')} dfobj = DataFrame(obj) mix = {'a': lrange(4), 'b': list('ab..')} dfmix = DataFrame(mix) # simplest cases # regex -> value # obj frame res = dfobj.copy() res.replace(r'\s*\.\s*', nan, regex=True, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() res.replace(r'\s*\.\s*', nan, regex=True, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex # obj frame res = dfobj.copy() res.replace(r'\s*(\.)\s*', r'\1\1\1', regex=True, inplace=True) objc = obj.copy() objc['a'] = ['a', 'b', '...', '...'] expec = DataFrame(objc) assert_frame_equal(res, expec) # with mixed res = dfmix.copy() res.replace(r'\s*(\.)\s*', r'\1\1\1', regex=True, inplace=True) mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) # everything with compiled regexs as well res = dfobj.copy() res.replace(re.compile(r'\s*\.\s*'), nan, regex=True, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() res.replace(re.compile(r'\s*\.\s*'), nan, regex=True, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex # obj frame res = dfobj.copy() res.replace(re.compile(r'\s*(\.)\s*'), r'\1\1\1', regex=True, inplace=True) objc = obj.copy() objc['a'] = ['a', 'b', '...', '...'] expec = DataFrame(objc) assert_frame_equal(res, expec) # with mixed res = dfmix.copy() res.replace(re.compile(r'\s*(\.)\s*'), r'\1\1\1', regex=True, inplace=True) mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) res = dfobj.copy() res.replace(regex=r'\s*\.\s*', value=nan, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() res.replace(regex=r'\s*\.\s*', value=nan, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex # obj frame res = dfobj.copy() res.replace(regex=r'\s*(\.)\s*', value=r'\1\1\1', inplace=True) objc = obj.copy() objc['a'] = ['a', 'b', '...', '...'] expec = DataFrame(objc) assert_frame_equal(res, expec) # with mixed res = dfmix.copy() res.replace(regex=r'\s*(\.)\s*', value=r'\1\1\1', inplace=True) mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) # everything with compiled regexs as well res = dfobj.copy() res.replace(regex=re.compile(r'\s*\.\s*'), value=nan, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() res.replace(regex=re.compile(r'\s*\.\s*'), value=nan, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex # obj frame res = dfobj.copy() res.replace(regex=re.compile(r'\s*(\.)\s*'), value=r'\1\1\1', inplace=True) objc = obj.copy() objc['a'] = ['a', 'b', '...', '...'] expec = DataFrame(objc) assert_frame_equal(res, expec) # with mixed res = dfmix.copy() res.replace(regex=re.compile(r'\s*(\.)\s*'), value=r'\1\1\1', inplace=True) mixc = mix.copy() mixc['b'] = ['a', 'b', '...', '...'] expec = DataFrame(mixc) assert_frame_equal(res, expec) def test_regex_replace_list_obj(self): obj = {'a': list('ab..'), 'b': list('efgh'), 'c': list('helo')} dfobj = DataFrame(obj) # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'e|f|g'] values = [nan, 'crap'] res = dfobj.replace(to_replace_res, values, regex=True) expec = DataFrame({'a': ['a', 'b', nan, nan], 'b': ['crap'] * 3 + ['h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [re1, re2, .., reN] to_replace_res = [r'\s*(\.)\s*', r'(e|f|g)'] values = [r'\1\1', r'\1_crap'] res = dfobj.replace(to_replace_res, values, regex=True) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['e_crap', 'f_crap', 'g_crap', 'h'], 'c': ['h', 'e_crap', 'l', 'o']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [(re1 or v1), (re2 or v2), ..., (reN # or vN)] to_replace_res = [r'\s*(\.)\s*', r'e'] values = [r'\1\1', r'crap'] res = dfobj.replace(to_replace_res, values, regex=True) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['crap', 'f', 'g', 'h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) to_replace_res = [r'\s*(\.)\s*', r'e'] values = [r'\1\1', r'crap'] res = dfobj.replace(value=values, regex=to_replace_res) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['crap', 'f', 'g', 'h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) def test_regex_replace_list_obj_inplace(self): # same as above with inplace=True # lists of regexes and values obj = {'a': list('ab..'), 'b': list('efgh'), 'c': list('helo')} dfobj = DataFrame(obj) # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'e|f|g'] values = [nan, 'crap'] res = dfobj.copy() res.replace(to_replace_res, values, inplace=True, regex=True) expec = DataFrame({'a': ['a', 'b', nan, nan], 'b': ['crap'] * 3 + ['h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [re1, re2, .., reN] to_replace_res = [r'\s*(\.)\s*', r'(e|f|g)'] values = [r'\1\1', r'\1_crap'] res = dfobj.copy() res.replace(to_replace_res, values, inplace=True, regex=True) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['e_crap', 'f_crap', 'g_crap', 'h'], 'c': ['h', 'e_crap', 'l', 'o']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [(re1 or v1), (re2 or v2), ..., (reN # or vN)] to_replace_res = [r'\s*(\.)\s*', r'e'] values = [r'\1\1', r'crap'] res = dfobj.copy() res.replace(to_replace_res, values, inplace=True, regex=True) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['crap', 'f', 'g', 'h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) to_replace_res = [r'\s*(\.)\s*', r'e'] values = [r'\1\1', r'crap'] res = dfobj.copy() res.replace(value=values, regex=to_replace_res, inplace=True) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['crap', 'f', 'g', 'h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) def test_regex_replace_list_mixed(self): # mixed frame to make sure this doesn't break things mix = {'a': lrange(4), 'b': list('ab..')} dfmix = DataFrame(mix) # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'a'] values = [nan, 'crap'] mix2 = {'a': lrange(4), 'b': list('ab..'), 'c': list('halo')} dfmix2 = DataFrame(mix2) res = dfmix2.replace(to_replace_res, values, regex=True) expec = DataFrame({'a': mix2['a'], 'b': ['crap', 'b', nan, nan], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [re1, re2, .., reN] to_replace_res = [r'\s*(\.)\s*', r'(a|b)'] values = [r'\1\1', r'\1_crap'] res = dfmix.replace(to_replace_res, values, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['a_crap', 'b_crap', '..', '..']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [(re1 or v1), (re2 or v2), ..., (reN # or vN)] to_replace_res = [r'\s*(\.)\s*', r'a', r'(b)'] values = [r'\1\1', r'crap', r'\1_crap'] res = dfmix.replace(to_replace_res, values, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b_crap', '..', '..']}) assert_frame_equal(res, expec) to_replace_res = [r'\s*(\.)\s*', r'a', r'(b)'] values = [r'\1\1', r'crap', r'\1_crap'] res = dfmix.replace(regex=to_replace_res, value=values) expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b_crap', '..', '..']}) assert_frame_equal(res, expec) def test_regex_replace_list_mixed_inplace(self): mix = {'a': lrange(4), 'b': list('ab..')} dfmix = DataFrame(mix) # the same inplace # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'a'] values = [nan, 'crap'] res = dfmix.copy() res.replace(to_replace_res, values, inplace=True, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b', nan, nan]}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [re1, re2, .., reN] to_replace_res = [r'\s*(\.)\s*', r'(a|b)'] values = [r'\1\1', r'\1_crap'] res = dfmix.copy() res.replace(to_replace_res, values, inplace=True, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['a_crap', 'b_crap', '..', '..']}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [(re1 or v1), (re2 or v2), ..., (reN # or vN)] to_replace_res = [r'\s*(\.)\s*', r'a', r'(b)'] values = [r'\1\1', r'crap', r'\1_crap'] res = dfmix.copy() res.replace(to_replace_res, values, inplace=True, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b_crap', '..', '..']}) assert_frame_equal(res, expec) to_replace_res = [r'\s*(\.)\s*', r'a', r'(b)'] values = [r'\1\1', r'crap', r'\1_crap'] res = dfmix.copy() res.replace(regex=to_replace_res, value=values, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b_crap', '..', '..']}) assert_frame_equal(res, expec) def test_regex_replace_dict_mixed(self): mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} dfmix = DataFrame(mix) # dicts # single dict {re1: v1}, search the whole frame # need test for this... # list of dicts {re1: v1, re2: v2, ..., re3: v3}, search the whole # frame res = dfmix.replace({'b': r'\s*\.\s*'}, {'b': nan}, regex=True) res2 = dfmix.copy() res2.replace({'b': r'\s*\.\s*'}, {'b': nan}, inplace=True, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) # list of dicts {re1: re11, re2: re12, ..., reN: re1N}, search the # whole frame res = dfmix.replace({'b': r'\s*(\.)\s*'}, {'b': r'\1ty'}, regex=True) res2 = dfmix.copy() res2.replace({'b': r'\s*(\.)\s*'}, {'b': r'\1ty'}, inplace=True, regex=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', '.ty', '.ty'], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) res = dfmix.replace(regex={'b': r'\s*(\.)\s*'}, value={'b': r'\1ty'}) res2 = dfmix.copy() res2.replace(regex={'b': r'\s*(\.)\s*'}, value={'b': r'\1ty'}, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', '.ty', '.ty'], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) # scalar -> dict # to_replace regex, {value: value} expec = DataFrame({'a': mix['a'], 'b': [nan, 'b', '.', '.'], 'c': mix['c']}) res = dfmix.replace('a', {'b': nan}, regex=True) res2 = dfmix.copy() res2.replace('a', {'b': nan}, regex=True, inplace=True) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) res = dfmix.replace('a', {'b': nan}, regex=True) res2 = dfmix.copy() res2.replace(regex='a', value={'b': nan}, inplace=True) expec = DataFrame({'a': mix['a'], 'b': [nan, 'b', '.', '.'], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) def test_regex_replace_dict_nested(self): # nested dicts will not work until this is implemented for Series mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} dfmix = DataFrame(mix) res = dfmix.replace({'b': {r'\s*\.\s*': nan}}, regex=True) res2 = dfmix.copy() res4 = dfmix.copy() res2.replace({'b': {r'\s*\.\s*': nan}}, inplace=True, regex=True) res3 = dfmix.replace(regex={'b': {r'\s*\.\s*': nan}}) res4.replace(regex={'b': {r'\s*\.\s*': nan}}, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) assert_frame_equal(res4, expec) def test_regex_replace_dict_nested_gh4115(self): df = pd.DataFrame({'Type': ['Q', 'T', 'Q', 'Q', 'T'], 'tmp': 2}) expected = DataFrame({'Type': [0, 1, 0, 0, 1], 'tmp': 2}) result = df.replace({'Type': {'Q': 0, 'T': 1}}) assert_frame_equal(result, expected) def test_regex_replace_list_to_scalar(self): mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} df = DataFrame(mix) expec = DataFrame({'a': mix['a'], 'b': np.array([nan] * 4), 'c': [nan, nan, nan, 'd']}) res = df.replace([r'\s*\.\s*', 'a|b'], nan, regex=True) res2 = df.copy() res3 = df.copy() res2.replace([r'\s*\.\s*', 'a|b'], nan, regex=True, inplace=True) res3.replace(regex=[r'\s*\.\s*', 'a|b'], value=nan, inplace=True) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_str_to_numeric(self): # what happens when you try to replace a numeric value with a regex? mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} df = DataFrame(mix) res = df.replace(r'\s*\.\s*', 0, regex=True) res2 = df.copy() res2.replace(r'\s*\.\s*', 0, inplace=True, regex=True) res3 = df.copy() res3.replace(regex=r'\s*\.\s*', value=0, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', 0, 0], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_regex_list_to_numeric(self): mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} df = DataFrame(mix) res = df.replace([r'\s*\.\s*', 'b'], 0, regex=True) res2 = df.copy() res2.replace([r'\s*\.\s*', 'b'], 0, regex=True, inplace=True) res3 = df.copy() res3.replace(regex=[r'\s*\.\s*', 'b'], value=0, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 0, 0, 0], 'c': ['a', 0, nan, 'd']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_series_of_regexes(self): mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} df = DataFrame(mix) s1 = Series({'b': r'\s*\.\s*'}) s2 = Series({'b': nan}) res = df.replace(s1, s2, regex=True) res2 = df.copy() res2.replace(s1, s2, inplace=True, regex=True) res3 = df.copy() res3.replace(regex=s1, value=s2, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_numeric_to_object_conversion(self): mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} df = DataFrame(mix) expec = DataFrame({'a': ['a', 1, 2, 3], 'b': mix['b'], 'c': mix['c']}) res = df.replace(0, 'a') assert_frame_equal(res, expec) assert res.a.dtype == np.object_ def test_replace_regex_metachar(self): metachars = '[]', '()', r'\d', r'\w', r'\s' for metachar in metachars: df = DataFrame({'a': [metachar, 'else']}) result = df.replace({'a': {metachar: 'paren'}}) expected = DataFrame({'a': ['paren', 'else']}) assert_frame_equal(result, expected) def test_replace(self): self.tsframe['A'][:5] = nan self.tsframe['A'][-5:] = nan zero_filled = self.tsframe.replace(nan, -1e8) assert_frame_equal(zero_filled, self.tsframe.fillna(-1e8)) assert_frame_equal(zero_filled.replace(-1e8, nan), self.tsframe) self.tsframe['A'][:5] = nan self.tsframe['A'][-5:] = nan self.tsframe['B'][:5] = -1e8 # empty df = DataFrame(index=['a', 'b']) assert_frame_equal(df, df.replace(5, 7)) # GH 11698 # test for mixed data types. df = pd.DataFrame([('-', pd.to_datetime('20150101')), ('a', pd.to_datetime('20150102'))]) df1 = df.replace('-', np.nan) expected_df = pd.DataFrame([(np.nan, pd.to_datetime('20150101')), ('a', pd.to_datetime('20150102'))]) assert_frame_equal(df1, expected_df) def test_replace_list(self): obj = {'a': list('ab..'), 'b': list('efgh'), 'c': list('helo')} dfobj = DataFrame(obj) # lists of regexes and values # list of [v1, v2, ..., vN] -> [v1, v2, ..., vN] to_replace_res = [r'.', r'e'] values = [nan, 'crap'] res = dfobj.replace(to_replace_res, values) expec = DataFrame({'a': ['a', 'b', nan, nan], 'b': ['crap', 'f', 'g', 'h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) # list of [v1, v2, ..., vN] -> [v1, v2, .., vN] to_replace_res = [r'.', r'f'] values = [r'..', r'crap'] res = dfobj.replace(to_replace_res, values) expec = DataFrame({'a': ['a', 'b', '..', '..'], 'b': ['e', 'crap', 'g', 'h'], 'c': ['h', 'e', 'l', 'o']}) assert_frame_equal(res, expec) def test_replace_series_dict(self): # from GH 3064 df = DataFrame({'zero': {'a': 0.0, 'b': 1}, 'one': {'a': 2.0, 'b': 0}}) result = df.replace(0, {'zero': 0.5, 'one': 1.0}) expected = DataFrame( {'zero': {'a': 0.5, 'b': 1}, 'one': {'a': 2.0, 'b': 1.0}}) assert_frame_equal(result, expected) result = df.replace(0, df.mean()) assert_frame_equal(result, expected) # series to series/dict df = DataFrame({'zero': {'a': 0.0, 'b': 1}, 'one': {'a': 2.0, 'b': 0}}) s = Series({'zero': 0.0, 'one': 2.0}) result = df.replace(s, {'zero': 0.5, 'one': 1.0}) expected = DataFrame( {'zero': {'a': 0.5, 'b': 1}, 'one': {'a': 1.0, 'b': 0.0}}) assert_frame_equal(result, expected) result = df.replace(s, df.mean()) assert_frame_equal(result, expected) def test_replace_convert(self): # gh 3907 df = DataFrame([['foo', 'bar', 'bah'], ['bar', 'foo', 'bah']]) m = {'foo': 1, 'bar': 2, 'bah': 3} rep = df.replace(m) expec = Series([np.int64] * 3) res = rep.dtypes assert_series_equal(expec, res) def test_replace_mixed(self): mf = self.mixed_frame mf.iloc[5:20, mf.columns.get_loc('foo')] = nan mf.iloc[-10:, mf.columns.get_loc('A')] = nan result = self.mixed_frame.replace(np.nan, -18) expected = self.mixed_frame.fillna(value=-18) assert_frame_equal(result, expected) assert_frame_equal(result.replace(-18, nan), self.mixed_frame) result = self.mixed_frame.replace(np.nan, -1e8) expected = self.mixed_frame.fillna(value=-1e8) assert_frame_equal(result, expected) assert_frame_equal(result.replace(-1e8, nan), self.mixed_frame) # int block upcasting df = DataFrame({'A': Series([1.0, 2.0], dtype='float64'), 'B': Series([0, 1], dtype='int64')}) expected = DataFrame({'A': Series([1.0, 2.0], dtype='float64'), 'B': Series([0.5, 1], dtype='float64')}) result = df.replace(0, 0.5) assert_frame_equal(result, expected) df.replace(0, 0.5, inplace=True) assert_frame_equal(df, expected) # int block splitting df = DataFrame({'A': Series([1.0, 2.0], dtype='float64'), 'B': Series([0, 1], dtype='int64'), 'C': Series([1, 2], dtype='int64')}) expected = DataFrame({'A': Series([1.0, 2.0], dtype='float64'), 'B': Series([0.5, 1], dtype='float64'), 'C': Series([1, 2], dtype='int64')}) result = df.replace(0, 0.5) assert_frame_equal(result, expected) # to object block upcasting df = DataFrame({'A': Series([1.0, 2.0], dtype='float64'), 'B': Series([0, 1], dtype='int64')}) expected = DataFrame({'A': Series([1, 'foo'], dtype='object'), 'B': Series([0, 1], dtype='int64')}) result = df.replace(2, 'foo') assert_frame_equal(result, expected) expected = DataFrame({'A': Series(['foo', 'bar'], dtype='object'), 'B': Series([0, 'foo'], dtype='object')}) result = df.replace([1, 2], ['foo', 'bar']) assert_frame_equal(result, expected) # test case from df = DataFrame({'A': Series([3, 0], dtype='int64'), 'B': Series([0, 3], dtype='int64')}) result = df.replace(3, df.mean().to_dict()) expected = df.copy().astype('float64') m = df.mean() expected.iloc[0, 0] = m[0] expected.iloc[1, 1] = m[1] assert_frame_equal(result, expected) def test_replace_simple_nested_dict(self): df = DataFrame({'col': range(1, 5)}) expected = DataFrame({'col': ['a', 2, 3, 'b']}) result = df.replace({'col': {1: 'a', 4: 'b'}}) assert_frame_equal(expected, result) # in this case, should be the same as the not nested version result = df.replace({1: 'a', 4: 'b'}) assert_frame_equal(expected, result) def test_replace_simple_nested_dict_with_nonexistent_value(self): df = DataFrame({'col': range(1, 5)}) expected = DataFrame({'col': ['a', 2, 3, 'b']}) result = df.replace({-1: '-', 1: 'a', 4: 'b'}) assert_frame_equal(expected, result) result = df.replace({'col': {-1: '-', 1: 'a', 4: 'b'}}) assert_frame_equal(expected, result) def test_replace_value_is_none(self): pytest.raises(TypeError, self.tsframe.replace, nan) orig_value = self.tsframe.iloc[0, 0] orig2 = self.tsframe.iloc[1, 0] self.tsframe.iloc[0, 0] = nan self.tsframe.iloc[1, 0] = 1 result = self.tsframe.replace(to_replace={nan: 0}) expected = self.tsframe.T.replace(to_replace={nan: 0}).T assert_frame_equal(result, expected) result = self.tsframe.replace(to_replace={nan: 0, 1: -1e8}) tsframe = self.tsframe.copy() tsframe.iloc[0, 0] = 0 tsframe.iloc[1, 0] = -1e8 expected = tsframe assert_frame_equal(expected, result) self.tsframe.iloc[0, 0] = orig_value self.tsframe.iloc[1, 0] = orig2 def test_replace_for_new_dtypes(self): # dtypes tsframe = self.tsframe.copy().astype(np.float32) tsframe['A'][:5] = nan tsframe['A'][-5:] = nan zero_filled = tsframe.replace(nan, -1e8) assert_frame_equal(zero_filled, tsframe.fillna(-1e8)) assert_frame_equal(zero_filled.replace(-1e8, nan), tsframe) tsframe['A'][:5] = nan tsframe['A'][-5:] = nan tsframe['B'][:5] = -1e8 b = tsframe['B'] b[b == -1e8] = nan tsframe['B'] = b result = tsframe.fillna(method='bfill') assert_frame_equal(result, tsframe.fillna(method='bfill')) def test_replace_dtypes(self): # int df = DataFrame({'ints': [1, 2, 3]}) result = df.replace(1, 0) expected = DataFrame({'ints': [0, 2, 3]}) assert_frame_equal(result, expected) df = DataFrame({'ints': [1, 2, 3]}, dtype=np.int32) result = df.replace(1, 0) expected = DataFrame({'ints': [0, 2, 3]}, dtype=np.int32) assert_frame_equal(result, expected) df = DataFrame({'ints': [1, 2, 3]}, dtype=np.int16) result = df.replace(1, 0) expected = DataFrame({'ints': [0, 2, 3]}, dtype=np.int16) assert_frame_equal(result, expected) # bools df = DataFrame({'bools': [True, False, True]}) result = df.replace(False, True) assert result.values.all() # complex blocks df = DataFrame({'complex': [1j, 2j, 3j]}) result = df.replace(1j, 0j) expected = DataFrame({'complex': [0j, 2j, 3j]}) assert_frame_equal(result, expected) # datetime blocks prev = datetime.today() now = datetime.today() df = DataFrame({'datetime64': Index([prev, now, prev])}) result = df.replace(prev, now) expected = DataFrame({'datetime64': Index([now] * 3)}) assert_frame_equal(result, expected) def test_replace_input_formats_listlike(self): # both dicts to_rep = {'A': np.nan, 'B': 0, 'C': ''} values = {'A': 0, 'B': -1, 'C': 'missing'} df = DataFrame({'A': [np.nan, 0, np.inf], 'B': [0, 2, 5], 'C': ['', 'asdf', 'fd']}) filled = df.replace(to_rep, values) expected = {} for k, v in compat.iteritems(df): expected[k] = v.replace(to_rep[k], values[k]) assert_frame_equal(filled, DataFrame(expected)) result = df.replace([0, 2, 5], [5, 2, 0]) expected = DataFrame({'A': [np.nan, 5, np.inf], 'B': [5, 2, 0], 'C': ['', 'asdf', 'fd']}) assert_frame_equal(result, expected) # scalar to dict values = {'A': 0, 'B': -1, 'C': 'missing'} df = DataFrame({'A': [np.nan, 0, np.nan], 'B': [0, 2, 5], 'C': ['', 'asdf', 'fd']}) filled = df.replace(np.nan, values) expected = {} for k, v in compat.iteritems(df): expected[k] = v.replace(np.nan, values[k]) assert_frame_equal(filled, DataFrame(expected)) # list to list to_rep = [np.nan, 0, ''] values = [-2, -1, 'missing'] result = df.replace(to_rep, values) expected = df.copy() for i in range(len(to_rep)): expected.replace(to_rep[i], values[i], inplace=True) assert_frame_equal(result, expected) pytest.raises(ValueError, df.replace, to_rep, values[1:]) def test_replace_input_formats_scalar(self): df = DataFrame({'A': [np.nan, 0, np.inf], 'B': [0, 2, 5], 'C': ['', 'asdf', 'fd']}) # dict to scalar to_rep = {'A': np.nan, 'B': 0, 'C': ''} filled = df.replace(to_rep, 0) expected = {} for k, v in compat.iteritems(df): expected[k] = v.replace(to_rep[k], 0) assert_frame_equal(filled, DataFrame(expected)) pytest.raises(TypeError, df.replace, to_rep, [np.nan, 0, '']) # list to scalar to_rep = [np.nan, 0, ''] result = df.replace(to_rep, -1) expected = df.copy() for i in range(len(to_rep)): expected.replace(to_rep[i], -1, inplace=True) assert_frame_equal(result, expected) def test_replace_limit(self): pass def test_replace_dict_no_regex(self): answer = Series({0: 'Strongly Agree', 1: 'Agree', 2: 'Neutral', 3: 'Disagree', 4: 'Strongly Disagree'}) weights = {'Agree': 4, 'Disagree': 2, 'Neutral': 3, 'Strongly Agree': 5, 'Strongly Disagree': 1} expected = Series({0: 5, 1: 4, 2: 3, 3: 2, 4: 1}) result = answer.replace(weights) assert_series_equal(result, expected) def test_replace_series_no_regex(self): answer = Series({0: 'Strongly Agree', 1: 'Agree', 2: 'Neutral', 3: 'Disagree', 4: 'Strongly Disagree'}) weights = Series({'Agree': 4, 'Disagree': 2, 'Neutral': 3, 'Strongly Agree': 5, 'Strongly Disagree': 1}) expected = Series({0: 5, 1: 4, 2: 3, 3: 2, 4: 1}) result = answer.replace(weights) assert_series_equal(result, expected) def test_replace_dict_tuple_list_ordering_remains_the_same(self): df = DataFrame(dict(A=[nan, 1])) res1 = df.replace(to_replace={nan: 0, 1: -1e8}) res2 = df.replace(to_replace=(1, nan), value=[-1e8, 0]) res3 = df.replace(to_replace=[1, nan], value=[-1e8, 0]) expected = DataFrame({'A': [0, -1e8]}) assert_frame_equal(res1, res2) assert_frame_equal(res2, res3) assert_frame_equal(res3, expected) def test_replace_doesnt_replace_without_regex(self): raw = """fol T_opp T_Dir T_Enh 0 1 0 0 vo 1 2 vr 0 0 2 2 0 0 0 3 3 0 bt 0""" df = pd.read_csv(StringIO(raw), sep=r'\s+') res = df.replace({r'\D': 1}) assert_frame_equal(df, res) def test_replace_bool_with_string(self): df = DataFrame({'a': [True, False], 'b': list('ab')}) result = df.replace(True, 'a') expected = DataFrame({'a': ['a', False], 'b': df.b}) assert_frame_equal(result, expected) def test_replace_pure_bool_with_string_no_op(self): df = DataFrame(np.random.rand(2, 2) > 0.5) result = df.replace('asdf', 'fdsa') assert_frame_equal(df, result) def test_replace_bool_with_bool(self): df = DataFrame(np.random.rand(2, 2) > 0.5) result = df.replace(False, True) expected = DataFrame(np.ones((2, 2), dtype=bool)) assert_frame_equal(result, expected) def test_replace_with_dict_with_bool_keys(self): df = DataFrame({0: [True, False], 1: [False, True]}) with tm.assert_raises_regex(TypeError, 'Cannot compare types .+'): df.replace({'asdf': 'asdb', True: 'yes'}) def test_replace_truthy(self): df = DataFrame({'a': [True, True]}) r = df.replace([np.inf, -np.inf], np.nan) e = df assert_frame_equal(r, e) def test_replace_int_to_int_chain(self): df = DataFrame({'a': lrange(1, 5)}) with tm.assert_raises_regex(ValueError, "Replacement not allowed .+"): df.replace({'a': dict(zip(range(1, 5), range(2, 6)))}) def test_replace_str_to_str_chain(self): a = np.arange(1, 5) astr = a.astype(str) bstr = np.arange(2, 6).astype(str) df = DataFrame({'a': astr}) with tm.assert_raises_regex(ValueError, "Replacement not allowed .+"): df.replace({'a': dict(zip(astr, bstr))}) def test_replace_swapping_bug(self): df = pd.DataFrame({'a': [True, False, True]}) res = df.replace({'a': {True: 'Y', False: 'N'}}) expect = pd.DataFrame({'a': ['Y', 'N', 'Y']}) assert_frame_equal(res, expect) df = pd.DataFrame({'a': [0, 1, 0]}) res = df.replace({'a': {0: 'Y', 1: 'N'}}) expect = pd.DataFrame({'a': ['Y', 'N', 'Y']}) assert_frame_equal(res, expect) def test_replace_period(self): d = { 'fname': { 'out_augmented_AUG_2011.json': pd.Period(year=2011, month=8, freq='M'), 'out_augmented_JAN_2011.json': pd.Period(year=2011, month=1, freq='M'), 'out_augmented_MAY_2012.json': pd.Period(year=2012, month=5, freq='M'), 'out_augmented_SUBSIDY_WEEK.json': pd.Period(year=2011, month=4, freq='M'), 'out_augmented_AUG_2012.json': pd.Period(year=2012, month=8, freq='M'), 'out_augmented_MAY_2011.json': pd.Period(year=2011, month=5, freq='M'), 'out_augmented_SEP_2013.json': pd.Period(year=2013, month=9, freq='M')}} df = pd.DataFrame(['out_augmented_AUG_2012.json', 'out_augmented_SEP_2013.json', 'out_augmented_SUBSIDY_WEEK.json', 'out_augmented_MAY_2012.json', 'out_augmented_MAY_2011.json', 'out_augmented_AUG_2011.json', 'out_augmented_JAN_2011.json'], columns=['fname']) assert set(df.fname.values) == set(d['fname'].keys()) expected = DataFrame({'fname': [d['fname'][k] for k in df.fname.values]}) result = df.replace(d) assert_frame_equal(result, expected) def test_replace_datetime(self): d = {'fname': {'out_augmented_AUG_2011.json': pd.Timestamp('2011-08'), 'out_augmented_JAN_2011.json': pd.Timestamp('2011-01'), 'out_augmented_MAY_2012.json': pd.Timestamp('2012-05'), 'out_augmented_SUBSIDY_WEEK.json': pd.Timestamp('2011-04'), 'out_augmented_AUG_2012.json': pd.Timestamp('2012-08'), 'out_augmented_MAY_2011.json': pd.Timestamp('2011-05'), 'out_augmented_SEP_2013.json': pd.Timestamp('2013-09')}} df = pd.DataFrame(['out_augmented_AUG_2012.json', 'out_augmented_SEP_2013.json', 'out_augmented_SUBSIDY_WEEK.json', 'out_augmented_MAY_2012.json', 'out_augmented_MAY_2011.json', 'out_augmented_AUG_2011.json', 'out_augmented_JAN_2011.json'], columns=['fname']) assert set(df.fname.values) == set(d['fname'].keys()) expected = DataFrame({'fname': [d['fname'][k] for k in df.fname.values]}) result = df.replace(d) assert_frame_equal(result, expected) def test_replace_datetimetz(self): # GH 11326 # behaving poorly when presented with a datetime64[ns, tz] df = DataFrame({'A': date_range('20130101', periods=3, tz='US/Eastern'), 'B': [0, np.nan, 2]}) result = df.replace(np.nan, 1) expected = DataFrame({'A': date_range('20130101', periods=3, tz='US/Eastern'), 'B': Series([0, 1, 2], dtype='float64')}) assert_frame_equal(result, expected) result = df.fillna(1) assert_frame_equal(result, expected) result = df.replace(0, np.nan) expected = DataFrame({'A': date_range('20130101', periods=3, tz='US/Eastern'), 'B': [np.nan, np.nan, 2]}) assert_frame_equal(result, expected) result = df.replace(Timestamp('20130102', tz='US/Eastern'), Timestamp('20130104', tz='US/Eastern')) expected = DataFrame({'A': [Timestamp('20130101', tz='US/Eastern'), Timestamp('20130104', tz='US/Eastern'), Timestamp('20130103', tz='US/Eastern')], 'B': [0, np.nan, 2]}) assert_frame_equal(result, expected) result = df.copy() result.iloc[1, 0] = np.nan result = result.replace( {'A': pd.NaT}, Timestamp('20130104', tz='US/Eastern')) assert_frame_equal(result, expected) # coerce to object result = df.copy() result.iloc[1, 0] = np.nan result = result.replace( {'A': pd.NaT}, Timestamp('20130104', tz='US/Pacific')) expected = DataFrame({'A': [Timestamp('20130101', tz='US/Eastern'), Timestamp('20130104', tz='US/Pacific'), Timestamp('20130103', tz='US/Eastern')], 'B': [0, np.nan, 2]}) assert_frame_equal(result, expected) result = df.copy() result.iloc[1, 0] = np.nan result = result.replace({'A': np.nan}, Timestamp('20130104')) expected = DataFrame({'A': [Timestamp('20130101', tz='US/Eastern'), Timestamp('20130104'), Timestamp('20130103', tz='US/Eastern')], 'B': [0, np.nan, 2]}) assert_frame_equal(result, expected) def test_replace_with_empty_dictlike(self): # GH 15289 mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} df = DataFrame(mix) assert_frame_equal(df, df.replace({})) assert_frame_equal(df, df.replace(Series([]))) assert_frame_equal(df, df.replace({'b': {}})) assert_frame_equal(df, df.replace(Series({'b': {}})))
apache-2.0
datapythonista/pandas
pandas/core/arrays/sparse/accessor.py
2
11479
"""Sparse accessor""" import numpy as np from pandas.compat._optional import import_optional_dependency from pandas.core.dtypes.cast import find_common_type from pandas.core.accessor import ( PandasDelegate, delegate_names, ) from pandas.core.arrays.sparse.array import SparseArray from pandas.core.arrays.sparse.dtype import SparseDtype class BaseAccessor: _validation_msg = "Can only use the '.sparse' accessor with Sparse data." def __init__(self, data=None): self._parent = data self._validate(data) def _validate(self, data): raise NotImplementedError @delegate_names( SparseArray, ["npoints", "density", "fill_value", "sp_values"], typ="property" ) class SparseAccessor(BaseAccessor, PandasDelegate): """ Accessor for SparseSparse from other sparse matrix data types. """ def _validate(self, data): if not isinstance(data.dtype, SparseDtype): raise AttributeError(self._validation_msg) def _delegate_property_get(self, name, *args, **kwargs): return getattr(self._parent.array, name) def _delegate_method(self, name, *args, **kwargs): if name == "from_coo": return self.from_coo(*args, **kwargs) elif name == "to_coo": return self.to_coo(*args, **kwargs) else: raise ValueError @classmethod def from_coo(cls, A, dense_index=False): """ Create a Series with sparse values from a scipy.sparse.coo_matrix. Parameters ---------- A : scipy.sparse.coo_matrix dense_index : bool, default False If False (default), the SparseSeries index consists of only the coords of the non-null entries of the original coo_matrix. If True, the SparseSeries index consists of the full sorted (row, col) coordinates of the coo_matrix. Returns ------- s : Series A Series with sparse values. Examples -------- >>> from scipy import sparse >>> A = sparse.coo_matrix( ... ([3.0, 1.0, 2.0], ([1, 0, 0], [0, 2, 3])), shape=(3, 4) ... ) >>> A <3x4 sparse matrix of type '<class 'numpy.float64'>' with 3 stored elements in COOrdinate format> >>> A.todense() matrix([[0., 0., 1., 2.], [3., 0., 0., 0.], [0., 0., 0., 0.]]) >>> ss = pd.Series.sparse.from_coo(A) >>> ss 0 2 1.0 3 2.0 1 0 3.0 dtype: Sparse[float64, nan] """ from pandas import Series from pandas.core.arrays.sparse.scipy_sparse import coo_to_sparse_series result = coo_to_sparse_series(A, dense_index=dense_index) result = Series(result.array, index=result.index, copy=False) return result def to_coo(self, row_levels=(0,), column_levels=(1,), sort_labels=False): """ Create a scipy.sparse.coo_matrix from a Series with MultiIndex. Use row_levels and column_levels to determine the row and column coordinates respectively. row_levels and column_levels are the names (labels) or numbers of the levels. {row_levels, column_levels} must be a partition of the MultiIndex level names (or numbers). Parameters ---------- row_levels : tuple/list column_levels : tuple/list sort_labels : bool, default False Sort the row and column labels before forming the sparse matrix. Returns ------- y : scipy.sparse.coo_matrix rows : list (row labels) columns : list (column labels) Examples -------- >>> s = pd.Series([3.0, np.nan, 1.0, 3.0, np.nan, np.nan]) >>> s.index = pd.MultiIndex.from_tuples( ... [ ... (1, 2, "a", 0), ... (1, 2, "a", 1), ... (1, 1, "b", 0), ... (1, 1, "b", 1), ... (2, 1, "b", 0), ... (2, 1, "b", 1) ... ], ... names=["A", "B", "C", "D"], ... ) >>> s A B C D 1 2 a 0 3.0 1 NaN 1 b 0 1.0 1 3.0 2 1 b 0 NaN 1 NaN dtype: float64 >>> ss = s.astype("Sparse") >>> ss A B C D 1 2 a 0 3.0 1 NaN 1 b 0 1.0 1 3.0 2 1 b 0 NaN 1 NaN dtype: Sparse[float64, nan] >>> A, rows, columns = ss.sparse.to_coo( ... row_levels=["A", "B"], column_levels=["C", "D"], sort_labels=True ... ) >>> A <3x4 sparse matrix of type '<class 'numpy.float64'>' with 3 stored elements in COOrdinate format> >>> A.todense() matrix([[0., 0., 1., 3.], [3., 0., 0., 0.], [0., 0., 0., 0.]]) >>> rows [(1, 1), (1, 2), (2, 1)] >>> columns [('a', 0), ('a', 1), ('b', 0), ('b', 1)] """ from pandas.core.arrays.sparse.scipy_sparse import sparse_series_to_coo A, rows, columns = sparse_series_to_coo( self._parent, row_levels, column_levels, sort_labels=sort_labels ) return A, rows, columns def to_dense(self): """ Convert a Series from sparse values to dense. .. versionadded:: 0.25.0 Returns ------- Series: A Series with the same values, stored as a dense array. Examples -------- >>> series = pd.Series(pd.arrays.SparseArray([0, 1, 0])) >>> series 0 0 1 1 2 0 dtype: Sparse[int64, 0] >>> series.sparse.to_dense() 0 0 1 1 2 0 dtype: int64 """ from pandas import Series return Series( self._parent.array.to_dense(), index=self._parent.index, name=self._parent.name, ) class SparseFrameAccessor(BaseAccessor, PandasDelegate): """ DataFrame accessor for sparse data. .. versionadded:: 0.25.0 """ def _validate(self, data): dtypes = data.dtypes if not all(isinstance(t, SparseDtype) for t in dtypes): raise AttributeError(self._validation_msg) @classmethod def from_spmatrix(cls, data, index=None, columns=None): """ Create a new DataFrame from a scipy sparse matrix. .. versionadded:: 0.25.0 Parameters ---------- data : scipy.sparse.spmatrix Must be convertible to csc format. index, columns : Index, optional Row and column labels to use for the resulting DataFrame. Defaults to a RangeIndex. Returns ------- DataFrame Each column of the DataFrame is stored as a :class:`arrays.SparseArray`. Examples -------- >>> import scipy.sparse >>> mat = scipy.sparse.eye(3) >>> pd.DataFrame.sparse.from_spmatrix(mat) 0 1 2 0 1.0 0.0 0.0 1 0.0 1.0 0.0 2 0.0 0.0 1.0 """ from pandas._libs.sparse import IntIndex from pandas import DataFrame data = data.tocsc() index, columns = cls._prep_index(data, index, columns) n_rows, n_columns = data.shape # We need to make sure indices are sorted, as we create # IntIndex with no input validation (i.e. check_integrity=False ). # Indices may already be sorted in scipy in which case this adds # a small overhead. data.sort_indices() indices = data.indices indptr = data.indptr array_data = data.data dtype = SparseDtype(array_data.dtype, 0) arrays = [] for i in range(n_columns): sl = slice(indptr[i], indptr[i + 1]) idx = IntIndex(n_rows, indices[sl], check_integrity=False) arr = SparseArray._simple_new(array_data[sl], idx, dtype) arrays.append(arr) return DataFrame._from_arrays( arrays, columns=columns, index=index, verify_integrity=False ) def to_dense(self): """ Convert a DataFrame with sparse values to dense. .. versionadded:: 0.25.0 Returns ------- DataFrame A DataFrame with the same values stored as dense arrays. Examples -------- >>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0])}) >>> df.sparse.to_dense() A 0 0 1 1 2 0 """ from pandas import DataFrame data = {k: v.array.to_dense() for k, v in self._parent.items()} return DataFrame(data, index=self._parent.index, columns=self._parent.columns) def to_coo(self): """ Return the contents of the frame as a sparse SciPy COO matrix. .. versionadded:: 0.25.0 Returns ------- coo_matrix : scipy.sparse.spmatrix If the caller is heterogeneous and contains booleans or objects, the result will be of dtype=object. See Notes. Notes ----- The dtype will be the lowest-common-denominator type (implicit upcasting); that is to say if the dtypes (even of numeric types) are mixed, the one that accommodates all will be chosen. e.g. If the dtypes are float16 and float32, dtype will be upcast to float32. By numpy.find_common_type convention, mixing int64 and and uint64 will result in a float64 dtype. """ import_optional_dependency("scipy") from scipy.sparse import coo_matrix dtype = find_common_type(self._parent.dtypes.to_list()) if isinstance(dtype, SparseDtype): dtype = dtype.subtype cols, rows, data = [], [], [] for col, name in enumerate(self._parent): s = self._parent[name] row = s.array.sp_index.to_int_index().indices cols.append(np.repeat(col, len(row))) rows.append(row) data.append(s.array.sp_values.astype(dtype, copy=False)) cols = np.concatenate(cols) rows = np.concatenate(rows) data = np.concatenate(data) return coo_matrix((data, (rows, cols)), shape=self._parent.shape) @property def density(self) -> float: """ Ratio of non-sparse points to total (dense) data points. """ tmp = np.mean([column.array.density for _, column in self._parent.items()]) return tmp @staticmethod def _prep_index(data, index, columns): from pandas.core.indexes.api import ensure_index import pandas.core.indexes.base as ibase N, K = data.shape if index is None: index = ibase.default_index(N) else: index = ensure_index(index) if columns is None: columns = ibase.default_index(K) else: columns = ensure_index(columns) if len(columns) != K: raise ValueError(f"Column length mismatch: {len(columns)} vs. {K}") if len(index) != N: raise ValueError(f"Index length mismatch: {len(index)} vs. {N}") return index, columns
bsd-3-clause