repo_name
stringlengths
6
112
path
stringlengths
4
204
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
714
810k
license
stringclasses
15 values
abimannans/scikit-learn
examples/calibration/plot_calibration_curve.py
225
5903
""" ============================== Probability Calibration curves ============================== When performing classification one often wants to predict not only the class label, but also the associated probability. This probability gives some kind of confidence on the prediction. This example demonstrates how to display how well calibrated the predicted probabilities are and how to calibrate an uncalibrated classifier. The experiment is performed on an artificial dataset for binary classification with 100.000 samples (1.000 of them are used for model fitting) with 20 features. Of the 20 features, only 2 are informative and 10 are redundant. The first figure shows the estimated probabilities obtained with logistic regression, Gaussian naive Bayes, and Gaussian naive Bayes with both isotonic calibration and sigmoid calibration. The calibration performance is evaluated with Brier score, reported in the legend (the smaller the better). One can observe here that logistic regression is well calibrated while raw Gaussian naive Bayes performs very badly. This is because of the redundant features which violate the assumption of feature-independence and result in an overly confident classifier, which is indicated by the typical transposed-sigmoid curve. Calibration of the probabilities of Gaussian naive Bayes with isotonic regression can fix this issue as can be seen from the nearly diagonal calibration curve. Sigmoid calibration also improves the brier score slightly, albeit not as strongly as the non-parametric isotonic regression. This can be attributed to the fact that we have plenty of calibration data such that the greater flexibility of the non-parametric model can be exploited. The second figure shows the calibration curve of a linear support-vector classifier (LinearSVC). LinearSVC shows the opposite behavior as Gaussian naive Bayes: the calibration curve has a sigmoid curve, which is typical for an under-confident classifier. In the case of LinearSVC, this is caused by the margin property of the hinge loss, which lets the model focus on hard samples that are close to the decision boundary (the support vectors). Both kinds of calibration can fix this issue and yield nearly identical results. This shows that sigmoid calibration can deal with situations where the calibration curve of the base classifier is sigmoid (e.g., for LinearSVC) but not where it is transposed-sigmoid (e.g., Gaussian naive Bayes). """ print(__doc__) # Author: Alexandre Gramfort <[email protected]> # Jan Hendrik Metzen <[email protected]> # License: BSD Style. import matplotlib.pyplot as plt from sklearn import datasets from sklearn.naive_bayes import GaussianNB from sklearn.svm import LinearSVC from sklearn.linear_model import LogisticRegression from sklearn.metrics import (brier_score_loss, precision_score, recall_score, f1_score) from sklearn.calibration import CalibratedClassifierCV, calibration_curve from sklearn.cross_validation import train_test_split # Create dataset of classification task with many redundant and few # informative features X, y = datasets.make_classification(n_samples=100000, n_features=20, n_informative=2, n_redundant=10, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.99, random_state=42) def plot_calibration_curve(est, name, fig_index): """Plot calibration curve for est w/o and with calibration. """ # Calibrated with isotonic calibration isotonic = CalibratedClassifierCV(est, cv=2, method='isotonic') # Calibrated with sigmoid calibration sigmoid = CalibratedClassifierCV(est, cv=2, method='sigmoid') # Logistic regression with no calibration as baseline lr = LogisticRegression(C=1., solver='lbfgs') fig = plt.figure(fig_index, figsize=(10, 10)) ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2) ax2 = plt.subplot2grid((3, 1), (2, 0)) ax1.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated") for clf, name in [(lr, 'Logistic'), (est, name), (isotonic, name + ' + Isotonic'), (sigmoid, name + ' + Sigmoid')]: clf.fit(X_train, y_train) y_pred = clf.predict(X_test) if hasattr(clf, "predict_proba"): prob_pos = clf.predict_proba(X_test)[:, 1] else: # use decision function prob_pos = clf.decision_function(X_test) prob_pos = \ (prob_pos - prob_pos.min()) / (prob_pos.max() - prob_pos.min()) clf_score = brier_score_loss(y_test, prob_pos, pos_label=y.max()) print("%s:" % name) print("\tBrier: %1.3f" % (clf_score)) print("\tPrecision: %1.3f" % precision_score(y_test, y_pred)) print("\tRecall: %1.3f" % recall_score(y_test, y_pred)) print("\tF1: %1.3f\n" % f1_score(y_test, y_pred)) fraction_of_positives, mean_predicted_value = \ calibration_curve(y_test, prob_pos, n_bins=10) ax1.plot(mean_predicted_value, fraction_of_positives, "s-", label="%s (%1.3f)" % (name, clf_score)) ax2.hist(prob_pos, range=(0, 1), bins=10, label=name, histtype="step", lw=2) ax1.set_ylabel("Fraction of positives") ax1.set_ylim([-0.05, 1.05]) ax1.legend(loc="lower right") ax1.set_title('Calibration plots (reliability curve)') ax2.set_xlabel("Mean predicted value") ax2.set_ylabel("Count") ax2.legend(loc="upper center", ncol=2) plt.tight_layout() # Plot calibration cuve for Gaussian Naive Bayes plot_calibration_curve(GaussianNB(), "Naive Bayes", 1) # Plot calibration cuve for Linear SVC plot_calibration_curve(LinearSVC(), "SVC", 2) plt.show()
bsd-3-clause
suiyuan2009/tensorflow
tensorflow/contrib/learn/python/learn/estimators/kmeans.py
34
10130
# 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. # ============================================================================== """Implementation of k-means clustering on top of tf.learn API.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import time import numpy as np from tensorflow.contrib.factorization.python.ops import clustering_ops from tensorflow.contrib.framework.python.ops import variables from tensorflow.contrib.learn.python.learn.estimators import estimator from tensorflow.contrib.learn.python.learn.estimators.model_fn import ModelFnOps from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops import state_ops from tensorflow.python.summary import summary from tensorflow.python.ops.control_flow_ops import with_dependencies from tensorflow.python.platform import tf_logging as logging from tensorflow.python.summary import summary from tensorflow.python.training import session_run_hook from tensorflow.python.training.session_run_hook import SessionRunArgs class _LossRelativeChangeHook(session_run_hook.SessionRunHook): """Stops when the change in loss goes below a tolerance.""" def __init__(self, tolerance): """Initializes _LossRelativeChangeHook. Args: tolerance: A relative tolerance of change between iterations. """ self._tolerance = tolerance self._prev_loss = None def begin(self): self._loss_tensor = ops.get_default_graph().get_tensor_by_name( KMeansClustering.LOSS_OP_NAME + ':0') assert self._loss_tensor is not None def before_run(self, run_context): del run_context return SessionRunArgs( fetches={KMeansClustering.LOSS_OP_NAME: self._loss_tensor}) def after_run(self, run_context, run_values): loss = run_values.results[KMeansClustering.LOSS_OP_NAME] assert loss is not None if self._prev_loss is not None: relative_change = (abs(loss - self._prev_loss) / (1 + abs(self._prev_loss))) if relative_change < self._tolerance: run_context.request_stop() self._prev_loss = loss class _InitializeClustersHook(session_run_hook.SessionRunHook): """Initializes clusters or waits for cluster initialization.""" def __init__(self, init_op, is_initialized_op, is_chief): self._init_op = init_op self._is_chief = is_chief self._is_initialized_op = is_initialized_op def after_create_session(self, session, _): assert self._init_op.graph == ops.get_default_graph() assert self._is_initialized_op.graph == self._init_op.graph while True: try: if session.run(self._is_initialized_op): break elif self._is_chief: session.run(self._init_op) else: time.sleep(1) except RuntimeError as e: logging.info(e) def _parse_tensor_or_dict(features): """Helper function to parse features.""" if isinstance(features, dict): keys = sorted(features.keys()) with ops.colocate_with(features[keys[0]]): features = array_ops.concat([features[k] for k in keys], 1) return features def _kmeans_clustering_model_fn(features, labels, mode, params, config): """Model function for KMeansClustering estimator.""" assert labels is None, labels (all_scores, model_predictions, losses, is_initialized, init_op, training_op) = clustering_ops.KMeans( _parse_tensor_or_dict(features), params.get('num_clusters'), initial_clusters=params.get('training_initial_clusters'), distance_metric=params.get('distance_metric'), use_mini_batch=params.get('use_mini_batch'), mini_batch_steps_per_iteration=params.get( 'mini_batch_steps_per_iteration'), random_seed=params.get('random_seed'), kmeans_plus_plus_num_retries=params.get( 'kmeans_plus_plus_num_retries')).training_graph() incr_step = state_ops.assign_add(variables.get_global_step(), 1) loss = math_ops.reduce_sum(losses, name=KMeansClustering.LOSS_OP_NAME) summary.scalar('loss/raw', loss) training_op = with_dependencies([training_op, incr_step], loss) predictions = { KMeansClustering.ALL_SCORES: all_scores[0], KMeansClustering.CLUSTER_IDX: model_predictions[0], } eval_metric_ops = {KMeansClustering.SCORES: loss} training_hooks = [_InitializeClustersHook( init_op, is_initialized, config.is_chief)] relative_tolerance = params.get('relative_tolerance') if relative_tolerance is not None: training_hooks.append(_LossRelativeChangeHook(relative_tolerance)) return ModelFnOps( mode=mode, predictions=predictions, eval_metric_ops=eval_metric_ops, loss=loss, train_op=training_op, training_hooks=training_hooks) # TODO(agarwal,ands): support sharded input. class KMeansClustering(estimator.Estimator): """An Estimator for K-Means clustering.""" SQUARED_EUCLIDEAN_DISTANCE = clustering_ops.SQUARED_EUCLIDEAN_DISTANCE COSINE_DISTANCE = clustering_ops.COSINE_DISTANCE RANDOM_INIT = clustering_ops.RANDOM_INIT KMEANS_PLUS_PLUS_INIT = clustering_ops.KMEANS_PLUS_PLUS_INIT SCORES = 'scores' CLUSTER_IDX = 'cluster_idx' CLUSTERS = 'clusters' ALL_SCORES = 'all_scores' LOSS_OP_NAME = 'kmeans_loss' def __init__(self, num_clusters, model_dir=None, initial_clusters=RANDOM_INIT, distance_metric=SQUARED_EUCLIDEAN_DISTANCE, random_seed=0, use_mini_batch=True, mini_batch_steps_per_iteration=1, kmeans_plus_plus_num_retries=2, relative_tolerance=None, config=None): """Creates a model for running KMeans training and inference. Args: num_clusters: number of clusters to train. model_dir: the directory to save the model results and log files. initial_clusters: specifies how to initialize the clusters for training. See clustering_ops.kmeans for the possible values. distance_metric: the distance metric used for clustering. See clustering_ops.kmeans for the possible values. random_seed: Python integer. Seed for PRNG used to initialize centers. use_mini_batch: If true, use the mini-batch k-means algorithm. Else assume full batch. mini_batch_steps_per_iteration: number of steps after which the updated cluster centers are synced back to a master copy. See clustering_ops.py for more details. kmeans_plus_plus_num_retries: For each point that is sampled during kmeans++ initialization, this parameter specifies the number of additional points to draw from the current distribution before selecting the best. If a negative value is specified, a heuristic is used to sample O(log(num_to_sample)) additional points. relative_tolerance: A relative tolerance of change in the loss between iterations. Stops learning if the loss changes less than this amount. Note that this may not work correctly if use_mini_batch=True. config: See Estimator """ params = {} params['num_clusters'] = num_clusters params['training_initial_clusters'] = initial_clusters params['distance_metric'] = distance_metric params['random_seed'] = random_seed params['use_mini_batch'] = use_mini_batch params['mini_batch_steps_per_iteration'] = mini_batch_steps_per_iteration params['kmeans_plus_plus_num_retries'] = kmeans_plus_plus_num_retries params['relative_tolerance'] = relative_tolerance super(KMeansClustering, self).__init__( model_fn=_kmeans_clustering_model_fn, params=params, model_dir=model_dir, config=config) def predict_cluster_idx(self, input_fn=None): """Yields predicted cluster indices.""" key = KMeansClustering.CLUSTER_IDX results = super(KMeansClustering, self).predict( input_fn=input_fn, outputs=[key]) for result in results: yield result[key] def score(self, input_fn=None, steps=None): """Predict total sum of distances to nearest clusters. Note that this function is different from the corresponding one in sklearn which returns the negative of the sum of distances. Args: input_fn: see predict. steps: see predict. Returns: Total sum of distances to nearest clusters. """ return np.sum( self.evaluate( input_fn=input_fn, steps=steps)[KMeansClustering.SCORES]) def transform(self, input_fn=None, as_iterable=False): """Transforms each element to distances to cluster centers. Note that this function is different from the corresponding one in sklearn. For SQUARED_EUCLIDEAN distance metric, sklearn transform returns the EUCLIDEAN distance, while this function returns the SQUARED_EUCLIDEAN distance. Args: input_fn: see predict. as_iterable: see predict Returns: Array with same number of rows as x, and num_clusters columns, containing distances to the cluster centers. """ key = KMeansClustering.ALL_SCORES results = super(KMeansClustering, self).predict( input_fn=input_fn, outputs=[key], as_iterable=as_iterable) if not as_iterable: return results[key] else: return results def clusters(self): """Returns cluster centers.""" return super(KMeansClustering, self).get_variable_value(self.CLUSTERS)
apache-2.0
EliHar/Pattern_recognition
openface1/util/tsne.py
11
1261
#!/usr/bin/env python2 import numpy as np import pandas as pd from sklearn.decomposition import PCA from sklearn.manifold import TSNE import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt import matplotlib.cm as cm plt.style.use('bmh') import argparse print(""" Note: This example assumes that `name i` corresponds to `label i` in `labels.csv`. """) parser = argparse.ArgumentParser() parser.add_argument('workDir', type=str) parser.add_argument('--names', type=str, nargs='+', required=True) args = parser.parse_args() y = pd.read_csv("{}/labels.csv".format(args.workDir)).as_matrix()[:, 0] X = pd.read_csv("{}/reps.csv".format(args.workDir)).as_matrix() target_names = np.array(args.names) colors = cm.Dark2(np.linspace(0, 1, len(target_names))) X_pca = PCA(n_components=50).fit_transform(X, X) tsne = TSNE(n_components=2, init='random', random_state=0) X_r = tsne.fit_transform(X_pca) for c, i, target_name in zip(colors, list(range(1, len(target_names) + 1)), target_names): plt.scatter(X_r[y == i, 0], X_r[y == i, 1], c=c, label=target_name) plt.legend() out = "{}/tsne.pdf".format(args.workDir) plt.savefig(out) print("Saved to: {}".format(out))
mit
ishanic/scikit-learn
sklearn/covariance/graph_lasso_.py
127
25626
"""GraphLasso: sparse inverse covariance estimation with an l1-penalized estimator. """ # Author: Gael Varoquaux <[email protected]> # 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
BryanCutler/spark
python/pyspark/pandas/tests/test_frame_spark.py
1
6247
# # 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 distutils.version import LooseVersion import os import pandas as pd import pyspark from pyspark import pandas as pp from pyspark.pandas.testing.utils import ReusedSQLTestCase, SQLTestUtils, TestUtils class SparkFrameMethodsTest(ReusedSQLTestCase, SQLTestUtils, TestUtils): def test_frame_apply_negative(self): with self.assertRaisesRegex( ValueError, "The output of the function.* pyspark.sql.DataFrame.*int" ): pp.range(10).spark.apply(lambda scol: 1) def test_hint(self): pdf1 = pd.DataFrame( {"lkey": ["foo", "bar", "baz", "foo"], "value": [1, 2, 3, 5]} ).set_index("lkey") pdf2 = pd.DataFrame( {"rkey": ["foo", "bar", "baz", "foo"], "value": [5, 6, 7, 8]} ).set_index("rkey") kdf1 = pp.from_pandas(pdf1) kdf2 = pp.from_pandas(pdf2) if LooseVersion(pyspark.__version__) >= LooseVersion("3.0"): hints = ["broadcast", "merge", "shuffle_hash", "shuffle_replicate_nl"] else: hints = ["broadcast"] for hint in hints: self.assert_eq( pdf1.merge(pdf2, left_index=True, right_index=True).sort_values( ["value_x", "value_y"] ), kdf1.merge(kdf2.spark.hint(hint), left_index=True, right_index=True).sort_values( ["value_x", "value_y"] ), almost=True, ) self.assert_eq( pdf1.merge(pdf2 + 1, left_index=True, right_index=True).sort_values( ["value_x", "value_y"] ), kdf1.merge( (kdf2 + 1).spark.hint(hint), left_index=True, right_index=True ).sort_values(["value_x", "value_y"]), almost=True, ) def test_repartition(self): kdf = pp.DataFrame({"age": [5, 5, 2, 2], "name": ["Bob", "Bob", "Alice", "Alice"]}) num_partitions = kdf.to_spark().rdd.getNumPartitions() + 1 num_partitions += 1 new_kdf = kdf.spark.repartition(num_partitions) self.assertEqual(new_kdf.to_spark().rdd.getNumPartitions(), num_partitions) self.assert_eq(kdf.sort_index(), new_kdf.sort_index()) # Reserves Index kdf = kdf.set_index("age") num_partitions += 1 new_kdf = kdf.spark.repartition(num_partitions) self.assertEqual(new_kdf.to_spark().rdd.getNumPartitions(), num_partitions) self.assert_eq(kdf.sort_index(), new_kdf.sort_index()) # Reflects internal changes kdf = kdf.reset_index() kdf = kdf.set_index("name") kdf2 = kdf + 1 num_partitions += 1 self.assert_eq(kdf2.sort_index(), (kdf + 1).spark.repartition(num_partitions).sort_index()) # Reserves MultiIndex kdf = pp.DataFrame({"a": ["a", "b", "c"]}, index=[[1, 2, 3], [4, 5, 6]]) num_partitions = kdf.to_spark().rdd.getNumPartitions() + 1 new_kdf = kdf.spark.repartition(num_partitions) self.assertEqual(new_kdf.to_spark().rdd.getNumPartitions(), num_partitions) self.assert_eq(kdf.sort_index(), new_kdf.sort_index()) def test_coalesce(self): num_partitions = 10 kdf = pp.DataFrame({"age": [5, 5, 2, 2], "name": ["Bob", "Bob", "Alice", "Alice"]}) kdf = kdf.spark.repartition(num_partitions) num_partitions -= 1 new_kdf = kdf.spark.coalesce(num_partitions) self.assertEqual(new_kdf.to_spark().rdd.getNumPartitions(), num_partitions) self.assert_eq(kdf.sort_index(), new_kdf.sort_index()) # Reserves Index kdf = kdf.set_index("age") num_partitions -= 1 new_kdf = kdf.spark.coalesce(num_partitions) self.assertEqual(new_kdf.to_spark().rdd.getNumPartitions(), num_partitions) self.assert_eq(kdf.sort_index(), new_kdf.sort_index()) # Reflects internal changes kdf = kdf.reset_index() kdf = kdf.set_index("name") kdf2 = kdf + 1 num_partitions -= 1 self.assert_eq(kdf2.sort_index(), (kdf + 1).spark.coalesce(num_partitions).sort_index()) # Reserves MultiIndex kdf = pp.DataFrame({"a": ["a", "b", "c"]}, index=[[1, 2, 3], [4, 5, 6]]) num_partitions -= 1 kdf = kdf.spark.repartition(num_partitions) num_partitions -= 1 new_kdf = kdf.spark.coalesce(num_partitions) self.assertEqual(new_kdf.to_spark().rdd.getNumPartitions(), num_partitions) self.assert_eq(kdf.sort_index(), new_kdf.sort_index()) def test_checkpoint(self): with self.temp_dir() as tmp: self.spark.sparkContext.setCheckpointDir(tmp) kdf = pp.DataFrame({"a": ["a", "b", "c"]}) new_kdf = kdf.spark.checkpoint() self.assertIsNotNone(os.listdir(tmp)) self.assert_eq(kdf, new_kdf) def test_local_checkpoint(self): kdf = pp.DataFrame({"a": ["a", "b", "c"]}) new_kdf = kdf.spark.local_checkpoint() self.assert_eq(kdf, new_kdf) if __name__ == "__main__": import unittest from pyspark.pandas.tests.test_frame_spark import * # noqa: F401 try: import xmlrunner # type: ignore[import] testRunner = xmlrunner.XMLTestRunner(output='target/test-reports', verbosity=2) except ImportError: testRunner = None unittest.main(testRunner=testRunner, verbosity=2)
apache-2.0
codekansas/spykes
examples/plot_neuropixels_example.py
2
7803
""" =================== Neuropixels Example =================== Use spykes to analyze data from UCL's Neuropixels """ # Authors: Mayank Agrawal <[email protected]> # # License: MIT ######################################################## import numpy as np import pandas as pd from spykes.plot.neurovis import NeuroVis from spykes.plot.popvis import PopVis import matplotlib.pyplot as plt from spykes.io.datasets import load_neuropixels_data plt.style.use('seaborn-ticks') ######################################################## # Neuropixels # ----------------------------- # Neuropixels is a new recording technique by UCL's `Cortex Lab # <http://www.ucl.ac.uk/neuropixels>`__ that is able to measure data from # hundreds of neurons. Below we show how this data can be worked with in Spykes # # 0 Download Data # ----------------------------- # # Download all data `here <http://data.cortexlab.net/dualPhase3/data/>`__. # # 1 Read In Data # ----------------------------- folder_names = ['posterior', 'frontal'] Fs = 30000.0 striatum = list() motor_ctx = list() thalamus = list() hippocampus = list() visual_ctx = list() # a lot of this code is adapted from Cortex Lab's MATLAB script # see here: http://data.cortexlab.net/dualPhase3/data/script_dualPhase3.m data_dict = load_neuropixels_data() for name in folder_names: clusters = np.squeeze(data_dict[name + '/spike_clusters.npy']) spike_times = (np.squeeze(data_dict[(name + '/spike_times.npy')])) / Fs spike_templates = (np.squeeze(data_dict[(name + '/spike_templates.npy')])) temps = (np.squeeze(data_dict[(name + '/templates.npy')])) winv = (np.squeeze(data_dict[(name + '/whitening_mat_inv.npy')])) y_coords = (np.squeeze(data_dict[(name + '/channel_positions.npy')]))[:, 1] # frontal times need to align with posterior if (name == 'frontal'): time_correction = data_dict[('timeCorrection.npy')] spike_times *= time_correction[0] spike_times += time_correction[1] data = data_dict[(name + '/cluster_groups.csv')] cids = np.array([x[0] for x in data]) cfg = np.array([x[1] for x in data]) # find good clusters and only use those spikes good_clusters = cids[cfg == 'good'] good_indices = (np.in1d(clusters, good_clusters)) real_spikes = spike_times[good_indices] real_clusters = clusters[good_indices] real_spike_templates = spike_templates[good_indices] # find how many spikes per cluster and then order spikes by which cluster # they are in counts_per_cluster = np.bincount(real_clusters) sort_idx = np.argsort(real_clusters) sorted_clusters = real_clusters[sort_idx] sorted_spikes = real_spikes[sort_idx] sorted_spike_templates = real_spike_templates[sort_idx] # find depth for each spike # this is translated from Cortex Lab's MATLAB code # for more details, check out the original code here: # https://github.com/cortex-lab/spikes/blob/master/analysis/templatePositionsAmplitudes.m temps_unw = np.zeros(temps.shape) for t in range(temps.shape[0]): temps_unw[t, :, :] = np.dot(temps[t, :, :], winv) temp_chan_amps = np.ptp(temps_unw, axis=1) temps_amps = np.max(temp_chan_amps, axis=1) thresh_vals = temps_amps * 0.3 thresh_vals = [thresh_vals for i in range(temp_chan_amps.shape[1])] thresh_vals = np.stack(thresh_vals, axis=1) temp_chan_amps[temp_chan_amps < thresh_vals] = 0 y_coords = np.reshape(y_coords, (y_coords.shape[0], 1)) temp_depths = np.sum( np.dot(temp_chan_amps, y_coords), axis=1) / (np.sum(temp_chan_amps, axis=1)) sorted_spike_depths = temp_depths[sorted_spike_templates] # create neurons and find region accumulator = 0 for idx, count in enumerate(counts_per_cluster): if count > 0: spike_times = sorted_spikes[accumulator:accumulator + count] neuron = NeuroVis(spiketimes=spike_times, name='%d' % (idx)) cluster_depth = np.mean( sorted_spike_depths[accumulator:accumulator + count]) if name == 'frontal': if (cluster_depth > 0 and cluster_depth < 1550): striatum.append(neuron) elif (cluster_depth > 1550 and cluster_depth < 3840): motor_ctx.append(neuron) elif name == 'posterior': if (cluster_depth > 0 and cluster_depth < 1634): thalamus.append(neuron) elif (cluster_depth > 1634 and cluster_depth < 2797): hippocampus.append(neuron) elif (cluster_depth > 2797 and cluster_depth < 3840): visual_ctx.append(neuron) accumulator += count print("Striatum (n = %d)" % len(striatum)) print("Motor Cortex (n = %d)" % len(motor_ctx)) print("Thalamus (n = %d)" % len(thalamus)) print("Hippocampus (n = %d)" % len(hippocampus)) print("Visual Cortex (n = %d)" % len(visual_ctx)) ######################################################## # 2 Create Data Frame # ----------------------------- df = pd.DataFrame() raw_data = data_dict['experiment1stimInfo.mat'] df['start'] = np.squeeze(raw_data['stimStarts']) df['stop'] = np.squeeze(raw_data['stimStops']) df['stimulus'] = np.squeeze(raw_data['stimIDs']) print(df.head()) ######################################################## # 3 Start Plotting # ----------------------------- # 3.1 Striatum # ~~~~~~~~~~~~ pop = PopVis(striatum, name='Striatum') fig = plt.figure(figsize=(30, 20)) all_psth = pop.get_all_psth( event='start', df=df, conditions='stimulus', plot=False, binsize=100, window=[-500, 2000]) pop.plot_heat_map(all_psth, cond_id=[ 2, 7, 13], sortorder='descend', neuron_names=False) ######################################################## pop.plot_population_psth(all_psth=all_psth, cond_id=[1, 7, 12]) ######################################################## # 3.2 Frontal # ~~~~~~~~~~~~ pop = PopVis(striatum + motor_ctx, name='Frontal') fig = plt.figure(figsize=(30, 20)) all_psth = pop.get_all_psth( event='start', df=df, conditions='stimulus', plot=False, binsize=100, window=[-500, 2000]) pop.plot_heat_map( all_psth, cond_id=[2, 7, 13], sortorder='descend', neuron_names=False) ######################################################## pop.plot_population_psth(all_psth=all_psth, cond_id=[1, 7, 12]) ######################################################## # 3.3 All Neurons # ~~~~~~~~~~~~ pop = PopVis(striatum + motor_ctx + thalamus + hippocampus + visual_ctx) fig = plt.figure(figsize=(30, 20)) all_psth = pop.get_all_psth( event='start', df=df, conditions='stimulus', plot=False, binsize=100, window=[-500, 2000]) pop.plot_heat_map( all_psth, cond_id=[2, 7, 13], sortorder='descend', neuron_names=False) ######################################################## pop.plot_population_psth(all_psth=all_psth, cond_id=[1, 7, 12]) ######################################################## # 3.4 Striatum vs. Motor Cortex # ~~~~~~~~~~~~ striatum_pop = PopVis(striatum, name='Striatum') motor_ctx_pop = PopVis(motor_ctx, name='Motor Cortex') striatum_psth = striatum_pop.get_all_psth( event='start', df=df, conditions='stimulus', plot=False, binsize=100, window=[-500, 2000]) motor_ctx_psth = motor_ctx_pop.get_all_psth( event='start', df=df, conditions='stimulus', plot=False, binsize=100, window=[-500, 2000]) ######################################################## striatum_pop.plot_population_psth(all_psth=striatum_psth, cond_id=[1, 7, 12]) ######################################################## motor_ctx_pop.plot_population_psth(all_psth=motor_ctx_psth, cond_id=[1, 7, 12])
mit
bobwalker99/Pydev
plugins/org.python.pydev/pysrc/pydevd.py
1
61172
''' Entry point module (keep at root): This module starts the debugger. ''' from __future__ import nested_scopes # Jython 2.1 support import atexit import os import sys import traceback from _pydevd_bundle.pydevd_constants import IS_JYTH_LESS25, IS_PY3K, IS_PY34_OLDER, get_thread_id, dict_keys, dict_pop, dict_contains, \ dict_iter_items, DebugInfoHolder, PYTHON_SUSPEND, STATE_SUSPEND, STATE_RUN, get_frame, xrange, \ clear_cached_thread_id from _pydev_bundle import fix_getpass from _pydev_bundle import pydev_imports, pydev_log from _pydev_bundle._pydev_filesystem_encoding import getfilesystemencoding from _pydev_bundle.pydev_is_thread_alive import is_thread_alive from _pydev_imps._pydev_saved_modules import threading from _pydev_imps._pydev_saved_modules import time from _pydev_imps._pydev_saved_modules import thread from _pydevd_bundle import pydevd_io, pydevd_vm_type, pydevd_tracing from _pydevd_bundle import pydevd_utils from _pydevd_bundle import pydevd_vars from _pydevd_bundle.pydevd_additional_thread_info import PyDBAdditionalThreadInfo from _pydevd_bundle.pydevd_breakpoints import ExceptionBreakpoint, update_exception_hook from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER, \ CMD_STEP_RETURN, CMD_STEP_INTO_MY_CODE, CMD_THREAD_SUSPEND, CMD_RUN_TO_LINE, \ CMD_ADD_EXCEPTION_BREAK, CMD_SMART_STEP_INTO, InternalConsoleExec, NetCommandFactory, \ PyDBDaemonThread, _queue, ReaderThread, GetGlobalDebugger, get_global_debugger, \ set_global_debugger, WriterThread, pydevd_find_thread_by_id, pydevd_log, \ start_client, start_server, InternalGetBreakpointException, InternalSendCurrExceptionTrace, \ InternalSendCurrExceptionTraceProceeded from _pydevd_bundle.pydevd_custom_frames import CustomFramesContainer, custom_frames_container_init from _pydevd_bundle.pydevd_frame_utils import add_exception_to_frame from _pydevd_bundle.pydevd_kill_all_pydevd_threads import kill_all_pydev_threads from _pydevd_bundle.pydevd_trace_dispatch import trace_dispatch as _trace_dispatch from _pydevd_bundle.pydevd_utils import save_main_module from pydevd_concurrency_analyser.pydevd_concurrency_logger import ThreadingLogger, AsyncioLogger, send_message, cur_time from pydevd_concurrency_analyser.pydevd_thread_wrappers import wrap_threads __version_info__ = (0, 0, 6) __version_info_str__ = [] for v in __version_info__: __version_info_str__.append(str(v)) __version__ = '.'.join(__version_info_str__) #IMPORTANT: pydevd_constants must be the 1st thing defined because it'll keep a reference to the original sys._getframe SUPPORT_PLUGINS = not IS_JYTH_LESS25 PluginManager = None if SUPPORT_PLUGINS: from _pydevd_bundle.pydevd_plugin_utils import PluginManager threadingEnumerate = threading.enumerate threadingCurrentThread = threading.currentThread try: 'dummy'.encode('utf-8') # Added because otherwise Jython 2.2.1 wasn't finding the encoding (if it wasn't loaded in the main thread). except: pass connected = False bufferStdOutToServer = False bufferStdErrToServer = False remote = False file_system_encoding = getfilesystemencoding() #======================================================================================================================= # PyDBCommandThread #======================================================================================================================= class PyDBCommandThread(PyDBDaemonThread): def __init__(self, py_db): PyDBDaemonThread.__init__(self) self._py_db_command_thread_event = py_db._py_db_command_thread_event self.py_db = py_db self.setName('pydevd.CommandThread') def _on_run(self): for i in xrange(1, 10): time.sleep(0.5) #this one will only start later on (because otherwise we may not have any non-daemon threads if self.killReceived: return if self.dontTraceMe: self.py_db.SetTrace(None) # no debugging on this thread try: while not self.killReceived: try: self.py_db.process_internal_commands() except: pydevd_log(0, 'Finishing debug communication...(2)') self._py_db_command_thread_event.clear() self._py_db_command_thread_event.wait(0.5) except: pydev_log.debug(sys.exc_info()[0]) #only got this error in interpreter shutdown #pydevd_log(0, 'Finishing debug communication...(3)') #======================================================================================================================= # CheckOutputThread # Non-daemonic thread guaranties that all data is written even if program is finished #======================================================================================================================= class CheckOutputThread(PyDBDaemonThread): def __init__(self, py_db): PyDBDaemonThread.__init__(self) self.py_db = py_db self.setName('pydevd.CheckAliveThread') self.daemon = False py_db.output_checker = self def _on_run(self): if self.dontTraceMe: disable_tracing = True if pydevd_vm_type.get_vm_type() == pydevd_vm_type.PydevdVmType.JYTHON and sys.hexversion <= 0x020201f0: # don't run untraced threads if we're in jython 2.2.1 or lower # jython bug: if we start a thread and another thread changes the tracing facility # it affects other threads (it's not set only for the thread but globally) # Bug: http://sourceforge.net/tracker/index.php?func=detail&aid=1870039&group_id=12867&atid=112867 disable_tracing = False if disable_tracing: pydevd_tracing.SetTrace(None) # no debugging on this thread while not self.killReceived: time.sleep(0.3) if not self.py_db.has_threads_alive() and self.py_db.writer.empty() \ and not has_data_to_redirect(): try: pydev_log.debug("No alive threads, finishing debug session") self.py_db.finish_debugging_session() kill_all_pydev_threads() except: traceback.print_exc() self.killReceived = True self.py_db.check_output_redirect() def do_kill_pydev_thread(self): self.killReceived = True #======================================================================================================================= # PyDB #======================================================================================================================= class PyDB: """ Main debugging class Lots of stuff going on here: PyDB starts two threads on startup that connect to remote debugger (RDB) The threads continuously read & write commands to RDB. PyDB communicates with these threads through command queues. Every RDB command is processed by calling process_net_command. Every PyDB net command is sent to the net by posting NetCommand to WriterThread queue Some commands need to be executed on the right thread (suspend/resume & friends) These are placed on the internal command queue. """ def __init__(self): set_global_debugger(self) pydevd_tracing.replace_sys_set_trace_func() self.reader = None self.writer = None self.output_checker = None self.quitting = None self.cmd_factory = NetCommandFactory() self._cmd_queue = {} # the hash of Queues. Key is thread id, value is thread self.breakpoints = {} self.file_to_id_to_line_breakpoint = {} self.file_to_id_to_plugin_breakpoint = {} # Note: breakpoints dict should not be mutated: a copy should be created # and later it should be assigned back (to prevent concurrency issues). self.break_on_uncaught_exceptions = {} self.break_on_caught_exceptions = {} self.ready_to_run = False self._main_lock = thread.allocate_lock() self._lock_running_thread_ids = thread.allocate_lock() self._py_db_command_thread_event = threading.Event() CustomFramesContainer._py_db_command_thread_event = self._py_db_command_thread_event self._finish_debugging_session = False self._termination_event_set = False self.signature_factory = None self.SetTrace = pydevd_tracing.SetTrace self.break_on_exceptions_thrown_in_same_context = False self.ignore_exceptions_thrown_in_lines_with_ignore_exception = True # Suspend debugger even if breakpoint condition raises an exception SUSPEND_ON_BREAKPOINT_EXCEPTION = True self.suspend_on_breakpoint_exception = SUSPEND_ON_BREAKPOINT_EXCEPTION # By default user can step into properties getter/setter/deleter methods self.disable_property_trace = False self.disable_property_getter_trace = False self.disable_property_setter_trace = False self.disable_property_deleter_trace = False #this is a dict of thread ids pointing to thread ids. Whenever a command is passed to the java end that #acknowledges that a thread was created, the thread id should be passed here -- and if at some time we do not #find that thread alive anymore, we must remove it from this list and make the java side know that the thread #was killed. self._running_thread_ids = {} self._set_breakpoints_with_id = False # This attribute holds the file-> lines which have an @IgnoreException. self.filename_to_lines_where_exceptions_are_ignored = {} #working with plugins (lazily initialized) self.plugin = None self.has_plugin_line_breaks = False self.has_plugin_exception_breaks = False self.thread_analyser = None self.asyncio_analyser = None # matplotlib support in debugger and debug console self.mpl_in_use = False self.mpl_hooks_in_debug_console = False self.mpl_modules_for_patching = {} self._filename_to_not_in_scope = {} self.first_breakpoint_reached = False self.is_filter_enabled = pydevd_utils.is_filter_enabled() self.is_filter_libraries = pydevd_utils.is_filter_libraries() def get_plugin_lazy_init(self): if self.plugin is None and SUPPORT_PLUGINS: self.plugin = PluginManager(self) return self.plugin def not_in_scope(self, filename): return pydevd_utils.not_in_project_roots(filename) def is_ignored_by_filters(self, filename): return pydevd_utils.is_ignored_by_filter(filename) def first_appearance_in_scope(self, trace): if trace is None or self.not_in_scope(trace.tb_frame.f_code.co_filename): return False else: trace = trace.tb_next while trace is not None: frame = trace.tb_frame if not self.not_in_scope(frame.f_code.co_filename): return False trace = trace.tb_next return True def has_threads_alive(self): for t in threadingEnumerate(): if getattr(t, 'is_pydev_daemon_thread', False): #Important: Jython 2.5rc4 has a bug where a thread created with thread.start_new_thread won't be #set as a daemon thread, so, we also have to check for the 'is_pydev_daemon_thread' flag. #See: https://github.com/fabioz/PyDev.Debugger/issues/11 continue if isinstance(t, PyDBDaemonThread): pydev_log.error_once( 'Error in debugger: Found PyDBDaemonThread not marked with is_pydev_daemon_thread=True.\n') if is_thread_alive(t): if not t.isDaemon() or hasattr(t, "__pydevd_main_thread"): return True return False def finish_debugging_session(self): self._finish_debugging_session = True def initialize_network(self, sock): try: sock.settimeout(None) # infinite, no timeouts from now on - jython does not have it except: pass self.writer = WriterThread(sock) self.reader = ReaderThread(sock) self.writer.start() self.reader.start() time.sleep(0.1) # give threads time to start def connect(self, host, port): if host: s = start_client(host, port) else: s = start_server(port) self.initialize_network(s) def get_internal_queue(self, thread_id): """ returns internal command queue for a given thread. if new queue is created, notify the RDB about it """ if thread_id.startswith('__frame__'): thread_id = thread_id[thread_id.rfind('|') + 1:] try: return self._cmd_queue[thread_id] except KeyError: return self._cmd_queue.setdefault(thread_id, _queue.Queue()) #@UndefinedVariable def post_internal_command(self, int_cmd, thread_id): """ if thread_id is *, post to all """ if thread_id == "*": threads = threadingEnumerate() for t in threads: thread_id = get_thread_id(t) queue = self.get_internal_queue(thread_id) queue.put(int_cmd) else: queue = self.get_internal_queue(thread_id) queue.put(int_cmd) def check_output_redirect(self): global bufferStdOutToServer global bufferStdErrToServer if bufferStdOutToServer: init_stdout_redirect() self.check_output(sys.stdoutBuf, 1) #@UndefinedVariable if bufferStdErrToServer: init_stderr_redirect() self.check_output(sys.stderrBuf, 2) #@UndefinedVariable def check_output(self, out, outCtx): '''Checks the output to see if we have to send some buffered output to the debug server @param out: sys.stdout or sys.stderr @param outCtx: the context indicating: 1=stdout and 2=stderr (to know the colors to write it) ''' try: v = out.getvalue() if v: self.cmd_factory.make_io_message(v, outCtx, self) except: traceback.print_exc() def init_matplotlib_in_debug_console(self): # import hook and patches for matplotlib support in debug console from _pydev_bundle.pydev_import_hook import import_hook_manager for module in dict_keys(self.mpl_modules_for_patching): import_hook_manager.add_module_name(module, dict_pop(self.mpl_modules_for_patching, module)) def init_matplotlib_support(self): # prepare debugger for integration with matplotlib GUI event loop from pydev_ipython.matplotlibtools import activate_matplotlib, activate_pylab, activate_pyplot, do_enable_gui # enable_gui_function in activate_matplotlib should be called in main thread. Unlike integrated console, # in the debug console we have no interpreter instance with exec_queue, but we run this code in the main # thread and can call it directly. class _MatplotlibHelper: _return_control_osc = False def return_control(): # Some of the input hooks (e.g. Qt4Agg) check return control without doing # a single operation, so we don't return True on every # call when the debug hook is in place to allow the GUI to run _MatplotlibHelper._return_control_osc = not _MatplotlibHelper._return_control_osc return _MatplotlibHelper._return_control_osc from pydev_ipython.inputhook import set_return_control_callback set_return_control_callback(return_control) self.mpl_modules_for_patching = {"matplotlib": lambda: activate_matplotlib(do_enable_gui), "matplotlib.pyplot": activate_pyplot, "pylab": activate_pylab } def process_internal_commands(self): '''This function processes internal commands ''' self._main_lock.acquire() try: self.check_output_redirect() curr_thread_id = get_thread_id(threadingCurrentThread()) program_threads_alive = {} all_threads = threadingEnumerate() program_threads_dead = [] self._lock_running_thread_ids.acquire() try: for t in all_threads: if getattr(t, 'is_pydev_daemon_thread', False): pass # I.e.: skip the DummyThreads created from pydev daemon threads elif isinstance(t, PyDBDaemonThread): pydev_log.error_once('Error in debugger: Found PyDBDaemonThread not marked with is_pydev_daemon_thread=True.\n') elif is_thread_alive(t): if not self._running_thread_ids: # Fix multiprocessing debug with breakpoints in both main and child processes # (https://youtrack.jetbrains.com/issue/PY-17092) When the new process is created, the main # thread in the new process already has the attribute 'pydevd_id', so the new thread doesn't # get new id with its process number and the debugger loses access to both threads. # Therefore we should update thread_id for every main thread in the new process. # TODO: Investigate: should we do this for all threads in threading.enumerate()? # (i.e.: if a fork happens on Linux, this seems likely). old_thread_id = get_thread_id(t) clear_cached_thread_id(t) clear_cached_thread_id(threadingCurrentThread()) thread_id = get_thread_id(t) curr_thread_id = get_thread_id(threadingCurrentThread()) if pydevd_vars.has_additional_frames_by_id(old_thread_id): frames_by_id = pydevd_vars.get_additional_frames_by_id(old_thread_id) pydevd_vars.add_additional_frame_by_id(thread_id, frames_by_id) else: thread_id = get_thread_id(t) program_threads_alive[thread_id] = t if not dict_contains(self._running_thread_ids, thread_id): if not hasattr(t, 'additional_info'): # see http://sourceforge.net/tracker/index.php?func=detail&aid=1955428&group_id=85796&atid=577329 # Let's create the additional info right away! t.additional_info = PyDBAdditionalThreadInfo() self._running_thread_ids[thread_id] = t self.writer.add_command(self.cmd_factory.make_thread_created_message(t)) queue = self.get_internal_queue(thread_id) cmdsToReadd = [] # some commands must be processed by the thread itself... if that's the case, # we will re-add the commands to the queue after executing. try: while True: int_cmd = queue.get(False) if not self.mpl_hooks_in_debug_console and isinstance(int_cmd, InternalConsoleExec): # add import hooks for matplotlib patches if only debug console was started try: self.init_matplotlib_in_debug_console() self.mpl_in_use = True except: pydevd_log(2, "Matplotlib support in debug console failed", traceback.format_exc()) self.mpl_hooks_in_debug_console = True if int_cmd.can_be_executed_by(curr_thread_id): pydevd_log(2, "processing internal command ", str(int_cmd)) int_cmd.do_it(self) else: pydevd_log(2, "NOT processing internal command ", str(int_cmd)) cmdsToReadd.append(int_cmd) except _queue.Empty: #@UndefinedVariable for int_cmd in cmdsToReadd: queue.put(int_cmd) # this is how we exit thread_ids = list(self._running_thread_ids.keys()) for tId in thread_ids: if not dict_contains(program_threads_alive, tId): program_threads_dead.append(tId) finally: self._lock_running_thread_ids.release() for tId in program_threads_dead: try: self._process_thread_not_alive(tId) except: sys.stderr.write('Error iterating through %s (%s) - %s\n' % ( program_threads_alive, program_threads_alive.__class__, dir(program_threads_alive))) raise if len(program_threads_alive) == 0: self.finish_debugging_session() for t in all_threads: if hasattr(t, 'do_kill_pydev_thread'): t.do_kill_pydev_thread() finally: self._main_lock.release() def set_tracing_for_untraced_contexts(self, ignore_frame=None, overwrite_prev_trace=False): # Enable the tracing for existing threads (because there may be frames being executed that # are currently untraced). threads = threadingEnumerate() try: for t in threads: if getattr(t, 'is_pydev_daemon_thread', False): continue # TODO: optimize so that we only actually add that tracing if it's in # the new breakpoint context. additional_info = None try: additional_info = t.additional_info except AttributeError: pass # that's ok, no info currently set if additional_info is not None: for frame in additional_info.iter_frames(t): if frame is not ignore_frame: self.set_trace_for_frame_and_parents(frame, overwrite_prev_trace=overwrite_prev_trace) finally: frame = None t = None threads = None additional_info = None def consolidate_breakpoints(self, file, id_to_breakpoint, breakpoints): break_dict = {} for breakpoint_id, pybreakpoint in dict_iter_items(id_to_breakpoint): break_dict[pybreakpoint.line] = pybreakpoint breakpoints[file] = break_dict def add_break_on_exception( self, exception, notify_always, notify_on_terminate, notify_on_first_raise_only, ignore_libraries=False ): try: eb = ExceptionBreakpoint( exception, notify_always, notify_on_terminate, notify_on_first_raise_only, ignore_libraries ) except ImportError: pydev_log.error("Error unable to add break on exception for: %s (exception could not be imported)\n" % (exception,)) return None if eb.notify_on_terminate: cp = self.break_on_uncaught_exceptions.copy() cp[exception] = eb if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0: pydev_log.error("Exceptions to hook on terminate: %s\n" % (cp,)) self.break_on_uncaught_exceptions = cp if eb.notify_always: cp = self.break_on_caught_exceptions.copy() cp[exception] = eb if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0: pydev_log.error("Exceptions to hook always: %s\n" % (cp,)) self.break_on_caught_exceptions = cp return eb def update_after_exceptions_added(self, added): updated_on_caught = False updated_on_uncaught = False for eb in added: if not updated_on_uncaught and eb.notify_on_terminate: updated_on_uncaught = True update_exception_hook(self) if not updated_on_caught and eb.notify_always: updated_on_caught = True self.set_tracing_for_untraced_contexts() def _process_thread_not_alive(self, threadId): """ if thread is not alive, cancel trace_dispatch processing """ self._lock_running_thread_ids.acquire() try: thread = self._running_thread_ids.pop(threadId, None) if thread is None: return wasNotified = thread.additional_info.pydev_notify_kill if not wasNotified: thread.additional_info.pydev_notify_kill = True finally: self._lock_running_thread_ids.release() cmd = self.cmd_factory.make_thread_killed_message(threadId) self.writer.add_command(cmd) def set_suspend(self, thread, stop_reason): thread.additional_info.suspend_type = PYTHON_SUSPEND thread.additional_info.pydev_state = STATE_SUSPEND thread.stop_reason = stop_reason # If conditional breakpoint raises any exception during evaluation send details to Java if stop_reason == CMD_SET_BREAK and self.suspend_on_breakpoint_exception: self._send_breakpoint_condition_exception(thread) def _send_breakpoint_condition_exception(self, thread): """If conditional breakpoint raises an exception during evaluation send exception details to java """ thread_id = get_thread_id(thread) conditional_breakpoint_exception_tuple = thread.additional_info.conditional_breakpoint_exception # conditional_breakpoint_exception_tuple - should contain 2 values (exception_type, stacktrace) if conditional_breakpoint_exception_tuple and len(conditional_breakpoint_exception_tuple) == 2: exc_type, stacktrace = conditional_breakpoint_exception_tuple int_cmd = InternalGetBreakpointException(thread_id, exc_type, stacktrace) # Reset the conditional_breakpoint_exception details to None thread.additional_info.conditional_breakpoint_exception = None self.post_internal_command(int_cmd, thread_id) def send_caught_exception_stack(self, thread, arg, curr_frame_id): """Sends details on the exception which was caught (and where we stopped) to the java side. arg is: exception type, description, traceback object """ thread_id = get_thread_id(thread) int_cmd = InternalSendCurrExceptionTrace(thread_id, arg, curr_frame_id) self.post_internal_command(int_cmd, thread_id) def send_caught_exception_stack_proceeded(self, thread): """Sends that some thread was resumed and is no longer showing an exception trace. """ thread_id = get_thread_id(thread) int_cmd = InternalSendCurrExceptionTraceProceeded(thread_id) self.post_internal_command(int_cmd, thread_id) self.process_internal_commands() def do_wait_suspend(self, thread, frame, event, arg): #@UnusedVariable """ busy waits until the thread state changes to RUN it expects thread's state as attributes of the thread. Upon running, processes any outstanding Stepping commands. """ self.process_internal_commands() message = thread.additional_info.pydev_message cmd = self.cmd_factory.make_thread_suspend_message(get_thread_id(thread), frame, thread.stop_reason, message) self.writer.add_command(cmd) CustomFramesContainer.custom_frames_lock.acquire() # @UndefinedVariable try: from_this_thread = [] for frame_id, custom_frame in dict_iter_items(CustomFramesContainer.custom_frames): if custom_frame.thread_id == thread.ident: # print >> sys.stderr, 'Frame created: ', frame_id self.writer.add_command(self.cmd_factory.make_custom_frame_created_message(frame_id, custom_frame.name)) self.writer.add_command(self.cmd_factory.make_thread_suspend_message(frame_id, custom_frame.frame, CMD_THREAD_SUSPEND, "")) from_this_thread.append(frame_id) finally: CustomFramesContainer.custom_frames_lock.release() # @UndefinedVariable imported = False info = thread.additional_info if info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session: # before every stop check if matplotlib modules were imported inside script code if len(self.mpl_modules_for_patching) > 0: for module in dict_keys(self.mpl_modules_for_patching): if module in sys.modules: activate_function = dict_pop(self.mpl_modules_for_patching, module) activate_function() self.mpl_in_use = True while info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session: if self.mpl_in_use: # call input hooks if only matplotlib is in use try: if not imported: from pydev_ipython.inputhook import get_inputhook imported = True inputhook = get_inputhook() if inputhook: inputhook() except: pass self.process_internal_commands() time.sleep(0.01) # process any stepping instructions if info.pydev_step_cmd == CMD_STEP_INTO or info.pydev_step_cmd == CMD_STEP_INTO_MY_CODE: info.pydev_step_stop = None info.pydev_smart_step_stop = None elif info.pydev_step_cmd == CMD_STEP_OVER: info.pydev_step_stop = frame info.pydev_smart_step_stop = None self.set_trace_for_frame_and_parents(frame) elif info.pydev_step_cmd == CMD_SMART_STEP_INTO: self.set_trace_for_frame_and_parents(frame) info.pydev_step_stop = None info.pydev_smart_step_stop = frame elif info.pydev_step_cmd == CMD_RUN_TO_LINE or info.pydev_step_cmd == CMD_SET_NEXT_STATEMENT : self.set_trace_for_frame_and_parents(frame) if event == 'line' or event == 'exception': #If we're already in the correct context, we have to stop it now, because we can act only on #line events -- if a return was the next statement it wouldn't work (so, we have this code #repeated at pydevd_frame). stop = False curr_func_name = frame.f_code.co_name #global context is set with an empty name if curr_func_name in ('?', '<module>'): curr_func_name = '' if curr_func_name == info.pydev_func_name: line = info.pydev_next_line if frame.f_lineno == line: stop = True else : if frame.f_trace is None: frame.f_trace = self.trace_dispatch frame.f_lineno = line frame.f_trace = None stop = True if stop: info.pydev_state = STATE_SUSPEND self.do_wait_suspend(thread, frame, event, arg) return elif info.pydev_step_cmd == CMD_STEP_RETURN: back_frame = frame.f_back if back_frame is not None: # steps back to the same frame (in a return call it will stop in the 'back frame' for the user) info.pydev_step_stop = frame self.set_trace_for_frame_and_parents(frame) else: # No back frame?!? -- this happens in jython when we have some frame created from an awt event # (the previous frame would be the awt event, but this doesn't make part of 'jython', only 'java') # so, if we're doing a step return in this situation, it's the same as just making it run info.pydev_step_stop = None info.pydev_step_cmd = -1 info.pydev_state = STATE_RUN del frame cmd = self.cmd_factory.make_thread_run_message(get_thread_id(thread), info.pydev_step_cmd) self.writer.add_command(cmd) CustomFramesContainer.custom_frames_lock.acquire() # @UndefinedVariable try: # The ones that remained on last_running must now be removed. for frame_id in from_this_thread: # print >> sys.stderr, 'Removing created frame: ', frame_id self.writer.add_command(self.cmd_factory.make_thread_killed_message(frame_id)) finally: CustomFramesContainer.custom_frames_lock.release() # @UndefinedVariable def handle_post_mortem_stop(self, thread, frame, frames_byid, exception): pydev_log.debug("We are stopping in post-mortem\n") thread_id = get_thread_id(thread) pydevd_vars.add_additional_frame_by_id(thread_id, frames_byid) try: try: add_exception_to_frame(frame, exception) self.set_suspend(thread, CMD_ADD_EXCEPTION_BREAK) self.do_wait_suspend(thread, frame, 'exception', None) except: pydev_log.error("We've got an error while stopping in post-mortem: %s\n"%sys.exc_info()[0]) finally: pydevd_vars.remove_additional_frame_by_id(thread_id) def set_trace_for_frame_and_parents(self, frame, also_add_to_passed_frame=True, overwrite_prev_trace=False, dispatch_func=None): if dispatch_func is None: dispatch_func = self.trace_dispatch if also_add_to_passed_frame: self.update_trace(frame, dispatch_func, overwrite_prev_trace) frame = frame.f_back while frame: self.update_trace(frame, dispatch_func, overwrite_prev_trace) frame = frame.f_back del frame def update_trace(self, frame, dispatch_func, overwrite_prev): if frame.f_trace is None: frame.f_trace = dispatch_func else: if overwrite_prev: frame.f_trace = dispatch_func else: try: #If it's the trace_exception, go back to the frame trace dispatch! if frame.f_trace.im_func.__name__ == 'trace_exception': frame.f_trace = frame.f_trace.im_self.trace_dispatch except AttributeError: pass frame = frame.f_back del frame def prepare_to_run(self): ''' Shared code to prepare debugging by installing traces and registering threads ''' self.patch_threads() pydevd_tracing.SetTrace(self.trace_dispatch) PyDBCommandThread(self).start() if self.signature_factory is not None or self.thread_analyser is not None: # we need all data to be sent to IDE even after program finishes CheckOutputThread(self).start() def patch_threads(self): try: # not available in jython! threading.settrace(self.trace_dispatch) # for all future threads except: pass from _pydev_bundle.pydev_monkey import patch_thread_modules patch_thread_modules() def get_fullname(self, mod_name): if IS_PY3K: import pkgutil else: from _pydev_imps import _pydev_pkgutil_old as pkgutil try: loader = pkgutil.get_loader(mod_name) except: return None if loader is not None: for attr in ("get_filename", "_get_filename"): meth = getattr(loader, attr, None) if meth is not None: return meth(mod_name) return None def run(self, file, globals=None, locals=None, module=False, set_trace=True): if module: filename = self.get_fullname(file) if filename is None: sys.stderr.write("No module named %s\n" % file) return else: file = filename if os.path.isdir(file): new_target = os.path.join(file, '__main__.py') if os.path.isfile(new_target): file = new_target if globals is None: m = save_main_module(file, 'pydevd') globals = m.__dict__ try: globals['__builtins__'] = __builtins__ except NameError: pass # Not there on Jython... if locals is None: locals = globals if set_trace: # Predefined (writable) attributes: __name__ is the module's name; # __doc__ is the module's documentation string, or None if unavailable; # __file__ is the pathname of the file from which the module was loaded, # if it was loaded from a file. The __file__ attribute is not present for # C modules that are statically linked into the interpreter; for extension modules # loaded dynamically from a shared library, it is the pathname of the shared library file. # I think this is an ugly hack, bug it works (seems to) for the bug that says that sys.path should be the same in # debug and run. if m.__file__.startswith(sys.path[0]): # print >> sys.stderr, 'Deleting: ', sys.path[0] del sys.path[0] # now, the local directory has to be added to the pythonpath # sys.path.insert(0, os.getcwd()) # Changed: it's not the local directory, but the directory of the file launched # The file being run ust be in the pythonpath (even if it was not before) sys.path.insert(0, os.path.split(file)[0]) self.prepare_to_run() while not self.ready_to_run: time.sleep(0.1) # busy wait until we receive run command if self.thread_analyser is not None: wrap_threads() t = threadingCurrentThread() self.thread_analyser.set_start_time(cur_time()) send_message("threading_event", 0, t.getName(), get_thread_id(t), "thread", "start", file, 1, None, parent=get_thread_id(t)) if self.asyncio_analyser is not None: # we don't have main thread in asyncio graph, so we should add a fake event send_message("asyncio_event", 0, "Task", "Task", "thread", "stop", file, 1, frame=None, parent=None) try: self.init_matplotlib_support() except: sys.stderr.write("Matplotlib support in debugger failed\n") traceback.print_exc() pydev_imports.execfile(file, globals, locals) # execute the script def exiting(self): sys.stdout.flush() sys.stderr.flush() self.check_output_redirect() cmd = self.cmd_factory.make_exit_message() self.writer.add_command(cmd) def wait_for_commands(self, globals): thread = threading.currentThread() from _pydevd_bundle import pydevd_frame_utils frame = pydevd_frame_utils.Frame(None, -1, pydevd_frame_utils.FCode("Console", os.path.abspath(os.path.dirname(__file__))), globals, globals) thread_id = get_thread_id(thread) from _pydevd_bundle import pydevd_vars pydevd_vars.add_additional_frame_by_id(thread_id, {id(frame): frame}) cmd = self.cmd_factory.make_show_console_message(thread_id, frame) self.writer.add_command(cmd) while True: self.process_internal_commands() time.sleep(0.01) trace_dispatch = _trace_dispatch def set_debug(setup): setup['DEBUG_RECORD_SOCKET_READS'] = True setup['DEBUG_TRACE_BREAKPOINTS'] = 1 setup['DEBUG_TRACE_LEVEL'] = 3 def enable_qt_support(): from _pydev_bundle import pydev_monkey_qt pydev_monkey_qt.patch_qt() def process_command_line(argv): """ parses the arguments. removes our arguments from the command line """ setup = {} setup['client'] = '' setup['server'] = False setup['port'] = 0 setup['file'] = '' setup['multiproc'] = False #Used by PyCharm (reuses connection: ssh tunneling) setup['multiprocess'] = False # Used by PyDev (creates new connection to ide) setup['save-signatures'] = False setup['save-threading'] = False setup['save-asyncio'] = False setup['qt-support'] = False setup['print-in-debugger-startup'] = False setup['cmd-line'] = False setup['module'] = False i = 0 del argv[0] while (i < len(argv)): if argv[i] == '--port': del argv[i] setup['port'] = int(argv[i]) del argv[i] elif argv[i] == '--vm_type': del argv[i] setup['vm_type'] = argv[i] del argv[i] elif argv[i] == '--client': del argv[i] setup['client'] = argv[i] del argv[i] elif argv[i] == '--server': del argv[i] setup['server'] = True elif argv[i] == '--file': del argv[i] setup['file'] = argv[i] i = len(argv) # pop out, file is our last argument elif argv[i] == '--DEBUG_RECORD_SOCKET_READS': del argv[i] setup['DEBUG_RECORD_SOCKET_READS'] = True elif argv[i] == '--DEBUG': del argv[i] set_debug(setup) elif argv[i] == '--multiproc': del argv[i] setup['multiproc'] = True elif argv[i] == '--multiprocess': del argv[i] setup['multiprocess'] = True elif argv[i] == '--save-signatures': del argv[i] setup['save-signatures'] = True elif argv[i] == '--save-threading': del argv[i] setup['save-threading'] = True elif argv[i] == '--save-asyncio': del argv[i] setup['save-asyncio'] = True elif argv[i] == '--qt-support': del argv[i] setup['qt-support'] = True elif argv[i] == '--print-in-debugger-startup': del argv[i] setup['print-in-debugger-startup'] = True elif (argv[i] == '--cmd-line'): del argv[i] setup['cmd-line'] = True elif (argv[i] == '--module'): del argv[i] setup['module'] = True else: raise ValueError("unexpected option " + argv[i]) return setup def usage(doExit=0): sys.stdout.write('Usage:\n') sys.stdout.write('pydevd.py --port=N [(--client hostname) | --server] --file executable [file_options]\n') if doExit: sys.exit(0) def init_stdout_redirect(): if not getattr(sys, 'stdoutBuf', None): sys.stdoutBuf = pydevd_io.IOBuf() sys.stdout_original = sys.stdout sys.stdout = pydevd_io.IORedirector(sys.stdout, sys.stdoutBuf) #@UndefinedVariable def init_stderr_redirect(): if not getattr(sys, 'stderrBuf', None): sys.stderrBuf = pydevd_io.IOBuf() sys.stderr_original = sys.stderr sys.stderr = pydevd_io.IORedirector(sys.stderr, sys.stderrBuf) #@UndefinedVariable def has_data_to_redirect(): if getattr(sys, 'stdoutBuf', None): if not sys.stdoutBuf.empty(): return True if getattr(sys, 'stderrBuf', None): if not sys.stderrBuf.empty(): return True return False #======================================================================================================================= # settrace #======================================================================================================================= def settrace( host=None, stdoutToServer=False, stderrToServer=False, port=5678, suspend=True, trace_only_current_thread=False, overwrite_prev_trace=False, patch_multiprocessing=False, ): '''Sets the tracing function with the pydev debug function and initializes needed facilities. @param host: the user may specify another host, if the debug server is not in the same machine (default is the local host) @param stdoutToServer: when this is true, the stdout is passed to the debug server @param stderrToServer: when this is true, the stderr is passed to the debug server so that they are printed in its console and not in this process console. @param port: specifies which port to use for communicating with the server (note that the server must be started in the same port). @note: currently it's hard-coded at 5678 in the client @param suspend: whether a breakpoint should be emulated as soon as this function is called. @param trace_only_current_thread: determines if only the current thread will be traced or all current and future threads will also have the tracing enabled. @param overwrite_prev_trace: if True we'll reset the frame.f_trace of frames which are already being traced @param patch_multiprocessing: if True we'll patch the functions which create new processes so that launched processes are debugged. ''' _set_trace_lock.acquire() try: _locked_settrace( host, stdoutToServer, stderrToServer, port, suspend, trace_only_current_thread, overwrite_prev_trace, patch_multiprocessing, ) finally: _set_trace_lock.release() _set_trace_lock = thread.allocate_lock() def _locked_settrace( host, stdoutToServer, stderrToServer, port, suspend, trace_only_current_thread, overwrite_prev_trace, patch_multiprocessing, ): if patch_multiprocessing: try: from _pydev_bundle import pydev_monkey except: pass else: pydev_monkey.patch_new_process_functions() if host is None: from _pydev_bundle import pydev_localhost host = pydev_localhost.get_localhost() global connected global bufferStdOutToServer global bufferStdErrToServer if not connected : pydevd_vm_type.setup_type() debugger = PyDB() debugger.connect(host, port) # Note: connect can raise error. # Mark connected only if it actually succeeded. connected = True bufferStdOutToServer = stdoutToServer bufferStdErrToServer = stderrToServer if bufferStdOutToServer: init_stdout_redirect() if bufferStdErrToServer: init_stderr_redirect() debugger.set_trace_for_frame_and_parents(get_frame(), False, overwrite_prev_trace=overwrite_prev_trace) CustomFramesContainer.custom_frames_lock.acquire() # @UndefinedVariable try: for _frameId, custom_frame in dict_iter_items(CustomFramesContainer.custom_frames): debugger.set_trace_for_frame_and_parents(custom_frame.frame, False) finally: CustomFramesContainer.custom_frames_lock.release() # @UndefinedVariable t = threadingCurrentThread() try: additional_info = t.additional_info except AttributeError: additional_info = PyDBAdditionalThreadInfo() t.additional_info = additional_info while not debugger.ready_to_run: time.sleep(0.1) # busy wait until we receive run command # note that we do that through pydevd_tracing.SetTrace so that the tracing # is not warned to the user! pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: # Trace future threads? debugger.patch_threads() # As this is the first connection, also set tracing for any untraced threads debugger.set_tracing_for_untraced_contexts(ignore_frame=get_frame(), overwrite_prev_trace=overwrite_prev_trace) # Stop the tracing as the last thing before the actual shutdown for a clean exit. atexit.register(stoptrace) PyDBCommandThread(debugger).start() CheckOutputThread(debugger).start() #Suspend as the last thing after all tracing is in place. if suspend: debugger.set_suspend(t, CMD_THREAD_SUSPEND) else: # ok, we're already in debug mode, with all set, so, let's just set the break debugger = get_global_debugger() debugger.set_trace_for_frame_and_parents(get_frame(), False) t = threadingCurrentThread() try: additional_info = t.additional_info except AttributeError: additional_info = PyDBAdditionalThreadInfo() t.additional_info = additional_info pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: # Trace future threads? debugger.patch_threads() if suspend: debugger.set_suspend(t, CMD_THREAD_SUSPEND) def stoptrace(): global connected if connected: pydevd_tracing.restore_sys_set_trace_func() sys.settrace(None) try: #not available in jython! threading.settrace(None) # for all future threads except: pass from _pydev_bundle.pydev_monkey import undo_patch_thread_modules undo_patch_thread_modules() debugger = get_global_debugger() if debugger: debugger.set_trace_for_frame_and_parents( get_frame(), also_add_to_passed_frame=True, overwrite_prev_trace=True, dispatch_func=lambda *args:None) debugger.exiting() kill_all_pydev_threads() connected = False class Dispatcher(object): def __init__(self): self.port = None def connect(self, host, port): self.host = host self.port = port self.client = start_client(self.host, self.port) self.reader = DispatchReader(self) self.reader.dontTraceMe = False #we run reader in the same thread so we don't want to loose tracing self.reader.run() def close(self): try: self.reader.do_kill_pydev_thread() except : pass class DispatchReader(ReaderThread): def __init__(self, dispatcher): self.dispatcher = dispatcher ReaderThread.__init__(self, self.dispatcher.client) def _on_run(self): dummy_thread = threading.currentThread() dummy_thread.is_pydev_daemon_thread = False return ReaderThread._on_run(self) def handle_except(self): ReaderThread.handle_except(self) def process_command(self, cmd_id, seq, text): if cmd_id == 99: self.dispatcher.port = int(text) self.killReceived = True DISPATCH_APPROACH_NEW_CONNECTION = 1 # Used by PyDev DISPATCH_APPROACH_EXISTING_CONNECTION = 2 # Used by PyCharm DISPATCH_APPROACH = DISPATCH_APPROACH_NEW_CONNECTION def dispatch(): setup = SetupHolder.setup host = setup['client'] port = setup['port'] if DISPATCH_APPROACH == DISPATCH_APPROACH_EXISTING_CONNECTION: dispatcher = Dispatcher() try: dispatcher.connect(host, port) port = dispatcher.port finally: dispatcher.close() return host, port def settrace_forked(): ''' When creating a fork from a process in the debugger, we need to reset the whole debugger environment! ''' host, port = dispatch() from _pydevd_bundle import pydevd_tracing pydevd_tracing.restore_sys_set_trace_func() if port is not None: global connected connected = False custom_frames_container_init() settrace( host, port=port, suspend=False, trace_only_current_thread=False, overwrite_prev_trace=True, patch_multiprocessing=True, ) #======================================================================================================================= # SetupHolder #======================================================================================================================= class SetupHolder: setup = None #======================================================================================================================= # main #======================================================================================================================= if __name__ == '__main__': # parse the command line. --file is our last argument that is required try: sys.original_argv = sys.argv[:] setup = process_command_line(sys.argv) SetupHolder.setup = setup except ValueError: traceback.print_exc() usage(1) if setup['print-in-debugger-startup']: try: pid = ' (pid: %s)' % os.getpid() except: pid = '' sys.stderr.write("pydev debugger: starting%s\n" % pid) fix_getpass.fix_getpass() pydev_log.debug("Executing file %s" % setup['file']) pydev_log.debug("arguments: %s"% str(sys.argv)) pydevd_vm_type.setup_type(setup.get('vm_type', None)) if os.getenv('PYCHARM_DEBUG') or os.getenv('PYDEV_DEBUG'): set_debug(setup) DebugInfoHolder.DEBUG_RECORD_SOCKET_READS = setup.get('DEBUG_RECORD_SOCKET_READS', DebugInfoHolder.DEBUG_RECORD_SOCKET_READS) DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS = setup.get('DEBUG_TRACE_BREAKPOINTS', DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS) DebugInfoHolder.DEBUG_TRACE_LEVEL = setup.get('DEBUG_TRACE_LEVEL', DebugInfoHolder.DEBUG_TRACE_LEVEL) port = setup['port'] host = setup['client'] f = setup['file'] fix_app_engine_debug = False debugger = PyDB() try: from _pydev_bundle import pydev_monkey except: pass #Not usable on jython 2.1 else: if setup['multiprocess']: # PyDev pydev_monkey.patch_new_process_functions() elif setup['multiproc']: # PyCharm pydev_log.debug("Started in multiproc mode\n") # Note: we're not inside method, so, no need for 'global' DISPATCH_APPROACH = DISPATCH_APPROACH_EXISTING_CONNECTION dispatcher = Dispatcher() try: dispatcher.connect(host, port) if dispatcher.port is not None: port = dispatcher.port pydev_log.debug("Received port %d\n" %port) pydev_log.info("pydev debugger: process %d is connecting\n"% os.getpid()) try: pydev_monkey.patch_new_process_functions() except: pydev_log.error("Error patching process functions\n") traceback.print_exc() else: pydev_log.error("pydev debugger: couldn't get port for new debug process\n") finally: dispatcher.close() else: pydev_log.info("pydev debugger: starting\n") try: pydev_monkey.patch_new_process_functions_with_warning() except: pydev_log.error("Error patching process functions\n") traceback.print_exc() # Only do this patching if we're not running with multiprocess turned on. if f.find('dev_appserver.py') != -1: if os.path.basename(f).startswith('dev_appserver.py'): appserver_dir = os.path.dirname(f) version_file = os.path.join(appserver_dir, 'VERSION') if os.path.exists(version_file): try: stream = open(version_file, 'r') try: for line in stream.read().splitlines(): line = line.strip() if line.startswith('release:'): line = line[8:].strip() version = line.replace('"', '') version = version.split('.') if int(version[0]) > 1: fix_app_engine_debug = True elif int(version[0]) == 1: if int(version[1]) >= 7: # Only fix from 1.7 onwards fix_app_engine_debug = True break finally: stream.close() except: traceback.print_exc() try: # In the default run (i.e.: run directly on debug mode), we try to patch stackless as soon as possible # on a run where we have a remote debug, we may have to be more careful because patching stackless means # that if the user already had a stackless.set_schedule_callback installed, he'd loose it and would need # to call it again (because stackless provides no way of getting the last function which was registered # in set_schedule_callback). # # So, ideally, if there's an application using stackless and the application wants to use the remote debugger # and benefit from stackless debugging, the application itself must call: # # import pydevd_stackless # pydevd_stackless.patch_stackless() # # itself to be able to benefit from seeing the tasklets created before the remote debugger is attached. from _pydevd_bundle import pydevd_stackless pydevd_stackless.patch_stackless() except: pass # It's ok not having stackless there... is_module = setup['module'] if fix_app_engine_debug: sys.stderr.write("pydev debugger: google app engine integration enabled\n") curr_dir = os.path.dirname(__file__) app_engine_startup_file = os.path.join(curr_dir, 'pydev_app_engine_debug_startup.py') sys.argv.insert(1, '--python_startup_script=' + app_engine_startup_file) import json setup['pydevd'] = __file__ sys.argv.insert(2, '--python_startup_args=%s' % json.dumps(setup),) sys.argv.insert(3, '--automatic_restart=no') sys.argv.insert(4, '--max_module_instances=1') # Run the dev_appserver debugger.run(setup['file'], None, None, is_module, set_trace=False) else: if setup['save-signatures']: if pydevd_vm_type.get_vm_type() == pydevd_vm_type.PydevdVmType.JYTHON: sys.stderr.write("Collecting run-time type information is not supported for Jython\n") else: # Only import it if we're going to use it! from _pydevd_bundle.pydevd_signature import SignatureFactory debugger.signature_factory = SignatureFactory() if setup['qt-support']: enable_qt_support() if setup['save-threading']: debugger.thread_analyser = ThreadingLogger() if setup['save-asyncio']: if IS_PY34_OLDER: debugger.asyncio_analyser = AsyncioLogger() try: debugger.connect(host, port) except: sys.stderr.write("Could not connect to %s: %s\n" % (host, port)) traceback.print_exc() sys.exit(1) connected = True # Mark that we're connected when started from inside ide. globals = debugger.run(setup['file'], None, None, is_module) if setup['cmd-line']: debugger.wait_for_commands(globals)
epl-1.0
moutai/scikit-learn
examples/gaussian_process/plot_gpr_prior_posterior.py
104
2878
""" ========================================================================== Illustration of prior and posterior Gaussian process for different kernels ========================================================================== This example illustrates the prior and posterior of a GPR with different kernels. Mean, standard deviation, and 10 samples are shown for both prior and posterior. """ print(__doc__) # Authors: Jan Hendrik Metzen <[email protected]> # # License: BSD 3 clause import numpy as np from matplotlib import pyplot as plt from sklearn.gaussian_process import GaussianProcessRegressor from sklearn.gaussian_process.kernels import (RBF, Matern, RationalQuadratic, ExpSineSquared, DotProduct, ConstantKernel) kernels = [1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0)), 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1), 1.0 * ExpSineSquared(length_scale=1.0, periodicity=3.0, length_scale_bounds=(0.1, 10.0), periodicity_bounds=(1.0, 10.0)), ConstantKernel(0.1, (0.01, 10.0)) * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.0, 10.0)) ** 2), 1.0 * Matern(length_scale=1.0, length_scale_bounds=(1e-1, 10.0), nu=1.5)] for fig_index, kernel in enumerate(kernels): # Specify Gaussian Process gp = GaussianProcessRegressor(kernel=kernel) # Plot prior plt.figure(fig_index, figsize=(8, 8)) plt.subplot(2, 1, 1) X_ = np.linspace(0, 5, 100) y_mean, y_std = gp.predict(X_[:, np.newaxis], return_std=True) plt.plot(X_, y_mean, 'k', lw=3, zorder=9) plt.fill_between(X_, y_mean - y_std, y_mean + y_std, alpha=0.5, color='k') y_samples = gp.sample_y(X_[:, np.newaxis], 10) plt.plot(X_, y_samples, lw=1) plt.xlim(0, 5) plt.ylim(-3, 3) plt.title("Prior (kernel: %s)" % kernel, fontsize=12) # Generate data and fit GP rng = np.random.RandomState(4) X = rng.uniform(0, 5, 10)[:, np.newaxis] y = np.sin((X[:, 0] - 2.5) ** 2) gp.fit(X, y) # Plot posterior plt.subplot(2, 1, 2) X_ = np.linspace(0, 5, 100) y_mean, y_std = gp.predict(X_[:, np.newaxis], return_std=True) plt.plot(X_, y_mean, 'k', lw=3, zorder=9) plt.fill_between(X_, y_mean - y_std, y_mean + y_std, alpha=0.5, color='k') y_samples = gp.sample_y(X_[:, np.newaxis], 10) plt.plot(X_, y_samples, lw=1) plt.scatter(X[:, 0], y, c='r', s=50, zorder=10) plt.xlim(0, 5) plt.ylim(-3, 3) plt.title("Posterior (kernel: %s)\n Log-Likelihood: %.3f" % (gp.kernel_, gp.log_marginal_likelihood(gp.kernel_.theta)), fontsize=12) plt.tight_layout() plt.show()
bsd-3-clause
jkthompson/nupic
external/linux32/lib/python2.6/site-packages/matplotlib/_mathtext_data.py
69
57988
""" font data tables for truetype and afm computer modern fonts """ # this dict maps symbol names to fontnames, glyphindex. To get the # glyph index from the character code, you have to use get_charmap """ from matplotlib.ft2font import FT2Font font = FT2Font('/usr/local/share/matplotlib/cmr10.ttf') items = font.get_charmap().items() items.sort() for charcode, glyphind in items: print charcode, glyphind """ latex_to_bakoma = { r'\oint' : ('cmex10', 45), r'\bigodot' : ('cmex10', 50), r'\bigoplus' : ('cmex10', 55), r'\bigotimes' : ('cmex10', 59), r'\sum' : ('cmex10', 51), r'\prod' : ('cmex10', 24), r'\int' : ('cmex10', 56), r'\bigcup' : ('cmex10', 28), r'\bigcap' : ('cmex10', 60), r'\biguplus' : ('cmex10', 32), r'\bigwedge' : ('cmex10', 4), r'\bigvee' : ('cmex10', 37), r'\coprod' : ('cmex10', 42), r'\__sqrt__' : ('cmex10', 48), r'\leftbrace' : ('cmex10', 92), r'{' : ('cmex10', 92), r'\{' : ('cmex10', 92), r'\rightbrace' : ('cmex10', 130), r'}' : ('cmex10', 130), r'\}' : ('cmex10', 130), r'\leftangle' : ('cmex10', 97), r'\rightangle' : ('cmex10', 64), r'\langle' : ('cmex10', 97), r'\rangle' : ('cmex10', 64), r'\widehat' : ('cmex10', 15), r'\widetilde' : ('cmex10', 52), r'\omega' : ('cmmi10', 29), r'\varepsilon' : ('cmmi10', 20), r'\vartheta' : ('cmmi10', 22), r'\varrho' : ('cmmi10', 61), r'\varsigma' : ('cmmi10', 41), r'\varphi' : ('cmmi10', 6), r'\leftharpoonup' : ('cmmi10', 108), r'\leftharpoondown' : ('cmmi10', 68), r'\rightharpoonup' : ('cmmi10', 117), r'\rightharpoondown' : ('cmmi10', 77), r'\triangleright' : ('cmmi10', 130), r'\triangleleft' : ('cmmi10', 89), r'.' : ('cmmi10', 51), r',' : ('cmmi10', 44), r'<' : ('cmmi10', 99), r'/' : ('cmmi10', 98), r'>' : ('cmmi10', 107), r'\flat' : ('cmmi10', 131), r'\natural' : ('cmmi10', 90), r'\sharp' : ('cmmi10', 50), r'\smile' : ('cmmi10', 97), r'\frown' : ('cmmi10', 58), r'\ell' : ('cmmi10', 102), r'\imath' : ('cmmi10', 8), r'\jmath' : ('cmmi10', 65), r'\wp' : ('cmmi10', 14), r'\alpha' : ('cmmi10', 13), r'\beta' : ('cmmi10', 35), r'\gamma' : ('cmmi10', 24), r'\delta' : ('cmmi10', 38), r'\epsilon' : ('cmmi10', 54), r'\zeta' : ('cmmi10', 10), r'\eta' : ('cmmi10', 5), r'\theta' : ('cmmi10', 18), r'\iota' : ('cmmi10', 28), r'\lambda' : ('cmmi10', 9), r'\mu' : ('cmmi10', 32), r'\nu' : ('cmmi10', 34), r'\xi' : ('cmmi10', 7), r'\pi' : ('cmmi10', 36), r'\kappa' : ('cmmi10', 30), r'\rho' : ('cmmi10', 39), r'\sigma' : ('cmmi10', 21), r'\tau' : ('cmmi10', 43), r'\upsilon' : ('cmmi10', 25), r'\phi' : ('cmmi10', 42), r'\chi' : ('cmmi10', 17), r'\psi' : ('cmmi10', 31), r'|' : ('cmsy10', 47), r'\|' : ('cmsy10', 47), r'(' : ('cmr10', 119), r'\leftparen' : ('cmr10', 119), r'\rightparen' : ('cmr10', 68), r')' : ('cmr10', 68), r'+' : ('cmr10', 76), r'0' : ('cmr10', 40), r'1' : ('cmr10', 100), r'2' : ('cmr10', 49), r'3' : ('cmr10', 110), r'4' : ('cmr10', 59), r'5' : ('cmr10', 120), r'6' : ('cmr10', 69), r'7' : ('cmr10', 127), r'8' : ('cmr10', 77), r'9' : ('cmr10', 22), r':' : ('cmr10', 85), r';' : ('cmr10', 31), r'=' : ('cmr10', 41), r'\leftbracket' : ('cmr10', 62), r'[' : ('cmr10', 62), r'\rightbracket' : ('cmr10', 72), r']' : ('cmr10', 72), r'\%' : ('cmr10', 48), r'%' : ('cmr10', 48), r'\$' : ('cmr10', 99), r'@' : ('cmr10', 111), r'\_' : ('cmtt10', 79), r'\Gamma' : ('cmr10', 19), r'\Delta' : ('cmr10', 6), r'\Theta' : ('cmr10', 7), r'\Lambda' : ('cmr10', 14), r'\Xi' : ('cmr10', 3), r'\Pi' : ('cmr10', 17), r'\Sigma' : ('cmr10', 10), r'\Upsilon' : ('cmr10', 11), r'\Phi' : ('cmr10', 9), r'\Psi' : ('cmr10', 15), r'\Omega' : ('cmr10', 12), # these are mathml names, I think. I'm just using them for the # tex methods noted r'\circumflexaccent' : ('cmr10', 124), # for \hat r'\combiningbreve' : ('cmr10', 81), # for \breve r'\combiningoverline' : ('cmr10', 131), # for \bar r'\combininggraveaccent' : ('cmr10', 114), # for \grave r'\combiningacuteaccent' : ('cmr10', 63), # for \accute r'\combiningdiaeresis' : ('cmr10', 91), # for \ddot r'\combiningtilde' : ('cmr10', 75), # for \tilde r'\combiningrightarrowabove' : ('cmmi10', 110), # for \vec r'\combiningdotabove' : ('cmr10', 26), # for \dot r'\leftarrow' : ('cmsy10', 10), r'\uparrow' : ('cmsy10', 25), r'\downarrow' : ('cmsy10', 28), r'\leftrightarrow' : ('cmsy10', 24), r'\nearrow' : ('cmsy10', 99), r'\searrow' : ('cmsy10', 57), r'\simeq' : ('cmsy10', 108), r'\Leftarrow' : ('cmsy10', 104), r'\Rightarrow' : ('cmsy10', 112), r'\Uparrow' : ('cmsy10', 60), r'\Downarrow' : ('cmsy10', 68), r'\Leftrightarrow' : ('cmsy10', 51), r'\nwarrow' : ('cmsy10', 65), r'\swarrow' : ('cmsy10', 116), r'\propto' : ('cmsy10', 15), r'\prime' : ('cmsy10', 73), r"'" : ('cmsy10', 73), r'\infty' : ('cmsy10', 32), r'\in' : ('cmsy10', 59), r'\ni' : ('cmsy10', 122), r'\bigtriangleup' : ('cmsy10', 80), r'\bigtriangledown' : ('cmsy10', 132), r'\slash' : ('cmsy10', 87), r'\forall' : ('cmsy10', 21), r'\exists' : ('cmsy10', 5), r'\neg' : ('cmsy10', 20), r'\emptyset' : ('cmsy10', 33), r'\Re' : ('cmsy10', 95), r'\Im' : ('cmsy10', 52), r'\top' : ('cmsy10', 100), r'\bot' : ('cmsy10', 11), r'\aleph' : ('cmsy10', 26), r'\cup' : ('cmsy10', 6), r'\cap' : ('cmsy10', 19), r'\uplus' : ('cmsy10', 58), r'\wedge' : ('cmsy10', 43), r'\vee' : ('cmsy10', 96), r'\vdash' : ('cmsy10', 109), r'\dashv' : ('cmsy10', 66), r'\lfloor' : ('cmsy10', 117), r'\rfloor' : ('cmsy10', 74), r'\lceil' : ('cmsy10', 123), r'\rceil' : ('cmsy10', 81), r'\lbrace' : ('cmsy10', 92), r'\rbrace' : ('cmsy10', 105), r'\mid' : ('cmsy10', 47), r'\vert' : ('cmsy10', 47), r'\Vert' : ('cmsy10', 44), r'\updownarrow' : ('cmsy10', 94), r'\Updownarrow' : ('cmsy10', 53), r'\backslash' : ('cmsy10', 126), r'\wr' : ('cmsy10', 101), r'\nabla' : ('cmsy10', 110), r'\sqcup' : ('cmsy10', 67), r'\sqcap' : ('cmsy10', 118), r'\sqsubseteq' : ('cmsy10', 75), r'\sqsupseteq' : ('cmsy10', 124), r'\S' : ('cmsy10', 129), r'\dag' : ('cmsy10', 71), r'\ddag' : ('cmsy10', 127), r'\P' : ('cmsy10', 130), r'\clubsuit' : ('cmsy10', 18), r'\diamondsuit' : ('cmsy10', 34), r'\heartsuit' : ('cmsy10', 22), r'-' : ('cmsy10', 17), r'\cdot' : ('cmsy10', 78), r'\times' : ('cmsy10', 13), r'*' : ('cmsy10', 9), r'\ast' : ('cmsy10', 9), r'\div' : ('cmsy10', 31), r'\diamond' : ('cmsy10', 48), r'\pm' : ('cmsy10', 8), r'\mp' : ('cmsy10', 98), r'\oplus' : ('cmsy10', 16), r'\ominus' : ('cmsy10', 56), r'\otimes' : ('cmsy10', 30), r'\oslash' : ('cmsy10', 107), r'\odot' : ('cmsy10', 64), r'\bigcirc' : ('cmsy10', 115), r'\circ' : ('cmsy10', 72), r'\bullet' : ('cmsy10', 84), r'\asymp' : ('cmsy10', 121), r'\equiv' : ('cmsy10', 35), r'\subseteq' : ('cmsy10', 103), r'\supseteq' : ('cmsy10', 42), r'\leq' : ('cmsy10', 14), r'\geq' : ('cmsy10', 29), r'\preceq' : ('cmsy10', 79), r'\succeq' : ('cmsy10', 131), r'\sim' : ('cmsy10', 27), r'\approx' : ('cmsy10', 23), r'\subset' : ('cmsy10', 50), r'\supset' : ('cmsy10', 86), r'\ll' : ('cmsy10', 85), r'\gg' : ('cmsy10', 40), r'\prec' : ('cmsy10', 93), r'\succ' : ('cmsy10', 49), r'\rightarrow' : ('cmsy10', 12), r'\to' : ('cmsy10', 12), r'\spadesuit' : ('cmsy10', 7), } latex_to_cmex = { r'\__sqrt__' : 112, r'\bigcap' : 92, r'\bigcup' : 91, r'\bigodot' : 75, r'\bigoplus' : 77, r'\bigotimes' : 79, r'\biguplus' : 93, r'\bigvee' : 95, r'\bigwedge' : 94, r'\coprod' : 97, r'\int' : 90, r'\leftangle' : 173, r'\leftbrace' : 169, r'\oint' : 73, r'\prod' : 89, r'\rightangle' : 174, r'\rightbrace' : 170, r'\sum' : 88, r'\widehat' : 98, r'\widetilde' : 101, } latex_to_standard = { r'\cong' : ('psyr', 64), r'\Delta' : ('psyr', 68), r'\Phi' : ('psyr', 70), r'\Gamma' : ('psyr', 89), r'\alpha' : ('psyr', 97), r'\beta' : ('psyr', 98), r'\chi' : ('psyr', 99), r'\delta' : ('psyr', 100), r'\varepsilon' : ('psyr', 101), r'\phi' : ('psyr', 102), r'\gamma' : ('psyr', 103), r'\eta' : ('psyr', 104), r'\iota' : ('psyr', 105), r'\varpsi' : ('psyr', 106), r'\kappa' : ('psyr', 108), r'\nu' : ('psyr', 110), r'\pi' : ('psyr', 112), r'\theta' : ('psyr', 113), r'\rho' : ('psyr', 114), r'\sigma' : ('psyr', 115), r'\tau' : ('psyr', 116), r'\upsilon' : ('psyr', 117), r'\varpi' : ('psyr', 118), r'\omega' : ('psyr', 119), r'\xi' : ('psyr', 120), r'\psi' : ('psyr', 121), r'\zeta' : ('psyr', 122), r'\sim' : ('psyr', 126), r'\leq' : ('psyr', 163), r'\infty' : ('psyr', 165), r'\clubsuit' : ('psyr', 167), r'\diamondsuit' : ('psyr', 168), r'\heartsuit' : ('psyr', 169), r'\spadesuit' : ('psyr', 170), r'\leftrightarrow' : ('psyr', 171), r'\leftarrow' : ('psyr', 172), r'\uparrow' : ('psyr', 173), r'\rightarrow' : ('psyr', 174), r'\downarrow' : ('psyr', 175), r'\pm' : ('psyr', 176), r'\geq' : ('psyr', 179), r'\times' : ('psyr', 180), r'\propto' : ('psyr', 181), r'\partial' : ('psyr', 182), r'\bullet' : ('psyr', 183), r'\div' : ('psyr', 184), r'\neq' : ('psyr', 185), r'\equiv' : ('psyr', 186), r'\approx' : ('psyr', 187), r'\ldots' : ('psyr', 188), r'\aleph' : ('psyr', 192), r'\Im' : ('psyr', 193), r'\Re' : ('psyr', 194), r'\wp' : ('psyr', 195), r'\otimes' : ('psyr', 196), r'\oplus' : ('psyr', 197), r'\oslash' : ('psyr', 198), r'\cap' : ('psyr', 199), r'\cup' : ('psyr', 200), r'\supset' : ('psyr', 201), r'\supseteq' : ('psyr', 202), r'\subset' : ('psyr', 204), r'\subseteq' : ('psyr', 205), r'\in' : ('psyr', 206), r'\notin' : ('psyr', 207), r'\angle' : ('psyr', 208), r'\nabla' : ('psyr', 209), r'\textregistered' : ('psyr', 210), r'\copyright' : ('psyr', 211), r'\texttrademark' : ('psyr', 212), r'\Pi' : ('psyr', 213), r'\prod' : ('psyr', 213), r'\surd' : ('psyr', 214), r'\__sqrt__' : ('psyr', 214), r'\cdot' : ('psyr', 215), r'\urcorner' : ('psyr', 216), r'\vee' : ('psyr', 217), r'\wedge' : ('psyr', 218), r'\Leftrightarrow' : ('psyr', 219), r'\Leftarrow' : ('psyr', 220), r'\Uparrow' : ('psyr', 221), r'\Rightarrow' : ('psyr', 222), r'\Downarrow' : ('psyr', 223), r'\Diamond' : ('psyr', 224), r'\langle' : ('psyr', 225), r'\Sigma' : ('psyr', 229), r'\sum' : ('psyr', 229), r'\forall' : ('psyr', 34), r'\exists' : ('psyr', 36), r'\lceil' : ('psyr', 233), r'\lbrace' : ('psyr', 123), r'\Psi' : ('psyr', 89), r'\bot' : ('psyr', 0136), r'\Omega' : ('psyr', 0127), r'\leftbracket' : ('psyr', 0133), r'\rightbracket' : ('psyr', 0135), r'\leftbrace' : ('psyr', 123), r'\leftparen' : ('psyr', 050), r'\prime' : ('psyr', 0242), r'\sharp' : ('psyr', 043), r'\slash' : ('psyr', 057), r'\Lamda' : ('psyr', 0114), r'\neg' : ('psyr', 0330), r'\Upsilon' : ('psyr', 0241), r'\rightbrace' : ('psyr', 0175), r'\rfloor' : ('psyr', 0373), r'\lambda' : ('psyr', 0154), r'\to' : ('psyr', 0256), r'\Xi' : ('psyr', 0130), r'\emptyset' : ('psyr', 0306), r'\lfloor' : ('psyr', 0353), r'\rightparen' : ('psyr', 051), r'\rceil' : ('psyr', 0371), r'\ni' : ('psyr', 047), r'\epsilon' : ('psyr', 0145), r'\Theta' : ('psyr', 0121), r'\langle' : ('psyr', 0341), r'\leftangle' : ('psyr', 0341), r'\rangle' : ('psyr', 0361), r'\rightangle' : ('psyr', 0361), r'\rbrace' : ('psyr', 0175), r'\circ' : ('psyr', 0260), r'\diamond' : ('psyr', 0340), r'\mu' : ('psyr', 0155), r'\mid' : ('psyr', 0352), r'\imath' : ('pncri8a', 105), r'\%' : ('pncr8a', 37), r'\$' : ('pncr8a', 36), r'\{' : ('pncr8a', 123), r'\}' : ('pncr8a', 125), r'\backslash' : ('pncr8a', 92), r'\ast' : ('pncr8a', 42), r'\circumflexaccent' : ('pncri8a', 124), # for \hat r'\combiningbreve' : ('pncri8a', 81), # for \breve r'\combininggraveaccent' : ('pncri8a', 114), # for \grave r'\combiningacuteaccent' : ('pncri8a', 63), # for \accute r'\combiningdiaeresis' : ('pncri8a', 91), # for \ddot r'\combiningtilde' : ('pncri8a', 75), # for \tilde r'\combiningrightarrowabove' : ('pncri8a', 110), # for \vec r'\combiningdotabove' : ('pncri8a', 26), # for \dot } # Automatically generated. type12uni = {'uni24C8': 9416, 'aring': 229, 'uni22A0': 8864, 'uni2292': 8850, 'quotedblright': 8221, 'uni03D2': 978, 'uni2215': 8725, 'uni03D0': 976, 'V': 86, 'dollar': 36, 'uni301E': 12318, 'uni03D5': 981, 'four': 52, 'uni25A0': 9632, 'uni013C': 316, 'uni013B': 315, 'uni013E': 318, 'Yacute': 221, 'uni25DE': 9694, 'uni013F': 319, 'uni255A': 9562, 'uni2606': 9734, 'uni0180': 384, 'uni22B7': 8887, 'uni044F': 1103, 'uni22B5': 8885, 'uni22B4': 8884, 'uni22AE': 8878, 'uni22B2': 8882, 'uni22B1': 8881, 'uni22B0': 8880, 'uni25CD': 9677, 'uni03CE': 974, 'uni03CD': 973, 'uni03CC': 972, 'uni03CB': 971, 'uni03CA': 970, 'uni22B8': 8888, 'uni22C9': 8905, 'uni0449': 1097, 'uni20DD': 8413, 'uni20DC': 8412, 'uni20DB': 8411, 'uni2231': 8753, 'uni25CF': 9679, 'uni306E': 12398, 'uni03D1': 977, 'uni01A1': 417, 'uni20D7': 8407, 'uni03D6': 982, 'uni2233': 8755, 'uni20D2': 8402, 'uni20D1': 8401, 'uni20D0': 8400, 'P': 80, 'uni22BE': 8894, 'uni22BD': 8893, 'uni22BC': 8892, 'uni22BB': 8891, 'underscore': 95, 'uni03C8': 968, 'uni03C7': 967, 'uni0328': 808, 'uni03C5': 965, 'uni03C4': 964, 'uni03C3': 963, 'uni03C2': 962, 'uni03C1': 961, 'uni03C0': 960, 'uni2010': 8208, 'uni0130': 304, 'uni0133': 307, 'uni0132': 306, 'uni0135': 309, 'uni0134': 308, 'uni0137': 311, 'uni0136': 310, 'uni0139': 313, 'uni0138': 312, 'uni2244': 8772, 'uni229A': 8858, 'uni2571': 9585, 'uni0278': 632, 'uni2239': 8761, 'p': 112, 'uni3019': 12313, 'uni25CB': 9675, 'uni03DB': 987, 'uni03DC': 988, 'uni03DA': 986, 'uni03DF': 991, 'uni03DD': 989, 'uni013D': 317, 'uni220A': 8714, 'uni220C': 8716, 'uni220B': 8715, 'uni220E': 8718, 'uni220D': 8717, 'uni220F': 8719, 'uni22CC': 8908, 'Otilde': 213, 'uni25E5': 9701, 'uni2736': 10038, 'perthousand': 8240, 'zero': 48, 'uni279B': 10139, 'dotlessi': 305, 'uni2279': 8825, 'Scaron': 352, 'zcaron': 382, 'uni21D8': 8664, 'egrave': 232, 'uni0271': 625, 'uni01AA': 426, 'uni2332': 9010, 'section': 167, 'uni25E4': 9700, 'Icircumflex': 206, 'ntilde': 241, 'uni041E': 1054, 'ampersand': 38, 'uni041C': 1052, 'uni041A': 1050, 'uni22AB': 8875, 'uni21DB': 8667, 'dotaccent': 729, 'uni0416': 1046, 'uni0417': 1047, 'uni0414': 1044, 'uni0415': 1045, 'uni0412': 1042, 'uni0413': 1043, 'degree': 176, 'uni0411': 1041, 'K': 75, 'uni25EB': 9707, 'uni25EF': 9711, 'uni0418': 1048, 'uni0419': 1049, 'uni2263': 8803, 'uni226E': 8814, 'uni2251': 8785, 'uni02C8': 712, 'uni2262': 8802, 'acircumflex': 226, 'uni22B3': 8883, 'uni2261': 8801, 'uni2394': 9108, 'Aring': 197, 'uni2260': 8800, 'uni2254': 8788, 'uni0436': 1078, 'uni2267': 8807, 'k': 107, 'uni22C8': 8904, 'uni226A': 8810, 'uni231F': 8991, 'smalltilde': 732, 'uni2201': 8705, 'uni2200': 8704, 'uni2203': 8707, 'uni02BD': 701, 'uni2205': 8709, 'uni2204': 8708, 'Agrave': 192, 'uni2206': 8710, 'uni2209': 8713, 'uni2208': 8712, 'uni226D': 8813, 'uni2264': 8804, 'uni263D': 9789, 'uni2258': 8792, 'uni02D3': 723, 'uni02D2': 722, 'uni02D1': 721, 'uni02D0': 720, 'uni25E1': 9697, 'divide': 247, 'uni02D5': 725, 'uni02D4': 724, 'ocircumflex': 244, 'uni2524': 9508, 'uni043A': 1082, 'uni24CC': 9420, 'asciitilde': 126, 'uni22B9': 8889, 'uni24D2': 9426, 'uni211E': 8478, 'uni211D': 8477, 'uni24DD': 9437, 'uni211A': 8474, 'uni211C': 8476, 'uni211B': 8475, 'uni25C6': 9670, 'uni017F': 383, 'uni017A': 378, 'uni017C': 380, 'uni017B': 379, 'uni0346': 838, 'uni22F1': 8945, 'uni22F0': 8944, 'two': 50, 'uni2298': 8856, 'uni24D1': 9425, 'E': 69, 'uni025D': 605, 'scaron': 353, 'uni2322': 8994, 'uni25E3': 9699, 'uni22BF': 8895, 'F': 70, 'uni0440': 1088, 'uni255E': 9566, 'uni22BA': 8890, 'uni0175': 373, 'uni0174': 372, 'uni0177': 375, 'uni0176': 374, 'bracketleft': 91, 'uni0170': 368, 'uni0173': 371, 'uni0172': 370, 'asciicircum': 94, 'uni0179': 377, 'uni2590': 9616, 'uni25E2': 9698, 'uni2119': 8473, 'uni2118': 8472, 'uni25CC': 9676, 'f': 102, 'ordmasculine': 186, 'uni229B': 8859, 'uni22A1': 8865, 'uni2111': 8465, 'uni2110': 8464, 'uni2113': 8467, 'uni2112': 8466, 'mu': 181, 'uni2281': 8833, 'paragraph': 182, 'nine': 57, 'uni25EC': 9708, 'v': 118, 'uni040C': 1036, 'uni0113': 275, 'uni22D0': 8912, 'uni21CC': 8652, 'uni21CB': 8651, 'uni21CA': 8650, 'uni22A5': 8869, 'uni21CF': 8655, 'uni21CE': 8654, 'uni21CD': 8653, 'guilsinglleft': 8249, 'backslash': 92, 'uni2284': 8836, 'uni224E': 8782, 'uni224D': 8781, 'uni224F': 8783, 'uni224A': 8778, 'uni2287': 8839, 'uni224C': 8780, 'uni224B': 8779, 'uni21BD': 8637, 'uni2286': 8838, 'uni030F': 783, 'uni030D': 781, 'uni030E': 782, 'uni030B': 779, 'uni030C': 780, 'uni030A': 778, 'uni026E': 622, 'uni026D': 621, 'six': 54, 'uni026A': 618, 'uni026C': 620, 'uni25C1': 9665, 'uni20D6': 8406, 'uni045B': 1115, 'uni045C': 1116, 'uni256B': 9579, 'uni045A': 1114, 'uni045F': 1119, 'uni045E': 1118, 'A': 65, 'uni2569': 9577, 'uni0458': 1112, 'uni0459': 1113, 'uni0452': 1106, 'uni0453': 1107, 'uni2562': 9570, 'uni0451': 1105, 'uni0456': 1110, 'uni0457': 1111, 'uni0454': 1108, 'uni0455': 1109, 'icircumflex': 238, 'uni0307': 775, 'uni0304': 772, 'uni0305': 773, 'uni0269': 617, 'uni0268': 616, 'uni0300': 768, 'uni0301': 769, 'uni0265': 613, 'uni0264': 612, 'uni0267': 615, 'uni0266': 614, 'uni0261': 609, 'uni0260': 608, 'uni0263': 611, 'uni0262': 610, 'a': 97, 'uni2207': 8711, 'uni2247': 8775, 'uni2246': 8774, 'uni2241': 8769, 'uni2240': 8768, 'uni2243': 8771, 'uni2242': 8770, 'uni2312': 8978, 'ogonek': 731, 'uni2249': 8777, 'uni2248': 8776, 'uni3030': 12336, 'q': 113, 'uni21C2': 8642, 'uni21C1': 8641, 'uni21C0': 8640, 'uni21C7': 8647, 'uni21C6': 8646, 'uni21C5': 8645, 'uni21C4': 8644, 'uni225F': 8799, 'uni212C': 8492, 'uni21C8': 8648, 'uni2467': 9319, 'oacute': 243, 'uni028F': 655, 'uni028E': 654, 'uni026F': 623, 'uni028C': 652, 'uni028B': 651, 'uni028A': 650, 'uni2510': 9488, 'ograve': 242, 'edieresis': 235, 'uni22CE': 8910, 'uni22CF': 8911, 'uni219F': 8607, 'comma': 44, 'uni22CA': 8906, 'uni0429': 1065, 'uni03C6': 966, 'uni0427': 1063, 'uni0426': 1062, 'uni0425': 1061, 'uni0424': 1060, 'uni0423': 1059, 'uni0422': 1058, 'uni0421': 1057, 'uni0420': 1056, 'uni2465': 9317, 'uni24D0': 9424, 'uni2464': 9316, 'uni0430': 1072, 'otilde': 245, 'uni2661': 9825, 'uni24D6': 9430, 'uni2466': 9318, 'uni24D5': 9429, 'uni219A': 8602, 'uni2518': 9496, 'uni22B6': 8886, 'uni2461': 9313, 'uni24D4': 9428, 'uni2460': 9312, 'uni24EA': 9450, 'guillemotright': 187, 'ecircumflex': 234, 'greater': 62, 'uni2011': 8209, 'uacute': 250, 'uni2462': 9314, 'L': 76, 'bullet': 8226, 'uni02A4': 676, 'uni02A7': 679, 'cedilla': 184, 'uni02A2': 674, 'uni2015': 8213, 'uni22C4': 8900, 'uni22C5': 8901, 'uni22AD': 8877, 'uni22C7': 8903, 'uni22C0': 8896, 'uni2016': 8214, 'uni22C2': 8898, 'uni22C3': 8899, 'uni24CF': 9423, 'uni042F': 1071, 'uni042E': 1070, 'uni042D': 1069, 'ydieresis': 255, 'l': 108, 'logicalnot': 172, 'uni24CA': 9418, 'uni0287': 647, 'uni0286': 646, 'uni0285': 645, 'uni0284': 644, 'uni0283': 643, 'uni0282': 642, 'uni0281': 641, 'uni027C': 636, 'uni2664': 9828, 'exclamdown': 161, 'uni25C4': 9668, 'uni0289': 649, 'uni0288': 648, 'uni039A': 922, 'endash': 8211, 'uni2640': 9792, 'uni20E4': 8420, 'uni0473': 1139, 'uni20E1': 8417, 'uni2642': 9794, 'uni03B8': 952, 'uni03B9': 953, 'agrave': 224, 'uni03B4': 948, 'uni03B5': 949, 'uni03B6': 950, 'uni03B7': 951, 'uni03B0': 944, 'uni03B1': 945, 'uni03B2': 946, 'uni03B3': 947, 'uni2555': 9557, 'Adieresis': 196, 'germandbls': 223, 'Odieresis': 214, 'space': 32, 'uni0126': 294, 'uni0127': 295, 'uni0124': 292, 'uni0125': 293, 'uni0122': 290, 'uni0123': 291, 'uni0120': 288, 'uni0121': 289, 'quoteright': 8217, 'uni2560': 9568, 'uni2556': 9558, 'ucircumflex': 251, 'uni2561': 9569, 'uni2551': 9553, 'uni25B2': 9650, 'uni2550': 9552, 'uni2563': 9571, 'uni2553': 9555, 'G': 71, 'uni2564': 9572, 'uni2552': 9554, 'quoteleft': 8216, 'uni2565': 9573, 'uni2572': 9586, 'uni2568': 9576, 'uni2566': 9574, 'W': 87, 'uni214A': 8522, 'uni012F': 303, 'uni012D': 301, 'uni012E': 302, 'uni012B': 299, 'uni012C': 300, 'uni255C': 9564, 'uni012A': 298, 'uni2289': 8841, 'Q': 81, 'uni2320': 8992, 'uni2321': 8993, 'g': 103, 'uni03BD': 957, 'uni03BE': 958, 'uni03BF': 959, 'uni2282': 8834, 'uni2285': 8837, 'uni03BA': 954, 'uni03BB': 955, 'uni03BC': 956, 'uni2128': 8488, 'uni25B7': 9655, 'w': 119, 'uni0302': 770, 'uni03DE': 990, 'uni25DA': 9690, 'uni0303': 771, 'uni0463': 1123, 'uni0462': 1122, 'uni3018': 12312, 'uni2514': 9492, 'question': 63, 'uni25B3': 9651, 'uni24E1': 9441, 'one': 49, 'uni200A': 8202, 'uni2278': 8824, 'ring': 730, 'uni0195': 405, 'figuredash': 8210, 'uni22EC': 8940, 'uni0339': 825, 'uni0338': 824, 'uni0337': 823, 'uni0336': 822, 'uni0335': 821, 'uni0333': 819, 'uni0332': 818, 'uni0331': 817, 'uni0330': 816, 'uni01C1': 449, 'uni01C0': 448, 'uni01C3': 451, 'uni01C2': 450, 'uni2353': 9043, 'uni0308': 776, 'uni2218': 8728, 'uni2219': 8729, 'uni2216': 8726, 'uni2217': 8727, 'uni2214': 8724, 'uni0309': 777, 'uni2609': 9737, 'uni2213': 8723, 'uni2210': 8720, 'uni2211': 8721, 'uni2245': 8773, 'B': 66, 'uni25D6': 9686, 'iacute': 237, 'uni02E6': 742, 'uni02E7': 743, 'uni02E8': 744, 'uni02E9': 745, 'uni221D': 8733, 'uni221E': 8734, 'Ydieresis': 376, 'uni221C': 8732, 'uni22D7': 8919, 'uni221A': 8730, 'R': 82, 'uni24DC': 9436, 'uni033F': 831, 'uni033E': 830, 'uni033C': 828, 'uni033B': 827, 'uni033A': 826, 'b': 98, 'uni228A': 8842, 'uni22DB': 8923, 'uni2554': 9556, 'uni046B': 1131, 'uni046A': 1130, 'r': 114, 'uni24DB': 9435, 'Ccedilla': 199, 'minus': 8722, 'uni24DA': 9434, 'uni03F0': 1008, 'uni03F1': 1009, 'uni20AC': 8364, 'uni2276': 8822, 'uni24C0': 9408, 'uni0162': 354, 'uni0163': 355, 'uni011E': 286, 'uni011D': 285, 'uni011C': 284, 'uni011B': 283, 'uni0164': 356, 'uni0165': 357, 'Lslash': 321, 'uni0168': 360, 'uni0169': 361, 'uni25C9': 9673, 'uni02E5': 741, 'uni21C3': 8643, 'uni24C4': 9412, 'uni24E2': 9442, 'uni2277': 8823, 'uni013A': 314, 'uni2102': 8450, 'Uacute': 218, 'uni2317': 8983, 'uni2107': 8455, 'uni221F': 8735, 'yacute': 253, 'uni3012': 12306, 'Ucircumflex': 219, 'uni015D': 349, 'quotedbl': 34, 'uni25D9': 9689, 'uni2280': 8832, 'uni22AF': 8879, 'onehalf': 189, 'uni221B': 8731, 'Thorn': 222, 'uni2226': 8742, 'M': 77, 'uni25BA': 9658, 'uni2463': 9315, 'uni2336': 9014, 'eight': 56, 'uni2236': 8758, 'multiply': 215, 'uni210C': 8460, 'uni210A': 8458, 'uni21C9': 8649, 'grave': 96, 'uni210E': 8462, 'uni0117': 279, 'uni016C': 364, 'uni0115': 277, 'uni016A': 362, 'uni016F': 367, 'uni0112': 274, 'uni016D': 365, 'uni016E': 366, 'Ocircumflex': 212, 'uni2305': 8965, 'm': 109, 'uni24DF': 9439, 'uni0119': 281, 'uni0118': 280, 'uni20A3': 8355, 'uni20A4': 8356, 'uni20A7': 8359, 'uni2288': 8840, 'uni24C3': 9411, 'uni251C': 9500, 'uni228D': 8845, 'uni222F': 8751, 'uni222E': 8750, 'uni222D': 8749, 'uni222C': 8748, 'uni222B': 8747, 'uni222A': 8746, 'uni255B': 9563, 'Ugrave': 217, 'uni24DE': 9438, 'guilsinglright': 8250, 'uni250A': 9482, 'Ntilde': 209, 'uni0279': 633, 'questiondown': 191, 'uni256C': 9580, 'Atilde': 195, 'uni0272': 626, 'uni0273': 627, 'uni0270': 624, 'ccedilla': 231, 'uni0276': 630, 'uni0277': 631, 'uni0274': 628, 'uni0275': 629, 'uni2252': 8786, 'uni041F': 1055, 'uni2250': 8784, 'Z': 90, 'uni2256': 8790, 'uni2257': 8791, 'copyright': 169, 'uni2255': 8789, 'uni043D': 1085, 'uni043E': 1086, 'uni043F': 1087, 'yen': 165, 'uni041D': 1053, 'uni043B': 1083, 'uni043C': 1084, 'uni21B0': 8624, 'uni21B1': 8625, 'uni21B2': 8626, 'uni21B3': 8627, 'uni21B4': 8628, 'uni21B5': 8629, 'uni21B6': 8630, 'uni21B7': 8631, 'uni21B8': 8632, 'Eacute': 201, 'uni2311': 8977, 'uni2310': 8976, 'uni228F': 8847, 'uni25DB': 9691, 'uni21BA': 8634, 'uni21BB': 8635, 'uni21BC': 8636, 'uni2017': 8215, 'uni21BE': 8638, 'uni21BF': 8639, 'uni231C': 8988, 'H': 72, 'uni0293': 659, 'uni2202': 8706, 'uni22A4': 8868, 'uni231E': 8990, 'uni2232': 8754, 'uni225B': 8795, 'uni225C': 8796, 'uni24D9': 9433, 'uni225A': 8794, 'uni0438': 1080, 'uni0439': 1081, 'uni225D': 8797, 'uni225E': 8798, 'uni0434': 1076, 'X': 88, 'uni007F': 127, 'uni0437': 1079, 'Idieresis': 207, 'uni0431': 1073, 'uni0432': 1074, 'uni0433': 1075, 'uni22AC': 8876, 'uni22CD': 8909, 'uni25A3': 9635, 'bar': 124, 'uni24BB': 9403, 'uni037E': 894, 'uni027B': 635, 'h': 104, 'uni027A': 634, 'uni027F': 639, 'uni027D': 637, 'uni027E': 638, 'uni2227': 8743, 'uni2004': 8196, 'uni2225': 8741, 'uni2224': 8740, 'uni2223': 8739, 'uni2222': 8738, 'uni2221': 8737, 'uni2220': 8736, 'x': 120, 'uni2323': 8995, 'uni2559': 9561, 'uni2558': 9560, 'uni2229': 8745, 'uni2228': 8744, 'udieresis': 252, 'uni029D': 669, 'ordfeminine': 170, 'uni22CB': 8907, 'uni233D': 9021, 'uni0428': 1064, 'uni24C6': 9414, 'uni22DD': 8925, 'uni24C7': 9415, 'uni015C': 348, 'uni015B': 347, 'uni015A': 346, 'uni22AA': 8874, 'uni015F': 351, 'uni015E': 350, 'braceleft': 123, 'uni24C5': 9413, 'uni0410': 1040, 'uni03AA': 938, 'uni24C2': 9410, 'uni03AC': 940, 'uni03AB': 939, 'macron': 175, 'uni03AD': 941, 'uni03AF': 943, 'uni0294': 660, 'uni0295': 661, 'uni0296': 662, 'uni0297': 663, 'uni0290': 656, 'uni0291': 657, 'uni0292': 658, 'atilde': 227, 'Acircumflex': 194, 'uni2370': 9072, 'uni24C1': 9409, 'uni0298': 664, 'uni0299': 665, 'Oslash': 216, 'uni029E': 670, 'C': 67, 'quotedblleft': 8220, 'uni029B': 667, 'uni029C': 668, 'uni03A9': 937, 'uni03A8': 936, 'S': 83, 'uni24C9': 9417, 'uni03A1': 929, 'uni03A0': 928, 'exclam': 33, 'uni03A5': 933, 'uni03A4': 932, 'uni03A7': 935, 'Zcaron': 381, 'uni2133': 8499, 'uni2132': 8498, 'uni0159': 345, 'uni0158': 344, 'uni2137': 8503, 'uni2005': 8197, 'uni2135': 8501, 'uni2134': 8500, 'uni02BA': 698, 'uni2033': 8243, 'uni0151': 337, 'uni0150': 336, 'uni0157': 343, 'equal': 61, 'uni0155': 341, 'uni0154': 340, 's': 115, 'uni233F': 9023, 'eth': 240, 'uni24BE': 9406, 'uni21E9': 8681, 'uni2060': 8288, 'Egrave': 200, 'uni255D': 9565, 'uni24CD': 9421, 'uni21E1': 8673, 'uni21B9': 8633, 'hyphen': 45, 'uni01BE': 446, 'uni01BB': 443, 'period': 46, 'igrave': 236, 'uni01BA': 442, 'uni2296': 8854, 'uni2297': 8855, 'uni2294': 8852, 'uni2295': 8853, 'colon': 58, 'uni2293': 8851, 'uni2290': 8848, 'uni2291': 8849, 'uni032D': 813, 'uni032E': 814, 'uni032F': 815, 'uni032A': 810, 'uni032B': 811, 'uni032C': 812, 'uni231D': 8989, 'Ecircumflex': 202, 'uni24D7': 9431, 'uni25DD': 9693, 'trademark': 8482, 'Aacute': 193, 'cent': 162, 'uni0445': 1093, 'uni266E': 9838, 'uni266D': 9837, 'uni266B': 9835, 'uni03C9': 969, 'uni2003': 8195, 'uni2047': 8263, 'lslash': 322, 'uni03A6': 934, 'uni2043': 8259, 'uni250C': 9484, 'uni2040': 8256, 'uni255F': 9567, 'uni24CB': 9419, 'uni0472': 1138, 'uni0446': 1094, 'uni0474': 1140, 'uni0475': 1141, 'uni2508': 9480, 'uni2660': 9824, 'uni2506': 9478, 'uni2502': 9474, 'c': 99, 'uni2500': 9472, 'N': 78, 'uni22A6': 8870, 'uni21E7': 8679, 'uni2130': 8496, 'uni2002': 8194, 'breve': 728, 'uni0442': 1090, 'Oacute': 211, 'uni229F': 8863, 'uni25C7': 9671, 'uni229D': 8861, 'uni229E': 8862, 'guillemotleft': 171, 'uni0329': 809, 'uni24E5': 9445, 'uni011F': 287, 'uni0324': 804, 'uni0325': 805, 'uni0326': 806, 'uni0327': 807, 'uni0321': 801, 'uni0322': 802, 'n': 110, 'uni2032': 8242, 'uni2269': 8809, 'uni2268': 8808, 'uni0306': 774, 'uni226B': 8811, 'uni21EA': 8682, 'uni0166': 358, 'uni203B': 8251, 'uni01B5': 437, 'idieresis': 239, 'uni02BC': 700, 'uni01B0': 432, 'braceright': 125, 'seven': 55, 'uni02BB': 699, 'uni011A': 282, 'uni29FB': 10747, 'brokenbar': 166, 'uni2036': 8246, 'uni25C0': 9664, 'uni0156': 342, 'uni22D5': 8917, 'uni0258': 600, 'ugrave': 249, 'uni22D6': 8918, 'uni22D1': 8913, 'uni2034': 8244, 'uni22D3': 8915, 'uni22D2': 8914, 'uni203C': 8252, 'uni223E': 8766, 'uni02BF': 703, 'uni22D9': 8921, 'uni22D8': 8920, 'uni25BD': 9661, 'uni25BE': 9662, 'uni25BF': 9663, 'uni041B': 1051, 'periodcentered': 183, 'uni25BC': 9660, 'uni019E': 414, 'uni019B': 411, 'uni019A': 410, 'uni2007': 8199, 'uni0391': 913, 'uni0390': 912, 'uni0393': 915, 'uni0392': 914, 'uni0395': 917, 'uni0394': 916, 'uni0397': 919, 'uni0396': 918, 'uni0399': 921, 'uni0398': 920, 'uni25C8': 9672, 'uni2468': 9320, 'sterling': 163, 'uni22EB': 8939, 'uni039C': 924, 'uni039B': 923, 'uni039E': 926, 'uni039D': 925, 'uni039F': 927, 'I': 73, 'uni03E1': 993, 'uni03E0': 992, 'uni2319': 8985, 'uni228B': 8843, 'uni25B5': 9653, 'uni25B6': 9654, 'uni22EA': 8938, 'uni24B9': 9401, 'uni044E': 1102, 'uni0199': 409, 'uni2266': 8806, 'Y': 89, 'uni22A2': 8866, 'Eth': 208, 'uni266F': 9839, 'emdash': 8212, 'uni263B': 9787, 'uni24BD': 9405, 'uni22DE': 8926, 'uni0360': 864, 'uni2557': 9559, 'uni22DF': 8927, 'uni22DA': 8922, 'uni22DC': 8924, 'uni0361': 865, 'i': 105, 'uni24BF': 9407, 'uni0362': 866, 'uni263E': 9790, 'uni028D': 653, 'uni2259': 8793, 'uni0323': 803, 'uni2265': 8805, 'daggerdbl': 8225, 'y': 121, 'uni010A': 266, 'plusminus': 177, 'less': 60, 'uni21AE': 8622, 'uni0315': 789, 'uni230B': 8971, 'uni21AF': 8623, 'uni21AA': 8618, 'uni21AC': 8620, 'uni21AB': 8619, 'uni01FB': 507, 'uni01FC': 508, 'uni223A': 8762, 'uni01FA': 506, 'uni01FF': 511, 'uni01FD': 509, 'uni01FE': 510, 'uni2567': 9575, 'uni25E0': 9696, 'uni0104': 260, 'uni0105': 261, 'uni0106': 262, 'uni0107': 263, 'uni0100': 256, 'uni0101': 257, 'uni0102': 258, 'uni0103': 259, 'uni2038': 8248, 'uni2009': 8201, 'uni2008': 8200, 'uni0108': 264, 'uni0109': 265, 'uni02A1': 673, 'uni223B': 8763, 'uni226C': 8812, 'uni25AC': 9644, 'uni24D3': 9427, 'uni21E0': 8672, 'uni21E3': 8675, 'Udieresis': 220, 'uni21E2': 8674, 'D': 68, 'uni21E5': 8677, 'uni2621': 9761, 'uni21D1': 8657, 'uni203E': 8254, 'uni22C6': 8902, 'uni21E4': 8676, 'uni010D': 269, 'uni010E': 270, 'uni010F': 271, 'five': 53, 'T': 84, 'uni010B': 267, 'uni010C': 268, 'uni2605': 9733, 'uni2663': 9827, 'uni21E6': 8678, 'uni24B6': 9398, 'uni22C1': 8897, 'oslash': 248, 'acute': 180, 'uni01F0': 496, 'd': 100, 'OE': 338, 'uni22E3': 8931, 'Igrave': 204, 'uni2308': 8968, 'uni2309': 8969, 'uni21A9': 8617, 't': 116, 'uni2313': 8979, 'uni03A3': 931, 'uni21A4': 8612, 'uni21A7': 8615, 'uni21A6': 8614, 'uni21A1': 8609, 'uni21A0': 8608, 'uni21A3': 8611, 'uni21A2': 8610, 'parenright': 41, 'uni256A': 9578, 'uni25DC': 9692, 'uni24CE': 9422, 'uni042C': 1068, 'uni24E0': 9440, 'uni042B': 1067, 'uni0409': 1033, 'uni0408': 1032, 'uni24E7': 9447, 'uni25B4': 9652, 'uni042A': 1066, 'uni228E': 8846, 'uni0401': 1025, 'adieresis': 228, 'uni0403': 1027, 'quotesingle': 39, 'uni0405': 1029, 'uni0404': 1028, 'uni0407': 1031, 'uni0406': 1030, 'uni229C': 8860, 'uni2306': 8966, 'uni2253': 8787, 'twodotenleader': 8229, 'uni2131': 8497, 'uni21DA': 8666, 'uni2234': 8756, 'uni2235': 8757, 'uni01A5': 421, 'uni2237': 8759, 'uni2230': 8752, 'uni02CC': 716, 'slash': 47, 'uni01A0': 416, 'ellipsis': 8230, 'uni2299': 8857, 'uni2238': 8760, 'numbersign': 35, 'uni21A8': 8616, 'uni223D': 8765, 'uni01AF': 431, 'uni223F': 8767, 'uni01AD': 429, 'uni01AB': 427, 'odieresis': 246, 'uni223C': 8764, 'uni227D': 8829, 'uni0280': 640, 'O': 79, 'uni227E': 8830, 'uni21A5': 8613, 'uni22D4': 8916, 'uni25D4': 9684, 'uni227F': 8831, 'uni0435': 1077, 'uni2302': 8962, 'uni2669': 9833, 'uni24E3': 9443, 'uni2720': 10016, 'uni22A8': 8872, 'uni22A9': 8873, 'uni040A': 1034, 'uni22A7': 8871, 'oe': 339, 'uni040B': 1035, 'uni040E': 1038, 'uni22A3': 8867, 'o': 111, 'uni040F': 1039, 'Edieresis': 203, 'uni25D5': 9685, 'plus': 43, 'uni044D': 1101, 'uni263C': 9788, 'uni22E6': 8934, 'uni2283': 8835, 'uni258C': 9612, 'uni219E': 8606, 'uni24E4': 9444, 'uni2136': 8502, 'dagger': 8224, 'uni24B7': 9399, 'uni219B': 8603, 'uni22E5': 8933, 'three': 51, 'uni210B': 8459, 'uni2534': 9524, 'uni24B8': 9400, 'uni230A': 8970, 'hungarumlaut': 733, 'parenleft': 40, 'uni0148': 328, 'uni0149': 329, 'uni2124': 8484, 'uni2125': 8485, 'uni2126': 8486, 'uni2127': 8487, 'uni0140': 320, 'uni2129': 8489, 'uni25C5': 9669, 'uni0143': 323, 'uni0144': 324, 'uni0145': 325, 'uni0146': 326, 'uni0147': 327, 'uni210D': 8461, 'fraction': 8260, 'uni2031': 8241, 'uni2196': 8598, 'uni2035': 8245, 'uni24E6': 9446, 'uni016B': 363, 'uni24BA': 9402, 'uni266A': 9834, 'uni0116': 278, 'uni2115': 8469, 'registered': 174, 'J': 74, 'uni25DF': 9695, 'uni25CE': 9678, 'uni273D': 10045, 'dieresis': 168, 'uni212B': 8491, 'uni0114': 276, 'uni212D': 8493, 'uni212E': 8494, 'uni212F': 8495, 'uni014A': 330, 'uni014B': 331, 'uni014C': 332, 'uni014D': 333, 'uni014E': 334, 'uni014F': 335, 'uni025E': 606, 'uni24E8': 9448, 'uni0111': 273, 'uni24E9': 9449, 'Ograve': 210, 'j': 106, 'uni2195': 8597, 'uni2194': 8596, 'uni2197': 8599, 'uni2037': 8247, 'uni2191': 8593, 'uni2190': 8592, 'uni2193': 8595, 'uni2192': 8594, 'uni29FA': 10746, 'uni2713': 10003, 'z': 122, 'uni2199': 8601, 'uni2198': 8600, 'uni2667': 9831, 'ae': 230, 'uni0448': 1096, 'semicolon': 59, 'uni2666': 9830, 'uni038F': 911, 'uni0444': 1092, 'uni0447': 1095, 'uni038E': 910, 'uni0441': 1089, 'uni038C': 908, 'uni0443': 1091, 'uni038A': 906, 'uni0250': 592, 'uni0251': 593, 'uni0252': 594, 'uni0253': 595, 'uni0254': 596, 'at': 64, 'uni0256': 598, 'uni0257': 599, 'uni0167': 359, 'uni0259': 601, 'uni228C': 8844, 'uni2662': 9826, 'uni0319': 793, 'uni0318': 792, 'uni24BC': 9404, 'uni0402': 1026, 'uni22EF': 8943, 'Iacute': 205, 'uni22ED': 8941, 'uni22EE': 8942, 'uni0311': 785, 'uni0310': 784, 'uni21E8': 8680, 'uni0312': 786, 'percent': 37, 'uni0317': 791, 'uni0316': 790, 'uni21D6': 8662, 'uni21D7': 8663, 'uni21D4': 8660, 'uni21D5': 8661, 'uni21D2': 8658, 'uni21D3': 8659, 'uni21D0': 8656, 'uni2138': 8504, 'uni2270': 8816, 'uni2271': 8817, 'uni2272': 8818, 'uni2273': 8819, 'uni2274': 8820, 'uni2275': 8821, 'bracketright': 93, 'uni21D9': 8665, 'uni21DF': 8671, 'uni21DD': 8669, 'uni21DE': 8670, 'AE': 198, 'uni03AE': 942, 'uni227A': 8826, 'uni227B': 8827, 'uni227C': 8828, 'asterisk': 42, 'aacute': 225, 'uni226F': 8815, 'uni22E2': 8930, 'uni0386': 902, 'uni22E0': 8928, 'uni22E1': 8929, 'U': 85, 'uni22E7': 8935, 'uni22E4': 8932, 'uni0387': 903, 'uni031A': 794, 'eacute': 233, 'uni22E8': 8936, 'uni22E9': 8937, 'uni24D8': 9432, 'uni025A': 602, 'uni025B': 603, 'uni025C': 604, 'e': 101, 'uni0128': 296, 'uni025F': 607, 'uni2665': 9829, 'thorn': 254, 'uni0129': 297, 'uni253C': 9532, 'uni25D7': 9687, 'u': 117, 'uni0388': 904, 'uni0389': 905, 'uni0255': 597, 'uni0171': 369, 'uni0384': 900, 'uni0385': 901, 'uni044A': 1098, 'uni252C': 9516, 'uni044C': 1100, 'uni044B': 1099} uni2type1 = dict([(v,k) for k,v in type12uni.items()]) tex2uni = { 'widehat': 0x0302, 'widetilde': 0x0303, 'langle': 0x27e8, 'rangle': 0x27e9, 'perp': 0x27c2, 'neq': 0x2260, 'Join': 0x2a1d, 'leqslant': 0x2a7d, 'geqslant': 0x2a7e, 'lessapprox': 0x2a85, 'gtrapprox': 0x2a86, 'lesseqqgtr': 0x2a8b, 'gtreqqless': 0x2a8c, 'triangleeq': 0x225c, 'eqslantless': 0x2a95, 'eqslantgtr': 0x2a96, 'backepsilon': 0x03f6, 'precapprox': 0x2ab7, 'succapprox': 0x2ab8, 'fallingdotseq': 0x2252, 'subseteqq': 0x2ac5, 'supseteqq': 0x2ac6, 'varpropto': 0x221d, 'precnapprox': 0x2ab9, 'succnapprox': 0x2aba, 'subsetneqq': 0x2acb, 'supsetneqq': 0x2acc, 'lnapprox': 0x2ab9, 'gnapprox': 0x2aba, 'longleftarrow': 0x27f5, 'longrightarrow': 0x27f6, 'longleftrightarrow': 0x27f7, 'Longleftarrow': 0x27f8, 'Longrightarrow': 0x27f9, 'Longleftrightarrow': 0x27fa, 'longmapsto': 0x27fc, 'leadsto': 0x21dd, 'dashleftarrow': 0x290e, 'dashrightarrow': 0x290f, 'circlearrowleft': 0x21ba, 'circlearrowright': 0x21bb, 'leftrightsquigarrow': 0x21ad, 'leftsquigarrow': 0x219c, 'rightsquigarrow': 0x219d, 'Game': 0x2141, 'hbar': 0x0127, 'hslash': 0x210f, 'ldots': 0x22ef, 'vdots': 0x22ee, 'doteqdot': 0x2251, 'doteq': 8784, 'partial': 8706, 'gg': 8811, 'asymp': 8781, 'blacktriangledown': 9662, 'otimes': 8855, 'nearrow': 8599, 'varpi': 982, 'vee': 8744, 'vec': 8407, 'smile': 8995, 'succnsim': 8937, 'gimel': 8503, 'vert': 124, '|': 124, 'varrho': 1009, 'P': 182, 'approxident': 8779, 'Swarrow': 8665, 'textasciicircum': 94, 'imageof': 8887, 'ntriangleleft': 8938, 'nleq': 8816, 'div': 247, 'nparallel': 8742, 'Leftarrow': 8656, 'lll': 8920, 'oiint': 8751, 'ngeq': 8817, 'Theta': 920, 'origof': 8886, 'blacksquare': 9632, 'solbar': 9023, 'neg': 172, 'sum': 8721, 'Vdash': 8873, 'coloneq': 8788, 'degree': 176, 'bowtie': 8904, 'blacktriangleright': 9654, 'varsigma': 962, 'leq': 8804, 'ggg': 8921, 'lneqq': 8808, 'scurel': 8881, 'stareq': 8795, 'BbbN': 8469, 'nLeftarrow': 8653, 'nLeftrightarrow': 8654, 'k': 808, 'bot': 8869, 'BbbC': 8450, 'Lsh': 8624, 'leftleftarrows': 8647, 'BbbZ': 8484, 'digamma': 989, 'BbbR': 8477, 'BbbP': 8473, 'BbbQ': 8474, 'vartriangleright': 8883, 'succsim': 8831, 'wedge': 8743, 'lessgtr': 8822, 'veebar': 8891, 'mapsdown': 8615, 'Rsh': 8625, 'chi': 967, 'prec': 8826, 'nsubseteq': 8840, 'therefore': 8756, 'eqcirc': 8790, 'textexclamdown': 161, 'nRightarrow': 8655, 'flat': 9837, 'notin': 8713, 'llcorner': 8990, 'varepsilon': 949, 'bigtriangleup': 9651, 'aleph': 8501, 'dotminus': 8760, 'upsilon': 965, 'Lambda': 923, 'cap': 8745, 'barleftarrow': 8676, 'mu': 956, 'boxplus': 8862, 'mp': 8723, 'circledast': 8859, 'tau': 964, 'in': 8712, 'backslash': 92, 'varnothing': 8709, 'sharp': 9839, 'eqsim': 8770, 'gnsim': 8935, 'Searrow': 8664, 'updownarrows': 8645, 'heartsuit': 9825, 'trianglelefteq': 8884, 'ddag': 8225, 'sqsubseteq': 8849, 'mapsfrom': 8612, 'boxbar': 9707, 'sim': 8764, 'Nwarrow': 8662, 'nequiv': 8802, 'succ': 8827, 'vdash': 8866, 'Leftrightarrow': 8660, 'parallel': 8741, 'invnot': 8976, 'natural': 9838, 'ss': 223, 'uparrow': 8593, 'nsim': 8769, 'hookrightarrow': 8618, 'Equiv': 8803, 'approx': 8776, 'Vvdash': 8874, 'nsucc': 8833, 'leftrightharpoons': 8651, 'Re': 8476, 'boxminus': 8863, 'equiv': 8801, 'Lleftarrow': 8666, 'thinspace': 8201, 'll': 8810, 'Cup': 8915, 'measeq': 8798, 'upharpoonleft': 8639, 'lq': 8216, 'Upsilon': 933, 'subsetneq': 8842, 'greater': 62, 'supsetneq': 8843, 'Cap': 8914, 'L': 321, 'spadesuit': 9824, 'lrcorner': 8991, 'not': 824, 'bar': 772, 'rightharpoonaccent': 8401, 'boxdot': 8865, 'l': 322, 'leftharpoondown': 8637, 'bigcup': 8899, 'iint': 8748, 'bigwedge': 8896, 'downharpoonleft': 8643, 'textasciitilde': 126, 'subset': 8834, 'leqq': 8806, 'mapsup': 8613, 'nvDash': 8877, 'looparrowleft': 8619, 'nless': 8814, 'rightarrowbar': 8677, 'Vert': 8214, 'downdownarrows': 8650, 'uplus': 8846, 'simeq': 8771, 'napprox': 8777, 'ast': 8727, 'twoheaduparrow': 8607, 'doublebarwedge': 8966, 'Sigma': 931, 'leftharpoonaccent': 8400, 'ntrianglelefteq': 8940, 'nexists': 8708, 'times': 215, 'measuredangle': 8737, 'bumpeq': 8783, 'carriagereturn': 8629, 'adots': 8944, 'checkmark': 10003, 'lambda': 955, 'xi': 958, 'rbrace': 125, 'rbrack': 93, 'Nearrow': 8663, 'maltese': 10016, 'clubsuit': 9827, 'top': 8868, 'overarc': 785, 'varphi': 966, 'Delta': 916, 'iota': 953, 'nleftarrow': 8602, 'candra': 784, 'supset': 8835, 'triangleleft': 9665, 'gtreqless': 8923, 'ntrianglerighteq': 8941, 'quad': 8195, 'Xi': 926, 'gtrdot': 8919, 'leftthreetimes': 8907, 'minus': 8722, 'preccurlyeq': 8828, 'nleftrightarrow': 8622, 'lambdabar': 411, 'blacktriangle': 9652, 'kernelcontraction': 8763, 'Phi': 934, 'angle': 8736, 'spadesuitopen': 9828, 'eqless': 8924, 'mid': 8739, 'varkappa': 1008, 'Ldsh': 8626, 'updownarrow': 8597, 'beta': 946, 'textquotedblleft': 8220, 'rho': 961, 'alpha': 945, 'intercal': 8890, 'beth': 8502, 'grave': 768, 'acwopencirclearrow': 8634, 'nmid': 8740, 'nsupset': 8837, 'sigma': 963, 'dot': 775, 'Rightarrow': 8658, 'turnednot': 8985, 'backsimeq': 8909, 'leftarrowtail': 8610, 'approxeq': 8778, 'curlyeqsucc': 8927, 'rightarrowtail': 8611, 'Psi': 936, 'copyright': 169, 'yen': 165, 'vartriangleleft': 8882, 'rasp': 700, 'triangleright': 9655, 'precsim': 8830, 'infty': 8734, 'geq': 8805, 'updownarrowbar': 8616, 'precnsim': 8936, 'H': 779, 'ulcorner': 8988, 'looparrowright': 8620, 'ncong': 8775, 'downarrow': 8595, 'circeq': 8791, 'subseteq': 8838, 'bigstar': 9733, 'prime': 8242, 'lceil': 8968, 'Rrightarrow': 8667, 'oiiint': 8752, 'curlywedge': 8911, 'vDash': 8872, 'lfloor': 8970, 'ddots': 8945, 'exists': 8707, 'underbar': 817, 'Pi': 928, 'leftrightarrows': 8646, 'sphericalangle': 8738, 'coprod': 8720, 'circledcirc': 8858, 'gtrsim': 8819, 'gneqq': 8809, 'between': 8812, 'theta': 952, 'complement': 8705, 'arceq': 8792, 'nVdash': 8878, 'S': 167, 'wr': 8768, 'wp': 8472, 'backcong': 8780, 'lasp': 701, 'c': 807, 'nabla': 8711, 'dotplus': 8724, 'eta': 951, 'forall': 8704, 'eth': 240, 'colon': 58, 'sqcup': 8852, 'rightrightarrows': 8649, 'sqsupset': 8848, 'mapsto': 8614, 'bigtriangledown': 9661, 'sqsupseteq': 8850, 'propto': 8733, 'pi': 960, 'pm': 177, 'dots': 8230, 'nrightarrow': 8603, 'textasciiacute': 180, 'Doteq': 8785, 'breve': 774, 'sqcap': 8851, 'twoheadrightarrow': 8608, 'kappa': 954, 'vartriangle': 9653, 'diamondsuit': 9826, 'pitchfork': 8916, 'blacktriangleleft': 9664, 'nprec': 8832, 'vdots': 8942, 'curvearrowright': 8631, 'barwedge': 8892, 'multimap': 8888, 'textquestiondown': 191, 'cong': 8773, 'rtimes': 8906, 'rightzigzagarrow': 8669, 'rightarrow': 8594, 'leftarrow': 8592, '__sqrt__': 8730, 'twoheaddownarrow': 8609, 'oint': 8750, 'bigvee': 8897, 'eqdef': 8797, 'sterling': 163, 'phi': 981, 'Updownarrow': 8661, 'backprime': 8245, 'emdash': 8212, 'Gamma': 915, 'i': 305, 'rceil': 8969, 'leftharpoonup': 8636, 'Im': 8465, 'curvearrowleft': 8630, 'wedgeq': 8793, 'fallingdotseq': 8786, 'curlyeqprec': 8926, 'questeq': 8799, 'less': 60, 'upuparrows': 8648, 'tilde': 771, 'textasciigrave': 96, 'smallsetminus': 8726, 'ell': 8467, 'cup': 8746, 'danger': 9761, 'nVDash': 8879, 'cdotp': 183, 'cdots': 8943, 'hat': 770, 'eqgtr': 8925, 'enspace': 8194, 'psi': 968, 'frown': 8994, 'acute': 769, 'downzigzagarrow': 8623, 'ntriangleright': 8939, 'cupdot': 8845, 'circleddash': 8861, 'oslash': 8856, 'mho': 8487, 'd': 803, 'sqsubset': 8847, 'cdot': 8901, 'Omega': 937, 'OE': 338, 'veeeq': 8794, 'Finv': 8498, 't': 865, 'leftrightarrow': 8596, 'swarrow': 8601, 'rightthreetimes': 8908, 'rightleftharpoons': 8652, 'lesssim': 8818, 'searrow': 8600, 'because': 8757, 'gtrless': 8823, 'star': 8902, 'nsubset': 8836, 'zeta': 950, 'dddot': 8411, 'bigcirc': 9675, 'Supset': 8913, 'circ': 8728, 'slash': 8725, 'ocirc': 778, 'prod': 8719, 'twoheadleftarrow': 8606, 'daleth': 8504, 'upharpoonright': 8638, 'odot': 8857, 'Uparrow': 8657, 'O': 216, 'hookleftarrow': 8617, 'trianglerighteq': 8885, 'nsime': 8772, 'oe': 339, 'nwarrow': 8598, 'o': 248, 'ddddot': 8412, 'downharpoonright': 8642, 'succcurlyeq': 8829, 'gamma': 947, 'scrR': 8475, 'dag': 8224, 'thickspace': 8197, 'frakZ': 8488, 'lessdot': 8918, 'triangledown': 9663, 'ltimes': 8905, 'scrB': 8492, 'endash': 8211, 'scrE': 8496, 'scrF': 8497, 'scrH': 8459, 'scrI': 8464, 'rightharpoondown': 8641, 'scrL': 8466, 'scrM': 8499, 'frakC': 8493, 'nsupseteq': 8841, 'circledR': 174, 'circledS': 9416, 'ngtr': 8815, 'bigcap': 8898, 'scre': 8495, 'Downarrow': 8659, 'scrg': 8458, 'overleftrightarrow': 8417, 'scro': 8500, 'lnsim': 8934, 'eqcolon': 8789, 'curlyvee': 8910, 'urcorner': 8989, 'lbrace': 123, 'Bumpeq': 8782, 'delta': 948, 'boxtimes': 8864, 'overleftarrow': 8406, 'prurel': 8880, 'clubsuitopen': 9831, 'cwopencirclearrow': 8635, 'geqq': 8807, 'rightleftarrows': 8644, 'ac': 8766, 'ae': 230, 'int': 8747, 'rfloor': 8971, 'risingdotseq': 8787, 'nvdash': 8876, 'diamond': 8900, 'ddot': 776, 'backsim': 8765, 'oplus': 8853, 'triangleq': 8796, 'check': 780, 'ni': 8715, 'iiint': 8749, 'ne': 8800, 'lesseqgtr': 8922, 'obar': 9021, 'supseteq': 8839, 'nu': 957, 'AA': 8491, 'AE': 198, 'models': 8871, 'ominus': 8854, 'dashv': 8867, 'omega': 969, 'rq': 8217, 'Subset': 8912, 'rightharpoonup': 8640, 'Rdsh': 8627, 'bullet': 8729, 'divideontimes': 8903, 'lbrack': 91, 'textquotedblright': 8221, 'Colon': 8759, '%': 37, '$': 36, '{': 123, '}': 125, '_': 95, 'imath': 0x131, 'circumflexaccent' : 770, 'combiningbreve' : 774, 'combiningoverline' : 772, 'combininggraveaccent' : 768, 'combiningacuteaccent' : 769, 'combiningdiaeresis' : 776, 'combiningtilde' : 771, 'combiningrightarrowabove' : 8407, 'combiningdotabove' : 775, 'to': 8594, 'succeq': 8829, 'emptyset': 8709, 'leftparen': 40, 'rightparen': 41, 'bigoplus': 10753, 'leftangle': 10216, 'rightangle': 10217, 'leftbrace': 124, 'rightbrace': 125, 'jmath': 567, 'bigodot': 10752, 'preceq': 8828, 'biguplus': 10756, 'epsilon': 949, 'vartheta': 977, 'bigotimes': 10754 } # Each element is a 4-tuple of the form: # src_start, src_end, dst_font, dst_start # stix_virtual_fonts = { 'bb': { 'rm': [ (0x0030, 0x0039, 'rm', 0x1d7d8), # 0-9 (0x0041, 0x0042, 'rm', 0x1d538), # A-B (0x0043, 0x0043, 'rm', 0x2102), # C (0x0044, 0x0047, 'rm', 0x1d53b), # D-G (0x0048, 0x0048, 'rm', 0x210d), # H (0x0049, 0x004d, 'rm', 0x1d540), # I-M (0x004e, 0x004e, 'rm', 0x2115), # N (0x004f, 0x004f, 'rm', 0x1d546), # O (0x0050, 0x0051, 'rm', 0x2119), # P-Q (0x0052, 0x0052, 'rm', 0x211d), # R (0x0053, 0x0059, 'rm', 0x1d54a), # S-Y (0x005a, 0x005a, 'rm', 0x2124), # Z (0x0061, 0x007a, 'rm', 0x1d552), # a-z (0x0393, 0x0393, 'rm', 0x213e), # \Gamma (0x03a0, 0x03a0, 'rm', 0x213f), # \Pi (0x03a3, 0x03a3, 'rm', 0x2140), # \Sigma (0x03b3, 0x03b3, 'rm', 0x213d), # \gamma (0x03c0, 0x03c0, 'rm', 0x213c), # \pi ], 'it': [ (0x0030, 0x0039, 'rm', 0x1d7d8), # 0-9 (0x0041, 0x0042, 'it', 0xe154), # A-B (0x0043, 0x0043, 'it', 0x2102), # C (missing in beta STIX fonts) (0x0044, 0x0044, 'it', 0x2145), # D (0x0045, 0x0047, 'it', 0xe156), # E-G (0x0048, 0x0048, 'it', 0x210d), # H (missing in beta STIX fonts) (0x0049, 0x004d, 'it', 0xe159), # I-M (0x004e, 0x004e, 'it', 0x2115), # N (missing in beta STIX fonts) (0x004f, 0x004f, 'it', 0xe15e), # O (0x0050, 0x0051, 'it', 0x2119), # P-Q (missing in beta STIX fonts) (0x0052, 0x0052, 'it', 0x211d), # R (missing in beta STIX fonts) (0x0053, 0x0059, 'it', 0xe15f), # S-Y (0x005a, 0x005a, 'it', 0x2124), # Z (missing in beta STIX fonts) (0x0061, 0x0063, 'it', 0xe166), # a-c (0x0064, 0x0065, 'it', 0x2146), # d-e (0x0066, 0x0068, 'it', 0xe169), # f-h (0x0069, 0x006a, 'it', 0x2148), # i-j (0x006b, 0x007a, 'it', 0xe16c), # k-z (0x0393, 0x0393, 'it', 0x213e), # \Gamma (missing in beta STIX fonts) (0x03a0, 0x03a0, 'it', 0x213f), # \Pi (0x03a3, 0x03a3, 'it', 0x2140), # \Sigma (missing in beta STIX fonts) (0x03b3, 0x03b3, 'it', 0x213d), # \gamma (missing in beta STIX fonts) (0x03c0, 0x03c0, 'it', 0x213c), # \pi ], 'bf': [ (0x0030, 0x0039, 'rm', 0x1d7d8), # 0-9 (0x0041, 0x005a, 'bf', 0xe38a), # A-Z (0x0061, 0x007a, 'bf', 0xe39d), # a-z (0x0393, 0x0393, 'bf', 0x213e), # \Gamma (0x03a0, 0x03a0, 'bf', 0x213f), # \Pi (0x03a3, 0x03a3, 'bf', 0x2140), # \Sigma (0x03b3, 0x03b3, 'bf', 0x213d), # \gamma (0x03c0, 0x03c0, 'bf', 0x213c), # \pi ], }, 'cal': [ (0x0041, 0x005a, 'it', 0xe22d), # A-Z ], 'circled': { 'rm': [ (0x0030, 0x0030, 'rm', 0x24ea), # 0 (0x0031, 0x0039, 'rm', 0x2460), # 1-9 (0x0041, 0x005a, 'rm', 0x24b6), # A-Z (0x0061, 0x007a, 'rm', 0x24d0) # a-z ], 'it': [ (0x0030, 0x0030, 'rm', 0x24ea), # 0 (0x0031, 0x0039, 'rm', 0x2460), # 1-9 (0x0041, 0x005a, 'it', 0x24b6), # A-Z (0x0061, 0x007a, 'it', 0x24d0) # a-z ], 'bf': [ (0x0030, 0x0030, 'bf', 0x24ea), # 0 (0x0031, 0x0039, 'bf', 0x2460), # 1-9 (0x0041, 0x005a, 'bf', 0x24b6), # A-Z (0x0061, 0x007a, 'bf', 0x24d0) # a-z ], }, 'frak': { 'rm': [ (0x0041, 0x0042, 'rm', 0x1d504), # A-B (0x0043, 0x0043, 'rm', 0x212d), # C (0x0044, 0x0047, 'rm', 0x1d507), # D-G (0x0048, 0x0048, 'rm', 0x210c), # H (0x0049, 0x0049, 'rm', 0x2111), # I (0x004a, 0x0051, 'rm', 0x1d50d), # J-Q (0x0052, 0x0052, 'rm', 0x211c), # R (0x0053, 0x0059, 'rm', 0x1d516), # S-Y (0x005a, 0x005a, 'rm', 0x2128), # Z (0x0061, 0x007a, 'rm', 0x1d51e), # a-z ], 'it': [ (0x0041, 0x0042, 'rm', 0x1d504), # A-B (0x0043, 0x0043, 'rm', 0x212d), # C (0x0044, 0x0047, 'rm', 0x1d507), # D-G (0x0048, 0x0048, 'rm', 0x210c), # H (0x0049, 0x0049, 'rm', 0x2111), # I (0x004a, 0x0051, 'rm', 0x1d50d), # J-Q (0x0052, 0x0052, 'rm', 0x211c), # R (0x0053, 0x0059, 'rm', 0x1d516), # S-Y (0x005a, 0x005a, 'rm', 0x2128), # Z (0x0061, 0x007a, 'rm', 0x1d51e), # a-z ], 'bf': [ (0x0041, 0x005a, 'bf', 0x1d56c), # A-Z (0x0061, 0x007a, 'bf', 0x1d586), # a-z ], }, 'scr': [ (0x0041, 0x0041, 'it', 0x1d49c), # A (0x0042, 0x0042, 'it', 0x212c), # B (0x0043, 0x0044, 'it', 0x1d49e), # C-D (0x0045, 0x0046, 'it', 0x2130), # E-F (0x0047, 0x0047, 'it', 0x1d4a2), # G (0x0048, 0x0048, 'it', 0x210b), # H (0x0049, 0x0049, 'it', 0x2110), # I (0x004a, 0x004b, 'it', 0x1d4a5), # J-K (0x004c, 0x004c, 'it', 0x2112), # L (0x004d, 0x003d, 'it', 0x2113), # M (0x004e, 0x0051, 'it', 0x1d4a9), # N-Q (0x0052, 0x0052, 'it', 0x211b), # R (0x0053, 0x005a, 'it', 0x1d4ae), # S-Z (0x0061, 0x0064, 'it', 0x1d4b6), # a-d (0x0065, 0x0065, 'it', 0x212f), # e (0x0066, 0x0066, 'it', 0x1d4bb), # f (0x0067, 0x0067, 'it', 0x210a), # g (0x0068, 0x006e, 'it', 0x1d4bd), # h-n (0x006f, 0x006f, 'it', 0x2134), # o (0x0070, 0x007a, 'it', 0x1d4c5), # p-z ], 'sf': { 'rm': [ (0x0030, 0x0039, 'rm', 0x1d7e2), # 0-9 (0x0041, 0x005a, 'rm', 0x1d5a0), # A-Z (0x0061, 0x007a, 'rm', 0x1d5ba), # a-z (0x0391, 0x03a9, 'rm', 0xe17d), # \Alpha-\Omega (0x03b1, 0x03c9, 'rm', 0xe196), # \alpha-\omega (0x03d1, 0x03d1, 'rm', 0xe1b0), # theta variant (0x03d5, 0x03d5, 'rm', 0xe1b1), # phi variant (0x03d6, 0x03d6, 'rm', 0xe1b3), # pi variant (0x03f1, 0x03f1, 'rm', 0xe1b2), # rho variant (0x03f5, 0x03f5, 'rm', 0xe1af), # lunate epsilon (0x2202, 0x2202, 'rm', 0xe17c), # partial differential ], 'it': [ # These numerals are actually upright. We don't actually # want italic numerals ever. (0x0030, 0x0039, 'rm', 0x1d7e2), # 0-9 (0x0041, 0x005a, 'it', 0x1d608), # A-Z (0x0061, 0x007a, 'it', 0x1d622), # a-z (0x0391, 0x03a9, 'rm', 0xe17d), # \Alpha-\Omega (0x03b1, 0x03c9, 'it', 0xe1d8), # \alpha-\omega (0x03d1, 0x03d1, 'it', 0xe1f2), # theta variant (0x03d5, 0x03d5, 'it', 0xe1f3), # phi variant (0x03d6, 0x03d6, 'it', 0xe1f5), # pi variant (0x03f1, 0x03f1, 'it', 0xe1f4), # rho variant (0x03f5, 0x03f5, 'it', 0xe1f1), # lunate epsilon ], 'bf': [ (0x0030, 0x0039, 'bf', 0x1d7ec), # 0-9 (0x0041, 0x005a, 'bf', 0x1d5d4), # A-Z (0x0061, 0x007a, 'bf', 0x1d5ee), # a-z (0x0391, 0x03a9, 'bf', 0x1d756), # \Alpha-\Omega (0x03b1, 0x03c9, 'bf', 0x1d770), # \alpha-\omega (0x03d1, 0x03d1, 'bf', 0x1d78b), # theta variant (0x03d5, 0x03d5, 'bf', 0x1d78d), # phi variant (0x03d6, 0x03d6, 'bf', 0x1d78f), # pi variant (0x03f0, 0x03f0, 'bf', 0x1d78c), # kappa variant (0x03f1, 0x03f1, 'bf', 0x1d78e), # rho variant (0x03f5, 0x03f5, 'bf', 0x1d78a), # lunate epsilon (0x2202, 0x2202, 'bf', 0x1d789), # partial differential (0x2207, 0x2207, 'bf', 0x1d76f), # \Nabla ], }, 'tt': [ (0x0030, 0x0039, 'rm', 0x1d7f6), # 0-9 (0x0041, 0x005a, 'rm', 0x1d670), # A-Z (0x0061, 0x007a, 'rm', 0x1d68a) # a-z ], }
gpl-3.0
nishantsbi/Data-Science-45min-Intros
choosing-k-in-kmeans/3d-example.py
25
2925
#!/usr/bin/env python # -*- coding: UTF-8 -*- __author__="Josh Montague" __license__="MIT License" """ This script is designed to run inline (%run 3d-example.py) in the corresponding IPython notebook. It generates a 3d scatter plot using scikit-learn data generation and with a number of samples and clusters determined by the variables near the top. """ import argparse import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn.datasets import make_blobs import seaborn as sns from gap_stats import gap_statistics from gap_stats import plot_gap_statistics def make_example_plot(args): """ Create artificial data (blobs) and color them according to the appropriate blob center. """ # read args samples = args.samples clusters = args.clusters # create some data X, y = make_blobs(n_samples=samples, centers=clusters, n_features=3, # increase variance for illustration cluster_std=1.5, # fix random_state if you believe in determinism #random_state=42 ) # seaborn display settings sns.set(style='whitegrid', palette=sns.color_palette("Set2", clusters)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for i in range(clusters): # for each center, add data to the figure w/ appropriate label ax.plot(X[y==i,0], X[y==i,1], X[y==i,2], 'o', alpha=0.6, label='cluster {}'.format(i) ) ax.set_title('{} labeled clusters (ground truth)'.format(clusters)) ax.legend(loc='upper left') # seaborn settings - no, really set these things this time, please sns.set(style='whitegrid', palette=sns.color_palette("Set2", clusters)) #plt.show() # potentially return the data for later use data = None if args.gap: data = (X, y) return data if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-s","--samples" , dest="samples" , type=int , default=100 ) parser.add_argument("-c","--clusters" , dest="clusters" , type=int , default=5 ) parser.add_argument("-g","--gap" , dest="gap" , type=bool , default=False ) args = parser.parse_args() data = make_example_plot(args) if args.gap: # i just really prefer the dark theme sns.set(style='darkgrid', palette='deep') # unpack X, y = data # run the gap statistic algorithm gaps, errs, difs = gap_statistics(X, ks=range(1, args.clusters+5)) # plot (intended for %matplotlib inline) plot_gap_statistics(gaps, errs, difs)
unlicense
planetarymike/IDL-Colorbars
IDL_py_test/009_GRN-WHT_EXPONENTIAL.py
1
8787
from matplotlib.colors import LinearSegmentedColormap from numpy import nan, inf cm_data = [[0.00392157, 0., 0.], [0.00392157, 0., 0.], [0.00392157, 0.00392157, 0.], [0.00784314, 0.00784314, 0.], [0.00784314, 0.00784314, 0.00392157], [0.00784314, 0.0117647, 0.00392157], [0.0117647, 0.0156863, 0.00392157], [0.0117647, 0.0156863, 0.00392157], [0.0156863, 0.0196078, 0.00784314], [0.0156863, 0.0235294, 0.00784314], [0.0156863, 0.0235294, 0.00784314], [0.0196078, 0.027451, 0.00784314], [0.0196078, 0.0313725, 0.0117647], [0.0196078, 0.0313725, 0.0117647], [0.0235294, 0.0352941, 0.0117647], [0.0235294, 0.0392157, 0.0117647], [0.027451, 0.0392157, 0.0156863], [0.027451, 0.0431373, 0.0156863], [0.027451, 0.0470588, 0.0156863], [0.0313725, 0.0470588, 0.0196078], [0.0313725, 0.0509804, 0.0196078], [0.0352941, 0.054902, 0.0196078], [0.0352941, 0.054902, 0.0196078], [0.0352941, 0.0588235, 0.0235294], [0.0392157, 0.0627451, 0.0235294], [0.0392157, 0.0627451, 0.0235294], [0.0392157, 0.0666667, 0.0235294], [0.0431373, 0.0705882, 0.027451], [0.0431373, 0.0705882, 0.027451], [0.0470588, 0.0745098, 0.027451], [0.0470588, 0.0784314, 0.027451], [0.0470588, 0.0784314, 0.0313725], [0.0509804, 0.0823529, 0.0313725], [0.0509804, 0.0862745, 0.0313725], [0.0509804, 0.0862745, 0.0313725], [0.054902, 0.0901961, 0.0352941], [0.054902, 0.0941176, 0.0352941], [0.0588235, 0.0941176, 0.0352941], [0.0588235, 0.0980392, 0.0392157], [0.0588235, 0.101961, 0.0392157], [0.0627451, 0.101961, 0.0392157], [0.0627451, 0.105882, 0.0392157], [0.0666667, 0.109804, 0.0431373], [0.0666667, 0.109804, 0.0431373], [0.0666667, 0.113725, 0.0431373], [0.0705882, 0.117647, 0.0431373], [0.0705882, 0.117647, 0.0470588], [0.0705882, 0.121569, 0.0470588], [0.0745098, 0.12549, 0.0470588], [0.0745098, 0.12549, 0.0470588], [0.0784314, 0.129412, 0.0509804], [0.0784314, 0.133333, 0.0509804], [0.0784314, 0.133333, 0.0509804], [0.0823529, 0.137255, 0.0509804], [0.0823529, 0.141176, 0.054902], [0.0823529, 0.141176, 0.054902], [0.0862745, 0.145098, 0.054902], [0.0862745, 0.14902, 0.0588235], [0.0901961, 0.14902, 0.0588235], [0.0901961, 0.152941, 0.0588235], [0.0901961, 0.156863, 0.0588235], [0.0941176, 0.156863, 0.0627451], [0.0941176, 0.160784, 0.0627451], [0.0980392, 0.164706, 0.0627451], [0.0980392, 0.164706, 0.0627451], [0.0980392, 0.168627, 0.0666667], [0.101961, 0.172549, 0.0666667], [0.101961, 0.172549, 0.0666667], [0.101961, 0.176471, 0.0666667], [0.105882, 0.180392, 0.0705882], [0.105882, 0.180392, 0.0705882], [0.109804, 0.184314, 0.0705882], [0.109804, 0.188235, 0.0705882], [0.109804, 0.188235, 0.0745098], [0.113725, 0.192157, 0.0745098], [0.113725, 0.196078, 0.0745098], [0.113725, 0.196078, 0.0784314], [0.117647, 0.2, 0.0784314], [0.117647, 0.203922, 0.0784314], [0.121569, 0.203922, 0.0784314], [0.121569, 0.207843, 0.0823529], [0.121569, 0.211765, 0.0823529], [0.12549, 0.211765, 0.0823529], [0.12549, 0.215686, 0.0823529], [0.129412, 0.219608, 0.0862745], [0.129412, 0.219608, 0.0862745], [0.129412, 0.223529, 0.0862745], [0.133333, 0.227451, 0.0862745], [0.133333, 0.227451, 0.0901961], [0.133333, 0.231373, 0.0901961], [0.137255, 0.235294, 0.0901961], [0.137255, 0.235294, 0.0901961], [0.141176, 0.239216, 0.0941176], [0.141176, 0.243137, 0.0941176], [0.141176, 0.243137, 0.0941176], [0.145098, 0.247059, 0.0980392], [0.145098, 0.25098, 0.0980392], [0.145098, 0.25098, 0.0980392], [0.14902, 0.254902, 0.0980392], [0.14902, 0.258824, 0.101961], [0.152941, 0.258824, 0.101961], [0.152941, 0.262745, 0.101961], [0.152941, 0.266667, 0.101961], [0.156863, 0.266667, 0.105882], [0.156863, 0.270588, 0.105882], [0.160784, 0.27451, 0.105882], [0.160784, 0.27451, 0.105882], [0.160784, 0.278431, 0.109804], [0.164706, 0.282353, 0.109804], [0.164706, 0.282353, 0.109804], [0.164706, 0.286275, 0.109804], [0.168627, 0.290196, 0.113725], [0.168627, 0.290196, 0.113725], [0.172549, 0.294118, 0.113725], [0.172549, 0.298039, 0.117647], [0.172549, 0.298039, 0.117647], [0.176471, 0.301961, 0.117647], [0.176471, 0.305882, 0.117647], [0.176471, 0.305882, 0.121569], [0.180392, 0.309804, 0.121569], [0.180392, 0.313725, 0.121569], [0.184314, 0.313725, 0.121569], [0.184314, 0.317647, 0.12549], [0.184314, 0.321569, 0.12549], [0.188235, 0.321569, 0.12549], [0.188235, 0.32549, 0.12549], [0.192157, 0.329412, 0.129412], [0.192157, 0.329412, 0.129412], [0.192157, 0.333333, 0.129412], [0.196078, 0.337255, 0.129412], [0.196078, 0.341176, 0.133333], [0.196078, 0.345098, 0.133333], [0.2, 0.352941, 0.133333], [0.2, 0.356863, 0.137255], [0.203922, 0.360784, 0.137255], [0.203922, 0.368627, 0.137255], [0.203922, 0.372549, 0.137255], [0.211765, 0.376471, 0.141176], [0.211765, 0.384314, 0.141176], [0.215686, 0.388235, 0.141176], [0.219608, 0.392157, 0.141176], [0.219608, 0.4, 0.145098], [0.223529, 0.403922, 0.145098], [0.227451, 0.407843, 0.145098], [0.227451, 0.415686, 0.145098], [0.231373, 0.419608, 0.14902], [0.235294, 0.423529, 0.14902], [0.235294, 0.431373, 0.14902], [0.239216, 0.435294, 0.14902], [0.243137, 0.439216, 0.152941], [0.243137, 0.447059, 0.152941], [0.247059, 0.45098, 0.152941], [0.25098, 0.454902, 0.156863], [0.25098, 0.462745, 0.156863], [0.258824, 0.466667, 0.156863], [0.266667, 0.470588, 0.156863], [0.27451, 0.478431, 0.160784], [0.278431, 0.482353, 0.160784], [0.286275, 0.486275, 0.160784], [0.294118, 0.494118, 0.160784], [0.301961, 0.498039, 0.164706], [0.309804, 0.501961, 0.164706], [0.317647, 0.509804, 0.164706], [0.32549, 0.513725, 0.164706], [0.333333, 0.517647, 0.168627], [0.337255, 0.52549, 0.168627], [0.345098, 0.529412, 0.168627], [0.352941, 0.533333, 0.168627], [0.360784, 0.541176, 0.172549], [0.368627, 0.545098, 0.172549], [0.376471, 0.54902, 0.172549], [0.384314, 0.556863, 0.176471], [0.388235, 0.560784, 0.176471], [0.396078, 0.568627, 0.176471], [0.403922, 0.572549, 0.176471], [0.411765, 0.576471, 0.180392], [0.419608, 0.584314, 0.180392], [0.427451, 0.588235, 0.180392], [0.435294, 0.592157, 0.180392], [0.443137, 0.6, 0.184314], [0.447059, 0.603922, 0.184314], [0.454902, 0.607843, 0.184314], [0.462745, 0.615686, 0.184314], [0.470588, 0.619608, 0.188235], [0.478431, 0.623529, 0.184314], [0.486275, 0.631373, 0.192157], [0.494118, 0.635294, 0.203922], [0.498039, 0.639216, 0.215686], [0.505882, 0.647059, 0.227451], [0.513725, 0.65098, 0.239216], [0.521569, 0.654902, 0.25098], [0.529412, 0.662745, 0.262745], [0.537255, 0.666667, 0.27451], [0.545098, 0.670588, 0.286275], [0.552941, 0.678431, 0.298039], [0.556863, 0.682353, 0.309804], [0.564706, 0.686275, 0.321569], [0.572549, 0.694118, 0.333333], [0.580392, 0.698039, 0.345098], [0.588235, 0.701961, 0.352941], [0.596078, 0.709804, 0.364706], [0.603922, 0.713725, 0.376471], [0.611765, 0.717647, 0.388235], [0.615686, 0.72549, 0.4], [0.623529, 0.729412, 0.411765], [0.631373, 0.733333, 0.423529], [0.639216, 0.741176, 0.435294], [0.647059, 0.745098, 0.447059], [0.654902, 0.74902, 0.458824], [0.662745, 0.756863, 0.470588], [0.666667, 0.760784, 0.482353], [0.67451, 0.764706, 0.494118], [0.682353, 0.772549, 0.505882], [0.690196, 0.776471, 0.513725], [0.698039, 0.784314, 0.52549], [0.705882, 0.788235, 0.537255], [0.713725, 0.792157, 0.54902], [0.721569, 0.8, 0.560784], [0.72549, 0.803922, 0.572549], [0.733333, 0.807843, 0.584314], [0.741176, 0.815686, 0.596078], [0.74902, 0.819608, 0.607843], [0.756863, 0.823529, 0.619608], [0.764706, 0.831373, 0.631373], [0.772549, 0.835294, 0.643137], [0.776471, 0.839216, 0.654902], [0.784314, 0.847059, 0.666667], [0.792157, 0.85098, 0.67451], [0.8, 0.854902, 0.686275], [0.807843, 0.862745, 0.698039], [0.815686, 0.866667, 0.709804], [0.823529, 0.870588, 0.721569], [0.831373, 0.878431, 0.733333], [0.835294, 0.882353, 0.745098], [0.843137, 0.886275, 0.756863], [0.85098, 0.894118, 0.768627], [0.858824, 0.898039, 0.780392], [0.866667, 0.901961, 0.792157], [0.87451, 0.909804, 0.803922], [0.882353, 0.913725, 0.815686], [0.886275, 0.917647, 0.827451], [0.894118, 0.92549, 0.835294], [0.901961, 0.929412, 0.847059], [0.909804, 0.933333, 0.858824], [0.917647, 0.941176, 0.870588], [0.92549, 0.945098, 0.882353], [0.933333, 0.94902, 0.894118], [0.941176, 0.956863, 0.905882], [0.945098, 0.960784, 0.917647], [0.952941, 0.964706, 0.929412], [0.960784, 0.972549, 0.941176], [0.968627, 0.976471, 0.952941], [0.976471, 0.980392, 0.964706], [0.984314, 0.988235, 0.976471], [0.992157, 0.992157, 0.988235], [1., 1., 1.]] test_cm = LinearSegmentedColormap.from_list(__file__, cm_data) if __name__ == "__main__": import matplotlib.pyplot as plt import numpy as np try: from pycam02ucs.cm.viscm import viscm viscm(test_cm) except ImportError: print("pycam02ucs not found, falling back on simple display") plt.imshow(np.linspace(0, 100, 256)[None, :], aspect='auto', cmap=test_cm) plt.show()
gpl-2.0
ryfeus/lambda-packs
Sklearn_scipy_numpy/source/sklearn/utils/testing.py
6
26970
"""Testing utilities.""" # Copyright (c) 2011, 2012 # Authors: Pietro Berkes, # Andreas Muller # Mathieu Blondel # Olivier Grisel # Arnaud Joly # Denis Engemann # Giorgio Patrini # License: BSD 3 clause import os import inspect import pkgutil import warnings import sys import re import platform import struct import scipy as sp import scipy.io from functools import wraps try: # Python 2 from urllib2 import urlopen from urllib2 import HTTPError except ImportError: # Python 3+ from urllib.request import urlopen from urllib.error import HTTPError import tempfile import shutil import os.path as op import atexit # WindowsError only exist on Windows try: WindowsError except NameError: WindowsError = None import sklearn from sklearn.base import BaseEstimator from sklearn.externals import joblib # Conveniently import all assertions in one place. from nose.tools import assert_equal from nose.tools import assert_not_equal from nose.tools import assert_true from nose.tools import assert_false from nose.tools import assert_raises from nose.tools import raises from nose import SkipTest from nose import with_setup from numpy.testing import assert_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_less from numpy.testing import assert_approx_equal import numpy as np from sklearn.base import (ClassifierMixin, RegressorMixin, TransformerMixin, ClusterMixin) from sklearn.cluster import DBSCAN __all__ = ["assert_equal", "assert_not_equal", "assert_raises", "assert_raises_regexp", "raises", "with_setup", "assert_true", "assert_false", "assert_almost_equal", "assert_array_equal", "assert_array_almost_equal", "assert_array_less", "assert_less", "assert_less_equal", "assert_greater", "assert_greater_equal", "assert_approx_equal"] try: from nose.tools import assert_in, assert_not_in except ImportError: # Nose < 1.0.0 def assert_in(x, container): assert_true(x in container, msg="%r in %r" % (x, container)) def assert_not_in(x, container): assert_false(x in container, msg="%r in %r" % (x, container)) try: from nose.tools import assert_raises_regex except ImportError: # for Python 2 def assert_raises_regex(expected_exception, expected_regexp, callable_obj=None, *args, **kwargs): """Helper function to check for message patterns in exceptions""" not_raised = False try: callable_obj(*args, **kwargs) not_raised = True except expected_exception as e: error_message = str(e) if not re.compile(expected_regexp).search(error_message): raise AssertionError("Error message should match pattern " "%r. %r does not." % (expected_regexp, error_message)) if not_raised: raise AssertionError("%s not raised by %s" % (expected_exception.__name__, callable_obj.__name__)) # assert_raises_regexp is deprecated in Python 3.4 in favor of # assert_raises_regex but lets keep the bacward compat in scikit-learn with # the old name for now assert_raises_regexp = assert_raises_regex def _assert_less(a, b, msg=None): message = "%r is not lower than %r" % (a, b) if msg is not None: message += ": " + msg assert a < b, message def _assert_greater(a, b, msg=None): message = "%r is not greater than %r" % (a, b) if msg is not None: message += ": " + msg assert a > b, message def assert_less_equal(a, b, msg=None): message = "%r is not lower than or equal to %r" % (a, b) if msg is not None: message += ": " + msg assert a <= b, message def assert_greater_equal(a, b, msg=None): message = "%r is not greater than or equal to %r" % (a, b) if msg is not None: message += ": " + msg assert a >= b, message def assert_warns(warning_class, func, *args, **kw): """Test that a certain warning occurs. Parameters ---------- warning_class : the warning class The class to test for, e.g. UserWarning. func : callable Calable object to trigger warnings. *args : the positional arguments to `func`. **kw : the keyword arguments to `func` Returns ------- result : the return value of `func` """ # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. result = func(*args, **kw) if hasattr(np, 'VisibleDeprecationWarning'): # Filter out numpy-specific warnings in numpy >= 1.9 w = [e for e in w if e.category is not np.VisibleDeprecationWarning] # Verify some things if not len(w) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) found = any(warning.category is warning_class for warning in w) if not found: raise AssertionError("%s did not give warning: %s( is %s)" % (func.__name__, warning_class, w)) return result def assert_warns_message(warning_class, message, func, *args, **kw): # very important to avoid uncontrolled state propagation """Test that a certain warning occurs and with a certain message. Parameters ---------- warning_class : the warning class The class to test for, e.g. UserWarning. message : str | callable The entire message or a substring to test for. If callable, it takes a string as argument and will trigger an assertion error if it returns `False`. func : callable Calable object to trigger warnings. *args : the positional arguments to `func`. **kw : the keyword arguments to `func`. Returns ------- result : the return value of `func` """ clean_warning_registry() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") if hasattr(np, 'VisibleDeprecationWarning'): # Let's not catch the numpy internal DeprecationWarnings warnings.simplefilter('ignore', np.VisibleDeprecationWarning) # Trigger a warning. result = func(*args, **kw) # Verify some things if not len(w) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) found = [issubclass(warning.category, warning_class) for warning in w] if not any(found): raise AssertionError("No warning raised for %s with class " "%s" % (func.__name__, warning_class)) message_found = False # Checks the message of all warnings belong to warning_class for index in [i for i, x in enumerate(found) if x]: # substring will match, the entire message with typo won't msg = w[index].message # For Python 3 compatibility msg = str(msg.args[0] if hasattr(msg, 'args') else msg) if callable(message): # add support for certain tests check_in_message = message else: check_in_message = lambda msg: message in msg if check_in_message(msg): message_found = True break if not message_found: raise AssertionError("Did not receive the message you expected " "('%s') for <%s>, got: '%s'" % (message, func.__name__, msg)) return result # To remove when we support numpy 1.7 def assert_no_warnings(func, *args, **kw): # XXX: once we may depend on python >= 2.6, this can be replaced by the # warnings module context manager. # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') result = func(*args, **kw) if hasattr(np, 'VisibleDeprecationWarning'): # Filter out numpy-specific warnings in numpy >= 1.9 w = [e for e in w if e.category is not np.VisibleDeprecationWarning] if len(w) > 0: raise AssertionError("Got warnings when calling %s: %s" % (func.__name__, w)) return result def ignore_warnings(obj=None): """ Context manager and decorator to ignore warnings Note. Using this (in both variants) will clear all warnings from all python modules loaded. In case you need to test cross-module-warning-logging this is not your tool of choice. Examples -------- >>> with ignore_warnings(): ... warnings.warn('buhuhuhu') >>> def nasty_warn(): ... warnings.warn('buhuhuhu') ... print(42) >>> ignore_warnings(nasty_warn)() 42 """ if callable(obj): return _ignore_warnings(obj) else: return _IgnoreWarnings() def _ignore_warnings(fn): """Decorator to catch and hide warnings without visual nesting""" @wraps(fn) def wrapper(*args, **kwargs): # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') return fn(*args, **kwargs) w[:] = [] return wrapper class _IgnoreWarnings(object): """Improved and simplified Python warnings context manager Copied from Python 2.7.5 and modified as required. """ def __init__(self): """ Parameters ========== category : warning class The category to filter. Defaults to Warning. If None, all categories will be muted. """ self._record = True self._module = sys.modules['warnings'] self._entered = False self.log = [] def __repr__(self): args = [] if self._record: args.append("record=True") if self._module is not sys.modules['warnings']: args.append("module=%r" % self._module) name = type(self).__name__ return "%s(%s)" % (name, ", ".join(args)) def __enter__(self): clean_warning_registry() # be safe and not propagate state + chaos warnings.simplefilter('always') if self._entered: raise RuntimeError("Cannot enter %r twice" % self) self._entered = True self._filters = self._module.filters self._module.filters = self._filters[:] self._showwarning = self._module.showwarning if self._record: self.log = [] def showwarning(*args, **kwargs): self.log.append(warnings.WarningMessage(*args, **kwargs)) self._module.showwarning = showwarning return self.log else: return None def __exit__(self, *exc_info): if not self._entered: raise RuntimeError("Cannot exit %r without entering first" % self) self._module.filters = self._filters self._module.showwarning = self._showwarning self.log[:] = [] clean_warning_registry() # be safe and not propagate state + chaos try: from nose.tools import assert_less except ImportError: assert_less = _assert_less try: from nose.tools import assert_greater except ImportError: assert_greater = _assert_greater def _assert_allclose(actual, desired, rtol=1e-7, atol=0, err_msg='', verbose=True): actual, desired = np.asanyarray(actual), np.asanyarray(desired) if np.allclose(actual, desired, rtol=rtol, atol=atol): return msg = ('Array not equal to tolerance rtol=%g, atol=%g: ' 'actual %s, desired %s') % (rtol, atol, actual, desired) raise AssertionError(msg) if hasattr(np.testing, 'assert_allclose'): assert_allclose = np.testing.assert_allclose else: assert_allclose = _assert_allclose def assert_raise_message(exceptions, message, function, *args, **kwargs): """Helper function to test error messages in exceptions Parameters ---------- exceptions : exception or tuple of exception Name of the estimator func : callable Calable object to raise error *args : the positional arguments to `func`. **kw : the keyword arguments to `func` """ try: function(*args, **kwargs) except exceptions as e: error_message = str(e) if message not in error_message: raise AssertionError("Error message does not include the expected" " string: %r. Observed error message: %r" % (message, error_message)) else: # concatenate exception names if isinstance(exceptions, tuple): names = " or ".join(e.__name__ for e in exceptions) else: names = exceptions.__name__ raise AssertionError("%s not raised by %s" % (names, function.__name__)) def fake_mldata(columns_dict, dataname, matfile, ordering=None): """Create a fake mldata data set. Parameters ---------- columns_dict : dict, keys=str, values=ndarray Contains data as columns_dict[column_name] = array of data. dataname : string Name of data set. matfile : string or file object The file name string or the file-like object of the output file. ordering : list, default None List of column_names, determines the ordering in the data set. Notes ----- This function transposes all arrays, while fetch_mldata only transposes 'data', keep that into account in the tests. """ datasets = dict(columns_dict) # transpose all variables for name in datasets: datasets[name] = datasets[name].T if ordering is None: ordering = sorted(list(datasets.keys())) # NOTE: setting up this array is tricky, because of the way Matlab # re-packages 1D arrays datasets['mldata_descr_ordering'] = sp.empty((1, len(ordering)), dtype='object') for i, name in enumerate(ordering): datasets['mldata_descr_ordering'][0, i] = name scipy.io.savemat(matfile, datasets, oned_as='column') class mock_mldata_urlopen(object): def __init__(self, mock_datasets): """Object that mocks the urlopen function to fake requests to mldata. `mock_datasets` is a dictionary of {dataset_name: data_dict}, or {dataset_name: (data_dict, ordering). `data_dict` itself is a dictionary of {column_name: data_array}, and `ordering` is a list of column_names to determine the ordering in the data set (see `fake_mldata` for details). When requesting a dataset with a name that is in mock_datasets, this object creates a fake dataset in a StringIO object and returns it. Otherwise, it raises an HTTPError. """ self.mock_datasets = mock_datasets def __call__(self, urlname): dataset_name = urlname.split('/')[-1] if dataset_name in self.mock_datasets: resource_name = '_' + dataset_name from io import BytesIO matfile = BytesIO() dataset = self.mock_datasets[dataset_name] ordering = None if isinstance(dataset, tuple): dataset, ordering = dataset fake_mldata(dataset, resource_name, matfile, ordering) matfile.seek(0) return matfile else: raise HTTPError(urlname, 404, dataset_name + " is not available", [], None) def install_mldata_mock(mock_datasets): # Lazy import to avoid mutually recursive imports from sklearn import datasets datasets.mldata.urlopen = mock_mldata_urlopen(mock_datasets) def uninstall_mldata_mock(): # Lazy import to avoid mutually recursive imports from sklearn import datasets datasets.mldata.urlopen = urlopen # Meta estimators need another estimator to be instantiated. META_ESTIMATORS = ["OneVsOneClassifier", "OutputCodeClassifier", "OneVsRestClassifier", "RFE", "RFECV", "BaseEnsemble"] # estimators that there is no way to default-construct sensibly OTHER = ["Pipeline", "FeatureUnion", "GridSearchCV", "RandomizedSearchCV", "SelectFromModel"] # some trange ones DONT_TEST = ['SparseCoder', 'EllipticEnvelope', 'DictVectorizer', 'LabelBinarizer', 'LabelEncoder', 'MultiLabelBinarizer', 'TfidfTransformer', 'TfidfVectorizer', 'IsotonicRegression', 'OneHotEncoder', 'RandomTreesEmbedding', 'FeatureHasher', 'DummyClassifier', 'DummyRegressor', 'TruncatedSVD', 'PolynomialFeatures', 'GaussianRandomProjectionHash', 'HashingVectorizer', 'CheckingClassifier', 'PatchExtractor', 'CountVectorizer', # GradientBoosting base estimators, maybe should # exclude them in another way 'ZeroEstimator', 'ScaledLogOddsEstimator', 'QuantileEstimator', 'MeanEstimator', 'LogOddsEstimator', 'PriorProbabilityEstimator', '_SigmoidCalibration', 'VotingClassifier'] def all_estimators(include_meta_estimators=False, include_other=False, type_filter=None, include_dont_test=False): """Get a list of all estimators from sklearn. This function crawls the module and gets all classes that inherit from BaseEstimator. Classes that are defined in test-modules are not included. By default meta_estimators such as GridSearchCV are also not included. Parameters ---------- include_meta_estimators : boolean, default=False Whether to include meta-estimators that can be constructed using an estimator as their first argument. These are currently BaseEnsemble, OneVsOneClassifier, OutputCodeClassifier, OneVsRestClassifier, RFE, RFECV. include_other : boolean, default=False Wether to include meta-estimators that are somehow special and can not be default-constructed sensibly. These are currently Pipeline, FeatureUnion and GridSearchCV include_dont_test : boolean, default=False Whether to include "special" label estimator or test processors. type_filter : string, list of string, or None, default=None Which kind of estimators should be returned. If None, no filter is applied and all estimators are returned. Possible values are 'classifier', 'regressor', 'cluster' and 'transformer' to get estimators only of these specific types, or a list of these to get the estimators that fit at least one of the types. Returns ------- estimators : list of tuples List of (name, class), where ``name`` is the class name as string and ``class`` is the actuall type of the class. """ def is_abstract(c): if not(hasattr(c, '__abstractmethods__')): return False if not len(c.__abstractmethods__): return False return True all_classes = [] # get parent folder path = sklearn.__path__ for importer, modname, ispkg in pkgutil.walk_packages( path=path, prefix='sklearn.', onerror=lambda x: None): if ".tests." in modname: continue module = __import__(modname, fromlist="dummy") classes = inspect.getmembers(module, inspect.isclass) all_classes.extend(classes) all_classes = set(all_classes) estimators = [c for c in all_classes if (issubclass(c[1], BaseEstimator) and c[0] != 'BaseEstimator')] # get rid of abstract base classes estimators = [c for c in estimators if not is_abstract(c[1])] if not include_dont_test: estimators = [c for c in estimators if not c[0] in DONT_TEST] if not include_other: estimators = [c for c in estimators if not c[0] in OTHER] # possibly get rid of meta estimators if not include_meta_estimators: estimators = [c for c in estimators if not c[0] in META_ESTIMATORS] if type_filter is not None: if not isinstance(type_filter, list): type_filter = [type_filter] else: type_filter = list(type_filter) # copy filtered_estimators = [] filters = {'classifier': ClassifierMixin, 'regressor': RegressorMixin, 'transformer': TransformerMixin, 'cluster': ClusterMixin} for name, mixin in filters.items(): if name in type_filter: type_filter.remove(name) filtered_estimators.extend([est for est in estimators if issubclass(est[1], mixin)]) estimators = filtered_estimators if type_filter: raise ValueError("Parameter type_filter must be 'classifier', " "'regressor', 'transformer', 'cluster' or None, got" " %s." % repr(type_filter)) # drop duplicates, sort for reproducibility return sorted(set(estimators)) def set_random_state(estimator, random_state=0): """Set random state of an estimator if it has the `random_state` param. Classes for whom random_state is deprecated are ignored. Currently DBSCAN is one such class. """ if isinstance(estimator, DBSCAN): return if "random_state" in estimator.get_params(): estimator.set_params(random_state=random_state) def if_matplotlib(func): """Test decorator that skips test if matplotlib not installed. """ @wraps(func) def run_test(*args, **kwargs): try: import matplotlib matplotlib.use('Agg', warn=False) # this fails if no $DISPLAY specified import matplotlib.pyplot as plt plt.figure() except ImportError: raise SkipTest('Matplotlib not available.') else: return func(*args, **kwargs) return run_test def skip_if_32bit(func): """Test decorator that skips tests on 32bit platforms.""" @wraps(func) def run_test(*args, **kwargs): bits = 8 * struct.calcsize("P") if bits == 32: raise SkipTest('Test skipped on 32bit platforms.') else: return func(*args, **kwargs) return run_test def if_not_mac_os(versions=('10.7', '10.8', '10.9'), message='Multi-process bug in Mac OS X >= 10.7 ' '(see issue #636)'): """Test decorator that skips test if OS is Mac OS X and its major version is one of ``versions``. """ warnings.warn("if_not_mac_os is deprecated in 0.17 and will be removed" " in 0.19: use the safer and more generic" " if_safe_multiprocessing_with_blas instead", DeprecationWarning) mac_version, _, _ = platform.mac_ver() skip = '.'.join(mac_version.split('.')[:2]) in versions def decorator(func): if skip: @wraps(func) def func(*args, **kwargs): raise SkipTest(message) return func return decorator def if_safe_multiprocessing_with_blas(func): """Decorator for tests involving both BLAS calls and multiprocessing Under POSIX (e.g. Linux or OSX), using multiprocessing in conjunction with some implementation of BLAS (or other libraries that manage an internal posix thread pool) can cause a crash or a freeze of the Python process. In practice all known packaged distributions (from Linux distros or Anaconda) of BLAS under Linux seems to be safe. So we this problem seems to only impact OSX users. This wrapper makes it possible to skip tests that can possibly cause this crash under OS X with. Under Python 3.4+ it is possible to use the `forkserver` start method for multiprocessing to avoid this issue. However it can cause pickling errors on interactively defined functions. It therefore not enabled by default. """ @wraps(func) def run_test(*args, **kwargs): if sys.platform == 'darwin': raise SkipTest( "Possible multi-process bug with some BLAS") return func(*args, **kwargs) return run_test def clean_warning_registry(): """Safe way to reset warnings """ warnings.resetwarnings() reg = "__warningregistry__" for mod_name, mod in list(sys.modules.items()): if 'six.moves' in mod_name: continue if hasattr(mod, reg): getattr(mod, reg).clear() def check_skip_network(): if int(os.environ.get('SKLEARN_SKIP_NETWORK_TESTS', 0)): raise SkipTest("Text tutorial requires large dataset download") def check_skip_travis(): """Skip test if being run on Travis.""" if os.environ.get('TRAVIS') == "true": raise SkipTest("This test needs to be skipped on Travis") def _delete_folder(folder_path, warn=False): """Utility function to cleanup a temporary folder if still existing. Copy from joblib.pool (for independance)""" try: if os.path.exists(folder_path): # This can fail under windows, # but will succeed when called by atexit shutil.rmtree(folder_path) except WindowsError: if warn: warnings.warn("Could not delete temporary folder %s" % folder_path) class TempMemmap(object): def __init__(self, data, mmap_mode='r'): self.temp_folder = tempfile.mkdtemp(prefix='sklearn_testing_') self.mmap_mode = mmap_mode self.data = data def __enter__(self): fpath = op.join(self.temp_folder, 'data.pkl') joblib.dump(self.data, fpath) data_read_only = joblib.load(fpath, mmap_mode=self.mmap_mode) atexit.register(lambda: _delete_folder(self.temp_folder, warn=True)) return data_read_only def __exit__(self, exc_type, exc_val, exc_tb): _delete_folder(self.temp_folder) with_network = with_setup(check_skip_network) with_travis = with_setup(check_skip_travis)
mit
huzq/scikit-learn
examples/neighbors/plot_digits_kde_sampling.py
50
2007
""" ========================= Kernel Density Estimation ========================= This example shows how kernel density estimation (KDE), a powerful non-parametric density estimation technique, can be used to learn a generative model for a dataset. With this generative model in place, new samples can be drawn. These new samples reflect the underlying model of the data. """ import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_digits from sklearn.neighbors import KernelDensity from sklearn.decomposition import PCA from sklearn.model_selection import GridSearchCV # load the data digits = load_digits() # project the 64-dimensional data to a lower dimension pca = PCA(n_components=15, whiten=False) data = pca.fit_transform(digits.data) # use grid search cross-validation to optimize the bandwidth params = {'bandwidth': np.logspace(-1, 1, 20)} grid = GridSearchCV(KernelDensity(), params) grid.fit(data) print("best bandwidth: {0}".format(grid.best_estimator_.bandwidth)) # use the best estimator to compute the kernel density estimate kde = grid.best_estimator_ # sample 44 new points from the data new_data = kde.sample(44, random_state=0) new_data = pca.inverse_transform(new_data) # turn data into a 4x11 grid new_data = new_data.reshape((4, 11, -1)) real_data = digits.data[:44].reshape((4, 11, -1)) # plot real digits and resampled digits fig, ax = plt.subplots(9, 11, subplot_kw=dict(xticks=[], yticks=[])) for j in range(11): ax[4, j].set_visible(False) for i in range(4): im = ax[i, j].imshow(real_data[i, j].reshape((8, 8)), cmap=plt.cm.binary, interpolation='nearest') im.set_clim(0, 16) im = ax[i + 5, j].imshow(new_data[i, j].reshape((8, 8)), cmap=plt.cm.binary, interpolation='nearest') im.set_clim(0, 16) ax[0, 5].set_title('Selection from the input data') ax[5, 5].set_title('"New" digits drawn from the kernel density model') plt.show()
bsd-3-clause
ArtsiomCh/tensorflow
tensorflow/examples/learn/iris_custom_model.py
37
3651
# 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 Estimator for Iris plant dataset.""" 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 X_FEATURE = 'x' # Name of the input feature. def my_model(features, labels, mode): """DNN with three hidden layers, and dropout of 0.1 probability.""" # Create three fully connected layers respectively of size 10, 20, and 10 with # each layer having a dropout probability of 0.1. net = features[X_FEATURE] for units in [10, 20, 10]: net = tf.layers.dense(net, units=units, activation=tf.nn.relu) net = tf.layers.dropout(net, rate=0.1) # Compute logits (1 per class). logits = tf.layers.dense(net, 3, activation=None) # Compute predictions. predicted_classes = tf.argmax(logits, 1) if mode == tf.estimator.ModeKeys.PREDICT: predictions = { 'class': predicted_classes, 'prob': tf.nn.softmax(logits) } return tf.estimator.EstimatorSpec(mode, predictions=predictions) # Convert the labels to a one-hot tensor of shape (length of features, 3) and # with a on-value of 1 for each one-hot vector of length 3. onehot_labels = tf.one_hot(labels, 3, 1, 0) # Compute loss. loss = tf.losses.softmax_cross_entropy( onehot_labels=onehot_labels, logits=logits) # Create training op. if mode == tf.estimator.ModeKeys.TRAIN: optimizer = tf.train.AdagradOptimizer(learning_rate=0.1) train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op) # Compute evaluation metrics. eval_metric_ops = { 'accuracy': tf.metrics.accuracy( labels=labels, predictions=predicted_classes) } return tf.estimator.EstimatorSpec( mode, loss=loss, eval_metric_ops=eval_metric_ops) def main(unused_argv): 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) classifier = tf.estimator.Estimator(model_fn=my_model) # 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=1000) # 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'] 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
jfsehuanes/aktivitarium
main.py
1
1331
__author__ = 'juan' from auxiliary import * import numpy as np import pandas as pd from IPython import embed def fill_curr_course(current_course, persons_in_course, persons_data_frame): """ Function returns the next matching person. :param current_course: :param persons_in_course: :param persons_data_frame: """ room_persons = persons_in_course embed() quit() pass if __name__ == '__main__': activities_df = pd.read_csv('./lista_de_actividades.csv') days = len(activities_df) persons_df = pd.read_csv('./lista_de_prueba.csv') tmp_dict = {'day': '', 'person': '', 'course': '', 'room': '', 'gender': ''} entries_df = pd.DataFrame(tmp_dict) for curr_day in np.arange(0, days): for curr_course in np.array(activities_df.index): max_persons = activities_df.places[curr_course] embed() quit() persons_in_course = entries_df[entries_df.course == activities_df.activities[curr_course]] if len(persons_in_course) < max_persons: fill_curr_course(curr_course, persons_in_course, persons_df) else: # Course is full, need to continue print 'Course is full, continuing with next person...' continue # embed() # quit()
gpl-2.0
jzt5132/scikit-learn
benchmarks/bench_lasso.py
297
3305
""" Benchmarks of Lasso vs LassoLars First, we fix a training set and increase the number of samples. Then we plot the computation time as function of the number of samples. In the second benchmark, we increase the number of dimensions of the training set. Then we plot the computation time as function of the number of dimensions. In both cases, only 10% of the features are informative. """ import gc from time import time import numpy as np from sklearn.datasets.samples_generator import make_regression def compute_bench(alpha, n_samples, n_features, precompute): lasso_results = [] lars_lasso_results = [] it = 0 for ns in n_samples: for nf in n_features: it += 1 print('==================') print('Iteration %s of %s' % (it, max(len(n_samples), len(n_features)))) print('==================') n_informative = nf // 10 X, Y, coef_ = make_regression(n_samples=ns, n_features=nf, n_informative=n_informative, noise=0.1, coef=True) X /= np.sqrt(np.sum(X ** 2, axis=0)) # Normalize data gc.collect() print("- benchmarking Lasso") clf = Lasso(alpha=alpha, fit_intercept=False, precompute=precompute) tstart = time() clf.fit(X, Y) lasso_results.append(time() - tstart) gc.collect() print("- benchmarking LassoLars") clf = LassoLars(alpha=alpha, fit_intercept=False, normalize=False, precompute=precompute) tstart = time() clf.fit(X, Y) lars_lasso_results.append(time() - tstart) return lasso_results, lars_lasso_results if __name__ == '__main__': from sklearn.linear_model import Lasso, LassoLars import pylab as pl alpha = 0.01 # regularization parameter n_features = 10 list_n_samples = np.linspace(100, 1000000, 5).astype(np.int) lasso_results, lars_lasso_results = compute_bench(alpha, list_n_samples, [n_features], precompute=True) pl.figure('scikit-learn LASSO benchmark results') pl.subplot(211) pl.plot(list_n_samples, lasso_results, 'b-', label='Lasso') pl.plot(list_n_samples, lars_lasso_results, 'r-', label='LassoLars') pl.title('precomputed Gram matrix, %d features, alpha=%s' % (n_features, alpha)) pl.legend(loc='upper left') pl.xlabel('number of samples') pl.ylabel('Time (s)') pl.axis('tight') n_samples = 2000 list_n_features = np.linspace(500, 3000, 5).astype(np.int) lasso_results, lars_lasso_results = compute_bench(alpha, [n_samples], list_n_features, precompute=False) pl.subplot(212) pl.plot(list_n_features, lasso_results, 'b-', label='Lasso') pl.plot(list_n_features, lars_lasso_results, 'r-', label='LassoLars') pl.title('%d samples, alpha=%s' % (n_samples, alpha)) pl.legend(loc='upper left') pl.xlabel('number of features') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
wzbozon/scikit-learn
sklearn/datasets/twenty_newsgroups.py
126
13591
"""Caching loader for the 20 newsgroups text classification dataset The description of the dataset is available on the official website at: http://people.csail.mit.edu/jrennie/20Newsgroups/ Quoting the introduction: The 20 Newsgroups data set is a collection of approximately 20,000 newsgroup documents, partitioned (nearly) evenly across 20 different newsgroups. To the best of my knowledge, it was originally collected by Ken Lang, probably for his Newsweeder: Learning to filter netnews paper, though he does not explicitly mention this collection. The 20 newsgroups collection has become a popular data set for experiments in text applications of machine learning techniques, such as text classification and text clustering. This dataset loader will download the recommended "by date" variant of the dataset and which features a point in time split between the train and test sets. The compressed dataset size is around 14 Mb compressed. Once uncompressed the train set is 52 MB and the test set is 34 MB. The data is downloaded, extracted and cached in the '~/scikit_learn_data' folder. The `fetch_20newsgroups` function will not vectorize the data into numpy arrays but the dataset lists the filenames of the posts and their categories as target labels. The `fetch_20newsgroups_vectorized` function will in addition do a simple tf-idf vectorization step. """ # Copyright (c) 2011 Olivier Grisel <[email protected]> # License: BSD 3 clause import os import logging import tarfile import pickle import shutil import re import codecs import numpy as np import scipy.sparse as sp from .base import get_data_home from .base import Bunch from .base import load_files from ..utils import check_random_state from ..feature_extraction.text import CountVectorizer from ..preprocessing import normalize from ..externals import joblib, six if six.PY3: from urllib.request import urlopen else: from urllib2 import urlopen logger = logging.getLogger(__name__) URL = ("http://people.csail.mit.edu/jrennie/" "20Newsgroups/20news-bydate.tar.gz") ARCHIVE_NAME = "20news-bydate.tar.gz" CACHE_NAME = "20news-bydate.pkz" TRAIN_FOLDER = "20news-bydate-train" TEST_FOLDER = "20news-bydate-test" def download_20newsgroups(target_dir, cache_path): """Download the 20 newsgroups data and stored it as a zipped pickle.""" archive_path = os.path.join(target_dir, ARCHIVE_NAME) train_path = os.path.join(target_dir, TRAIN_FOLDER) test_path = os.path.join(target_dir, TEST_FOLDER) if not os.path.exists(target_dir): os.makedirs(target_dir) if os.path.exists(archive_path): # Download is not complete as the .tar.gz file is removed after # download. logger.warning("Download was incomplete, downloading again.") os.remove(archive_path) logger.warning("Downloading dataset from %s (14 MB)", URL) opener = urlopen(URL) with open(archive_path, 'wb') as f: f.write(opener.read()) logger.info("Decompressing %s", archive_path) tarfile.open(archive_path, "r:gz").extractall(path=target_dir) os.remove(archive_path) # Store a zipped pickle cache = dict(train=load_files(train_path, encoding='latin1'), test=load_files(test_path, encoding='latin1')) compressed_content = codecs.encode(pickle.dumps(cache), 'zlib_codec') with open(cache_path, 'wb') as f: f.write(compressed_content) shutil.rmtree(target_dir) return cache def strip_newsgroup_header(text): """ Given text in "news" format, strip the headers, by removing everything before the first blank line. """ _before, _blankline, after = text.partition('\n\n') return after _QUOTE_RE = re.compile(r'(writes in|writes:|wrote:|says:|said:' r'|^In article|^Quoted from|^\||^>)') def strip_newsgroup_quoting(text): """ Given text in "news" format, strip lines beginning with the quote characters > or |, plus lines that often introduce a quoted section (for example, because they contain the string 'writes:'.) """ good_lines = [line for line in text.split('\n') if not _QUOTE_RE.search(line)] return '\n'.join(good_lines) def strip_newsgroup_footer(text): """ Given text in "news" format, attempt to remove a signature block. As a rough heuristic, we assume that signatures are set apart by either a blank line or a line made of hyphens, and that it is the last such line in the file (disregarding blank lines at the end). """ lines = text.strip().split('\n') for line_num in range(len(lines) - 1, -1, -1): line = lines[line_num] if line.strip().strip('-') == '': break if line_num > 0: return '\n'.join(lines[:line_num]) else: return text def fetch_20newsgroups(data_home=None, subset='train', categories=None, shuffle=True, random_state=42, remove=(), download_if_missing=True): """Load the filenames and data from the 20 newsgroups dataset. Read more in the :ref:`User Guide <20newsgroups>`. Parameters ---------- subset: 'train' or 'test', 'all', optional Select the dataset to load: 'train' for the training set, 'test' for the test set, 'all' for both, with shuffled ordering. data_home: optional, default: None Specify a download and cache folder for the datasets. If None, all scikit-learn data is stored in '~/scikit_learn_data' subfolders. categories: None or collection of string or unicode If None (default), load all the categories. If not None, list of category names to load (other categories ignored). shuffle: bool, optional Whether or not to shuffle the data: might be important for models that make the assumption that the samples are independent and identically distributed (i.i.d.), such as stochastic gradient descent. random_state: numpy random number generator or seed integer Used to shuffle the dataset. download_if_missing: optional, True by default If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site. remove: tuple May contain any subset of ('headers', 'footers', 'quotes'). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata. 'headers' removes newsgroup headers, 'footers' removes blocks at the ends of posts that look like signatures, and 'quotes' removes lines that appear to be quoting another post. 'headers' follows an exact standard; the other filters are not always correct. """ data_home = get_data_home(data_home=data_home) cache_path = os.path.join(data_home, CACHE_NAME) twenty_home = os.path.join(data_home, "20news_home") cache = None if os.path.exists(cache_path): try: with open(cache_path, 'rb') as f: compressed_content = f.read() uncompressed_content = codecs.decode( compressed_content, 'zlib_codec') cache = pickle.loads(uncompressed_content) except Exception as e: print(80 * '_') print('Cache loading failed') print(80 * '_') print(e) if cache is None: if download_if_missing: cache = download_20newsgroups(target_dir=twenty_home, cache_path=cache_path) else: raise IOError('20Newsgroups dataset not found') if subset in ('train', 'test'): data = cache[subset] elif subset == 'all': data_lst = list() target = list() filenames = list() for subset in ('train', 'test'): data = cache[subset] data_lst.extend(data.data) target.extend(data.target) filenames.extend(data.filenames) data.data = data_lst data.target = np.array(target) data.filenames = np.array(filenames) else: raise ValueError( "subset can only be 'train', 'test' or 'all', got '%s'" % subset) data.description = 'the 20 newsgroups by date dataset' if 'headers' in remove: data.data = [strip_newsgroup_header(text) for text in data.data] if 'footers' in remove: data.data = [strip_newsgroup_footer(text) for text in data.data] if 'quotes' in remove: data.data = [strip_newsgroup_quoting(text) for text in data.data] if categories is not None: labels = [(data.target_names.index(cat), cat) for cat in categories] # Sort the categories to have the ordering of the labels labels.sort() labels, categories = zip(*labels) mask = np.in1d(data.target, labels) data.filenames = data.filenames[mask] data.target = data.target[mask] # searchsorted to have continuous labels data.target = np.searchsorted(labels, data.target) data.target_names = list(categories) # Use an object array to shuffle: avoids memory copy data_lst = np.array(data.data, dtype=object) data_lst = data_lst[mask] data.data = data_lst.tolist() if shuffle: random_state = check_random_state(random_state) indices = np.arange(data.target.shape[0]) random_state.shuffle(indices) data.filenames = data.filenames[indices] data.target = data.target[indices] # Use an object array to shuffle: avoids memory copy data_lst = np.array(data.data, dtype=object) data_lst = data_lst[indices] data.data = data_lst.tolist() return data def fetch_20newsgroups_vectorized(subset="train", remove=(), data_home=None): """Load the 20 newsgroups dataset and transform it into tf-idf vectors. This is a convenience function; the tf-idf transformation is done using the default settings for `sklearn.feature_extraction.text.Vectorizer`. For more advanced usage (stopword filtering, n-gram extraction, etc.), combine fetch_20newsgroups with a custom `Vectorizer` or `CountVectorizer`. Read more in the :ref:`User Guide <20newsgroups>`. Parameters ---------- subset: 'train' or 'test', 'all', optional Select the dataset to load: 'train' for the training set, 'test' for the test set, 'all' for both, with shuffled ordering. data_home: optional, default: None Specify an download and cache folder for the datasets. If None, all scikit-learn data is stored in '~/scikit_learn_data' subfolders. remove: tuple May contain any subset of ('headers', 'footers', 'quotes'). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata. 'headers' removes newsgroup headers, 'footers' removes blocks at the ends of posts that look like signatures, and 'quotes' removes lines that appear to be quoting another post. Returns ------- bunch : Bunch object bunch.data: sparse matrix, shape [n_samples, n_features] bunch.target: array, shape [n_samples] bunch.target_names: list, length [n_classes] """ data_home = get_data_home(data_home=data_home) filebase = '20newsgroup_vectorized' if remove: filebase += 'remove-' + ('-'.join(remove)) target_file = os.path.join(data_home, filebase + ".pk") # we shuffle but use a fixed seed for the memoization data_train = fetch_20newsgroups(data_home=data_home, subset='train', categories=None, shuffle=True, random_state=12, remove=remove) data_test = fetch_20newsgroups(data_home=data_home, subset='test', categories=None, shuffle=True, random_state=12, remove=remove) if os.path.exists(target_file): X_train, X_test = joblib.load(target_file) else: vectorizer = CountVectorizer(dtype=np.int16) X_train = vectorizer.fit_transform(data_train.data).tocsr() X_test = vectorizer.transform(data_test.data).tocsr() joblib.dump((X_train, X_test), target_file, compress=9) # the data is stored as int16 for compactness # but normalize needs floats X_train = X_train.astype(np.float64) X_test = X_test.astype(np.float64) normalize(X_train, copy=False) normalize(X_test, copy=False) target_names = data_train.target_names if subset == "train": data = X_train target = data_train.target elif subset == "test": data = X_test target = data_test.target elif subset == "all": data = sp.vstack((X_train, X_test)).tocsr() target = np.concatenate((data_train.target, data_test.target)) else: raise ValueError("%r is not a valid subset: should be one of " "['train', 'test', 'all']" % subset) return Bunch(data=data, target=target, target_names=target_names)
bsd-3-clause
puruckertom/ubertool
ubertool/iec/iec_exe.py
1
2127
from __future__ import division import numpy as np import pandas as pd from base.uber_model import UberModel, ModelSharedInputs from .iec_functions import IecFunctions class IecInputs(ModelSharedInputs): """ Input class for IEC. """ def __init__(self): """Class representing the inputs for IEC""" super(IecInputs, self).__init__() self.dose_response = pd.Series([], dtype="float") self.lc50 = pd.Series([], dtype="float") self.threshold = pd.Series([], dtype="float") class IecOutputs(object): """ Output class for IEC. """ def __init__(self): """Class representing the outputs for IEC""" super(IecOutputs, self).__init__() self.out_z_score_f = pd.Series([], dtype="float", name="out_z_score_f") self.out_f8_f = pd.Series([], dtype="float", name="out_f8_f") self.out_chance_f = pd.Series([], dtype="float", name="out_chance_f") class Iec(UberModel, IecInputs, IecOutputs, IecFunctions): """ IEC model for proportional population effect based on normal distribution. """ def __init__(self, pd_obj, pd_obj_exp): """Class representing the IEC model and containing all its methods""" super(Iec, self).__init__() self.pd_obj = pd_obj self.pd_obj_exp = pd_obj_exp self.pd_obj_out = None def execute_model(self): """ Callable to execute the running of the model: 1) Populate input parameters 2) Create output DataFrame to hold the model outputs 3) Run the model's methods to generate outputs 4) Fill the output DataFrame with the generated model outputs """ self.populate_inputs(self.pd_obj) self.pd_obj_out = self.populate_outputs() self.run_methods() self.fill_output_dataframe() def run_methods(self): """ Execute all algorithm methods for model logic. :return: """ try: self.z_score_f() self.f8_f() self.chance_f() except Exception as e: pass
unlicense
ronalcc/zipline
zipline/finance/performance/position_tracker.py
4
13048
from __future__ import division import logbook import numpy as np import pandas as pd from pandas.lib import checknull try: # optional cython based OrderedDict from cyordereddict import OrderedDict except ImportError: from collections import OrderedDict from six import iteritems, itervalues from zipline.finance.slippage import Transaction from zipline.utils.serialization_utils import ( VERSION_LABEL ) import zipline.protocol as zp from zipline.assets import ( Equity, Future ) from zipline.finance.trading import with_environment from . position import positiondict log = logbook.Logger('Performance') class PositionTracker(object): def __init__(self): # sid => position object self.positions = positiondict() # Arrays for quick calculations of positions value self._position_amounts = OrderedDict() self._position_last_sale_prices = OrderedDict() self._position_value_multipliers = OrderedDict() self._position_exposure_multipliers = OrderedDict() self._position_payout_multipliers = OrderedDict() self._unpaid_dividends = pd.DataFrame( columns=zp.DIVIDEND_PAYMENT_FIELDS, ) self._positions_store = zp.Positions() @with_environment() def _retrieve_asset(self, sid, env=None): return env.asset_finder.retrieve_asset(sid) def _update_multipliers(self, sid): try: self._position_value_multipliers[sid] self._position_exposure_multipliers[sid] self._position_payout_multipliers[sid] except KeyError: # Collect the value multipliers from applicable sids asset = self._retrieve_asset(sid) if isinstance(asset, Equity): self._position_value_multipliers[sid] = 1 self._position_exposure_multipliers[sid] = 1 self._position_payout_multipliers[sid] = 0 if isinstance(asset, Future): self._position_value_multipliers[sid] = 0 self._position_exposure_multipliers[sid] = \ asset.contract_multiplier self._position_payout_multipliers[sid] = \ asset.contract_multiplier def update_last_sale(self, event): # NOTE, PerformanceTracker already vetted as TRADE type sid = event.sid if sid not in self.positions: return 0 price = event.price if checknull(price): return 0 pos = self.positions[sid] old_price = pos.last_sale_price pos.last_sale_date = event.dt pos.last_sale_price = price self._position_last_sale_prices[sid] = price # Calculate cash adjustment on assets with multipliers return ((price - old_price) * self._position_payout_multipliers[sid] * pos.amount) def update_positions(self, positions): # update positions in batch self.positions.update(positions) for sid, pos in iteritems(positions): self._position_amounts[sid] = pos.amount self._position_last_sale_prices[sid] = pos.last_sale_price self._update_multipliers(sid) def update_position(self, sid, amount=None, last_sale_price=None, last_sale_date=None, cost_basis=None): pos = self.positions[sid] if amount is not None: pos.amount = amount self._position_amounts[sid] = amount self._position_values = None # invalidate cache self._update_multipliers(sid=sid) if last_sale_price is not None: pos.last_sale_price = last_sale_price self._position_last_sale_prices[sid] = last_sale_price self._position_values = None # invalidate cache if last_sale_date is not None: pos.last_sale_date = last_sale_date if cost_basis is not None: pos.cost_basis = cost_basis def execute_transaction(self, txn): # Update Position # ---------------- sid = txn.sid position = self.positions[sid] position.update(txn) self._position_amounts[sid] = position.amount self._position_last_sale_prices[sid] = position.last_sale_price self._update_multipliers(sid) def handle_commission(self, commission): # Adjust the cost basis of the stock if we own it if commission.sid in self.positions: self.positions[commission.sid].\ adjust_commission_cost_basis(commission) @property def position_values(self): iter_amount_price_multiplier = zip( itervalues(self._position_amounts), itervalues(self._position_last_sale_prices), itervalues(self._position_value_multipliers), ) return [ price * amount * multiplier for price, amount, multiplier in iter_amount_price_multiplier ] @property def position_exposures(self): iter_amount_price_multiplier = zip( itervalues(self._position_amounts), itervalues(self._position_last_sale_prices), itervalues(self._position_exposure_multipliers), ) return [ price * amount * multiplier for price, amount, multiplier in iter_amount_price_multiplier ] def calculate_positions_value(self): if len(self.position_values) == 0: return np.float64(0) return sum(self.position_values) def calculate_positions_exposure(self): if len(self.position_exposures) == 0: return np.float64(0) return sum(self.position_exposures) def _longs_count(self): return sum(1 for i in self.position_exposures if i > 0) def _long_exposure(self): return sum(i for i in self.position_exposures if i > 0) def _long_value(self): return sum(i for i in self.position_values if i > 0) def _shorts_count(self): return sum(1 for i in self.position_exposures if i < 0) def _short_exposure(self): return sum(i for i in self.position_exposures if i < 0) def _short_value(self): return sum(i for i in self.position_values if i < 0) def _gross_exposure(self): return self._long_exposure() + abs(self._short_exposure()) def _gross_value(self): return self._long_value() + abs(self._short_value()) def _net_exposure(self): return self.calculate_positions_exposure() def _net_value(self): return self.calculate_positions_value() def handle_split(self, split): if split.sid in self.positions: # Make the position object handle the split. It returns the # leftover cash from a fractional share, if there is any. position = self.positions[split.sid] leftover_cash = position.handle_split(split) self._position_amounts[split.sid] = position.amount self._position_last_sale_prices[split.sid] = \ position.last_sale_price self._update_multipliers(split.sid) return leftover_cash def _maybe_earn_dividend(self, dividend): """ Take a historical dividend record and return a Series with fields in zipline.protocol.DIVIDEND_FIELDS (plus an 'id' field) representing the cash/stock amount we are owed when the dividend is paid. """ if dividend['sid'] in self.positions: return self.positions[dividend['sid']].earn_dividend(dividend) else: return zp.dividend_payment() def earn_dividends(self, dividend_frame): """ Given a frame of dividends whose ex_dates are all the next trading day, calculate and store the cash and/or stock payments to be paid on each dividend's pay date. """ earned = dividend_frame.apply(self._maybe_earn_dividend, axis=1)\ .dropna(how='all') if len(earned) > 0: # Store the earned dividends so that they can be paid on the # dividends' pay_dates. self._unpaid_dividends = pd.concat( [self._unpaid_dividends, earned], ) def _maybe_pay_dividend(self, dividend): """ Take a historical dividend record, look up any stored record of cash/stock we are owed for that dividend, and return a Series with fields drawn from zipline.protocol.DIVIDEND_PAYMENT_FIELDS. """ try: unpaid_dividend = self._unpaid_dividends.loc[dividend['id']] return unpaid_dividend except KeyError: return zp.dividend_payment() def pay_dividends(self, dividend_frame): """ Given a frame of dividends whose pay_dates are all the next trading day, grant the cash and/or stock payments that were calculated on the given dividends' ex dates. """ payments = dividend_frame.apply(self._maybe_pay_dividend, axis=1)\ .dropna(how='all') # Mark these dividends as paid by dropping them from our unpaid # table. self._unpaid_dividends.drop(payments.index) # Add stock for any stock dividends paid. Again, the values here may # be negative in the case of short positions. stock_payments = payments[payments['payment_sid'].notnull()] for _, row in stock_payments.iterrows(): stock = row['payment_sid'] share_count = row['share_count'] # note we create a Position for stock dividend if we don't # already own the asset position = self.positions[stock] position.amount += share_count self._position_amounts[stock] = position.amount self._position_last_sale_prices[stock] = position.last_sale_price self._update_multipliers(stock) # Add cash equal to the net cash payed from all dividends. Note that # "negative cash" is effectively paid if we're short an asset, # representing the fact that we're required to reimburse the owner of # the stock for any dividends paid while borrowing. net_cash_payment = payments['cash_amount'].fillna(0).sum() return net_cash_payment def create_close_position_transaction(self, event): if not self._position_amounts.get(event.sid): return None txn = Transaction( sid=event.sid, amount=(-1 * self._position_amounts[event.sid]), dt=event.dt, price=event.price, commission=0, order_id=0 ) return txn def get_positions(self): positions = self._positions_store for sid, pos in iteritems(self.positions): if pos.amount == 0: # Clear out the position if it has become empty since the last # time get_positions was called. Catching the KeyError is # faster than checking `if sid in positions`, and this can be # potentially called in a tight inner loop. try: del positions[sid] except KeyError: pass continue # Note that this will create a position if we don't currently have # an entry position = positions[sid] position.amount = pos.amount position.cost_basis = pos.cost_basis position.last_sale_price = pos.last_sale_price return positions def get_positions_list(self): positions = [] for sid, pos in iteritems(self.positions): if pos.amount != 0: positions.append(pos.to_dict()) return positions def __getstate__(self): state_dict = {} state_dict['positions'] = dict(self.positions) state_dict['unpaid_dividends'] = self._unpaid_dividends STATE_VERSION = 1 state_dict[VERSION_LABEL] = STATE_VERSION return state_dict def __setstate__(self, state): OLDEST_SUPPORTED_STATE = 1 version = state.pop(VERSION_LABEL) if version < OLDEST_SUPPORTED_STATE: raise BaseException("PositionTracker saved state is too old.") self.positions = positiondict() # note that positions_store is temporary and gets regened from # .positions self._positions_store = zp.Positions() self._unpaid_dividends = state['unpaid_dividends'] # Arrays for quick calculations of positions value self._position_amounts = OrderedDict() self._position_last_sale_prices = OrderedDict() self._position_value_multipliers = OrderedDict() self._position_exposure_multipliers = OrderedDict() self._position_payout_multipliers = OrderedDict() self.update_positions(state['positions'])
apache-2.0
BorisJeremic/Real-ESSI-Examples
analytic_solution/test_cases/Contact/Stress_Based_Contact_Verification/SoftContact_NonLinHardSoftShear/Area/A_1e2/Normalized_Shear_Stress_Plot.py
48
3533
#!/usr/bin/python import h5py import matplotlib.pylab as plt import matplotlib as mpl import sys import numpy as np; plt.rcParams.update({'font.size': 28}) # set tick width mpl.rcParams['xtick.major.size'] = 10 mpl.rcParams['xtick.major.width'] = 5 mpl.rcParams['xtick.minor.size'] = 10 mpl.rcParams['xtick.minor.width'] = 5 plt.rcParams['xtick.labelsize']=24 mpl.rcParams['ytick.major.size'] = 10 mpl.rcParams['ytick.major.width'] = 5 mpl.rcParams['ytick.minor.size'] = 10 mpl.rcParams['ytick.minor.width'] = 5 plt.rcParams['ytick.labelsize']=24 ############################################################### ## Analytical Solution ############################################################### # Go over each feioutput and plot each one. thefile = "Analytical_Solution_Shear.feioutput"; finput = h5py.File(thefile) # Read the time and displacement times = finput["time"][:] shear_strain_x = finput["/Model/Elements/Element_Outputs"][4,:] shear_strain_y = finput["/Model/Elements/Element_Outputs"][5,:] shear_stress_x = finput["/Model/Elements/Element_Outputs"][7,:] shear_stress_y = finput["/Model/Elements/Element_Outputs"][8,:] normal_stress = -finput["/Model/Elements/Element_Outputs"][9,:]; shear_strain = np.sqrt(shear_strain_x*shear_strain_x + shear_strain_y*shear_strain_y) ; shear_stress = np.sqrt(shear_stress_x*shear_stress_x + shear_stress_y*shear_stress_y ); shear_stress = shear_stress_x; shear_strain = shear_strain_x; # Configure the figure filename, according to the input filename. outfig=thefile.replace("_","-") outfigname=outfig.replace("h5.feioutput","pdf") # Plot the figure. Add labels and titles. plt.figure(figsize=(12,10)) plt.plot(shear_strain*5,shear_stress/normal_stress,'-r',label='Analytical Solution', Linewidth=4) plt.xlabel(r"Shear Displacement $\Delta_t [mm]$") plt.ylabel(r"Normalized Shear Stress $\tau/\sigma_n$") ############################################################### ## Numerical Solution ############################################################### # Go over each feioutput and plot each one. thefile = "Monotonic_Contact_Behaviour_Adding_Tangential_Load.h5.feioutput"; finput = h5py.File(thefile) # Read the time and displacement times = finput["time"][:] shear_strain_x = finput["/Model/Elements/Element_Outputs"][4,:] shear_strain_y = finput["/Model/Elements/Element_Outputs"][5,:] shear_stress_x = finput["/Model/Elements/Element_Outputs"][7,:] shear_stress_y = finput["/Model/Elements/Element_Outputs"][8,:] normal_stress = -finput["/Model/Elements/Element_Outputs"][9,:]; shear_strain = np.sqrt(shear_strain_x*shear_strain_x + shear_strain_y*shear_strain_y) ; shear_stress = np.sqrt(shear_stress_x*shear_stress_x + shear_stress_y*shear_stress_y ); shear_stress = shear_stress_x; shear_strain = shear_strain_x; # Configure the figure filename, according to the input filename. outfig=thefile.replace("_","-") outfigname=outfig.replace("h5.feioutput","pdf") # Plot the figure. Add labels and titles. plt.plot(shear_strain*5,shear_stress/normal_stress,'-k',label='Numerical Solution', Linewidth=4) plt.xlabel(r"Shear Displacement $\Delta_t [mm]$") plt.ylabel(r"Normalized Shear Stress $\tau/\sigma_n$") ######################################################## # # axes = plt.gca() # # axes.set_xlim([-7,7]) # # axes.set_ylim([-1,1]) outfigname = "Normalized_Shear_Stress.pdf"; legend = plt.legend() legend.get_frame().set_linewidth(0.0) legend.get_frame().set_facecolor('none') plt.savefig(outfigname, bbox_inches='tight') # plt.show()
cc0-1.0
ttfnrob/NLTalk
process_comments.py
1
9429
# Script to train a Naive Bayesian Classifier with NLTK - based on https://github.com/abromberg/sentiment_analysis_python # Classifier is trained using 1.6M Tweets pre-procesed at Sanford and available at http://help.sentiment140.com/for-students # Other data is also save din the training-data folder # Script and HTML tepmate are designed for specific Zooniverse data. # This is extracted from the Zooniverse discussion platform 'Talk' - please contact [email protected] for more information. # Inputs are a MySQL DB of text comments, and NLTK+training data # Outputs are CSV for of sentiment scores, and HTML files to show positive and negative comments # Basic Components import math import time import datetime import os import numpy as np import pandas as pd import sys # For database and data IO import pymysql import json import csv import urllib2 from subprocess import call # For Text processing import re, collections, itertools import nltk, nltk.classify.util, nltk.metrics from nltk.classify import NaiveBayesClassifier from nltk.metrics import BigramAssocMeasures from nltk.probability import FreqDist, ConditionalFreqDist ########################## Set parameters for different projects ########################## # project_slug = 'galaxy_zoo' # talk_url = 'talk.galaxyzoo.org' # min_comments = 5 # imgy='250' # project_slug = 'serengeti' # talk_url = 'talk.snapshotserengeti.org' # min_comments = 5 # imgy='175' project_slug = 'planet_four' talk_url = 'talk.planetfour.org' min_comments = 5 imgy='193' # project_slug = 'milky_way' # talk_url = 'talk.milkywayproject.org' # min_comments = 5 # imgy='125' ######################## ------------------------------------- ########################### # Function to create HTML file from list of results def focus_list_to_html_table(focus_list): html = """<style type="text/css"> body {font-size:14px; color: #ffffff; font-family: sans-serif;} div.thumbnail {display: inline-block; position: relative; margin: 5px;} div.thumbnail img {width:250px;} div.details {position:absolute; bottom:5px; left:5px; width:240px;} span.pos_frac {color: #8DFF87;} span.neg_frac {color: #FF7B6B;} span.zoo_id {position:absolute; top:5px; left:5px;} span.comments {position:absolute; top:5px; right:5px;} span.words {display:none;} </style> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> function id2url(zooID) { $.getJSON( "https://api.zooniverse.org/projects/"""+project_slug+"""/subjects/"+zooID, function( data ) { if (data.location.standard instanceof Array) { $("#"+zooID).attr("src", data.location.standard[0]); } else { $("#"+zooID).attr("src", data.location.standard); } }); } $(document).ready(function() { $( ".img_waiting" ).each(function( index ) { id2url($( this ).attr('id')); }); }) </script> <body> """ c=0 focus_list.reverse() total = len(focus_list) for r in focus_list: if r[3][0]=="A": img_html="<a href='http://"+talk_url+"/#/subjects/"+r[3]+"' target='_blank'><img class='img_waiting' id='"+r[3]+"' src='http://placehold.it/250x"+imgy+"' /></a>" else: img_html="<img class='thumb' src='http://placehold.it/250x"+imgy+"' />" html+="""<div class='thumbnail'> """+img_html+""" <span class='zoo_id'>"""+str(r[3])+"""</span> <div class='details'> <span class='pos_frac'>"""+"{:.2f}".format(r[1])+"""</span> <span class='neg_frac'>"""+"{:.2f}".format(r[2])+"""</span> </div> <span class='comments'>"""+str(r[4])+"""</span> </div>""" c+=1 html+="""</body>""" return html # Function to get the feature words from text def extract_features(document): document_words = set(document) features = {} for word in document_words: features[word] = (word in document_words) return features # Function to create dictionary of text def make_full_dict(words): return dict([(word, True) for word in words]) # Function to score words in text and make distributions def create_word_scores(): posWords = [] negWords = [] with open(POS_DATA_FILE, 'r') as posSentences: for i in posSentences: posWord = re.findall(r"[\w']+|[.,!?;]", i.rstrip()) posWords.append(posWord) with open(NEG_DATA_FILE, 'r') as negSentences: for i in negSentences: negWord = re.findall(r"[\w']+|[.,!?;]", i.rstrip()) negWords.append(negWord) posWords = list(itertools.chain(*posWords)) negWords = list(itertools.chain(*negWords)) # Build frequency distibution of all words and then frequency distributions of words within positive and negative labels word_fd = FreqDist() cond_word_fd = ConditionalFreqDist() for word in posWords: word_fd.inc(word.lower()) cond_word_fd['pos'].inc(word.lower()) for word in negWords: word_fd.inc(word.lower()) cond_word_fd['neg'].inc(word.lower()) # Create counts of positive, negative, and total words pos_word_count = cond_word_fd['pos'].N() neg_word_count = cond_word_fd['neg'].N() total_word_count = pos_word_count + neg_word_count # Builds dictionary of word scores based on chi-squared test word_scores = {} for word, freq in word_fd.iteritems(): pos_score = BigramAssocMeasures.chi_sq(cond_word_fd['pos'][word], (freq, pos_word_count), total_word_count) neg_score = BigramAssocMeasures.chi_sq(cond_word_fd['neg'][word], (freq, neg_word_count), total_word_count) word_scores[word] = pos_score + neg_score return word_scores if len(sys.argv) < 2: print "No file specificed\n" else: input_filename = sys.argv[1] print "Initialized "+project_slug+" with min of "+str(min_comments)+" - processing file "+input_filename print "Loading training data..." DATA_DIRECTORY = os.path.join('training-data', 'twitter_data') POS_DATA_FILE = os.path.join(DATA_DIRECTORY, 'positive_tweets.txt') NEG_DATA_FILE = os.path.join(DATA_DIRECTORY, 'negative_tweets.txt') # DATA_DIRECTORY = os.path.join('training-data', 'combined') # POS_DATA_FILE = os.path.join(DATA_DIRECTORY, 'positive.txt') # NEG_DATA_FILE = os.path.join(DATA_DIRECTORY, 'negative.txt') print "Training NLTK Bayesian classifier..." posFeatures = [] negFeatures = [] # Process text into words with pos/neg connotation with open(POS_DATA_FILE, 'r') as posSentences: for i in posSentences: posWords = re.findall(r"[\w']+|[.,!?;]", i.rstrip()) posWords = [make_full_dict(posWords), 'pos'] posFeatures.append(posWords) with open(NEG_DATA_FILE, 'r') as negSentences: for i in negSentences: negWords = re.findall(r"[\w']+|[.,!?;]", i.rstrip()) negWords = [make_full_dict(negWords), 'neg'] negFeatures.append(negWords) # Selects 5/6 of the features to be used for training and 1/6 to be used for testing posCutoff = int(math.floor(len(posFeatures)*5/6)) negCutoff = int(math.floor(len(negFeatures)*5/6)) trainFeatures = posFeatures[:posCutoff] + negFeatures[:negCutoff] testFeatures = posFeatures[posCutoff:] + negFeatures[negCutoff:] # Train a Naive Bayes Classifier classifier = NaiveBayesClassifier.train(trainFeatures) # Create reference and test set referenceSets = collections.defaultdict(set) testSets = collections.defaultdict(set) # Puts correctly labeled sentences in referenceSets and the predictively labeled version in testSets for i, (features, label) in enumerate(testFeatures): referenceSets[label].add(i) predicted = classifier.classify(features) testSets[predicted].add(i) print "Esimated accuracy: ", nltk.classify.util.accuracy(classifier, testFeatures) print "Talk data loaded from file" print "Performing sentiment analysis..." df = pd.read_csv(input_filename, skipinitialspace=True, sep='\t') g = df.groupby('focus_id') flist = g['body'].apply(list) focus_list = [] for k,v in flist.iteritems(): if (isinstance(v, list)): if (len(v)>min_comments): string = ' '.join([str(i) for i in v]) # print string ob = (classifier.classify(extract_features(string.split())), classifier.prob_classify(extract_features(string.split())).prob('pos'), classifier.prob_classify(extract_features(string.split())).prob('neg'), k, len(v), extract_features(string.split())) focus_list.insert(0, ob) # Create lists sorted_list = sorted(focus_list, key=lambda x: (-x[1], x[4])) sorted_list_rev = list(sorted_list) sorted_list_rev.reverse() # Filter lists pos_list = filter(lambda x: x[0] == 'pos', sorted_list_rev) neg_list = filter(lambda x: x[0] == 'neg', sorted_list) n = int(len(sorted_list)*1.00) print "%i positive and %i negative items" % (len(pos_list), len(neg_list)) # Output files as CSV and HTML print "Writing CSV..." filename = os.path.join('output', project_slug, project_slug+'_'+str(min_comments)+'.csv') with open(filename, "wb") as f: writer = csv.writer(f) writer.writerows(sorted_list) print "Writing HTML files..." html = focus_list_to_html_table(pos_list) filename = os.path.join('output', project_slug, project_slug+'_'+str(min_comments)+'_positive.html') with open(filename, "w") as text_file: text_file.write(html) call(["open", filename]) html = focus_list_to_html_table(neg_list) filename = os.path.join('output', project_slug, project_slug+'_'+str(min_comments)+'_negative.html') with open(filename, "w") as text_file: text_file.write(html) call(["open", filename]) print "Done!"
mit
zorroblue/scikit-learn
sklearn/kernel_approximation.py
29
19022
""" The :mod:`sklearn.kernel_approximation` module implements several approximate kernel feature maps base on Fourier transforms. """ # Author: Andreas Mueller <[email protected]> # # License: BSD 3 clause import warnings import numpy as np import scipy.sparse as sp from scipy.linalg import svd from .base import BaseEstimator from .base import TransformerMixin from .utils import check_array, check_random_state, as_float_array from .utils.extmath import safe_sparse_dot from .utils.validation import check_is_fitted from .metrics.pairwise import pairwise_kernels, KERNEL_PARAMS class RBFSampler(BaseEstimator, TransformerMixin): """Approximates feature map of an RBF kernel by Monte Carlo approximation of its Fourier transform. It implements a variant of Random Kitchen Sinks.[1] Read more in the :ref:`User Guide <rbf_kernel_approx>`. Parameters ---------- gamma : float Parameter of RBF kernel: exp(-gamma * x^2) n_components : int Number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space. 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`. Notes ----- See "Random Features for Large-Scale Kernel Machines" by A. Rahimi and Benjamin Recht. [1] "Weighted Sums of Random Kitchen Sinks: Replacing minimization with randomization in learning" by A. Rahimi and Benjamin Recht. (http://people.eecs.berkeley.edu/~brecht/papers/08.rah.rec.nips.pdf) """ def __init__(self, gamma=1., n_components=100, random_state=None): self.gamma = gamma self.n_components = n_components self.random_state = random_state def fit(self, X, y=None): """Fit the model with X. Samples random projection according to n_features. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data, where n_samples in the number of samples and n_features is the number of features. Returns ------- self : object Returns the transformer. """ X = check_array(X, accept_sparse='csr') random_state = check_random_state(self.random_state) n_features = X.shape[1] self.random_weights_ = (np.sqrt(2 * self.gamma) * random_state.normal( size=(n_features, self.n_components))) self.random_offset_ = random_state.uniform(0, 2 * np.pi, size=self.n_components) return self def transform(self, X): """Apply the approximate feature map to X. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) New data, where n_samples in the number of samples and n_features is the number of features. Returns ------- X_new : array-like, shape (n_samples, n_components) """ check_is_fitted(self, 'random_weights_') X = check_array(X, accept_sparse='csr') projection = safe_sparse_dot(X, self.random_weights_) projection += self.random_offset_ np.cos(projection, projection) projection *= np.sqrt(2.) / np.sqrt(self.n_components) return projection class SkewedChi2Sampler(BaseEstimator, TransformerMixin): """Approximates feature map of the "skewed chi-squared" kernel by Monte Carlo approximation of its Fourier transform. Read more in the :ref:`User Guide <skewed_chi_kernel_approx>`. Parameters ---------- skewedness : float "skewedness" parameter of the kernel. Needs to be cross-validated. n_components : int number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space. 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`. References ---------- See "Random Fourier Approximations for Skewed Multiplicative Histogram Kernels" by Fuxin Li, Catalin Ionescu and Cristian Sminchisescu. See also -------- AdditiveChi2Sampler : A different approach for approximating an additive variant of the chi squared kernel. sklearn.metrics.pairwise.chi2_kernel : The exact chi squared kernel. """ def __init__(self, skewedness=1., n_components=100, random_state=None): self.skewedness = skewedness self.n_components = n_components self.random_state = random_state def fit(self, X, y=None): """Fit the model with X. Samples random projection according to n_features. Parameters ---------- X : array-like, shape (n_samples, n_features) Training data, where n_samples in the number of samples and n_features is the number of features. Returns ------- self : object Returns the transformer. """ X = check_array(X) random_state = check_random_state(self.random_state) n_features = X.shape[1] uniform = random_state.uniform(size=(n_features, self.n_components)) # transform by inverse CDF of sech self.random_weights_ = (1. / np.pi * np.log(np.tan(np.pi / 2. * uniform))) self.random_offset_ = random_state.uniform(0, 2 * np.pi, size=self.n_components) return self def transform(self, X): """Apply the approximate feature map to X. Parameters ---------- X : array-like, shape (n_samples, n_features) New data, where n_samples in the number of samples and n_features is the number of features. All values of X must be strictly greater than "-skewedness". Returns ------- X_new : array-like, shape (n_samples, n_components) """ check_is_fitted(self, 'random_weights_') X = as_float_array(X, copy=True) X = check_array(X, copy=False) if (X <= -self.skewedness).any(): raise ValueError("X may not contain entries smaller than" " -skewedness.") X += self.skewedness np.log(X, X) projection = safe_sparse_dot(X, self.random_weights_) projection += self.random_offset_ np.cos(projection, projection) projection *= np.sqrt(2.) / np.sqrt(self.n_components) return projection class AdditiveChi2Sampler(BaseEstimator, TransformerMixin): """Approximate feature map for additive chi2 kernel. Uses sampling the fourier transform of the kernel characteristic at regular intervals. Since the kernel that is to be approximated is additive, the components of the input vectors can be treated separately. Each entry in the original space is transformed into 2*sample_steps+1 features, where sample_steps is a parameter of the method. Typical values of sample_steps include 1, 2 and 3. Optimal choices for the sampling interval for certain data ranges can be computed (see the reference). The default values should be reasonable. Read more in the :ref:`User Guide <additive_chi_kernel_approx>`. Parameters ---------- sample_steps : int, optional Gives the number of (complex) sampling points. sample_interval : float, optional Sampling interval. Must be specified when sample_steps not in {1,2,3}. Notes ----- This estimator approximates a slightly different version of the additive chi squared kernel then ``metric.additive_chi2`` computes. See also -------- SkewedChi2Sampler : A Fourier-approximation to a non-additive variant of the chi squared kernel. sklearn.metrics.pairwise.chi2_kernel : The exact chi squared kernel. sklearn.metrics.pairwise.additive_chi2_kernel : The exact additive chi squared kernel. References ---------- See `"Efficient additive kernels via explicit feature maps" <http://www.robots.ox.ac.uk/~vedaldi/assets/pubs/vedaldi11efficient.pdf>`_ A. Vedaldi and A. Zisserman, Pattern Analysis and Machine Intelligence, 2011 """ def __init__(self, sample_steps=2, sample_interval=None): self.sample_steps = sample_steps self.sample_interval = sample_interval def fit(self, X, y=None): """Set parameters.""" X = check_array(X, accept_sparse='csr') if self.sample_interval is None: # See reference, figure 2 c) if self.sample_steps == 1: self.sample_interval_ = 0.8 elif self.sample_steps == 2: self.sample_interval_ = 0.5 elif self.sample_steps == 3: self.sample_interval_ = 0.4 else: raise ValueError("If sample_steps is not in [1, 2, 3]," " you need to provide sample_interval") else: self.sample_interval_ = self.sample_interval return self def transform(self, X): """Apply approximate feature map to X. Parameters ---------- X : {array-like, sparse matrix}, shape = (n_samples, n_features) Returns ------- X_new : {array, sparse matrix}, \ shape = (n_samples, n_features * (2*sample_steps + 1)) Whether the return value is an array of sparse matrix depends on the type of the input X. """ msg = ("%(name)s is not fitted. Call fit to set the parameters before" " calling transform") check_is_fitted(self, "sample_interval_", msg=msg) X = check_array(X, accept_sparse='csr') sparse = sp.issparse(X) # check if X has negative values. Doesn't play well with np.log. if ((X.data if sparse else X) < 0).any(): raise ValueError("Entries of X must be non-negative.") # zeroth component # 1/cosh = sech # cosh(0) = 1.0 transf = self._transform_sparse if sparse else self._transform_dense return transf(X) def _transform_dense(self, X): non_zero = (X != 0.0) X_nz = X[non_zero] X_step = np.zeros_like(X) X_step[non_zero] = np.sqrt(X_nz * self.sample_interval_) X_new = [X_step] log_step_nz = self.sample_interval_ * np.log(X_nz) step_nz = 2 * X_nz * self.sample_interval_ for j in range(1, self.sample_steps): factor_nz = np.sqrt(step_nz / np.cosh(np.pi * j * self.sample_interval_)) X_step = np.zeros_like(X) X_step[non_zero] = factor_nz * np.cos(j * log_step_nz) X_new.append(X_step) X_step = np.zeros_like(X) X_step[non_zero] = factor_nz * np.sin(j * log_step_nz) X_new.append(X_step) return np.hstack(X_new) def _transform_sparse(self, X): indices = X.indices.copy() indptr = X.indptr.copy() data_step = np.sqrt(X.data * self.sample_interval_) X_step = sp.csr_matrix((data_step, indices, indptr), shape=X.shape, dtype=X.dtype, copy=False) X_new = [X_step] log_step_nz = self.sample_interval_ * np.log(X.data) step_nz = 2 * X.data * self.sample_interval_ for j in range(1, self.sample_steps): factor_nz = np.sqrt(step_nz / np.cosh(np.pi * j * self.sample_interval_)) data_step = factor_nz * np.cos(j * log_step_nz) X_step = sp.csr_matrix((data_step, indices, indptr), shape=X.shape, dtype=X.dtype, copy=False) X_new.append(X_step) data_step = factor_nz * np.sin(j * log_step_nz) X_step = sp.csr_matrix((data_step, indices, indptr), shape=X.shape, dtype=X.dtype, copy=False) X_new.append(X_step) return sp.hstack(X_new) class Nystroem(BaseEstimator, TransformerMixin): """Approximate a kernel map using a subset of the training data. Constructs an approximate feature map for an arbitrary kernel using a subset of the data as basis. Read more in the :ref:`User Guide <nystroem_kernel_approx>`. Parameters ---------- kernel : string or callable, default="rbf" Kernel map to be approximated. A callable should accept two arguments and the keyword arguments passed to this object as kernel_params, and should return a floating point number. n_components : int Number of features to construct. How many data points will be used to construct the mapping. gamma : float, default=None Gamma parameter for the RBF, laplacian, 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=None Degree of the polynomial kernel. Ignored by other kernels. coef0 : float, default=None 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. 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`. Attributes ---------- components_ : array, shape (n_components, n_features) Subset of training points used to construct the feature map. component_indices_ : array, shape (n_components) Indices of ``components_`` in the training set. normalization_ : array, shape (n_components, n_components) Normalization matrix needed for embedding. Square root of the kernel matrix on ``components_``. References ---------- * Williams, C.K.I. and Seeger, M. "Using the Nystroem method to speed up kernel machines", Advances in neural information processing systems 2001 * T. Yang, Y. Li, M. Mahdavi, R. Jin and Z. Zhou "Nystroem Method vs Random Fourier Features: A Theoretical and Empirical Comparison", Advances in Neural Information Processing Systems 2012 See also -------- RBFSampler : An approximation to the RBF kernel using random Fourier features. sklearn.metrics.pairwise.kernel_metrics : List of built-in kernels. """ def __init__(self, kernel="rbf", gamma=None, coef0=None, degree=None, kernel_params=None, n_components=100, random_state=None): self.kernel = kernel self.gamma = gamma self.coef0 = coef0 self.degree = degree self.kernel_params = kernel_params self.n_components = n_components self.random_state = random_state def fit(self, X, y=None): """Fit estimator to data. Samples a subset of training points, computes kernel on these and computes normalization matrix. Parameters ---------- X : array-like, shape=(n_samples, n_feature) Training data. """ X = check_array(X, accept_sparse='csr') rnd = check_random_state(self.random_state) n_samples = X.shape[0] # get basis vectors if self.n_components > n_samples: # XXX should we just bail? n_components = n_samples warnings.warn("n_components > n_samples. This is not possible.\n" "n_components was set to n_samples, which results" " in inefficient evaluation of the full kernel.") else: n_components = self.n_components n_components = min(n_samples, n_components) inds = rnd.permutation(n_samples) basis_inds = inds[:n_components] basis = X[basis_inds] basis_kernel = pairwise_kernels(basis, metric=self.kernel, filter_params=True, **self._get_kernel_params()) # sqrt of kernel matrix on basis vectors U, S, V = svd(basis_kernel) S = np.maximum(S, 1e-12) self.normalization_ = np.dot(U / np.sqrt(S), V) self.components_ = basis self.component_indices_ = inds return self def transform(self, X): """Apply feature map to X. Computes an approximate feature map using the kernel between some training points and X. Parameters ---------- X : array-like, shape=(n_samples, n_features) Data to transform. Returns ------- X_transformed : array, shape=(n_samples, n_components) Transformed data. """ check_is_fitted(self, 'components_') X = check_array(X, accept_sparse='csr') kernel_params = self._get_kernel_params() embedded = pairwise_kernels(X, self.components_, metric=self.kernel, filter_params=True, **kernel_params) return np.dot(embedded, self.normalization_.T) def _get_kernel_params(self): params = self.kernel_params if params is None: params = {} if not callable(self.kernel): for param in (KERNEL_PARAMS[self.kernel]): if getattr(self, param) is not None: params[param] = getattr(self, param) else: if (self.gamma is not None or self.coef0 is not None or self.degree is not None): warnings.warn( "Passing gamma, coef0 or degree to Nystroem when using a" " callable kernel is deprecated in version 0.19 and will" " raise an error in 0.21, as they are ignored. Use " "kernel_params instead.", DeprecationWarning) return params
bsd-3-clause
NelisVerhoef/scikit-learn
examples/linear_model/plot_bayesian_ridge.py
248
2588
""" ========================= Bayesian Ridge Regression ========================= Computes a Bayesian Ridge Regression on a synthetic dataset. See :ref:`bayesian_ridge_regression` for more information on the regressor. Compared to the OLS (ordinary least squares) estimator, the coefficient weights are slightly shifted toward zeros, which stabilises them. As the prior on the weights is a Gaussian prior, the histogram of the estimated weights is Gaussian. The estimation of the model is done by iteratively maximizing the marginal log-likelihood of the observations. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn.linear_model import BayesianRidge, LinearRegression ############################################################################### # Generating simulated data with Gaussian weigthts np.random.seed(0) n_samples, n_features = 100, 100 X = np.random.randn(n_samples, n_features) # Create Gaussian data # Create weigts with a precision lambda_ of 4. lambda_ = 4. w = np.zeros(n_features) # Only keep 10 weights of interest relevant_features = np.random.randint(0, n_features, 10) for i in relevant_features: w[i] = stats.norm.rvs(loc=0, scale=1. / np.sqrt(lambda_)) # Create noise with a precision alpha of 50. alpha_ = 50. noise = stats.norm.rvs(loc=0, scale=1. / np.sqrt(alpha_), size=n_samples) # Create the target y = np.dot(X, w) + noise ############################################################################### # Fit the Bayesian Ridge Regression and an OLS for comparison clf = BayesianRidge(compute_score=True) clf.fit(X, y) ols = LinearRegression() ols.fit(X, y) ############################################################################### # Plot true weights, estimated weights and histogram of the weights plt.figure(figsize=(6, 5)) plt.title("Weights of the model") plt.plot(clf.coef_, 'b-', label="Bayesian Ridge estimate") plt.plot(w, 'g-', label="Ground truth") plt.plot(ols.coef_, 'r--', label="OLS estimate") plt.xlabel("Features") plt.ylabel("Values of the weights") plt.legend(loc="best", prop=dict(size=12)) plt.figure(figsize=(6, 5)) plt.title("Histogram of the weights") plt.hist(clf.coef_, bins=n_features, log=True) plt.plot(clf.coef_[relevant_features], 5 * np.ones(len(relevant_features)), 'ro', label="Relevant features") plt.ylabel("Features") plt.xlabel("Values of the weights") plt.legend(loc="lower left") plt.figure(figsize=(6, 5)) plt.title("Marginal log-likelihood") plt.plot(clf.scores_) plt.ylabel("Score") plt.xlabel("Iterations") plt.show()
bsd-3-clause
QuLogic/burnman
misc/paper_incorrect_averaging.py
1
8987
# BurnMan - a lower mantle toolkit # Copyright (C) 2012, 2013, Heister, T., Unterborn, C., Rose, I. and Cottaar, S. # Released under GPL v2 or later. """ paper_incorrect_averaging ------------------------- This script reproduces :cite:`Cottaar2014`, Figure 5. Attempt to reproduce Figure 6.12 from :cite:`Murakami2013` """ import os, sys, numpy as np, matplotlib.pyplot as plt #hack to allow scripts to be placed in subdirectories next to burnman: if not os.path.exists('burnman') and os.path.exists('../burnman'): sys.path.insert(1,os.path.abspath('..')) import burnman from burnman import minerals from burnman.mineral_helpers import HelperSolidSolution import matplotlib.image as mpimg import colors if __name__ == "__main__": plt.figure(dpi=100,figsize=(12,6)) prop={'size':12} plt.rc('text', usetex=True) plt.rcParams['text.latex.preamble'] = '\usepackage{relsize}' dashstyle2=(7,3) dashstyle3=(3,2) method = 'slb2' #define the minerals from table 6.3 mg_perovskite = burnman.Mineral() mg_perovskite.params = {'name': 'Mg perovskite', 'molar_mass' : 0.1004, 'V_0': 24.43e-6, 'K_0': 253.0e9, 'Kprime_0': 3.9, 'G_0' : 172.9e9, 'Gprime_0' : 1.56, 'n': 5.0, 'Debye_0': 1100., 'grueneisen_0': 1.40, 'q_0': 1.40, 'eta_s_0' : 2.6} mg_perovskite.set_method('slb2') fe_perovskite = burnman.Mineral() fe_perovskite.params = {'name': 'Fe perovskite', 'molar_mass' : 0.1319, 'V_0': 25.49e-6, 'K_0': 281.0e9, 'Kprime_0': 4.1, 'G_0' : 138.0e9, 'Gprime_0' : 1.70, 'n': 5.0, 'Debye_0': 841., 'grueneisen_0': 1.48, 'q_0': 1.40, 'eta_s_0' : 2.1} fe_perovskite.set_method(method) periclase = burnman.Mineral() periclase.params = {'name': 'periclase', 'molar_mass' : 0.0403, 'V_0': 11.24e-6, 'K_0': 161.0e9, 'Kprime_0': 3.9, 'G_0' : 130.9e9, 'Gprime_0' : 1.92, 'n': 2.0, 'Debye_0': 773., 'grueneisen_0': 1.50, 'q_0': 1.50, 'eta_s_0' : 2.3} periclase.set_method(method) wustite = burnman.Mineral() wustite.params = {'name': 'wustite', 'molar_mass' : 0.07184, 'V_0': 12.06e-6, 'K_0': 152.0e9, 'Kprime_0': 4.9, 'G_0' : 47.0e9, 'Gprime_0' : 0.70, 'n': 2.0, 'Debye_0': 455., 'grueneisen_0': 1.28, 'q_0': 1.50, 'eta_s_0' : 0.8} wustite.set_method(method) #in the text for the book chapter a linear relationship in elastic properties #for the solid solutions is assumed... class ferropericlase(HelperSolidSolution): def __init__(self, fe_num): endmembers = [periclase, wustite] molar_fractions = [1. - fe_num, 0.0 + fe_num] HelperSolidSolution.__init__(self, endmembers, molar_fractions) class perovskite(HelperSolidSolution): def __init__(self, fe_num): endmembers = [mg_perovskite, fe_perovskite] molar_fractions = [1. - fe_num, 0.0 + fe_num] HelperSolidSolution.__init__(self, endmembers, molar_fractions) #define the P-T path pressure = np.linspace(28.0e9, 129e9, 25.) temperature_bs = burnman.geotherm.brown_shankland(pressure) temperature_an = burnman.geotherm.anderson(pressure) #seismic model for comparison: seismic_model = burnman.seismic.PREM() # pick from .prem() .slow() .fast() (see burnman/seismic.py) depths = map(seismic_model.depth, pressure) seis_p, seis_rho, seis_vp, seis_vs, seis_vphi = seismic_model.evaluate_all_at(depths) #pure perovskite perovskitite = burnman.Composite( ( (perovskite(0.06), 1.0),) ) perovskitite.set_method(method) #pure periclase periclasite = burnman.Composite( ( (ferropericlase(0.21), 1.0),)) periclasite.set_method(method) #pyrolite (80% perovskite) pyrolite = burnman.Composite( ( (perovskite(0.06), 0.834), (ferropericlase(0.21), 0.166) ) ) pyrolite.set_method(method) #preferred mixture? amount_perovskite = 0.92 preferred_mixture = burnman.Composite( ( (perovskite(0.06), amount_perovskite), (ferropericlase(0.21), 1.0-amount_perovskite) ) ) preferred_mixture.set_method(method) mat_rho_1, mat_vp_1, mat_vs_1, mat_vphi_1, mat_K_1, mat_G_1 = burnman.velocities_from_rock(perovskitite,seis_p, temperature_bs) mat_rho_2, mat_vp_2, mat_vs_2, mat_vphi_2, mat_K_2, mat_G_2 = burnman.velocities_from_rock(periclasite,seis_p, temperature_bs) mat_rho_3, mat_vp_3, mat_vs_3, mat_vphi_3, mat_K_3, mat_G_3 = burnman.velocities_from_rock(pyrolite,seis_p, temperature_bs) mat_rho_4, mat_vp_4, mat_vs_4, mat_vphi_4, mat_K_4, mat_G_4 = burnman.velocities_from_rock(preferred_mixture,seis_p, temperature_bs) ### HERE IS THE STEP WITH THE INCORRECT MIXING ### # comment this out to have correct phase averaging, leave it in to have incorrect phase averaging mat_vs_3_wr = 0.5*((0.834*mat_vs_1 + 0.166*mat_vs_2) + np.ones_like(mat_vs_1)/(0.834/mat_vs_1 + 0.166/mat_vs_2)) mat_vs_4_wr = 0.5*((0.92*mat_vs_1 + 0.08*mat_vs_2) + np.ones_like(mat_vs_1)/(0.92/mat_vs_1 + 0.08/mat_vs_2)) plt.subplot(1,2,2) plt.ylim(5.2,7.4) plt.xlim(25,135) #fig1 = mpimg.imread('input_figures/murakami_book_chapter.png') #plt.imshow(fig1, extent=[25,135,5.0,7.6], aspect='auto') plt.plot(seis_p/1.e9,seis_vs/1.e3,color='k',linestyle='-',marker='None',markerfacecolor='w',markersize=4,label='PREM',linewidth=3.0,mew=1.5) plt.plot(seis_p/1.e9,mat_vs_1/1.e3,color=colors.color(3),marker='v',markerfacecolor=colors.color(3), \ markersize=4, markeredgecolor=colors.color(3),markevery=2,linewidth=1.5,label='perovskite') plt.plot(seis_p/1.e9,mat_vs_2/1.e3,color=colors.color(1),linestyle='-', \ linewidth=1.5,marker='^',markerfacecolor=colors.color(1), markersize=4, \ markeredgecolor=colors.color(1),markevery=2,label='periclase') plt.plot(seis_p/1.e9,mat_vs_4_wr/1.e3,color=colors.color(4),dashes=dashstyle3, \ linewidth=1.5,marker='o',markerfacecolor=colors.color(4), markersize=4, \ markeredgecolor=colors.color(4),markevery=2,label='92\% pv') plt.plot(seis_p/1.e9,mat_vs_3_wr/1.e3,color='g',linestyle='-',dashes=dashstyle2, \ linewidth=1.5,marker='o',markerfacecolor='w', markersize=4, markeredgecolor='g',markevery=2,label='83\% pv') plt.legend(loc='lower right',prop={'size':12}) plt.title("Phase average on velocities") plt.xlabel("Pressure (GPa)") plt.subplot(1,2,1) plt.ylim(5.2,7.4) plt.xlim(25,135) #fig1 = mpimg.imread('input_figures/murakami_book_chapter.png') #plt.imshow(fig1, extent=[25,135,5.0,7.6], aspect='auto') plt.plot(seis_p/1.e9,seis_vs/1.e3,color='k',linestyle='-',marker='None',markerfacecolor='w',markersize=4,label='PREM',linewidth=3.0,mew=1.5) plt.plot(seis_p/1.e9,mat_vs_1/1.e3,color=colors.color(3),marker='v',markerfacecolor=colors.color(3), \ markersize=4, markeredgecolor=colors.color(3),markevery=2,linewidth=1.5,label='perovskite') plt.plot(seis_p/1.e9,mat_vs_2/1.e3,color=colors.color(1),linestyle='-', \ linewidth=1.5,marker='^',markerfacecolor=colors.color(1), markersize=4, \ markeredgecolor=colors.color(1),markevery=2,label='periclase') plt.plot(seis_p/1.e9,mat_vs_4/1.e3,color=colors.color(4),dashes=dashstyle3, \ linewidth=1.5,marker='o',markerfacecolor=colors.color(4), markersize=4, \ markeredgecolor=colors.color(4),markevery=2,label='92\% pv') plt.plot(seis_p/1.e9,mat_vs_3/1.e3,color='g',linestyle='-',dashes=dashstyle2, \ linewidth=1.5,marker='o',markerfacecolor='w', markersize=4, markeredgecolor='g',markevery=2, label='83\% pv') plt.title(" V.-R.-H. on moduli") plt.xlabel("Pressure (GPa)") plt.ylabel("Shear Velocity Vs (km/s)") plt.tight_layout() plt.savefig("example_incorrect_averaging.pdf",bbox_inches='tight') plt.show()
gpl-2.0
DailyActie/Surrogate-Model
01-codes/scikit-learn-master/sklearn/linear_model/tests/test_ransac.py
1
17468
import numpy as np from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_equal, assert_raises from scipy import sparse from sklearn.linear_model import LinearRegression, RANSACRegressor, Lasso from sklearn.linear_model.ransac import _dynamic_max_trials from sklearn.utils import check_random_state from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_less from sklearn.utils.testing import assert_raises_regexp from sklearn.utils.testing import assert_warns # Generate coordinates of line X = np.arange(-200, 200) y = 0.2 * X + 20 data = np.column_stack([X, y]) # Add some faulty data outliers = np.array((10, 30, 200)) data[outliers[0], :] = (1000, 1000) data[outliers[1], :] = (-1000, -1000) data[outliers[2], :] = (-100, -50) X = data[:, 0][:, np.newaxis] y = data[:, 1] def test_ransac_inliers_outliers(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) # Estimate parameters of corrupted data ransac_estimator.fit(X, y) # Ground truth / reference inlier mask ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) def test_ransac_is_data_valid(): def is_data_valid(X, y): assert_equal(X.shape[0], 2) assert_equal(y.shape[0], 2) return False X = np.random.rand(10, 2) y = np.random.rand(10, 1) base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, is_data_valid=is_data_valid, random_state=0) assert_raises(ValueError, ransac_estimator.fit, X, y) def test_ransac_is_model_valid(): def is_model_valid(estimator, X, y): assert_equal(X.shape[0], 2) assert_equal(y.shape[0], 2) return False base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, is_model_valid=is_model_valid, random_state=0) assert_raises(ValueError, ransac_estimator.fit, X, y) def test_ransac_max_trials(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, max_trials=0, random_state=0) assert_raises(ValueError, ransac_estimator.fit, X, y) ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, max_trials=11, random_state=0) assert getattr(ransac_estimator, 'n_trials_', None) is None ransac_estimator.fit(X, y) assert_equal(ransac_estimator.n_trials_, 2) def test_ransac_stop_n_inliers(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, stop_n_inliers=2, random_state=0) ransac_estimator.fit(X, y) assert_equal(ransac_estimator.n_trials_, 1) def test_ransac_stop_score(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, stop_score=0, random_state=0) ransac_estimator.fit(X, y) assert_equal(ransac_estimator.n_trials_, 1) def test_ransac_score(): X = np.arange(100)[:, None] y = np.zeros((100,)) y[0] = 1 y[1] = 100 base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=0.5, random_state=0) ransac_estimator.fit(X, y) assert_equal(ransac_estimator.score(X[2:], y[2:]), 1) assert_less(ransac_estimator.score(X[:2], y[:2]), 1) def test_ransac_predict(): X = np.arange(100)[:, None] y = np.zeros((100,)) y[0] = 1 y[1] = 100 base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=0.5, random_state=0) ransac_estimator.fit(X, y) assert_equal(ransac_estimator.predict(X), np.zeros(100)) def test_ransac_resid_thresh_no_inliers(): # When residual_threshold=0.0 there are no inliers and a # ValueError with a message should be raised base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=0.0, random_state=0) assert_raises_regexp(ValueError, "No inliers.*residual_threshold.*0\.0", ransac_estimator.fit, X, y) def test_ransac_sparse_coo(): X_sparse = sparse.coo_matrix(X) base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_estimator.fit(X_sparse, y) ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) def test_ransac_sparse_csr(): X_sparse = sparse.csr_matrix(X) base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_estimator.fit(X_sparse, y) ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) def test_ransac_sparse_csc(): X_sparse = sparse.csc_matrix(X) base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_estimator.fit(X_sparse, y) ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) def test_ransac_none_estimator(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_none_estimator = RANSACRegressor(None, 2, 5, random_state=0) ransac_estimator.fit(X, y) ransac_none_estimator.fit(X, y) assert_array_almost_equal(ransac_estimator.predict(X), ransac_none_estimator.predict(X)) def test_ransac_min_n_samples(): base_estimator = LinearRegression() ransac_estimator1 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_estimator2 = RANSACRegressor(base_estimator, min_samples=2. / X.shape[0], residual_threshold=5, random_state=0) ransac_estimator3 = RANSACRegressor(base_estimator, min_samples=-1, residual_threshold=5, random_state=0) ransac_estimator4 = RANSACRegressor(base_estimator, min_samples=5.2, residual_threshold=5, random_state=0) ransac_estimator5 = RANSACRegressor(base_estimator, min_samples=2.0, residual_threshold=5, random_state=0) ransac_estimator6 = RANSACRegressor(base_estimator, residual_threshold=5, random_state=0) ransac_estimator7 = RANSACRegressor(base_estimator, min_samples=X.shape[0] + 1, residual_threshold=5, random_state=0) ransac_estimator1.fit(X, y) ransac_estimator2.fit(X, y) ransac_estimator5.fit(X, y) ransac_estimator6.fit(X, y) assert_array_almost_equal(ransac_estimator1.predict(X), ransac_estimator2.predict(X)) assert_array_almost_equal(ransac_estimator1.predict(X), ransac_estimator5.predict(X)) assert_array_almost_equal(ransac_estimator1.predict(X), ransac_estimator6.predict(X)) assert_raises(ValueError, ransac_estimator3.fit, X, y) assert_raises(ValueError, ransac_estimator4.fit, X, y) assert_raises(ValueError, ransac_estimator7.fit, X, y) def test_ransac_multi_dimensional_targets(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) # 3-D target values yyy = np.column_stack([y, y, y]) # Estimate parameters of corrupted data ransac_estimator.fit(X, yyy) # Ground truth / reference inlier mask ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) # XXX: Remove in 0.20 def test_ransac_residual_metric(): residual_metric1 = lambda dy: np.sum(np.abs(dy), axis=1) residual_metric2 = lambda dy: np.sum(dy ** 2, axis=1) yyy = np.column_stack([y, y, y]) base_estimator = LinearRegression() ransac_estimator0 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_estimator1 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0, residual_metric=residual_metric1) ransac_estimator2 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0, residual_metric=residual_metric2) # multi-dimensional ransac_estimator0.fit(X, yyy) assert_warns(DeprecationWarning, ransac_estimator1.fit, X, yyy) assert_warns(DeprecationWarning, ransac_estimator2.fit, X, yyy) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator1.predict(X)) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator2.predict(X)) # one-dimensional ransac_estimator0.fit(X, y) assert_warns(DeprecationWarning, ransac_estimator2.fit, X, y) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator2.predict(X)) def test_ransac_residual_loss(): loss_multi1 = lambda y_true, y_pred: np.sum(np.abs(y_true - y_pred), axis=1) loss_multi2 = lambda y_true, y_pred: np.sum((y_true - y_pred) ** 2, axis=1) loss_mono = lambda y_true, y_pred: np.abs(y_true - y_pred) yyy = np.column_stack([y, y, y]) base_estimator = LinearRegression() ransac_estimator0 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0) ransac_estimator1 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0, loss=loss_multi1) ransac_estimator2 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0, loss=loss_multi2) # multi-dimensional ransac_estimator0.fit(X, yyy) ransac_estimator1.fit(X, yyy) ransac_estimator2.fit(X, yyy) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator1.predict(X)) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator2.predict(X)) # one-dimensional ransac_estimator0.fit(X, y) ransac_estimator2.loss = loss_mono ransac_estimator2.fit(X, y) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator2.predict(X)) ransac_estimator3 = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=5, random_state=0, loss="squared_loss") ransac_estimator3.fit(X, y) assert_array_almost_equal(ransac_estimator0.predict(X), ransac_estimator2.predict(X)) def test_ransac_default_residual_threshold(): base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, random_state=0) # Estimate parameters of corrupted data ransac_estimator.fit(X, y) # Ground truth / reference inlier mask ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) def test_ransac_dynamic_max_trials(): # Numbers hand-calculated and confirmed on page 119 (Table 4.3) in # Hartley, R.~I. and Zisserman, A., 2004, # Multiple View Geometry in Computer Vision, Second Edition, # Cambridge University Press, ISBN: 0521540518 # e = 0%, min_samples = X assert_equal(_dynamic_max_trials(100, 100, 2, 0.99), 1) # e = 5%, min_samples = 2 assert_equal(_dynamic_max_trials(95, 100, 2, 0.99), 2) # e = 10%, min_samples = 2 assert_equal(_dynamic_max_trials(90, 100, 2, 0.99), 3) # e = 30%, min_samples = 2 assert_equal(_dynamic_max_trials(70, 100, 2, 0.99), 7) # e = 50%, min_samples = 2 assert_equal(_dynamic_max_trials(50, 100, 2, 0.99), 17) # e = 5%, min_samples = 8 assert_equal(_dynamic_max_trials(95, 100, 8, 0.99), 5) # e = 10%, min_samples = 8 assert_equal(_dynamic_max_trials(90, 100, 8, 0.99), 9) # e = 30%, min_samples = 8 assert_equal(_dynamic_max_trials(70, 100, 8, 0.99), 78) # e = 50%, min_samples = 8 assert_equal(_dynamic_max_trials(50, 100, 8, 0.99), 1177) # e = 0%, min_samples = 10 assert_equal(_dynamic_max_trials(1, 100, 10, 0), 0) assert_equal(_dynamic_max_trials(1, 100, 10, 1), float('inf')) base_estimator = LinearRegression() ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, stop_probability=-0.1) assert_raises(ValueError, ransac_estimator.fit, X, y) ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, stop_probability=1.1) assert_raises(ValueError, ransac_estimator.fit, X, y) def test_ransac_fit_sample_weight(): ransac_estimator = RANSACRegressor(random_state=0) n_samples = y.shape[0] weights = np.ones(n_samples) ransac_estimator.fit(X, y, weights) # sanity check assert_equal(ransac_estimator.inlier_mask_.shape[0], n_samples) ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_ ).astype(np.bool_) ref_inlier_mask[outliers] = False # check that mask is correct assert_array_equal(ransac_estimator.inlier_mask_, ref_inlier_mask) # check that fit(X) = fit([X1, X2, X3],sample_weight = [n1, n2, n3]) where # X = X1 repeated n1 times, X2 repeated n2 times and so forth random_state = check_random_state(0) X_ = random_state.randint(0, 200, [10, 1]) y_ = np.ndarray.flatten(0.2 * X_ + 2) sample_weight = random_state.randint(0, 10, 10) outlier_X = random_state.randint(0, 1000, [1, 1]) outlier_weight = random_state.randint(0, 10, 1) outlier_y = random_state.randint(-1000, 0, 1) X_flat = np.append(np.repeat(X_, sample_weight, axis=0), np.repeat(outlier_X, outlier_weight, axis=0), axis=0) y_flat = np.ndarray.flatten(np.append(np.repeat(y_, sample_weight, axis=0), np.repeat(outlier_y, outlier_weight, axis=0), axis=0)) ransac_estimator.fit(X_flat, y_flat) ref_coef_ = ransac_estimator.estimator_.coef_ sample_weight = np.append(sample_weight, outlier_weight) X_ = np.append(X_, outlier_X, axis=0) y_ = np.append(y_, outlier_y) ransac_estimator.fit(X_, y_, sample_weight) assert_almost_equal(ransac_estimator.estimator_.coef_, ref_coef_) # check that if base_estimator.fit doesn't support # sample_weight, raises error base_estimator = Lasso() ransac_estimator = RANSACRegressor(base_estimator) assert_raises(ValueError, ransac_estimator.fit, X, y, weights)
mit
jwdebelius/break_4w
break4w/data_dictionary.py
1
22592
""" I need really good documentation for this module that I haven't begun to write yet. This will be really important? """ from collections import OrderedDict import datetime import pydoc import numpy as np import pandas as pd from pandas.api.types import CategoricalDtype from break4w.question import Question from break4w.categorical import Categorical from break4w.bool import Bool from break4w.continous import Continous import break4w._defaults as b4wdefaults type_lookup = {'continous': Continous, 'categorical': Categorical, 'multiple choice': Categorical, 'ordinal': Categorical, 'bool': Bool, 'boolean': Bool, 'yes/no': Bool, } class DataDictionary(OrderedDict): """ Generates a data dictionary object Parameters ---------- columns: list of dicts A list of dictionaries representing each column in the metadata. The dictionaries must contain a `name` key, describing the column name. The values in the dictionary should the variables needed for each type of question object in a data dictionary. types: list of strings A description of the type of question being asked. These come from a relatively controlled vocabulary and include types such as `"continous", "categorical", "bool"`. If the question type does not conform to the controlled vocabulary, the column will be read as a Question object with limited functionality. description: str A description of the data dictionary or study of no more than 80 characters. """ default_cols = ['name', 'description', 'type', 'dtype', 'order', 'units', 'ambigious', 'missing', 'notes'] def __init__(self, columns, types, description=None): """Initializes the dictionary object This is a very basic prototype of the data dictionary object """ self.log = [] if description is None: self.description = '' elif len(description) > 80: raise ValueError('The dictionary description cannot be more than ' '80 characters') else: self.description = description # Adds the question objects to the dictionary for col_, type_ in zip(*(columns, types)): self.add_question(question_data=col_, question_type=type_, record=False, check=False) self.columns = list(self.keys()) def __str__(self): """ Generates printed summary """ summary = ['Data Dictionary with %i columns' % len(self)] if len(self.description) > 0: summary.append('\t%s' % self.description) summary.append('-----------------------------------------------------' '-------------------------------') for col in self.values(): summary.append('%s (%s)\n\t%s' % (col.name, col.type, col.description)) summary.append('-----------------------------------------------------' '-------------------------------') return '\n'.join(summary) def _update_log(self, command, column=None, transform_type=None, transformation=None): """Used for internal tracking of the columns and data Every time a Question acts on data, a record should be made of the transformation. (See break4w.question.Question._update_log). However, this also tracks the transformation on the dictionary level. Parameters ---------- command : str A short textual description of the command performed. This may be the function name in text format. column : str, optional The column in the metadata being explored. transform_type: str, optional A more general description of the type of action that was performed. Ideally, this comes for a preset list of possible actions, and the descriptions are consistent. transformation: str, optional Explains exactly how values were changed. """ self.log.append({ 'timestamp': datetime.datetime.now(), 'column': column, 'command': command, 'transform_type': transform_type, 'transformation': transformation, }) def _pull_question_log(self, column=None): """Adds information from the specified column to the log.""" raise NotImplementedError def add_question(self, question_data, question_type='', check=True, record=True, var_delim=' | ', code_delim='=', null_value='None'): """ Adds a new question object to the data dictionary Parameters ---------- question_data: Dict, Question Describes the data dictionary entry for the question. This can be a break4w question object created directly, or a dictionary objecting with information like the name in the metadata representation, data type, a description, and specific information for the type of question. For instance, `question_type` specified the qustion was `"continous"`, the `question_data` must also describe units for the question. question_type: str, optional Describes the type of question object that should be selected for the question. If `question_data` is a `Question` object, then no `question_type` is needed. check: bool, optional Checks whether a name already exists in the question name space. If this is true, then the function will check if the column already exists in the dictionary. If the column does exist and check is true, an error will be raised. If check is not true, the data dictionary entry for the column will be overwritten and any information in that column will be lost. record, bool, optional Indicates where the addition should be logged. read_numeric_codes: bool, optional Whether columns should be read with a numerical delimiter (i.e "=") to parse a numeric value into a categorical one. For example, if numeric is true, then "0=female | 1=male" would be parsed that any data encoded as 0 maps to female, any data encoded as 1 maps to male, and the order of hte values is `[0, 1]` (corresponding to `['female', 'male']`). Otherwise, the line would be read literally, and the order is read as `["0=female", "1=male"]`. val_delim: str, optional The seperator between values in the "order" column. code_delim: str, optional The delimiter between a numericly coded categorical variable and the value it maps to. Raises ------ ValueError When the function is checking for the column and the column name is already in the dictionary. If this is the case, the dictionary entry should be adjusted using `update_question`, not `add_question`, since this function will otherwise over write the existing column. """ error1 = False # Converts to a Question object question_object = type_lookup.get(question_type.lower(), Question) if isinstance(question_data, pd.Series): question_data.dropna(inplace=True) question_data = question_object._read_series( question_data, var_delim=var_delim, code_delim=code_delim, null_value=null_value, ) elif isinstance(question_data, dict): question_data = question_object(**question_data) elif isinstance(question_data, Question): pass else: message = ('question_data must be a Question, dict, or' ' Series') if record: self._update_log('add column', column=None, transformation=message, transform_type='error') raise ValueError(message) name = question_data.name # Checks if the question is in the dictionary if (name in self.keys()) and check: error1 = True message = '%s already has a dictionary entry' % name transform_type = 'error' else: message = '%s was added to the dictionary' % name transform_type = None # Updates the log if record: self._update_log('add column', column=name, transformation=message, transform_type=transform_type) # Raises an error or updates the dictionary, as appropriate if error1: raise ValueError(message) else: self[name] = question_data self.columns = list(self.keys()) def get_question(self, name): """ Returns the data dictionary column Parameters ---------- name: str The name of the dictionary column to be returned Returns ------- Question The question object for the appropriate dictionary object Raises ------ ValueError When the column being asked for does not exist. """ if name not in self.keys(): message = 'There is no entry for %s' % name self._update_log(column=name, command='get question', transform_type='error', transformation=message) raise ValueError(message) self._update_log(column=name, command='get question') return self[name] def drop_question(self, name): """ Removes a dictionary entry for the specified column. Parameters ---------- name: str The name of the dictionary column to be returned """ if name in self.keys(): del self[name] self.columns = list(self.keys()) self._update_log(command='remove question', column=name) def update_question(self, update, name=None): """ Updates dictionary entry for the data Parameters ---------- update: Dict, Question Describes the data dictionary entry for the question. This can be a break4w question object created directly, or a dictionary objecting with information like the name in the metadata representation, data type, a description, and specific information for the type of question. For instance, `question_type` specified the qustion was `"continous"`, the `question_data` must also describe units for the question. name: str, optional The name of the dictionary column to be returned. If `update` is a Question object, this can be infered from the question. """ # Gets the dictionary of the new column and column name if isinstance(update, Question): update = vars(update) if name is None: name = update['name'] # Checks if the data is already in the dictionary if name not in self.keys(): message = ('%s is not a question in the current dictionary.\n' 'Have you tried adding the question?') % name self._update_log(command='update question', column=name, transform_type='error', transformation=message) raise ValueError(message) current = vars(self[name]) diff = {k: v for k, v in update.items() if (((k not in current) or (v != current[k])) and (k not in {'log'})) } change_keys = {} for k, v in diff.items(): if k in current: change_keys[k] = (current[k], v) else: change_keys[k] = ('add', v) setattr(self[name], k, v) if 'log' in update: self[name].log.extend(update['log']) self._update_log( command='update question', column=name, transform_type='update dictionary values', transformation=' | '.join(['%s : %s > %s' % (k, v[0], v[1]) for k, v in change_keys.items()])) def validate(self, map_, check_order=True): """ Checks columns appear in the mapping file in the appropriate order and conform to the standards set in the data dictionary. Parameters ---------- map_ : DataFrame A pandas object containing the metadata being analyzed. check_order: bool, optional Do the order of columns in the data dictionary and metadata have to match? """ pass_ = True failures = [] fail_message = [] self._validate_question_order(map_, check_order) for name, question in self.items(): if question.type == 'Question': continue try: question.validate(map_) except: pass_ = False failures.append( '\t%s - %s' % (name, question.log[-1]['transformation']) ) self.log.append(question.log[-1]) if pass_: self._update_log('validate', transform_type='pass', transformation='All columns passed') else: message = ('There were issues with the following columns:\n%s' % '\n'.join(failures)) message_l = (('There were issues with the following columns:\n%s' '\nPlease See the log for more details.') % '\n'.join([fail.split(' - ')[0].replace('\t', '') for fail in failures])) self._update_log('validate', transform_type='error', transformation=message_l) raise ValueError(message) def _validate_question_order(self, map_, check_order=True, record=True, verbose=False): """ Checks all the required questions are present in the mapping file and that they are in the correct order. Parameters ---------- map_ : DataFrame A pandas object containing the metadata being analyzed. check_order: bool, optional Do the order of columns in the data dictionary and metadata have to match? record: bool, optional Indicates where the addition should be logged. verbose: bool, optional Provides more detailed information about the error Raises ------ ValueError """ pass_ = True message = ('The columns in the mapping file match the columns in ' 'the data dictionary.') map_columns = list(map_.columns) dict_columns = list(self.keys()) if not set(map_columns) == set(dict_columns): pass_ = False in_map = list(set(map_columns) - set(dict_columns)) in_dict = list(set(dict_columns) - set(map_columns)) text = ('There are %i columns in the data dictionary ' 'not in the mapping file, and %i from the mapping' ' file not in the data dictionary.' % (len(in_dict), len(in_map))) if len(in_dict) > 0: not_map = ('In the dictionary but not in the map: \n\t%s\n' % '; '.join(in_dict)) else: not_map = '' if len(in_map) > 0: t_ = '\nIn the map but not in the dictionary:\n\t%s\n' not_dict = t_ % '; '.join(in_map) else: not_dict = '' if verbose: message = '%s%s%s' % (text, not_map, not_dict) # message = not_dict else: message = text elif not (map_columns == dict_columns) and check_order: pass_ = False message = ('The columns in the dictionary and map are not in' ' the same order.') if record and pass_: self._update_log(command='validate', transform_type='pass', transformation=message) elif record and not pass_: self._update_log(command='validate', transform_type='fail', transformation=message) raise ValueError(message) elif not pass_: raise ValueError(message) def to_dataframe(self, clean=False, val_delim=' | ', code_delim='='): u"""Converts data dictionary to a pandas dataframe Parameters ---------- clean: bool, optional Returns a subset of columns for the data dictionary. When True, the data dicitonary will return the following columns: * `name` -- the name of the column * `description` -- the 80 character description * `type` -- the type of question (Continous, Question, Categorical, or Boolean) * `dtype` -- the datatype for the pandas column. * `order` -- the order of data for categorical objects or range of values for continous values * `units` -- units for continous values * `ambigious` -- values for ambigious results * `missing` -- values for missing values * `notes` -- any notes passed into the data dictionary object to be retained val_delim: str, optional The seperator between values in the "order" column. code_delim: str, optional The delimiter between a numericly coded categorical variable and the value it maps to. Returns ------- DataFrame A dataframe mapping the variable name to its description, question type, datatype, and order. Example ------- """ cols = [] for col in self.values(): ser_ = col._to_series() # if isinstance(col, Continous): ser_.rename({'limits': 'order'}, inplace=True) cols.append(ser_) df_ = pd.concat(axis=1, sort=False, objs=cols).T if ('var_labels' in df_.columns): df_.loc[df_['var_labels'].notna(), 'order'] = \ df_.loc[df_['var_labels'].notna(), 'var_labels'] df_.drop(columns=['var_labels'], inplace=True) if clean: cols = [c for c in self.default_cols if c in df_] df_ = df_[cols] df_.drop(columns=df_.columns[df_.isna().all(axis=0)], inplace=True) return df_.set_index('name') def to_pandas_stata(self): """ Generates strings and dictionary compatible with writing to stata Returns ------- str A stata-compatible dataset description for `pandas.write_stata` dictionary A stata-compatible description for each variable, compatible with `pandas.write_stata`. """ variable_desc = {k: v.description for k,v in self.items()} return self.description, variable_desc def to_ddi_xml(self): pass @classmethod def read_dataframe(cls, df_, description=None, var_delim=' | ', code_delim='=', null_value='None'): """Builds the data dictionary from a dataframe Parameters ---------- df_ : DataFrame A pandas dataframe version of the data dictionary where the data is indexed by `name` description: str A description of the data dictionary or study of no more than 80 characters. read_codes: bool, optional Whether columns should be read with a numerical delimiter (i.e "=") to parse a numeric value into a categorical one. For example, if numeric is true, then "0=female | 1=male" would be parsed that any data encoded as 0 maps to female, any data encoded as 1 maps to male, and the order of hte values is `[0, 1]` (corresponding to `['female', 'male']`). Otherwise, the line would be read literally, and the order is read as `["0=female", "1=male"]`. val_delim: str, optional The seperator between values in the "order" column. code_delim: str, optional The delimiter between a numericly coded categorical variable and the value it maps to. Returns ------- DataDictionary A data dictionary object with the newly described study. Examples -------- """ types = [] cols = [] if 'name' not in df_.columns: df_.reset_index(inplace=True) for name_, var_ in df_.iterrows(): # Describes the question type type_ = var_['type'] qclass = type_lookup.get(type_.lower(), Question) var_.drop('type', inplace=True) # Updates the column and type objects types.append(type_) cols.append(qclass._read_series(var_.dropna(), var_delim=var_delim, code_delim=code_delim, null_value=null_value)) return cls(columns=cols, types=types, description=description) # @classmethod def to_usgs_xml(self): """Converts the data dictionary to a usgs xlm format""" pass @classmethod def read_stata(cls, iter_, ): pass
bsd-2-clause
kewitz/mestrado
Eletromagnetismo Computacional II/MOM.carganofio.py
1
1063
# -*- coding: utf-8 -*- """ Created on Fri Aug 1 15:09:54 2014 @author: leo """ import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d # Defs irange = lambda a: zip(range(len(a)),a) pi = np.pi # Parâmetros L = np.float64(1.0) # Comprimento do fio em m. a = np.float64(1E-3) # Raio do condutor. Delta = np.float64(.01) # Delta. V0 = np.float64(1.0) Eps = np.float64(8.854E-12) # Funções l = lambda m, n: Delta/np.abs(y[m-1]-y[n-1]) if m != n else 2*np.log(Delta/a) g = lambda m: 4*pi*Eps*V0 # Domínios e constantes y = np.arange(0,L,Delta, dtype=np.float64) ns = y.size ms = ns L = np.matrix(np.zeros((ms,ns))) G = np.matrix(np.zeros((ns))) # Processamento for i in range(ms): m = np.float64(i+1.0) G[0,i] = g(m) # Monta o vetor de tensões for j in range(ns): n = np.float64(j+1.0) L[i,j] = l(m,n) # Monta a matriz de ? rho = np.linalg.solve(L,G.T) # Obtem o vetor 5de Permeabilidades Q = rho.sum() * Delta print "Carga total no condutor %.3eC" % Q # Plots plt.plot(y,rho)
mit
astroML/astroML
astroML/plotting/regression.py
2
3574
import numpy as np import matplotlib.pyplot as plt from scipy import optimize from astroML.linear_model import TLS_logL, LinearRegression # TLS: def get_m_b(beta): b = np.dot(beta, beta) / beta[1] m = -beta[0] / beta[1] return m, b def plot_regressions(ksi, eta, x, y, sigma_x, sigma_y, add_regression_lines=False, alpha_in=1, beta_in=0.5, basis='linear'): figure = plt.figure(figsize=(8, 6)) ax = figure.add_subplot(111) ax.scatter(x, y, alpha=0.5) ax.errorbar(x, y, xerr=sigma_x, yerr=sigma_y, alpha=0.3, ls='') ax.set_xlabel('x') ax.set_ylabel('y') x0 = np.linspace(np.min(x) - 0.5, np.max(x) + 0.5, 20) # True regression line if alpha_in is not None and beta_in is not None: if basis == 'linear': y0 = alpha_in + x0 * beta_in elif basis == 'poly': y0 = alpha_in + beta_in[0] * x0 + beta_in[1] * x0 * x0 + beta_in[2] * x0 * x0 * x0 ax.plot(x0, y0, color='black', label='True regression') else: y0 = None if add_regression_lines: for label, data, *target in [['fit no errors', x, y, 1], ['fit y errors only', x, y, sigma_y], ['fit x errors only', y, x, sigma_x]]: linreg = LinearRegression() linreg.fit(data[:, None], *target) if label == 'fit x errors only' and y0 is not None: x_fit = linreg.predict(y0[:, None]) ax.plot(x_fit, y0, label=label) else: y_fit = linreg.predict(x0[:, None]) ax.plot(x0, y_fit, label=label) # TLS X = np.vstack((x, y)).T dX = np.zeros((len(x), 2, 2)) dX[:, 0, 0] = sigma_x dX[:, 1, 1] = sigma_y def min_func(beta): return -TLS_logL(beta, X, dX) beta_fit = optimize.fmin(min_func, x0=[-1, 1]) m_fit, b_fit = get_m_b(beta_fit) x_fit = np.linspace(-10, 10, 20) ax.plot(x_fit, m_fit * x_fit + b_fit, label='TLS') ax.set_xlim(np.min(x)-0.5, np.max(x)+0.5) ax.set_ylim(np.min(y)-0.5, np.max(y)+0.5) ax.legend() def plot_regression_from_trace(fitted, observed, ax=None, chains=None, multidim_ind=None): traces = [fitted.trace, ] xi, yi, sigx, sigy = observed if multidim_ind is not None: xi = xi[multidim_ind] x = np.linspace(np.min(xi)-0.5, np.max(xi)+0.5, 50) for i, trace in enumerate(traces): if 'theta' in trace.varnames and 'slope' not in trace.varnames: trace.add_values({'slope': np.tan(trace['theta'])}) if multidim_ind is not None: trace_slope = trace['slope'][:, multidim_ind] else: trace_slope = trace['slope'][:, 0] if chains is not None: for chain in range(100, len(trace) * trace.nchains, chains): y = trace['inter'][chain] + trace_slope[chain] * x ax.plot(x, y, alpha=0.03, c='red') # plot the best-fit line only H2D, bins1, bins2 = np.histogram2d(trace_slope, trace['inter'], bins=50) w = np.where(H2D == H2D.max()) # choose the maximum posterior slope and intercept slope_best = bins1[w[0][0]] intercept_best = bins2[w[1][0]] print("beta:", slope_best, "alpha:", intercept_best) y = intercept_best + slope_best * x # y_pre = fitted.predict(x[:, None]) ax.plot(x, y, ':', label='fitted') ax.legend() break
bsd-2-clause
yuanagain/seniorthesis
venv/lib/python2.7/site-packages/matplotlib/tests/test_delaunay.py
7
7137
from __future__ import (absolute_import, division, print_function, unicode_literals) from matplotlib.externals import six from matplotlib.externals.six.moves import xrange import warnings import numpy as np from matplotlib.testing.decorators import image_comparison, knownfailureif from matplotlib.cbook import MatplotlibDeprecationWarning with warnings.catch_warnings(): # the module is deprecated. The tests should be removed when the module is. warnings.simplefilter('ignore', MatplotlibDeprecationWarning) from matplotlib.delaunay.triangulate import Triangulation from matplotlib import pyplot as plt import matplotlib as mpl def constant(x, y): return np.ones(x.shape, x.dtype) constant.title = 'Constant' def xramp(x, y): return x xramp.title = 'X Ramp' def yramp(x, y): return y yramp.title = 'Y Ramp' def exponential(x, y): x = x*9 y = y*9 x1 = x+1.0 x2 = x-2.0 x4 = x-4.0 x7 = x-7.0 y1 = x+1.0 y2 = y-2.0 y3 = y-3.0 y7 = y-7.0 f = (0.75 * np.exp(-(x2*x2+y2*y2)/4.0) + 0.75 * np.exp(-x1*x1/49.0 - y1/10.0) + 0.5 * np.exp(-(x7*x7 + y3*y3)/4.0) - 0.2 * np.exp(-x4*x4 -y7*y7)) return f exponential.title = 'Exponential and Some Gaussians' def cliff(x, y): f = np.tanh(9.0*(y-x) + 1.0)/9.0 return f cliff.title = 'Cliff' def saddle(x, y): f = (1.25 + np.cos(5.4*y))/(6.0 + 6.0*(3*x-1.0)**2) return f saddle.title = 'Saddle' def gentle(x, y): f = np.exp(-5.0625*((x-0.5)**2+(y-0.5)**2))/3.0 return f gentle.title = 'Gentle Peak' def steep(x, y): f = np.exp(-20.25*((x-0.5)**2+(y-0.5)**2))/3.0 return f steep.title = 'Steep Peak' def sphere(x, y): circle = 64-81*((x-0.5)**2 + (y-0.5)**2) f = np.where(circle >= 0, np.sqrt(np.clip(circle,0,100)) - 0.5, 0.0) return f sphere.title = 'Sphere' def trig(x, y): f = 2.0*np.cos(10.0*x)*np.sin(10.0*y) + np.sin(10.0*x*y) return f trig.title = 'Cosines and Sines' def gauss(x, y): x = 5.0-10.0*x y = 5.0-10.0*y g1 = np.exp(-x*x/2) g2 = np.exp(-y*y/2) f = g1 + 0.75*g2*(1 + g1) return f gauss.title = 'Gaussian Peak and Gaussian Ridges' def cloverleaf(x, y): ex = np.exp((10.0-20.0*x)/3.0) ey = np.exp((10.0-20.0*y)/3.0) logitx = 1.0/(1.0+ex) logity = 1.0/(1.0+ey) f = (((20.0/3.0)**3 * ex*ey)**2 * (logitx*logity)**5 * (ex-2.0*logitx)*(ey-2.0*logity)) return f cloverleaf.title = 'Cloverleaf' def cosine_peak(x, y): circle = np.hypot(80*x-40.0, 90*y-45.) f = np.exp(-0.04*circle) * np.cos(0.15*circle) return f cosine_peak.title = 'Cosine Peak' allfuncs = [exponential, cliff, saddle, gentle, steep, sphere, trig, gauss, cloverleaf, cosine_peak] class LinearTester(object): name = 'Linear' def __init__(self, xrange=(0.0, 1.0), yrange=(0.0, 1.0), nrange=101, npoints=250): self.xrange = xrange self.yrange = yrange self.nrange = nrange self.npoints = npoints rng = np.random.RandomState(1234567890) self.x = rng.uniform(xrange[0], xrange[1], size=npoints) self.y = rng.uniform(yrange[0], yrange[1], size=npoints) self.tri = Triangulation(self.x, self.y) def replace_data(self, dataset): self.x = dataset.x self.y = dataset.y self.tri = Triangulation(self.x, self.y) def interpolator(self, func): z = func(self.x, self.y) return self.tri.linear_extrapolator(z, bbox=self.xrange+self.yrange) def plot(self, func, interp=True, plotter='imshow'): if interp: lpi = self.interpolator(func) z = lpi[self.yrange[0]:self.yrange[1]:complex(0,self.nrange), self.xrange[0]:self.xrange[1]:complex(0,self.nrange)] else: y, x = np.mgrid[self.yrange[0]:self.yrange[1]:complex(0,self.nrange), self.xrange[0]:self.xrange[1]:complex(0,self.nrange)] z = func(x, y) z = np.where(np.isinf(z), 0.0, z) extent = (self.xrange[0], self.xrange[1], self.yrange[0], self.yrange[1]) fig = plt.figure() plt.hot() # Some like it hot if plotter == 'imshow': plt.imshow(np.nan_to_num(z), interpolation='nearest', extent=extent, origin='lower') elif plotter == 'contour': Y, X = np.ogrid[self.yrange[0]:self.yrange[1]:complex(0,self.nrange), self.xrange[0]:self.xrange[1]:complex(0,self.nrange)] plt.contour(np.ravel(X), np.ravel(Y), z, 20) x = self.x y = self.y lc = mpl.collections.LineCollection(np.array([((x[i], y[i]), (x[j], y[j])) for i, j in self.tri.edge_db]), colors=[(0,0,0,0.2)]) ax = plt.gca() ax.add_collection(lc) if interp: title = '%s Interpolant' % self.name else: title = 'Reference' if hasattr(func, 'title'): plt.title('%s: %s' % (func.title, title)) else: plt.title(title) class NNTester(LinearTester): name = 'Natural Neighbors' def interpolator(self, func): z = func(self.x, self.y) return self.tri.nn_extrapolator(z, bbox=self.xrange+self.yrange) def make_all_2d_testfuncs(allfuncs=allfuncs): def make_test(func): filenames = [ '%s-%s' % (func.__name__, x) for x in ['ref-img', 'nn-img', 'lin-img', 'ref-con', 'nn-con', 'lin-con']] # We only generate PNGs to save disk space -- we just assume # that any backend differences are caught by other tests. @image_comparison(filenames, extensions=['png'], freetype_version=('2.4.5', '2.4.9'), remove_text=True) def reference_test(): nnt.plot(func, interp=False, plotter='imshow') nnt.plot(func, interp=True, plotter='imshow') lpt.plot(func, interp=True, plotter='imshow') nnt.plot(func, interp=False, plotter='contour') nnt.plot(func, interp=True, plotter='contour') lpt.plot(func, interp=True, plotter='contour') tester = reference_test tester.__name__ = str('test_%s' % func.__name__) return tester nnt = NNTester(npoints=1000) lpt = LinearTester(npoints=1000) for func in allfuncs: globals()['test_%s' % func.__name__] = make_test(func) make_all_2d_testfuncs() # 1d and 0d grid tests ref_interpolator = Triangulation([0,10,10,0], [0,0,10,10]).linear_interpolator([1,10,5,2.0]) def test_1d_grid(): res = ref_interpolator[3:6:2j,1:1:1j] assert np.allclose(res, [[1.6],[1.9]], rtol=0) def test_0d_grid(): res = ref_interpolator[3:3:1j,1:1:1j] assert np.allclose(res, [[1.6]], rtol=0) @image_comparison(baseline_images=['delaunay-1d-interp'], extensions=['png']) def test_1d_plots(): x_range = slice(0.25,9.75,20j) x = np.mgrid[x_range] ax = plt.gca() for y in xrange(2,10,2): plt.plot(x, ref_interpolator[x_range,y:y:1j]) ax.set_xticks([]) ax.set_yticks([])
mit
kobejean/tensorflow
tensorflow/examples/get_started/regression/test.py
41
4037
# 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 simple smoke test that runs these examples for 1 training iteration.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import pandas as pd from six.moves import StringIO import tensorflow.examples.get_started.regression.imports85 as imports85 sys.modules["imports85"] = imports85 # pylint: disable=g-bad-import-order,g-import-not-at-top import tensorflow.contrib.data as data import tensorflow.examples.get_started.regression.dnn_regression as dnn_regression import tensorflow.examples.get_started.regression.linear_regression as linear_regression import tensorflow.examples.get_started.regression.linear_regression_categorical as linear_regression_categorical import tensorflow.examples.get_started.regression.custom_regression as custom_regression from tensorflow.python.platform import googletest from tensorflow.python.platform import test # pylint: disable=g-bad-import-order,g-import-not-at-top # pylint: disable=line-too-long FOUR_LINES = "\n".join([ "1,?,alfa-romero,gas,std,two,hatchback,rwd,front,94.50,171.20,65.50,52.40,2823,ohcv,six,152,mpfi,2.68,3.47,9.00,154,5000,19,26,16500", "2,164,audi,gas,std,four,sedan,fwd,front,99.80,176.60,66.20,54.30,2337,ohc,four,109,mpfi,3.19,3.40,10.00,102,5500,24,30,13950", "2,164,audi,gas,std,four,sedan,4wd,front,99.40,176.60,66.40,54.30,2824,ohc,five,136,mpfi,3.19,3.40,8.00,115,5500,18,22,17450", "2,?,audi,gas,std,two,sedan,fwd,front,99.80,177.30,66.30,53.10,2507,ohc,five,136,mpfi,3.19,3.40,8.50,110,5500,19,25,15250",]) # pylint: enable=line-too-long def four_lines_dataframe(): text = StringIO(FOUR_LINES) return pd.read_csv(text, names=imports85.types.keys(), dtype=imports85.types, na_values="?") def four_lines_dataset(*args, **kwargs): del args, kwargs return data.Dataset.from_tensor_slices(FOUR_LINES.split("\n")) class RegressionTest(googletest.TestCase): """Test the regression examples in this directory.""" @test.mock.patch.dict(data.__dict__, {"TextLineDataset": four_lines_dataset}) @test.mock.patch.dict(imports85.__dict__, {"_get_imports85": (lambda: None)}) @test.mock.patch.dict(linear_regression.__dict__, {"STEPS": 1}) def test_linear_regression(self): linear_regression.main([""]) @test.mock.patch.dict(data.__dict__, {"TextLineDataset": four_lines_dataset}) @test.mock.patch.dict(imports85.__dict__, {"_get_imports85": (lambda: None)}) @test.mock.patch.dict(linear_regression_categorical.__dict__, {"STEPS": 1}) def test_linear_regression_categorical(self): linear_regression_categorical.main([""]) @test.mock.patch.dict(data.__dict__, {"TextLineDataset": four_lines_dataset}) @test.mock.patch.dict(imports85.__dict__, {"_get_imports85": (lambda: None)}) @test.mock.patch.dict(dnn_regression.__dict__, {"STEPS": 1}) def test_dnn_regression(self): dnn_regression.main([""]) @test.mock.patch.dict(data.__dict__, {"TextLineDataset": four_lines_dataset}) @test.mock.patch.dict(imports85.__dict__, {"_get_imports85": (lambda: None)}) @test.mock.patch.dict(custom_regression.__dict__, {"STEPS": 1}) def test_custom_regression(self): custom_regression.main([""]) if __name__ == "__main__": googletest.main()
apache-2.0
BhallaLab/moose-full
moose-examples/snippets/MULTI/midchan.py
2
13452
# midchan.py --- # Upi Bhalla, NCBS Bangalore 2014. # # Commentary: # # This loads in a medium-detail model incorporating # reac-diff and elec signaling in neurons. The reac-diff model # has just Ca and CaM in it, and there are no-cross-compartment # reactions though Ca diffuses everywhere. The elec model controls the # Ca levels in the chem compartments. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 3, or # (at your option) any later version. # # This program 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 # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, Fifth # Floor, Boston, MA 02110-1301, USA. # # Code: import sys sys.path.append('../../python') import os os.environ['NUMPTHREADS'] = '1' import math import numpy import matplotlib.pyplot as plt import moose import proto18 EREST_ACT = -70e-3 def loadElec(): library = moose.Neutral( '/library' ) moose.setCwe( '/library' ) proto18.make_Ca() proto18.make_Ca_conc() proto18.make_K_AHP() proto18.make_K_C() proto18.make_Na() proto18.make_K_DR() proto18.make_K_A() proto18.make_glu() proto18.make_NMDA() proto18.make_Ca_NMDA() proto18.make_NMDA_Ca_conc() proto18.make_axon() moose.setCwe( '/library' ) model = moose.Neutral( '/model' ) cellId = moose.loadModel( 'ca1_asym.p', '/model/elec', "Neutral" ) return cellId def loadChem( diffLength ): chem = moose.Neutral( '/model/chem' ) neuroCompt = moose.NeuroMesh( '/model/chem/kinetics' ) neuroCompt.separateSpines = 1 neuroCompt.geometryPolicy = 'cylinder' spineCompt = moose.SpineMesh( '/model/chem/compartment_1' ) moose.connect( neuroCompt, 'spineListOut', spineCompt, 'spineList', 'OneToOne' ) psdCompt = moose.PsdMesh( '/model/chem/compartment_2' ) #print 'Meshvolume[neuro, spine, psd] = ', neuroCompt.mesh[0].volume, spineCompt.mesh[0].volume, psdCompt.mesh[0].volume moose.connect( neuroCompt, 'psdListOut', psdCompt, 'psdList', 'OneToOne' ) modelId = moose.loadModel( 'minimal.g', '/model/chem', 'ee' ) #modelId = moose.loadModel( 'psd_merged31d.g', '/model/chem', 'ee' ) neuroCompt.name = 'dend' spineCompt.name = 'spine' psdCompt.name = 'psd' def makeNeuroMeshModel(): diffLength = 10e-6 # Aim for 2 soma compartments. elec = loadElec() loadChem( diffLength ) neuroCompt = moose.element( '/model/chem/dend' ) neuroCompt.diffLength = diffLength neuroCompt.cellPortion( elec, '/model/elec/#' ) for x in moose.wildcardFind( '/model/chem/##[ISA=PoolBase]' ): if (x.diffConst > 0): x.diffConst = 1e-11 for x in moose.wildcardFind( '/model/chem/##/Ca' ): x.diffConst = 1e-10 # Put in dend solvers ns = neuroCompt.numSegments ndc = neuroCompt.numDiffCompts print 'ns = ', ns, ', ndc = ', ndc assert( neuroCompt.numDiffCompts == neuroCompt.mesh.num ) assert( ns == 36 ) # assert( ndc == 278 ) # nmksolve = moose.Ksolve( '/model/chem/dend/ksolve' ) nmdsolve = moose.Dsolve( '/model/chem/dend/dsolve' ) nmstoich = moose.Stoich( '/model/chem/dend/stoich' ) nmstoich.compartment = neuroCompt nmstoich.ksolve = nmksolve nmstoich.dsolve = nmdsolve nmstoich.path = "/model/chem/dend/##" print 'done setting path, numPools = ', nmdsolve.numPools assert( nmdsolve.numPools == 1 ) assert( nmdsolve.numAllVoxels == ndc ) assert( nmstoich.numAllPools == 1 ) # oddly, numLocalFields does not work. ca = moose.element( '/model/chem/dend/DEND/Ca' ) assert( ca.numData == ndc ) # Put in spine solvers. Note that these get info from the neuroCompt spineCompt = moose.element( '/model/chem/spine' ) sdc = spineCompt.mesh.num print 'sdc = ', sdc assert( sdc == 13 ) smksolve = moose.Ksolve( '/model/chem/spine/ksolve' ) smdsolve = moose.Dsolve( '/model/chem/spine/dsolve' ) smstoich = moose.Stoich( '/model/chem/spine/stoich' ) smstoich.compartment = spineCompt smstoich.ksolve = smksolve smstoich.dsolve = smdsolve smstoich.path = "/model/chem/spine/##" print 'spine num Pools = ', smstoich.numAllPools assert( smstoich.numAllPools == 3 ) assert( smdsolve.numPools == 3 ) assert( smdsolve.numAllVoxels == sdc ) # Put in PSD solvers. Note that these get info from the neuroCompt psdCompt = moose.element( '/model/chem/psd' ) pdc = psdCompt.mesh.num assert( pdc == 13 ) pmksolve = moose.Ksolve( '/model/chem/psd/ksolve' ) pmdsolve = moose.Dsolve( '/model/chem/psd/dsolve' ) pmstoich = moose.Stoich( '/model/chem/psd/stoich' ) pmstoich.compartment = psdCompt pmstoich.ksolve = pmksolve pmstoich.dsolve = pmdsolve pmstoich.path = "/model/chem/psd/##" assert( pmstoich.numAllPools == 3 ) assert( pmdsolve.numPools == 3 ) assert( pmdsolve.numAllVoxels == pdc ) foo = moose.element( '/model/chem/psd/Ca' ) print 'PSD: numfoo = ', foo.numData print 'PSD: numAllVoxels = ', pmksolve.numAllVoxels # Put in junctions between the diffusion solvers nmdsolve.buildNeuroMeshJunctions( smdsolve, pmdsolve ) """ CaNpsd = moose.vec( '/model/chem/psdMesh/PSD/PP1_PSD/CaN' ) print 'numCaN in PSD = ', CaNpsd.nInit, ', vol = ', CaNpsd.volume CaNspine = moose.vec( '/model/chem/spine/SPINE/CaN_BULK/CaN' ) print 'numCaN in spine = ', CaNspine.nInit, ', vol = ', CaNspine.volume """ ################################################################## # set up adaptors aCa = moose.Adaptor( '/model/chem/spine/adaptCa', sdc ) adaptCa = moose.vec( '/model/chem/spine/adaptCa' ) chemCa = moose.vec( '/model/chem/spine/Ca' ) #print 'aCa = ', aCa, ' foo = ', foo, "len( ChemCa ) = ", len( chemCa ), ", numData = ", chemCa.numData, "len( adaptCa ) = ", len( adaptCa ) assert( len( adaptCa ) == sdc ) assert( len( chemCa ) == sdc ) for i in range( sdc ): elecCa = moose.element( '/model/elec/spine_head_14_' + str(i+1) + '/NMDA_Ca_conc' ) #print elecCa moose.connect( elecCa, 'concOut', adaptCa[i], 'input', 'Single' ) moose.connect( adaptCa, 'output', chemCa, 'setConc', 'OneToOne' ) adaptCa.inputOffset = 0.0 # adaptCa.outputOffset = 0.00008 # 80 nM offset in chem. adaptCa.scale = 1e-4 # 520 to 0.0052 mM #print adaptCa.outputOffset moose.le( '/model/chem/dend/DEND' ) compts = neuroCompt.elecComptList begin = neuroCompt.startVoxelInCompt end = neuroCompt.endVoxelInCompt aCa = moose.Adaptor( '/model/chem/dend/DEND/adaptCa', len( compts)) adaptCa = moose.vec( '/model/chem/dend/DEND/adaptCa' ) chemCa = moose.vec( '/model/chem/dend/DEND/Ca' ) #print 'aCa = ', aCa, ' foo = ', foo, "len( ChemCa ) = ", len( chemCa ), ", numData = ", chemCa.numData, "len( adaptCa ) = ", len( adaptCa ) assert( len( chemCa ) == ndc ) for i in zip( compts, adaptCa, begin, end ): name = i[0].path + '/Ca_conc' if ( moose.exists( name ) ): elecCa = moose.element( name ) #print i[2], i[3], ' ', elecCa #print i[1] moose.connect( elecCa, 'concOut', i[1], 'input', 'Single' ) for j in range( i[2], i[3] ): moose.connect( i[1], 'output', chemCa[j], 'setConc', 'Single' ) adaptCa.inputOffset = 0.0 # adaptCa.outputOffset = 0.00008 # 80 nM offset in chem. adaptCa.scale = 20e-6 # 10 arb units to 2 uM. def addPlot( objpath, field, plot ): #assert moose.exists( objpath ) if moose.exists( objpath ): tab = moose.Table( '/graphs/' + plot ) obj = moose.element( objpath ) if obj.className == 'Neutral': print "addPlot failed: object is a Neutral: ", objpath return moose.element( '/' ) else: #print "object was found: ", objpath, obj.className moose.connect( tab, 'requestOut', obj, field ) return tab else: print "addPlot failed: object not found: ", objpath return moose.element( '/' ) def makeElecPlots(): graphs = moose.Neutral( '/graphs' ) elec = moose.Neutral( '/graphs/elec' ) addPlot( '/model/elec/soma', 'getVm', 'elec/somaVm' ) addPlot( '/model/elec/spine_head_14_4', 'getVm', 'elec/spineVm' ) addPlot( '/model/elec/soma/Ca_conc', 'getCa', 'elec/somaCa' ) addPlot( '/model/elec/lat_11_2/Ca_conc', 'getCa', 'elec/lat11Ca' ) addPlot( '/model/elec/spine_head_14_4/NMDA_Ca_conc', 'getCa', 'elec/spine4Ca' ) addPlot( '/model/elec/spine_head_14_12/NMDA_Ca_conc', 'getCa', 'elec/spine12Ca' ) def makeChemPlots(): graphs = moose.Neutral( '/graphs' ) chem = moose.Neutral( '/graphs/chem' ) addPlot( '/model/chem/psd/Ca_CaM', 'getConc', 'chem/psdCaCam' ) addPlot( '/model/chem/psd/Ca', 'getConc', 'chem/psdCa' ) addPlot( '/model/chem/spine/Ca_CaM', 'getConc', 'chem/spineCaCam' ) addPlot( '/model/chem/spine/Ca[3]', 'getConc', 'chem/spine4Ca' ) addPlot( '/model/chem/spine/Ca[11]', 'getConc', 'chem/spine12Ca' ) addPlot( '/model/chem/dend/DEND/Ca', 'getConc', 'chem/dendCa' ) addPlot( '/model/chem/dend/DEND/Ca[20]', 'getConc', 'chem/dendCa20' ) def testNeuroMeshMultiscale(): elecDt = 50e-6 chemDt = 0.005 ePlotDt = 0.5e-3 cPlotDt = 0.005 plotName = 'nm.plot' makeNeuroMeshModel() print "after model is completely done" for i in moose.wildcardFind( '/model/chem/#/#/#/transloc#' ): print i[0].name, i[0].Kf, i[0].Kb, i[0].kf, i[0].kb makeChemPlots() makeElecPlots() moose.setClock( 0, elecDt ) moose.setClock( 1, elecDt ) moose.setClock( 2, elecDt ) moose.setClock( 4, chemDt ) moose.setClock( 5, chemDt ) moose.setClock( 6, chemDt ) moose.setClock( 7, cPlotDt ) moose.setClock( 8, ePlotDt ) moose.useClock( 0, '/model/elec/##[ISA=Compartment]', 'init' ) moose.useClock( 1, '/model/elec/##[ISA=Compartment]', 'process' ) moose.useClock( 1, '/model/elec/##[ISA=SpikeGen]', 'process' ) moose.useClock( 2, '/model/elec/##[ISA=ChanBase],/model/##[ISA=SynBase],/model/##[ISA=CaConc]','process') #moose.useClock( 5, '/model/chem/##[ISA=PoolBase],/model/##[ISA=ReacBase],/model/##[ISA=EnzBase]', 'process' ) #moose.useClock( 4, '/model/chem/##[ISA=Adaptor]', 'process' ) moose.useClock( 4, '/model/chem/#/dsolve', 'process' ) moose.useClock( 5, '/model/chem/#/ksolve', 'process' ) moose.useClock( 6, '/model/chem/spine/adaptCa', 'process' ) moose.useClock( 6, '/model/chem/dend/DEND/adaptCa', 'process' ) moose.useClock( 7, '/graphs/chem/#', 'process' ) moose.useClock( 8, '/graphs/elec/#', 'process' ) #hsolve = moose.HSolve( '/model/elec/hsolve' ) #moose.useClock( 1, '/model/elec/hsolve', 'process' ) #hsolve.dt = elecDt #hsolve.target = '/model/elec/compt' #moose.reinit() moose.element( '/model/elec/soma' ).inject = 2e-10 moose.element( '/model/chem/psd/Ca' ).concInit = 0.001 moose.element( '/model/chem/spine/Ca' ).concInit = 0.002 moose.element( '/model/chem/dend/DEND/Ca' ).concInit = 0.003 moose.reinit() moose.start( 0.25 ) # moose.element( '/model/elec/soma' ).inject = 0 # moose.start( 0.25 ) plt.ion() fig = plt.figure( figsize=(8,8) ) chem = fig.add_subplot( 311 ) chem.set_ylim( 0, 0.002 ) plt.ylabel( 'Conc (mM)' ) plt.xlabel( 'time (seconds)' ) for x in moose.wildcardFind( '/graphs/chem/#[ISA=Table]' ): pos = numpy.arange( 0, x.vector.size, 1 ) * cPlotDt line1, = chem.plot( pos, x.vector, label=x.name ) plt.legend() elec = fig.add_subplot( 312 ) plt.ylabel( 'Vm (V)' ) plt.xlabel( 'time (seconds)' ) for x in moose.wildcardFind( '/graphs/elec/#[ISA=Table]' ): pos = numpy.arange( 0, x.vector.size, 1 ) * ePlotDt line1, = elec.plot( pos, x.vector, label=x.name ) plt.legend() lenplot = fig.add_subplot( 313 ) plt.ylabel( 'Ca (mM )' ) plt.xlabel( 'Voxel#)' ) spineCa = moose.vec( '/model/chem/spine/Ca' ) dendCa = moose.vec( '/model/chem/dend/DEND/Ca' ) line1, = lenplot.plot( range( len( spineCa ) ), spineCa.conc, label='spine' ) line2, = lenplot.plot( range( len( dendCa ) ), dendCa.conc, label='dend' ) ca = [ x.Ca * 0.0001 for x in moose.wildcardFind( '/model/elec/##[ISA=CaConc]') ] line3, = lenplot.plot( range( len( ca ) ), ca, label='elec' ) spineCaM = moose.vec( '/model/chem/spine/Ca_CaM' ) line4, = lenplot.plot( range( len( spineCaM ) ), spineCaM.conc, label='spineCaM' ) psdCaM = moose.vec( '/model/chem/psd/Ca_CaM' ) line5, = lenplot.plot( range( len( psdCaM ) ), psdCaM.conc, label='psdCaM' ) plt.legend() fig.canvas.draw() raw_input() ''' for x in moose.wildcardFind( '/graphs/##[ISA=Table]' ): t = numpy.arange( 0, x.vector.size, 1 ) pylab.plot( t, x.vector, label=x.name ) pylab.legend() pylab.show() ''' print 'All done' def main(): testNeuroMeshMultiscale() if __name__ == '__main__': main() # # minimal.py ends here.
gpl-2.0
timqian/sms-tools
software/transformations_interface/hpsTransformations_function.py
23
6610
# function call to the transformation functions of relevance for the hpsModel import numpy as np import matplotlib.pyplot as plt from scipy.signal import get_window import sys, os sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../models/')) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../transformations/')) import hpsModel as HPS import hpsTransformations as HPST import harmonicTransformations as HT import utilFunctions as UF def analysis(inputFile='../../sounds/sax-phrase-short.wav', window='blackman', M=601, N=1024, t=-100, minSineDur=0.1, nH=100, minf0=350, maxf0=700, f0et=5, harmDevSlope=0.01, stocf=0.1): """ Analyze a sound with the harmonic plus stochastic model inputFile: input sound file (monophonic with sampling rate of 44100) window: analysis window type (rectangular, hanning, hamming, blackman, blackmanharris) M: analysis window size N: fft size (power of two, bigger or equal than M) t: magnitude threshold of spectral peaks minSineDur: minimum duration of sinusoidal tracks nH: maximum number of harmonics minf0: minimum fundamental frequency in sound maxf0: maximum fundamental frequency in sound f0et: maximum error accepted in f0 detection algorithm harmDevSlope: allowed deviation of harmonic tracks, higher harmonics have higher allowed deviation stocf: decimation factor used for the stochastic approximation returns inputFile: input file name; fs: sampling rate of input file, hfreq, hmag: harmonic frequencies, magnitude; mYst: stochastic residual """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # read input sound (fs, x) = UF.wavread(inputFile) # compute analysis window w = get_window(window, M) # compute the harmonic plus stochastic model of the whole sound hfreq, hmag, hphase, mYst = HPS.hpsModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope, minSineDur, Ns, stocf) # synthesize the harmonic plus stochastic model without original phases y, yh, yst = HPS.hpsModelSynth(hfreq, hmag, np.array([]), mYst, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_hpsModel.wav' UF.wavwrite(y,fs, outputFile) # create figure to plot plt.figure(figsize=(12, 9)) # frequency range to plot maxplotfreq = 15000.0 # plot the input sound plt.subplot(3,1,1) plt.plot(np.arange(x.size)/float(fs), x) plt.axis([0, x.size/float(fs), min(x), max(x)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('input sound: x') # plot spectrogram stochastic compoment plt.subplot(3,1,2) numFrames = int(mYst[:,0].size) sizeEnv = int(mYst[0,:].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = (.5*fs)*np.arange(sizeEnv*maxplotfreq/(.5*fs))/sizeEnv plt.pcolormesh(frmTime, binFreq, np.transpose(mYst[:,:sizeEnv*maxplotfreq/(.5*fs)+1])) plt.autoscale(tight=True) # plot harmonic on top of stochastic spectrogram if (hfreq.shape[1] > 0): harms = hfreq*np.less(hfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram') # plot the output sound plt.subplot(3,1,3) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show(block=False) return inputFile, fs, hfreq, hmag, mYst def transformation_synthesis(inputFile, fs, hfreq, hmag, mYst, freqScaling = np.array([0, 1.2, 2.01, 1.2, 2.679, .7, 3.146, .7]), freqStretching = np.array([0, 1, 2.01, 1, 2.679, 1.5, 3.146, 1.5]), timbrePreservation = 1, timeScaling = np.array([0, 0, 2.138, 2.138-1.0, 3.146, 3.146])): """ transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file fs: sampling rate of input file hfreq, hmag: harmonic frequencies and magnitudes mYst: stochastic residual freqScaling: frequency scaling factors, in time-value pairs (value of 1 no scaling) freqStretching: frequency stretching factors, in time-value pairs (value of 1 no stretching) timbrePreservation: 1 preserves original timbre, 0 it does not timeScaling: time scaling factors, in time-value pairs """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # frequency scaling of the harmonics hfreqt, hmagt = HT.harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs) # time scaling the sound yhfreq, yhmag, ystocEnv = HPST.hpsTimeScale(hfreqt, hmagt, mYst, timeScaling) # synthesis from the trasformed hps representation y, yh, yst = HPS.hpsModelSynth(yhfreq, yhmag, np.array([]), ystocEnv, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_hpsModelTransformation.wav' UF.wavwrite(y,fs, outputFile) # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 # plot spectrogram of transformed stochastic compoment plt.subplot(2,1,1) numFrames = int(ystocEnv[:,0].size) sizeEnv = int(ystocEnv[0,:].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = (.5*fs)*np.arange(sizeEnv*maxplotfreq/(.5*fs))/sizeEnv plt.pcolormesh(frmTime, binFreq, np.transpose(ystocEnv[:,:sizeEnv*maxplotfreq/(.5*fs)+1])) plt.autoscale(tight=True) # plot transformed harmonic on top of stochastic spectrogram if (yhfreq.shape[1] > 0): harms = yhfreq*np.less(yhfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram') # plot the output sound plt.subplot(2,1,2) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show() if __name__ == "__main__": # analysis inputFile, fs, hfreq, hmag, mYst = analysis() # transformation and synthesis transformation_synthesis(inputFile, fs, hfreq, hmag, mYst) plt.show()
agpl-3.0
adammenges/statsmodels
statsmodels/regression/linear_model.py
16
93645
# TODO: Determine which tests are valid for GLSAR, and under what conditions # TODO: Fix issue with constant and GLS # TODO: GLS: add options Iterative GLS, for iterative fgls if sigma is None # TODO: GLS: default if sigma is none should be two-step GLS # TODO: Check nesting when performing model based tests, lr, wald, lm """ This module implements standard regression models: Generalized Least Squares (GLS) Ordinary Least Squares (OLS) Weighted Least Squares (WLS) Generalized Least Squares with autoregressive error terms GLSAR(p) Models are specified with an endogenous response variable and an exogenous design matrix and are fit using their `fit` method. Subclasses that have more complicated covariance matrices should write over the 'whiten' method as the fit method prewhitens the response by calling 'whiten'. General reference for regression models: D. C. Montgomery and E.A. Peck. "Introduction to Linear Regression Analysis." 2nd. Ed., Wiley, 1992. Econometrics references for regression models: R. Davidson and J.G. MacKinnon. "Econometric Theory and Methods," Oxford, 2004. W. Green. "Econometric Analysis," 5th ed., Pearson, 2003. """ from __future__ import print_function from statsmodels.compat.python import lrange, lzip, range __docformat__ = 'restructuredtext en' __all__ = ['GLS', 'WLS', 'OLS', 'GLSAR'] import numpy as np import pandas as pd from scipy.linalg import toeplitz from scipy import stats from scipy import optimize from statsmodels.compat.numpy import np_matrix_rank from statsmodels.tools.data import _is_using_pandas from statsmodels.tools.tools import add_constant, chain_dot, pinv_extended from statsmodels.tools.decorators import (resettable_cache, cache_readonly, cache_writable) import statsmodels.base.model as base import statsmodels.base.wrapper as wrap from statsmodels.emplike.elregress import _ELRegOpts import warnings from statsmodels.tools.sm_exceptions import InvalidTestWarning # need import in module instead of lazily to copy `__doc__` from . import _prediction as pred def _get_sigma(sigma, nobs): """ Returns sigma (matrix, nobs by nobs) for GLS and the inverse of its Cholesky decomposition. Handles dimensions and checks integrity. If sigma is None, returns None, None. Otherwise returns sigma, cholsigmainv. """ if sigma is None: return None, None sigma = np.asarray(sigma).squeeze() if sigma.ndim == 0: sigma = np.repeat(sigma, nobs) if sigma.ndim == 1: if sigma.shape != (nobs,): raise ValueError("Sigma must be a scalar, 1d of length %s or a 2d " "array of shape %s x %s" % (nobs, nobs, nobs)) cholsigmainv = 1/np.sqrt(sigma) else: if sigma.shape != (nobs, nobs): raise ValueError("Sigma must be a scalar, 1d of length %s or a 2d " "array of shape %s x %s" % (nobs, nobs, nobs)) cholsigmainv = np.linalg.cholesky(np.linalg.pinv(sigma)).T return sigma, cholsigmainv class RegressionModel(base.LikelihoodModel): """ Base class for linear regression models. Should not be directly called. Intended for subclassing. """ def __init__(self, endog, exog, **kwargs): super(RegressionModel, self).__init__(endog, exog, **kwargs) self._data_attr.extend(['pinv_wexog', 'wendog', 'wexog', 'weights']) def initialize(self): self.wexog = self.whiten(self.exog) self.wendog = self.whiten(self.endog) # overwrite nobs from class Model: self.nobs = float(self.wexog.shape[0]) self._df_model = None self._df_resid = None self.rank = None @property def df_model(self): """ The model degree of freedom, defined as the rank of the regressor matrix minus 1 if a constant is included. """ if self._df_model is None: if self.rank is None: self.rank = np_matrix_rank(self.exog) self._df_model = float(self.rank - self.k_constant) return self._df_model @df_model.setter def df_model(self, value): self._df_model = value @property def df_resid(self): """ The residual degree of freedom, defined as the number of observations minus the rank of the regressor matrix. """ if self._df_resid is None: if self.rank is None: self.rank = np_matrix_rank(self.exog) self._df_resid = self.nobs - self.rank return self._df_resid @df_resid.setter def df_resid(self, value): self._df_resid = value def whiten(self, X): raise NotImplementedError("Subclasses should implement.") def fit(self, method="pinv", cov_type='nonrobust', cov_kwds=None, use_t=None, **kwargs): """ Full fit of the model. The results include an estimate of covariance matrix, (whitened) residuals and an estimate of scale. Parameters ---------- method : str, optional Can be "pinv", "qr". "pinv" uses the Moore-Penrose pseudoinverse to solve the least squares problem. "qr" uses the QR factorization. cov_type : str, optional See `regression.linear_model.RegressionResults` for a description of the available covariance estimators cov_kwds : list or None, optional See `linear_model.RegressionResults.get_robustcov_results` for a description required keywords for alternative covariance estimators use_t : bool, optional Flag indicating to use the Student's t distribution when computing p-values. Default behavior depends on cov_type. See `linear_model.RegressionResults.get_robustcov_results` for implementation details. Returns ------- A RegressionResults class instance. See Also --------- regression.linear_model.RegressionResults regression.linear_model.RegressionResults.get_robustcov_results Notes ----- The fit method uses the pseudoinverse of the design/exogenous variables to solve the least squares minimization. """ if method == "pinv": if ((not hasattr(self, 'pinv_wexog')) or (not hasattr(self, 'normalized_cov_params')) or (not hasattr(self, 'rank'))): self.pinv_wexog, singular_values = pinv_extended(self.wexog) self.normalized_cov_params = np.dot(self.pinv_wexog, np.transpose(self.pinv_wexog)) # Cache these singular values for use later. self.wexog_singular_values = singular_values self.rank = np_matrix_rank(np.diag(singular_values)) beta = np.dot(self.pinv_wexog, self.wendog) elif method == "qr": if ((not hasattr(self, 'exog_Q')) or (not hasattr(self, 'exog_R')) or (not hasattr(self, 'normalized_cov_params')) or (getattr(self, 'rank', None) is None)): Q, R = np.linalg.qr(self.wexog) self.exog_Q, self.exog_R = Q, R self.normalized_cov_params = np.linalg.inv(np.dot(R.T, R)) # Cache singular values from R. self.wexog_singular_values = np.linalg.svd(R, 0, 0) self.rank = np_matrix_rank(R) else: Q, R = self.exog_Q, self.exog_R # used in ANOVA self.effects = effects = np.dot(Q.T, self.wendog) beta = np.linalg.solve(R, effects) if self._df_model is None: self._df_model = float(self.rank - self.k_constant) if self._df_resid is None: self.df_resid = self.nobs - self.rank if isinstance(self, OLS): lfit = OLSResults(self, beta, normalized_cov_params=self.normalized_cov_params, cov_type=cov_type, cov_kwds=cov_kwds, use_t=use_t) else: lfit = RegressionResults(self, beta, normalized_cov_params=self.normalized_cov_params, cov_type=cov_type, cov_kwds=cov_kwds, use_t=use_t, **kwargs) return RegressionResultsWrapper(lfit) def fit_regularized(self, method="coord_descent", maxiter=1000, alpha=0., L1_wt=1., start_params=None, cnvrg_tol=1e-8, zero_tol=1e-8, **kwargs): """ Return a regularized fit to a linear regression model. Parameters ---------- method : string Only the coordinate descent algorithm is implemented. maxiter : integer The maximum number of iteration cycles (an iteration cycle involves running coordinate descent on all variables). alpha : scalar or array-like The penalty weight. If a scalar, the same penalty weight applies to all variables in the model. If a vector, it must have the same length as `params`, and contains a penalty weight for each coefficient. L1_wt : scalar The fraction of the penalty given to the L1 penalty term. Must be between 0 and 1 (inclusive). If 0, the fit is ridge regression. If 1, the fit is the lasso. start_params : array-like Starting values for ``params``. cnvrg_tol : scalar If ``params`` changes by less than this amount (in sup-norm) in once iteration cycle, the algorithm terminates with convergence. zero_tol : scalar Any estimated coefficient smaller than this value is replaced with zero. Returns ------- A RegressionResults object, of the same type returned by ``fit``. Notes ----- The approach closely follows that implemented in the glmnet package in R. The penalty is the "elastic net" penalty, which is a convex combination of L1 and L2 penalties. The function that is minimized is: ..math:: 0.5*RSS/n + alpha*((1-L1_wt)*|params|_2^2/2 + L1_wt*|params|_1) where RSS is the usual regression sum of squares, n is the sample size, and :math:`|*|_1` and :math:`|*|_2` are the L1 and L2 norms. Post-estimation results are based on the same data used to select variables, hence may be subject to overfitting biases. References ---------- Friedman, Hastie, Tibshirani (2008). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software 33(1), 1-22 Feb 2010. """ k_exog = self.wexog.shape[1] if np.isscalar(alpha): alpha = alpha * np.ones(k_exog, dtype=np.float64) # Below we work with RSS + penalty, so we need to rescale. alpha *= 2 * self.wexog.shape[0] if start_params is None: params = np.zeros(k_exog, dtype=np.float64) else: params = start_params.copy() converged = False xxprod = 2*(self.wexog**2).sum(0) # Coordinate descent for itr in range(maxiter): params_save = params.copy() for k in range(self.wexog.shape[1]): params[k] = 0. wendog_adj = self.wendog - np.dot(self.wexog, params) xyprod = 2*np.dot(self.wexog[:,k], wendog_adj) den = xxprod[k] + alpha[k] * (1 - L1_wt) a = alpha[k] * L1_wt if a >= np.abs(xyprod): params[k] = 0. elif xyprod > 0: params[k] = (xyprod - a) / den else: params[k] = (xyprod + a) / den # Check for convergence pchange = np.max(np.abs(params - params_save)) if pchange < cnvrg_tol: converged = True break # Set approximate zero coefficients to be exactly zero params *= np.abs(params) >= zero_tol # Fit the reduced model to get standard errors and other # post-estimation results. ii = np.flatnonzero(params) cov = np.zeros((k_exog, k_exog), dtype=np.float64) if len(ii) > 0: model = self.__class__(self.wendog, self.wexog[:,ii]) rslt = model.fit() cov[np.ix_(ii, ii)] = rslt.normalized_cov_params lfit = RegressionResults(self, params, normalized_cov_params=cov) lfit.converged = converged return RegressionResultsWrapper(lfit) def predict(self, params, exog=None): """ Return linear predicted values from a design matrix. Parameters ---------- params : array-like Parameters of a linear model exog : array-like, optional. Design / exogenous data. Model exog is used if None. Returns ------- An array of fitted values Notes ----- If the model has not yet been fit, params is not optional. """ #JP: this doesn't look correct for GLMAR #SS: it needs its own predict method if exog is None: exog = self.exog return np.dot(exog, params) class GLS(RegressionModel): __doc__ = """ Generalized least squares model with a general covariance structure. %(params)s sigma : scalar or array `sigma` is the weighting matrix of the covariance. The default is None for no scaling. If `sigma` is a scalar, it is assumed that `sigma` is an n x n diagonal matrix with the given scalar, `sigma` as the value of each diagonal element. If `sigma` is an n-length vector, then `sigma` is assumed to be a diagonal matrix with the given `sigma` on the diagonal. This should be the same as WLS. %(extra_params)s **Attributes** pinv_wexog : array `pinv_wexog` is the p x n Moore-Penrose pseudoinverse of `wexog`. cholsimgainv : array The transpose of the Cholesky decomposition of the pseudoinverse. df_model : float p - 1, where p is the number of regressors including the intercept. of freedom. df_resid : float Number of observations n less the number of parameters p. llf : float The value of the likelihood function of the fitted model. nobs : float The number of observations n. normalized_cov_params : array p x p array :math:`(X^{T}\Sigma^{-1}X)^{-1}` results : RegressionResults instance A property that returns the RegressionResults class if fit. sigma : array `sigma` is the n x n covariance structure of the error terms. wexog : array Design matrix whitened by `cholsigmainv` wendog : array Response variable whitened by `cholsigmainv` Notes ----- If sigma is a function of the data making one of the regressors a constant, then the current postestimation statistics will not be correct. Examples -------- >>> import numpy as np >>> import statsmodels.api as sm >>> data = sm.datasets.longley.load() >>> data.exog = sm.add_constant(data.exog) >>> ols_resid = sm.OLS(data.endog, data.exog).fit().resid >>> res_fit = sm.OLS(ols_resid[1:], ols_resid[:-1]).fit() >>> rho = res_fit.params `rho` is a consistent estimator of the correlation of the residuals from an OLS fit of the longley data. It is assumed that this is the true rho of the AR process data. >>> from scipy.linalg import toeplitz >>> order = toeplitz(np.arange(16)) >>> sigma = rho**order `sigma` is an n x n matrix of the autocorrelation structure of the data. >>> gls_model = sm.GLS(data.endog, data.exog, sigma=sigma) >>> gls_results = gls_model.fit() >>> print(gls_results.summary())) """ % {'params' : base._model_params_doc, 'extra_params' : base._missing_param_doc + base._extra_param_doc} def __init__(self, endog, exog, sigma=None, missing='none', hasconst=None, **kwargs): #TODO: add options igls, for iterative fgls if sigma is None #TODO: default if sigma is none should be two-step GLS sigma, cholsigmainv = _get_sigma(sigma, len(endog)) super(GLS, self).__init__(endog, exog, missing=missing, hasconst=hasconst, sigma=sigma, cholsigmainv=cholsigmainv, **kwargs) #store attribute names for data arrays self._data_attr.extend(['sigma', 'cholsigmainv']) def whiten(self, X): """ GLS whiten method. Parameters ----------- X : array-like Data to be whitened. Returns ------- np.dot(cholsigmainv,X) See Also -------- regression.GLS """ X = np.asarray(X) if self.sigma is None or self.sigma.shape == (): return X elif self.sigma.ndim == 1: if X.ndim == 1: return X * self.cholsigmainv else: return X * self.cholsigmainv[:, None] else: return np.dot(self.cholsigmainv, X) def loglike(self, params): """ Returns the value of the Gaussian log-likelihood function at params. Given the whitened design matrix, the log-likelihood is evaluated at the parameter vector `params` for the dependent variable `endog`. Parameters ---------- params : array-like The parameter estimates Returns ------- loglike : float The value of the log-likelihood function for a GLS Model. Notes ----- The log-likelihood function for the normal distribution is .. math:: -\\frac{n}{2}\\log\\left(\\left(Y-\\hat{Y}\\right)^{\\prime}\\left(Y-\\hat{Y}\\right)\\right)-\\frac{n}{2}\\left(1+\\log\\left(\\frac{2\\pi}{n}\\right)\\right)-\\frac{1}{2}\\log\\left(\\left|\\Sigma\\right|\\right) Y and Y-hat are whitened. """ #TODO: combine this with OLS/WLS loglike and add _det_sigma argument nobs2 = self.nobs / 2.0 SSR = np.sum((self.wendog - np.dot(self.wexog, params))**2, axis=0) llf = -np.log(SSR) * nobs2 # concentrated likelihood llf -= (1+np.log(np.pi/nobs2))*nobs2 # with likelihood constant if np.any(self.sigma): #FIXME: robust-enough check? unneeded if _det_sigma gets defined if self.sigma.ndim==2: det = np.linalg.slogdet(self.sigma) llf -= .5*det[1] else: llf -= 0.5*np.sum(np.log(self.sigma)) # with error covariance matrix return llf class WLS(RegressionModel): __doc__ = """ A regression model with diagonal but non-identity covariance structure. The weights are presumed to be (proportional to) the inverse of the variance of the observations. That is, if the variables are to be transformed by 1/sqrt(W) you must supply weights = 1/W. %(params)s weights : array-like, optional 1d array of weights. If you supply 1/W then the variables are pre- multiplied by 1/sqrt(W). If no weights are supplied the default value is 1 and WLS reults are the same as OLS. %(extra_params)s Attributes ---------- weights : array The stored weights supplied as an argument. See regression.GLS Examples --------- >>> import numpy as np >>> import statsmodels.api as sm >>> Y = [1,3,4,5,2,3,4] >>> X = range(1,8) >>> X = sm.add_constant(X) >>> wls_model = sm.WLS(Y,X, weights=list(range(1,8))) >>> results = wls_model.fit() >>> results.params array([ 2.91666667, 0.0952381 ]) >>> results.tvalues array([ 2.0652652 , 0.35684428]) >>> print(results.t_test([1, 0])) <T test: effect=array([ 2.91666667]), sd=array([[ 1.41224801]]), t=array([[ 2.0652652]]), p=array([[ 0.04690139]]), df_denom=5> >>> print(results.f_test([0, 1])) <F test: F=array([[ 0.12733784]]), p=[[ 0.73577409]], df_denom=5, df_num=1> Notes ----- If the weights are a function of the data, then the post estimation statistics such as fvalue and mse_model might not be correct, as the package does not yet support no-constant regression. """ % {'params' : base._model_params_doc, 'extra_params' : base._missing_param_doc + base._extra_param_doc} def __init__(self, endog, exog, weights=1., missing='none', hasconst=None, **kwargs): weights = np.array(weights) if weights.shape == (): if (missing == 'drop' and 'missing_idx' in kwargs and kwargs['missing_idx'] is not None): # patsy may have truncated endog weights = np.repeat(weights, len(kwargs['missing_idx'])) else: weights = np.repeat(weights, len(endog)) # handle case that endog might be of len == 1 if len(weights) == 1: weights = np.array([weights.squeeze()]) else: weights = weights.squeeze() super(WLS, self).__init__(endog, exog, missing=missing, weights=weights, hasconst=hasconst, **kwargs) nobs = self.exog.shape[0] weights = self.weights # Experimental normalization of weights weights = weights / np.sum(weights) * nobs if weights.size != nobs and weights.shape[0] != nobs: raise ValueError('Weights must be scalar or same length as design') def whiten(self, X): """ Whitener for WLS model, multiplies each column by sqrt(self.weights) Parameters ---------- X : array-like Data to be whitened Returns ------- sqrt(weights)*X """ #print(self.weights.var())) X = np.asarray(X) if X.ndim == 1: return X * np.sqrt(self.weights) elif X.ndim == 2: return np.sqrt(self.weights)[:, None]*X def loglike(self, params): """ Returns the value of the gaussian log-likelihood function at params. Given the whitened design matrix, the log-likelihood is evaluated at the parameter vector `params` for the dependent variable `Y`. Parameters ---------- params : array-like The parameter estimates. Returns ------- llf : float The value of the log-likelihood function for a WLS Model. Notes -------- .. math:: -\\frac{n}{2}\\log\\left(Y-\\hat{Y}\\right)-\\frac{n}{2}\\left(1+\\log\\left(\\frac{2\\pi}{n}\\right)\\right)-\\frac{1}{2}log\\left(\\left|W\\right|\\right) where :math:`W` is a diagonal matrix """ nobs2 = self.nobs / 2.0 SSR = np.sum((self.wendog - np.dot(self.wexog,params))**2, axis=0) llf = -np.log(SSR) * nobs2 # concentrated likelihood llf -= (1+np.log(np.pi/nobs2))*nobs2 # with constant llf += 0.5 * np.sum(np.log(self.weights)) return llf class OLS(WLS): __doc__ = """ A simple ordinary least squares model. %(params)s %(extra_params)s Attributes ---------- weights : scalar Has an attribute weights = array(1.0) due to inheritance from WLS. See Also -------- GLS Examples -------- >>> import numpy as np >>> >>> import statsmodels.api as sm >>> >>> Y = [1,3,4,5,2,3,4] >>> X = range(1,8) >>> X = sm.add_constant(X) >>> >>> model = sm.OLS(Y,X) >>> results = model.fit() >>> results.params array([ 2.14285714, 0.25 ]) >>> results.tvalues array([ 1.87867287, 0.98019606]) >>> print(results.t_test([1, 0]))) <T test: effect=array([ 2.14285714]), sd=array([[ 1.14062282]]), t=array([[ 1.87867287]]), p=array([[ 0.05953974]]), df_denom=5> >>> print(results.f_test(np.identity(2))) <F test: F=array([[ 19.46078431]]), p=[[ 0.00437251]], df_denom=5, df_num=2> Notes ----- No constant is added by the model unless you are using formulas. """ % {'params' : base._model_params_doc, 'extra_params' : base._missing_param_doc + base._extra_param_doc} #TODO: change example to use datasets. This was the point of datasets! def __init__(self, endog, exog=None, missing='none', hasconst=None, **kwargs): super(OLS, self).__init__(endog, exog, missing=missing, hasconst=hasconst, **kwargs) if "weights" in self._init_keys: self._init_keys.remove("weights") def loglike(self, params): """ The likelihood function for the clasical OLS model. Parameters ---------- params : array-like The coefficients with which to estimate the log-likelihood. Returns ------- The concentrated likelihood function evaluated at params. """ nobs2 = self.nobs / 2.0 return -nobs2*np.log(2*np.pi)-nobs2*np.log(1/(2*nobs2) *\ np.dot(np.transpose(self.endog - np.dot(self.exog, params)), (self.endog - np.dot(self.exog,params)))) -\ nobs2 def whiten(self, Y): """ OLS model whitener does nothing: returns Y. """ return Y class GLSAR(GLS): __doc__ = """ A regression model with an AR(p) covariance structure. %(params)s rho : int Order of the autoregressive covariance %(extra_params)s Examples -------- >>> import statsmodels.api as sm >>> X = range(1,8) >>> X = sm.add_constant(X) >>> Y = [1,3,4,5,8,10,9] >>> model = sm.GLSAR(Y, X, rho=2) >>> for i in range(6): ... results = model.fit() ... print("AR coefficients: {0}".format(model.rho)) ... rho, sigma = sm.regression.yule_walker(results.resid, ... order=model.order) ... model = sm.GLSAR(Y, X, rho) ... AR coefficients: [ 0. 0.] AR coefficients: [-0.52571491 -0.84496178] AR coefficients: [-0.6104153 -0.86656458] AR coefficients: [-0.60439494 -0.857867 ] AR coefficients: [-0.6048218 -0.85846157] AR coefficients: [-0.60479146 -0.85841922] >>> results.params array([-0.66661205, 1.60850853]) >>> results.tvalues array([ -2.10304127, 21.8047269 ]) >>> print(results.t_test([1, 0])) <T test: effect=array([-0.66661205]), sd=array([[ 0.31697526]]), t=array([[-2.10304127]]), p=array([[ 0.06309969]]), df_denom=3> >>> print(results.f_test(np.identity(2))) <F test: F=array([[ 1815.23061844]]), p=[[ 0.00002372]], df_denom=3, df_num=2> Or, equivalently >>> model2 = sm.GLSAR(Y, X, rho=2) >>> res = model2.iterative_fit(maxiter=6) >>> model2.rho array([-0.60479146, -0.85841922]) Notes ----- GLSAR is considered to be experimental. The linear autoregressive process of order p--AR(p)--is defined as: TODO """ % {'params' : base._model_params_doc, 'extra_params' : base._missing_param_doc + base._extra_param_doc} def __init__(self, endog, exog=None, rho=1, missing='none', **kwargs): #this looks strange, interpreting rho as order if it is int if isinstance(rho, np.int): self.order = rho self.rho = np.zeros(self.order, np.float64) else: self.rho = np.squeeze(np.asarray(rho)) if len(self.rho.shape) not in [0,1]: raise ValueError("AR parameters must be a scalar or a vector") if self.rho.shape == (): self.rho.shape = (1,) self.order = self.rho.shape[0] if exog is None: #JP this looks wrong, should be a regression on constant #results for rho estimate now identical to yule-walker on y #super(AR, self).__init__(endog, add_constant(endog)) super(GLSAR, self).__init__(endog, np.ones((endog.shape[0],1)), missing=missing, **kwargs) else: super(GLSAR, self).__init__(endog, exog, missing=missing, **kwargs) def iterative_fit(self, maxiter=3, rtol=1e-4, **kwds): """ Perform an iterative two-stage procedure to estimate a GLS model. The model is assumed to have AR(p) errors, AR(p) parameters and regression coefficients are estimated iteratively. Parameters ---------- maxiter : integer, optional the number of iterations rtol : float, optional Relative tolerance between estimated coefficients to stop the estimation. Stops if max(abs(last - current) / abs(last)) < rtol """ # TODO: update this after going through example. converged = False i = -1 # need to initialize for maxiter < 1 (skip loop) history = {'params': [], 'rho':[self.rho]} for i in range(maxiter - 1): if hasattr(self, 'pinv_wexog'): del self.pinv_wexog self.initialize() results = self.fit() history['params'].append(results.params) if i == 0: last = results.params else: diff = np.max(np.abs(last - results.params) / np.abs(last)) if diff < rtol: converged = True break last = results.params self.rho, _ = yule_walker(results.resid, order=self.order, df=None) history['rho'].append(self.rho) # why not another call to self.initialize # Use kwarg to insert history if not converged and maxiter > 0: # maxiter <= 0 just does OLS if hasattr(self, 'pinv_wexog'): del self.pinv_wexog self.initialize() # if converged then this is a duplicate fit, because we didn't update rho results = self.fit(history=history, **kwds) results.iter = i + 1 # add last fit to history, not if duplicate fit if not converged: results.history['params'].append(results.params) results.iter += 1 results.converged = converged return results def whiten(self, X): """ Whiten a series of columns according to an AR(p) covariance structure. This drops initial p observations. Parameters ---------- X : array-like The data to be whitened, Returns ------- whitened array """ #TODO: notation for AR process X = np.asarray(X, np.float64) _X = X.copy() #the following loops over the first axis, works for 1d and nd for i in range(self.order): _X[(i+1):] = _X[(i+1):] - self.rho[i] * X[0:-(i+1)] return _X[self.order:] def yule_walker(X, order=1, method="unbiased", df=None, inv=False, demean=True): """ Estimate AR(p) parameters from a sequence X using Yule-Walker equation. Unbiased or maximum-likelihood estimator (mle) See, for example: http://en.wikipedia.org/wiki/Autoregressive_moving_average_model Parameters ---------- X : array-like 1d array order : integer, optional The order of the autoregressive process. Default is 1. method : string, optional Method can be "unbiased" or "mle" and this determines denominator in estimate of autocorrelation function (ACF) at lag k. If "mle", the denominator is n=X.shape[0], if "unbiased" the denominator is n-k. The default is unbiased. df : integer, optional Specifies the degrees of freedom. If `df` is supplied, then it is assumed the X has `df` degrees of freedom rather than `n`. Default is None. inv : bool If inv is True the inverse of R is also returned. Default is False. demean : bool True, the mean is subtracted from `X` before estimation. Returns ------- rho The autoregressive coefficients sigma TODO Examples -------- >>> import statsmodels.api as sm >>> from statsmodels.datasets.sunspots import load >>> data = load() >>> rho, sigma = sm.regression.yule_walker(data.endog, order=4, method="mle") >>> rho array([ 1.28310031, -0.45240924, -0.20770299, 0.04794365]) >>> sigma 16.808022730464351 """ #TODO: define R better, look back at notes and technical notes on YW. #First link here is useful #http://www-stat.wharton.upenn.edu/~steele/Courses/956/ResourceDetails/YuleWalkerAndMore.htm method = str(method).lower() if method not in ["unbiased", "mle"]: raise ValueError("ACF estimation method must be 'unbiased' or 'MLE'") X = np.array(X, dtype=np.float64) if demean: X -= X.mean() # automatically demean's X n = df or X.shape[0] if method == "unbiased": # this is df_resid ie., n - p denom = lambda k: n - k else: denom = lambda k: n if X.ndim > 1 and X.shape[1] != 1: raise ValueError("expecting a vector to estimate AR parameters") r = np.zeros(order+1, np.float64) r[0] = (X**2).sum() / denom(0) for k in range(1,order+1): r[k] = (X[0:-k]*X[k:]).sum() / denom(k) R = toeplitz(r[:-1]) rho = np.linalg.solve(R, r[1:]) sigmasq = r[0] - (r[1:]*rho).sum() if inv==True: return rho, np.sqrt(sigmasq), np.linalg.inv(R) else: return rho, np.sqrt(sigmasq) class RegressionResults(base.LikelihoodModelResults): """ This class summarizes the fit of a linear regression model. It handles the output of contrasts, estimates of covariance, etc. Returns ------- **Attributes** aic Aikake's information criteria. For a model with a constant :math:`-2llf + 2(df_model + 1)`. For a model without a constant :math:`-2llf + 2(df_model)`. bic Bayes' information criteria For a model with a constant :math:`-2llf + \log(n)(df_model+1)`. For a model without a constant :math:`-2llf + \log(n)(df_model)` bse The standard errors of the parameter estimates. pinv_wexog See specific model class docstring centered_tss The total (weighted) sum of squares centered about the mean. cov_HC0 Heteroscedasticity robust covariance matrix. See HC0_se below. cov_HC1 Heteroscedasticity robust covariance matrix. See HC1_se below. cov_HC2 Heteroscedasticity robust covariance matrix. See HC2_se below. cov_HC3 Heteroscedasticity robust covariance matrix. See HC3_se below. cov_type Parameter covariance estimator used for standard errors and t-stats df_model Model degress of freedom. The number of regressors `p`. Does not include the constant if one is present df_resid Residual degrees of freedom. `n - p - 1`, if a constant is present. `n - p` if a constant is not included. ess Explained sum of squares. If a constant is present, the centered total sum of squares minus the sum of squared residuals. If there is no constant, the uncentered total sum of squares is used. fvalue F-statistic of the fully specified model. Calculated as the mean squared error of the model divided by the mean squared error of the residuals. f_pvalue p-value of the F-statistic fittedvalues The predicted the values for the original (unwhitened) design. het_scale adjusted squared residuals for heteroscedasticity robust standard errors. Is only available after `HC#_se` or `cov_HC#` is called. See HC#_se for more information. history Estimation history for iterative estimators HC0_se White's (1980) heteroskedasticity robust standard errors. Defined as sqrt(diag(X.T X)^(-1)X.T diag(e_i^(2)) X(X.T X)^(-1) where e_i = resid[i] HC0_se is a cached property. When HC0_se or cov_HC0 is called the RegressionResults instance will then have another attribute `het_scale`, which is in this case is just resid**2. HC1_se MacKinnon and White's (1985) alternative heteroskedasticity robust standard errors. Defined as sqrt(diag(n/(n-p)*HC_0) HC1_see is a cached property. When HC1_se or cov_HC1 is called the RegressionResults instance will then have another attribute `het_scale`, which is in this case is n/(n-p)*resid**2. HC2_se MacKinnon and White's (1985) alternative heteroskedasticity robust standard errors. Defined as (X.T X)^(-1)X.T diag(e_i^(2)/(1-h_ii)) X(X.T X)^(-1) where h_ii = x_i(X.T X)^(-1)x_i.T HC2_see is a cached property. When HC2_se or cov_HC2 is called the RegressionResults instance will then have another attribute `het_scale`, which is in this case is resid^(2)/(1-h_ii). HC3_se MacKinnon and White's (1985) alternative heteroskedasticity robust standard errors. Defined as (X.T X)^(-1)X.T diag(e_i^(2)/(1-h_ii)^(2)) X(X.T X)^(-1) where h_ii = x_i(X.T X)^(-1)x_i.T HC3_see is a cached property. When HC3_se or cov_HC3 is called the RegressionResults instance will then have another attribute `het_scale`, which is in this case is resid^(2)/(1-h_ii)^(2). model A pointer to the model instance that called fit() or results. mse_model Mean squared error the model. This is the explained sum of squares divided by the model degrees of freedom. mse_resid Mean squared error of the residuals. The sum of squared residuals divided by the residual degrees of freedom. mse_total Total mean squared error. Defined as the uncentered total sum of squares divided by n the number of observations. nobs Number of observations n. normalized_cov_params See specific model class docstring params The linear coefficients that minimize the least squares criterion. This is usually called Beta for the classical linear model. pvalues The two-tailed p values for the t-stats of the params. resid The residuals of the model. resid_pearson `wresid` normalized to have unit variance. rsquared R-squared of a model with an intercept. This is defined here as 1 - `ssr`/`centered_tss` if the constant is included in the model and 1 - `ssr`/`uncentered_tss` if the constant is omitted. rsquared_adj Adjusted R-squared. This is defined here as 1 - (`nobs`-1)/`df_resid` * (1-`rsquared`) if a constant is included and 1 - `nobs`/`df_resid` * (1-`rsquared`) if no constant is included. scale A scale factor for the covariance matrix. Default value is ssr/(n-p). Note that the square root of `scale` is often called the standard error of the regression. ssr Sum of squared (whitened) residuals. uncentered_tss Uncentered sum of squares. Sum of the squared values of the (whitened) endogenous response variable. wresid The residuals of the transformed/whitened regressand and regressor(s) """ _cache = {} # needs to be a class attribute for scale setter? def __init__(self, model, params, normalized_cov_params=None, scale=1., cov_type='nonrobust', cov_kwds=None, use_t=None, **kwargs): super(RegressionResults, self).__init__(model, params, normalized_cov_params, scale) self._cache = resettable_cache() if hasattr(model, 'wexog_singular_values'): self._wexog_singular_values = model.wexog_singular_values else: self._wexog_singular_values = None self.df_model = model.df_model self.df_resid = model.df_resid if cov_type == 'nonrobust': self.cov_type = 'nonrobust' self.cov_kwds = {'description' : 'Standard Errors assume that the ' + 'covariance matrix of the errors is correctly ' + 'specified.'} if use_t is None: self.use_t = True # TODO: class default else: if cov_kwds is None: cov_kwds = {} if 'use_t' in cov_kwds: # TODO: we want to get rid of 'use_t' in cov_kwds use_t_2 = cov_kwds.pop('use_t') if use_t is None: use_t = use_t_2 # TODO: warn or not? self.get_robustcov_results(cov_type=cov_type, use_self=True, use_t=use_t, **cov_kwds) for key in kwargs: setattr(self, key, kwargs[key]) def __str__(self): self.summary() def conf_int(self, alpha=.05, cols=None): """ Returns the confidence interval of the fitted parameters. Parameters ---------- alpha : float, optional The `alpha` level for the confidence interval. ie., The default `alpha` = .05 returns a 95% confidence interval. cols : array-like, optional `cols` specifies which confidence intervals to return Notes ----- The confidence interval is based on Student's t-distribution. """ # keep method for docstring for now ci = super(RegressionResults, self).conf_int(alpha=alpha, cols=cols) return ci @cache_readonly def nobs(self): return float(self.model.wexog.shape[0]) @cache_readonly def fittedvalues(self): return self.model.predict(self.params, self.model.exog) @cache_readonly def wresid(self): return self.model.wendog - self.model.predict(self.params, self.model.wexog) @cache_readonly def resid(self): return self.model.endog - self.model.predict(self.params, self.model.exog) #TODO: fix writable example @cache_writable() def scale(self): wresid = self.wresid return np.dot(wresid, wresid) / self.df_resid @cache_readonly def ssr(self): wresid = self.wresid return np.dot(wresid, wresid) @cache_readonly def centered_tss(self): model = self.model weights = getattr(model, 'weights', None) if weights is not None: return np.sum(weights*(model.endog - np.average(model.endog, weights=weights))**2) else: # this is probably broken for GLS centered_endog = model.wendog - model.wendog.mean() return np.dot(centered_endog, centered_endog) @cache_readonly def uncentered_tss(self): wendog = self.model.wendog return np.dot(wendog, wendog) @cache_readonly def ess(self): if self.k_constant: return self.centered_tss - self.ssr else: return self.uncentered_tss - self.ssr @cache_readonly def rsquared(self): if self.k_constant: return 1 - self.ssr/self.centered_tss else: return 1 - self.ssr/self.uncentered_tss @cache_readonly def rsquared_adj(self): return 1 - np.divide(self.nobs - self.k_constant, self.df_resid) * (1 - self.rsquared) @cache_readonly def mse_model(self): return self.ess/self.df_model @cache_readonly def mse_resid(self): return self.ssr/self.df_resid @cache_readonly def mse_total(self): if self.k_constant: return self.centered_tss / (self.df_resid + self.df_model) else: return self.uncentered_tss / (self.df_resid + self.df_model) @cache_readonly def fvalue(self): if hasattr(self, 'cov_type') and self.cov_type != 'nonrobust': # with heteroscedasticity or correlation robustness k_params = self.normalized_cov_params.shape[0] mat = np.eye(k_params) const_idx = self.model.data.const_idx # TODO: What if model includes implcit constant, e.g. all dummies but no constant regressor? # TODO: Restats as LM test by projecting orthogonalizing to constant? if self.model.data.k_constant == 1: # if constant is implicit, return nan see #2444 if const_idx is None: return np.nan idx = lrange(k_params) idx.pop(const_idx) mat = mat[idx] # remove constant ft = self.f_test(mat) # using backdoor to set another attribute that we already have self._cache['f_pvalue'] = ft.pvalue return ft.fvalue else: # for standard homoscedastic case return self.mse_model/self.mse_resid @cache_readonly def f_pvalue(self): return stats.f.sf(self.fvalue, self.df_model, self.df_resid) @cache_readonly def bse(self): return np.sqrt(np.diag(self.cov_params())) @cache_readonly def aic(self): return -2 * self.llf + 2 * (self.df_model + self.k_constant) @cache_readonly def bic(self): return (-2 * self.llf + np.log(self.nobs) * (self.df_model + self.k_constant)) @cache_readonly def eigenvals(self): """ Return eigenvalues sorted in decreasing order. """ if self._wexog_singular_values is not None: eigvals = self._wexog_singular_values ** 2 else: eigvals = np.linalg.linalg.eigvalsh(np.dot(self.model.wexog.T, self.model.wexog)) return np.sort(eigvals)[::-1] @cache_readonly def condition_number(self): """ Return condition number of exogenous matrix. Calculated as ratio of largest to smallest eigenvalue. """ eigvals = self.eigenvals return np.sqrt(eigvals[0]/eigvals[-1]) #TODO: make these properties reset bse def _HCCM(self, scale): H = np.dot(self.model.pinv_wexog, scale[:,None]*self.model.pinv_wexog.T) return H @cache_readonly def cov_HC0(self): """ See statsmodels.RegressionResults """ self.het_scale = self.wresid**2 cov_HC0 = self._HCCM(self.het_scale) return cov_HC0 @cache_readonly def cov_HC1(self): """ See statsmodels.RegressionResults """ self.het_scale = self.nobs/(self.df_resid)*(self.wresid**2) cov_HC1 = self._HCCM(self.het_scale) return cov_HC1 @cache_readonly def cov_HC2(self): """ See statsmodels.RegressionResults """ # probably could be optimized h = np.diag(chain_dot(self.model.wexog, self.normalized_cov_params, self.model.wexog.T)) self.het_scale = self.wresid**2/(1-h) cov_HC2 = self._HCCM(self.het_scale) return cov_HC2 @cache_readonly def cov_HC3(self): """ See statsmodels.RegressionResults """ h = np.diag(chain_dot(self.model.wexog, self.normalized_cov_params, self.model.wexog.T)) self.het_scale=(self.wresid/(1-h))**2 cov_HC3 = self._HCCM(self.het_scale) return cov_HC3 @cache_readonly def HC0_se(self): """ See statsmodels.RegressionResults """ return np.sqrt(np.diag(self.cov_HC0)) @cache_readonly def HC1_se(self): """ See statsmodels.RegressionResults """ return np.sqrt(np.diag(self.cov_HC1)) @cache_readonly def HC2_se(self): """ See statsmodels.RegressionResults """ return np.sqrt(np.diag(self.cov_HC2)) @cache_readonly def HC3_se(self): """ See statsmodels.RegressionResults """ return np.sqrt(np.diag(self.cov_HC3)) @cache_readonly def resid_pearson(self): """ Residuals, normalized to have unit variance. Returns ------- An array wresid/sqrt(scale) """ if not hasattr(self, 'resid'): raise ValueError('Method requires residuals.') eps = np.finfo(self.wresid.dtype).eps if np.sqrt(self.scale) < 10 * eps * self.model.endog.mean(): # don't divide if scale is zero close to numerical precision from warnings import warn warn("All residuals are 0, cannot compute normed residuals.", RuntimeWarning) return self.wresid else: return self.wresid / np.sqrt(self.scale) def _is_nested(self, restricted): """ Parameters ---------- restricted : Result instance The restricted model is assumed to be nested in the current model. The result instance of the restricted model is required to have two attributes, residual sum of squares, `ssr`, residual degrees of freedom, `df_resid`. Returns ------- nested : bool True if nested, otherwise false Notes ----- A most nests another model if the regressors in the smaller model are spanned by the regressors in the larger model and the regressand is identical. """ if self.model.nobs != restricted.model.nobs: return False full_rank = self.model.rank restricted_rank = restricted.model.rank if full_rank <= restricted_rank: return False restricted_exog = restricted.model.wexog full_wresid = self.wresid scores = restricted_exog * full_wresid[:,None] score_l2 = np.sqrt(np.mean(scores.mean(0) ** 2)) # TODO: Could be improved, and may fail depending on scale of regressors return np.allclose(score_l2,0) def compare_lm_test(self, restricted, demean=True, use_lr=False): """Use Lagrange Multiplier test to test whether restricted model is correct Parameters ---------- restricted : Result instance The restricted model is assumed to be nested in the current model. The result instance of the restricted model is required to have two attributes, residual sum of squares, `ssr`, residual degrees of freedom, `df_resid`. demean : bool Flag indicating whether the demean the scores based on the residuals from the restricted model. If True, the covariance of the scores are used and the LM test is identical to the large sample version of the LR test. Returns ------- lm_value : float test statistic, chi2 distributed p_value : float p-value of the test statistic df_diff : int degrees of freedom of the restriction, i.e. difference in df between models Notes ----- TODO: explain LM text """ import statsmodels.stats.sandwich_covariance as sw from numpy.linalg import inv if not self._is_nested(restricted): raise ValueError("Restricted model is not nested by full model.") wresid = restricted.wresid wexog = self.model.wexog scores = wexog * wresid[:,None] n = self.nobs df_full = self.df_resid df_restr = restricted.df_resid df_diff = (df_restr - df_full) s = scores.mean(axis=0) if use_lr: scores = wexog * self.wresid[:,None] demean = False if demean: scores = scores - scores.mean(0)[None,:] # Form matters here. If homoskedastics can be sigma^2 (X'X)^-1 # If Heteroskedastic then the form below is fine # If HAC then need to use HAC # If Cluster, shoudl use cluster cov_type = getattr(self, 'cov_type', 'nonrobust') if cov_type == 'nonrobust': sigma2 = np.mean(wresid**2) XpX = np.dot(wexog.T,wexog) / n Sinv = inv(sigma2 * XpX) elif cov_type in ('HC0', 'HC1', 'HC2', 'HC3'): Sinv = inv(np.dot(scores.T,scores) / n) elif cov_type == 'HAC': print("HAC") maxlags = self.cov_kwds['maxlags'] Sinv = inv(sw.S_hac_simple(scores, maxlags) / n) elif cov_type == 'cluster': #cluster robust standard errors groups = self.cov_kwds['groups'] # TODO: Might need demean option in S_crosssection by group? Sinv = inv(sw.S_crosssection(scores, groups)) else: raise ValueError('Only nonrobust, HC, HAC and cluster are ' + 'currently connected') lm_value = n * chain_dot(s,Sinv,s.T) p_value = stats.chi2.sf(lm_value, df_diff) return lm_value, p_value, df_diff def compare_f_test(self, restricted): """use F test to test whether restricted model is correct Parameters ---------- restricted : Result instance The restricted model is assumed to be nested in the current model. The result instance of the restricted model is required to have two attributes, residual sum of squares, `ssr`, residual degrees of freedom, `df_resid`. Returns ------- f_value : float test statistic, F distributed p_value : float p-value of the test statistic df_diff : int degrees of freedom of the restriction, i.e. difference in df between models Notes ----- See mailing list discussion October 17, This test compares the residual sum of squares of the two models. This is not a valid test, if there is unspecified heteroscedasticity or correlation. This method will issue a warning if this is detected but still return the results under the assumption of homoscedasticity and no autocorrelation (sphericity). """ has_robust1 = getattr(self, 'cov_type', 'nonrobust') != 'nonrobust' has_robust2 = (getattr(restricted, 'cov_type', 'nonrobust') != 'nonrobust') if has_robust1 or has_robust2: warnings.warn('F test for comparison is likely invalid with ' + 'robust covariance, proceeding anyway', InvalidTestWarning) ssr_full = self.ssr ssr_restr = restricted.ssr df_full = self.df_resid df_restr = restricted.df_resid df_diff = (df_restr - df_full) f_value = (ssr_restr - ssr_full) / df_diff / ssr_full * df_full p_value = stats.f.sf(f_value, df_diff, df_full) return f_value, p_value, df_diff def compare_lr_test(self, restricted, large_sample=False): """ Likelihood ratio test to test whether restricted model is correct Parameters ---------- restricted : Result instance The restricted model is assumed to be nested in the current model. The result instance of the restricted model is required to have two attributes, residual sum of squares, `ssr`, residual degrees of freedom, `df_resid`. large_sample : bool Flag indicating whether to use a heteroskedasticity robust version of the LR test, which is a modified LM test. Returns ------- lr_stat : float likelihood ratio, chisquare distributed with df_diff degrees of freedom p_value : float p-value of the test statistic df_diff : int degrees of freedom of the restriction, i.e. difference in df between models Notes ----- The exact likelihood ratio is valid for homoskedastic data, and is defined as .. math:: D=-2\\log\\left(\\frac{\\mathcal{L}_{null}} {\\mathcal{L}_{alternative}}\\right) where :math:`\mathcal{L}` is the likelihood of the model. With :math:`D` distributed as chisquare with df equal to difference in number of parameters or equivalently difference in residual degrees of freedom. The large sample version of the likelihood ratio is defined as .. math:: D=n s^{\\prime}S^{-1}s where :math:`s=n^{-1}\\sum_{i=1}^{n} s_{i}` .. math:: s_{i} = x_{i,alternative} \\epsilon_{i,null} is the average score of the model evaluated using the residuals from null model and the regressors from the alternative model and :math:`S` is the covariance of the scores, :math:`s_{i}`. The covariance of the scores is estimated using the same estimator as in the alternative model. This test compares the loglikelihood of the two models. This may not be a valid test, if there is unspecified heteroscedasticity or correlation. This method will issue a warning if this is detected but still return the results without taking unspecified heteroscedasticity or correlation into account. This test compares the loglikelihood of the two models. This may not be a valid test, if there is unspecified heteroscedasticity or correlation. This method will issue a warning if this is detected but still return the results without taking unspecified heteroscedasticity or correlation into account. is the average score of the model evaluated using the residuals from null model and the regressors from the alternative model and :math:`S` is the covariance of the scores, :math:`s_{i}`. The covariance of the scores is estimated using the same estimator as in the alternative model. TODO: put into separate function, needs tests """ # See mailing list discussion October 17, if large_sample: return self.compare_lm_test(restricted, use_lr=True) has_robust1 = (getattr(self, 'cov_type', 'nonrobust') != 'nonrobust') has_robust2 = (getattr(restricted, 'cov_type', 'nonrobust') != 'nonrobust') if has_robust1 or has_robust2: warnings.warn('Likelihood Ratio test is likely invalid with ' + 'robust covariance, proceeding anyway', InvalidTestWarning) llf_full = self.llf llf_restr = restricted.llf df_full = self.df_resid df_restr = restricted.df_resid lrdf = (df_restr - df_full) lrstat = -2*(llf_restr - llf_full) lr_pvalue = stats.chi2.sf(lrstat, lrdf) return lrstat, lr_pvalue, lrdf def get_robustcov_results(self, cov_type='HC1', use_t=None, **kwds): """create new results instance with robust covariance as default Parameters ---------- cov_type : string the type of robust sandwich estimator to use. see Notes below use_t : bool If true, then the t distribution is used for inference. If false, then the normal distribution is used. If `use_t` is None, then an appropriate default is used, which is `true` if the cov_type is nonrobust, and `false` in all other cases. kwds : depends on cov_type Required or optional arguments for robust covariance calculation. see Notes below Returns ------- results : results instance This method creates a new results instance with the requested robust covariance as the default covariance of the parameters. Inferential statistics like p-values and hypothesis tests will be based on this covariance matrix. Notes ----- The following covariance types and required or optional arguments are currently available: - 'fixed scale' and optional keyword argument 'scale' which uses a predefined scale estimate with default equal to one. - 'HC0', 'HC1', 'HC2', 'HC3' and no keyword arguments: heteroscedasticity robust covariance - 'HAC' and keywords - `maxlag` integer (required) : number of lags to use - `kernel` string (optional) : kernel, default is Bartlett - `use_correction` bool (optional) : If true, use small sample correction - 'cluster' and required keyword `groups`, integer group indicator - `groups` array_like, integer (required) : index of clusters or groups - `use_correction` bool (optional) : If True the sandwich covariance is calulated with a small sample correction. If False the the sandwich covariance is calulated without small sample correction. - `df_correction` bool (optional) If True (default), then the degrees of freedom for the inferential statistics and hypothesis tests, such as pvalues, f_pvalue, conf_int, and t_test and f_test, are based on the number of groups minus one instead of the total number of observations minus the number of explanatory variables. `df_resid` of the results instance is adjusted. If False, then `df_resid` of the results instance is not adjusted. - 'hac-groupsum' Driscoll and Kraay, heteroscedasticity and autocorrelation robust standard errors in panel data keywords - `time` array_like (required) : index of time periods - `maxlag` integer (required) : number of lags to use - `kernel` string (optional) : kernel, default is Bartlett - `use_correction` False or string in ['hac', 'cluster'] (optional) : If False the the sandwich covariance is calulated without small sample correction. If `use_correction = 'cluster'` (default), then the same small sample correction as in the case of 'covtype='cluster'' is used. - `df_correction` bool (optional) adjustment to df_resid, see cov_type 'cluster' above #TODO: we need more options here - 'hac-panel' heteroscedasticity and autocorrelation robust standard errors in panel data. The data needs to be sorted in this case, the time series for each panel unit or cluster need to be stacked. keywords - `time` array_like (required) : index of time periods - `maxlag` integer (required) : number of lags to use - `kernel` string (optional) : kernel, default is Bartlett - `use_correction` False or string in ['hac', 'cluster'] (optional) : If False the the sandwich covariance is calulated without small sample correction. - `df_correction` bool (optional) adjustment to df_resid, see cov_type 'cluster' above #TODO: we need more options here Reminder: `use_correction` in "nw-groupsum" and "nw-panel" is not bool, needs to be in [False, 'hac', 'cluster'] TODO: Currently there is no check for extra or misspelled keywords, except in the case of cov_type `HCx` """ import statsmodels.stats.sandwich_covariance as sw # TODO: make separate function that returns a robust cov plus info use_self = kwds.pop('use_self', False) if use_self: res = self else: res = self.__class__(self.model, self.params, normalized_cov_params=self.normalized_cov_params, scale=self.scale) res.cov_type = cov_type # use_t might already be defined by the class, and already set if use_t is None: use_t = self.use_t res.cov_kwds = {'use_t':use_t} # store for information res.use_t = use_t adjust_df = False if cov_type in ['cluster', 'nw-panel', 'nw-groupsum']: df_correction = kwds.get('df_correction', None) # TODO: check also use_correction, do I need all combinations? if df_correction is not False: # i.e. in [None, True]: # user didn't explicitely set it to False adjust_df = True res.cov_kwds['adjust_df'] = adjust_df # verify and set kwds, and calculate cov # TODO: this should be outsourced in a function so we can reuse it in # other models # TODO: make it DRYer repeated code for checking kwds if cov_type in ['fixed scale', 'fixed_scale']: res.cov_kwds['description'] = ('Standard Errors are based on ' + 'fixed scale') res.cov_kwds['scale'] = scale = kwds.get('scale', 1.) res.cov_params_default = scale * res.normalized_cov_params elif cov_type in ('HC0', 'HC1', 'HC2', 'HC3'): if kwds: raise ValueError('heteroscedasticity robust covarians ' + 'does not use keywords') res.cov_kwds['description'] = ('Standard Errors are heteroscedasticity ' + 'robust ' + '(' + cov_type + ')') # TODO cannot access cov without calling se first getattr(self, cov_type.upper() + '_se') res.cov_params_default = getattr(self, 'cov_' + cov_type.upper()) elif cov_type == 'HAC': maxlags = kwds['maxlags'] # required?, default in cov_hac_simple res.cov_kwds['maxlags'] = maxlags use_correction = kwds.get('use_correction', False) res.cov_kwds['use_correction'] = use_correction res.cov_kwds['description'] = ('Standard Errors are heteroscedasticity ' + 'and autocorrelation robust (HAC) using %d lags and %s small ' + 'sample correction') % (maxlags, ['without', 'with'][use_correction]) res.cov_params_default = sw.cov_hac_simple(self, nlags=maxlags, use_correction=use_correction) elif cov_type == 'cluster': #cluster robust standard errors, one- or two-way groups = kwds['groups'] if not hasattr(groups, 'shape'): groups = np.asarray(groups).T if groups.ndim >= 2: groups = groups.squeeze() res.cov_kwds['groups'] = groups use_correction = kwds.get('use_correction', True) res.cov_kwds['use_correction'] = use_correction if groups.ndim == 1: if adjust_df: # need to find number of groups # duplicate work self.n_groups = n_groups = len(np.unique(groups)) res.cov_params_default = sw.cov_cluster(self, groups, use_correction=use_correction) elif groups.ndim == 2: if hasattr(groups, 'values'): groups = groups.values if adjust_df: # need to find number of groups # duplicate work n_groups0 = len(np.unique(groups[:,0])) n_groups1 = len(np.unique(groups[:, 1])) self.n_groups = (n_groups0, n_groups1) n_groups = min(n_groups0, n_groups1) # use for adjust_df # Note: sw.cov_cluster_2groups has 3 returns res.cov_params_default = sw.cov_cluster_2groups(self, groups, use_correction=use_correction)[0] else: raise ValueError('only two groups are supported') res.cov_kwds['description'] = ('Standard Errors are robust to' + 'cluster correlation ' + '(' + cov_type + ')') elif cov_type == 'nw-panel': #cluster robust standard errors res.cov_kwds['time'] = time = kwds['time'] #TODO: nlags is currently required #nlags = kwds.get('nlags', True) #res.cov_kwds['nlags'] = nlags #TODO: `nlags` or `maxlags` res.cov_kwds['maxlags'] = maxlags = kwds['maxlags'] use_correction = kwds.get('use_correction', 'hac') res.cov_kwds['use_correction'] = use_correction weights_func = kwds.get('weights_func', sw.weights_bartlett) res.cov_kwds['weights_func'] = weights_func # TODO: clumsy time index in cov_nw_panel tt = (np.nonzero(np.diff(time) < 0)[0] + 1).tolist() groupidx = lzip([0] + tt, tt + [len(time)]) self.n_groups = n_groups = len(groupidx) res.cov_params_default = sw.cov_nw_panel(self, maxlags, groupidx, weights_func=weights_func, use_correction=use_correction) res.cov_kwds['description'] = ('Standard Errors are robust to' + 'cluster correlation ' + '(' + cov_type + ')') elif cov_type == 'nw-groupsum': # Driscoll-Kraay standard errors res.cov_kwds['time'] = time = kwds['time'] #TODO: nlags is currently required #nlags = kwds.get('nlags', True) #res.cov_kwds['nlags'] = nlags #TODO: `nlags` or `maxlags` res.cov_kwds['maxlags'] = maxlags = kwds['maxlags'] use_correction = kwds.get('use_correction', 'cluster') res.cov_kwds['use_correction'] = use_correction weights_func = kwds.get('weights_func', sw.weights_bartlett) res.cov_kwds['weights_func'] = weights_func if adjust_df: # need to find number of groups tt = (np.nonzero(np.diff(time) < 0)[0] + 1) self.n_groups = n_groups = len(tt) + 1 res.cov_params_default = sw.cov_nw_groupsum(self, maxlags, time, weights_func=weights_func, use_correction=use_correction) res.cov_kwds['description'] = ( 'Driscoll and Kraay Standard Errors are robust to ' + 'cluster correlation ' + '(' + cov_type + ')') else: raise ValueError('cov_type not recognized. See docstring for ' + 'available options and spelling') if adjust_df: # Note: df_resid is used for scale and others, add new attribute res.df_resid_inference = n_groups - 1 return res def get_prediction(self, exog=None, transform=True, weights=None, row_labels=None, **kwds): return pred.get_prediction(self, exog=exog, transform=transform, weights=weights, row_labels=row_labels, **kwds) get_prediction.__doc__ = pred.get_prediction.__doc__ def summary(self, yname=None, xname=None, title=None, alpha=.05): """Summarize the Regression Results Parameters ----------- yname : string, optional Default is `y` xname : list of strings, optional Default is `var_##` for ## in p the number of regressors title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ #TODO: import where we need it (for now), add as cached attributes from statsmodels.stats.stattools import (jarque_bera, omni_normtest, durbin_watson) jb, jbpv, skew, kurtosis = jarque_bera(self.wresid) omni, omnipv = omni_normtest(self.wresid) eigvals = self.eigenvals condno = self.condition_number self.diagn = dict(jb=jb, jbpv=jbpv, skew=skew, kurtosis=kurtosis, omni=omni, omnipv=omnipv, condno=condno, mineigval=eigvals[-1]) #TODO not used yet #diagn_left_header = ['Models stats'] #diagn_right_header = ['Residual stats'] #TODO: requiring list/iterable is a bit annoying #need more control over formatting #TODO: default don't work if it's not identically spelled top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['Least Squares']), ('Date:', None), ('Time:', None), ('No. Observations:', None), ('Df Residuals:', None), #[self.df_resid]), #TODO: spelling ('Df Model:', None), #[self.df_model]) ] if hasattr(self, 'cov_type'): top_left.append(('Covariance Type:', [self.cov_type])) top_right = [('R-squared:', ["%#8.3f" % self.rsquared]), ('Adj. R-squared:', ["%#8.3f" % self.rsquared_adj]), ('F-statistic:', ["%#8.4g" % self.fvalue] ), ('Prob (F-statistic):', ["%#6.3g" % self.f_pvalue]), ('Log-Likelihood:', None), #["%#6.4g" % self.llf]), ('AIC:', ["%#8.4g" % self.aic]), ('BIC:', ["%#8.4g" % self.bic]) ] diagn_left = [('Omnibus:', ["%#6.3f" % omni]), ('Prob(Omnibus):', ["%#6.3f" % omnipv]), ('Skew:', ["%#6.3f" % skew]), ('Kurtosis:', ["%#6.3f" % kurtosis]) ] diagn_right = [('Durbin-Watson:', ["%#8.3f" % durbin_watson(self.wresid)]), ('Jarque-Bera (JB):', ["%#8.3f" % jb]), ('Prob(JB):', ["%#8.3g" % jbpv]), ('Cond. No.', ["%#8.3g" % condno]) ] if title is None: title = self.model.__class__.__name__ + ' ' + "Regression Results" #create summary table instance from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, yname=yname, xname=xname, title="") #add warnings/notes, added to text format only etext =[] if hasattr(self, 'cov_type'): etext.append(self.cov_kwds['description']) if self.model.exog.shape[0] < self.model.exog.shape[1]: wstr = "The input rank is higher than the number of observations." etext.append(wstr) if eigvals[-1] < 1e-10: wstr = "The smallest eigenvalue is %6.3g. This might indicate " wstr += "that there are\n" wstr += "strong multicollinearity problems or that the design " wstr += "matrix is singular." wstr = wstr % eigvals[-1] etext.append(wstr) elif condno > 1000: #TODO: what is recommended wstr = "The condition number is large, %6.3g. This might " wstr += "indicate that there are\n" wstr += "strong multicollinearity or other numerical " wstr += "problems." wstr = wstr % condno etext.append(wstr) if etext: etext = ["[{0}] {1}".format(i + 1, text) for i, text in enumerate(etext)] etext.insert(0, "Warnings:") smry.add_extra_txt(etext) return smry #top = summary_top(self, gleft=topleft, gright=diagn_left, #[], # yname=yname, xname=xname, # title=self.model.__class__.__name__ + ' ' + # "Regression Results") #par = summary_params(self, yname=yname, xname=xname, alpha=.05, # use_t=False) # #diagn = summary_top(self, gleft=diagn_left, gright=diagn_right, # yname=yname, xname=xname, # title="Linear Model") # #return summary_return([top, par, diagn], return_fmt=return_fmt) def summary2(self, yname=None, xname=None, title=None, alpha=.05, float_format="%.4f"): """Experimental summary function to summarize the regression results Parameters ----------- xname : List of strings of length equal to the number of parameters Names of the independent variables (optional) yname : string Name of the dependent variable (optional) title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals float_format: string print format for floats in parameters summary Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ # Diagnostics from statsmodels.stats.stattools import (jarque_bera, omni_normtest, durbin_watson) from statsmodels.compat.collections import OrderedDict jb, jbpv, skew, kurtosis = jarque_bera(self.wresid) omni, omnipv = omni_normtest(self.wresid) dw = durbin_watson(self.wresid) eigvals = self.eigenvals condno = self.condition_number eigvals = np.sort(eigvals) #in increasing order diagnostic = OrderedDict([ ('Omnibus:', "%.3f" % omni), ('Prob(Omnibus):', "%.3f" % omnipv), ('Skew:', "%.3f" % skew), ('Kurtosis:', "%.3f" % kurtosis), ('Durbin-Watson:', "%.3f" % dw), ('Jarque-Bera (JB):', "%.3f" % jb), ('Prob(JB):', "%.3f" % jbpv), ('Condition No.:', "%.0f" % condno) ]) # Summary from statsmodels.iolib import summary2 smry = summary2.Summary() smry.add_base(results=self, alpha=alpha, float_format=float_format, xname=xname, yname=yname, title=title) smry.add_dict(diagnostic) # Warnings if eigvals[-1] < 1e-10: warn = "The smallest eigenvalue is %6.3g. This might indicate that\ there are strong multicollinearity problems or that the design\ matrix is singular." % eigvals[-1] smry.add_text(warn) if condno > 1000: warn = "* The condition number is large (%.g). This might indicate \ strong multicollinearity or other numerical problems." % condno smry.add_text(warn) return smry class OLSResults(RegressionResults): """ Results class for for an OLS model. Most of the methods and attributes are inherited from RegressionResults. The special methods that are only available for OLS are: - get_influence - outlier_test - el_test - conf_int_el See Also -------- RegressionResults """ def get_influence(self): """ get an instance of Influence with influence and outlier measures Returns ------- infl : Influence instance the instance has methods to calculate the main influence and outlier measures for the OLS regression See also -------- :class:`statsmodels.stats.outliers_influence.OLSInfluence` """ from statsmodels.stats.outliers_influence import OLSInfluence return OLSInfluence(self) def outlier_test(self, method='bonf', alpha=.05): """ Test observations for outliers according to method Parameters ---------- method : str - `bonferroni` : one-step correction - `sidak` : one-step correction - `holm-sidak` : - `holm` : - `simes-hochberg` : - `hommel` : - `fdr_bh` : Benjamini/Hochberg - `fdr_by` : Benjamini/Yekutieli See `statsmodels.stats.multitest.multipletests` for details. alpha : float familywise error rate Returns ------- table : ndarray or DataFrame Returns either an ndarray or a DataFrame if labels is not None. Will attempt to get labels from model_results if available. The columns are the Studentized residuals, the unadjusted p-value, and the corrected p-value according to method. Notes ----- The unadjusted p-value is stats.t.sf(abs(resid), df) where df = df_resid - 1. """ from statsmodels.stats.outliers_influence import outlier_test return outlier_test(self, method, alpha) def el_test(self, b0_vals, param_nums, return_weights=0, ret_params=0, method='nm', stochastic_exog=1, return_params=0): """ Tests single or joint hypotheses of the regression parameters using Empirical Likelihood. Parameters ---------- b0_vals : 1darray The hypothesized value of the parameter to be tested param_nums : 1darray The parameter number to be tested print_weights : bool If true, returns the weights that optimize the likelihood ratio at b0_vals. Default is False ret_params : bool If true, returns the parameter vector that maximizes the likelihood ratio at b0_vals. Also returns the weights. Default is False method : string Can either be 'nm' for Nelder-Mead or 'powell' for Powell. The optimization method that optimizes over nuisance parameters. Default is 'nm' stochastic_exog : bool When TRUE, the exogenous variables are assumed to be stochastic. When the regressors are nonstochastic, moment conditions are placed on the exogenous variables. Confidence intervals for stochastic regressors are at least as large as non-stochastic regressors. Default = TRUE Returns ------- res : tuple The p-value and -2 times the log-likelihood ratio for the hypothesized values. Examples -------- >>> import statsmodels.api as sm >>> data = sm.datasets.stackloss.load() >>> endog = data.endog >>> exog = sm.add_constant(data.exog) >>> model = sm.OLS(endog, exog) >>> fitted = model.fit() >>> fitted.params >>> array([-39.91967442, 0.7156402 , 1.29528612, -0.15212252]) >>> fitted.rsquared >>> 0.91357690446068196 >>> # Test that the slope on the first variable is 0 >>> fitted.test_beta([0], [1]) >>> (1.7894660442330235e-07, 27.248146353709153) """ params = np.copy(self.params) opt_fun_inst = _ELRegOpts() # to store weights if len(param_nums) == len(params): llr = opt_fun_inst._opt_nuis_regress([], param_nums=param_nums, endog=self.model.endog, exog=self.model.exog, nobs=self.model.nobs, nvar=self.model.exog.shape[1], params=params, b0_vals=b0_vals, stochastic_exog=stochastic_exog) pval = 1 - stats.chi2.cdf(llr, len(param_nums)) if return_weights: return llr, pval, opt_fun_inst.new_weights else: return llr, pval x0 = np.delete(params, param_nums) args = (param_nums, self.model.endog, self.model.exog, self.model.nobs, self.model.exog.shape[1], params, b0_vals, stochastic_exog) if method == 'nm': llr = optimize.fmin(opt_fun_inst._opt_nuis_regress, x0, maxfun=10000, maxiter=10000, full_output=1, disp=0, args=args)[1] if method == 'powell': llr = optimize.fmin_powell(opt_fun_inst._opt_nuis_regress, x0, full_output=1, disp=0, args=args)[1] pval = 1 - stats.chi2.cdf(llr, len(param_nums)) if ret_params: return llr, pval, opt_fun_inst.new_weights, opt_fun_inst.new_params elif return_weights: return llr, pval, opt_fun_inst.new_weights else: return llr, pval def conf_int_el(self, param_num, sig=.05, upper_bound=None, lower_bound=None, method='nm', stochastic_exog=1): """ Computes the confidence interval for the parameter given by param_num using Empirical Likelihood Parameters ---------- param_num : float The parameter for which the confidence interval is desired sig : float The significance level. Default is .05 upper_bound : float The maximum value the upper limit can be. Default is the 99.9% confidence value under OLS assumptions. lower_bound : float The minimum value the lower limit can be. Default is the 99.9% confidence value under OLS assumptions. method : string Can either be 'nm' for Nelder-Mead or 'powell' for Powell. The optimization method that optimizes over nuisance parameters. Default is 'nm' Returns ------- ci : tuple The confidence interval See Also -------- el_test Notes ----- This function uses brentq to find the value of beta where test_beta([beta], param_num)[1] is equal to the critical value. The function returns the results of each iteration of brentq at each value of beta. The current function value of the last printed optimization should be the critical value at the desired significance level. For alpha=.05, the value is 3.841459. To ensure optimization terminated successfully, it is suggested to do el_test([lower_limit], [param_num]) If the optimization does not terminate successfully, consider switching optimization algorithms. If optimization is still not successful, try changing the values of start_int_params. If the current function value repeatedly jumps from a number between 0 and the critical value and a very large number (>50), the starting parameters of the interior minimization need to be changed. """ r0 = stats.chi2.ppf(1 - sig, 1) if upper_bound is None: upper_bound = self.conf_int(.01)[param_num][1] if lower_bound is None: lower_bound = self.conf_int(.01)[param_num][0] f = lambda b0: self.el_test(np.array([b0]), np.array([param_num]), method=method, stochastic_exog=stochastic_exog)[0]-r0 lowerl = optimize.brenth(f, lower_bound, self.params[param_num]) upperl = optimize.brenth(f, self.params[param_num], upper_bound) # ^ Seems to be faster than brentq in most cases return (lowerl, upperl) class RegressionResultsWrapper(wrap.ResultsWrapper): _attrs = { 'chisq' : 'columns', 'sresid' : 'rows', 'weights' : 'rows', 'wresid' : 'rows', 'bcov_unscaled' : 'cov', 'bcov_scaled' : 'cov', 'HC0_se' : 'columns', 'HC1_se' : 'columns', 'HC2_se' : 'columns', 'HC3_se' : 'columns', 'norm_resid' : 'rows', } _wrap_attrs = wrap.union_dicts(base.LikelihoodResultsWrapper._attrs, _attrs) _methods = {} _wrap_methods = wrap.union_dicts( base.LikelihoodResultsWrapper._wrap_methods, _methods) wrap.populate_wrapper(RegressionResultsWrapper, RegressionResults) if __name__ == "__main__": import statsmodels.api as sm data = sm.datasets.longley.load() data.exog = add_constant(data.exog, prepend=False) ols_results = OLS(data.endog, data.exog).fit() #results gls_results = GLS(data.endog, data.exog).fit() #results print(ols_results.summary()) tables = ols_results.summary(returns='tables') csv = ols_results.summary(returns='csv') """ Summary of Regression Results ======================================= | Dependent Variable: ['y']| | Model: OLS| | Method: Least Squares| | Date: Tue, 29 Jun 2010| | Time: 22:32:21| | # obs: 16.0| | Df residuals: 9.0| | Df model: 6.0| =========================================================================== | coefficient std. error t-statistic prob.| --------------------------------------------------------------------------- | x1 15.0619 84.9149 0.1774 0.8631| | x2 -0.0358 0.0335 -1.0695 0.3127| | x3 -2.0202 0.4884 -4.1364 0.002535| | x4 -1.0332 0.2143 -4.8220 0.0009444| | x5 -0.0511 0.2261 -0.2261 0.8262| | x6 1829.1515 455.4785 4.0159 0.003037| | const -3482258.6346 890420.3836 -3.9108 0.003560| =========================================================================== | Models stats Residual stats | --------------------------------------------------------------------------- | R-squared: 0.995479 Durbin-Watson: 2.55949 | | Adjusted R-squared: 0.992465 Omnibus: 0.748615 | | F-statistic: 330.285 Prob(Omnibus): 0.687765 | | Prob (F-statistic): 4.98403e-10 JB: 0.352773 | | Log likelihood: -109.617 Prob(JB): 0.838294 | | AIC criterion: 233.235 Skew: 0.419984 | | BIC criterion: 238.643 Kurtosis: 2.43373 | --------------------------------------------------------------------------- """
bsd-3-clause
Lucas-Armand/genetic-algorithm
dev/8ºSemana/testes of speed.py
5
3255
# -*- coding: utf-8 -*- import os import csv import random import numpy as np import timeit import time as Time import matplotlib.pyplot as plt import matplotlib.animation as animation from itertools import product, combinations class Block: def __init__(self,point,a,b,c,weight,btype): self.p=point self.a=a self.b=b self.c=c self.w=weight self.t=btype def csv_read(name): #Metodo de leitura, transforma um arquivo CSV em um vetor CSV=open(name,'r') dados=CSV.read() dados=dados.replace(',','.') dados=dados.replace(';',',') CSV.close() CSV=open("temp.csv",'w') CSV.write(dados) CSV.close() CSV=open("temp.csv",'r') dados=csv.reader(CSV) v=[] for i in dados: I=[] for j in i: try: j = float(j) except: pass I.append(j) v.append(I) CSV.close() os.remove("temp.csv") return (v) def defineGeometry(name): vect = csv_read(name) blockNumber ={} for i in vect: a = i[1] b = i[2] c = i[3] point = [i[4],i[5],i[6]] weight = i[7] btype = i[-1] block = Block(point,a,b,c,weight,btype) blockNumber[i[0]] = block return blockNumber bNumb=defineGeometry('GeometriaNavio.csv') # Define vicinity #deck vicinity={1:[2]} for i in range(2,16): vicinity[i] = [i-1,i+1] vicinity[16] = [15] #side vicinity[17] = [18,19] vicinity[18] = [17,20] for i in range(19,31): v = i-1 if i%2==0 else i+1 vicinity[i] = [v,i-2,i+2] vicinity[31] = [29,32] vicinity[32] = [30,31] #bott vicinity[33] = [34,35] vicinity[34] = [33,36] for i in range(35,63): v = i-1 if i%2==0 else i+1 vicinity[i] = [v,i-2,i+2] vicinity[63] = [61,64] vicinity[64] = [63,62] #coff vicinity[65] = [66] for i in range(66,70): vicinity[i] = [i-1,i+1] vicinity[70] = [69] alfa = 10 beta = 1 built = [] time = 0 append = built.append def order(x): return vicinity[x] def time(bNumb,vicinity,chromo): t_time = Time.time() alfa = 1 built = [] time = 0 append = built.append def time_vector(x,y): for i in y: if i in built: time = alfa break try:time except: time = 10*alfa append(x) return time vic = [vicinity[x] for x in chromo] time = sum((time_vector(x,y) for x,y in zip(chromo,vic))) return time chromo = [44, 39, 56, 47, 49, 37, 42, 46, 51, 58, 60, 62, 52, 41, 35, 33, 50, 61, 54, 34, 59, 43, 48, 45, 55, 53, 38, 57, 64, 67, 68, 63, 40, 36, 21, 66, 22, 6, 20, 65, 18, 5, 17, 69, 28, 27, 70, 29, 1, 12, 30, 13, 14, 26, 31, 24, 19, 2, 3, 4, 25, 11, 32, 10, 15, 16, 9, 23, 7, 8] import cProfile cProfile.run('time(bNumb,vicinity,chromo)') ## ##print timeit.timeit(setup='from __main__ import chromo;'+ ## 'from __main__ import bNumb;'+ ## 'from __main__ import time;'+ ## 'from __main__ import vicinity ' ## ,stmt='time(bNumb,vicinity,chromo)') #print t.timeit(number = 1000000)
gpl-3.0
jchodera/assaytools
scripts/xml2png4scans.py
2
5177
# This script takes xml data file output from the Tecan Infinite m1000 Pro plate reader # and makes quick and dirty images of the raw data. #But with scans and not just singlet reads. # The same procedure can be used to make matrices suitable for analysis using # matrix = dataframe.values # Made by Sonya Hanson, with some help from things that worked in xml2png.py # Friday, June 20,2014 # Usage: python xml2png4scans.py *.xml ############ For future to combine with xml2png.py # # for i, sect in enumerate(Sections): # reads = sect.xpath("*/Well") # parameters = root.xpath(path)[0] # if reads[0].attrib['Type'] == "Scan": # ############## import numpy as np import matplotlib.pyplot as plt from lxml import etree import pandas as pd import matplotlib.cm as cm import seaborn import sys import os # Define extract function that extracts parameters def extract(taglist): result = [] for p in taglist: print "Attempting to extract tag '%s'..." % p try: param = parameters.xpath("*[@Name='" + p + "']")[0] result.append( p + '=' + param.attrib['Value']) except: # tag not found result.append(None) return result def process_files(xml_files): so_many = len(xml_files) print "****This script is about to make png files for %s xml files. ****" % so_many for file in xml_files: # Parse XML file. root = etree.parse(file) # Remove extension from xml filename. file_name = os.path.splitext(file)[0] # Extract plate type and barcode. plate = root.xpath("/*/Header/Parameters/Parameter[@Name='Plate']")[0] plate_type = plate.attrib['Value'] bar = root.xpath("/*/Plate/BC")[0] barcode = bar.text # Define Sections. Sections = root.xpath("/*/Section") much = len(Sections) print "****The xml file " + file + " has %s data sections:****" % much for sect in Sections: print sect.attrib['Name'] data = [] for i, sect in enumerate(Sections): # Extract Parameters for this section. path = "/*/Section[@Name='" + sect.attrib['Name'] + "']/Parameters" parameters = root.xpath(path)[0] # Parameters are extracted slightly differently depending on Absorbance or Fluorescence read. if parameters[0].attrib['Value'] == "Absorbance": result = extract(["Mode", "Wavelength Start", "Wavelength End", "Wavelength Step Size"]) title = '%s, %s, %s, %s' % tuple(result) else: result = extract(["Gain", "Excitation Wavelength", "Emission Wavelength", "Part of Plate", "Mode"]) title = '%s, %s, %s, \n %s, %s' % tuple(result) print "****The %sth section has the parameters:****" %i print title # Extract Reads for this section. Sections = root.xpath("/*/Section") reads = root.xpath("/*/*/*/Well") wellIDs = [read.attrib['Pos'] for read in reads] data = [(float(s.text), float(s.attrib['WL']), r.attrib['Pos']) for r in reads for s in r] dataframe = pd.DataFrame(data, columns=['fluorescence','wavelength (nm)','Well']) dataframe_pivot = pd.pivot_table(dataframe, index = 'wavelength (nm)', columns = ['Well']) # Make plot, complete with separate png for each section. section_name = sect.attrib['Name'] fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(12, 12)) for i in range(1,12): dataframe_pivot.fluorescence.get('A' + str(i)).plot(ax=axes[0,0], title='A', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('B' + str(i)).plot(ax=axes[0,1], title='B', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('C' + str(i)).plot(ax=axes[0,2], title='C', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('D' + str(i)).plot(ax=axes[1,0], title='D', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('E' + str(i)).plot(ax=axes[1,1], title='E', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('F' + str(i)).plot(ax=axes[1,2], title='F', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('G' + str(i)).plot(ax=axes[2,0], title='G', c=cm.hsv(i*15)) for i in range(1,12): dataframe_pivot.fluorescence.get('H' + str(i)).plot(ax=axes[2,1], title='H', c=cm.hsv(i*15)) fig.suptitle('%s \n %s \n Barcode = %s' % (title, plate_type, barcode), fontsize=14) fig.subplots_adjust(hspace=0.3) plt.savefig('%s_%s.png' % (file_name, section_name)) return def entry_point(): xml_files = sys.argv[1:] process_files(xml_files) if __name__ == '__main__': xml_files = sys.argv[1:] process_files(xml_files)
lgpl-2.1
kazemakase/scikit-learn
examples/plot_multilabel.py
87
4279
# Authors: Vlad Niculae, Mathieu Blondel # License: BSD 3 clause """ ========================= Multilabel classification ========================= This example simulates a multi-label document classification problem. The dataset is generated randomly based on the following process: - pick the number of labels: n ~ Poisson(n_labels) - n times, choose a class c: c ~ Multinomial(theta) - pick the document length: k ~ Poisson(length) - k times, choose a word: w ~ Multinomial(theta_c) In the above process, rejection sampling is used to make sure that n is more than 2, and that the document length is never zero. Likewise, we reject classes which have already been chosen. The documents that are assigned to both classes are plotted surrounded by two colored circles. The classification is performed by projecting to the first two principal components found by PCA and CCA for visualisation purposes, followed by using the :class:`sklearn.multiclass.OneVsRestClassifier` metaclassifier using two SVCs with linear kernels to learn a discriminative model for each class. Note that PCA is used to perform an unsupervised dimensionality reduction, while CCA is used to perform a supervised one. Note: in the plot, "unlabeled samples" does not mean that we don't know the labels (as in semi-supervised learning) but that the samples simply do *not* have a label. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_multilabel_classification from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import SVC from sklearn.preprocessing import LabelBinarizer from sklearn.decomposition import PCA from sklearn.cross_decomposition import CCA def plot_hyperplane(clf, min_x, max_x, linestyle, label): # get the separating hyperplane w = clf.coef_[0] a = -w[0] / w[1] xx = np.linspace(min_x - 5, max_x + 5) # make sure the line is long enough yy = a * xx - (clf.intercept_[0]) / w[1] plt.plot(xx, yy, linestyle, label=label) def plot_subfigure(X, Y, subplot, title, transform): if transform == "pca": X = PCA(n_components=2).fit_transform(X) elif transform == "cca": X = CCA(n_components=2).fit(X, Y).transform(X) else: raise ValueError min_x = np.min(X[:, 0]) max_x = np.max(X[:, 0]) min_y = np.min(X[:, 1]) max_y = np.max(X[:, 1]) classif = OneVsRestClassifier(SVC(kernel='linear')) classif.fit(X, Y) plt.subplot(2, 2, subplot) plt.title(title) zero_class = np.where(Y[:, 0]) one_class = np.where(Y[:, 1]) plt.scatter(X[:, 0], X[:, 1], s=40, c='gray') plt.scatter(X[zero_class, 0], X[zero_class, 1], s=160, edgecolors='b', facecolors='none', linewidths=2, label='Class 1') plt.scatter(X[one_class, 0], X[one_class, 1], s=80, edgecolors='orange', facecolors='none', linewidths=2, label='Class 2') plot_hyperplane(classif.estimators_[0], min_x, max_x, 'k--', 'Boundary\nfor class 1') plot_hyperplane(classif.estimators_[1], min_x, max_x, 'k-.', 'Boundary\nfor class 2') plt.xticks(()) plt.yticks(()) plt.xlim(min_x - .5 * max_x, max_x + .5 * max_x) plt.ylim(min_y - .5 * max_y, max_y + .5 * max_y) if subplot == 2: plt.xlabel('First principal component') plt.ylabel('Second principal component') plt.legend(loc="upper left") plt.figure(figsize=(8, 6)) X, Y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=True, return_indicator=True, random_state=1) plot_subfigure(X, Y, 1, "With unlabeled samples + CCA", "cca") plot_subfigure(X, Y, 2, "With unlabeled samples + PCA", "pca") X, Y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=False, return_indicator=True, random_state=1) plot_subfigure(X, Y, 3, "Without unlabeled samples + CCA", "cca") plot_subfigure(X, Y, 4, "Without unlabeled samples + PCA", "pca") plt.subplots_adjust(.04, .02, .97, .94, .09, .2) plt.show()
bsd-3-clause
andaag/scikit-learn
examples/plot_johnson_lindenstrauss_bound.py
134
7452
""" ===================================================================== The Johnson-Lindenstrauss bound for embedding with random projections ===================================================================== The `Johnson-Lindenstrauss lemma`_ states that any high dimensional dataset can be randomly projected into a lower dimensional Euclidean space while controlling the distortion in the pairwise distances. .. _`Johnson-Lindenstrauss lemma`: http://en.wikipedia.org/wiki/Johnson%E2%80%93Lindenstrauss_lemma Theoretical bounds ================== The distortion introduced by a random projection `p` is asserted by the fact that `p` is defining an eps-embedding with good probability as defined by: (1 - eps) ||u - v||^2 < ||p(u) - p(v)||^2 < (1 + eps) ||u - v||^2 Where u and v are any rows taken from a dataset of shape [n_samples, n_features] and p is a projection by a random Gaussian N(0, 1) matrix with shape [n_components, n_features] (or a sparse Achlioptas matrix). The minimum number of components to guarantees the eps-embedding is given by: n_components >= 4 log(n_samples) / (eps^2 / 2 - eps^3 / 3) The first plot shows that with an increasing number of samples ``n_samples``, the minimal number of dimensions ``n_components`` increased logarithmically in order to guarantee an ``eps``-embedding. The second plot shows that an increase of the admissible distortion ``eps`` allows to reduce drastically the minimal number of dimensions ``n_components`` for a given number of samples ``n_samples`` Empirical validation ==================== We validate the above bounds on the the digits dataset or on the 20 newsgroups text document (TF-IDF word frequencies) dataset: - for the digits dataset, some 8x8 gray level pixels data for 500 handwritten digits pictures are randomly projected to spaces for various larger number of dimensions ``n_components``. - for the 20 newsgroups dataset some 500 documents with 100k features in total are projected using a sparse random matrix to smaller euclidean spaces with various values for the target number of dimensions ``n_components``. The default dataset is the digits dataset. To run the example on the twenty newsgroups dataset, pass the --twenty-newsgroups command line argument to this script. For each value of ``n_components``, we plot: - 2D distribution of sample pairs with pairwise distances in original and projected spaces as x and y axis respectively. - 1D histogram of the ratio of those distances (projected / original). We can see that for low values of ``n_components`` the distribution is wide with many distorted pairs and a skewed distribution (due to the hard limit of zero ratio on the left as distances are always positives) while for larger values of n_components the distortion is controlled and the distances are well preserved by the random projection. Remarks ======= According to the JL lemma, projecting 500 samples without too much distortion will require at least several thousands dimensions, irrespective of the number of features of the original dataset. Hence using random projections on the digits dataset which only has 64 features in the input space does not make sense: it does not allow for dimensionality reduction in this case. On the twenty newsgroups on the other hand the dimensionality can be decreased from 56436 down to 10000 while reasonably preserving pairwise distances. """ print(__doc__) import sys from time import time import numpy as np import matplotlib.pyplot as plt from sklearn.random_projection import johnson_lindenstrauss_min_dim from sklearn.random_projection import SparseRandomProjection from sklearn.datasets import fetch_20newsgroups_vectorized from sklearn.datasets import load_digits from sklearn.metrics.pairwise import euclidean_distances # Part 1: plot the theoretical dependency between n_components_min and # n_samples # range of admissible distortions eps_range = np.linspace(0.1, 0.99, 5) colors = plt.cm.Blues(np.linspace(0.3, 1.0, len(eps_range))) # range of number of samples (observation) to embed n_samples_range = np.logspace(1, 9, 9) plt.figure() for eps, color in zip(eps_range, colors): min_n_components = johnson_lindenstrauss_min_dim(n_samples_range, eps=eps) plt.loglog(n_samples_range, min_n_components, color=color) plt.legend(["eps = %0.1f" % eps for eps in eps_range], loc="lower right") plt.xlabel("Number of observations to eps-embed") plt.ylabel("Minimum number of dimensions") plt.title("Johnson-Lindenstrauss bounds:\nn_samples vs n_components") # range of admissible distortions eps_range = np.linspace(0.01, 0.99, 100) # range of number of samples (observation) to embed n_samples_range = np.logspace(2, 6, 5) colors = plt.cm.Blues(np.linspace(0.3, 1.0, len(n_samples_range))) plt.figure() for n_samples, color in zip(n_samples_range, colors): min_n_components = johnson_lindenstrauss_min_dim(n_samples, eps=eps_range) plt.semilogy(eps_range, min_n_components, color=color) plt.legend(["n_samples = %d" % n for n in n_samples_range], loc="upper right") plt.xlabel("Distortion eps") plt.ylabel("Minimum number of dimensions") plt.title("Johnson-Lindenstrauss bounds:\nn_components vs eps") # Part 2: perform sparse random projection of some digits images which are # quite low dimensional and dense or documents of the 20 newsgroups dataset # which is both high dimensional and sparse if '--twenty-newsgroups' in sys.argv: # Need an internet connection hence not enabled by default data = fetch_20newsgroups_vectorized().data[:500] else: data = load_digits().data[:500] n_samples, n_features = data.shape print("Embedding %d samples with dim %d using various random projections" % (n_samples, n_features)) n_components_range = np.array([300, 1000, 10000]) dists = euclidean_distances(data, squared=True).ravel() # select only non-identical samples pairs nonzero = dists != 0 dists = dists[nonzero] for n_components in n_components_range: t0 = time() rp = SparseRandomProjection(n_components=n_components) projected_data = rp.fit_transform(data) print("Projected %d samples from %d to %d in %0.3fs" % (n_samples, n_features, n_components, time() - t0)) if hasattr(rp, 'components_'): n_bytes = rp.components_.data.nbytes n_bytes += rp.components_.indices.nbytes print("Random matrix with size: %0.3fMB" % (n_bytes / 1e6)) projected_dists = euclidean_distances( projected_data, squared=True).ravel()[nonzero] plt.figure() plt.hexbin(dists, projected_dists, gridsize=100, cmap=plt.cm.PuBu) plt.xlabel("Pairwise squared distances in original space") plt.ylabel("Pairwise squared distances in projected space") plt.title("Pairwise distances distribution for n_components=%d" % n_components) cb = plt.colorbar() cb.set_label('Sample pairs counts') rates = projected_dists / dists print("Mean distances rate: %0.2f (%0.2f)" % (np.mean(rates), np.std(rates))) plt.figure() plt.hist(rates, bins=50, normed=True, range=(0., 2.)) plt.xlabel("Squared distances rate: projected / original") plt.ylabel("Distribution of samples pairs") plt.title("Histogram of pairwise distance rates for n_components=%d" % n_components) # TODO: compute the expected value of eps and add them to the previous plot # as vertical lines / region plt.show()
bsd-3-clause
smorante/continuous-goal-directed-actions
demonstration-feature-selection/src/alternatives/main_dtw_mds_dbscan.py
2
3384
# -*- coding: utf-8 -*- """ Author: Santiago Morante Robotics Lab. Universidad Carlos III de Madrid """ ########################## DTW #################################### import libmddtw import matplotlib.pyplot as plt from dtw import dtw ########################## MDS #################################### import numpy as np from sklearn.metrics import euclidean_distances import libmds ########################## DBSCAN #################################### import libdbscan from sklearn.preprocessing import StandardScaler # to normalize import glob from sklearn import preprocessing EXPERIMENT = "experiment-1" PATH = "../datasets/" + EXPERIMENT +"/raw/*.csv" def normalize(X): return StandardScaler().fit_transform(X) def standardize(X): return preprocessing.scale(X) def main(): demons=[] demoNames = sorted(glob.glob(PATH)) print demoNames for elem in demoNames: tmp = np.loadtxt(elem) tmp_clean = tmp[:,1:] tmp_clean = standardize(tmp_clean) demons.append(tmp_clean) dist=np.zeros((len(demoNames),len(demoNames))) ########################################################################## ########################## DTW #################################### ########################################################################## # fill distance matrix for i in range(len(demoNames)): for j in range(len(demoNames)): mddtw = libmddtw.Mddtw() x,y = mddtw.collapseRows(demons[i],demons[j]) #fig = plt.figure() #plt.plot(x) #plt.plot(y) singleDist, singleCost, singlePath = mddtw.compute(demons[i],demons[j]) dist[i][j]=singleDist # print 'Minimum distance found:', singleDist #fig = plt.figure() # plt.imshow(cost.T, origin='lower', cmap=plt.cm.gray, interpolation='nearest') # plt.plot(path[0], path[1], 'w') # plt.xlim((-0.5, cost.shape[0]-0.5)) # plt.ylim((-0.5, cost.shape[1]-0.5)) # print "dist", dist ########################################################################### ########################### MDS #################################### ########################################################################### md = libmds.Mds() md.create(n_components=2, metric=True, max_iter=3000, eps=1e-12, random_state=None, dissimilarity="precomputed", n_jobs=-1, n_init=100) points = md.compute(dist) print "points", points # md.plot() ########################################################################## ########################## DBSCAN #################################### ########################################################################## # normalize normalizedPoints = normalize(points) # ########################## dbscan db = libdbscan.Dbscan() db.create(eps=1.5, min_samples=2) db.compute(normalizedPoints) db.plot() print "[INFO] Detected outliers: ", db.getOutliers() ############################################################################## ############################################################################## if __name__ == "__main__": main()
mit
sandeepgupta2k4/tensorflow
tensorflow/examples/learn/iris.py
35
1654
# 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.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from sklearn import datasets from sklearn import metrics from sklearn import model_selection import tensorflow as tf 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) # Build 3 layer DNN with 10, 20, 10 units respectively. feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input( x_train) classifier = tf.contrib.learn.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3) # Fit and predict. classifier.fit(x_train, y_train, steps=200) predictions = list(classifier.predict(x_test, as_iterable=True)) score = metrics.accuracy_score(y_test, predictions) print('Accuracy: {0:f}'.format(score)) if __name__ == '__main__': tf.app.run()
apache-2.0
raincoatrun/ThinkStats2
code/chap13soln.py
68
2961
"""This file contains code for use with "Think Stats", by Allen B. Downey, available from greenteapress.com Copyright 2014 Allen B. Downey License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html """ from __future__ import print_function import pandas import numpy as np import thinkplot import thinkstats2 import survival def CleanData(resp): """Cleans respondent data. resp: DataFrame """ resp.cmdivorcx.replace([9998, 9999], np.nan, inplace=True) resp['notdivorced'] = resp.cmdivorcx.isnull().astype(int) resp['duration'] = (resp.cmdivorcx - resp.cmmarrhx) / 12.0 resp['durationsofar'] = (resp.cmintvw - resp.cmmarrhx) / 12.0 month0 = pandas.to_datetime('1899-12-15') dates = [month0 + pandas.DateOffset(months=cm) for cm in resp.cmbirth] resp['decade'] = (pandas.DatetimeIndex(dates).year - 1900) // 10 def ResampleDivorceCurve(resps): """Plots divorce curves based on resampled data. resps: list of respondent DataFrames """ for _ in range(41): samples = [thinkstats2.ResampleRowsWeighted(resp) for resp in resps] sample = pandas.concat(samples, ignore_index=True) PlotDivorceCurveByDecade(sample, color='#225EA8', alpha=0.1) thinkplot.Show(xlabel='years', axis=[0, 28, 0, 1]) def ResampleDivorceCurveByDecade(resps): """Plots divorce curves for each birth cohort. resps: list of respondent DataFrames """ for i in range(41): samples = [thinkstats2.ResampleRowsWeighted(resp) for resp in resps] sample = pandas.concat(samples, ignore_index=True) groups = sample.groupby('decade') if i == 0: survival.AddLabelsByDecade(groups, alpha=0.7) EstimateSurvivalByDecade(groups, alpha=0.1) thinkplot.Save(root='survival7', xlabel='years', axis=[0, 28, 0, 1]) def EstimateSurvivalByDecade(groups, **options): """Groups respondents by decade and plots survival curves. groups: GroupBy object """ thinkplot.PrePlot(len(groups)) for name, group in groups: print(name, len(group)) _, sf = EstimateSurvival(group) thinkplot.Plot(sf, **options) def EstimateSurvival(resp): """Estimates the survival curve. resp: DataFrame of respondents returns: pair of HazardFunction, SurvivalFunction """ complete = resp[resp.notdivorced == 0].duration ongoing = resp[resp.notdivorced == 1].durationsofar hf = survival.EstimateHazardFunction(complete, ongoing) sf = hf.MakeSurvival() return hf, sf def main(): resp6 = survival.ReadFemResp2002() CleanData(resp6) married6 = resp6[resp6.evrmarry==1] resp7 = survival.ReadFemResp2010() CleanData(resp7) married7 = resp7[resp7.evrmarry==1] ResampleDivorceCurveByDecade([married6, married7]) if __name__ == '__main__': main()
gpl-3.0
mistercrunch/panoramix
superset/db_engine_specs/hive.py
1
20297
# 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. import logging import os import re import tempfile import time from datetime import datetime from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING from urllib import parse import numpy as np import pandas as pd import pyarrow as pa import pyarrow.parquet as pq from flask import current_app, g from sqlalchemy import Column, text from sqlalchemy.engine.base import Engine from sqlalchemy.engine.reflection import Inspector from sqlalchemy.engine.url import make_url, URL from sqlalchemy.orm import Session from sqlalchemy.sql.expression import ColumnClause, Select from superset.db_engine_specs.base import BaseEngineSpec from superset.db_engine_specs.presto import PrestoEngineSpec from superset.exceptions import SupersetException from superset.extensions import cache_manager from superset.models.sql_lab import Query from superset.sql_parse import ParsedQuery, Table from superset.utils import core as utils if TYPE_CHECKING: # prevent circular imports from superset.models.core import Database QueryStatus = utils.QueryStatus logger = logging.getLogger(__name__) def upload_to_s3(filename: str, upload_prefix: str, table: Table) -> str: """ Upload the file to S3. :param filename: The file to upload :param upload_prefix: The S3 prefix :param table: The table that will be created :returns: The S3 location of the table """ # Optional dependency import boto3 # pylint: disable=import-error bucket_path = current_app.config["CSV_TO_HIVE_UPLOAD_S3_BUCKET"] if not bucket_path: logger.info("No upload bucket specified") raise Exception( "No upload bucket specified. You can specify one in the config file." ) s3 = boto3.client("s3") location = os.path.join("s3a://", bucket_path, upload_prefix, table.table) s3.upload_file( filename, bucket_path, os.path.join(upload_prefix, table.table, os.path.basename(filename)), ) return location class HiveEngineSpec(PrestoEngineSpec): """Reuses PrestoEngineSpec functionality.""" engine = "hive" engine_name = "Apache Hive" max_column_name_length = 767 allows_alias_to_source_column = True allows_hidden_ordeby_agg = False # When running `SHOW FUNCTIONS`, what is the name of the column with the # function names? _show_functions_column = "tab_name" # pylint: disable=line-too-long _time_grain_expressions = { None: "{col}", "PT1S": "from_unixtime(unix_timestamp({col}), 'yyyy-MM-dd HH:mm:ss')", "PT1M": "from_unixtime(unix_timestamp({col}), 'yyyy-MM-dd HH:mm:00')", "PT1H": "from_unixtime(unix_timestamp({col}), 'yyyy-MM-dd HH:00:00')", "P1D": "from_unixtime(unix_timestamp({col}), 'yyyy-MM-dd 00:00:00')", "P1W": "date_format(date_sub({col}, CAST(7-from_unixtime(unix_timestamp({col}),'u') as int)), 'yyyy-MM-dd 00:00:00')", "P1M": "from_unixtime(unix_timestamp({col}), 'yyyy-MM-01 00:00:00')", "P0.25Y": "date_format(add_months(trunc({col}, 'MM'), -(month({col})-1)%3), 'yyyy-MM-dd 00:00:00')", "P1Y": "from_unixtime(unix_timestamp({col}), 'yyyy-01-01 00:00:00')", "P1W/1970-01-03T00:00:00Z": "date_format(date_add({col}, INT(6-from_unixtime(unix_timestamp({col}), 'u'))), 'yyyy-MM-dd 00:00:00')", "1969-12-28T00:00:00Z/P1W": "date_format(date_add({col}, -INT(from_unixtime(unix_timestamp({col}), 'u'))), 'yyyy-MM-dd 00:00:00')", } # Scoping regex at class level to avoid recompiling # 17/02/07 19:36:38 INFO ql.Driver: Total jobs = 5 jobs_stats_r = re.compile(r".*INFO.*Total jobs = (?P<max_jobs>[0-9]+)") # 17/02/07 19:37:08 INFO ql.Driver: Launching Job 2 out of 5 launching_job_r = re.compile( ".*INFO.*Launching Job (?P<job_number>[0-9]+) out of " "(?P<max_jobs>[0-9]+)" ) # 17/02/07 19:36:58 INFO exec.Task: 2017-02-07 19:36:58,152 Stage-18 # map = 0%, reduce = 0% stage_progress_r = re.compile( r".*INFO.*Stage-(?P<stage_number>[0-9]+).*" r"map = (?P<map_progress>[0-9]+)%.*" r"reduce = (?P<reduce_progress>[0-9]+)%.*" ) @classmethod def patch(cls) -> None: from pyhive import hive from TCLIService import ( constants as patched_constants, TCLIService as patched_TCLIService, ttypes as patched_ttypes, ) from superset.db_engines import hive as patched_hive hive.TCLIService = patched_TCLIService hive.constants = patched_constants hive.ttypes = patched_ttypes hive.Cursor.fetch_logs = patched_hive.fetch_logs @classmethod def get_all_datasource_names( cls, database: "Database", datasource_type: str ) -> List[utils.DatasourceName]: return BaseEngineSpec.get_all_datasource_names(database, datasource_type) @classmethod def fetch_data( cls, cursor: Any, limit: Optional[int] = None ) -> List[Tuple[Any, ...]]: import pyhive from TCLIService import ttypes state = cursor.poll() if state.operationState == ttypes.TOperationState.ERROR_STATE: raise Exception("Query error", state.errorMessage) try: return super().fetch_data(cursor, limit) except pyhive.exc.ProgrammingError: return [] @classmethod def df_to_sql( cls, database: "Database", table: Table, df: pd.DataFrame, to_sql_kwargs: Dict[str, Any], ) -> None: """ Upload data from a Pandas DataFrame to a database. The data is stored via the binary Parquet format which is both less problematic and more performant than a text file. More specifically storing a table as a CSV text file has severe limitations including the fact that the Hive CSV SerDe does not support multiline fields. Note this method does not create metadata for the table. :param database: The database to upload the data to :param: table The table to upload the data to :param df: The dataframe with data to be uploaded :param to_sql_kwargs: The kwargs to be passed to pandas.DataFrame.to_sql` method """ engine = cls.get_engine(database) if to_sql_kwargs["if_exists"] == "append": raise SupersetException("Append operation not currently supported") if to_sql_kwargs["if_exists"] == "fail": # Ensure table doesn't already exist. if table.schema: table_exists = not database.get_df( f"SHOW TABLES IN {table.schema} LIKE '{table.table}'" ).empty else: table_exists = not database.get_df( f"SHOW TABLES LIKE '{table.table}'" ).empty if table_exists: raise SupersetException("Table already exists") elif to_sql_kwargs["if_exists"] == "replace": engine.execute(f"DROP TABLE IF EXISTS {str(table)}") def _get_hive_type(dtype: np.dtype) -> str: hive_type_by_dtype = { np.dtype("bool"): "BOOLEAN", np.dtype("float64"): "DOUBLE", np.dtype("int64"): "BIGINT", np.dtype("object"): "STRING", } return hive_type_by_dtype.get(dtype, "STRING") schema_definition = ", ".join( f"`{name}` {_get_hive_type(dtype)}" for name, dtype in df.dtypes.items() ) with tempfile.NamedTemporaryFile( dir=current_app.config["UPLOAD_FOLDER"], suffix=".parquet" ) as file: pq.write_table(pa.Table.from_pandas(df), where=file.name) engine.execute( text( f""" CREATE TABLE {str(table)} ({schema_definition}) STORED AS PARQUET LOCATION :location """ ), location=upload_to_s3( filename=file.name, upload_prefix=current_app.config[ "CSV_TO_HIVE_UPLOAD_DIRECTORY_FUNC" ](database, g.user, table.schema), table=table, ), ) @classmethod def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]: tt = target_type.upper() if tt == utils.TemporalType.DATE: return f"CAST('{dttm.date().isoformat()}' AS DATE)" if tt == utils.TemporalType.TIMESTAMP: return f"""CAST('{dttm .isoformat(sep=" ", timespec="microseconds")}' AS TIMESTAMP)""" return None @classmethod def adjust_database_uri( cls, uri: URL, selected_schema: Optional[str] = None ) -> None: if selected_schema: uri.database = parse.quote(selected_schema, safe="") @classmethod def _extract_error_message(cls, ex: Exception) -> str: msg = str(ex) match = re.search(r'errorMessage="(.*?)(?<!\\)"', msg) if match: msg = match.group(1) return msg @classmethod def progress(cls, log_lines: List[str]) -> int: total_jobs = 1 # assuming there's at least 1 job current_job = 1 stages: Dict[int, float] = {} for line in log_lines: match = cls.jobs_stats_r.match(line) if match: total_jobs = int(match.groupdict()["max_jobs"]) or 1 match = cls.launching_job_r.match(line) if match: current_job = int(match.groupdict()["job_number"]) total_jobs = int(match.groupdict()["max_jobs"]) or 1 stages = {} match = cls.stage_progress_r.match(line) if match: stage_number = int(match.groupdict()["stage_number"]) map_progress = int(match.groupdict()["map_progress"]) reduce_progress = int(match.groupdict()["reduce_progress"]) stages[stage_number] = (map_progress + reduce_progress) / 2 logger.info( "Progress detail: {}, " # pylint: disable=logging-format-interpolation "current job {}, " "total jobs: {}".format(stages, current_job, total_jobs) ) stage_progress = sum(stages.values()) / len(stages.values()) if stages else 0 progress = 100 * (current_job - 1) / total_jobs + stage_progress / total_jobs return int(progress) @classmethod def get_tracking_url(cls, log_lines: List[str]) -> Optional[str]: lkp = "Tracking URL = " for line in log_lines: if lkp in line: return line.split(lkp)[1] return None @classmethod def handle_cursor( # pylint: disable=too-many-locals cls, cursor: Any, query: Query, session: Session ) -> None: """Updates progress information""" from pyhive import hive unfinished_states = ( hive.ttypes.TOperationState.INITIALIZED_STATE, hive.ttypes.TOperationState.RUNNING_STATE, ) polled = cursor.poll() last_log_line = 0 tracking_url = None job_id = None query_id = query.id while polled.operationState in unfinished_states: query = session.query(type(query)).filter_by(id=query_id).one() if query.status == QueryStatus.STOPPED: cursor.cancel() break log = cursor.fetch_logs() or "" if log: log_lines = log.splitlines() progress = cls.progress(log_lines) logger.info( "Query %s: Progress total: %s", str(query_id), str(progress) ) needs_commit = False if progress > query.progress: query.progress = progress needs_commit = True if not tracking_url: tracking_url = cls.get_tracking_url(log_lines) if tracking_url: job_id = tracking_url.split("/")[-2] logger.info( "Query %s: Found the tracking url: %s", str(query_id), tracking_url, ) tracking_url = current_app.config["TRACKING_URL_TRANSFORMER"] logger.info( "Query %s: Transformation applied: %s", str(query_id), tracking_url, ) query.tracking_url = tracking_url logger.info("Query %s: Job id: %s", str(query_id), str(job_id)) needs_commit = True if job_id and len(log_lines) > last_log_line: # Wait for job id before logging things out # this allows for prefixing all log lines and becoming # searchable in something like Kibana for l in log_lines[last_log_line:]: logger.info("Query %s: [%s] %s", str(query_id), str(job_id), l) last_log_line = len(log_lines) if needs_commit: session.commit() time.sleep(current_app.config["HIVE_POLL_INTERVAL"]) polled = cursor.poll() @classmethod def get_columns( cls, inspector: Inspector, table_name: str, schema: Optional[str] ) -> List[Dict[str, Any]]: return inspector.get_columns(table_name, schema) @classmethod def where_latest_partition( # pylint: disable=too-many-arguments cls, table_name: str, schema: Optional[str], database: "Database", query: Select, columns: Optional[List[Dict[str, str]]] = None, ) -> Optional[Select]: try: col_names, values = cls.latest_partition( table_name, schema, database, show_first=True ) except Exception: # pylint: disable=broad-except # table is not partitioned return None if values is not None and columns is not None: for col_name, value in zip(col_names, values): for clm in columns: if clm.get("name") == col_name: query = query.where(Column(col_name) == value) return query return None @classmethod def _get_fields(cls, cols: List[Dict[str, Any]]) -> List[ColumnClause]: return BaseEngineSpec._get_fields(cols) # pylint: disable=protected-access @classmethod def latest_sub_partition( cls, table_name: str, schema: Optional[str], database: "Database", **kwargs: Any ) -> str: # TODO(bogdan): implement` pass @classmethod def _latest_partition_from_df(cls, df: pd.DataFrame) -> Optional[List[str]]: """Hive partitions look like ds={partition name}""" if not df.empty: return [df.ix[:, 0].max().split("=")[1]] return None @classmethod def _partition_query( # pylint: disable=too-many-arguments cls, table_name: str, database: "Database", limit: int = 0, order_by: Optional[List[Tuple[str, bool]]] = None, filters: Optional[Dict[Any, Any]] = None, ) -> str: return f"SHOW PARTITIONS {table_name}" @classmethod def select_star( # pylint: disable=too-many-arguments cls, database: "Database", table_name: str, engine: Engine, schema: Optional[str] = None, limit: int = 100, show_cols: bool = False, indent: bool = True, latest_partition: bool = True, cols: Optional[List[Dict[str, Any]]] = None, ) -> str: return super( # pylint: disable=bad-super-call PrestoEngineSpec, cls ).select_star( database, table_name, engine, schema, limit, show_cols, indent, latest_partition, cols, ) @classmethod def modify_url_for_impersonation( cls, url: URL, impersonate_user: bool, username: Optional[str] ) -> None: """ Modify the SQL Alchemy URL object with the user to impersonate if applicable. :param url: SQLAlchemy URL object :param impersonate_user: Flag indicating if impersonation is enabled :param username: Effective username """ # Do nothing in the URL object since instead this should modify # the configuraiton dictionary. See get_configuration_for_impersonation @classmethod def update_impersonation_config( cls, connect_args: Dict[str, Any], uri: str, username: Optional[str], ) -> None: """ Update a configuration dictionary that can set the correct properties for impersonating users :param connect_args: :param uri: URI string :param impersonate_user: Flag indicating if impersonation is enabled :param username: Effective username :return: None """ url = make_url(uri) backend_name = url.get_backend_name() # Must be Hive connection, enable impersonation, and set optional param # auth=LDAP|KERBEROS # this will set hive.server2.proxy.user=$effective_username on connect_args['configuration'] if backend_name == "hive" and username is not None: configuration = connect_args.get("configuration", {}) configuration["hive.server2.proxy.user"] = username connect_args["configuration"] = configuration @staticmethod def execute( # type: ignore cursor, query: str, async_: bool = False ): # pylint: disable=arguments-differ kwargs = {"async": async_} cursor.execute(query, **kwargs) @classmethod @cache_manager.cache.memoize() def get_function_names(cls, database: "Database") -> List[str]: """ Get a list of function names that are able to be called on the database. Used for SQL Lab autocomplete. :param database: The database to get functions for :return: A list of function names useable in the database """ df = database.get_df("SHOW FUNCTIONS") if cls._show_functions_column in df: return df[cls._show_functions_column].tolist() columns = df.columns.values.tolist() logger.error( "Payload from `SHOW FUNCTIONS` has the incorrect format. " "Expected column `%s`, found: %s.", cls._show_functions_column, ", ".join(columns), exc_info=True, ) # if the results have a single column, use that if len(columns) == 1: return df[columns[0]].tolist() # otherwise, return no function names to prevent errors return [] @classmethod def is_readonly_query(cls, parsed_query: ParsedQuery) -> bool: """Pessimistic readonly, 100% sure statement won't mutate anything""" return ( super().is_readonly_query(parsed_query) or parsed_query.is_set() or parsed_query.is_show() )
apache-2.0
aurelieladier/openturns
validation/src/optimal_lhs/validate_MC_small.py
7
1877
#! /usr/bin/env python import openturns as ot import matplotlib import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from openturns.viewer import View import time ot.Log.Show(ot.Log.INFO) # Bounds are [0,1]^dimension dimension = 2 bounds = ot.Interval(dimension) nSimu = 10000 c2 = ot.SpaceFillingC2() # Size of sample size = 10 print("dimension=%d, size=%d"%(dimension, size)) for nSimu in [100, 200, 400, 800, 1600, 3200, 6400, 12800, 25600, 51200, 102400, 204800, 409600]: ot.RandomGenerator.SetSeed(0) # Factory: lhs generates lhsDesign = ot.LHSExperiment(ot.ComposedDistribution([ot.Uniform(0.0, 1.0)] * dimension), size) lhsDesign.setAlwaysShuffle(True) # randomized mc = ot.MonteCarloLHS(lhsDesign, nSimu, c2) tic = time.time() design = mc.generate() result = mc.getResult() toc = time.time() print("%d %f %f"%(nSimu,result.getOptimalValue(), toc-tic)) pp = PdfPages('small_mc_OTLHS.pdf') # plot criterion & save it crit = result.drawHistoryCriterion() fig = View(crit, plot_kwargs={'color':'blue'}).getFigure() pp.savefig(fig) plt.close(fig) minDist = ot.SpaceFillingMinDist() # Factory: lhs generates lhsDesign = ot.LHSExperiment(ot.ComposedDistribution([ot.Uniform(0.0, 1.0)] * dimension), size) lhsDesign.setAlwaysShuffle(True) # randomized mc = ot.MonteCarloLHS(lhsDesign, nSimu, minDist) tic = time.time() design = mc.generate() result = mc.getResult() toc = time.time() print("cpu time=%f"%(toc-tic)) print("dimension=%d, size=%d,mc=%s"%(dimension, size, mc)) print("optimal value="+ str(result.getOptimalValue())+" c2="+str(result.getC2())+" phiP="+str(result.getPhiP())+" minDist="+str(result.getMinDist())) # plot criterion & save it crit = result.drawHistoryCriterion() fig = View(crit, plot_kwargs={'color':'blue'}).getFigure() pp.savefig(fig) plt.close(fig) pp.close()
lgpl-3.0
ueshin/apache-spark
python/pyspark/pandas/tests/test_spark_functions.py
11
2127
# # 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. # import numpy as np from pyspark.pandas.spark import functions as SF from pyspark.pandas.utils import spark_column_equals from pyspark.sql import functions as F from pyspark.sql.types import ( ByteType, FloatType, IntegerType, LongType, ) from pyspark.testing.pandasutils import PandasOnSparkTestCase class SparkFunctionsTests(PandasOnSparkTestCase): def test_lit(self): self.assertTrue(spark_column_equals(SF.lit(np.int64(1)), F.lit(1).astype(LongType()))) self.assertTrue(spark_column_equals(SF.lit(np.int32(1)), F.lit(1).astype(IntegerType()))) self.assertTrue(spark_column_equals(SF.lit(np.int8(1)), F.lit(1).astype(ByteType()))) self.assertTrue(spark_column_equals(SF.lit(np.byte(1)), F.lit(1).astype(ByteType()))) self.assertTrue( spark_column_equals(SF.lit(np.float32(1)), F.lit(float(1)).astype(FloatType())) ) self.assertTrue(spark_column_equals(SF.lit(1), F.lit(1))) if __name__ == "__main__": import unittest from pyspark.pandas.tests.test_spark_functions import * # noqa: F401 try: import xmlrunner # type: ignore[import] testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2) except ImportError: testRunner = None unittest.main(testRunner=testRunner, verbosity=2)
apache-2.0
btabibian/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 <[email protected]> # # 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
wbengine/SPMILM
egs/1-billion/run_trf_2.py
1
6271
import os import sys import numpy as np import matplotlib.pyplot as plt sys.path.insert(0, os.getcwd() + '/../../tools/') import wb import trf # revise this function to config the dataset used to train different model def data(tskdir): train = tskdir + 'data/train.txt' valid = tskdir + 'data/valid.txt' test = tskdir + 'data/test.txt' return data_verfy([train, valid, test]) + data_wsj92nbest() def data_verfy(paths): for w in paths: if not os.path.isfile(w): print('[ERROR] no such file: ' + w) return paths def data_wsj92nbest(): root = './data/WSJ92-test-data/' nbest = root + '1000best.sent' trans = root + 'transcript.txt' ac = root + '1000best.acscore' lm = root + '1000best.lmscore' return data_verfy([nbest, trans, ac, lm]) def evaulate_trf(model, vocab, read_model, tsize, fres): res_name = '{}:'.format(int(tsize)) + os.path.split(read_model)[-1] tskdir = '{}/'.format(tsize) # rescore config = ' -vocab {} '.format(vocab) config += ' -read {}.model '.format(read_model) config += ' -nbest {} '.format(data(tskdir)[3]) config += ' -lmscore {0}.lmscore'.format(read_model) model.use(config) # WER [read_nbest, read_templ, read_acscore, read_lmscore] = data(tskdir)[3:7] read_lmscore = read_model + '.lmscore' [wer, lmscale, acscale] = wb.TuneWER(read_nbest, read_templ, wb.LoadScore(read_lmscore), wb.LoadScore(read_acscore), np.linspace(0.1,0.9,9)) print('wer={:.4f} lmscale={:.2f} acscale={:.2f}'.format(wer, lmscale, acscale)) # calculate the ppl on wsj test templ_txt = model.workdir + os.path.split(read_templ)[-1] + '.rmlabel' wb.file_rmlabel(read_templ, templ_txt) PPL_templ = model.ppl(vocab, read_model+'.model', templ_txt) LL_templ = -wb.PPL2LL(PPL_templ, templ_txt) # output the result fres.Add(res_name, ['LL-wsj', 'PPL-wsj'], [LL_templ, PPL_templ]) fres.AddWER(res_name, wer) def main(): if len(sys.argv) == 1: print('\"python run.py -train\" train LSTM\n', '\"python run.py -rescore\" rescore nbest\n', '\"python run.py -wer\" compute WER' ) for tsize in [2]: bindir = '../../tools/trf/bin/' tskdir = '{}/'.format(tsize) workdir = tskdir + 'trflm/' fres = wb.FRes('result.txt') model = trf.model(bindir, workdir) class_num = 200 train = workdir + 'train.id' valid = workdir + 'valid.id' test = workdir + 'test.id' vocab = workdir + 'vocab_c{}.list'.format(class_num) order = 4 feat = 'g4_w_c_ws_cs_wsh_csh_tied.fs' #feat = 'g4_w_c_ws_cs_cpw.fs' maxlen = 100 tmax = 50000 t0 = 2000 minibatch = 100 gamma_lambda = '1000,0' gamma_zeta = '0,0.6' reg = 1e-5 thread = 8 write_model = workdir + 'trf_c{}_{}'.format(class_num, feat[0:-3]) write_name = '{}:{}'.format(tsize, os.path.split(write_model)[1]) if '-class' in sys.argv: # just cluster for each tsks. model.prepare(data(tskdir)[0], data(tskdir)[1], data(tskdir)[2], class_num) if '-train' in sys.argv or '-all' in sys.argv: config = '-vocab {} -train {} -valid {} -test {} '.format(vocab, train, valid, test) config += ' -order {} -feat {} '.format(order, feat) config += ' -len {} '.format(maxlen) config += ' -write {0}.model -log {0}.log '.format(write_model) config += ' -t0 {} -iter {}'.format(t0, tmax) config += ' -gamma-lambda {} -gamma-zeta {}'.format(gamma_lambda, gamma_zeta) config += ' -L2 {} '.format(reg) config += ' -mini-batch {} '.format(minibatch) config += ' -thread {} '.format(thread) config += ' -print-per-iter 10 -write-at-iter 10000:10000:{}'.format(tmax) model.prepare(data(tskdir)[0], data(tskdir)[1], data(tskdir)[2], class_num) model.train(config) # output LL = model.get_last_value(write_model + '.log') fres.AddLL(write_name, LL, data(tskdir)[0:3]) if '-plot' in sys.argv: baseline = fres.Get('{}:KN5'.format(tsize)) trf.PlotLog([write_model], [baseline]) if '-rescore' in sys.argv or '-all' in sys.argv: config = ' -vocab {} '.format(vocab) config += ' -read {}.model '.format(write_model) config += ' -nbest {} '.format(data(tskdir)[3]) config += ' -lmscore {0}.lmscore -lmscore-test-id {0}.test-id '.format(write_model) model.use(config) if '-wer' in sys.argv or '-all' in sys.argv: [read_nbest, read_templ, read_acscore, read_lmscore] = data(tskdir)[3:7] read_lmscore = write_model + '.lmscore' [wer, lmscale, acscale] = wb.TuneWER(read_nbest, read_templ, wb.LoadScore(read_lmscore), wb.LoadScore(read_acscore), np.linspace(0.1,0.9,9)) print('wer={:.4f} lmscale={:.2f} acscale={:.2f}'.format(wer, lmscale, acscale)) # calculate the ppl on wsj test write_templ_id = workdir + os.path.split(read_templ)[1] + '.id' v = trf.ReadVocab(vocab) trf.NbestToID(read_templ, write_templ_id, v) config = ' -vocab {} '.format(vocab) config += ' -read {}.model '.format(write_model) config += ' -test {} '.format(write_templ_id) LL_templ = model.use(config) PPL_templ = wb.LL2PPL(-LL_templ, write_templ_id) # output the result fres.Add(write_name, ['LL-wsj', 'PPL-wsj'], [LL_templ, PPL_templ]) fres.AddWER(write_name, wer) if '-inter' in sys.argv: # calculate the WER for intermediate models for n in np.linspace(10000, 40000, 4): inter_model = workdir + 'trf_c{}_{}.n{}'.format(class_num, feat[0:-3], int(n)) evaulate_trf(model, vocab, inter_model, tsize, fres) if __name__ == '__main__': main()
apache-2.0
plissonf/scikit-learn
examples/svm/plot_svm_scale_c.py
223
5375
""" ============================================== Scaling the regularization parameter for SVCs ============================================== The following example illustrates the effect of scaling the regularization parameter when using :ref:`svm` for :ref:`classification <svm_classification>`. For SVC classification, we are interested in a risk minimization for the equation: .. math:: C \sum_{i=1, n} \mathcal{L} (f(x_i), y_i) + \Omega (w) where - :math:`C` is used to set the amount of regularization - :math:`\mathcal{L}` is a `loss` function of our samples and our model parameters. - :math:`\Omega` is a `penalty` function of our model parameters If we consider the loss function to be the individual error per sample, then the data-fit term, or the sum of the error for each sample, will increase as we add more samples. The penalization term, however, will not increase. When using, for example, :ref:`cross validation <cross_validation>`, to set the amount of regularization with `C`, there will be a different amount of samples between the main problem and the smaller problems within the folds of the cross validation. Since our loss function is dependent on the amount of samples, the latter will influence the selected value of `C`. The question that arises is `How do we optimally adjust C to account for the different amount of training samples?` The figures below are used to illustrate the effect of scaling our `C` to compensate for the change in the number of samples, in the case of using an `l1` penalty, as well as the `l2` penalty. l1-penalty case ----------------- In the `l1` case, theory says that prediction consistency (i.e. that under given hypothesis, the estimator learned predicts as well as a model knowing the true distribution) is not possible because of the bias of the `l1`. It does say, however, that model consistency, in terms of finding the right set of non-zero parameters as well as their signs, can be achieved by scaling `C1`. l2-penalty case ----------------- The theory says that in order to achieve prediction consistency, the penalty parameter should be kept constant as the number of samples grow. Simulations ------------ The two figures below plot the values of `C` on the `x-axis` and the corresponding cross-validation scores on the `y-axis`, for several different fractions of a generated data-set. In the `l1` penalty case, the cross-validation-error correlates best with the test-error, when scaling our `C` with the number of samples, `n`, which can be seen in the first figure. For the `l2` penalty case, the best result comes from the case where `C` is not scaled. .. topic:: Note: Two separate datasets are used for the two different plots. The reason behind this is the `l1` case works better on sparse data, while `l2` is better suited to the non-sparse case. """ print(__doc__) # Author: Andreas Mueller <[email protected]> # Jaques Grobler <[email protected]> # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn.svm import LinearSVC from sklearn.cross_validation import ShuffleSplit from sklearn.grid_search import GridSearchCV from sklearn.utils import check_random_state from sklearn import datasets rnd = check_random_state(1) # set up dataset n_samples = 100 n_features = 300 # l1 data (only 5 informative features) X_1, y_1 = datasets.make_classification(n_samples=n_samples, n_features=n_features, n_informative=5, random_state=1) # l2 data: non sparse, but less features y_2 = np.sign(.5 - rnd.rand(n_samples)) X_2 = rnd.randn(n_samples, n_features / 5) + y_2[:, np.newaxis] X_2 += 5 * rnd.randn(n_samples, n_features / 5) clf_sets = [(LinearSVC(penalty='l1', loss='squared_hinge', dual=False, tol=1e-3), np.logspace(-2.3, -1.3, 10), X_1, y_1), (LinearSVC(penalty='l2', loss='squared_hinge', dual=True, tol=1e-4), np.logspace(-4.5, -2, 10), X_2, y_2)] colors = ['b', 'g', 'r', 'c'] for fignum, (clf, cs, X, y) in enumerate(clf_sets): # set up the plot for each regressor plt.figure(fignum, figsize=(9, 10)) for k, train_size in enumerate(np.linspace(0.3, 0.7, 3)[::-1]): param_grid = dict(C=cs) # To get nice curve, we need a large number of iterations to # reduce the variance grid = GridSearchCV(clf, refit=False, param_grid=param_grid, cv=ShuffleSplit(n=n_samples, train_size=train_size, n_iter=250, random_state=1)) grid.fit(X, y) scores = [x[1] for x in grid.grid_scores_] scales = [(1, 'No scaling'), ((n_samples * train_size), '1/n_samples'), ] for subplotnum, (scaler, name) in enumerate(scales): plt.subplot(2, 1, subplotnum + 1) plt.xlabel('C') plt.ylabel('CV Score') grid_cs = cs * float(scaler) # scale the C's plt.semilogx(grid_cs, scores, label="fraction %.2f" % train_size) plt.title('scaling=%s, penalty=%s, loss=%s' % (name, clf.penalty, clf.loss)) plt.legend(loc="best") plt.show()
bsd-3-clause
jaredwo/topowx
twx/utils/config.py
1
10923
from ConfigParser import ConfigParser from twx.utils import ymdL, mkdir_p import numpy as np import os import pandas as pd class TwxConfig(): '''Class to load and access TopoWx configuration settings in a INI file. Upon initialization, also creates necessary sub-directories in the TopoWx data root directory if they do not exist. Example TopoWx INI File: [TOPOWX_CONFIG] # Path to TopoWx data root TWX_DATA_ROOT=[a path] # Lon/lat bounding box for station observations STN_BBOX=-126.0,22.0,-64.0,53.0 # Start date for which to process station observations OBS_START_DATE=1895-01-01 # End data for which to process station observations OBS_END_DATE=2016-03-29 # Start date for interpolation INTERP_START_DATE=1948-01-01 # End date for interpolation INTERP_END_DATE=2015-12-31 # Station observation elements to process OBS_ELEMS=tmin,tmax,prcp,tobs_tmin,tobs_tmax,tobs_prcp # Primary station observation elements OBS_MAIN_ELEMS=tmin,tmax,prcp # Station chunk size for which to load and process station observations STN_READ_CHUNK_GHCND=100 STN_READ_CHUNK_SNOTEL=20 STN_READ_CHUNK_RAWS=20 # Station chunk size for loading and writing to netcdf file STN_WRITE_CHUNK_NC=100 # Station chunk size for creating aggregated data (e.g.--monthly from daily) STN_AGG_CHUNK=1000 # A geonames username for accessing DEM elevation services USERNAME_GEONAMES=[a username] ''' def __init__(self, fpath_ini): cfg = ConfigParser() cfg.read(fpath_ini) self.twx_data_root = cfg.get('TOPOWX_CONFIG', 'twx_data_root') self.obs_start_date = pd.Timestamp(cfg.get('TOPOWX_CONFIG', 'obs_start_date')) self.obs_end_date = pd.Timestamp(cfg.get('TOPOWX_CONFIG', 'obs_end_date')) self.interp_start_date = pd.Timestamp(cfg.get('TOPOWX_CONFIG', 'interp_start_date')) self.interp_end_date = pd.Timestamp(cfg.get('TOPOWX_CONFIG', 'interp_end_date')) bbox_str = cfg.get('TOPOWX_CONFIG', 'stn_bbox') self.stn_bbox = tuple([np.float(i) for i in bbox_str.split(',')]) self.obs_elems = tuple(cfg.get('TOPOWX_CONFIG', 'obs_elems').split(',')) self.obs_main_elems = tuple(cfg.get('TOPOWX_CONFIG', 'obs_main_elems').split(',')) self.stn_read_chunk_ghcnd = int(cfg.get('TOPOWX_CONFIG', 'stn_read_chunk_ghcnd')) self.stn_read_chunk_snotel = int(cfg.get('TOPOWX_CONFIG', 'stn_read_chunk_snotel')) self.stn_read_chunk_raws = int(cfg.get('TOPOWX_CONFIG', 'stn_read_chunk_raws')) self.stn_write_chunk_nc = int(cfg.get('TOPOWX_CONFIG', 'stn_write_chunk_nc')) self.stn_agg_chunk = int(cfg.get('TOPOWX_CONFIG', 'stn_agg_chunk')) self.username_geonames = cfg.get('TOPOWX_CONFIG', 'username_geonames') self.fpath_log_daily_infill = cfg.get('TOPOWX_CONFIG', 'fpath_log_daily_infill') self.twx_data_version = cfg.get('TOPOWX_CONFIG', 'twx_data_version') # Make TopoWx data directory for local storage of station observations self.path_stndata = os.path.join(self.twx_data_root, 'station_data') mkdir_p(self.path_stndata) fname_stndata_hdf_ghcnd = 'obs_ghcnd_%d_%d.hdf' % (ymdL(self.obs_start_date), ymdL(self.obs_end_date)) self.fpath_stndata_hdf_ghcnd = os.path.join(self.path_stndata, fname_stndata_hdf_ghcnd) fname_stndata_hdf_snotel = 'obs_snotel_%d_%d.hdf' % (ymdL(self.obs_start_date), ymdL(self.obs_end_date)) self.fpath_stndata_hdf_snotel = os.path.join(self.path_stndata, fname_stndata_hdf_snotel) fname_stndata_hdf_raws = 'obs_raws_%d_%d.hdf' % (ymdL(self.obs_start_date), ymdL(self.obs_end_date)) self.fpath_stndata_hdf_raws = os.path.join(self.path_stndata, fname_stndata_hdf_raws) fname_stndata_nc_all = 'obs_all_%d_%d.nc' % (ymdL(self.obs_start_date), ymdL(self.obs_end_date)) self.fpath_stndata_nc_all = os.path.join(self.path_stndata, fname_stndata_nc_all) fname_stndata_nc_tair_tobs_adj = 'tair_tobs_adj_%d_%d.nc' % (ymdL(self.obs_start_date), ymdL(self.obs_end_date)) self.fpath_stndata_nc_tair_tobs_adj = os.path.join(self.path_stndata, fname_stndata_nc_tair_tobs_adj) fname_stndata_nc_tair_homog = 'tair_homog_%d_%d.nc' % (ymdL(self.obs_start_date), ymdL(self.obs_end_date)) self.fpath_stndata_nc_tair_homog = os.path.join(self.path_stndata, fname_stndata_nc_tair_homog) self.fpath_locqa_hdf = os.path.join(self.path_stndata, 'locqa.hdf') self.fpath_locqa_fail_csv = os.path.join(self.path_stndata, 'locqa_fail.csv') # Make TopoWx data directory for PHA-based homogenization self.path_homog_pha = os.path.join(self.path_stndata, 'homog') mkdir_p(self.path_homog_pha) self.fpath_pha_tgz = os.path.join(self.path_homog_pha, 'phav52i.tar.gz') # Make TopoWx data directories for reanalysis data self.path_reanalysis_data = os.path.join(self.twx_data_root, 'reanalysis_data') mkdir_p(self.path_reanalysis_data) self.path_reanalysis_namerica = os.path.join(self.path_reanalysis_data, 'n_america_subset') mkdir_p(self.path_reanalysis_namerica) # Make TopoWx data directory for infilled station observations self.path_stndata_infill = os.path.join(self.path_stndata, 'infill') mkdir_p(self.path_stndata_infill) self.fpath_xval_infill_nc = os.path.join(self.path_stndata_infill, 'xval_infill_tair.nc') self.fpath_stndata_nc_infill_tmin = os.path.join(self.path_stndata_infill, 'infill_tmin.nc') self.fpath_stndata_nc_infill_tmax = os.path.join(self.path_stndata_infill, 'infill_tmax.nc') self.fpath_flagged_bad_stns = os.path.join(self.path_stndata_infill, 'bad_stns.csv') self.fpath_stndata_nc_serial_tmin = os.path.join(self.path_stndata_infill, 'serial_tmin.nc') self.fpath_stndata_nc_serial_tmax = os.path.join(self.path_stndata_infill, 'serial_tmax.nc') # Make data directories for storing interp param optimization files # Temperature normals self.path_interp_optim_norms = os.path.join(self.path_stndata_infill, 'optim_norm') mkdir_p(self.path_interp_optim_norms) # Daily anomalies self.path_interp_optim_anoms = os.path.join(self.path_stndata_infill, 'optim_anom') mkdir_p(self.path_interp_optim_anoms) self.fpath_xval_interp_nc_tmin = os.path.join(self.path_stndata_infill, 'xval_interp_tmin.nc') self.fpath_xval_interp_nc_tmax = os.path.join(self.path_stndata_infill, 'xval_interp_tmax.nc') # Make TopoWx data directory for raster data self.path_rasters = os.path.join(self.twx_data_root, 'rasters') mkdir_p(self.path_rasters) self.path_predictor_rasters = os.path.join(self.path_rasters, 'conus_interp_grids', 'ncdf') mkdir_p(self.path_predictor_rasters) # Make TopoWx data directory for writing output tiles self.path_tile_out = os.path.join(self.twx_data_root, 'tile_output') mkdir_p(self.path_tile_out) # Make TopoWx log directory self.path_logs = os.path.join(self.twx_data_root, 'logs') mkdir_p(self.path_logs) ################################## # Make TopoWx data directory for final outputs ################################## self.path_final_output = os.path.join(self.twx_data_root, 'final_output_data') mkdir_p(self.path_final_output) # Final auxiliary data directories self.path_aux_data = os.path.join(self.path_final_output, 'auxiliary_data') mkdir_p(self.path_aux_data) self.path_aux_stndata = os.path.join(self.path_aux_data, 'station_data') mkdir_p(self.path_aux_stndata) self.fpath_stndata_nc_aux_tmin = os.path.join(self.path_aux_stndata, 'stn_obs_tmin.nc') self.fpath_stndata_nc_aux_tmax = os.path.join(self.path_aux_stndata, 'stn_obs_tmax.nc') self.fpath_pha_adj_aux = os.path.join(self.path_aux_stndata, 'homog_adjust.csv') self.path_aux_grids = os.path.join(self.path_aux_data, 'auxiliary_grids') mkdir_p(self.path_aux_grids) # Final TopoWx output mosaics for normals, daily, and monthly data self.path_mosaic_norms = os.path.join(self.path_final_output, 'normals') mkdir_p(self.path_mosaic_norms) self.path_mosaic_daily = os.path.join(self.path_final_output, 'daily') mkdir_p(self.path_mosaic_daily) self.path_mosaic_monthly = os.path.join(self.path_final_output, 'monthly') mkdir_p(self.path_mosaic_monthly)
gpl-3.0
cdiazbas/MPySIR
all2maps.py
1
4874
# Author: [email protected] import matplotlib.pyplot as plt import pyLib.imtools as imtools import numpy as np # ========================= CREANDO PHIMAP import matplotlib.colors as mcolors def make_colormap(seq): seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3] cdict = {'red': [], 'green': [], 'blue': []} for i, item in enumerate(seq): if isinstance(item, float): r1, g1, b1 = seq[i - 1] r2, g2, b2 = seq[i + 1] cdict['red'].append([item, r1, r2]) cdict['green'].append([item, g1, g2]) cdict['blue'].append([item, b1, b2]) return mcolors.LinearSegmentedColormap('CustomMap', cdict) c = mcolors.ColorConverter().to_rgb phimap = make_colormap([c('white'), c('tomato'), 0.33, c('tomato'), c('deepskyblue'), 0.66, c('deepskyblue'),c('white')]) def dimMap(resultadoSir): height = resultadoSir.shape[0]*(resultadoSir[0][-1][0][0]+1) width = (resultadoSir[0][-1][0][1]+1) return [height, width] def readmapa(resultadoSir, mapa, magnitud): cont = 0 for fila in range(0, height): for columna in range(0, width): punto = cont % resultadoSir.shape[1] veces = int(cont/resultadoSir.shape[1]) if magnitud == 8 or magnitud == 9 or magnitud == 10 or magnitud == 11: mapa[columna,fila] = resultadoSir[veces][punto][1][0][magnitud] else: mapa[columna,fila] = resultadoSir[veces][punto][1][0][magnitud][index] cont += 1 return mapa def corrphi(mapa): mapa[mapa<0] = (mapa[mapa<0]+360) % 360; mapa[mapa>180] = (mapa[mapa>180]-180) def do1map(logTau, magnitud): # ============================================================================================== # global index # global magnitud # ========================= INPUT invSir1 = 'MAPA1.npy' invSir2 = 'MAPA2.npy' # logTau = 0.0 # magnitud = 2 # hsv cmapArray = ['gray','gray','gray','bone','bone','seismic','Spectral_r',phimap,'bone','gray','gray','cubehelix'] magTitle = [r'${\rm log(\tau)=}$',r'${\rm T\ [kK]}$','p',r'${\rm v\ [km/s]}$',r'${\rm B\ [kG]}$',r'${\rm v\ [km/s]}$',r'${\rm \gamma\ [d]}$',r'${\rm \phi\ [d]}$','vmacro','fillingf','difusa',r'${\rm \chi^2}$'] magFile = ['TAU','TEMP','PRESION','VMICRO','CAMPO','VLOS','GAMMA','PHI','VMACRO','FILLING','DIFUSA','CHI2'] # ========================= MAP resultadoSir1 = np.load(invSir1) resultadoSir2 = np.load(invSir2) # height, width = dimMap(resultadoSir1) # print('height:',height,'width:',width) # mapa = np.zeros((height, width)) index = np.where(resultadoSir1[0][0][1][0][0] == logTau)[0][0] print('logTau: '+str(logTau)+' -> index: '+str(index)) # readmapa(resultadoSir1, mapa.T ,magnitud) from pySir import sirtools as st mapa1 = st.readSIRMap(resultadoSir1, magnitud, index) mapa2 = st.readSIRMap(resultadoSir2, magnitud, index) mapa = np.concatenate((mapa1, mapa2)) from scipy import ndimage mapa = ndimage.median_filter(np.flipud(mapa), 3) # Limites en la escala de color if magnitud == 7: corrphi(mapa) print('3sigma_map: {0:2.2f}'.format(3*np.std(mapa))) print('Mean_map: {0:2.2f}'.format(np.mean(mapa))) print('Min_map: {0:2.2f}'.format(np.min(mapa))) print('Max_map: {0:2.2f}'.format(np.max(mapa))) vmini = np.mean(mapa)-3*np.std(mapa) if np.min(mapa) >= 0.0 and magnitud != 1: vmini = 0. vmaxi = np.mean(mapa)+3*np.std(mapa) if magnitud == 1 or magnitud == 4: vmini = np.min(mapa); vmaxi = np.max(mapa) if magnitud == 6: vmaxi = 180. if magnitud == 7: vmaxi = 180.;vmini = 0. if magnitud == 11: vmaxi = np.max(mapa); vmini = 0. if magnitud == 5: vmini = np.mean(mapa)-4*np.std(mapa); vmaxi = -vmini from matplotlib.colors import LogNorm plt.imshow(mapa,cmap=cmapArray[magnitud],origin='lower',interpolation='None',vmin=vmini,vmax=vmaxi)#norm=LogNorm() plt.title('Map 17jun14.006 (1-2)') plt.xlabel('Slit Axis [pix]') plt.ylabel('Time Axis [pix]') cb = plt.colorbar(shrink=.46)#, ticks=[0.6, 0.8, 1., 1.2]) #cb = plt.colorbar(shrink=.46, ticks=[0.3, 0.6, 0.9, 1.2, 1.5]) # cb.set_label(r'Intensity HeI ({0:4.1f}) /$I_{{qs}}$({1:4.1f})'.format(xLambda[341],xLambda[posicontinuo]), labelpad=5., y=0.5, fontsize=12.) loglabel = r'${\rm log(\tau)=}$' cb.set_label(r""+magTitle[magnitud]+r", "+loglabel+"{0}".format(logTau), labelpad=8., y=0.5, fontsize=12.) # plt.show() plt.savefig(magFile[magnitud]+'_log{0:02d}.pdf'.format(int(logTau)), bbox_inches='tight') print(magFile[magnitud]+'_log{0:02d}.pdf SAVE'.format(int(logTau))) print('-----------------------'+str(magnitud)) plt.clf() for magnitud in range(12): do1map(0.0, magnitud)
mit
timberhill/blablaplot
blablaplot.py
1
6659
#!/usr/bin/python from numpy import loadtxt, asarray from numpy.random import normal as gaussian_noise import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import warnings """ Here you register new characters in format: '<char>' : (<width>, <height>, '<filename>'), """ charlist = { 'a' : (0.7, 1.0, 'a'), 'b' : (0.7, 1.0, 'b'), 'c' : (0.7, 1.0, 'c'), 'd' : (0.7, 1.0, 'd'), 'e' : (0.7, 1.0, 'e'), 'f' : (0.7, 1.0, 'f'), 'g' : (0.7, 1.0, 'g'), 'h' : (0.7, 1.0, 'h'), 'i' : (0.4, 1.0, 'i'), 'j' : (0.4, 1.0, 'j'), 'k' : (0.7, 1.0, 'k'), 'l' : (0.7, 1.0, 'l'), 'm' : (0.7, 1.0, 'm'), 'n' : (0.7, 1.0, 'n'), 'o' : (0.7, 1.0, 'o'), 'p' : (0.7, 1.0, 'p'), 'q' : (0.7, 1.0, 'q'), 'r' : (0.7, 1.0, 'r'), 's' : (0.7, 1.0, 's'), 't' : (0.7, 1.0, 't'), 'u' : (0.7, 1.0, 'u'), 'v' : (0.7, 1.0, 'v'), 'w' : (0.7, 1.0, 'w'), 'x' : (0.7, 1.0, 'x'), 'y' : (0.7, 1.0, 'y'), 'z' : (0.7, 1.0, 'z'), '0' : (0.7, 1.0, '0'), '1' : (0.5, 1.0, '1'), '2' : (0.7, 1.0, '2'), '3' : (0.7, 1.0, '3'), '4' : (0.7, 1.0, '4'), '5' : (0.7, 1.0, '5'), '6' : (0.7, 1.0, '6'), '7' : (0.7, 1.0, '7'), '8' : (0.7, 1.0, '8'), '9' : (0.7, 1.0, '9'), ' ' : (0.7, 0.0, 'space'), '?' : (0.7, 1.0, 'questionmark'), '!' : (0.2, 1.0, 'exclamationmark'), ',' : (0.1, 0.1, 'comma'), '.' : (0.2, 0.1, 'fullstop'), '&' : (0.6, 1.0, 'ampersand'), '$' : (0.5, 1.0, 'dollar'), '@' : (0.7, 1.0, 'at'), '(' : (0.3, 1.0, 'brackets_open'), ')' : (0.3, 1.0, 'brackets_close'), '#' : (0.7, 1.0, 'hash'), '%' : (0.7, 1.0, 'percent'), } class Character(object): """ ARGUMENTS char - single character (first one is chosen) size - size of the letter (width, height) self.xs, self.ys - arrays with letter points """ def __init__(self, char, filename='', size=(1.0, 1.0), jitter=0.0): if len(char) < 1: raise Exception('Empty string is passed to Character() constructor.') self.char = char[0] if len(filename) > 0: self.filename = filename else: 'chars/' + self.char + '.dat' self._getPoints() self.resize(size=size) def _getPoints(self): xs, ys = loadtxt('chars/' + self.filename + '.dat', unpack=True) self.xs = asarray(xs) self.ys = asarray(ys) self._sort() def _sort(self): points = zip(self.xs, self.ys) sorted_points = sorted(points) self.xs = asarray([point[0] for point in sorted_points]) self.ys = asarray([point[1] for point in sorted_points]) def resize(self, size=(1.0, 1.0)): self.size = size if len(self.xs) < 1: self._getPoints() xmin = min(self.xs) xmax = max(self.xs) ymin = min(self.ys) ymax = max(self.ys) for i in range(0, len(self.xs)): self.xs[i] = self.size[0] * (self.xs[i] - xmin) / (xmax - xmin) self.ys[i] = self.size[1] * (self.ys[i] - ymin) / (ymax - ymin) class TextyPloty(object): """ ARGUMENTS jitter - to randomize points locations, represents sigma for gaussian noise spacing - distance between letters offset - offset from zero point if format (x, y) scale - scale/size of the letters func - function to add text to """ def __init__(self, jitter=0.0, spacing=0.1, offset=(0.0, 0.0), scale=(1.0, 1.0), func=None): self.jitter = jitter self.spacing = spacing self.offset = offset self.scale = scale self.func = func self.charlist = charlist """ ARGUMENTS text - string to plot RETURNS xs, ys - points coordinates """ def get(self, text): xs, ys = [], [] xoffset = self.offset[0] for char in text: if char == ' ': xoffset += self.charlist[char][0] * self.scale[0] elif char == '\t': xoffset += self.charlist[char][0] * self.scale[0] * 4 elif char in self.charlist: charobj = Character(char=char, filename=self.charlist[char][2], size=self.charlist[char]) xs.extend(self.scale[0] * charobj.xs + xoffset) ys.extend(self.scale[1] * charobj.ys + self.offset[1]) xoffset += self.charlist[char][0] * self.scale[0] else: warnings.warn('Could not find file with "' + char + '" character. Skipping...', Warning) xoffset += self.spacing * self.scale[0] if self.func != None: for i in range(0,len(xs)): ys[i] += self.func(xs[i]) if self.jitter > 0: noise = gaussian_noise(0.0, self.jitter*self.scale[1], (len(ys))) ys = [x+y for x, y in zip(ys, noise)] return asarray(xs), asarray(ys) class ResidualsPlot(object): """ """ def __init__(self, data=([],[]), datastyle='k.', xs_fit=[], func=None, fitstyle='r-', \ xlabel='', ylabel='', reslabel='', ratio=[4, 1], figsize=(10,6), axis=None, res_axis=None, \ fitlabel='fit', datalabel='points'): self.plt_instance = plt self.xs = data[0] self.ys = data[1] self.datastyle = datastyle self.xs_fit = xs_fit self.func = func self.ys_fit = self.func(self.xs_fit) self.fitstyle = fitstyle self.xlabel = xlabel self.ylabel = ylabel self.reslabel = reslabel self.ratio = ratio self.figsize = figsize self.axis = axis self.res_axis = res_axis self.fitlabel = fitlabel self.datalabel = datalabel def draw(self): self.redraw() def redraw(self): self.plt_instance = plt self.plt_instance.figure(figsize=self.figsize) self.gridspec_instance = gridspec.GridSpec(2, 1, height_ratios=self.ratio) self.gridspec_instance.update(hspace=0.00) self.ax0 = self.plt_instance.subplot(self.gridspec_instance[0]) self.ax1 = self.plt_instance.subplot(self.gridspec_instance[1]) self.ys_res = self.ys - self.func(self.xs) # set axis ranges if self.axis == None: self.ax0.axis([min(self.xs_fit) * 1.1, max(self.xs_fit)*1.1, min(self.ys_fit) * 1.1, max(self.ys_fit) * 1.1]) elif len(self.axis) != 4: raise Exception('ResidualsPlot: axis should contain 4 numbers: (x1, x2, y1, y2)') else: self.ax0.axis(self.axis) if self.res_axis == None: self.ax1.axis([min(self.xs_fit) * 1.1, max(self.xs_fit)*1.1, min(self.ys_res) * 1.1, max(self.ys_res)*1.1]) elif len(self.res_axis) != 4: raise Exception('ResidualsPlot: res_axis should contain 4 numbers: (x1, x2, y1, y2)') else: self.ax1.axis(self.res_axis) # set axis labels self.ax0.set_ylabel(self.ylabel) self.ax1.set_ylabel(self.reslabel) self.ax1.set_xlabel(self.xlabel) # first subplot: datapoints and fit self.ax0.plot(self.xs_fit, self.ys_fit, self.fitstyle, label=self.fitlabel) self.ax0.plot(self.xs, self.ys, self.datastyle, label=self.datalabel) # second subplot: residuals self.ax1.plot([min(self.xs), max(self.xs)], [0,0], self.fitstyle) self.ax1.plot(self.xs, self.ys_res, self.datastyle) self.ax0.legend(loc="upper right") def show(self): self.plt_instance.show() def savefig(self, name='plot.pdf'): self.plt_instance.savefig(name)
mit
tiankanl/2014_fall_ASTR599
notebooks/fig_code/helpers.py
74
2301
""" Small helpers for code that is not shown in the notebooks """ from sklearn import neighbors, datasets, linear_model import pylab as pl import numpy as np from matplotlib.colors import ListedColormap # Create color maps for 3-class classification problem, as with iris cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF']) cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF']) def plot_iris_knn(): 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 knn = neighbors.KNeighborsClassifier(n_neighbors=3) knn.fit(X, y) 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.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100)) Z = knn.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) pl.figure() pl.pcolormesh(xx, yy, Z, cmap=cmap_light) # Plot also the training points pl.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) pl.xlabel('sepal length (cm)') pl.ylabel('sepal width (cm)') pl.axis('tight') def plot_polynomial_regression(): rng = np.random.RandomState(0) x = 2*rng.rand(100) - 1 f = lambda t: 1.2 * t**2 + .1 * t**3 - .4 * t **5 - .5 * t ** 9 y = f(x) + .4 * rng.normal(size=100) x_test = np.linspace(-1, 1, 100) pl.figure() pl.scatter(x, y, s=4) X = np.array([x**i for i in range(5)]).T X_test = np.array([x_test**i for i in range(5)]).T regr = linear_model.LinearRegression() regr.fit(X, y) pl.plot(x_test, regr.predict(X_test), label='4th order') X = np.array([x**i for i in range(10)]).T X_test = np.array([x_test**i for i in range(10)]).T regr = linear_model.LinearRegression() regr.fit(X, y) pl.plot(x_test, regr.predict(X_test), label='9th order') pl.legend(loc='best') pl.axis('tight') pl.title('Fitting a 4th and a 9th order polynomial') pl.figure() pl.scatter(x, y, s=4) pl.plot(x_test, f(x_test), label="truth") pl.axis('tight') pl.title('Ground truth (9th order polynomial)')
apache-2.0
mwv/scikit-learn
examples/datasets/plot_random_dataset.py
348
2254
""" ============================================== Plot randomly generated classification dataset ============================================== Plot several randomly generated 2D classification datasets. This example illustrates the :func:`datasets.make_classification` :func:`datasets.make_blobs` and :func:`datasets.make_gaussian_quantiles` functions. For ``make_classification``, three binary and two multi-class classification datasets are generated, with different numbers of informative features and clusters per class. """ print(__doc__) import matplotlib.pyplot as plt from sklearn.datasets import make_classification from sklearn.datasets import make_blobs from sklearn.datasets import make_gaussian_quantiles plt.figure(figsize=(8, 8)) plt.subplots_adjust(bottom=.05, top=.9, left=.05, right=.95) plt.subplot(321) plt.title("One informative feature, one cluster per class", fontsize='small') X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(322) plt.title("Two informative features, one cluster per class", fontsize='small') X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(323) plt.title("Two informative features, two clusters per class", fontsize='small') X2, Y2 = make_classification(n_features=2, n_redundant=0, n_informative=2) plt.scatter(X2[:, 0], X2[:, 1], marker='o', c=Y2) plt.subplot(324) plt.title("Multi-class, two informative features, one cluster", fontsize='small') X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1, n_classes=3) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(325) plt.title("Three blobs", fontsize='small') X1, Y1 = make_blobs(n_features=2, centers=3) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(326) plt.title("Gaussian divided into three quantiles", fontsize='small') X1, Y1 = make_gaussian_quantiles(n_features=2, n_classes=3) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.show()
bsd-3-clause
r-mart/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
MusicVisualizationUMass/TeamNameGenerator
src/musicvisualizer/pipeline/models/tests/losc_manual_tests.py
1
1565
#!/usr/bin/env python3 '''A set of manual tests to make sure bits and pieces are working. Not part of the general testing framework but kept for now in case there is some useful data here later ''' from musicvisualizer.pipeline.models.linear_oscillator import LinearOscillatorModel import matplotlib.pyplot as plt #plt.style.use('ggplot') if __name__ == '__main__': points = 1024 def dataIn(): print("CUSTOM DATA IN") t = 0.0 MAXT = 30 while t < MAXT: t += 0.1 def dataIn_empty(): for i in range(1000): yield (0.0, 0.0) def dataIn_singlePulse(): yield(points/2, 20) for i in range(1000): yield (0.0, 0.0) M = LinearOscillatorModel( sampleRate = 24, # Visual sample rate dataInFPS = 96, # Data sample rate (to generate visual) number_of_points = points, # how many points in simulation? hook = 11.0, dataIn = None, damping = 0.9999) I = iter(M) plt.ion() # Interactive I/O for frame in I: ys = [ p[0] for p in frame] vs = [ p[1] for p in frame] xs = range(len(frame)) # print("max(ys) = {}".format(max(ys))) # print("max(vs) = {}".format(max(vs))) ys = ys + [1.0, -1.0] xs = list(xs) + [0, 0] plt.scatter(xs, ys) plt.show() # Update visuals plt.pause(0.02) # Pause plt.cla() # Clear
mit
Ziqi-Li/bknqgis
pandas/pandas/core/dtypes/inference.py
8
8381
""" basic inference routines """ import collections import re import numpy as np from numbers import Number from pandas.compat import (PY2, string_types, text_type, string_and_binary_types) from pandas._libs import lib is_bool = lib.is_bool is_integer = lib.is_integer is_float = lib.is_float is_complex = lib.is_complex is_scalar = lib.isscalar is_decimal = lib.is_decimal is_interval = lib.is_interval def is_number(obj): """ Check if the object is a number. Parameters ---------- obj : The object to check. Returns ------- is_number : bool Whether `obj` is a number or not. Examples -------- >>> is_number(1) True >>> is_number("foo") False """ return isinstance(obj, (Number, np.number)) def is_string_like(obj): """ Check if the object is a string. Parameters ---------- obj : The object to check. Examples -------- >>> is_string_like("foo") True >>> is_string_like(1) False Returns ------- is_str_like : bool Whether `obj` is a string or not. """ return isinstance(obj, (text_type, string_types)) def _iterable_not_string(obj): """ Check if the object is an iterable but not a string. Parameters ---------- obj : The object to check. Returns ------- is_iter_not_string : bool Whether `obj` is a non-string iterable. Examples -------- >>> _iterable_not_string([1, 2, 3]) True >>> _iterable_not_string("foo") False >>> _iterable_not_string(1) False """ return (isinstance(obj, collections.Iterable) and not isinstance(obj, string_types)) def is_iterator(obj): """ Check if the object is an iterator. For example, lists are considered iterators but not strings or datetime objects. Parameters ---------- obj : The object to check. Returns ------- is_iter : bool Whether `obj` is an iterator. Examples -------- >>> is_iterator([1, 2, 3]) True >>> is_iterator(datetime(2017, 1, 1)) False >>> is_iterator("foo") False >>> is_iterator(1) False """ if not hasattr(obj, '__iter__'): return False if PY2: return hasattr(obj, 'next') else: # Python 3 generators have # __next__ instead of next return hasattr(obj, '__next__') def is_file_like(obj): """ Check if the object is a file-like object. For objects to be considered file-like, they must be an iterator AND have either a `read` and/or `write` method as an attribute. Note: file-like objects must be iterable, but iterable objects need not be file-like. .. versionadded:: 0.20.0 Parameters ---------- obj : The object to check. Returns ------- is_file_like : bool Whether `obj` has file-like properties. Examples -------- >>> buffer(StringIO("data")) >>> is_file_like(buffer) True >>> is_file_like([1, 2, 3]) False """ if not (hasattr(obj, 'read') or hasattr(obj, 'write')): return False if not hasattr(obj, "__iter__"): return False return True def is_re(obj): """ Check if the object is a regex pattern instance. Parameters ---------- obj : The object to check. Returns ------- is_regex : bool Whether `obj` is a regex pattern. Examples -------- >>> is_re(re.compile(".*")) True >>> is_re("foo") False """ return isinstance(obj, re._pattern_type) def is_re_compilable(obj): """ Check if the object can be compiled into a regex pattern instance. Parameters ---------- obj : The object to check. Returns ------- is_regex_compilable : bool Whether `obj` can be compiled as a regex pattern. Examples -------- >>> is_re_compilable(".*") True >>> is_re_compilable(1) False """ try: re.compile(obj) except TypeError: return False else: return True def is_list_like(obj): """ Check if the object is list-like. Objects that are considered list-like are for example Python lists, tuples, sets, NumPy arrays, and Pandas Series. Strings and datetime objects, however, are not considered list-like. Parameters ---------- obj : The object to check. Returns ------- is_list_like : bool Whether `obj` has list-like properties. Examples -------- >>> is_list_like([1, 2, 3]) True >>> is_list_like({1, 2, 3}) True >>> is_list_like(datetime(2017, 1, 1)) False >>> is_list_like("foo") False >>> is_list_like(1) False """ return (hasattr(obj, '__iter__') and not isinstance(obj, string_and_binary_types)) def is_nested_list_like(obj): """ Check if the object is list-like, and that all of its elements are also list-like. .. versionadded:: 0.20.0 Parameters ---------- obj : The object to check. Returns ------- is_list_like : bool Whether `obj` has list-like properties. Examples -------- >>> is_nested_list_like([[1, 2, 3]]) True >>> is_nested_list_like([{1, 2, 3}, {1, 2, 3}]) True >>> is_nested_list_like(["foo"]) False >>> is_nested_list_like([]) False >>> is_nested_list_like([[1, 2, 3], 1]) False Notes ----- This won't reliably detect whether a consumable iterator (e. g. a generator) is a nested-list-like without consuming the iterator. To avoid consuming it, we always return False if the outer container doesn't define `__len__`. See Also -------- is_list_like """ return (is_list_like(obj) and hasattr(obj, '__len__') and len(obj) > 0 and all(is_list_like(item) for item in obj)) def is_dict_like(obj): """ Check if the object is dict-like. Parameters ---------- obj : The object to check. Returns ------- is_dict_like : bool Whether `obj` has dict-like properties. Examples -------- >>> is_dict_like({1: 2}) True >>> is_dict_like([1, 2, 3]) False """ return hasattr(obj, '__getitem__') and hasattr(obj, 'keys') def is_named_tuple(obj): """ Check if the object is a named tuple. Parameters ---------- obj : The object to check. Returns ------- is_named_tuple : bool Whether `obj` is a named tuple. Examples -------- >>> Point = namedtuple("Point", ["x", "y"]) >>> p = Point(1, 2) >>> >>> is_named_tuple(p) True >>> is_named_tuple((1, 2)) False """ return isinstance(obj, tuple) and hasattr(obj, '_fields') def is_hashable(obj): """Return True if hash(obj) will succeed, False otherwise. Some types will pass a test against collections.Hashable but fail when they are actually hashed with hash(). Distinguish between these and other types by trying the call to hash() and seeing if they raise TypeError. Examples -------- >>> a = ([],) >>> isinstance(a, collections.Hashable) True >>> is_hashable(a) False """ # Unfortunately, we can't use isinstance(obj, collections.Hashable), which # can be faster than calling hash. That is because numpy scalars on Python # 3 fail this test. # Reconsider this decision once this numpy bug is fixed: # https://github.com/numpy/numpy/issues/5562 try: hash(obj) except TypeError: return False else: return True def is_sequence(obj): """ Check if the object is a sequence of objects. String types are not included as sequences here. Parameters ---------- obj : The object to check. Returns ------- is_sequence : bool Whether `obj` is a sequence of objects. Examples -------- >>> l = [1, 2, 3] >>> >>> is_sequence(l) True >>> is_sequence(iter(l)) False """ try: iter(obj) # Can iterate over it. len(obj) # Has a length associated with it. return not isinstance(obj, string_and_binary_types) except (TypeError, AttributeError): return False
gpl-2.0
yassersouri/omgh
src/scripts/grid_search_c.py
1
2365
import sys import os sys.path.append(os.path.dirname(os.path.dirname(__file__))) from dataset import CUB_200_2011 from storage import datastore from deep_extractor import CNN_Features_CAFFE_REFERENCE from datetime import datetime as dt import settings import utils import numpy as np cub = CUB_200_2011(settings.CUB_ROOT) features_storage_r = datastore(settings.storage('ccrft')) feature_extractor_r = CNN_Features_CAFFE_REFERENCE(features_storage_r, make_net=False) features_storage_c = datastore(settings.storage('cccft')) feature_extractor_c = CNN_Features_CAFFE_REFERENCE(features_storage_c, make_net=False) features_storage_p_h = datastore(settings.storage('ccpheadft-100000')) feature_extractor_p_h = CNN_Features_CAFFE_REFERENCE(features_storage_p_h, make_net=False) features_storage_p_h = datastore(settings.storage('ccpheadft-100000')) feature_extractor_p_h = CNN_Features_CAFFE_REFERENCE(features_storage_p_h, make_net=False) features_storage_p_b = datastore(settings.storage('ccpbodyft-10000')) feature_extractor_p_b = CNN_Features_CAFFE_REFERENCE(features_storage_p_b, make_net=False) Xtrain_r, ytrain_r, Xtest_r, ytest_r = cub.get_train_test(feature_extractor_r.extract_one) Xtrain_c, ytrain_c, Xtest_c, ytest_c = cub.get_train_test(feature_extractor_c.extract_one) Xtrain_p_h, ytrain_p_h, Xtest_p_h, ytest_p_h = cub.get_train_test(feature_extractor_p_h.extract_one) Xtrain_p_b, ytrain_p_b, Xtest_p_b, ytest_p_b = cub.get_train_test(feature_extractor_p_b.extract_one) Xtrain = np.concatenate((Xtrain_r, Xtrain_c, Xtrain_p_h, Xtrain_p_b), axis=1) Xtest = np.concatenate((Xtest_r, Xtest_c, Xtest_p_h, Xtest_p_b), axis=1) import numpy from sklearn import svm from sklearn.metrics import accuracy_score from sklearn.grid_search import GridSearchCV CS = numpy.array([100, 10, 1, 0.1, 0.01, 0.001, 0.0001]) model = svm.LinearSVC() grid_search = GridSearchCV(estimator=model, param_grid=dict(C=CS), n_jobs=3) grid_search.fit(Xtrain, ytrain_r) print 'best c:', grid_search.best_params_ a = dt.now() model = svm.LinearSVC(C=grid_search.best_params_['C']) model.fit(Xtrain, ytrain_r) b = dt.now() print 'fitted in: %s' % (b - a) a = dt.now() predictions = model.predict(Xtest) b = dt.now() print 'predicted in: %s' % (b - a) print 'accuracy', accuracy_score(ytest_r, predictions) print 'mean accuracy', utils.mean_accuracy(ytest_r, predictions)
mit
louispotok/pandas
pandas/tests/indexes/datetimes/test_partial_slicing.py
1
15706
""" test partial slicing on Series/Frame """ import pytest from datetime import datetime import numpy as np import pandas as pd import operator as op from pandas import (DatetimeIndex, Series, DataFrame, date_range, Index, Timedelta, Timestamp) from pandas.util import testing as tm class TestSlicing(object): def test_dti_slicing(self): dti = DatetimeIndex(start='1/1/2005', end='12/1/2005', freq='M') dti2 = dti[[1, 3, 5]] v1 = dti2[0] v2 = dti2[1] v3 = dti2[2] assert v1 == Timestamp('2/28/2005') assert v2 == Timestamp('4/30/2005') assert v3 == Timestamp('6/30/2005') # don't carry freq through irregular slicing assert dti2.freq is None def test_slice_keeps_name(self): # GH4226 st = pd.Timestamp('2013-07-01 00:00:00', tz='America/Los_Angeles') et = pd.Timestamp('2013-07-02 00:00:00', tz='America/Los_Angeles') dr = pd.date_range(st, et, freq='H', name='timebucket') assert dr[1:].name == dr.name def test_slice_with_negative_step(self): ts = Series(np.arange(20), date_range('2014-01-01', periods=20, freq='MS')) SLC = pd.IndexSlice def assert_slices_equivalent(l_slc, i_slc): tm.assert_series_equal(ts[l_slc], ts.iloc[i_slc]) tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc]) tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc]) assert_slices_equivalent(SLC[Timestamp('2014-10-01')::-1], SLC[9::-1]) assert_slices_equivalent(SLC['2014-10-01'::-1], SLC[9::-1]) assert_slices_equivalent(SLC[:Timestamp('2014-10-01'):-1], SLC[:8:-1]) assert_slices_equivalent(SLC[:'2014-10-01':-1], SLC[:8:-1]) assert_slices_equivalent(SLC['2015-02-01':'2014-10-01':-1], SLC[13:8:-1]) assert_slices_equivalent(SLC[Timestamp('2015-02-01'):Timestamp( '2014-10-01'):-1], SLC[13:8:-1]) assert_slices_equivalent(SLC['2015-02-01':Timestamp('2014-10-01'):-1], SLC[13:8:-1]) assert_slices_equivalent(SLC[Timestamp('2015-02-01'):'2014-10-01':-1], SLC[13:8:-1]) assert_slices_equivalent(SLC['2014-10-01':'2015-02-01':-1], SLC[:0]) def test_slice_with_zero_step_raises(self): ts = Series(np.arange(20), date_range('2014-01-01', periods=20, freq='MS')) tm.assert_raises_regex(ValueError, 'slice step cannot be zero', lambda: ts[::0]) tm.assert_raises_regex(ValueError, 'slice step cannot be zero', lambda: ts.loc[::0]) tm.assert_raises_regex(ValueError, 'slice step cannot be zero', lambda: ts.loc[::0]) def test_slice_bounds_empty(self): # GH 14354 empty_idx = DatetimeIndex(freq='1H', periods=0, end='2015') right = empty_idx._maybe_cast_slice_bound('2015-01-02', 'right', 'loc') exp = Timestamp('2015-01-02 23:59:59.999999999') assert right == exp left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc') exp = Timestamp('2015-01-02 00:00:00') assert left == exp def test_slice_duplicate_monotonic(self): # https://github.com/pandas-dev/pandas/issues/16515 idx = pd.DatetimeIndex(['2017', '2017']) result = idx._maybe_cast_slice_bound('2017-01-01', 'left', 'loc') expected = Timestamp('2017-01-01') assert result == expected def test_monotone_DTI_indexing_bug(self): # GH 19362 # Testing accessing the first element in a montononic descending # partial string indexing. df = pd.DataFrame(list(range(5))) date_list = ['2018-01-02', '2017-02-10', '2016-03-10', '2015-03-15', '2014-03-16'] date_index = pd.to_datetime(date_list) df['date'] = date_index expected = pd.DataFrame({0: list(range(5)), 'date': date_index}) tm.assert_frame_equal(df, expected) df = pd.DataFrame({'A': [1, 2, 3]}, index=pd.date_range('20170101', periods=3)[::-1]) expected = pd.DataFrame({'A': 1}, index=pd.date_range('20170103', periods=1)) tm.assert_frame_equal(df.loc['2017-01-03'], expected) def test_slice_year(self): dti = DatetimeIndex(freq='B', start=datetime(2005, 1, 1), periods=500) s = Series(np.arange(len(dti)), index=dti) result = s['2005'] expected = s[s.index.year == 2005] tm.assert_series_equal(result, expected) df = DataFrame(np.random.rand(len(dti), 5), index=dti) result = df.loc['2005'] expected = df[df.index.year == 2005] tm.assert_frame_equal(result, expected) rng = date_range('1/1/2000', '1/1/2010') result = rng.get_loc('2009') expected = slice(3288, 3653) assert result == expected def test_slice_quarter(self): dti = DatetimeIndex(freq='D', start=datetime(2000, 6, 1), periods=500) s = Series(np.arange(len(dti)), index=dti) assert len(s['2001Q1']) == 90 df = DataFrame(np.random.rand(len(dti), 5), index=dti) assert len(df.loc['1Q01']) == 90 def test_slice_month(self): dti = DatetimeIndex(freq='D', start=datetime(2005, 1, 1), periods=500) s = Series(np.arange(len(dti)), index=dti) assert len(s['2005-11']) == 30 df = DataFrame(np.random.rand(len(dti), 5), index=dti) assert len(df.loc['2005-11']) == 30 tm.assert_series_equal(s['2005-11'], s['11-2005']) def test_partial_slice(self): rng = DatetimeIndex(freq='D', start=datetime(2005, 1, 1), periods=500) s = Series(np.arange(len(rng)), index=rng) result = s['2005-05':'2006-02'] expected = s['20050501':'20060228'] tm.assert_series_equal(result, expected) result = s['2005-05':] expected = s['20050501':] tm.assert_series_equal(result, expected) result = s[:'2006-02'] expected = s[:'20060228'] tm.assert_series_equal(result, expected) result = s['2005-1-1'] assert result == s.iloc[0] pytest.raises(Exception, s.__getitem__, '2004-12-31') def test_partial_slice_daily(self): rng = DatetimeIndex(freq='H', start=datetime(2005, 1, 31), periods=500) s = Series(np.arange(len(rng)), index=rng) result = s['2005-1-31'] tm.assert_series_equal(result, s.iloc[:24]) pytest.raises(Exception, s.__getitem__, '2004-12-31 00') def test_partial_slice_hourly(self): rng = DatetimeIndex(freq='T', start=datetime(2005, 1, 1, 20, 0, 0), periods=500) s = Series(np.arange(len(rng)), index=rng) result = s['2005-1-1'] tm.assert_series_equal(result, s.iloc[:60 * 4]) result = s['2005-1-1 20'] tm.assert_series_equal(result, s.iloc[:60]) assert s['2005-1-1 20:00'] == s.iloc[0] pytest.raises(Exception, s.__getitem__, '2004-12-31 00:15') def test_partial_slice_minutely(self): rng = DatetimeIndex(freq='S', start=datetime(2005, 1, 1, 23, 59, 0), periods=500) s = Series(np.arange(len(rng)), index=rng) result = s['2005-1-1 23:59'] tm.assert_series_equal(result, s.iloc[:60]) result = s['2005-1-1'] tm.assert_series_equal(result, s.iloc[:60]) assert s[Timestamp('2005-1-1 23:59:00')] == s.iloc[0] pytest.raises(Exception, s.__getitem__, '2004-12-31 00:00:00') def test_partial_slice_second_precision(self): rng = DatetimeIndex(start=datetime(2005, 1, 1, 0, 0, 59, microsecond=999990), periods=20, freq='US') s = Series(np.arange(20), rng) tm.assert_series_equal(s['2005-1-1 00:00'], s.iloc[:10]) tm.assert_series_equal(s['2005-1-1 00:00:59'], s.iloc[:10]) tm.assert_series_equal(s['2005-1-1 00:01'], s.iloc[10:]) tm.assert_series_equal(s['2005-1-1 00:01:00'], s.iloc[10:]) assert s[Timestamp('2005-1-1 00:00:59.999990')] == s.iloc[0] tm.assert_raises_regex(KeyError, '2005-1-1 00:00:00', lambda: s['2005-1-1 00:00:00']) def test_partial_slicing_dataframe(self): # GH14856 # Test various combinations of string slicing resolution vs. # index resolution # - If string resolution is less precise than index resolution, # string is considered a slice # - If string resolution is equal to or more precise than index # resolution, string is considered an exact match formats = ['%Y', '%Y-%m', '%Y-%m-%d', '%Y-%m-%d %H', '%Y-%m-%d %H:%M', '%Y-%m-%d %H:%M:%S'] resolutions = ['year', 'month', 'day', 'hour', 'minute', 'second'] for rnum, resolution in enumerate(resolutions[2:], 2): # we check only 'day', 'hour', 'minute' and 'second' unit = Timedelta("1 " + resolution) middate = datetime(2012, 1, 1, 0, 0, 0) index = DatetimeIndex([middate - unit, middate, middate + unit]) values = [1, 2, 3] df = DataFrame({'a': values}, index, dtype=np.int64) assert df.index.resolution == resolution # Timestamp with the same resolution as index # Should be exact match for Series (return scalar) # and raise KeyError for Frame for timestamp, expected in zip(index, values): ts_string = timestamp.strftime(formats[rnum]) # make ts_string as precise as index result = df['a'][ts_string] assert isinstance(result, np.int64) assert result == expected pytest.raises(KeyError, df.__getitem__, ts_string) # Timestamp with resolution less precise than index for fmt in formats[:rnum]: for element, theslice in [[0, slice(None, 1)], [1, slice(1, None)]]: ts_string = index[element].strftime(fmt) # Series should return slice result = df['a'][ts_string] expected = df['a'][theslice] tm.assert_series_equal(result, expected) # Frame should return slice as well result = df[ts_string] expected = df[theslice] tm.assert_frame_equal(result, expected) # Timestamp with resolution more precise than index # Compatible with existing key # Should return scalar for Series # and raise KeyError for Frame for fmt in formats[rnum + 1:]: ts_string = index[1].strftime(fmt) result = df['a'][ts_string] assert isinstance(result, np.int64) assert result == 2 pytest.raises(KeyError, df.__getitem__, ts_string) # Not compatible with existing key # Should raise KeyError for fmt, res in list(zip(formats, resolutions))[rnum + 1:]: ts = index[1] + Timedelta("1 " + res) ts_string = ts.strftime(fmt) pytest.raises(KeyError, df['a'].__getitem__, ts_string) pytest.raises(KeyError, df.__getitem__, ts_string) def test_partial_slicing_with_multiindex(self): # GH 4758 # partial string indexing with a multi-index buggy df = DataFrame({'ACCOUNT': ["ACCT1", "ACCT1", "ACCT1", "ACCT2"], 'TICKER': ["ABC", "MNP", "XYZ", "XYZ"], 'val': [1, 2, 3, 4]}, index=date_range("2013-06-19 09:30:00", periods=4, freq='5T')) df_multi = df.set_index(['ACCOUNT', 'TICKER'], append=True) expected = DataFrame([ [1] ], index=Index(['ABC'], name='TICKER'), columns=['val']) result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1')] tm.assert_frame_equal(result, expected) expected = df_multi.loc[ (pd.Timestamp('2013-06-19 09:30:00', tz=None), 'ACCT1', 'ABC')] result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1', 'ABC')] tm.assert_series_equal(result, expected) # this is a KeyError as we don't do partial string selection on # multi-levels def f(): df_multi.loc[('2013-06-19', 'ACCT1', 'ABC')] pytest.raises(KeyError, f) # GH 4294 # partial slice on a series mi s = pd.DataFrame(np.random.rand(1000, 1000), index=pd.date_range( '2000-1-1', periods=1000)).stack() s2 = s[:-1].copy() expected = s2['2000-1-4'] result = s2[pd.Timestamp('2000-1-4')] tm.assert_series_equal(result, expected) result = s[pd.Timestamp('2000-1-4')] expected = s['2000-1-4'] tm.assert_series_equal(result, expected) df2 = pd.DataFrame(s) expected = df2.xs('2000-1-4') result = df2.loc[pd.Timestamp('2000-1-4')] tm.assert_frame_equal(result, expected) def test_partial_slice_doesnt_require_monotonicity(self): # For historical reasons. s = pd.Series(np.arange(10), pd.date_range('2014-01-01', periods=10)) nonmonotonic = s[[3, 5, 4]] expected = nonmonotonic.iloc[:0] timestamp = pd.Timestamp('2014-01-10') tm.assert_series_equal(nonmonotonic['2014-01-10':], expected) tm.assert_raises_regex(KeyError, r"Timestamp\('2014-01-10 00:00:00'\)", lambda: nonmonotonic[timestamp:]) tm.assert_series_equal(nonmonotonic.loc['2014-01-10':], expected) tm.assert_raises_regex(KeyError, r"Timestamp\('2014-01-10 00:00:00'\)", lambda: nonmonotonic.loc[timestamp:]) def test_loc_datetime_length_one(self): # GH16071 df = pd.DataFrame(columns=['1'], index=pd.date_range('2016-10-01T00:00:00', '2016-10-01T23:59:59')) result = df.loc[datetime(2016, 10, 1):] tm.assert_frame_equal(result, df) result = df.loc['2016-10-01T00:00:00':] tm.assert_frame_equal(result, df) @pytest.mark.parametrize('datetimelike', [ Timestamp('20130101'), datetime(2013, 1, 1), np.datetime64('2013-01-01T00:00', 'ns')]) @pytest.mark.parametrize('op,expected', [ (op.lt, [True, False, False, False]), (op.le, [True, True, False, False]), (op.eq, [False, True, False, False]), (op.gt, [False, False, False, True])]) def test_selection_by_datetimelike(self, datetimelike, op, expected): # GH issue #17965, test for ability to compare datetime64[ns] columns # to datetimelike df = DataFrame({'A': [pd.Timestamp('20120101'), pd.Timestamp('20130101'), np.nan, pd.Timestamp('20130103')]}) result = op(df.A, datetimelike) expected = Series(expected, name='A') tm.assert_series_equal(result, expected)
bsd-3-clause
dorvaljulien/StarFiddle
density_scatter_plot.py
1
4635
""" A "density scatter plot" is a scatter plot with points displaying a color corresponding to the local "point density". """ import matplotlib.pyplot as plt, numpy as np, numpy.random, scipy #mport cubehelix from kdtree import Tree log=np.log10 def DensityScatter(xdat,ydat,ax=None,NNb=15,Nbins=20,logx=False,logy=False,**kwargs): """ ax = DensityScatter( xdat, ydat,ax=None, NNb=15, Nbins=20, logx=False, logy=False, **kwargs) ------------------------------------------------------------ xdat : data array for x ydat : data array for y ax : If needed, previously existing matplotlib axis object Nnb : Number of neighbour points to compute local density Nbins : Number of density(colour) bins logx : Boolean, do you want xdata to be displayed on a logscale ? logy : Boolean, do you want ydata to be displayed on a logscale ? **kwargs : Means any additionnal keyword will be passed to plt.plot Display a scatter plot of xdat, ydat and attribute colors to points according to the local point density. Allows to visualize the distribution of points in high density regions without doing an histogram2d. """ N=len(xdat) xdat = np.array(xdat); ydat = np.array(ydat) X = (xdat - min(xdat))/(max(xdat) - min(xdat)) Y = (ydat - min(ydat))/(max(ydat) - min(ydat)) if logx: X = log(xdat/max(xdat)) if logy: Y = log(ydat/max(ydat)) T = Tree(X, Y) density = np.zeros(N) def ComputeDensity(nb,d): return nb/( np.pi*d**2) for i in range(N): _, dist = T.nnearest(i, NNb) density[i] = ComputeDensity(NNb,dist[-1]) density_bins = np.logspace( 0.5*(log(min(density))+log(max(density))), log(max(density)), Nbins) density_bins = np.array( [0] + list(density_bins) ) SelectionIndices = [] for i in range(Nbins): ind_arr = np.nonzero(( density_bins[i] <= density ) * ( density < density_bins[i+1]))[0] SelectionIndices.append(ind_arr) if ax is None: fig = plt.figure() ax = fig.add_subplot(111) cm = plt.get_cmap("rainbow") for i,(ind,alph) in enumerate(zip(SelectionIndices,np.linspace(1.,0.,Nbins))): color = cm(1.*(i)/Nbins) ax.plot( xdat[ind], ydat[ind], "o", color=color, alpha=alph, markeredgecolor="none",**kwargs) if logy: ax.set_yscale("log") if logx: ax.set_xscale("log") return ax def DensityScatter3D(xdat,ydat,zdat,ax=None,NNb=15,Nbins=20,**kwargs): """ ax = DensityScatter3D( xdat, ydat, zdat, ax=None, NNb=15, Nbins=20, **kwargs) ------------------------------------------------------------ xdat : data array for x ydat : data array for y zdat : data array for z ax : If needed, previously existing matplotlib axis object Nnb : Number of neighbour points to compute local density Nbins : Number of density(colour) bins **kwargs : Means any additionnal keyword will be passed to plt.plot Display a 3d scatter plot of xdat, ydat, zdat and attribute colors to points according to the local point density. It's kind of experimental, I played with transparency and order or display to be able to see what's going on in high density regions. """ N=len(xdat) xdat = np.array(xdat); ydat = np.array(ydat) X = (xdat - min(xdat))/(max(xdat) - min(xdat)) Y = (ydat - min(ydat))/(max(ydat) - min(ydat)) Z = (zdat - min(zdat))/(max(zdat) - min(zdat)) T = Tree(X, Y, Z) density = np.zeros(N) def ComputeDensity(nb,d): return nb/( 4./3 *np.pi*d**3) for i in range(N): _, dist = T.nnearest(i, NNb) density[i] = ComputeDensity(NNb,dist[-1]) density_bins = np.logspace( 0.5*(log(min(density))+log(max(density))), log(max(density)), Nbins) density_bins = np.array( [0] + list(density_bins) ) SelectionIndices = [] for i in range(Nbins): ind_arr = np.nonzero(( density_bins[i] <= density ) * ( density < density_bins[i+1]))[0] SelectionIndices.append(ind_arr) if ax is None: fig = plt.figure() ax = fig.add_subplot(111, projection="3d") cm = plt.get_cmap("rainbow") for i,(ind,alph) in enumerate(zip(SelectionIndices,np.linspace(1.,0.,Nbins))): color = cm(1.*(i)/Nbins) ax.plot( xdat[ind], ydat[ind], zdat[ind], "o", color=color, alpha=alph, markeredgecolor="none",**kwargs) return ax
mit
Huyuwei/tvm
docs/conf.py
2
8618
# -*- coding: utf-8 -*- # 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. # # documentation build configuration file, created by # sphinx-quickstart on Thu Jul 23 19:40:08 2015. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys import os, subprocess import shlex import recommonmark import sphinx_gallery from recommonmark.parser import CommonMarkParser from recommonmark.transform import AutoStructify # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) sys.path.insert(0, os.path.join(curr_path, '../python/')) sys.path.insert(0, os.path.join(curr_path, '../topi/python')) sys.path.insert(0, os.path.join(curr_path, '../nnvm/python')) sys.path.insert(0, os.path.join(curr_path, '../vta/python')) # -- General configuration ------------------------------------------------ # General information about the project. project = u'tvm' author = u'%s developers' % project copyright = u'2018, %s' % author github_doc_root = 'https://github.com/tqchen/tvm/tree/master/docs/' # add markdown parser CommonMarkParser.github_doc_root = github_doc_root source_parsers = { '.md': CommonMarkParser } os.environ['TVM_BUILD_DOC'] = '1' os.environ['NNVM_BUILD_DOC'] = '1' # Version information. import tvm version = tvm.__version__ release = tvm.__version__ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinx.ext.intersphinx', 'sphinx.ext.napoleon', 'sphinx.ext.mathjax', 'sphinx_gallery.gen_gallery', ] breathe_projects = {'tvm' : 'doxygen/xml/'} breathe_default_project = 'tvm' # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] source_suffix = ['.rst', '.md'] # The encoding of source files. #source_encoding = 'utf-8-sig' # generate autosummary even if no references autosummary_generate = True # The master toctree document. master_doc = 'index' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False # -- Options for HTML output ---------------------------------------------- # The theme is set by the make target html_theme = os.environ.get('TVM_THEME', 'rtd') on_rtd = os.environ.get('READTHEDOCS', None) == 'True' # only import rtd theme and set it if want to build docs locally if not on_rtd and html_theme == 'rtd': import sphinx_rtd_theme html_theme = 'sphinx_rtd_theme' html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] html_theme_options = { 'analytics_id': 'UA-75982049-2', 'logo_only': True, } html_logo = "_static/img/tvm-logo-small.png" html_favicon = "_static/img/tvm-logo-square.png" # Output file base name for HTML help builder. htmlhelp_basename = project + 'doc' # -- Options for LaTeX output --------------------------------------------- latex_elements = { } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, '%s.tex' % project, project, author, 'manual'), ] # hook for doxygen def run_doxygen(folder): """Run the doxygen make command in the designated folder.""" try: #retcode = subprocess.call("cd %s; make doc" % folder, shell=True) retcode = subprocess.call("rm -rf _build/html/doxygen", shell=True) retcode = subprocess.call("mkdir -p _build/html", shell=True) retcode = subprocess.call("cp -rf doxygen/html _build/html/doxygen", shell=True) if retcode < 0: sys.stderr.write("doxygen terminated by signal %s" % (-retcode)) except OSError as e: sys.stderr.write("doxygen execution failed: %s" % e) intersphinx_mapping = { 'python': ('https://docs.python.org/{.major}'.format(sys.version_info), None), 'numpy': ('http://docs.scipy.org/doc/numpy/', None), 'scipy': ('http://docs.scipy.org/doc/scipy/reference', None), 'matplotlib': ('http://matplotlib.org/', None), } from sphinx_gallery.sorting import ExplicitOrder examples_dirs = ["../tutorials/", "../vta/tutorials/"] gallery_dirs = ["tutorials", "vta/tutorials"] subsection_order = ExplicitOrder( ['../tutorials/frontend', '../tutorials/language', '../tutorials/optimize', '../tutorials/autotvm', '../tutorials/dev', '../tutorials/topi', '../tutorials/deployment', '../vta/tutorials/frontend', '../vta/tutorials/optimize', '../vta/tutorials/autotvm']) def generate_doxygen_xml(app): """Run the doxygen make commands if we're on the ReadTheDocs server""" run_doxygen('..') def setup(app): # Add hook for building doxygen xml when needed # no c++ API for now app.connect("builder-inited", generate_doxygen_xml) app.add_stylesheet('css/tvm_theme.css') app.add_config_value('recommonmark_config', { 'url_resolver': lambda url: github_doc_root + url, 'auto_doc_ref': True }, True) app.add_transform(AutoStructify) sphinx_gallery_conf = { 'backreferences_dir': 'gen_modules/backreferences', 'doc_module': ('tvm', 'numpy'), 'reference_url': { 'tvm': None, 'matplotlib': 'http://matplotlib.org', 'numpy': 'http://docs.scipy.org/doc/numpy-1.9.1'}, 'examples_dirs': examples_dirs, 'gallery_dirs': gallery_dirs, 'subsection_order': subsection_order, 'filename_pattern': os.environ.get("TVM_TUTORIAL_EXEC_PATTERN", ".py"), 'find_mayavi_figures': False, 'expected_failing_examples': [] }
apache-2.0
dpshelio/sunpy
examples/parse_time.py
2
3839
""" ======================================== Parsing times with sunpy.time.parse_time ======================================== This is an example to show some possible usage of ``parse_time``. ``parse_time`` is a function that can be useful to create `~astropy.time.Time` objects from various other time objects and strings. """ ############################################################################## # Import the required modules. from datetime import datetime, date import time import numpy as np import pandas from sunpy.time import parse_time ############################################################################## # Suppose you want to parse some strings, ``parse_time`` can do that. t1 = parse_time('1995-12-31 23:59:60') ############################################################################## # Of course you could do the same with `~astropy.time.Time`. # But SunPy ``parse_time`` can parse even more formats of time strings. # And as you see from the examples, thanks to `~astropy.time.Time`, ``parse_time`` # can handle leap seconds too. t2 = parse_time('1995-Dec-31 23:59:60') ############################################################################## # You can mention the scale of the time as a keyword parameter if you need. # Similar to scale you can pass in any astropy Time compatible keywords to # ``parse_time``. See all arguments # `here: <https://docs.astropy.org/en/stable/time/#creating-a-time-object>`__ t3 = parse_time('2012:124:21:08:12', scale='tai') ############################################################################## # Now that you are done with strings, let's see other type ``parse_time`` handles, # tuples. `~astropy.time.Time` does not handle tuples but ``parse_time`` does. t4 = parse_time((1998, 11, 14)) t5 = parse_time((2001, 1, 1, 12, 12, 12, 8899)) ############################################################################## # This also means that you can parse a ``time.struct_time``. t6 = parse_time(time.localtime()) ############################################################################## # ``parse_time`` also parses ``datetime`` and ``date`` objects. t7 = parse_time(datetime.now()) t8 = parse_time(date.today()) ############################################################################## # ``parse_time`` can return ``astropy.time.Time`` objects for ``pandas.Timestamp``, # ``pandas.Series`` and ``pandas.DatetimeIndex``. t9 = parse_time(pandas.Timestamp(datetime(1966, 2, 3))) t10 = parse_time( pandas.Series([[datetime(2012, 1, 1, 0, 0), datetime(2012, 1, 2, 0, 0)], [datetime(2012, 1, 3, 0, 0), datetime(2012, 1, 4, 0, 0)]])) t11 = parse_time( pandas.DatetimeIndex([ datetime(2012, 1, 1, 0, 0), datetime(2012, 1, 2, 0, 0), datetime(2012, 1, 3, 0, 0), datetime(2012, 1, 4, 0, 0) ])) ############################################################################## # ``parse_time`` can parse ``numpy.datetime64`` objects. t12 = parse_time(np.datetime64('2014-02-07T16:47:51.008288123-0500')) t13 = parse_time( np.array( ['2014-02-07T16:47:51.008288123', '2014-02-07T18:47:51.008288123'], dtype='datetime64')) ############################################################################## # Parse time returns `~astropy.time.Time` object for every parsable input that # you give to it. # ``parse_time`` can handle all formats that `~astropy.time.Time` can handle. # That is, # ['jd', 'mjd', 'decimalyear', 'unix', 'cxcsec', 'gps', 'plot_date', 'datetime', # 'iso', 'isot', 'yday', 'fits', 'byear', 'jyear', 'byear_str', 'jyear_str'] # at the time of writing. This can be used by passing format keyword argument # to ``parse_time``. parse_time(1234.0, format='jd') parse_time('B1950.0', format='byear_str')
bsd-2-clause
ekadhanda/bin
python/coda-cont.py
1
7175
#! /usr/bin/env python # Written by Vasaant S/O Krishnan Friday, 19 May 2017 # Run without arguments for instructions. import sys usrFile = sys.argv[1:] if len(usrFile) == 0: print "" print "# Script to read in file of the CODA format and plot a multivariate" print "# distribution with contours." print "# An index.txt and chain.txt file must be provided and the script" print "# will automatically identify them for internal use. Options are:" print "" print "# samp = Sample chain.txt data at this frequency (computational consideration)." print "" print " -->$ coda-cont.py CODAindex.txt CODAchain.txt samp=xx" print "" exit() import re import numpy as np import matplotlib.pyplot as plt import seaborn as sns #===================================================================== # Define variables. # ints = '\s+?([+-]?\d+)' # Integers for regex #floats = '\s+?([+-]?\d+(?:\.\d+)?)' # Floats or int floats = '\s+?([+-]?\d+(?:\.\d+)?|\.\d+)([eE][+-]?\d+)?' # Floats or int or scientific codaFiles = [] # CODAindex and CODAchain files indexFileFnd = False # CODAindex file identified? chainFileFnd = False # CODAchain file identified? indexCodes = {} # Dictionary containing CODAindex info. # chainIndx = [] # Indexes/Column 1 of CODAchain.txt file chainData = [] # Data/Column 2 of CODAchain.txt file varOne = '' # x data varTwo = '' # y data #===================================================================== #===================================================================== # Determine which are the CODAindex and CODAchain files and # automatically assign them to their respective variables. # for i in usrFile: codaSearch = re.search('.txt',i) if codaSearch: codaFiles.append(i) if len(codaFiles) == 2: # Assuming 1 index and 1 chain file for j in codaFiles: with open(j,'r') as chkTyp: # Run a quick check on the first line only firstLine = chkTyp.readline() codaIndex = re.search('^(\S+)' + ints + ints + '$', firstLine) codaChain = re.search('^(\d+)' + floats + '$', firstLine) if codaIndex: indexFile = j indexFileFnd = True if codaChain: chainFile = j chainFileFnd = True else: print "Insfficient files of CODA*.txt format." print "Check your input files." #===================================================================== #===================================================================== # Determine user requested variable from CODAIndex file # for i in usrFile: userReqCodaIndx = re.search('var=(\S+),(\S+)',i) if userReqCodaIndx: varOne = str(userReqCodaIndx.group(1)) varTwo = str(userReqCodaIndx.group(2)) #===================================================================== if indexFileFnd and chainFileFnd: #===================================================================== # Harvest index file for the variable list and corresponding # [start,stop] coords: # for line in open(indexFile, 'r'): reqIndex = re.search('^(\S+)' + ints + ints + '$', line) if reqIndex: key = str(reqIndex.group(1)) value = [int(reqIndex.group(2)), int(reqIndex.group(3))] indexCodes[key] = value maxElement = max(indexCodes, key = indexCodes.get) # The key with the largest value chainLen = max(indexCodes[maxElement]) # The largest value (expected amt. of data) if len(indexCodes) < 2: print "Insufficient variables in %s for contour plot."%(indexFile) contVarsOk = False elif len(indexCodes) == 2: varOne = indexCodes.keys()[0] varTwo = indexCodes.keys()[1] contOne = indexCodes[varOne] contTwo = indexCodes[varTwo] contVarsOk = True else: if varOne == '' or varTwo == '': print "Manually select variables for contour plot." contVarsOk = False else: contOne = indexCodes[varOne] contTwo = indexCodes[varTwo] contVarsOk = True #===================================================================== #===================================================================== # Harvest chain file # for line in open(chainFile, 'r'): reqChain = re.search('^(\d+)' + floats + '$', line) if reqChain: #chainIndx.append( int(reqChain.group(1))) chainData.append(float(reqChain.group(2))) #chainIndx = np.array(chainIndx) chainData = np.array(chainData) #===================================================================== #===================================================================== # Basic check on the harvest by comparing harvested vs. expected # no. of data. # if len(chainData) != chainLen: print " Warning! " print " %10d lines expected from %s."%(chainLen,indexFile) print " %10d lines harvested from %s."%(len(chainData),chainFile) #===================================================================== #===================================================================== # Contour plot # # if contVarsOk: dataOne = chainData[contOne[0]-1:contOne[1]] # Python starts from 0. CODAindex from 1 dataTwo = chainData[contTwo[0]-1:contTwo[1]] # Ensure same amount of data from both variables if (contOne[0]-contOne[1]) != (contTwo[0]-contTwo[1]): print " %10d lines harvested from %s."%(len(dataOne),varOne) print " %10d lines harvested from %s."%(len(dataTwo),varTwo) else: # This section to get data to the ~100s for computational consideration... if len(dataOne) >= 1000: sampleFactor = 10**int(np.floor(np.log10(len(dataOne)) - 2)) elif len(dataOne) > 500 and len(dataOne) < 1000: sampleFactor = int(len(dataOne)/5.0) else: sampleFactor = 1 # ... unless you want a customised option: for i in usrFile: userReqSamp = re.search('samp=(\d+)',i) if userReqSamp: if int(userReqSamp.group(1)) < len(dataOne): sampleFactor = int(userReqSamp.group(1)) dataOne = dataOne[0::sampleFactor] # Select data at intervals dataTwo = dataTwo[0::sampleFactor] dataComb = {varOne:dataOne, # Apparently jointplot likes dict format varTwo:dataTwo} sns.jointplot(x=varOne,y=varTwo,data=dataComb,kind="kde").set_axis_labels(varOne,varTwo) plt.show() #=====================================================================
mit
alephu5/Soundbyte
environment/lib/python3.3/site-packages/pandas/sparse/tests/test_libsparse.py
1
11260
from pandas import Series import nose from numpy import nan import numpy as np import operator from numpy.testing import assert_almost_equal, assert_equal import pandas.util.testing as tm from pandas.core.sparse import SparseSeries from pandas import DataFrame from pandas._sparse import IntIndex, BlockIndex import pandas._sparse as splib TEST_LENGTH = 20 plain_case = dict(xloc=[0, 7, 15], xlen=[3, 5, 5], yloc=[2, 9, 14], ylen=[2, 3, 5], intersect_loc=[2, 9, 15], intersect_len=[1, 3, 4]) delete_blocks = dict(xloc=[0, 5], xlen=[4, 4], yloc=[1], ylen=[4], intersect_loc=[1], intersect_len=[3]) split_blocks = dict(xloc=[0], xlen=[10], yloc=[0, 5], ylen=[3, 7], intersect_loc=[0, 5], intersect_len=[3, 5]) skip_block = dict(xloc=[10], xlen=[5], yloc=[0, 12], ylen=[5, 3], intersect_loc=[12], intersect_len=[3]) no_intersect = dict(xloc=[0, 10], xlen=[4, 6], yloc=[5, 17], ylen=[4, 2], intersect_loc=[], intersect_len=[]) def check_cases(_check_case): def _check_case_dict(case): _check_case(case['xloc'], case['xlen'], case['yloc'], case['ylen'], case['intersect_loc'], case['intersect_len']) _check_case_dict(plain_case) _check_case_dict(delete_blocks) _check_case_dict(split_blocks) _check_case_dict(skip_block) _check_case_dict(no_intersect) # one or both is empty _check_case([0], [5], [], [], [], []) _check_case([], [], [], [], [], []) def test_index_make_union(): def _check_case(xloc, xlen, yloc, ylen, eloc, elen): xindex = BlockIndex(TEST_LENGTH, xloc, xlen) yindex = BlockIndex(TEST_LENGTH, yloc, ylen) bresult = xindex.make_union(yindex) assert(isinstance(bresult, BlockIndex)) assert_equal(bresult.blocs, eloc) assert_equal(bresult.blengths, elen) ixindex = xindex.to_int_index() iyindex = yindex.to_int_index() iresult = ixindex.make_union(iyindex) assert(isinstance(iresult, IntIndex)) assert_equal(iresult.indices, bresult.to_int_index().indices) """ x: ---- y: ---- r: -------- """ xloc = [0] xlen = [5] yloc = [5] ylen = [4] eloc = [0] elen = [9] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: ----- ----- y: ----- -- """ xloc = [0, 10] xlen = [5, 5] yloc = [2, 17] ylen = [5, 2] eloc = [0, 10, 17] elen = [7, 5, 2] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: ------ y: ------- r: ---------- """ xloc = [1] xlen = [5] yloc = [3] ylen = [5] eloc = [1] elen = [7] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: ------ ----- y: ------- r: ------------- """ xloc = [2, 10] xlen = [4, 4] yloc = [4] ylen = [8] eloc = [2] elen = [12] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: --- ----- y: ------- r: ------------- """ xloc = [0, 5] xlen = [3, 5] yloc = [0] ylen = [7] eloc = [0] elen = [10] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: ------ ----- y: ------- --- r: ------------- """ xloc = [2, 10] xlen = [4, 4] yloc = [4, 13] ylen = [8, 4] eloc = [2] elen = [15] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: ---------------------- y: ---- ---- --- r: ---------------------- """ xloc = [2] xlen = [15] yloc = [4, 9, 14] ylen = [3, 2, 2] eloc = [2] elen = [15] _check_case(xloc, xlen, yloc, ylen, eloc, elen) """ x: ---- --- y: --- --- """ xloc = [0, 10] xlen = [3, 3] yloc = [5, 15] ylen = [2, 2] eloc = [0, 5, 10, 15] elen = [3, 2, 3, 2] _check_case(xloc, xlen, yloc, ylen, eloc, elen) # TODO: different-length index objects def test_lookup(): def _check(index): assert(index.lookup(0) == -1) assert(index.lookup(5) == 0) assert(index.lookup(7) == 2) assert(index.lookup(8) == -1) assert(index.lookup(9) == -1) assert(index.lookup(10) == -1) assert(index.lookup(11) == -1) assert(index.lookup(12) == 3) assert(index.lookup(17) == 8) assert(index.lookup(18) == -1) bindex = BlockIndex(20, [5, 12], [3, 6]) iindex = bindex.to_int_index() _check(bindex) _check(iindex) # corner cases def test_intersect(): def _check_correct(a, b, expected): result = a.intersect(b) assert(result.equals(expected)) def _check_length_exc(a, longer): nose.tools.assert_raises(Exception, a.intersect, longer) def _check_case(xloc, xlen, yloc, ylen, eloc, elen): xindex = BlockIndex(TEST_LENGTH, xloc, xlen) yindex = BlockIndex(TEST_LENGTH, yloc, ylen) expected = BlockIndex(TEST_LENGTH, eloc, elen) longer_index = BlockIndex(TEST_LENGTH + 1, yloc, ylen) _check_correct(xindex, yindex, expected) _check_correct(xindex.to_int_index(), yindex.to_int_index(), expected.to_int_index()) _check_length_exc(xindex, longer_index) _check_length_exc(xindex.to_int_index(), longer_index.to_int_index()) check_cases(_check_case) class TestBlockIndex(tm.TestCase): def test_equals(self): index = BlockIndex(10, [0, 4], [2, 5]) self.assert_(index.equals(index)) self.assert_(not index.equals(BlockIndex(10, [0, 4], [2, 6]))) def test_check_integrity(self): locs = [] lengths = [] # 0-length OK index = BlockIndex(0, locs, lengths) # also OK even though empty index = BlockIndex(1, locs, lengths) # block extend beyond end self.assertRaises(Exception, BlockIndex, 10, [5], [10]) # block overlap self.assertRaises(Exception, BlockIndex, 10, [2, 5], [5, 3]) def test_to_int_index(self): locs = [0, 10] lengths = [4, 6] exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15] block = BlockIndex(20, locs, lengths) dense = block.to_int_index() assert_equal(dense.indices, exp_inds) def test_to_block_index(self): index = BlockIndex(10, [0, 5], [4, 5]) self.assert_(index.to_block_index() is index) class TestIntIndex(tm.TestCase): def test_equals(self): index = IntIndex(10, [0, 1, 2, 3, 4]) self.assert_(index.equals(index)) self.assert_(not index.equals(IntIndex(10, [0, 1, 2, 3]))) def test_to_block_index(self): def _check_case(xloc, xlen, yloc, ylen, eloc, elen): xindex = BlockIndex(TEST_LENGTH, xloc, xlen) yindex = BlockIndex(TEST_LENGTH, yloc, ylen) # see if survive the round trip xbindex = xindex.to_int_index().to_block_index() ybindex = yindex.to_int_index().to_block_index() tm.assert_isinstance(xbindex, BlockIndex) self.assert_(xbindex.equals(xindex)) self.assert_(ybindex.equals(yindex)) check_cases(_check_case) def test_to_int_index(self): index = IntIndex(10, [2, 3, 4, 5, 6]) self.assert_(index.to_int_index() is index) class TestSparseOperators(tm.TestCase): def _nan_op_tests(self, sparse_op, python_op): def _check_case(xloc, xlen, yloc, ylen, eloc, elen): xindex = BlockIndex(TEST_LENGTH, xloc, xlen) yindex = BlockIndex(TEST_LENGTH, yloc, ylen) xdindex = xindex.to_int_index() ydindex = yindex.to_int_index() x = np.arange(xindex.npoints) * 10. + 1 y = np.arange(yindex.npoints) * 100. + 1 result_block_vals, rb_index = sparse_op(x, xindex, y, yindex) result_int_vals, ri_index = sparse_op(x, xdindex, y, ydindex) self.assert_(rb_index.to_int_index().equals(ri_index)) assert_equal(result_block_vals, result_int_vals) # check versus Series... xseries = Series(x, xdindex.indices) yseries = Series(y, ydindex.indices) series_result = python_op(xseries, yseries).valid() assert_equal(result_block_vals, series_result.values) assert_equal(result_int_vals, series_result.values) check_cases(_check_case) def _op_tests(self, sparse_op, python_op): def _check_case(xloc, xlen, yloc, ylen, eloc, elen): xindex = BlockIndex(TEST_LENGTH, xloc, xlen) yindex = BlockIndex(TEST_LENGTH, yloc, ylen) xdindex = xindex.to_int_index() ydindex = yindex.to_int_index() x = np.arange(xindex.npoints) * 10. + 1 y = np.arange(yindex.npoints) * 100. + 1 xfill = 0 yfill = 2 result_block_vals, rb_index = sparse_op( x, xindex, xfill, y, yindex, yfill) result_int_vals, ri_index = sparse_op(x, xdindex, xfill, y, ydindex, yfill) self.assert_(rb_index.to_int_index().equals(ri_index)) assert_equal(result_block_vals, result_int_vals) # check versus Series... xseries = Series(x, xdindex.indices) xseries = xseries.reindex(np.arange(TEST_LENGTH)).fillna(xfill) yseries = Series(y, ydindex.indices) yseries = yseries.reindex(np.arange(TEST_LENGTH)).fillna(yfill) series_result = python_op(xseries, yseries) series_result = series_result.reindex(ri_index.indices) assert_equal(result_block_vals, series_result.values) assert_equal(result_int_vals, series_result.values) check_cases(_check_case) # too cute? oh but how I abhor code duplication check_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv'] def make_nanoptestf(op): def f(self): sparse_op = getattr(splib, 'sparse_nan%s' % op) python_op = getattr(operator, op) self._nan_op_tests(sparse_op, python_op) f.__name__ = 'test_nan%s' % op return f def make_optestf(op): def f(self): sparse_op = getattr(splib, 'sparse_%s' % op) python_op = getattr(operator, op) self._op_tests(sparse_op, python_op) f.__name__ = 'test_%s' % op return f for op in check_ops: f = make_nanoptestf(op) g = make_optestf(op) setattr(TestSparseOperators, f.__name__, f) setattr(TestSparseOperators, g.__name__, g) del f del g if __name__ == '__main__': import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)
gpl-3.0
tionn/holo-at-on
code/get.py
1
1360
# -*- coding: utf-8 -*- import os import io import urllib2 import string from BeautifulSoup import BeautifulSoup import pandas as pd import sys city_url = 'http://twblg.dict.edu.tw/holodict_new/index/xiangzhen_level1.jsp?county=1' def extract_items(base_url): html = urllib2.urlopen(base_url).read() soup = BeautifulSoup(html) #print(soup.prettify()) data = [] table = soup.findAll('tr', attrs={'class':['all_space1', 'all_space2']}) for row in table: cols = row.findAll('td') cols = [ele.text.strip() for ele in cols] data.append([ele for ele in cols if ele]) # Get rid of empty values return data def get_area_url(): base_url = 'http://twblg.dict.edu.tw/holodict_new/index/xiangzhen_level1.jsp?county=%s' url = [] for i in string.ascii_uppercase: url.append(base_url % i) return url if __name__=='__main__': # 縣市名稱 data = extract_items(city_url) data.pop() # ignore data from '其他' print 'Cities and countries are done.' # 鄉鎮區名稱 area_url = get_area_url() for i in area_url: area_data = extract_items(i) data.extend(area_data) print 'Townships are done.' #df = pd.DataFrame(data, columns=['name', 'holo']) df = pd.DataFrame(data) df.to_csv('moe_mapping.csv', encoding='utf-8', index=False, header=0) print 'csv file done.'
cc0-1.0
jmetzen/scikit-learn
examples/tree/plot_tree_regression_multioutput.py
22
1848
""" =================================================================== Multi-output Decision Tree Regression =================================================================== An example to illustrate multi-output regression with decision tree. The :ref:`decision trees <tree>` is used to predict simultaneously the noisy x and y observations of a circle given a single underlying feature. As a result, it learns local linear regressions approximating the circle. We can see that if the maximum depth of the tree (controlled by the `max_depth` parameter) is set too high, the decision trees learn too fine details of the training data and learn from the noise, i.e. they overfit. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Create a random dataset rng = np.random.RandomState(1) X = np.sort(200 * rng.rand(100, 1) - 100, axis=0) y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T y[::5, :] += (0.5 - rng.rand(20, 2)) # Fit regression model regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5) regr_3 = DecisionTreeRegressor(max_depth=8) regr_1.fit(X, y) regr_2.fit(X, y) regr_3.fit(X, y) # Predict X_test = np.arange(-100.0, 100.0, 0.01)[:, np.newaxis] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test) y_3 = regr_3.predict(X_test) # Plot the results plt.figure() s = 50 plt.scatter(y[:, 0], y[:, 1], c="navy", s=s, label="data") plt.scatter(y_1[:, 0], y_1[:, 1], c="cornflowerblue", s=s, label="max_depth=2") plt.scatter(y_2[:, 0], y_2[:, 1], c="c", s=s, label="max_depth=5") plt.scatter(y_3[:, 0], y_3[:, 1], c="orange", s=s, label="max_depth=8") plt.xlim([-6, 6]) plt.ylim([-6, 6]) plt.xlabel("data") plt.ylabel("target") plt.title("Multi-output Decision Tree Regression") plt.legend() plt.show()
bsd-3-clause
wzbozon/scikit-learn
benchmarks/bench_glm.py
297
1493
""" A comparison of different methods in GLM Data comes from a random square matrix. """ from datetime import datetime import numpy as np from sklearn import linear_model from sklearn.utils.bench import total_seconds if __name__ == '__main__': import pylab as pl n_iter = 40 time_ridge = np.empty(n_iter) time_ols = np.empty(n_iter) time_lasso = np.empty(n_iter) dimensions = 500 * np.arange(1, n_iter + 1) for i in range(n_iter): print('Iteration %s of %s' % (i, n_iter)) n_samples, n_features = 10 * i + 3, 10 * i + 3 X = np.random.randn(n_samples, n_features) Y = np.random.randn(n_samples) start = datetime.now() ridge = linear_model.Ridge(alpha=1.) ridge.fit(X, Y) time_ridge[i] = total_seconds(datetime.now() - start) start = datetime.now() ols = linear_model.LinearRegression() ols.fit(X, Y) time_ols[i] = total_seconds(datetime.now() - start) start = datetime.now() lasso = linear_model.LassoLars() lasso.fit(X, Y) time_lasso[i] = total_seconds(datetime.now() - start) pl.figure('scikit-learn GLM benchmark results') pl.xlabel('Dimensions') pl.ylabel('Time (s)') pl.plot(dimensions, time_ridge, color='r') pl.plot(dimensions, time_ols, color='g') pl.plot(dimensions, time_lasso, color='b') pl.legend(['Ridge', 'OLS', 'LassoLars'], loc='upper left') pl.axis('tight') pl.show()
bsd-3-clause
hainm/scikit-learn
sklearn/utils/extmath.py
142
21102
""" Extended math utilities. """ # Authors: Gael Varoquaux # Alexandre Gramfort # Alexandre T. Passos # Olivier Grisel # Lars Buitinck # Stefan van der Walt # Kyle Kastner # License: BSD 3 clause from __future__ import division from functools import partial import warnings import numpy as np from scipy import linalg from scipy.sparse import issparse from . import check_random_state from .fixes import np_version from ._logistic_sigmoid import _log_logistic_sigmoid from ..externals.six.moves import xrange from .sparsefuncs_fast import csr_row_norms from .validation import check_array, NonBLASDotWarning def norm(x): """Compute the Euclidean or Frobenius norm of x. Returns the Euclidean norm when x is a vector, the Frobenius norm when x is a matrix (2-d array). More precise than sqrt(squared_norm(x)). """ x = np.asarray(x) nrm2, = linalg.get_blas_funcs(['nrm2'], [x]) return nrm2(x) # Newer NumPy has a ravel that needs less copying. if np_version < (1, 7, 1): _ravel = np.ravel else: _ravel = partial(np.ravel, order='K') def squared_norm(x): """Squared Euclidean or Frobenius norm of x. Returns the Euclidean norm when x is a vector, the Frobenius norm when x is a matrix (2-d array). Faster than norm(x) ** 2. """ x = _ravel(x) return np.dot(x, x) def row_norms(X, squared=False): """Row-wise (squared) Euclidean norm of X. Equivalent to np.sqrt((X * X).sum(axis=1)), but also supports CSR sparse matrices and does not create an X.shape-sized temporary. Performs no input validation. """ if issparse(X): norms = csr_row_norms(X) else: norms = np.einsum('ij,ij->i', X, X) if not squared: np.sqrt(norms, norms) return norms def fast_logdet(A): """Compute log(det(A)) for A symmetric Equivalent to : np.log(nl.det(A)) but more robust. It returns -Inf if det(A) is non positive or is not defined. """ sign, ld = np.linalg.slogdet(A) if not sign > 0: return -np.inf return ld def _impose_f_order(X): """Helper Function""" # important to access flags instead of calling np.isfortran, # this catches corner cases. if X.flags.c_contiguous: return check_array(X.T, copy=False, order='F'), True else: return check_array(X, copy=False, order='F'), False def _fast_dot(A, B): if B.shape[0] != A.shape[A.ndim - 1]: # check adopted from '_dotblas.c' raise ValueError if A.dtype != B.dtype or any(x.dtype not in (np.float32, np.float64) for x in [A, B]): warnings.warn('Data must be of same type. Supported types ' 'are 32 and 64 bit float. ' 'Falling back to np.dot.', NonBLASDotWarning) raise ValueError if min(A.shape) == 1 or min(B.shape) == 1 or A.ndim != 2 or B.ndim != 2: raise ValueError # scipy 0.9 compliant API dot = linalg.get_blas_funcs(['gemm'], (A, B))[0] A, trans_a = _impose_f_order(A) B, trans_b = _impose_f_order(B) return dot(alpha=1.0, a=A, b=B, trans_a=trans_a, trans_b=trans_b) def _have_blas_gemm(): try: linalg.get_blas_funcs(['gemm']) return True except (AttributeError, ValueError): warnings.warn('Could not import BLAS, falling back to np.dot') return False # Only use fast_dot for older NumPy; newer ones have tackled the speed issue. if np_version < (1, 7, 2) and _have_blas_gemm(): def fast_dot(A, B): """Compute fast dot products directly calling BLAS. This function calls BLAS directly while warranting Fortran contiguity. This helps avoiding extra copies `np.dot` would have created. For details see section `Linear Algebra on large Arrays`: http://wiki.scipy.org/PerformanceTips Parameters ---------- A, B: instance of np.ndarray Input arrays. Arrays are supposed to be of the same dtype and to have exactly 2 dimensions. Currently only floats are supported. In case these requirements aren't met np.dot(A, B) is returned instead. To activate the related warning issued in this case execute the following lines of code: >> import warnings >> from sklearn.utils.validation import NonBLASDotWarning >> warnings.simplefilter('always', NonBLASDotWarning) """ try: return _fast_dot(A, B) except ValueError: # Maltyped or malformed data. return np.dot(A, B) else: fast_dot = np.dot def density(w, **kwargs): """Compute density of a sparse vector Return a value between 0 and 1 """ if hasattr(w, "toarray"): d = float(w.nnz) / (w.shape[0] * w.shape[1]) else: d = 0 if w is None else float((w != 0).sum()) / w.size return d def safe_sparse_dot(a, b, dense_output=False): """Dot product that handle the sparse matrix case correctly Uses BLAS GEMM as replacement for numpy.dot where possible to avoid unnecessary copies. """ if issparse(a) or issparse(b): ret = a * b if dense_output and hasattr(ret, "toarray"): ret = ret.toarray() return ret else: return fast_dot(a, b) def randomized_range_finder(A, size, n_iter, random_state=None): """Computes an orthonormal matrix whose range approximates the range of A. Parameters ---------- A: 2D array The input data matrix size: integer Size of the return array n_iter: integer Number of power iterations used to stabilize the result random_state: RandomState or an int seed (0 by default) A random number generator instance Returns ------- Q: 2D array A (size x size) projection matrix, the range of which approximates well the range of the input matrix A. Notes ----- Follows Algorithm 4.3 of Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 (arXiv:909) http://arxiv.org/pdf/0909.4061 """ random_state = check_random_state(random_state) # generating random gaussian vectors r with shape: (A.shape[1], size) R = random_state.normal(size=(A.shape[1], size)) # sampling the range of A using by linear projection of r Y = safe_sparse_dot(A, R) del R # perform power iterations with Y to further 'imprint' the top # singular vectors of A in Y for i in xrange(n_iter): Y = safe_sparse_dot(A, safe_sparse_dot(A.T, Y)) # extracting an orthonormal basis of the A range samples Q, R = linalg.qr(Y, mode='economic') return Q def randomized_svd(M, n_components, n_oversamples=10, n_iter=0, transpose='auto', flip_sign=True, random_state=0): """Computes a truncated randomized SVD Parameters ---------- M: ndarray or sparse matrix Matrix to decompose n_components: int Number of singular values and vectors to extract. n_oversamples: int (default is 10) Additional number of random vectors to sample the range of M so as to ensure proper conditioning. The total number of random vectors used to find the range of M is n_components + n_oversamples. n_iter: int (default is 0) Number of power iterations (can be used to deal with very noisy problems). transpose: True, False or 'auto' (default) Whether the algorithm should be applied to M.T instead of M. The result should approximately be the same. The 'auto' mode will trigger the transposition if M.shape[1] > M.shape[0] since this implementation of randomized SVD tend to be a little faster in that case). flip_sign: boolean, (True by default) The output of a singular value decomposition is only unique up to a permutation of the signs of the singular vectors. If `flip_sign` is set to `True`, the sign ambiguity is resolved by making the largest loadings for each component in the left singular vectors positive. random_state: RandomState or an int seed (0 by default) A random number generator instance to make behavior Notes ----- This algorithm finds a (usually very good) approximate truncated singular value decomposition using randomization to speed up the computations. It is particularly fast on large matrices on which you wish to extract only a small number of components. References ---------- * Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 http://arxiv.org/abs/arXiv:0909.4061 * A randomized algorithm for the decomposition of matrices Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert """ random_state = check_random_state(random_state) n_random = n_components + n_oversamples n_samples, n_features = M.shape if transpose == 'auto' and n_samples > n_features: transpose = True if transpose: # this implementation is a bit faster with smaller shape[1] M = M.T Q = randomized_range_finder(M, n_random, n_iter, random_state) # project M to the (k + p) dimensional space using the basis vectors B = safe_sparse_dot(Q.T, M) # compute the SVD on the thin matrix: (k + p) wide Uhat, s, V = linalg.svd(B, full_matrices=False) del B U = np.dot(Q, Uhat) if flip_sign: U, V = svd_flip(U, V) if transpose: # transpose back the results according to the input convention return V[:n_components, :].T, s[:n_components], U[:, :n_components].T else: return U[:, :n_components], s[:n_components], V[:n_components, :] def logsumexp(arr, axis=0): """Computes the sum of arr assuming arr is in the log domain. Returns log(sum(exp(arr))) while minimizing the possibility of over/underflow. Examples -------- >>> import numpy as np >>> from sklearn.utils.extmath import logsumexp >>> a = np.arange(10) >>> np.log(np.sum(np.exp(a))) 9.4586297444267107 >>> logsumexp(a) 9.4586297444267107 """ arr = np.rollaxis(arr, axis) # Use the max to normalize, as with the log this is what accumulates # the less errors vmax = arr.max(axis=0) out = np.log(np.sum(np.exp(arr - vmax), axis=0)) out += vmax return out def weighted_mode(a, w, axis=0): """Returns an array of the weighted modal (most common) value in a If there is more than one such value, only the first is returned. The bin-count for the modal bins is also returned. This is an extension of the algorithm in scipy.stats.mode. Parameters ---------- a : array_like n-dimensional array of which to find mode(s). w : array_like n-dimensional array of weights for each value axis : int, optional Axis along which to operate. Default is 0, i.e. the first axis. Returns ------- vals : ndarray Array of modal values. score : ndarray Array of weighted counts for each mode. Examples -------- >>> from sklearn.utils.extmath import weighted_mode >>> x = [4, 1, 4, 2, 4, 2] >>> weights = [1, 1, 1, 1, 1, 1] >>> weighted_mode(x, weights) (array([ 4.]), array([ 3.])) The value 4 appears three times: with uniform weights, the result is simply the mode of the distribution. >>> weights = [1, 3, 0.5, 1.5, 1, 2] # deweight the 4's >>> weighted_mode(x, weights) (array([ 2.]), array([ 3.5])) The value 2 has the highest score: it appears twice with weights of 1.5 and 2: the sum of these is 3. See Also -------- scipy.stats.mode """ if axis is None: a = np.ravel(a) w = np.ravel(w) axis = 0 else: a = np.asarray(a) w = np.asarray(w) axis = axis if a.shape != w.shape: w = np.zeros(a.shape, dtype=w.dtype) + w scores = np.unique(np.ravel(a)) # get ALL unique values testshape = list(a.shape) testshape[axis] = 1 oldmostfreq = np.zeros(testshape) oldcounts = np.zeros(testshape) for score in scores: template = np.zeros(a.shape) ind = (a == score) template[ind] = w[ind] counts = np.expand_dims(np.sum(template, axis), axis) mostfrequent = np.where(counts > oldcounts, score, oldmostfreq) oldcounts = np.maximum(counts, oldcounts) oldmostfreq = mostfrequent return mostfrequent, oldcounts def pinvh(a, cond=None, rcond=None, lower=True): """Compute the (Moore-Penrose) pseudo-inverse of a hermetian matrix. Calculate a generalized inverse of a symmetric matrix using its eigenvalue decomposition and including all 'large' eigenvalues. Parameters ---------- a : array, shape (N, N) Real symmetric or complex hermetian matrix to be pseudo-inverted cond : float or None, default None Cutoff for 'small' eigenvalues. Singular values smaller than rcond * largest_eigenvalue are considered zero. If None or -1, suitable machine precision is used. rcond : float or None, default None (deprecated) Cutoff for 'small' eigenvalues. Singular values smaller than rcond * largest_eigenvalue are considered zero. If None or -1, suitable machine precision is used. lower : boolean Whether the pertinent array data is taken from the lower or upper triangle of a. (Default: lower) Returns ------- B : array, shape (N, N) Raises ------ LinAlgError If eigenvalue does not converge Examples -------- >>> import numpy as np >>> a = np.random.randn(9, 6) >>> a = np.dot(a, a.T) >>> B = pinvh(a) >>> np.allclose(a, np.dot(a, np.dot(B, a))) True >>> np.allclose(B, np.dot(B, np.dot(a, B))) True """ a = np.asarray_chkfinite(a) s, u = linalg.eigh(a, lower=lower) if rcond is not None: cond = rcond if cond in [None, -1]: t = u.dtype.char.lower() factor = {'f': 1E3, 'd': 1E6} cond = factor[t] * np.finfo(t).eps # unlike svd case, eigh can lead to negative eigenvalues above_cutoff = (abs(s) > cond * np.max(abs(s))) psigma_diag = np.zeros_like(s) psigma_diag[above_cutoff] = 1.0 / s[above_cutoff] return np.dot(u * psigma_diag, np.conjugate(u).T) def cartesian(arrays, out=None): """Generate a cartesian product of input arrays. Parameters ---------- arrays : list of array-like 1-D arrays to form the cartesian product of. out : ndarray Array to place the cartesian product in. Returns ------- out : ndarray 2-D array of shape (M, len(arrays)) containing cartesian products formed of input arrays. Examples -------- >>> cartesian(([1, 2, 3], [4, 5], [6, 7])) array([[1, 4, 6], [1, 4, 7], [1, 5, 6], [1, 5, 7], [2, 4, 6], [2, 4, 7], [2, 5, 6], [2, 5, 7], [3, 4, 6], [3, 4, 7], [3, 5, 6], [3, 5, 7]]) """ arrays = [np.asarray(x) for x in arrays] shape = (len(x) for x in arrays) dtype = arrays[0].dtype ix = np.indices(shape) ix = ix.reshape(len(arrays), -1).T if out is None: out = np.empty_like(ix, dtype=dtype) for n, arr in enumerate(arrays): out[:, n] = arrays[n][ix[:, n]] return out def svd_flip(u, v, u_based_decision=True): """Sign correction to ensure deterministic output from SVD. Adjusts the columns of u and the rows of v such that the loadings in the columns in u that are largest in absolute value are always positive. Parameters ---------- u, v : ndarray u and v are the output of `linalg.svd` or `sklearn.utils.extmath.randomized_svd`, with matching inner dimensions so one can compute `np.dot(u * s, v)`. u_based_decision : boolean, (default=True) If True, use the columns of u as the basis for sign flipping. Otherwise, use the rows of v. The choice of which variable to base the decision on is generally algorithm dependent. Returns ------- u_adjusted, v_adjusted : arrays with the same dimensions as the input. """ if u_based_decision: # columns of u, rows of v max_abs_cols = np.argmax(np.abs(u), axis=0) signs = np.sign(u[max_abs_cols, xrange(u.shape[1])]) u *= signs v *= signs[:, np.newaxis] else: # rows of v, columns of u max_abs_rows = np.argmax(np.abs(v), axis=1) signs = np.sign(v[xrange(v.shape[0]), max_abs_rows]) u *= signs v *= signs[:, np.newaxis] return u, v def log_logistic(X, out=None): """Compute the log of the logistic function, ``log(1 / (1 + e ** -x))``. This implementation is numerically stable because it splits positive and negative values:: -log(1 + exp(-x_i)) if x_i > 0 x_i - log(1 + exp(x_i)) if x_i <= 0 For the ordinary logistic function, use ``sklearn.utils.fixes.expit``. Parameters ---------- X: array-like, shape (M, N) Argument to the logistic function out: array-like, shape: (M, N), optional: Preallocated output array. Returns ------- out: array, shape (M, N) Log of the logistic function evaluated at every point in x Notes ----- See the blog post describing this implementation: http://fa.bianp.net/blog/2013/numerical-optimizers-for-logistic-regression/ """ is_1d = X.ndim == 1 X = check_array(X, dtype=np.float) n_samples, n_features = X.shape if out is None: out = np.empty_like(X) _log_logistic_sigmoid(n_samples, n_features, X, out) if is_1d: return np.squeeze(out) return out def safe_min(X): """Returns the minimum value of a dense or a CSR/CSC matrix. Adapated from http://stackoverflow.com/q/13426580 """ if issparse(X): if len(X.data) == 0: return 0 m = X.data.min() return m if X.getnnz() == X.size else min(m, 0) else: return X.min() def make_nonnegative(X, min_value=0): """Ensure `X.min()` >= `min_value`.""" min_ = safe_min(X) if min_ < min_value: if issparse(X): raise ValueError("Cannot make the data matrix" " nonnegative because it is sparse." " Adding a value to every entry would" " make it no longer sparse.") X = X + (min_value - min_) return X def _batch_mean_variance_update(X, old_mean, old_variance, old_sample_count): """Calculate an average mean update and a Youngs and Cramer variance update. From the paper "Algorithms for computing the sample variance: analysis and recommendations", by Chan, Golub, and LeVeque. Parameters ---------- X : array-like, shape (n_samples, n_features) Data to use for variance update old_mean : array-like, shape: (n_features,) old_variance : array-like, shape: (n_features,) old_sample_count : int Returns ------- updated_mean : array, shape (n_features,) updated_variance : array, shape (n_features,) updated_sample_count : int References ---------- T. Chan, G. Golub, R. LeVeque. Algorithms for computing the sample variance: recommendations, The American Statistician, Vol. 37, No. 3, pp. 242-247 """ new_sum = X.sum(axis=0) new_variance = X.var(axis=0) * X.shape[0] old_sum = old_mean * old_sample_count n_samples = X.shape[0] updated_sample_count = old_sample_count + n_samples partial_variance = old_sample_count / (n_samples * updated_sample_count) * ( n_samples / old_sample_count * old_sum - new_sum) ** 2 unnormalized_variance = old_variance * old_sample_count + new_variance + \ partial_variance return ((old_sum + new_sum) / updated_sample_count, unnormalized_variance / updated_sample_count, updated_sample_count) def _deterministic_vector_sign_flip(u): """Modify the sign of vectors for reproducibility Flips the sign of elements of all the vectors (rows of u) such that the absolute maximum element of each vector is positive. Parameters ---------- u : ndarray Array with vectors as its rows. Returns ------- u_flipped : ndarray with same shape as u Array with the sign flipped vectors as its rows. """ max_abs_rows = np.argmax(np.abs(u), axis=1) signs = np.sign(u[range(u.shape[0]), max_abs_rows]) u *= signs[:, np.newaxis] return u
bsd-3-clause
RMKD/networkx
examples/drawing/unix_email.py
62
2683
#!/usr/bin/env python """ Create a directed graph, allowing multiple edges and self loops, from a unix mailbox. The nodes are email addresses with links that point from the sender to the recievers. The edge data is a Python email.Message object which contains all of the email message data. This example shows the power of XDiGraph to hold edge data of arbitrary Python objects (in this case a list of email messages). By default, load the sample unix email mailbox called "unix_email.mbox". You can load your own mailbox by naming it on the command line, eg python unixemail.py /var/spool/mail/username """ __author__ = """Aric Hagberg ([email protected])""" # Copyright (C) 2005 by # Aric Hagberg <[email protected]> # Dan Schult <[email protected]> # Pieter Swart <[email protected]> # All rights reserved. # BSD license. import email from email.utils import getaddresses,parseaddr import mailbox import sys # unix mailbox recipe # see http://www.python.org/doc/current/lib/module-mailbox.html def msgfactory(fp): try: return email.message_from_file(fp) except email.Errors.MessageParseError: # Don't return None since that will stop the mailbox iterator return '' if __name__ == '__main__': import networkx as nx try: import matplotlib.pyplot as plt except: pass if len(sys.argv)==1: filePath = "unix_email.mbox" else: filePath = sys.argv[1] mbox = mailbox.mbox(filePath, msgfactory) # parse unix mailbox G=nx.MultiDiGraph() # create empty graph # parse each messages and build graph for msg in mbox: # msg is python email.Message.Message object (source_name,source_addr) = parseaddr(msg['From']) # sender # get all recipients # see http://www.python.org/doc/current/lib/module-email.Utils.html tos = msg.get_all('to', []) ccs = msg.get_all('cc', []) resent_tos = msg.get_all('resent-to', []) resent_ccs = msg.get_all('resent-cc', []) all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs) # now add the edges for this mail message for (target_name,target_addr) in all_recipients: G.add_edge(source_addr,target_addr,message=msg) # print edges with message subject for (u,v,d) in G.edges_iter(data=True): print("From: %s To: %s Subject: %s"%(u,v,d['message']["Subject"])) try: # draw pos=nx.spring_layout(G,iterations=10) nx.draw(G,pos,node_size=0,alpha=0.4,edge_color='r',font_size=16) plt.savefig("unix_email.png") plt.show() except: # matplotlib not available pass
bsd-3-clause
kdebrab/pandas
pandas/tests/frame/test_convert_to.py
3
12494
# -*- coding: utf-8 -*- from datetime import datetime import pytest import pytz import collections from collections import OrderedDict, defaultdict import numpy as np from pandas import compat from pandas.compat import long from pandas import (DataFrame, Series, MultiIndex, Timestamp, date_range) import pandas.util.testing as tm from pandas.tests.frame.common import TestData class TestDataFrameConvertTo(TestData): def test_to_dict_timestamp(self): # GH11247 # split/records producing np.datetime64 rather than Timestamps # on datetime64[ns] dtypes only tsmp = Timestamp('20130101') test_data = DataFrame({'A': [tsmp, tsmp], 'B': [tsmp, tsmp]}) test_data_mixed = DataFrame({'A': [tsmp, tsmp], 'B': [1, 2]}) expected_records = [{'A': tsmp, 'B': tsmp}, {'A': tsmp, 'B': tsmp}] expected_records_mixed = [{'A': tsmp, 'B': 1}, {'A': tsmp, 'B': 2}] assert (test_data.to_dict(orient='records') == expected_records) assert (test_data_mixed.to_dict(orient='records') == expected_records_mixed) expected_series = { 'A': Series([tsmp, tsmp], name='A'), 'B': Series([tsmp, tsmp], name='B'), } expected_series_mixed = { 'A': Series([tsmp, tsmp], name='A'), 'B': Series([1, 2], name='B'), } tm.assert_dict_equal(test_data.to_dict(orient='series'), expected_series) tm.assert_dict_equal(test_data_mixed.to_dict(orient='series'), expected_series_mixed) expected_split = { 'index': [0, 1], 'data': [[tsmp, tsmp], [tsmp, tsmp]], 'columns': ['A', 'B'] } expected_split_mixed = { 'index': [0, 1], 'data': [[tsmp, 1], [tsmp, 2]], 'columns': ['A', 'B'] } tm.assert_dict_equal(test_data.to_dict(orient='split'), expected_split) tm.assert_dict_equal(test_data_mixed.to_dict(orient='split'), expected_split_mixed) def test_to_dict_invalid_orient(self): df = DataFrame({'A': [0, 1]}) pytest.raises(ValueError, df.to_dict, orient='xinvalid') def test_to_records_dt64(self): df = DataFrame([["one", "two", "three"], ["four", "five", "six"]], index=date_range("2012-01-01", "2012-01-02")) # convert_datetime64 defaults to None expected = df.index.values[0] result = df.to_records()['index'][0] assert expected == result # check for FutureWarning if convert_datetime64=False is passed with tm.assert_produces_warning(FutureWarning): expected = df.index.values[0] result = df.to_records(convert_datetime64=False)['index'][0] assert expected == result # check for FutureWarning if convert_datetime64=True is passed with tm.assert_produces_warning(FutureWarning): expected = df.index[0] result = df.to_records(convert_datetime64=True)['index'][0] assert expected == result def test_to_records_with_multindex(self): # GH3189 index = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] data = np.zeros((8, 4)) df = DataFrame(data, index=index) r = df.to_records(index=True)['level_0'] assert 'bar' in r assert 'one' not in r def test_to_records_with_Mapping_type(self): import email from email.parser import Parser import collections collections.Mapping.register(email.message.Message) headers = Parser().parsestr('From: <[email protected]>\n' 'To: <[email protected]>\n' 'Subject: Test message\n' '\n' 'Body would go here\n') frame = DataFrame.from_records([headers]) all(x in frame for x in ['Type', 'Subject', 'From']) def test_to_records_floats(self): df = DataFrame(np.random.rand(10, 10)) df.to_records() def test_to_records_index_name(self): df = DataFrame(np.random.randn(3, 3)) df.index.name = 'X' rs = df.to_records() assert 'X' in rs.dtype.fields df = DataFrame(np.random.randn(3, 3)) rs = df.to_records() assert 'index' in rs.dtype.fields df.index = MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'z')]) df.index.names = ['A', None] rs = df.to_records() assert 'level_0' in rs.dtype.fields def test_to_records_with_unicode_index(self): # GH13172 # unicode_literals conflict with to_records result = DataFrame([{u'a': u'x', u'b': 'y'}]).set_index(u'a')\ .to_records() expected = np.rec.array([('x', 'y')], dtype=[('a', 'O'), ('b', 'O')]) tm.assert_almost_equal(result, expected) def test_to_records_with_unicode_column_names(self): # xref issue: https://github.com/numpy/numpy/issues/2407 # Issue #11879. to_records used to raise an exception when used # with column names containing non-ascii characters in Python 2 result = DataFrame(data={u"accented_name_é": [1.0]}).to_records() # Note that numpy allows for unicode field names but dtypes need # to be specified using dictionary instead of list of tuples. expected = np.rec.array( [(0, 1.0)], dtype={"names": ["index", u"accented_name_é"], "formats": ['=i8', '=f8']} ) tm.assert_almost_equal(result, expected) def test_to_records_with_categorical(self): # GH8626 # dict creation df = DataFrame({'A': list('abc')}, dtype='category') expected = Series(list('abc'), dtype='category', name='A') tm.assert_series_equal(df['A'], expected) # list-like creation df = DataFrame(list('abc'), dtype='category') expected = Series(list('abc'), dtype='category', name=0) tm.assert_series_equal(df[0], expected) # to record array # this coerces result = df.to_records() expected = np.rec.array([(0, 'a'), (1, 'b'), (2, 'c')], dtype=[('index', '=i8'), ('0', 'O')]) tm.assert_almost_equal(result, expected) @pytest.mark.parametrize('mapping', [ dict, collections.defaultdict(list), collections.OrderedDict]) def test_to_dict(self, mapping): test_data = { 'A': {'1': 1, '2': 2}, 'B': {'1': '1', '2': '2', '3': '3'}, } # GH16122 recons_data = DataFrame(test_data).to_dict(into=mapping) for k, v in compat.iteritems(test_data): for k2, v2 in compat.iteritems(v): assert (v2 == recons_data[k][k2]) recons_data = DataFrame(test_data).to_dict("l", mapping) for k, v in compat.iteritems(test_data): for k2, v2 in compat.iteritems(v): assert (v2 == recons_data[k][int(k2) - 1]) recons_data = DataFrame(test_data).to_dict("s", mapping) for k, v in compat.iteritems(test_data): for k2, v2 in compat.iteritems(v): assert (v2 == recons_data[k][k2]) recons_data = DataFrame(test_data).to_dict("sp", mapping) expected_split = {'columns': ['A', 'B'], 'index': ['1', '2', '3'], 'data': [[1.0, '1'], [2.0, '2'], [np.nan, '3']]} tm.assert_dict_equal(recons_data, expected_split) recons_data = DataFrame(test_data).to_dict("r", mapping) expected_records = [{'A': 1.0, 'B': '1'}, {'A': 2.0, 'B': '2'}, {'A': np.nan, 'B': '3'}] assert isinstance(recons_data, list) assert (len(recons_data) == 3) for l, r in zip(recons_data, expected_records): tm.assert_dict_equal(l, r) # GH10844 recons_data = DataFrame(test_data).to_dict("i") for k, v in compat.iteritems(test_data): for k2, v2 in compat.iteritems(v): assert (v2 == recons_data[k2][k]) df = DataFrame(test_data) df['duped'] = df[df.columns[0]] recons_data = df.to_dict("i") comp_data = test_data.copy() comp_data['duped'] = comp_data[df.columns[0]] for k, v in compat.iteritems(comp_data): for k2, v2 in compat.iteritems(v): assert (v2 == recons_data[k2][k]) @pytest.mark.parametrize('mapping', [ list, collections.defaultdict, []]) def test_to_dict_errors(self, mapping): # GH16122 df = DataFrame(np.random.randn(3, 3)) with pytest.raises(TypeError): df.to_dict(into=mapping) def test_to_dict_not_unique_warning(self): # GH16927: When converting to a dict, if a column has a non-unique name # it will be dropped, throwing a warning. df = DataFrame([[1, 2, 3]], columns=['a', 'a', 'b']) with tm.assert_produces_warning(UserWarning): df.to_dict() @pytest.mark.parametrize('tz', ['UTC', 'GMT', 'US/Eastern']) def test_to_records_datetimeindex_with_tz(self, tz): # GH13937 dr = date_range('2016-01-01', periods=10, freq='S', tz=tz) df = DataFrame({'datetime': dr}, index=dr) expected = df.to_records() result = df.tz_convert("UTC").to_records() # both converted to UTC, so they are equal tm.assert_numpy_array_equal(result, expected) def test_to_dict_box_scalars(self): # 14216 # make sure that we are boxing properly d = {'a': [1], 'b': ['b']} result = DataFrame(d).to_dict() assert isinstance(list(result['a'])[0], (int, long)) assert isinstance(list(result['b'])[0], (int, long)) result = DataFrame(d).to_dict(orient='records') assert isinstance(result[0]['a'], (int, long)) def test_frame_to_dict_tz(self): # GH18372 When converting to dict with orient='records' columns of # datetime that are tz-aware were not converted to required arrays data = [(datetime(2017, 11, 18, 21, 53, 0, 219225, tzinfo=pytz.utc),), (datetime(2017, 11, 18, 22, 6, 30, 61810, tzinfo=pytz.utc,),)] df = DataFrame(list(data), columns=["d", ]) result = df.to_dict(orient='records') expected = [ {'d': Timestamp('2017-11-18 21:53:00.219225+0000', tz=pytz.utc)}, {'d': Timestamp('2017-11-18 22:06:30.061810+0000', tz=pytz.utc)}, ] tm.assert_dict_equal(result[0], expected[0]) tm.assert_dict_equal(result[1], expected[1]) @pytest.mark.parametrize('into, expected', [ (dict, {0: {'int_col': 1, 'float_col': 1.0}, 1: {'int_col': 2, 'float_col': 2.0}, 2: {'int_col': 3, 'float_col': 3.0}}), (OrderedDict, OrderedDict([(0, {'int_col': 1, 'float_col': 1.0}), (1, {'int_col': 2, 'float_col': 2.0}), (2, {'int_col': 3, 'float_col': 3.0})])), (defaultdict(list), defaultdict(list, {0: {'int_col': 1, 'float_col': 1.0}, 1: {'int_col': 2, 'float_col': 2.0}, 2: {'int_col': 3, 'float_col': 3.0}})) ]) def test_to_dict_index_dtypes(self, into, expected): # GH 18580 # When using to_dict(orient='index') on a dataframe with int # and float columns only the int columns were cast to float df = DataFrame({'int_col': [1, 2, 3], 'float_col': [1.0, 2.0, 3.0]}) result = df.to_dict(orient='index', into=into) cols = ['int_col', 'float_col'] result = DataFrame.from_dict(result, orient='index')[cols] expected = DataFrame.from_dict(expected, orient='index')[cols] tm.assert_frame_equal(result, expected)
bsd-3-clause
rrohan/scikit-learn
examples/model_selection/plot_validation_curve.py
229
1823
""" ========================== Plotting Validation Curves ========================== In this plot you can see the training scores and validation scores of an SVM for different values of the kernel parameter gamma. For very low values of gamma, you can see that both the training score and the validation score are low. This is called underfitting. Medium values of gamma will result in high values for both scores, i.e. the classifier is performing fairly well. If gamma is too high, the classifier will overfit, which means that the training score is good but the validation score is poor. """ print(__doc__) import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_digits from sklearn.svm import SVC from sklearn.learning_curve import validation_curve digits = load_digits() X, y = digits.data, digits.target param_range = np.logspace(-6, -1, 5) train_scores, test_scores = validation_curve( SVC(), X, y, param_name="gamma", param_range=param_range, cv=10, scoring="accuracy", n_jobs=1) train_scores_mean = np.mean(train_scores, axis=1) train_scores_std = np.std(train_scores, axis=1) test_scores_mean = np.mean(test_scores, axis=1) test_scores_std = np.std(test_scores, axis=1) plt.title("Validation Curve with SVM") plt.xlabel("$\gamma$") plt.ylabel("Score") plt.ylim(0.0, 1.1) plt.semilogx(param_range, train_scores_mean, label="Training score", color="r") plt.fill_between(param_range, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.2, color="r") plt.semilogx(param_range, test_scores_mean, label="Cross-validation score", color="g") plt.fill_between(param_range, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.2, color="g") plt.legend(loc="best") plt.show()
bsd-3-clause
slipguru/palladio
palladio/config_templates/default_config.py
1
2485
# Configuration file example for PALLADIO # version: 2.0 import numpy as np from sklearn.feature_selection import RFE from sklearn.svm import LinearSVC from sklearn.model_selection import GridSearchCV from palladio import datasets import os ##################### # DATASET PATHS ### ##################### # * All the path are w.r.t. config file path # The list of all files required for the experiments data_path = 'data/gedm.csv' target_path = 'data/labels.csv' # pandas.read_csv options data_loading_options = { 'delimiter': ',', 'header': 0, 'index_col': 0 } target_loading_options = data_loading_options dataset = datasets.load_csv(os.path.join(os.path.dirname(__file__),data_path), os.path.join(os.path.dirname(__file__),target_path), data_loading_options=data_loading_options, target_loading_options=target_loading_options, samples_on='col') data, labels = dataset.data, dataset.target feature_names = dataset.feature_names ####################### # SESSION OPTIONS ### ####################### session_folder = 'palladio_test_session' # The learning task, if None palladio tries to guess it # [see sklearn.utils.multiclass.type_of_target] learning_task = None # The number of repetitions of 'regular' experiments n_splits_regular = 50 # The number of repetitions of 'permutation' experiments n_splits_permutation = 50 ####################### # LEARNER OPTIONS ### ####################### model = RFE(LinearSVC(loss='hinge'), step=0.3) # Set the estimator to be a GridSearchCV param_grid = { 'n_features_to_select': [10, 20, 50], 'estimator__C': np.logspace(-4, 0, 5), } estimator = GridSearchCV(model, param_grid=param_grid, cv=3, scoring='accuracy', n_jobs=1) # Set options for ModelAssessment ma_options = { 'test_size': 0.25, 'scoring': 'accuracy', 'n_jobs': -1, 'n_splits': n_splits_regular } # For the Pipeline object, indicate the name of the step from which to # retrieve the list of selected features # For a single estimator which has a `coef_` attributes (e.g., elastic net or # lasso) set to True vs_analysis = True # ~~ Signature Parameters # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ frequency_threshold = 0.75 # ~~ Plotting Options # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ score_surfaces_options = { 'logspace': ['estimator__C'], 'plot_errors': True }
gpl-3.0
DonBeo/scikit-learn
sklearn/svm/tests/test_sparse.py
1
10550
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 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 # 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 test_svc(): # Check that sparse SVC gives the same result as SVC clf = svm.SVC(kernel='linear', probability=True, random_state=0) clf.fit(X, Y) sp_clf = svm.SVC(kernel='linear', probability=True, random_state=0) sp_clf.fit(X_sp, Y) assert_array_equal(sp_clf.predict(T), true_result) assert_true(sparse.issparse(sp_clf.support_vectors_)) assert_array_almost_equal(clf.support_vectors_, sp_clf.support_vectors_.toarray()) assert_true(sparse.issparse(sp_clf.dual_coef_)) assert_array_almost_equal(clf.dual_coef_, sp_clf.dual_coef_.toarray()) assert_true(sparse.issparse(sp_clf.coef_)) assert_array_almost_equal(clf.coef_, sp_clf.coef_.toarray()) assert_array_almost_equal(clf.support_, sp_clf.support_) assert_array_almost_equal(clf.predict(T), sp_clf.predict(T)) # refit with a different dataset clf.fit(X2, Y2) sp_clf.fit(X2_sp, Y2) 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.coef_, sp_clf.coef_.toarray()) assert_array_almost_equal(clf.support_, sp_clf.support_) assert_array_almost_equal(clf.predict(T2), sp_clf.predict(T2)) assert_array_almost_equal(clf.predict_proba(T2), sp_clf.predict_proba(T2), 4) 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_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/contrib/learn/python/learn/estimators/estimator_input_test.py
10
12872
# 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. # ============================================================================== """Tests for Estimator input.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import tempfile import numpy as np from tensorflow.python.training import training_util from tensorflow.contrib.layers.python.layers import optimizers from tensorflow.contrib.learn.python.learn import metric_spec from tensorflow.contrib.learn.python.learn import models from tensorflow.contrib.learn.python.learn.datasets import base from tensorflow.contrib.learn.python.learn.estimators import _sklearn from tensorflow.contrib.learn.python.learn.estimators import estimator from tensorflow.contrib.learn.python.learn.estimators import model_fn from tensorflow.contrib.metrics.python.ops import metric_ops from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.ops import array_ops from tensorflow.python.ops import data_flow_ops from tensorflow.python.ops import math_ops from tensorflow.python.platform import test from tensorflow.python.training import input as input_lib from tensorflow.python.training import queue_runner_impl _BOSTON_INPUT_DIM = 13 _IRIS_INPUT_DIM = 4 def boston_input_fn(num_epochs=None): boston = base.load_boston() features = input_lib.limit_epochs( array_ops.reshape( constant_op.constant(boston.data), [-1, _BOSTON_INPUT_DIM]), num_epochs=num_epochs) labels = array_ops.reshape(constant_op.constant(boston.target), [-1, 1]) return features, labels def boston_input_fn_with_queue(num_epochs=None): features, labels = boston_input_fn(num_epochs=num_epochs) # Create a minimal queue runner. fake_queue = data_flow_ops.FIFOQueue(30, dtypes.int32) queue_runner = queue_runner_impl.QueueRunner(fake_queue, [constant_op.constant(0)]) queue_runner_impl.add_queue_runner(queue_runner) return features, labels def iris_input_fn(): iris = base.load_iris() features = array_ops.reshape( constant_op.constant(iris.data), [-1, _IRIS_INPUT_DIM]) labels = array_ops.reshape(constant_op.constant(iris.target), [-1]) return features, labels def iris_input_fn_labels_dict(): iris = base.load_iris() features = array_ops.reshape( constant_op.constant(iris.data), [-1, _IRIS_INPUT_DIM]) labels = { 'labels': array_ops.reshape(constant_op.constant(iris.target), [-1]) } return features, labels def boston_eval_fn(): boston = base.load_boston() n_examples = len(boston.target) features = array_ops.reshape( constant_op.constant(boston.data), [n_examples, _BOSTON_INPUT_DIM]) labels = array_ops.reshape( constant_op.constant(boston.target), [n_examples, 1]) return array_ops.concat([features, features], 0), array_ops.concat( [labels, labels], 0) def extract(data, key): if isinstance(data, dict): assert key in data return data[key] else: return data def linear_model_params_fn(features, labels, mode, params): features = extract(features, 'input') labels = extract(labels, 'labels') assert mode in (model_fn.ModeKeys.TRAIN, model_fn.ModeKeys.EVAL, model_fn.ModeKeys.INFER) prediction, loss = (models.linear_regression_zero_init(features, labels)) train_op = optimizers.optimize_loss( loss, training_util.get_global_step(), optimizer='Adagrad', learning_rate=params['learning_rate']) return prediction, loss, train_op def linear_model_fn(features, labels, mode): features = extract(features, 'input') labels = extract(labels, 'labels') assert mode in (model_fn.ModeKeys.TRAIN, model_fn.ModeKeys.EVAL, model_fn.ModeKeys.INFER) if isinstance(features, dict): (_, features), = features.items() prediction, loss = (models.linear_regression_zero_init(features, labels)) train_op = optimizers.optimize_loss( loss, training_util.get_global_step(), optimizer='Adagrad', learning_rate=0.1) return prediction, loss, train_op def linear_model_fn_with_model_fn_ops(features, labels, mode): """Same as linear_model_fn, but returns `ModelFnOps`.""" assert mode in (model_fn.ModeKeys.TRAIN, model_fn.ModeKeys.EVAL, model_fn.ModeKeys.INFER) prediction, loss = (models.linear_regression_zero_init(features, labels)) train_op = optimizers.optimize_loss( loss, training_util.get_global_step(), optimizer='Adagrad', learning_rate=0.1) return model_fn.ModelFnOps( mode=mode, predictions=prediction, loss=loss, train_op=train_op) def logistic_model_no_mode_fn(features, labels): features = extract(features, 'input') labels = extract(labels, 'labels') labels = array_ops.one_hot(labels, 3, 1, 0) prediction, loss = (models.logistic_regression_zero_init(features, labels)) train_op = optimizers.optimize_loss( loss, training_util.get_global_step(), optimizer='Adagrad', learning_rate=0.1) return { 'class': math_ops.argmax(prediction, 1), 'prob': prediction }, loss, train_op VOCAB_FILE_CONTENT = 'emerson\nlake\npalmer\n' EXTRA_FILE_CONTENT = 'kermit\npiggy\nralph\n' class EstimatorInputTest(test.TestCase): def testContinueTrainingDictionaryInput(self): boston = base.load_boston() output_dir = tempfile.mkdtemp() est = estimator.Estimator(model_fn=linear_model_fn, model_dir=output_dir) boston_input = {'input': boston.data} float64_target = {'labels': boston.target.astype(np.float64)} est.fit(x=boston_input, y=float64_target, steps=50) scores = est.evaluate( x=boston_input, y=float64_target, metrics={'MSE': metric_ops.streaming_mean_squared_error}) del est # Create another estimator object with the same output dir. est2 = estimator.Estimator(model_fn=linear_model_fn, model_dir=output_dir) # Check we can evaluate and predict. scores2 = est2.evaluate( x=boston_input, y=float64_target, metrics={'MSE': metric_ops.streaming_mean_squared_error}) self.assertAllClose(scores2['MSE'], scores['MSE']) predictions = np.array(list(est2.predict(x=boston_input))) other_score = _sklearn.mean_squared_error(predictions, float64_target['labels']) self.assertAllClose(other_score, scores['MSE']) def testBostonAll(self): boston = base.load_boston() est = estimator.SKCompat(estimator.Estimator(model_fn=linear_model_fn)) float64_labels = boston.target.astype(np.float64) est.fit(x=boston.data, y=float64_labels, steps=100) scores = est.score( x=boston.data, y=float64_labels, metrics={'MSE': metric_ops.streaming_mean_squared_error}) predictions = np.array(list(est.predict(x=boston.data))) other_score = _sklearn.mean_squared_error(predictions, boston.target) self.assertAllClose(scores['MSE'], other_score) self.assertTrue('global_step' in scores) self.assertEqual(100, scores['global_step']) def testBostonAllDictionaryInput(self): boston = base.load_boston() est = estimator.Estimator(model_fn=linear_model_fn) boston_input = {'input': boston.data} float64_target = {'labels': boston.target.astype(np.float64)} est.fit(x=boston_input, y=float64_target, steps=100) scores = est.evaluate( x=boston_input, y=float64_target, metrics={'MSE': metric_ops.streaming_mean_squared_error}) predictions = np.array(list(est.predict(x=boston_input))) other_score = _sklearn.mean_squared_error(predictions, boston.target) self.assertAllClose(other_score, scores['MSE']) self.assertTrue('global_step' in scores) self.assertEqual(scores['global_step'], 100) def testIrisAll(self): iris = base.load_iris() est = estimator.SKCompat( estimator.Estimator(model_fn=logistic_model_no_mode_fn)) est.fit(iris.data, iris.target, steps=100) scores = est.score( x=iris.data, y=iris.target, metrics={('accuracy', 'class'): metric_ops.streaming_accuracy}) predictions = est.predict(x=iris.data) predictions_class = est.predict(x=iris.data, outputs=['class'])['class'] self.assertEqual(predictions['prob'].shape[0], iris.target.shape[0]) self.assertAllClose(predictions['class'], predictions_class) self.assertAllClose( predictions['class'], np.argmax( predictions['prob'], axis=1)) other_score = _sklearn.accuracy_score(iris.target, predictions['class']) self.assertAllClose(scores['accuracy'], other_score) self.assertTrue('global_step' in scores) self.assertEqual(100, scores['global_step']) def testIrisAllDictionaryInput(self): iris = base.load_iris() est = estimator.Estimator(model_fn=logistic_model_no_mode_fn) iris_data = {'input': iris.data} iris_target = {'labels': iris.target} est.fit(iris_data, iris_target, steps=100) scores = est.evaluate( x=iris_data, y=iris_target, metrics={('accuracy', 'class'): metric_ops.streaming_accuracy}) predictions = list(est.predict(x=iris_data)) predictions_class = list(est.predict(x=iris_data, outputs=['class'])) self.assertEqual(len(predictions), iris.target.shape[0]) classes_batch = np.array([p['class'] for p in predictions]) self.assertAllClose(classes_batch, np.array([p['class'] for p in predictions_class])) self.assertAllClose( classes_batch, np.argmax( np.array([p['prob'] for p in predictions]), axis=1)) other_score = _sklearn.accuracy_score(iris.target, classes_batch) self.assertAllClose(other_score, scores['accuracy']) self.assertTrue('global_step' in scores) self.assertEqual(scores['global_step'], 100) def testIrisInputFn(self): iris = base.load_iris() est = estimator.Estimator(model_fn=logistic_model_no_mode_fn) est.fit(input_fn=iris_input_fn, steps=100) _ = est.evaluate(input_fn=iris_input_fn, steps=1) predictions = list(est.predict(x=iris.data)) self.assertEqual(len(predictions), iris.target.shape[0]) def testIrisInputFnLabelsDict(self): iris = base.load_iris() est = estimator.Estimator(model_fn=logistic_model_no_mode_fn) est.fit(input_fn=iris_input_fn_labels_dict, steps=100) _ = est.evaluate( input_fn=iris_input_fn_labels_dict, steps=1, metrics={ 'accuracy': metric_spec.MetricSpec( metric_fn=metric_ops.streaming_accuracy, prediction_key='class', label_key='labels') }) predictions = list(est.predict(x=iris.data)) self.assertEqual(len(predictions), iris.target.shape[0]) def testTrainInputFn(self): est = estimator.Estimator(model_fn=linear_model_fn) est.fit(input_fn=boston_input_fn, steps=1) _ = est.evaluate(input_fn=boston_eval_fn, steps=1) def testPredictInputFn(self): est = estimator.Estimator(model_fn=linear_model_fn) boston = base.load_boston() est.fit(input_fn=boston_input_fn, steps=1) input_fn = functools.partial(boston_input_fn, num_epochs=1) output = list(est.predict(input_fn=input_fn)) self.assertEqual(len(output), boston.target.shape[0]) def testPredictInputFnWithQueue(self): est = estimator.Estimator(model_fn=linear_model_fn) boston = base.load_boston() est.fit(input_fn=boston_input_fn, steps=1) input_fn = functools.partial(boston_input_fn_with_queue, num_epochs=2) output = list(est.predict(input_fn=input_fn)) self.assertEqual(len(output), boston.target.shape[0] * 2) def testPredictConstInputFn(self): est = estimator.Estimator(model_fn=linear_model_fn) boston = base.load_boston() est.fit(input_fn=boston_input_fn, steps=1) def input_fn(): features = array_ops.reshape( constant_op.constant(boston.data), [-1, _BOSTON_INPUT_DIM]) labels = array_ops.reshape(constant_op.constant(boston.target), [-1, 1]) return features, labels output = list(est.predict(input_fn=input_fn)) self.assertEqual(len(output), boston.target.shape[0]) if __name__ == '__main__': test.main()
apache-2.0
arabenjamin/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
mo-g/iris
lib/iris/quickplot.py
3
8992
# (C) British Crown Copyright 2010 - 2015, Met Office # # This file is part of Iris. # # Iris 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. # # Iris 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 Iris. If not, see <http://www.gnu.org/licenses/>. """ High-level plotting extensions to :mod:`iris.plot`. These routines work much like their :mod:`iris.plot` counterparts, but they automatically add a plot title, axis titles, and a colour bar when appropriate. See also: :ref:`matplotlib <matplotlib:users-guide-index>`. """ from __future__ import (absolute_import, division, print_function) from six.moves import (filter, input, map, range, zip) # noqa import cf_units import matplotlib.pyplot as plt import iris.config import iris.coords import iris.plot as iplt def _use_symbol(units): # For non-time units use the shortest unit representation. # E.g. prefer 'K' over 'kelvin', but not '0.0174532925199433 rad' # over 'degrees' return (not units.is_time() and not units.is_time_reference() and len(units.symbol) < len(str(units))) def _title(cube_or_coord, with_units): if cube_or_coord is None: title = '' else: title = cube_or_coord.name().replace('_', ' ').capitalize() units = cube_or_coord.units if with_units and not (units.is_unknown() or units.is_no_unit() or units == cf_units.Unit('1')): if _use_symbol(units): units = units.symbol title += ' / {}'.format(units) return title def _label(cube, mode, result=None, ndims=2, coords=None): """Puts labels on the current plot using the given cube.""" plt.title(_title(cube, with_units=False)) if result is not None: draw_edges = mode == iris.coords.POINT_MODE bar = plt.colorbar(result, orientation='horizontal', drawedges=draw_edges) has_known_units = not (cube.units.is_unknown() or cube.units.is_no_unit()) if has_known_units and cube.units != cf_units.Unit('1'): # Use shortest unit representation for anything other than time if _use_symbol(cube.units): bar.set_label(cube.units.symbol) else: bar.set_label(cube.units) # Remove the tick which is put on the colorbar by default. bar.ax.tick_params(length=0) if coords is None: plot_defn = iplt._get_plot_defn(cube, mode, ndims) else: plot_defn = iplt._get_plot_defn_custom_coords_picked( cube, coords, mode, ndims=ndims) if ndims == 2: if not iplt._can_draw_map(plot_defn.coords): plt.ylabel(_title(plot_defn.coords[0], with_units=True)) plt.xlabel(_title(plot_defn.coords[1], with_units=True)) elif ndims == 1: plt.xlabel(_title(plot_defn.coords[0], with_units=True)) plt.ylabel(_title(cube, with_units=True)) else: msg = 'Unexpected number of dimensions (%s) given to _label.' % ndims raise ValueError(msg) def _label_with_bounds(cube, result=None, ndims=2, coords=None): _label(cube, iris.coords.BOUND_MODE, result, ndims, coords) def _label_with_points(cube, result=None, ndims=2, coords=None): _label(cube, iris.coords.POINT_MODE, result, ndims, coords) def _get_titles(u_object, v_object): if u_object is None: u_object = iplt._u_object_from_v_object(v_object) xunits = u_object is not None and not u_object.units.is_time_reference() yunits = not v_object.units.is_time_reference() xlabel = _title(u_object, with_units=xunits) ylabel = _title(v_object, with_units=yunits) title = '' if u_object is None: title = _title(v_object, with_units=False) elif isinstance(u_object, iris.cube.Cube) and \ not isinstance(v_object, iris.cube.Cube): title = _title(u_object, with_units=False) elif isinstance(v_object, iris.cube.Cube) and \ not isinstance(u_object, iris.cube.Cube): title = _title(v_object, with_units=False) return xlabel, ylabel, title def _label_1d_plot(*args): if len(args) > 1 and isinstance(args[1], (iris.cube.Cube, iris.coords.Coord)): xlabel, ylabel, title = _get_titles(*args[:2]) else: xlabel, ylabel, title = _get_titles(None, args[0]) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) def contour(cube, *args, **kwargs): """ Draws contour lines on a labelled plot based on the given Cube. With the basic call signature, contour "level" values are chosen automatically:: contour(cube) Supply a number to use *N* automatically chosen levels:: contour(cube, N) Supply a sequence *V* to use explicitly defined levels:: contour(cube, V) See :func:`iris.plot.contour` for details of valid keyword arguments. """ coords = kwargs.get('coords') result = iplt.contour(cube, *args, **kwargs) _label_with_points(cube, coords=coords) return result def contourf(cube, *args, **kwargs): """ Draws filled contours on a labelled plot based on the given Cube. With the basic call signature, contour "level" values are chosen automatically:: contour(cube) Supply a number to use *N* automatically chosen levels:: contour(cube, N) Supply a sequence *V* to use explicitly defined levels:: contour(cube, V) See :func:`iris.plot.contourf` for details of valid keyword arguments. """ coords = kwargs.get('coords') result = iplt.contourf(cube, *args, **kwargs) _label_with_points(cube, result, coords=coords) return result def outline(cube, coords=None, color='k', linewidth=None): """ Draws cell outlines on a labelled plot based on the given Cube. Kwargs: * coords: list of :class:`~iris.coords.Coord` objects or coordinate names Use the given coordinates as the axes for the plot. The order of the given coordinates indicates which axis to use for each, where the first element is the horizontal axis of the plot and the second element is the vertical axis of the plot. * color: None or mpl color The color of the cell outlines. If None, the matplotlibrc setting patch.edgecolor is used by default. * linewidth: None or number The width of the lines showing the cell outlines. If None, the default width in patch.linewidth in matplotlibrc is used. """ result = iplt.outline(cube, color=color, linewidth=linewidth, coords=coords) _label_with_bounds(cube, coords=coords) return result def pcolor(cube, *args, **kwargs): """ Draws a labelled pseudocolor plot based on the given Cube. See :func:`iris.plot.pcolor` for details of valid keyword arguments. """ coords = kwargs.get('coords') result = iplt.pcolor(cube, *args, **kwargs) _label_with_bounds(cube, result, coords=coords) return result def pcolormesh(cube, *args, **kwargs): """ Draws a labelled pseudocolour plot based on the given Cube. See :func:`iris.plot.pcolormesh` for details of valid keyword arguments. """ coords = kwargs.get('coords') result = iplt.pcolormesh(cube, *args, **kwargs) _label_with_bounds(cube, result, coords=coords) return result def points(cube, *args, **kwargs): """ Draws sample point positions on a labelled plot based on the given Cube. See :func:`iris.plot.points` for details of valid keyword arguments. """ coords = kwargs.get('coords') result = iplt.points(cube, *args, **kwargs) _label_with_points(cube, coords=coords) return result def plot(*args, **kwargs): """ Draws a labelled line plot based on the given cube(s) or coordinate(s). See :func:`iris.plot.plot` for details of valid arguments and keyword arguments. """ result = iplt.plot(*args, **kwargs) _label_1d_plot(*args) return result def scatter(x, y, *args, **kwargs): """ Draws a labelled scatter plot based on the given cubes or coordinates. See :func:`iris.plot.scatter` for details of valid arguments and keyword arguments. """ result = iplt.scatter(x, y, *args, **kwargs) _label_1d_plot(x, y) return result # Provide a convenience show method from pyplot. show = plt.show
gpl-3.0
liyinwei/pandas
quickstart/12_getting_data_in_out.py
1
1191
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @Author: liyinwei @E-mail: [email protected] @Time: 2016/11/21 11:35 @Description: 1.文件读写,包括: a)csv Writing to a csv file: http://pandas.pydata.org/pandas-docs/stable/io.html#io-store-in-csv Reading from a csv file: http://pandas.pydata.org/pandas-docs/stable/io.html#io-read-csv-table b)HDF5: http://pandas.pydata.org/pandas-docs/stable/io.html#io-hdf5 c)Excel: http://pandas.pydata.org/pandas-docs/stable/io.html#io-excel """ """ ID: 12_01 Desc: Writing to a csv file """ # df.to_csv('foo.csv') """ ID: 12_02 Desc: Reading from a csv file """ # pd.read_csv('foo.csv') """ ID: 12_03 Desc: Writing to a HDF5 Store """ # df.to_hdf('foo.h5','df') """ ID: 12_04 Desc: Reading from a HDF5 Store """ # pd.read_hdf('foo.h5','df') """ ID: 12_05 Desc: Writing to an excel file """ # df.to_excel('foo.xlsx', sheet_name='Sheet1') """ ID: 12_06 Desc: Reading from an excel file """ # pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA']) if __name__ == '__main__': pass
gpl-3.0
ryandougherty/mwa-capstone
MWA_Tools/build/matplotlib/doc/mpl_examples/pylab_examples/legend_auto.py
7
2267
""" This file was written to test matplotlib's autolegend placement algorithm, but shows lots of different ways to create legends so is useful as a general examples Thanks to John Gill and Phil ?? for help at the matplotlib sprint at pycon 2005 where the auto-legend support was written. """ from pylab import * import sys rcParams['legend.loc'] = 'best' N = 100 x = arange(N) def fig_1(): figure(1) t = arange(0, 40.0 * pi, 0.1) l, = plot(t, 100*sin(t), 'r', label='sine') legend() def fig_2(): figure(2) plot(x, 'o', label='x=y') legend() def fig_3(): figure(3) plot(x, -x, 'o', label='x= -y') legend() def fig_4(): figure(4) plot(x, ones(len(x)), 'o', label='y=1') plot(x, -ones(len(x)), 'o', label='y=-1') legend() def fig_5(): figure(5) n, bins, patches = hist(randn(1000), 40, normed=1) l, = plot(bins, normpdf(bins, 0.0, 1.0), 'r--', label='fit', linewidth=3) legend([l, patches[0]], ['fit', 'hist']) def fig_6(): figure(6) plot(x, 50-x, 'o', label='y=1') plot(x, x-50, 'o', label='y=-1') legend() def fig_7(): figure(7) xx = x - (N/2.0) plot(xx, (xx*xx)-1225, 'bo', label='$y=x^2$') plot(xx, 25*xx, 'go', label='$y=25x$') plot(xx, -25*xx, 'mo', label='$y=-25x$') legend() def fig_8(): figure(8) b1 = bar(x, x, color='m') b2 = bar(x, x[::-1], color='g') legend([b1[0], b2[0]], ['up', 'down']) def fig_9(): figure(9) b1 = bar(x, -x) b2 = bar(x, -x[::-1], color='r') legend([b1[0], b2[0]], ['down', 'up']) def fig_10(): figure(10) b1 = bar(x, x, bottom=-100, color='m') b2 = bar(x, x[::-1], bottom=-100, color='g') b3 = bar(x, -x, bottom=100) b4 = bar(x, -x[::-1], bottom=100, color='r') legend([b1[0], b2[0], b3[0], b4[0]], ['bottom right', 'bottom left', 'top left', 'top right']) if __name__ == '__main__': nfigs = 10 figures = [] for f in sys.argv[1:]: try: figures.append(int(f)) except ValueError: pass if len(figures) == 0: figures = range(1, nfigs+1) for fig in figures: fn_name = "fig_%d" % fig fn = globals()[fn_name] fn() show()
gpl-2.0
paulrbrenner/GOS
examples/migration/visualization/plotlyviz.py
2
1188
import plotly import pandas as pd def map(dataframe, title = "Map", colorbarName = None): #---The next line and the line at bottom are for the Jupyter Notebook--- #plotly.offline.init_notebook_mode(connected=True) data = [ dict( type = 'choropleth', locations = dataframe['country'], z = dataframe['value'], # text = dataframe['name'], colorscale = [[0,"rgb(215,25,28)"],[0.25,"rgb(253,174,97)"],[0.5,"rgb(255,255,191)"],[0.75,"rgb(166,217,106)"],[1,"rgb(26,150,65)"]], autocolorscale = False, reversescale = False, marker = dict( line = dict ( color = 'rgb(180,180,180)', width = 0.5 ) ), colorbar = dict( autotick = False, # tickprefix = 'V', title = colorbarName), ) ] layout = dict( title = title, titlefont = dict( size = 60 ), geo = dict( showframe = False, showcoastlines = False, showcountries = True, countrycolor = "#f0f0f0", projection = dict( type = 'Mercator' ) ) ) fig = dict( data=data, layout=layout ) #plotly.offline.iplot(fig, validate=False, filename='plotly-map' ) plotly.offline.plot(fig, validate=False, filename='plotly-map' )
apache-2.0
awni/tensorflow
tensorflow/examples/skflow/multiple_gpu.py
1
1664
# Copyright 2015-present The Scikit Flow 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. from __future__ import absolute_import from __future__ import division from __future__ import print_function from sklearn import datasets, metrics, cross_validation import tensorflow as tf from tensorflow.contrib import skflow iris = datasets.load_iris() X_train, X_test, y_train, y_test = cross_validation.train_test_split(iris.data, iris.target, test_size=0.2, random_state=42) def my_model(X, y): """ This is DNN with 10, 20, 10 hidden layers, and dropout of 0.5 probability. Note: If you want to run this example with multiple GPUs, Cuda Toolkit 7.0 and CUDNN 6.5 V2 from NVIDIA need to be installed beforehand. """ with tf.device('/gpu:1'): layers = skflow.ops.dnn(X, [10, 20, 10], keep_prob=0.5) with tf.device('/gpu:2'): return skflow.models.logistic_regression(layers, y) classifier = skflow.TensorFlowEstimator(model_fn=my_model, n_classes=3) classifier.fit(X_train, y_train) score = metrics.accuracy_score(y_test, classifier.predict(X_test)) print('Accuracy: {0:f}'.format(score))
apache-2.0
skudriashev/incubator-airflow
airflow/hooks/base_hook.py
8
2950
# -*- coding: utf-8 -*- # # 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 division from __future__ import print_function from __future__ import unicode_literals import os import random from airflow import settings from airflow.models import Connection from airflow.exceptions import AirflowException from airflow.utils.log.logging_mixin import LoggingMixin CONN_ENV_PREFIX = 'AIRFLOW_CONN_' class BaseHook(LoggingMixin): """ Abstract base class for hooks, hooks are meant as an interface to interact with external systems. MySqlHook, HiveHook, PigHook return object that can handle the connection and interaction to specific instances of these systems, and expose consistent methods to interact with them. """ def __init__(self, source): pass @classmethod def _get_connections_from_db(cls, conn_id): session = settings.Session() db = ( session.query(Connection) .filter(Connection.conn_id == conn_id) .all() ) session.expunge_all() session.close() if not db: raise AirflowException( "The conn_id `{0}` isn't defined".format(conn_id)) return db @classmethod def _get_connection_from_env(cls, conn_id): environment_uri = os.environ.get(CONN_ENV_PREFIX + conn_id.upper()) conn = None if environment_uri: conn = Connection(conn_id=conn_id, uri=environment_uri) return conn @classmethod def get_connections(cls, conn_id): conn = cls._get_connection_from_env(conn_id) if conn: conns = [conn] else: conns = cls._get_connections_from_db(conn_id) return conns @classmethod def get_connection(cls, conn_id): conn = random.choice(cls.get_connections(conn_id)) if conn.host: log = LoggingMixin().log log.info("Using connection to: %s", conn.host) return conn @classmethod def get_hook(cls, conn_id): connection = cls.get_connection(conn_id) return connection.get_hook() def get_conn(self): raise NotImplementedError() def get_records(self, sql): raise NotImplementedError() def get_pandas_df(self, sql): raise NotImplementedError() def run(self, sql): raise NotImplementedError()
apache-2.0
tinkerinestudio/Tinkerine-Suite
TinkerineSuite/python/Lib/numpy/core/function_base.py
82
5474
__all__ = ['logspace', 'linspace'] import numeric as _nx from numeric import array def linspace(start, stop, num=50, endpoint=True, retstep=False): """ Return evenly spaced numbers over a specified interval. Returns `num` evenly spaced samples, calculated over the interval [`start`, `stop` ]. The endpoint of the interval can optionally be excluded. Parameters ---------- start : scalar The starting value of the sequence. stop : scalar The end value of the sequence, unless `endpoint` is set to False. In that case, the sequence consists of all but the last of ``num + 1`` evenly spaced samples, so that `stop` is excluded. Note that the step size changes when `endpoint` is False. num : int, optional Number of samples to generate. Default is 50. endpoint : bool, optional If True, `stop` is the last sample. Otherwise, it is not included. Default is True. retstep : bool, optional If True, return (`samples`, `step`), where `step` is the spacing between samples. Returns ------- samples : ndarray There are `num` equally spaced samples in the closed interval ``[start, stop]`` or the half-open interval ``[start, stop)`` (depending on whether `endpoint` is True or False). step : float (only if `retstep` is True) Size of spacing between samples. See Also -------- arange : Similiar to `linspace`, but uses a step size (instead of the number of samples). logspace : Samples uniformly distributed in log space. Examples -------- >>> np.linspace(2.0, 3.0, num=5) array([ 2. , 2.25, 2.5 , 2.75, 3. ]) >>> np.linspace(2.0, 3.0, num=5, endpoint=False) array([ 2. , 2.2, 2.4, 2.6, 2.8]) >>> np.linspace(2.0, 3.0, num=5, retstep=True) (array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25) Graphical illustration: >>> import matplotlib.pyplot as plt >>> N = 8 >>> y = np.zeros(N) >>> x1 = np.linspace(0, 10, N, endpoint=True) >>> x2 = np.linspace(0, 10, N, endpoint=False) >>> plt.plot(x1, y, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.plot(x2, y + 0.5, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.ylim([-0.5, 1]) (-0.5, 1) >>> plt.show() """ num = int(num) if num <= 0: return array([], float) if endpoint: if num == 1: return array([float(start)]) step = (stop-start)/float((num-1)) y = _nx.arange(0, num) * step + start y[-1] = stop else: step = (stop-start)/float(num) y = _nx.arange(0, num) * step + start if retstep: return y, step else: return y def logspace(start,stop,num=50,endpoint=True,base=10.0): """ Return numbers spaced evenly on a log scale. In linear space, the sequence starts at ``base ** start`` (`base` to the power of `start`) and ends with ``base ** stop`` (see `endpoint` below). Parameters ---------- start : float ``base ** start`` is the starting value of the sequence. stop : float ``base ** stop`` is the final value of the sequence, unless `endpoint` is False. In that case, ``num + 1`` values are spaced over the interval in log-space, of which all but the last (a sequence of length ``num``) are returned. num : integer, optional Number of samples to generate. Default is 50. endpoint : boolean, optional If true, `stop` is the last sample. Otherwise, it is not included. Default is True. base : float, optional The base of the log space. The step size between the elements in ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform. Default is 10.0. Returns ------- samples : ndarray `num` samples, equally spaced on a log scale. See Also -------- arange : Similiar to linspace, with the step size specified instead of the number of samples. Note that, when used with a float endpoint, the endpoint may or may not be included. linspace : Similar to logspace, but with the samples uniformly distributed in linear space, instead of log space. Notes ----- Logspace is equivalent to the code >>> y = np.linspace(start, stop, num=num, endpoint=endpoint) ... # doctest: +SKIP >>> power(base, y) ... # doctest: +SKIP Examples -------- >>> np.logspace(2.0, 3.0, num=4) array([ 100. , 215.443469 , 464.15888336, 1000. ]) >>> np.logspace(2.0, 3.0, num=4, endpoint=False) array([ 100. , 177.827941 , 316.22776602, 562.34132519]) >>> np.logspace(2.0, 3.0, num=4, base=2.0) array([ 4. , 5.0396842 , 6.34960421, 8. ]) Graphical illustration: >>> import matplotlib.pyplot as plt >>> N = 10 >>> x1 = np.logspace(0.1, 1, N, endpoint=True) >>> x2 = np.logspace(0.1, 1, N, endpoint=False) >>> y = np.zeros(N) >>> plt.plot(x1, y, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.plot(x2, y + 0.5, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.ylim([-0.5, 1]) (-0.5, 1) >>> plt.show() """ y = linspace(start,stop,num=num,endpoint=endpoint) return _nx.power(base,y)
agpl-3.0
kwentz10/Photosynthesis_Optimization_Modeling
Traits_Physical_Factorial_CummulativeGS.py
1
9910
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu May 25 10:10:28 2017 @author: Katherine """ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue May 23 16:04:21 2017 @author: Katherine """ # -*- coding: utf-8 -*- """ Photosynthesis and Stomatal Conductance Model Created 9/27/2016 Katherine Wentz This is a program that runs photosynthesis and stomatal conductance models given changes in leaf- level traits. The end product is graphs of NUE vs. WUE. Update: I am going to run the model for plants with traits that are distinctive of the meadow moisture gradient in the alpine tundra. Fix: correct for atmospheric pressure differences in co2, o2, and vapor pressure Fix: vcmax temp dependence (pg 63 in plant physiological ecology book) Fix: NEW VARIBALE TRAIT-->make the fraction of leaf N in rubisco go down with increasing SLA, chlorophyll content, and decreasing light (wet meadow)--more N is allocated to thylakoids. The only way for chl/m2 to increase even when g N/m2 goes down or is constant is for the leaf to allocate more of leaf N to chl...also, note that there is more organic N designated to photo in leaf when SLA goes up because less N is used in structure. see "Photosynthesis or persistence: N allocation in leaves of evergreen and deciduous... by Takashima et al. 2004. Also see Photosynthetic nitrogen-use efficiency of species...by Poorter and Evans 1998 Note to self: NUE and WUE relationship flipflops with change in air temperature; NUE makes sense because C:N decreases from dry to wet meadows; WUE increasing in snowbed does not necessarilly make sense--look in the literature for this herbs have a higher NUE """ #---------------Import Modules---------------# import itertools as it import numpy as np from matplotlib import pyplot as plt #Import combinations of variable parameters from uncertain_params import monte_carlo #Import photosynthesis model from Photosynthesis_Model import photo_bound_meso_eqstom as photo #Import functions to switch between Pa and umol/mol at sea level from photo_functions import pa_con_atmfrac #import timeseries of vwc and temp from time_dep_params import surtemp_dm, surtemp_wm, vwc_dm, vwc_wm, na_dm_min_inter,na_wm_min_inter,na_dm_max_inter,na_wm_max_inter #---------------Determine if I Want to Keep Any of the Variable Parameters Constant---------------# const_params=[] for xxx in it.combinations(['ht'],0): #keep ht and t constant for constant vpd const_params+=[xxx] #do this when I do not put any of the variable parameters as constant. instead I #vary each parameter one at a time while keeping the other parameters constant. if const_params==[()]: const_params=[[-999999]] #---------------Begin Looping Through Photosynthesis Model---------------# #each loop is for a constant value, or combinatin of constant values, of variable parameter as determined above for ii in range(len(const_params)): #---------------Run through time series---------------# days=np.linspace(1,365,365) #dry meadow tot_nue_dm_avg=[] tot_wue_dm_avg=[] tot_nue_dm_min=[] tot_wue_dm_min=[] tot_nue_dm_max=[] tot_wue_dm_max=[] tot_A_dm_avg=[] tot_gs_dm_avg=[] #moist meadow tot_nue_mm_avg=[] tot_wue_mm_avg=[] tot_nue_mm_min=[] tot_wue_mm_min=[] tot_nue_mm_max=[] tot_wue_mm_max=[] tot_A_mm_avg=[] tot_gs_mm_avg=[] #wet meadow tot_nue_wm_avg=[] tot_wue_wm_avg=[] tot_nue_wm_min=[] tot_wue_wm_min=[] tot_nue_wm_max=[] tot_wue_wm_max=[] tot_A_wm_avg=[] tot_gs_wm_avg=[] #---------------Photosynthesis + Stomatal Conductance Model---------------# ##---Constant Parameter Arrays for Model---## #----Params Used in Model Currently----# tk_25=298.16; #absolute temperature at 25 C ekc=80500.0 #Activation energy for K of CO2 (J mol-1) eko=14500.0 #Activation energy for K of O2 (J mol-1) etau=-29000.0 #Activation energy for tau (???) (J mol-1) ev=55000.0 #Activation energy for carboxylation (J mol-1) ej=55000.0 #Activation energy for electron transport (J mol-1) toptv=303.0 #Optimum temperature for maximum carboxylation (K) toptj=303.0 #Optimum temperature for maximum electron transport (K) ra=np.zeros(shape=1)+20.7 #specific rubisco activity (umol CO2/g Rub s) flnr=np.zeros(shape=1)+0.1 #fraction of leaf nitrogen in rubisco (g N Rub/g N leaf) frnr=np.zeros(shape=1)+6.25 #weight fraction of nitrogen in rubisco molecule (g Rub/g N Rub) rh=np.zeros(shape=1)+0.5 #relative humidity (kPa/kPa) ca=np.zeros(shape=1)+405 #ambient carbon dioxide (umol CO2/mol air) ko25=np.zeros(shape=1)+30000 #Michaelis-Menten kinetic coefficient for oxygen at 25 C(Pa) kc25=np.zeros(shape=1)+30 #Michaelis-Menten kinetic coefficient for carbon dioxide at 25 C (Pa) o=np.zeros(shape=1)+210000 #concentration of ambient oxygen (umol/mol) g0=np.zeros(shape=1)+0.002 #Ball-Berry stomatal conductance intercept parameter (mol H2O/m2s) a=np.zeros(shape=1)+1.6 #Conversion Coefficient between stomatal conductance to water and carbon dioxide (unitless) ij=np.zeros(shape=1)+1.0 #leaf angle index--downregulates jmax m=np.zeros(shape=1)+9.0 #ball-berry parameter (unitless) b=1.37 #Conversion Coefficient between boundary layer conductance to water and carbon dioxide u=5.0 #windspeed (m/s) qeff=0.32 #leaf quantum yield, electrons PAR=2000 #photosynthetic active radiation (umol/m2s) jm=2.68 #slope coefficient vwc_min=0.08 #minimum soil water content for photosynthesis to occur (permanent wilting point) (cm3/cm3) vwc_max=0.68 #maximum soil water content where increases in soil water do not affect photosynthesis (field capacity?) (cm3/cm3) q=0.2 #parameter for soil water affect on photosynthesis (unitless) #------constant variable params for sensitivty analysis-----# chl_c=np.zeros(shape=1)+(np.mean([396,465,476])) #Chlorophyll Content of the Leaf (umol chl/m2) ht_c=np.zeros(shape=1)+(np.mean([9.2,19.5,20.0])) #Temperature of the Leaf (K) dia_c=np.zeros(shape=1)+(np.mean([1.4,2.3,2.6])/100.) #Mean diameter or size of leaf (m) na_c=np.zeros(shape=1)+(np.mean([2.5,5.6,6.3])) #leaf nitrogen (g N/ m2) t_c=np.zeros(shape=1)+15.0 #temp (C) #-----which timeseries should I use--based on factorial meadow type---# na_min=[na_dm_min_inter, na_dm_min_inter, na_wm_min_inter, na_wm_min_inter] na_max=[na_dm_max_inter, na_dm_max_inter, na_wm_max_inter, na_wm_max_inter] vwc_type=[vwc_dm,vwc_dm,vwc_wm,vwc_wm] temp_type=[surtemp_dm,surtemp_dm,surtemp_wm,surtemp_wm] A_tot_all=[] chl_mean=[[395.7132],[475.8913],[395.7132],[475.8913]] chl_sd=[[24.410199999999975],[29.185099999999977],[24.410199999999975],[29.185099999999977]] dia_mean=[[1.6/100.],[3.0/100.],[1.6/100.],[3.0/100.]] dia_sd=[[0.9/100.0],[1.2/100.0],[0.9/100.0],[1.2/100.0]] ht_mean=[[9.183549],[19.98519],[9.183549],[19.98519]] ht_sd=[[1.5],[3.1],[1.5],[3.1]] depth=[0.2,0.2,0.2,0.4,0.4,0.4] #---------------Import Variable Parameter Arrays from Leaf Parameter File---------------# for iii in range(len(chl_mean)): A_tot=0 for time in range(129): params=monte_carlo(chl_mean[iii], chl_sd[iii], dia_mean[iii], dia_sd[iii], [na_min[iii][time]], [na_max[iii][time]], ht_mean[iii], ht_sd[iii]) A_day=[] for xx in range(len(params)): for yy in range(len(params[xx])): for key,val in params[xx][yy].items(): exec(key + '=val') #set variable parameters constant if I specify this above if 'na' in const_params[ii]: na=na_c if 'dia' in const_params[ii]: dia=dia_c if 'chl' in const_params[ii]: chl=chl_c if 'ht' in const_params[ii]: ht=ht_c #------calculate vapor pressure-----# pa_v=611*np.exp((17.27*temp_type[iii][time])/(temp_type[iii][time]+237.3)) #saturation vapor pressure of air (Pa) ea_str=pa_con_atmfrac(pa_v,3528) #saturation vapor pressure of air (Pa-->umol h20/mol air) ea=rh*ea_str #vapor pressure (umol h2O/mol air) #correct for leaf temperatures using leaf height t_diff=18-0.4*ht tl=temp_type[iii][time]+t_diff z=depth[iii] #---------------Photosynthesis Function---------------# #alter this line of code for when implementing different photosynthesis functions wue, nue, A, E, cs, ci, gsw, gs, gbw, gb, gm, cc,dd =photo(tk_25,ekc,eko,etau,ev,ej,toptv,toptj,na, qeff, PAR,tl,ea,chl,ij,kc25,ko25,o,ca,rh,m,a,frnr,flnr,ra,jm,g0,b,dia,u,q,vwc_min,vwc_max,vwc_type[iii][time],z) #test to make sure wue and nue are positive at not 'nan' if wue[0]==-999 and nue[0]==-999: continue if np.isnan(A[0]): A[0]=0.0 A_day+=[(A[0]*3600*6)/1000000.*44.] A_tot+=np.mean(A_day) A_tot_all+=[A_tot] print A_tot_all print A_tot_all
mit
tu-rbo/omip
shape_reconstruction/src/plot_statistics.py
1
1935
#!/usr/bin/python ############## # View the statistics for a single experiment (bag file) # # Statistics are plotted separately, i.e. one window per object per variant, # showing precision and recall. import numpy as np import matplotlib.pylab as plt import matplotlib matplotlib.rcParams['ps.useafm'] = True matplotlib.rcParams['pdf.use14corefonts'] = True matplotlib.rcParams['text.usetex'] = True import sys import os import os.path def plot_statistics(folder): for f in os.listdir(folder): if f[-3:] != "txt": continue filename = os.path.join(folder, f) data = np.genfromtxt(filename, dtype=float, delimiter=' ', names=True) precision = data['tp'] / (data['tp']+data['fp']) recall = data['tp'] / (data['tp']+data['fn']) seg_acc = data['tp'] / (data['tp']+data['fp']+data['fn']) time = data['time'] - data['time'][0] # clean up precision[np.where(np.isnan(precision))] = 0. recall[np.where(np.isnan(recall))] = 0. seg_acc[np.where(np.isnan(seg_acc))] = 0. plt.figure(figsize=(7.5,5)) for y in np.arange(0.0, 1.1, 0.2): plt.plot(time, [y] * len(time), "--", lw=0.5, color="black", alpha=0.3) plt.plot(time, seg_acc, "k", label="Segmentation Accuracy", lw=3.0) plt.plot(time, precision, "b", label="Precision", lw=2.0, ls="--") plt.plot(time, recall, "r", label="Recall", lw=2.0, ls="--") plt.xlim(0, data['time'][-1] - data['time'][0]) plt.ylim(-0.1, 1.1) plt.xlabel("time") plt.title(os.path.basename(filename[:-3])) plt.legend(loc=4) img_path = os.path.join(folder, f[:-3]+"pdf") print ("Saving image at %s" % img_path) plt.savefig(img_path, bbox_inches="tight") if __name__ == "__main__": if len(sys.argv) != 2: print "Usage: plot_statistics.py <experiment folder>" sys.exit() print ("Starting plot statistics") plot_statistics(sys.argv[1]) plt.show()
mit
nrhine1/scikit-learn
examples/decomposition/plot_kernel_pca.py
353
2011
""" ========== Kernel PCA ========== This example shows that Kernel PCA is able to find a projection of the data that makes data linearly separable. """ print(__doc__) # Authors: Mathieu Blondel # Andreas Mueller # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA, KernelPCA from sklearn.datasets import make_circles np.random.seed(0) X, y = make_circles(n_samples=400, factor=.3, noise=.05) kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10) X_kpca = kpca.fit_transform(X) X_back = kpca.inverse_transform(X_kpca) pca = PCA() X_pca = pca.fit_transform(X) # Plot results plt.figure() plt.subplot(2, 2, 1, aspect='equal') plt.title("Original space") reds = y == 0 blues = y == 1 plt.plot(X[reds, 0], X[reds, 1], "ro") plt.plot(X[blues, 0], X[blues, 1], "bo") plt.xlabel("$x_1$") plt.ylabel("$x_2$") X1, X2 = np.meshgrid(np.linspace(-1.5, 1.5, 50), np.linspace(-1.5, 1.5, 50)) X_grid = np.array([np.ravel(X1), np.ravel(X2)]).T # projection on the first principal component (in the phi space) Z_grid = kpca.transform(X_grid)[:, 0].reshape(X1.shape) plt.contour(X1, X2, Z_grid, colors='grey', linewidths=1, origin='lower') plt.subplot(2, 2, 2, aspect='equal') plt.plot(X_pca[reds, 0], X_pca[reds, 1], "ro") plt.plot(X_pca[blues, 0], X_pca[blues, 1], "bo") plt.title("Projection by PCA") plt.xlabel("1st principal component") plt.ylabel("2nd component") plt.subplot(2, 2, 3, aspect='equal') plt.plot(X_kpca[reds, 0], X_kpca[reds, 1], "ro") plt.plot(X_kpca[blues, 0], X_kpca[blues, 1], "bo") plt.title("Projection by KPCA") plt.xlabel("1st principal component in space induced by $\phi$") plt.ylabel("2nd component") plt.subplot(2, 2, 4, aspect='equal') plt.plot(X_back[reds, 0], X_back[reds, 1], "ro") plt.plot(X_back[blues, 0], X_back[blues, 1], "bo") plt.title("Original space after inverse transform") plt.xlabel("$x_1$") plt.ylabel("$x_2$") plt.subplots_adjust(0.02, 0.10, 0.98, 0.94, 0.04, 0.35) plt.show()
bsd-3-clause
cjayb/mne-python
mne/decoding/receptive_field.py
2
20189
# -*- coding: utf-8 -*- # Authors: Chris Holdgraf <[email protected]> # Eric Larson <[email protected]> # License: BSD (3-clause) import numbers import numpy as np from scipy import linalg from .base import get_coef, BaseEstimator, _check_estimator from .time_delaying_ridge import TimeDelayingRidge from ..fixes import is_regressor from ..utils import _validate_type, verbose class ReceptiveField(BaseEstimator): """Fit a receptive field model. This allows you to fit an encoding model (stimulus to brain) or a decoding model (brain to stimulus) using time-lagged input features (for example, a spectro- or spatio-temporal receptive field, or STRF). Parameters ---------- tmin : float The starting lag, in seconds (or samples if ``sfreq`` == 1). tmax : float The ending lag, in seconds (or samples if ``sfreq`` == 1). Must be >= tmin. sfreq : float The sampling frequency used to convert times into samples. feature_names : array, shape (n_features,) | None Names for input features to the model. If None, feature names will be auto-generated from the shape of input data after running `fit`. estimator : instance of sklearn.base.BaseEstimator | float | None The model used in fitting inputs and outputs. This can be any scikit-learn-style model that contains a fit and predict method. If a float is passed, it will be interpreted as the ``alpha`` parameter to be passed to a Ridge regression model. If `None`, then a Ridge regression model with an alpha of 0 will be used. fit_intercept : bool | None If True (default), the sample mean is removed before fitting. If ``estimator`` is a :class:`sklearn.base.BaseEstimator`, this must be None or match ``estimator.fit_intercept``. scoring : ['r2', 'corrcoef'] Defines how predictions will be scored. Currently must be one of 'r2' (coefficient of determination) or 'corrcoef' (the correlation coefficient). patterns : bool If True, inverse coefficients will be computed upon fitting using the covariance matrix of the inputs, and the cross-covariance of the inputs/outputs, according to [5]_. Defaults to False. n_jobs : int | str Number of jobs to run in parallel. Can be 'cuda' if CuPy is installed properly and ``estimator is None``. .. versionadded:: 0.18 edge_correction : bool If True (default), correct the autocorrelation coefficients for non-zero delays for the fact that fewer samples are available. Disabling this speeds up performance at the cost of accuracy depending on the relationship between epoch length and model duration. Only used if ``estimator`` is float or None. .. versionadded:: 0.18 verbose : bool, str, int, or None If not None, override default verbose level (see :func:`mne.verbose` and :ref:`Logging documentation <tut_logging>` for more). Attributes ---------- coef_ : array, shape ([n_outputs, ]n_features, n_delays) The coefficients from the model fit, reshaped for easy visualization. During :meth:`mne.decoding.ReceptiveField.fit`, if ``y`` has one dimension (time), the ``n_outputs`` dimension here is omitted. patterns_ : array, shape ([n_outputs, ]n_features, n_delays) If fit, the inverted coefficients from the model. delays_ : array, shape (n_delays,), dtype int The delays used to fit the model, in indices. To return the delays in seconds, use ``self.delays_ / self.sfreq`` valid_samples_ : slice The rows to keep during model fitting after removing rows with missing values due to time delaying. This can be used to get an output equivalent to using :func:`numpy.convolve` or :func:`numpy.correlate` with ``mode='valid'``. See Also -------- mne.decoding.TimeDelayingRidge Notes ----- For a causal system, the encoding model will have significant non-zero values only at positive lags. In other words, lags point backward in time relative to the input, so positive lags correspond to previous input time samples, while negative lags correspond to future input time samples. References ---------- .. [1] Theunissen, F. E. et al. Estimating spatio-temporal receptive fields of auditory and visual neurons from their responses to natural stimuli. Network 12, 289-316 (2001). .. [2] Willmore, B. & Smyth, D. Methods for first-order kernel estimation: simple-cell receptive fields from responses to natural scenes. Network 14, 553-77 (2003). .. [3] Crosse, M. J., Di Liberto, G. M., Bednar, A. & Lalor, E. C. (2016). The Multivariate Temporal Response Function (mTRF) Toolbox: A MATLAB Toolbox for Relating Neural Signals to Continuous Stimuli. Frontiers in Human Neuroscience 10, 604. doi:10.3389/fnhum.2016.00604 .. [4] Holdgraf, C. R. et al. Rapid tuning shifts in human auditory cortex enhance speech intelligibility. Nature Communications, 7, 13654 (2016). doi:10.1038/ncomms13654 .. [5] Haufe, S., Meinecke, F., Goergen, K., Daehne, S., Haynes, J.-D., Blankertz, B., & Biessmann, F. (2014). On the interpretation of weight vectors of linear models in multivariate neuroimaging. NeuroImage, 87, 96-110. doi:10.1016/j.neuroimage.2013.10.067 """ @verbose def __init__(self, tmin, tmax, sfreq, feature_names=None, estimator=None, fit_intercept=None, scoring='r2', patterns=False, n_jobs=1, edge_correction=True, verbose=None): self.feature_names = feature_names self.sfreq = float(sfreq) self.tmin = tmin self.tmax = tmax self.estimator = 0. if estimator is None else estimator self.fit_intercept = fit_intercept self.scoring = scoring self.patterns = patterns self.n_jobs = n_jobs self.edge_correction = edge_correction self.verbose = verbose def __repr__(self): # noqa: D105 s = "tmin, tmax : (%.3f, %.3f), " % (self.tmin, self.tmax) estimator = self.estimator if not isinstance(estimator, str): estimator = type(self.estimator) s += "estimator : %s, " % (estimator,) if hasattr(self, 'coef_'): if self.feature_names is not None: feats = self.feature_names if len(feats) == 1: s += "feature: %s, " % feats[0] else: s += "features : [%s, ..., %s], " % (feats[0], feats[-1]) s += "fit: True" else: s += "fit: False" if hasattr(self, 'scores_'): s += "scored (%s)" % self.scoring return "<ReceptiveField | %s>" % s def _delay_and_reshape(self, X, y=None): """Delay and reshape the variables.""" if not isinstance(self.estimator_, TimeDelayingRidge): # X is now shape (n_times, n_epochs, n_feats, n_delays) X = _delay_time_series(X, self.tmin, self.tmax, self.sfreq, fill_mean=self.fit_intercept) X = _reshape_for_est(X) # Concat times + epochs if y is not None: y = y.reshape(-1, y.shape[-1], order='F') return X, y @verbose def fit(self, X, y): """Fit a receptive field model. Parameters ---------- X : array, shape (n_times[, n_epochs], n_features) The input features for the model. y : array, shape (n_times[, n_epochs][, n_outputs]) The output features for the model. Returns ------- self : instance The instance so you can chain operations. """ if self.scoring not in _SCORERS.keys(): raise ValueError('scoring must be one of %s, got' '%s ' % (sorted(_SCORERS.keys()), self.scoring)) from sklearn.base import clone X, y, _, self._y_dim = self._check_dimensions(X, y) if self.tmin > self.tmax: raise ValueError('tmin (%s) must be at most tmax (%s)' % (self.tmin, self.tmax)) # Initialize delays self.delays_ = _times_to_delays(self.tmin, self.tmax, self.sfreq) # Define the slice that we should use in the middle self.valid_samples_ = _delays_to_slice(self.delays_) if isinstance(self.estimator, numbers.Real): if self.fit_intercept is None: self.fit_intercept = True estimator = TimeDelayingRidge( self.tmin, self.tmax, self.sfreq, alpha=self.estimator, fit_intercept=self.fit_intercept, n_jobs=self.n_jobs, edge_correction=self.edge_correction) elif is_regressor(self.estimator): estimator = clone(self.estimator) if self.fit_intercept is not None and \ estimator.fit_intercept != self.fit_intercept: raise ValueError( 'Estimator fit_intercept (%s) != initialization ' 'fit_intercept (%s), initialize ReceptiveField with the ' 'same fit_intercept value or use fit_intercept=None' % (estimator.fit_intercept, self.fit_intercept)) self.fit_intercept = estimator.fit_intercept else: raise ValueError('`estimator` must be a float or an instance' ' of `BaseEstimator`,' ' got type %s.' % type(self.estimator)) self.estimator_ = estimator del estimator _check_estimator(self.estimator_) # Create input features n_times, n_epochs, n_feats = X.shape n_outputs = y.shape[-1] n_delays = len(self.delays_) # Update feature names if we have none if ((self.feature_names is not None) and (len(self.feature_names) != n_feats)): raise ValueError('n_features in X does not match feature names ' '(%s != %s)' % (n_feats, len(self.feature_names))) # Create input features X, y = self._delay_and_reshape(X, y) self.estimator_.fit(X, y) coef = get_coef(self.estimator_, 'coef_') # (n_targets, n_features) shape = [n_feats, n_delays] if self._y_dim > 1: shape.insert(0, -1) self.coef_ = coef.reshape(shape) # Inverse-transform model weights if self.patterns: if isinstance(self.estimator_, TimeDelayingRidge): cov_ = self.estimator_.cov_ / float(n_times * n_epochs - 1) y = y.reshape(-1, y.shape[-1], order='F') else: X = X - X.mean(0, keepdims=True) cov_ = np.cov(X.T) del X # Inverse output covariance if y.ndim == 2 and y.shape[1] != 1: y = y - y.mean(0, keepdims=True) inv_Y = linalg.pinv(np.cov(y.T)) else: inv_Y = 1. / float(n_times * n_epochs - 1) del y # Inverse coef according to Haufe's method # patterns has shape (n_feats * n_delays, n_outputs) coef = np.reshape(self.coef_, (n_feats * n_delays, n_outputs)) patterns = cov_.dot(coef.dot(inv_Y)) self.patterns_ = patterns.reshape(shape) return self def predict(self, X): """Generate predictions with a receptive field. Parameters ---------- X : array, shape (n_times[, n_epochs], n_channels) The input features for the model. Returns ------- y_pred : array, shape (n_times[, n_epochs][, n_outputs]) The output predictions. "Note that valid samples (those unaffected by edge artifacts during the time delaying step) can be obtained using ``y_pred[rf.valid_samples_]``. """ if not hasattr(self, 'delays_'): raise ValueError('Estimator has not been fit yet.') X, _, X_dim = self._check_dimensions(X, None, predict=True)[:3] del _ # convert to sklearn and back pred_shape = X.shape[:-1] if self._y_dim > 1: pred_shape = pred_shape + (self.coef_.shape[0],) X, _ = self._delay_and_reshape(X) y_pred = self.estimator_.predict(X) y_pred = y_pred.reshape(pred_shape, order='F') shape = list(y_pred.shape) if X_dim <= 2: shape.pop(1) # epochs extra = 0 else: extra = 1 shape = shape[:self._y_dim + extra] y_pred.shape = shape return y_pred def score(self, X, y): """Score predictions generated with a receptive field. This calls ``self.predict``, then masks the output of this and ``y` with ``self.mask_prediction_``. Finally, it passes this to a :mod:`sklearn.metrics` scorer. Parameters ---------- X : array, shape (n_times[, n_epochs], n_channels) The input features for the model. y : array, shape (n_times[, n_epochs][, n_outputs]) Used for scikit-learn compatibility. Returns ------- scores : list of float, shape (n_outputs,) The scores estimated by the model for each output (e.g. mean R2 of ``predict(X)``). """ # Create our scoring object scorer_ = _SCORERS[self.scoring] # Generate predictions, then reshape so we can mask time X, y = self._check_dimensions(X, y, predict=True)[:2] n_times, n_epochs, n_outputs = y.shape y_pred = self.predict(X) y_pred = y_pred[self.valid_samples_] y = y[self.valid_samples_] # Re-vectorize and call scorer y = y.reshape([-1, n_outputs], order='F') y_pred = y_pred.reshape([-1, n_outputs], order='F') assert y.shape == y_pred.shape scores = scorer_(y, y_pred, multioutput='raw_values') return scores def _check_dimensions(self, X, y, predict=False): X_dim = X.ndim y_dim = y.ndim if y is not None else 0 if X_dim == 2: # Ensure we have a 3D input by adding singleton epochs dimension X = X[:, np.newaxis, :] if y is not None: if y_dim == 1: y = y[:, np.newaxis, np.newaxis] # epochs, outputs elif y_dim == 2: y = y[:, np.newaxis, :] # epochs else: raise ValueError('y must be shape (n_times[, n_epochs]' '[,n_outputs], got %s' % (y.shape,)) elif X.ndim == 3: if y is not None: if y.ndim == 2: y = y[:, :, np.newaxis] # Add an outputs dim elif y.ndim != 3: raise ValueError('If X has 3 dimensions, ' 'y must have 2 or 3 dimensions') else: raise ValueError('X must be shape (n_times[, n_epochs],' ' n_features), got %s' % (X.shape,)) if y is not None: if X.shape[0] != y.shape[0]: raise ValueError('X and y do not have the same n_times\n' '%s != %s' % (X.shape[0], y.shape[0])) if X.shape[1] != y.shape[1]: raise ValueError('X and y do not have the same n_epochs\n' '%s != %s' % (X.shape[1], y.shape[1])) if predict and y.shape[-1] != len(self.estimator_.coef_): raise ValueError('Number of outputs does not match' ' estimator coefficients dimensions') return X, y, X_dim, y_dim def _delay_time_series(X, tmin, tmax, sfreq, fill_mean=False): """Return a time-lagged input time series. Parameters ---------- X : array, shape (n_times[, n_epochs], n_features) The time series to delay. Must be 2D or 3D. tmin : int | float The starting lag. tmax : int | float The ending lag. Must be >= tmin. sfreq : int | float The sampling frequency of the series. Defaults to 1.0. fill_mean : bool If True, the fill value will be the mean along the time dimension of the feature, and each cropped and delayed segment of data will be shifted to have the same mean value (ensuring that mean subtraction works properly). If False, the fill value will be zero. Returns ------- delayed : array, shape(n_times[, n_epochs][, n_features], n_delays) The delayed data. It has the same shape as X, with an extra dimension appended to the end. Examples -------- >>> tmin, tmax = -0.1, 0.2 >>> sfreq = 10. >>> x = np.arange(1, 6) >>> x_del = _delay_time_series(x, tmin, tmax, sfreq) >>> print(x_del) # doctest:+SKIP [[2. 1. 0. 0.] [3. 2. 1. 0.] [4. 3. 2. 1.] [5. 4. 3. 2.] [0. 5. 4. 3.]] """ _check_delayer_params(tmin, tmax, sfreq) delays = _times_to_delays(tmin, tmax, sfreq) # Iterate through indices and append delayed = np.zeros(X.shape + (len(delays),)) if fill_mean: mean_value = X.mean(axis=0) if X.ndim == 3: mean_value = np.mean(mean_value, axis=0) delayed[:] = mean_value[:, np.newaxis] for ii, ix_delay in enumerate(delays): # Create zeros to populate w/ delays if ix_delay < 0: out = delayed[:ix_delay, ..., ii] use_X = X[-ix_delay:] elif ix_delay > 0: out = delayed[ix_delay:, ..., ii] use_X = X[:-ix_delay] else: # == 0 out = delayed[..., ii] use_X = X out[:] = use_X if fill_mean: out[:] += (mean_value - use_X.mean(axis=0)) return delayed def _times_to_delays(tmin, tmax, sfreq): """Convert a tmin/tmax in seconds to delays.""" # Convert seconds to samples delays = np.arange(int(np.round(tmin * sfreq)), int(np.round(tmax * sfreq) + 1)) return delays def _delays_to_slice(delays): """Find the slice to be taken in order to remove missing values.""" # Negative values == cut off rows at the end min_delay = None if delays[-1] <= 0 else delays[-1] # Positive values == cut off rows at the end max_delay = None if delays[0] >= 0 else delays[0] return slice(min_delay, max_delay) def _check_delayer_params(tmin, tmax, sfreq): """Check delayer input parameters. For future custom delay support.""" _validate_type(sfreq, 'numeric', '`sfreq`') for tlim in (tmin, tmax): _validate_type(tlim, 'numeric', 'tmin/tmax') if not tmin <= tmax: raise ValueError('tmin must be <= tmax') def _reshape_for_est(X_del): """Convert X_del to a sklearn-compatible shape.""" n_times, n_epochs, n_feats, n_delays = X_del.shape X_del = X_del.reshape(n_times, n_epochs, -1) # concatenate feats X_del = X_del.reshape(n_times * n_epochs, -1, order='F') return X_del # Create a correlation scikit-learn-style scorer def _corr_score(y_true, y, multioutput=None): from scipy.stats import pearsonr assert multioutput == 'raw_values' for this_y in (y_true, y): if this_y.ndim != 2: raise ValueError('inputs must be shape (samples, outputs), got %s' % (this_y.shape,)) return np.array([pearsonr(y_true[:, ii], y[:, ii])[0] for ii in range(y.shape[-1])]) def _r2_score(y_true, y, multioutput=None): from sklearn.metrics import r2_score return r2_score(y_true, y, multioutput=multioutput) _SCORERS = {'r2': _r2_score, 'corrcoef': _corr_score}
bsd-3-clause
IntelLabs/hpat
sdc/datatypes/hpat_pandas_stringmethods_functions.py
1
44761
# ***************************************************************************** # Copyright (c) 2020, Intel Corporation All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ***************************************************************************** """ | :class:`pandas.core.strings.StringMethods` functions and operators implementations in HPAT .. only:: developer This is autogenerated sources for all Unicode string functions supported by Numba. Currently tested 45 functions only. List of functions obtained automatically from `numba.types.misc.UnicodeType` class Example of the generated method (for method upper()): `hpat_pandas_stringmethods_upper_parallel_impl` is paralell version (required additional import mentioned in the body) @sdc_overload_method(StringMethodsType, 'upper') def hpat_pandas_stringmethods_upper(self): ty_checker = TypeChecker('Method stringmethods.upper().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_upper_parallel_impl(self): from numba.parfor import (init_prange, min_checker, internal_prange) init_prange() result = [] item_count = len(self._data) min_checker(item_count) for i in internal_prange(item_count): item = self._data[i] item_method = item.upper() result.append(item_method) return pandas.Series(result) return hpat_pandas_stringmethods_upper_parallel_impl def hpat_pandas_stringmethods_upper_impl(self): result = [] item_count = len(self._data) for i in range(item_count): item = self._data[i] item_method = item.upper() result.append(item_method) return pandas.Series(result) return hpat_pandas_stringmethods_upper_impl Test: python -m sdc.runtests sdc.tests.test_hiframes.TestHiFrames.test_str_split_filter """ import numpy import pandas import numba from numba.types import (Boolean, Integer, NoneType, Omitted, StringLiteral, UnicodeType) from sdc.utilities.sdc_typing_utils import TypeChecker from sdc.datatypes.hpat_pandas_stringmethods_types import StringMethodsType from sdc.utilities.utils import sdc_overload_method from sdc.hiframes.api import get_nan_mask from sdc.str_arr_ext import str_arr_set_na_by_mask, create_str_arr_from_list _hpat_pandas_stringmethods_autogen_global_dict = { 'pandas': pandas, 'numpy': numpy, 'numba': numba, 'StringMethodsType': StringMethodsType, 'TypeChecker': TypeChecker } _hpat_pandas_stringmethods_functions_params = { 'cat': ', others=None, sep=None, na_rep=None, join="left"', 'center': ', width, fillchar=" "', 'contains': ', pat, case=True, flags=0, na=numpy.nan, regex=True', 'count': ', pat, flags=0', 'decode': ', encoding, errors="strict"', 'encode': ', encoding, errors="strict"', 'endswith': ', pat, na=numpy.nan', 'extractall': ', pat, flags=0', 'extract': ', pat, flags=0, expand=True', 'findall': ', pat, flags=0', 'find': ', sub, start=0, end=None', 'get': ', i', 'get_dummies': ', sep="|"', 'index': ', sub, start=0, end=None', 'join': ', sep', 'ljust': ', width, fillchar=" "', 'lstrip': ', to_strip=None', 'match': ', pat, case=True, flags=0, na=numpy.nan', 'normalize': ', form', 'pad': ', width, side="left", fillchar=" "', 'partition': ', sep=" ", expand=True', 'repeat': ', repeats', 'replace': ', pat, repl, n=-1, case=None, flags=0, regex=True', 'rfind': ', sub, start=0, end=None', 'rindex': ', sub, start=0, end=None', 'rjust': ', width, fillchar=" "', 'rpartition': ', sep=" ", expand=True', 'rsplit': ', pat=None, n=-1, expand=False', 'rstrip': ', to_strip=None', 'slice_replace': ', start=None, stop=None, repl=None', 'slice': ', start=None, stop=None, step=None', 'split': ', pat=None, n=-1, expand=False', 'startswith': ', pat, na=numpy.nan', 'strip': ', to_strip=None', 'translate': ', table', 'wrap': ', width', 'zfill': ', width', } _hpat_pandas_stringmethods_functions_template = """ # @sdc_overload_method(StringMethodsType, '{methodname}') def hpat_pandas_stringmethods_{methodname}(self{methodparams}): \"\"\" Pandas Series method :meth:`pandas.core.strings.StringMethods.{methodname}()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests sdc.tests.test_strings.TestStrings.test_str2str python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_str2str python -m sdc.runtests sdc.tests.test_hiframes.TestHiFrames.test_str_get python -m sdc.runtests sdc.tests.test_hiframes.TestHiFrames.test_str_replace_noregex python -m sdc.runtests sdc.tests.test_hiframes.TestHiFrames.test_str_split python -m sdc.runtests sdc.tests.test_hiframes.TestHiFrames.test_str_contains_regex Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg other: {methodparams} input arguments decription in https://pandas.pydata.org/pandas-docs/version/0.25/reference/series.html#string-handling Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object \"\"\" ty_checker = TypeChecker('Method {methodname}().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_{methodname}_impl(self{methodparams}): item_count = len(self._data) result = [''] * item_count # result = numba.typed.List.empty_list(numba.types.unicode_type) for it in range(item_count): item = self._data._data[it] if len(item) > 0: result[it] = item.{methodname}({methodparams_call}) else: result[it] = item return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_{methodname}_impl """ @sdc_overload_method(StringMethodsType, 'center') def hpat_pandas_stringmethods_center(self, width, fillchar=' '): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.center Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_center.py :language: python :lines: 27- :caption: Filling left and right side of strings in the Series with an additional character :name: ex_series_str_center .. command-output:: python ./series/str/series_str_center.py :cwd: ../../../examples .. todo:: Add support of 32-bit Unicode for `str.center()` Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.center()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_center Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg width: :obj:`int` Minimum width of resulting string fillchar: :obj:`str` Additional character for filling, default is whitespace Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method center().') ty_checker.check(self, StringMethodsType) if not isinstance(width, Integer): ty_checker.raise_exc(width, 'int', 'width') accepted_types = (Omitted, StringLiteral, UnicodeType) if not isinstance(fillchar, accepted_types) and fillchar != ' ': ty_checker.raise_exc(fillchar, 'str', 'fillchar') def hpat_pandas_stringmethods_center_impl(self, width, fillchar=' '): item_count = len(self._data) result = [''] * item_count for idx, item in enumerate(self._data._data): result[idx] = item.center(width, fillchar) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_center_impl @sdc_overload_method(StringMethodsType, 'endswith') def hpat_pandas_stringmethods_endswith(self, pat, na=None): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.endswith Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_endswith.py :language: python :lines: 27- :caption: Test if the end of each string element matches a string :name: ex_series_str_endswith .. command-output:: python ./series/str/series_str_endswith.py :cwd: ../../../examples .. todo:: - Add support of matching the end of each string by a pattern - Add support of parameter ``na`` .. seealso:: `str.endswith <https://docs.python.org/3/library/stdtypes.html#str.endswith>`_ Python standard library string method. :ref:`Series.str.startswith <pandas.Series.str.startswith>` Same as endswith, but tests the start of string. :ref:`Series.str.contains <pandas.Series.str.contains>` Tests if string element contains a pattern. Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.endswith()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_endswith Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg pat: :obj:`str` Character sequence na: :obj:`bool` Object shown if element tested is not a string *unsupported* Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method endswith().') ty_checker.check(self, StringMethodsType) if not isinstance(pat, (StringLiteral, UnicodeType)): ty_checker.raise_exc(pat, 'str', 'pat') if not isinstance(na, (Boolean, NoneType, Omitted)) and na is not None: ty_checker.raise_exc(na, 'bool', 'na') def hpat_pandas_stringmethods_endswith_impl(self, pat, na=None): if na is not None: msg = 'Method endswith(). The object na\n expected: None' raise ValueError(msg) item_endswith = len(self._data) result = numpy.empty(item_endswith, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.endswith(pat) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_endswith_impl @sdc_overload_method(StringMethodsType, 'find') def hpat_pandas_stringmethods_find(self, sub, start=0, end=None): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.find Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_find.py :language: python :lines: 27- :caption: Return lowest indexes in each strings in the Series :name: ex_series_str_find .. command-output:: python ./series/str/series_str_find.py :cwd: ../../../examples .. todo:: Add support of parameters ``start`` and ``end`` .. seealso:: :ref:`Series.str.rfind <pandas.Series.str.rfind>` Return highest indexes in each strings. Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.find()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_find Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg sub: :obj:`str` Substring being searched start: :obj:`int` Left edge index *unsupported* end: :obj:`int` Right edge index *unsupported* Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method find().') ty_checker.check(self, StringMethodsType) if not isinstance(sub, (StringLiteral, UnicodeType)): ty_checker.raise_exc(sub, 'str', 'sub') accepted_types = (Integer, NoneType, Omitted) if not isinstance(start, accepted_types) and start != 0: ty_checker.raise_exc(start, 'None, int', 'start') if not isinstance(end, accepted_types) and end is not None: ty_checker.raise_exc(end, 'None, int', 'end') def hpat_pandas_stringmethods_find_impl(self, sub, start=0, end=None): if start != 0: raise ValueError('Method find(). The object start\n expected: 0') if end is not None: raise ValueError('Method find(). The object end\n expected: None') item_count = len(self._data) result = numpy.empty(item_count, numba.types.int64) for idx, item in enumerate(self._data._data): result[idx] = item.find(sub) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_find_impl @sdc_overload_method(StringMethodsType, 'isupper') def hpat_pandas_stringmethods_isupper(self): ty_checker = TypeChecker('Method isupper().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isupper_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isupper() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isupper_impl @sdc_overload_method(StringMethodsType, 'len') def hpat_pandas_stringmethods_len(self): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.len Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_len.py :language: python :lines: 27- :caption: Compute the length of each element in the Series :name: ex_series_str_len .. command-output:: python ./series/str/series_str_len.py :cwd: ../../../examples .. seealso:: `str.len` Python built-in function returning the length of an object. :ref:`Series.size <pandas.Series.size>` Returns the length of the Series. Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.len()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_str_len1 Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method len().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_len_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.int64) for idx, item in enumerate(self._data._data): result[idx] = len(item) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_len_impl @sdc_overload_method(StringMethodsType, 'ljust') def hpat_pandas_stringmethods_ljust(self, width, fillchar=' '): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.ljust Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_ljust.py :language: python :lines: 27- :caption: Filling right side of strings in the Series with an additional character :name: ex_series_str_ljust .. command-output:: python ./series/str/series_str_ljust.py :cwd: ../../../examples .. todo:: Add support of 32-bit Unicode for `str.ljust()` Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.ljust()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_ljust Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg width: :obj:`int` Minimum width of resulting string fillchar: :obj:`str` Additional character for filling, default is whitespace Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method ljust().') ty_checker.check(self, StringMethodsType) if not isinstance(width, Integer): ty_checker.raise_exc(width, 'int', 'width') accepted_types = (Omitted, StringLiteral, UnicodeType) if not isinstance(fillchar, accepted_types) and fillchar != ' ': ty_checker.raise_exc(fillchar, 'str', 'fillchar') def hpat_pandas_stringmethods_ljust_impl(self, width, fillchar=' '): item_count = len(self._data) result = [''] * item_count for idx, item in enumerate(self._data._data): result[idx] = item.ljust(width, fillchar) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_ljust_impl @sdc_overload_method(StringMethodsType, 'rjust') def hpat_pandas_stringmethods_rjust(self, width, fillchar=' '): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.rjust Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_rjust.py :language: python :lines: 27- :caption: Filling left side of strings in the Series with an additional character :name: ex_series_str_rjust .. command-output:: python ./series/str/series_str_rjust.py :cwd: ../../../examples .. todo:: Add support of 32-bit Unicode for `str.rjust()` Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.rjust()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_rjust Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg width: :obj:`int` Minimum width of resulting string fillchar: :obj:`str` Additional character for filling, default is whitespace Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method rjust().') ty_checker.check(self, StringMethodsType) if not isinstance(width, Integer): ty_checker.raise_exc(width, 'int', 'width') accepted_types = (Omitted, StringLiteral, UnicodeType) if not isinstance(fillchar, accepted_types) and fillchar != ' ': ty_checker.raise_exc(fillchar, 'str', 'fillchar') def hpat_pandas_stringmethods_rjust_impl(self, width, fillchar=' '): item_count = len(self._data) result = [''] * item_count for idx, item in enumerate(self._data._data): result[idx] = item.rjust(width, fillchar) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_rjust_impl @sdc_overload_method(StringMethodsType, 'startswith') def hpat_pandas_stringmethods_startswith(self, pat, na=None): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.startswith Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_startswith.py :language: python :lines: 27- :caption: Test if the start of each string element matches a string :name: ex_series_str_startswith .. command-output:: python ./series/str/series_str_startswith.py :cwd: ../../../examples .. todo:: - Add support of matching the start of each string by a pattern - Add support of parameter ``na`` .. seealso:: `str.startswith <https://docs.python.org/3/library/stdtypes.html#str.startswith>`_ Python standard library string method. :ref:`Series.str.endswith <pandas.Series.str.endswith>` Same as startswith, but tests the end of string. :ref:`Series.str.contains <pandas.Series.str.contains>` Tests if string element contains a pattern. Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.startswith()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_startswith Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg pat: :obj:`str` Character sequence na: :obj:`bool` Object shown if element tested is not a string *unsupported* Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method startswith().') ty_checker.check(self, StringMethodsType) if not isinstance(pat, (StringLiteral, UnicodeType)): ty_checker.raise_exc(pat, 'str', 'pat') if not isinstance(na, (Boolean, NoneType, Omitted)) and na is not None: ty_checker.raise_exc(na, 'bool', 'na') def hpat_pandas_stringmethods_startswith_impl(self, pat, na=None): if na is not None: msg = 'Method startswith(). The object na\n expected: None' raise ValueError(msg) item_startswith = len(self._data) result = numpy.empty(item_startswith, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.startswith(pat) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_startswith_impl @sdc_overload_method(StringMethodsType, 'zfill') def hpat_pandas_stringmethods_zfill(self, width): """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.zfill Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_zfill.py :language: python :lines: 27- :caption: Pad strings in the Series by prepending '0' characters :name: ex_series_str_zfill .. command-output:: python ./series/str/series_str_zfill.py :cwd: ../../../examples .. todo:: Add support of 32-bit Unicode for `str.zfill()` .. seealso:: :ref:`Series.str.rjust <pandas.Series.str.rjust>` Fills the left side of strings with an arbitrary character. :ref:`Series.str.ljust <pandas.Series.str.ljust>` Fills the right side of strings with an arbitrary character. :ref:`Series.str.pad <pandas.Series.str.pad>` Fills the specified sides of strings with an arbitrary character. :ref:`Series.str.center <pandas.Series.str.center>` Fills boths sides of strings with an arbitrary character. Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.zfill()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_zfill Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg width: :obj:`int` Minimum width of resulting string Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ ty_checker = TypeChecker('Method zfill().') ty_checker.check(self, StringMethodsType) if not isinstance(width, Integer): ty_checker.raise_exc(width, 'int', 'width') def hpat_pandas_stringmethods_zfill_impl(self, width): item_count = len(self._data) result = [''] * item_count for idx, item in enumerate(self._data._data): result[idx] = item.zfill(width) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_zfill_impl def _hpat_pandas_stringmethods_autogen(method_name): """" The function generates a function for 'method_name' from source text that is created on the fly. """ params = "" params_call = "" # get function parameters by name params_dict = _hpat_pandas_stringmethods_functions_params.get(method_name) if params_dict is not None: params = params_dict if len(params) > 0: """ Translate parameters string for method For example: parameters for split(): ', pat=None, n=-1, expand=False' translate into: 'pat, n, expand' """ params_call_splitted = params.split(',') params_call_list = [] for item in params_call_splitted: params_call_list.append(item.split("=")[0]) params_call = ",".join(params_call_list) if len(params_call) > 1: params_call = params_call[2:] sourcecode = _hpat_pandas_stringmethods_functions_template.format(methodname=method_name, methodparams=params, methodparams_call=params_call) exec(sourcecode, _hpat_pandas_stringmethods_autogen_global_dict) global_dict_name = 'hpat_pandas_stringmethods_{methodname}'.format(methodname=method_name) return _hpat_pandas_stringmethods_autogen_global_dict[global_dict_name] sdc_pandas_series_str_docstring_template = """ Intel Scalable Dataframe Compiler User Guide ******************************************** Pandas API: pandas.Series.str.{method_name} Limitations ----------- Series elements are expected to be Unicode strings. Elements cannot be NaN. Examples -------- .. literalinclude:: ../../../examples/series/str/series_str_{method_name}.py :language: python :lines: 27- :caption: {caption} :name: ex_series_str_{method_name} .. command-output:: python ./series/str/series_str_{method_name}.py :cwd: ../../../examples .. seealso:: {seealso} Intel Scalable Dataframe Compiler Developer Guide ************************************************* Pandas Series method :meth:`pandas.core.strings.StringMethods.{method_name}()` implementation. Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements. .. only:: developer Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_{method_name}_str Parameters ---------- self: :class:`pandas.core.strings.StringMethods` input arg Returns ------- :obj:`pandas.Series` returns :obj:`pandas.Series` object """ @sdc_overload_method(StringMethodsType, 'istitle') def hpat_pandas_stringmethods_istitle(self): ty_checker = TypeChecker('Method istitle().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_istitle_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.istitle() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_istitle_impl @sdc_overload_method(StringMethodsType, 'isspace') def hpat_pandas_stringmethods_isspace(self): ty_checker = TypeChecker('Method isspace().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isspace_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isspace() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isspace_impl @sdc_overload_method(StringMethodsType, 'isalpha') def hpat_pandas_stringmethods_isalpha(self): ty_checker = TypeChecker('Method isalpha().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isalpha_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isalpha() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isalpha_impl @sdc_overload_method(StringMethodsType, 'islower') def hpat_pandas_stringmethods_islower(self): ty_checker = TypeChecker('Method islower().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_islower_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.islower() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_islower_impl @sdc_overload_method(StringMethodsType, 'isalnum') def hpat_pandas_stringmethods_isalnum(self): ty_checker = TypeChecker('Method isalnum().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isalnum_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isalnum() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isalnum_impl @sdc_overload_method(StringMethodsType, 'isnumeric') def hpat_pandas_stringmethods_isnumeric(self): ty_checker = TypeChecker('Method isnumeric().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isnumeric_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isnumeric() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isnumeric_impl @sdc_overload_method(StringMethodsType, 'isdigit') def hpat_pandas_stringmethods_isdigit(self): ty_checker = TypeChecker('Method isdigit().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isdigit_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isdigit() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isdigit_impl @sdc_overload_method(StringMethodsType, 'isdecimal') def hpat_pandas_stringmethods_isdecimal(self): ty_checker = TypeChecker('Method isdecimal().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_isdecimal_impl(self): item_count = len(self._data) result = numpy.empty(item_count, numba.types.boolean) for idx, item in enumerate(self._data._data): result[idx] = item.isdecimal() return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_isdecimal_impl @sdc_overload_method(StringMethodsType, 'capitalize') def hpat_pandas_stringmethods_capitalize(self): ty_checker = TypeChecker('Method capitalize().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_capitalize_impl(self): mask = get_nan_mask(self._data._data) item_count = len(self._data) res_list = [''] * item_count for idx in numba.prange(item_count): res_list[idx] = self._data._data[idx].capitalize() str_arr = create_str_arr_from_list(res_list) result = str_arr_set_na_by_mask(str_arr, mask) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_capitalize_impl @sdc_overload_method(StringMethodsType, 'title') def hpat_pandas_stringmethods_title(self): ty_checker = TypeChecker('Method title().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_title_impl(self): mask = get_nan_mask(self._data._data) item_count = len(self._data) res_list = [''] * item_count for idx in numba.prange(item_count): res_list[idx] = self._data._data[idx].title() str_arr = create_str_arr_from_list(res_list) result = str_arr_set_na_by_mask(str_arr, mask) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_title_impl @sdc_overload_method(StringMethodsType, 'swapcase') def hpat_pandas_stringmethods_swapcase(self): ty_checker = TypeChecker('Method swapcase().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_swapcase_impl(self): mask = get_nan_mask(self._data._data) item_count = len(self._data) res_list = [''] * item_count for idx in numba.prange(item_count): res_list[idx] = self._data._data[idx].swapcase() str_arr = create_str_arr_from_list(res_list) result = str_arr_set_na_by_mask(str_arr, mask) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_swapcase_impl @sdc_overload_method(StringMethodsType, 'casefold') def hpat_pandas_stringmethods_casefold(self): ty_checker = TypeChecker('Method casefold().') ty_checker.check(self, StringMethodsType) def hpat_pandas_stringmethods_casefold_impl(self): mask = get_nan_mask(self._data._data) item_count = len(self._data) res_list = [''] * item_count for idx in numba.prange(item_count): res_list[idx] = self._data._data[idx].casefold() str_arr = create_str_arr_from_list(res_list) result = str_arr_set_na_by_mask(str_arr, mask) return pandas.Series(result, self._data._index, name=self._data._name) return hpat_pandas_stringmethods_casefold_impl seealso_check_methods = """ :ref:`Series.str.isalpha <pandas.Series.str.isalpha>` Check whether all characters are alphabetic. :ref:`Series.str.isnumeric <pandas.Series.str.isnumeric>` Check whether all characters are numeric. :ref:`Series.str.isalnum <pandas.Series.str.isalnum>` Check whether all characters are alphanumeric. :ref:`Series.str.isdigit <pandas.Series.str.isdigit>` Check whether all characters are digits. :ref:`Series.str.isdecimal <pandas.Series.str.isdecimal>` Check whether all characters are decimal. :ref:`Series.str.isspace <pandas.Series.str.isspace>` Check whether all characters are whitespace. :ref:`Series.str.islower <pandas.Series.str.islower>` Check whether all characters are lowercase. :ref:`Series.str.isupper <pandas.Series.str.isupper>` Check whether all characters are uppercase. :ref:`Series.str.istitle <pandas.Series.str.istitle>` Check whether all characters are titlecase. """ seealso_transform_methods = """ :ref:`Series.str.lower <pandas.Series.str.lower>` Converts all characters to lowercase. :ref:`Series.str.upper <pandas.Series.str.upper>` Converts all characters to uppercase. :ref:`Series.str.title <pandas.Series.str.title>` Converts first character of each word to uppercase and remaining to lowercase. :ref:`Series.str.capitalize <pandas.Series.str.capitalize>` Converts first character to uppercase and remaining to lowercase. :ref:`Series.str.swapcase <pandas.Series.str.swapcase>` Converts uppercase to lowercase and lowercase to uppercase. :ref:`Series.str.casefold <pandas.Series.str.casefold>` Removes all case distinctions in the string. """ stringmethods_funcs = { 'istitle': {'method': hpat_pandas_stringmethods_istitle, 'caption': 'Check if each word start with an upper case letter', 'seealso': seealso_check_methods}, 'isspace': {'method': hpat_pandas_stringmethods_isspace, 'caption': 'Check if all the characters in the text are whitespaces', 'seealso': seealso_check_methods}, 'isalpha': {'method': hpat_pandas_stringmethods_isalpha, 'caption': 'Check whether all characters in each string are alphabetic', 'seealso': seealso_check_methods}, 'islower': {'method': hpat_pandas_stringmethods_islower, 'caption': 'Check if all the characters in the text are alphanumeric', 'seealso': seealso_check_methods}, 'isalnum': {'method': hpat_pandas_stringmethods_isalnum, 'caption': 'Check if all the characters in the text are alphanumeric', 'seealso': seealso_check_methods}, 'isnumeric': {'method': hpat_pandas_stringmethods_isnumeric, 'caption': 'Check whether all characters in each string are numeric.', 'seealso': seealso_check_methods}, 'isdigit': {'method': hpat_pandas_stringmethods_isdigit, 'caption': 'Check whether all characters in each string in the Series/Index are digits.', 'seealso': seealso_check_methods}, 'isdecimal': {'method': hpat_pandas_stringmethods_isdecimal, 'caption': 'Check whether all characters in each string are decimal.', 'seealso': seealso_check_methods}, 'isupper': {'method': hpat_pandas_stringmethods_isupper, 'caption': 'Check whether all characters in each string are uppercase.', 'seealso': seealso_check_methods}, 'capitalize': {'method': hpat_pandas_stringmethods_capitalize, 'caption': 'Convert strings in the Series/Index to be capitalized.', 'seealso': seealso_transform_methods}, 'title': {'method': hpat_pandas_stringmethods_title, 'caption': 'Convert strings in the Series/Index to titlecase.', 'seealso': seealso_transform_methods}, 'swapcase': {'method': hpat_pandas_stringmethods_swapcase, 'caption': 'Convert strings in the Series/Index to be swapcased.', 'seealso': seealso_transform_methods}, 'casefold': {'method': hpat_pandas_stringmethods_casefold, 'caption': 'Convert strings in the Series/Index to be casefolded.', 'seealso': seealso_transform_methods}, } for name, data in stringmethods_funcs.items(): data['method'].__doc__ = sdc_pandas_series_str_docstring_template.format(**{'method_name': name, 'caption': data['caption'], 'seealso': data['seealso']}) # _hpat_pandas_stringmethods_autogen_methods = sorted(dir(numba.types.misc.UnicodeType.__getattribute__.__qualname__)) _hpat_pandas_stringmethods_autogen_methods = ['upper', 'lower', 'lstrip', 'rstrip', 'strip'] """ This is the list of function which are autogenerated to be used from Numba directly. """ _hpat_pandas_stringmethods_autogen_exceptions = ['split', 'get', 'replace'] for method_name in _hpat_pandas_stringmethods_autogen_methods: if not (method_name.startswith('__') or method_name in _hpat_pandas_stringmethods_autogen_exceptions): sdc_overload_method(StringMethodsType, method_name)(_hpat_pandas_stringmethods_autogen(method_name))
bsd-2-clause
abimannans/scikit-learn
examples/datasets/plot_iris_dataset.py
283
1928
#!/usr/bin/python # -*- coding: utf-8 -*- """ ========================================================= The Iris Dataset ========================================================= This data sets consists of 3 different types of irises' (Setosa, Versicolour, and Virginica) petal and sepal length, stored in a 150x4 numpy.ndarray The rows being the samples and the columns being: Sepal Length, Sepal Width, Petal Length and Petal Width. The below plot uses the first two features. See `here <http://en.wikipedia.org/wiki/Iris_flower_data_set>`_ for more information on this dataset. """ print(__doc__) # Code source: Gaël Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn import datasets from sklearn.decomposition import PCA # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. Y = iris.target x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 plt.figure(2, figsize=(8, 6)) plt.clf() # Plot 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(x_min, x_max) plt.ylim(y_min, y_max) plt.xticks(()) plt.yticks(()) # To getter a better understanding of interaction of the dimensions # plot the first three PCA dimensions fig = plt.figure(1, figsize=(8, 6)) ax = Axes3D(fig, elev=-150, azim=110) X_reduced = PCA(n_components=3).fit_transform(iris.data) ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=Y, cmap=plt.cm.Paired) ax.set_title("First three PCA directions") ax.set_xlabel("1st eigenvector") ax.w_xaxis.set_ticklabels([]) ax.set_ylabel("2nd eigenvector") ax.w_yaxis.set_ticklabels([]) ax.set_zlabel("3rd eigenvector") ax.w_zaxis.set_ticklabels([]) plt.show()
bsd-3-clause
mdeff/ntds_2017
projects/reports/arab_springs/lib/clustering.py
1
1319
import pickle import pandas as pd import json import numpy as np import matplotlib.pyplot as plt from scipy import spatial, sparse import scipy.sparse.linalg import scipy from sklearn.cluster import KMeans from pygsp import graphs, filters, plotting import operator import io from lib import models, graph, coarsening, utils get_ipython().magic('matplotlib inline') def take_eigenvectors(laplacian, K=5): eigenvalues, eigenvectors = sparse.linalg.eigsh(laplacian, k=K, which = 'SA') return eigenvalues, eigenvectors def do_kmeans(eigenvectors, K=5): #kmeans to find clusters kmeans = KMeans(n_clusters=K, random_state=0).fit(eigenvectors) return kmeans.labels_ def label_data(df, kmeans_labels, K=5, NUMBER = 40): counts = [dict() for x in range(K)] for i, label in enumerate(kmeans_labels): words = df.loc[i].Tokens for w in words: try: counts[label][w]+=1 except: counts[label][w]=1 total = {} for k in range(K): sorted_words = sorted(counts[k], key=operator.itemgetter(1), reverse=True)[:NUMBER] for w in sorted_words: try: total[w]+=1 except: total[w]=1 labels = [[] for i in range(K)] for k in range(K): sorted_words = sorted(counts[k], key=operator.itemgetter(1), reverse=True)[:NUMBER] for w in sorted_words: if total[w]==1: labels[k].append(w) return labels
mit
bubae/gazeAssistRecognize
train_model.py
1
5633
import init_path import rcnnModule from sklearn import svm import numpy as np import os, sys, cv2 import csv from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import LinearSVC from utils.timer import Timer from sklearn.externals import joblib CLASSES = ('__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor') NETS = {'vgg_cnn_m_1024': ('VGG_CNN_M_1024', 'vgg_cnn_m_1024_fast_rcnn_iter_40000.caffemodel'), 'caffenet': ('CaffeNet', 'caffenet_fast_rcnn_iter_40000.caffemodel'), 'zf': ('ZF', 'ZF_faster_rcnn_final.caffemodel')} def init_train(): print "Init Train..." setting = {} setting['NET'] = 'zf' setting['ROOT_DIR'] = os.getcwd() setting['DATA_DIR'] = os.path.join(setting['ROOT_DIR'], 'data') setting['IMAGE_DIR'] = os.path.join(setting['DATA_DIR'], 'imageNet', 'images') setting['TEST_DIR'] = os.path.join(setting['DATA_DIR'], 'Test') setting['DST_DIR'] = os.path.join(setting['DATA_DIR'], 'result') setting['DST_MODEL_DIR'] = os.path.join(setting['DST_DIR'], 'imageNet', setting['NET']) setting['featureDstDir'] = os.path.join(setting['DST_MODEL_DIR'], "FEATURE") categories = sorted([f for f in os.listdir(setting['IMAGE_DIR'])]) categoryDirPath = [os.path.join(setting['IMAGE_DIR'], f) for f in categories] cid2name = categories cid2path = categoryDirPath iid2path = np.array([]) iid2name = np.array([]) iid2cid = np.array([]) cNum = len(cid2path) cid = 0 for dirPath in categoryDirPath: # dirPath = cid2path[i] imList = np.array(sorted([f for f in os.listdir(dirPath)])) imPath = np.array([os.path.join(dirPath, im) for im in imList]) iid2name = np.append(iid2name, imList) iid2path = np.append(iid2path, imPath) iid2cid = np.append(iid2cid, np.ones(len(imPath))*cid) cid = cid + 1 iid2cid = iid2cid.astype(int) cid2name = np.array(cid2name) cid2path = np.array(cid2path) return setting, cid2name, cid2path, iid2path, iid2name, iid2cid def train_SVM(setting, y): print "train SVM" # SVM Training # SVM options # svm_kernel = 'rbf'; # svm_C = 1.0; # svm_loss = 'squared_hinge' # svm_penalty = 'l2' # svm_multi_class = 'ovr' # svm_random_state = 0 filePath = os.path.join(setting['DST_MODEL_DIR'], "svm_trained.pkl") try: clf = joblib.load(filePath) print "using trained model" except: print "building svm model" X = loadDesc(setting) X = X.astype('float') timer = Timer() timer.tic() clf = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X, y) timer.toc() print timer.total_time joblib.dump(clf, filePath) # TEST # print clf.decision_function(X[0]) # print clf.predict(X[5000]) return clf def loadDesc(setting): print "Load Desc..." timer = Timer() featureDstDir = setting['featureDstDir'] sortedList = sorted([ f for f in os.listdir(featureDstDir)]) descPath = np.array([ os.path.join(featureDstDir, x) for x in sortedList]) X = [] cnt = 0 size = len(descPath) timer.tic() for path in descPath: feature = readCSV(path) X.append(feature) print "%d / %d file loaded" % (cnt, size) cnt = cnt + 1 timer.toc() # print timer.total_time X = np.array(X) X = np.reshape(X, X.shape[0:2]) return X def readCSV(path): rlist = [] with open(path, 'rb') as f: reader = csv.reader(f, delimiter=' ') for row in reader: rlist.append(row) return np.array(rlist) def writeCSV(data, path): with open(path, 'wb') as fout: writer = csv.writer(fout, delimiter=',') for d in data: writer.writerow([d]) def featureExtraction(setting, cid2name, cid2path, iid2path, iid2name, iid2cid, rcnnModel): print "Feature Extraction.." featureDstDir = setting['featureDstDir'] if not os.path.exists(featureDstDir): os.makedirs(featureDstDir) numIm = len(iid2path) descExist = np.zeros(numIm) fList = np.array([ int(x[0:-4]) for x in os.listdir(featureDstDir) ]) for i in fList: descExist[i] = 1 nonDescList = np.where(descExist == 0)[0] numDesc = len(nonDescList) if numDesc==0: print "No image to desc." cnt = 0 for i in nonDescList: print i, cid2name[iid2cid[i]], iid2name[i],": %0.2f percent finished" % (cnt*100.0/numDesc) im = cv2.imread(iid2path[i]) [features, bbox] = rcnnModel.getFeatureIm(im) feature = np.mean(features, axis=0) fileName = "%06d.csv" % i filePath = os.path.join(featureDstDir, fileName) writeCSV(feature, filePath) cnt = cnt+1 def TestModel(setting, rcnnModel, clf): print "Test trained Model" testDir = setting['TEST_DIR'] sortedList = sorted([ f for f in os.listdir(testDir)]) imPath = np.array([ os.path.join(testDir, x) for x in sortedList]) for path in imPath: im = cv2.imread(path) [features, bbox] = rcnnModel.getFeatureIm(im) feature = np.mean(features, axis=0) predict_result = clf.predict(features) print clf.predict(feature) print len(np.where(predict_result==0)[0]) # print imPath def main(): [setting, cid2name, cid2path, iid2path, iid2name, iid2cid] = init_train(); print "rcnnModel loading..." rcnnModel = rcnnModule.RcnnObject('zf', False); featureExtraction(setting, cid2name, cid2path, iid2path, iid2name, iid2cid, rcnnModel) clf = train_SVM(setting, iid2cid) TestModel(setting, rcnnModel, clf) if __name__ == '__main__': main()
mit
robbymeals/scikit-learn
sklearn/ensemble/partial_dependence.py
251
15097
"""Partial dependence plots for tree ensembles. """ # Authors: Peter Prettenhofer # License: BSD 3 clause from itertools import count import numbers import numpy as np from scipy.stats.mstats import mquantiles from ..utils.extmath import cartesian from ..externals.joblib import Parallel, delayed from ..externals import six from ..externals.six.moves import map, range, zip from ..utils import check_array from ..tree._tree import DTYPE from ._gradient_boosting import _partial_dependence_tree from .gradient_boosting import BaseGradientBoosting def _grid_from_X(X, percentiles=(0.05, 0.95), grid_resolution=100): """Generate a grid of points based on the ``percentiles of ``X``. The grid is generated by placing ``grid_resolution`` equally spaced points between the ``percentiles`` of each column of ``X``. Parameters ---------- X : ndarray The data percentiles : tuple of floats The percentiles which are used to construct the extreme values of the grid axes. grid_resolution : int The number of equally spaced points that are placed on the grid. Returns ------- grid : ndarray All data points on the grid; ``grid.shape[1] == X.shape[1]`` and ``grid.shape[0] == grid_resolution * X.shape[1]``. axes : seq of ndarray The axes with which the grid has been created. """ if len(percentiles) != 2: raise ValueError('percentile must be tuple of len 2') if not all(0. <= x <= 1. for x in percentiles): raise ValueError('percentile values must be in [0, 1]') axes = [] for col in range(X.shape[1]): uniques = np.unique(X[:, col]) if uniques.shape[0] < grid_resolution: # feature has low resolution use unique vals axis = uniques else: emp_percentiles = mquantiles(X, prob=percentiles, axis=0) # create axis based on percentiles and grid resolution axis = np.linspace(emp_percentiles[0, col], emp_percentiles[1, col], num=grid_resolution, endpoint=True) axes.append(axis) return cartesian(axes), axes def partial_dependence(gbrt, target_variables, grid=None, X=None, percentiles=(0.05, 0.95), grid_resolution=100): """Partial dependence of ``target_variables``. Partial dependence plots show the dependence between the joint values of the ``target_variables`` and the function represented by the ``gbrt``. Read more in the :ref:`User Guide <partial_dependence>`. Parameters ---------- gbrt : BaseGradientBoosting A fitted gradient boosting model. target_variables : array-like, dtype=int The target features for which the partial dependecy should be computed (size should be smaller than 3 for visual renderings). grid : array-like, shape=(n_points, len(target_variables)) The grid of ``target_variables`` values for which the partial dependecy should be evaluated (either ``grid`` or ``X`` must be specified). X : array-like, shape=(n_samples, n_features) The data on which ``gbrt`` was trained. It is used to generate a ``grid`` for the ``target_variables``. The ``grid`` comprises ``grid_resolution`` equally spaced points between the two ``percentiles``. percentiles : (low, high), default=(0.05, 0.95) The lower and upper percentile used create the extreme values for the ``grid``. Only if ``X`` is not None. grid_resolution : int, default=100 The number of equally spaced points on the ``grid``. Returns ------- pdp : array, shape=(n_classes, n_points) The partial dependence function evaluated on the ``grid``. For regression and binary classification ``n_classes==1``. axes : seq of ndarray or None The axes with which the grid has been created or None if the grid has been given. Examples -------- >>> samples = [[0, 0, 2], [1, 0, 0]] >>> labels = [0, 1] >>> from sklearn.ensemble import GradientBoostingClassifier >>> gb = GradientBoostingClassifier(random_state=0).fit(samples, labels) >>> kwargs = dict(X=samples, percentiles=(0, 1), grid_resolution=2) >>> partial_dependence(gb, [0], **kwargs) # doctest: +SKIP (array([[-4.52..., 4.52...]]), [array([ 0., 1.])]) """ if not isinstance(gbrt, BaseGradientBoosting): raise ValueError('gbrt has to be an instance of BaseGradientBoosting') if gbrt.estimators_.shape[0] == 0: raise ValueError('Call %s.fit before partial_dependence' % gbrt.__class__.__name__) if (grid is None and X is None) or (grid is not None and X is not None): raise ValueError('Either grid or X must be specified') target_variables = np.asarray(target_variables, dtype=np.int32, order='C').ravel() if any([not (0 <= fx < gbrt.n_features) for fx in target_variables]): raise ValueError('target_variables must be in [0, %d]' % (gbrt.n_features - 1)) if X is not None: X = check_array(X, dtype=DTYPE, order='C') grid, axes = _grid_from_X(X[:, target_variables], percentiles, grid_resolution) else: assert grid is not None # dont return axes if grid is given axes = None # grid must be 2d if grid.ndim == 1: grid = grid[:, np.newaxis] if grid.ndim != 2: raise ValueError('grid must be 2d but is %dd' % grid.ndim) grid = np.asarray(grid, dtype=DTYPE, order='C') assert grid.shape[1] == target_variables.shape[0] n_trees_per_stage = gbrt.estimators_.shape[1] n_estimators = gbrt.estimators_.shape[0] pdp = np.zeros((n_trees_per_stage, grid.shape[0],), dtype=np.float64, order='C') for stage in range(n_estimators): for k in range(n_trees_per_stage): tree = gbrt.estimators_[stage, k].tree_ _partial_dependence_tree(tree, grid, target_variables, gbrt.learning_rate, pdp[k]) return pdp, axes def plot_partial_dependence(gbrt, X, features, feature_names=None, label=None, n_cols=3, grid_resolution=100, percentiles=(0.05, 0.95), n_jobs=1, verbose=0, ax=None, line_kw=None, contour_kw=None, **fig_kw): """Partial dependence plots for ``features``. The ``len(features)`` plots are arranged in a grid with ``n_cols`` columns. Two-way partial dependence plots are plotted as contour plots. Read more in the :ref:`User Guide <partial_dependence>`. Parameters ---------- gbrt : BaseGradientBoosting A fitted gradient boosting model. X : array-like, shape=(n_samples, n_features) The data on which ``gbrt`` was trained. features : seq of tuples or ints If seq[i] is an int or a tuple with one int value, a one-way PDP is created; if seq[i] is a tuple of two ints, a two-way PDP is created. feature_names : seq of str Name of each feature; feature_names[i] holds the name of the feature with index i. label : object The class label for which the PDPs should be computed. Only if gbrt is a multi-class model. Must be in ``gbrt.classes_``. n_cols : int The number of columns in the grid plot (default: 3). percentiles : (low, high), default=(0.05, 0.95) The lower and upper percentile used to create the extreme values for the PDP axes. grid_resolution : int, default=100 The number of equally spaced points on the axes. n_jobs : int The number of CPUs to use to compute the PDs. -1 means 'all CPUs'. Defaults to 1. verbose : int Verbose output during PD computations. Defaults to 0. ax : Matplotlib axis object, default None An axis object onto which the plots will be drawn. line_kw : dict Dict with keywords passed to the ``pylab.plot`` call. For one-way partial dependence plots. contour_kw : dict Dict with keywords passed to the ``pylab.plot`` call. For two-way partial dependence plots. fig_kw : dict Dict with keywords passed to the figure() call. Note that all keywords not recognized above will be automatically included here. Returns ------- fig : figure The Matplotlib Figure object. axs : seq of Axis objects A seq of Axis objects, one for each subplot. Examples -------- >>> from sklearn.datasets import make_friedman1 >>> from sklearn.ensemble import GradientBoostingRegressor >>> X, y = make_friedman1() >>> clf = GradientBoostingRegressor(n_estimators=10).fit(X, y) >>> fig, axs = plot_partial_dependence(clf, X, [0, (0, 1)]) #doctest: +SKIP ... """ import matplotlib.pyplot as plt from matplotlib import transforms from matplotlib.ticker import MaxNLocator from matplotlib.ticker import ScalarFormatter if not isinstance(gbrt, BaseGradientBoosting): raise ValueError('gbrt has to be an instance of BaseGradientBoosting') if gbrt.estimators_.shape[0] == 0: raise ValueError('Call %s.fit before partial_dependence' % gbrt.__class__.__name__) # set label_idx for multi-class GBRT if hasattr(gbrt, 'classes_') and np.size(gbrt.classes_) > 2: if label is None: raise ValueError('label is not given for multi-class PDP') label_idx = np.searchsorted(gbrt.classes_, label) if gbrt.classes_[label_idx] != label: raise ValueError('label %s not in ``gbrt.classes_``' % str(label)) else: # regression and binary classification label_idx = 0 X = check_array(X, dtype=DTYPE, order='C') if gbrt.n_features != X.shape[1]: raise ValueError('X.shape[1] does not match gbrt.n_features') if line_kw is None: line_kw = {'color': 'green'} if contour_kw is None: contour_kw = {} # convert feature_names to list if feature_names is None: # if not feature_names use fx indices as name feature_names = [str(i) for i in range(gbrt.n_features)] elif isinstance(feature_names, np.ndarray): feature_names = feature_names.tolist() def convert_feature(fx): if isinstance(fx, six.string_types): try: fx = feature_names.index(fx) except ValueError: raise ValueError('Feature %s not in feature_names' % fx) return fx # convert features into a seq of int tuples tmp_features = [] for fxs in features: if isinstance(fxs, (numbers.Integral,) + six.string_types): fxs = (fxs,) try: fxs = np.array([convert_feature(fx) for fx in fxs], dtype=np.int32) except TypeError: raise ValueError('features must be either int, str, or tuple ' 'of int/str') if not (1 <= np.size(fxs) <= 2): raise ValueError('target features must be either one or two') tmp_features.append(fxs) features = tmp_features names = [] try: for fxs in features: l = [] # explicit loop so "i" is bound for exception below for i in fxs: l.append(feature_names[i]) names.append(l) except IndexError: raise ValueError('features[i] must be in [0, n_features) ' 'but was %d' % i) # compute PD functions pd_result = Parallel(n_jobs=n_jobs, verbose=verbose)( delayed(partial_dependence)(gbrt, fxs, X=X, grid_resolution=grid_resolution, percentiles=percentiles) for fxs in features) # get global min and max values of PD grouped by plot type pdp_lim = {} for pdp, axes in pd_result: min_pd, max_pd = pdp[label_idx].min(), pdp[label_idx].max() n_fx = len(axes) old_min_pd, old_max_pd = pdp_lim.get(n_fx, (min_pd, max_pd)) min_pd = min(min_pd, old_min_pd) max_pd = max(max_pd, old_max_pd) pdp_lim[n_fx] = (min_pd, max_pd) # create contour levels for two-way plots if 2 in pdp_lim: Z_level = np.linspace(*pdp_lim[2], num=8) if ax is None: fig = plt.figure(**fig_kw) else: fig = ax.get_figure() fig.clear() n_cols = min(n_cols, len(features)) n_rows = int(np.ceil(len(features) / float(n_cols))) axs = [] for i, fx, name, (pdp, axes) in zip(count(), features, names, pd_result): ax = fig.add_subplot(n_rows, n_cols, i + 1) if len(axes) == 1: ax.plot(axes[0], pdp[label_idx].ravel(), **line_kw) else: # make contour plot assert len(axes) == 2 XX, YY = np.meshgrid(axes[0], axes[1]) Z = pdp[label_idx].reshape(list(map(np.size, axes))).T CS = ax.contour(XX, YY, Z, levels=Z_level, linewidths=0.5, colors='k') ax.contourf(XX, YY, Z, levels=Z_level, vmax=Z_level[-1], vmin=Z_level[0], alpha=0.75, **contour_kw) ax.clabel(CS, fmt='%2.2f', colors='k', fontsize=10, inline=True) # plot data deciles + axes labels deciles = mquantiles(X[:, fx[0]], prob=np.arange(0.1, 1.0, 0.1)) trans = transforms.blended_transform_factory(ax.transData, ax.transAxes) ylim = ax.get_ylim() ax.vlines(deciles, [0], 0.05, transform=trans, color='k') ax.set_xlabel(name[0]) ax.set_ylim(ylim) # prevent x-axis ticks from overlapping ax.xaxis.set_major_locator(MaxNLocator(nbins=6, prune='lower')) tick_formatter = ScalarFormatter() tick_formatter.set_powerlimits((-3, 4)) ax.xaxis.set_major_formatter(tick_formatter) if len(axes) > 1: # two-way PDP - y-axis deciles + labels deciles = mquantiles(X[:, fx[1]], prob=np.arange(0.1, 1.0, 0.1)) trans = transforms.blended_transform_factory(ax.transAxes, ax.transData) xlim = ax.get_xlim() ax.hlines(deciles, [0], 0.05, transform=trans, color='k') ax.set_ylabel(name[1]) # hline erases xlim ax.set_xlim(xlim) else: ax.set_ylabel('Partial dependence') if len(axes) == 1: ax.set_ylim(pdp_lim[1]) axs.append(ax) fig.subplots_adjust(bottom=0.15, top=0.7, left=0.1, right=0.95, wspace=0.4, hspace=0.3) return fig, axs
bsd-3-clause
BeatsonLab-MicrobialGenomics/DiscoPlot
discoplot/DiscoPlot.py
1
23712
#!/usr/bin/env python # DiscoPlot: identify genomic rearrangements, misassemblies and sequencing # artefacts in NGS data # Copyright (C) 2013-2015 Mitchell Sullivan # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # Mitchell Sullivan # [email protected] # School of Chemistry & Molecular Biosciences # The University of Queensland # Brisbane, QLD 4072. # Australia __title__ = 'DiscoPlot' __version__ = '1.0.2' __description__ = ("DiscoPlot: identify genomic rearrangements, misassemblies " "and sequencing artefacts in NGS data") __author__ = 'Mitchell Sullivan' __license__ = 'GPLv3' __author_email__ = "[email protected]" __url__ = 'https://github.com/BeatsonLab-MicrobialGenomics/DiscoPlot' import argparse import numpy import sys import subprocess def read_sbam(args): import pysam if not args.bam_file is None: sam = pysam.Samfile(args.bam_file, 'rb') elif not args.sam_file: sam = pysam.Samfile(args.sam_file) global refpos global cuta global cutb cuta = 0 cutb = float('inf') refpos = {} if not args.subsection is None: if len(args.subsection) == 1: refpos[args.subsection[0]] = 0 totallength = None for i in range(0, len(sam.references)): if sam.references[i] == args.subsection[0]: totallength = sam.lengths[i] if totallength is None: sys.stderr.write('Selected reference not found.') sys.exit() elif len(args.subsection) == 2: refpos[sam.references[0]] = 0 cuta = int(args.subsection[0]) cutb = int(args.subsection[1]) totallength = cutb - cuta elif len(args.subsection) == 3: refpos[args.subsection[0]] = 0 cuta = int(args.subsection[1]) cutb = int(args.subsection[2]) totallength = cutb - cuta else: sys.stderr.write('Too many arguments given for subsection') sys.exit() if args.bin_size is None: args.bin_size = totallength / args.size + 1 else: args.size = totallength / args.bin_size + 1 else: references = sam.references reflengths = sam.lengths currpos = 0 if args.bin_size is None: args.bin_size = sum(reflengths) / (args.size - (len(reflengths) -1) * (args.gap + 1)) + 1 else: args.size = sum(map(lambda x: x/args.bin_size, reflengths)) + (len(reflengths) -1) * args.gap + 1 for i in range(len(references)): refpos[references[i]] = currpos currpos += reflengths[i] / args.bin_size + args.gap global invgrid, dirgrid, unmapped_for, unmapped_rev unmapped_rev = {} unmapped_for = {} invgrid = {} dirgrid = {} for read in sam.fetch(): ref = sam.getrname(read.tid) if ref in refpos: if read.is_read1: if cuta <= read.pos <= cutb: pos1 = (read.pos - cuta) / args.bin_size + refpos[ref] if read.mate_is_unmapped: if read.is_reverse: if pos1 in unmapped_rev: unmapped_rev[pos1] += 1 else: unmapped_rev[pos1] = 1 else: if pos1 in unmapped_for: unmapped_for[pos1] += 1 else: unmapped_for[pos1] = 1 else: mref = sam.getrname(read.rnext) if mref in refpos: if cuta <= read.pnext <= cutb: pos2 = (read.pnext - cuta) / args.bin_size + refpos[mref] if read.is_reverse: if read.mate_is_reverse: if pos1 < pos2: if pos2 in dirgrid and pos1 in dirgrid[pos2]: dirgrid[pos2][pos1] += 1 elif pos2 in dirgrid: dirgrid[pos2][pos1] = 1 else: dirgrid[pos2] = {pos1:1} else: if pos1 in dirgrid and pos2 in dirgrid[pos1]: dirgrid[pos1][pos2] += 1 elif pos1 in dirgrid: dirgrid[pos1][pos2] = 1 else: dirgrid[pos1] = {pos2:1} else: if pos2 in invgrid and pos1 in invgrid[pos2]: invgrid[pos2][pos1] += 1 elif pos2 in invgrid: invgrid[pos2][pos1] = 1 else: invgrid[pos2] = {pos1:1} else: if read.mate_is_reverse: if pos1 in invgrid and pos2 in invgrid[pos1]: invgrid[pos1][pos2] += 1 elif pos1 in invgrid: invgrid[pos1][pos2] = 1 else: invgrid[pos1] = {pos2:1} else: if pos1 < pos2: if pos1 in dirgrid and pos2 in dirgrid[pos1]: dirgrid[pos1][pos2] += 1 elif pos1 in dirgrid: dirgrid[pos1][pos2] = 1 else: dirgrid[pos1] = {pos2:1} else: if pos2 in dirgrid and pos1 in dirgrid[pos2]: dirgrid[pos2][pos1] += 1 elif pos2 in dirgrid: dirgrid[pos2][pos1] = 1 else: dirgrid[pos2] = {pos1:1} else: if read.mate_is_unmapped: ref = sam.getrname(read.tid) if ref in refpos: if cuta <= read.pos <= cutb: pos = (read.pos - cuta) / args.bin_size + refpos[ref] if read.is_reverse: if pos in unmapped_rev: unmapped_rev[pos] += 1 else: unmapped_rev[pos] = 1 else: if pos in unmapped_for: unmapped_for[pos] += 1 else: unmapped_for[pos] = 1 def read_sing(args): readlen = None if not args.read_file is None: reads = open(args.read_file) first = True getfq = 0 readlen = {} for line in reads: if first: first = False if line.startswith('@'): getfq = 2 name = line.rstrip()[1:] seq = '' elif line.startswith('>'): readlen[name] = len(seq) name = line.rstrip()[1:] seq = '' elif getfq == 0: seq += line.rstrip() elif getfq == 1: readlen[name] = len(seq) name = line.rstrip() seq = '' elif getfq == 2: seq += line.rstrip() getfq = 3 elif getfq == 3: getfq = 4 elif getfq == 4: getfq = 1 readlen[name] = len(seq) if not args.reference_file is None: ref = open(args.reference_file) first = True references = [] reflengths = [] for line in ref: if line.startswith('>'): if first: first = False else: references.append(name) reflengths.append(len(seq)) name = line.rstrip()[1:] seq = '' else: seq += line references.append(name) reflengths.append(len(seq)) else: blast = open(args.blast_file) refdict = {} for line in blast: if line.split()[1] in refdict: if max([int(line.split()[8]), int(line.split()[9])]) > refdict[line.split()[1]]: refdict[line.split()[1]] = max([int(line.split()[8]), int(line.split()[9])]) else: refdict[line.split()[1]] = max([int(line.split()[8]), int(line.split()[9])]) blast.close() references = [] reflengths = [] for i in refdict: references.append(i) reflengths.append(refdict[i]) cuta = 0 cutb = float('inf') refpos = {} if not args.subsection is None: if len(args.subsection) == 1: refpos[args.subsection[0]] = 0 totallength = None for i in range(0, len(references)): if references[i] == args.subsection[0]: totallength = reflengths[i] if totallength is None: sys.stderr.write('Selected reference not found.') sys.exit() elif len(args.subsection) == 2: refpos[references[0]] = 0 cuta = int(args.subsection[0]) cutb = int(args.subsection[1]) totallength = cutb - cuta elif len(args.subsection) == 3: refpos[args.subsection[0]] = 0 cuta = int(args.subsection[0]) cutb = int(args.subsection[1]) totallength = cutb - cuta else: sys.stderr.write('Too many arguments given for subsection') sys.exit() if args.bin_size is None: args.bin_size = totallength / args.size else: args.size = totallength / args.bin_size else: currpos = 0 if args.bin_size is None: args.bin_size = sum(reflengths) / (args.size - (len(reflengths) -1) * (args.gap + 1)) else: args.size = sum(map(lambda x: x/args.bin_size, reflengths)) + (len(reflengths) -1) * args.gap for i in range(len(references)): refpos[references[i]] = currpos currpos += reflengths[i] / args.bin_size + args.gap global invgrid, dirgrid, unmapped_for, unmapped_rev unmapped_rev = {} unmapped_for = {} invgrid = {} dirgrid = {} blast = open(args.blast_file) lastquery = '' hits = [] for line in blast: query, subject, ident, length, mm, indel, qstart, qstop, rstart, rstop, eval, bitscore = line.split() qstart, qstop, rstart, rstop, length, mm, indel = map(int, [qstart, qstop, rstart, rstop, length, mm, indel]) if query != lastquery and lastquery != '': hits.sort(reverse=True) newhits = [hits[0]] qtaken = set() for i in range(hits[2], hits[3] + 1): qtaken.add(i) for i in hits[1:]: if i[:-3] == newhits[-1][:-3]: newhits.append(i) else: getit = False for j in range(hits[2], hits[3] + 1): if not j in qtaken: getit = True qtaken.add(j) if getit: newhits.append(i) anchor = None revseq = None for i in newhits: bitscore, length, qstart, qstop, rstart, rstop, subject = i if anchor is None: if rstart < rstop: anchor = rstart revseq = False else: anchor = rstop revseq = True if min(qtaken) >= args.unmapped: if revseq: if anchor in unmapped_for: unmapped_for[anchor] += 1 else: unmapped_for[anchor] = 1 else: if anchor in unmapped_rev: unmapped_rev[anchor] += 1 else: unmapped_rev[anchor] = 1 if max(qtaken) <= readlen[lastquery] - args.unmapped: if revseq: if anchor in unmapped_rev: unmapped_rev[anchor] += 1 else: unmapped_rev[anchor] = 1 else: if anchor in unmapped_for: unmapped_for[anchor] += 1 else: unmapped_for[anchor] = 1 lastxpos = None lastypos = None oldstart, oldstop = qstart, qstop if revseq: rstart, rstop = rstop, rstart qstart = readlen[lastquery] - qstop qstop = readlen[lastquery] - oldstart for j in range(qstart, qstop): xpos = refpos[subject] + (anchor + j - cuta) / args.bin_size ypos = refpos[subject] + (rstart + int(((j - qstart) * 1.0 / (qstop - qstart)) * (rstop - rstart))) / args.bin_size if xpos != lastxpos or ypos != lastypos: if rstart < rstop: if xpos in dirgrid: if ypos in dirgrid[xpos]: dirgrid[xpos][ypos] += 1 else: dirgrid[xpos][ypos] = 1 else: dirgrid[xpos] = {ypos:1} else: if xpos in invgrid: if ypos in invgrid[xpos]: invgrid[xpos][ypos] += 1 else: invgrid[xpos][ypos] = 1 else: invgrid[xpos] = {ypos:1} lastxpos, lastypos = xpos, ypos if ident >= args.min_ident and length >= args.min_length and subject in refpos and ((cuta <= rstart <= cutb) or (cuta <= rstop <= cutb)): hits.append((float(bitscore), length, qstart, qstop, rstart, rstop, subject)) lastquery = query def generate_blast(args): subprocess.Popen('makeblastdb -dbtype nucl -out ' + args.gen_blast + '.db -in ' + args.reference_file, shell=True, stdout=subprocess.PIPE).wait() subprocess.Popen('blastn -db ' + args.gen_blast + '.db -outfmt 6 -query ' + args.read_file + ' -out ' + args.gen_blast + '.out', shell=True).wait() args.blast_file = args.gen_blast + '.out' def draw_dotplot(args): global refpos global cuta global cutb vals1, vals2 = [], [] for i in invgrid: for j in invgrid[i]: vals1.append(invgrid[i][j]) vals2.append(invgrid[i][j]) for i in dirgrid: for j in dirgrid[i]: vals1.append(dirgrid[i][j]) vals2.append(dirgrid[i][j]) vals2 = numpy.array(vals2) for i in unmapped_rev: vals1.append(unmapped_rev[i]) for i in unmapped_for: vals1.append(unmapped_for[i]) vals1 = numpy.array(vals1) med = numpy.median(vals2) numvals = numpy.size(vals1) sizemod = 2000.0 / args.size / med fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111, aspect='equal') x = numpy.zeros(numvals, dtype='u4') y = numpy.zeros(numvals, dtype='u4') sizes = numpy.zeros(numvals, dtype='f4') colours = numpy.array(['x' for i in range(numvals)]) count = 0 for i in dirgrid: for j in dirgrid[i]: if args.max_hits >= dirgrid[i][j] >= args.min_hits: x[count] = i * args.bin_size + cuta y[count] = j * args.bin_size + cuta sizes[count] = dirgrid[i][j] * sizemod colours[count] = 'r' count += 1 for i in invgrid: for j in invgrid[i]: if args.max_hits >= invgrid[i][j] >= args.min_hits: x[count] = i * args.bin_size + cuta y[count] = j * args.bin_size + cuta sizes[count] = invgrid[i][j] * sizemod colours[count] = 'b' count += 1 for i in unmapped_for: if args.max_hits >= unmapped_for[i] >= args.min_hits: x[count] = cuta y[count] = i * args.bin_size + cuta sizes[count] = unmapped_for[i] * sizemod colours[count] = 'g' count += 1 for i in unmapped_rev: if args.max_hits >= unmapped_rev[i] >= args.min_hits: x[count] = i * args.bin_size + cuta y[count] = cuta sizes[count] = unmapped_rev[i] * sizemod colours[count] = 'g' count += 1 count1, count2, count3 = 0, 0, 0 for i in colours: if i == 'b': count1 += 1 elif i == 'r': count2 += 1 elif i == 'g': count3 += 1 ax.scatter(x, y, s=sizes, c=colours, edgecolor='none', alpha=0.3) sizes = [] names = [] for i in [10, 25, 50, 75, 90]: sizes.append(numpy.percentile(vals2, i)) names.append(str(i) + '% Normal ' + str(sizes[-1])) names.append('50% Inverted ' + str(sizes[2])) a = plt.scatter(-100, -100, s=sizes[2] * sizemod, c='b', edgecolor='none', alpha=0.3) b = plt.scatter(-100, -100, s=sizes[0] * sizemod, c='r', edgecolor='none', alpha=0.3) c = plt.scatter(-100, -100, s=sizes[1] * sizemod, c='r', edgecolor='none', alpha=0.3) d = plt.scatter(-100, -100, s=sizes[2] * sizemod, c='r', edgecolor='none', alpha=0.3) e = plt.scatter(-100, -100, s=sizes[3] * sizemod, c='r', edgecolor='none', alpha=0.3) f = plt.scatter(-100, -100, s=sizes[4] * sizemod, c='r', edgecolor='none', alpha=0.3) leg = ax.legend([b, c, d, e, f, a], names, loc=4) leg.draggable(state=True) for i in refpos: if not refpos[i] == 0: ax.axhspan(refpos[i] * args.bin_size, refpos[i] * args.bin_size - args.gap * args.bin_size, facecolor='g', alpha=0.3) ax.axvspan(refpos[i] * args.bin_size, refpos[i] * args.bin_size - args.gap * args.bin_size, facecolor='g', alpha=0.3) if cutb == float('inf'): cutb = args.size * args.bin_size + cuta plt.xlim([cuta - args.bin_size * 10, cutb]) plt.ylim([cuta - args.bin_size * 10, cutb]) plt.grid(True) if not args.output_file is None: plt.savefig(args.output_file, dpi=args.image_quality) else: plt.show() parser = argparse.ArgumentParser(prog='DiscoPlot', formatter_class=argparse.RawDescriptionHelpFormatter, description=''' DiscoPlot - read mapping visualisation in the large USAGE: DiscoPlot -bam bamfile.bam -o output_file.bmp -size 5000 Create a bmp file from a bamfile of paired-end reads with a width and height of 5000px DiscoPlot -r reads.fa -B blast_prefix -r reference -o output_file.png -bin bin_size Create a png file from reads.fa, generate blast file. Image size will be reference length / bin_size ''', epilog="Thanks for using DiscoPlot") parser.add_argument('-r', '--read_file', action='store', default=None, help='read file') parser.add_argument('-ref', '--reference_file', action='store', default=None, help='reference file') parser.add_argument('-bam', '--bam_file', action='store', default=None, help='bam file') parser.add_argument('-sam', '--sam_file', action='store', default=None, help='sam file') parser.add_argument('-B', '--gen_blast', action='store', default=None, help='Generate blast files, use argument as prefix for output.') parser.add_argument('-b', '--blast_file', action='store', default=None, help='Blast file (output format 6)') parser.add_argument('-o', '--output_file', action='store', default=None, help='output file [gif/bmp/png]') parser.add_argument('-s', '--size', action='store', type=int, default=None, help='Number of bins') parser.add_argument('-bin', '--bin_size', action='store', type=int, default=None, help='Bin size (in bp)') parser.add_argument('-g', '--gap', action='store', type=int, default=5, help='Gap size') parser.add_argument('-sub', '--subsection', nargs='+', action='store', default=None, help='Only display subection of genome [ref]/[min_cutoff max_cutoff]/[ref min_cutoff max_cutoff]') parser.add_argument('-c', '--min_hits', action='store', type=int, default=1, help='Min hits to be shown') parser.add_argument('-m', '--max_hits', action='store', type=float, default=float('inf'), help='Bins with more hits than this will be skipped.') parser.add_argument('-dpi', '--image_quality', action='store', type=int, default=1600, help='Image quality (in DPI)') args = parser.parse_args() if args.size is None and args.bin_size is None: sys.stderr.write('Please give a image size or bin size.') sys.exit() if not args.gen_blast is None: if args.reference_file is None: sys.stderr.write('Please provide a reference file') sys.exit() if args.read_file is None: sys.stderr.write('Please provide a read file (FASTA)') sys.exit() generate_blast(args) if not args.output_file is None: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt if not args.size is None and not args.bin_size is None: sys.stderr.write('Only provide bin size or image size, not both.') sys.exit() if not args.sam_file is None or not args.bam_file is None: read_sbam(args) elif args.blast_file is None: sys.stderr.write('Please either generate or provide a BLAST comparison') sys.exit() else: read_sing(args) draw_dotplot(args)
gpl-3.0
SSDS-Croatia/SSDS-2017
Day-5/util.py
1
5277
import os, sys, gzip, math, urllib import numpy as np from PIL import Image import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec class Dataset: def __init__(self, data, labels=None): self.data = data if type(labels) == None: self.supervised = False else: self.supervised = True self.labels = labels self.n = len(data) self.batches_complete = 0 self.position_in_epoch = 0 def next_batch(self, batch_size, return_labels=False): new_epoch = False if self.position_in_epoch + batch_size >= self.n: self.position_in_epoch = 0 self.batches_complete += 1 new_epoch = True batch = self.data[self.position_in_epoch:self.position_in_epoch + batch_size] if self.supervised and return_labels: batch_labels = self.labels[self.position_in_epoch, self.position_in_epoch + batch_size] batch = (batch, batch_labels) self.position_in_epoch += batch_size return new_epoch, batch def plot(samples): fig = plt.figure(figsize=(4, 4)) gs = gridspec.GridSpec(4, 4) gs.update(wspace=0.05, hspace=0.05) for i, sample in enumerate(samples): ax = plt.subplot(gs[i]) plt.axis('off') ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') plt.imshow(sample.reshape(28, 28), cmap='Greys_r') return fig def plot_single(sample, epoch=0): plt.axis('off') ax = plt.gca() ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') plt.imshow(sample.reshape(28, 28), cmap='Greys_r') def download_mnist(data_folder, dataset): """ Download and extract database :param database_name: Database name """ image_files = ['train-images-idx3-ubyte.gz', 't10k-images-idx3-ubyte.gz'] label_files = ['train-labels-idx1-ubyte.gz', 't10k-labels-idx1-ubyte.gz'] url = 'http://yann.lecun.com/exdb/mnist/' dataset_folder = os.path.join(data_folder, dataset) if not os.path.exists(dataset_folder): os.makedirs(dataset_folder) for filename in image_files + label_files: filepath = os.path.join(dataset_folder, filename) filepath, _ = urllib.request.urlretrieve(url + filename, filepath) statinfo = os.stat(filepath) print('Successfully downloaded', filename, statinfo.st_size, 'bytes.') else: print('Found {} Data'.format(dataset)) return dataset_folder def extract_data(filename, num_data, head_size, data_size): with gzip.open(filename) as bytestream: bytestream.read(head_size) buf = bytestream.read(data_size * num_data) data = np.frombuffer(buf, dtype=np.uint8).astype(np.float) return data def load_mnist(dataset_folder): data = extract_data(dataset_folder + '/train-images-idx3-ubyte.gz', 60000, 16, 28 * 28) trX = data.reshape((60000, 28, 28, 1)) data = extract_data(dataset_folder + '/train-labels-idx1-ubyte.gz', 60000, 8, 1) trY = data.reshape((60000)) data = extract_data(dataset_folder + '/t10k-images-idx3-ubyte.gz', 10000, 16, 28 * 28) teX = data.reshape((10000, 28, 28, 1)) data = extract_data(dataset_folder + '/t10k-labels-idx1-ubyte.gz', 10000, 8, 1) teY = data.reshape((10000)) trY = np.asarray(trY) teY = np.asarray(teY) X = np.concatenate((trX, teX), axis=0) y = np.concatenate((trY, teY), axis=0).astype(np.int) seed = 547 np.random.seed(seed) np.random.shuffle(X) np.random.seed(seed) np.random.shuffle(y) y_vec = np.zeros((len(y), 10), dtype=np.float) for i, label in enumerate(y): y_vec[i, y[i]] = 1.0 return X / 255., y_vec def images_square_grid(images, mode): """ Save images as a square grid :param images: Images to be used for the grid :param mode: The mode to use for images :return: Image of images in a square grid """ # Get maximum size for square grid of images save_size = math.floor(np.sqrt(images.shape[0])) # Scale to 0-255 images = (((images - images.min()) * 255) / (images.max() - images.min())).astype(np.uint8) # Put images in a square arrangement images_in_square = np.reshape( images[:save_size*save_size], (save_size, save_size, images.shape[1], images.shape[2], images.shape[3])) if mode == 'L': images_in_square = np.squeeze(images_in_square, 4) # Combine images to grid image new_im = Image.new(mode, (images.shape[1] * save_size, images.shape[2] * save_size)) for col_i, col_images in enumerate(images_in_square): for image_i, image in enumerate(col_images): im = Image.fromarray(image, mode) new_im.paste(im, (col_i * images.shape[1], image_i * images.shape[2])) return new_im def get_sample_images(data, dataset='mnist', n=25): """ Get a sample of n images from a dataset, able to be displayed with matplotlib :param data_dir: Root directory of the dataset :param dataset: """ # Display options if dataset == 'mnist': mode = 'L' else: mode = 'RGB' return data[:n], mode
mit
miku/siskin
docs/btag-2017/scripts/pie.py
2
1614
# coding: utf-8 """ Sources and sizes. """ import base64 import json import requests import matplotlib.pyplot as plt import numpy as np from matplotlib import cm addr = base64.b64decode("""aHR0cDovLzE3Mi4xOC4xMTMuNzo4MDg1L3NvbHIvYmlibGlv""") def total(): """ Return the total number of docs. """ r = requests.get('%s/select?wt=json&q=*:*' % (addr, label)) if r.status_code >= 300: raise RuntimeError("got HTTP %s on %s" % (r.status_code, r.url)) doc = json.loads(r.text) return doc['response']['numFound'] sources = ( ('28', 'DOAJ'), ('48', 'WISO'), ('49', 'Crossref'), ('50', 'De Gruyter'), ('55', 'JSTOR'), ('60', 'Thieme'), ('85', 'Elsevier'), ('89', 'IEEE'), ('105', 'Springer'), ('121', 'Arxiv'), ) labels, names, sizes = [s[0] for s in sources], [s[1] for s in sources], [] for label in labels: r = requests.get('%s/select?wt=json&q=source_id:%s' % (addr, label)) if r.status_code >= 300: raise RuntimeError("got HTTP %s on %s" % (r.status_code, r.url)) doc = json.loads(r.text) found = doc['response']['numFound'] sizes.append(found) explode = [0 for _ in range(len(labels))] explode[2] = 0.1 fig1, ax1 = plt.subplots() cmap = plt.get_cmap('Set1') colors = [cmap(i) for i in np.linspace(0, 1, len(labels))] patches, texts = plt.pie(sizes, startangle=90, colors=colors, shadow=False, explode=explode) plt.legend(patches, names, loc="lower left") ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.title('Article Metadata Index Sources (2017)') plt.savefig('pie.png')
gpl-3.0
pratapvardhan/pandas
pandas/tests/indexes/timedeltas/test_timedelta_range.py
3
3021
import pytest import numpy as np import pandas as pd import pandas.util.testing as tm from pandas.tseries.offsets import Day, Second from pandas import to_timedelta, timedelta_range class TestTimedeltas(object): def test_timedelta_range(self): expected = to_timedelta(np.arange(5), unit='D') result = timedelta_range('0 days', periods=5, freq='D') tm.assert_index_equal(result, expected) expected = to_timedelta(np.arange(11), unit='D') result = timedelta_range('0 days', '10 days', freq='D') tm.assert_index_equal(result, expected) expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day() result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02', freq='D') tm.assert_index_equal(result, expected) expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2) result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D') tm.assert_index_equal(result, expected) expected = to_timedelta(np.arange(50), unit='T') * 30 result = timedelta_range('0 days', freq='30T', periods=50) tm.assert_index_equal(result, expected) # GH 11776 arr = np.arange(10).reshape(2, 5) df = pd.DataFrame(np.arange(10).reshape(2, 5)) for arg in (arr, df): with tm.assert_raises_regex(TypeError, "1-d array"): to_timedelta(arg) for errors in ['ignore', 'raise', 'coerce']: with tm.assert_raises_regex(TypeError, "1-d array"): to_timedelta(arg, errors=errors) # issue10583 df = pd.DataFrame(np.random.normal(size=(10, 4))) df.index = pd.timedelta_range(start='0s', periods=10, freq='s') expected = df.loc[pd.Timedelta('0s'):, :] result = df.loc['0s':, :] tm.assert_frame_equal(expected, result) @pytest.mark.parametrize('periods, freq', [ (3, '2D'), (5, 'D'), (6, '19H12T'), (7, '16H'), (9, '12H')]) def test_linspace_behavior(self, periods, freq): # GH 20976 result = timedelta_range(start='0 days', end='4 days', periods=periods) expected = timedelta_range(start='0 days', end='4 days', freq=freq) tm.assert_index_equal(result, expected) def test_errors(self): # not enough params msg = ('Of the four parameters: start, end, periods, and freq, ' 'exactly three must be specified') with tm.assert_raises_regex(ValueError, msg): timedelta_range(start='0 days') with tm.assert_raises_regex(ValueError, msg): timedelta_range(end='5 days') with tm.assert_raises_regex(ValueError, msg): timedelta_range(periods=2) with tm.assert_raises_regex(ValueError, msg): timedelta_range() # too many params with tm.assert_raises_regex(ValueError, msg): timedelta_range(start='0 days', end='5 days', periods=10, freq='H')
bsd-3-clause
tttr222/autumn_ner
test.py
1
5277
#!/usr/bin/env python import sys, os, random, pickle, json, codecs, time import numpy as np import sklearn.metrics as skm import argparse from model import AutumnNER from utility import load_dataset from utility import load_embeddings from utility import report_performance parser = argparse.ArgumentParser(description='Train and evaluate BiLSTM on a given dataset') parser.add_argument('--datapath', dest='datapath', type=str, default='CoNLL2003', help='path to the datasets') parser.add_argument('--embeddings', dest='embeddings_path', type=str, default=None, help='path to the testing dataset') parser.add_argument('--optimizer', dest='optimizer', type=str, default='default', help='choose the optimizer: default, rmsprop, adagrad, adam.') parser.add_argument('--batch-size', dest='batch_size', type=int, default=64, help='number of instances in a minibatch') parser.add_argument('--num-epoch', dest='num_epoch', type=int, default=50, help='number of passes over the training set') parser.add_argument('--learning-rate', dest='learning_rate', type=str, default='default', help='learning rate') parser.add_argument('--embedding-factor', dest='embedding_factor', type=float, default=1.0, help='learning rate multiplier for embeddings') parser.add_argument('--decay', dest='decay_rate', type=float, default=0.95, help='exponential decay for learning rate') parser.add_argument('--keep-prob', dest='keep_prob', type=float, default=0.7, help='dropout keep rate') parser.add_argument('--num-cores', dest='num_cores', type=int, default=5, help='seed for training') parser.add_argument('--seed', dest='seed', type=int, default=1, help='seed for training') def main(args): print >> sys.stderr, "Running Autumn NER model testing module" print >> sys.stderr, args random.seed(args.seed) trainset = [] devset = [] testset_standalone = {} word_vocab = [] print "Loading dataset.." assert(os.path.isdir(args.datapath)) for fname in sorted(os.listdir(args.datapath)): if os.path.isdir(fname): continue if fname.endswith('.ner.txt'): dataset, vocab = load_dataset(os.path.join(args.datapath,fname)) word_vocab += vocab if fname.endswith('train.ner.txt'): trainset += dataset if fname.endswith('dev.ner.txt'): devset += dataset if fname.endswith('test.ner.txt'): testset_standalone[fname] = dataset print "Loaded {} instances with a vocab size of {} from {}".format(len(dataset),len(vocab),fname) word_vocab = sorted(set(word_vocab)) if args.embeddings_path: embeddings = load_embeddings(args.embeddings_path, word_vocab, 300) else: embeddings = None print "Loaded {}/{} instances from training/dev set".format(len(trainset),len(devset)) X_train, y_train = zip(*trainset) X_dev, y_dev = zip(*devset) labels = [] for lb in y_train + y_dev: labels += lb labels = sorted(set(labels)) # Create the model, passing in relevant parameters bilstm = AutumnNER(labels=labels, word_vocab=word_vocab, word_embeddings=embeddings, optimizer=args.optimizer, embedding_size=300, char_embedding_size=32, lstm_dim=200, num_cores=args.num_cores, embedding_factor=args.embedding_factor, learning_rate=args.learning_rate, decay_rate=args.decay_rate, dropout_keep=args.keep_prob) model_path = './scratch/saved_model_d{}_s{}'.format(hash(args.datapath),args.seed) if not os.path.exists(model_path + '.meta'): if not os.path.exists('./scratch'): os.mkdir('./scratch') print "Training.." bilstm.fit(X_train,y_train, X_dev, y_dev, num_epoch=args.num_epoch, batch_size=args.batch_size, seed=args.seed) bilstm.save(model_path) else: print "Loading saved model.." bilstm.restore(model_path) print "Evaluating.." print "Performance on DEV set ----------------------------" report_performance(bilstm, X_dev,y_dev, 'evaluation/devset_predictions.txt') print "Performance on TEST set(s) ----------------------------" overall_testset = [] for key, testset in testset_standalone: X_test, y_test = zip(*testset) report_performance(bilstm, X_test,y_test, 'evaluation/testset_{}_predictions.txt'.format(key)) overall_testset += testset X_test, y_test = zip(*overall_testset) report_performance(bilstm, X_test,y_test, 'evaluation/testset_overall_predictions.txt') if __name__ == '__main__': main(parser.parse_args())
mit
RachitKansal/scikit-learn
doc/conf.py
210
8446
# -*- coding: utf-8 -*- # # scikit-learn documentation build configuration file, created by # sphinx-quickstart on Fri Jan 8 09:13:42 2010. # # This file is execfile()d with the current directory set to its containing # dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. from __future__ import print_function import sys import os from sklearn.externals.six import u # If extensions (or modules to document with autodoc) are in another # directory, add these directories to sys.path here. If the directory # is relative to the documentation root, use os.path.abspath to make it # absolute, like shown here. sys.path.insert(0, os.path.abspath('sphinxext')) from github_link import make_linkcode_resolve # -- General configuration --------------------------------------------------- # Try to override the matplotlib configuration as early as possible try: import gen_rst except: pass # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = ['gen_rst', 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinx.ext.pngmath', 'numpy_ext.numpydoc', 'sphinx.ext.linkcode', ] autosummary_generate = True autodoc_default_flags = ['members', 'inherited-members'] # Add any paths that contain templates here, relative to this directory. templates_path = ['templates'] # generate autosummary even if no references autosummary_generate = True # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8' # Generate the plots for the gallery plot_gallery = True # The master toctree document. master_doc = 'index' # General information about the project. project = u('scikit-learn') copyright = u('2010 - 2014, scikit-learn developers (BSD License)') # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. import sklearn version = sklearn.__version__ # The full version, including alpha/beta/rc tags. release = sklearn.__version__ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of documents that shouldn't be included in the build. #unused_docs = [] # List of directories, relative to source directory, that shouldn't be # searched for source files. exclude_trees = ['_build', 'templates', 'includes'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. add_function_parentheses = False # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. Major themes that come with # Sphinx are currently 'default' and 'sphinxdoc'. html_theme = 'scikit-learn' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. html_theme_options = {'oldversion': False, 'collapsiblesidebar': True, 'google_analytics': True, 'surveybanner': False, 'sprintbanner': True} # Add any paths that contain custom themes here, relative to this directory. html_theme_path = ['themes'] # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. html_short_title = 'scikit-learn' # The name of an image file (relative to this directory) to place at the top # of the sidebar. html_logo = 'logos/scikit-learn-logo-small.png' # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. html_favicon = 'logos/favicon.ico' # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['images'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. html_domain_indices = False # If false, no index is generated. html_use_index = False # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, an OpenSearch description file will be output, and all pages will # contain a <link> tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = '' # Output file base name for HTML help builder. htmlhelp_basename = 'scikit-learndoc' # -- Options for LaTeX output ------------------------------------------------ # The paper size ('letter' or 'a4'). #latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). #latex_font_size = '10pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass # [howto/manual]). latex_documents = [('index', 'user_guide.tex', u('scikit-learn user guide'), u('scikit-learn developers'), 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. latex_logo = "logos/scikit-learn-logo.png" # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # Additional stuff for the LaTeX preamble. latex_preamble = r""" \usepackage{amsmath}\usepackage{amsfonts}\usepackage{bm}\usepackage{morefloats} \usepackage{enumitem} \setlistdepth{10} """ # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. latex_domain_indices = False trim_doctests_flags = True def generate_example_rst(app, what, name, obj, options, lines): # generate empty examples files, so that we don't get # inclusion errors if there are no examples for a class / module examples_path = os.path.join(app.srcdir, "modules", "generated", "%s.examples" % name) if not os.path.exists(examples_path): # touch file open(examples_path, 'w').close() def setup(app): # to hide/show the prompt in code examples: app.add_javascript('js/copybutton.js') app.connect('autodoc-process-docstring', generate_example_rst) # The following is used by sphinx.ext.linkcode to provide links to github linkcode_resolve = make_linkcode_resolve('sklearn', u'https://github.com/scikit-learn/' 'scikit-learn/blob/{revision}/' '{package}/{path}#L{lineno}')
bsd-3-clause
KonradBreitsprecher/espresso
samples/lb_profile.py
1
1902
import numpy as np import matplotlib.pyplot as plt import espressomd import espressomd.lb import espressomd.observables import espressomd.shapes import espressomd.lbboundaries import espressomd.accumulators system = espressomd.System(box_l=[10.0, 10.0, 5.0]) system.time_step = 0.01 system.cell_system.skin = 0.4 lb_fluid = espressomd.lb.LBFluidGPU(agrid=1.0, fric=1.0, dens=1.0, visc=1.0, tau=0.01, ext_force=[0, 0, 0.15]) system.actors.add(lb_fluid) system.thermostat.set_lb(kT=1.0) fluid_obs = espressomd.observables.CylindricalLBVelocityProfile( center = [5.0, 5.0, 0.0], axis = 'z', n_r_bins = 100, n_phi_bins = 1, n_z_bins = 1, min_r = 0.0, max_r = 4.0, min_phi = -np.pi, max_phi = np.pi, min_z = 0.0, max_z = 10.0, sampling_delta_x = 0.05, sampling_delta_y = 0.05, sampling_delta_z = 1.0) cylinder_shape = espressomd.shapes.Cylinder( center = [5.0, 5.0, 5.0], axis = [0, 0, 1], direction = -1, radius = 4.0, length = 20.0) cylinder_boundary = espressomd.lbboundaries.LBBoundary(shape=cylinder_shape) system.lbboundaries.add(cylinder_boundary) system.integrator.run(5000) accumulator = espressomd.accumulators.MeanVarianceCalculator(obs=fluid_obs) system.auto_update_accumulators.add(accumulator) system.integrator.run(5000) lb_fluid_profile = accumulator.get_mean() lb_fluid_profile = np.reshape(lb_fluid_profile, (100, 1, 1, 3)) def poiseuille_flow(r, R, ext_force): return ext_force * 1./4 * (R**2.0-r**2.0) # Please note that due to symmetry and interpolation a plateau is seen near r=0. n_bins = len(lb_fluid_profile[:, 0, 0, 2]) r_max = 4.0 r = np.linspace(0.0, r_max, n_bins) plt.plot(r, lb_fluid_profile[:, 0, 0, 2], label='LB profile') plt.plot(r, poiseuille_flow(r, r_max, 0.15), label='analytical solution') plt.show()
gpl-3.0
sambitgaan/nupic
external/linux32/lib/python2.6/site-packages/matplotlib/axes.py
69
259904
from __future__ import division, generators import math, sys, warnings, datetime, new import numpy as np from numpy import ma import matplotlib rcParams = matplotlib.rcParams import matplotlib.artist as martist import matplotlib.axis as maxis import matplotlib.cbook as cbook import matplotlib.collections as mcoll import matplotlib.colors as mcolors import matplotlib.contour as mcontour import matplotlib.dates as mdates import matplotlib.font_manager as font_manager import matplotlib.image as mimage import matplotlib.legend as mlegend import matplotlib.lines as mlines import matplotlib.mlab as mlab import matplotlib.patches as mpatches import matplotlib.quiver as mquiver import matplotlib.scale as mscale import matplotlib.table as mtable import matplotlib.text as mtext import matplotlib.ticker as mticker import matplotlib.transforms as mtransforms iterable = cbook.iterable is_string_like = cbook.is_string_like def _process_plot_format(fmt): """ Process a matlab(TM) style color/line style format string. Return a (*linestyle*, *color*) tuple as a result of the processing. Default values are ('-', 'b'). Example format strings include: * 'ko': black circles * '.b': blue dots * 'r--': red dashed lines .. seealso:: :func:`~matplotlib.Line2D.lineStyles` and :func:`~matplotlib.pyplot.colors`: for all possible styles and color format string. """ linestyle = None marker = None color = None # Is fmt just a colorspec? try: color = mcolors.colorConverter.to_rgb(fmt) return linestyle, marker, color # Yes. except ValueError: pass # No, not just a color. # handle the multi char special cases and strip them from the # string if fmt.find('--')>=0: linestyle = '--' fmt = fmt.replace('--', '') if fmt.find('-.')>=0: linestyle = '-.' fmt = fmt.replace('-.', '') if fmt.find(' ')>=0: linestyle = 'None' fmt = fmt.replace(' ', '') chars = [c for c in fmt] for c in chars: if c in mlines.lineStyles: if linestyle is not None: raise ValueError( 'Illegal format string "%s"; two linestyle symbols' % fmt) linestyle = c elif c in mlines.lineMarkers: if marker is not None: raise ValueError( 'Illegal format string "%s"; two marker symbols' % fmt) marker = c elif c in mcolors.colorConverter.colors: if color is not None: raise ValueError( 'Illegal format string "%s"; two color symbols' % fmt) color = c else: raise ValueError( 'Unrecognized character %c in format string' % c) if linestyle is None and marker is None: linestyle = rcParams['lines.linestyle'] if linestyle is None: linestyle = 'None' if marker is None: marker = 'None' return linestyle, marker, color def set_default_color_cycle(clist): """ Change the default cycle of colors that will be used by the plot command. This must be called before creating the :class:`Axes` to which it will apply; it will apply to all future axes. *clist* is a sequence of mpl color specifiers """ _process_plot_var_args.defaultColors = clist[:] rcParams['lines.color'] = clist[0] class _process_plot_var_args: """ Process variable length arguments to the plot command, so that plot commands like the following are supported:: plot(t, s) plot(t1, s1, t2, s2) plot(t1, s1, 'ko', t2, s2) plot(t1, s1, 'ko', t2, s2, 'r--', t3, e3) an arbitrary number of *x*, *y*, *fmt* are allowed """ defaultColors = ['b','g','r','c','m','y','k'] def __init__(self, axes, command='plot'): self.axes = axes self.command = command self._clear_color_cycle() def _clear_color_cycle(self): self.colors = _process_plot_var_args.defaultColors[:] # if the default line color is a color format string, move it up # in the que try: ind = self.colors.index(rcParams['lines.color']) except ValueError: self.firstColor = rcParams['lines.color'] else: self.colors[0], self.colors[ind] = self.colors[ind], self.colors[0] self.firstColor = self.colors[0] self.Ncolors = len(self.colors) self.count = 0 def set_color_cycle(self, clist): self.colors = clist[:] self.firstColor = self.colors[0] self.Ncolors = len(self.colors) self.count = 0 def _get_next_cycle_color(self): if self.count==0: color = self.firstColor else: color = self.colors[int(self.count % self.Ncolors)] self.count += 1 return color def __call__(self, *args, **kwargs): if self.axes.xaxis is not None and self.axes.yaxis is not None: xunits = kwargs.pop( 'xunits', self.axes.xaxis.units) yunits = kwargs.pop( 'yunits', self.axes.yaxis.units) if xunits!=self.axes.xaxis.units: self.axes.xaxis.set_units(xunits) if yunits!=self.axes.yaxis.units: self.axes.yaxis.set_units(yunits) ret = self._grab_next_args(*args, **kwargs) return ret def set_lineprops(self, line, **kwargs): assert self.command == 'plot', 'set_lineprops only works with "plot"' for key, val in kwargs.items(): funcName = "set_%s"%key if not hasattr(line,funcName): raise TypeError, 'There is no line property "%s"'%key func = getattr(line,funcName) func(val) def set_patchprops(self, fill_poly, **kwargs): assert self.command == 'fill', 'set_patchprops only works with "fill"' for key, val in kwargs.items(): funcName = "set_%s"%key if not hasattr(fill_poly,funcName): raise TypeError, 'There is no patch property "%s"'%key func = getattr(fill_poly,funcName) func(val) def _xy_from_y(self, y): if self.axes.yaxis is not None: b = self.axes.yaxis.update_units(y) if b: return np.arange(len(y)), y, False if not ma.isMaskedArray(y): y = np.asarray(y) if len(y.shape) == 1: y = y[:,np.newaxis] nr, nc = y.shape x = np.arange(nr) if len(x.shape) == 1: x = x[:,np.newaxis] return x,y, True def _xy_from_xy(self, x, y): if self.axes.xaxis is not None and self.axes.yaxis is not None: bx = self.axes.xaxis.update_units(x) by = self.axes.yaxis.update_units(y) # right now multicol is not supported if either x or y are # unit enabled but this can be fixed.. if bx or by: return x, y, False x = ma.asarray(x) y = ma.asarray(y) if len(x.shape) == 1: x = x[:,np.newaxis] if len(y.shape) == 1: y = y[:,np.newaxis] nrx, ncx = x.shape nry, ncy = y.shape assert nrx == nry, 'Dimensions of x and y are incompatible' if ncx == ncy: return x, y, True if ncx == 1: x = np.repeat(x, ncy, axis=1) if ncy == 1: y = np.repeat(y, ncx, axis=1) assert x.shape == y.shape, 'Dimensions of x and y are incompatible' return x, y, True def _plot_1_arg(self, y, **kwargs): assert self.command == 'plot', 'fill needs at least 2 arguments' ret = [] x, y, multicol = self._xy_from_y(y) if multicol: for j in xrange(y.shape[1]): color = self._get_next_cycle_color() seg = mlines.Line2D(x, y[:,j], color = color, axes=self.axes, ) self.set_lineprops(seg, **kwargs) ret.append(seg) else: color = self._get_next_cycle_color() seg = mlines.Line2D(x, y, color = color, axes=self.axes, ) self.set_lineprops(seg, **kwargs) ret.append(seg) return ret def _plot_2_args(self, tup2, **kwargs): ret = [] if is_string_like(tup2[1]): assert self.command == 'plot', ('fill needs at least 2 non-string ' 'arguments') y, fmt = tup2 x, y, multicol = self._xy_from_y(y) linestyle, marker, color = _process_plot_format(fmt) def makeline(x, y): _color = color if _color is None: _color = self._get_next_cycle_color() seg = mlines.Line2D(x, y, color=_color, linestyle=linestyle, marker=marker, axes=self.axes, ) self.set_lineprops(seg, **kwargs) ret.append(seg) if multicol: for j in xrange(y.shape[1]): makeline(x[:,j], y[:,j]) else: makeline(x, y) return ret else: x, y = tup2 x, y, multicol = self._xy_from_xy(x, y) def makeline(x, y): color = self._get_next_cycle_color() seg = mlines.Line2D(x, y, color=color, axes=self.axes, ) self.set_lineprops(seg, **kwargs) ret.append(seg) def makefill(x, y): x = self.axes.convert_xunits(x) y = self.axes.convert_yunits(y) facecolor = self._get_next_cycle_color() seg = mpatches.Polygon(np.hstack( (x[:,np.newaxis],y[:,np.newaxis])), facecolor = facecolor, fill=True, closed=closed ) self.set_patchprops(seg, **kwargs) ret.append(seg) if self.command == 'plot': func = makeline else: closed = kwargs.get('closed', True) func = makefill if multicol: for j in xrange(y.shape[1]): func(x[:,j], y[:,j]) else: func(x, y) return ret def _plot_3_args(self, tup3, **kwargs): ret = [] x, y, fmt = tup3 x, y, multicol = self._xy_from_xy(x, y) linestyle, marker, color = _process_plot_format(fmt) def makeline(x, y): _color = color if _color is None: _color = self._get_next_cycle_color() seg = mlines.Line2D(x, y, color=_color, linestyle=linestyle, marker=marker, axes=self.axes, ) self.set_lineprops(seg, **kwargs) ret.append(seg) def makefill(x, y): facecolor = color x = self.axes.convert_xunits(x) y = self.axes.convert_yunits(y) seg = mpatches.Polygon(np.hstack( (x[:,np.newaxis],y[:,np.newaxis])), facecolor = facecolor, fill=True, closed=closed ) self.set_patchprops(seg, **kwargs) ret.append(seg) if self.command == 'plot': func = makeline else: closed = kwargs.get('closed', True) func = makefill if multicol: for j in xrange(y.shape[1]): func(x[:,j], y[:,j]) else: func(x, y) return ret def _grab_next_args(self, *args, **kwargs): remaining = args while 1: if len(remaining)==0: return if len(remaining)==1: for seg in self._plot_1_arg(remaining[0], **kwargs): yield seg remaining = [] continue if len(remaining)==2: for seg in self._plot_2_args(remaining, **kwargs): yield seg remaining = [] continue if len(remaining)==3: if not is_string_like(remaining[2]): raise ValueError, 'third arg must be a format string' for seg in self._plot_3_args(remaining, **kwargs): yield seg remaining=[] continue if is_string_like(remaining[2]): for seg in self._plot_3_args(remaining[:3], **kwargs): yield seg remaining=remaining[3:] else: for seg in self._plot_2_args(remaining[:2], **kwargs): yield seg remaining=remaining[2:] class Axes(martist.Artist): """ The :class:`Axes` contains most of the figure elements: :class:`~matplotlib.axis.Axis`, :class:`~matplotlib.axis.Tick`, :class:`~matplotlib.lines.Line2D`, :class:`~matplotlib.text.Text`, :class:`~matplotlib.patches.Polygon`, etc., and sets the coordinate system. The :class:`Axes` instance supports callbacks through a callbacks attribute which is a :class:`~matplotlib.cbook.CallbackRegistry` instance. The events you can connect to are 'xlim_changed' and 'ylim_changed' and the callback will be called with func(*ax*) where *ax* is the :class:`Axes` instance. """ name = "rectilinear" _shared_x_axes = cbook.Grouper() _shared_y_axes = cbook.Grouper() def __str__(self): return "Axes(%g,%g;%gx%g)" % tuple(self._position.bounds) def __init__(self, fig, rect, axisbg = None, # defaults to rc axes.facecolor frameon = True, sharex=None, # use Axes instance's xaxis info sharey=None, # use Axes instance's yaxis info label='', **kwargs ): """ Build an :class:`Axes` instance in :class:`~matplotlib.figure.Figure` *fig* with *rect=[left, bottom, width, height]* in :class:`~matplotlib.figure.Figure` coordinates Optional keyword arguments: ================ ========================================= Keyword Description ================ ========================================= *adjustable* [ 'box' | 'datalim' ] *alpha* float: the alpha transparency *anchor* [ 'C', 'SW', 'S', 'SE', 'E', 'NE', 'N', 'NW', 'W' ] *aspect* [ 'auto' | 'equal' | aspect_ratio ] *autoscale_on* [ *True* | *False* ] whether or not to autoscale the *viewlim* *axis_bgcolor* any matplotlib color, see :func:`~matplotlib.pyplot.colors` *axisbelow* draw the grids and ticks below the other artists *cursor_props* a (*float*, *color*) tuple *figure* a :class:`~matplotlib.figure.Figure` instance *frame_on* a boolean - draw the axes frame *label* the axes label *navigate* [ *True* | *False* ] *navigate_mode* [ 'PAN' | 'ZOOM' | None ] the navigation toolbar button status *position* [left, bottom, width, height] in class:`~matplotlib.figure.Figure` coords *sharex* an class:`~matplotlib.axes.Axes` instance to share the x-axis with *sharey* an class:`~matplotlib.axes.Axes` instance to share the y-axis with *title* the title string *visible* [ *True* | *False* ] whether the axes is visible *xlabel* the xlabel *xlim* (*xmin*, *xmax*) view limits *xscale* [%(scale)s] *xticklabels* sequence of strings *xticks* sequence of floats *ylabel* the ylabel strings *ylim* (*ymin*, *ymax*) view limits *yscale* [%(scale)s] *yticklabels* sequence of strings *yticks* sequence of floats ================ ========================================= """ % {'scale': ' | '.join([repr(x) for x in mscale.get_scale_names()])} martist.Artist.__init__(self) if isinstance(rect, mtransforms.Bbox): self._position = rect else: self._position = mtransforms.Bbox.from_bounds(*rect) self._originalPosition = self._position.frozen() self.set_axes(self) self.set_aspect('auto') self._adjustable = 'box' self.set_anchor('C') self._sharex = sharex self._sharey = sharey if sharex is not None: self._shared_x_axes.join(self, sharex) if sharex._adjustable == 'box': sharex._adjustable = 'datalim' #warnings.warn( # 'shared axes: "adjustable" is being changed to "datalim"') self._adjustable = 'datalim' if sharey is not None: self._shared_y_axes.join(self, sharey) if sharey._adjustable == 'box': sharey._adjustable = 'datalim' #warnings.warn( # 'shared axes: "adjustable" is being changed to "datalim"') self._adjustable = 'datalim' self.set_label(label) self.set_figure(fig) # this call may differ for non-sep axes, eg polar self._init_axis() if axisbg is None: axisbg = rcParams['axes.facecolor'] self._axisbg = axisbg self._frameon = frameon self._axisbelow = rcParams['axes.axisbelow'] self._hold = rcParams['axes.hold'] self._connected = {} # a dict from events to (id, func) self.cla() # funcs used to format x and y - fall back on major formatters self.fmt_xdata = None self.fmt_ydata = None self.set_cursor_props((1,'k')) # set the cursor properties for axes self._cachedRenderer = None self.set_navigate(True) self.set_navigate_mode(None) if len(kwargs): martist.setp(self, **kwargs) if self.xaxis is not None: self._xcid = self.xaxis.callbacks.connect('units finalize', self.relim) if self.yaxis is not None: self._ycid = self.yaxis.callbacks.connect('units finalize', self.relim) def get_window_extent(self, *args, **kwargs): ''' get the axes bounding box in display space; *args* and *kwargs* are empty ''' return self.bbox def _init_axis(self): "move this out of __init__ because non-separable axes don't use it" self.xaxis = maxis.XAxis(self) self.yaxis = maxis.YAxis(self) self._update_transScale() def set_figure(self, fig): """ Set the class:`~matplotlib.axes.Axes` figure accepts a class:`~matplotlib.figure.Figure` instance """ martist.Artist.set_figure(self, fig) self.bbox = mtransforms.TransformedBbox(self._position, fig.transFigure) #these will be updated later as data is added self.dataLim = mtransforms.Bbox.unit() self.viewLim = mtransforms.Bbox.unit() self.transScale = mtransforms.TransformWrapper( mtransforms.IdentityTransform()) self._set_lim_and_transforms() def _set_lim_and_transforms(self): """ set the *dataLim* and *viewLim* :class:`~matplotlib.transforms.Bbox` attributes and the *transScale*, *transData*, *transLimits* and *transAxes* transformations. """ self.transAxes = mtransforms.BboxTransformTo(self.bbox) # Transforms the x and y axis separately by a scale factor # It is assumed that this part will have non-linear components self.transScale = mtransforms.TransformWrapper( mtransforms.IdentityTransform()) # An affine transformation on the data, generally to limit the # range of the axes self.transLimits = mtransforms.BboxTransformFrom( mtransforms.TransformedBbox(self.viewLim, self.transScale)) # The parentheses are important for efficiency here -- they # group the last two (which are usually affines) separately # from the first (which, with log-scaling can be non-affine). self.transData = self.transScale + (self.transLimits + self.transAxes) self._xaxis_transform = mtransforms.blended_transform_factory( self.axes.transData, self.axes.transAxes) self._yaxis_transform = mtransforms.blended_transform_factory( self.axes.transAxes, self.axes.transData) def get_xaxis_transform(self): """ Get the transformation used for drawing x-axis labels, ticks and gridlines. The x-direction is in data coordinates and the y-direction is in axis coordinates. .. note:: This transformation is primarily used by the :class:`~matplotlib.axis.Axis` class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ return self._xaxis_transform def get_xaxis_text1_transform(self, pad_points): """ Get the transformation used for drawing x-axis labels, which will add the given amount of padding (in points) between the axes and the label. The x-direction is in data coordinates and the y-direction is in axis coordinates. Returns a 3-tuple of the form:: (transform, valign, halign) where *valign* and *halign* are requested alignments for the text. .. note:: This transformation is primarily used by the :class:`~matplotlib.axis.Axis` class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ return (self._xaxis_transform + mtransforms.ScaledTranslation(0, -1 * pad_points / 72.0, self.figure.dpi_scale_trans), "top", "center") def get_xaxis_text2_transform(self, pad_points): """ Get the transformation used for drawing the secondary x-axis labels, which will add the given amount of padding (in points) between the axes and the label. The x-direction is in data coordinates and the y-direction is in axis coordinates. Returns a 3-tuple of the form:: (transform, valign, halign) where *valign* and *halign* are requested alignments for the text. .. note:: This transformation is primarily used by the :class:`~matplotlib.axis.Axis` class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ return (self._xaxis_transform + mtransforms.ScaledTranslation(0, pad_points / 72.0, self.figure.dpi_scale_trans), "bottom", "center") def get_yaxis_transform(self): """ Get the transformation used for drawing y-axis labels, ticks and gridlines. The x-direction is in axis coordinates and the y-direction is in data coordinates. .. note:: This transformation is primarily used by the :class:`~matplotlib.axis.Axis` class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ return self._yaxis_transform def get_yaxis_text1_transform(self, pad_points): """ Get the transformation used for drawing y-axis labels, which will add the given amount of padding (in points) between the axes and the label. The x-direction is in axis coordinates and the y-direction is in data coordinates. Returns a 3-tuple of the form:: (transform, valign, halign) where *valign* and *halign* are requested alignments for the text. .. note:: This transformation is primarily used by the :class:`~matplotlib.axis.Axis` class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ return (self._yaxis_transform + mtransforms.ScaledTranslation(-1 * pad_points / 72.0, 0, self.figure.dpi_scale_trans), "center", "right") def get_yaxis_text2_transform(self, pad_points): """ Get the transformation used for drawing the secondary y-axis labels, which will add the given amount of padding (in points) between the axes and the label. The x-direction is in axis coordinates and the y-direction is in data coordinates. Returns a 3-tuple of the form:: (transform, valign, halign) where *valign* and *halign* are requested alignments for the text. .. note:: This transformation is primarily used by the :class:`~matplotlib.axis.Axis` class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ return (self._yaxis_transform + mtransforms.ScaledTranslation(pad_points / 72.0, 0, self.figure.dpi_scale_trans), "center", "left") def _update_transScale(self): self.transScale.set( mtransforms.blended_transform_factory( self.xaxis.get_transform(), self.yaxis.get_transform())) if hasattr(self, "lines"): for line in self.lines: line._transformed_path.invalidate() def get_position(self, original=False): 'Return the a copy of the axes rectangle as a Bbox' if original: return self._originalPosition.frozen() else: return self._position.frozen() def set_position(self, pos, which='both'): """ Set the axes position with:: pos = [left, bottom, width, height] in relative 0,1 coords, or *pos* can be a :class:`~matplotlib.transforms.Bbox` There are two position variables: one which is ultimately used, but which may be modified by :meth:`apply_aspect`, and a second which is the starting point for :meth:`apply_aspect`. Optional keyword arguments: *which* ========== ==================== value description ========== ==================== 'active' to change the first 'original' to change the second 'both' to change both ========== ==================== """ if not isinstance(pos, mtransforms.BboxBase): pos = mtransforms.Bbox.from_bounds(*pos) if which in ('both', 'active'): self._position.set(pos) if which in ('both', 'original'): self._originalPosition.set(pos) def reset_position(self): 'Make the original position the active position' pos = self.get_position(original=True) self.set_position(pos, which='active') def _set_artist_props(self, a): 'set the boilerplate props for artists added to axes' a.set_figure(self.figure) if not a.is_transform_set(): a.set_transform(self.transData) a.set_axes(self) def _gen_axes_patch(self): """ Returns the patch used to draw the background of the axes. It is also used as the clipping path for any data elements on the axes. In the standard axes, this is a rectangle, but in other projections it may not be. .. note:: Intended to be overridden by new projection types. """ return mpatches.Rectangle((0.0, 0.0), 1.0, 1.0) def cla(self): 'Clear the current axes' # Note: this is called by Axes.__init__() self.xaxis.cla() self.yaxis.cla() self.ignore_existing_data_limits = True self.callbacks = cbook.CallbackRegistry(('xlim_changed', 'ylim_changed')) if self._sharex is not None: # major and minor are class instances with # locator and formatter attributes self.xaxis.major = self._sharex.xaxis.major self.xaxis.minor = self._sharex.xaxis.minor x0, x1 = self._sharex.get_xlim() self.set_xlim(x0, x1, emit=False) self.xaxis.set_scale(self._sharex.xaxis.get_scale()) else: self.xaxis.set_scale('linear') if self._sharey is not None: self.yaxis.major = self._sharey.yaxis.major self.yaxis.minor = self._sharey.yaxis.minor y0, y1 = self._sharey.get_ylim() self.set_ylim(y0, y1, emit=False) self.yaxis.set_scale(self._sharey.yaxis.get_scale()) else: self.yaxis.set_scale('linear') self._autoscaleon = True self._update_transScale() # needed? self._get_lines = _process_plot_var_args(self) self._get_patches_for_fill = _process_plot_var_args(self, 'fill') self._gridOn = rcParams['axes.grid'] self.lines = [] self.patches = [] self.texts = [] self.tables = [] self.artists = [] self.images = [] self.legend_ = None self.collections = [] # collection.Collection instances self.grid(self._gridOn) props = font_manager.FontProperties(size=rcParams['axes.titlesize']) self.titleOffsetTrans = mtransforms.ScaledTranslation( 0.0, 5.0 / 72.0, self.figure.dpi_scale_trans) self.title = mtext.Text( x=0.5, y=1.0, text='', fontproperties=props, verticalalignment='bottom', horizontalalignment='center', ) self.title.set_transform(self.transAxes + self.titleOffsetTrans) self.title.set_clip_box(None) self._set_artist_props(self.title) # the patch draws the background of the axes. we want this to # be below the other artists; the axesPatch name is # deprecated. We use the frame to draw the edges so we are # setting the edgecolor to None self.patch = self.axesPatch = self._gen_axes_patch() self.patch.set_figure(self.figure) self.patch.set_facecolor(self._axisbg) self.patch.set_edgecolor('None') self.patch.set_linewidth(0) self.patch.set_transform(self.transAxes) # the frame draws the border around the axes and we want this # above. this is a place holder for a more sophisticated # artist that might just draw a left, bottom frame, or a # centered frame, etc the axesFrame name is deprecated self.frame = self.axesFrame = self._gen_axes_patch() self.frame.set_figure(self.figure) self.frame.set_facecolor('none') self.frame.set_edgecolor(rcParams['axes.edgecolor']) self.frame.set_linewidth(rcParams['axes.linewidth']) self.frame.set_transform(self.transAxes) self.frame.set_zorder(2.5) self.axison = True self.xaxis.set_clip_path(self.patch) self.yaxis.set_clip_path(self.patch) self._shared_x_axes.clean() self._shared_y_axes.clean() def clear(self): 'clear the axes' self.cla() def set_color_cycle(self, clist): """ Set the color cycle for any future plot commands on this Axes. clist is a list of mpl color specifiers. """ self._get_lines.set_color_cycle(clist) def ishold(self): 'return the HOLD status of the axes' return self._hold def hold(self, b=None): """ call signature:: hold(b=None) Set the hold state. If *hold* is *None* (default), toggle the *hold* state. Else set the *hold* state to boolean value *b*. Examples: * toggle hold: >>> hold() * turn hold on: >>> hold(True) * turn hold off >>> hold(False) When hold is True, subsequent plot commands will be added to the current axes. When hold is False, the current axes and figure will be cleared on the next plot command """ if b is None: self._hold = not self._hold else: self._hold = b def get_aspect(self): return self._aspect def set_aspect(self, aspect, adjustable=None, anchor=None): """ *aspect* ======== ================================================ value description ======== ================================================ 'auto' automatic; fill position rectangle with data 'normal' same as 'auto'; deprecated 'equal' same scaling from data to plot units for x and y num a circle will be stretched such that the height is num times the width. aspect=1 is the same as aspect='equal'. ======== ================================================ *adjustable* ========= ============================ value description ========= ============================ 'box' change physical size of axes 'datalim' change xlim or ylim ========= ============================ *anchor* ===== ===================== value description ===== ===================== 'C' centered 'SW' lower left corner 'S' middle of bottom edge 'SE' lower right corner etc. ===== ===================== """ if aspect in ('normal', 'auto'): self._aspect = 'auto' elif aspect == 'equal': self._aspect = 'equal' else: self._aspect = float(aspect) # raise ValueError if necessary if adjustable is not None: self.set_adjustable(adjustable) if anchor is not None: self.set_anchor(anchor) def get_adjustable(self): return self._adjustable def set_adjustable(self, adjustable): """ ACCEPTS: [ 'box' | 'datalim' ] """ if adjustable in ('box', 'datalim'): if self in self._shared_x_axes or self in self._shared_y_axes: if adjustable == 'box': raise ValueError( 'adjustable must be "datalim" for shared axes') self._adjustable = adjustable else: raise ValueError('argument must be "box", or "datalim"') def get_anchor(self): return self._anchor def set_anchor(self, anchor): """ *anchor* ===== ============ value description ===== ============ 'C' Center 'SW' bottom left 'S' bottom 'SE' bottom right 'E' right 'NE' top right 'N' top 'NW' top left 'W' left ===== ============ """ if anchor in mtransforms.Bbox.coefs.keys() or len(anchor) == 2: self._anchor = anchor else: raise ValueError('argument must be among %s' % ', '.join(mtransforms.BBox.coefs.keys())) def get_data_ratio(self): """ Returns the aspect ratio of the raw data. This method is intended to be overridden by new projection types. """ xmin,xmax = self.get_xbound() xsize = max(math.fabs(xmax-xmin), 1e-30) ymin,ymax = self.get_ybound() ysize = max(math.fabs(ymax-ymin), 1e-30) return ysize/xsize def apply_aspect(self, position=None): ''' Use :meth:`_aspect` and :meth:`_adjustable` to modify the axes box or the view limits. ''' if position is None: position = self.get_position(original=True) aspect = self.get_aspect() if aspect == 'auto': self.set_position( position , which='active') return if aspect == 'equal': A = 1 else: A = aspect #Ensure at drawing time that any Axes involved in axis-sharing # does not have its position changed. if self in self._shared_x_axes or self in self._shared_y_axes: if self._adjustable == 'box': self._adjustable = 'datalim' warnings.warn( 'shared axes: "adjustable" is being changed to "datalim"') figW,figH = self.get_figure().get_size_inches() fig_aspect = figH/figW if self._adjustable == 'box': box_aspect = A * self.get_data_ratio() pb = position.frozen() pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect) self.set_position(pb1.anchored(self.get_anchor(), pb), 'active') return # reset active to original in case it had been changed # by prior use of 'box' self.set_position(position, which='active') xmin,xmax = self.get_xbound() xsize = max(math.fabs(xmax-xmin), 1e-30) ymin,ymax = self.get_ybound() ysize = max(math.fabs(ymax-ymin), 1e-30) l,b,w,h = position.bounds box_aspect = fig_aspect * (h/w) data_ratio = box_aspect / A y_expander = (data_ratio*xsize/ysize - 1.0) #print 'y_expander', y_expander # If y_expander > 0, the dy/dx viewLim ratio needs to increase if abs(y_expander) < 0.005: #print 'good enough already' return dL = self.dataLim xr = 1.05 * dL.width yr = 1.05 * dL.height xmarg = xsize - xr ymarg = ysize - yr Ysize = data_ratio * xsize Xsize = ysize / data_ratio Xmarg = Xsize - xr Ymarg = Ysize - yr xm = 0 # Setting these targets to, e.g., 0.05*xr does not seem to help. ym = 0 #print 'xmin, xmax, ymin, ymax', xmin, xmax, ymin, ymax #print 'xsize, Xsize, ysize, Ysize', xsize, Xsize, ysize, Ysize changex = (self in self._shared_y_axes and self not in self._shared_x_axes) changey = (self in self._shared_x_axes and self not in self._shared_y_axes) if changex and changey: warnings.warn("adjustable='datalim' cannot work with shared " "x and y axes") return if changex: adjust_y = False else: #print 'xmarg, ymarg, Xmarg, Ymarg', xmarg, ymarg, Xmarg, Ymarg if xmarg > xm and ymarg > ym: adjy = ((Ymarg > 0 and y_expander < 0) or (Xmarg < 0 and y_expander > 0)) else: adjy = y_expander > 0 #print 'y_expander, adjy', y_expander, adjy adjust_y = changey or adjy #(Ymarg > xmarg) if adjust_y: yc = 0.5*(ymin+ymax) y0 = yc - Ysize/2.0 y1 = yc + Ysize/2.0 self.set_ybound((y0, y1)) #print 'New y0, y1:', y0, y1 #print 'New ysize, ysize/xsize', y1-y0, (y1-y0)/xsize else: xc = 0.5*(xmin+xmax) x0 = xc - Xsize/2.0 x1 = xc + Xsize/2.0 self.set_xbound((x0, x1)) #print 'New x0, x1:', x0, x1 #print 'New xsize, ysize/xsize', x1-x0, ysize/(x1-x0) def axis(self, *v, **kwargs): ''' Convenience method for manipulating the x and y view limits and the aspect ratio of the plot. *kwargs* are passed on to :meth:`set_xlim` and :meth:`set_ylim` ''' if len(v)==1 and is_string_like(v[0]): s = v[0].lower() if s=='on': self.set_axis_on() elif s=='off': self.set_axis_off() elif s in ('equal', 'tight', 'scaled', 'normal', 'auto', 'image'): self.set_autoscale_on(True) self.set_aspect('auto') self.autoscale_view() # self.apply_aspect() if s=='equal': self.set_aspect('equal', adjustable='datalim') elif s == 'scaled': self.set_aspect('equal', adjustable='box', anchor='C') self.set_autoscale_on(False) # Req. by Mark Bakker elif s=='tight': self.autoscale_view(tight=True) self.set_autoscale_on(False) elif s == 'image': self.autoscale_view(tight=True) self.set_autoscale_on(False) self.set_aspect('equal', adjustable='box', anchor='C') else: raise ValueError('Unrecognized string %s to axis; ' 'try on or off' % s) xmin, xmax = self.get_xlim() ymin, ymax = self.get_ylim() return xmin, xmax, ymin, ymax try: v[0] except IndexError: emit = kwargs.get('emit', True) xmin = kwargs.get('xmin', None) xmax = kwargs.get('xmax', None) xmin, xmax = self.set_xlim(xmin, xmax, emit) ymin = kwargs.get('ymin', None) ymax = kwargs.get('ymax', None) ymin, ymax = self.set_ylim(ymin, ymax, emit) return xmin, xmax, ymin, ymax v = v[0] if len(v) != 4: raise ValueError('v must contain [xmin xmax ymin ymax]') self.set_xlim([v[0], v[1]]) self.set_ylim([v[2], v[3]]) return v def get_child_artists(self): """ Return a list of artists the axes contains. .. deprecated:: 0.98 """ raise DeprecationWarning('Use get_children instead') def get_frame(self): 'Return the axes Rectangle frame' warnings.warn('use ax.patch instead', DeprecationWarning) return self.patch def get_legend(self): 'Return the legend.Legend instance, or None if no legend is defined' return self.legend_ def get_images(self): 'return a list of Axes images contained by the Axes' return cbook.silent_list('AxesImage', self.images) def get_lines(self): 'Return a list of lines contained by the Axes' return cbook.silent_list('Line2D', self.lines) def get_xaxis(self): 'Return the XAxis instance' return self.xaxis def get_xgridlines(self): 'Get the x grid lines as a list of Line2D instances' return cbook.silent_list('Line2D xgridline', self.xaxis.get_gridlines()) def get_xticklines(self): 'Get the xtick lines as a list of Line2D instances' return cbook.silent_list('Text xtickline', self.xaxis.get_ticklines()) def get_yaxis(self): 'Return the YAxis instance' return self.yaxis def get_ygridlines(self): 'Get the y grid lines as a list of Line2D instances' return cbook.silent_list('Line2D ygridline', self.yaxis.get_gridlines()) def get_yticklines(self): 'Get the ytick lines as a list of Line2D instances' return cbook.silent_list('Line2D ytickline', self.yaxis.get_ticklines()) #### Adding and tracking artists def has_data(self): '''Return *True* if any artists have been added to axes. This should not be used to determine whether the *dataLim* need to be updated, and may not actually be useful for anything. ''' return ( len(self.collections) + len(self.images) + len(self.lines) + len(self.patches))>0 def add_artist(self, a): 'Add any :class:`~matplotlib.artist.Artist` to the axes' a.set_axes(self) self.artists.append(a) self._set_artist_props(a) a.set_clip_path(self.patch) a._remove_method = lambda h: self.artists.remove(h) def add_collection(self, collection, autolim=True): ''' add a :class:`~matplotlib.collections.Collection` instance to the axes ''' label = collection.get_label() if not label: collection.set_label('collection%d'%len(self.collections)) self.collections.append(collection) self._set_artist_props(collection) collection.set_clip_path(self.patch) if autolim: if collection._paths and len(collection._paths): self.update_datalim(collection.get_datalim(self.transData)) collection._remove_method = lambda h: self.collections.remove(h) def add_line(self, line): ''' Add a :class:`~matplotlib.lines.Line2D` to the list of plot lines ''' self._set_artist_props(line) line.set_clip_path(self.patch) self._update_line_limits(line) if not line.get_label(): line.set_label('_line%d'%len(self.lines)) self.lines.append(line) line._remove_method = lambda h: self.lines.remove(h) def _update_line_limits(self, line): p = line.get_path() if p.vertices.size > 0: self.dataLim.update_from_path(p, self.ignore_existing_data_limits, updatex=line.x_isdata, updatey=line.y_isdata) self.ignore_existing_data_limits = False def add_patch(self, p): """ Add a :class:`~matplotlib.patches.Patch` *p* to the list of axes patches; the clipbox will be set to the Axes clipping box. If the transform is not set, it will be set to :attr:`transData`. """ self._set_artist_props(p) p.set_clip_path(self.patch) self._update_patch_limits(p) self.patches.append(p) p._remove_method = lambda h: self.patches.remove(h) def _update_patch_limits(self, patch): 'update the data limits for patch *p*' # hist can add zero height Rectangles, which is useful to keep # the bins, counts and patches lined up, but it throws off log # scaling. We'll ignore rects with zero height or width in # the auto-scaling if (isinstance(patch, mpatches.Rectangle) and (patch.get_width()==0 or patch.get_height()==0)): return vertices = patch.get_path().vertices if vertices.size > 0: xys = patch.get_patch_transform().transform(vertices) if patch.get_data_transform() != self.transData: transform = (patch.get_data_transform() + self.transData.inverted()) xys = transform.transform(xys) self.update_datalim(xys, updatex=patch.x_isdata, updatey=patch.y_isdata) def add_table(self, tab): ''' Add a :class:`~matplotlib.tables.Table` instance to the list of axes tables ''' self._set_artist_props(tab) self.tables.append(tab) tab.set_clip_path(self.patch) tab._remove_method = lambda h: self.tables.remove(h) def relim(self): 'recompute the data limits based on current artists' # Collections are deliberately not supported (yet); see # the TODO note in artists.py. self.dataLim.ignore(True) self.ignore_existing_data_limits = True for line in self.lines: self._update_line_limits(line) for p in self.patches: self._update_patch_limits(p) def update_datalim(self, xys, updatex=True, updatey=True): 'Update the data lim bbox with seq of xy tups or equiv. 2-D array' # if no data is set currently, the bbox will ignore its # limits and set the bound to be the bounds of the xydata. # Otherwise, it will compute the bounds of it's current data # and the data in xydata if iterable(xys) and not len(xys): return if not ma.isMaskedArray(xys): xys = np.asarray(xys) self.dataLim.update_from_data_xy(xys, self.ignore_existing_data_limits, updatex=updatex, updatey=updatey) self.ignore_existing_data_limits = False def update_datalim_numerix(self, x, y): 'Update the data lim bbox with seq of xy tups' # if no data is set currently, the bbox will ignore it's # limits and set the bound to be the bounds of the xydata. # Otherwise, it will compute the bounds of it's current data # and the data in xydata if iterable(x) and not len(x): return self.dataLim.update_from_data(x, y, self.ignore_existing_data_limits) self.ignore_existing_data_limits = False def update_datalim_bounds(self, bounds): ''' Update the datalim to include the given :class:`~matplotlib.transforms.Bbox` *bounds* ''' self.dataLim.set(mtransforms.Bbox.union([self.dataLim, bounds])) def _process_unit_info(self, xdata=None, ydata=None, kwargs=None): 'look for unit *kwargs* and update the axis instances as necessary' if self.xaxis is None or self.yaxis is None: return #print 'processing', self.get_geometry() if xdata is not None: # we only need to update if there is nothing set yet. if not self.xaxis.have_units(): self.xaxis.update_units(xdata) #print '\tset from xdata', self.xaxis.units if ydata is not None: # we only need to update if there is nothing set yet. if not self.yaxis.have_units(): self.yaxis.update_units(ydata) #print '\tset from ydata', self.yaxis.units # process kwargs 2nd since these will override default units if kwargs is not None: xunits = kwargs.pop( 'xunits', self.xaxis.units) if xunits!=self.xaxis.units: #print '\tkw setting xunits', xunits self.xaxis.set_units(xunits) # If the units being set imply a different converter, # we need to update. if xdata is not None: self.xaxis.update_units(xdata) yunits = kwargs.pop('yunits', self.yaxis.units) if yunits!=self.yaxis.units: #print '\tkw setting yunits', yunits self.yaxis.set_units(yunits) # If the units being set imply a different converter, # we need to update. if ydata is not None: self.yaxis.update_units(ydata) def in_axes(self, mouseevent): ''' return *True* if the given *mouseevent* (in display coords) is in the Axes ''' return self.patch.contains(mouseevent)[0] def get_autoscale_on(self): """ Get whether autoscaling is applied on plot commands """ return self._autoscaleon def set_autoscale_on(self, b): """ Set whether autoscaling is applied on plot commands accepts: [ *True* | *False* ] """ self._autoscaleon = b def autoscale_view(self, tight=False, scalex=True, scaley=True): """ autoscale the view limits using the data limits. You can selectively autoscale only a single axis, eg, the xaxis by setting *scaley* to *False*. The autoscaling preserves any axis direction reversal that has already been done. """ # if image data only just use the datalim if not self._autoscaleon: return if scalex: xshared = self._shared_x_axes.get_siblings(self) dl = [ax.dataLim for ax in xshared] bb = mtransforms.BboxBase.union(dl) x0, x1 = bb.intervalx if scaley: yshared = self._shared_y_axes.get_siblings(self) dl = [ax.dataLim for ax in yshared] bb = mtransforms.BboxBase.union(dl) y0, y1 = bb.intervaly if (tight or (len(self.images)>0 and len(self.lines)==0 and len(self.patches)==0)): if scalex: self.set_xbound(x0, x1) if scaley: self.set_ybound(y0, y1) return if scalex: XL = self.xaxis.get_major_locator().view_limits(x0, x1) self.set_xbound(XL) if scaley: YL = self.yaxis.get_major_locator().view_limits(y0, y1) self.set_ybound(YL) #### Drawing def draw(self, renderer=None, inframe=False): "Draw everything (plot lines, axes, labels)" if renderer is None: renderer = self._cachedRenderer if renderer is None: raise RuntimeError('No renderer defined') if not self.get_visible(): return renderer.open_group('axes') self.apply_aspect() # the patch draws the background rectangle -- the frame below # will draw the edges if self.axison and self._frameon: self.patch.draw(renderer) artists = [] if len(self.images)<=1 or renderer.option_image_nocomposite(): for im in self.images: im.draw(renderer) else: # make a composite image blending alpha # list of (mimage.Image, ox, oy) mag = renderer.get_image_magnification() ims = [(im.make_image(mag),0,0) for im in self.images if im.get_visible()] l, b, r, t = self.bbox.extents width = mag*((round(r) + 0.5) - (round(l) - 0.5)) height = mag*((round(t) + 0.5) - (round(b) - 0.5)) im = mimage.from_images(height, width, ims) im.is_grayscale = False l, b, w, h = self.bbox.bounds # composite images need special args so they will not # respect z-order for now renderer.draw_image( round(l), round(b), im, self.bbox, self.patch.get_path(), self.patch.get_transform()) artists.extend(self.collections) artists.extend(self.patches) artists.extend(self.lines) artists.extend(self.texts) artists.extend(self.artists) if self.axison and not inframe: if self._axisbelow: self.xaxis.set_zorder(0.5) self.yaxis.set_zorder(0.5) else: self.xaxis.set_zorder(2.5) self.yaxis.set_zorder(2.5) artists.extend([self.xaxis, self.yaxis]) if not inframe: artists.append(self.title) artists.extend(self.tables) if self.legend_ is not None: artists.append(self.legend_) # the frame draws the edges around the axes patch -- we # decouple these so the patch can be in the background and the # frame in the foreground. if self.axison and self._frameon: artists.append(self.frame) dsu = [ (a.zorder, i, a) for i, a in enumerate(artists) if not a.get_animated() ] dsu.sort() for zorder, i, a in dsu: a.draw(renderer) renderer.close_group('axes') self._cachedRenderer = renderer def draw_artist(self, a): """ This method can only be used after an initial draw which caches the renderer. It is used to efficiently update Axes data (axis ticks, labels, etc are not updated) """ assert self._cachedRenderer is not None a.draw(self._cachedRenderer) def redraw_in_frame(self): """ This method can only be used after an initial draw which caches the renderer. It is used to efficiently update Axes data (axis ticks, labels, etc are not updated) """ assert self._cachedRenderer is not None self.draw(self._cachedRenderer, inframe=True) def get_renderer_cache(self): return self._cachedRenderer def __draw_animate(self): # ignore for now; broken if self._lastRenderer is None: raise RuntimeError('You must first call ax.draw()') dsu = [(a.zorder, a) for a in self.animated.keys()] dsu.sort() renderer = self._lastRenderer renderer.blit() for tmp, a in dsu: a.draw(renderer) #### Axes rectangle characteristics def get_frame_on(self): """ Get whether the axes rectangle patch is drawn """ return self._frameon def set_frame_on(self, b): """ Set whether the axes rectangle patch is drawn ACCEPTS: [ *True* | *False* ] """ self._frameon = b def get_axisbelow(self): """ Get whether axis below is true or not """ return self._axisbelow def set_axisbelow(self, b): """ Set whether the axis ticks and gridlines are above or below most artists ACCEPTS: [ *True* | *False* ] """ self._axisbelow = b def grid(self, b=None, **kwargs): """ call signature:: grid(self, b=None, **kwargs) Set the axes grids on or off; *b* is a boolean If *b* is *None* and ``len(kwargs)==0``, toggle the grid state. If *kwargs* are supplied, it is assumed that you want a grid and *b* is thus set to *True* *kawrgs* are used to set the grid line properties, eg:: ax.grid(color='r', linestyle='-', linewidth=2) Valid :class:`~matplotlib.lines.Line2D` kwargs are %(Line2D)s """ if len(kwargs): b = True self.xaxis.grid(b, **kwargs) self.yaxis.grid(b, **kwargs) grid.__doc__ = cbook.dedent(grid.__doc__) % martist.kwdocd def ticklabel_format(self, **kwargs): """ Convenience method for manipulating the ScalarFormatter used by default for linear axes. Optional keyword arguments: ============ ===================================== Keyword Description ============ ===================================== *style* [ 'sci' (or 'scientific') | 'plain' ] plain turns off scientific notation *scilimits* (m, n), pair of integers; if *style* is 'sci', scientific notation will be used for numbers outside the range 10`-m`:sup: to 10`n`:sup:. Use (0,0) to include all numbers. *axis* [ 'x' | 'y' | 'both' ] ============ ===================================== Only the major ticks are affected. If the method is called when the :class:`~matplotlib.ticker.ScalarFormatter` is not the :class:`~matplotlib.ticker.Formatter` being used, an :exc:`AttributeError` will be raised. """ style = kwargs.pop('style', '').lower() scilimits = kwargs.pop('scilimits', None) if scilimits is not None: try: m, n = scilimits m+n+1 # check that both are numbers except (ValueError, TypeError): raise ValueError("scilimits must be a sequence of 2 integers") axis = kwargs.pop('axis', 'both').lower() if style[:3] == 'sci': sb = True elif style in ['plain', 'comma']: sb = False if style == 'plain': cb = False else: cb = True raise NotImplementedError, "comma style remains to be added" elif style == '': sb = None else: raise ValueError, "%s is not a valid style value" try: if sb is not None: if axis == 'both' or axis == 'x': self.xaxis.major.formatter.set_scientific(sb) if axis == 'both' or axis == 'y': self.yaxis.major.formatter.set_scientific(sb) if scilimits is not None: if axis == 'both' or axis == 'x': self.xaxis.major.formatter.set_powerlimits(scilimits) if axis == 'both' or axis == 'y': self.yaxis.major.formatter.set_powerlimits(scilimits) except AttributeError: raise AttributeError( "This method only works with the ScalarFormatter.") def set_axis_off(self): """turn off the axis""" self.axison = False def set_axis_on(self): """turn on the axis""" self.axison = True def get_axis_bgcolor(self): 'Return the axis background color' return self._axisbg def set_axis_bgcolor(self, color): """ set the axes background color ACCEPTS: any matplotlib color - see :func:`~matplotlib.pyplot.colors` """ self._axisbg = color self.patch.set_facecolor(color) ### data limits, ticks, tick labels, and formatting def invert_xaxis(self): "Invert the x-axis." left, right = self.get_xlim() self.set_xlim(right, left) def xaxis_inverted(self): 'Returns True if the x-axis is inverted.' left, right = self.get_xlim() return right < left def get_xbound(self): """ Returns the x-axis numerical bounds where:: lowerBound < upperBound """ left, right = self.get_xlim() if left < right: return left, right else: return right, left def set_xbound(self, lower=None, upper=None): """ Set the lower and upper numerical bounds of the x-axis. This method will honor axes inversion regardless of parameter order. """ if upper is None and iterable(lower): lower,upper = lower old_lower,old_upper = self.get_xbound() if lower is None: lower = old_lower if upper is None: upper = old_upper if self.xaxis_inverted(): if lower < upper: self.set_xlim(upper, lower) else: self.set_xlim(lower, upper) else: if lower < upper: self.set_xlim(lower, upper) else: self.set_xlim(upper, lower) def get_xlim(self): """ Get the x-axis range [*xmin*, *xmax*] """ return tuple(self.viewLim.intervalx) def set_xlim(self, xmin=None, xmax=None, emit=True, **kwargs): """ call signature:: set_xlim(self, *args, **kwargs) Set the limits for the xaxis Returns the current xlimits as a length 2 tuple: [*xmin*, *xmax*] Examples:: set_xlim((valmin, valmax)) set_xlim(valmin, valmax) set_xlim(xmin=1) # xmax unchanged set_xlim(xmax=1) # xmin unchanged Keyword arguments: *ymin*: scalar the min of the ylim *ymax*: scalar the max of the ylim *emit*: [ True | False ] notify observers of lim change ACCEPTS: len(2) sequence of floats """ if xmax is None and iterable(xmin): xmin,xmax = xmin self._process_unit_info(xdata=(xmin, xmax)) if xmin is not None: xmin = self.convert_xunits(xmin) if xmax is not None: xmax = self.convert_xunits(xmax) old_xmin,old_xmax = self.get_xlim() if xmin is None: xmin = old_xmin if xmax is None: xmax = old_xmax xmin, xmax = mtransforms.nonsingular(xmin, xmax, increasing=False) xmin, xmax = self.xaxis.limit_range_for_scale(xmin, xmax) self.viewLim.intervalx = (xmin, xmax) if emit: self.callbacks.process('xlim_changed', self) # Call all of the other x-axes that are shared with this one for other in self._shared_x_axes.get_siblings(self): if other is not self: other.set_xlim(self.viewLim.intervalx, emit=False) if (other.figure != self.figure and other.figure.canvas is not None): other.figure.canvas.draw_idle() return xmin, xmax def get_xscale(self): 'return the xaxis scale string: %s' % ( ", ".join(mscale.get_scale_names())) return self.xaxis.get_scale() def set_xscale(self, value, **kwargs): """ call signature:: set_xscale(value) Set the scaling of the x-axis: %(scale)s ACCEPTS: [%(scale)s] Different kwargs are accepted, depending on the scale: %(scale_docs)s """ self.xaxis.set_scale(value, **kwargs) self.autoscale_view() self._update_transScale() set_xscale.__doc__ = cbook.dedent(set_xscale.__doc__) % { 'scale': ' | '.join([repr(x) for x in mscale.get_scale_names()]), 'scale_docs': mscale.get_scale_docs().strip()} def get_xticks(self, minor=False): 'Return the x ticks as a list of locations' return self.xaxis.get_ticklocs(minor=minor) def set_xticks(self, ticks, minor=False): """ Set the x ticks with list of *ticks* ACCEPTS: sequence of floats """ return self.xaxis.set_ticks(ticks, minor=minor) def get_xmajorticklabels(self): 'Get the xtick labels as a list of Text instances' return cbook.silent_list('Text xticklabel', self.xaxis.get_majorticklabels()) def get_xminorticklabels(self): 'Get the xtick labels as a list of Text instances' return cbook.silent_list('Text xticklabel', self.xaxis.get_minorticklabels()) def get_xticklabels(self, minor=False): 'Get the xtick labels as a list of Text instances' return cbook.silent_list('Text xticklabel', self.xaxis.get_ticklabels(minor=minor)) def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs): """ call signature:: set_xticklabels(labels, fontdict=None, minor=False, **kwargs) Set the xtick labels with list of strings *labels*. Return a list of axis text instances. *kwargs* set the :class:`~matplotlib.text.Text` properties. Valid properties are %(Text)s ACCEPTS: sequence of strings """ return self.xaxis.set_ticklabels(labels, fontdict, minor=minor, **kwargs) set_xticklabels.__doc__ = cbook.dedent( set_xticklabels.__doc__) % martist.kwdocd def invert_yaxis(self): "Invert the y-axis." left, right = self.get_ylim() self.set_ylim(right, left) def yaxis_inverted(self): 'Returns True if the y-axis is inverted.' left, right = self.get_ylim() return right < left def get_ybound(self): "Return y-axis numerical bounds in the form of lowerBound < upperBound" left, right = self.get_ylim() if left < right: return left, right else: return right, left def set_ybound(self, lower=None, upper=None): """Set the lower and upper numerical bounds of the y-axis. This method will honor axes inversion regardless of parameter order. """ if upper is None and iterable(lower): lower,upper = lower old_lower,old_upper = self.get_ybound() if lower is None: lower = old_lower if upper is None: upper = old_upper if self.yaxis_inverted(): if lower < upper: self.set_ylim(upper, lower) else: self.set_ylim(lower, upper) else: if lower < upper: self.set_ylim(lower, upper) else: self.set_ylim(upper, lower) def get_ylim(self): """ Get the y-axis range [*ymin*, *ymax*] """ return tuple(self.viewLim.intervaly) def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs): """ call signature:: set_ylim(self, *args, **kwargs): Set the limits for the yaxis; v = [ymin, ymax]:: set_ylim((valmin, valmax)) set_ylim(valmin, valmax) set_ylim(ymin=1) # ymax unchanged set_ylim(ymax=1) # ymin unchanged Keyword arguments: *ymin*: scalar the min of the ylim *ymax*: scalar the max of the ylim *emit*: [ True | False ] notify observers of lim change Returns the current ylimits as a length 2 tuple ACCEPTS: len(2) sequence of floats """ if ymax is None and iterable(ymin): ymin,ymax = ymin if ymin is not None: ymin = self.convert_yunits(ymin) if ymax is not None: ymax = self.convert_yunits(ymax) old_ymin,old_ymax = self.get_ylim() if ymin is None: ymin = old_ymin if ymax is None: ymax = old_ymax ymin, ymax = mtransforms.nonsingular(ymin, ymax, increasing=False) ymin, ymax = self.yaxis.limit_range_for_scale(ymin, ymax) self.viewLim.intervaly = (ymin, ymax) if emit: self.callbacks.process('ylim_changed', self) # Call all of the other y-axes that are shared with this one for other in self._shared_y_axes.get_siblings(self): if other is not self: other.set_ylim(self.viewLim.intervaly, emit=False) if (other.figure != self.figure and other.figure.canvas is not None): other.figure.canvas.draw_idle() return ymin, ymax def get_yscale(self): 'return the xaxis scale string: %s' % ( ", ".join(mscale.get_scale_names())) return self.yaxis.get_scale() def set_yscale(self, value, **kwargs): """ call signature:: set_yscale(value) Set the scaling of the y-axis: %(scale)s ACCEPTS: [%(scale)s] Different kwargs are accepted, depending on the scale: %(scale_docs)s """ self.yaxis.set_scale(value, **kwargs) self.autoscale_view() self._update_transScale() set_yscale.__doc__ = cbook.dedent(set_yscale.__doc__) % { 'scale': ' | '.join([repr(x) for x in mscale.get_scale_names()]), 'scale_docs': mscale.get_scale_docs().strip()} def get_yticks(self, minor=False): 'Return the y ticks as a list of locations' return self.yaxis.get_ticklocs(minor=minor) def set_yticks(self, ticks, minor=False): """ Set the y ticks with list of *ticks* ACCEPTS: sequence of floats Keyword arguments: *minor*: [ False | True ] Sets the minor ticks if True """ return self.yaxis.set_ticks(ticks, minor=minor) def get_ymajorticklabels(self): 'Get the xtick labels as a list of Text instances' return cbook.silent_list('Text yticklabel', self.yaxis.get_majorticklabels()) def get_yminorticklabels(self): 'Get the xtick labels as a list of Text instances' return cbook.silent_list('Text yticklabel', self.yaxis.get_minorticklabels()) def get_yticklabels(self, minor=False): 'Get the xtick labels as a list of Text instances' return cbook.silent_list('Text yticklabel', self.yaxis.get_ticklabels(minor=minor)) def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs): """ call signature:: set_yticklabels(labels, fontdict=None, minor=False, **kwargs) Set the ytick labels with list of strings *labels*. Return a list of :class:`~matplotlib.text.Text` instances. *kwargs* set :class:`~matplotlib.text.Text` properties for the labels. Valid properties are %(Text)s ACCEPTS: sequence of strings """ return self.yaxis.set_ticklabels(labels, fontdict, minor=minor, **kwargs) set_yticklabels.__doc__ = cbook.dedent( set_yticklabels.__doc__) % martist.kwdocd def xaxis_date(self, tz=None): """Sets up x-axis ticks and labels that treat the x data as dates. *tz* is the time zone to use in labeling dates. Defaults to rc value. """ xmin, xmax = self.dataLim.intervalx if xmin==0.: # no data has been added - let's set the default datalim. # We should probably use a better proxy for the datalim # have been updated than the ignore setting dmax = today = datetime.date.today() dmin = today-datetime.timedelta(days=10) self._process_unit_info(xdata=(dmin, dmax)) dmin, dmax = self.convert_xunits([dmin, dmax]) self.viewLim.intervalx = dmin, dmax self.dataLim.intervalx = dmin, dmax locator = self.xaxis.get_major_locator() if not isinstance(locator, mdates.DateLocator): locator = mdates.AutoDateLocator(tz) self.xaxis.set_major_locator(locator) # the autolocator uses the viewlim to pick the right date # locator, but it may not have correct viewlim before an # autoscale. If the viewlim is still zero..1, set it to the # datalim and the autoscaler will update it on request if self.viewLim.intervalx[0]==0.: self.viewLim.intervalx = tuple(self.dataLim.intervalx) locator.refresh() formatter = self.xaxis.get_major_formatter() if not isinstance(formatter, mdates.DateFormatter): formatter = mdates.AutoDateFormatter(locator, tz) self.xaxis.set_major_formatter(formatter) def yaxis_date(self, tz=None): """Sets up y-axis ticks and labels that treat the y data as dates. *tz* is the time zone to use in labeling dates. Defaults to rc value. """ ymin, ymax = self.dataLim.intervaly if ymin==0.: # no data has been added - let's set the default datalim. # We should probably use a better proxy for the datalim # have been updated than the ignore setting dmax = today = datetime.date.today() dmin = today-datetime.timedelta(days=10) self._process_unit_info(ydata=(dmin, dmax)) dmin, dmax = self.convert_yunits([dmin, dmax]) self.viewLim.intervaly = dmin, dmax self.dataLim.intervaly = dmin, dmax locator = self.yaxis.get_major_locator() if not isinstance(locator, mdates.DateLocator): locator = mdates.AutoDateLocator(tz) self.yaxis.set_major_locator(locator) # the autolocator uses the viewlim to pick the right date # locator, but it may not have correct viewlim before an # autoscale. If the viewlim is still zero..1, set it to the # datalim and the autoscaler will update it on request if self.viewLim.intervaly[0]==0.: self.viewLim.intervaly = tuple(self.dataLim.intervaly) locator.refresh() formatter = self.xaxis.get_major_formatter() if not isinstance(formatter, mdates.DateFormatter): formatter = mdates.AutoDateFormatter(locator, tz) self.yaxis.set_major_formatter(formatter) def format_xdata(self, x): """ Return *x* string formatted. This function will use the attribute self.fmt_xdata if it is callable, else will fall back on the xaxis major formatter """ try: return self.fmt_xdata(x) except TypeError: func = self.xaxis.get_major_formatter().format_data_short val = func(x) return val def format_ydata(self, y): """ Return y string formatted. This function will use the :attr:`fmt_ydata` attribute if it is callable, else will fall back on the yaxis major formatter """ try: return self.fmt_ydata(y) except TypeError: func = self.yaxis.get_major_formatter().format_data_short val = func(y) return val def format_coord(self, x, y): 'return a format string formatting the *x*, *y* coord' if x is None: x = '???' if y is None: y = '???' xs = self.format_xdata(x) ys = self.format_ydata(y) return 'x=%s, y=%s'%(xs,ys) #### Interactive manipulation def can_zoom(self): """ Return *True* if this axes support the zoom box """ return True def get_navigate(self): """ Get whether the axes responds to navigation commands """ return self._navigate def set_navigate(self, b): """ Set whether the axes responds to navigation toolbar commands ACCEPTS: [ True | False ] """ self._navigate = b def get_navigate_mode(self): """ Get the navigation toolbar button status: 'PAN', 'ZOOM', or None """ return self._navigate_mode def set_navigate_mode(self, b): """ Set the navigation toolbar button status; .. warning:: this is not a user-API function. """ self._navigate_mode = b def start_pan(self, x, y, button): """ Called when a pan operation has started. *x*, *y* are the mouse coordinates in display coords. button is the mouse button number: * 1: LEFT * 2: MIDDLE * 3: RIGHT .. note:: Intended to be overridden by new projection types. """ self._pan_start = cbook.Bunch( lim = self.viewLim.frozen(), trans = self.transData.frozen(), trans_inverse = self.transData.inverted().frozen(), bbox = self.bbox.frozen(), x = x, y = y ) def end_pan(self): """ Called when a pan operation completes (when the mouse button is up.) .. note:: Intended to be overridden by new projection types. """ del self._pan_start def drag_pan(self, button, key, x, y): """ Called when the mouse moves during a pan operation. *button* is the mouse button number: * 1: LEFT * 2: MIDDLE * 3: RIGHT *key* is a "shift" key *x*, *y* are the mouse coordinates in display coords. .. note:: Intended to be overridden by new projection types. """ def format_deltas(key, dx, dy): if key=='control': if(abs(dx)>abs(dy)): dy = dx else: dx = dy elif key=='x': dy = 0 elif key=='y': dx = 0 elif key=='shift': if 2*abs(dx) < abs(dy): dx=0 elif 2*abs(dy) < abs(dx): dy=0 elif(abs(dx)>abs(dy)): dy=dy/abs(dy)*abs(dx) else: dx=dx/abs(dx)*abs(dy) return (dx,dy) p = self._pan_start dx = x - p.x dy = y - p.y if dx == 0 and dy == 0: return if button == 1: dx, dy = format_deltas(key, dx, dy) result = p.bbox.translated(-dx, -dy) \ .transformed(p.trans_inverse) elif button == 3: try: dx = -dx / float(self.bbox.width) dy = -dy / float(self.bbox.height) dx, dy = format_deltas(key, dx, dy) if self.get_aspect() != 'auto': dx = 0.5 * (dx + dy) dy = dx alpha = np.power(10.0, (dx, dy)) start = p.trans_inverse.transform_point((p.x, p.y)) lim_points = p.lim.get_points() result = start + alpha * (lim_points - start) result = mtransforms.Bbox(result) except OverflowError: warnings.warn('Overflow while panning') return self.set_xlim(*result.intervalx) self.set_ylim(*result.intervaly) def get_cursor_props(self): """ return the cursor propertiess as a (*linewidth*, *color*) tuple, where *linewidth* is a float and *color* is an RGBA tuple """ return self._cursorProps def set_cursor_props(self, *args): """ Set the cursor property as:: ax.set_cursor_props(linewidth, color) or:: ax.set_cursor_props((linewidth, color)) ACCEPTS: a (*float*, *color*) tuple """ if len(args)==1: lw, c = args[0] elif len(args)==2: lw, c = args else: raise ValueError('args must be a (linewidth, color) tuple') c =mcolors.colorConverter.to_rgba(c) self._cursorProps = lw, c def connect(self, s, func): """ Register observers to be notified when certain events occur. Register with callback functions with the following signatures. The function has the following signature:: func(ax) # where ax is the instance making the callback. The following events can be connected to: 'xlim_changed','ylim_changed' The connection id is is returned - you can use this with disconnect to disconnect from the axes event """ raise DeprecationWarning('use the callbacks CallbackRegistry instance ' 'instead') def disconnect(self, cid): 'disconnect from the Axes event.' raise DeprecationWarning('use the callbacks CallbackRegistry instance ' 'instead') def get_children(self): 'return a list of child artists' children = [] children.append(self.xaxis) children.append(self.yaxis) children.extend(self.lines) children.extend(self.patches) children.extend(self.texts) children.extend(self.tables) children.extend(self.artists) children.extend(self.images) if self.legend_ is not None: children.append(self.legend_) children.extend(self.collections) children.append(self.title) children.append(self.patch) children.append(self.frame) return children def contains(self,mouseevent): """Test whether the mouse event occured in the axes. Returns T/F, {} """ if callable(self._contains): return self._contains(self,mouseevent) return self.patch.contains(mouseevent) def pick(self, *args): """ call signature:: pick(mouseevent) each child artist will fire a pick event if mouseevent is over the artist and the artist has picker set """ if len(args)>1: raise DeprecationWarning('New pick API implemented -- ' 'see API_CHANGES in the src distribution') martist.Artist.pick(self,args[0]) def __pick(self, x, y, trans=None, among=None): """ Return the artist under point that is closest to the *x*, *y*. If *trans* is *None*, *x*, and *y* are in window coords, (0,0 = lower left). Otherwise, *trans* is a :class:`~matplotlib.transforms.Transform` that specifies the coordinate system of *x*, *y*. The selection of artists from amongst which the pick function finds an artist can be narrowed using the optional keyword argument *among*. If provided, this should be either a sequence of permitted artists or a function taking an artist as its argument and returning a true value if and only if that artist can be selected. Note this algorithm calculates distance to the vertices of the polygon, so if you want to pick a patch, click on the edge! """ # MGDTODO: Needs updating if trans is not None: xywin = trans.transform_point((x,y)) else: xywin = x,y def dist_points(p1, p2): 'return the distance between two points' x1, y1 = p1 x2, y2 = p2 return math.sqrt((x1-x2)**2+(y1-y2)**2) def dist_x_y(p1, x, y): '*x* and *y* are arrays; return the distance to the closest point' x1, y1 = p1 return min(np.sqrt((x-x1)**2+(y-y1)**2)) def dist(a): if isinstance(a, Text): bbox = a.get_window_extent() l,b,w,h = bbox.bounds verts = (l,b), (l,b+h), (l+w,b+h), (l+w, b) xt, yt = zip(*verts) elif isinstance(a, Patch): path = a.get_path() tverts = a.get_transform().transform_path(path) xt, yt = zip(*tverts) elif isinstance(a, mlines.Line2D): xdata = a.get_xdata(orig=False) ydata = a.get_ydata(orig=False) xt, yt = a.get_transform().numerix_x_y(xdata, ydata) return dist_x_y(xywin, np.asarray(xt), np.asarray(yt)) artists = self.lines + self.patches + self.texts if callable(among): artists = filter(test, artists) elif iterable(among): amongd = dict([(k,1) for k in among]) artists = [a for a in artists if a in amongd] elif among is None: pass else: raise ValueError('among must be callable or iterable') if not len(artists): return None ds = [ (dist(a),a) for a in artists] ds.sort() return ds[0][1] #### Labelling def get_title(self): """ Get the title text string. """ return self.title.get_text() def set_title(self, label, fontdict=None, **kwargs): """ call signature:: set_title(label, fontdict=None, **kwargs): Set the title for the axes. kwargs are Text properties: %(Text)s ACCEPTS: str .. seealso:: :meth:`text`: for information on how override and the optional args work """ default = { 'fontsize':rcParams['axes.titlesize'], 'verticalalignment' : 'bottom', 'horizontalalignment' : 'center' } self.title.set_text(label) self.title.update(default) if fontdict is not None: self.title.update(fontdict) self.title.update(kwargs) return self.title set_title.__doc__ = cbook.dedent(set_title.__doc__) % martist.kwdocd def get_xlabel(self): """ Get the xlabel text string. """ label = self.xaxis.get_label() return label.get_text() def set_xlabel(self, xlabel, fontdict=None, **kwargs): """ call signature:: set_xlabel(xlabel, fontdict=None, **kwargs) Set the label for the xaxis. Valid kwargs are Text properties: %(Text)s ACCEPTS: str .. seealso:: :meth:`text`: for information on how override and the optional args work """ label = self.xaxis.get_label() label.set_text(xlabel) if fontdict is not None: label.update(fontdict) label.update(kwargs) return label set_xlabel.__doc__ = cbook.dedent(set_xlabel.__doc__) % martist.kwdocd def get_ylabel(self): """ Get the ylabel text string. """ label = self.yaxis.get_label() return label.get_text() def set_ylabel(self, ylabel, fontdict=None, **kwargs): """ call signature:: set_ylabel(ylabel, fontdict=None, **kwargs) Set the label for the yaxis Valid kwargs are Text properties: %(Text)s ACCEPTS: str .. seealso:: :meth:`text`: for information on how override and the optional args work """ label = self.yaxis.get_label() label.set_text(ylabel) if fontdict is not None: label.update(fontdict) label.update(kwargs) return label set_ylabel.__doc__ = cbook.dedent(set_ylabel.__doc__) % martist.kwdocd def text(self, x, y, s, fontdict=None, withdash=False, **kwargs): """ call signature:: text(x, y, s, fontdict=None, **kwargs) Add text in string *s* to axis at location *x*, *y*, data coordinates. Keyword arguments: *fontdict*: A dictionary to override the default text properties. If *fontdict* is *None*, the defaults are determined by your rc parameters. *withdash*: [ False | True ] Creates a :class:`~matplotlib.text.TextWithDash` instance instead of a :class:`~matplotlib.text.Text` instance. Individual keyword arguments can be used to override any given parameter:: text(x, y, s, fontsize=12) The default transform specifies that text is in data coords, alternatively, you can specify text in axis coords (0,0 is lower-left and 1,1 is upper-right). The example below places text in the center of the axes:: text(0.5, 0.5,'matplotlib', horizontalalignment='center', verticalalignment='center', transform = ax.transAxes) You can put a rectangular box around the text instance (eg. to set a background color) by using the keyword *bbox*. *bbox* is a dictionary of :class:`matplotlib.patches.Rectangle` properties. For example:: text(x, y, s, bbox=dict(facecolor='red', alpha=0.5)) Valid kwargs are :class:`matplotlib.text.Text` properties: %(Text)s """ default = { 'verticalalignment' : 'bottom', 'horizontalalignment' : 'left', #'verticalalignment' : 'top', 'transform' : self.transData, } # At some point if we feel confident that TextWithDash # is robust as a drop-in replacement for Text and that # the performance impact of the heavier-weight class # isn't too significant, it may make sense to eliminate # the withdash kwarg and simply delegate whether there's # a dash to TextWithDash and dashlength. if withdash: t = mtext.TextWithDash( x=x, y=y, text=s, ) else: t = mtext.Text( x=x, y=y, text=s, ) self._set_artist_props(t) t.update(default) if fontdict is not None: t.update(fontdict) t.update(kwargs) self.texts.append(t) t._remove_method = lambda h: self.texts.remove(h) #if t.get_clip_on(): t.set_clip_box(self.bbox) if 'clip_on' in kwargs: t.set_clip_box(self.bbox) return t text.__doc__ = cbook.dedent(text.__doc__) % martist.kwdocd def annotate(self, *args, **kwargs): """ call signature:: annotate(s, xy, xytext=None, xycoords='data', textcoords='data', arrowprops=None, **kwargs) Keyword arguments: %(Annotation)s .. plot:: mpl_examples/pylab_examples/annotation_demo2.py """ a = mtext.Annotation(*args, **kwargs) a.set_transform(mtransforms.IdentityTransform()) self._set_artist_props(a) if kwargs.has_key('clip_on'): a.set_clip_path(self.patch) self.texts.append(a) return a annotate.__doc__ = cbook.dedent(annotate.__doc__) % martist.kwdocd #### Lines and spans def axhline(self, y=0, xmin=0, xmax=1, **kwargs): """ call signature:: axhline(y=0, xmin=0, xmax=1, **kwargs) Axis Horizontal Line Draw a horizontal line at *y* from *xmin* to *xmax*. With the default values of *xmin* = 0 and *xmax* = 1, this line will always span the horizontal extent of the axes, regardless of the xlim settings, even if you change them, eg. with the :meth:`set_xlim` command. That is, the horizontal extent is in axes coords: 0=left, 0.5=middle, 1.0=right but the *y* location is in data coordinates. Return value is the :class:`~matplotlib.lines.Line2D` instance. kwargs are the same as kwargs to plot, and can be used to control the line properties. Eg., * draw a thick red hline at *y* = 0 that spans the xrange >>> axhline(linewidth=4, color='r') * draw a default hline at *y* = 1 that spans the xrange >>> axhline(y=1) * draw a default hline at *y* = .5 that spans the the middle half of the xrange >>> axhline(y=.5, xmin=0.25, xmax=0.75) Valid kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s .. seealso:: :meth:`axhspan`: for example plot and source code """ ymin, ymax = self.get_ybound() # We need to strip away the units for comparison with # non-unitized bounds yy = self.convert_yunits( y ) scaley = (yy<ymin) or (yy>ymax) trans = mtransforms.blended_transform_factory( self.transAxes, self.transData) l = mlines.Line2D([xmin,xmax], [y,y], transform=trans, **kwargs) l.x_isdata = False self.add_line(l) self.autoscale_view(scalex=False, scaley=scaley) return l axhline.__doc__ = cbook.dedent(axhline.__doc__) % martist.kwdocd def axvline(self, x=0, ymin=0, ymax=1, **kwargs): """ call signature:: axvline(x=0, ymin=0, ymax=1, **kwargs) Axis Vertical Line Draw a vertical line at *x* from *ymin* to *ymax*. With the default values of *ymin* = 0 and *ymax* = 1, this line will always span the vertical extent of the axes, regardless of the xlim settings, even if you change them, eg. with the :meth:`set_xlim` command. That is, the vertical extent is in axes coords: 0=bottom, 0.5=middle, 1.0=top but the *x* location is in data coordinates. Return value is the :class:`~matplotlib.lines.Line2D` instance. kwargs are the same as kwargs to plot, and can be used to control the line properties. Eg., * draw a thick red vline at *x* = 0 that spans the yrange >>> axvline(linewidth=4, color='r') * draw a default vline at *x* = 1 that spans the yrange >>> axvline(x=1) * draw a default vline at *x* = .5 that spans the the middle half of the yrange >>> axvline(x=.5, ymin=0.25, ymax=0.75) Valid kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s .. seealso:: :meth:`axhspan`: for example plot and source code """ xmin, xmax = self.get_xbound() # We need to strip away the units for comparison with # non-unitized bounds xx = self.convert_xunits( x ) scalex = (xx<xmin) or (xx>xmax) trans = mtransforms.blended_transform_factory( self.transData, self.transAxes) l = mlines.Line2D([x,x], [ymin,ymax] , transform=trans, **kwargs) l.y_isdata = False self.add_line(l) self.autoscale_view(scalex=scalex, scaley=False) return l axvline.__doc__ = cbook.dedent(axvline.__doc__) % martist.kwdocd def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): """ call signature:: axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) Axis Horizontal Span. *y* coords are in data units and *x* coords are in axes (relative 0-1) units. Draw a horizontal span (rectangle) from *ymin* to *ymax*. With the default values of *xmin* = 0 and *xmax* = 1, this always spans the xrange, regardless of the xlim settings, even if you change them, eg. with the :meth:`set_xlim` command. That is, the horizontal extent is in axes coords: 0=left, 0.5=middle, 1.0=right but the *y* location is in data coordinates. Return value is a :class:`matplotlib.patches.Polygon` instance. Examples: * draw a gray rectangle from *y* = 0.25-0.75 that spans the horizontal extent of the axes >>> axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: %(Polygon)s **Example:** .. plot:: mpl_examples/pylab_examples/axhspan_demo.py """ trans = mtransforms.blended_transform_factory( self.transAxes, self.transData) # process the unit information self._process_unit_info( [xmin, xmax], [ymin, ymax], kwargs=kwargs ) # first we need to strip away the units xmin, xmax = self.convert_xunits( [xmin, xmax] ) ymin, ymax = self.convert_yunits( [ymin, ymax] ) verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin) p = mpatches.Polygon(verts, **kwargs) p.set_transform(trans) p.x_isdata = False self.add_patch(p) return p axhspan.__doc__ = cbook.dedent(axhspan.__doc__) % martist.kwdocd def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): """ call signature:: axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs) Axis Vertical Span. *x* coords are in data units and *y* coords are in axes (relative 0-1) units. Draw a vertical span (rectangle) from *xmin* to *xmax*. With the default values of *ymin* = 0 and *ymax* = 1, this always spans the yrange, regardless of the ylim settings, even if you change them, eg. with the :meth:`set_ylim` command. That is, the vertical extent is in axes coords: 0=bottom, 0.5=middle, 1.0=top but the *y* location is in data coordinates. Return value is the :class:`matplotlib.patches.Polygon` instance. Examples: * draw a vertical green translucent rectangle from x=1.25 to 1.55 that spans the yrange of the axes >>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5) Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: %(Polygon)s .. seealso:: :meth:`axhspan`: for example plot and source code """ trans = mtransforms.blended_transform_factory( self.transData, self.transAxes) # process the unit information self._process_unit_info( [xmin, xmax], [ymin, ymax], kwargs=kwargs ) # first we need to strip away the units xmin, xmax = self.convert_xunits( [xmin, xmax] ) ymin, ymax = self.convert_yunits( [ymin, ymax] ) verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)] p = mpatches.Polygon(verts, **kwargs) p.set_transform(trans) p.y_isdata = False self.add_patch(p) return p axvspan.__doc__ = cbook.dedent(axvspan.__doc__) % martist.kwdocd def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs): """ call signature:: hlines(y, xmin, xmax, colors='k', linestyles='solid', **kwargs) Plot horizontal lines at each *y* from *xmin* to *xmax*. Returns the :class:`~matplotlib.collections.LineCollection` that was added. Required arguments: *y*: a 1-D numpy array or iterable. *xmin* and *xmax*: can be scalars or ``len(x)`` numpy arrays. If they are scalars, then the respective values are constant, else the widths of the lines are determined by *xmin* and *xmax*. Optional keyword arguments: *colors*: a line collections color argument, either a single color or a ``len(y)`` list of colors *linestyles*: [ 'solid' | 'dashed' | 'dashdot' | 'dotted' ] **Example:** .. plot:: mpl_examples/pylab_examples/hline_demo.py """ if kwargs.get('fmt') is not None: raise DeprecationWarning('hlines now uses a ' 'collections.LineCollection and not a ' 'list of Line2D to draw; see API_CHANGES') # We do the conversion first since not all unitized data is uniform y = self.convert_yunits( y ) xmin = self.convert_xunits( xmin ) xmax = self.convert_xunits( xmax ) if not iterable(y): y = [y] if not iterable(xmin): xmin = [xmin] if not iterable(xmax): xmax = [xmax] y = np.asarray(y) xmin = np.asarray(xmin) xmax = np.asarray(xmax) if len(xmin)==1: xmin = np.resize( xmin, y.shape ) if len(xmax)==1: xmax = np.resize( xmax, y.shape ) if len(xmin)!=len(y): raise ValueError, 'xmin and y are unequal sized sequences' if len(xmax)!=len(y): raise ValueError, 'xmax and y are unequal sized sequences' verts = [ ((thisxmin, thisy), (thisxmax, thisy)) for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] coll = mcoll.LineCollection(verts, colors=colors, linestyles=linestyles, label=label) self.add_collection(coll) coll.update(kwargs) minx = min(xmin.min(), xmax.min()) maxx = max(xmin.max(), xmax.max()) miny = y.min() maxy = y.max() corners = (minx, miny), (maxx, maxy) self.update_datalim(corners) self.autoscale_view() return coll hlines.__doc__ = cbook.dedent(hlines.__doc__) def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', label='', **kwargs): """ call signature:: vlines(x, ymin, ymax, color='k', linestyles='solid') Plot vertical lines at each *x* from *ymin* to *ymax*. *ymin* or *ymax* can be scalars or len(*x*) numpy arrays. If they are scalars, then the respective values are constant, else the heights of the lines are determined by *ymin* and *ymax*. *colors* a line collections color args, either a single color or a len(*x*) list of colors *linestyles* one of [ 'solid' | 'dashed' | 'dashdot' | 'dotted' ] Returns the :class:`matplotlib.collections.LineCollection` that was added. kwargs are :class:`~matplotlib.collections.LineCollection` properties: %(LineCollection)s """ if kwargs.get('fmt') is not None: raise DeprecationWarning('vlines now uses a ' 'collections.LineCollection and not a ' 'list of Line2D to draw; see API_CHANGES') self._process_unit_info(xdata=x, ydata=ymin, kwargs=kwargs) # We do the conversion first since not all unitized data is uniform x = self.convert_xunits( x ) ymin = self.convert_yunits( ymin ) ymax = self.convert_yunits( ymax ) if not iterable(x): x = [x] if not iterable(ymin): ymin = [ymin] if not iterable(ymax): ymax = [ymax] x = np.asarray(x) ymin = np.asarray(ymin) ymax = np.asarray(ymax) if len(ymin)==1: ymin = np.resize( ymin, x.shape ) if len(ymax)==1: ymax = np.resize( ymax, x.shape ) if len(ymin)!=len(x): raise ValueError, 'ymin and x are unequal sized sequences' if len(ymax)!=len(x): raise ValueError, 'ymax and x are unequal sized sequences' Y = np.array([ymin, ymax]).T verts = [ ((thisx, thisymin), (thisx, thisymax)) for thisx, (thisymin, thisymax) in zip(x,Y)] #print 'creating line collection' coll = mcoll.LineCollection(verts, colors=colors, linestyles=linestyles, label=label) self.add_collection(coll) coll.update(kwargs) minx = min( x ) maxx = max( x ) miny = min( min(ymin), min(ymax) ) maxy = max( max(ymin), max(ymax) ) corners = (minx, miny), (maxx, maxy) self.update_datalim(corners) self.autoscale_view() return coll vlines.__doc__ = cbook.dedent(vlines.__doc__) % martist.kwdocd #### Basic plotting def plot(self, *args, **kwargs): """ Plot lines and/or markers to the :class:`~matplotlib.axes.Axes`. *args* is a variable length argument, allowing for multiple *x*, *y* pairs with an optional format string. For example, each of the following is legal:: plot(x, y) # plot x and y using default line style and color plot(x, y, 'bo') # plot x and y using blue circle markers plot(y) # plot y using x as index array 0..N-1 plot(y, 'r+') # ditto, but with red plusses If *x* and/or *y* is 2-dimensional, then the corresponding columns will be plotted. An arbitrary number of *x*, *y*, *fmt* groups can be specified, as in:: a.plot(x1, y1, 'g^', x2, y2, 'g-') Return value is a list of lines that were added. The following format string characters are accepted to control the line style or marker: ================ =============================== character description ================ =============================== '-' solid line style '--' dashed line style '-.' dash-dot line style ':' dotted line style '.' point marker ',' pixel marker 'o' circle marker 'v' triangle_down marker '^' triangle_up marker '<' triangle_left marker '>' triangle_right marker '1' tri_down marker '2' tri_up marker '3' tri_left marker '4' tri_right marker 's' square marker 'p' pentagon marker '*' star marker 'h' hexagon1 marker 'H' hexagon2 marker '+' plus marker 'x' x marker 'D' diamond marker 'd' thin_diamond marker '|' vline marker '_' hline marker ================ =============================== The following color abbreviations are supported: ========== ======== character color ========== ======== 'b' blue 'g' green 'r' red 'c' cyan 'm' magenta 'y' yellow 'k' black 'w' white ========== ======== In addition, you can specify colors in many weird and wonderful ways, including full names (``'green'``), hex strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or grayscale intensities as a string (``'0.8'``). Of these, the string specifications can be used in place of a ``fmt`` group, but the tuple forms can be used only as ``kwargs``. Line styles and colors are combined in a single format string, as in ``'bo'`` for blue circles. The *kwargs* can be used to set line properties (any property that has a ``set_*`` method). You can use this to set a line label (for auto legends), linewidth, anitialising, marker face color, etc. Here is an example:: plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2) plot([1,2,3], [1,4,9], 'rs', label='line 2') axis([0, 4, 0, 10]) legend() If you make multiple lines with one plot command, the kwargs apply to all those lines, e.g.:: plot(x1, y1, x2, y2, antialised=False) Neither line will be antialiased. You do not need to use format strings, which are just abbreviations. All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with:: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12). See :class:`~matplotlib.lines.Line2D` for details. The kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s kwargs *scalex* and *scaley*, if defined, are passed on to :meth:`~matplotlib.axes.Axes.autoscale_view` to determine whether the *x* and *y* axes are autoscaled; the default is *True*. """ scalex = kwargs.pop( 'scalex', True) scaley = kwargs.pop( 'scaley', True) if not self._hold: self.cla() lines = [] for line in self._get_lines(*args, **kwargs): self.add_line(line) lines.append(line) self.autoscale_view(scalex=scalex, scaley=scaley) return lines plot.__doc__ = cbook.dedent(plot.__doc__) % martist.kwdocd def plot_date(self, x, y, fmt='bo', tz=None, xdate=True, ydate=False, **kwargs): """ call signature:: plot_date(x, y, fmt='bo', tz=None, xdate=True, ydate=False, **kwargs) Similar to the :func:`~matplotlib.pyplot.plot` command, except the *x* or *y* (or both) data is considered to be dates, and the axis is labeled accordingly. *x* and/or *y* can be a sequence of dates represented as float days since 0001-01-01 UTC. Keyword arguments: *fmt*: string The plot format string. *tz*: [ None | timezone string ] The time zone to use in labeling dates. If *None*, defaults to rc value. *xdate*: [ True | False ] If *True*, the *x*-axis will be labeled with dates. *ydate*: [ False | True ] If *True*, the *y*-axis will be labeled with dates. Note if you are using custom date tickers and formatters, it may be necessary to set the formatters/locators after the call to :meth:`plot_date` since :meth:`plot_date` will set the default tick locator to :class:`matplotlib.ticker.AutoDateLocator` (if the tick locator is not already set to a :class:`matplotlib.ticker.DateLocator` instance) and the default tick formatter to :class:`matplotlib.ticker.AutoDateFormatter` (if the tick formatter is not already set to a :class:`matplotlib.ticker.DateFormatter` instance). Valid kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s .. seealso:: :mod:`~matplotlib.dates`: for helper functions :func:`~matplotlib.dates.date2num`, :func:`~matplotlib.dates.num2date` and :func:`~matplotlib.dates.drange`: for help on creating the required floating point dates. """ if not self._hold: self.cla() ret = self.plot(x, y, fmt, **kwargs) if xdate: self.xaxis_date(tz) if ydate: self.yaxis_date(tz) self.autoscale_view() return ret plot_date.__doc__ = cbook.dedent(plot_date.__doc__) % martist.kwdocd def loglog(self, *args, **kwargs): """ call signature:: loglog(*args, **kwargs) Make a plot with log scaling on the *x* and *y* axis. :func:`~matplotlib.pyplot.loglog` supports all the keyword arguments of :func:`~matplotlib.pyplot.plot` and :meth:`matplotlib.axes.Axes.set_xscale` / :meth:`matplotlib.axes.Axes.set_yscale`. Notable keyword arguments: *basex*/*basey*: scalar > 1 base of the *x*/*y* logarithm *subsx*/*subsy*: [ None | sequence ] the location of the minor *x*/*y* ticks; *None* defaults to autosubs, which depend on the number of decades in the plot; see :meth:`matplotlib.axes.Axes.set_xscale` / :meth:`matplotlib.axes.Axes.set_yscale` for details The remaining valid kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s **Example:** .. plot:: mpl_examples/pylab_examples/log_demo.py """ if not self._hold: self.cla() dx = {'basex': kwargs.pop('basex', 10), 'subsx': kwargs.pop('subsx', None), } dy = {'basey': kwargs.pop('basey', 10), 'subsy': kwargs.pop('subsy', None), } self.set_xscale('log', **dx) self.set_yscale('log', **dy) b = self._hold self._hold = True # we've already processed the hold l = self.plot(*args, **kwargs) self._hold = b # restore the hold return l loglog.__doc__ = cbook.dedent(loglog.__doc__) % martist.kwdocd def semilogx(self, *args, **kwargs): """ call signature:: semilogx(*args, **kwargs) Make a plot with log scaling on the *x* axis. :func:`semilogx` supports all the keyword arguments of :func:`~matplotlib.pyplot.plot` and :meth:`matplotlib.axes.Axes.set_xscale`. Notable keyword arguments: *basex*: scalar > 1 base of the *x* logarithm *subsx*: [ None | sequence ] The location of the minor xticks; *None* defaults to autosubs, which depend on the number of decades in the plot; see :meth:`~matplotlib.axes.Axes.set_xscale` for details. The remaining valid kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s .. seealso:: :meth:`loglog`: For example code and figure """ if not self._hold: self.cla() d = {'basex': kwargs.pop( 'basex', 10), 'subsx': kwargs.pop( 'subsx', None), } self.set_xscale('log', **d) b = self._hold self._hold = True # we've already processed the hold l = self.plot(*args, **kwargs) self._hold = b # restore the hold return l semilogx.__doc__ = cbook.dedent(semilogx.__doc__) % martist.kwdocd def semilogy(self, *args, **kwargs): """ call signature:: semilogy(*args, **kwargs) Make a plot with log scaling on the *y* axis. :func:`semilogy` supports all the keyword arguments of :func:`~matplotlib.pylab.plot` and :meth:`matplotlib.axes.Axes.set_yscale`. Notable keyword arguments: *basey*: scalar > 1 Base of the *y* logarithm *subsy*: [ None | sequence ] The location of the minor yticks; *None* defaults to autosubs, which depend on the number of decades in the plot; see :meth:`~matplotlib.axes.Axes.set_yscale` for details. The remaining valid kwargs are :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s .. seealso:: :meth:`loglog`: For example code and figure """ if not self._hold: self.cla() d = {'basey': kwargs.pop('basey', 10), 'subsy': kwargs.pop('subsy', None), } self.set_yscale('log', **d) b = self._hold self._hold = True # we've already processed the hold l = self.plot(*args, **kwargs) self._hold = b # restore the hold return l semilogy.__doc__ = cbook.dedent(semilogy.__doc__) % martist.kwdocd def acorr(self, x, **kwargs): """ call signature:: acorr(x, normed=False, detrend=mlab.detrend_none, usevlines=False, maxlags=None, **kwargs) Plot the autocorrelation of *x*. If *normed* = *True*, normalize the data by the autocorrelation at 0-th lag. *x* is detrended by the *detrend* callable (default no normalization). Data are plotted as ``plot(lags, c, **kwargs)`` Return value is a tuple (*lags*, *c*, *line*) where: - *lags* are a length 2*maxlags+1 lag vector - *c* is the 2*maxlags+1 auto correlation vector - *line* is a :class:`~matplotlib.lines.Line2D` instance returned by :meth:`plot` The default *linestyle* is None and the default *marker* is ``'o'``, though these can be overridden with keyword args. The cross correlation is performed with :func:`numpy.correlate` with *mode* = 2. If *usevlines* is *True*, :meth:`~matplotlib.axes.Axes.vlines` rather than :meth:`~matplotlib.axes.Axes.plot` is used to draw vertical lines from the origin to the acorr. Otherwise, the plot style is determined by the kwargs, which are :class:`~matplotlib.lines.Line2D` properties. *maxlags* is a positive integer detailing the number of lags to show. The default value of *None* will return all :math:`2 \mathrm{len}(x) - 1` lags. The return value is a tuple (*lags*, *c*, *linecol*, *b*) where - *linecol* is the :class:`~matplotlib.collections.LineCollection` - *b* is the *x*-axis. .. seealso:: :meth:`~matplotlib.axes.Axes.plot` or :meth:`~matplotlib.axes.Axes.vlines`: For documentation on valid kwargs. **Example:** :func:`~matplotlib.pyplot.xcorr` above, and :func:`~matplotlib.pyplot.acorr` below. **Example:** .. plot:: mpl_examples/pylab_examples/xcorr_demo.py """ return self.xcorr(x, x, **kwargs) acorr.__doc__ = cbook.dedent(acorr.__doc__) % martist.kwdocd def xcorr(self, x, y, normed=False, detrend=mlab.detrend_none, usevlines=False, maxlags=None, **kwargs): """ call signature:: xcorr(x, y, normed=False, detrend=mlab.detrend_none, usevlines=False, **kwargs): Plot the cross correlation between *x* and *y*. If *normed* = *True*, normalize the data by the cross correlation at 0-th lag. *x* and y are detrended by the *detrend* callable (default no normalization). *x* and *y* must be equal length. Data are plotted as ``plot(lags, c, **kwargs)`` Return value is a tuple (*lags*, *c*, *line*) where: - *lags* are a length ``2*maxlags+1`` lag vector - *c* is the ``2*maxlags+1`` auto correlation vector - *line* is a :class:`~matplotlib.lines.Line2D` instance returned by :func:`~matplotlib.pyplot.plot`. The default *linestyle* is *None* and the default *marker* is 'o', though these can be overridden with keyword args. The cross correlation is performed with :func:`numpy.correlate` with *mode* = 2. If *usevlines* is *True*: :func:`~matplotlib.pyplot.vlines` rather than :func:`~matplotlib.pyplot.plot` is used to draw vertical lines from the origin to the xcorr. Otherwise the plotstyle is determined by the kwargs, which are :class:`~matplotlib.lines.Line2D` properties. The return value is a tuple (*lags*, *c*, *linecol*, *b*) where *linecol* is the :class:`matplotlib.collections.LineCollection` instance and *b* is the *x*-axis. *maxlags* is a positive integer detailing the number of lags to show. The default value of *None* will return all ``(2*len(x)-1)`` lags. **Example:** :func:`~matplotlib.pyplot.xcorr` above, and :func:`~matplotlib.pyplot.acorr` below. **Example:** .. plot:: mpl_examples/pylab_examples/xcorr_demo.py """ Nx = len(x) if Nx!=len(y): raise ValueError('x and y must be equal length') x = detrend(np.asarray(x)) y = detrend(np.asarray(y)) c = np.correlate(x, y, mode=2) if normed: c/= np.sqrt(np.dot(x,x) * np.dot(y,y)) if maxlags is None: maxlags = Nx - 1 if maxlags >= Nx or maxlags < 1: raise ValueError('maglags must be None or strictly ' 'positive < %d'%Nx) lags = np.arange(-maxlags,maxlags+1) c = c[Nx-1-maxlags:Nx+maxlags] if usevlines: a = self.vlines(lags, [0], c, **kwargs) b = self.axhline(**kwargs) else: kwargs.setdefault('marker', 'o') kwargs.setdefault('linestyle', 'None') a, = self.plot(lags, c, **kwargs) b = None return lags, c, a, b xcorr.__doc__ = cbook.dedent(xcorr.__doc__) % martist.kwdocd def legend(self, *args, **kwargs): """ call signature:: legend(*args, **kwargs) Place a legend on the current axes at location *loc*. Labels are a sequence of strings and *loc* can be a string or an integer specifying the legend location. To make a legend with existing lines:: legend() :meth:`legend` by itself will try and build a legend using the label property of the lines/patches/collections. You can set the label of a line by doing:: plot(x, y, label='my data') or:: line.set_label('my data'). If label is set to '_nolegend_', the item will not be shown in legend. To automatically generate the legend from labels:: legend( ('label1', 'label2', 'label3') ) To make a legend for a list of lines and labels:: legend( (line1, line2, line3), ('label1', 'label2', 'label3') ) To make a legend at a given location, using a location argument:: legend( ('label1', 'label2', 'label3'), loc='upper left') or:: legend( (line1, line2, line3), ('label1', 'label2', 'label3'), loc=2) The location codes are =============== ============= Location String Location Code =============== ============= 'best' 0 'upper right' 1 'upper left' 2 'lower left' 3 'lower right' 4 'right' 5 'center left' 6 'center right' 7 'lower center' 8 'upper center' 9 'center' 10 =============== ============= If none of these are locations are suitable, loc can be a 2-tuple giving x,y in axes coords, ie:: loc = 0, 1 # left top loc = 0.5, 0.5 # center Keyword arguments: *isaxes*: [ True | False ] Indicates that this is an axes legend *numpoints*: integer The number of points in the legend line, default is 4 *prop*: [ None | FontProperties ] A :class:`matplotlib.font_manager.FontProperties` instance, or *None* to use rc settings. *pad*: [ None | scalar ] The fractional whitespace inside the legend border, between 0 and 1. If *None*, use rc settings. *markerscale*: [ None | scalar ] The relative size of legend markers vs. original. If *None*, use rc settings. *shadow*: [ None | False | True ] If *True*, draw a shadow behind legend. If *None*, use rc settings. *labelsep*: [ None | scalar ] The vertical space between the legend entries. If *None*, use rc settings. *handlelen*: [ None | scalar ] The length of the legend lines. If *None*, use rc settings. *handletextsep*: [ None | scalar ] The space between the legend line and legend text. If *None*, use rc settings. *axespad*: [ None | scalar ] The border between the axes and legend edge. If *None*, use rc settings. **Example:** .. plot:: mpl_examples/api/legend_demo.py """ def get_handles(): handles = self.lines[:] handles.extend(self.patches) handles.extend([c for c in self.collections if isinstance(c, mcoll.LineCollection)]) handles.extend([c for c in self.collections if isinstance(c, mcoll.RegularPolyCollection)]) return handles if len(args)==0: handles = [] labels = [] for handle in get_handles(): label = handle.get_label() if (label is not None and label != '' and not label.startswith('_')): handles.append(handle) labels.append(label) if len(handles) == 0: warnings.warn("No labeled objects found. " "Use label='...' kwarg on individual plots.") return None elif len(args)==1: # LABELS labels = args[0] handles = [h for h, label in zip(get_handles(), labels)] elif len(args)==2: if is_string_like(args[1]) or isinstance(args[1], int): # LABELS, LOC labels, loc = args handles = [h for h, label in zip(get_handles(), labels)] kwargs['loc'] = loc else: # LINES, LABELS handles, labels = args elif len(args)==3: # LINES, LABELS, LOC handles, labels, loc = args kwargs['loc'] = loc else: raise TypeError('Invalid arguments to legend') handles = cbook.flatten(handles) self.legend_ = mlegend.Legend(self, handles, labels, **kwargs) return self.legend_ #### Specialized plotting def step(self, x, y, *args, **kwargs): ''' call signature:: step(x, y, *args, **kwargs) Make a step plot. Additional keyword args to :func:`step` are the same as those for :func:`~matplotlib.pyplot.plot`. *x* and *y* must be 1-D sequences, and it is assumed, but not checked, that *x* is uniformly increasing. Keyword arguments: *where*: [ 'pre' | 'post' | 'mid' ] If 'pre', the interval from x[i] to x[i+1] has level y[i] If 'post', that interval has level y[i+1] If 'mid', the jumps in *y* occur half-way between the *x*-values. ''' where = kwargs.pop('where', 'pre') if where not in ('pre', 'post', 'mid'): raise ValueError("'where' argument to step must be " "'pre', 'post' or 'mid'") kwargs['linestyle'] = 'steps-' + where return self.plot(x, y, *args, **kwargs) def bar(self, left, height, width=0.8, bottom=None, color=None, edgecolor=None, linewidth=None, yerr=None, xerr=None, ecolor=None, capsize=3, align='edge', orientation='vertical', log=False, **kwargs ): """ call signature:: bar(left, height, width=0.8, bottom=0, color=None, edgecolor=None, linewidth=None, yerr=None, xerr=None, ecolor=None, capsize=3, align='edge', orientation='vertical', log=False) Make a bar plot with rectangles bounded by: *left*, *left* + *width*, *bottom*, *bottom* + *height* (left, right, bottom and top edges) *left*, *height*, *width*, and *bottom* can be either scalars or sequences Return value is a list of :class:`matplotlib.patches.Rectangle` instances. Required arguments: ======== =============================================== Argument Description ======== =============================================== *left* the x coordinates of the left sides of the bars *height* the heights of the bars ======== =============================================== Optional keyword arguments: =============== ========================================== Keyword Description =============== ========================================== *width* the widths of the bars *bottom* the y coordinates of the bottom edges of the bars *color* the colors of the bars *edgecolor* the colors of the bar edges *linewidth* width of bar edges; None means use default linewidth; 0 means don't draw edges. *xerr* if not None, will be used to generate errorbars on the bar chart *yerr* if not None, will be used to generate errorbars on the bar chart *ecolor* specifies the color of any errorbar *capsize* (default 3) determines the length in points of the error bar caps *align* 'edge' (default) | 'center' *orientation* 'vertical' | 'horizontal' *log* [False|True] False (default) leaves the orientation axis as-is; True sets it to log scale =============== ========================================== For vertical bars, *align* = 'edge' aligns bars by their left edges in left, while *align* = 'center' interprets these values as the *x* coordinates of the bar centers. For horizontal bars, *align* = 'edge' aligns bars by their bottom edges in bottom, while *align* = 'center' interprets these values as the *y* coordinates of the bar centers. The optional arguments *color*, *edgecolor*, *linewidth*, *xerr*, and *yerr* can be either scalars or sequences of length equal to the number of bars. This enables you to use bar as the basis for stacked bar charts, or candlestick plots. Other optional kwargs: %(Rectangle)s **Example:** A stacked bar chart. .. plot:: mpl_examples/pylab_examples/bar_stacked.py """ if not self._hold: self.cla() label = kwargs.pop('label', '') def make_iterable(x): if not iterable(x): return [x] else: return x # make them safe to take len() of _left = left left = make_iterable(left) height = make_iterable(height) width = make_iterable(width) _bottom = bottom bottom = make_iterable(bottom) linewidth = make_iterable(linewidth) adjust_ylim = False adjust_xlim = False if orientation == 'vertical': self._process_unit_info(xdata=left, ydata=height, kwargs=kwargs) if log: self.set_yscale('log') # size width and bottom according to length of left if _bottom is None: if self.get_yscale() == 'log': bottom = [1e-100] adjust_ylim = True else: bottom = [0] nbars = len(left) if len(width) == 1: width *= nbars if len(bottom) == 1: bottom *= nbars elif orientation == 'horizontal': self._process_unit_info(xdata=width, ydata=bottom, kwargs=kwargs) if log: self.set_xscale('log') # size left and height according to length of bottom if _left is None: if self.get_xscale() == 'log': left = [1e-100] adjust_xlim = True else: left = [0] nbars = len(bottom) if len(left) == 1: left *= nbars if len(height) == 1: height *= nbars else: raise ValueError, 'invalid orientation: %s' % orientation # do not convert to array here as unit info is lost #left = np.asarray(left) #height = np.asarray(height) #width = np.asarray(width) #bottom = np.asarray(bottom) if len(linewidth) < nbars: linewidth *= nbars if color is None: color = [None] * nbars else: color = list(mcolors.colorConverter.to_rgba_array(color)) if len(color) < nbars: color *= nbars if edgecolor is None: edgecolor = [None] * nbars else: edgecolor = list(mcolors.colorConverter.to_rgba_array(edgecolor)) if len(edgecolor) < nbars: edgecolor *= nbars if yerr is not None: if not iterable(yerr): yerr = [yerr]*nbars if xerr is not None: if not iterable(xerr): xerr = [xerr]*nbars # FIXME: convert the following to proper input validation # raising ValueError; don't use assert for this. assert len(left)==nbars, "argument 'left' must be %d or scalar" % nbars assert len(height)==nbars, ("argument 'height' must be %d or scalar" % nbars) assert len(width)==nbars, ("argument 'width' must be %d or scalar" % nbars) assert len(bottom)==nbars, ("argument 'bottom' must be %d or scalar" % nbars) if yerr is not None and len(yerr)!=nbars: raise ValueError( "bar() argument 'yerr' must be len(%s) or scalar" % nbars) if xerr is not None and len(xerr)!=nbars: raise ValueError( "bar() argument 'xerr' must be len(%s) or scalar" % nbars) patches = [] # lets do some conversions now since some types cannot be # subtracted uniformly if self.xaxis is not None: xconv = self.xaxis.converter if xconv is not None: units = self.xaxis.get_units() left = xconv.convert( left, units ) width = xconv.convert( width, units ) if self.yaxis is not None: yconv = self.yaxis.converter if yconv is not None : units = self.yaxis.get_units() bottom = yconv.convert( bottom, units ) height = yconv.convert( height, units ) if align == 'edge': pass elif align == 'center': if orientation == 'vertical': left = [left[i] - width[i]/2. for i in xrange(len(left))] elif orientation == 'horizontal': bottom = [bottom[i] - height[i]/2. for i in xrange(len(bottom))] else: raise ValueError, 'invalid alignment: %s' % align args = zip(left, bottom, width, height, color, edgecolor, linewidth) for l, b, w, h, c, e, lw in args: if h<0: b += h h = abs(h) if w<0: l += w w = abs(w) r = mpatches.Rectangle( xy=(l, b), width=w, height=h, facecolor=c, edgecolor=e, linewidth=lw, label=label ) label = '_nolegend_' r.update(kwargs) #print r.get_label(), label, 'label' in kwargs self.add_patch(r) patches.append(r) holdstate = self._hold self.hold(True) # ensure hold is on before plotting errorbars if xerr is not None or yerr is not None: if orientation == 'vertical': # using list comps rather than arrays to preserve unit info x = [l+0.5*w for l, w in zip(left, width)] y = [b+h for b,h in zip(bottom, height)] elif orientation == 'horizontal': # using list comps rather than arrays to preserve unit info x = [l+w for l,w in zip(left, width)] y = [b+0.5*h for b,h in zip(bottom, height)] self.errorbar( x, y, yerr=yerr, xerr=xerr, fmt=None, ecolor=ecolor, capsize=capsize) self.hold(holdstate) # restore previous hold state if adjust_xlim: xmin, xmax = self.dataLim.intervalx xmin = np.amin(width[width!=0]) # filter out the 0 width rects if xerr is not None: xmin = xmin - np.amax(xerr) xmin = max(xmin*0.9, 1e-100) self.dataLim.intervalx = (xmin, xmax) if adjust_ylim: ymin, ymax = self.dataLim.intervaly ymin = np.amin(height[height!=0]) # filter out the 0 height rects if yerr is not None: ymin = ymin - np.amax(yerr) ymin = max(ymin*0.9, 1e-100) self.dataLim.intervaly = (ymin, ymax) self.autoscale_view() return patches bar.__doc__ = cbook.dedent(bar.__doc__) % martist.kwdocd def barh(self, bottom, width, height=0.8, left=None, **kwargs): """ call signature:: barh(bottom, width, height=0.8, left=0, **kwargs) Make a horizontal bar plot with rectangles bounded by: *left*, *left* + *width*, *bottom*, *bottom* + *height* (left, right, bottom and top edges) *bottom*, *width*, *height*, and *left* can be either scalars or sequences Return value is a list of :class:`matplotlib.patches.Rectangle` instances. Required arguments: ======== ====================================================== Argument Description ======== ====================================================== *bottom* the vertical positions of the bottom edges of the bars *width* the lengths of the bars ======== ====================================================== Optional keyword arguments: =============== ========================================== Keyword Description =============== ========================================== *height* the heights (thicknesses) of the bars *left* the x coordinates of the left edges of the bars *color* the colors of the bars *edgecolor* the colors of the bar edges *linewidth* width of bar edges; None means use default linewidth; 0 means don't draw edges. *xerr* if not None, will be used to generate errorbars on the bar chart *yerr* if not None, will be used to generate errorbars on the bar chart *ecolor* specifies the color of any errorbar *capsize* (default 3) determines the length in points of the error bar caps *align* 'edge' (default) | 'center' *log* [False|True] False (default) leaves the horizontal axis as-is; True sets it to log scale =============== ========================================== Setting *align* = 'edge' aligns bars by their bottom edges in bottom, while *align* = 'center' interprets these values as the *y* coordinates of the bar centers. The optional arguments *color*, *edgecolor*, *linewidth*, *xerr*, and *yerr* can be either scalars or sequences of length equal to the number of bars. This enables you to use barh as the basis for stacked bar charts, or candlestick plots. other optional kwargs: %(Rectangle)s """ patches = self.bar(left=left, height=height, width=width, bottom=bottom, orientation='horizontal', **kwargs) return patches barh.__doc__ = cbook.dedent(barh.__doc__) % martist.kwdocd def broken_barh(self, xranges, yrange, **kwargs): """ call signature:: broken_barh(self, xranges, yrange, **kwargs) A collection of horizontal bars spanning *yrange* with a sequence of *xranges*. Required arguments: ========= ============================== Argument Description ========= ============================== *xranges* sequence of (*xmin*, *xwidth*) *yrange* sequence of (*ymin*, *ywidth*) ========= ============================== kwargs are :class:`matplotlib.collections.BrokenBarHCollection` properties: %(BrokenBarHCollection)s these can either be a single argument, ie:: facecolors = 'black' or a sequence of arguments for the various bars, ie:: facecolors = ('black', 'red', 'green') **Example:** .. plot:: mpl_examples/pylab_examples/broken_barh.py """ col = mcoll.BrokenBarHCollection(xranges, yrange, **kwargs) self.add_collection(col, autolim=True) self.autoscale_view() return col broken_barh.__doc__ = cbook.dedent(broken_barh.__doc__) % martist.kwdocd def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-'): """ call signature:: stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-') A stem plot plots vertical lines (using *linefmt*) at each *x* location from the baseline to *y*, and places a marker there using *markerfmt*. A horizontal line at 0 is is plotted using *basefmt*. Return value is a tuple (*markerline*, *stemlines*, *baseline*). .. seealso:: `this document`__ for details :file:`examples/pylab_examples/stem_plot.py`: for a demo __ http://www.mathworks.com/access/helpdesk/help/techdoc/ref/stem.html """ remember_hold=self._hold if not self._hold: self.cla() self.hold(True) markerline, = self.plot(x, y, markerfmt) stemlines = [] for thisx, thisy in zip(x, y): l, = self.plot([thisx,thisx], [0, thisy], linefmt) stemlines.append(l) baseline, = self.plot([np.amin(x), np.amax(x)], [0,0], basefmt) self.hold(remember_hold) return markerline, stemlines, baseline def pie(self, x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1): r""" call signature:: pie(x, explode=None, labels=None, colors=('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'), autopct=None, pctdistance=0.6, labeldistance=1.1, shadow=False) Make a pie chart of array *x*. The fractional area of each wedge is given by x/sum(x). If sum(x) <= 1, then the values of x give the fractional area directly and the array will not be normalized. Keyword arguments: *explode*: [ None | len(x) sequence ] If not *None*, is a len(*x*) array which specifies the fraction of the radius with which to offset each wedge. *colors*: [ None | color sequence ] A sequence of matplotlib color args through which the pie chart will cycle. *labels*: [ None | len(x) sequence of strings ] A sequence of strings providing the labels for each wedge *autopct*: [ None | format string | format function ] If not *None*, is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If it is a format string, the label will be ``fmt%pct``. If it is a function, it will be called. *pctdistance*: scalar The ratio between the center of each pie slice and the start of the text generated by *autopct*. Ignored if *autopct* is *None*; default is 0.6. *labeldistance*: scalar The radial distance at which the pie labels are drawn *shadow*: [ False | True ] Draw a shadow beneath the pie. The pie chart will probably look best if the figure and axes are square. Eg.:: figure(figsize=(8,8)) ax = axes([0.1, 0.1, 0.8, 0.8]) Return value: If *autopct* is None, return the tuple (*patches*, *texts*): - *patches* is a sequence of :class:`matplotlib.patches.Wedge` instances - *texts* is a list of the label :class:`matplotlib.text.Text` instances. If *autopct* is not *None*, return the tuple (*patches*, *texts*, *autotexts*), where *patches* and *texts* are as above, and *autotexts* is a list of :class:`~matplotlib.text.Text` instances for the numeric labels. """ self.set_frame_on(False) x = np.asarray(x).astype(np.float32) sx = float(x.sum()) if sx>1: x = np.divide(x,sx) if labels is None: labels = ['']*len(x) if explode is None: explode = [0]*len(x) assert(len(x)==len(labels)) assert(len(x)==len(explode)) if colors is None: colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w') center = 0,0 radius = 1 theta1 = 0 i = 0 texts = [] slices = [] autotexts = [] for frac, label, expl in cbook.safezip(x,labels, explode): x, y = center theta2 = theta1 + frac thetam = 2*math.pi*0.5*(theta1+theta2) x += expl*math.cos(thetam) y += expl*math.sin(thetam) w = mpatches.Wedge((x,y), radius, 360.*theta1, 360.*theta2, facecolor=colors[i%len(colors)]) slices.append(w) self.add_patch(w) w.set_label(label) if shadow: # make sure to add a shadow after the call to # add_patch so the figure and transform props will be # set shad = mpatches.Shadow(w, -0.02, -0.02, #props={'facecolor':w.get_facecolor()} ) shad.set_zorder(0.9*w.get_zorder()) self.add_patch(shad) xt = x + labeldistance*radius*math.cos(thetam) yt = y + labeldistance*radius*math.sin(thetam) label_alignment = xt > 0 and 'left' or 'right' t = self.text(xt, yt, label, size=rcParams['xtick.labelsize'], horizontalalignment=label_alignment, verticalalignment='center') texts.append(t) if autopct is not None: xt = x + pctdistance*radius*math.cos(thetam) yt = y + pctdistance*radius*math.sin(thetam) if is_string_like(autopct): s = autopct%(100.*frac) elif callable(autopct): s = autopct(100.*frac) else: raise TypeError( 'autopct must be callable or a format string') t = self.text(xt, yt, s, horizontalalignment='center', verticalalignment='center') autotexts.append(t) theta1 = theta2 i += 1 self.set_xlim((-1.25, 1.25)) self.set_ylim((-1.25, 1.25)) self.set_xticks([]) self.set_yticks([]) if autopct is None: return slices, texts else: return slices, texts, autotexts def errorbar(self, x, y, yerr=None, xerr=None, fmt='-', ecolor=None, elinewidth=None, capsize=3, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, **kwargs): """ call signature:: errorbar(x, y, yerr=None, xerr=None, fmt='-', ecolor=None, elinewidth=None, capsize=3, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False) Plot *x* versus *y* with error deltas in *yerr* and *xerr*. Vertical errorbars are plotted if *yerr* is not *None*. Horizontal errorbars are plotted if *xerr* is not *None*. *x*, *y*, *xerr*, and *yerr* can all be scalars, which plots a single error bar at *x*, *y*. Optional keyword arguments: *xerr*/*yerr*: [ scalar | N, Nx1, Nx2 array-like ] If a scalar number, len(N) array-like object, or an Nx1 array-like object, errorbars are drawn +/- value. If a rank-1, Nx2 Numpy array, errorbars are drawn at -column1 and +column2 *fmt*: '-' The plot format symbol for *y*. If *fmt* is *None*, just plot the errorbars with no line symbols. This can be useful for creating a bar plot with errorbars. *ecolor*: [ None | mpl color ] a matplotlib color arg which gives the color the errorbar lines; if *None*, use the marker color. *elinewidth*: scalar the linewidth of the errorbar lines. If *None*, use the linewidth. *capsize*: scalar the size of the error bar caps in points *barsabove*: [ True | False ] if *True*, will plot the errorbars above the plot symbols. Default is below. *lolims*/*uplims*/*xlolims*/*xuplims*: [ False | True ] These arguments can be used to indicate that a value gives only upper/lower limits. In that case a caret symbol is used to indicate this. lims-arguments may be of the same type as *xerr* and *yerr*. All other keyword arguments are passed on to the plot command for the markers, so you can add additional key=value pairs to control the errorbar markers. For example, this code makes big red squares with thick green edges:: x,y,yerr = rand(3,10) errorbar(x, y, yerr, marker='s', mfc='red', mec='green', ms=20, mew=4) where *mfc*, *mec*, *ms* and *mew* are aliases for the longer property names, *markerfacecolor*, *markeredgecolor*, *markersize* and *markeredgewith*. valid kwargs for the marker properties are %(Line2D)s Return value is a length 3 tuple. The first element is the :class:`~matplotlib.lines.Line2D` instance for the *y* symbol lines. The second element is a list of error bar cap lines, the third element is a list of :class:`~matplotlib.collections.LineCollection` instances for the horizontal and vertical error ranges. **Example:** .. plot:: mpl_examples/pylab_examples/errorbar_demo.py """ self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs) if not self._hold: self.cla() # make sure all the args are iterable; use lists not arrays to # preserve units if not iterable(x): x = [x] if not iterable(y): y = [y] if xerr is not None: if not iterable(xerr): xerr = [xerr]*len(x) if yerr is not None: if not iterable(yerr): yerr = [yerr]*len(y) l0 = None if barsabove and fmt is not None: l0, = self.plot(x,y,fmt,**kwargs) barcols = [] caplines = [] lines_kw = {'label':'_nolegend_'} if elinewidth: lines_kw['linewidth'] = elinewidth else: if 'linewidth' in kwargs: lines_kw['linewidth']=kwargs['linewidth'] if 'lw' in kwargs: lines_kw['lw']=kwargs['lw'] if 'transform' in kwargs: lines_kw['transform'] = kwargs['transform'] # arrays fine here, they are booleans and hence not units if not iterable(lolims): lolims = np.asarray([lolims]*len(x), bool) else: lolims = np.asarray(lolims, bool) if not iterable(uplims): uplims = np.array([uplims]*len(x), bool) else: uplims = np.asarray(uplims, bool) if not iterable(xlolims): xlolims = np.array([xlolims]*len(x), bool) else: xlolims = np.asarray(xlolims, bool) if not iterable(xuplims): xuplims = np.array([xuplims]*len(x), bool) else: xuplims = np.asarray(xuplims, bool) def xywhere(xs, ys, mask): """ return xs[mask], ys[mask] where mask is True but xs and ys are not arrays """ assert len(xs)==len(ys) assert len(xs)==len(mask) xs = [thisx for thisx, b in zip(xs, mask) if b] ys = [thisy for thisy, b in zip(ys, mask) if b] return xs, ys if capsize > 0: plot_kw = { 'ms':2*capsize, 'label':'_nolegend_'} if 'markeredgewidth' in kwargs: plot_kw['markeredgewidth']=kwargs['markeredgewidth'] if 'mew' in kwargs: plot_kw['mew']=kwargs['mew'] if 'transform' in kwargs: plot_kw['transform'] = kwargs['transform'] if xerr is not None: if (iterable(xerr) and len(xerr)==2 and iterable(xerr[0]) and iterable(xerr[1])): # using list comps rather than arrays to preserve units left = [thisx-thiserr for (thisx, thiserr) in cbook.safezip(x,xerr[0])] right = [thisx+thiserr for (thisx, thiserr) in cbook.safezip(x,xerr[1])] else: # using list comps rather than arrays to preserve units left = [thisx-thiserr for (thisx, thiserr) in cbook.safezip(x,xerr)] right = [thisx+thiserr for (thisx, thiserr) in cbook.safezip(x,xerr)] barcols.append( self.hlines(y, left, right, **lines_kw ) ) if capsize > 0: if xlolims.any(): # can't use numpy logical indexing since left and # y are lists leftlo, ylo = xywhere(left, y, xlolims) caplines.extend( self.plot(leftlo, ylo, ls='None', marker=mlines.CARETLEFT, **plot_kw) ) xlolims = ~xlolims leftlo, ylo = xywhere(left, y, xlolims) caplines.extend( self.plot(leftlo, ylo, 'k|', **plot_kw) ) else: caplines.extend( self.plot(left, y, 'k|', **plot_kw) ) if xuplims.any(): rightup, yup = xywhere(right, y, xuplims) caplines.extend( self.plot(rightup, yup, ls='None', marker=mlines.CARETRIGHT, **plot_kw) ) xuplims = ~xuplims rightup, yup = xywhere(right, y, xuplims) caplines.extend( self.plot(rightup, yup, 'k|', **plot_kw) ) else: caplines.extend( self.plot(right, y, 'k|', **plot_kw) ) if yerr is not None: if (iterable(yerr) and len(yerr)==2 and iterable(yerr[0]) and iterable(yerr[1])): # using list comps rather than arrays to preserve units lower = [thisy-thiserr for (thisy, thiserr) in cbook.safezip(y,yerr[0])] upper = [thisy+thiserr for (thisy, thiserr) in cbook.safezip(y,yerr[1])] else: # using list comps rather than arrays to preserve units lower = [thisy-thiserr for (thisy, thiserr) in cbook.safezip(y,yerr)] upper = [thisy+thiserr for (thisy, thiserr) in cbook.safezip(y,yerr)] barcols.append( self.vlines(x, lower, upper, **lines_kw) ) if capsize > 0: if lolims.any(): xlo, lowerlo = xywhere(x, lower, lolims) caplines.extend( self.plot(xlo, lowerlo, ls='None', marker=mlines.CARETDOWN, **plot_kw) ) lolims = ~lolims xlo, lowerlo = xywhere(x, lower, lolims) caplines.extend( self.plot(xlo, lowerlo, 'k_', **plot_kw) ) else: caplines.extend( self.plot(x, lower, 'k_', **plot_kw) ) if uplims.any(): xup, upperup = xywhere(x, upper, uplims) caplines.extend( self.plot(xup, upperup, ls='None', marker=mlines.CARETUP, **plot_kw) ) uplims = ~uplims xup, upperup = xywhere(x, upper, uplims) caplines.extend( self.plot(xup, upperup, 'k_', **plot_kw) ) else: caplines.extend( self.plot(x, upper, 'k_', **plot_kw) ) if not barsabove and fmt is not None: l0, = self.plot(x,y,fmt,**kwargs) if ecolor is None: if l0 is None: ecolor = self._get_lines._get_next_cycle_color() else: ecolor = l0.get_color() for l in barcols: l.set_color(ecolor) for l in caplines: l.set_color(ecolor) self.autoscale_view() return (l0, caplines, barcols) errorbar.__doc__ = cbook.dedent(errorbar.__doc__) % martist.kwdocd def boxplot(self, x, notch=0, sym='b+', vert=1, whis=1.5, positions=None, widths=None): """ call signature:: boxplot(x, notch=0, sym='+', vert=1, whis=1.5, positions=None, widths=None) Make a box and whisker plot for each column of *x* or each vector in sequence *x*. The box extends from the lower to upper quartile values of the data, with a line at the median. The whiskers extend from the box to show the range of the data. Flier points are those past the end of the whiskers. - *notch* = 0 (default) produces a rectangular box plot. - *notch* = 1 will produce a notched box plot *sym* (default 'b+') is the default symbol for flier points. Enter an empty string ('') if you don't want to show fliers. - *vert* = 1 (default) makes the boxes vertical. - *vert* = 0 makes horizontal boxes. This seems goofy, but that's how Matlab did it. *whis* (default 1.5) defines the length of the whiskers as a function of the inner quartile range. They extend to the most extreme data point within ( ``whis*(75%-25%)`` ) data range. *positions* (default 1,2,...,n) sets the horizontal positions of the boxes. The ticks and limits are automatically set to match the positions. *widths* is either a scalar or a vector and sets the width of each box. The default is 0.5, or ``0.15*(distance between extreme positions)`` if that is smaller. *x* is an array or a sequence of vectors. Returns a dictionary mapping each component of the boxplot to a list of the :class:`matplotlib.lines.Line2D` instances created. **Example:** .. plot:: pyplots/boxplot_demo.py """ if not self._hold: self.cla() holdStatus = self._hold whiskers, caps, boxes, medians, fliers = [], [], [], [], [] # convert x to a list of vectors if hasattr(x, 'shape'): if len(x.shape) == 1: if hasattr(x[0], 'shape'): x = list(x) else: x = [x,] elif len(x.shape) == 2: nr, nc = x.shape if nr == 1: x = [x] elif nc == 1: x = [x.ravel()] else: x = [x[:,i] for i in xrange(nc)] else: raise ValueError, "input x can have no more than 2 dimensions" if not hasattr(x[0], '__len__'): x = [x] col = len(x) # get some plot info if positions is None: positions = range(1, col + 1) if widths is None: distance = max(positions) - min(positions) widths = min(0.15*max(distance,1.0), 0.5) if isinstance(widths, float) or isinstance(widths, int): widths = np.ones((col,), float) * widths # loop through columns, adding each to plot self.hold(True) for i,pos in enumerate(positions): d = np.ravel(x[i]) row = len(d) # get median and quartiles q1, med, q3 = mlab.prctile(d,[25,50,75]) # get high extreme iq = q3 - q1 hi_val = q3 + whis*iq wisk_hi = np.compress( d <= hi_val , d ) if len(wisk_hi) == 0: wisk_hi = q3 else: wisk_hi = max(wisk_hi) # get low extreme lo_val = q1 - whis*iq wisk_lo = np.compress( d >= lo_val, d ) if len(wisk_lo) == 0: wisk_lo = q1 else: wisk_lo = min(wisk_lo) # get fliers - if we are showing them flier_hi = [] flier_lo = [] flier_hi_x = [] flier_lo_x = [] if len(sym) != 0: flier_hi = np.compress( d > wisk_hi, d ) flier_lo = np.compress( d < wisk_lo, d ) flier_hi_x = np.ones(flier_hi.shape[0]) * pos flier_lo_x = np.ones(flier_lo.shape[0]) * pos # get x locations for fliers, whisker, whisker cap and box sides box_x_min = pos - widths[i] * 0.5 box_x_max = pos + widths[i] * 0.5 wisk_x = np.ones(2) * pos cap_x_min = pos - widths[i] * 0.25 cap_x_max = pos + widths[i] * 0.25 cap_x = [cap_x_min, cap_x_max] # get y location for median med_y = [med, med] # calculate 'regular' plot if notch == 0: # make our box vectors box_x = [box_x_min, box_x_max, box_x_max, box_x_min, box_x_min ] box_y = [q1, q1, q3, q3, q1 ] # make our median line vectors med_x = [box_x_min, box_x_max] # calculate 'notch' plot else: notch_max = med + 1.57*iq/np.sqrt(row) notch_min = med - 1.57*iq/np.sqrt(row) if notch_max > q3: notch_max = q3 if notch_min < q1: notch_min = q1 # make our notched box vectors box_x = [box_x_min, box_x_max, box_x_max, cap_x_max, box_x_max, box_x_max, box_x_min, box_x_min, cap_x_min, box_x_min, box_x_min ] box_y = [q1, q1, notch_min, med, notch_max, q3, q3, notch_max, med, notch_min, q1] # make our median line vectors med_x = [cap_x_min, cap_x_max] med_y = [med, med] # vertical or horizontal plot? if vert: def doplot(*args): return self.plot(*args) else: def doplot(*args): shuffled = [] for i in xrange(0, len(args), 3): shuffled.extend([args[i+1], args[i], args[i+2]]) return self.plot(*shuffled) whiskers.extend(doplot(wisk_x, [q1, wisk_lo], 'b--', wisk_x, [q3, wisk_hi], 'b--')) caps.extend(doplot(cap_x, [wisk_hi, wisk_hi], 'k-', cap_x, [wisk_lo, wisk_lo], 'k-')) boxes.extend(doplot(box_x, box_y, 'b-')) medians.extend(doplot(med_x, med_y, 'r-')) fliers.extend(doplot(flier_hi_x, flier_hi, sym, flier_lo_x, flier_lo, sym)) # fix our axes/ticks up a little if 1 == vert: setticks, setlim = self.set_xticks, self.set_xlim else: setticks, setlim = self.set_yticks, self.set_ylim newlimits = min(positions)-0.5, max(positions)+0.5 setlim(newlimits) setticks(positions) # reset hold status self.hold(holdStatus) return dict(whiskers=whiskers, caps=caps, boxes=boxes, medians=medians, fliers=fliers) def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=1.0, linewidths=None, faceted=True, verts=None, **kwargs): """ call signatures:: scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=1.0, linewidths=None, verts=None, **kwargs) Make a scatter plot of *x* versus *y*, where *x*, *y* are 1-D sequences of the same length, *N*. Keyword arguments: *s*: size in points^2. It is a scalar or an array of the same length as *x* and *y*. *c*: a color. *c* can be a single color format string, or a sequence of color specifications of length *N*, or a sequence of *N* numbers to be mapped to colors using the *cmap* and *norm* specified via kwargs (see below). Note that *c* should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. *c* can be a 2-D array in which the rows are RGB or RGBA, however. *marker*: can be one of: ===== ============== Value Description ===== ============== 's' square 'o' circle '^' triangle up '>' triangle right 'v' triangle down '<' triangle left 'd' diamond 'p' pentagram 'h' hexagon '8' octagon '+' plus 'x' cross ===== ============== The marker can also be a tuple (*numsides*, *style*, *angle*), which will create a custom, regular symbol. *numsides*: the number of sides *style*: the style of the regular symbol: ===== ============================================= Value Description ===== ============================================= 0 a regular polygon 1 a star-like symbol 2 an asterisk 3 a circle (*numsides* and *angle* is ignored) ===== ============================================= *angle*: the angle of rotation of the symbol Finally, *marker* can be (*verts*, 0): *verts* is a sequence of (*x*, *y*) vertices for a custom scatter symbol. Alternatively, use the kwarg combination *marker* = *None*, *verts* = *verts*. Any or all of *x*, *y*, *s*, and *c* may be masked arrays, in which case all masks will be combined and only unmasked points will be plotted. Other keyword arguments: the color mapping and normalization arguments will be used only if *c* is an array of floats. *cmap*: [ None | Colormap ] A :class:`matplotlib.colors.Colormap` instance. If *None*, defaults to rc ``image.cmap``. *cmap* is only used if *c* is an array of floats. *norm*: [ None | Normalize ] A :class:`matplotlib.colors.Normalize` instance is used to scale luminance data to 0, 1. If *None*, use the default :func:`normalize`. *norm* is only used if *c* is an array of floats. *vmin*/*vmax*: *vmin* and *vmax* are used in conjunction with norm to normalize luminance data. If either are None, the min and max of the color array *C* is used. Note if you pass a *norm* instance, your settings for *vmin* and *vmax* will be ignored. *alpha*: 0 <= scalar <= 1 The alpha value for the patches *linewidths*: [ None | scalar | sequence ] If *None*, defaults to (lines.linewidth,). Note that this is a tuple, and if you set the linewidths argument you must set it as a sequence of floats, as required by :class:`~matplotlib.collections.RegularPolyCollection`. Optional kwargs control the :class:`~matplotlib.collections.Collection` properties; in particular: *edgecolors*: 'none' to plot faces with no outlines *facecolors*: 'none' to plot unfilled outlines Here are the standard descriptions of all the :class:`~matplotlib.collections.Collection` kwargs: %(Collection)s A :class:`~matplotlib.collections.Collection` instance is returned. """ if not self._hold: self.cla() syms = { # a dict from symbol to (numsides, angle) 's' : (4,math.pi/4.0,0), # square 'o' : (20,3,0), # circle '^' : (3,0,0), # triangle up '>' : (3,math.pi/2.0,0), # triangle right 'v' : (3,math.pi,0), # triangle down '<' : (3,3*math.pi/2.0,0), # triangle left 'd' : (4,0,0), # diamond 'p' : (5,0,0), # pentagram 'h' : (6,0,0), # hexagon '8' : (8,0,0), # octagon '+' : (4,0,2), # plus 'x' : (4,math.pi/4.0,2) # cross } self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs) x, y, s, c = cbook.delete_masked_points(x, y, s, c) if is_string_like(c) or cbook.is_sequence_of_strings(c): colors = mcolors.colorConverter.to_rgba_array(c, alpha) else: sh = np.shape(c) # The inherent ambiguity is resolved in favor of color # mapping, not interpretation as rgb or rgba: if len(sh) == 1 and sh[0] == len(x): colors = None # use cmap, norm after collection is created else: colors = mcolors.colorConverter.to_rgba_array(c, alpha) if not iterable(s): scales = (s,) else: scales = s if faceted: edgecolors = None else: edgecolors = 'none' warnings.warn( '''replace "faceted=False" with "edgecolors='none'"''', DeprecationWarning) #2008/04/18 sym = None symstyle = 0 # to be API compatible if marker is None and not (verts is None): marker = (verts, 0) verts = None if is_string_like(marker): # the standard way to define symbols using a string character sym = syms.get(marker) if sym is None and verts is None: raise ValueError('Unknown marker symbol to scatter') numsides, rotation, symstyle = syms[marker] elif iterable(marker): # accept marker to be: # (numsides, style, [angle]) # or # (verts[], style, [angle]) if len(marker)<2 or len(marker)>3: raise ValueError('Cannot create markersymbol from marker') if cbook.is_numlike(marker[0]): # (numsides, style, [angle]) if len(marker)==2: numsides, rotation = marker[0], 0. elif len(marker)==3: numsides, rotation = marker[0], marker[2] sym = True if marker[1] in (1,2): symstyle = marker[1] else: verts = np.asarray(marker[0]) if sym is not None: if symstyle==0: collection = mcoll.RegularPolyCollection( numsides, rotation, scales, facecolors = colors, edgecolors = edgecolors, linewidths = linewidths, offsets = zip(x,y), transOffset = self.transData, ) elif symstyle==1: collection = mcoll.StarPolygonCollection( numsides, rotation, scales, facecolors = colors, edgecolors = edgecolors, linewidths = linewidths, offsets = zip(x,y), transOffset = self.transData, ) elif symstyle==2: collection = mcoll.AsteriskPolygonCollection( numsides, rotation, scales, facecolors = colors, edgecolors = edgecolors, linewidths = linewidths, offsets = zip(x,y), transOffset = self.transData, ) elif symstyle==3: collection = mcoll.CircleCollection( scales, facecolors = colors, edgecolors = edgecolors, linewidths = linewidths, offsets = zip(x,y), transOffset = self.transData, ) else: rescale = np.sqrt(max(verts[:,0]**2+verts[:,1]**2)) verts /= rescale collection = mcoll.PolyCollection( (verts,), scales, facecolors = colors, edgecolors = edgecolors, linewidths = linewidths, offsets = zip(x,y), transOffset = self.transData, ) collection.set_transform(mtransforms.IdentityTransform()) collection.set_alpha(alpha) collection.update(kwargs) if colors is None: if norm is not None: assert(isinstance(norm, mcolors.Normalize)) if cmap is not None: assert(isinstance(cmap, mcolors.Colormap)) collection.set_array(np.asarray(c)) collection.set_cmap(cmap) collection.set_norm(norm) if vmin is not None or vmax is not None: collection.set_clim(vmin, vmax) else: collection.autoscale_None() temp_x = x temp_y = y minx = np.amin(temp_x) maxx = np.amax(temp_x) miny = np.amin(temp_y) maxy = np.amax(temp_y) w = maxx-minx h = maxy-miny # the pad is a little hack to deal with the fact that we don't # want to transform all the symbols whose scales are in points # to data coords to get the exact bounding box for efficiency # reasons. It can be done right if this is deemed important padx, pady = 0.05*w, 0.05*h corners = (minx-padx, miny-pady), (maxx+padx, maxy+pady) self.update_datalim( corners) self.autoscale_view() # add the collection last self.add_collection(collection) return collection scatter.__doc__ = cbook.dedent(scatter.__doc__) % martist.kwdocd def hexbin(self, x, y, C = None, gridsize = 100, bins = None, xscale = 'linear', yscale = 'linear', cmap=None, norm=None, vmin=None, vmax=None, alpha=1.0, linewidths=None, edgecolors='none', reduce_C_function = np.mean, **kwargs): """ call signature:: hexbin(x, y, C = None, gridsize = 100, bins = None, xscale = 'linear', yscale = 'linear', cmap=None, norm=None, vmin=None, vmax=None, alpha=1.0, linewidths=None, edgecolors='none' reduce_C_function = np.mean, **kwargs) Make a hexagonal binning plot of *x* versus *y*, where *x*, *y* are 1-D sequences of the same length, *N*. If *C* is None (the default), this is a histogram of the number of occurences of the observations at (x[i],y[i]). If *C* is specified, it specifies values at the coordinate (x[i],y[i]). These values are accumulated for each hexagonal bin and then reduced according to *reduce_C_function*, which defaults to numpy's mean function (np.mean). (If *C* is specified, it must also be a 1-D sequence of the same length as *x* and *y*.) *x*, *y* and/or *C* may be masked arrays, in which case only unmasked points will be plotted. Optional keyword arguments: *gridsize*: [ 100 | integer ] The number of hexagons in the *x*-direction, default is 100. The corresponding number of hexagons in the *y*-direction is chosen such that the hexagons are approximately regular. Alternatively, gridsize can be a tuple with two elements specifying the number of hexagons in the *x*-direction and the *y*-direction. *bins*: [ None | 'log' | integer | sequence ] If *None*, no binning is applied; the color of each hexagon directly corresponds to its count value. If 'log', use a logarithmic scale for the color map. Internally, :math:`log_{10}(i+1)` is used to determine the hexagon color. If an integer, divide the counts in the specified number of bins, and color the hexagons accordingly. If a sequence of values, the values of the lower bound of the bins to be used. *xscale*: [ 'linear' | 'log' ] Use a linear or log10 scale on the horizontal axis. *scale*: [ 'linear' | 'log' ] Use a linear or log10 scale on the vertical axis. Other keyword arguments controlling color mapping and normalization arguments: *cmap*: [ None | Colormap ] a :class:`matplotlib.cm.Colormap` instance. If *None*, defaults to rc ``image.cmap``. *norm*: [ None | Normalize ] :class:`matplotlib.colors.Normalize` instance is used to scale luminance data to 0,1. *vmin*/*vmax*: scalar *vmin* and *vmax* are used in conjunction with *norm* to normalize luminance data. If either are *None*, the min and max of the color array *C* is used. Note if you pass a norm instance, your settings for *vmin* and *vmax* will be ignored. *alpha*: scalar the alpha value for the patches *linewidths*: [ None | scalar ] If *None*, defaults to rc lines.linewidth. Note that this is a tuple, and if you set the linewidths argument you must set it as a sequence of floats, as required by :class:`~matplotlib.collections.RegularPolyCollection`. Other keyword arguments controlling the Collection properties: *edgecolors*: [ None | mpl color | color sequence ] If 'none', draws the edges in the same color as the fill color. This is the default, as it avoids unsightly unpainted pixels between the hexagons. If *None*, draws the outlines in the default color. If a matplotlib color arg or sequence of rgba tuples, draws the outlines in the specified color. Here are the standard descriptions of all the :class:`~matplotlib.collections.Collection` kwargs: %(Collection)s The return value is a :class:`~matplotlib.collections.PolyCollection` instance; use :meth:`~matplotlib.collection.PolyCollection.get_array` on this :class:`~matplotlib.collections.PolyCollection` to get the counts in each hexagon. **Example:** .. plot:: mpl_examples/pylab_examples/hexbin_demo.py """ if not self._hold: self.cla() self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs) x, y, C = cbook.delete_masked_points(x, y, C) # Set the size of the hexagon grid if iterable(gridsize): nx, ny = gridsize else: nx = gridsize ny = int(nx/math.sqrt(3)) # Count the number of data in each hexagon x = np.array(x, float) y = np.array(y, float) if xscale=='log': x = np.log10(x) if yscale=='log': y = np.log10(y) xmin = np.amin(x) xmax = np.amax(x) ymin = np.amin(y) ymax = np.amax(y) # In the x-direction, the hexagons exactly cover the region from # xmin to xmax. Need some padding to avoid roundoff errors. padding = 1.e-9 * (xmax - xmin) xmin -= padding xmax += padding sx = (xmax-xmin) / nx sy = (ymax-ymin) / ny x = (x-xmin)/sx y = (y-ymin)/sy ix1 = np.round(x).astype(int) iy1 = np.round(y).astype(int) ix2 = np.floor(x).astype(int) iy2 = np.floor(y).astype(int) nx1 = nx + 1 ny1 = ny + 1 nx2 = nx ny2 = ny n = nx1*ny1+nx2*ny2 d1 = (x-ix1)**2 + 3.0 * (y-iy1)**2 d2 = (x-ix2-0.5)**2 + 3.0 * (y-iy2-0.5)**2 bdist = (d1<d2) if C is None: accum = np.zeros(n) # Create appropriate views into "accum" array. lattice1 = accum[:nx1*ny1] lattice2 = accum[nx1*ny1:] lattice1.shape = (nx1,ny1) lattice2.shape = (nx2,ny2) for i in xrange(len(x)): if bdist[i]: lattice1[ix1[i], iy1[i]]+=1 else: lattice2[ix2[i], iy2[i]]+=1 else: # create accumulation arrays lattice1 = np.empty((nx1,ny1),dtype=object) for i in xrange(nx1): for j in xrange(ny1): lattice1[i,j] = [] lattice2 = np.empty((nx2,ny2),dtype=object) for i in xrange(nx2): for j in xrange(ny2): lattice2[i,j] = [] for i in xrange(len(x)): if bdist[i]: lattice1[ix1[i], iy1[i]].append( C[i] ) else: lattice2[ix2[i], iy2[i]].append( C[i] ) for i in xrange(nx1): for j in xrange(ny1): vals = lattice1[i,j] if len(vals): lattice1[i,j] = reduce_C_function( vals ) else: lattice1[i,j] = np.nan for i in xrange(nx2): for j in xrange(ny2): vals = lattice2[i,j] if len(vals): lattice2[i,j] = reduce_C_function( vals ) else: lattice2[i,j] = np.nan accum = np.hstack(( lattice1.astype(float).ravel(), lattice2.astype(float).ravel())) good_idxs = ~np.isnan(accum) px = xmin + sx * np.array([ 0.5, 0.5, 0.0, -0.5, -0.5, 0.0]) py = ymin + sy * np.array([-0.5, 0.5, 1.0, 0.5, -0.5, -1.0]) / 3.0 polygons = np.zeros((6, n, 2), float) polygons[:,:nx1*ny1,0] = np.repeat(np.arange(nx1), ny1) polygons[:,:nx1*ny1,1] = np.tile(np.arange(ny1), nx1) polygons[:,nx1*ny1:,0] = np.repeat(np.arange(nx2) + 0.5, ny2) polygons[:,nx1*ny1:,1] = np.tile(np.arange(ny2), nx2) + 0.5 if C is not None: # remove accumulation bins with no data polygons = polygons[:,good_idxs,:] accum = accum[good_idxs] polygons = np.transpose(polygons, axes=[1,0,2]) polygons[:,:,0] *= sx polygons[:,:,1] *= sy polygons[:,:,0] += px polygons[:,:,1] += py if xscale=='log': polygons[:,:,0] = 10**(polygons[:,:,0]) xmin = 10**xmin xmax = 10**xmax self.set_xscale('log') if yscale=='log': polygons[:,:,1] = 10**(polygons[:,:,1]) ymin = 10**ymin ymax = 10**ymax self.set_yscale('log') if edgecolors=='none': edgecolors = 'face' collection = mcoll.PolyCollection( polygons, edgecolors = edgecolors, linewidths = linewidths, transOffset = self.transData, ) # Transform accum if needed if bins=='log': accum = np.log10(accum+1) elif bins!=None: if not iterable(bins): minimum, maximum = min(accum), max(accum) bins-=1 # one less edge than bins bins = minimum + (maximum-minimum)*np.arange(bins)/bins bins = np.sort(bins) accum = bins.searchsorted(accum) if norm is not None: assert(isinstance(norm, mcolors.Normalize)) if cmap is not None: assert(isinstance(cmap, mcolors.Colormap)) collection.set_array(accum) collection.set_cmap(cmap) collection.set_norm(norm) collection.set_alpha(alpha) collection.update(kwargs) if vmin is not None or vmax is not None: collection.set_clim(vmin, vmax) else: collection.autoscale_None() corners = ((xmin, ymin), (xmax, ymax)) self.update_datalim( corners) self.autoscale_view() # add the collection last self.add_collection(collection) return collection hexbin.__doc__ = cbook.dedent(hexbin.__doc__) % martist.kwdocd def arrow(self, x, y, dx, dy, **kwargs): """ call signature:: arrow(x, y, dx, dy, **kwargs) Draws arrow on specified axis from (*x*, *y*) to (*x* + *dx*, *y* + *dy*). Optional kwargs control the arrow properties: %(FancyArrow)s **Example:** .. plot:: mpl_examples/pylab_examples/arrow_demo.py """ a = mpatches.FancyArrow(x, y, dx, dy, **kwargs) self.add_artist(a) return a arrow.__doc__ = cbook.dedent(arrow.__doc__) % martist.kwdocd def quiverkey(self, *args, **kw): qk = mquiver.QuiverKey(*args, **kw) self.add_artist(qk) return qk quiverkey.__doc__ = mquiver.QuiverKey.quiverkey_doc def quiver(self, *args, **kw): if not self._hold: self.cla() q = mquiver.Quiver(self, *args, **kw) self.add_collection(q, False) self.update_datalim(q.XY) self.autoscale_view() return q quiver.__doc__ = mquiver.Quiver.quiver_doc def barbs(self, *args, **kw): """ %(barbs_doc)s **Example:** .. plot:: mpl_examples/pylab_examples/barb_demo.py """ if not self._hold: self.cla() b = mquiver.Barbs(self, *args, **kw) self.add_collection(b) self.update_datalim(b.get_offsets()) self.autoscale_view() return b barbs.__doc__ = cbook.dedent(barbs.__doc__) % { 'barbs_doc': mquiver.Barbs.barbs_doc} def fill(self, *args, **kwargs): """ call signature:: fill(*args, **kwargs) Plot filled polygons. *args* is a variable length argument, allowing for multiple *x*, *y* pairs with an optional color format string; see :func:`~matplotlib.pyplot.plot` for details on the argument parsing. For example, to plot a polygon with vertices at *x*, *y* in blue.:: ax.fill(x,y, 'b' ) An arbitrary number of *x*, *y*, *color* groups can be specified:: ax.fill(x1, y1, 'g', x2, y2, 'r') Return value is a list of :class:`~matplotlib.patches.Patch` instances that were added. The same color strings that :func:`~matplotlib.pyplot.plot` supports are supported by the fill format string. If you would like to fill below a curve, eg. shade a region between 0 and *y* along *x*, use :meth:`fill_between` The *closed* kwarg will close the polygon when *True* (default). kwargs control the Polygon properties: %(Polygon)s **Example:** .. plot:: mpl_examples/pylab_examples/fill_demo.py """ if not self._hold: self.cla() patches = [] for poly in self._get_patches_for_fill(*args, **kwargs): self.add_patch( poly ) patches.append( poly ) self.autoscale_view() return patches fill.__doc__ = cbook.dedent(fill.__doc__) % martist.kwdocd def fill_between(self, x, y1, y2=0, where=None, **kwargs): """ call signature:: fill_between(x, y1, y2=0, where=None, **kwargs) Create a :class:`~matplotlib.collections.PolyCollection` filling the regions between *y1* and *y2* where ``where==True`` *x* an N length np array of the x data *y1* an N length scalar or np array of the x data *y2* an N length scalar or np array of the x data *where* if None, default to fill between everywhere. If not None, it is a a N length numpy boolean array and the fill will only happen over the regions where ``where==True`` *kwargs* keyword args passed on to the :class:`PolyCollection` kwargs control the Polygon properties: %(PolyCollection)s .. plot:: mpl_examples/pylab_examples/fill_between.py """ # Handle united data, such as dates self._process_unit_info(xdata=x, ydata=y1, kwargs=kwargs) self._process_unit_info(ydata=y2) # Convert the arrays so we can work with them x = np.asarray(self.convert_xunits(x)) y1 = np.asarray(self.convert_yunits(y1)) y2 = np.asarray(self.convert_yunits(y2)) if not cbook.iterable(y1): y1 = np.ones_like(x)*y1 if not cbook.iterable(y2): y2 = np.ones_like(x)*y2 if where is None: where = np.ones(len(x), np.bool) where = np.asarray(where) assert( (len(x)==len(y1)) and (len(x)==len(y2)) and len(x)==len(where)) polys = [] for ind0, ind1 in mlab.contiguous_regions(where): theseverts = [] xslice = x[ind0:ind1] y1slice = y1[ind0:ind1] y2slice = y2[ind0:ind1] if not len(xslice): continue N = len(xslice) X = np.zeros((2*N+2, 2), np.float) # the purpose of the next two lines is for when y2 is a # scalar like 0 and we want the fill to go all the way # down to 0 even if none of the y1 sample points do X[0] = xslice[0], y2slice[0] X[N+1] = xslice[-1], y2slice[-1] X[1:N+1,0] = xslice X[1:N+1,1] = y1slice X[N+2:,0] = xslice[::-1] X[N+2:,1] = y2slice[::-1] polys.append(X) collection = mcoll.PolyCollection(polys, **kwargs) # now update the datalim and autoscale XY1 = np.array([x[where], y1[where]]).T XY2 = np.array([x[where], y2[where]]).T self.dataLim.update_from_data_xy(XY1, self.ignore_existing_data_limits, updatex=True, updatey=True) self.dataLim.update_from_data_xy(XY2, self.ignore_existing_data_limits, updatex=False, updatey=True) self.add_collection(collection) self.autoscale_view() return collection fill_between.__doc__ = cbook.dedent(fill_between.__doc__) % martist.kwdocd #### plotting z(x,y): imshow, pcolor and relatives, contour def imshow(self, X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=1.0, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, **kwargs): """ call signature:: imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=1.0, vmin=None, vmax=None, origin=None, extent=None, **kwargs) Display the image in *X* to current axes. *X* may be a float array, a uint8 array or a PIL image. If *X* is an array, *X* can have the following shapes: * MxN -- luminance (grayscale, float array only) * MxNx3 -- RGB (float or uint8 array) * MxNx4 -- RGBA (float or uint8 array) The value for each component of MxNx3 and MxNx4 float arrays should be in the range 0.0 to 1.0; MxN float arrays may be normalised. An :class:`matplotlib.image.AxesImage` instance is returned. Keyword arguments: *cmap*: [ None | Colormap ] A :class:`matplotlib.cm.Colormap` instance, eg. cm.jet. If *None*, default to rc ``image.cmap`` value. *cmap* is ignored when *X* has RGB(A) information *aspect*: [ None | 'auto' | 'equal' | scalar ] If 'auto', changes the image aspect ratio to match that of the axes If 'equal', and *extent* is *None*, changes the axes aspect ratio to match that of the image. If *extent* is not *None*, the axes aspect ratio is changed to match that of the extent. If *None*, default to rc ``image.aspect`` value. *interpolation*: Acceptable values are *None*, 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos', If *interpolation* is *None*, default to rc ``image.interpolation``. See also the *filternorm* and *filterrad* parameters *norm*: [ None | Normalize ] An :class:`matplotlib.colors.Normalize` instance; if *None*, default is ``normalization()``. This scales luminance -> 0-1 *norm* is only used for an MxN float array. *vmin*/*vmax*: [ None | scalar ] Used to scale a luminance image to 0-1. If either is *None*, the min and max of the luminance values will be used. Note if *norm* is not *None*, the settings for *vmin* and *vmax* will be ignored. *alpha*: scalar The alpha blending value, between 0 (transparent) and 1 (opaque) *origin*: [ None | 'upper' | 'lower' ] Place the [0,0] index of the array in the upper left or lower left corner of the axes. If *None*, default to rc ``image.origin``. *extent*: [ None | scalars (left, right, bottom, top) ] Eata values of the axes. The default assigns zero-based row, column indices to the *x*, *y* centers of the pixels. *shape*: [ None | scalars (columns, rows) ] For raw buffer images *filternorm*: A parameter for the antigrain image resize filter. From the antigrain documentation, if *filternorm* = 1, the filter normalizes integer values and corrects the rounding errors. It doesn't do anything with the source floating point values, it corrects only integers according to the rule of 1.0 which means that any sum of pixel weights must be equal to 1.0. So, the filter function must produce a graph of the proper shape. *filterrad*: The filter radius for filters that have a radius parameter, i.e. when interpolation is one of: 'sinc', 'lanczos' or 'blackman' Additional kwargs are :class:`~matplotlib.artist.Artist` properties: %(Artist)s **Example:** .. plot:: mpl_examples/pylab_examples/image_demo.py """ if not self._hold: self.cla() if norm is not None: assert(isinstance(norm, mcolors.Normalize)) if cmap is not None: assert(isinstance(cmap, mcolors.Colormap)) if aspect is None: aspect = rcParams['image.aspect'] self.set_aspect(aspect) im = mimage.AxesImage(self, cmap, norm, interpolation, origin, extent, filternorm=filternorm, filterrad=filterrad, resample=resample, **kwargs) im.set_data(X) im.set_alpha(alpha) self._set_artist_props(im) im.set_clip_path(self.patch) #if norm is None and shape is None: # im.set_clim(vmin, vmax) if vmin is not None or vmax is not None: im.set_clim(vmin, vmax) else: im.autoscale_None() im.set_url(url) xmin, xmax, ymin, ymax = im.get_extent() corners = (xmin, ymin), (xmax, ymax) self.update_datalim(corners) if self._autoscaleon: self.set_xlim((xmin, xmax)) self.set_ylim((ymin, ymax)) self.images.append(im) return im imshow.__doc__ = cbook.dedent(imshow.__doc__) % martist.kwdocd def _pcolorargs(self, funcname, *args): if len(args)==1: C = args[0] numRows, numCols = C.shape X, Y = np.meshgrid(np.arange(numCols+1), np.arange(numRows+1) ) elif len(args)==3: X, Y, C = args else: raise TypeError( 'Illegal arguments to %s; see help(%s)' % (funcname, funcname)) Nx = X.shape[-1] Ny = Y.shape[0] if len(X.shape) <> 2 or X.shape[0] == 1: x = X.reshape(1,Nx) X = x.repeat(Ny, axis=0) if len(Y.shape) <> 2 or Y.shape[1] == 1: y = Y.reshape(Ny, 1) Y = y.repeat(Nx, axis=1) if X.shape != Y.shape: raise TypeError( 'Incompatible X, Y inputs to %s; see help(%s)' % ( funcname, funcname)) return X, Y, C def pcolor(self, *args, **kwargs): """ call signatures:: pcolor(C, **kwargs) pcolor(X, Y, C, **kwargs) Create a pseudocolor plot of a 2-D array. *C* is the array of color values. *X* and *Y*, if given, specify the (*x*, *y*) coordinates of the colored quadrilaterals; the quadrilateral for C[i,j] has corners at:: (X[i, j], Y[i, j]), (X[i, j+1], Y[i, j+1]), (X[i+1, j], Y[i+1, j]), (X[i+1, j+1], Y[i+1, j+1]). Ideally the dimensions of *X* and *Y* should be one greater than those of *C*; if the dimensions are the same, then the last row and column of *C* will be ignored. Note that the the column index corresponds to the *x*-coordinate, and the row index corresponds to *y*; for details, see the :ref:`Grid Orientation <axes-pcolor-grid-orientation>` section below. If either or both of *X* and *Y* are 1-D arrays or column vectors, they will be expanded as needed into the appropriate 2-D arrays, making a rectangular grid. *X*, *Y* and *C* may be masked arrays. If either C[i, j], or one of the vertices surrounding C[i,j] (*X* or *Y* at [i, j], [i+1, j], [i, j+1],[i+1, j+1]) is masked, nothing is plotted. Keyword arguments: *cmap*: [ None | Colormap ] A :class:`matplotlib.cm.Colormap` instance. If *None*, use rc settings. norm: [ None | Normalize ] An :class:`matplotlib.colors.Normalize` instance is used to scale luminance data to 0,1. If *None*, defaults to :func:`normalize`. *vmin*/*vmax*: [ None | scalar ] *vmin* and *vmax* are used in conjunction with *norm* to normalize luminance data. If either are *None*, the min and max of the color array *C* is used. If you pass a *norm* instance, *vmin* and *vmax* will be ignored. *shading*: [ 'flat' | 'faceted' ] If 'faceted', a black grid is drawn around each rectangle; if 'flat', edges are not drawn. Default is 'flat', contrary to Matlab(TM). This kwarg is deprecated; please use 'edgecolors' instead: * shading='flat' -- edgecolors='None' * shading='faceted -- edgecolors='k' *edgecolors*: [ None | 'None' | color | color sequence] If *None*, the rc setting is used by default. If 'None', edges will not be visible. An mpl color or sequence of colors will set the edge color *alpha*: 0 <= scalar <= 1 the alpha blending value Return value is a :class:`matplotlib.collection.Collection` instance. .. _axes-pcolor-grid-orientation: The grid orientation follows the Matlab(TM) convention: an array *C* with shape (*nrows*, *ncolumns*) is plotted with the column number as *X* and the row number as *Y*, increasing up; hence it is plotted the way the array would be printed, except that the *Y* axis is reversed. That is, *C* is taken as *C*(*y*, *x*). Similarly for :func:`~matplotlib.pyplot.meshgrid`:: x = np.arange(5) y = np.arange(3) X, Y = meshgrid(x,y) is equivalent to: X = array([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]) Y = array([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]]) so if you have:: C = rand( len(x), len(y)) then you need:: pcolor(X, Y, C.T) or:: pcolor(C.T) Matlab :func:`pcolor` always discards the last row and column of *C*, but matplotlib displays the last row and column if *X* and *Y* are not specified, or if *X* and *Y* have one more row and column than *C*. kwargs can be used to control the :class:`~matplotlib.collection.PolyCollection` properties: %(PolyCollection)s """ if not self._hold: self.cla() alpha = kwargs.pop('alpha', 1.0) norm = kwargs.pop('norm', None) cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) shading = kwargs.pop('shading', 'flat') X, Y, C = self._pcolorargs('pcolor', *args) Ny, Nx = X.shape # convert to MA, if necessary. C = ma.asarray(C) X = ma.asarray(X) Y = ma.asarray(Y) mask = ma.getmaskarray(X)+ma.getmaskarray(Y) xymask = mask[0:-1,0:-1]+mask[1:,1:]+mask[0:-1,1:]+mask[1:,0:-1] # don't plot if C or any of the surrounding vertices are masked. mask = ma.getmaskarray(C)[0:Ny-1,0:Nx-1]+xymask newaxis = np.newaxis compress = np.compress ravelmask = (mask==0).ravel() X1 = compress(ravelmask, ma.filled(X[0:-1,0:-1]).ravel()) Y1 = compress(ravelmask, ma.filled(Y[0:-1,0:-1]).ravel()) X2 = compress(ravelmask, ma.filled(X[1:,0:-1]).ravel()) Y2 = compress(ravelmask, ma.filled(Y[1:,0:-1]).ravel()) X3 = compress(ravelmask, ma.filled(X[1:,1:]).ravel()) Y3 = compress(ravelmask, ma.filled(Y[1:,1:]).ravel()) X4 = compress(ravelmask, ma.filled(X[0:-1,1:]).ravel()) Y4 = compress(ravelmask, ma.filled(Y[0:-1,1:]).ravel()) npoly = len(X1) xy = np.concatenate((X1[:,newaxis], Y1[:,newaxis], X2[:,newaxis], Y2[:,newaxis], X3[:,newaxis], Y3[:,newaxis], X4[:,newaxis], Y4[:,newaxis], X1[:,newaxis], Y1[:,newaxis]), axis=1) verts = xy.reshape((npoly, 5, 2)) #verts = zip(zip(X1,Y1),zip(X2,Y2),zip(X3,Y3),zip(X4,Y4)) C = compress(ravelmask, ma.filled(C[0:Ny-1,0:Nx-1]).ravel()) if shading == 'faceted': edgecolors = (0,0,0,1), linewidths = (0.25,) else: edgecolors = 'face' linewidths = (1.0,) kwargs.setdefault('edgecolors', edgecolors) kwargs.setdefault('antialiaseds', (0,)) kwargs.setdefault('linewidths', linewidths) collection = mcoll.PolyCollection(verts, **kwargs) collection.set_alpha(alpha) collection.set_array(C) if norm is not None: assert(isinstance(norm, mcolors.Normalize)) if cmap is not None: assert(isinstance(cmap, mcolors.Colormap)) collection.set_cmap(cmap) collection.set_norm(norm) if vmin is not None or vmax is not None: collection.set_clim(vmin, vmax) else: collection.autoscale_None() self.grid(False) x = X.compressed() y = Y.compressed() minx = np.amin(x) maxx = np.amax(x) miny = np.amin(y) maxy = np.amax(y) corners = (minx, miny), (maxx, maxy) self.update_datalim( corners) self.autoscale_view() self.add_collection(collection) return collection pcolor.__doc__ = cbook.dedent(pcolor.__doc__) % martist.kwdocd def pcolormesh(self, *args, **kwargs): """ call signatures:: pcolormesh(C) pcolormesh(X, Y, C) pcolormesh(C, **kwargs) *C* may be a masked array, but *X* and *Y* may not. Masked array support is implemented via *cmap* and *norm*; in contrast, :func:`~matplotlib.pyplot.pcolor` simply does not draw quadrilaterals with masked colors or vertices. Keyword arguments: *cmap*: [ None | Colormap ] A :class:`matplotlib.cm.Colormap` instance. If None, use rc settings. *norm*: [ None | Normalize ] A :class:`matplotlib.colors.Normalize` instance is used to scale luminance data to 0,1. If None, defaults to :func:`normalize`. *vmin*/*vmax*: [ None | scalar ] *vmin* and *vmax* are used in conjunction with *norm* to normalize luminance data. If either are *None*, the min and max of the color array *C* is used. If you pass a *norm* instance, *vmin* and *vmax* will be ignored. *shading*: [ 'flat' | 'faceted' ] If 'faceted', a black grid is drawn around each rectangle; if 'flat', edges are not drawn. Default is 'flat', contrary to Matlab(TM). This kwarg is deprecated; please use 'edgecolors' instead: * shading='flat' -- edgecolors='None' * shading='faceted -- edgecolors='k' *edgecolors*: [ None | 'None' | color | color sequence] If None, the rc setting is used by default. If 'None', edges will not be visible. An mpl color or sequence of colors will set the edge color *alpha*: 0 <= scalar <= 1 the alpha blending value Return value is a :class:`matplotlib.collection.QuadMesh` object. kwargs can be used to control the :class:`matplotlib.collections.QuadMesh` properties: %(QuadMesh)s .. seealso:: :func:`~matplotlib.pyplot.pcolor`: For an explanation of the grid orientation and the expansion of 1-D *X* and/or *Y* to 2-D arrays. """ if not self._hold: self.cla() alpha = kwargs.pop('alpha', 1.0) norm = kwargs.pop('norm', None) cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) shading = kwargs.pop('shading', 'flat') edgecolors = kwargs.pop('edgecolors', 'None') antialiased = kwargs.pop('antialiased', False) X, Y, C = self._pcolorargs('pcolormesh', *args) Ny, Nx = X.shape # convert to one dimensional arrays C = ma.ravel(C[0:Ny-1, 0:Nx-1]) # data point in each cell is value at # lower left corner X = X.ravel() Y = Y.ravel() coords = np.zeros(((Nx * Ny), 2), dtype=float) coords[:, 0] = X coords[:, 1] = Y if shading == 'faceted' or edgecolors != 'None': showedges = 1 else: showedges = 0 collection = mcoll.QuadMesh( Nx - 1, Ny - 1, coords, showedges, antialiased=antialiased) # kwargs are not used collection.set_alpha(alpha) collection.set_array(C) if norm is not None: assert(isinstance(norm, mcolors.Normalize)) if cmap is not None: assert(isinstance(cmap, mcolors.Colormap)) collection.set_cmap(cmap) collection.set_norm(norm) if vmin is not None or vmax is not None: collection.set_clim(vmin, vmax) else: collection.autoscale_None() self.grid(False) minx = np.amin(X) maxx = np.amax(X) miny = np.amin(Y) maxy = np.amax(Y) corners = (minx, miny), (maxx, maxy) self.update_datalim( corners) self.autoscale_view() self.add_collection(collection) return collection pcolormesh.__doc__ = cbook.dedent(pcolormesh.__doc__) % martist.kwdocd def pcolorfast(self, *args, **kwargs): """ pseudocolor plot of a 2-D array Experimental; this is a version of pcolor that does not draw lines, that provides the fastest possible rendering with the Agg backend, and that can handle any quadrilateral grid. Call signatures:: pcolor(C, **kwargs) pcolor(xr, yr, C, **kwargs) pcolor(x, y, C, **kwargs) pcolor(X, Y, C, **kwargs) C is the 2D array of color values corresponding to quadrilateral cells. Let (nr, nc) be its shape. C may be a masked array. ``pcolor(C, **kwargs)`` is equivalent to ``pcolor([0,nc], [0,nr], C, **kwargs)`` *xr*, *yr* specify the ranges of *x* and *y* corresponding to the rectangular region bounding *C*. If:: xr = [x0, x1] and:: yr = [y0,y1] then *x* goes from *x0* to *x1* as the second index of *C* goes from 0 to *nc*, etc. (*x0*, *y0*) is the outermost corner of cell (0,0), and (*x1*, *y1*) is the outermost corner of cell (*nr*-1, *nc*-1). All cells are rectangles of the same size. This is the fastest version. *x*, *y* are 1D arrays of length *nc* +1 and *nr* +1, respectively, giving the x and y boundaries of the cells. Hence the cells are rectangular but the grid may be nonuniform. The speed is intermediate. (The grid is checked, and if found to be uniform the fast version is used.) *X* and *Y* are 2D arrays with shape (*nr* +1, *nc* +1) that specify the (x,y) coordinates of the corners of the colored quadrilaterals; the quadrilateral for C[i,j] has corners at (X[i,j],Y[i,j]), (X[i,j+1],Y[i,j+1]), (X[i+1,j],Y[i+1,j]), (X[i+1,j+1],Y[i+1,j+1]). The cells need not be rectangular. This is the most general, but the slowest to render. It may produce faster and more compact output using ps, pdf, and svg backends, however. Note that the the column index corresponds to the x-coordinate, and the row index corresponds to y; for details, see the "Grid Orientation" section below. Optional keyword arguments: *cmap*: [ None | Colormap ] A cm Colormap instance from cm. If None, use rc settings. *norm*: [ None | Normalize ] An mcolors.Normalize instance is used to scale luminance data to 0,1. If None, defaults to normalize() *vmin*/*vmax*: [ None | scalar ] *vmin* and *vmax* are used in conjunction with norm to normalize luminance data. If either are *None*, the min and max of the color array *C* is used. If you pass a norm instance, *vmin* and *vmax* will be *None*. *alpha*: 0 <= scalar <= 1 the alpha blending value Return value is an image if a regular or rectangular grid is specified, and a QuadMesh collection in the general quadrilateral case. """ if not self._hold: self.cla() alpha = kwargs.pop('alpha', 1.0) norm = kwargs.pop('norm', None) cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) if norm is not None: assert(isinstance(norm, mcolors.Normalize)) if cmap is not None: assert(isinstance(cmap, mcolors.Colormap)) C = args[-1] nr, nc = C.shape if len(args) == 1: style = "image" x = [0, nc] y = [0, nr] elif len(args) == 3: x, y = args[:2] x = np.asarray(x) y = np.asarray(y) if x.ndim == 1 and y.ndim == 1: if x.size == 2 and y.size == 2: style = "image" else: dx = np.diff(x) dy = np.diff(y) if (np.ptp(dx) < 0.01*np.abs(dx.mean()) and np.ptp(dy) < 0.01*np.abs(dy.mean())): style = "image" else: style = "pcolorimage" elif x.ndim == 2 and y.ndim == 2: style = "quadmesh" else: raise TypeError("arguments do not match valid signatures") else: raise TypeError("need 1 argument or 3 arguments") if style == "quadmesh": # convert to one dimensional arrays # This should also be moved to the QuadMesh class C = ma.ravel(C) # data point in each cell is value # at lower left corner X = x.ravel() Y = y.ravel() Nx = nc+1 Ny = nr+1 # The following needs to be cleaned up; the renderer # requires separate contiguous arrays for X and Y, # but the QuadMesh class requires the 2D array. coords = np.empty(((Nx * Ny), 2), np.float64) coords[:, 0] = X coords[:, 1] = Y # The QuadMesh class can also be changed to # handle relevant superclass kwargs; the initializer # should do much more than it does now. collection = mcoll.QuadMesh(nc, nr, coords, 0) collection.set_alpha(alpha) collection.set_array(C) collection.set_cmap(cmap) collection.set_norm(norm) self.add_collection(collection) xl, xr, yb, yt = X.min(), X.max(), Y.min(), Y.max() ret = collection else: # One of the image styles: xl, xr, yb, yt = x[0], x[-1], y[0], y[-1] if style == "image": im = mimage.AxesImage(self, cmap, norm, interpolation='nearest', origin='lower', extent=(xl, xr, yb, yt), **kwargs) im.set_data(C) im.set_alpha(alpha) self.images.append(im) ret = im if style == "pcolorimage": im = mimage.PcolorImage(self, x, y, C, cmap=cmap, norm=norm, alpha=alpha, **kwargs) self.images.append(im) ret = im self._set_artist_props(ret) if vmin is not None or vmax is not None: ret.set_clim(vmin, vmax) else: ret.autoscale_None() self.update_datalim(np.array([[xl, yb], [xr, yt]])) self.autoscale_view(tight=True) return ret def contour(self, *args, **kwargs): if not self._hold: self.cla() kwargs['filled'] = False return mcontour.ContourSet(self, *args, **kwargs) contour.__doc__ = mcontour.ContourSet.contour_doc def contourf(self, *args, **kwargs): if not self._hold: self.cla() kwargs['filled'] = True return mcontour.ContourSet(self, *args, **kwargs) contourf.__doc__ = mcontour.ContourSet.contour_doc def clabel(self, CS, *args, **kwargs): return CS.clabel(*args, **kwargs) clabel.__doc__ = mcontour.ContourSet.clabel.__doc__ def table(self, **kwargs): """ call signature:: table(cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None): Add a table to the current axes. Returns a :class:`matplotlib.table.Table` instance. For finer grained control over tables, use the :class:`~matplotlib.table.Table` class and add it to the axes with :meth:`~matplotlib.axes.Axes.add_table`. Thanks to John Gill for providing the class and table. kwargs control the :class:`~matplotlib.table.Table` properties: %(Table)s """ return mtable.table(self, **kwargs) table.__doc__ = cbook.dedent(table.__doc__) % martist.kwdocd def twinx(self): """ call signature:: ax = twinx() create a twin of Axes for generating a plot with a sharex x-axis but independent y axis. The y-axis of self will have ticks on left and the returned axes will have ticks on the right """ ax2 = self.figure.add_axes(self.get_position(True), sharex=self, frameon=False) ax2.yaxis.tick_right() ax2.yaxis.set_label_position('right') self.yaxis.tick_left() return ax2 def twiny(self): """ call signature:: ax = twiny() create a twin of Axes for generating a plot with a shared y-axis but independent x axis. The x-axis of self will have ticks on bottom and the returned axes will have ticks on the top """ ax2 = self.figure.add_axes(self.get_position(True), sharey=self, frameon=False) ax2.xaxis.tick_top() ax2.xaxis.set_label_position('top') self.xaxis.tick_bottom() return ax2 def get_shared_x_axes(self): 'Return a copy of the shared axes Grouper object for x axes' return self._shared_x_axes def get_shared_y_axes(self): 'Return a copy of the shared axes Grouper object for y axes' return self._shared_y_axes #### Data analysis def hist(self, x, bins=10, range=None, normed=False, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, **kwargs): """ call signature:: hist(x, bins=10, range=None, normed=False, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, **kwargs) Compute and draw the histogram of *x*. The return value is a tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*, [*patches0*, *patches1*,...]) if the input contains multiple data. Keyword arguments: *bins*: Either an integer number of bins or a sequence giving the bins. *x* are the data to be binned. *x* can be an array, a 2D array with multiple data in its columns, or a list of arrays with data of different length. Note, if *bins* is an integer input argument=numbins, *bins* + 1 bin edges will be returned, compatible with the semantics of :func:`numpy.histogram` with the *new* = True argument. Unequally spaced bins are supported if *bins* is a sequence. *range*: The lower and upper range of the bins. Lower and upper outliers are ignored. If not provided, *range* is (x.min(), x.max()). Range has no effect if *bins* is a sequence. If *bins* is a sequence or *range* is specified, autoscaling is set off (*autoscale_on* is set to *False*) and the xaxis limits are set to encompass the full specified bin range. *normed*: If *True*, the first element of the return tuple will be the counts normalized to form a probability density, i.e., ``n/(len(x)*dbin)``. In a probability density, the integral of the histogram should be 1; you can verify that with a trapezoidal integration of the probability density function:: pdf, bins, patches = ax.hist(...) print np.sum(pdf * np.diff(bins)) *cumulative*: If *True*, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values. The last bin gives the total number of datapoints. If *normed* is also *True* then the histogram is normalized such that the last bin equals 1. If *cumulative* evaluates to less than 0 (e.g. -1), the direction of accumulation is reversed. In this case, if *normed* is also *True*, then the histogram is normalized such that the first bin equals 1. *histtype*: [ 'bar' | 'barstacked' | 'step' | 'stepfilled' ] The type of histogram to draw. - 'bar' is a traditional bar-type histogram. If multiple data are given the bars are aranged side by side. - 'barstacked' is a bar-type histogram where multiple data are stacked on top of each other. - 'step' generates a lineplot that is by default unfilled. - 'stepfilled' generates a lineplot that is by default filled. *align*: ['left' | 'mid' | 'right' ] Controls how the histogram is plotted. - 'left': bars are centered on the left bin edges. - 'mid': bars are centered between the bin edges. - 'right': bars are centered on the right bin edges. *orientation*: [ 'horizontal' | 'vertical' ] If 'horizontal', :func:`~matplotlib.pyplot.barh` will be used for bar-type histograms and the *bottom* kwarg will be the left edges. *rwidth*: The relative width of the bars as a fraction of the bin width. If *None*, automatically compute the width. Ignored if *histtype* = 'step' or 'stepfilled'. *log*: If *True*, the histogram axis will be set to a log scale. If *log* is *True* and *x* is a 1D array, empty bins will be filtered out and only the non-empty (*n*, *bins*, *patches*) will be returned. kwargs are used to update the properties of the hist :class:`~matplotlib.patches.Rectangle` instances: %(Rectangle)s You can use labels for your histogram, and only the first :class:`~matplotlib.patches.Rectangle` gets the label (the others get the magic string '_nolegend_'. This will make the histograms work in the intuitive way for bar charts:: ax.hist(10+2*np.random.randn(1000), label='men') ax.hist(12+3*np.random.randn(1000), label='women', alpha=0.5) ax.legend() **Example:** .. plot:: mpl_examples/pylab_examples/histogram_demo.py """ if not self._hold: self.cla() # NOTE: the range keyword overwrites the built-in func range !!! # needs to be fixed in with numpy !!! if kwargs.get('width') is not None: raise DeprecationWarning( 'hist now uses the rwidth to give relative width ' 'and not absolute width') try: # make sure a copy is created: don't use asarray x = np.transpose(np.array(x)) if len(x.shape)==1: x.shape = (1,x.shape[0]) elif len(x.shape)==2 and x.shape[1]<x.shape[0]: warnings.warn('2D hist should be nsamples x nvariables; ' 'this looks transposed') except ValueError: # multiple hist with data of different length if iterable(x[0]) and not is_string_like(x[0]): tx = [] for i in xrange(len(x)): tx.append( np.array(x[i]) ) x = tx else: raise ValueError, 'Can not use providet data to create a histogram' # Check whether bins or range are given explicitly. In that # case do not autoscale axes. binsgiven = (cbook.iterable(bins) or range != None) # check the version of the numpy if np.__version__ < "1.3": # version 1.1 and 1.2 hist_kwargs = dict(range=range, normed=bool(normed), new=True) else: # version 1.3 and later, drop new=True hist_kwargs = dict(range=range, normed=bool(normed)) n = [] for i in xrange(len(x)): # this will automatically overwrite bins, # so that each histogram uses the same bins m, bins = np.histogram(x[i], bins, **hist_kwargs) n.append(m) if cumulative: slc = slice(None) if cbook.is_numlike(cumulative) and cumulative < 0: slc = slice(None,None,-1) if normed: n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n] else: n = [m[slc].cumsum()[slc] for m in n] patches = [] if histtype.startswith('bar'): totwidth = np.diff(bins) stacked = False if rwidth is not None: dr = min(1., max(0., rwidth)) elif len(n)>1: dr = 0.8 else: dr = 1.0 if histtype=='bar': width = dr*totwidth/len(n) dw = width if len(n)>1: boffset = -0.5*dr*totwidth*(1.-1./len(n)) else: boffset = 0.0 elif histtype=='barstacked': width = dr*totwidth boffset, dw = 0.0, 0.0 stacked = True else: raise ValueError, 'invalid histtype: %s' % histtype if align == 'mid' or align == 'edge': boffset += 0.5*totwidth elif align == 'right': boffset += totwidth elif align != 'left' and align != 'center': raise ValueError, 'invalid align: %s' % align if orientation == 'horizontal': for m in n: color = self._get_lines._get_next_cycle_color() patch = self.barh(bins[:-1]+boffset, m, height=width, left=bottom, align='center', log=log, color=color) patches.append(patch) if stacked: if bottom is None: bottom = 0.0 bottom += m boffset += dw elif orientation == 'vertical': for m in n: color = self._get_lines._get_next_cycle_color() patch = self.bar(bins[:-1]+boffset, m, width=width, bottom=bottom, align='center', log=log, color=color) patches.append(patch) if stacked: if bottom is None: bottom = 0.0 bottom += m boffset += dw else: raise ValueError, 'invalid orientation: %s' % orientation elif histtype.startswith('step'): x = np.zeros( 2*len(bins), np.float ) y = np.zeros( 2*len(bins), np.float ) x[0::2], x[1::2] = bins, bins if align == 'left' or align == 'center': x -= 0.5*(bins[1]-bins[0]) elif align == 'right': x += 0.5*(bins[1]-bins[0]) elif align != 'mid' and align != 'edge': raise ValueError, 'invalid align: %s' % align if log: y[0],y[-1] = 1e-100, 1e-100 if orientation == 'horizontal': self.set_xscale('log') elif orientation == 'vertical': self.set_yscale('log') fill = False if histtype == 'stepfilled': fill = True elif histtype != 'step': raise ValueError, 'invalid histtype: %s' % histtype for m in n: y[1:-1:2], y[2::2] = m, m if orientation == 'horizontal': x,y = y,x elif orientation != 'vertical': raise ValueError, 'invalid orientation: %s' % orientation color = self._get_lines._get_next_cycle_color() if fill: patches.append( self.fill(x, y, closed=False, facecolor=color) ) else: patches.append( self.fill(x, y, closed=False, edgecolor=color, fill=False) ) # adopted from adjust_x/ylim part of the bar method if orientation == 'horizontal': xmin, xmax = 0, self.dataLim.intervalx[1] for m in n: xmin = np.amin(m[m!=0]) # filter out the 0 height bins xmin = max(xmin*0.9, 1e-100) self.dataLim.intervalx = (xmin, xmax) elif orientation == 'vertical': ymin, ymax = 0, self.dataLim.intervaly[1] for m in n: ymin = np.amin(m[m!=0]) # filter out the 0 height bins ymin = max(ymin*0.9, 1e-100) self.dataLim.intervaly = (ymin, ymax) self.autoscale_view() else: raise ValueError, 'invalid histtype: %s' % histtype label = kwargs.pop('label', '') for patch in patches: for p in patch: p.update(kwargs) p.set_label(label) label = '_nolegend_' if binsgiven: self.set_autoscale_on(False) if orientation == 'vertical': self.autoscale_view(scalex=False, scaley=True) XL = self.xaxis.get_major_locator().view_limits(bins[0], bins[-1]) self.set_xbound(XL) else: self.autoscale_view(scalex=True, scaley=False) YL = self.yaxis.get_major_locator().view_limits(bins[0], bins[-1]) self.set_ybound(YL) if len(n)==1: return n[0], bins, cbook.silent_list('Patch', patches[0]) else: return n, bins, cbook.silent_list('Lists of Patches', patches) hist.__doc__ = cbook.dedent(hist.__doc__) % martist.kwdocd def psd(self, x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None, **kwargs): """ call signature:: psd(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None, **kwargs) The power spectral density by Welch's average periodogram method. The vector *x* is divided into *NFFT* length segments. Each segment is detrended by function *detrend* and windowed by function *window*. *noverlap* gives the length of the overlap between segments. The :math:`|\mathrm{fft}(i)|^2` of each segment :math:`i` are averaged to compute *Pxx*, with a scaling to correct for power loss due to windowing. *Fs* is the sampling frequency. %(PSD)s *Fc*: integer The center frequency of *x* (defaults to 0), which offsets the x extents of the plot to reflect the frequency range used when a signal is acquired and then filtered and downsampled to baseband. Returns the tuple (*Pxx*, *freqs*). For plotting, the power is plotted as :math:`10\log_{10}(P_{xx})` for decibels, though *Pxx* itself is returned. References: Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) kwargs control the :class:`~matplotlib.lines.Line2D` properties: %(Line2D)s **Example:** .. plot:: mpl_examples/pylab_examples/psd_demo.py """ if not self._hold: self.cla() pxx, freqs = mlab.psd(x, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq) pxx.shape = len(freqs), freqs += Fc if scale_by_freq in (None, True): psd_units = 'dB/Hz' else: psd_units = 'dB' self.plot(freqs, 10*np.log10(pxx), **kwargs) self.set_xlabel('Frequency') self.set_ylabel('Power Spectral Density (%s)' % psd_units) self.grid(True) vmin, vmax = self.viewLim.intervaly intv = vmax-vmin logi = int(np.log10(intv)) if logi==0: logi=.1 step = 10*logi #print vmin, vmax, step, intv, math.floor(vmin), math.ceil(vmax)+1 ticks = np.arange(math.floor(vmin), math.ceil(vmax)+1, step) self.set_yticks(ticks) return pxx, freqs psd_doc_dict = dict() psd_doc_dict.update(martist.kwdocd) psd_doc_dict.update(mlab.kwdocd) psd_doc_dict['PSD'] = cbook.dedent(psd_doc_dict['PSD']) psd.__doc__ = cbook.dedent(psd.__doc__) % psd_doc_dict def csd(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None, **kwargs): """ call signature:: csd(x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None, **kwargs) The cross spectral density :math:`P_{xy}` by Welch's average periodogram method. The vectors *x* and *y* are divided into *NFFT* length segments. Each segment is detrended by function *detrend* and windowed by function *window*. The product of the direct FFTs of *x* and *y* are averaged over each segment to compute :math:`P_{xy}`, with a scaling to correct for power loss due to windowing. Returns the tuple (*Pxy*, *freqs*). *P* is the cross spectrum (complex valued), and :math:`10\log_{10}|P_{xy}|` is plotted. %(PSD)s *Fc*: integer The center frequency of *x* (defaults to 0), which offsets the x extents of the plot to reflect the frequency range used when a signal is acquired and then filtered and downsampled to baseband. References: Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) kwargs control the Line2D properties: %(Line2D)s **Example:** .. plot:: mpl_examples/pylab_examples/csd_demo.py .. seealso: :meth:`psd` For a description of the optional parameters. """ if not self._hold: self.cla() pxy, freqs = mlab.csd(x, y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq) pxy.shape = len(freqs), # pxy is complex freqs += Fc self.plot(freqs, 10*np.log10(np.absolute(pxy)), **kwargs) self.set_xlabel('Frequency') self.set_ylabel('Cross Spectrum Magnitude (dB)') self.grid(True) vmin, vmax = self.viewLim.intervaly intv = vmax-vmin step = 10*int(np.log10(intv)) ticks = np.arange(math.floor(vmin), math.ceil(vmax)+1, step) self.set_yticks(ticks) return pxy, freqs csd.__doc__ = cbook.dedent(csd.__doc__) % psd_doc_dict def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None, **kwargs): """ call signature:: cohere(x, y, NFFT=256, Fs=2, Fc=0, detrend = mlab.detrend_none, window = mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None, **kwargs) cohere the coherence between *x* and *y*. Coherence is the normalized cross spectral density: .. math:: C_{xy} = \\frac{|P_{xy}|^2}{P_{xx}P_{yy}} %(PSD)s *Fc*: integer The center frequency of *x* (defaults to 0), which offsets the x extents of the plot to reflect the frequency range used when a signal is acquired and then filtered and downsampled to baseband. The return value is a tuple (*Cxy*, *f*), where *f* are the frequencies of the coherence vector. kwargs are applied to the lines. References: * Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) kwargs control the :class:`~matplotlib.lines.Line2D` properties of the coherence plot: %(Line2D)s **Example:** .. plot:: mpl_examples/pylab_examples/cohere_demo.py """ if not self._hold: self.cla() cxy, freqs = mlab.cohere(x, y, NFFT, Fs, detrend, window, noverlap, scale_by_freq) freqs += Fc self.plot(freqs, cxy, **kwargs) self.set_xlabel('Frequency') self.set_ylabel('Coherence') self.grid(True) return cxy, freqs cohere.__doc__ = cbook.dedent(cohere.__doc__) % psd_doc_dict def specgram(self, x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=128, cmap=None, xextent=None, pad_to=None, sides='default', scale_by_freq=None): """ call signature:: specgram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=128, cmap=None, xextent=None, pad_to=None, sides='default', scale_by_freq=None) Compute a spectrogram of data in *x*. Data are split into *NFFT* length segments and the PSD of each section is computed. The windowing function *window* is applied to each segment, and the amount of overlap of each segment is specified with *noverlap*. %(PSD)s *Fc*: integer The center frequency of *x* (defaults to 0), which offsets the y extents of the plot to reflect the frequency range used when a signal is acquired and then filtered and downsampled to baseband. *cmap*: A :class:`matplotlib.cm.Colormap` instance; if *None* use default determined by rc *xextent*: The image extent along the x-axis. xextent = (xmin,xmax) The default is (0,max(bins)), where bins is the return value from :func:`mlab.specgram` Return value is (*Pxx*, *freqs*, *bins*, *im*): - *bins* are the time points the spectrogram is calculated over - *freqs* is an array of frequencies - *Pxx* is a len(times) x len(freqs) array of power - *im* is a :class:`matplotlib.image.AxesImage` instance Note: If *x* is real (i.e. non-complex), only the positive spectrum is shown. If *x* is complex, both positive and negative parts of the spectrum are shown. This can be overridden using the *sides* keyword argument. **Example:** .. plot:: mpl_examples/pylab_examples/specgram_demo.py """ if not self._hold: self.cla() Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq) Z = 10. * np.log10(Pxx) Z = np.flipud(Z) if xextent is None: xextent = 0, np.amax(bins) xmin, xmax = xextent freqs += Fc extent = xmin, xmax, freqs[0], freqs[-1] im = self.imshow(Z, cmap, extent=extent) self.axis('auto') return Pxx, freqs, bins, im specgram.__doc__ = cbook.dedent(specgram.__doc__) % psd_doc_dict del psd_doc_dict #So that this does not become an Axes attribute def spy(self, Z, precision=0, marker=None, markersize=None, aspect='equal', **kwargs): """ call signature:: spy(Z, precision=0, marker=None, markersize=None, aspect='equal', **kwargs) ``spy(Z)`` plots the sparsity pattern of the 2-D array *Z*. If *precision* is 0, any non-zero value will be plotted; else, values of :math:`|Z| > precision` will be plotted. For :class:`scipy.sparse.spmatrix` instances, there is a special case: if *precision* is 'present', any value present in the array will be plotted, even if it is identically zero. The array will be plotted as it would be printed, with the first index (row) increasing down and the second index (column) increasing to the right. By default aspect is 'equal', so that each array element occupies a square space; set the aspect kwarg to 'auto' to allow the plot to fill the plot box, or to any scalar number to specify the aspect ratio of an array element directly. Two plotting styles are available: image or marker. Both are available for full arrays, but only the marker style works for :class:`scipy.sparse.spmatrix` instances. If *marker* and *markersize* are *None*, an image will be returned and any remaining kwargs are passed to :func:`~matplotlib.pyplot.imshow`; else, a :class:`~matplotlib.lines.Line2D` object will be returned with the value of marker determining the marker type, and any remaining kwargs passed to the :meth:`~matplotlib.axes.Axes.plot` method. If *marker* and *markersize* are *None*, useful kwargs include: * *cmap* * *alpha* .. seealso:: :func:`~matplotlib.pyplot.imshow` For controlling colors, e.g. cyan background and red marks, use:: cmap = mcolors.ListedColormap(['c','r']) If *marker* or *markersize* is not *None*, useful kwargs include: * *marker* * *markersize* * *color* Useful values for *marker* include: * 's' square (default) * 'o' circle * '.' point * ',' pixel .. seealso:: :func:`~matplotlib.pyplot.plot` """ if precision is None: precision = 0 warnings.DeprecationWarning("Use precision=0 instead of None") # 2008/10/03 if marker is None and markersize is None and hasattr(Z, 'tocoo'): marker = 's' if marker is None and markersize is None: Z = np.asarray(Z) mask = np.absolute(Z)>precision if 'cmap' not in kwargs: kwargs['cmap'] = mcolors.ListedColormap(['w', 'k'], name='binary') nr, nc = Z.shape extent = [-0.5, nc-0.5, nr-0.5, -0.5] ret = self.imshow(mask, interpolation='nearest', aspect=aspect, extent=extent, origin='upper', **kwargs) else: if hasattr(Z, 'tocoo'): c = Z.tocoo() if precision == 'present': y = c.row x = c.col else: nonzero = np.absolute(c.data) > precision y = c.row[nonzero] x = c.col[nonzero] else: Z = np.asarray(Z) nonzero = np.absolute(Z)>precision y, x = np.nonzero(nonzero) if marker is None: marker = 's' if markersize is None: markersize = 10 marks = mlines.Line2D(x, y, linestyle='None', marker=marker, markersize=markersize, **kwargs) self.add_line(marks) nr, nc = Z.shape self.set_xlim(xmin=-0.5, xmax=nc-0.5) self.set_ylim(ymin=nr-0.5, ymax=-0.5) self.set_aspect(aspect) ret = marks self.title.set_y(1.05) self.xaxis.tick_top() self.xaxis.set_ticks_position('both') self.xaxis.set_major_locator(mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True)) self.yaxis.set_major_locator(mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True)) return ret def matshow(self, Z, **kwargs): ''' Plot a matrix or array as an image. The matrix will be shown the way it would be printed, with the first row at the top. Row and column numbering is zero-based. Argument: *Z* anything that can be interpreted as a 2-D array kwargs all are passed to :meth:`~matplotlib.axes.Axes.imshow`. :meth:`matshow` sets defaults for *extent*, *origin*, *interpolation*, and *aspect*; use care in overriding the *extent* and *origin* kwargs, because they interact. (Also, if you want to change them, you probably should be using imshow directly in your own version of matshow.) Returns: an :class:`matplotlib.image.AxesImage` instance. ''' Z = np.asarray(Z) nr, nc = Z.shape extent = [-0.5, nc-0.5, nr-0.5, -0.5] kw = {'extent': extent, 'origin': 'upper', 'interpolation': 'nearest', 'aspect': 'equal'} # (already the imshow default) kw.update(kwargs) im = self.imshow(Z, **kw) self.title.set_y(1.05) self.xaxis.tick_top() self.xaxis.set_ticks_position('both') self.xaxis.set_major_locator(mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True)) self.yaxis.set_major_locator(mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True)) return im class SubplotBase: """ Base class for subplots, which are :class:`Axes` instances with additional methods to facilitate generating and manipulating a set of :class:`Axes` within a figure. """ def __init__(self, fig, *args, **kwargs): """ *fig* is a :class:`matplotlib.figure.Figure` instance. *args* is the tuple (*numRows*, *numCols*, *plotNum*), where the array of subplots in the figure has dimensions *numRows*, *numCols*, and where *plotNum* is the number of the subplot being created. *plotNum* starts at 1 in the upper left corner and increases to the right. If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*. """ self.figure = fig if len(args)==1: s = str(args[0]) if len(s) != 3: raise ValueError('Argument to subplot must be a 3 digits long') rows, cols, num = map(int, s) elif len(args)==3: rows, cols, num = args else: raise ValueError( 'Illegal argument to subplot') total = rows*cols num -= 1 # convert from matlab to python indexing # ie num in range(0,total) if num >= total: raise ValueError( 'Subplot number exceeds total subplots') self._rows = rows self._cols = cols self._num = num self.update_params() # _axes_class is set in the subplot_class_factory self._axes_class.__init__(self, fig, self.figbox, **kwargs) def get_geometry(self): 'get the subplot geometry, eg 2,2,3' return self._rows, self._cols, self._num+1 # COVERAGE NOTE: Never used internally or from examples def change_geometry(self, numrows, numcols, num): 'change subplot geometry, eg. from 1,1,1 to 2,2,3' self._rows = numrows self._cols = numcols self._num = num-1 self.update_params() self.set_position(self.figbox) def update_params(self): 'update the subplot position from fig.subplotpars' rows = self._rows cols = self._cols num = self._num pars = self.figure.subplotpars left = pars.left right = pars.right bottom = pars.bottom top = pars.top wspace = pars.wspace hspace = pars.hspace totWidth = right-left totHeight = top-bottom figH = totHeight/(rows + hspace*(rows-1)) sepH = hspace*figH figW = totWidth/(cols + wspace*(cols-1)) sepW = wspace*figW rowNum, colNum = divmod(num, cols) figBottom = top - (rowNum+1)*figH - rowNum*sepH figLeft = left + colNum*(figW + sepW) self.figbox = mtransforms.Bbox.from_bounds(figLeft, figBottom, figW, figH) self.rowNum = rowNum self.colNum = colNum self.numRows = rows self.numCols = cols if 0: print 'rcn', rows, cols, num print 'lbrt', left, bottom, right, top print 'self.figBottom', self.figBottom print 'self.figLeft', self.figLeft print 'self.figW', self.figW print 'self.figH', self.figH print 'self.rowNum', self.rowNum print 'self.colNum', self.colNum print 'self.numRows', self.numRows print 'self.numCols', self.numCols def is_first_col(self): return self.colNum==0 def is_first_row(self): return self.rowNum==0 def is_last_row(self): return self.rowNum==self.numRows-1 def is_last_col(self): return self.colNum==self.numCols-1 # COVERAGE NOTE: Never used internally or from examples def label_outer(self): """ set the visible property on ticklabels so xticklabels are visible only if the subplot is in the last row and yticklabels are visible only if the subplot is in the first column """ lastrow = self.is_last_row() firstcol = self.is_first_col() for label in self.get_xticklabels(): label.set_visible(lastrow) for label in self.get_yticklabels(): label.set_visible(firstcol) _subplot_classes = {} def subplot_class_factory(axes_class=None): # This makes a new class that inherits from SubclassBase and the # given axes_class (which is assumed to be a subclass of Axes). # This is perhaps a little bit roundabout to make a new class on # the fly like this, but it means that a new Subplot class does # not have to be created for every type of Axes. if axes_class is None: axes_class = Axes new_class = _subplot_classes.get(axes_class) if new_class is None: new_class = new.classobj("%sSubplot" % (axes_class.__name__), (SubplotBase, axes_class), {'_axes_class': axes_class}) _subplot_classes[axes_class] = new_class return new_class # This is provided for backward compatibility Subplot = subplot_class_factory() martist.kwdocd['Axes'] = martist.kwdocd['Subplot'] = martist.kwdoc(Axes) """ # this is some discarded code I was using to find the minimum positive # data point for some log scaling fixes. I realized there was a # cleaner way to do it, but am keeping this around as an example for # how to get the data out of the axes. Might want to make something # like this a method one day, or better yet make get_verts an Artist # method minx, maxx = self.get_xlim() if minx<=0 or maxx<=0: # find the min pos value in the data xs = [] for line in self.lines: xs.extend(line.get_xdata(orig=False)) for patch in self.patches: xs.extend([x for x,y in patch.get_verts()]) for collection in self.collections: xs.extend([x for x,y in collection.get_verts()]) posx = [x for x in xs if x>0] if len(posx): minx = min(posx) maxx = max(posx) # warning, probably breaks inverted axis self.set_xlim((0.1*minx, maxx)) """
agpl-3.0
nvoron23/scikit-learn
doc/sphinxext/numpy_ext/docscrape_sphinx.py
408
8061
import re import inspect import textwrap import pydoc from .docscrape import NumpyDocString from .docscrape import FunctionDoc from .docscrape import ClassDoc class SphinxDocString(NumpyDocString): def __init__(self, docstring, config=None): config = {} if config is None else config self.use_plots = config.get('use_plots', False) NumpyDocString.__init__(self, docstring, config=config) # string conversion routines def _str_header(self, name, symbol='`'): return ['.. rubric:: ' + name, ''] def _str_field_list(self, name): return [':' + name + ':'] def _str_indent(self, doc, indent=4): out = [] for line in doc: out += [' ' * indent + line] return out def _str_signature(self): return [''] if self['Signature']: return ['``%s``' % self['Signature']] + [''] else: return [''] def _str_summary(self): return self['Summary'] + [''] def _str_extended_summary(self): return self['Extended Summary'] + [''] def _str_param_list(self, name): out = [] if self[name]: out += self._str_field_list(name) out += [''] for param, param_type, desc in self[name]: out += self._str_indent(['**%s** : %s' % (param.strip(), param_type)]) out += [''] out += self._str_indent(desc, 8) out += [''] return out @property def _obj(self): if hasattr(self, '_cls'): return self._cls elif hasattr(self, '_f'): return self._f return None def _str_member_list(self, name): """ Generate a member listing, autosummary:: table where possible, and a table where not. """ out = [] if self[name]: out += ['.. rubric:: %s' % name, ''] prefix = getattr(self, '_name', '') if prefix: prefix = '~%s.' % prefix autosum = [] others = [] for param, param_type, desc in self[name]: param = param.strip() if not self._obj or hasattr(self._obj, param): autosum += [" %s%s" % (prefix, param)] else: others.append((param, param_type, desc)) if autosum: # GAEL: Toctree commented out below because it creates # hundreds of sphinx warnings # out += ['.. autosummary::', ' :toctree:', ''] out += ['.. autosummary::', ''] out += autosum if others: maxlen_0 = max([len(x[0]) for x in others]) maxlen_1 = max([len(x[1]) for x in others]) hdr = "=" * maxlen_0 + " " + "=" * maxlen_1 + " " + "=" * 10 fmt = '%%%ds %%%ds ' % (maxlen_0, maxlen_1) n_indent = maxlen_0 + maxlen_1 + 4 out += [hdr] for param, param_type, desc in others: out += [fmt % (param.strip(), param_type)] out += self._str_indent(desc, n_indent) out += [hdr] out += [''] return out def _str_section(self, name): out = [] if self[name]: out += self._str_header(name) out += [''] content = textwrap.dedent("\n".join(self[name])).split("\n") out += content out += [''] return out def _str_see_also(self, func_role): out = [] if self['See Also']: see_also = super(SphinxDocString, self)._str_see_also(func_role) out = ['.. seealso::', ''] out += self._str_indent(see_also[2:]) return out def _str_warnings(self): out = [] if self['Warnings']: out = ['.. warning::', ''] out += self._str_indent(self['Warnings']) return out def _str_index(self): idx = self['index'] out = [] if len(idx) == 0: return out out += ['.. index:: %s' % idx.get('default', '')] for section, references in idx.iteritems(): if section == 'default': continue elif section == 'refguide': out += [' single: %s' % (', '.join(references))] else: out += [' %s: %s' % (section, ','.join(references))] return out def _str_references(self): out = [] if self['References']: out += self._str_header('References') if isinstance(self['References'], str): self['References'] = [self['References']] out.extend(self['References']) out += [''] # Latex collects all references to a separate bibliography, # so we need to insert links to it import sphinx # local import to avoid test dependency if sphinx.__version__ >= "0.6": out += ['.. only:: latex', ''] else: out += ['.. latexonly::', ''] items = [] for line in self['References']: m = re.match(r'.. \[([a-z0-9._-]+)\]', line, re.I) if m: items.append(m.group(1)) out += [' ' + ", ".join(["[%s]_" % item for item in items]), ''] return out def _str_examples(self): examples_str = "\n".join(self['Examples']) if (self.use_plots and 'import matplotlib' in examples_str and 'plot::' not in examples_str): out = [] out += self._str_header('Examples') out += ['.. plot::', ''] out += self._str_indent(self['Examples']) out += [''] return out else: return self._str_section('Examples') def __str__(self, indent=0, func_role="obj"): out = [] out += self._str_signature() out += self._str_index() + [''] out += self._str_summary() out += self._str_extended_summary() for param_list in ('Parameters', 'Returns', 'Raises', 'Attributes'): out += self._str_param_list(param_list) out += self._str_warnings() out += self._str_see_also(func_role) out += self._str_section('Notes') out += self._str_references() out += self._str_examples() for param_list in ('Methods',): out += self._str_member_list(param_list) out = self._str_indent(out, indent) return '\n'.join(out) class SphinxFunctionDoc(SphinxDocString, FunctionDoc): def __init__(self, obj, doc=None, config={}): self.use_plots = config.get('use_plots', False) FunctionDoc.__init__(self, obj, doc=doc, config=config) class SphinxClassDoc(SphinxDocString, ClassDoc): def __init__(self, obj, doc=None, func_doc=None, config={}): self.use_plots = config.get('use_plots', False) ClassDoc.__init__(self, obj, doc=doc, func_doc=None, config=config) class SphinxObjDoc(SphinxDocString): def __init__(self, obj, doc=None, config=None): self._f = obj SphinxDocString.__init__(self, doc, config=config) def get_doc_object(obj, what=None, doc=None, config={}): if what is None: if inspect.isclass(obj): what = 'class' elif inspect.ismodule(obj): what = 'module' elif callable(obj): what = 'function' else: what = 'object' if what == 'class': return SphinxClassDoc(obj, func_doc=SphinxFunctionDoc, doc=doc, config=config) elif what in ('function', 'method'): return SphinxFunctionDoc(obj, doc=doc, config=config) else: if doc is None: doc = pydoc.getdoc(obj) return SphinxObjDoc(obj, doc, config=config)
bsd-3-clause
MJuddBooth/pandas
pandas/tests/frame/test_rank.py
1
11364
# -*- coding: utf-8 -*- from datetime import datetime, timedelta from distutils.version import LooseVersion import numpy as np import pytest from pandas import DataFrame, Series from pandas.tests.frame.common import TestData import pandas.util.testing as tm from pandas.util.testing import assert_frame_equal class TestRank(TestData): s = Series([1, 3, 4, 2, np.nan, 2, 1, 5, np.nan, 3]) df = DataFrame({'A': s, 'B': s}) results = { 'average': np.array([1.5, 5.5, 7.0, 3.5, np.nan, 3.5, 1.5, 8.0, np.nan, 5.5]), 'min': np.array([1, 5, 7, 3, np.nan, 3, 1, 8, np.nan, 5]), 'max': np.array([2, 6, 7, 4, np.nan, 4, 2, 8, np.nan, 6]), 'first': np.array([1, 5, 7, 3, np.nan, 4, 2, 8, np.nan, 6]), 'dense': np.array([1, 3, 4, 2, np.nan, 2, 1, 5, np.nan, 3]), } @pytest.fixture(params=['average', 'min', 'max', 'first', 'dense']) def method(self, request): """ Fixture for trying all rank methods """ return request.param def test_rank(self): rankdata = pytest.importorskip('scipy.stats.rankdata') self.frame['A'][::2] = np.nan self.frame['B'][::3] = np.nan self.frame['C'][::4] = np.nan self.frame['D'][::5] = np.nan ranks0 = self.frame.rank() ranks1 = self.frame.rank(1) mask = np.isnan(self.frame.values) fvals = self.frame.fillna(np.inf).values exp0 = np.apply_along_axis(rankdata, 0, fvals) exp0[mask] = np.nan exp1 = np.apply_along_axis(rankdata, 1, fvals) exp1[mask] = np.nan tm.assert_almost_equal(ranks0.values, exp0) tm.assert_almost_equal(ranks1.values, exp1) # integers df = DataFrame(np.random.randint(0, 5, size=40).reshape((10, 4))) result = df.rank() exp = df.astype(float).rank() tm.assert_frame_equal(result, exp) result = df.rank(1) exp = df.astype(float).rank(1) tm.assert_frame_equal(result, exp) def test_rank2(self): df = DataFrame([[1, 3, 2], [1, 2, 3]]) expected = DataFrame([[1.0, 3.0, 2.0], [1, 2, 3]]) / 3.0 result = df.rank(1, pct=True) tm.assert_frame_equal(result, expected) df = DataFrame([[1, 3, 2], [1, 2, 3]]) expected = df.rank(0) / 2.0 result = df.rank(0, pct=True) tm.assert_frame_equal(result, expected) df = DataFrame([['b', 'c', 'a'], ['a', 'c', 'b']]) expected = DataFrame([[2.0, 3.0, 1.0], [1, 3, 2]]) result = df.rank(1, numeric_only=False) tm.assert_frame_equal(result, expected) expected = DataFrame([[2.0, 1.5, 1.0], [1, 1.5, 2]]) result = df.rank(0, numeric_only=False) tm.assert_frame_equal(result, expected) df = DataFrame([['b', np.nan, 'a'], ['a', 'c', 'b']]) expected = DataFrame([[2.0, np.nan, 1.0], [1.0, 3.0, 2.0]]) result = df.rank(1, numeric_only=False) tm.assert_frame_equal(result, expected) expected = DataFrame([[2.0, np.nan, 1.0], [1.0, 1.0, 2.0]]) result = df.rank(0, numeric_only=False) tm.assert_frame_equal(result, expected) # f7u12, this does not work without extensive workaround data = [[datetime(2001, 1, 5), np.nan, datetime(2001, 1, 2)], [datetime(2000, 1, 2), datetime(2000, 1, 3), datetime(2000, 1, 1)]] df = DataFrame(data) # check the rank expected = DataFrame([[2., np.nan, 1.], [2., 3., 1.]]) result = df.rank(1, numeric_only=False, ascending=True) tm.assert_frame_equal(result, expected) expected = DataFrame([[1., np.nan, 2.], [2., 1., 3.]]) result = df.rank(1, numeric_only=False, ascending=False) tm.assert_frame_equal(result, expected) # mixed-type frames self.mixed_frame['datetime'] = datetime.now() self.mixed_frame['timedelta'] = timedelta(days=1, seconds=1) result = self.mixed_frame.rank(1) expected = self.mixed_frame.rank(1, numeric_only=True) tm.assert_frame_equal(result, expected) df = DataFrame({"a": [1e-20, -5, 1e-20 + 1e-40, 10, 1e60, 1e80, 1e-30]}) exp = DataFrame({"a": [3.5, 1., 3.5, 5., 6., 7., 2.]}) tm.assert_frame_equal(df.rank(), exp) def test_rank_na_option(self): rankdata = pytest.importorskip('scipy.stats.rankdata') self.frame['A'][::2] = np.nan self.frame['B'][::3] = np.nan self.frame['C'][::4] = np.nan self.frame['D'][::5] = np.nan # bottom ranks0 = self.frame.rank(na_option='bottom') ranks1 = self.frame.rank(1, na_option='bottom') fvals = self.frame.fillna(np.inf).values exp0 = np.apply_along_axis(rankdata, 0, fvals) exp1 = np.apply_along_axis(rankdata, 1, fvals) tm.assert_almost_equal(ranks0.values, exp0) tm.assert_almost_equal(ranks1.values, exp1) # top ranks0 = self.frame.rank(na_option='top') ranks1 = self.frame.rank(1, na_option='top') fval0 = self.frame.fillna((self.frame.min() - 1).to_dict()).values fval1 = self.frame.T fval1 = fval1.fillna((fval1.min() - 1).to_dict()).T fval1 = fval1.fillna(np.inf).values exp0 = np.apply_along_axis(rankdata, 0, fval0) exp1 = np.apply_along_axis(rankdata, 1, fval1) tm.assert_almost_equal(ranks0.values, exp0) tm.assert_almost_equal(ranks1.values, exp1) # descending # bottom ranks0 = self.frame.rank(na_option='top', ascending=False) ranks1 = self.frame.rank(1, na_option='top', ascending=False) fvals = self.frame.fillna(np.inf).values exp0 = np.apply_along_axis(rankdata, 0, -fvals) exp1 = np.apply_along_axis(rankdata, 1, -fvals) tm.assert_almost_equal(ranks0.values, exp0) tm.assert_almost_equal(ranks1.values, exp1) # descending # top ranks0 = self.frame.rank(na_option='bottom', ascending=False) ranks1 = self.frame.rank(1, na_option='bottom', ascending=False) fval0 = self.frame.fillna((self.frame.min() - 1).to_dict()).values fval1 = self.frame.T fval1 = fval1.fillna((fval1.min() - 1).to_dict()).T fval1 = fval1.fillna(np.inf).values exp0 = np.apply_along_axis(rankdata, 0, -fval0) exp1 = np.apply_along_axis(rankdata, 1, -fval1) tm.assert_numpy_array_equal(ranks0.values, exp0) tm.assert_numpy_array_equal(ranks1.values, exp1) # bad values throw error msg = "na_option must be one of 'keep', 'top', or 'bottom'" with pytest.raises(ValueError, match=msg): self.frame.rank(na_option='bad', ascending=False) # invalid type with pytest.raises(ValueError, match=msg): self.frame.rank(na_option=True, ascending=False) def test_rank_axis(self): # check if using axes' names gives the same result df = DataFrame([[2, 1], [4, 3]]) tm.assert_frame_equal(df.rank(axis=0), df.rank(axis='index')) tm.assert_frame_equal(df.rank(axis=1), df.rank(axis='columns')) def test_rank_methods_frame(self): pytest.importorskip('scipy.stats.special') rankdata = pytest.importorskip('scipy.stats.rankdata') import scipy xs = np.random.randint(0, 21, (100, 26)) xs = (xs - 10.0) / 10.0 cols = [chr(ord('z') - i) for i in range(xs.shape[1])] for vals in [xs, xs + 1e6, xs * 1e-6]: df = DataFrame(vals, columns=cols) for ax in [0, 1]: for m in ['average', 'min', 'max', 'first', 'dense']: result = df.rank(axis=ax, method=m) sprank = np.apply_along_axis( rankdata, ax, vals, m if m != 'first' else 'ordinal') sprank = sprank.astype(np.float64) expected = DataFrame(sprank, columns=cols) if (LooseVersion(scipy.__version__) >= LooseVersion('0.17.0')): expected = expected.astype('float64') tm.assert_frame_equal(result, expected) @pytest.mark.parametrize('dtype', ['O', 'f8', 'i8']) def test_rank_descending(self, method, dtype): if 'i' in dtype: df = self.df.dropna() else: df = self.df.astype(dtype) res = df.rank(ascending=False) expected = (df.max() - df).rank() assert_frame_equal(res, expected) if method == 'first' and dtype == 'O': return expected = (df.max() - df).rank(method=method) if dtype != 'O': res2 = df.rank(method=method, ascending=False, numeric_only=True) assert_frame_equal(res2, expected) res3 = df.rank(method=method, ascending=False, numeric_only=False) assert_frame_equal(res3, expected) @pytest.mark.parametrize('axis', [0, 1]) @pytest.mark.parametrize('dtype', [None, object]) def test_rank_2d_tie_methods(self, method, axis, dtype): df = self.df def _check2d(df, expected, method='average', axis=0): exp_df = DataFrame({'A': expected, 'B': expected}) if axis == 1: df = df.T exp_df = exp_df.T result = df.rank(method=method, axis=axis) assert_frame_equal(result, exp_df) disabled = {(object, 'first')} if (dtype, method) in disabled: return frame = df if dtype is None else df.astype(dtype) _check2d(frame, self.results[method], method=method, axis=axis) @pytest.mark.parametrize( "method,exp", [("dense", [[1., 1., 1.], [1., 0.5, 2. / 3], [1., 0.5, 1. / 3]]), ("min", [[1. / 3, 1., 1.], [1. / 3, 1. / 3, 2. / 3], [1. / 3, 1. / 3, 1. / 3]]), ("max", [[1., 1., 1.], [1., 2. / 3, 2. / 3], [1., 2. / 3, 1. / 3]]), ("average", [[2. / 3, 1., 1.], [2. / 3, 0.5, 2. / 3], [2. / 3, 0.5, 1. / 3]]), ("first", [[1. / 3, 1., 1.], [2. / 3, 1. / 3, 2. / 3], [3. / 3, 2. / 3, 1. / 3]])]) def test_rank_pct_true(self, method, exp): # see gh-15630. df = DataFrame([[2012, 66, 3], [2012, 65, 2], [2012, 65, 1]]) result = df.rank(method=method, pct=True) expected = DataFrame(exp) tm.assert_frame_equal(result, expected) @pytest.mark.single @pytest.mark.high_memory def test_pct_max_many_rows(self): # GH 18271 df = DataFrame({'A': np.arange(2**24 + 1), 'B': np.arange(2**24 + 1, 0, -1)}) result = df.rank(pct=True).max() assert (result == 1).all()
bsd-3-clause
frank-tancf/scikit-learn
sklearn/linear_model/tests/test_logistic.py
24
39507
import numpy as np import scipy.sparse as sp from scipy import linalg, optimize, sparse from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_warns from sklearn.utils.testing import assert_warns_message from sklearn.utils.testing import raises from sklearn.utils.testing import ignore_warnings from sklearn.utils.testing import assert_raise_message from sklearn.exceptions import ConvergenceWarning from sklearn.utils import compute_class_weight from sklearn.utils.fixes import sp_version from sklearn.linear_model.logistic import ( LogisticRegression, logistic_regression_path, LogisticRegressionCV, _logistic_loss_and_grad, _logistic_grad_hess, _multinomial_grad_hess, _logistic_loss, ) from sklearn.model_selection import StratifiedKFold from sklearn.datasets import load_iris, make_classification from sklearn.metrics import log_loss X = [[-1, 0], [0, 1], [1, 1]] X_sp = sp.csr_matrix(X) Y1 = [0, 1, 1] Y2 = [2, 1, 0] iris = load_iris() def check_predictions(clf, X, y): """Check that the model is able to fit the classification data""" n_samples = len(y) classes = np.unique(y) n_classes = classes.shape[0] predicted = clf.fit(X, y).predict(X) assert_array_equal(clf.classes_, classes) assert_equal(predicted.shape, (n_samples,)) assert_array_equal(predicted, y) probabilities = clf.predict_proba(X) assert_equal(probabilities.shape, (n_samples, n_classes)) assert_array_almost_equal(probabilities.sum(axis=1), np.ones(n_samples)) assert_array_equal(probabilities.argmax(axis=1), y) def test_predict_2_classes(): # Simple sanity check on a 2 classes dataset # Make sure it predicts the correct result on simple datasets. check_predictions(LogisticRegression(random_state=0), X, Y1) check_predictions(LogisticRegression(random_state=0), X_sp, Y1) check_predictions(LogisticRegression(C=100, random_state=0), X, Y1) check_predictions(LogisticRegression(C=100, random_state=0), X_sp, Y1) check_predictions(LogisticRegression(fit_intercept=False, random_state=0), X, Y1) check_predictions(LogisticRegression(fit_intercept=False, random_state=0), X_sp, Y1) def test_error(): # Test for appropriate exception on errors msg = "Penalty term must be positive" assert_raise_message(ValueError, msg, LogisticRegression(C=-1).fit, X, Y1) assert_raise_message(ValueError, msg, LogisticRegression(C="test").fit, X, Y1) for LR in [LogisticRegression, LogisticRegressionCV]: msg = "Tolerance for stopping criteria must be positive" assert_raise_message(ValueError, msg, LR(tol=-1).fit, X, Y1) assert_raise_message(ValueError, msg, LR(tol="test").fit, X, Y1) msg = "Maximum number of iteration must be positive" assert_raise_message(ValueError, msg, LR(max_iter=-1).fit, X, Y1) assert_raise_message(ValueError, msg, LR(max_iter="test").fit, X, Y1) def test_predict_3_classes(): check_predictions(LogisticRegression(C=10), X, Y2) check_predictions(LogisticRegression(C=10), X_sp, Y2) def test_predict_iris(): # Test logistic regression with the iris dataset n_samples, n_features = iris.data.shape target = iris.target_names[iris.target] # Test that both multinomial and OvR solvers handle # multiclass data correctly and give good accuracy # score (>0.95) for the training data. for clf in [LogisticRegression(C=len(iris.data)), LogisticRegression(C=len(iris.data), solver='lbfgs', multi_class='multinomial'), LogisticRegression(C=len(iris.data), solver='newton-cg', multi_class='multinomial'), LogisticRegression(C=len(iris.data), solver='sag', tol=1e-2, multi_class='ovr', random_state=42)]: clf.fit(iris.data, target) assert_array_equal(np.unique(target), clf.classes_) pred = clf.predict(iris.data) assert_greater(np.mean(pred == target), .95) probabilities = clf.predict_proba(iris.data) assert_array_almost_equal(probabilities.sum(axis=1), np.ones(n_samples)) pred = iris.target_names[probabilities.argmax(axis=1)] assert_greater(np.mean(pred == target), .95) def test_multinomial_validation(): for solver in ['lbfgs', 'newton-cg', 'sag']: lr = LogisticRegression(C=-1, solver=solver, multi_class='multinomial') assert_raises(ValueError, lr.fit, [[0, 1], [1, 0]], [0, 1]) def test_check_solver_option(): X, y = iris.data, iris.target for LR in [LogisticRegression, LogisticRegressionCV]: msg = ("Logistic Regression supports only liblinear, newton-cg, lbfgs" " and sag solvers, got wrong_name") lr = LR(solver="wrong_name") assert_raise_message(ValueError, msg, lr.fit, X, y) msg = "multi_class should be either multinomial or ovr, got wrong_name" lr = LR(solver='newton-cg', multi_class="wrong_name") assert_raise_message(ValueError, msg, lr.fit, X, y) # only 'liblinear' solver msg = "Solver liblinear does not support a multinomial backend." lr = LR(solver='liblinear', multi_class='multinomial') assert_raise_message(ValueError, msg, lr.fit, X, y) # all solvers except 'liblinear' for solver in ['newton-cg', 'lbfgs', 'sag']: msg = ("Solver %s supports only l2 penalties, got l1 penalty." % solver) lr = LR(solver=solver, penalty='l1') assert_raise_message(ValueError, msg, lr.fit, X, y) msg = ("Solver %s supports only dual=False, got dual=True" % solver) lr = LR(solver=solver, dual=True) assert_raise_message(ValueError, msg, lr.fit, X, y) def test_multinomial_binary(): # Test multinomial LR on a binary problem. target = (iris.target > 0).astype(np.intp) target = np.array(["setosa", "not-setosa"])[target] for solver in ['lbfgs', 'newton-cg', 'sag']: clf = LogisticRegression(solver=solver, multi_class='multinomial', random_state=42, max_iter=2000) clf.fit(iris.data, target) assert_equal(clf.coef_.shape, (1, iris.data.shape[1])) assert_equal(clf.intercept_.shape, (1,)) assert_array_equal(clf.predict(iris.data), target) mlr = LogisticRegression(solver=solver, multi_class='multinomial', random_state=42, fit_intercept=False) mlr.fit(iris.data, target) pred = clf.classes_[np.argmax(clf.predict_log_proba(iris.data), axis=1)] assert_greater(np.mean(pred == target), .9) def test_sparsify(): # Test sparsify and densify members. n_samples, n_features = iris.data.shape target = iris.target_names[iris.target] clf = LogisticRegression(random_state=0).fit(iris.data, target) pred_d_d = clf.decision_function(iris.data) clf.sparsify() assert_true(sp.issparse(clf.coef_)) pred_s_d = clf.decision_function(iris.data) sp_data = sp.coo_matrix(iris.data) pred_s_s = clf.decision_function(sp_data) clf.densify() pred_d_s = clf.decision_function(sp_data) assert_array_almost_equal(pred_d_d, pred_s_d) assert_array_almost_equal(pred_d_d, pred_s_s) assert_array_almost_equal(pred_d_d, pred_d_s) def test_inconsistent_input(): # Test that an exception is raised on inconsistent input rng = np.random.RandomState(0) X_ = rng.random_sample((5, 10)) y_ = np.ones(X_.shape[0]) y_[0] = 0 clf = LogisticRegression(random_state=0) # Wrong dimensions for training data y_wrong = y_[:-1] assert_raises(ValueError, clf.fit, X, y_wrong) # Wrong dimensions for test data assert_raises(ValueError, clf.fit(X_, y_).predict, rng.random_sample((3, 12))) def test_write_parameters(): # Test that we can write to coef_ and intercept_ clf = LogisticRegression(random_state=0) clf.fit(X, Y1) clf.coef_[:] = 0 clf.intercept_[:] = 0 assert_array_almost_equal(clf.decision_function(X), 0) @raises(ValueError) def test_nan(): # Test proper NaN handling. # Regression test for Issue #252: fit used to go into an infinite loop. Xnan = np.array(X, dtype=np.float64) Xnan[0, 1] = np.nan LogisticRegression(random_state=0).fit(Xnan, Y1) def test_consistency_path(): # Test that the path algorithm is consistent rng = np.random.RandomState(0) X = np.concatenate((rng.randn(100, 2) + [1, 1], rng.randn(100, 2))) y = [1] * 100 + [-1] * 100 Cs = np.logspace(0, 4, 10) f = ignore_warnings # can't test with fit_intercept=True since LIBLINEAR # penalizes the intercept for solver in ('lbfgs', 'newton-cg', 'liblinear', 'sag'): coefs, Cs, _ = f(logistic_regression_path)( X, y, Cs=Cs, fit_intercept=False, tol=1e-5, solver=solver, random_state=0) for i, C in enumerate(Cs): lr = LogisticRegression(C=C, fit_intercept=False, tol=1e-5, random_state=0) lr.fit(X, y) lr_coef = lr.coef_.ravel() assert_array_almost_equal(lr_coef, coefs[i], decimal=4, err_msg="with solver = %s" % solver) # test for fit_intercept=True for solver in ('lbfgs', 'newton-cg', 'liblinear', 'sag'): Cs = [1e3] coefs, Cs, _ = f(logistic_regression_path)( X, y, Cs=Cs, fit_intercept=True, tol=1e-6, solver=solver, intercept_scaling=10000., random_state=0) lr = LogisticRegression(C=Cs[0], fit_intercept=True, tol=1e-4, intercept_scaling=10000., random_state=0) lr.fit(X, y) lr_coef = np.concatenate([lr.coef_.ravel(), lr.intercept_]) assert_array_almost_equal(lr_coef, coefs[0], decimal=4, err_msg="with solver = %s" % solver) def test_liblinear_dual_random_state(): # random_state is relevant for liblinear solver only if dual=True X, y = make_classification(n_samples=20) lr1 = LogisticRegression(random_state=0, dual=True, max_iter=1, tol=1e-15) lr1.fit(X, y) lr2 = LogisticRegression(random_state=0, dual=True, max_iter=1, tol=1e-15) lr2.fit(X, y) lr3 = LogisticRegression(random_state=8, dual=True, max_iter=1, tol=1e-15) lr3.fit(X, y) # same result for same random state assert_array_almost_equal(lr1.coef_, lr2.coef_) # different results for different random states msg = "Arrays are not almost equal to 6 decimals" assert_raise_message(AssertionError, msg, assert_array_almost_equal, lr1.coef_, lr3.coef_) def test_logistic_loss_and_grad(): X_ref, y = make_classification(n_samples=20) n_features = X_ref.shape[1] X_sp = X_ref.copy() X_sp[X_sp < .1] = 0 X_sp = sp.csr_matrix(X_sp) for X in (X_ref, X_sp): w = np.zeros(n_features) # First check that our derivation of the grad is correct loss, grad = _logistic_loss_and_grad(w, X, y, alpha=1.) approx_grad = optimize.approx_fprime( w, lambda w: _logistic_loss_and_grad(w, X, y, alpha=1.)[0], 1e-3 ) assert_array_almost_equal(grad, approx_grad, decimal=2) # Second check that our intercept implementation is good w = np.zeros(n_features + 1) loss_interp, grad_interp = _logistic_loss_and_grad( w, X, y, alpha=1. ) assert_array_almost_equal(loss, loss_interp) approx_grad = optimize.approx_fprime( w, lambda w: _logistic_loss_and_grad(w, X, y, alpha=1.)[0], 1e-3 ) assert_array_almost_equal(grad_interp, approx_grad, decimal=2) def test_logistic_grad_hess(): rng = np.random.RandomState(0) n_samples, n_features = 50, 5 X_ref = rng.randn(n_samples, n_features) y = np.sign(X_ref.dot(5 * rng.randn(n_features))) X_ref -= X_ref.mean() X_ref /= X_ref.std() X_sp = X_ref.copy() X_sp[X_sp < .1] = 0 X_sp = sp.csr_matrix(X_sp) for X in (X_ref, X_sp): w = .1 * np.ones(n_features) # First check that _logistic_grad_hess is consistent # with _logistic_loss_and_grad loss, grad = _logistic_loss_and_grad(w, X, y, alpha=1.) grad_2, hess = _logistic_grad_hess(w, X, y, alpha=1.) assert_array_almost_equal(grad, grad_2) # Now check our hessian along the second direction of the grad vector = np.zeros_like(grad) vector[1] = 1 hess_col = hess(vector) # Computation of the Hessian is particularly fragile to numerical # errors when doing simple finite differences. Here we compute the # grad along a path in the direction of the vector and then use a # least-square regression to estimate the slope e = 1e-3 d_x = np.linspace(-e, e, 30) d_grad = np.array([ _logistic_loss_and_grad(w + t * vector, X, y, alpha=1.)[1] for t in d_x ]) d_grad -= d_grad.mean(axis=0) approx_hess_col = linalg.lstsq(d_x[:, np.newaxis], d_grad)[0].ravel() assert_array_almost_equal(approx_hess_col, hess_col, decimal=3) # Second check that our intercept implementation is good w = np.zeros(n_features + 1) loss_interp, grad_interp = _logistic_loss_and_grad(w, X, y, alpha=1.) loss_interp_2 = _logistic_loss(w, X, y, alpha=1.) grad_interp_2, hess = _logistic_grad_hess(w, X, y, alpha=1.) assert_array_almost_equal(loss_interp, loss_interp_2) assert_array_almost_equal(grad_interp, grad_interp_2) def test_logistic_cv(): # test for LogisticRegressionCV object n_samples, n_features = 50, 5 rng = np.random.RandomState(0) X_ref = rng.randn(n_samples, n_features) y = np.sign(X_ref.dot(5 * rng.randn(n_features))) X_ref -= X_ref.mean() X_ref /= X_ref.std() lr_cv = LogisticRegressionCV(Cs=[1.], fit_intercept=False, solver='liblinear') lr_cv.fit(X_ref, y) lr = LogisticRegression(C=1., fit_intercept=False) lr.fit(X_ref, y) assert_array_almost_equal(lr.coef_, lr_cv.coef_) assert_array_equal(lr_cv.coef_.shape, (1, n_features)) assert_array_equal(lr_cv.classes_, [-1, 1]) assert_equal(len(lr_cv.classes_), 2) coefs_paths = np.asarray(list(lr_cv.coefs_paths_.values())) assert_array_equal(coefs_paths.shape, (1, 3, 1, n_features)) assert_array_equal(lr_cv.Cs_.shape, (1, )) scores = np.asarray(list(lr_cv.scores_.values())) assert_array_equal(scores.shape, (1, 3, 1)) def test_logistic_cv_sparse(): X, y = make_classification(n_samples=50, n_features=5, random_state=0) X[X < 1.0] = 0.0 csr = sp.csr_matrix(X) clf = LogisticRegressionCV(fit_intercept=True) clf.fit(X, y) clfs = LogisticRegressionCV(fit_intercept=True) clfs.fit(csr, y) assert_array_almost_equal(clfs.coef_, clf.coef_) assert_array_almost_equal(clfs.intercept_, clf.intercept_) assert_equal(clfs.C_, clf.C_) def test_intercept_logistic_helper(): n_samples, n_features = 10, 5 X, y = make_classification(n_samples=n_samples, n_features=n_features, random_state=0) # Fit intercept case. alpha = 1. w = np.ones(n_features + 1) grad_interp, hess_interp = _logistic_grad_hess(w, X, y, alpha) loss_interp = _logistic_loss(w, X, y, alpha) # Do not fit intercept. This can be considered equivalent to adding # a feature vector of ones, i.e column of one vectors. X_ = np.hstack((X, np.ones(10)[:, np.newaxis])) grad, hess = _logistic_grad_hess(w, X_, y, alpha) loss = _logistic_loss(w, X_, y, alpha) # In the fit_intercept=False case, the feature vector of ones is # penalized. This should be taken care of. assert_almost_equal(loss_interp + 0.5 * (w[-1] ** 2), loss) # Check gradient. assert_array_almost_equal(grad_interp[:n_features], grad[:n_features]) assert_almost_equal(grad_interp[-1] + alpha * w[-1], grad[-1]) rng = np.random.RandomState(0) grad = rng.rand(n_features + 1) hess_interp = hess_interp(grad) hess = hess(grad) assert_array_almost_equal(hess_interp[:n_features], hess[:n_features]) assert_almost_equal(hess_interp[-1] + alpha * grad[-1], hess[-1]) def test_ovr_multinomial_iris(): # Test that OvR and multinomial are correct using the iris dataset. train, target = iris.data, iris.target n_samples, n_features = train.shape # The cv indices from stratified kfold (where stratification is done based # on the fine-grained iris classes, i.e, before the classes 0 and 1 are # conflated) is used for both clf and clf1 n_cv = 2 cv = StratifiedKFold(n_cv) precomputed_folds = list(cv.split(train, target)) # Train clf on the original dataset where classes 0 and 1 are separated clf = LogisticRegressionCV(cv=precomputed_folds) clf.fit(train, target) # Conflate classes 0 and 1 and train clf1 on this modified dataset clf1 = LogisticRegressionCV(cv=precomputed_folds) target_copy = target.copy() target_copy[target_copy == 0] = 1 clf1.fit(train, target_copy) # Ensure that what OvR learns for class2 is same regardless of whether # classes 0 and 1 are separated or not assert_array_almost_equal(clf.scores_[2], clf1.scores_[2]) assert_array_almost_equal(clf.intercept_[2:], clf1.intercept_) assert_array_almost_equal(clf.coef_[2][np.newaxis, :], clf1.coef_) # Test the shape of various attributes. assert_equal(clf.coef_.shape, (3, n_features)) assert_array_equal(clf.classes_, [0, 1, 2]) coefs_paths = np.asarray(list(clf.coefs_paths_.values())) assert_array_almost_equal(coefs_paths.shape, (3, n_cv, 10, n_features + 1)) assert_equal(clf.Cs_.shape, (10, )) scores = np.asarray(list(clf.scores_.values())) assert_equal(scores.shape, (3, n_cv, 10)) # Test that for the iris data multinomial gives a better accuracy than OvR for solver in ['lbfgs', 'newton-cg', 'sag']: max_iter = 100 if solver == 'sag' else 15 clf_multi = LogisticRegressionCV( solver=solver, multi_class='multinomial', max_iter=max_iter, random_state=42, tol=1e-2, cv=2) clf_multi.fit(train, target) multi_score = clf_multi.score(train, target) ovr_score = clf.score(train, target) assert_greater(multi_score, ovr_score) # Test attributes of LogisticRegressionCV assert_equal(clf.coef_.shape, clf_multi.coef_.shape) assert_array_equal(clf_multi.classes_, [0, 1, 2]) coefs_paths = np.asarray(list(clf_multi.coefs_paths_.values())) assert_array_almost_equal(coefs_paths.shape, (3, n_cv, 10, n_features + 1)) assert_equal(clf_multi.Cs_.shape, (10, )) scores = np.asarray(list(clf_multi.scores_.values())) assert_equal(scores.shape, (3, n_cv, 10)) def test_logistic_regression_solvers(): X, y = make_classification(n_features=10, n_informative=5, random_state=0) ncg = LogisticRegression(solver='newton-cg', fit_intercept=False) lbf = LogisticRegression(solver='lbfgs', fit_intercept=False) lib = LogisticRegression(fit_intercept=False) sag = LogisticRegression(solver='sag', fit_intercept=False, random_state=42) ncg.fit(X, y) lbf.fit(X, y) sag.fit(X, y) lib.fit(X, y) assert_array_almost_equal(ncg.coef_, lib.coef_, decimal=3) assert_array_almost_equal(lib.coef_, lbf.coef_, decimal=3) assert_array_almost_equal(ncg.coef_, lbf.coef_, decimal=3) assert_array_almost_equal(sag.coef_, lib.coef_, decimal=3) assert_array_almost_equal(sag.coef_, ncg.coef_, decimal=3) assert_array_almost_equal(sag.coef_, lbf.coef_, decimal=3) def test_logistic_regression_solvers_multiclass(): X, y = make_classification(n_samples=20, n_features=20, n_informative=10, n_classes=3, random_state=0) tol = 1e-6 ncg = LogisticRegression(solver='newton-cg', fit_intercept=False, tol=tol) lbf = LogisticRegression(solver='lbfgs', fit_intercept=False, tol=tol) lib = LogisticRegression(fit_intercept=False, tol=tol) sag = LogisticRegression(solver='sag', fit_intercept=False, tol=tol, max_iter=1000, random_state=42) ncg.fit(X, y) lbf.fit(X, y) sag.fit(X, y) lib.fit(X, y) assert_array_almost_equal(ncg.coef_, lib.coef_, decimal=4) assert_array_almost_equal(lib.coef_, lbf.coef_, decimal=4) assert_array_almost_equal(ncg.coef_, lbf.coef_, decimal=4) assert_array_almost_equal(sag.coef_, lib.coef_, decimal=4) assert_array_almost_equal(sag.coef_, ncg.coef_, decimal=4) assert_array_almost_equal(sag.coef_, lbf.coef_, decimal=4) def test_logistic_regressioncv_class_weights(): X, y = make_classification(n_samples=20, n_features=20, n_informative=10, n_classes=3, random_state=0) msg = ("In LogisticRegressionCV the liblinear solver cannot handle " "multiclass with class_weight of type dict. Use the lbfgs, " "newton-cg or sag solvers or set class_weight='balanced'") clf_lib = LogisticRegressionCV(class_weight={0: 0.1, 1: 0.2}, solver='liblinear') assert_raise_message(ValueError, msg, clf_lib.fit, X, y) y_ = y.copy() y_[y == 2] = 1 clf_lib.fit(X, y_) assert_array_equal(clf_lib.classes_, [0, 1]) # Test for class_weight=balanced X, y = make_classification(n_samples=20, n_features=20, n_informative=10, random_state=0) clf_lbf = LogisticRegressionCV(solver='lbfgs', fit_intercept=False, class_weight='balanced') clf_lbf.fit(X, y) clf_lib = LogisticRegressionCV(solver='liblinear', fit_intercept=False, class_weight='balanced') clf_lib.fit(X, y) clf_sag = LogisticRegressionCV(solver='sag', fit_intercept=False, class_weight='balanced', max_iter=2000) clf_sag.fit(X, y) assert_array_almost_equal(clf_lib.coef_, clf_lbf.coef_, decimal=4) assert_array_almost_equal(clf_sag.coef_, clf_lbf.coef_, decimal=4) assert_array_almost_equal(clf_lib.coef_, clf_sag.coef_, decimal=4) def test_logistic_regression_sample_weights(): X, y = make_classification(n_samples=20, n_features=5, n_informative=3, n_classes=2, random_state=0) sample_weight = y + 1 for LR in [LogisticRegression, LogisticRegressionCV]: # Test that passing sample_weight as ones is the same as # not passing them at all (default None) for solver in ['lbfgs', 'liblinear']: clf_sw_none = LR(solver=solver, fit_intercept=False) clf_sw_none.fit(X, y) clf_sw_ones = LR(solver=solver, fit_intercept=False) clf_sw_ones.fit(X, y, sample_weight=np.ones(y.shape[0])) assert_array_almost_equal( clf_sw_none.coef_, clf_sw_ones.coef_, decimal=4) # Test that sample weights work the same with the lbfgs, # newton-cg, and 'sag' solvers clf_sw_lbfgs = LR(solver='lbfgs', fit_intercept=False) clf_sw_lbfgs.fit(X, y, sample_weight=sample_weight) clf_sw_n = LR(solver='newton-cg', fit_intercept=False) clf_sw_n.fit(X, y, sample_weight=sample_weight) clf_sw_sag = LR(solver='sag', fit_intercept=False, tol=1e-10) # ignore convergence warning due to small dataset with ignore_warnings(): clf_sw_sag.fit(X, y, sample_weight=sample_weight) clf_sw_liblinear = LR(solver='liblinear', fit_intercept=False) clf_sw_liblinear.fit(X, y, sample_weight=sample_weight) assert_array_almost_equal( clf_sw_lbfgs.coef_, clf_sw_n.coef_, decimal=4) assert_array_almost_equal( clf_sw_lbfgs.coef_, clf_sw_sag.coef_, decimal=4) assert_array_almost_equal( clf_sw_lbfgs.coef_, clf_sw_liblinear.coef_, decimal=4) # Test that passing class_weight as [1,2] is the same as # passing class weight = [1,1] but adjusting sample weights # to be 2 for all instances of class 2 for solver in ['lbfgs', 'liblinear']: clf_cw_12 = LR(solver=solver, fit_intercept=False, class_weight={0: 1, 1: 2}) clf_cw_12.fit(X, y) clf_sw_12 = LR(solver=solver, fit_intercept=False) clf_sw_12.fit(X, y, sample_weight=sample_weight) assert_array_almost_equal( clf_cw_12.coef_, clf_sw_12.coef_, decimal=4) # Test the above for l1 penalty and l2 penalty with dual=True. # since the patched liblinear code is different. clf_cw = LogisticRegression( solver="liblinear", fit_intercept=False, class_weight={0: 1, 1: 2}, penalty="l1") clf_cw.fit(X, y) clf_sw = LogisticRegression( solver="liblinear", fit_intercept=False, penalty="l1") clf_sw.fit(X, y, sample_weight) assert_array_almost_equal(clf_cw.coef_, clf_sw.coef_, decimal=4) clf_cw = LogisticRegression( solver="liblinear", fit_intercept=False, class_weight={0: 1, 1: 2}, penalty="l2", dual=True) clf_cw.fit(X, y) clf_sw = LogisticRegression( solver="liblinear", fit_intercept=False, penalty="l2", dual=True) clf_sw.fit(X, y, sample_weight) assert_array_almost_equal(clf_cw.coef_, clf_sw.coef_, decimal=4) def _compute_class_weight_dictionary(y): # helper for returning a dictionary instead of an array classes = np.unique(y) class_weight = compute_class_weight("balanced", classes, y) class_weight_dict = dict(zip(classes, class_weight)) return class_weight_dict def test_logistic_regression_class_weights(): # Multinomial case: remove 90% of class 0 X = iris.data[45:, :] y = iris.target[45:] solvers = ("lbfgs", "newton-cg") class_weight_dict = _compute_class_weight_dictionary(y) for solver in solvers: clf1 = LogisticRegression(solver=solver, multi_class="multinomial", class_weight="balanced") clf2 = LogisticRegression(solver=solver, multi_class="multinomial", class_weight=class_weight_dict) clf1.fit(X, y) clf2.fit(X, y) assert_array_almost_equal(clf1.coef_, clf2.coef_, decimal=4) # Binary case: remove 90% of class 0 and 100% of class 2 X = iris.data[45:100, :] y = iris.target[45:100] solvers = ("lbfgs", "newton-cg", "liblinear") class_weight_dict = _compute_class_weight_dictionary(y) for solver in solvers: clf1 = LogisticRegression(solver=solver, multi_class="ovr", class_weight="balanced") clf2 = LogisticRegression(solver=solver, multi_class="ovr", class_weight=class_weight_dict) clf1.fit(X, y) clf2.fit(X, y) assert_array_almost_equal(clf1.coef_, clf2.coef_, decimal=6) def test_multinomial_logistic_regression_with_classweight_auto(): X, y = iris.data, iris.target model = LogisticRegression(multi_class='multinomial', class_weight='auto', solver='lbfgs') # 'auto' is deprecated and will be removed in 0.19 assert_warns_message(DeprecationWarning, "class_weight='auto' heuristic is deprecated", model.fit, X, y) def test_logistic_regression_convergence_warnings(): # Test that warnings are raised if model does not converge X, y = make_classification(n_samples=20, n_features=20) clf_lib = LogisticRegression(solver='liblinear', max_iter=2, verbose=1) assert_warns(ConvergenceWarning, clf_lib.fit, X, y) assert_equal(clf_lib.n_iter_, 2) def test_logistic_regression_multinomial(): # Tests for the multinomial option in logistic regression # Some basic attributes of Logistic Regression n_samples, n_features, n_classes = 50, 20, 3 X, y = make_classification(n_samples=n_samples, n_features=n_features, n_informative=10, n_classes=n_classes, random_state=0) # 'lbfgs' is used as a referenced solver = 'lbfgs' ref_i = LogisticRegression(solver=solver, multi_class='multinomial') ref_w = LogisticRegression(solver=solver, multi_class='multinomial', fit_intercept=False) ref_i.fit(X, y) ref_w.fit(X, y) assert_array_equal(ref_i.coef_.shape, (n_classes, n_features)) assert_array_equal(ref_w.coef_.shape, (n_classes, n_features)) for solver in ['sag', 'newton-cg']: clf_i = LogisticRegression(solver=solver, multi_class='multinomial', random_state=42, max_iter=1000, tol=1e-6) clf_w = LogisticRegression(solver=solver, multi_class='multinomial', random_state=42, max_iter=1000, tol=1e-6, fit_intercept=False) clf_i.fit(X, y) clf_w.fit(X, y) assert_array_equal(clf_i.coef_.shape, (n_classes, n_features)) assert_array_equal(clf_w.coef_.shape, (n_classes, n_features)) # Compare solutions between lbfgs and the other solvers assert_almost_equal(ref_i.coef_, clf_i.coef_, decimal=3) assert_almost_equal(ref_w.coef_, clf_w.coef_, decimal=3) assert_almost_equal(ref_i.intercept_, clf_i.intercept_, decimal=3) # Test that the path give almost the same results. However since in this # case we take the average of the coefs after fitting across all the # folds, it need not be exactly the same. for solver in ['lbfgs', 'newton-cg', 'sag']: clf_path = LogisticRegressionCV(solver=solver, max_iter=2000, tol=1e-6, multi_class='multinomial', Cs=[1.]) clf_path.fit(X, y) assert_array_almost_equal(clf_path.coef_, ref_i.coef_, decimal=3) assert_almost_equal(clf_path.intercept_, ref_i.intercept_, decimal=3) def test_multinomial_grad_hess(): rng = np.random.RandomState(0) n_samples, n_features, n_classes = 100, 5, 3 X = rng.randn(n_samples, n_features) w = rng.rand(n_classes, n_features) Y = np.zeros((n_samples, n_classes)) ind = np.argmax(np.dot(X, w.T), axis=1) Y[range(0, n_samples), ind] = 1 w = w.ravel() sample_weights = np.ones(X.shape[0]) grad, hessp = _multinomial_grad_hess(w, X, Y, alpha=1., sample_weight=sample_weights) # extract first column of hessian matrix vec = np.zeros(n_features * n_classes) vec[0] = 1 hess_col = hessp(vec) # Estimate hessian using least squares as done in # test_logistic_grad_hess e = 1e-3 d_x = np.linspace(-e, e, 30) d_grad = np.array([ _multinomial_grad_hess(w + t * vec, X, Y, alpha=1., sample_weight=sample_weights)[0] for t in d_x ]) d_grad -= d_grad.mean(axis=0) approx_hess_col = linalg.lstsq(d_x[:, np.newaxis], d_grad)[0].ravel() assert_array_almost_equal(hess_col, approx_hess_col) def test_liblinear_decision_function_zero(): # Test negative prediction when decision_function values are zero. # Liblinear predicts the positive class when decision_function values # are zero. This is a test to verify that we do not do the same. # See Issue: https://github.com/scikit-learn/scikit-learn/issues/3600 # and the PR https://github.com/scikit-learn/scikit-learn/pull/3623 X, y = make_classification(n_samples=5, n_features=5) clf = LogisticRegression(fit_intercept=False) clf.fit(X, y) # Dummy data such that the decision function becomes zero. X = np.zeros((5, 5)) assert_array_equal(clf.predict(X), np.zeros(5)) def test_liblinear_logregcv_sparse(): # Test LogRegCV with solver='liblinear' works for sparse matrices X, y = make_classification(n_samples=10, n_features=5) clf = LogisticRegressionCV(solver='liblinear') clf.fit(sparse.csr_matrix(X), y) def test_logreg_intercept_scaling(): # Test that the right error message is thrown when intercept_scaling <= 0 for i in [-1, 0]: clf = LogisticRegression(intercept_scaling=i) msg = ('Intercept scaling is %r but needs to be greater than 0.' ' To disable fitting an intercept,' ' set fit_intercept=False.' % clf.intercept_scaling) assert_raise_message(ValueError, msg, clf.fit, X, Y1) def test_logreg_intercept_scaling_zero(): # Test that intercept_scaling is ignored when fit_intercept is False clf = LogisticRegression(fit_intercept=False) clf.fit(X, Y1) assert_equal(clf.intercept_, 0.) def test_logreg_cv_penalty(): # Test that the correct penalty is passed to the final fit. X, y = make_classification(n_samples=50, n_features=20, random_state=0) lr_cv = LogisticRegressionCV(penalty="l1", Cs=[1.0], solver='liblinear') lr_cv.fit(X, y) lr = LogisticRegression(penalty="l1", C=1.0, solver='liblinear') lr.fit(X, y) assert_equal(np.count_nonzero(lr_cv.coef_), np.count_nonzero(lr.coef_)) def test_logreg_predict_proba_multinomial(): X, y = make_classification(n_samples=10, n_features=20, random_state=0, n_classes=3, n_informative=10) # Predicted probabilites using the true-entropy loss should give a # smaller loss than those using the ovr method. clf_multi = LogisticRegression(multi_class="multinomial", solver="lbfgs") clf_multi.fit(X, y) clf_multi_loss = log_loss(y, clf_multi.predict_proba(X)) clf_ovr = LogisticRegression(multi_class="ovr", solver="lbfgs") clf_ovr.fit(X, y) clf_ovr_loss = log_loss(y, clf_ovr.predict_proba(X)) assert_greater(clf_ovr_loss, clf_multi_loss) # Predicted probabilites using the soft-max function should give a # smaller loss than those using the logistic function. clf_multi_loss = log_loss(y, clf_multi.predict_proba(X)) clf_wrong_loss = log_loss(y, clf_multi._predict_proba_lr(X)) assert_greater(clf_wrong_loss, clf_multi_loss) @ignore_warnings def test_max_iter(): # Test that the maximum number of iteration is reached X, y_bin = iris.data, iris.target.copy() y_bin[y_bin == 2] = 0 solvers = ['newton-cg', 'liblinear', 'sag'] # old scipy doesn't have maxiter if sp_version >= (0, 12): solvers.append('lbfgs') for max_iter in range(1, 5): for solver in solvers: for multi_class in ['ovr', 'multinomial']: if solver == 'liblinear' and multi_class == 'multinomial': continue lr = LogisticRegression(max_iter=max_iter, tol=1e-15, multi_class=multi_class, random_state=0, solver=solver) lr.fit(X, y_bin) assert_equal(lr.n_iter_[0], max_iter) def test_n_iter(): # Test that self.n_iter_ has the correct format. X, y = iris.data, iris.target y_bin = y.copy() y_bin[y_bin == 2] = 0 n_Cs = 4 n_cv_fold = 2 for solver in ['newton-cg', 'liblinear', 'sag', 'lbfgs']: # OvR case n_classes = 1 if solver == 'liblinear' else np.unique(y).shape[0] clf = LogisticRegression(tol=1e-2, multi_class='ovr', solver=solver, C=1., random_state=42, max_iter=100) clf.fit(X, y) assert_equal(clf.n_iter_.shape, (n_classes,)) n_classes = np.unique(y).shape[0] clf = LogisticRegressionCV(tol=1e-2, multi_class='ovr', solver=solver, Cs=n_Cs, cv=n_cv_fold, random_state=42, max_iter=100) clf.fit(X, y) assert_equal(clf.n_iter_.shape, (n_classes, n_cv_fold, n_Cs)) clf.fit(X, y_bin) assert_equal(clf.n_iter_.shape, (1, n_cv_fold, n_Cs)) # multinomial case n_classes = 1 if solver in ('liblinear', 'sag'): break clf = LogisticRegression(tol=1e-2, multi_class='multinomial', solver=solver, C=1., random_state=42, max_iter=100) clf.fit(X, y) assert_equal(clf.n_iter_.shape, (n_classes,)) clf = LogisticRegressionCV(tol=1e-2, multi_class='multinomial', solver=solver, Cs=n_Cs, cv=n_cv_fold, random_state=42, max_iter=100) clf.fit(X, y) assert_equal(clf.n_iter_.shape, (n_classes, n_cv_fold, n_Cs)) clf.fit(X, y_bin) assert_equal(clf.n_iter_.shape, (1, n_cv_fold, n_Cs)) @ignore_warnings def test_warm_start(): # A 1-iteration second fit on same data should give almost same result # with warm starting, and quite different result without warm starting. # Warm starting does not work with liblinear solver. X, y = iris.data, iris.target solvers = ['newton-cg', 'sag'] # old scipy doesn't have maxiter if sp_version >= (0, 12): solvers.append('lbfgs') for warm_start in [True, False]: for fit_intercept in [True, False]: for solver in solvers: for multi_class in ['ovr', 'multinomial']: clf = LogisticRegression(tol=1e-4, multi_class=multi_class, warm_start=warm_start, solver=solver, random_state=42, max_iter=100, fit_intercept=fit_intercept) clf.fit(X, y) coef_1 = clf.coef_ clf.max_iter = 1 with ignore_warnings(): clf.fit(X, y) cum_diff = np.sum(np.abs(coef_1 - clf.coef_)) msg = ("Warm starting issue with %s solver in %s mode " "with fit_intercept=%s and warm_start=%s" % (solver, multi_class, str(fit_intercept), str(warm_start))) if warm_start: assert_greater(2.0, cum_diff, msg) else: assert_greater(cum_diff, 2.0, msg)
bsd-3-clause
sanghack81/SDCIT
experiments/draw_figures.py
1
18832
import collections import matplotlib.pyplot as plt import numpy as np import pandas as pd import scipy import scipy.stats import seaborn as sns from os.path import exists from experiments.exp_setup import SDCIT_RESULT_DIR, SDCIT_FIGURE_DIR from sdcit.utils import AUPC names_chsic_chaotic = ['independent', 'gamma', 'noise', 'trial', 'N', 'runtime', 'statistic', 'pvalue'] names_chsic_postnonlinear = ['independent', 'noise', 'trial', 'N', 'runtime', 'statistic', 'pvalue'] names_kcit_chaotic = ['independent', 'gamma', 'noise', 'trial', 'N', 'runtime', 'statistic', 'boot_p_value', 'appr_p_value'] names_kcit_postnonlinear = ['independent', 'noise', 'trial', 'N', 'runtime', 'statistic', 'boot_p_value', 'appr_p_value'] names_sdcit_chaotic = ['independent', 'gamma', 'trial', 'N', 'statistic', 'pvalue'] names_sdcit_postnonlinear = ['independent', 'noise', 'trial', 'N', 'statistic', 'pvalue'] names_kcipt_chaotic = ['independent', 'gamma', 'trial', 'N', 'statistic', 'pvalue', 'B'] names_kcipt_postnonlinear = ['independent', 'noise', 'trial', 'N', 'statistic', 'pvalue', 'B'] names = {('CHSIC', 'chaotic'): names_chsic_chaotic, ('CHSIC', 'postnonlinear'): names_chsic_postnonlinear, ('KCIT', 'chaotic'): names_kcit_chaotic, ('KCIT', 'postnonlinear'): names_kcit_postnonlinear, ('KCIT2', 'chaotic'): names_kcit_chaotic, ('KCIT2', 'postnonlinear'): names_kcit_postnonlinear, ('SDCIT', 'chaotic'): names_sdcit_chaotic, ('SDCIT', 'postnonlinear'): names_sdcit_postnonlinear, ('KCIPT', 'chaotic'): names_kcipt_chaotic, ('KCIPT', 'postnonlinear'): names_kcipt_postnonlinear, } pvalue_column = collections.defaultdict(lambda: 'pvalue') pvalue_column['KCIT'] = 'boot_p_value' pvalue_column['KCIT2'] = 'boot_p_value' color_palettes = sns.color_palette('Paired', 10) method_color_codes = {'KCIT': 3, 'SDCIT': 5, 'KCIPT': 1, 'CHSIC': 9, 'KCIT2': 2} markers = collections.defaultdict(lambda: 'o') markers.update({'KCIT': 'o', 'SDCIT': 's', 'KCIPT': '*', 'CHSIC': '^', 'KCIT2': 'o'}) all_algos = ['KCIT', 'SDCIT', 'KCIPT', 'CHSIC', 'KCIT2'] def algo_name(org_name): map = {'KCIT2': 'KCIT', 'KCIT': 'KCIT (org.)'} if org_name in map: return map[org_name] else: return org_name def draw_aupc_chaotic(): data = 'chaotic' aupc_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for group_key, group_df in df.groupby(by=['gamma', 'independent', 'N']): group_key = (int(group_key[0] * 10) / 10, *group_key[1:]) if group_key[1] == 0: aupc_data.append([algo, *group_key, AUPC(group_df[pvalue_column[algo]])]) print(draw_aupc_chaotic.__name__) [print(xx) for xx in aupc_data] aupc_data = np.array(aupc_data) aupc_df = pd.DataFrame({'algorithm': aupc_data[:, 0], 'gamma': aupc_data[:, 1], 'independent': aupc_data[:, 2], 'N': aupc_data[:, 3], 'AUPC': aupc_data[:, 4]}) aupc_df['gamma'] = aupc_df['gamma'].astype(float) aupc_df['independent'] = aupc_df['independent'].astype(int) aupc_df['N'] = aupc_df['N'].map(int) aupc_df['AUPC'] = aupc_df['AUPC'].astype(float) aupc_df = aupc_df[aupc_df['independent'] == 0] aupc_df["algo-N"] = aupc_df["algorithm"].map(str) + aupc_df["N"].map(lambda xxx: ' (' + str(xxx) + ')') sns_setting() for k, gdf in aupc_df.groupby(['algorithm', 'N']): print('chaotic', k, gdf['AUPC']) if k[1] == 400: plt.plot(gdf['gamma'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot(gdf['gamma'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') plt.axes().set_xlabel(r'$\gamma$') plt.axes().set_ylabel('Area Under Power Curve') plt.axes().set_ylim([0.45, 1.05]) handles, labels = plt.axes().get_legend_handles_labels() # plt.axes().legend(handles[::-1], labels[::-1]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/{}_aupc.pdf'.format(data), transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def draw_calib_chaotic(): data = 'chaotic' calib_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for k, gdf in df.groupby(by=['independent', 'gamma', 'N']): if float(k[0]) == 1: D, _ = scipy.stats.kstest(gdf[pvalue_column[algo]], 'uniform') calib_data.append([algo, float(k[1]), int(k[2]), D]) print(draw_calib_chaotic.__name__) [print(xx) for xx in calib_data] df = pd.DataFrame(calib_data, columns=['algo', 'gamma', 'N', 'D']) df['gamma'] = df['gamma'].astype(float) df['N'] = df['N'].map(int) df['D'] = df['D'].astype(float) sns_setting() for k, gdf in df.groupby(['algo', 'N']): if k[1] == 400: plt.plot(gdf['gamma'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot(gdf['gamma'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') handles, labels = plt.axes().get_legend_handles_labels() plt.axes().legend(handles[::-1], labels[::-1], ncol=2) plt.axes().set_xlabel(r'$\gamma$') plt.axes().set_ylabel('KS test statistic') plt.axes().set_ylim([0.0, 0.5]) plt.axes().invert_yaxis() plt.axes().set_yticks([0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) handles, labels = plt.axes().get_legend_handles_labels() # plt.axes().legend(handles[::-1], labels[::-1]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/chaotic_calib.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def draw_type_I_error_chaotic(): data = 'chaotic' calib_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for k, gdf in df.groupby(by=['independent', 'gamma', 'N']): if float(k[0]) == 1: calib_data.append([algo, float(k[1]), int(k[2]), np.mean(gdf[pvalue_column[algo]] <= 0.05)]) print(draw_type_I_error_chaotic.__name__) [print(xx) for xx in calib_data] df = pd.DataFrame(calib_data, columns=['algo', 'gamma', 'N', 'D']) df['gamma'] = df['gamma'].astype(float) df['N'] = df['N'].map(int) df['D'] = df['D'].astype(float) sns_setting() for k, gdf in df.groupby(['algo', 'N']): if k[1] == 400: plt.plot(gdf['gamma'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot(gdf['gamma'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') plt.axes().set_xlabel(r'$\gamma$') plt.axes().set_xticks([0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) plt.axes().set_ylabel('Type I error') plt.axes().set_ylim([0.0, 0.2]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/chaotic_type_I.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def draw_aupc_postnonlinear(): data = 'postnonlinear' aupc_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for group_key, group_df in df.groupby(by=['noise', 'independent', 'N']): group_key = (int(group_key[0] * 10) / 10, int(group_key[1]), int(group_key[2])) aupc_data.append([algo, *group_key, AUPC(group_df[pvalue_column[algo]])]) print(draw_aupc_postnonlinear.__name__) [print(xx) for xx in aupc_data] aupc_data = np.array(aupc_data) aupc_df = pd.DataFrame({'algorithm': [str(v) for v in aupc_data[:, 0]], 'noise': [int(float(v)) for v in aupc_data[:, 1]], 'independent': [int(v) for v in aupc_data[:, 2]], 'N': [int(v) for v in aupc_data[:, 3]], 'AUPC': [float(v) for v in aupc_data[:, 4]]}) aupc_df['dimension'] = (aupc_df['noise'] + 1).astype(int) aupc_df = aupc_df[aupc_df['independent'] == 0] aupc_df["algo-N"] = aupc_df["algorithm"].map(str) + aupc_df["N"].map(lambda xxx: ' (' + str(xxx) + ')') sns_setting() for k, gdf in aupc_df.groupby(['algorithm', 'N']): gdf = gdf[gdf['dimension'] <= 5] if k[1] == 400: plt.plot(gdf['dimension'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot(gdf['dimension'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') plt.axes().set_xlabel('dimension') plt.axes().set_ylabel('Area Under Power Curve') plt.axes().set_ylim([0.45, 1.05]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_aupc.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def draw_aupc_postnonlinear_highdim(): data = 'postnonlinear' aupc_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for group_key, group_df in df.groupby(by=['noise', 'independent', 'N']): group_key = (int(group_key[0] * 10) / 10, int(group_key[1]), int(group_key[2])) aupc_data.append([algo, *group_key, AUPC(group_df[pvalue_column[algo]])]) print(draw_aupc_postnonlinear_highdim.__name__) [print(xx) for xx in aupc_data] aupc_data = np.array(aupc_data) aupc_df = pd.DataFrame({'algorithm': [str(v) for v in aupc_data[:, 0]], 'noise': [int(float(v)) for v in aupc_data[:, 1]], 'independent': [int(v) for v in aupc_data[:, 2]], 'N': [int(v) for v in aupc_data[:, 3]], 'AUPC': [float(v) for v in aupc_data[:, 4]]}) aupc_df['dimension'] = (aupc_df['noise'] + 1).astype(int) aupc_df = aupc_df[aupc_df['independent'] == 0] aupc_df["algo-N"] = aupc_df["algorithm"].map(str) + aupc_df["N"].map(lambda xxx: ' (' + str(xxx) + ')') sns_setting() for k, gdf in aupc_df.groupby(['algorithm', 'N']): if k[1] == 400: plt.plot([int(v) for v in gdf['dimension']], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) plt.axes().set_xlabel('dimension') plt.axes().set_ylabel('Area Under Power Curve') plt.axes().set_ylim([0.95, 1.01]) plt.axes().set_xscale('log') plt.xticks([1, 5, 10, 20, 50], [1, 5, 10, 20, 50]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_aupc_highdim.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def draw_calib_postnonlinear(): data = 'postnonlinear' calib_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for k, gdf in df.groupby(by=['independent', 'noise', 'N']): if float(k[0]) == 1: D, _ = scipy.stats.kstest(gdf[pvalue_column[algo]], 'uniform') calib_data.append([algo, float(k[1]), int(k[2]), D]) print(draw_calib_postnonlinear.__name__) [print(xx) for xx in calib_data] df = pd.DataFrame(calib_data, columns=['algo', 'noise', 'N', 'D']) df['noise'] = df['noise'].map(int) df['dimension'] = (df['noise'] + 1).astype(int) df['N'] = df['N'].map(int) df['D'] = df['D'].astype(float) sns_setting() for k, gdf in df.groupby(['algo', 'N']): gdf = gdf[gdf['dimension'] <= 5] if k[1] == 400: plt.plot([int(v) for v in gdf['dimension']], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot([int(v) for v in gdf['dimension']], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') plt.axes().set_xlabel('dimension') plt.axes().set_ylabel('KS test statistic') plt.axes().set_ylim([0.0, 0.5]) plt.axes().invert_yaxis() plt.axes().set_yticks([0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_calib.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def sns_setting(): paper_rc = {'lines.linewidth': 1, 'lines.markersize': 2} sns.set_context("paper", rc=paper_rc) sns.set(style='white', font_scale=1.4) plt.figure(figsize=[4, 3]) plt.rc('text', usetex=True) plt.rc('text.latex', preamble=r'\usepackage{cmbright}') def draw_calib_postnonlinear_highdim(): data = 'postnonlinear' calib_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for k, gdf in df.groupby(by=['independent', 'noise', 'N']): if float(k[0]) == 1 and k[2] == 400: dd, _ = scipy.stats.kstest(gdf[pvalue_column[algo]], 'uniform') calib_data.append([algo, float(k[1]), int(k[2]), dd]) print(draw_calib_postnonlinear_highdim.__name__) [print(xx) for xx in calib_data] df = pd.DataFrame(calib_data, columns=['algo', 'noise', 'N', 'D']) df['noise'] = df['noise'].map(int) df['dimension'] = (df['noise'] + 1).astype(int) df['N'] = df['N'].map(int) df['D'] = df['D'].astype(float) sns_setting() for k, gdf in df.groupby(['algo', 'N']): print('postnonlinear', k, gdf['D']) if k[1] == 400: plt.plot(gdf['dimension'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot(gdf['dimension'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') plt.axes().set_xlabel('dimension') plt.axes().set_ylabel('KS test statistic') plt.axes().set_xscale('log') plt.axes().set_ylim([0.0, 0.5]) plt.axes().invert_yaxis() plt.xticks([1, 5, 10, 20, 50], [1, 5, 10, 20, 50]) plt.axes().set_yticks([0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_calib_highdim.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() def draw_type_I_postnonlinear_highdim(): data = 'postnonlinear' calib_data = [] for algo in all_algos: df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)]) for k, gdf in df.groupby(by=['independent', 'noise', 'N']): if float(k[0]) == 1 and k[2] == 400: dd = np.mean(gdf[pvalue_column[algo]] <= 0.05) calib_data.append([algo, float(k[1]), int(k[2]), dd]) print(draw_type_I_postnonlinear_highdim.__name__) [print(xx) for xx in calib_data] df = pd.DataFrame(calib_data, columns=['algo', 'noise', 'N', 'D']) df['noise'] = df['noise'].map(int) df['dimension'] = (df['noise'] + 1).astype(int) df['N'] = df['N'].map(int) df['D'] = df['D'].astype(float) sns_setting() for k, gdf in df.groupby(['algo', 'N']): if k[1] == 400: plt.plot(gdf['dimension'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0]))) else: plt.plot(gdf['dimension'], gdf['D'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]], ls='-' if k[1] == 400 else ':', label='_nolegend_') plt.axes().set_xlabel('dimension') plt.axes().set_xscale('log') plt.xticks([1, 5, 10, 20, 50], [1, 5, 10, 20, 50]) plt.axes().set_ylim([0.0, 0.2]) handles, labels = plt.axes().get_legend_handles_labels() plt.axes().legend(handles[::-1], labels[::-1]) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_type_I_highdim.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() if __name__ == '__main__': for data in ['chaotic', 'postnonlinear']: for algo in all_algos: assert exists(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv'), 'run tests first -- missing {}'.format(algo.lower() + '_' + data + '.csv') if True: # chaotic series draw_aupc_chaotic() draw_calib_chaotic() # postnonlinear-noise draw_aupc_postnonlinear() draw_calib_postnonlinear() draw_aupc_postnonlinear_highdim() draw_calib_postnonlinear_highdim() # type I for both draw_type_I_error_chaotic() draw_type_I_postnonlinear_highdim()
mit