repo_name
stringlengths
6
67
path
stringlengths
5
185
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
1.02k
962k
license
stringclasses
15 values
google/qkeras
examples/example_ternary.py
1
3557
# Copyright 2020 Google LLC # # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== from __future__ import absolute_import # Not necessary in a Python 3-only module from __future__ import division # Not necessary in a Python 3-only module from __future__ import google_type_annotations # Not necessary in a Python 3-only module from __future__ import print_function # Not necessary in a Python 3-only module from absl import app from absl import flags import matplotlib import numpy as np matplotlib.use('TkAgg') import matplotlib.pyplot as plt FLAGS = flags.FLAGS def _stochastic_rounding(x, precision, resolution, delta): """Stochastic_rounding for ternary. Args: x: precision: A float. The area we want to make this stochastic rounding. [delta-precision, delta] [delta, delta+precision] resolution: control the quantization resolution. delta: the undiscountinued point (positive number) Return: A tensor with stochastic rounding numbers. """ delta_left = delta - precision delta_right = delta + precision scale = 1 / resolution scale_delta_left = delta_left * scale scale_delta_right = delta_right * scale scale_2_delta = scale_delta_right - scale_delta_left scale_x = x * scale fraction = scale_x - scale_delta_left # print(precision, scale, x[0], np.floor(scale_x[0]), scale_x[0], fraction[0]) # we use uniform distribution random_selector = np.random.uniform(0, 1, size=x.shape) * scale_2_delta # print(precision, scale, x[0], delta_left[0], delta_right[0]) # print('x', scale_x[0], fraction[0], random_selector[0], scale_2_delta[0]) # rounddown = fraction < random_selector result = np.where(fraction < random_selector, scale_delta_left / scale, scale_delta_right / scale) return result def _ternary(x, sto=False): m = np.amax(np.abs(x), keepdims=True) scale = 2 * m / 3.0 thres = scale / 2.0 ratio = 0.1 if sto: sign_bit = np.sign(x) x = np.abs(x) prec = x / scale x = ( sign_bit * scale * _stochastic_rounding( x / scale, precision=0.3, resolution=0.01, # those two are all normalized. delta=thres / scale)) # prec + prec *ratio) # mm = np.amax(np.abs(x), keepdims=True) return np.where(np.abs(x) < thres, np.zeros_like(x), np.sign(x)) def main(argv): if len(argv) > 1: raise app.UsageError('Too many command-line arguments.') # x = np.arange(-3.0, 3.0, 0.01) # x = np.random.uniform(-0.01, 0.01, size=1000) x = np.random.uniform(-10.0, 10.0, size=1000) # x = np.random.uniform(-1, 1, size=1000) x = np.sort(x) tr = np.zeros_like(x) t = np.zeros_like(x) iter_count = 500 for _ in range(iter_count): y = _ternary(x) yr = _ternary(x, sto=True) t = t + y tr = tr + yr plt.plot(x, t/iter_count) plt.plot(x, tr/iter_count) plt.ylabel('mean (%s samples)' % iter_count) plt.show() if __name__ == '__main__': app.run(main)
apache-2.0
florian-f/sklearn
sklearn/datasets/species_distributions.py
4
7844
""" ============================= Species distribution dataset ============================= This dataset represents the geographic distribution of species. The dataset is provided by Phillips et. al. (2006). The two species are: - `"Bradypus variegatus" <http://www.iucnredlist.org/apps/redlist/details/3038/0>`_ , the Brown-throated Sloth. - `"Microryzomys minutus" <http://www.iucnredlist.org/apps/redlist/details/13408/0>`_ , also known as the Forest Small Rice Rat, a rodent that lives in Peru, Colombia, Ecuador, Peru, and Venezuela. References: * `"Maximum entropy modeling of species geographic distributions" <http://www.cs.princeton.edu/~schapire/papers/ecolmod.pdf>`_ S. J. Phillips, R. P. Anderson, R. E. Schapire - Ecological Modelling, 190:231-259, 2006. Notes: * See examples/applications/plot_species_distribution_modeling.py for an example of using this dataset """ # Authors: Peter Prettenhofer <peter.prettenhofer@gmail.com> # Jake Vanderplas <vanderplas@astro.washington.edu> # # License: Simplified BSD from io import BytesIO from os import makedirs from os.path import join from os.path import exists try: # Python 2 from urllib2 import urlopen except ImportError: # Python 3 from urllib.request import urlopen import numpy as np from sklearn.datasets.base import get_data_home, Bunch from sklearn.externals import joblib DIRECTORY_URL = "http://www.cs.princeton.edu/~schapire/maxent/datasets/" SAMPLES_URL = join(DIRECTORY_URL, "samples.zip") COVERAGES_URL = join(DIRECTORY_URL, "coverages.zip") DATA_ARCHIVE_NAME = "species_coverage.pkz" def _load_coverage(F, header_length=6, dtype=np.int16): """ load a coverage file. This will return a numpy array of the given dtype """ try: header = [F.readline() for i in range(header_length)] except: F = open(F) header = [F.readline() for i in range(header_length)] make_tuple = lambda t: (t.split()[0], float(t.split()[1])) header = dict([make_tuple(line) for line in header]) M = np.loadtxt(F, dtype=dtype) nodata = header['NODATA_value'] if nodata != -9999: M[nodata] = -9999 return M def _load_csv(F): """Load csv file. Paramters --------- F : string or file object file object or name of file Returns ------- rec : np.ndarray record array representing the data """ try: names = F.readline().strip().split(',') except: F = open(F) names = F.readline().strip().split(',') rec = np.loadtxt(F, skiprows=1, delimiter=',', dtype='a22,f4,f4') rec.dtype.names = names return rec def construct_grids(batch): """Construct the map grid from the batch object Parameters ---------- batch : Batch object The object returned by :func:`fetch_species_distributions` Returns ------- (xgrid, ygrid) : 1-D arrays The grid corresponding to the values in batch.coverages """ # x,y coordinates for corner cells xmin = batch.x_left_lower_corner + batch.grid_size xmax = xmin + (batch.Nx * batch.grid_size) ymin = batch.y_left_lower_corner + batch.grid_size ymax = ymin + (batch.Ny * batch.grid_size) # x coordinates of the grid cells xgrid = np.arange(xmin, xmax, batch.grid_size) # y coordinates of the grid cells ygrid = np.arange(ymin, ymax, batch.grid_size) return (xgrid, ygrid) def fetch_species_distributions(data_home=None, download_if_missing=True): """Loader for species distribution dataset from Phillips et. al. (2006) Parameters ---------- data_home : optional, default: None Specify another download and cache folder for the datasets. By default all scikit learn data is stored in '~/scikit_learn_data' subfolders. download_if_missing: optional, True by default If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site. Notes ------ This dataset represents the geographic distribution of species. The dataset is provided by Phillips et. al. (2006). The two species are: - `"Bradypus variegatus" <http://www.iucnredlist.org/apps/redlist/details/3038/0>`_ , the Brown-throated Sloth. - `"Microryzomys minutus" <http://www.iucnredlist.org/apps/redlist/details/13408/0>`_ , also known as the Forest Small Rice Rat, a rodent that lives in Peru, Colombia, Ecuador, Peru, and Venezuela. The data is returned as a Bunch object with the following attributes: coverages : array, shape = [14, 1592, 1212] These represent the 14 features measured at each point of the map grid. The latitude/longitude values for the grid are discussed below. Missing data is represented by the value -9999. train : record array, shape = (1623,) The training points for the data. Each point has three fields: - train['species'] is the species name - train['dd long'] is the longitude, in degrees - train['dd lat'] is the latitude, in degrees test : record array, shape = (619,) The test points for the data. Same format as the training data. Nx, Ny : integers The number of longitudes (x) and latitudes (y) in the grid x_left_lower_corner, y_left_lower_corner : floats The (x,y) position of the lower-left corner, in degrees grid_size : float The spacing between points of the grid, in degrees References ---------- * `"Maximum entropy modeling of species geographic distributions" <http://www.cs.princeton.edu/~schapire/papers/ecolmod.pdf>`_ S. J. Phillips, R. P. Anderson, R. E. Schapire - Ecological Modelling, 190:231-259, 2006. Notes ----- * See examples/applications/plot_species_distribution_modeling.py for an example of using this dataset with scikit-learn """ data_home = get_data_home(data_home) if not exists(data_home): makedirs(data_home) # Define parameters for the data files. These should not be changed # unless the data model changes. They will be saved in the npz file # with the downloaded data. extra_params = dict(x_left_lower_corner=-94.8, Nx=1212, y_left_lower_corner=-56.05, Ny=1592, grid_size=0.05) dtype = np.int16 if not exists(join(data_home, DATA_ARCHIVE_NAME)): print('Downloading species data from %s to %s' % (SAMPLES_URL, data_home)) X = np.load(BytesIO(urlopen(SAMPLES_URL).read())) for f in X.files: fhandle = BytesIO(X[f]) if 'train' in f: train = _load_csv(fhandle) if 'test' in f: test = _load_csv(fhandle) print('Downloading coverage data from %s to %s' % (COVERAGES_URL, data_home)) X = np.load(BytesIO(urlopen(COVERAGES_URL).read())) coverages = [] for f in X.files: fhandle = BytesIO(X[f]) print(' - converting', f) coverages.append(_load_coverage(fhandle)) coverages = np.asarray(coverages, dtype=dtype) bunch = Bunch(coverages=coverages, test=test, train=train, **extra_params) joblib.dump(bunch, join(data_home, DATA_ARCHIVE_NAME), compress=9) else: bunch = joblib.load(join(data_home, DATA_ARCHIVE_NAME)) return bunch
bsd-3-clause
sniemi/SamPy
sandbox/src1/examples/animation_blit_qt4.py
1
1976
# For detailed comments on animation and the techniqes used here, see # the wiki entry http://www.scipy.org/Cookbook/Matplotlib/Animations import os, sys import matplotlib matplotlib.use('Qt4Agg') # qt4 example from PyQt4 import QtCore, QtGui ITERS = 1000 import pylab as p import numpy as npy import time class BlitQT(QtCore.QObject): def __init__(self): self.ax = p.subplot(111) self.canvas = self.ax.figure.canvas # By making this a child of the canvas we make sure that it is # destroyed first and avoids a possible exception when the user clicks # on the window's close box. QtCore.QObject.__init__(self, self.canvas) self.cnt = 0 # create the initial line self.x = npy.arange(0,2*npy.pi,0.01) self.line, = p.plot(self.x, npy.sin(self.x), animated=True, lw=2) self.background = None self.old_size = 0, 0 def timerEvent(self, evt): # See if the size has changed since last time round. current_size = self.ax.bbox.width(), self.ax.bbox.height() if self.old_size != current_size: self.old_size = current_size self.background = self.canvas.copy_from_bbox(self.ax.bbox) # restore the clean slate background self.canvas.restore_region(self.background) # update the data self.line.set_ydata(npy.sin(self.x+self.cnt/10.0)) # just draw the animated artist self.ax.draw_artist(self.line) # just redraw the axes rectangle self.canvas.blit(self.ax.bbox) if self.cnt==ITERS: # print the timing info and quit print 'FPS:' , ITERS/(time.time()-self.tstart) sys.exit() else: self.cnt += 1 p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs p.grid() # to ensure proper background restore app = BlitQT() # for profiling app.tstart = time.time() app.startTimer(0) p.show()
bsd-2-clause
kwikadi/orange3
Orange/classification/logistic_regression.py
2
1302
import numpy as np import sklearn.linear_model as skl_linear_model from Orange.classification import SklLearner, SklModel from Orange.preprocess import Normalize from Orange.preprocess.score import LearnerScorer from Orange.data import Variable, DiscreteVariable __all__ = ["LogisticRegressionLearner"] class _FeatureScorerMixin(LearnerScorer): feature_type = Variable class_type = DiscreteVariable def score(self, model): # Take the maximum attribute score across all classes return np.max(np.abs(model.coefficients), axis=0) class LogisticRegressionClassifier(SklModel): @property def intercept(self): return self.skl_model.intercept_ @property def coefficients(self): return self.skl_model.coef_ class LogisticRegressionLearner(SklLearner, _FeatureScorerMixin): __wraps__ = skl_linear_model.LogisticRegression __returns__ = LogisticRegressionClassifier name = 'logreg' preprocessors = SklLearner.preprocessors + [Normalize()] def __init__(self, penalty="l2", dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None, preprocessors=None): super().__init__(preprocessors=preprocessors) self.params = vars()
bsd-2-clause
detrout/debian-statsmodels
statsmodels/graphics/tests/test_regressionplots.py
5
4406
'''Tests for regressionplots, entire module is skipped ''' import numpy as np import nose import statsmodels.api as sm from statsmodels.graphics.regressionplots import (plot_fit, plot_ccpr, plot_partregress, plot_regress_exog, abline_plot, plot_partregress_grid, plot_ccpr_grid, add_lowess) from pandas import Series, DataFrame try: import matplotlib.pyplot as plt #makes plt available for test functions have_matplotlib = True except: have_matplotlib = False def setup(): if not have_matplotlib: raise nose.SkipTest('No tests here') def teardown_module(): plt.close('all') class TestPlot(object): def __init__(self): self.setup() #temp: for testing without nose def setup(self): nsample = 100 sig = 0.5 x1 = np.linspace(0, 20, nsample) x2 = 5 + 3* np.random.randn(nsample) X = np.c_[x1, x2, np.sin(0.5*x1), (x2-5)**2, np.ones(nsample)] beta = [0.5, 0.5, 1, -0.04, 5.] y_true = np.dot(X, beta) y = y_true + sig * np.random.normal(size=nsample) exog0 = sm.add_constant(np.c_[x1, x2], prepend=False) res = sm.OLS(y, exog0).fit() self.res = res def test_plot_fit(self): res = self.res fig = plot_fit(res, 0, y_true=None) x0 = res.model.exog[:, 0] yf = res.fittedvalues y = res.model.endog px1, px2 = fig.axes[0].get_lines()[0].get_data() np.testing.assert_equal(x0, px1) np.testing.assert_equal(y, px2) px1, px2 = fig.axes[0].get_lines()[1].get_data() np.testing.assert_equal(x0, px1) np.testing.assert_equal(yf, px2) plt.close(fig) def test_plot_oth(self): #just test that they run res = self.res endog = res.model.endog exog = res.model.exog plot_fit(res, 0, y_true=None) plot_partregress_grid(res, exog_idx=[0,1]) plot_regress_exog(res, exog_idx=0) plot_ccpr(res, exog_idx=0) plot_ccpr_grid(res, exog_idx=[0]) fig = plot_ccpr_grid(res, exog_idx=[0,1]) for ax in fig.axes: add_lowess(ax) plt.close('all') class TestPlotPandas(TestPlot): def setup(self): nsample = 100 sig = 0.5 x1 = np.linspace(0, 20, nsample) x2 = 5 + 3* np.random.randn(nsample) X = np.c_[x1, x2, np.sin(0.5*x1), (x2-5)**2, np.ones(nsample)] beta = [0.5, 0.5, 1, -0.04, 5.] y_true = np.dot(X, beta) y = y_true + sig * np.random.normal(size=nsample) exog0 = sm.add_constant(np.c_[x1, x2], prepend=False) exog0 = DataFrame(exog0, columns=["const", "var1", "var2"]) y = Series(y, name="outcome") res = sm.OLS(y, exog0).fit() self.res = res class TestABLine(object): @classmethod def setupClass(cls): np.random.seed(12345) X = sm.add_constant(np.random.normal(0, 20, size=30)) y = np.dot(X, [25, 3.5]) + np.random.normal(0, 30, size=30) mod = sm.OLS(y,X).fit() cls.X = X cls.y = y cls.mod = mod def test_abline_model(self): fig = abline_plot(model_results=self.mod) ax = fig.axes[0] ax.scatter(self.X[:,1], self.y) plt.close(fig) def test_abline_model_ax(self): fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(self.X[:,1], self.y) fig = abline_plot(model_results=self.mod, ax=ax) plt.close(fig) def test_abline_ab(self): mod = self.mod intercept, slope = mod.params fig = abline_plot(intercept=intercept, slope=slope) plt.close(fig) def test_abline_ab_ax(self): mod = self.mod intercept, slope = mod.params fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(self.X[:,1], self.y) fig = abline_plot(intercept=intercept, slope=slope, ax=ax) plt.close(fig) class TestABLinePandas(TestABLine): @classmethod def setupClass(cls): np.random.seed(12345) X = sm.add_constant(np.random.normal(0, 20, size=30)) y = np.dot(X, [25, 3.5]) + np.random.normal(0, 30, size=30) cls.X = X cls.y = y X = DataFrame(X, columns=["const", "someX"]) y = Series(y, name="outcome") mod = sm.OLS(y,X).fit() cls.mod = mod
bsd-3-clause
hmendozap/auto-sklearn
autosklearn/pipeline/components/feature_preprocessing/kitchen_sinks.py
1
2125
from HPOlibConfigSpace.configuration_space import ConfigurationSpace from HPOlibConfigSpace.hyperparameters import UniformFloatHyperparameter, \ UniformIntegerHyperparameter from autosklearn.pipeline.components.base import AutoSklearnPreprocessingAlgorithm from autosklearn.pipeline.constants import * class RandomKitchenSinks(AutoSklearnPreprocessingAlgorithm): def __init__(self, gamma, n_components, random_state=None): """ Parameters: gamma: float Parameter of the rbf kernel to be approximated exp(-gamma * x^2) n_components: int Number of components (output dimensionality) used to approximate the kernel """ self.gamma = gamma self.n_components = n_components self.random_state = random_state def fit(self, X, Y=None): import sklearn.kernel_approximation self.preprocessor = sklearn.kernel_approximation.RBFSampler( self.gamma, self.n_components, self.random_state) self.preprocessor.fit(X) return self def transform(self, X): if self.preprocessor is None: raise NotImplementedError() return self.preprocessor.transform(X) @staticmethod def get_properties(dataset_properties=None): return {'shortname': 'KitchenSink', 'name': 'Random Kitchen Sinks', 'handles_regression': True, 'handles_classification': True, 'handles_multiclass': True, 'handles_multilabel': True, 'is_deterministic': True, 'input': (SPARSE, DENSE, UNSIGNED_DATA), 'output': (INPUT, UNSIGNED_DATA)} @staticmethod def get_hyperparameter_search_space(dataset_properties=None): gamma = UniformFloatHyperparameter( "gamma", 0.3, 2., default=1.0) n_components = UniformIntegerHyperparameter( "n_components", 50, 10000, default=100, log=True) cs = ConfigurationSpace() cs.add_hyperparameter(gamma) cs.add_hyperparameter(n_components) return cs
bsd-3-clause
junwucs/h2o-3
h2o-docs/src/api/data-science-example-1/example-native-pandas-scikit.py
22
2796
# -*- coding: utf-8 -*- # <nbformat>3.0</nbformat> # <codecell> from pandas import Series, DataFrame import pandas as pd import numpy as np import sklearn from sklearn.ensemble import GradientBoostingClassifier from sklearn import preprocessing # <codecell> air_raw = DataFrame.from_csv("allyears_tiny.csv", index_col = False) print(air_raw.head()) air_raw['RandNum'] = Series(np.random.uniform(size = len(air_raw['Origin']))) print(air_raw.head()) # <codecell> air_mapped = DataFrame() air_mapped['RandNum'] = air_raw['RandNum'] air_mapped['IsDepDelayed'] = air_raw['IsDepDelayed'] air_mapped['IsDepDelayedInt'] = air_mapped.apply(lambda row: 1 if row['IsDepDelayed'] == 'YES' else 0, axis=1) del air_mapped['IsDepDelayed'] print(air_mapped.shape) lb_origin = sklearn.preprocessing.LabelBinarizer() lb_origin.fit(air_raw['Origin']) tmp_origin = lb_origin.transform(air_raw['Origin']) tmp_origin_df = DataFrame(tmp_origin) print(tmp_origin_df.shape) lb_dest = sklearn.preprocessing.LabelBinarizer() lb_dest.fit(air_raw['Dest']) tmp_dest = lb_origin.transform(air_raw['Dest']) tmp_dest_df = DataFrame(tmp_dest) print(tmp_dest_df.shape) lb_uniquecarrier = sklearn.preprocessing.LabelBinarizer() lb_uniquecarrier.fit(air_raw['UniqueCarrier']) tmp_uniquecarrier = lb_origin.transform(air_raw['UniqueCarrier']) tmp_uniquecarrier_df = DataFrame(tmp_uniquecarrier) print(tmp_uniquecarrier_df.shape) air_mapped = pd.concat([ air_mapped, tmp_origin_df, tmp_dest_df, air_raw['Distance'], tmp_uniquecarrier_df, air_raw['Month'], air_raw['DayofMonth'], air_raw['DayOfWeek'], ], axis=1) print(air_mapped.shape) air_mapped air = air_mapped # <codecell> air_train = air.ix[air['RandNum'] <= 0.8] # air_valid = air.ix[(air['RandNum'] > 0.8) & (air['RandNum'] <= 0.9)] air_test = air.ix[air['RandNum'] > 0.9] print(air_train.shape) print(air_test.shape) # <codecell> X_train = air_train.copy(deep=True) del X_train['RandNum'] del X_train['IsDepDelayedInt'] print(list(X_train.columns.values)) print(X_train.shape) y_train = air_train['IsDepDelayedInt'] print(y_train.shape) # <codecell> clf = GradientBoostingClassifier(n_estimators = 10, max_depth = 3, learning_rate = 0.01) clf.fit(X_train, y_train) # <codecell> X_test = air_test.copy(deep=True) del X_test['RandNum'] del X_test['IsDepDelayedInt'] print(list(X_test.columns.values)) print(X_test.shape) print("") print("--- PREDICTIONS ---") print("") pred = clf.predict(X_test) print(pred)
apache-2.0
ygorshenin/omim
tools/python/transit/transit_graph_generator.py
10
18195
#!/usr/bin/env python3 # Generates transit graph for MWM transit section generator. # Also shows preview of transit scheme lines. import argparse import copy import json import math import numpy as np import os.path import bezier_curves import transit_color_palette class OsmIdCode: NODE = 0x4000000000000000 WAY = 0x8000000000000000 RELATION = 0xC000000000000000 RESET = ~(NODE | WAY | RELATION) TYPE2CODE = { 'n': NODE, 'r': RELATION, 'w': WAY } def get_extended_osm_id(osm_id, osm_type): try: return str(osm_id | OsmIdCode.TYPE2CODE[osm_type[0]]) except KeyError: raise ValueError('Unknown OSM type: ' + osm_type) def get_line_id(road_id, line_index): return road_id << 4 | line_index def get_interchange_node_id(min_stop_id): return 1 << 62 | min_stop_id def clamp(value, min_value, max_value): return max(min(value, max_value), min_value) def get_mercator_point(lat, lon): lat = clamp(lat, -86.0, 86.0) sin_x = math.sin(math.radians(lat)) y = math.degrees(0.5 * math.log((1.0 + sin_x) / (1.0 - sin_x))) y = clamp(y, -180, 180) return {'x': lon, 'y': y} class TransitGraphBuilder: def __init__(self, input_data, transit_colors, points_per_curve=100, alpha=0.5): self.palette = transit_color_palette.Palette(transit_colors) self.input_data = input_data self.points_per_curve = points_per_curve self.alpha = alpha self.networks = [] self.lines = [] self.stops = {} self.interchange_nodes = set() self.transfers = {} self.gates = {} self.edges = [] self.segments = {} self.shapes = [] self.transit_graph = None self.matched_colors = {} self.stop_names = {} def __get_average_stops_point(self, stop_ids): """Returns an average position of the stops.""" count = len(stop_ids) if count == 0: raise ValueError('Average stops point calculation failed: the list of stop id is empty.') average_point = [0, 0] for stop_id in stop_ids: point = self.__get_stop(stop_id)['point'] average_point[0] += point['x'] average_point[1] += point['y'] return [average_point[0] / count, average_point[1] / count] def __add_gate(self, osm_id, is_entrance, is_exit, point, weight, stop_id): """Creates a new gate or adds information to the existing with the same weight.""" if (osm_id, weight) in self.gates: gate_ref = self.gates[(osm_id, weight)] if stop_id not in gate_ref['stop_ids']: gate_ref['stop_ids'].append(stop_id) gate_ref['entrance'] |= is_entrance gate_ref['exit'] |= is_exit return gate = {'osm_id': osm_id, 'point': point, 'weight': weight, 'stop_ids': [stop_id], 'entrance': is_entrance, 'exit': is_exit } self.gates[(osm_id, weight)] = gate def __get_interchange_node(self, stop_id): """Returns the existing interchange node or creates a new one.""" for node_stops in self.interchange_nodes: if stop_id in node_stops: return node_stops return (stop_id,) def __get_stop(self, stop_id): """Returns the stop or the interchange node.""" if stop_id in self.stops: return self.stops[stop_id] return self.transfers[stop_id] def __check_line_title(self, line, route_name): """Formats correct line name.""" if line['title']: return name = route_name if route_name else line['number'] if len(line['stop_ids']) > 1: first_stop = self.stop_names[line['stop_ids'][0]] last_stop = self.stop_names[line['stop_ids'][-1]] if first_stop and last_stop: line['title'] = u'{0}: {1} - {2}'.format(name, first_stop, last_stop) return line['title'] = name def __read_stops(self): """Reads stops, their exits and entrances.""" for stop_item in self.input_data['stops']: stop = {} stop['id'] = stop_item['id'] stop['osm_id'] = get_extended_osm_id(stop_item['osm_id'], stop_item['osm_type']) if 'zone_id' in stop_item: stop['zone_id'] = stop_item['zone_id'] stop['point'] = get_mercator_point(stop_item['lat'], stop_item['lon']) stop['line_ids'] = [] # TODO: Save stop names stop_item['name'] and stop_item['int_name'] for text anchors calculation. stop['title_anchors'] = [] self.stops[stop['id']] = stop self.stop_names[stop['id']] = stop_item['name'] for entrance_item in stop_item['entrances']: ex_id = get_extended_osm_id(entrance_item['osm_id'], entrance_item['osm_type']) point = get_mercator_point(entrance_item['lat'], entrance_item['lon']) self.__add_gate(ex_id, True, False, point, entrance_item['distance'], stop['id']) for exit_item in stop_item['exits']: ex_id = get_extended_osm_id(exit_item['osm_id'], exit_item['osm_type']) point = get_mercator_point(exit_item['lat'], exit_item['lon']) self.__add_gate(ex_id, False, True, point, exit_item['distance'], stop['id']) def __read_transfers(self): """Reads transfers between stops.""" for transfer_item in self.input_data['transfers']: edge = {'stop1_id': transfer_item[0], 'stop2_id': transfer_item[1], 'weight': transfer_item[2], 'transfer': True } self.edges.append(copy.deepcopy(edge)) edge['stop1_id'], edge['stop2_id'] = edge['stop2_id'], edge['stop1_id'] self.edges.append(edge) def __read_networks(self): """Reads networks and routes.""" for network_item in self.input_data['networks']: network_id = network_item['agency_id'] network = {'id': network_id, 'title': network_item['network']} self.networks.append(network) for route_item in network_item['routes']: line_index = 0 # Create a line for each itinerary. for line_item in route_item['itineraries']: line_stops = line_item['stops'] line_id = get_line_id(route_item['route_id'], line_index) line = {'id': line_id, 'title': line_item.get('name', ''), 'type': route_item['type'], 'network_id': network_id, 'number': route_item['ref'], 'interval': line_item['interval'], 'stop_ids': [] } line['color'] = self.__match_color(route_item.get('colour', ''), route_item.get('casing', '')) # TODO: Add processing of line_item['shape'] when this data will be available. # TODO: Add processing of line_item['trip_ids'] when this data will be available. # Create an edge for each connection of stops. for i in range(len(line_stops)): stop1 = line_stops[i] line['stop_ids'].append(stop1[0]) self.stops[stop1[0]]['line_ids'].append(line_id) if i + 1 < len(line_stops): stop2 = line_stops[i + 1] edge = {'stop1_id': stop1[0], 'stop2_id': stop2[0], 'weight': stop2[1] - stop1[1], 'transfer': False, 'line_id': line_id, 'shape_ids': [] } self.edges.append(edge) self.__check_line_title(line, route_item.get('name', '')) self.lines.append(line) line_index += 1 def __match_color(self, color_str, casing_str): if color_str is None or len(color_str) == 0: return self.palette.get_default_color() if casing_str is None: casing_str = '' matched_colors_key = color_str + "/" + casing_str if matched_colors_key in self.matched_colors: return self.matched_colors[matched_colors_key] c = self.palette.get_nearest_color(color_str, casing_str, self.matched_colors.values()) if c != self.palette.get_default_color(): self.matched_colors[matched_colors_key] = c return c def __generate_transfer_nodes(self): """Merges stops into transfer nodes.""" for edge in self.edges: if edge['transfer']: node1 = self.__get_interchange_node(edge['stop1_id']) node2 = self.__get_interchange_node(edge['stop2_id']) merged_node = tuple(sorted(set(node1 + node2))) self.interchange_nodes.discard(node1) self.interchange_nodes.discard(node2) self.interchange_nodes.add(merged_node) for node_stop_ids in self.interchange_nodes: point = self.__get_average_stops_point(node_stop_ids) transfer = {'id': get_interchange_node_id(self.stops[node_stop_ids[0]]['id']), 'stop_ids': list(node_stop_ids), 'point': {'x': point[0], 'y': point[1]}, 'title_anchors': [] } for stop_id in node_stop_ids: self.stops[stop_id]['transfer_id'] = transfer['id'] self.transfers[transfer['id']] = transfer def __collect_segments(self): """Prepares collection of segments for shapes generation.""" # Each line divided on segments by its stops and transfer nodes. # Merge equal segments from different lines into a single one and collect adjacent stops of that segment. # Average positions of these stops will be used as guide points for a curve generation. for line in self.lines: prev_seg = None prev_id1 = None for i in range(len(line['stop_ids']) - 1): node1 = self.stops[line['stop_ids'][i]] node2 = self.stops[line['stop_ids'][i + 1]] id1 = node1.get('transfer_id', node1['id']) id2 = node2.get('transfer_id', node2['id']) if id1 == id2: continue seg = tuple(sorted([id1, id2])) if seg not in self.segments: self.segments[seg] = {'guide_points': {id1: set(), id2: set()}} if prev_seg is not None: self.segments[seg]['guide_points'][id1].add(prev_id1) self.segments[prev_seg]['guide_points'][id1].add(id2) prev_seg = seg prev_id1 = id1 def __generate_shapes_for_segments(self): """Generates a curve for each connection of two stops / transfer nodes.""" for (id1, id2), info in self.segments.items(): point1 = [self.__get_stop(id1)['point']['x'], self.__get_stop(id1)['point']['y']] point2 = [self.__get_stop(id2)['point']['x'], self.__get_stop(id2)['point']['y']] if info['guide_points'][id1]: guide1 = self.__get_average_stops_point(info['guide_points'][id1]) else: guide1 = [2 * point1[0] - point2[0], 2 * point1[1] - point2[1]] if info['guide_points'][id2]: guide2 = self.__get_average_stops_point(info['guide_points'][id2]) else: guide2 = [2 * point2[0] - point1[0], 2 * point2[1] - point1[1]] curve_points = bezier_curves.segment_to_Catmull_Rom_curve(guide1, point1, point2, guide2, self.points_per_curve, self.alpha) info['curve'] = np.array(curve_points) polyline = [] for point in curve_points: polyline.append({'x': point[0], 'y': point[1]}) shape = {'id': {'stop1_id': id1, 'stop2_id': id2}, 'polyline': polyline} self.shapes.append(shape) def __assign_shapes_to_edges(self): """Assigns a shape to each non-transfer edge.""" for edge in self.edges: if not edge['transfer']: stop1 = self.stops[edge['stop1_id']] stop2 = self.stops[edge['stop2_id']] id1 = stop1.get('transfer_id', stop1['id']) id2 = stop2.get('transfer_id', stop2['id']) seg = tuple(sorted([id1, id2])) if seg in self.segments: edge['shape_ids'].append({'stop1_id': seg[0], 'stop2_id': seg[1]}) def __create_scheme_shapes(self): self.__collect_segments() self.__generate_shapes_for_segments() self.__assign_shapes_to_edges() def build(self): if self.transit_graph is not None: return self.transit_graph self.__read_stops() self.__read_transfers() self.__read_networks() self.__generate_transfer_nodes() self.__create_scheme_shapes() self.transit_graph = {'networks': self.networks, 'lines': self.lines, 'gates': list(self.gates.values()), 'stops': list(self.stops.values()), 'transfers': list(self.transfers.values()), 'shapes': self.shapes, 'edges': self.edges} return self.transit_graph def show_preview(self): import matplotlib.pyplot as plt for (s1, s2), info in self.segments.items(): plt.plot(info['curve'][:, 0], info['curve'][:, 1], 'g') for stop in self.stops.values(): if 'transfer_id' in stop: point = self.transfers[stop['transfer_id']]['point'] size = 60 color = 'r' else: point = stop['point'] if len(stop['line_ids']) > 2: size = 40 color = 'b' else: size = 20 color = 'g' plt.scatter([point['x']], [point['y']], size, color) plt.show() def show_color_maching_table(self, title, colors_ref_table): import matplotlib.pyplot as plt import matplotlib.patches as patches fig = plt.figure() ax = fig.add_subplot(111, aspect='equal') plt.title(title) sz = 1.0 / (2.0 * len(self.matched_colors)) delta_y = sz * 0.5 for c in self.matched_colors: tokens = c.split('/') if len(tokens[1]) == 0: tokens[1] = tokens[0] ax.add_patch(patches.Rectangle((sz, delta_y), sz, sz, facecolor="#" + tokens[0], edgecolor="#" + tokens[1])) rect_title = tokens[0] if tokens[0] != tokens[1]: rect_title += "/" + tokens[1] ax.text(2.5 * sz, delta_y, rect_title + " -> ") ref_color = colors_ref_table[self.matched_colors[c]] ax.add_patch(patches.Rectangle((0.3 + sz, delta_y), sz, sz, facecolor="#" + ref_color)) ax.text(0.3 + 2.5 * sz, delta_y, ref_color + " (" + self.matched_colors[c] + ")") delta_y += sz * 2.0 plt.show() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('input_file', help='input file name of transit data') parser.add_argument('output_file', nargs='?', help='output file name of generated graph') default_colors_path = os.path.dirname(os.path.abspath(__file__)) + '/../../../data/transit_colors.txt' parser.add_argument('-c', '--colors', type=str, default=default_colors_path, help='transit colors file COLORS_FILE_PATH', metavar='COLORS_FILE_PATH') parser.add_argument('-p', '--preview', action="store_true", default=False, help="show preview of the transit scheme") parser.add_argument('-m', '--matched_colors', action="store_true", default=False, help="show the matched colors table") parser.add_argument('-a', '--alpha', type=float, default=0.5, help='the curves generator parameter value ALPHA', metavar='ALPHA') parser.add_argument('-n', '--num', type=int, default=100, help='the number NUM of points in a generated curve', metavar='NUM') args = parser.parse_args() with open(args.input_file, 'r') as input_file: data = json.load(input_file) with open(args.colors, 'r') as colors_file: colors = json.load(colors_file) transit = TransitGraphBuilder(data, colors, args.num, args.alpha) result = transit.build() output_file = args.output_file head, tail = os.path.split(os.path.abspath(args.input_file)) name, extension = os.path.splitext(tail) if output_file is None: output_file = os.path.join(head, name + '.transit' + extension) with open(output_file, 'w') as json_file: result_data = json.dumps(result, ensure_ascii=False, indent=4, sort_keys=True) json_file.write(result_data) print('Transit graph generated:', output_file) if args.preview: transit.show_preview() if args.matched_colors: colors_ref_table = {} for color_name, color_info in colors['colors'].items(): colors_ref_table[color_name] = color_info['clear'] transit.show_color_maching_table(name, colors_ref_table)
apache-2.0
pydata/pandas-gbq
tests/system/test_read_gbq_with_bqstorage.py
1
2024
"""System tests for read_gbq using the BigQuery Storage API.""" import functools import uuid import pytest pytest.importorskip("google.cloud.bigquery", minversion="1.24.0") @pytest.fixture def method_under_test(credentials): import pandas_gbq return functools.partial(pandas_gbq.read_gbq, credentials=credentials) @pytest.mark.parametrize( "query_string", ( ("SELECT * FROM (SELECT 1) WHERE TRUE = FALSE;"), ( "SELECT * FROM (SELECT TIMESTAMP('2020-02-11 16:33:32-06:00')) WHERE TRUE = FALSE;" ), ), ) def test_empty_results(method_under_test, query_string): """Test with an empty dataframe. See: https://github.com/pydata/pandas-gbq/issues/299 """ df = method_under_test( query_string, use_bqstorage_api=True, ) assert len(df.index) == 0 @pytest.mark.slow(reason="Large query for BQ Storage API tests.") def test_large_results(random_dataset, method_under_test): df = method_under_test( """ SELECT total_amount, passenger_count, trip_distance FROM `bigquery-public-data.new_york_taxi_trips.tlc_green_trips_2014` -- Select non-null rows for no-copy conversion from Arrow to pandas. WHERE total_amount IS NOT NULL AND passenger_count IS NOT NULL AND trip_distance IS NOT NULL LIMIT 10000000 """, use_bqstorage_api=True, configuration={ "query": { "destinationTable": { "projectId": random_dataset.project, "datasetId": random_dataset.dataset_id, "tableId": "".join( [ "test_read_gbq_w_bqstorage_api_", str(uuid.uuid4()).replace("-", "_"), ] ), }, "writeDisposition": "WRITE_TRUNCATE", } }, ) assert len(df) == 10000000
bsd-3-clause
charanpald/wallhack
wallhack/modelselect/RealDataSVMExp4.py
1
4740
""" Plot the ideal versus estimated penalty and see where the largest mistakes occur. """ import logging import numpy import sys import multiprocessing from sandbox.util.PathDefaults import PathDefaults from exp.modelselect.ModelSelectUtils import ModelSelectUtils from sandbox.util.Sampling import Sampling from apgl.predictors.LibSVM import LibSVM from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) numpy.seterr(all="raise") numpy.random.seed(21) dataDir = PathDefaults.getDataDir() dataDir += "modelPenalisation/regression/" outputDir = PathDefaults.getOutputDir() + "modelPenalisation/regression/SVR/" figInd = 0 loadMethod = ModelSelectUtils.loadRegressDataset datasets = ModelSelectUtils.getRegressionDatasets(True) numProcesses = multiprocessing.cpu_count() learner = LibSVM(kernel="rbf", processes=numProcesses, type="Epsilon_SVR") learner.setChunkSize(3) Cs = 2.0**numpy.arange(-10, 14, 2, dtype=numpy.float) gammas = 2.0**numpy.arange(-10, 4, 2, dtype=numpy.float) epsilons = learner.getEpsilons() numCs = Cs.shape[0] numGammas = gammas.shape[0] numEpsilons = epsilons.shape[0] learner.normModelSelect = True paramDict = {} paramDict["setC"] = Cs paramDict["setGamma"] = gammas paramDict["setEpsilon"] = epsilons #datasets = [datasets[1]] for datasetName, numRealisations in datasets: logging.debug("Dataset " + datasetName) errors = numpy.zeros(numRealisations) sampleMethod = Sampling.crossValidation alpha = 1.0 folds = 10 numRealisations = 5 numMethods = 3 sampleSizes = [50, 100, 200] sampleSizeInd = 2 sampleSize = sampleSizes[sampleSizeInd] #Lets load the learning rates betaFilename = outputDir + datasetName + "Beta.npz" beta = numpy.load(betaFilename)["arr_0"] beta = numpy.clip(beta, 0, 1) meanPenalties = numpy.zeros((numGammas, numEpsilons, numCs)) meanBetaPenalties = numpy.zeros((numGammas, numEpsilons, numCs)) meanIdealPenalities = numpy.zeros((numGammas, numEpsilons, numCs)) for j in range(numRealisations): print("") logging.debug("j=" + str(j)) trainX, trainY, testX, testY = loadMethod(dataDir, datasetName, j) logging.debug("Loaded dataset with " + str(trainX.shape) + " train and " + str(testX.shape) + " test examples") trainInds = numpy.random.permutation(trainX.shape[0])[0:sampleSize] trainX = trainX[trainInds,:] trainY = trainY[trainInds] idx = Sampling.crossValidation(folds, trainX.shape[0]) Cvs = [(folds-1)*alpha, beta[j, sampleSizeInd, :]] #Now try penalisation methodInd = 0 resultsList = learner.parallelPen(trainX, trainY, idx, paramDict, Cvs) bestLearner, trainErrors, currentPenalties = resultsList[0] meanPenalties += currentPenalties predY = bestLearner.predict(testX) #Learning rate penalisation methodInd = 1 bestLearner, trainErrors, currentPenalties = resultsList[1] meanBetaPenalties += currentPenalties predY = bestLearner.predict(testX) #Compute ideal penalties and error on training data meanIdealPenalities += learner.parallelPenaltyGrid(trainX, trainY, testX, testY, paramDict) numRealisations = float(numRealisations) meanPenalties /= numRealisations meanBetaPenalties /= numRealisations meanIdealPenalities /= numRealisations print("\n") highPens = meanIdealPenalities > 0.6 inds = numpy.nonzero(highPens) print(inds) for ind in range(inds[0].shape[0]): a = inds[0][ind] b = inds[1][ind] c = inds[2][ind] print(gammas[a], epsilons[b], Cs[c]) approxPenalties = meanPenalties.flatten() betaPenalties = meanBetaPenalties.flatten() idealPenalties = meanIdealPenalities.flatten() lr = LinearRegression() lr.fit(numpy.array([idealPenalties]).T, numpy.array([approxPenalties]).T) predApprox = lr.predict(numpy.array([idealPenalties]).T) print(lr.coef_) lr.fit(numpy.array([idealPenalties]).T, numpy.array([betaPenalties]).T) predBeta = lr.predict(numpy.array([idealPenalties]).T) print(lr.coef_) plt.figure(figInd) plt.scatter(idealPenalties, approxPenalties, label="Penalty", c="b") plt.plot(idealPenalties, predApprox, "b") plt.scatter(idealPenalties, betaPenalties, label="Beta", c="r") plt.plot(idealPenalties, predBeta, "r") plt.xlabel("Ideal Penalty") plt.ylabel("Approx Penalty") plt.legend(loc="upper left") figInd += 1 plt.show()
gpl-3.0
dbaranchuk/hnsw
plots/new_graphic.py
1
10314
import matplotlib.patches as mpatches from matplotlib.backends.backend_pdf import PdfPages import matplotlib.pyplot as plt import numpy import seaborn as sns sns.set(style='ticks', palette='Set2') sns.despine() SIFT_16_IMI_16384_recall = [0.329, 0.348, 0.353] SIFT_16_IMI_16384_time = [2.56, 5.31, 8.46] SIFT_16_IMI_4096_recall = [0.270, 0.307, 0.316] SIFT_16_IMI_4096_time = [0.73, 1.27, 1.85] SIFT_16_IVF_recall = [0.292, 0.331, 0.341] SIFT_16_IVF_time = [0.53, 1.04, 1.47] SIFT_16_IVF_Grouping_recall = [0.305, 0.349, 0.361] SIFT_16_IVF_Grouping_time = [0.61, 1.14, 1.62] SIFT_16_IVF_Pruning_recall = [0.330, 0.361, 0.369] SIFT_16_IVF_Pruning_time = [0.69, 1.31, 2.03] SIFT_8_IMI_16384_recall = [0.174, 0.177, 0.178] SIFT_8_IMI_16384_time = [2.16, 3.95, 6.16] SIFT_8_IMI_4096_recall = [0.145, 0.153, 0.155] SIFT_8_IMI_4096_time = [0.56, 0.89, 1.25] SIFT_8_IVF_recall = [0.146, 0.158, 0.161] SIFT_8_IVF_time = [0.47, 0.83, 1.25] SIFT_8_IVF_Grouping_recall = [0.167, 0.184, 0.188] SIFT_8_IVF_Grouping_time = [0.52, 0.99, 1.42] SIFT_8_IVF_Pruning_recall = [0.176, 0.187, 0.189] SIFT_8_IVF_Pruning_time = [0.60, 1.15, 1.76] dataset = "DEEP10" l = 1 if dataset == "SIFT": # lineIMI_16384, = plt.plot(SIFT_16_IMI_16384_time[:l], SIFT_16_IMI_16384_recall[:l], 'r^', label = 'Inverted Multi-Index 16384$^2$') # lineIMI_4096, = plt.plot(SIFT_16_IMI_4096_time[:l], SIFT_16_IMI_4096_recall[:l], 'c^', label = 'Inverted Multi-Index 4096$^2$') # lineIVF, = plt.plot(SIFT_16_IVF_time[:l], SIFT_16_IVF_recall[:l], 'g^', label = 'Inverted Index 2$^{20}$') # lineGrouping, = plt.plot(SIFT_16_IVF_Grouping_time[:l], SIFT_16_IVF_Grouping_recall[:l], 'b^', label = 'Inverted Index\nGrouping 2$^{20}$') # linePruning, = plt.plot(SIFT_16_IVF_Pruning_time[:l], SIFT_16_IVF_Pruning_recall[:l], 'k^', label = 'Inverted Index\nGrouping + Pruning 2$^{20}$') # # lineIMI_16384, = plt.plot(SIFT_8_IMI_16384_time[:l], SIFT_8_IMI_16384_recall[:l], 'ro') # lineIMI_4096, = plt.plot(SIFT_8_IMI_4096_time[:l], SIFT_8_IMI_4096_recall[:l], 'co') # lineIVF, = plt.plot(SIFT_8_IVF_time[:l], SIFT_8_IVF_recall[:l], 'go') # lineGrouping, = plt.plot(SIFT_8_IVF_Grouping_time[:l], SIFT_8_IVF_Grouping_recall[:l], 'bo') # linePruning, = plt.plot(SIFT_8_IVF_Pruning_time[:l], SIFT_8_IVF_Pruning_recall[:l], 'ko') lineIMI_16384, = plt.plot(SIFT_8_IMI_16384_time[:l]+SIFT_16_IMI_16384_time[:l], SIFT_8_IMI_16384_recall[:l]+SIFT_16_IMI_16384_recall[:l], 'r')#, label = 'Inverted Multi-Index 16384$^2$') lineIMI_4096, = plt.plot(SIFT_8_IMI_4096_time[:l]+SIFT_16_IMI_4096_time[:l], SIFT_8_IMI_4096_recall[:l]+SIFT_16_IMI_4096_recall[:l], 'c')#, label = 'Inverted Multi-Index 4096$^2$') lineIVF, = plt.plot(SIFT_8_IVF_time[:l]+SIFT_16_IVF_time[:l], SIFT_8_IVF_recall[:l]+SIFT_16_IVF_recall[:l], 'g')#, label = 'Inverted Index 2$^{20}$') lineGrouping, = plt.plot(SIFT_8_IVF_Grouping_time[:l]+SIFT_16_IVF_Grouping_time[:l], SIFT_8_IVF_Grouping_recall[:l]+SIFT_16_IVF_Grouping_recall[:l], 'b')#, label = 'Inverted Index\nGrouping 2$^{20}$') linePruning, = plt.plot(SIFT_8_IVF_Pruning_time[:l]+SIFT_16_IVF_Pruning_time[:l], SIFT_8_IVF_Pruning_recall[:l]+SIFT_16_IVF_Pruning_recall[:l], 'k')#, label = 'Inverted Index\nGrouping + Pruning 2$^{20}$') plt.xticks(numpy.arange(0., 7., 0.25)) plt.yticks(numpy.arange(0., 0.51, 0.05)) plt.axis([0, 2.6, 0.12, 0.351]) plt.xlabel('Time', fontsize=11) plt.ylabel('Recall@1', fontsize=11) #plt.legend(fontsize=9, loc=1) pp = PdfPages('R@1_SIFT.pdf') pp.savefig() pp.close() DEEP_16_IMI_16384_recall1 = [0.320, 0.359] DEEP_16_IMI_16384_time = [1.89, 2.93] DEEP_16_IMI_4096_recall1 = [0.245, 0.292] DEEP_16_IMI_4096_time = [0.81, 1.23] DEEP_16_IVF_recall1 = [0.349, 0.388] DEEP_16_IVF_time = [0.51, 0.95] DEEP_16_IVF_Grouping_recall1 = [0.369, 0.411] DEEP_16_IVF_Grouping_time = [0.56, 1.04] DEEP_16_IVF_Pruning_recall1 = [0.389, 0.421] DEEP_16_IVF_Pruning_time = [0.63, 1.12] DEEP_8_IMI_16384_recall1 = [0.196, 0.210] DEEP_8_IMI_16384_time = [1.66, 2.39] DEEP_8_IMI_4096_recall1 = [0.161, 0.179] DEEP_8_IMI_4096_time = [0.56, 0.82] DEEP_8_IVF_recall1 = [0.214, 0.228] DEEP_8_IVF_time = [0.42, 0.78] DEEP_8_IVF_Grouping_recall1 = [0.226, 0.241] DEEP_8_IVF_Grouping_time = [0.46, 0.87] DEEP_8_IVF_Pruning_recall1 = [0.234, 0.245] DEEP_8_IVF_Pruning_time = [0.50, 1.01] if dataset == "DEEP": lineIMI_16384, = plt.plot(DEEP_16_IMI_16384_time, DEEP_16_IMI_16384_recall1, 'r^', label = 'Inverted Multi-Index 16384$^2$') lineIMI_4096, = plt.plot(DEEP_16_IMI_4096_time, DEEP_16_IMI_4096_recall1, 'c^', label = 'Inverted Multi-Index 4096$^2$') lineIVF, = plt.plot(DEEP_16_IVF_time, DEEP_16_IVF_recall1, 'g^', label = 'Inverted Index 2$^{20}$') lineGrouping, = plt.plot(DEEP_16_IVF_Grouping_time, DEEP_16_IVF_Grouping_recall1, 'b^', label = 'Inverted Index\nGrouping 2$^{20}$') linePruning, = plt.plot(DEEP_16_IVF_Pruning_time, DEEP_16_IVF_Pruning_recall1, 'k^', label = 'Inverted Index\nGrouping + Pruning 2$^{20}$') lineIMI_16384, = plt.plot(DEEP_8_IMI_16384_time, DEEP_8_IMI_16384_recall1, 'ro') lineIMI_4096, = plt.plot(DEEP_8_IMI_4096_time, DEEP_8_IMI_4096_recall1, 'co') lineIVF, = plt.plot(DEEP_8_IVF_time, DEEP_8_IVF_recall1, 'go') lineGrouping, = plt.plot(DEEP_8_IVF_Grouping_time, DEEP_8_IVF_Grouping_recall1, 'bo') linePruning, = plt.plot(DEEP_8_IVF_Pruning_time, DEEP_8_IVF_Pruning_recall1, 'ko') plt.xticks(numpy.arange(0., 7., 0.2)) plt.yticks(numpy.arange(0., 0.51, 0.05)) plt.axis([0, 2, 0.12, 0.46]) plt.xlabel('Time', fontsize=11) plt.ylabel('Recall@1', fontsize=11) plt.legend(fontsize=11, loc=1) pp = PdfPages('R@1_DEEP.pdf') pp.savefig() pp.close() if dataset == "DEEP10": DEEP_16_IMI_16384_recall10 = [0.557, 0.671] DEEP_16_IMI_4096_recall10 = [0.431, 0.542] DEEP_16_IVF_recall10 = [0.612, 0.719] DEEP_16_IVF_Grouping_recall10 = [0.627, 0.736] DEEP_16_IVF_Pruning_recall10 = [0.679, 0.68] DEEP_8_IMI_16384_recall10 = [0.413, 0.457] DEEP_8_IMI_4096_recall10 = [0.320, 0.382] DEEP_8_IVF_recall10 = [0.447, 0.492] DEEP_8_IVF_Grouping_recall10 = [0.470, 0.519] DEEP_8_IVF_Pruning_recall10 = [0.496, 0.531] sns.set_style("ticks") lineIMI_16384_16, = plt.plot(DEEP_16_IMI_16384_time[:l], DEEP_16_IMI_16384_recall10[:l], 'r^', label = 'Inverted Multi-Index 16384$^2$') lineIMI_4096_16, = plt.plot(DEEP_16_IMI_4096_time[:l], DEEP_16_IMI_4096_recall10[:l], 'c^', label = 'Inverted Multi-Index 4096$^2$') lineIVF_16, = plt.plot(DEEP_16_IVF_time[:l], DEEP_16_IVF_recall10[:l], 'g^', label = 'Inverted Index 2$^{20}$') lineGrouping_16, = plt.plot(DEEP_16_IVF_Grouping_time[:l], DEEP_16_IVF_Grouping_recall10[:l], 'b^', label = 'Inverted Index Grouping 2$^{20}$') linePruning_16, = plt.plot(DEEP_16_IVF_Pruning_time[:l], DEEP_16_IVF_Pruning_recall10[:l], 'm^', label = 'Inverted Index Grouping + Pruning 2$^{20}$') lineIMI_16384_8, = plt.plot(DEEP_8_IMI_16384_time[:l], DEEP_8_IMI_16384_recall10[:l], 'ro', label = '') lineIMI_4096_8, = plt.plot(DEEP_8_IMI_4096_time[:l], DEEP_8_IMI_4096_recall10[:l], 'co', label = '') lineIVF_8, = plt.plot(DEEP_8_IVF_time[:l], DEEP_8_IVF_recall10[:l], 'go', label = '') lineGrouping_8, = plt.plot(DEEP_8_IVF_Grouping_time[:l], DEEP_8_IVF_Grouping_recall10[:l], 'bo', label = '') linePruning_8, = plt.plot(DEEP_8_IVF_Pruning_time[:l], DEEP_8_IVF_Pruning_recall10[:l], 'mo', label = '') plt.xticks(numpy.arange(0., 7., 0.2)) plt.yticks(numpy.arange(0., 1, 0.05)) plt.axis([0, 2, 0.30, 0.71]) plt.xlabel('Time', fontsize=11) plt.ylabel('Recall@10', fontsize=11) red_patch = mpatches.Patch(color='red', label='Inverted Multi-Index 16384$^2$') cyan_patch = mpatches.Patch(color='cyan', label='Inverted Multi-Index 4096$^2$') green_patch = mpatches.Patch(color='green', label='Inverted Index 2$^{20}$') blue_patch = mpatches.Patch(color='blue', label='Inverted Index Grouping 2$^{20}$') magenta_patch = mpatches.Patch(color='magenta', label='Inverted Index Grouping + Pruning 2$^{20}$') leg = plt.legend(frameon = False, fontsize=9, handles=[red_patch, cyan_patch, green_patch, blue_patch, magenta_patch], loc='best') PQ8, = plt.plot([], [], 'k^', label = '8 bytes') PQ16, = plt.plot([], [], 'ko', label = '16 bytes') leg1 = plt.legend(frameon = False, fontsize=9, handles=[PQ8, PQ16], bbox_to_anchor=[0.5, 1], loc=1) # redLine = plt.plot([100], [100], 'r', label = 'Inverted Multi-Index 16384$^2$') # ceulLine, = plt.plot(DEEP_16_IMI_4096_time[:l], DEEP_16_IMI_4096_recall10[:l], 'c^', label = 'Inverted Multi-Index 4096$^2$') # greenLine, = plt.plot(DEEP_16_IVF_time[:l], DEEP_16_IVF_recall10[:l], 'g^', label = 'Inverted Index 2$^{20}$') # lineGrouping_16, = plt.plot(DEEP_16_IVF_Grouping_time[:l], DEEP_16_IVF_Grouping_recall10[:l], 'b^', label = 'Inverted Index\nGrouping 2$^{20}$') # linePruning_16, = plt.plot(DEEP_16_IVF_Pruning_time[:l], DEEP_16_IVF_Pruning_recall10[:l], 'k^', label = 'Inverted Index\nGrouping + Pruning 2$^{20}$') #leg1 = plt.legend((lineIMI_16384_8, lineIMI_4096_8, lineIVF_8, lineGrouping_8, linePruning_8), ['','','','',''], ncol=1, numpoints=1, # title='', handletextpad=-0.4, # bbox_to_anchor=[0.47, 1.], fontsize=9) # leg2 = plt.legend((lineIMI_4096_16, lineIMI_4096_8), ['', ''], ncol=1, numpoints=1, # title='Inverted Multi-Index 4096$^2$', handletextpad=-0.4, # bbox_to_anchor=[0.87, 1.], fontsize=12) # leg3 = plt.legend((lineIVF_16, lineIVF_8), ['', ''], ncol=1, numpoints=1, # title='Inverted Index 2$^{20}$', handletextpad=-0.4, # bbox_to_anchor=[0.99, 1.], fontsize=12) plt.gca().add_artist(leg) plt.gca().add_artist(leg1) # plt.gca().add_artist(leg3) pp = PdfPages('R@10_DEEP.pdf') pp.savefig(bbox_inches='tight') pp.close()
apache-2.0
opencobra/cobrapy
src/cobra/medium/boundary_types.py
1
6160
"""Provide functions to identify the type of boundary reactions. This module uses various heuristics to decide whether a boundary reaction is an exchange, demand or sink reaction. It mostly orientates on the following paper: Thiele, I., & Palsson, B. Ø. (2010, January). A protocol for generating a high-quality genome-scale metabolic reconstruction. Nature protocols. Nature Publishing Group. http://doi.org/10.1038/nprot.2009.203 """ import logging from typing import TYPE_CHECKING, List, Optional import pandas as pd from .annotations import compartment_shortlist, excludes, sbo_terms if TYPE_CHECKING: from cobra import Model, Reaction logger = logging.getLogger(__name__) def find_external_compartment(model: "Model") -> str: """Find the external compartment in the model. Uses a simple heuristic where the external compartment should be the one with the most exchange reactions. Parameters ---------- model : cobra.Model The cobra model whose external compartments are to be identified. Returns ------- str The putative external compartment. Raises ------ RuntimeError If several compartments are similar and thus difficult to identify, or, recognized names usually used for external compartment are absent. """ if model.boundary: counts = pd.Series(tuple(r.compartments)[0] for r in model.boundary) most = counts.value_counts() most = most.index[most == most.max()].to_series() else: most = None like_external = compartment_shortlist["e"] + ["e"] matches = pd.Series( [co in like_external for co in model.compartments], index=model.compartments ) if matches.sum() == 1: compartment = matches.index[matches][0] logger.info( f"Compartment `{compartment}` sounds like an external compartment. " "Using this one without counting boundary reactions." ) return compartment elif most is not None and matches.sum() > 1 and matches[most].sum() == 1: compartment = most[matches[most]][0] logger.warning( "There are several compartments that look like an " f"external compartment but `{compartment}` has the most boundary " "reactions, so using that as the external compartment." ) return compartment elif matches.sum() > 1: raise RuntimeError( "There are several compartments that look " "like external compartments but we can't tell " "which one to use. Consider renaming your " "compartments please." ) if most is not None: return most[0] logger.warning( "Could not identify an external compartment by name and " "choosing one with the most boundary reactions. That " "might be complete nonsense or change suddenly. " "Consider renaming your compartments using " "`Model.compartments` to fix this." ) # No info in the model, so give up raise RuntimeError( "The heuristic for discovering an external compartment " "relies on names and boundary reactions. Yet, there " "are neither compartments with recognized names nor " "boundary reactions in the model." ) def is_boundary_type( reaction: "Reaction", boundary_type: str, external_compartment: str ) -> bool: """Check whether a reaction is an exchange reaction. Parameters ---------- reaction : cobra.Reaction The reaction to check. boundary_type : {"exchange", "demand", "sink"} Boundary type to check for. external_compartment : str The ID for the external compartment. Returns ------- bool Whether the reaction looks like the requested type. Might be based on a heuristic. """ # Check if the reaction has an annotation. Annotations dominate everything. sbo_term = reaction.annotation.get("sbo", "") if isinstance(sbo_term, list): sbo_term = sbo_term[0] sbo_term = sbo_term.upper() if sbo_term == sbo_terms[boundary_type]: return True if sbo_term in [sbo_terms[k] for k in sbo_terms if k != boundary_type]: return False # Check if the reaction is in the correct compartment (exterior or inside) correct_compartment = external_compartment in reaction.compartments if boundary_type != "exchange": correct_compartment = not correct_compartment # Check if the reaction has the correct reversibility rev_type = True if boundary_type == "demand": rev_type = not reaction.reversibility elif boundary_type == "sink": rev_type = reaction.reversibility return ( reaction.boundary and not any(ex in reaction.id for ex in excludes[boundary_type]) and correct_compartment and rev_type ) def find_boundary_types( model: "Model", boundary_type: str, external_compartment: Optional[str] = None ) -> List["Reaction"]: """Find specific boundary reactions. Parameters ---------- model : cobra.Model The cobra model whose boundary reactions are to be found. boundary_type : {"exchange", "demand", "sink"} Boundary type to check for. external_compartment : str, optional The ID for the external compartment. If None, it will be detected automatically (default None). Returns ------- list of cobra.Reaction or an empty list A list of likely boundary reactions of a user defined type. """ if not model.boundary: logger.warning( "There are no boundary reactions in this model. " "Therefore, specific types of boundary reactions such " "as 'exchanges', 'demands' or 'sinks' cannot be " "identified." ) return [] if external_compartment is None: external_compartment = find_external_compartment(model) return model.reactions.query( lambda r: is_boundary_type(r, boundary_type, external_compartment) )
gpl-2.0
siutanwong/scikit-learn
benchmarks/bench_plot_lasso_path.py
301
4003
"""Benchmarks of Lasso regularization path computation using Lars and CD The input data is mostly low rank but is a fat infinite tail. """ from __future__ import print_function from collections import defaultdict import gc import sys from time import time import numpy as np from sklearn.linear_model import lars_path from sklearn.linear_model import lasso_path from sklearn.datasets.samples_generator import make_regression def compute_bench(samples_range, features_range): it = 0 results = defaultdict(lambda: []) max_it = len(samples_range) * len(features_range) for n_samples in samples_range: for n_features in features_range: it += 1 print('====================') print('Iteration %03d of %03d' % (it, max_it)) print('====================') dataset_kwargs = { 'n_samples': n_samples, 'n_features': n_features, 'n_informative': n_features / 10, 'effective_rank': min(n_samples, n_features) / 10, #'effective_rank': None, 'bias': 0.0, } print("n_samples: %d" % n_samples) print("n_features: %d" % n_features) X, y = make_regression(**dataset_kwargs) gc.collect() print("benchmarking lars_path (with Gram):", end='') sys.stdout.flush() tstart = time() G = np.dot(X.T, X) # precomputed Gram matrix Xy = np.dot(X.T, y) lars_path(X, y, Xy=Xy, Gram=G, method='lasso') delta = time() - tstart print("%0.3fs" % delta) results['lars_path (with Gram)'].append(delta) gc.collect() print("benchmarking lars_path (without Gram):", end='') sys.stdout.flush() tstart = time() lars_path(X, y, method='lasso') delta = time() - tstart print("%0.3fs" % delta) results['lars_path (without Gram)'].append(delta) gc.collect() print("benchmarking lasso_path (with Gram):", end='') sys.stdout.flush() tstart = time() lasso_path(X, y, precompute=True) delta = time() - tstart print("%0.3fs" % delta) results['lasso_path (with Gram)'].append(delta) gc.collect() print("benchmarking lasso_path (without Gram):", end='') sys.stdout.flush() tstart = time() lasso_path(X, y, precompute=False) delta = time() - tstart print("%0.3fs" % delta) results['lasso_path (without Gram)'].append(delta) return results if __name__ == '__main__': from mpl_toolkits.mplot3d import axes3d # register the 3d projection import matplotlib.pyplot as plt samples_range = np.linspace(10, 2000, 5).astype(np.int) features_range = np.linspace(10, 2000, 5).astype(np.int) results = compute_bench(samples_range, features_range) max_time = max(max(t) for t in results.values()) fig = plt.figure('scikit-learn Lasso path benchmark results') i = 1 for c, (label, timings) in zip('bcry', sorted(results.items())): ax = fig.add_subplot(2, 2, i, projection='3d') X, Y = np.meshgrid(samples_range, features_range) Z = np.asarray(timings).reshape(samples_range.shape[0], features_range.shape[0]) # plot the actual surface ax.plot_surface(X, Y, Z.T, cstride=1, rstride=1, color=c, alpha=0.8) # dummy point plot to stick the legend to since surface plot do not # support legends (yet?) #ax.plot([1], [1], [1], color=c, label=label) ax.set_xlabel('n_samples') ax.set_ylabel('n_features') ax.set_zlabel('Time (s)') ax.set_zlim3d(0.0, max_time * 1.1) ax.set_title(label) #ax.legend() i += 1 plt.show()
bsd-3-clause
cdr-stats/cdr-stats
cdr_stats/cdr_alert/tasks.py
1
19322
# -*- coding: utf-8 -*- # # CDR-Stats License # http://www.cdr-stats.org # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. # # Copyright (C) 2011-2015 Star2Billing S.L. # # The Initial Developer of the Original Code is # Arezqui Belaid <info@star2billing.com> # from __future__ import division from django.conf import settings from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ from django.core.mail import send_mail, mail_admins from django.core.mail import EmailMultiAlternatives from django.template.loader import get_template from django.template import Context from celery.task import PeriodicTask, task from notification import models as notification from django_lets_go.only_one_task import only_one from cdr_alert.constants import PERIOD, ALARM_TYPE, ALERT_CONDITION, ALERT_CONDITION_ADD_ON,\ ALARM_REPROT_STATUS from aggregator.pandas_cdr import get_report_cdr_per_switch from aggregator.aggregate_cdr import custom_sql_aggr_top_hangup from cdr_alert.models import Alarm, AlarmReport from cdr.functions_def import get_hangupcause_id from cdr.views import get_cdr_mail_report from user_profile.models import UserProfile from user_profile.constants import NOTICE_TYPE from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta import math # Lock expires in 30 minutes LOCK_EXPIRE = 60 * 30 def get_start_end_date(alert_condition_add_on): """Get start and end date according to alert_condition return a list with * p_start_date * p_end_date * c_start_date * c_end_date """ date_dict = {} # yesterday's date end_date = datetime.today() + relativedelta(days=-1) if alert_condition_add_on == ALERT_CONDITION_ADD_ON.SAME_DAY: # Same day compare_days = 1 if alert_condition_add_on == ALERT_CONDITION_ADD_ON.SAME_DAY_IN_PRE_WEEK: # Same day in the previous week compare_days = 7 start_date = end_date + relativedelta(days=-int(compare_days)) # get Previous dates and Current dates date_dict['p_start_date'] = datetime(start_date.year, start_date.month, start_date.day, 0, 0, 0, 0) date_dict['p_end_date'] = datetime(start_date.year, start_date.month, start_date.day, 23, 59, 59, 999999) date_dict['c_start_date'] = datetime(end_date.year, end_date.month, end_date.day, 0, 0, 0, 0) date_dict['c_end_date'] = datetime(end_date.year, end_date.month, end_date.day, 23, 59, 59, 999999) return date_dict def notify_admin_with_mail(notice_id, email_id): """Send notification to all admin as well as mail to recipient of alarm >>> notify_admin_with_mail(1, 'xyz@localhost.com') True """ # Get all the admin users - admin superuser for user in User.objects.filter(is_superuser=True): recipient = user # send notification if notification: note_label = notification.NoticeType.objects.get(default=notice_id).label notification.send([recipient], note_label, {'from_user': user}, sender=user) # Send mail to ADMINS subject = _('Alert') message = _('Alert Message "%(user)s" - "%(user_id)s"') % {'user': user, 'user_id': user.id} try: send_mail(subject, message, settings.SERVER_EMAIL, email_id) except: # send an email to the site admins as defined in the ADMINS setting mail_admins(subject, message) # html_message='text/html' return True def create_alarm_report_object(alarm_obj, status): # create alarm report # status - 1 - No alarm sent # status - 2 - Alarm sent try: AlarmReport.objects.create(alarm=alarm_obj, calculatedvalue=alarm_obj.alert_value, status=status) return True except: return False def chk_alert_value(alarm_obj, current_value, previous_value=None): """ compare values with following conditions against alarm alert value * Is less than | Is greater than * Decrease by more than | Increase by more than * % decrease by more than | % Increase by more than """ if alarm_obj.alert_condition == ALERT_CONDITION.IS_LESS_THAN: # Is less than if alarm_obj.alert_value < current_value: notify_admin_with_mail(alarm_obj.type, alarm_obj.email_to_send_alarm) create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.ALARM_SENT) else: create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.NO_ALARM_SENT) if alarm_obj.alert_condition == ALERT_CONDITION.IS_GREATER_THAN: # Is greater than if alarm_obj.alert_value > current_value: notify_admin_with_mail(alarm_obj.type, alarm_obj.email_to_send_alarm) create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.ALARM_SENT) else: create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.NO_ALARM_SENT) if alarm_obj.alert_condition == ALERT_CONDITION.DECREASE_BY_MORE_THAN: # Decrease by more than diff = abs(current_value - previous_value) if diff < alarm_obj.alert_value: notify_admin_with_mail(alarm_obj.type, alarm_obj.email_to_send_alarm) create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.ALARM_SENT) else: create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.NO_ALARM_SENT) if alarm_obj.alert_condition == ALERT_CONDITION.INCREASE_BY_MORE_THAN: # Increase by more than diff = abs(current_value - previous_value) if diff > alarm_obj.alert_value: notify_admin_with_mail(alarm_obj.type, alarm_obj.email_to_send_alarm) create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.ALARM_SENT) else: create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.NO_ALARM_SENT) # http://www.mathsisfun.com/percentage-difference.html if alarm_obj.alert_condition == ALERT_CONDITION.PERCENTAGE_DECREASE_BY_MORE_THAN: # % decrease by more than diff = abs(current_value - previous_value) avg = (current_value + previous_value) / 2 avg = avg if avg != 0 else 1 percentage = diff / avg * 100 if percentage < alarm_obj.alert_value: notify_admin_with_mail(alarm_obj.type, alarm_obj.email_to_send_alarm) create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.ALARM_SENT) else: create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.NO_ALARM_SENT) if alarm_obj.alert_condition == ALERT_CONDITION.PERCENTAGE_INCREASE_BY_MORE_THAN: # % Increase by more than diff = abs(current_value - previous_value) avg = (current_value + previous_value) / 2 avg = avg if avg != 0 else 1 percentage = diff / avg * 100 if percentage > alarm_obj.alert_value: notify_admin_with_mail(alarm_obj.type, alarm_obj.email_to_send_alarm) create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.ALARM_SENT) else: create_alarm_report_object(alarm_obj, status=ALARM_REPROT_STATUS.NO_ALARM_SENT) return True def run_alarm(alarm_obj, logger): """ Perform Alarm Check """ running_alarm_test_data = { 'running_alarm_status': True, 'current_value': None, 'previous_value': None, } user = False switch_id = 0 if alarm_obj.type == ALARM_TYPE.ALOC: # ALOC (average length of call) logger.debug('ALOC (Average Length Of Call)') # return start and end date of previous/current day date_dict = get_start_end_date(alarm_obj.alert_condition_add_on) # Previous date data start_date = date_dict['p_start_date'] end_date = date_dict['p_end_date'] daily_data = get_report_cdr_per_switch(user, 'day', start_date, end_date, switch_id) total_calls = daily_data["nbcalls"]["total"] total_duration = daily_data["duration"]["total"] ACD = math.floor(total_duration / total_calls) if alarm_obj.alert_condition == ALERT_CONDITION.IS_LESS_THAN or \ alarm_obj.alert_condition == ALERT_CONDITION.IS_GREATER_THAN: running_alarm_test_data['previous_value'] = ACD chk_alert_value(alarm_obj, ACD) else: previous_date_duration = ACD # Current date data start_date = date_dict['c_start_date'] end_date = date_dict['c_end_date'] daily_data = get_report_cdr_per_switch(user, 'day', start_date, end_date, switch_id) total_calls = daily_data["nbcalls"]["total"] total_duration = daily_data["duration"]["total"] ACD = math.floor(total_duration / total_calls) if alarm_obj.alert_condition == ALERT_CONDITION.IS_LESS_THAN or \ alarm_obj.alert_condition == ALERT_CONDITION.IS_GREATER_THAN: running_alarm_test_data['current_value'] = ACD chk_alert_value(alarm_obj, ACD) else: current_date_duration = ACD running_alarm_test_data['current_value'] = ACD running_alarm_test_data['previous_value'] = previous_date_duration chk_alert_value(alarm_obj, current_date_duration, previous_date_duration) elif alarm_obj.type == ALARM_TYPE.ASR: # ASR (Answer Seize Ratio) logger.debug('ASR (Answer Seize Ratio)') # return start and end date of previous/current day date_dict = get_start_end_date(alarm_obj.alert_condition_add_on) # hangup_cause_q850 - 16 - NORMAL_CLEARING hangup_cause_q850 = 16 # Previous date data start_date = date_dict['p_start_date'] end_date = date_dict['p_end_date'] limit = 10 hangup_cause_id = False # TODO: Regroup the 2 calls to custom_sql_aggr_top_hangup to get the hangup (hangup_cause_data, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost) = \ custom_sql_aggr_top_hangup(user, switch_id, hangup_cause_id, limit, start_date, end_date) pre_total_record = total_calls hangup_cause_id = get_hangupcause_id(hangup_cause_q850) (hangup_cause_data, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost) = \ custom_sql_aggr_top_hangup(user, switch_id, hangup_cause_id, limit, start_date, end_date) pre_total_answered_record = total_calls # pre_total_record should not be 0 pre_total_record = 1 if pre_total_record == 0 else pre_total_record previous_asr = pre_total_answered_record / pre_total_record if alarm_obj.alert_condition == ALERT_CONDITION.IS_LESS_THAN or \ alarm_obj.alert_condition == ALERT_CONDITION.IS_GREATER_THAN: running_alarm_test_data['previous_value'] = previous_asr chk_alert_value(alarm_obj, previous_asr) else: previous_asr = previous_asr # Current date data start_date = date_dict['c_start_date'] end_date = date_dict['c_end_date'] limit = 10 hangup_cause_id = False # TODO: Regroup the 2 calls to custom_sql_aggr_top_hangup to get the hangup (hangup_cause_data, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost) = \ custom_sql_aggr_top_hangup(user, switch_id, hangup_cause_id, limit, start_date, end_date) cur_total_record = total_calls hangup_cause_id = get_hangupcause_id(hangup_cause_q850) (hangup_cause_data, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost) = \ custom_sql_aggr_top_hangup(user, switch_id, hangup_cause_id, limit, start_date, end_date) cur_total_answered_record = total_calls # cur_total_record should not be 0 cur_total_record = 1 if cur_total_record == 0 else cur_total_record current_asr = cur_total_answered_record / cur_total_record if alarm_obj.alert_condition == ALERT_CONDITION.IS_LESS_THAN or \ alarm_obj.alert_condition == ALERT_CONDITION.IS_GREATER_THAN: running_alarm_test_data['current_value'] = current_asr chk_alert_value(alarm_obj, current_asr) else: running_alarm_test_data['current_value'] = current_asr running_alarm_test_data['previous_value'] = previous_asr chk_alert_value(alarm_obj, current_asr, previous_asr) return running_alarm_test_data class chk_alarm(PeriodicTask): """A periodic task to determine unusual call patterns. Sends an email if an alert condition is matched. **Usage**: chk_alarm.delay() """ run_every = timedelta(seconds=86400) # every day def run(self, **kwargs): logger = self.get_logger(**kwargs) logger.info('TASK :: chk_alarm called') alarm_objs = Alarm.objects.filter(status=1) # all active alarms alarm_status = {} alarm_status['running_alarm_status'] = True for alarm_obj in alarm_objs: try: alarm_report = AlarmReport.objects.filter(alarm=alarm_obj).latest('daterun') diff_run = (datetime.now() - alarm_report.daterun).days diff_run = 1 if alarm_obj.period == PERIOD.DAY: # Day if diff_run == 1: # every day # Run alert task logger.debug('Run alarm') alarm_status = run_alarm(alarm_obj, logger) if alarm_obj.period == PERIOD.WEEK: # Week if diff_run == 7: # every week # Run alert task logger.debug('Run alarm') alarm_status = run_alarm(alarm_obj, logger) if alarm_obj.period == PERIOD.MONTH: # Month if diff_run == 30: # every month # Run alert task logger.debug('Run alarm') alarm_status = run_alarm(alarm_obj, logger) except: # create alarm report AlarmReport.objects.create(alarm=alarm_obj, calculatedvalue=alarm_obj.alert_value, status=1) logger.debug('TASK :: chk_alarm finished') return alarm_status['running_alarm_status'] def notify_admin_without_mail(notice_id, email_id): """Send notification to admin as well as mail to recipient of alarm""" # Get all the admin users - admin superuser all_admin_user = User.objects.filter(is_superuser=True) for user in all_admin_user: recipient = user # send notification if notification: note_label = notification.NoticeType.objects.get(default=notice_id).label notification.send([recipient], note_label, {'from_user': user}, sender=user) return True @task def blacklist_whitelist_notification(notice_type): """ Send email notification whne destination number matched with blacklist or whitelist. **Usage**: blacklist_whitelist_notification.delay(notice_type) """ if notice_type == NOTICE_TYPE.blacklist_prefix: notice_type_name = 'blacklist' if notice_type == NOTICE_TYPE.whitelist_prefix: notice_type_name = 'whitelist' logger = blacklist_whitelist_notification.get_logger() logger.info('TASK :: %s_notification called' % notice_type_name) notice_type_obj = notification.NoticeType.objects.get(default=notice_type) try: notice_obj = notification.Notice.objects.\ filter(notice_type=notice_type_obj).\ latest('added') # Get time difference between two time intervals prevtime = str(datetime.time(notice_obj.added.replace(microsecond=0))) curtime = str(datetime.time(datetime.now().replace(microsecond=0))) FMT = '%H:%M:%S' diff = datetime.strptime(curtime, FMT) - datetime.strptime(prevtime, FMT) # if difference is more than X min than notification resend if int(diff.seconds / 60) >= settings.DELAY_BETWEEN_MAIL_NOTIFICATION: # blacklist notification id - 3 | whitelist notification type - 4 notify_admin_without_mail(notice_type, 'admin@localhost.com') except: # blacklist notification type - 3 | whitelist notification type - 4 notify_admin_without_mail(notice_type, 'admin@localhost.com') logger.debug('TASK :: %s_notification finished' % notice_type_name) return True # Email previous day's CDR Report class send_cdr_report(PeriodicTask): """A periodic task to send previous day's CDR Report as mail **Usage**: send_cdr_report.delay() """ run_every = timedelta(seconds=86400) # every day @only_one(ikey="send_cdr_report", timeout=LOCK_EXPIRE) def run(self, **kwargs): logger = self.get_logger() logger.info('TASK :: send_cdr_report') list_users = User.objects.filter(is_staff=True, is_active=True) for c_user in list_users: if not c_user.email: logger.error("User (%s) -> This user doesn't have an email." % c_user.username) continue else: logger.error("Send Report from User (%s - %s)." % (c_user.username, c_user.email)) try: to_email = UserProfile.objects.get(user=c_user).multiple_email except UserProfile.DoesNotExist: logger.error('Error : UserProfile notfound (user_id:%d)' % c_user.id) continue if not to_email: logger.error('Error: UserProfile multiple_email not set (user_id:' + str(c_user.id) + ')') continue from_email = c_user.email mail_data = get_cdr_mail_report(c_user) subject = 'CDR Report' html_content = get_template('cdr/mail_report_template.html')\ .render(Context({ 'yesterday_date': mail_data['yesterday_date'], 'rows': mail_data['rows'], 'total_duration': mail_data['total_duration'], 'total_calls': mail_data['total_calls'], 'total_buy_cost': mail_data['total_buy_cost'], 'total_sell_cost': mail_data['total_sell_cost'], 'metric_aggr': mail_data['metric_aggr'], 'country_data': mail_data['country_data'], 'hangup_cause_data': mail_data['hangup_cause_data'] })) msg = EmailMultiAlternatives(subject, html_content, from_email, [to_email]) logger.info('Email sent to %s' % str(to_email)) msg.content_subtype = 'html' msg.send() logger.debug('TASK :: send_cdr_report finished') return True
mpl-2.0
ElDeveloper/scikit-learn
benchmarks/bench_tree.py
297
3617
""" To run this, you'll need to have installed. * scikit-learn Does two benchmarks First, we fix a training set, increase the number of samples to classify and plot number of classified samples as a function of time. In the second benchmark, we increase the number of dimensions of the training set, classify a sample and plot the time taken as a function of the number of dimensions. """ import numpy as np import pylab as pl import gc from datetime import datetime # to store the results scikit_classifier_results = [] scikit_regressor_results = [] mu_second = 0.0 + 10 ** 6 # number of microseconds in a second def bench_scikit_tree_classifier(X, Y): """Benchmark with scikit-learn decision tree classifier""" from sklearn.tree import DecisionTreeClassifier gc.collect() # start time tstart = datetime.now() clf = DecisionTreeClassifier() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_classifier_results.append( delta.seconds + delta.microseconds / mu_second) def bench_scikit_tree_regressor(X, Y): """Benchmark with scikit-learn decision tree regressor""" from sklearn.tree import DecisionTreeRegressor gc.collect() # start time tstart = datetime.now() clf = DecisionTreeRegressor() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_regressor_results.append( delta.seconds + delta.microseconds / mu_second) if __name__ == '__main__': print('============================================') print('Warning: this is going to take a looong time') print('============================================') n = 10 step = 10000 n_samples = 10000 dim = 10 n_classes = 10 for i in range(n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') n_samples += step X = np.random.randn(n_samples, dim) Y = np.random.randint(0, n_classes, (n_samples,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(n_samples) bench_scikit_tree_regressor(X, Y) xx = range(0, n * step, step) pl.figure('scikit-learn tree benchmark results') pl.subplot(211) pl.title('Learning with varying number of samples') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of samples') pl.ylabel('Time (s)') scikit_classifier_results = [] scikit_regressor_results = [] n = 10 step = 500 start_dim = 500 n_classes = 10 dim = start_dim for i in range(0, n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') dim += step X = np.random.randn(100, dim) Y = np.random.randint(0, n_classes, (100,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(100) bench_scikit_tree_regressor(X, Y) xx = np.arange(start_dim, start_dim + n * step, step) pl.subplot(212) pl.title('Learning in high dimensional spaces') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of dimensions') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
nicocardiel/numina
numina/array/wavecalib/peaks_spectrum.py
3
8947
# # Copyright 2015-2021 Universidad Complutense de Madrid # # This file is part of Numina # # SPDX-License-Identifier: GPL-3.0+ # License-Filename: LICENSE.txt # import numpy as np from numpy.polynomial import Polynomial from ..display.matplotlib_qt import set_window_geometry from ..display.pause_debugplot import pause_debugplot def find_peaks_spectrum(sx, nwinwidth, threshold=0, debugplot=0): """Find peaks in array. The algorithm imposes that the signal at both sides of the peak decreases monotonically. Parameters ---------- sx : 1d numpy array, floats Input array. nwinwidth : int Width of the window where each peak must be found. threshold : float Minimum signal in the peaks. debugplot : int Determines whether intermediate computations and/or plots are displayed: 00 : no debug, no plots 01 : no debug, plots without pauses 02 : no debug, plots with pauses 10 : debug, no plots 11 : debug, plots without pauses 12 : debug, plots with pauses Returns ------- ixpeaks : 1d numpy array, int Peak locations, in array coordinates (integers). """ if not isinstance(sx, np.ndarray): raise ValueError("sx=" + str(sx) + " must be a numpy.ndarray") elif sx.ndim != 1: raise ValueError("sx.ndim=" + str(sx.ndim) + " must be 1") sx_shape = sx.shape nmed = nwinwidth//2 if debugplot >= 10: print('find_peaks_spectrum> sx shape......:', sx_shape) print('find_peaks_spectrum> nwinwidth.....:', nwinwidth) print('find_peaks_spectrum> nmed..........:', nmed) print('find_peaks_spectrum> data_threshold:', threshold) print('find_peaks_spectrum> the first and last', nmed, 'pixels will be ignored') xpeaks = [] # list to store the peaks if sx_shape[0] < nwinwidth: print('find_peaks_spectrum> sx shape......:', sx_shape) print('find_peaks_spectrum> nwinwidth.....:', nwinwidth) raise ValueError('sx.shape < nwinwidth') i = nmed while i < sx_shape[0] - nmed: if sx[i] > threshold: peak_ok = True j = 0 loop = True while loop: if sx[i - nmed + j] > sx[i - nmed + j + 1]: peak_ok = False j += 1 loop = (j < nmed) and peak_ok if peak_ok: j = nmed + 1 loop = True while loop: if sx[i - nmed + j - 1] < sx[i - nmed + j]: peak_ok = False j += 1 loop = (j < nwinwidth) and peak_ok if peak_ok: xpeaks.append(i) i += nwinwidth - 1 else: i += 1 else: i += 1 ixpeaks = np.array(xpeaks) if debugplot >= 10: print('find_peaks_spectrum> number of peaks found:', len(ixpeaks)) print(ixpeaks) return ixpeaks def refine_peaks_spectrum(sx, ixpeaks, nwinwidth, method=None, geometry=None, debugplot=0): """Refine line peaks in spectrum. Parameters ---------- sx : 1d numpy array, floats Input array. ixpeaks : 1d numpy array, int Initial peak locations, in array coordinates (integers). These values can be the output from the function find_peaks_spectrum(). nwinwidth : int Width of the window where each peak must be refined. method : string "poly2" : fit to a 2nd order polynomial "gaussian" : fit to a Gaussian geometry : tuple (4 integers) or None x, y, dx, dy values employed to set the window geometry. debugplot : int Determines whether intermediate computations and/or plots are displayed: 00 : no debug, no plots 01 : no debug, plots without pauses 02 : no debug, plots with pauses 10 : debug, no plots 11 : debug, plots without pauses 12 : debug, plots with pauses Returns ------- fxpeaks : 1d numpy array, float Refined peak locations, in array coordinates. sxpeaks : 1d numpy array, float When fitting Gaussians, this array stores the fitted line widths (sigma). Otherwise, this array returns zeros. """ nmed = nwinwidth//2 xfpeaks = np.zeros(len(ixpeaks)) sfpeaks = np.zeros(len(ixpeaks)) for iline in range(len(ixpeaks)): jmax = ixpeaks[iline] x_fit = np.arange(-nmed, nmed+1, dtype=np.float) # prevent possible problem when fitting a line too near to any # of the borders of the spectrum j1 = jmax - nmed j2 = jmax + nmed + 1 if j1 < 0: j1 = 0 j2 = 2 * nmed + 1 if j2 >= len(sx): raise ValueError("Unexpected j2=" + str(j2) + " value when len(sx)=" + str(len(sx))) if j2 >= len(sx): j2 = len(sx) j1 = j2 - (2 * nmed + 1) if j1 < 0: raise ValueError("Unexpected j1=" + str(j1) + " value when len(sx)=" + str(len(sx))) # it is important to create a copy in the next instruction in # order to avoid modifying the original array when normalizing # the data to be fitted y_fit = np.copy(sx[j1:j2].astype(float)) sx_peak_flux = y_fit.max() if sx_peak_flux != 0: y_fit /= sx_peak_flux # normalize to maximum value if method == "gaussian": # check that there are no negative or null values if y_fit.min() <= 0: if debugplot >= 10: print("WARNING: negative or null value encountered" + " in refine_peaks_spectrum with gaussian.") print(" Using poly2 method instead.") final_method = "poly2" else: final_method = "gaussian" else: final_method = method if final_method == "poly2": poly_funct = Polynomial.fit(x_fit, y_fit, 2) poly_funct = Polynomial.cast(poly_funct) coef = poly_funct.coef if len(coef) == 3: if coef[2] != 0: refined_peak = -coef[1]/(2.0*coef[2]) + jmax else: refined_peak = 0.0 + jmax else: refined_peak = 0.0 + jmax elif final_method == "gaussian": poly_funct = Polynomial.fit(x_fit, np.log(y_fit), 2) poly_funct = Polynomial.cast(poly_funct) coef = poly_funct.coef if len(coef) == 3: if coef[2] != 0: refined_peak = -coef[1]/(2.0*coef[2]) + jmax else: refined_peak = 0.0 + jmax if coef[2] >= 0: sfpeaks[iline] = None else: sfpeaks[iline] = np.sqrt(-1 / (2.0 * coef[2])) else: refined_peak = 0.0 + jmax sfpeaks[iline] = None else: raise ValueError("Invalid method=" + str(final_method) + " value") xfpeaks[iline] = refined_peak if debugplot % 10 != 0: from numina.array.display.matplotlib_qt import plt fig = plt.figure() set_window_geometry(geometry) ax = fig.add_subplot(111) xmin = x_fit.min()-1 xmax = x_fit.max()+1 ymin = 0 ymax = y_fit.max()*1.10 ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) ax.set_xlabel('index around initial integer peak') ax.set_ylabel('Normalized number of counts') ax.set_title("Fit to line at array index " + str(jmax) + "\n(method=" + final_method + ")") plt.plot(x_fit, y_fit, "bo") x_plot = np.linspace(start=-nmed, stop=nmed, num=1000, dtype=np.float) if final_method == "poly2": y_plot = poly_funct(x_plot) elif final_method == "gaussian": amp = np.exp(coef[0] - coef[1] * coef[1] / (4 * coef[2])) x0 = -coef[1] / (2.0 * coef[2]) sigma = np.sqrt(-1 / (2.0 * coef[2])) y_plot = amp * np.exp(-(x_plot - x0)**2 / (2 * sigma**2)) else: raise ValueError("Invalid method=" + str(final_method) + " value") ax.plot(x_plot, y_plot, color="red") print('Refined peak location:', refined_peak) plt.show(block=False) plt.pause(0.001) pause_debugplot(debugplot) return xfpeaks, sfpeaks
gpl-3.0
ningchi/scikit-learn
sklearn/utils/tests/test_estimator_checks.py
4
2571
import scipy.sparse as sp import numpy as np import sys from sklearn.externals.six.moves import cStringIO as StringIO from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.utils.testing import assert_raises_regex, assert_true from sklearn.utils.estimator_checks import check_estimator from sklearn.linear_model import LogisticRegression from sklearn.utils.validation import check_X_y, check_array class BaseBadClassifier(BaseEstimator, ClassifierMixin): def fit(self, X, y): return self def predict(self, X): return np.ones(X.shape[0]) class NoCheckinPredict(BaseBadClassifier): def fit(self, X, y): X, y = check_X_y(X, y) return self class NoSparseClassifier(BaseBadClassifier): def fit(self, X, y): X, y = check_X_y(X, y, accept_sparse=['csr', 'csc']) if sp.issparse(X): raise ValueError("Nonsensical Error") return self def predict(self, X): X = check_array(X) return np.ones(X.shape[0]) def test_check_estimator(): # tests that the estimator actually fails on "bad" estimators. # not a complete test of all checks, which are very extensive. # check that we have a set_params and can clone msg = "it does not implement a 'get_params' methods" assert_raises_regex(TypeError, msg, check_estimator, object) # check that we have a fit method msg = "object has no attribute 'fit'" assert_raises_regex(AttributeError, msg, check_estimator, BaseEstimator) # check that fit does input validation msg = "TypeError not raised by fit" assert_raises_regex(AssertionError, msg, check_estimator, BaseBadClassifier) # check that predict does input validation (doesn't accept dicts in input) msg = "Estimator doesn't check for NaN and inf in predict" assert_raises_regex(AssertionError, msg, check_estimator, NoCheckinPredict) # check for sparse matrix input handling msg = "Estimator type doesn't seem to fail gracefully on sparse data" # the check for sparse input handling prints to the stdout, # instead of raising an error, so as not to remove the original traceback. # that means we need to jump through some hoops to catch it. old_stdout = sys.stdout string_buffer = StringIO() sys.stdout = string_buffer try: check_estimator(NoSparseClassifier) except: pass finally: sys.stdout = old_stdout assert_true(msg in string_buffer.getvalue()) # doesn't error on actual estimator check_estimator(LogisticRegression)
bsd-3-clause
google/madi
src/madi/datasets/gaussian_mixture_dataset.py
1
5652
# Lint as: python3 # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://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. """Dataset generator for multimodal, multidimensional Gaussian data.""" from typing import Optional, Iterable from madi.datasets.base_dataset import BaseDataset import numpy as np import pandas as pd class GaussianMixtureDataset(BaseDataset): """Generates a multimodal, multidimensional dataset for Anomaly Detection.""" def __init__(self, n_dim: int, n_modes: int, n_pts_pos: int, sample_ratio: float, upper_bound: float, lower_bound: float): self._n_dim = n_dim self._n_modes = n_modes self._n_pts_pos = n_pts_pos self._sample_ratio = sample_ratio self._upper_bound = upper_bound self._lower_bound = lower_bound self._sample = self._get_mdim_gaussian_sample( n_pts_pos=self._n_pts_pos, n_dim=self._n_dim, sample_ratio=self._sample_ratio, noise_dim=None, n_modes=self._n_modes, neg_min=self._lower_bound, neg_max=self._upper_bound) @property def sample(self) -> pd.DataFrame: return self._sample @property def name(self) -> str: return "gaussian" @property def description(self) -> str: return ("{dim}-Dimensional, {modes}-Modal Gaussian distribution with sample" " ratio = {sample_ratio} and {n_points} sample points.").format( dim=self._n_dim, modes=self._modes, sample_ratio=self._sample_ratio, n_points=len(self._sample)) def _get_mdim_gaussian_sample(self, n_pts_pos: int, n_dim: int, sample_ratio: float, noise_dim: Optional[Iterable[int]] = None, n_modes: int = 1, neg_min: float = -3, neg_max: float = 3) -> pd.DataFrame: """Generates a multidimensional Gaussian synthetic test set. Args: n_pts_pos: number of positive sample points. n_dim: number of dimensions sample_ratio: proportion of negative sample size to positive noise_dim: array of dimensions to add uniform noise n_modes: number of modes distributed along x001 = x002 = ... = x[d] neg_min: minimum mode postion neg_max: maximum mode position Returns: a shuffled sample dataframe, of d-dim, n-points and class label. """ n_pts_neg = int(n_pts_pos * sample_ratio) def _get_pos_sample_synthetic(mean: float, cov: float, n_points: int) -> pd.DataFrame: """Generates a positive sample from a Gaussian distribution with n_points. Args: mean: d-dimensional vector of mean values. cov: dxd dimensional covariance matrix. n_points: Number of points to return. Returns: DataFrame with cols x001...x[d] and n_points rows drawn from Guassian with mean and cov. """ pos_mat = np.random.multivariate_normal(mean, cov, n_points).T df_pos = pd.DataFrame({"class_label": [1 for _ in range(n_points)]}) for i in range(pos_mat.shape[0]): df_pos["x%03d" % (i + 1)] = pos_mat[i] return df_pos def get_multidim_gaussian(n_points, n_dim, meanv=0, varv=1): cov = np.identity(n_dim) * varv mean_vec = np.ones(n_dim) * meanv return _get_pos_sample_synthetic(mean_vec, cov, n_points) def get_uniform_sample(n_points, n_dim, min_val, max_val): s = np.random.uniform(min_val, max_val, n_points * n_dim).reshape( (n_points, n_dim)) cols = ["x%03d" % (1 + i) for i in range(n_dim)] neg = pd.DataFrame(s, columns=cols) neg["class_label"] = 0 return neg varv = 1 / float(n_modes) # With one mode, just place the gaussian at the origin. if n_modes == 1: sample = get_multidim_gaussian( n_points=n_pts_pos, n_dim=n_dim, meanv=0, varv=varv) else: sample = pd.DataFrame() n_pts_msample = int(n_pts_pos / n_modes) # Want to resize the modes so that they (a) separate reasonably and (b) # yield sufficiently sparse regions between modes while still being # contained (mostly) within the neg_min and neg_max. The factor 0.8 wa # chosen because it enables 2 - 8 modes to meert those criteria. h = 0.8 * (neg_max - neg_min) / (n_modes - 1) for m in range(n_modes): meanv = neg_min * 0.8 + float(m) * h msample = get_multidim_gaussian( n_points=n_pts_msample, n_dim=n_dim, meanv=meanv, varv=varv) sample = pd.concat([sample, msample]) if noise_dim: for index_id in noise_dim: sample[index_id] = np.random.uniform(neg_min, neg_max, len(sample)) if n_pts_neg > 0: neg_sample = get_uniform_sample( n_pts_neg, n_dim=n_dim, min_val=neg_min * 2, max_val=neg_max * 2) sample = pd.concat([sample, neg_sample], ignore_index=True, sort=True) return sample.reindex(np.random.permutation(sample.index))
apache-2.0
dongsenfo/pymatgen
pymatgen/io/abinit/tasks.py
2
180142
# coding: utf-8 # Copyright (c) Pymatgen Development Team. # Distributed under the terms of the MIT License. """This module provides functions and classes related to Task objects.""" import os import time import datetime import shutil import collections import abc import copy import ruamel.yaml as yaml from io import StringIO import numpy as np from pprint import pprint from itertools import product from monty.string import is_string, list_strings from monty.termcolor import colored, cprint from monty.collections import AttrDict from monty.functools import lazy_property, return_none_if_raise from monty.json import MSONable from monty.fnmatch import WildCard from pymatgen.core.units import Memory from pymatgen.util.serialization import json_pretty_dump, pmg_serialize from .utils import File, Directory, irdvars_for_ext, abi_splitext, FilepathFixer, Condition, SparseHistogram from .qadapters import make_qadapter, QueueAdapter, QueueAdapterError from . import qutils as qu from .db import DBConnector from .nodes import Status, Node, NodeError, NodeResults, NodeCorrections, FileNode, check_spectator from . import abiinspect from . import events __author__ = "Matteo Giantomassi" __copyright__ = "Copyright 2013, The Materials Project" __version__ = "0.1" __maintainer__ = "Matteo Giantomassi" __all__ = [ "TaskManager", "AbinitBuild", "ParalHintsParser", "ParalHints", "AbinitTask", "ScfTask", "NscfTask", "RelaxTask", "DdkTask", "PhononTask", "ElasticTask", "SigmaTask", "EphTask", "OpticTask", "AnaddbTask", "set_user_config_taskmanager", ] import logging logger = logging.getLogger(__name__) # Tools and helper functions. def straceback(): """Returns a string with the traceback.""" import traceback return traceback.format_exc() def lennone(PropperOrNone): if PropperOrNone is None: return 0 else: return len(PropperOrNone) def nmltostring(nml): """Convert a dictionary representing a Fortran namelist into a string.""" if not isinstance(nml,dict): raise ValueError("nml should be a dict !") curstr = "" for key,group in nml.items(): namelist = ["&" + key] for k, v in group.items(): if isinstance(v, list) or isinstance(v, tuple): namelist.append(k + " = " + ",".join(map(str, v)) + ",") elif is_string(v): namelist.append(k + " = '" + str(v) + "',") else: namelist.append(k + " = " + str(v) + ",") namelist.append("/") curstr = curstr + "\n".join(namelist) + "\n" return curstr class TaskResults(NodeResults): JSON_SCHEMA = NodeResults.JSON_SCHEMA.copy() JSON_SCHEMA["properties"] = { "executable": {"type": "string", "required": True}, } @classmethod def from_node(cls, task): """Initialize an instance from an :class:`AbinitTask` instance.""" new = super().from_node(task) new.update( executable=task.executable, #executable_version: #task_events= pseudos=[p.as_dict() for p in task.input.pseudos], #input=task.input ) new.register_gridfs_files( run_abi=(task.input_file.path, "t"), run_abo=(task.output_file.path, "t"), ) return new class ParalConf(AttrDict): """ This object store the parameters associated to one of the possible parallel configurations reported by ABINIT. Essentially it is a dictionary whose values can also be accessed as attributes. It also provides default values for selected keys that might not be present in the ABINIT dictionary. Example: --- !Autoparal info: version: 1 autoparal: 1 max_ncpus: 108 configurations: - tot_ncpus: 2 # Total number of CPUs mpi_ncpus: 2 # Number of MPI processes. omp_ncpus: 1 # Number of OMP threads (1 if not present) mem_per_cpu: 10 # Estimated memory requirement per MPI processor in Megabytes. efficiency: 0.4 # 1.0 corresponds to an "expected" optimal efficiency (strong scaling). vars: { # Dictionary with the variables that should be added to the input. varname1: varvalue1 varname2: varvalue2 } - ... For paral_kgb we have: nproc npkpt npspinor npband npfft bandpp weight 108 1 1 12 9 2 0.25 108 1 1 108 1 2 27.00 96 1 1 24 4 1 1.50 84 1 1 12 7 2 0.25 """ _DEFAULTS = { "omp_ncpus": 1, "mem_per_cpu": 0.0, "vars": {} } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Add default values if not already in self. for k, v in self._DEFAULTS.items(): if k not in self: self[k] = v def __str__(self): stream = StringIO() pprint(self, stream=stream) return stream.getvalue() @property def num_cores(self): return self.mpi_procs * self.omp_threads @property def mem_per_proc(self): return self.mem_per_cpu @property def mpi_procs(self): return self.mpi_ncpus @property def omp_threads(self): return self.omp_ncpus @property def speedup(self): """Estimated speedup reported by ABINIT.""" return self.efficiency * self.num_cores @property def tot_mem(self): """Estimated total memory in Mbs (computed from mem_per_proc)""" return self.mem_per_proc * self.mpi_procs class ParalHintsError(Exception): """Base error class for `ParalHints`.""" class ParalHintsParser: Error = ParalHintsError def __init__(self): # Used to push error strings. self._errors = collections.deque(maxlen=100) def add_error(self, errmsg): self._errors.append(errmsg) def parse(self, filename): """ Read the `AutoParal` section (YAML format) from filename. Assumes the file contains only one section. """ with abiinspect.YamlTokenizer(filename) as r: doc = r.next_doc_with_tag("!Autoparal") try: d = yaml.safe_load(doc.text_notag) return ParalHints(info=d["info"], confs=d["configurations"]) except: import traceback sexc = traceback.format_exc() err_msg = "Wrong YAML doc:\n%s\n\nException:\n%s" % (doc.text, sexc) self.add_error(err_msg) logger.critical(err_msg) raise self.Error(err_msg) class ParalHints(collections.abc.Iterable): """ Iterable with the hints for the parallel execution reported by ABINIT. """ Error = ParalHintsError def __init__(self, info, confs): self.info = info self._confs = [ParalConf(**d) for d in confs] @classmethod def from_mpi_omp_lists(cls, mpi_procs, omp_threads): """ Build a list of Parallel configurations from two lists containing the number of MPI processes and the number of OpenMP threads i.e. product(mpi_procs, omp_threads). The configuration have parallel efficiency set to 1.0 and no input variables. Mainly used for preparing benchmarks. """ info = {} confs = [ParalConf(mpi_ncpus=p, omp_ncpus=p, efficiency=1.0) for p, t in product(mpi_procs, omp_threads)] return cls(info, confs) def __getitem__(self, key): return self._confs[key] def __iter__(self): return self._confs.__iter__() def __len__(self): return self._confs.__len__() def __repr__(self): return "\n".join(str(conf) for conf in self) def __str__(self): return repr(self) @lazy_property def max_cores(self): """Maximum number of cores.""" return max(c.mpi_procs * c.omp_threads for c in self) @lazy_property def max_mem_per_proc(self): """Maximum memory per MPI process.""" return max(c.mem_per_proc for c in self) @lazy_property def max_speedup(self): """Maximum speedup.""" return max(c.speedup for c in self) @lazy_property def max_efficiency(self): """Maximum parallel efficiency.""" return max(c.efficiency for c in self) @pmg_serialize def as_dict(self, **kwargs): return {"info": self.info, "confs": self._confs} @classmethod def from_dict(cls, d): return cls(info=d["info"], confs=d["confs"]) def copy(self): """Shallow copy of self.""" return copy.copy(self) def select_with_condition(self, condition, key=None): """ Remove all the configurations that do not satisfy the given condition. Args: condition: dict or :class:`Condition` object with operators expressed with a Mongodb-like syntax key: Selects the sub-dictionary on which condition is applied, e.g. key="vars" if we have to filter the configurations depending on the values in vars """ condition = Condition.as_condition(condition) new_confs = [] for conf in self: # Select the object on which condition is applied obj = conf if key is None else AttrDict(conf[key]) add_it = condition(obj=obj) #if key is "vars": print("conf", conf, "added:", add_it) if add_it: new_confs.append(conf) self._confs = new_confs def sort_by_efficiency(self, reverse=True): """Sort the configurations in place. items with highest efficiency come first""" self._confs.sort(key=lambda c: c.efficiency, reverse=reverse) return self def sort_by_speedup(self, reverse=True): """Sort the configurations in place. items with highest speedup come first""" self._confs.sort(key=lambda c: c.speedup, reverse=reverse) return self def sort_by_mem_per_proc(self, reverse=False): """Sort the configurations in place. items with lowest memory per proc come first.""" # Avoid sorting if mem_per_cpu is not available. if any(c.mem_per_proc > 0.0 for c in self): self._confs.sort(key=lambda c: c.mem_per_proc, reverse=reverse) return self def multidimensional_optimization(self, priorities=("speedup", "efficiency")): # Mapping property --> options passed to sparse_histogram opts = dict(speedup=dict(step=1.0), efficiency=dict(step=0.1), mem_per_proc=dict(memory=1024)) #opts = dict(zip(priorities, bin_widths)) opt_confs = self._confs for priority in priorities: histogram = SparseHistogram(opt_confs, key=lambda c: getattr(c, priority), **opts[priority]) pos = 0 if priority == "mem_per_proc" else -1 opt_confs = histogram.values[pos] #histogram.plot(show=True, savefig="hello.pdf") return self.__class__(info=self.info, confs=opt_confs) #def histogram_efficiency(self, step=0.1): # """Returns a :class:`SparseHistogram` with configuration grouped by parallel efficiency.""" # return SparseHistogram(self._confs, key=lambda c: c.efficiency, step=step) #def histogram_speedup(self, step=1.0): # """Returns a :class:`SparseHistogram` with configuration grouped by parallel speedup.""" # return SparseHistogram(self._confs, key=lambda c: c.speedup, step=step) #def histogram_memory(self, step=1024): # """Returns a :class:`SparseHistogram` with configuration grouped by memory.""" # return SparseHistogram(self._confs, key=lambda c: c.speedup, step=step) #def filter(self, qadapter): # """Return a new list of configurations that can be executed on the `QueueAdapter` qadapter.""" # new_confs = [pconf for pconf in self if qadapter.can_run_pconf(pconf)] # return self.__class__(info=self.info, confs=new_confs) def get_ordered_with_policy(self, policy, max_ncpus): """ Sort and return a new list of configurations ordered according to the :class:`TaskPolicy` policy. """ # Build new list since we are gonna change the object in place. hints = self.__class__(self.info, confs=[c for c in self if c.num_cores <= max_ncpus]) # First select the configurations satisfying the condition specified by the user (if any) bkp_hints = hints.copy() if policy.condition: logger.info("Applying condition %s" % str(policy.condition)) hints.select_with_condition(policy.condition) # Undo change if no configuration fullfills the requirements. if not hints: hints = bkp_hints logger.warning("Empty list of configurations after policy.condition") # Now filter the configurations depending on the values in vars bkp_hints = hints.copy() if policy.vars_condition: logger.info("Applying vars_condition %s" % str(policy.vars_condition)) hints.select_with_condition(policy.vars_condition, key="vars") # Undo change if no configuration fullfills the requirements. if not hints: hints = bkp_hints logger.warning("Empty list of configurations after policy.vars_condition") if len(policy.autoparal_priorities) == 1: # Example: hints.sort_by_speedup() if policy.autoparal_priorities[0] in ['efficiency', 'speedup', 'mem_per_proc']: getattr(hints, "sort_by_" + policy.autoparal_priorities[0])() elif isinstance(policy.autoparal_priorities[0], collections.Mapping): if policy.autoparal_priorities[0]['meta_priority'] == 'highest_speedup_minimum_efficiency_cutoff': min_efficiency = policy.autoparal_priorities[0].get('minimum_efficiency', 1.0) hints.select_with_condition({'efficiency': {'$gte': min_efficiency}}) hints.sort_by_speedup() else: hints = hints.multidimensional_optimization(priorities=policy.autoparal_priorities) if len(hints) == 0: raise ValueError("len(hints) == 0") #TODO: make sure that num_cores == 1 is never selected when we have more than one configuration #if len(hints) > 1: # hints.select_with_condition(dict(num_cores={"$eq": 1))) # Return final (orderded ) list of configurations (best first). return hints class TaskPolicy: """ This object stores the parameters used by the :class:`TaskManager` to create the submission script and/or to modify the ABINIT variables governing the parallel execution. A `TaskPolicy` object contains a set of variables that specify the launcher, as well as the options and the conditions used to select the optimal configuration for the parallel run """ @classmethod def as_policy(cls, obj): """ Converts an object obj into a `:class:`TaskPolicy. Accepts: * None * TaskPolicy * dict-like object """ if obj is None: # Use default policy. return TaskPolicy() else: if isinstance(obj, cls): return obj elif isinstance(obj, collections.abc.Mapping): return cls(**obj) else: raise TypeError("Don't know how to convert type %s to %s" % (type(obj), cls)) @classmethod def autodoc(cls): return """ autoparal: # (integer). 0 to disable the autoparal feature (DEFAULT: 1 i.e. autoparal is on) condition: # condition used to filter the autoparal configurations (Mongodb-like syntax). # DEFAULT: empty i.e. ignored. vars_condition: # Condition used to filter the list of ABINIT variables reported by autoparal # (Mongodb-like syntax). DEFAULT: empty i.e. ignored. frozen_timeout: # A job is considered frozen and its status is set to ERROR if no change to # the output file has been done for `frozen_timeout` seconds. Accepts int with seconds or # string in slurm form i.e. days-hours:minutes:seconds. DEFAULT: 1 hour. precedence: # Under development. autoparal_priorities: # Under development. """ def __init__(self, **kwargs): """ See autodoc """ self.autoparal = kwargs.pop("autoparal", 1) self.condition = Condition(kwargs.pop("condition", {})) self.vars_condition = Condition(kwargs.pop("vars_condition", {})) self.precedence = kwargs.pop("precedence", "autoparal_conf") self.autoparal_priorities = kwargs.pop("autoparal_priorities", ["speedup"]) #self.autoparal_priorities = kwargs.pop("autoparal_priorities", ["speedup", "efficiecy", "memory"] # TODO frozen_timeout could be computed as a fraction of the timelimit of the qadapter! self.frozen_timeout = qu.slurm_parse_timestr(kwargs.pop("frozen_timeout", "0-1:00:00")) if kwargs: raise ValueError("Found invalid keywords in policy section:\n %s" % str(kwargs.keys())) # Consistency check. if self.precedence not in ("qadapter", "autoparal_conf"): raise ValueError("Wrong value for policy.precedence, should be qadapter or autoparal_conf") def __str__(self): lines = [] app = lines.append for k, v in self.__dict__.items(): if k.startswith("_"): continue app("%s: %s" % (k, v)) return "\n".join(lines) class ManagerIncreaseError(Exception): """ Exception raised by the manager if the increase request failed """ class FixQueueCriticalError(Exception): """ error raised when an error could not be fixed at the task level """ # Global variable used to store the task manager returned by `from_user_config`. _USER_CONFIG_TASKMANAGER = None def set_user_config_taskmanager(manager): """Change the default manager returned by TaskManager.from_user_config.""" global _USER_CONFIG_TASKMANAGER _USER_CONFIG_TASKMANAGER = manager class TaskManager(MSONable): """ A `TaskManager` is responsible for the generation of the job script and the submission of the task, as well as for the specification of the parameters passed to the resource manager (e.g. Slurm, PBS ...) and/or the run-time specification of the ABINIT variables governing the parallel execution. A `TaskManager` delegates the generation of the submission script and the submission of the task to the :class:`QueueAdapter`. A `TaskManager` has a :class:`TaskPolicy` that governs the specification of the parameters for the parallel executions. Ideally, the TaskManager should be the **main entry point** used by the task to deal with job submission/optimization """ YAML_FILE = "manager.yml" USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".abinit", "abipy") ENTRIES = {"policy", "qadapters", "db_connector", "batch_adapter"} @classmethod def autodoc(cls): from .db import DBConnector s = """ # TaskManager configuration file (YAML Format) policy: # Dictionary with options used to control the execution of the tasks. qadapters: # List of qadapters objects (mandatory) - # qadapter_1 - # qadapter_2 db_connector: # Connection to MongoDB database (optional) batch_adapter: # Adapter used to submit flows with batch script. (optional) ########################################## # Individual entries are documented below: ########################################## """ s += "policy: " + TaskPolicy.autodoc() + "\n" s += "qadapter: " + QueueAdapter.autodoc() + "\n" #s += "db_connector: " + DBConnector.autodoc() return s @classmethod def from_user_config(cls): """ Initialize the :class:`TaskManager` from the YAML file 'manager.yaml'. Search first in the working directory and then in the AbiPy configuration directory. Raises: RuntimeError if file is not found. """ global _USER_CONFIG_TASKMANAGER if _USER_CONFIG_TASKMANAGER is not None: return _USER_CONFIG_TASKMANAGER # Try in the current directory then in user configuration directory. path = os.path.join(os.getcwd(), cls.YAML_FILE) if not os.path.exists(path): path = os.path.join(cls.USER_CONFIG_DIR, cls.YAML_FILE) if not os.path.exists(path): raise RuntimeError(colored( "\nCannot locate %s neither in current directory nor in %s\n" "!!! PLEASE READ THIS: !!!\n" "To use AbiPy to run jobs this file must be present\n" "It provides a description of the cluster/computer you are running on\n" "Examples are provided in abipy/data/managers." % (cls.YAML_FILE, path), color="red")) _USER_CONFIG_TASKMANAGER = cls.from_file(path) return _USER_CONFIG_TASKMANAGER @classmethod def from_file(cls, filename): """Read the configuration parameters from the Yaml file filename.""" try: with open(filename, "r") as fh: return cls.from_dict(yaml.safe_load(fh)) except Exception as exc: print("Error while reading TaskManager parameters from %s\n" % filename) raise @classmethod def from_string(cls, s): """Create an instance from string s containing a YAML dictionary.""" return cls.from_dict(yaml.safe_load(s)) @classmethod def as_manager(cls, obj): """ Convert obj into TaskManager instance. Accepts string, filepath, dictionary, `TaskManager` object. If obj is None, the manager is initialized from the user config file. """ if isinstance(obj, cls): return obj if obj is None: return cls.from_user_config() if is_string(obj): if os.path.exists(obj): return cls.from_file(obj) else: return cls.from_string(obj) elif isinstance(obj, collections.abc.Mapping): return cls.from_dict(obj) else: raise TypeError("Don't know how to convert type %s to TaskManager" % type(obj)) @classmethod def from_dict(cls, d): """Create an instance from a dictionary.""" return cls(**{k: v for k, v in d.items() if k in cls.ENTRIES}) @pmg_serialize def as_dict(self): return copy.deepcopy(self._kwargs) def __init__(self, **kwargs): """ Args: policy:None qadapters:List of qadapters in YAML format db_connector:Dictionary with data used to connect to the database (optional) """ # Keep a copy of kwargs self._kwargs = copy.deepcopy(kwargs) self.policy = TaskPolicy.as_policy(kwargs.pop("policy", None)) # Initialize database connector (if specified) self.db_connector = DBConnector(**kwargs.pop("db_connector", {})) # Build list of QAdapters. Neglect entry if priority == 0 or `enabled: no" qads = [] for d in kwargs.pop("qadapters"): if d.get("enabled", False): continue qad = make_qadapter(**d) if qad.priority > 0: qads.append(qad) elif qad.priority < 0: raise ValueError("qadapter cannot have negative priority:\n %s" % qad) if not qads: raise ValueError("Received emtpy list of qadapters") #if len(qads) != 1: # raise NotImplementedError("For the time being multiple qadapters are not supported! Please use one adapter") # Order qdapters according to priority. qads = sorted(qads, key=lambda q: q.priority) priorities = [q.priority for q in qads] if len(priorities) != len(set(priorities)): raise ValueError("Two or more qadapters have same priority. This is not allowed. Check taskmanager.yml") self._qads, self._qadpos = tuple(qads), 0 # Initialize the qadapter for batch script submission. d = kwargs.pop("batch_adapter", None) self.batch_adapter = None if d: self.batch_adapter = make_qadapter(**d) #print("batch_adapter", self.batch_adapter) if kwargs: raise ValueError("Found invalid keywords in the taskmanager file:\n %s" % str(list(kwargs.keys()))) @lazy_property def abinit_build(self): """:class:`AbinitBuild` object with Abinit version and options used to build the code""" return AbinitBuild(manager=self) def to_shell_manager(self, mpi_procs=1): """ Returns a new `TaskManager` with the same parameters as self but replace the :class:`QueueAdapter` with a :class:`ShellAdapter` with mpi_procs so that we can submit the job without passing through the queue. """ my_kwargs = copy.deepcopy(self._kwargs) my_kwargs["policy"] = TaskPolicy(autoparal=0) # On BlueGene we need at least two qadapters. # One for running jobs on the computing nodes and another one # for running small jobs on the fronted. These two qadapters # will have different enviroments and different executables. # If None of the q-adapters has qtype==shell, we change qtype to shell # and we return a new Manager for sequential jobs with the same parameters as self. # If the list contains a qadapter with qtype == shell, we ignore the remaining qadapters # when we build the new Manager. has_shell_qad = False for d in my_kwargs["qadapters"]: if d["queue"]["qtype"] == "shell": has_shell_qad = True if has_shell_qad: my_kwargs["qadapters"] = [d for d in my_kwargs["qadapters"] if d["queue"]["qtype"] == "shell"] for d in my_kwargs["qadapters"]: d["queue"]["qtype"] = "shell" d["limits"]["min_cores"] = mpi_procs d["limits"]["max_cores"] = mpi_procs # If shell_runner is specified, replace mpi_runner with shell_runner # in the script used to run jobs on the frontend. # On same machines based on Slurm, indeed, mpirun/mpiexec is not available # and jobs should be executed with `srun -n4 exec` when running on the computing nodes # or with `exec` when running in sequential on the frontend. if "job" in d and "shell_runner" in d["job"]: shell_runner = d["job"]["shell_runner"] #print("shell_runner:", shell_runner, type(shell_runner)) if not shell_runner or shell_runner == "None": shell_runner = "" d["job"]["mpi_runner"] = shell_runner #print("shell_runner:", shell_runner) #print(my_kwargs) new = self.__class__(**my_kwargs) new.set_mpi_procs(mpi_procs) return new def new_with_fixed_mpi_omp(self, mpi_procs, omp_threads): """ Return a new `TaskManager` in which autoparal has been disabled. The jobs will be executed with `mpi_procs` MPI processes and `omp_threads` OpenMP threads. Useful for generating input files for benchmarks. """ new = self.deepcopy() new.policy.autoparal = 0 new.set_mpi_procs(mpi_procs) new.set_omp_threads(omp_threads) return new @property def has_queue(self): """True if we are submitting jobs via a queue manager.""" return self.qadapter.QTYPE.lower() != "shell" @property def qads(self): """List of :class:`QueueAdapter` objects sorted according to priorities (highest comes first)""" return self._qads @property def qadapter(self): """The qadapter used to submit jobs.""" return self._qads[self._qadpos] def select_qadapter(self, pconfs): """ Given a list of parallel configurations, pconfs, this method select an `optimal` configuration according to some criterion as well as the :class:`QueueAdapter` to use. Args: pconfs: :class:`ParalHints` object with the list of parallel configurations Returns: :class:`ParallelConf` object with the `optimal` configuration. """ # Order the list of configurations according to policy. policy, max_ncpus = self.policy, self.max_cores pconfs = pconfs.get_ordered_with_policy(policy, max_ncpus) if policy.precedence == "qadapter": # Try to run on the qadapter with the highest priority. for qadpos, qad in enumerate(self.qads): possible_pconfs = [pc for pc in pconfs if qad.can_run_pconf(pc)] if qad.allocation == "nodes": #if qad.allocation in ["nodes", "force_nodes"]: # Select the configuration divisible by nodes if possible. for pconf in possible_pconfs: if pconf.num_cores % qad.hw.cores_per_node == 0: return self._use_qadpos_pconf(qadpos, pconf) # Here we select the first one. if possible_pconfs: return self._use_qadpos_pconf(qadpos, possible_pconfs[0]) elif policy.precedence == "autoparal_conf": # Try to run on the first pconf irrespectively of the priority of the qadapter. for pconf in pconfs: for qadpos, qad in enumerate(self.qads): if qad.allocation == "nodes" and not pconf.num_cores % qad.hw.cores_per_node == 0: continue # Ignore it. not very clean if qad.can_run_pconf(pconf): return self._use_qadpos_pconf(qadpos, pconf) else: raise ValueError("Wrong value of policy.precedence = %s" % policy.precedence) # No qadapter could be found raise RuntimeError("Cannot find qadapter for this run!") def _use_qadpos_pconf(self, qadpos, pconf): """ This function is called when we have accepted the :class:`ParalConf` pconf. Returns pconf """ self._qadpos = qadpos # Change the number of MPI/OMP cores. self.set_mpi_procs(pconf.mpi_procs) if self.has_omp: self.set_omp_threads(pconf.omp_threads) # Set memory per proc. #FIXME: Fixer may have changed the memory per proc and should not be resetted by ParalConf #self.set_mem_per_proc(pconf.mem_per_proc) return pconf def __str__(self): """String representation.""" lines = [] app = lines.append #app("[Task policy]\n%s" % str(self.policy)) for i, qad in enumerate(self.qads): app("[Qadapter %d]\n%s" % (i, str(qad))) app("Qadapter selected: %d" % self._qadpos) if self.has_db: app("[MongoDB database]:") app(str(self.db_connector)) return "\n".join(lines) @property def has_db(self): """True if we are using MongoDB database""" return bool(self.db_connector) @property def has_omp(self): """True if we are using OpenMP parallelization.""" return self.qadapter.has_omp @property def num_cores(self): """Total number of CPUs used to run the task.""" return self.qadapter.num_cores @property def mpi_procs(self): """Number of MPI processes.""" return self.qadapter.mpi_procs @property def mem_per_proc(self): """Memory per MPI process.""" return self.qadapter.mem_per_proc @property def omp_threads(self): """Number of OpenMP threads""" return self.qadapter.omp_threads def deepcopy(self): """Deep copy of self.""" return copy.deepcopy(self) def set_mpi_procs(self, mpi_procs): """Set the number of MPI processes to use.""" self.qadapter.set_mpi_procs(mpi_procs) def set_omp_threads(self, omp_threads): """Set the number of OpenMp threads to use.""" self.qadapter.set_omp_threads(omp_threads) def set_mem_per_proc(self, mem_mb): """Set the memory (in Megabytes) per CPU.""" self.qadapter.set_mem_per_proc(mem_mb) @property def max_cores(self): """ Maximum number of cores that can be used. This value is mainly used in the autoparal part to get the list of possible configurations. """ return max(q.hint_cores for q in self.qads) def get_njobs_in_queue(self, username=None): """ returns the number of jobs in the queue, returns None when the number of jobs cannot be determined. Args: username: (str) the username of the jobs to count (default is to autodetect) """ return self.qadapter.get_njobs_in_queue(username=username) def cancel(self, job_id): """Cancel the job. Returns exit status.""" return self.qadapter.cancel(job_id) def write_jobfile(self, task, **kwargs): """ Write the submission script. Return the path of the script ================ ============================================ kwargs Meaning ================ ============================================ exec_args List of arguments passed to task.executable. Default: no arguments. ================ ============================================ """ script = self.qadapter.get_script_str( job_name=task.name, launch_dir=task.workdir, executable=task.executable, qout_path=task.qout_file.path, qerr_path=task.qerr_file.path, stdin=task.files_file.path, stdout=task.log_file.path, stderr=task.stderr_file.path, exec_args=kwargs.pop("exec_args", []), ) # Write the script. with open(task.job_file.path, "w") as fh: fh.write(script) task.job_file.chmod(0o740) return task.job_file.path def launch(self, task, **kwargs): """ Build the input files and submit the task via the :class:`Qadapter` Args: task: :class:`TaskObject` Returns: Process object. """ if task.status == task.S_LOCKED: raise ValueError("You shall not submit a locked task!") # Build the task task.build() # Pass information on the time limit to Abinit (we always assume ndtset == 1) if isinstance(task, AbinitTask): args = kwargs.get("exec_args", []) if args is None: args = [] args = args[:] args.append("--timelimit %s" % qu.time2slurm(self.qadapter.timelimit)) kwargs["exec_args"] = args # Write the submission script script_file = self.write_jobfile(task, **kwargs) # Submit the task and save the queue id. try: qjob, process = self.qadapter.submit_to_queue(script_file) task.set_status(task.S_SUB, msg='Submitted to queue') task.set_qjob(qjob) return process except self.qadapter.MaxNumLaunchesError as exc: # TODO: Here we should try to switch to another qadapter # 1) Find a new parallel configuration in those stored in task.pconfs # 2) Change the input file. # 3) Regenerate the submission script # 4) Relaunch task.set_status(task.S_ERROR, msg="max_num_launches reached: %s" % str(exc)) raise def get_collection(self, **kwargs): """Return the MongoDB collection used to store the results.""" return self.db_connector.get_collection(**kwargs) def increase_mem(self): # OLD # with GW calculations in mind with GW mem = 10, # the response fuction is in memory and not distributed # we need to increase memory if jobs fail ... # return self.qadapter.more_mem_per_proc() try: self.qadapter.more_mem_per_proc() except QueueAdapterError: # here we should try to switch to an other qadapter raise ManagerIncreaseError('manager failed to increase mem') def increase_ncpus(self): """ increase the number of cpus, first ask the current qadapter, if that one raises a QadapterIncreaseError switch to the next qadapter. If all fail raise an ManagerIncreaseError """ try: self.qadapter.more_cores() except QueueAdapterError: # here we should try to switch to an other qadapter raise ManagerIncreaseError('manager failed to increase ncpu') def increase_resources(self): try: self.qadapter.more_cores() return except QueueAdapterError: pass try: self.qadapter.more_mem_per_proc() except QueueAdapterError: # here we should try to switch to an other qadapter raise ManagerIncreaseError('manager failed to increase resources') def exclude_nodes(self, nodes): try: self.qadapter.exclude_nodes(nodes=nodes) except QueueAdapterError: # here we should try to switch to an other qadapter raise ManagerIncreaseError('manager failed to exclude nodes') def increase_time(self): try: self.qadapter.more_time() except QueueAdapterError: # here we should try to switch to an other qadapter raise ManagerIncreaseError('manager failed to increase time') class AbinitBuild: """ This object stores information on the options used to build Abinit .. attribute:: info String with build information as produced by `abinit -b` .. attribute:: version Abinit version number e.g 8.0.1 (string) .. attribute:: has_netcdf True if netcdf is enabled. .. attribute:: has_omp True if OpenMP is enabled. .. attribute:: has_mpi True if MPI is enabled. .. attribute:: has_mpiio True if MPI-IO is supported. """ def __init__(self, workdir=None, manager=None): manager = TaskManager.as_manager(manager).to_shell_manager(mpi_procs=1) # Build a simple manager to run the job in a shell subprocess import tempfile workdir = tempfile.mkdtemp() if workdir is None else workdir # Generate a shell script to execute `abinit -b` stdout = os.path.join(workdir, "run.abo") script = manager.qadapter.get_script_str( job_name="abinit_b", launch_dir=workdir, executable="abinit", qout_path=os.path.join(workdir, "queue.qout"), qerr_path=os.path.join(workdir, "queue.qerr"), #stdin=os.path.join(workdir, "run.files"), stdout=stdout, stderr=os.path.join(workdir, "run.err"), exec_args=["-b"], ) # Execute the script. script_file = os.path.join(workdir, "job.sh") with open(script_file, "wt") as fh: fh.write(script) qjob, process = manager.qadapter.submit_to_queue(script_file) process.wait() if process.returncode != 0: logger.critical("Error while executing %s" % script_file) print("stderr:\n", process.stderr.read()) #print("stdout:", process.stdout.read()) # To avoid: ResourceWarning: unclosed file <_io.BufferedReader name=87> in py3k process.stderr.close() with open(stdout, "rt") as fh: self.info = fh.read() # info string has the following format. """ === Build Information === Version : 8.0.1 Build target : x86_64_darwin15.0.0_gnu5.3 Build date : 20160122 === Compiler Suite === C compiler : gnu C++ compiler : gnuApple Fortran compiler : gnu5.3 CFLAGS : -g -O2 -mtune=native -march=native CXXFLAGS : -g -O2 -mtune=native -march=native FCFLAGS : -g -ffree-line-length-none FC_LDFLAGS : === Optimizations === Debug level : basic Optimization level : standard Architecture : unknown_unknown === Multicore === Parallel build : yes Parallel I/O : yes openMP support : no GPU support : no === Connectors / Fallbacks === Connectors on : yes Fallbacks on : yes DFT flavor : libxc-fallback+atompaw-fallback+wannier90-fallback FFT flavor : none LINALG flavor : netlib MATH flavor : none TIMER flavor : abinit TRIO flavor : netcdf+etsf_io-fallback === Experimental features === Bindings : @enable_bindings@ Exports : no GW double-precision : yes === Bazaar branch information === Branch ID : gmatteo@gmac-20160112110440-lf6exhneqim9082h Revision : 1226 Committed : 0 """ self.version = "0.0.0" self.has_netcdf = False self.has_omp = False self.has_mpi, self.has_mpiio = False, False def yesno2bool(line): ans = line.split()[-1].lower() return dict(yes=True, no=False, auto=True)[ans] # Parse info. for line in self.info.splitlines(): if "Version" in line: self.version = line.split()[-1] if "TRIO flavor" in line: self.has_netcdf = "netcdf" in line if "openMP support" in line: self.has_omp = yesno2bool(line) if "Parallel build" in line: self.has_mpi = yesno2bool(line) if "Parallel I/O" in line: self.has_mpiio = yesno2bool(line) def __str__(self): lines = [] app = lines.append app("Abinit Build Information:") app(" Abinit version: %s" % self.version) app(" MPI: %s, MPI-IO: %s, OpenMP: %s" % (self.has_mpi, self.has_mpiio, self.has_omp)) app(" Netcdf: %s" % self.has_netcdf) return "\n".join(lines) def version_ge(self, version_string): """True is Abinit version is >= version_string""" return self.compare_version(version_string, ">=") def compare_version(self, version_string, op): """Compare Abinit version to `version_string` with operator `op`""" from pkg_resources import parse_version from monty.operator import operator_from_str op = operator_from_str(op) return op(parse_version(self.version), parse_version(version_string)) class FakeProcess: """ This object is attached to a :class:`Task` instance if the task has not been submitted This trick allows us to simulate a process that is still running so that we can safely poll task.process. """ def poll(self): return None def wait(self): raise RuntimeError("Cannot wait a FakeProcess") def communicate(self, input=None): raise RuntimeError("Cannot communicate with a FakeProcess") def kill(self): raise RuntimeError("Cannot kill a FakeProcess") @property def returncode(self): return None class MyTimedelta(datetime.timedelta): """A customized version of timedelta whose __str__ method doesn't print microseconds.""" def __new__(cls, days, seconds, microseconds): return datetime.timedelta.__new__(cls, days, seconds, microseconds) def __str__(self): """Remove microseconds from timedelta default __str__""" s = super().__str__() microsec = s.find(".") if microsec != -1: s = s[:microsec] return s @classmethod def as_timedelta(cls, delta): """Convert delta into a MyTimedelta object.""" # Cannot monkey patch the __class__ and must pass through __new__ as the object is immutable. if isinstance(delta, cls): return delta return cls(delta.days, delta.seconds, delta.microseconds) class TaskDateTimes: """ Small object containing useful :class:`datetime.datatime` objects associated to important events. .. attributes: init: initialization datetime submission: submission datetime start: Begin of execution. end: End of execution. """ def __init__(self): self.init = datetime.datetime.now() self.submission, self.start, self.end = None, None, None def __str__(self): lines = [] app = lines.append app("Initialization done on: %s" % self.init) if self.submission is not None: app("Submitted on: %s" % self.submission) if self.start is not None: app("Started on: %s" % self.start) if self.end is not None: app("Completed on: %s" % self.end) return "\n".join(lines) def reset(self): """Reinitialize the counters.""" self = self.__class__() def get_runtime(self): """:class:`timedelta` with the run-time, None if the Task is not running""" if self.start is None: return None if self.end is None: delta = datetime.datetime.now() - self.start else: delta = self.end - self.start return MyTimedelta.as_timedelta(delta) def get_time_inqueue(self): """ :class:`timedelta` with the time spent in the Queue, None if the Task is not running .. note: This value is always greater than the real value computed by the resource manager as we start to count only when check_status sets the `Task` status to S_RUN. """ if self.submission is None: return None if self.start is None: delta = datetime.datetime.now() - self.submission else: delta = self.start - self.submission # This happens when we read the exact start datetime from the ABINIT log file. if delta.total_seconds() < 0: delta = datetime.timedelta(seconds=0) return MyTimedelta.as_timedelta(delta) class TaskError(NodeError): """Base Exception for :class:`Task` methods""" class TaskRestartError(TaskError): """Exception raised while trying to restart the :class:`Task`.""" class Task(Node, metaclass=abc.ABCMeta): """ A Task is a node that performs some kind of calculation. This is base class providing low-level methods. """ # Use class attributes for TaskErrors so that we don't have to import them. Error = TaskError RestartError = TaskRestartError # List of `AbinitEvent` subclasses that are tested in the check_status method. # Subclasses should provide their own list if they need to check the converge status. CRITICAL_EVENTS = [] # Prefixes for Abinit (input, output, temporary) files. Prefix = collections.namedtuple("Prefix", "idata odata tdata") pj = os.path.join prefix = Prefix(pj("indata", "in"), pj("outdata", "out"), pj("tmpdata", "tmp")) del Prefix, pj def __init__(self, input, workdir=None, manager=None, deps=None): """ Args: input: :class:`AbinitInput` object. workdir: Path to the working directory. manager: :class:`TaskManager` object. deps: Dictionary specifying the dependency of this node. None means that this Task has no dependency. """ # Init the node super().__init__() self._input = input if workdir is not None: self.set_workdir(workdir) if manager is not None: self.set_manager(manager) # Handle possible dependencies. if deps: self.add_deps(deps) # Date-time associated to submission, start and end. self.datetimes = TaskDateTimes() # Count the number of restarts. self.num_restarts = 0 self._qjob = None self.queue_errors = [] self.abi_errors = [] # two flags that provide, dynamically, information on the scaling behavious of a task. If any process of fixing # finds none scaling behaviour, they should be switched. If a task type is clearly not scaling they should be # swiched. self.mem_scales = True self.load_scales = True def __getstate__(self): """ Return state is pickled as the contents for the instance. In this case we just remove the process since Subprocess objects cannot be pickled. This is the reason why we have to store the returncode in self._returncode instead of using self.process.returncode. """ return {k: v for k, v in self.__dict__.items() if k not in ["_process"]} #@check_spectator def set_workdir(self, workdir, chroot=False): """Set the working directory. Cannot be set more than once unless chroot is True""" if not chroot and hasattr(self, "workdir") and self.workdir != workdir: raise ValueError("self.workdir != workdir: %s, %s" % (self.workdir, workdir)) self.workdir = os.path.abspath(workdir) # Files required for the execution. self.input_file = File(os.path.join(self.workdir, "run.abi")) self.output_file = File(os.path.join(self.workdir, "run.abo")) self.files_file = File(os.path.join(self.workdir, "run.files")) self.job_file = File(os.path.join(self.workdir, "job.sh")) self.log_file = File(os.path.join(self.workdir, "run.log")) self.stderr_file = File(os.path.join(self.workdir, "run.err")) self.start_lockfile = File(os.path.join(self.workdir, "__startlock__")) # This file is produced by Abinit if nprocs > 1 and MPI_ABORT. self.mpiabort_file = File(os.path.join(self.workdir, "__ABI_MPIABORTFILE__")) # Directories with input|output|temporary data. self.wdir = Directory(self.workdir) self.indir = Directory(os.path.join(self.workdir, "indata")) self.outdir = Directory(os.path.join(self.workdir, "outdata")) self.tmpdir = Directory(os.path.join(self.workdir, "tmpdata")) # stderr and output file of the queue manager. Note extensions. self.qerr_file = File(os.path.join(self.workdir, "queue.qerr")) self.qout_file = File(os.path.join(self.workdir, "queue.qout")) def set_manager(self, manager): """Set the :class:`TaskManager` used to launch the Task.""" self.manager = manager.deepcopy() @property def work(self): """The :class:`Work` containing this `Task`.""" return self._work def set_work(self, work): """Set the :class:`Work` associated to this `Task`.""" if not hasattr(self, "_work"): self._work = work else: if self._work != work: raise ValueError("self._work != work") @property def flow(self): """The :class:`Flow` containing this `Task`.""" return self.work.flow @lazy_property def pos(self): """The position of the task in the :class:`Flow`""" for i, task in enumerate(self.work): if self == task: return self.work.pos, i raise ValueError("Cannot find the position of %s in flow %s" % (self, self.flow)) @property def pos_str(self): """String representation of self.pos""" return "w" + str(self.pos[0]) + "_t" + str(self.pos[1]) @property def num_launches(self): """ Number of launches performed. This number includes both possible ABINIT restarts as well as possible launches done due to errors encountered with the resource manager or the hardware/software.""" return sum(q.num_launches for q in self.manager.qads) @property def input(self): """AbinitInput object.""" return self._input def get_inpvar(self, varname, default=None): """Return the value of the ABINIT variable varname, None if not present.""" return self.input.get(varname, default) def set_vars(self, *args, **kwargs): """ Set the values of the ABINIT variables in the input file. Return dict with old values. """ kwargs.update(dict(*args)) old_values = {vname: self.input.get(vname) for vname in kwargs} self.input.set_vars(**kwargs) if kwargs or old_values: self.history.info("Setting input variables: %s" % str(kwargs)) self.history.info("Old values: %s" % str(old_values)) return old_values @property def initial_structure(self): """Initial structure of the task.""" return self.input.structure def make_input(self, with_header=False): """Construct the input file of the calculation.""" s = str(self.input) if with_header: s = str(self) + "\n" + s return s def ipath_from_ext(self, ext): """ Returns the path of the input file with extension ext. Use it when the file does not exist yet. """ return os.path.join(self.workdir, self.prefix.idata + "_" + ext) def opath_from_ext(self, ext): """ Returns the path of the output file with extension ext. Use it when the file does not exist yet. """ return os.path.join(self.workdir, self.prefix.odata + "_" + ext) @property @abc.abstractmethod def executable(self): """ Path to the executable associated to the task (internally stored in self._executable). """ def set_executable(self, executable): """Set the executable associate to this task.""" self._executable = executable @property def process(self): try: return self._process except AttributeError: # Attach a fake process so that we can poll it. return FakeProcess() @property def is_abinit_task(self): """True if this task is a subclass of AbinitTask.""" return isinstance(self, AbinitTask) @property def is_anaddb_task(self): """True if this task is a subclass of OpticTask.""" return isinstance(self, AnaddbTask) @property def is_optic_task(self): """True if this task is a subclass of OpticTask.""" return isinstance(self, OpticTask) @property def is_completed(self): """True if the task has been executed.""" return self.status >= self.S_DONE @property def can_run(self): """The task can run if its status is < S_SUB and all the other dependencies (if any) are done!""" all_ok = all(stat == self.S_OK for stat in self.deps_status) return self.status < self.S_SUB and self.status != self.S_LOCKED and all_ok #@check_spectator def cancel(self): """Cancel the job. Returns 1 if job was cancelled.""" if self.queue_id is None: return 0 if self.status >= self.S_DONE: return 0 exit_status = self.manager.cancel(self.queue_id) if exit_status != 0: logger.warning("manager.cancel returned exit_status: %s" % exit_status) return 0 # Remove output files and reset the status. self.history.info("Job %s cancelled by user" % self.queue_id) self.reset() return 1 def with_fixed_mpi_omp(self, mpi_procs, omp_threads): """ Disable autoparal and force execution with `mpi_procs` MPI processes and `omp_threads` OpenMP threads. Useful for generating benchmarks. """ manager = self.manager if hasattr(self, "manager") else self.flow.manager self.manager = manager.new_with_fixed_mpi_omp(mpi_procs, omp_threads) #def set_max_ncores(self, max_ncores): # """ # """ # manager = self.manager if hasattr(self, "manager") else self.flow.manager # self.manager = manager.new_with_max_ncores(mpi_procs, omp_threads) #@check_spectator def _on_done(self): self.fix_ofiles() #@check_spectator def _on_ok(self): # Fix output file names. self.fix_ofiles() # Get results results = self.on_ok() self.finalized = True return results #@check_spectator def on_ok(self): """ This method is called once the `Task` has reached status S_OK. Subclasses should provide their own implementation Returns: Dictionary that must contain at least the following entries: returncode: 0 on success. message: a string that should provide a human-readable description of what has been performed. """ return dict(returncode=0, message="Calling on_all_ok of the base class!") #@check_spectator def fix_ofiles(self): """ This method is called when the task reaches S_OK. It changes the extension of particular output files produced by Abinit so that the 'official' extension is preserved e.g. out_1WF14 --> out_1WF """ filepaths = self.outdir.list_filepaths() #logger.info("in fix_ofiles with filepaths %s" % list(filepaths)) old2new = FilepathFixer().fix_paths(filepaths) for old, new in old2new.items(): self.history.info("will rename old %s to new %s" % (old, new)) os.rename(old, new) #@check_spectator def _restart(self, submit=True): """ Called by restart once we have finished preparing the task for restarting. Return: True if task has been restarted """ self.set_status(self.S_READY, msg="Restarted on %s" % time.asctime()) # Increase the counter. self.num_restarts += 1 self.history.info("Restarted, num_restarts %d" % self.num_restarts) # Reset datetimes self.datetimes.reset() # Remove the lock file self.start_lockfile.remove() if submit: # Relaunch the task. fired = self.start() if not fired: self.history.warning("Restart failed") else: fired = False return fired #@check_spectator def restart(self): """ Restart the calculation. Subclasses should provide a concrete version that performs all the actions needed for preparing the restart and then calls self._restart to restart the task. The default implementation is empty. Returns: 1 if job was restarted, 0 otherwise. """ logger.debug("Calling the **empty** restart method of the base class") return 0 def poll(self): """Check if child process has terminated. Set and return returncode attribute.""" self._returncode = self.process.poll() if self._returncode is not None: self.set_status(self.S_DONE, "status set to Done") return self._returncode def wait(self): """Wait for child process to terminate. Set and return returncode attribute.""" self._returncode = self.process.wait() try: self.process.stderr.close() except: pass self.set_status(self.S_DONE, "status set to Done") return self._returncode def communicate(self, input=None): """ Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdoutdata, stderrdata). """ stdoutdata, stderrdata = self.process.communicate(input=input) self._returncode = self.process.returncode self.set_status(self.S_DONE, "status set to Done") return stdoutdata, stderrdata def kill(self): """Kill the child.""" self.process.kill() self.set_status(self.S_ERROR, "status set to Error by task.kill") self._returncode = self.process.returncode @property def returncode(self): """ The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn't terminated yet. A negative value -N indicates that the child was terminated by signal N (Unix only). """ try: return self._returncode except AttributeError: return 0 def reset(self): """ Reset the task status. Mainly used if we made a silly mistake in the initial setup of the queue manager and we want to fix it and rerun the task. Returns: 0 on success, 1 if reset failed. """ # Can only reset tasks that are done. # One should be able to reset 'Submitted' tasks (sometimes, they are not in the queue # and we want to restart them) #if self.status != self.S_SUB and self.status < self.S_DONE: return 1 # Remove output files otherwise the EventParser will think the job is still running self.output_file.remove() self.log_file.remove() self.stderr_file.remove() self.start_lockfile.remove() self.qerr_file.remove() self.qout_file.remove() if self.mpiabort_file.exists: self.mpiabort_file.remove() self.set_status(self.S_INIT, msg="Reset on %s" % time.asctime()) self.num_restarts = 0 self.set_qjob(None) # Reset finalized flags. self.work.finalized = False self.flow.finalized = False return 0 @property @return_none_if_raise(AttributeError) def queue_id(self): """Queue identifier returned by the Queue manager. None if not set""" return self.qjob.qid @property @return_none_if_raise(AttributeError) def qname(self): """Queue name identifier returned by the Queue manager. None if not set""" return self.qjob.qname @property def qjob(self): return self._qjob def set_qjob(self, qjob): """Set info on queue after submission.""" self._qjob = qjob @property def has_queue(self): """True if we are submitting jobs via a queue manager.""" return self.manager.qadapter.QTYPE.lower() != "shell" @property def num_cores(self): """Total number of CPUs used to run the task.""" return self.manager.num_cores @property def mpi_procs(self): """Number of CPUs used for MPI.""" return self.manager.mpi_procs @property def omp_threads(self): """Number of CPUs used for OpenMP.""" return self.manager.omp_threads @property def mem_per_proc(self): """Memory per MPI process.""" return Memory(self.manager.mem_per_proc, "Mb") @property def status(self): """Gives the status of the task.""" return self._status def lock(self, source_node): """Lock the task, source is the :class:`Node` that applies the lock.""" if self.status != self.S_INIT: raise ValueError("Trying to lock a task with status %s" % self.status) self._status = self.S_LOCKED self.history.info("Locked by node %s", source_node) def unlock(self, source_node, check_status=True): """ Unlock the task, set its status to `S_READY` so that the scheduler can submit it. source_node is the :class:`Node` that removed the lock Call task.check_status if check_status is True. """ if self.status != self.S_LOCKED: raise RuntimeError("Trying to unlock a task with status %s" % self.status) self._status = self.S_READY if check_status: self.check_status() self.history.info("Unlocked by %s", source_node) #@check_spectator def set_status(self, status, msg): """ Set and return the status of the task. Args: status: Status object or string representation of the status msg: string with human-readable message used in the case of errors. """ # truncate string if it's long. msg will be logged in the object and we don't want to waste memory. if len(msg) > 2000: msg = msg[:2000] msg += "\n... snip ...\n" # Locked files must be explicitly unlocked if self.status == self.S_LOCKED or status == self.S_LOCKED: err_msg = ( "Locked files must be explicitly unlocked before calling set_status but\n" "task.status = %s, input status = %s" % (self.status, status)) raise RuntimeError(err_msg) status = Status.as_status(status) changed = True if hasattr(self, "_status"): changed = (status != self._status) self._status = status if status == self.S_RUN: # Set datetimes.start when the task enters S_RUN if self.datetimes.start is None: self.datetimes.start = datetime.datetime.now() # Add new entry to history only if the status has changed. if changed: if status == self.S_SUB: self.datetimes.submission = datetime.datetime.now() self.history.info("Submitted with MPI=%s, Omp=%s, Memproc=%.1f [Gb] %s " % ( self.mpi_procs, self.omp_threads, self.mem_per_proc.to("Gb"), msg)) elif status == self.S_OK: self.history.info("Task completed %s", msg) elif status == self.S_ABICRITICAL: self.history.info("Status set to S_ABI_CRITICAL due to: %s", msg) else: self.history.info("Status changed to %s. msg: %s", status, msg) ####################################################### # The section belows contains callbacks that should not # be executed if we are in spectator_mode ####################################################### if status == self.S_DONE: # Execute the callback self._on_done() if status == self.S_OK: # Finalize the task. if not self.finalized: self._on_ok() # here we remove the output files of the task and of its parents. if self.gc is not None and self.gc.policy == "task": self.clean_output_files() if self.status == self.S_OK: # Because _on_ok might have changed the status. self.send_signal(self.S_OK) return status def check_status(self): """ This function checks the status of the task by inspecting the output and the error files produced by the application and by the queue manager. """ # 1) see it the job is blocked # 2) see if an error occured at submitting the job the job was submitted, TODO these problems can be solved # 3) see if there is output # 4) see if abinit reports problems # 5) see if both err files exist and are empty # 6) no output and no err files, the job must still be running # 7) try to find out what caused the problems # 8) there is a problem but we did not figure out what ... # 9) the only way of landing here is if there is a output file but no err files... # 1) A locked task can only be unlocked by calling set_status explicitly. # an errored task, should not end up here but just to be sure black_list = (self.S_LOCKED, self.S_ERROR) #if self.status in black_list: return self.status # 2) Check the returncode of the job script if self.returncode != 0: msg = "job.sh return code: %s\nPerhaps the job was not submitted properly?" % self.returncode return self.set_status(self.S_QCRITICAL, msg=msg) # If we have an abort file produced by Abinit if self.mpiabort_file.exists: return self.set_status(self.S_ABICRITICAL, msg="Found ABINIT abort file") # Analyze the stderr file for Fortran runtime errors. # getsize is 0 if the file is empty or it does not exist. err_msg = None if self.stderr_file.getsize() != 0: err_msg = self.stderr_file.read() # Analyze the stderr file of the resource manager runtime errors. # TODO: Why are we looking for errors in queue.qerr? qerr_info = None if self.qerr_file.getsize() != 0: qerr_info = self.qerr_file.read() # Analyze the stdout file of the resource manager (needed for PBS !) qout_info = None if self.qout_file.getsize(): qout_info = self.qout_file.read() # Start to check ABINIT status if the output file has been created. #if self.output_file.getsize() != 0: if self.output_file.exists: try: report = self.get_event_report() except Exception as exc: msg = "%s exception while parsing event_report:\n%s" % (self, exc) return self.set_status(self.S_ABICRITICAL, msg=msg) if report is None: return self.set_status(self.S_ERROR, msg="got None report!") if report.run_completed: # Here we set the correct timing data reported by Abinit self.datetimes.start = report.start_datetime self.datetimes.end = report.end_datetime # Check if the calculation converged. not_ok = report.filter_types(self.CRITICAL_EVENTS) if not_ok: return self.set_status(self.S_UNCONVERGED, msg='status set to unconverged based on abiout') else: return self.set_status(self.S_OK, msg="status set to ok based on abiout") # Calculation still running or errors? if report.errors: # Abinit reported problems logger.debug('Found errors in report') for error in report.errors: logger.debug(str(error)) try: self.abi_errors.append(error) except AttributeError: self.abi_errors = [error] # The job is unfixable due to ABINIT errors logger.debug("%s: Found Errors or Bugs in ABINIT main output!" % self) msg = "\n".join(map(repr, report.errors)) return self.set_status(self.S_ABICRITICAL, msg=msg) # 5) if self.stderr_file.exists and not err_msg: if self.qerr_file.exists and not qerr_info: # there is output and no errors # The job still seems to be running return self.set_status(self.S_RUN, msg='there is output and no errors: job still seems to be running') # 6) if not self.output_file.exists: logger.debug("output_file does not exists") if not self.stderr_file.exists and not self.qerr_file.exists: # No output at allThe job is still in the queue. return self.status # 7) Analyze the files of the resource manager and abinit and execution err (mvs) # MG: This section has been disabled: several portability issues # Need more robust logic in error_parser, perhaps logic provided by users via callbacks. if False and (qerr_info or qout_info): from pymatgen.io.abinit.scheduler_error_parsers import get_parser scheduler_parser = get_parser(self.manager.qadapter.QTYPE, err_file=self.qerr_file.path, out_file=self.qout_file.path, run_err_file=self.stderr_file.path) if scheduler_parser is None: return self.set_status(self.S_QCRITICAL, msg="Cannot find scheduler_parser for qtype %s" % self.manager.qadapter.QTYPE) scheduler_parser.parse() if scheduler_parser.errors: # Store the queue errors in the task self.queue_errors = scheduler_parser.errors # The job is killed or crashed and we know what happened msg = "scheduler errors found:\n%s" % str(scheduler_parser.errors) return self.set_status(self.S_QCRITICAL, msg=msg) elif lennone(qerr_info) > 0: # if only qout_info, we are not necessarily in QCRITICAL state, # since there will always be info in the qout file self.history.info('Found unknown message in the queue qerr file: %s' % str(qerr_info)) #try: # rt = self.datetimes.get_runtime().seconds #except: # rt = -1.0 #tl = self.manager.qadapter.timelimit #if rt > tl: # msg += 'set to error : runtime (%s) exceded walltime (%s)' % (rt, tl) # print(msg) # return self.set_status(self.S_ERROR, msg=msg) # The job may be killed or crashed but we don't know what happened # It may also be that an innocent message was written to qerr, so we wait for a while # it is set to QCritical, we will attempt to fix it by running on more resources # 8) analyzing the err files and abinit output did not identify a problem # but if the files are not empty we do have a problem but no way of solving it: # The job is killed or crashed but we don't know what happend # it is set to QCritical, we will attempt to fix it by running on more resources if err_msg: msg = 'Found error message:\n %s' % str(err_msg) self.history.warning(msg) #return self.set_status(self.S_QCRITICAL, msg=msg) # 9) if we still haven't returned there is no indication of any error and the job can only still be running # but we should actually never land here, or we have delays in the file system .... # print('the job still seems to be running maybe it is hanging without producing output... ') # Check time of last modification. if self.output_file.exists and \ (time.time() - self.output_file.get_stat().st_mtime > self.manager.policy.frozen_timeout): msg = "Task seems to be frozen, last change more than %s [s] ago" % self.manager.policy.frozen_timeout return self.set_status(self.S_ERROR, msg=msg) # Handle weird case in which either run.abo, or run.log have not been produced #if self.status not in (self.S_INIT, self.S_READY) and (not self.output.file.exists or not self.log_file.exits): # msg = "Task have been submitted but cannot find the log file or the output file" # return self.set_status(self.S_ERROR, msg) return self.set_status(self.S_RUN, msg='final option: nothing seems to be wrong, the job must still be running') def reduce_memory_demand(self): """ Method that can be called by the scheduler to decrease the memory demand of a specific task. Returns True in case of success, False in case of Failure. Should be overwritten by specific tasks. """ return False def speed_up(self): """ Method that can be called by the flow to decrease the time needed for a specific task. Returns True in case of success, False in case of Failure Should be overwritten by specific tasks. """ return False def out_to_in(self, out_file): """ Move an output file to the output data directory of the `Task` and rename the file so that ABINIT will read it as an input data file. Returns: The absolute path of the new file in the indata directory. """ in_file = os.path.basename(out_file).replace("out", "in", 1) dest = os.path.join(self.indir.path, in_file) if os.path.exists(dest) and not os.path.islink(dest): logger.warning("Will overwrite %s with %s" % (dest, out_file)) os.rename(out_file, dest) return dest def inlink_file(self, filepath): """ Create a symbolic link to the specified file in the directory containing the input files of the task. """ if not os.path.exists(filepath): logger.debug("Creating symbolic link to not existent file %s" % filepath) # Extract the Abinit extension and add the prefix for input files. root, abiext = abi_splitext(filepath) infile = "in_" + abiext infile = self.indir.path_in(infile) # Link path to dest if dest link does not exist. # else check that it points to the expected file. self.history.info("Linking path %s --> %s" % (filepath, infile)) if not os.path.exists(infile): os.symlink(filepath, infile) else: if os.path.realpath(infile) != filepath: raise self.Error("infile %s does not point to filepath %s" % (infile, filepath)) def make_links(self): """ Create symbolic links to the output files produced by the other tasks. .. warning:: This method should be called only when the calculation is READY because it uses a heuristic approach to find the file to link. """ for dep in self.deps: filepaths, exts = dep.get_filepaths_and_exts() for path, ext in zip(filepaths, exts): logger.info("Need path %s with ext %s" % (path, ext)) dest = self.ipath_from_ext(ext) if not os.path.exists(path): # Try netcdf file. # TODO: this case should be treated in a cleaner way. path += ".nc" if os.path.exists(path): dest += ".nc" if not os.path.exists(path): raise self.Error("%s: %s is needed by this task but it does not exist" % (self, path)) if path.endswith(".nc") and not dest.endswith(".nc"): # NC --> NC file dest += ".nc" # Link path to dest if dest link does not exist. # else check that it points to the expected file. logger.debug("Linking path %s --> %s" % (path, dest)) if not os.path.exists(dest): os.symlink(path, dest) else: # check links but only if we haven't performed the restart. # in this case, indeed we may have replaced the file pointer with the # previous output file of the present task. if os.path.realpath(dest) != path and self.num_restarts == 0: raise self.Error("\nDestination:\n %s\ndoes not point to path:\n %s" % (dest, path)) @abc.abstractmethod def setup(self): """Public method called before submitting the task.""" def _setup(self): """ This method calls self.setup after having performed additional operations such as the creation of the symbolic links needed to connect different tasks. """ self.make_links() self.setup() def get_event_report(self, source="log"): """ Analyzes the main logfile of the calculation for possible Errors or Warnings. If the ABINIT abort file is found, the error found in this file are added to the output report. Args: source: "output" for the main output file,"log" for the log file. Returns: :class:`EventReport` instance or None if the source file file does not exist. """ # By default, we inspect the main log file. ofile = { "output": self.output_file, "log": self.log_file}[source] parser = events.EventsParser() if not ofile.exists: if not self.mpiabort_file.exists: return None else: # ABINIT abort file without log! abort_report = parser.parse(self.mpiabort_file.path) return abort_report try: report = parser.parse(ofile.path) #self._prev_reports[source] = report # Add events found in the ABI_MPIABORTFILE. if self.mpiabort_file.exists: logger.critical("Found ABI_MPIABORTFILE!!!!!") abort_report = parser.parse(self.mpiabort_file.path) if len(abort_report) != 1: logger.critical("Found more than one event in ABI_MPIABORTFILE") # Weird case: empty abort file, let's skip the part # below and hope that the log file contains the error message. #if not len(abort_report): return report # Add it to the initial report only if it differs # from the last one found in the main log file. last_abort_event = abort_report[-1] if report and last_abort_event != report[-1]: report.append(last_abort_event) else: report.append(last_abort_event) return report #except parser.Error as exc: except Exception as exc: # Return a report with an error entry with info on the exception. msg = "%s: Exception while parsing ABINIT events:\n %s" % (ofile, str(exc)) self.set_status(self.S_ABICRITICAL, msg=msg) return parser.report_exception(ofile.path, exc) def get_results(self, **kwargs): """ Returns :class:`NodeResults` instance. Subclasses should extend this method (if needed) by adding specialized code that performs some kind of post-processing. """ # Check whether the process completed. if self.returncode is None: raise self.Error("return code is None, you should call wait, communicate or poll") if self.status is None or self.status < self.S_DONE: raise self.Error("Task is not completed") return self.Results.from_node(self) def move(self, dest, is_abspath=False): """ Recursively move self.workdir to another location. This is similar to the Unix "mv" command. The destination path must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics. Be default, dest is located in the parent directory of self.workdir. Use is_abspath=True to specify an absolute path. """ if not is_abspath: dest = os.path.join(os.path.dirname(self.workdir), dest) shutil.move(self.workdir, dest) def in_files(self): """Return all the input data files used.""" return self.indir.list_filepaths() def out_files(self): """Return all the output data files produced.""" return self.outdir.list_filepaths() def tmp_files(self): """Return all the input data files produced.""" return self.tmpdir.list_filepaths() def path_in_workdir(self, filename): """Create the absolute path of filename in the top-level working directory.""" return os.path.join(self.workdir, filename) def rename(self, src_basename, dest_basename, datadir="outdir"): """ Rename a file located in datadir. src_basename and dest_basename are the basename of the source file and of the destination file, respectively. """ directory = { "indir": self.indir, "outdir": self.outdir, "tmpdir": self.tmpdir, }[datadir] src = directory.path_in(src_basename) dest = directory.path_in(dest_basename) os.rename(src, dest) #@check_spectator def build(self, *args, **kwargs): """ Creates the working directory and the input files of the :class:`Task`. It does not overwrite files if they already exist. """ # Create dirs for input, output and tmp data. self.indir.makedirs() self.outdir.makedirs() self.tmpdir.makedirs() # Write files file and input file. if not self.files_file.exists: self.files_file.write(self.filesfile_string) self.input_file.write(self.make_input()) self.manager.write_jobfile(self) #@check_spectator def rmtree(self, exclude_wildcard=""): """ Remove all files and directories in the working directory Args: exclude_wildcard: Optional string with regular expressions separated by |. Files matching one of the regular expressions will be preserved. example: exclude_wildcard="*.nc|*.txt" preserves all the files whose extension is in ["nc", "txt"]. """ if not exclude_wildcard: shutil.rmtree(self.workdir) else: w = WildCard(exclude_wildcard) for dirpath, dirnames, filenames in os.walk(self.workdir): for fname in filenames: filepath = os.path.join(dirpath, fname) if not w.match(fname): os.remove(filepath) def remove_files(self, *filenames): """Remove all the files listed in filenames.""" filenames = list_strings(filenames) for dirpath, dirnames, fnames in os.walk(self.workdir): for fname in fnames: if fname in filenames: filepath = os.path.join(dirpath, fname) os.remove(filepath) def clean_output_files(self, follow_parents=True): """ This method is called when the task reaches S_OK. It removes all the output files produced by the task that are not needed by its children as well as the output files produced by its parents if no other node needs them. Args: follow_parents: If true, the output files of the parents nodes will be removed if possible. Return: list with the absolute paths of the files that have been removed. """ paths = [] if self.status != self.S_OK: logger.warning("Calling task.clean_output_files on a task whose status != S_OK") # Remove all files in tmpdir. self.tmpdir.clean() # Find the file extensions that should be preserved since these files are still # needed by the children who haven't reached S_OK except_exts = set() for child in self.get_children(): if child.status == self.S_OK: continue # Find the position of self in child.deps and add the extensions. i = [dep.node for dep in child.deps].index(self) except_exts.update(child.deps[i].exts) # Remove the files in the outdir of the task but keep except_exts. exts = self.gc.exts.difference(except_exts) #print("Will remove its extensions: ", exts) paths += self.outdir.remove_exts(exts) if not follow_parents: return paths # Remove the files in the outdir of my parents if all the possible dependencies have been fulfilled. for parent in self.get_parents(): # Here we build a dictionary file extension --> list of child nodes requiring this file from parent # e.g {"WFK": [node1, node2]} ext2nodes = collections.defaultdict(list) for child in parent.get_children(): if child.status == child.S_OK: continue i = [d.node for d in child.deps].index(parent) for ext in child.deps[i].exts: ext2nodes[ext].append(child) # Remove extension only if no node depends on it! except_exts = [k for k, lst in ext2nodes.items() if lst] exts = self.gc.exts.difference(except_exts) #print("%s removes extensions %s from parent node %s" % (self, exts, parent)) paths += parent.outdir.remove_exts(exts) self.history.info("Removed files: %s" % paths) return paths def setup(self): """Base class does not provide any hook.""" #@check_spectator def start(self, **kwargs): """ Starts the calculation by performing the following steps: - build dirs and files - call the _setup method - execute the job file by executing/submitting the job script. Main entry point for the `Launcher`. ============== ============================================================== kwargs Meaning ============== ============================================================== autoparal False to skip the autoparal step (default True) exec_args List of arguments passed to executable. ============== ============================================================== Returns: 1 if task was started, 0 otherwise. """ if self.status >= self.S_SUB: raise self.Error("Task status: %s" % str(self.status)) if self.start_lockfile.exists: self.history.warning("Found lock file: %s" % self.start_lockfile.path) return 0 self.start_lockfile.write("Started on %s" % time.asctime()) self.build() self._setup() # Add the variables needed to connect the node. for d in self.deps: cvars = d.connecting_vars() self.history.info("Adding connecting vars %s" % cvars) self.set_vars(cvars) # Get (python) data from other nodes d.apply_getters(self) # Automatic parallelization if kwargs.pop("autoparal", True) and hasattr(self, "autoparal_run"): try: self.autoparal_run() #except QueueAdapterError as exc: # # If autoparal cannot find a qadapter to run the calculation raises an Exception # self.history.critical(exc) # msg = "Error while trying to run autoparal in task:%s\n%s" % (repr(task), straceback()) # cprint(msg, "yellow") # self.set_status(self.S_QCRITICAL, msg=msg) # return 0 except Exception as exc: # Sometimes autoparal_run fails because Abinit aborts # at the level of the parser e.g. cannot find the spacegroup # due to some numerical noise in the structure. # In this case we call fix_abicritical and then we try to run autoparal again. self.history.critical("First call to autoparal failed with `%s`. Will try fix_abicritical" % exc) msg = "autoparal_fake_run raised:\n%s" % straceback() logger.critical(msg) fixed = self.fix_abicritical() if not fixed: self.set_status(self.S_ABICRITICAL, msg="fix_abicritical could not solve the problem") return 0 try: self.autoparal_run() self.history.info("Second call to autoparal succeeded!") #cprint("Second call to autoparal succeeded!", "green") except Exception as exc: self.history.critical("Second call to autoparal failed with %s. Cannot recover!", exc) msg = "Tried autoparal again but got:\n%s" % straceback() cprint(msg, "red") self.set_status(self.S_ABICRITICAL, msg=msg) return 0 # Start the calculation in a subprocess and return. self._process = self.manager.launch(self, **kwargs) return 1 def start_and_wait(self, *args, **kwargs): """ Helper method to start the task and wait for completion. Mainly used when we are submitting the task via the shell without passing through a queue manager. """ self.start(*args, **kwargs) retcode = self.wait() return retcode def get_graphviz(self, engine="automatic", graph_attr=None, node_attr=None, edge_attr=None): """ Generate task graph in the DOT language (only parents and children of this task). Args: engine: ['dot', 'neato', 'twopi', 'circo', 'fdp', 'sfdp', 'patchwork', 'osage'] graph_attr: Mapping of (attribute, value) pairs for the graph. node_attr: Mapping of (attribute, value) pairs set for all nodes. edge_attr: Mapping of (attribute, value) pairs set for all edges. Returns: graphviz.Digraph <https://graphviz.readthedocs.io/en/stable/api.html#digraph> """ # https://www.graphviz.org/doc/info/ from graphviz import Digraph fg = Digraph("task", # filename="task_%s.gv" % os.path.basename(self.workdir), engine="dot" if engine == "automatic" else engine) # Set graph attributes. #fg.attr(label="%s@%s" % (self.__class__.__name__, self.relworkdir)) fg.attr(label=repr(self)) #fg.attr(fontcolor="white", bgcolor='purple:pink') #fg.attr(rankdir="LR", pagedir="BL") #fg.attr(constraint="false", pack="true", packMode="clust") fg.node_attr.update(color='lightblue2', style='filled') # Add input attributes. if graph_attr is not None: fg.graph_attr.update(**graph_attr) if node_attr is not None: fg.node_attr.update(**node_attr) if edge_attr is not None: fg.edge_attr.update(**edge_attr) def node_kwargs(node): return dict( #shape="circle", color=node.color_hex, label=(str(node) if not hasattr(node, "pos_str") else node.pos_str + "\n" + node.__class__.__name__), ) edge_kwargs = dict(arrowType="vee", style="solid") cluster_kwargs = dict(rankdir="LR", pagedir="BL", style="rounded", bgcolor="azure2") # Build cluster with tasks. cluster_name = "cluster%s" % self.work.name with fg.subgraph(name=cluster_name) as wg: wg.attr(**cluster_kwargs) wg.attr(label="%s (%s)" % (self.__class__.__name__, self.name)) wg.node(self.name, **node_kwargs(self)) # Connect task to children. for child in self.get_children(): # Test if child is in the same work. myg = wg if child in self.work else fg myg.node(child.name, **node_kwargs(child)) # Find file extensions required by this task i = [dep.node for dep in child.deps].index(self) edge_label = "+".join(child.deps[i].exts) myg.edge(self.name, child.name, label=edge_label, color=self.color_hex, **edge_kwargs) # Connect task to parents for parent in self.get_parents(): # Test if parent is in the same work. myg = wg if parent in self.work else fg myg.node(parent.name, **node_kwargs(parent)) # Find file extensions required by self (task) i = [dep.node for dep in self.deps].index(parent) edge_label = "+".join(self.deps[i].exts) myg.edge(parent.name, self.name, label=edge_label, color=parent.color_hex, **edge_kwargs) # Treat the case in which we have a work producing output for other tasks. #for work in self: # children = work.get_children() # if not children: continue # cluster_name = "cluster%s" % work.name # seen = set() # for child in children: # # This is not needed, too much confusing # #fg.edge(cluster_name, child.name, color=work.color_hex, **edge_kwargs) # # Find file extensions required by work # i = [dep.node for dep in child.deps].index(work) # for ext in child.deps[i].exts: # out = "%s (%s)" % (ext, work.name) # fg.node(out) # fg.edge(out, child.name, **edge_kwargs) # key = (cluster_name, out) # if key not in seen: # fg.edge(cluster_name, out, color=work.color_hex, **edge_kwargs) # seen.add(key) return fg class DecreaseDemandsError(Exception): """ exception to be raised by a task if the request to decrease some demand, load or memory, could not be performed """ class AbinitTask(Task): """ Base class defining an ABINIT calculation """ Results = TaskResults @classmethod def from_input(cls, input, workdir=None, manager=None): """ Create an instance of `AbinitTask` from an ABINIT input. Args: ainput: `AbinitInput` object. workdir: Path to the working directory. manager: :class:`TaskManager` object. """ return cls(input, workdir=workdir, manager=manager) @classmethod def temp_shell_task(cls, inp, mpi_procs=1, workdir=None, manager=None): """ Build a Task with a temporary workdir. The task is executed via the shell with 1 MPI proc. Mainly used for invoking Abinit to get important parameters needed to prepare the real task. Args: mpi_procs: Number of MPI processes to use. """ # Build a simple manager to run the job in a shell subprocess import tempfile workdir = tempfile.mkdtemp() if workdir is None else workdir if manager is None: manager = TaskManager.from_user_config() # Construct the task and run it task = cls.from_input(inp, workdir=workdir, manager=manager.to_shell_manager(mpi_procs=mpi_procs)) task.set_name('temp_shell_task') return task def setup(self): """ Abinit has the very *bad* habit of changing the file extension by appending the characters in [A,B ..., Z] to the output file, and this breaks a lot of code that relies of the use of a unique file extension. Here we fix this issue by renaming run.abo to run.abo_[number] if the output file "run.abo" already exists. A few lines of code in python, a lot of problems if you try to implement this trick in Fortran90. """ def rename_file(afile): """Helper function to rename :class:`File` objects. Return string for logging purpose.""" # Find the index of the last file (if any). # TODO: Maybe it's better to use run.abo --> run(1).abo fnames = [f for f in os.listdir(self.workdir) if f.startswith(afile.basename)] nums = [int(f) for f in [f.split("_")[-1] for f in fnames] if f.isdigit()] last = max(nums) if nums else 0 new_path = afile.path + "_" + str(last+1) os.rename(afile.path, new_path) return "Will rename %s to %s" % (afile.path, new_path) logs = [] if self.output_file.exists: logs.append(rename_file(self.output_file)) if self.log_file.exists: logs.append(rename_file(self.log_file)) if logs: self.history.info("\n".join(logs)) @property def executable(self): """Path to the executable required for running the Task.""" try: return self._executable except AttributeError: return "abinit" @property def pseudos(self): """List of pseudos used in the calculation.""" return self.input.pseudos @property def isnc(self): """True if norm-conserving calculation.""" return self.input.isnc @property def ispaw(self): """True if PAW calculation""" return self.input.ispaw @property def is_gs_task(self): """True if task is GsTask subclass.""" return isinstance(self, GsTask) @property def is_dfpt_task(self): """True if task is a DftpTask subclass.""" return isinstance(self, DfptTask) @lazy_property def cycle_class(self): """ Return the subclass of ScfCycle associated to the task or None if no SCF algorithm if associated to the task. """ if isinstance(self, RelaxTask): return abiinspect.Relaxation elif isinstance(self, GsTask): return abiinspect.GroundStateScfCycle elif self.is_dfpt_task: return abiinspect.D2DEScfCycle return None @property def filesfile_string(self): """String with the list of files and prefixes needed to execute ABINIT.""" lines = [] app = lines.append pj = os.path.join app(self.input_file.path) # Path to the input file app(self.output_file.path) # Path to the output file app(pj(self.workdir, self.prefix.idata)) # Prefix for input data app(pj(self.workdir, self.prefix.odata)) # Prefix for output data app(pj(self.workdir, self.prefix.tdata)) # Prefix for temporary data # Paths to the pseudopotential files. # Note that here the pseudos **must** be sorted according to znucl. # Here we reorder the pseudos if the order is wrong. ord_pseudos = [] znucl = [specie.number for specie in self.input.structure.types_of_specie] for z in znucl: for p in self.pseudos: if p.Z == z: ord_pseudos.append(p) break else: raise ValueError("Cannot find pseudo with znucl %s in pseudos:\n%s" % (z, self.pseudos)) for pseudo in ord_pseudos: app(pseudo.path) return "\n".join(lines) def set_pconfs(self, pconfs): """Set the list of autoparal configurations.""" self._pconfs = pconfs @property def pconfs(self): """List of autoparal configurations.""" try: return self._pconfs except AttributeError: return None def uses_paral_kgb(self, value=1): """True if the task is a GS Task and uses paral_kgb with the given value.""" paral_kgb = self.get_inpvar("paral_kgb", 0) # paral_kgb is used only in the GS part. return paral_kgb == value and isinstance(self, GsTask) def _change_structure(self, new_structure): """Change the input structure.""" # Compare new and old structure for logging purpose. # TODO: Write method of structure to compare self and other and return a dictionary old_structure = self.input.structure old_lattice = old_structure.lattice abc_diff = np.array(new_structure.lattice.abc) - np.array(old_lattice.abc) angles_diff = np.array(new_structure.lattice.angles) - np.array(old_lattice.angles) cart_diff = new_structure.cart_coords - old_structure.cart_coords displs = np.array([np.sqrt(np.dot(v, v)) for v in cart_diff]) recs, tol_angle, tol_length = [], 10**-2, 10**-5 if np.any(np.abs(angles_diff) > tol_angle): recs.append("new_agles - old_angles = %s" % angles_diff) if np.any(np.abs(abc_diff) > tol_length): recs.append("new_abc - old_abc = %s" % abc_diff) if np.any(np.abs(displs) > tol_length): min_pos, max_pos = displs.argmin(), displs.argmax() recs.append("Mean displ: %.2E, Max_displ: %.2E (site %d), min_displ: %.2E (site %d)" % (displs.mean(), displs[max_pos], max_pos, displs[min_pos], min_pos)) self.history.info("Changing structure (only significant diffs are shown):") if not recs: self.history.info("Input and output structure seems to be equal within the given tolerances") else: for rec in recs: self.history.info(rec) self.input.set_structure(new_structure) #assert self.input.structure == new_structure def autoparal_run(self): """ Find an optimal set of parameters for the execution of the task This method can change the ABINIT input variables and/or the submission parameters e.g. the number of CPUs for MPI and OpenMp. Set: self.pconfs where pconfs is a :class:`ParalHints` object with the configuration reported by autoparal and optimal is the optimal configuration selected. Returns 0 if success """ policy = self.manager.policy if policy.autoparal == 0: # or policy.max_ncpus in [None, 1]: logger.info("Nothing to do in autoparal, returning (None, None)") return 0 if policy.autoparal != 1: raise NotImplementedError("autoparal != 1") ############################################################################ # Run ABINIT in sequential to get the possible configurations with max_ncpus ############################################################################ # Set the variables for automatic parallelization # Will get all the possible configurations up to max_ncpus # Return immediately if max_ncpus == 1 max_ncpus = self.manager.max_cores if max_ncpus == 1: return 0 autoparal_vars = dict(autoparal=policy.autoparal, max_ncpus=max_ncpus, mem_test=0) self.set_vars(autoparal_vars) # Run the job in a shell subprocess with mpi_procs = 1 # we don't want to make a request to the queue manager for this simple job! # Return code is always != 0 process = self.manager.to_shell_manager(mpi_procs=1).launch(self) self.history.pop() retcode = process.wait() # To avoid: ResourceWarning: unclosed file <_io.BufferedReader name=87> in py3k process.stderr.close() #process.stdout.close() # Remove the variables added for the automatic parallelization self.input.remove_vars(list(autoparal_vars.keys())) ############################################################## # Parse the autoparal configurations from the main output file ############################################################## parser = ParalHintsParser() try: pconfs = parser.parse(self.output_file.path) except parser.Error: # In principle Abinit should have written a complete log file # because we called .wait() but sometimes the Yaml doc is incomplete and # the parser raises. Let's wait 5 secs and then try again. time.sleep(5) try: pconfs = parser.parse(self.output_file.path) except parser.Error: logger.critical("Error while parsing Autoparal section:\n%s" % straceback()) return 2 ###################################################### # Select the optimal configuration according to policy ###################################################### optconf = self.find_optconf(pconfs) #################################################### # Change the input file and/or the submission script #################################################### self.set_vars(optconf.vars) # Write autoparal configurations to JSON file. d = pconfs.as_dict() d["optimal_conf"] = optconf json_pretty_dump(d, os.path.join(self.workdir, "autoparal.json")) ############## # Finalization ############## # Reset the status, remove garbage files ... self.set_status(self.S_INIT, msg='finished autoparal run') # Remove the output file since Abinit likes to create new files # with extension .outA, .outB if the file already exists. os.remove(self.output_file.path) os.remove(self.log_file.path) os.remove(self.stderr_file.path) return 0 def find_optconf(self, pconfs): """Find the optimal Parallel configuration.""" # Save pconfs for future reference. self.set_pconfs(pconfs) # Select the partition on which we'll be running and set MPI/OMP cores. optconf = self.manager.select_qadapter(pconfs) return optconf def select_files(self, what="o"): """ Helper function used to select the files of a task. Args: what: string with the list of characters selecting the file type Possible choices: i ==> input_file, o ==> output_file, f ==> files_file, j ==> job_file, l ==> log_file, e ==> stderr_file, q ==> qout_file, all ==> all files. """ choices = collections.OrderedDict([ ("i", self.input_file), ("o", self.output_file), ("f", self.files_file), ("j", self.job_file), ("l", self.log_file), ("e", self.stderr_file), ("q", self.qout_file), ]) if what == "all": return [getattr(v, "path") for v in choices.values()] selected = [] for c in what: try: selected.append(getattr(choices[c], "path")) except KeyError: logger.warning("Wrong keyword %s" % c) return selected def restart(self): """ general restart used when scheduler problems have been taken care of """ return self._restart() #@check_spectator def reset_from_scratch(self): """ Restart from scratch, this is to be used if a job is restarted with more resources after a crash Move output files produced in workdir to _reset otherwise check_status continues to see the task as crashed even if the job did not run """ # Create reset directory if not already done. reset_dir = os.path.join(self.workdir, "_reset") reset_file = os.path.join(reset_dir, "_counter") if not os.path.exists(reset_dir): os.mkdir(reset_dir) num_reset = 1 else: with open(reset_file, "rt") as fh: num_reset = 1 + int(fh.read()) # Move files to reset and append digit with reset index. def move_file(f): if not f.exists: return try: f.move(os.path.join(reset_dir, f.basename + "_" + str(num_reset))) except OSError as exc: logger.warning("Couldn't move file {}. exc: {}".format(f, str(exc))) for fname in ("output_file", "log_file", "stderr_file", "qout_file", "qerr_file"): move_file(getattr(self, fname)) with open(reset_file, "wt") as fh: fh.write(str(num_reset)) self.start_lockfile.remove() # Reset datetimes self.datetimes.reset() return self._restart(submit=False) #@check_spectator def fix_abicritical(self): """ method to fix crashes/error caused by abinit Returns: 1 if task has been fixed else 0. """ event_handlers = self.event_handlers if not event_handlers: self.set_status(status=self.S_ERROR, msg='Empty list of event handlers. Cannot fix abi_critical errors') return 0 count, done = 0, len(event_handlers) * [0] report = self.get_event_report() if report is None: self.set_status(status=self.S_ERROR, msg='get_event_report returned None') return 0 # Note we have loop over all possible events (slow, I know) # because we can have handlers for Error, Bug or Warning # (ideally only for CriticalWarnings but this is not done yet) for event in report: for i, handler in enumerate(self.event_handlers): if handler.can_handle(event) and not done[i]: logger.info("handler %s will try to fix event %s" % (handler, event)) try: d = handler.handle_task_event(self, event) if d: done[i] += 1 count += 1 except Exception as exc: logger.critical(str(exc)) if count: self.reset_from_scratch() return 1 self.set_status(status=self.S_ERROR, msg='We encountered AbiCritical events that could not be fixed') return 0 #@check_spectator def fix_queue_critical(self): """ This function tries to fix critical events originating from the queue submission system. General strategy, first try to increase resources in order to fix the problem, if this is not possible, call a task specific method to attempt to decrease the demands. Returns: 1 if task has been fixed else 0. """ from pymatgen.io.abinit.scheduler_error_parsers import NodeFailureError, MemoryCancelError, TimeCancelError #assert isinstance(self.manager, TaskManager) self.history.info('fixing queue critical') ret = "task.fix_queue_critical: " if not self.queue_errors: # TODO # paral_kgb = 1 leads to nasty sigegv that are seen as Qcritical errors! # Try to fallback to the conjugate gradient. #if self.uses_paral_kgb(1): # logger.critical("QCRITICAL with PARAL_KGB==1. Will try CG!") # self.set_vars(paral_kgb=0) # self.reset_from_scratch() # return # queue error but no errors detected, try to solve by increasing ncpus if the task scales # if resources are at maximum the task is definitively turned to errored if self.mem_scales or self.load_scales: try: self.manager.increase_resources() # acts either on the policy or on the qadapter self.reset_from_scratch() ret += "increased resources" return ret except ManagerIncreaseError: self.set_status(self.S_ERROR, msg='unknown queue error, could not increase resources any further') raise FixQueueCriticalError else: self.set_status(self.S_ERROR, msg='unknown queue error, no options left') raise FixQueueCriticalError else: print("Fix_qcritical: received %d queue_errors" % len(self.queue_errors)) print("type_list: %s" % list(type(qe) for qe in self.queue_errors)) for error in self.queue_errors: self.history.info('fixing: %s' % str(error)) ret += str(error) if isinstance(error, NodeFailureError): # if the problematic node is known, exclude it if error.nodes is not None: try: self.manager.exclude_nodes(error.nodes) self.reset_from_scratch() self.set_status(self.S_READY, msg='excluding nodes') except: raise FixQueueCriticalError else: self.set_status(self.S_ERROR, msg='Node error but no node identified.') raise FixQueueCriticalError elif isinstance(error, MemoryCancelError): # ask the qadapter to provide more resources, i.e. more cpu's so more total memory if the code # scales this should fix the memeory problem # increase both max and min ncpu of the autoparalel and rerun autoparalel if self.mem_scales: try: self.manager.increase_ncpus() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased ncps to solve memory problem') return except ManagerIncreaseError: self.history.warning('increasing ncpus failed') # if the max is reached, try to increase the memory per cpu: try: self.manager.increase_mem() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased mem') return except ManagerIncreaseError: self.history.warning('increasing mem failed') # if this failed ask the task to provide a method to reduce the memory demand try: self.reduce_memory_demand() self.reset_from_scratch() self.set_status(self.S_READY, msg='decreased mem demand') return except DecreaseDemandsError: self.history.warning('decreasing demands failed') msg = ('Memory error detected but the memory could not be increased neither could the\n' 'memory demand be decreased. Unrecoverable error.') self.set_status(self.S_ERROR, msg) raise FixQueueCriticalError elif isinstance(error, TimeCancelError): # ask the qadapter to provide more time print('trying to increase time') try: self.manager.increase_time() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased wall time') return except ManagerIncreaseError: self.history.warning('increasing the waltime failed') # if this fails ask the qadapter to increase the number of cpus if self.load_scales: try: self.manager.increase_ncpus() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased number of cpus') return except ManagerIncreaseError: self.history.warning('increase ncpus to speed up the calculation to stay in the walltime failed') # if this failed ask the task to provide a method to speed up the task try: self.speed_up() self.reset_from_scratch() self.set_status(self.S_READY, msg='task speedup') return except DecreaseDemandsError: self.history.warning('decreasing demands failed') msg = ('Time cancel error detected but the time could not be increased neither could\n' 'the time demand be decreased by speedup of increasing the number of cpus.\n' 'Unrecoverable error.') self.set_status(self.S_ERROR, msg) else: msg = 'No solution provided for error %s. Unrecoverable error.' % error.name self.set_status(self.S_ERROR, msg) return 0 def parse_timing(self): """ Parse the timer data in the main output file of Abinit. Requires timopt /= 0 in the input file (usually timopt = -1) Return: :class:`AbinitTimerParser` instance, None if error. """ from .abitimer import AbinitTimerParser parser = AbinitTimerParser() read_ok = parser.parse(self.output_file.path) if read_ok: return parser return None class ProduceHist: """ Mixin class for an :class:`AbinitTask` producing a HIST file. Provide the method `open_hist` that reads and return a HIST file. """ @property def hist_path(self): """Absolute path of the HIST file. Empty string if file is not present.""" # Lazy property to avoid multiple calls to has_abiext. try: return self._hist_path except AttributeError: path = self.outdir.has_abiext("HIST") if path: self._hist_path = path return path def open_hist(self): """ Open the HIST file located in the in self.outdir. Returns :class:`HistFile` object, None if file could not be found or file is not readable. """ if not self.hist_path: if self.status == self.S_OK: logger.critical("%s reached S_OK but didn't produce a HIST file in %s" % (self, self.outdir)) return None # Open the HIST file from abipy.dynamics.hist import HistFile try: return HistFile(self.hist_path) except Exception as exc: logger.critical("Exception while reading HIST file at %s:\n%s" % (self.hist_path, str(exc))) return None class GsTask(AbinitTask): """ Base class for ground-state tasks. A ground state task produces a GSR file Provides the method `open_gsr` that reads and returns a GSR file. """ @property def gsr_path(self): """Absolute path of the GSR file. Empty string if file is not present.""" # Lazy property to avoid multiple calls to has_abiext. try: return self._gsr_path except AttributeError: path = self.outdir.has_abiext("GSR") if path: self._gsr_path = path return path def open_gsr(self): """ Open the GSR file located in the in self.outdir. Returns :class:`GsrFile` object, None if file could not be found or file is not readable. """ gsr_path = self.gsr_path if not gsr_path: if self.status == self.S_OK: logger.critical("%s reached S_OK but didn't produce a GSR file in %s" % (self, self.outdir)) return None # Open the GSR file. from abipy.electrons.gsr import GsrFile try: return GsrFile(gsr_path) except Exception as exc: logger.critical("Exception while reading GSR file at %s:\n%s" % (gsr_path, str(exc))) return None class ScfTask(GsTask): """ Self-consistent ground-state calculations. Provide support for in-place restart via (WFK|DEN) files """ CRITICAL_EVENTS = [ events.ScfConvergenceWarning, ] color_rgb = np.array((255, 0, 0)) / 255 def restart(self): """SCF calculations can be restarted if we have either the WFK file or the DEN file.""" # Prefer WFK over DEN files since we can reuse the wavefunctions. for ext in ("WFK", "DEN"): restart_file = self.outdir.has_abiext(ext) irdvars = irdvars_for_ext(ext) if restart_file: break else: raise self.RestartError("%s: Cannot find WFK or DEN file to restart from." % self) # Move out --> in. self.out_to_in(restart_file) # Add the appropriate variable for restarting. self.set_vars(irdvars) # Now we can resubmit the job. self.history.info("Will restart from %s", restart_file) return self._restart() def inspect(self, **kwargs): """ Plot the SCF cycle results with matplotlib. Returns `matplotlib` figure, None if some error occurred. """ try: scf_cycle = abiinspect.GroundStateScfCycle.from_file(self.output_file.path) except IOError: return None if scf_cycle is not None: if "title" not in kwargs: kwargs["title"] = str(self) return scf_cycle.plot(**kwargs) return None def get_results(self, **kwargs): results = super().get_results(**kwargs) # Open the GSR file and add its data to results.out with self.open_gsr() as gsr: results["out"].update(gsr.as_dict()) # Add files to GridFS results.register_gridfs_files(GSR=gsr.filepath) return results class CollinearThenNonCollinearScfTask(ScfTask): """ A specialized ScfTaks that performs an initial SCF run with nsppol = 2. The spin polarized WFK file is then used to start a non-collinear SCF run (nspinor == 2) initialized from the previous WFK file. """ def __init__(self, input, workdir=None, manager=None, deps=None): super().__init__(input, workdir=workdir, manager=manager, deps=deps) # Enforce nspinor = 1, nsppol = 2 and prtwf = 1. self._input = self.input.deepcopy() self.input.set_spin_mode("polarized") self.input.set_vars(prtwf=1) self.collinear_done = False def _on_ok(self): results = super()._on_ok() if not self.collinear_done: self.input.set_spin_mode("spinor") self.collinear_done = True self.finalized = False self.restart() return results class NscfTask(GsTask): """ Non-Self-consistent GS calculation. Provide in-place restart via WFK files """ CRITICAL_EVENTS = [ events.NscfConvergenceWarning, ] color_rgb = np.array((200, 80, 100)) / 255 def setup(self): """ NSCF calculations should use the same FFT mesh as the one employed in the GS task (in principle, it's possible to interpolate inside Abinit but tests revealed some numerical noise Here we change the input file of the NSCF task to have the same FFT mesh. """ for dep in self.deps: if "DEN" in dep.exts: parent_task = dep.node break else: raise RuntimeError("Cannot find parent node producing DEN file") with parent_task.open_gsr() as gsr: den_mesh = 3 * [None] den_mesh[0] = gsr.reader.read_dimvalue("number_of_grid_points_vector1") den_mesh[1] = gsr.reader.read_dimvalue("number_of_grid_points_vector2") den_mesh[2] = gsr.reader.read_dimvalue("number_of_grid_points_vector3") if self.ispaw: self.set_vars(ngfftdg=den_mesh) else: self.set_vars(ngfft=den_mesh) super().setup() def restart(self): """NSCF calculations can be restarted only if we have the WFK file.""" ext = "WFK" restart_file = self.outdir.has_abiext(ext) if not restart_file: raise self.RestartError("%s: Cannot find the WFK file to restart from." % self) # Move out --> in. self.out_to_in(restart_file) # Add the appropriate variable for restarting. irdvars = irdvars_for_ext(ext) self.set_vars(irdvars) # Now we can resubmit the job. self.history.info("Will restart from %s", restart_file) return self._restart() def get_results(self, **kwargs): results = super().get_results(**kwargs) # Read the GSR file. with self.open_gsr() as gsr: results["out"].update(gsr.as_dict()) # Add files to GridFS results.register_gridfs_files(GSR=gsr.filepath) return results class RelaxTask(GsTask, ProduceHist): """ Task for structural optimizations. """ # TODO possible ScfConvergenceWarning? CRITICAL_EVENTS = [ events.RelaxConvergenceWarning, ] color_rgb = np.array((255, 61, 255)) / 255 def get_final_structure(self): """Read the final structure from the GSR file.""" try: with self.open_gsr() as gsr: return gsr.structure except AttributeError: raise RuntimeError("Cannot find the GSR file with the final structure to restart from.") def restart(self): """ Restart the structural relaxation. Structure relaxations can be restarted only if we have the WFK file or the DEN or the GSR file from which we can read the last structure (mandatory) and the wavefunctions (not mandatory but useful). Prefer WFK over other files since we can reuse the wavefunctions. .. note:: The problem in the present approach is that some parameters in the input are computed from the initial structure and may not be consistent with the modification of the structure done during the structure relaxation. """ restart_file = None # Try to restart from the WFK file if possible. # FIXME: This part has been disabled because WFK=IO is a mess if paral_kgb == 1 # This is also the reason why I wrote my own MPI-IO code for the GW part! wfk_file = self.outdir.has_abiext("WFK") if False and wfk_file: irdvars = irdvars_for_ext("WFK") restart_file = self.out_to_in(wfk_file) # Fallback to DEN file. Note that here we look for out_DEN instead of out_TIM?_DEN # This happens when the previous run completed and task.on_done has been performed. # ******************************************************************************** # Note that it's possible to have an undetected error if we have multiple restarts # and the last relax died badly. In this case indeed out_DEN is the file produced # by the last run that has executed on_done. # ******************************************************************************** if restart_file is None: for ext in ("", ".nc"): out_den = self.outdir.path_in("out_DEN" + ext) if os.path.exists(out_den): irdvars = irdvars_for_ext("DEN") restart_file = self.out_to_in(out_den) break if restart_file is None: # Try to restart from the last TIM?_DEN file. # This should happen if the previous run didn't complete in clean way. # Find the last TIM?_DEN file. last_timden = self.outdir.find_last_timden_file() if last_timden is not None: if last_timden.path.endswith(".nc"): ofile = self.outdir.path_in("out_DEN.nc") else: ofile = self.outdir.path_in("out_DEN") os.rename(last_timden.path, ofile) restart_file = self.out_to_in(ofile) irdvars = irdvars_for_ext("DEN") if restart_file is None: # Don't raise RestartError as we can still change the structure. self.history.warning("Cannot find the WFK|DEN|TIM?_DEN file to restart from.") else: # Add the appropriate variable for restarting. self.set_vars(irdvars) self.history.info("Will restart from %s", restart_file) # FIXME Here we should read the HIST file but restartxf if broken! #self.set_vars({"restartxf": -1}) # Read the relaxed structure from the GSR file and change the input. self._change_structure(self.get_final_structure()) # Now we can resubmit the job. return self._restart() def inspect(self, **kwargs): """ Plot the evolution of the structural relaxation with matplotlib. Args: what: Either "hist" or "scf". The first option (default) extracts data from the HIST file and plot the evolution of the structural parameters, forces, pressures and energies. The second option, extracts data from the main output file and plot the evolution of the SCF cycles (etotal, residuals, etc). Returns: `matplotlib` figure, None if some error occurred. """ what = kwargs.pop("what", "hist") if what == "hist": # Read the hist file to get access to the structure. with self.open_hist() as hist: return hist.plot(**kwargs) if hist else None elif what == "scf": # Get info on the different SCF cycles relaxation = abiinspect.Relaxation.from_file(self.output_file.path) if "title" not in kwargs: kwargs["title"] = str(self) return relaxation.plot(**kwargs) if relaxation is not None else None else: raise ValueError("Wrong value for what %s" % what) def get_results(self, **kwargs): results = super().get_results(**kwargs) # Open the GSR file and add its data to results.out with self.open_gsr() as gsr: results["out"].update(gsr.as_dict()) # Add files to GridFS results.register_gridfs_files(GSR=gsr.filepath) return results def reduce_dilatmx(self, target=1.01): actual_dilatmx = self.get_inpvar('dilatmx', 1.) new_dilatmx = actual_dilatmx - min((actual_dilatmx-target), actual_dilatmx*0.05) self.set_vars(dilatmx=new_dilatmx) def fix_ofiles(self): """ Note that ABINIT produces lots of out_TIM1_DEN files for each step. Here we list all TIM*_DEN files, we select the last one and we rename it in out_DEN This change is needed so that we can specify dependencies with the syntax {node: "DEN"} without having to know the number of iterations needed to converge the run in node! """ super().fix_ofiles() # Find the last TIM?_DEN file. last_timden = self.outdir.find_last_timden_file() if last_timden is None: logger.warning("Cannot find TIM?_DEN files") return # Rename last TIMDEN with out_DEN. ofile = self.outdir.path_in("out_DEN") if last_timden.path.endswith(".nc"): ofile += ".nc" self.history.info("Renaming last_denfile %s --> %s" % (last_timden.path, ofile)) os.rename(last_timden.path, ofile) class DfptTask(AbinitTask): """ Base class for DFPT tasks (Phonons, DdeTask, DdkTask, ElasticTask ...) Mainly used to implement methods that are common to DFPT calculations with Abinit. Provide the method `open_ddb` that reads and return a Ddb file. .. warning:: This class should not be instantiated directly. """ # TODO: # for the time being we don't discern between GS and PhononCalculations. CRITICAL_EVENTS = [ events.ScfConvergenceWarning, ] def __repr__(self): # Get info about DFT perturbation from input file. qpt = self.input.get("qpt", [0, 0, 0]) rfphon = self.input.get("rfphon", 0) rfatpol = self.input.get("rfatpol", [1, 1]) rfelfd = self.input.get("rfelfd", 0) rfstrs = self.input.get("rfstrs", 0) rfdir = self.input.get("rfdir", [0, 0, 0]) irdddk = self.input.get("irdddk", 0) dfpt_info = "" if rfphon != 0: dfpt_info = "qpt: {}, rfphon: {}, rfatpol: {}, rfdir: {}, irdddk: {}".format( qpt, rfphon, rfatpol, rfdir, irdddk) elif rfelfd != 0: dfpt_info = "qpt: {}, rfelfd: {} rfdir: {}, irdddk: {}".format( qpt, rfelfd, rfdir, irdddk) elif rfstrs != 0: dfpt_info = "qpt: {}, rfstrs: {}, rfdir: {}, irdddk: {}".format( qpt, rfstrs, rfdir, irdddk) try: return "<%s, node_id=%s, workdir=%s, %s>" % ( self.__class__.__name__, self.node_id, self.relworkdir, dfpt_info) except AttributeError: # this usually happens when workdir has not been initialized return "<%s, node_id=%s, workdir=None, %s>" % ( self.__class__.__name__, self.node_id, dfpt_info) @property def ddb_path(self): """Absolute path of the DDB file. Empty string if file is not present.""" # Lazy property to avoid multiple calls to has_abiext. try: return self._ddb_path except AttributeError: path = self.outdir.has_abiext("DDB") if path: self._ddb_path = path return path def open_ddb(self): """ Open the DDB file located in the in self.outdir. Returns :class:`DdbFile` object, None if file could not be found or file is not readable. """ ddb_path = self.ddb_path if not ddb_path: if self.status == self.S_OK: logger.critical("%s reached S_OK but didn't produce a DDB file in %s" % (self, self.outdir)) return None # Open the DDB file. from abipy.dfpt.ddb import DdbFile try: return DdbFile(ddb_path) except Exception as exc: logger.critical("Exception while reading DDB file at %s:\n%s" % (ddb_path, str(exc))) return None def make_links(self): """ Replace the default behaviour of make_links. More specifically, this method implements the logic required to connect DFPT calculation to `DDK` files. Remember that DDK is an extension introduced in AbiPy to deal with the irdddk input variable and the fact that the 3 files with du/dk produced by Abinit have a file extension constructed from the number of atom (e.g. 1WF[3natom +1]). AbiPy uses the user-friendly syntax deps={node: "DDK"} to specify that the children will read the DDK from `node` but this also means that we have to implement extract logic to handle this case at runtime. """ for dep in self.deps: for d in dep.exts: if d == "DDK": ddk_task = dep.node out_ddk = ddk_task.outdir.has_abiext("DDK") if not out_ddk: raise RuntimeError("%s didn't produce the DDK file" % ddk_task) # Get (fortran) idir and costruct the name of the 1WF expected by Abinit rfdir = list(ddk_task.input["rfdir"]) if rfdir.count(1) != 1: raise RuntimeError("Only one direction should be specifned in rfdir but rfdir = %s" % rfdir) idir = rfdir.index(1) + 1 ddk_case = idir + 3 * len(ddk_task.input.structure) infile = self.indir.path_in("in_1WF%d" % ddk_case) os.symlink(out_ddk, infile) elif d in ("WFK", "WFQ"): gs_task = dep.node out_wfk = gs_task.outdir.has_abiext(d) if not out_wfk: raise RuntimeError("%s didn't produce the %s file" % (gs_task, d)) if d == "WFK": bname = "in_WFK" elif d == "WFQ": bname = "in_WFQ" else: raise ValueError("Don't know how to handle `%s`" % d) if not os.path.exists(self.indir.path_in(bname)): os.symlink(out_wfk, self.indir.path_in(bname)) elif d == "DEN": gs_task = dep.node out_wfk = gs_task.outdir.has_abiext("DEN") if not out_wfk: raise RuntimeError("%s didn't produce the DEN file" % gs_task) if not os.path.exists(self.indir.path_in("in_DEN")): os.symlink(out_wfk, self.indir.path_in("in_DEN")) elif d == "1WF": gs_task = dep.node out_wfk = gs_task.outdir.has_abiext("1WF") if not out_wfk: raise RuntimeError("%s didn't produce the 1WF file" % gs_task) dest = self.indir.path_in("in_" + out_wfk.split("_")[-1]) if not os.path.exists(dest): os.symlink(out_wfk, dest) elif d == "1DEN": gs_task = dep.node out_wfk = gs_task.outdir.has_abiext("DEN") if not out_wfk: raise RuntimeError("%s didn't produce the 1DEN file" % gs_task) dest = self.indir.path_in("in_" + out_wfk.split("_")[-1]) if not os.path.exists(dest): os.symlink(out_wfk, dest) else: raise ValueError("Don't know how to handle extension: %s" % str(dep.exts)) def restart(self): """ DFPT calculations can be restarted only if we have the 1WF file or the 1DEN file. from which we can read the first-order wavefunctions or the first order density. Prefer 1WF over 1DEN since we can reuse the wavefunctions. """ # Abinit adds the idir-ipert index at the end of the file and this breaks the extension # e.g. out_1WF4, out_DEN4. find_1wf_files and find_1den_files returns the list of files found restart_file, irdvars = None, None # Highest priority to the 1WF file because restart is more efficient. wf_files = self.outdir.find_1wf_files() if wf_files is not None: restart_file = wf_files[0].path irdvars = irdvars_for_ext("1WF") if len(wf_files) != 1: restart_file = None self.history.critical("Found more than one 1WF file in outdir. Restart is ambiguous!") if restart_file is None: den_files = self.outdir.find_1den_files() if den_files is not None: restart_file = den_files[0].path irdvars = {"ird1den": 1} if len(den_files) != 1: restart_file = None self.history.critical("Found more than one 1DEN file in outdir. Restart is ambiguous!") if restart_file is None: # Raise because otherwise restart is equivalent to a run from scratch --> infinite loop! raise self.RestartError("%s: Cannot find the 1WF|1DEN file to restart from." % self) # Move file. self.history.info("Will restart from %s", restart_file) restart_file = self.out_to_in(restart_file) # Add the appropriate variable for restarting. self.set_vars(irdvars) # Now we can resubmit the job. return self._restart() class DdeTask(DfptTask): """Task for DDE calculations (perturbation wrt electric field).""" color_rgb = np.array((61, 158, 255)) / 255 def get_results(self, **kwargs): results = super().get_results(**kwargs) return results.register_gridfs_file(DDB=(self.outdir.has_abiext("DDE"), "t")) class DteTask(DfptTask): """Task for DTE calculations.""" color_rgb = np.array((204, 0, 204)) / 255 # @check_spectator def start(self, **kwargs): kwargs['autoparal'] = False return super().start(**kwargs) def get_results(self, **kwargs): results = super().get_results(**kwargs) return results.register_gridfs_file(DDB=(self.outdir.has_abiext("DDE"), "t")) class DdkTask(DfptTask): """Task for DDK calculations.""" color_rgb = np.array((0, 204, 204)) / 255 #@check_spectator def _on_ok(self): super()._on_ok() # Client code expects to find du/dk in DDK file. # Here I create a symbolic link out_1WF13 --> out_DDK # so that we can use deps={ddk_task: "DDK"} in the high-level API. # The price to pay is that we have to handle the DDK extension in make_links. # See DfptTask.make_links self.outdir.symlink_abiext('1WF', 'DDK') def get_results(self, **kwargs): results = super().get_results(**kwargs) return results.register_gridfs_file(DDK=(self.outdir.has_abiext("DDK"), "t")) class BecTask(DfptTask): """ Task for the calculation of Born effective charges. bec_deps = {ddk_task: "DDK" for ddk_task in ddk_tasks} bec_deps.update({scf_task: "WFK"}) """ color_rgb = np.array((122, 122, 255)) / 255 class PhononTask(DfptTask): """ DFPT calculations for a single atomic perturbation. Provide support for in-place restart via (1WF|1DEN) files """ color_rgb = np.array((0, 150, 250)) / 255 def inspect(self, **kwargs): """ Plot the Phonon SCF cycle results with matplotlib. Returns: `matplotlib` figure, None if some error occurred. """ scf_cycle = abiinspect.PhononScfCycle.from_file(self.output_file.path) if scf_cycle is not None: if "title" not in kwargs: kwargs["title"] = str(self) return scf_cycle.plot(**kwargs) def get_results(self, **kwargs): results = super().get_results(**kwargs) return results.register_gridfs_files(DDB=(self.outdir.has_abiext("DDB"), "t")) class ElasticTask(DfptTask): """ DFPT calculations for a single strain perturbation (uniaxial or shear strain). Provide support for in-place restart via (1WF|1DEN) files """ color_rgb = np.array((255, 204, 255)) / 255 class EphTask(AbinitTask): """ Class for electron-phonon calculations. """ color_rgb = np.array((255, 128, 0)) / 255 class ManyBodyTask(AbinitTask): """ Base class for Many-body tasks (Screening, Sigma, Bethe-Salpeter) Mainly used to implement methods that are common to MBPT calculations with Abinit. .. warning:: This class should not be instantiated directly. """ def reduce_memory_demand(self): """ Method that can be called by the scheduler to decrease the memory demand of a specific task. Returns True in case of success, False in case of Failure. """ # The first digit governs the storage of W(q), the second digit the storage of u(r) # Try to avoid the storage of u(r) first since reading W(q) from file will lead to a drammatic slowdown. prev_gwmem = int(self.get_inpvar("gwmem", default=11)) first_dig, second_dig = prev_gwmem // 10, prev_gwmem % 10 if second_dig == 1: self.set_vars(gwmem="%.2d" % (10 * first_dig)) return True if first_dig == 1: self.set_vars(gwmem="%.2d" % 00) return True # gwmem 00 d'oh! return False class ScrTask(ManyBodyTask): """Tasks for SCREENING calculations """ color_rgb = np.array((255, 128, 0)) / 255 #def inspect(self, **kwargs): # """Plot graph showing the number of q-points computed and the wall-time used""" @property def scr_path(self): """Absolute path of the SCR file. Empty string if file is not present.""" # Lazy property to avoid multiple calls to has_abiext. try: return self._scr_path except AttributeError: path = self.outdir.has_abiext("SCR.nc") if path: self._scr_path = path return path def open_scr(self): """ Open the SIGRES file located in the in self.outdir. Returns :class:`ScrFile` object, None if file could not be found or file is not readable. """ scr_path = self.scr_path if not scr_path: logger.critical("%s didn't produce a SCR.nc file in %s" % (self, self.outdir)) return None # Open the GSR file and add its data to results.out from abipy.electrons.scr import ScrFile try: return ScrFile(scr_path) except Exception as exc: logger.critical("Exception while reading SCR file at %s:\n%s" % (scr_path, str(exc))) return None class SigmaTask(ManyBodyTask): """ Tasks for SIGMA calculations. Provides support for in-place restart via QPS files """ CRITICAL_EVENTS = [ events.QPSConvergenceWarning, ] color_rgb = np.array((0, 255, 0)) / 255 def restart(self): # G calculations can be restarted only if we have the QPS file # from which we can read the results of the previous step. ext = "QPS" restart_file = self.outdir.has_abiext(ext) if not restart_file: raise self.RestartError("%s: Cannot find the QPS file to restart from." % self) self.out_to_in(restart_file) # Add the appropriate variable for restarting. irdvars = irdvars_for_ext(ext) self.set_vars(irdvars) # Now we can resubmit the job. self.history.info("Will restart from %s", restart_file) return self._restart() #def inspect(self, **kwargs): # """Plot graph showing the number of k-points computed and the wall-time used""" @property def sigres_path(self): """Absolute path of the SIGRES file. Empty string if file is not present.""" # Lazy property to avoid multiple calls to has_abiext. try: return self._sigres_path except AttributeError: path = self.outdir.has_abiext("SIGRES") if path: self._sigres_path = path return path def open_sigres(self): """ Open the SIGRES file located in the in self.outdir. Returns :class:`SigresFile` object, None if file could not be found or file is not readable. """ sigres_path = self.sigres_path if not sigres_path: logger.critical("%s didn't produce a SIGRES file in %s" % (self, self.outdir)) return None # Open the SIGRES file and add its data to results.out from abipy.electrons.gw import SigresFile try: return SigresFile(sigres_path) except Exception as exc: logger.critical("Exception while reading SIGRES file at %s:\n%s" % (sigres_path, str(exc))) return None def get_scissors_builder(self): """ Returns an instance of :class:`ScissorsBuilder` from the SIGRES file. Raise: `RuntimeError` if SIGRES file is not found. """ from abipy.electrons.scissors import ScissorsBuilder if self.sigres_path: return ScissorsBuilder.from_file(self.sigres_path) else: raise RuntimeError("Cannot find SIGRES file!") def get_results(self, **kwargs): results = super().get_results(**kwargs) # Open the SIGRES file and add its data to results.out with self.open_sigres() as sigres: #results["out"].update(sigres.as_dict()) results.register_gridfs_files(SIGRES=sigres.filepath) return results class BseTask(ManyBodyTask): """ Task for Bethe-Salpeter calculations. .. note:: The BSE codes provides both iterative and direct schemes for the computation of the dielectric function. The direct diagonalization cannot be restarted whereas Haydock and CG support restarting. """ CRITICAL_EVENTS = [ events.HaydockConvergenceWarning, #events.BseIterativeDiagoConvergenceWarning, ] color_rgb = np.array((128, 0, 255)) / 255 def restart(self): """ BSE calculations with Haydock can be restarted only if we have the excitonic Hamiltonian and the HAYDR_SAVE file. """ # TODO: This version seems to work but the main output file is truncated # TODO: Handle restart if CG method is used # TODO: restart should receive a list of critical events # the log file is complete though. irdvars = {} # Move the BSE blocks to indata. # This is done only once at the end of the first run. # Successive restarts will use the BSR|BSC files in the indir directory # to initialize the excitonic Hamiltonian count = 0 for ext in ("BSR", "BSC"): ofile = self.outdir.has_abiext(ext) if ofile: count += 1 irdvars.update(irdvars_for_ext(ext)) self.out_to_in(ofile) if not count: # outdir does not contain the BSR|BSC file. # This means that num_restart > 1 and the files should be in task.indir count = 0 for ext in ("BSR", "BSC"): ifile = self.indir.has_abiext(ext) if ifile: count += 1 if not count: raise self.RestartError("%s: Cannot find BSR|BSC files in %s" % (self, self.indir)) # Rename HAYDR_SAVE files count = 0 for ext in ("HAYDR_SAVE", "HAYDC_SAVE"): ofile = self.outdir.has_abiext(ext) if ofile: count += 1 irdvars.update(irdvars_for_ext(ext)) self.out_to_in(ofile) if not count: raise self.RestartError("%s: Cannot find the HAYDR_SAVE file to restart from." % self) # Add the appropriate variable for restarting. self.set_vars(irdvars) # Now we can resubmit the job. #self.history.info("Will restart from %s", restart_file) return self._restart() #def inspect(self, **kwargs): # """ # Plot the Haydock iterations with matplotlib. # # Returns # `matplotlib` figure, None if some error occurred. # """ # haydock_cycle = abiinspect.HaydockIterations.from_file(self.output_file.path) # if haydock_cycle is not None: # if "title" not in kwargs: kwargs["title"] = str(self) # return haydock_cycle.plot(**kwargs) @property def mdf_path(self): """Absolute path of the MDF file. Empty string if file is not present.""" # Lazy property to avoid multiple calls to has_abiext. try: return self._mdf_path except AttributeError: path = self.outdir.has_abiext("MDF.nc") if path: self._mdf_path = path return path def open_mdf(self): """ Open the MDF file located in the in self.outdir. Returns :class:`MdfFile` object, None if file could not be found or file is not readable. """ mdf_path = self.mdf_path if not mdf_path: logger.critical("%s didn't produce a MDF file in %s" % (self, self.outdir)) return None # Open the DFF file and add its data to results.out from abipy.electrons.bse import MdfFile try: return MdfFile(mdf_path) except Exception as exc: logger.critical("Exception while reading MDF file at %s:\n%s" % (mdf_path, str(exc))) return None def get_results(self, **kwargs): results = super().get_results(**kwargs) with self.open_mdf() as mdf: #results["out"].update(mdf.as_dict()) #epsilon_infinity optical_gap results.register_gridfs_files(MDF=mdf.filepath) return results class OpticTask(Task): """ Task for the computation of optical spectra with optic i.e. RPA without local-field effects and velocity operator computed from DDK files. """ color_rgb = np.array((255, 204, 102)) / 255 def __init__(self, optic_input, nscf_node, ddk_nodes, use_ddknc=False, workdir=None, manager=None): """ Create an instance of :class:`OpticTask` from an string containing the input. Args: optic_input: :class:`OpticInput` object with optic variables. nscf_node: The task that will produce the WFK file with the KS energies or path to the WFK file. ddk_nodes: List of :class:`DdkTask` nodes that will produce the DDK files or list of DDK filepaths. Order (x, y, z) workdir: Path to the working directory. manager: :class:`TaskManager` object. """ # Convert paths to FileNodes self.nscf_node = Node.as_node(nscf_node) self.ddk_nodes = [Node.as_node(n) for n in ddk_nodes] assert len(ddk_nodes) == 3 #print(self.nscf_node, self.ddk_nodes) # Use DDK extension instead of 1WF if use_ddknc: deps = {n: "DDK.nc" for n in self.ddk_nodes} else: deps = {n: "1WF" for n in self.ddk_nodes} deps.update({self.nscf_node: "WFK"}) super().__init__(optic_input, workdir=workdir, manager=manager, deps=deps) def set_workdir(self, workdir, chroot=False): """Set the working directory of the task.""" super().set_workdir(workdir, chroot=chroot) # Small hack: the log file of optics is actually the main output file. self.output_file = self.log_file def set_vars(self, *args, **kwargs): """ Optic does not use `get` or `ird` variables hence we should never try to change the input when we connect this task """ kwargs.update(dict(*args)) self.history.info("OpticTask intercepted set_vars with args %s" % kwargs) if "autoparal" in kwargs: self.input.set_vars(autoparal=kwargs["autoparal"]) if "max_ncpus" in kwargs: self.input.set_vars(max_ncpus=kwargs["max_ncpus"]) @property def executable(self): """Path to the executable required for running the :class:`OpticTask`.""" try: return self._executable except AttributeError: return "optic" @property def filesfile_string(self): """String with the list of files and prefixes needed to execute ABINIT.""" lines = [] app = lines.append #optic.in ! Name of input file #optic.out ! Unused #optic ! Root name for all files that will be produced app(self.input_file.path) # Path to the input file app(os.path.join(self.workdir, "unused")) # Path to the output file app(os.path.join(self.workdir, self.prefix.odata)) # Prefix for output data return "\n".join(lines) @property def wfk_filepath(self): """Returns (at runtime) the absolute path of the WFK file produced by the NSCF run.""" return self.nscf_node.outdir.has_abiext("WFK") @property def ddk_filepaths(self): """Returns (at runtime) the absolute path of the DDK files produced by the DDK runs.""" # This to support new version of optic that used DDK.nc paths = [ddk_task.outdir.has_abiext("DDK.nc") for ddk_task in self.ddk_nodes] if all(p for p in paths): return paths # This is deprecated and can be removed when new version of Abinit is released. return [ddk_task.outdir.has_abiext("1WF") for ddk_task in self.ddk_nodes] def make_input(self): """Construct and write the input file of the calculation.""" # Set the file paths. all_files ={"ddkfile_" + str(n + 1): ddk for n, ddk in enumerate(self.ddk_filepaths)} all_files.update({"wfkfile": self.wfk_filepath}) files_nml = {"FILES": all_files} files= nmltostring(files_nml) # Get the input specified by the user user_file = nmltostring(self.input.as_dict()) # Join them. return files + user_file def setup(self): """Public method called before submitting the task.""" def make_links(self): """ Optic allows the user to specify the paths of the input file. hence we don't need to create symbolic links. """ def get_results(self, **kwargs): return super().get_results(**kwargs) def fix_abicritical(self): """ Cannot fix abicritical errors for optic """ return 0 #@check_spectator def reset_from_scratch(self): """ restart from scratch, this is to be used if a job is restarted with more resources after a crash """ # Move output files produced in workdir to _reset otherwise check_status continues # to see the task as crashed even if the job did not run # Create reset directory if not already done. reset_dir = os.path.join(self.workdir, "_reset") reset_file = os.path.join(reset_dir, "_counter") if not os.path.exists(reset_dir): os.mkdir(reset_dir) num_reset = 1 else: with open(reset_file, "rt") as fh: num_reset = 1 + int(fh.read()) # Move files to reset and append digit with reset index. def move_file(f): if not f.exists: return try: f.move(os.path.join(reset_dir, f.basename + "_" + str(num_reset))) except OSError as exc: logger.warning("Couldn't move file {}. exc: {}".format(f, str(exc))) for fname in ("output_file", "log_file", "stderr_file", "qout_file", "qerr_file", "mpiabort_file"): move_file(getattr(self, fname)) with open(reset_file, "wt") as fh: fh.write(str(num_reset)) self.start_lockfile.remove() # Reset datetimes self.datetimes.reset() return self._restart(submit=False) def fix_queue_critical(self): """ This function tries to fix critical events originating from the queue submission system. General strategy, first try to increase resources in order to fix the problem, if this is not possible, call a task specific method to attempt to decrease the demands. Returns: 1 if task has been fixed else 0. """ from pymatgen.io.abinit.scheduler_error_parsers import NodeFailureError, MemoryCancelError, TimeCancelError #assert isinstance(self.manager, TaskManager) if not self.queue_errors: if self.mem_scales or self.load_scales: try: self.manager.increase_resources() # acts either on the policy or on the qadapter self.reset_from_scratch() return except ManagerIncreaseError: self.set_status(self.S_ERROR, msg='unknown queue error, could not increase resources any further') raise FixQueueCriticalError else: self.set_status(self.S_ERROR, msg='unknown queue error, no options left') raise FixQueueCriticalError else: for error in self.queue_errors: logger.info('fixing: %s' % str(error)) if isinstance(error, NodeFailureError): # if the problematic node is known, exclude it if error.nodes is not None: try: self.manager.exclude_nodes(error.nodes) self.reset_from_scratch() self.set_status(self.S_READY, msg='excluding nodes') except: raise FixQueueCriticalError else: self.set_status(self.S_ERROR, msg='Node error but no node identified.') raise FixQueueCriticalError elif isinstance(error, MemoryCancelError): # ask the qadapter to provide more resources, i.e. more cpu's so more total memory if the code # scales this should fix the memeory problem # increase both max and min ncpu of the autoparalel and rerun autoparalel if self.mem_scales: try: self.manager.increase_ncpus() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased ncps to solve memory problem') return except ManagerIncreaseError: logger.warning('increasing ncpus failed') # if the max is reached, try to increase the memory per cpu: try: self.manager.increase_mem() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased mem') return except ManagerIncreaseError: logger.warning('increasing mem failed') # if this failed ask the task to provide a method to reduce the memory demand try: self.reduce_memory_demand() self.reset_from_scratch() self.set_status(self.S_READY, msg='decreased mem demand') return except DecreaseDemandsError: logger.warning('decreasing demands failed') msg = ('Memory error detected but the memory could not be increased neither could the\n' 'memory demand be decreased. Unrecoverable error.') self.set_status(self.S_ERROR, msg) raise FixQueueCriticalError elif isinstance(error, TimeCancelError): # ask the qadapter to provide more time try: self.manager.increase_time() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased wall time') return except ManagerIncreaseError: logger.warning('increasing the walltime failed') # if this fails ask the qadapter to increase the number of cpus if self.load_scales: try: self.manager.increase_ncpus() self.reset_from_scratch() self.set_status(self.S_READY, msg='increased number of cpus') return except ManagerIncreaseError: logger.warning('increase ncpus to speed up the calculation to stay in the walltime failed') # if this failed ask the task to provide a method to speed up the task try: self.speed_up() self.reset_from_scratch() self.set_status(self.S_READY, msg='task speedup') return except DecreaseDemandsError: logger.warning('decreasing demands failed') msg = ('Time cancel error detected but the time could not be increased neither could\n' 'the time demand be decreased by speedup of increasing the number of cpus.\n' 'Unrecoverable error.') self.set_status(self.S_ERROR, msg) else: msg = 'No solution provided for error %s. Unrecoverable error.' % error.name self.set_status(self.S_ERROR, msg) return 0 def autoparal_run(self): """ Find an optimal set of parameters for the execution of the Optic task This method can change the submission parameters e.g. the number of CPUs for MPI and OpenMp. Returns 0 if success """ policy = self.manager.policy if policy.autoparal == 0: # or policy.max_ncpus in [None, 1]: logger.info("Nothing to do in autoparal, returning (None, None)") return 0 if policy.autoparal != 1: raise NotImplementedError("autoparal != 1") ############################################################################ # Run ABINIT in sequential to get the possible configurations with max_ncpus ############################################################################ # Set the variables for automatic parallelization # Will get all the possible configurations up to max_ncpus # Return immediately if max_ncpus == 1 max_ncpus = self.manager.max_cores if max_ncpus == 1: return 0 autoparal_vars = dict(autoparal=policy.autoparal, max_ncpus=max_ncpus) self.set_vars(autoparal_vars) # Run the job in a shell subprocess with mpi_procs = 1 # we don't want to make a request to the queue manager for this simple job! # Return code is always != 0 process = self.manager.to_shell_manager(mpi_procs=1).launch(self) self.history.pop() retcode = process.wait() # To avoid: ResourceWarning: unclosed file <_io.BufferedReader name=87> in py3k process.stderr.close() #process.stdout.close() # Remove the variables added for the automatic parallelization self.input.remove_vars(list(autoparal_vars.keys())) ############################################################## # Parse the autoparal configurations from the main output file ############################################################## parser = ParalHintsParser() try: pconfs = parser.parse(self.output_file.path) except parser.Error: # In principle Abinit should have written a complete log file # because we called .wait() but sometimes the Yaml doc is incomplete and # the parser raises. Let's wait 5 secs and then try again. time.sleep(5) try: pconfs = parser.parse(self.output_file.path) except parser.Error: logger.critical("Error while parsing Autoparal section:\n%s" % straceback()) return 2 ###################################################### # Select the optimal configuration according to policy ###################################################### #optconf = self.find_optconf(pconfs) # Select the partition on which we'll be running and set MPI/OMP cores. optconf = self.manager.select_qadapter(pconfs) #################################################### # Change the input file and/or the submission script #################################################### self.set_vars(optconf.vars) # Write autoparal configurations to JSON file. d = pconfs.as_dict() d["optimal_conf"] = optconf json_pretty_dump(d, os.path.join(self.workdir, "autoparal.json")) ############## # Finalization ############## # Reset the status, remove garbage files ... self.set_status(self.S_INIT, msg='finished auto paralell') # Remove the output file since Abinit likes to create new files # with extension .outA, .outB if the file already exists. os.remove(self.output_file.path) #os.remove(self.log_file.path) os.remove(self.stderr_file.path) return 0 class AnaddbTask(Task): """Task for Anaddb runs (post-processing of DFPT calculations).""" color_rgb = np.array((204, 102, 255)) / 255 def __init__(self, anaddb_input, ddb_node, gkk_node=None, md_node=None, ddk_node=None, workdir=None, manager=None): """ Create an instance of :class:`AnaddbTask` from a string containing the input. Args: anaddb_input: string with the anaddb variables. ddb_node: The node that will produce the DDB file. Accept :class:`Task`, :class:`Work` or filepath. gkk_node: The node that will produce the GKK file (optional). Accept :class:`Task`, :class:`Work` or filepath. md_node: The node that will produce the MD file (optional). Accept `Task`, `Work` or filepath. gkk_node: The node that will produce the GKK file (optional). Accept `Task`, `Work` or filepath. workdir: Path to the working directory (optional). manager: :class:`TaskManager` object (optional). """ # Keep a reference to the nodes. self.ddb_node = Node.as_node(ddb_node) deps = {self.ddb_node: "DDB"} self.gkk_node = Node.as_node(gkk_node) if self.gkk_node is not None: deps.update({self.gkk_node: "GKK"}) # I never used it! self.md_node = Node.as_node(md_node) if self.md_node is not None: deps.update({self.md_node: "MD"}) self.ddk_node = Node.as_node(ddk_node) if self.ddk_node is not None: deps.update({self.ddk_node: "DDK"}) super().__init__(input=anaddb_input, workdir=workdir, manager=manager, deps=deps) @classmethod def temp_shell_task(cls, inp, ddb_node, mpi_procs=1, gkk_node=None, md_node=None, ddk_node=None, workdir=None, manager=None): """ Build a :class:`AnaddbTask` with a temporary workdir. The task is executed via the shell with 1 MPI proc. Mainly used for post-processing the DDB files. Args: mpi_procs: Number of MPI processes to use. anaddb_input: string with the anaddb variables. ddb_node: The node that will produce the DDB file. Accept :class:`Task`, :class:`Work` or filepath. See `AnaddbInit` for the meaning of the other arguments. """ # Build a simple manager to run the job in a shell subprocess import tempfile workdir = tempfile.mkdtemp() if workdir is None else workdir if manager is None: manager = TaskManager.from_user_config() # Construct the task and run it return cls(inp, ddb_node, gkk_node=gkk_node, md_node=md_node, ddk_node=ddk_node, workdir=workdir, manager=manager.to_shell_manager(mpi_procs=mpi_procs)) @property def executable(self): """Path to the executable required for running the :class:`AnaddbTask`.""" try: return self._executable except AttributeError: return "anaddb" @property def filesfile_string(self): """String with the list of files and prefixes needed to execute ABINIT.""" lines = [] app = lines.append app(self.input_file.path) # 1) Path of the input file app(self.output_file.path) # 2) Path of the output file app(self.ddb_filepath) # 3) Input derivative database e.g. t13.ddb.in app(self.md_filepath) # 4) Output molecular dynamics e.g. t13.md app(self.gkk_filepath) # 5) Input elphon matrix elements (GKK file) app(self.outdir.path_join("out")) # 6) Base name for elphon output files e.g. t13 app(self.ddk_filepath) # 7) File containing ddk filenames for elphon/transport. return "\n".join(lines) @property def ddb_filepath(self): """Returns (at runtime) the absolute path of the input DDB file.""" # This is not very elegant! A possible approach could to be path self.ddb_node.outdir! if isinstance(self.ddb_node, FileNode): return self.ddb_node.filepath path = self.ddb_node.outdir.has_abiext("DDB") return path if path else "DDB_FILE_DOES_NOT_EXIST" @property def md_filepath(self): """Returns (at runtime) the absolute path of the input MD file.""" if self.md_node is None: return "MD_FILE_DOES_NOT_EXIST" if isinstance(self.md_node, FileNode): return self.md_node.filepath path = self.md_node.outdir.has_abiext("MD") return path if path else "MD_FILE_DOES_NOT_EXIST" @property def gkk_filepath(self): """Returns (at runtime) the absolute path of the input GKK file.""" if self.gkk_node is None: return "GKK_FILE_DOES_NOT_EXIST" if isinstance(self.gkk_node, FileNode): return self.gkk_node.filepath path = self.gkk_node.outdir.has_abiext("GKK") return path if path else "GKK_FILE_DOES_NOT_EXIST" @property def ddk_filepath(self): """Returns (at runtime) the absolute path of the input DKK file.""" if self.ddk_node is None: return "DDK_FILE_DOES_NOT_EXIST" if isinstance(self.ddk_node, FileNode): return self.ddk_node.filepath path = self.ddk_node.outdir.has_abiext("DDK") return path if path else "DDK_FILE_DOES_NOT_EXIST" def setup(self): """Public method called before submitting the task.""" def make_links(self): """ Anaddb allows the user to specify the paths of the input file. hence we don't need to create symbolic links. """ def open_phbst(self): """Open PHBST file produced by Anaddb and returns :class:`PhbstFile` object.""" from abipy.dfpt.phonons import PhbstFile phbst_path = os.path.join(self.workdir, "run.abo_PHBST.nc") if not phbst_path: if self.status == self.S_OK: logger.critical("%s reached S_OK but didn't produce a PHBST file in %s" % (self, self.outdir)) return None try: return PhbstFile(phbst_path) except Exception as exc: logger.critical("Exception while reading GSR file at %s:\n%s" % (phbst_path, str(exc))) return None def open_phdos(self): """Open PHDOS file produced by Anaddb and returns :class:`PhdosFile` object.""" from abipy.dfpt.phonons import PhdosFile phdos_path = os.path.join(self.workdir, "run.abo_PHDOS.nc") if not phdos_path: if self.status == self.S_OK: logger.critical("%s reached S_OK but didn't produce a PHBST file in %s" % (self, self.outdir)) return None try: return PhdosFile(phdos_path) except Exception as exc: logger.critical("Exception while reading GSR file at %s:\n%s" % (phdos_path, str(exc))) return None def get_results(self, **kwargs): results = super().get_results(**kwargs) return results
mit
desihub/fiberassign
py/fiberassign/test/test_qa.py
1
11199
""" Test fiberassign target operations. """ import os import subprocess import re import shutil import unittest from datetime import datetime import json import glob import numpy as np import fitsio import desimodel import fiberassign from fiberassign.utils import option_list, GlobalTimers from fiberassign.hardware import load_hardware from fiberassign.tiles import load_tiles, Tiles from fiberassign.targets import (TARGET_TYPE_SCIENCE, TARGET_TYPE_SKY, TARGET_TYPE_SUPPSKY, TARGET_TYPE_STANDARD, TARGET_TYPE_SAFE, Targets, TargetsAvailable, LocationsAvailable, load_target_file, targets_in_tiles, create_tagalong) from fiberassign.assign import (Assignment, write_assignment_fits, write_assignment_ascii, merge_results) from fiberassign.qa import qa_tiles, qa_targets from fiberassign.vis import plot_tiles, plot_qa, set_matplotlib_pdf_backend from fiberassign.scripts.assign import parse_assign, run_assign_full from fiberassign.scripts.plot import parse_plot, run_plot from fiberassign.scripts.qa import parse_qa, run_qa from fiberassign.scripts.qa_plot import parse_plot_qa, run_plot_qa from .simulate import (test_subdir_create, sim_tiles, sim_targets, sim_focalplane, petal_rotation, test_assign_date) class TestQA(unittest.TestCase): @classmethod def setUpClass(cls): # Find the location of scripts. First try the case where we are running # tests from the top level of the source tree. cls.topDir = os.path.dirname( # top-level os.path.dirname( # build/ os.path.dirname( # lib.arch/ os.path.dirname( # fiberassign/ os.path.dirname(os.path.abspath(__file__)) # test/ ) ) ) ) cls.binDir = os.path.join(cls.topDir, "bin") if not os.path.isdir(cls.binDir): # We are running from some other directory from an installed package cls.topDir = os.path.dirname( # top-level os.path.dirname( # lib/ os.path.dirname( # python3.x/ os.path.dirname( # site-packages/ os.path.dirname( # egg/ os.path.dirname( # fiberassign/ os.path.dirname(os.path.abspath(__file__)) # test/ ) ) ) ) ) ) cls.binDir = os.path.join(cls.topDir, "bin") def setUp(self): self.density_science = 5000 self.density_standards = 5000 self.density_sky = 10 self.density_suppsky = 5000 pass def tearDown(self): pass def test_science(self): set_matplotlib_pdf_backend() import matplotlib.pyplot as plt test_dir = test_subdir_create("qa_test_science") log_file = os.path.join(test_dir, "log.txt") np.random.seed(123456789) input_mtl = os.path.join(test_dir, "mtl.fits") # For this test, we will use just 2 science target classes, in order to verify # we get approximately the correct distribution sdist = [ (3000, 1, 0.25, "QSO"), (2000, 1, 0.75, "ELG") ] nscience = sim_targets( input_mtl, TARGET_TYPE_SCIENCE, 0, density=self.density_science, science_frac=sdist ) log_msg = "Simulated {} science targets\n".format(nscience) tgs = Targets() tagalong = create_tagalong(plate_radec=False) load_target_file(tgs, tagalong, input_mtl) # Read hardware properties fp, exclude, state = sim_focalplane(rundate=test_assign_date) hw = load_hardware(focalplane=(fp, exclude, state)) tfile = os.path.join(test_dir, "footprint.fits") sim_tiles(tfile) tiles = load_tiles(tiles_file=tfile) # Precompute target positions tile_targetids, tile_x, tile_y = targets_in_tiles(hw, tgs, tiles, tagalong) # Compute the targets available to each fiber for each tile. tgsavail = TargetsAvailable(hw, tiles, tile_targetids, tile_x, tile_y) # Compute the fibers on all tiles available for each target favail = LocationsAvailable(tgsavail) # Pass empty map of STUCK positioners that land on good sky stucksky = {} # Create assignment object asgn = Assignment(tgs, tgsavail, favail, stucksky) # First-pass assignment of science targets asgn.assign_unused(TARGET_TYPE_SCIENCE) # Redistribute asgn.redistribute_science() write_assignment_fits(tiles, tagalong, asgn, out_dir=test_dir, all_targets=True) tile_ids = list(tiles.id) merge_results( [input_mtl], list(), tile_ids, result_dir=test_dir, copy_fba=False ) # FIXME: In order to use the qa_targets function, we need to know the # starting requested number of observations (NUMOBS_INIT). Then we can use # that value for each target and compare to the number actually assigned. # However, the NUMOBS_INIT column was removed from the merged TARGET table. # If we are ever able to reach consensus on restoring that column, then we # can re-enable these tests below. # # qa_targets( # hw, # tiles, # result_dir=test_dir, # result_prefix="fiberassign-" # ) # # # Load the target catalog so that we have access to the target properties # # fd = fitsio.FITS(input_mtl, "r") # scidata = np.array(np.sort(fd[1].read(), order="TARGETID")) # fd.close() # del fd # # # How many possible positioner assignments did we have? # nassign = 5000 * len(tile_ids) # # possible = dict() # achieved = dict() # # namepat = re.compile(r".*/qa_target_count_(.*)_init-(.*)\.fits") # for qafile in glob.glob("{}/qa_target_count_*.fits".format(test_dir)): # namemat = namepat.match(qafile) # name = namemat.group(1) # obs = int(namemat.group(2)) # if obs == 0: # continue # fd = fitsio.FITS(qafile, "r") # fdata = fd["COUNTS"].read() # # Sort by target ID so we can select easily # fdata = np.sort(fdata, order="TARGETID") # tgid = np.array(fdata["TARGETID"]) # counts = np.array(fdata["NUMOBS_DONE"]) # avail = np.array(fdata["NUMOBS_AVAIL"]) # del fdata # fd.close() # # # Select target properties. BOTH TARGET LISTS MUST BE SORTED. # rows = np.where(np.isin(scidata["TARGETID"], tgid, assume_unique=True))[0] # # ra = np.array(scidata["RA"][rows]) # dec = np.array(scidata["DEC"][rows]) # dtarget = np.array(scidata["DESI_TARGET"][rows]) # init = np.array(scidata["NUMOBS_INIT"][rows]) # # requested = obs * np.ones_like(avail) # # under = np.where(avail < requested)[0] # over = np.where(avail > requested)[0] # # limavail = np.array(avail) # limavail[over] = obs # # deficit = np.zeros(len(limavail), dtype=np.int) # # deficit[:] = limavail - counts # deficit[avail == 0] = 0 # # possible[name] = np.sum(limavail) # achieved[name] = np.sum(counts) # # log_msg += "{}-{}:\n".format(name, obs) # # pindx = np.where(deficit > 0)[0] # poor_tgid = tgid[pindx] # poor_dtarget = dtarget[pindx] # log_msg += " Deficit > 0: {}\n".format(len(poor_tgid)) # poor_ra = ra[pindx] # poor_dec = dec[pindx] # poor_deficit = deficit[pindx] # # # Plot Target availability # # Commented out by default, since in the case of high target density # # needed for maximizing assignments, there are far more targets than # # the number of available fiber placements. # # # marksize = 4 * np.ones_like(deficit) # # # # fig = plt.figure(figsize=(12, 12)) # # ax = fig.add_subplot(1, 1, 1) # # ax.scatter(ra, dec, s=2, c="black", marker="o") # # for pt, pr, pd, pdef in zip(poor_tgid, poor_ra, poor_dec, poor_deficit): # # ploc = plt.Circle( # # (pr, pd), radius=(0.05*pdef), fc="none", ec="red" # # ) # # ax.add_artist(ploc) # # ax.set_xlabel("RA", fontsize="large") # # ax.set_ylabel("DEC", fontsize="large") # # ax.set_title( # # "Target \"{}\": (min(avail, requested) - counts) > 0".format( # # name, obs # # ) # # ) # # #ax.legend(handles=lg, framealpha=1.0, loc="upper right") # # plt.savefig(os.path.join(test_dir, "{}-{}_deficit.pdf".format(name, obs)), dpi=300, format="pdf") # # log_msg += \ # "Assigned {} tiles for total of {} possible target observations\n".format( # len(tile_ids), nassign # ) # ach = 0 # for nm in possible.keys(): # ach += achieved[nm] # log_msg += \ # " type {} had {} possible target obs and achieved {}\n".format( # nm, possible[nm], achieved[nm] # ) # frac = 100.0 * ach / nassign # log_msg += \ # " {} / {} = {:0.2f}% of fibers were assigned\n".format( # ach, nassign, frac # ) # for nm in possible.keys(): # log_msg += \ # " type {} had {:0.2f}% of achieved observations\n".format( # nm, achieved[nm] / ach # ) # with open(log_file, "w") as f: # f.write(log_msg) # # self.assertGreaterEqual(frac, 99.0) # Test if qa-fiberassign script runs without crashing script = os.path.join(self.binDir, "qa-fiberassign") if os.path.exists(script): fafiles = glob.glob(f"{test_dir}/fiberassign-*.fits") cmd = "{} --targets {}".format(script, " ".join(fafiles)) err = subprocess.call(cmd.split()) self.assertEqual(err, 0, f"FAILED ({err}): {cmd}") else: print(f"ERROR: didn't find {script}") def test_suite(): """Allows testing of only this module with the command:: python setup.py test -m <modulename> """ return unittest.defaultTestLoader.loadTestsFromName(__name__)
bsd-3-clause
mequanta/z-runner
examples/quanto/ta_lib_example.py
1
1960
# This example algorithm uses the Relative Strength Index indicator as a buy/sell signal. # When the RSI is over 70, a stock can be seen as overbought and it's time to sell. # When the RSI is below 30, a stock can be seen as oversold and it's time to buy. # Because this algorithm uses the history function, it will only run in minute mode. # We will constrain the trading to once per day at market open in this example. import talib import numpy as np import math from zipline.api import history # Setup our variables def initialize(context): context.max_notional = 100000 context.intc = symbol('INTC') # Intel context.LOW_RSI = 30 context.HIGH_RSI = 70 def handle_data(context, data): #Get a trailing window of data prices = history(15, '1d', 'price') # Use pandas dataframe.apply to get the last RSI value # for for each stock in our basket rsi_data = prices.apply(talib.RSI, timeperiod=14).iloc[-1] intc_rsi = rsi_data[context.intc] # check how many shares of Intel we currently own current_intel_shares = context.portfolio.positions[context.intc].amount # until 14 time periods have gone by, the rsi value will be numpy.nan # RSI is above 70 and we own GOOG, time to close the position. if intc_rsi > context.HIGH_RSI and current_intel_shares > 0: order_target(context.intc, 0) log.info('RSI is at ' + str(intc_rsi) + ', selling ' + str(current_intel_shares) + ' shares') # RSI is below 30 and we don't have any Intel stock, time to buy. elif intc_rsi < context.LOW_RSI and current_intel_shares == 0: num_shares = math.floor(context.max_notional / data[context.intc].close_price) order(context.intc, num_shares) log.info('RSI is at ' + str(intc_rsi) + ', buying ' + str(num_shares) + ' shares') # record the current RSI value and the current price of INTC. record(intcRSI=intc_rsi, intcPRICE=data[context.intc].close_price)
agpl-3.0
poryfly/scikit-learn
sklearn/tests/test_naive_bayes.py
142
17496
import pickle from io import BytesIO import numpy as np import scipy.sparse from sklearn.datasets import load_digits, load_iris from sklearn.cross_validation import cross_val_score, train_test_split from sklearn.externals.six.moves import zip 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_raises from sklearn.utils.testing import assert_greater from sklearn.naive_bayes import GaussianNB, BernoulliNB, MultinomialNB # Data is just 6 separable points in the plane X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) y = np.array([1, 1, 1, 2, 2, 2]) # A bit more random tests rng = np.random.RandomState(0) X1 = rng.normal(size=(10, 3)) y1 = (rng.normal(size=(10)) > 0).astype(np.int) # Data is 6 random integer points in a 100 dimensional space classified to # three classes. X2 = rng.randint(5, size=(6, 100)) y2 = np.array([1, 1, 2, 2, 3, 3]) def test_gnb(): # Gaussian Naive Bayes classification. # This checks that GaussianNB implements fit and predict and returns # correct values for a simple toy dataset. clf = GaussianNB() y_pred = clf.fit(X, y).predict(X) assert_array_equal(y_pred, y) y_pred_proba = clf.predict_proba(X) y_pred_log_proba = clf.predict_log_proba(X) assert_array_almost_equal(np.log(y_pred_proba), y_pred_log_proba, 8) # Test whether label mismatch between target y and classes raises # an Error # FIXME Remove this test once the more general partial_fit tests are merged assert_raises(ValueError, GaussianNB().partial_fit, X, y, classes=[0, 1]) def test_gnb_prior(): # Test whether class priors are properly set. clf = GaussianNB().fit(X, y) assert_array_almost_equal(np.array([3, 3]) / 6.0, clf.class_prior_, 8) clf.fit(X1, y1) # Check that the class priors sum to 1 assert_array_almost_equal(clf.class_prior_.sum(), 1) def test_gnb_sample_weight(): """Test whether sample weights are properly used in GNB. """ # Sample weights all being 1 should not change results sw = np.ones(6) clf = GaussianNB().fit(X, y) clf_sw = GaussianNB().fit(X, y, sw) assert_array_almost_equal(clf.theta_, clf_sw.theta_) assert_array_almost_equal(clf.sigma_, clf_sw.sigma_) # Fitting twice with half sample-weights should result # in same result as fitting once with full weights sw = rng.rand(y.shape[0]) clf1 = GaussianNB().fit(X, y, sample_weight=sw) clf2 = GaussianNB().partial_fit(X, y, classes=[1, 2], sample_weight=sw / 2) clf2.partial_fit(X, y, sample_weight=sw / 2) assert_array_almost_equal(clf1.theta_, clf2.theta_) assert_array_almost_equal(clf1.sigma_, clf2.sigma_) # Check that duplicate entries and correspondingly increased sample # weights yield the same result ind = rng.randint(0, X.shape[0], 20) sample_weight = np.bincount(ind, minlength=X.shape[0]) clf_dupl = GaussianNB().fit(X[ind], y[ind]) clf_sw = GaussianNB().fit(X, y, sample_weight) assert_array_almost_equal(clf_dupl.theta_, clf_sw.theta_) assert_array_almost_equal(clf_dupl.sigma_, clf_sw.sigma_) def test_discrete_prior(): # Test whether class priors are properly set. for cls in [BernoulliNB, MultinomialNB]: clf = cls().fit(X2, y2) assert_array_almost_equal(np.log(np.array([2, 2, 2]) / 6.0), clf.class_log_prior_, 8) def test_mnnb(): # Test Multinomial Naive Bayes classification. # This checks that MultinomialNB implements fit and predict and returns # correct values for a simple toy dataset. for X in [X2, scipy.sparse.csr_matrix(X2)]: # Check the ability to predict the learning set. clf = MultinomialNB() assert_raises(ValueError, clf.fit, -X, y2) y_pred = clf.fit(X, y2).predict(X) assert_array_equal(y_pred, y2) # Verify that np.log(clf.predict_proba(X)) gives the same results as # clf.predict_log_proba(X) y_pred_proba = clf.predict_proba(X) y_pred_log_proba = clf.predict_log_proba(X) assert_array_almost_equal(np.log(y_pred_proba), y_pred_log_proba, 8) # Check that incremental fitting yields the same results clf2 = MultinomialNB() clf2.partial_fit(X[:2], y2[:2], classes=np.unique(y2)) clf2.partial_fit(X[2:5], y2[2:5]) clf2.partial_fit(X[5:], y2[5:]) y_pred2 = clf2.predict(X) assert_array_equal(y_pred2, y2) y_pred_proba2 = clf2.predict_proba(X) y_pred_log_proba2 = clf2.predict_log_proba(X) assert_array_almost_equal(np.log(y_pred_proba2), y_pred_log_proba2, 8) assert_array_almost_equal(y_pred_proba2, y_pred_proba) assert_array_almost_equal(y_pred_log_proba2, y_pred_log_proba) # Partial fit on the whole data at once should be the same as fit too clf3 = MultinomialNB() clf3.partial_fit(X, y2, classes=np.unique(y2)) y_pred3 = clf3.predict(X) assert_array_equal(y_pred3, y2) y_pred_proba3 = clf3.predict_proba(X) y_pred_log_proba3 = clf3.predict_log_proba(X) assert_array_almost_equal(np.log(y_pred_proba3), y_pred_log_proba3, 8) assert_array_almost_equal(y_pred_proba3, y_pred_proba) assert_array_almost_equal(y_pred_log_proba3, y_pred_log_proba) def check_partial_fit(cls): clf1 = cls() clf1.fit([[0, 1], [1, 0]], [0, 1]) clf2 = cls() clf2.partial_fit([[0, 1], [1, 0]], [0, 1], classes=[0, 1]) assert_array_equal(clf1.class_count_, clf2.class_count_) assert_array_equal(clf1.feature_count_, clf2.feature_count_) clf3 = cls() clf3.partial_fit([[0, 1]], [0], classes=[0, 1]) clf3.partial_fit([[1, 0]], [1]) assert_array_equal(clf1.class_count_, clf3.class_count_) assert_array_equal(clf1.feature_count_, clf3.feature_count_) def test_discretenb_partial_fit(): for cls in [MultinomialNB, BernoulliNB]: yield check_partial_fit, cls def test_gnb_partial_fit(): clf = GaussianNB().fit(X, y) clf_pf = GaussianNB().partial_fit(X, y, np.unique(y)) assert_array_almost_equal(clf.theta_, clf_pf.theta_) assert_array_almost_equal(clf.sigma_, clf_pf.sigma_) assert_array_almost_equal(clf.class_prior_, clf_pf.class_prior_) clf_pf2 = GaussianNB().partial_fit(X[0::2, :], y[0::2], np.unique(y)) clf_pf2.partial_fit(X[1::2], y[1::2]) assert_array_almost_equal(clf.theta_, clf_pf2.theta_) assert_array_almost_equal(clf.sigma_, clf_pf2.sigma_) assert_array_almost_equal(clf.class_prior_, clf_pf2.class_prior_) def test_discretenb_pickle(): # Test picklability of discrete naive Bayes classifiers for cls in [BernoulliNB, MultinomialNB, GaussianNB]: clf = cls().fit(X2, y2) y_pred = clf.predict(X2) store = BytesIO() pickle.dump(clf, store) clf = pickle.load(BytesIO(store.getvalue())) assert_array_equal(y_pred, clf.predict(X2)) if cls is not GaussianNB: # TODO re-enable me when partial_fit is implemented for GaussianNB # Test pickling of estimator trained with partial_fit clf2 = cls().partial_fit(X2[:3], y2[:3], classes=np.unique(y2)) clf2.partial_fit(X2[3:], y2[3:]) store = BytesIO() pickle.dump(clf2, store) clf2 = pickle.load(BytesIO(store.getvalue())) assert_array_equal(y_pred, clf2.predict(X2)) def test_input_check_fit(): # Test input checks for the fit method for cls in [BernoulliNB, MultinomialNB, GaussianNB]: # check shape consistency for number of samples at fit time assert_raises(ValueError, cls().fit, X2, y2[:-1]) # check shape consistency for number of input features at predict time clf = cls().fit(X2, y2) assert_raises(ValueError, clf.predict, X2[:, :-1]) def test_input_check_partial_fit(): for cls in [BernoulliNB, MultinomialNB]: # check shape consistency assert_raises(ValueError, cls().partial_fit, X2, y2[:-1], classes=np.unique(y2)) # classes is required for first call to partial fit assert_raises(ValueError, cls().partial_fit, X2, y2) # check consistency of consecutive classes values clf = cls() clf.partial_fit(X2, y2, classes=np.unique(y2)) assert_raises(ValueError, clf.partial_fit, X2, y2, classes=np.arange(42)) # check consistency of input shape for partial_fit assert_raises(ValueError, clf.partial_fit, X2[:, :-1], y2) # check consistency of input shape for predict assert_raises(ValueError, clf.predict, X2[:, :-1]) def test_discretenb_predict_proba(): # Test discrete NB classes' probability scores # The 100s below distinguish Bernoulli from multinomial. # FIXME: write a test to show this. X_bernoulli = [[1, 100, 0], [0, 1, 0], [0, 100, 1]] X_multinomial = [[0, 1], [1, 3], [4, 0]] # test binary case (1-d output) y = [0, 0, 2] # 2 is regression test for binary case, 02e673 for cls, X in zip([BernoulliNB, MultinomialNB], [X_bernoulli, X_multinomial]): clf = cls().fit(X, y) assert_equal(clf.predict(X[-1]), 2) assert_equal(clf.predict_proba(X[0]).shape, (1, 2)) assert_array_almost_equal(clf.predict_proba(X[:2]).sum(axis=1), np.array([1., 1.]), 6) # test multiclass case (2-d output, must sum to one) y = [0, 1, 2] for cls, X in zip([BernoulliNB, MultinomialNB], [X_bernoulli, X_multinomial]): clf = cls().fit(X, y) assert_equal(clf.predict_proba(X[0]).shape, (1, 3)) assert_equal(clf.predict_proba(X[:2]).shape, (2, 3)) assert_almost_equal(np.sum(clf.predict_proba(X[1])), 1) assert_almost_equal(np.sum(clf.predict_proba(X[-1])), 1) assert_almost_equal(np.sum(np.exp(clf.class_log_prior_)), 1) assert_almost_equal(np.sum(np.exp(clf.intercept_)), 1) def test_discretenb_uniform_prior(): # Test whether discrete NB classes fit a uniform prior # when fit_prior=False and class_prior=None for cls in [BernoulliNB, MultinomialNB]: clf = cls() clf.set_params(fit_prior=False) clf.fit([[0], [0], [1]], [0, 0, 1]) prior = np.exp(clf.class_log_prior_) assert_array_equal(prior, np.array([.5, .5])) def test_discretenb_provide_prior(): # Test whether discrete NB classes use provided prior for cls in [BernoulliNB, MultinomialNB]: clf = cls(class_prior=[0.5, 0.5]) clf.fit([[0], [0], [1]], [0, 0, 1]) prior = np.exp(clf.class_log_prior_) assert_array_equal(prior, np.array([.5, .5])) # Inconsistent number of classes with prior assert_raises(ValueError, clf.fit, [[0], [1], [2]], [0, 1, 2]) assert_raises(ValueError, clf.partial_fit, [[0], [1]], [0, 1], classes=[0, 1, 1]) def test_discretenb_provide_prior_with_partial_fit(): # Test whether discrete NB classes use provided prior # when using partial_fit iris = load_iris() iris_data1, iris_data2, iris_target1, iris_target2 = train_test_split( iris.data, iris.target, test_size=0.4, random_state=415) for cls in [BernoulliNB, MultinomialNB]: for prior in [None, [0.3, 0.3, 0.4]]: clf_full = cls(class_prior=prior) clf_full.fit(iris.data, iris.target) clf_partial = cls(class_prior=prior) clf_partial.partial_fit(iris_data1, iris_target1, classes=[0, 1, 2]) clf_partial.partial_fit(iris_data2, iris_target2) assert_array_almost_equal(clf_full.class_log_prior_, clf_partial.class_log_prior_) def test_sample_weight_multiclass(): for cls in [BernoulliNB, MultinomialNB]: # check shape consistency for number of samples at fit time yield check_sample_weight_multiclass, cls def check_sample_weight_multiclass(cls): X = [ [0, 0, 1], [0, 1, 1], [0, 1, 1], [1, 0, 0], ] y = [0, 0, 1, 2] sample_weight = np.array([1, 1, 2, 2], dtype=np.float) sample_weight /= sample_weight.sum() clf = cls().fit(X, y, sample_weight=sample_weight) assert_array_equal(clf.predict(X), [0, 1, 1, 2]) # Check sample weight using the partial_fit method clf = cls() clf.partial_fit(X[:2], y[:2], classes=[0, 1, 2], sample_weight=sample_weight[:2]) clf.partial_fit(X[2:3], y[2:3], sample_weight=sample_weight[2:3]) clf.partial_fit(X[3:], y[3:], sample_weight=sample_weight[3:]) assert_array_equal(clf.predict(X), [0, 1, 1, 2]) def test_sample_weight_mnb(): clf = MultinomialNB() clf.fit([[1, 2], [1, 2], [1, 0]], [0, 0, 1], sample_weight=[1, 1, 4]) assert_array_equal(clf.predict([1, 0]), [1]) positive_prior = np.exp(clf.intercept_[0]) assert_array_almost_equal([1 - positive_prior, positive_prior], [1 / 3., 2 / 3.]) def test_coef_intercept_shape(): # coef_ and intercept_ should have shapes as in other linear models. # Non-regression test for issue #2127. X = [[1, 0, 0], [1, 1, 1]] y = [1, 2] # binary classification for clf in [MultinomialNB(), BernoulliNB()]: clf.fit(X, y) assert_equal(clf.coef_.shape, (1, 3)) assert_equal(clf.intercept_.shape, (1,)) def test_check_accuracy_on_digits(): # Non regression test to make sure that any further refactoring / optim # of the NB models do not harm the performance on a slightly non-linearly # separable dataset digits = load_digits() X, y = digits.data, digits.target binary_3v8 = np.logical_or(digits.target == 3, digits.target == 8) X_3v8, y_3v8 = X[binary_3v8], y[binary_3v8] # Multinomial NB scores = cross_val_score(MultinomialNB(alpha=10), X, y, cv=10) assert_greater(scores.mean(), 0.86) scores = cross_val_score(MultinomialNB(alpha=10), X_3v8, y_3v8, cv=10) assert_greater(scores.mean(), 0.94) # Bernoulli NB scores = cross_val_score(BernoulliNB(alpha=10), X > 4, y, cv=10) assert_greater(scores.mean(), 0.83) scores = cross_val_score(BernoulliNB(alpha=10), X_3v8 > 4, y_3v8, cv=10) assert_greater(scores.mean(), 0.92) # Gaussian NB scores = cross_val_score(GaussianNB(), X, y, cv=10) assert_greater(scores.mean(), 0.77) scores = cross_val_score(GaussianNB(), X_3v8, y_3v8, cv=10) assert_greater(scores.mean(), 0.86) def test_feature_log_prob_bnb(): # Test for issue #4268. # Tests that the feature log prob value computed by BernoulliNB when # alpha=1.0 is equal to the expression given in Manning, Raghavan, # and Schuetze's "Introduction to Information Retrieval" book: # http://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html X = np.array([[0, 0, 0], [1, 1, 0], [0, 1, 0], [1, 0, 1], [0, 1, 0]]) Y = np.array([0, 0, 1, 2, 2]) # Fit Bernoulli NB w/ alpha = 1.0 clf = BernoulliNB(alpha=1.0) clf.fit(X, Y) # Manually form the (log) numerator and denominator that # constitute P(feature presence | class) num = np.log(clf.feature_count_ + 1.0) denom = np.tile(np.log(clf.class_count_ + 2.0), (X.shape[1], 1)).T # Check manual estimate matches assert_array_equal(clf.feature_log_prob_, (num - denom)) def test_bnb(): # Tests that BernoulliNB when alpha=1.0 gives the same values as # those given for the toy example in Manning, Raghavan, and # Schuetze's "Introduction to Information Retrieval" book: # http://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html # Training data points are: # Chinese Beijing Chinese (class: China) # Chinese Chinese Shanghai (class: China) # Chinese Macao (class: China) # Tokyo Japan Chinese (class: Japan) # Features are Beijing, Chinese, Japan, Macao, Shanghai, and Tokyo X = np.array([[1, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0], [0, 1, 1, 0, 0, 1]]) # Classes are China (0), Japan (1) Y = np.array([0, 0, 0, 1]) # Fit BernoulliBN w/ alpha = 1.0 clf = BernoulliNB(alpha=1.0) clf.fit(X, Y) # Check the class prior is correct class_prior = np.array([0.75, 0.25]) assert_array_almost_equal(np.exp(clf.class_log_prior_), class_prior) # Check the feature probabilities are correct feature_prob = np.array([[0.4, 0.8, 0.2, 0.4, 0.4, 0.2], [1/3.0, 2/3.0, 2/3.0, 1/3.0, 1/3.0, 2/3.0]]) assert_array_almost_equal(np.exp(clf.feature_log_prob_), feature_prob) # Testing data point is: # Chinese Chinese Chinese Tokyo Japan X_test = np.array([0, 1, 1, 0, 0, 1]) # Check the predictive probabilities are correct unnorm_predict_proba = np.array([[0.005183999999999999, 0.02194787379972565]]) predict_proba = unnorm_predict_proba / np.sum(unnorm_predict_proba) assert_array_almost_equal(clf.predict_proba(X_test), predict_proba)
bsd-3-clause
rwl/PYPOWER-Dynamics
examples/02_SMIB_AVR_Step/test_SMIB.py
2
2615
#!python3 # # Copyright (C) 2014-2015 Julius Susanto. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. """ PYPOWER-Dynamics Single Machine Infinite Bus (SMIB) Test """ # Dynamic model classes from pydyn.controller import controller from pydyn.sym_order6a import sym_order6a from pydyn.sym_order4 import sym_order4 from pydyn.ext_grid import ext_grid # Simulation modules from pydyn.events import events from pydyn.recorder import recorder from pydyn.run_sim import run_sim # External modules from pypower.loadcase import loadcase import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': ######### # SETUP # ######### print('----------------------------') print('PYPOWER-Dynamics - SMIB Test') print('----------------------------') # Load PYPOWER case ppc = loadcase('smib_case.py') # Program options dynopt = {} dynopt['h'] = 0.01 # step length (s) dynopt['t_sim'] = 15 # simulation time (s) dynopt['max_err'] = 0.0001 # Maximum error in network iteration (voltage mismatches) dynopt['max_iter'] = 25 # Maximum number of network iterations dynopt['verbose'] = False # option for verbose messages dynopt['fn'] = 50 # Nominal system frequency (Hz) # Integrator option #dynopt['iopt'] = 'mod_euler' dynopt['iopt'] = 'runge_kutta' # Create dynamic model objects oCtrl = controller('smib.dyn', dynopt) oMach = sym_order6a('smib_round.mach', dynopt) #oMach = sym_order4('smib_round.mach', iopt) oGrid = ext_grid('GRID1', 0, 0.1, 99999, dynopt) # Create dictionary of elements # Hard-coded placeholder (to be replaced by a more generic loop) elements = {} elements[oCtrl.id] = oCtrl elements[oMach.id] = oMach elements[oGrid.id] = oGrid # Create event stack oEvents = events('smib_events.evnt') # Create recorder object oRecord = recorder('smib_recorder.rcd') # Run simulation oRecord = run_sim(ppc,elements,dynopt,oEvents,oRecord) # Calculate relative rotor angles rel_delta = np.array(oRecord.results['GEN1:delta']) - np.array(oRecord.results['GRID1:delta']) # Plot variables #plt.plot(oRecord.t_axis,rel_delta) plt.plot(oRecord.t_axis,oRecord.results['GEN1:Vt']) plt.xlabel('Time (s)') plt.ylabel('GEN1:Vt (pu)') plt.show() # Write recorded variables to output file oRecord.write_output('output.csv')
bsd-3-clause
xyguo/scikit-learn
examples/cluster/plot_mean_shift.py
351
1793
""" ============================================= A demo of the mean-shift clustering algorithm ============================================= Reference: Dorin Comaniciu and Peter Meer, "Mean Shift: A robust approach toward feature space analysis". IEEE Transactions on Pattern Analysis and Machine Intelligence. 2002. pp. 603-619. """ print(__doc__) import numpy as np from sklearn.cluster import MeanShift, estimate_bandwidth from sklearn.datasets.samples_generator import make_blobs ############################################################################### # Generate sample data centers = [[1, 1], [-1, -1], [1, -1]] X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6) ############################################################################### # Compute clustering with MeanShift # The following bandwidth can be automatically detected using bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500) ms = MeanShift(bandwidth=bandwidth, bin_seeding=True) ms.fit(X) labels = ms.labels_ cluster_centers = ms.cluster_centers_ labels_unique = np.unique(labels) n_clusters_ = len(labels_unique) print("number of estimated clusters : %d" % n_clusters_) ############################################################################### # Plot result import matplotlib.pyplot as plt from itertools import cycle plt.figure(1) plt.clf() colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk') for k, col in zip(range(n_clusters_), colors): my_members = labels == k cluster_center = cluster_centers[k] plt.plot(X[my_members, 0], X[my_members, 1], col + '.') plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markeredgecolor='k', markersize=14) plt.title('Estimated number of clusters: %d' % n_clusters_) plt.show()
bsd-3-clause
herilalaina/scikit-learn
sklearn/feature_extraction/text.py
11
53904
# -*- coding: utf-8 -*- # Authors: Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Lars Buitinck # Robert Layton <robertlayton@gmail.com> # Jochen Wersdörfer <jochen@wersdoerfer.de> # Roman Sinayev <roman.sinayev@gmail.com> # # License: BSD 3 clause """ The :mod:`sklearn.feature_extraction.text` submodule gathers utilities to build feature vectors from text documents. """ from __future__ import unicode_literals import array from collections import Mapping, defaultdict import numbers from operator import itemgetter import re import unicodedata import numpy as np import scipy.sparse as sp from ..base import BaseEstimator, TransformerMixin from ..externals import six from ..externals.six.moves import xrange from ..preprocessing import normalize from .hashing import FeatureHasher from .stop_words import ENGLISH_STOP_WORDS from ..utils.validation import check_is_fitted from ..utils.fixes import sp_version __all__ = ['CountVectorizer', 'ENGLISH_STOP_WORDS', 'TfidfTransformer', 'TfidfVectorizer', 'strip_accents_ascii', 'strip_accents_unicode', 'strip_tags'] def strip_accents_unicode(s): """Transform accentuated unicode symbols into their simple counterpart Warning: the python-level loop and join operations make this implementation 20 times slower than the strip_accents_ascii basic normalization. See also -------- strip_accents_ascii Remove accentuated char for any unicode symbol that has a direct ASCII equivalent. """ normalized = unicodedata.normalize('NFKD', s) if normalized == s: return s else: return ''.join([c for c in normalized if not unicodedata.combining(c)]) def strip_accents_ascii(s): """Transform accentuated unicode symbols into ascii or nothing Warning: this solution is only suited for languages that have a direct transliteration to ASCII symbols. See also -------- strip_accents_unicode Remove accentuated char for any unicode symbol. """ nkfd_form = unicodedata.normalize('NFKD', s) return nkfd_form.encode('ASCII', 'ignore').decode('ASCII') def strip_tags(s): """Basic regexp based HTML / XML tag stripper function For serious HTML/XML preprocessing you should rather use an external library such as lxml or BeautifulSoup. """ return re.compile(r"<([^>]+)>", flags=re.UNICODE).sub(" ", s) def _check_stop_list(stop): if stop == "english": return ENGLISH_STOP_WORDS elif isinstance(stop, six.string_types): raise ValueError("not a built-in stop list: %s" % stop) elif stop is None: return None else: # assume it's a collection return frozenset(stop) class VectorizerMixin(object): """Provides common code for text vectorizers (tokenization logic).""" _white_spaces = re.compile(r"\s\s+") def decode(self, doc): """Decode the input into a string of unicode symbols The decoding strategy depends on the vectorizer parameters. """ if self.input == 'filename': with open(doc, 'rb') as fh: doc = fh.read() elif self.input == 'file': doc = doc.read() if isinstance(doc, bytes): doc = doc.decode(self.encoding, self.decode_error) if doc is np.nan: raise ValueError("np.nan is an invalid document, expected byte or " "unicode string.") return doc def _word_ngrams(self, tokens, stop_words=None): """Turn tokens into a sequence of n-grams after stop words filtering""" # handle stop words if stop_words is not None: tokens = [w for w in tokens if w not in stop_words] # handle token n-grams min_n, max_n = self.ngram_range if max_n != 1: original_tokens = tokens if min_n == 1: # no need to do any slicing for unigrams # just iterate through the original tokens tokens = list(original_tokens) min_n += 1 else: tokens = [] n_original_tokens = len(original_tokens) # bind method outside of loop to reduce overhead tokens_append = tokens.append space_join = " ".join for n in xrange(min_n, min(max_n + 1, n_original_tokens + 1)): for i in xrange(n_original_tokens - n + 1): tokens_append(space_join(original_tokens[i: i + n])) return tokens def _char_ngrams(self, text_document): """Tokenize text_document into a sequence of character n-grams""" # normalize white spaces text_document = self._white_spaces.sub(" ", text_document) text_len = len(text_document) min_n, max_n = self.ngram_range if min_n == 1: # no need to do any slicing for unigrams # iterate through the string ngrams = list(text_document) min_n += 1 else: ngrams = [] # bind method outside of loop to reduce overhead ngrams_append = ngrams.append for n in xrange(min_n, min(max_n + 1, text_len + 1)): for i in xrange(text_len - n + 1): ngrams_append(text_document[i: i + n]) return ngrams def _char_wb_ngrams(self, text_document): """Whitespace sensitive char-n-gram tokenization. Tokenize text_document into a sequence of character n-grams operating only inside word boundaries. n-grams at the edges of words are padded with space.""" # normalize white spaces text_document = self._white_spaces.sub(" ", text_document) min_n, max_n = self.ngram_range ngrams = [] # bind method outside of loop to reduce overhead ngrams_append = ngrams.append for w in text_document.split(): w = ' ' + w + ' ' w_len = len(w) for n in xrange(min_n, max_n + 1): offset = 0 ngrams_append(w[offset:offset + n]) while offset + n < w_len: offset += 1 ngrams_append(w[offset:offset + n]) if offset == 0: # count a short word (w_len < n) only once break return ngrams def build_preprocessor(self): """Return a function to preprocess the text before tokenization""" if self.preprocessor is not None: return self.preprocessor # unfortunately python functools package does not have an efficient # `compose` function that would have allowed us to chain a dynamic # number of functions. However the cost of a lambda call is a few # hundreds of nanoseconds which is negligible when compared to the # cost of tokenizing a string of 1000 chars for instance. noop = lambda x: x # accent stripping if not self.strip_accents: strip_accents = noop elif callable(self.strip_accents): strip_accents = self.strip_accents elif self.strip_accents == 'ascii': strip_accents = strip_accents_ascii elif self.strip_accents == 'unicode': strip_accents = strip_accents_unicode else: raise ValueError('Invalid value for "strip_accents": %s' % self.strip_accents) if self.lowercase: return lambda x: strip_accents(x.lower()) else: return strip_accents def build_tokenizer(self): """Return a function that splits a string into a sequence of tokens""" if self.tokenizer is not None: return self.tokenizer token_pattern = re.compile(self.token_pattern) return lambda doc: token_pattern.findall(doc) def get_stop_words(self): """Build or fetch the effective stop words list""" return _check_stop_list(self.stop_words) def build_analyzer(self): """Return a callable that handles preprocessing and tokenization""" if callable(self.analyzer): return self.analyzer preprocess = self.build_preprocessor() if self.analyzer == 'char': return lambda doc: self._char_ngrams(preprocess(self.decode(doc))) elif self.analyzer == 'char_wb': return lambda doc: self._char_wb_ngrams( preprocess(self.decode(doc))) elif self.analyzer == 'word': stop_words = self.get_stop_words() tokenize = self.build_tokenizer() return lambda doc: self._word_ngrams( tokenize(preprocess(self.decode(doc))), stop_words) else: raise ValueError('%s is not a valid tokenization scheme/analyzer' % self.analyzer) def _validate_vocabulary(self): vocabulary = self.vocabulary if vocabulary is not None: if isinstance(vocabulary, set): vocabulary = sorted(vocabulary) if not isinstance(vocabulary, Mapping): vocab = {} for i, t in enumerate(vocabulary): if vocab.setdefault(t, i) != i: msg = "Duplicate term in vocabulary: %r" % t raise ValueError(msg) vocabulary = vocab else: indices = set(six.itervalues(vocabulary)) if len(indices) != len(vocabulary): raise ValueError("Vocabulary contains repeated indices.") for i in xrange(len(vocabulary)): if i not in indices: msg = ("Vocabulary of size %d doesn't contain index " "%d." % (len(vocabulary), i)) raise ValueError(msg) if not vocabulary: raise ValueError("empty vocabulary passed to fit") self.fixed_vocabulary_ = True self.vocabulary_ = dict(vocabulary) else: self.fixed_vocabulary_ = False def _check_vocabulary(self): """Check if vocabulary is empty or missing (not fit-ed)""" msg = "%(name)s - Vocabulary wasn't fitted." check_is_fitted(self, 'vocabulary_', msg=msg), if len(self.vocabulary_) == 0: raise ValueError("Vocabulary is empty") class HashingVectorizer(BaseEstimator, VectorizerMixin, TransformerMixin): """Convert a collection of text documents to a matrix of token occurrences It turns a collection of text documents into a scipy.sparse matrix holding token occurrence counts (or binary occurrence information), possibly normalized as token frequencies if norm='l1' or projected on the euclidean unit sphere if norm='l2'. This text vectorizer implementation uses the hashing trick to find the token string name to feature integer index mapping. This strategy has several advantages: - it is very low memory scalable to large datasets as there is no need to store a vocabulary dictionary in memory - it is fast to pickle and un-pickle as it holds no state besides the constructor parameters - it can be used in a streaming (partial fit) or parallel pipeline as there is no state computed during fit. There are also a couple of cons (vs using a CountVectorizer with an in-memory vocabulary): - there is no way to compute the inverse transform (from feature indices to string feature names) which can be a problem when trying to introspect which features are most important to a model. - there can be collisions: distinct tokens can be mapped to the same feature index. However in practice this is rarely an issue if n_features is large enough (e.g. 2 ** 18 for text classification problems). - no IDF weighting as this would render the transformer stateful. The hash function employed is the signed 32-bit version of Murmurhash3. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, default='utf-8' If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char', 'char_wb'} or callable Whether the feature should be made of word or character n-grams. Option 'char_wb' creates character n-grams only from text inside word boundaries; n-grams at the edges of words are padded with space. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n), default=(1, 1) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If 'english', a built-in stop word list for English is used. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. lowercase : boolean, default=True Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp selects tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). n_features : integer, default=(2 ** 20) The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners. norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. binary : boolean, default=False. If True, all non zero counts are set to 1. This is useful for discrete probabilistic models that model binary events rather than integer counts. dtype : type, optional Type of the matrix returned by fit_transform() or transform(). alternate_sign : boolean, optional, default True When True, an alternating sign is added to the features as to approximately conserve the inner product in the hashed space even for small n_features. This approach is similar to sparse random projection. .. versionadded:: 0.19 non_negative : boolean, optional, default False When True, an absolute value is applied to the features matrix prior to returning it. When used in conjunction with alternate_sign=True, this significantly reduces the inner product preservation property. .. deprecated:: 0.19 This option will be removed in 0.21. See also -------- CountVectorizer, TfidfVectorizer """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', n_features=(2 ** 20), binary=False, norm='l2', alternate_sign=True, non_negative=False, dtype=np.float64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.n_features = n_features self.ngram_range = ngram_range self.binary = binary self.norm = norm self.alternate_sign = alternate_sign self.non_negative = non_negative self.dtype = dtype def partial_fit(self, X, y=None): """Does nothing: this transformer is stateless. This method is just there to mark the fact that this transformer can work in a streaming setup. """ return self def fit(self, X, y=None): """Does nothing: this transformer is stateless.""" # triggers a parameter validation if isinstance(X, six.string_types): raise ValueError( "Iterable over raw text documents expected, " "string object received.") self._get_hasher().fit(X, y=y) return self def transform(self, X): """Transform a sequence of documents to a document-term matrix. Parameters ---------- X : iterable over raw text documents, length = n_samples Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed. Returns ------- X : scipy.sparse matrix, shape = (n_samples, self.n_features) Document-term matrix. """ if isinstance(X, six.string_types): raise ValueError( "Iterable over raw text documents expected, " "string object received.") analyzer = self.build_analyzer() X = self._get_hasher().transform(analyzer(doc) for doc in X) if self.binary: X.data.fill(1) if self.norm is not None: X = normalize(X, norm=self.norm, copy=False) return X def _get_hasher(self): return FeatureHasher(n_features=self.n_features, input_type='string', dtype=self.dtype, alternate_sign=self.alternate_sign, non_negative=self.non_negative) def _document_frequency(X): """Count the number of non-zero values for each feature in sparse X.""" if sp.isspmatrix_csr(X): return np.bincount(X.indices, minlength=X.shape[1]) else: return np.diff(sp.csc_matrix(X, copy=False).indptr) class CountVectorizer(BaseEstimator, VectorizerMixin): """Convert a collection of text documents to a matrix of token counts This implementation produces a sparse representation of the counts using scipy.sparse.csr_matrix. If you do not provide an a-priori dictionary and you do not use an analyzer that does some kind of feature selection then the number of features will be equal to the vocabulary size found by analyzing the data. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, 'utf-8' by default. If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char', 'char_wb'} or callable Whether the feature should be made of word or character n-grams. Option 'char_wb' creates character n-grams only from text inside word boundaries; n-grams at the edges of words are padded with space. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If 'english', a built-in stop word list for English is used. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. If None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms. lowercase : boolean, True by default Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp select tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). max_df : float in range [0.0, 1.0] or int, default=1.0 When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. min_df : float in range [0.0, 1.0] or int, default=1 When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. max_features : int or None, default=None If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. This parameter is ignored if vocabulary is not None. vocabulary : Mapping or iterable, optional Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. Indices in the mapping should not be repeated and should not have any gap between 0 and the largest index. binary : boolean, default=False If True, all non zero counts are set to 1. This is useful for discrete probabilistic models that model binary events rather than integer counts. dtype : type, optional Type of the matrix returned by fit_transform() or transform(). Attributes ---------- vocabulary_ : dict A mapping of terms to feature indices. stop_words_ : set Terms that were ignored because they either: - occurred in too many documents (`max_df`) - occurred in too few documents (`min_df`) - were cut off by feature selection (`max_features`). This is only available if no vocabulary was given. See also -------- HashingVectorizer, TfidfVectorizer Notes ----- The ``stop_words_`` attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling. """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.max_df = max_df self.min_df = min_df if max_df < 0 or min_df < 0: raise ValueError("negative value for max_df or min_df") self.max_features = max_features if max_features is not None: if (not isinstance(max_features, numbers.Integral) or max_features <= 0): raise ValueError( "max_features=%r, neither a positive integer nor None" % max_features) self.ngram_range = ngram_range self.vocabulary = vocabulary self.binary = binary self.dtype = dtype def _sort_features(self, X, vocabulary): """Sort features by name Returns a reordered matrix and modifies the vocabulary in place """ sorted_features = sorted(six.iteritems(vocabulary)) map_index = np.empty(len(sorted_features), dtype=np.int32) for new_val, (term, old_val) in enumerate(sorted_features): vocabulary[term] = new_val map_index[old_val] = new_val X.indices = map_index.take(X.indices, mode='clip') return X def _limit_features(self, X, vocabulary, high=None, low=None, limit=None): """Remove too rare or too common features. Prune features that are non zero in more samples than high or less documents than low, modifying the vocabulary, and restricting it to at most the limit most frequent. This does not prune samples with zero features. """ if high is None and low is None and limit is None: return X, set() # Calculate a mask based on document frequencies dfs = _document_frequency(X) tfs = np.asarray(X.sum(axis=0)).ravel() mask = np.ones(len(dfs), dtype=bool) if high is not None: mask &= dfs <= high if low is not None: mask &= dfs >= low if limit is not None and mask.sum() > limit: mask_inds = (-tfs[mask]).argsort()[:limit] new_mask = np.zeros(len(dfs), dtype=bool) new_mask[np.where(mask)[0][mask_inds]] = True mask = new_mask new_indices = np.cumsum(mask) - 1 # maps old indices to new removed_terms = set() for term, old_index in list(six.iteritems(vocabulary)): if mask[old_index]: vocabulary[term] = new_indices[old_index] else: del vocabulary[term] removed_terms.add(term) kept_indices = np.where(mask)[0] if len(kept_indices) == 0: raise ValueError("After pruning, no terms remain. Try a lower" " min_df or a higher max_df.") return X[:, kept_indices], removed_terms def _count_vocab(self, raw_documents, fixed_vocab): """Create sparse feature matrix, and vocabulary where fixed_vocab=False """ if fixed_vocab: vocabulary = self.vocabulary_ else: # Add a new value when a new vocabulary item is seen vocabulary = defaultdict() vocabulary.default_factory = vocabulary.__len__ analyze = self.build_analyzer() j_indices = [] indptr = [] values = _make_int_array() indptr.append(0) for doc in raw_documents: feature_counter = {} for feature in analyze(doc): try: feature_idx = vocabulary[feature] if feature_idx not in feature_counter: feature_counter[feature_idx] = 1 else: feature_counter[feature_idx] += 1 except KeyError: # Ignore out-of-vocabulary items for fixed_vocab=True continue j_indices.extend(feature_counter.keys()) values.extend(feature_counter.values()) indptr.append(len(j_indices)) if not fixed_vocab: # disable defaultdict behaviour vocabulary = dict(vocabulary) if not vocabulary: raise ValueError("empty vocabulary; perhaps the documents only" " contain stop words") if indptr[-1] > 2147483648: # = 2**31 - 1 if sp_version >= (0, 14): indices_dtype = np.int64 else: raise ValueError(('sparse CSR array has {} non-zero ' 'elements and requires 64 bit indexing, ' ' which is unsupported with scipy {}. ' 'Please upgrade to scipy >=0.14') .format(indptr[-1], '.'.join(sp_version))) else: indices_dtype = np.int32 j_indices = np.asarray(j_indices, dtype=indices_dtype) indptr = np.asarray(indptr, dtype=indices_dtype) values = np.frombuffer(values, dtype=np.intc) X = sp.csr_matrix((values, j_indices, indptr), shape=(len(indptr) - 1, len(vocabulary)), dtype=self.dtype) X.sort_indices() return vocabulary, X def fit(self, raw_documents, y=None): """Learn a vocabulary dictionary of all tokens in the raw documents. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- self """ self.fit_transform(raw_documents) return self def fit_transform(self, raw_documents, y=None): """Learn the vocabulary dictionary and return term-document matrix. This is equivalent to fit followed by transform, but more efficiently implemented. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- X : array, [n_samples, n_features] Document-term matrix. """ # We intentionally don't call the transform method to make # fit_transform overridable without unwanted side effects in # TfidfVectorizer. if isinstance(raw_documents, six.string_types): raise ValueError( "Iterable over raw text documents expected, " "string object received.") self._validate_vocabulary() max_df = self.max_df min_df = self.min_df max_features = self.max_features vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary_) if self.binary: X.data.fill(1) if not self.fixed_vocabulary_: X = self._sort_features(X, vocabulary) n_doc = X.shape[0] max_doc_count = (max_df if isinstance(max_df, numbers.Integral) else max_df * n_doc) min_doc_count = (min_df if isinstance(min_df, numbers.Integral) else min_df * n_doc) if max_doc_count < min_doc_count: raise ValueError( "max_df corresponds to < documents than min_df") X, self.stop_words_ = self._limit_features(X, vocabulary, max_doc_count, min_doc_count, max_features) self.vocabulary_ = vocabulary return X def transform(self, raw_documents): """Transform documents to document-term matrix. Extract token counts out of raw text documents using the vocabulary fitted with fit or the one provided to the constructor. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- X : sparse matrix, [n_samples, n_features] Document-term matrix. """ if isinstance(raw_documents, six.string_types): raise ValueError( "Iterable over raw text documents expected, " "string object received.") if not hasattr(self, 'vocabulary_'): self._validate_vocabulary() self._check_vocabulary() # use the same matrix-building strategy as fit_transform _, X = self._count_vocab(raw_documents, fixed_vocab=True) if self.binary: X.data.fill(1) return X def inverse_transform(self, X): """Return terms per document with nonzero entries in X. Parameters ---------- X : {array, sparse matrix}, shape = [n_samples, n_features] Returns ------- X_inv : list of arrays, len = n_samples List of arrays of terms. """ self._check_vocabulary() if sp.issparse(X): # We need CSR format for fast row manipulations. X = X.tocsr() else: # We need to convert X to a matrix, so that the indexing # returns 2D objects X = np.asmatrix(X) n_samples = X.shape[0] terms = np.array(list(self.vocabulary_.keys())) indices = np.array(list(self.vocabulary_.values())) inverse_vocabulary = terms[np.argsort(indices)] return [inverse_vocabulary[X[i, :].nonzero()[1]].ravel() for i in range(n_samples)] def get_feature_names(self): """Array mapping from feature integer indices to feature name""" self._check_vocabulary() return [t for t, i in sorted(six.iteritems(self.vocabulary_), key=itemgetter(1))] def _make_int_array(): """Construct an array.array of a type suitable for scipy.sparse indices.""" return array.array(str("i")) class TfidfTransformer(BaseEstimator, TransformerMixin): """Transform a count matrix to a normalized tf or tf-idf representation Tf means term-frequency while tf-idf means term-frequency times inverse document-frequency. This is a common term weighting scheme in information retrieval, that has also found good use in document classification. The goal of using tf-idf instead of the raw frequencies of occurrence of a token in a given document is to scale down the impact of tokens that occur very frequently in a given corpus and that are hence empirically less informative than features that occur in a small fraction of the training corpus. The formula that is used to compute the tf-idf of term t is tf-idf(d, t) = tf(t) * idf(d, t), and the idf is computed as idf(d, t) = log [ n / df(d, t) ] + 1 (if ``smooth_idf=False``), where n is the total number of documents and df(d, t) is the document frequency; the document frequency is the number of documents d that contain term t. The effect of adding "1" to the idf in the equation above is that terms with zero idf, i.e., terms that occur in all documents in a training set, will not be entirely ignored. (Note that the idf formula above differs from the standard textbook notation that defines the idf as idf(d, t) = log [ n / (df(d, t) + 1) ]). If ``smooth_idf=True`` (the default), the constant "1" is added to the numerator and denominator of the idf as if an extra document was seen containing every term in the collection exactly once, which prevents zero divisions: idf(d, t) = log [ (1 + n) / (1 + df(d, t)) ] + 1. Furthermore, the formulas used to compute tf and idf depend on parameter settings that correspond to the SMART notation used in IR as follows: Tf is "n" (natural) by default, "l" (logarithmic) when ``sublinear_tf=True``. Idf is "t" when use_idf is given, "n" (none) otherwise. Normalization is "c" (cosine) when ``norm='l2'``, "n" (none) when ``norm=None``. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. use_idf : boolean, default=True Enable inverse-document-frequency reweighting. smooth_idf : boolean, default=True Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions. sublinear_tf : boolean, default=False Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). References ---------- .. [Yates2011] `R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 68-74.` .. [MRS2008] `C.D. Manning, P. Raghavan and H. Schütze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 118-120.` """ def __init__(self, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False): self.norm = norm self.use_idf = use_idf self.smooth_idf = smooth_idf self.sublinear_tf = sublinear_tf def fit(self, X, y=None): """Learn the idf vector (global term weights) Parameters ---------- X : sparse matrix, [n_samples, n_features] a matrix of term/token counts """ if not sp.issparse(X): X = sp.csc_matrix(X) if self.use_idf: n_samples, n_features = X.shape df = _document_frequency(X) # perform idf smoothing if required df += int(self.smooth_idf) n_samples += int(self.smooth_idf) # log+1 instead of log makes sure terms with zero idf don't get # suppressed entirely. idf = np.log(float(n_samples) / df) + 1.0 self._idf_diag = sp.spdiags(idf, diags=0, m=n_features, n=n_features, format='csr') return self def transform(self, X, copy=True): """Transform a count matrix to a tf or tf-idf representation Parameters ---------- X : sparse matrix, [n_samples, n_features] a matrix of term/token counts copy : boolean, default True Whether to copy X and operate on the copy or perform in-place operations. Returns ------- vectors : sparse matrix, [n_samples, n_features] """ if hasattr(X, 'dtype') and np.issubdtype(X.dtype, np.floating): # preserve float family dtype X = sp.csr_matrix(X, copy=copy) else: # convert counts or binary occurrences to floats X = sp.csr_matrix(X, dtype=np.float64, copy=copy) n_samples, n_features = X.shape if self.sublinear_tf: np.log(X.data, X.data) X.data += 1 if self.use_idf: check_is_fitted(self, '_idf_diag', 'idf vector is not fitted') expected_n_features = self._idf_diag.shape[0] if n_features != expected_n_features: raise ValueError("Input has n_features=%d while the model" " has been trained with n_features=%d" % ( n_features, expected_n_features)) # *= doesn't work X = X * self._idf_diag if self.norm: X = normalize(X, norm=self.norm, copy=False) return X @property def idf_(self): # if _idf_diag is not set, this will raise an attribute error, # which means hasattr(self, "idf_") is False return np.ravel(self._idf_diag.sum(axis=0)) class TfidfVectorizer(CountVectorizer): """Convert a collection of raw documents to a matrix of TF-IDF features. Equivalent to CountVectorizer followed by TfidfTransformer. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, 'utf-8' by default. If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char'} or callable Whether the feature should be made of word or character n-grams. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If a string, it is passed to _check_stop_list and the appropriate stop list is returned. 'english' is currently the only supported string value. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. If None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms. lowercase : boolean, default True Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp selects tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). max_df : float in range [0.0, 1.0] or int, default=1.0 When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. min_df : float in range [0.0, 1.0] or int, default=1 When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. max_features : int or None, default=None If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. This parameter is ignored if vocabulary is not None. vocabulary : Mapping or iterable, optional Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. binary : boolean, default=False If True, all non-zero term counts are set to 1. This does not mean outputs will have only 0/1 values, only that the tf term in tf-idf is binary. (Set idf and normalization to False to get 0/1 outputs.) dtype : type, optional Type of the matrix returned by fit_transform() or transform(). norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. use_idf : boolean, default=True Enable inverse-document-frequency reweighting. smooth_idf : boolean, default=True Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions. sublinear_tf : boolean, default=False Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). Attributes ---------- vocabulary_ : dict A mapping of terms to feature indices. idf_ : array, shape = [n_features], or None The learned idf vector (global term weights) when ``use_idf`` is set to True, None otherwise. stop_words_ : set Terms that were ignored because they either: - occurred in too many documents (`max_df`) - occurred in too few documents (`min_df`) - were cut off by feature selection (`max_features`). This is only available if no vocabulary was given. See also -------- CountVectorizer Tokenize the documents and count the occurrences of token and return them as a sparse matrix TfidfTransformer Apply Term Frequency Inverse Document Frequency normalization to a sparse matrix of occurrence counts. Notes ----- The ``stop_words_`` attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling. """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, analyzer='word', stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False): super(TfidfVectorizer, self).__init__( input=input, encoding=encoding, decode_error=decode_error, strip_accents=strip_accents, lowercase=lowercase, preprocessor=preprocessor, tokenizer=tokenizer, analyzer=analyzer, stop_words=stop_words, token_pattern=token_pattern, ngram_range=ngram_range, max_df=max_df, min_df=min_df, max_features=max_features, vocabulary=vocabulary, binary=binary, dtype=dtype) self._tfidf = TfidfTransformer(norm=norm, use_idf=use_idf, smooth_idf=smooth_idf, sublinear_tf=sublinear_tf) # Broadcast the TF-IDF parameters to the underlying transformer instance # for easy grid search and repr @property def norm(self): return self._tfidf.norm @norm.setter def norm(self, value): self._tfidf.norm = value @property def use_idf(self): return self._tfidf.use_idf @use_idf.setter def use_idf(self, value): self._tfidf.use_idf = value @property def smooth_idf(self): return self._tfidf.smooth_idf @smooth_idf.setter def smooth_idf(self, value): self._tfidf.smooth_idf = value @property def sublinear_tf(self): return self._tfidf.sublinear_tf @sublinear_tf.setter def sublinear_tf(self, value): self._tfidf.sublinear_tf = value @property def idf_(self): return self._tfidf.idf_ def fit(self, raw_documents, y=None): """Learn vocabulary and idf from training set. Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects Returns ------- self : TfidfVectorizer """ X = super(TfidfVectorizer, self).fit_transform(raw_documents) self._tfidf.fit(X) return self def fit_transform(self, raw_documents, y=None): """Learn vocabulary and idf, return term-document matrix. This is equivalent to fit followed by transform, but more efficiently implemented. Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects Returns ------- X : sparse matrix, [n_samples, n_features] Tf-idf-weighted document-term matrix. """ X = super(TfidfVectorizer, self).fit_transform(raw_documents) self._tfidf.fit(X) # X is already a transformed view of raw_documents so # we set copy to False return self._tfidf.transform(X, copy=False) def transform(self, raw_documents, copy=True): """Transform documents to document-term matrix. Uses the vocabulary and document frequencies (df) learned by fit (or fit_transform). Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects copy : boolean, default True Whether to copy X and operate on the copy or perform in-place operations. Returns ------- X : sparse matrix, [n_samples, n_features] Tf-idf-weighted document-term matrix. """ check_is_fitted(self, '_tfidf', 'The tfidf vector is not fitted') X = super(TfidfVectorizer, self).transform(raw_documents) return self._tfidf.transform(X, copy=False)
bsd-3-clause
hsuantien/scikit-learn
examples/classification/plot_lda_qda.py
164
4806
""" ==================================================================== Linear and Quadratic Discriminant Analysis with confidence ellipsoid ==================================================================== Plot the confidence ellipsoids of each class and decision boundary """ print(__doc__) from scipy import linalg import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import colors from sklearn.lda import LDA from sklearn.qda import QDA ############################################################################### # colormap cmap = colors.LinearSegmentedColormap( 'red_blue_classes', {'red': [(0, 1, 1), (1, 0.7, 0.7)], 'green': [(0, 0.7, 0.7), (1, 0.7, 0.7)], 'blue': [(0, 0.7, 0.7), (1, 1, 1)]}) plt.cm.register_cmap(cmap=cmap) ############################################################################### # generate datasets def dataset_fixed_cov(): '''Generate 2 Gaussians samples with the same covariance matrix''' n, dim = 300, 2 np.random.seed(0) C = np.array([[0., -0.23], [0.83, .23]]) X = np.r_[np.dot(np.random.randn(n, dim), C), np.dot(np.random.randn(n, dim), C) + np.array([1, 1])] y = np.hstack((np.zeros(n), np.ones(n))) return X, y def dataset_cov(): '''Generate 2 Gaussians samples with different covariance matrices''' n, dim = 300, 2 np.random.seed(0) C = np.array([[0., -1.], [2.5, .7]]) * 2. X = np.r_[np.dot(np.random.randn(n, dim), C), np.dot(np.random.randn(n, dim), C.T) + np.array([1, 4])] y = np.hstack((np.zeros(n), np.ones(n))) return X, y ############################################################################### # plot functions def plot_data(lda, X, y, y_pred, fig_index): splot = plt.subplot(2, 2, fig_index) if fig_index == 1: plt.title('Linear Discriminant Analysis') plt.ylabel('Data with fixed covariance') elif fig_index == 2: plt.title('Quadratic Discriminant Analysis') elif fig_index == 3: plt.ylabel('Data with varying covariances') tp = (y == y_pred) # True Positive tp0, tp1 = tp[y == 0], tp[y == 1] X0, X1 = X[y == 0], X[y == 1] X0_tp, X0_fp = X0[tp0], X0[~tp0] X1_tp, X1_fp = X1[tp1], X1[~tp1] xmin, xmax = X[:, 0].min(), X[:, 0].max() ymin, ymax = X[:, 1].min(), X[:, 1].max() # class 0: dots plt.plot(X0_tp[:, 0], X0_tp[:, 1], 'o', color='red') plt.plot(X0_fp[:, 0], X0_fp[:, 1], '.', color='#990000') # dark red # class 1: dots plt.plot(X1_tp[:, 0], X1_tp[:, 1], 'o', color='blue') plt.plot(X1_fp[:, 0], X1_fp[:, 1], '.', color='#000099') # dark blue # class 0 and 1 : areas nx, ny = 200, 100 x_min, x_max = plt.xlim() y_min, y_max = plt.ylim() xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx), np.linspace(y_min, y_max, ny)) Z = lda.predict_proba(np.c_[xx.ravel(), yy.ravel()]) Z = Z[:, 1].reshape(xx.shape) plt.pcolormesh(xx, yy, Z, cmap='red_blue_classes', norm=colors.Normalize(0., 1.)) plt.contour(xx, yy, Z, [0.5], linewidths=2., colors='k') # means plt.plot(lda.means_[0][0], lda.means_[0][1], 'o', color='black', markersize=10) plt.plot(lda.means_[1][0], lda.means_[1][1], 'o', color='black', markersize=10) return splot def plot_ellipse(splot, mean, cov, color): v, w = linalg.eigh(cov) u = w[0] / linalg.norm(w[0]) angle = np.arctan(u[1] / u[0]) angle = 180 * angle / np.pi # convert to degrees # filled Gaussian at 2 standard deviation ell = mpl.patches.Ellipse(mean, 2 * v[0] ** 0.5, 2 * v[1] ** 0.5, 180 + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(0.5) splot.add_artist(ell) splot.set_xticks(()) splot.set_yticks(()) def plot_lda_cov(lda, splot): plot_ellipse(splot, lda.means_[0], lda.covariance_, 'red') plot_ellipse(splot, lda.means_[1], lda.covariance_, 'blue') def plot_qda_cov(qda, splot): plot_ellipse(splot, qda.means_[0], qda.covariances_[0], 'red') plot_ellipse(splot, qda.means_[1], qda.covariances_[1], 'blue') ############################################################################### for i, (X, y) in enumerate([dataset_fixed_cov(), dataset_cov()]): # LDA lda = LDA(solver="svd", store_covariance=True) y_pred = lda.fit(X, y).predict(X) splot = plot_data(lda, X, y, y_pred, fig_index=2 * i + 1) plot_lda_cov(lda, splot) plt.axis('tight') # QDA qda = QDA() y_pred = qda.fit(X, y, store_covariances=True).predict(X) splot = plot_data(qda, X, y, y_pred, fig_index=2 * i + 2) plot_qda_cov(qda, splot) plt.axis('tight') plt.suptitle('LDA vs QDA') plt.show()
bsd-3-clause
bgris/ODL_bgris
lib/python3.5/site-packages/scipy/stats/_multivariate.py
13
99071
# # Author: Joris Vankerschaver 2013 # from __future__ import division, print_function, absolute_import import math import numpy as np import scipy.linalg from scipy.misc import doccer from scipy.special import gammaln, psi, multigammaln from scipy._lib._util import check_random_state from scipy.linalg.blas import drot __all__ = ['multivariate_normal', 'matrix_normal', 'dirichlet', 'wishart', 'invwishart', 'special_ortho_group', 'ortho_group', 'random_correlation'] _LOG_2PI = np.log(2 * np.pi) _LOG_2 = np.log(2) _LOG_PI = np.log(np.pi) _doc_random_state = """\ random_state : None or int or np.random.RandomState instance, optional If int or RandomState, use it for drawing the random variates. If None (or np.random), the global np.random state is used. Default is None. """ def _squeeze_output(out): """ Remove single-dimensional entries from array and convert to scalar, if necessary. """ out = out.squeeze() if out.ndim == 0: out = out[()] return out def _eigvalsh_to_eps(spectrum, cond=None, rcond=None): """ Determine which eigenvalues are "small" given the spectrum. This is for compatibility across various linear algebra functions that should agree about whether or not a Hermitian matrix is numerically singular and what is its numerical matrix rank. This is designed to be compatible with scipy.linalg.pinvh. Parameters ---------- spectrum : 1d ndarray Array of eigenvalues of a Hermitian matrix. cond, rcond : float, optional Cutoff for small eigenvalues. Singular values smaller than rcond * largest_eigenvalue are considered zero. If None or -1, suitable machine precision is used. Returns ------- eps : float Magnitude cutoff for numerical negligibility. """ if rcond is not None: cond = rcond if cond in [None, -1]: t = spectrum.dtype.char.lower() factor = {'f': 1E3, 'd': 1E6} cond = factor[t] * np.finfo(t).eps eps = cond * np.max(abs(spectrum)) return eps def _pinv_1d(v, eps=1e-5): """ A helper function for computing the pseudoinverse. Parameters ---------- v : iterable of numbers This may be thought of as a vector of eigenvalues or singular values. eps : float Values with magnitude no greater than eps are considered negligible. Returns ------- v_pinv : 1d float ndarray A vector of pseudo-inverted numbers. """ return np.array([0 if abs(x) <= eps else 1/x for x in v], dtype=float) class _PSD(object): """ Compute coordinated functions of a symmetric positive semidefinite matrix. This class addresses two issues. Firstly it allows the pseudoinverse, the logarithm of the pseudo-determinant, and the rank of the matrix to be computed using one call to eigh instead of three. Secondly it allows these functions to be computed in a way that gives mutually compatible results. All of the functions are computed with a common understanding as to which of the eigenvalues are to be considered negligibly small. The functions are designed to coordinate with scipy.linalg.pinvh() but not necessarily with np.linalg.det() or with np.linalg.matrix_rank(). Parameters ---------- M : array_like Symmetric positive semidefinite matrix (2-D). cond, rcond : float, optional Cutoff for small eigenvalues. Singular values smaller than rcond * largest_eigenvalue are considered zero. If None or -1, suitable machine precision is used. lower : bool, optional Whether the pertinent array data is taken from the lower or upper triangle of M. (Default: lower) check_finite : bool, optional Whether to check that the input matrices contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. allow_singular : bool, optional Whether to allow a singular matrix. (Default: True) Notes ----- The arguments are similar to those of scipy.linalg.pinvh(). """ def __init__(self, M, cond=None, rcond=None, lower=True, check_finite=True, allow_singular=True): # Compute the symmetric eigendecomposition. # Note that eigh takes care of array conversion, chkfinite, # and assertion that the matrix is square. s, u = scipy.linalg.eigh(M, lower=lower, check_finite=check_finite) eps = _eigvalsh_to_eps(s, cond, rcond) if np.min(s) < -eps: raise ValueError('the input matrix must be positive semidefinite') d = s[s > eps] if len(d) < len(s) and not allow_singular: raise np.linalg.LinAlgError('singular matrix') s_pinv = _pinv_1d(s, eps) U = np.multiply(u, np.sqrt(s_pinv)) # Initialize the eagerly precomputed attributes. self.rank = len(d) self.U = U self.log_pdet = np.sum(np.log(d)) # Initialize an attribute to be lazily computed. self._pinv = None @property def pinv(self): if self._pinv is None: self._pinv = np.dot(self.U, self.U.T) return self._pinv class multi_rv_generic(object): """ Class which encapsulates common functionality between all multivariate distributions. """ def __init__(self, seed=None): super(multi_rv_generic, self).__init__() self._random_state = check_random_state(seed) @property def random_state(self): """ Get or set the RandomState object for generating random variates. This can be either None or an existing RandomState object. If None (or np.random), use the RandomState singleton used by np.random. If already a RandomState instance, use it. If an int, use a new RandomState instance seeded with seed. """ return self._random_state @random_state.setter def random_state(self, seed): self._random_state = check_random_state(seed) def _get_random_state(self, random_state): if random_state is not None: return check_random_state(random_state) else: return self._random_state class multi_rv_frozen(object): """ Class which encapsulates common functionality between all frozen multivariate distributions. """ @property def random_state(self): return self._dist._random_state @random_state.setter def random_state(self, seed): self._dist._random_state = check_random_state(seed) _mvn_doc_default_callparams = """\ mean : array_like, optional Mean of the distribution (default zero) cov : array_like, optional Covariance matrix of the distribution (default one) allow_singular : bool, optional Whether to allow a singular covariance matrix. (Default: False) """ _mvn_doc_callparams_note = \ """Setting the parameter `mean` to `None` is equivalent to having `mean` be the zero-vector. The parameter `cov` can be a scalar, in which case the covariance matrix is the identity times that value, a vector of diagonal entries for the covariance matrix, or a two-dimensional array_like. """ _mvn_doc_frozen_callparams = "" _mvn_doc_frozen_callparams_note = \ """See class definition for a detailed description of parameters.""" mvn_docdict_params = { '_mvn_doc_default_callparams': _mvn_doc_default_callparams, '_mvn_doc_callparams_note': _mvn_doc_callparams_note, '_doc_random_state': _doc_random_state } mvn_docdict_noparams = { '_mvn_doc_default_callparams': _mvn_doc_frozen_callparams, '_mvn_doc_callparams_note': _mvn_doc_frozen_callparams_note, '_doc_random_state': _doc_random_state } class multivariate_normal_gen(multi_rv_generic): r""" A multivariate normal random variable. The `mean` keyword specifies the mean. The `cov` keyword specifies the covariance matrix. Methods ------- ``pdf(x, mean=None, cov=1, allow_singular=False)`` Probability density function. ``logpdf(x, mean=None, cov=1, allow_singular=False)`` Log of the probability density function. ``rvs(mean=None, cov=1, size=1, random_state=None)`` Draw random samples from a multivariate normal distribution. ``entropy()`` Compute the differential entropy of the multivariate normal. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_mvn_doc_default_callparams)s %(_doc_random_state)s Alternatively, the object may be called (as a function) to fix the mean and covariance parameters, returning a "frozen" multivariate normal random variable: rv = multivariate_normal(mean=None, cov=1, allow_singular=False) - Frozen object with the same methods but holding the given mean and covariance fixed. Notes ----- %(_mvn_doc_callparams_note)s The covariance matrix `cov` must be a (symmetric) positive semi-definite matrix. The determinant and inverse of `cov` are computed as the pseudo-determinant and pseudo-inverse, respectively, so that `cov` does not need to have full rank. The probability density function for `multivariate_normal` is .. math:: f(x) = \frac{1}{\sqrt{(2 \pi)^k \det \Sigma}} \exp\left( -\frac{1}{2} (x - \mu)^T \Sigma^{-1} (x - \mu) \right), where :math:`\mu` is the mean, :math:`\Sigma` the covariance matrix, and :math:`k` is the dimension of the space where :math:`x` takes values. .. versionadded:: 0.14.0 Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy.stats import multivariate_normal >>> x = np.linspace(0, 5, 10, endpoint=False) >>> y = multivariate_normal.pdf(x, mean=2.5, cov=0.5); y array([ 0.00108914, 0.01033349, 0.05946514, 0.20755375, 0.43939129, 0.56418958, 0.43939129, 0.20755375, 0.05946514, 0.01033349]) >>> fig1 = plt.figure() >>> ax = fig1.add_subplot(111) >>> ax.plot(x, y) The input quantiles can be any shape of array, as long as the last axis labels the components. This allows us for instance to display the frozen pdf for a non-isotropic random variable in 2D as follows: >>> x, y = np.mgrid[-1:1:.01, -1:1:.01] >>> pos = np.dstack((x, y)) >>> rv = multivariate_normal([0.5, -0.2], [[2.0, 0.3], [0.3, 0.5]]) >>> fig2 = plt.figure() >>> ax2 = fig2.add_subplot(111) >>> ax2.contourf(x, y, rv.pdf(pos)) """ def __init__(self, seed=None): super(multivariate_normal_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__, mvn_docdict_params) def __call__(self, mean=None, cov=1, allow_singular=False, seed=None): """ Create a frozen multivariate normal distribution. See `multivariate_normal_frozen` for more information. """ return multivariate_normal_frozen(mean, cov, allow_singular=allow_singular, seed=seed) def _process_parameters(self, dim, mean, cov): """ Infer dimensionality from mean or covariance matrix, ensure that mean and covariance are full vector resp. matrix. """ # Try to infer dimensionality if dim is None: if mean is None: if cov is None: dim = 1 else: cov = np.asarray(cov, dtype=float) if cov.ndim < 2: dim = 1 else: dim = cov.shape[0] else: mean = np.asarray(mean, dtype=float) dim = mean.size else: if not np.isscalar(dim): raise ValueError("Dimension of random variable must be a scalar.") # Check input sizes and return full arrays for mean and cov if necessary if mean is None: mean = np.zeros(dim) mean = np.asarray(mean, dtype=float) if cov is None: cov = 1.0 cov = np.asarray(cov, dtype=float) if dim == 1: mean.shape = (1,) cov.shape = (1, 1) if mean.ndim != 1 or mean.shape[0] != dim: raise ValueError("Array 'mean' must be a vector of length %d." % dim) if cov.ndim == 0: cov = cov * np.eye(dim) elif cov.ndim == 1: cov = np.diag(cov) elif cov.ndim == 2 and cov.shape != (dim, dim): rows, cols = cov.shape if rows != cols: msg = ("Array 'cov' must be square if it is two dimensional," " but cov.shape = %s." % str(cov.shape)) else: msg = ("Dimension mismatch: array 'cov' is of shape %s," " but 'mean' is a vector of length %d.") msg = msg % (str(cov.shape), len(mean)) raise ValueError(msg) elif cov.ndim > 2: raise ValueError("Array 'cov' must be at most two-dimensional," " but cov.ndim = %d" % cov.ndim) return dim, mean, cov def _process_quantiles(self, x, dim): """ Adjust quantiles array so that last axis labels the components of each data point. """ x = np.asarray(x, dtype=float) if x.ndim == 0: x = x[np.newaxis] elif x.ndim == 1: if dim == 1: x = x[:, np.newaxis] else: x = x[np.newaxis, :] return x def _logpdf(self, x, mean, prec_U, log_det_cov, rank): """ Parameters ---------- x : ndarray Points at which to evaluate the log of the probability density function mean : ndarray Mean of the distribution prec_U : ndarray A decomposition such that np.dot(prec_U, prec_U.T) is the precision matrix, i.e. inverse of the covariance matrix. log_det_cov : float Logarithm of the determinant of the covariance matrix rank : int Rank of the covariance matrix. Notes ----- As this function does no argument checking, it should not be called directly; use 'logpdf' instead. """ dev = x - mean maha = np.sum(np.square(np.dot(dev, prec_U)), axis=-1) return -0.5 * (rank * _LOG_2PI + log_det_cov + maha) def logpdf(self, x, mean=None, cov=1, allow_singular=False): """ Log of the multivariate normal probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_mvn_doc_default_callparams)s Returns ------- pdf : ndarray Log of the probability density function evaluated at `x` Notes ----- %(_mvn_doc_callparams_note)s """ dim, mean, cov = self._process_parameters(None, mean, cov) x = self._process_quantiles(x, dim) psd = _PSD(cov, allow_singular=allow_singular) out = self._logpdf(x, mean, psd.U, psd.log_pdet, psd.rank) return _squeeze_output(out) def pdf(self, x, mean=None, cov=1, allow_singular=False): """ Multivariate normal probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_mvn_doc_default_callparams)s Returns ------- pdf : ndarray Probability density function evaluated at `x` Notes ----- %(_mvn_doc_callparams_note)s """ dim, mean, cov = self._process_parameters(None, mean, cov) x = self._process_quantiles(x, dim) psd = _PSD(cov, allow_singular=allow_singular) out = np.exp(self._logpdf(x, mean, psd.U, psd.log_pdet, psd.rank)) return _squeeze_output(out) def rvs(self, mean=None, cov=1, size=1, random_state=None): """ Draw random samples from a multivariate normal distribution. Parameters ---------- %(_mvn_doc_default_callparams)s size : integer, optional Number of samples to draw (default 1). %(_doc_random_state)s Returns ------- rvs : ndarray or scalar Random variates of size (`size`, `N`), where `N` is the dimension of the random variable. Notes ----- %(_mvn_doc_callparams_note)s """ dim, mean, cov = self._process_parameters(None, mean, cov) random_state = self._get_random_state(random_state) out = random_state.multivariate_normal(mean, cov, size) return _squeeze_output(out) def entropy(self, mean=None, cov=1): """ Compute the differential entropy of the multivariate normal. Parameters ---------- %(_mvn_doc_default_callparams)s Returns ------- h : scalar Entropy of the multivariate normal distribution Notes ----- %(_mvn_doc_callparams_note)s """ dim, mean, cov = self._process_parameters(None, mean, cov) _, logdet = np.linalg.slogdet(2 * np.pi * np.e * cov) return 0.5 * logdet multivariate_normal = multivariate_normal_gen() class multivariate_normal_frozen(multi_rv_frozen): def __init__(self, mean=None, cov=1, allow_singular=False, seed=None): """ Create a frozen multivariate normal distribution. Parameters ---------- mean : array_like, optional Mean of the distribution (default zero) cov : array_like, optional Covariance matrix of the distribution (default one) allow_singular : bool, optional If this flag is True then tolerate a singular covariance matrix (default False). seed : None or int or np.random.RandomState instance, optional This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance Default is None. Examples -------- When called with the default parameters, this will create a 1D random variable with mean 0 and covariance 1: >>> from scipy.stats import multivariate_normal >>> r = multivariate_normal() >>> r.mean array([ 0.]) >>> r.cov array([[1.]]) """ self._dist = multivariate_normal_gen(seed) self.dim, self.mean, self.cov = self._dist._process_parameters( None, mean, cov) self.cov_info = _PSD(self.cov, allow_singular=allow_singular) def logpdf(self, x): x = self._dist._process_quantiles(x, self.dim) out = self._dist._logpdf(x, self.mean, self.cov_info.U, self.cov_info.log_pdet, self.cov_info.rank) return _squeeze_output(out) def pdf(self, x): return np.exp(self.logpdf(x)) def rvs(self, size=1, random_state=None): return self._dist.rvs(self.mean, self.cov, size, random_state) def entropy(self): """ Computes the differential entropy of the multivariate normal. Returns ------- h : scalar Entropy of the multivariate normal distribution """ log_pdet = self.cov_info.log_pdet rank = self.cov_info.rank return 0.5 * (rank * (_LOG_2PI + 1) + log_pdet) # Set frozen generator docstrings from corresponding docstrings in # multivariate_normal_gen and fill in default strings in class docstrings for name in ['logpdf', 'pdf', 'rvs']: method = multivariate_normal_gen.__dict__[name] method_frozen = multivariate_normal_frozen.__dict__[name] method_frozen.__doc__ = doccer.docformat(method.__doc__, mvn_docdict_noparams) method.__doc__ = doccer.docformat(method.__doc__, mvn_docdict_params) _matnorm_doc_default_callparams = """\ mean : array_like, optional Mean of the distribution (default: `None`) rowcov : array_like, optional Among-row covariance matrix of the distribution (default: `1`) colcov : array_like, optional Among-column covariance matrix of the distribution (default: `1`) """ _matnorm_doc_callparams_note = \ """If `mean` is set to `None` then a matrix of zeros is used for the mean. The dimensions of this matrix are inferred from the shape of `rowcov` and `colcov`, if these are provided, or set to `1` if ambiguous. `rowcov` and `colcov` can be two-dimensional array_likes specifying the covariance matrices directly. Alternatively, a one-dimensional array will be be interpreted as the entries of a diagonal matrix, and a scalar or zero-dimensional array will be interpreted as this value times the identity matrix. """ _matnorm_doc_frozen_callparams = "" _matnorm_doc_frozen_callparams_note = \ """See class definition for a detailed description of parameters.""" matnorm_docdict_params = { '_matnorm_doc_default_callparams': _matnorm_doc_default_callparams, '_matnorm_doc_callparams_note': _matnorm_doc_callparams_note, '_doc_random_state': _doc_random_state } matnorm_docdict_noparams = { '_matnorm_doc_default_callparams': _matnorm_doc_frozen_callparams, '_matnorm_doc_callparams_note': _matnorm_doc_frozen_callparams_note, '_doc_random_state': _doc_random_state } class matrix_normal_gen(multi_rv_generic): r""" A matrix normal random variable. The `mean` keyword specifies the mean. The `rowcov` keyword specifies the among-row covariance matrix. The 'colcov' keyword specifies the among-column covariance matrix. Methods ------- ``pdf(X, mean=None, rowcov=1, colcov=1)`` Probability density function. ``logpdf(X, mean=None, rowcov=1, colcov=1)`` Log of the probability density function. ``rvs(mean=None, rowcov=1, colcov=1, size=1, random_state=None)`` Draw random samples. Parameters ---------- X : array_like Quantiles, with the last two axes of `X` denoting the components. %(_matnorm_doc_default_callparams)s %(_doc_random_state)s Alternatively, the object may be called (as a function) to fix the mean and covariance parameters, returning a "frozen" matrix normal random variable: rv = matrix_normal(mean=None, rowcov=1, colcov=1) - Frozen object with the same methods but holding the given mean and covariance fixed. Notes ----- %(_matnorm_doc_callparams_note)s The covariance matrices specified by `rowcov` and `colcov` must be (symmetric) positive definite. If the samples in `X` are :math:`m \times n`, then `rowcov` must be :math:`m \times m` and `colcov` must be :math:`n \times n`. `mean` must be the same shape as `X`. The probability density function for `matrix_normal` is .. math:: f(X) = (2 \pi)^{-\frac{mn}{2}}|U|^{-\frac{n}{2}} |V|^{-\frac{m}{2}} \exp\left( -\frac{1}{2} \mathrm{Tr}\left[ U^{-1} (X-M) V^{-1} (X-M)^T \right] \right), where :math:`M` is the mean, :math:`U` the among-row covariance matrix, :math:`V` the among-column covariance matrix. The `allow_singular` behaviour of the `multivariate_normal` distribution is not currently supported. Covariance matrices must be full rank. The `matrix_normal` distribution is closely related to the `multivariate_normal` distribution. Specifically, :math:`\mathrm{Vec}(X)` (the vector formed by concatenating the columns of :math:`X`) has a multivariate normal distribution with mean :math:`\mathrm{Vec}(M)` and covariance :math:`V \otimes U` (where :math:`\otimes` is the Kronecker product). Sampling and pdf evaluation are :math:`\mathcal{O}(m^3 + n^3 + m^2 n + m n^2)` for the matrix normal, but :math:`\mathcal{O}(m^3 n^3)` for the equivalent multivariate normal, making this equivalent form algorithmically inefficient. .. versionadded:: 0.17.0 Examples -------- >>> from scipy.stats import matrix_normal >>> M = np.arange(6).reshape(3,2); M array([[0, 1], [2, 3], [4, 5]]) >>> U = np.diag([1,2,3]); U array([[1, 0, 0], [0, 2, 0], [0, 0, 3]]) >>> V = 0.3*np.identity(2); V array([[ 0.3, 0. ], [ 0. , 0.3]]) >>> X = M + 0.1; X array([[ 0.1, 1.1], [ 2.1, 3.1], [ 4.1, 5.1]]) >>> matrix_normal.pdf(X, mean=M, rowcov=U, colcov=V) 0.023410202050005054 >>> # Equivalent multivariate normal >>> from scipy.stats import multivariate_normal >>> vectorised_X = X.T.flatten() >>> equiv_mean = M.T.flatten() >>> equiv_cov = np.kron(V,U) >>> multivariate_normal.pdf(vectorised_X, mean=equiv_mean, cov=equiv_cov) 0.023410202050005054 """ def __init__(self, seed=None): super(matrix_normal_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__, matnorm_docdict_params) def __call__(self, mean=None, rowcov=1, colcov=1, seed=None): """ Create a frozen matrix normal distribution. See `matrix_normal_frozen` for more information. """ return matrix_normal_frozen(mean, rowcov, colcov, seed=seed) def _process_parameters(self, mean, rowcov, colcov): """ Infer dimensionality from mean or covariance matrices. Handle defaults. Ensure compatible dimensions. """ # Process mean if mean is not None: mean = np.asarray(mean, dtype=float) meanshape = mean.shape if len(meanshape) != 2: raise ValueError("Array `mean` must be two dimensional.") if np.any(meanshape == 0): raise ValueError("Array `mean` has invalid shape.") # Process among-row covariance rowcov = np.asarray(rowcov, dtype=float) if rowcov.ndim == 0: if mean is not None: rowcov = rowcov * np.identity(meanshape[0]) else: rowcov = rowcov * np.identity(1) elif rowcov.ndim == 1: rowcov = np.diag(rowcov) rowshape = rowcov.shape if len(rowshape) != 2: raise ValueError("`rowcov` must be a scalar or a 2D array.") if rowshape[0] != rowshape[1]: raise ValueError("Array `rowcov` must be square.") if rowshape[0] == 0: raise ValueError("Array `rowcov` has invalid shape.") numrows = rowshape[0] # Process among-column covariance colcov = np.asarray(colcov, dtype=float) if colcov.ndim == 0: if mean is not None: colcov = colcov * np.identity(meanshape[1]) else: colcov = colcov * np.identity(1) elif colcov.ndim == 1: colcov = np.diag(colcov) colshape = colcov.shape if len(colshape) != 2: raise ValueError("`colcov` must be a scalar or a 2D array.") if colshape[0] != colshape[1]: raise ValueError("Array `colcov` must be square.") if colshape[0] == 0: raise ValueError("Array `colcov` has invalid shape.") numcols = colshape[0] # Ensure mean and covariances compatible if mean is not None: if meanshape[0] != numrows: raise ValueError("Arrays `mean` and `rowcov` must have the" "same number of rows.") if meanshape[1] != numcols: raise ValueError("Arrays `mean` and `colcov` must have the" "same number of columns.") else: mean = np.zeros((numrows,numcols)) dims = (numrows, numcols) return dims, mean, rowcov, colcov def _process_quantiles(self, X, dims): """ Adjust quantiles array so that last two axes labels the components of each data point. """ X = np.asarray(X, dtype=float) if X.ndim == 2: X = X[np.newaxis, :] if X.shape[-2:] != dims: raise ValueError("The shape of array `X` is not compatible " "with the distribution parameters.") return X def _logpdf(self, dims, X, mean, row_prec_rt, log_det_rowcov, col_prec_rt, log_det_colcov): """ Parameters ---------- dims : tuple Dimensions of the matrix variates X : ndarray Points at which to evaluate the log of the probability density function mean : ndarray Mean of the distribution row_prec_rt : ndarray A decomposition such that np.dot(row_prec_rt, row_prec_rt.T) is the inverse of the among-row covariance matrix log_det_rowcov : float Logarithm of the determinant of the among-row covariance matrix col_prec_rt : ndarray A decomposition such that np.dot(col_prec_rt, col_prec_rt.T) is the inverse of the among-column covariance matrix log_det_colcov : float Logarithm of the determinant of the among-column covariance matrix Notes ----- As this function does no argument checking, it should not be called directly; use 'logpdf' instead. """ numrows, numcols = dims roll_dev = np.rollaxis(X-mean, axis=-1, start=0) scale_dev = np.tensordot(col_prec_rt.T, np.dot(roll_dev, row_prec_rt), 1) maha = np.sum(np.sum(np.square(scale_dev), axis=-1), axis=0) return -0.5 * (numrows*numcols*_LOG_2PI + numcols*log_det_rowcov + numrows*log_det_colcov + maha) def logpdf(self, X, mean=None, rowcov=1, colcov=1): """ Log of the matrix normal probability density function. Parameters ---------- X : array_like Quantiles, with the last two axes of `X` denoting the components. %(_matnorm_doc_default_callparams)s Returns ------- logpdf : ndarray Log of the probability density function evaluated at `X` Notes ----- %(_matnorm_doc_callparams_note)s """ dims, mean, rowcov, colcov = self._process_parameters(mean, rowcov, colcov) X = self._process_quantiles(X, dims) rowpsd = _PSD(rowcov, allow_singular=False) colpsd = _PSD(colcov, allow_singular=False) out = self._logpdf(dims, X, mean, rowpsd.U, rowpsd.log_pdet, colpsd.U, colpsd.log_pdet) return _squeeze_output(out) def pdf(self, X, mean=None, rowcov=1, colcov=1): """ Matrix normal probability density function. Parameters ---------- X : array_like Quantiles, with the last two axes of `X` denoting the components. %(_matnorm_doc_default_callparams)s Returns ------- pdf : ndarray Probability density function evaluated at `X` Notes ----- %(_matnorm_doc_callparams_note)s """ return np.exp(self.logpdf(X, mean, rowcov, colcov)) def rvs(self, mean=None, rowcov=1, colcov=1, size=1, random_state=None): """ Draw random samples from a matrix normal distribution. Parameters ---------- %(_matnorm_doc_default_callparams)s size : integer, optional Number of samples to draw (default 1). %(_doc_random_state)s Returns ------- rvs : ndarray or scalar Random variates of size (`size`, `dims`), where `dims` is the dimension of the random matrices. Notes ----- %(_matnorm_doc_callparams_note)s """ size = int(size) dims, mean, rowcov, colcov = self._process_parameters(mean, rowcov, colcov) rowchol = scipy.linalg.cholesky(rowcov, lower=True) colchol = scipy.linalg.cholesky(colcov, lower=True) random_state = self._get_random_state(random_state) std_norm = random_state.standard_normal(size=(dims[1],size,dims[0])) roll_rvs = np.tensordot(colchol, np.dot(std_norm, rowchol.T), 1) out = np.rollaxis(roll_rvs.T, axis=1, start=0) + mean[np.newaxis,:,:] if size == 1: #out = np.squeeze(out, axis=0) out = out.reshape(mean.shape) return out matrix_normal = matrix_normal_gen() class matrix_normal_frozen(multi_rv_frozen): def __init__(self, mean=None, rowcov=1, colcov=1, seed=None): """ Create a frozen matrix normal distribution. Parameters ---------- %(_matnorm_doc_default_callparams)s seed : None or int or np.random.RandomState instance, optional If int or RandomState, use it for drawing the random variates. If None (or np.random), the global np.random state is used. Default is None. Examples -------- >>> from scipy.stats import matrix_normal >>> distn = matrix_normal(mean=np.zeros((3,3))) >>> X = distn.rvs(); X array([[-0.02976962, 0.93339138, -0.09663178], [ 0.67405524, 0.28250467, -0.93308929], [-0.31144782, 0.74535536, 1.30412916]]) >>> distn.pdf(X) 2.5160642368346784e-05 >>> distn.logpdf(X) -10.590229595124615 """ self._dist = matrix_normal_gen(seed) self.dims, self.mean, self.rowcov, self.colcov = \ self._dist._process_parameters(mean, rowcov, colcov) self.rowpsd = _PSD(self.rowcov, allow_singular=False) self.colpsd = _PSD(self.colcov, allow_singular=False) def logpdf(self, X): X = self._dist._process_quantiles(X, self.dims) out = self._dist._logpdf(self.dims, X, self.mean, self.rowpsd.U, self.rowpsd.log_pdet, self.colpsd.U, self.colpsd.log_pdet) return _squeeze_output(out) def pdf(self, X): return np.exp(self.logpdf(X)) def rvs(self, size=1, random_state=None): return self._dist.rvs(self.mean, self.rowcov, self.colcov, size, random_state) # Set frozen generator docstrings from corresponding docstrings in # matrix_normal_gen and fill in default strings in class docstrings for name in ['logpdf', 'pdf', 'rvs']: method = matrix_normal_gen.__dict__[name] method_frozen = matrix_normal_frozen.__dict__[name] method_frozen.__doc__ = doccer.docformat(method.__doc__, matnorm_docdict_noparams) method.__doc__ = doccer.docformat(method.__doc__, matnorm_docdict_params) _dirichlet_doc_default_callparams = """\ alpha : array_like The concentration parameters. The number of entries determines the dimensionality of the distribution. """ _dirichlet_doc_frozen_callparams = "" _dirichlet_doc_frozen_callparams_note = \ """See class definition for a detailed description of parameters.""" dirichlet_docdict_params = { '_dirichlet_doc_default_callparams': _dirichlet_doc_default_callparams, '_doc_random_state': _doc_random_state } dirichlet_docdict_noparams = { '_dirichlet_doc_default_callparams': _dirichlet_doc_frozen_callparams, '_doc_random_state': _doc_random_state } def _dirichlet_check_parameters(alpha): alpha = np.asarray(alpha) if np.min(alpha) <= 0: raise ValueError("All parameters must be greater than 0") elif alpha.ndim != 1: raise ValueError("Parameter vector 'a' must be one dimensional, " "but a.shape = %s." % (alpha.shape, )) return alpha def _dirichlet_check_input(alpha, x): x = np.asarray(x) if x.shape[0] + 1 != alpha.shape[0] and x.shape[0] != alpha.shape[0]: raise ValueError("Vector 'x' must have either the same number " "of entries as, or one entry fewer than, " "parameter vector 'a', but alpha.shape = %s " "and x.shape = %s." % (alpha.shape, x.shape)) if x.shape[0] != alpha.shape[0]: xk = np.array([1 - np.sum(x, 0)]) if xk.ndim == 1: x = np.append(x, xk) elif xk.ndim == 2: x = np.vstack((x, xk)) else: raise ValueError("The input must be one dimensional or a two " "dimensional matrix containing the entries.") if np.min(x) <= 0: raise ValueError("Each entry in 'x' must be greater than zero.") if np.max(x) > 1: raise ValueError("Each entry in 'x' must be smaller or equal one.") if (np.abs(np.sum(x, 0) - 1.0) > 10e-10).any(): raise ValueError("The input vector 'x' must lie within the normal " "simplex. but np.sum(x, 0) = %s." % np.sum(x, 0)) return x def _lnB(alpha): r""" Internal helper function to compute the log of the useful quotient .. math:: B(\alpha) = \frac{\prod_{i=1}{K}\Gamma(\alpha_i)}{\Gamma\left(\sum_{i=1}^{K}\alpha_i\right)} Parameters ---------- %(_dirichlet_doc_default_callparams)s Returns ------- B : scalar Helper quotient, internal use only """ return np.sum(gammaln(alpha)) - gammaln(np.sum(alpha)) class dirichlet_gen(multi_rv_generic): r""" A Dirichlet random variable. The `alpha` keyword specifies the concentration parameters of the distribution. .. versionadded:: 0.15.0 Methods ------- ``pdf(x, alpha)`` Probability density function. ``logpdf(x, alpha)`` Log of the probability density function. ``rvs(alpha, size=1, random_state=None)`` Draw random samples from a Dirichlet distribution. ``mean(alpha)`` The mean of the Dirichlet distribution ``var(alpha)`` The variance of the Dirichlet distribution ``entropy(alpha)`` Compute the differential entropy of the multivariate normal. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_dirichlet_doc_default_callparams)s %(_doc_random_state)s Alternatively, the object may be called (as a function) to fix concentration parameters, returning a "frozen" Dirichlet random variable: rv = dirichlet(alpha) - Frozen object with the same methods but holding the given concentration parameters fixed. Notes ----- Each :math:`\alpha` entry must be positive. The distribution has only support on the simplex defined by .. math:: \sum_{i=1}^{K} x_i \le 1 The probability density function for `dirichlet` is .. math:: f(x) = \frac{1}{\mathrm{B}(\boldsymbol\alpha)} \prod_{i=1}^K x_i^{\alpha_i - 1} where .. math:: \mathrm{B}(\boldsymbol\alpha) = \frac{\prod_{i=1}^K \Gamma(\alpha_i)} {\Gamma\bigl(\sum_{i=1}^K \alpha_i\bigr)} and :math:`\boldsymbol\alpha=(\alpha_1,\ldots,\alpha_K)`, the concentration parameters and :math:`K` is the dimension of the space where :math:`x` takes values. Note that the dirichlet interface is somewhat inconsistent. The array returned by the rvs function is transposed with respect to the format expected by the pdf and logpdf. """ def __init__(self, seed=None): super(dirichlet_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__, dirichlet_docdict_params) def __call__(self, alpha, seed=None): return dirichlet_frozen(alpha, seed=seed) def _logpdf(self, x, alpha): """ Parameters ---------- x : ndarray Points at which to evaluate the log of the probability density function %(_dirichlet_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'logpdf' instead. """ lnB = _lnB(alpha) return - lnB + np.sum((np.log(x.T) * (alpha - 1)).T, 0) def logpdf(self, x, alpha): """ Log of the Dirichlet probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_dirichlet_doc_default_callparams)s Returns ------- pdf : ndarray Log of the probability density function evaluated at `x`. """ alpha = _dirichlet_check_parameters(alpha) x = _dirichlet_check_input(alpha, x) out = self._logpdf(x, alpha) return _squeeze_output(out) def pdf(self, x, alpha): """ The Dirichlet probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_dirichlet_doc_default_callparams)s Returns ------- pdf : ndarray The probability density function evaluated at `x`. """ alpha = _dirichlet_check_parameters(alpha) x = _dirichlet_check_input(alpha, x) out = np.exp(self._logpdf(x, alpha)) return _squeeze_output(out) def mean(self, alpha): """ Compute the mean of the dirichlet distribution. Parameters ---------- %(_dirichlet_doc_default_callparams)s Returns ------- mu : scalar Mean of the Dirichlet distribution """ alpha = _dirichlet_check_parameters(alpha) out = alpha / (np.sum(alpha)) return _squeeze_output(out) def var(self, alpha): """ Compute the variance of the dirichlet distribution. Parameters ---------- %(_dirichlet_doc_default_callparams)s Returns ------- v : scalar Variance of the Dirichlet distribution """ alpha = _dirichlet_check_parameters(alpha) alpha0 = np.sum(alpha) out = (alpha * (alpha0 - alpha)) / ((alpha0 * alpha0) * (alpha0 + 1)) return out def entropy(self, alpha): """ Compute the differential entropy of the dirichlet distribution. Parameters ---------- %(_dirichlet_doc_default_callparams)s Returns ------- h : scalar Entropy of the Dirichlet distribution """ alpha = _dirichlet_check_parameters(alpha) alpha0 = np.sum(alpha) lnB = _lnB(alpha) K = alpha.shape[0] out = lnB + (alpha0 - K) * scipy.special.psi(alpha0) - np.sum( (alpha - 1) * scipy.special.psi(alpha)) return _squeeze_output(out) def rvs(self, alpha, size=1, random_state=None): """ Draw random samples from a Dirichlet distribution. Parameters ---------- %(_dirichlet_doc_default_callparams)s size : int, optional Number of samples to draw (default 1). %(_doc_random_state)s Returns ------- rvs : ndarray or scalar Random variates of size (`size`, `N`), where `N` is the dimension of the random variable. """ alpha = _dirichlet_check_parameters(alpha) random_state = self._get_random_state(random_state) return random_state.dirichlet(alpha, size=size) dirichlet = dirichlet_gen() class dirichlet_frozen(multi_rv_frozen): def __init__(self, alpha, seed=None): self.alpha = _dirichlet_check_parameters(alpha) self._dist = dirichlet_gen(seed) def logpdf(self, x): return self._dist.logpdf(x, self.alpha) def pdf(self, x): return self._dist.pdf(x, self.alpha) def mean(self): return self._dist.mean(self.alpha) def var(self): return self._dist.var(self.alpha) def entropy(self): return self._dist.entropy(self.alpha) def rvs(self, size=1, random_state=None): return self._dist.rvs(self.alpha, size, random_state) # Set frozen generator docstrings from corresponding docstrings in # multivariate_normal_gen and fill in default strings in class docstrings for name in ['logpdf', 'pdf', 'rvs', 'mean', 'var', 'entropy']: method = dirichlet_gen.__dict__[name] method_frozen = dirichlet_frozen.__dict__[name] method_frozen.__doc__ = doccer.docformat( method.__doc__, dirichlet_docdict_noparams) method.__doc__ = doccer.docformat(method.__doc__, dirichlet_docdict_params) _wishart_doc_default_callparams = """\ df : int Degrees of freedom, must be greater than or equal to dimension of the scale matrix scale : array_like Symmetric positive definite scale matrix of the distribution """ _wishart_doc_callparams_note = "" _wishart_doc_frozen_callparams = "" _wishart_doc_frozen_callparams_note = \ """See class definition for a detailed description of parameters.""" wishart_docdict_params = { '_doc_default_callparams': _wishart_doc_default_callparams, '_doc_callparams_note': _wishart_doc_callparams_note, '_doc_random_state': _doc_random_state } wishart_docdict_noparams = { '_doc_default_callparams': _wishart_doc_frozen_callparams, '_doc_callparams_note': _wishart_doc_frozen_callparams_note, '_doc_random_state': _doc_random_state } class wishart_gen(multi_rv_generic): r""" A Wishart random variable. The `df` keyword specifies the degrees of freedom. The `scale` keyword specifies the scale matrix, which must be symmetric and positive definite. In this context, the scale matrix is often interpreted in terms of a multivariate normal precision matrix (the inverse of the covariance matrix). Methods ------- ``pdf(x, df, scale)`` Probability density function. ``logpdf(x, df, scale)`` Log of the probability density function. ``rvs(df, scale, size=1, random_state=None)`` Draw random samples from a Wishart distribution. ``entropy()`` Compute the differential entropy of the Wishart distribution. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_doc_default_callparams)s %(_doc_random_state)s Alternatively, the object may be called (as a function) to fix the degrees of freedom and scale parameters, returning a "frozen" Wishart random variable: rv = wishart(df=1, scale=1) - Frozen object with the same methods but holding the given degrees of freedom and scale fixed. See Also -------- invwishart, chi2 Notes ----- %(_doc_callparams_note)s The scale matrix `scale` must be a symmetric positive definite matrix. Singular matrices, including the symmetric positive semi-definite case, are not supported. The Wishart distribution is often denoted .. math:: W_p(\nu, \Sigma) where :math:`\nu` is the degrees of freedom and :math:`\Sigma` is the :math:`p \times p` scale matrix. The probability density function for `wishart` has support over positive definite matrices :math:`S`; if :math:`S \sim W_p(\nu, \Sigma)`, then its PDF is given by: .. math:: f(S) = \frac{|S|^{\frac{\nu - p - 1}{2}}}{2^{ \frac{\nu p}{2} } |\Sigma|^\frac{\nu}{2} \Gamma_p \left ( \frac{\nu}{2} \right )} \exp\left( -tr(\Sigma^{-1} S) / 2 \right) If :math:`S \sim W_p(\nu, \Sigma)` (Wishart) then :math:`S^{-1} \sim W_p^{-1}(\nu, \Sigma^{-1})` (inverse Wishart). If the scale matrix is 1-dimensional and equal to one, then the Wishart distribution :math:`W_1(\nu, 1)` collapses to the :math:`\chi^2(\nu)` distribution. .. versionadded:: 0.16.0 References ---------- .. [1] M.L. Eaton, "Multivariate Statistics: A Vector Space Approach", Wiley, 1983. .. [2] W.B. Smith and R.R. Hocking, "Algorithm AS 53: Wishart Variate Generator", Applied Statistics, vol. 21, pp. 341-345, 1972. Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy.stats import wishart, chi2 >>> x = np.linspace(1e-5, 8, 100) >>> w = wishart.pdf(x, df=3, scale=1); w[:5] array([ 0.00126156, 0.10892176, 0.14793434, 0.17400548, 0.1929669 ]) >>> c = chi2.pdf(x, 3); c[:5] array([ 0.00126156, 0.10892176, 0.14793434, 0.17400548, 0.1929669 ]) >>> plt.plot(x, w) The input quantiles can be any shape of array, as long as the last axis labels the components. """ def __init__(self, seed=None): super(wishart_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__, wishart_docdict_params) def __call__(self, df=None, scale=None, seed=None): """ Create a frozen Wishart distribution. See `wishart_frozen` for more information. """ return wishart_frozen(df, scale, seed) def _process_parameters(self, df, scale): if scale is None: scale = 1.0 scale = np.asarray(scale, dtype=float) if scale.ndim == 0: scale = scale[np.newaxis,np.newaxis] elif scale.ndim == 1: scale = np.diag(scale) elif scale.ndim == 2 and not scale.shape[0] == scale.shape[1]: raise ValueError("Array 'scale' must be square if it is two" " dimensional, but scale.scale = %s." % str(scale.shape)) elif scale.ndim > 2: raise ValueError("Array 'scale' must be at most two-dimensional," " but scale.ndim = %d" % scale.ndim) dim = scale.shape[0] if df is None: df = dim elif not np.isscalar(df): raise ValueError("Degrees of freedom must be a scalar.") elif df < dim: raise ValueError("Degrees of freedom cannot be less than dimension" " of scale matrix, but df = %d" % df) return dim, df, scale def _process_quantiles(self, x, dim): """ Adjust quantiles array so that last axis labels the components of each data point. """ x = np.asarray(x, dtype=float) if x.ndim == 0: x = x * np.eye(dim)[:, :, np.newaxis] if x.ndim == 1: if dim == 1: x = x[np.newaxis, np.newaxis, :] else: x = np.diag(x)[:, :, np.newaxis] elif x.ndim == 2: if not x.shape[0] == x.shape[1]: raise ValueError("Quantiles must be square if they are two" " dimensional, but x.shape = %s." % str(x.shape)) x = x[:, :, np.newaxis] elif x.ndim == 3: if not x.shape[0] == x.shape[1]: raise ValueError("Quantiles must be square in the first two" " dimensions if they are three dimensional" ", but x.shape = %s." % str(x.shape)) elif x.ndim > 3: raise ValueError("Quantiles must be at most two-dimensional with" " an additional dimension for multiple" "components, but x.ndim = %d" % x.ndim) # Now we have 3-dim array; should have shape [dim, dim, *] if not x.shape[0:2] == (dim, dim): raise ValueError('Quantiles have incompatible dimensions: should' ' be %s, got %s.' % ((dim, dim), x.shape[0:2])) return x def _process_size(self, size): size = np.asarray(size) if size.ndim == 0: size = size[np.newaxis] elif size.ndim > 1: raise ValueError('Size must be an integer or tuple of integers;' ' thus must have dimension <= 1.' ' Got size.ndim = %s' % str(tuple(size))) n = size.prod() shape = tuple(size) return n, shape def _logpdf(self, x, dim, df, scale, log_det_scale, C): """ Parameters ---------- x : ndarray Points at which to evaluate the log of the probability density function dim : int Dimension of the scale matrix df : int Degrees of freedom scale : ndarray Scale matrix log_det_scale : float Logarithm of the determinant of the scale matrix C : ndarray Cholesky factorization of the scale matrix, lower triagular. Notes ----- As this function does no argument checking, it should not be called directly; use 'logpdf' instead. """ # log determinant of x # Note: x has components along the last axis, so that x.T has # components alone the 0-th axis. Then since det(A) = det(A'), this # gives us a 1-dim vector of determinants # Retrieve tr(scale^{-1} x) log_det_x = np.zeros(x.shape[-1]) scale_inv_x = np.zeros(x.shape) tr_scale_inv_x = np.zeros(x.shape[-1]) for i in range(x.shape[-1]): _, log_det_x[i] = self._cholesky_logdet(x[:,:,i]) scale_inv_x[:,:,i] = scipy.linalg.cho_solve((C, True), x[:,:,i]) tr_scale_inv_x[i] = scale_inv_x[:,:,i].trace() # Log PDF out = ((0.5 * (df - dim - 1) * log_det_x - 0.5 * tr_scale_inv_x) - (0.5 * df * dim * _LOG_2 + 0.5 * df * log_det_scale + multigammaln(0.5*df, dim))) return out def logpdf(self, x, df, scale): """ Log of the Wishart probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. Each quantile must be a symmetric positive definite matrix. %(_doc_default_callparams)s Returns ------- pdf : ndarray Log of the probability density function evaluated at `x` Notes ----- %(_doc_callparams_note)s """ dim, df, scale = self._process_parameters(df, scale) x = self._process_quantiles(x, dim) # Cholesky decomposition of scale, get log(det(scale)) C, log_det_scale = self._cholesky_logdet(scale) out = self._logpdf(x, dim, df, scale, log_det_scale, C) return _squeeze_output(out) def pdf(self, x, df, scale): """ Wishart probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. Each quantile must be a symmetric positive definite matrix. %(_doc_default_callparams)s Returns ------- pdf : ndarray Probability density function evaluated at `x` Notes ----- %(_doc_callparams_note)s """ return np.exp(self.logpdf(x, df, scale)) def _mean(self, dim, df, scale): """ Parameters ---------- dim : int Dimension of the scale matrix %(_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'mean' instead. """ return df * scale def mean(self, df, scale): """ Mean of the Wishart distribution Parameters ---------- %(_doc_default_callparams)s Returns ------- mean : float The mean of the distribution """ dim, df, scale = self._process_parameters(df, scale) out = self._mean(dim, df, scale) return _squeeze_output(out) def _mode(self, dim, df, scale): """ Parameters ---------- dim : int Dimension of the scale matrix %(_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'mode' instead. """ if df >= dim + 1: out = (df-dim-1) * scale else: out = None return out def mode(self, df, scale): """ Mode of the Wishart distribution Only valid if the degrees of freedom are greater than the dimension of the scale matrix. Parameters ---------- %(_doc_default_callparams)s Returns ------- mode : float or None The Mode of the distribution """ dim, df, scale = self._process_parameters(df, scale) out = self._mode(dim, df, scale) return _squeeze_output(out) if out is not None else out def _var(self, dim, df, scale): """ Parameters ---------- dim : int Dimension of the scale matrix %(_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'var' instead. """ var = scale**2 diag = scale.diagonal() # 1 x dim array var += np.outer(diag, diag) var *= df return var def var(self, df, scale): """ Variance of the Wishart distribution Parameters ---------- %(_doc_default_callparams)s Returns ------- var : float The variance of the distribution """ dim, df, scale = self._process_parameters(df, scale) out = self._var(dim, df, scale) return _squeeze_output(out) def _standard_rvs(self, n, shape, dim, df, random_state): """ Parameters ---------- n : integer Number of variates to generate shape : iterable Shape of the variates to generate dim : int Dimension of the scale matrix df : int Degrees of freedom random_state : np.random.RandomState instance RandomState used for drawing the random variates. Notes ----- As this function does no argument checking, it should not be called directly; use 'rvs' instead. """ # Random normal variates for off-diagonal elements n_tril = dim * (dim-1) // 2 covariances = random_state.normal( size=n*n_tril).reshape(shape+(n_tril,)) # Random chi-square variates for diagonal elements variances = np.r_[[random_state.chisquare(df-(i+1)+1, size=n)**0.5 for i in range(dim)]].reshape((dim,) + shape[::-1]).T # Create the A matri(ces) - lower triangular A = np.zeros(shape + (dim, dim)) # Input the covariances size_idx = tuple([slice(None,None,None)]*len(shape)) tril_idx = np.tril_indices(dim, k=-1) A[size_idx + tril_idx] = covariances # Input the variances diag_idx = np.diag_indices(dim) A[size_idx + diag_idx] = variances return A def _rvs(self, n, shape, dim, df, C, random_state): """ Parameters ---------- n : integer Number of variates to generate shape : iterable Shape of the variates to generate dim : int Dimension of the scale matrix df : int Degrees of freedom scale : ndarray Scale matrix C : ndarray Cholesky factorization of the scale matrix, lower triangular. %(_doc_random_state)s Notes ----- As this function does no argument checking, it should not be called directly; use 'rvs' instead. """ random_state = self._get_random_state(random_state) # Calculate the matrices A, which are actually lower triangular # Cholesky factorizations of a matrix B such that B ~ W(df, I) A = self._standard_rvs(n, shape, dim, df, random_state) # Calculate SA = C A A' C', where SA ~ W(df, scale) # Note: this is the product of a (lower) (lower) (lower)' (lower)' # or, denoting B = AA', it is C B C' where C is the lower # triangular Cholesky factorization of the scale matrix. # this appears to conflict with the instructions in [1]_, which # suggest that it should be D' B D where D is the lower # triangular factorization of the scale matrix. However, it is # meant to refer to the Bartlett (1933) representation of a # Wishart random variate as L A A' L' where L is lower triangular # so it appears that understanding D' to be upper triangular # is either a typo in or misreading of [1]_. for index in np.ndindex(shape): CA = np.dot(C, A[index]) A[index] = np.dot(CA, CA.T) return A def rvs(self, df, scale, size=1, random_state=None): """ Draw random samples from a Wishart distribution. Parameters ---------- %(_doc_default_callparams)s size : integer or iterable of integers, optional Number of samples to draw (default 1). %(_doc_random_state)s Returns ------- rvs : ndarray Random variates of shape (`size`) + (`dim`, `dim), where `dim` is the dimension of the scale matrix. Notes ----- %(_doc_callparams_note)s """ n, shape = self._process_size(size) dim, df, scale = self._process_parameters(df, scale) # Cholesky decomposition of scale C = scipy.linalg.cholesky(scale, lower=True) out = self._rvs(n, shape, dim, df, C, random_state) return _squeeze_output(out) def _entropy(self, dim, df, log_det_scale): """ Parameters ---------- dim : int Dimension of the scale matrix df : int Degrees of freedom log_det_scale : float Logarithm of the determinant of the scale matrix Notes ----- As this function does no argument checking, it should not be called directly; use 'entropy' instead. """ return ( 0.5 * (dim+1) * log_det_scale + 0.5 * dim * (dim+1) * _LOG_2 + multigammaln(0.5*df, dim) - 0.5 * (df - dim - 1) * np.sum( [psi(0.5*(df + 1 - (i+1))) for i in range(dim)] ) + 0.5 * df * dim ) def entropy(self, df, scale): """ Compute the differential entropy of the Wishart. Parameters ---------- %(_doc_default_callparams)s Returns ------- h : scalar Entropy of the Wishart distribution Notes ----- %(_doc_callparams_note)s """ dim, df, scale = self._process_parameters(df, scale) _, log_det_scale = self._cholesky_logdet(scale) return self._entropy(dim, df, log_det_scale) def _cholesky_logdet(self, scale): """ Compute Cholesky decomposition and determine (log(det(scale)). Parameters ---------- scale : ndarray Scale matrix. Returns ------- c_decomp : ndarray The Cholesky decomposition of `scale`. logdet : scalar The log of the determinant of `scale`. Notes ----- This computation of ``logdet`` is equivalent to ``np.linalg.slogdet(scale)``. It is ~2x faster though. """ c_decomp = scipy.linalg.cholesky(scale, lower=True) logdet = 2 * np.sum(np.log(c_decomp.diagonal())) return c_decomp, logdet wishart = wishart_gen() class wishart_frozen(multi_rv_frozen): """ Create a frozen Wishart distribution. Parameters ---------- df : array_like Degrees of freedom of the distribution scale : array_like Scale matrix of the distribution seed : None or int or np.random.RandomState instance, optional This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance Default is None. """ def __init__(self, df, scale, seed=None): self._dist = wishart_gen(seed) self.dim, self.df, self.scale = self._dist._process_parameters( df, scale) self.C, self.log_det_scale = self._dist._cholesky_logdet(self.scale) def logpdf(self, x): x = self._dist._process_quantiles(x, self.dim) out = self._dist._logpdf(x, self.dim, self.df, self.scale, self.log_det_scale, self.C) return _squeeze_output(out) def pdf(self, x): return np.exp(self.logpdf(x)) def mean(self): out = self._dist._mean(self.dim, self.df, self.scale) return _squeeze_output(out) def mode(self): out = self._dist._mode(self.dim, self.df, self.scale) return _squeeze_output(out) if out is not None else out def var(self): out = self._dist._var(self.dim, self.df, self.scale) return _squeeze_output(out) def rvs(self, size=1, random_state=None): n, shape = self._dist._process_size(size) out = self._dist._rvs(n, shape, self.dim, self.df, self.C, random_state) return _squeeze_output(out) def entropy(self): return self._dist._entropy(self.dim, self.df, self.log_det_scale) # Set frozen generator docstrings from corresponding docstrings in # Wishart and fill in default strings in class docstrings for name in ['logpdf', 'pdf', 'mean', 'mode', 'var', 'rvs', 'entropy']: method = wishart_gen.__dict__[name] method_frozen = wishart_frozen.__dict__[name] method_frozen.__doc__ = doccer.docformat( method.__doc__, wishart_docdict_noparams) method.__doc__ = doccer.docformat(method.__doc__, wishart_docdict_params) from numpy import asarray_chkfinite, asarray from scipy.linalg.misc import LinAlgError from scipy.linalg.lapack import get_lapack_funcs def _cho_inv_batch(a, check_finite=True): """ Invert the matrices a_i, using a Cholesky factorization of A, where a_i resides in the last two dimensions of a and the other indices describe the index i. Overwrites the data in a. Parameters ---------- a : array Array of matrices to invert, where the matrices themselves are stored in the last two dimensions. check_finite : bool, optional Whether to check that the input matrices contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. Returns ------- x : array Array of inverses of the matrices ``a_i``. See also -------- scipy.linalg.cholesky : Cholesky factorization of a matrix """ if check_finite: a1 = asarray_chkfinite(a) else: a1 = asarray(a) if len(a1.shape) < 2 or a1.shape[-2] != a1.shape[-1]: raise ValueError('expected square matrix in last two dimensions') potrf, potri = get_lapack_funcs(('potrf','potri'), (a1,)) tril_idx = np.tril_indices(a.shape[-2], k=-1) triu_idx = np.triu_indices(a.shape[-2], k=1) for index in np.ndindex(a1.shape[:-2]): # Cholesky decomposition a1[index], info = potrf(a1[index], lower=True, overwrite_a=False, clean=False) if info > 0: raise LinAlgError("%d-th leading minor not positive definite" % info) if info < 0: raise ValueError('illegal value in %d-th argument of internal' ' potrf' % -info) # Inversion a1[index], info = potri(a1[index], lower=True, overwrite_c=False) if info > 0: raise LinAlgError("the inverse could not be computed") if info < 0: raise ValueError('illegal value in %d-th argument of internal' ' potrf' % -info) # Make symmetric (dpotri only fills in the lower triangle) a1[index][triu_idx] = a1[index][tril_idx] return a1 class invwishart_gen(wishart_gen): r""" An inverse Wishart random variable. The `df` keyword specifies the degrees of freedom. The `scale` keyword specifies the scale matrix, which must be symmetric and positive definite. In this context, the scale matrix is often interpreted in terms of a multivariate normal covariance matrix. Methods ------- ``pdf(x, df, scale)`` Probability density function. ``logpdf(x, df, scale)`` Log of the probability density function. ``rvs(df, scale, size=1, random_state=None)`` Draw random samples from an inverse Wishart distribution. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. %(_doc_default_callparams)s %(_doc_random_state)s Alternatively, the object may be called (as a function) to fix the degrees of freedom and scale parameters, returning a "frozen" inverse Wishart random variable: rv = invwishart(df=1, scale=1) - Frozen object with the same methods but holding the given degrees of freedom and scale fixed. See Also -------- wishart Notes ----- %(_doc_callparams_note)s The scale matrix `scale` must be a symmetric positive definite matrix. Singular matrices, including the symmetric positive semi-definite case, are not supported. The inverse Wishart distribution is often denoted .. math:: W_p^{-1}(\nu, \Psi) where :math:`\nu` is the degrees of freedom and :math:`\Psi` is the :math:`p \times p` scale matrix. The probability density function for `invwishart` has support over positive definite matrices :math:`S`; if :math:`S \sim W^{-1}_p(\nu, \Sigma)`, then its PDF is given by: .. math:: f(S) = \frac{|\Sigma|^\frac{\nu}{2}}{2^{ \frac{\nu p}{2} } |S|^{\frac{\nu + p + 1}{2}} \Gamma_p \left(\frac{\nu}{2} \right)} \exp\left( -tr(\Sigma S^{-1}) / 2 \right) If :math:`S \sim W_p^{-1}(\nu, \Psi)` (inverse Wishart) then :math:`S^{-1} \sim W_p(\nu, \Psi^{-1})` (Wishart). If the scale matrix is 1-dimensional and equal to one, then the inverse Wishart distribution :math:`W_1(\nu, 1)` collapses to the inverse Gamma distribution with parameters shape = :math:`\frac{\nu}{2}` and scale = :math:`\frac{1}{2}`. .. versionadded:: 0.16.0 References ---------- .. [1] M.L. Eaton, "Multivariate Statistics: A Vector Space Approach", Wiley, 1983. .. [2] M.C. Jones, "Generating Inverse Wishart Matrices", Communications in Statistics - Simulation and Computation, vol. 14.2, pp.511-514, 1985. Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy.stats import invwishart, invgamma >>> x = np.linspace(0.01, 1, 100) >>> iw = invwishart.pdf(x, df=6, scale=1) >>> iw[:3] array([ 1.20546865e-15, 5.42497807e-06, 4.45813929e-03]) >>> ig = invgamma.pdf(x, 6/2., scale=1./2) >>> ig[:3] array([ 1.20546865e-15, 5.42497807e-06, 4.45813929e-03]) >>> plt.plot(x, iw) The input quantiles can be any shape of array, as long as the last axis labels the components. """ def __init__(self, seed=None): super(invwishart_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__, wishart_docdict_params) def __call__(self, df=None, scale=None, seed=None): """ Create a frozen inverse Wishart distribution. See `invwishart_frozen` for more information. """ return invwishart_frozen(df, scale, seed) def _logpdf(self, x, dim, df, scale, log_det_scale): """ Parameters ---------- x : ndarray Points at which to evaluate the log of the probability density function. dim : int Dimension of the scale matrix df : int Degrees of freedom scale : ndarray Scale matrix log_det_scale : float Logarithm of the determinant of the scale matrix Notes ----- As this function does no argument checking, it should not be called directly; use 'logpdf' instead. """ log_det_x = np.zeros(x.shape[-1]) #scale_x_inv = np.zeros(x.shape) x_inv = np.copy(x).T if dim > 1: _cho_inv_batch(x_inv) # works in-place else: x_inv = 1./x_inv tr_scale_x_inv = np.zeros(x.shape[-1]) for i in range(x.shape[-1]): C, lower = scipy.linalg.cho_factor(x[:,:,i], lower=True) log_det_x[i] = 2 * np.sum(np.log(C.diagonal())) #scale_x_inv[:,:,i] = scipy.linalg.cho_solve((C, True), scale).T tr_scale_x_inv[i] = np.dot(scale, x_inv[i]).trace() # Log PDF out = ((0.5 * df * log_det_scale - 0.5 * tr_scale_x_inv) - (0.5 * df * dim * _LOG_2 + 0.5 * (df + dim + 1) * log_det_x) - multigammaln(0.5*df, dim)) return out def logpdf(self, x, df, scale): """ Log of the inverse Wishart probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. Each quantile must be a symmetric positive definite matrix. %(_doc_default_callparams)s Returns ------- pdf : ndarray Log of the probability density function evaluated at `x` Notes ----- %(_doc_callparams_note)s """ dim, df, scale = self._process_parameters(df, scale) x = self._process_quantiles(x, dim) _, log_det_scale = self._cholesky_logdet(scale) out = self._logpdf(x, dim, df, scale, log_det_scale) return _squeeze_output(out) def pdf(self, x, df, scale): """ Inverse Wishart probability density function. Parameters ---------- x : array_like Quantiles, with the last axis of `x` denoting the components. Each quantile must be a symmetric positive definite matrix. %(_doc_default_callparams)s Returns ------- pdf : ndarray Probability density function evaluated at `x` Notes ----- %(_doc_callparams_note)s """ return np.exp(self.logpdf(x, df, scale)) def _mean(self, dim, df, scale): """ Parameters ---------- dim : int Dimension of the scale matrix %(_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'mean' instead. """ if df > dim + 1: out = scale / (df - dim - 1) else: out = None return out def mean(self, df, scale): """ Mean of the inverse Wishart distribution Only valid if the degrees of freedom are greater than the dimension of the scale matrix plus one. Parameters ---------- %(_doc_default_callparams)s Returns ------- mean : float or None The mean of the distribution """ dim, df, scale = self._process_parameters(df, scale) out = self._mean(dim, df, scale) return _squeeze_output(out) if out is not None else out def _mode(self, dim, df, scale): """ Parameters ---------- dim : int Dimension of the scale matrix %(_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'mode' instead. """ return scale / (df + dim + 1) def mode(self, df, scale): """ Mode of the inverse Wishart distribution Parameters ---------- %(_doc_default_callparams)s Returns ------- mode : float The Mode of the distribution """ dim, df, scale = self._process_parameters(df, scale) out = self._mode(dim, df, scale) return _squeeze_output(out) def _var(self, dim, df, scale): """ Parameters ---------- dim : int Dimension of the scale matrix %(_doc_default_callparams)s Notes ----- As this function does no argument checking, it should not be called directly; use 'var' instead. """ if df > dim + 3: var = (df - dim + 1) * scale**2 diag = scale.diagonal() # 1 x dim array var += (df - dim - 1) * np.outer(diag, diag) var /= (df - dim) * (df - dim - 1)**2 * (df - dim - 3) else: var = None return var def var(self, df, scale): """ Variance of the inverse Wishart distribution Only valid if the degrees of freedom are greater than the dimension of the scale matrix plus three. Parameters ---------- %(_doc_default_callparams)s Returns ------- var : float The variance of the distribution """ dim, df, scale = self._process_parameters(df, scale) out = self._var(dim, df, scale) return _squeeze_output(out) if out is not None else out def _rvs(self, n, shape, dim, df, C, random_state): """ Parameters ---------- n : integer Number of variates to generate shape : iterable Shape of the variates to generate dim : int Dimension of the scale matrix df : int Degrees of freedom C : ndarray Cholesky factorization of the scale matrix, lower triagular. %(_doc_random_state)s Notes ----- As this function does no argument checking, it should not be called directly; use 'rvs' instead. """ random_state = self._get_random_state(random_state) # Get random draws A such that A ~ W(df, I) A = super(invwishart_gen, self)._standard_rvs(n, shape, dim, df, random_state) # Calculate SA = (CA)'^{-1} (CA)^{-1} ~ iW(df, scale) eye = np.eye(dim) trtrs = get_lapack_funcs(('trtrs'), (A,)) for index in np.ndindex(A.shape[:-2]): # Calculate CA CA = np.dot(C, A[index]) # Get (C A)^{-1} via triangular solver if dim > 1: CA, info = trtrs(CA, eye, lower=True) if info > 0: raise LinAlgError("Singular matrix.") if info < 0: raise ValueError('Illegal value in %d-th argument of' ' internal trtrs' % -info) else: CA = 1. / CA # Get SA A[index] = np.dot(CA.T, CA) return A def rvs(self, df, scale, size=1, random_state=None): """ Draw random samples from an inverse Wishart distribution. Parameters ---------- %(_doc_default_callparams)s size : integer or iterable of integers, optional Number of samples to draw (default 1). %(_doc_random_state)s Returns ------- rvs : ndarray Random variates of shape (`size`) + (`dim`, `dim), where `dim` is the dimension of the scale matrix. Notes ----- %(_doc_callparams_note)s """ n, shape = self._process_size(size) dim, df, scale = self._process_parameters(df, scale) # Invert the scale eye = np.eye(dim) L, lower = scipy.linalg.cho_factor(scale, lower=True) inv_scale = scipy.linalg.cho_solve((L, lower), eye) # Cholesky decomposition of inverted scale C = scipy.linalg.cholesky(inv_scale, lower=True) out = self._rvs(n, shape, dim, df, C, random_state) return _squeeze_output(out) def entropy(self): # Need to find reference for inverse Wishart entropy raise AttributeError invwishart = invwishart_gen() class invwishart_frozen(multi_rv_frozen): def __init__(self, df, scale, seed=None): """ Create a frozen inverse Wishart distribution. Parameters ---------- df : array_like Degrees of freedom of the distribution scale : array_like Scale matrix of the distribution seed : None or int or np.random.RandomState instance, optional This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance Default is None. """ self._dist = invwishart_gen(seed) self.dim, self.df, self.scale = self._dist._process_parameters( df, scale ) # Get the determinant via Cholesky factorization C, lower = scipy.linalg.cho_factor(self.scale, lower=True) self.log_det_scale = 2 * np.sum(np.log(C.diagonal())) # Get the inverse using the Cholesky factorization eye = np.eye(self.dim) self.inv_scale = scipy.linalg.cho_solve((C, lower), eye) # Get the Cholesky factorization of the inverse scale self.C = scipy.linalg.cholesky(self.inv_scale, lower=True) def logpdf(self, x): x = self._dist._process_quantiles(x, self.dim) out = self._dist._logpdf(x, self.dim, self.df, self.scale, self.log_det_scale) return _squeeze_output(out) def pdf(self, x): return np.exp(self.logpdf(x)) def mean(self): out = self._dist._mean(self.dim, self.df, self.scale) return _squeeze_output(out) if out is not None else out def mode(self): out = self._dist._mode(self.dim, self.df, self.scale) return _squeeze_output(out) def var(self): out = self._dist._var(self.dim, self.df, self.scale) return _squeeze_output(out) if out is not None else out def rvs(self, size=1, random_state=None): n, shape = self._dist._process_size(size) out = self._dist._rvs(n, shape, self.dim, self.df, self.C, random_state) return _squeeze_output(out) def entropy(self): # Need to find reference for inverse Wishart entropy raise AttributeError # Set frozen generator docstrings from corresponding docstrings in # inverse Wishart and fill in default strings in class docstrings for name in ['logpdf', 'pdf', 'mean', 'mode', 'var', 'rvs']: method = invwishart_gen.__dict__[name] method_frozen = wishart_frozen.__dict__[name] method_frozen.__doc__ = doccer.docformat( method.__doc__, wishart_docdict_noparams) method.__doc__ = doccer.docformat(method.__doc__, wishart_docdict_params) class special_ortho_group_gen(multi_rv_generic): r""" A matrix-valued SO(N) random variable. Return a random rotation matrix, drawn from the Haar distribution (the only uniform distribution on SO(n)). The `dim` keyword specifies the dimension N. Methods ------- ``rvs(dim=None, size=1, random_state=None)`` Draw random samples from SO(N). Parameters ---------- dim : scalar Dimension of matrices Notes ---------- This class is wrapping the random_rot code from the MDP Toolkit, https://github.com/mdp-toolkit/mdp-toolkit Return a random rotation matrix, drawn from the Haar distribution (the only uniform distribution on SO(n)). The algorithm is described in the paper Stewart, G.W., "The efficient generation of random orthogonal matrices with an application to condition estimators", SIAM Journal on Numerical Analysis, 17(3), pp. 403-409, 1980. For more information see http://en.wikipedia.org/wiki/Orthogonal_matrix#Randomization See also the similar `ortho_group`. Examples -------- >>> from scipy.stats import special_ortho_group >>> x = special_ortho_group.rvs(3) >>> np.dot(x, x.T) array([[ 1.00000000e+00, 1.13231364e-17, -2.86852790e-16], [ 1.13231364e-17, 1.00000000e+00, -1.46845020e-16], [ -2.86852790e-16, -1.46845020e-16, 1.00000000e+00]]) >>> import scipy.linalg >>> scipy.linalg.det(x) 1.0 This generates one random matrix from SO(3). It is orthogonal and has a determinant of 1. """ def __init__(self, seed=None): super(special_ortho_group_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__) def __call__(self, dim=None, seed=None): """ Create a frozen SO(N) distribution. See `special_ortho_group_frozen` for more information. """ return special_ortho_group_frozen(dim, seed=seed) def _process_parameters(self, dim): """ Dimension N must be specified; it cannot be inferred. """ if dim is None or not np.isscalar(dim) or dim <= 1 or dim != int(dim): raise ValueError("""Dimension of rotation must be specified, and must be a scalar greater than 1.""") return dim def rvs(self, dim, size=1, random_state=None): """ Draw random samples from SO(N). Parameters ---------- dim : integer Dimension of rotation space (N). size : integer, optional Number of samples to draw (default 1). Returns ------- rvs : ndarray or scalar Random size N-dimensional matrices, dimension (size, dim, dim) """ size = int(size) if size > 1: return np.array([self.rvs(dim, size=1, random_state=random_state) for i in range(size)]) dim = self._process_parameters(dim) random_state = self._get_random_state(random_state) H = np.eye(dim) D = np.ones((dim,)) for n in range(1, dim): x = random_state.normal(size=(dim-n+1,)) D[n-1] = np.sign(x[0]) x[0] -= D[n-1]*np.sqrt((x*x).sum()) # Householder transformation Hx = (np.eye(dim-n+1) - 2.*np.outer(x, x)/(x*x).sum()) mat = np.eye(dim) mat[n-1:, n-1:] = Hx H = np.dot(H, mat) # Fix the last sign such that the determinant is 1 D[-1] = (-1)**(1-(dim % 2))*D.prod() # Equivalent to np.dot(np.diag(D), H) but faster, apparently H = (D*H.T).T return H special_ortho_group = special_ortho_group_gen() class special_ortho_group_frozen(multi_rv_frozen): def __init__(self, dim=None, seed=None): """ Create a frozen SO(N) distribution. Parameters ---------- dim : scalar Dimension of matrices seed : None or int or np.random.RandomState instance, optional This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance Default is None. Examples -------- >>> from scipy.stats import special_ortho_group >>> g = special_ortho_group(5) >>> x = g.rvs() """ self._dist = special_ortho_group_gen(seed) self.dim = self._dist._process_parameters(dim) def rvs(self, size=1, random_state=None): return self._dist.rvs(self.dim, size, random_state) class ortho_group_gen(multi_rv_generic): r""" A matrix-valued O(N) random variable. Return a random orthogonal matrix, drawn from the O(N) Haar distribution (the only uniform distribution on O(N)). The `dim` keyword specifies the dimension N. Methods ------- ``rvs(dim=None, size=1, random_state=None)`` Draw random samples from O(N). Parameters ---------- dim : scalar Dimension of matrices Notes ---------- This class is closely related to `special_ortho_group`. Some care is taken to avoid numerical error, as per the paper by Mezzadri. References ---------- .. [1] F. Mezzadri, "How to generate random matrices from the classical compact groups", arXiv:math-ph/0609050v2. Examples -------- >>> from scipy.stats import ortho_group >>> x = ortho_group.rvs(3) >>> np.dot(x, x.T) array([[ 1.00000000e+00, 1.13231364e-17, -2.86852790e-16], [ 1.13231364e-17, 1.00000000e+00, -1.46845020e-16], [ -2.86852790e-16, -1.46845020e-16, 1.00000000e+00]]) >>> import scipy.linalg >>> np.fabs(scipy.linalg.det(x)) 1.0 This generates one random matrix from O(3). It is orthogonal and has a determinant of +1 or -1. """ def __init__(self, seed=None): super(ortho_group_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__) def _process_parameters(self, dim): """ Dimension N must be specified; it cannot be inferred. """ if dim is None or not np.isscalar(dim) or dim <= 1 or dim != int(dim): raise ValueError("Dimension of rotation must be specified," "and must be a scalar greater than 1.") return dim def rvs(self, dim, size=1, random_state=None): """ Draw random samples from O(N). Parameters ---------- dim : integer Dimension of rotation space (N). size : integer, optional Number of samples to draw (default 1). Returns ------- rvs : ndarray or scalar Random size N-dimensional matrices, dimension (size, dim, dim) """ size = int(size) if size > 1: return np.array([self.rvs(dim, size=1, random_state=random_state) for i in range(size)]) dim = self._process_parameters(dim) random_state = self._get_random_state(random_state) H = np.eye(dim) for n in range(1, dim): x = random_state.normal(size=(dim-n+1,)) # random sign, 50/50, but chosen carefully to avoid roundoff error D = np.sign(x[0]) x[0] += D*np.sqrt((x*x).sum()) # Householder transformation Hx = -D*(np.eye(dim-n+1) - 2.*np.outer(x, x)/(x*x).sum()) mat = np.eye(dim) mat[n-1:, n-1:] = Hx H = np.dot(H, mat) return H ortho_group = ortho_group_gen() class random_correlation_gen(multi_rv_generic): r""" A random correlation matrix. Return a random correlation matrix, given a vector of eigenvalues. The `eigs` keyword specifies the eigenvalues of the correlation matrix, and implies the dimension. Methods ------- ``rvs(eigs=None, random_state=None)`` Draw random correlation matrices, all with eigenvalues eigs. Parameters ---------- eigs : 1d ndarray Eigenvalues of correlation matrix. Notes ---------- Generates a random correlation matrix following a numerically stable algorithm spelled out by Davies & Higham. This algorithm uses a single O(N) similarity transformation to construct a symmetric positive semi-definite matrix, and applies a series of Givens rotations to scale it to have ones on the diagonal. References ---------- .. [1] Davies, Philip I; Higham, Nicholas J; "Numerically stable generation of correlation matrices and their factors", BIT 2000, Vol. 40, No. 4, pp. 640 651 Examples -------- >>> from scipy.stats import random_correlation >>> np.random.seed(514) >>> x = random_correlation.rvs((.5, .8, 1.2, 1.5)) >>> x array([[ 1. , -0.20387311, 0.18366501, -0.04953711], [-0.20387311, 1. , -0.24351129, 0.06703474], [ 0.18366501, -0.24351129, 1. , 0.38530195], [-0.04953711, 0.06703474, 0.38530195, 1. ]]) >>> import scipy.linalg >>> e, v = scipy.linalg.eigh(x) >>> e array([ 0.5, 0.8, 1.2, 1.5]) """ def __init__(self, seed=None): super(random_correlation_gen, self).__init__(seed) self.__doc__ = doccer.docformat(self.__doc__) def _process_parameters(self, eigs, tol): eigs = np.asarray(eigs, dtype=float) dim = eigs.size if eigs.ndim != 1 or eigs.shape[0] != dim or dim <= 1: raise ValueError("Array 'eigs' must be a vector of length greater than 1.") if np.fabs(np.sum(eigs) - dim) > tol: raise ValueError("Sum of eigenvalues must equal dimensionality.") for x in eigs: if x < -tol: raise ValueError("All eigenvalues must be non-negative.") return dim, eigs def _givens_to_1(self, aii, ajj, aij): """Computes a 2x2 Givens matrix to put 1's on the diagonal for the input matrix. The input matrix is a 2x2 symmetric matrix M = [ aii aij ; aij ajj ]. The output matrix g is a 2x2 anti-symmetric matrix of the form [ c s ; -s c ]; the elements c and s are returned. Applying the output matrix to the input matrix (as b=g.T M g) results in a matrix with bii=1, provided tr(M) - det(M) >= 1 and floating point issues do not occur. Otherwise, some other valid rotation is returned. When tr(M)==2, also bjj=1. """ aiid = aii - 1. ajjd = ajj - 1. if ajjd == 0: # ajj==1, so swap aii and ajj to avoid division by zero return 0., 1. dd = math.sqrt(max(aij**2 - aiid*ajjd, 0)) # The choice of t should be chosen to avoid cancellation [1] t = (aij + math.copysign(dd, aij)) / ajjd c = 1. / math.sqrt(1. + t*t) if c == 0: # Underflow s = 1.0 else: s = c*t return c, s def _to_corr(self, m): """ Given a psd matrix m, rotate to put one's on the diagonal, turning it into a correlation matrix. This also requires the trace equal the dimensionality. Note: modifies input matrix """ # Check requirements for in-place Givens if not (m.flags.c_contiguous and m.dtype == np.float64 and m.shape[0] == m.shape[1]): raise ValueError() d = m.shape[0] for i in range(d-1): if m[i,i] == 1: continue elif m[i, i] > 1: for j in range(i+1, d): if m[j, j] < 1: break else: for j in range(i+1, d): if m[j, j] > 1: break c, s = self._givens_to_1(m[i,i], m[j,j], m[i,j]) # Use BLAS to apply Givens rotations in-place. Equivalent to: # g = np.eye(d) # g[i, i] = g[j,j] = c # g[j, i] = -s; g[i, j] = s # m = np.dot(g.T, np.dot(m, g)) mv = m.ravel() drot(mv, mv, c, -s, n=d, offx=i*d, incx=1, offy=j*d, incy=1, overwrite_x=True, overwrite_y=True) drot(mv, mv, c, -s, n=d, offx=i, incx=d, offy=j, incy=d, overwrite_x=True, overwrite_y=True) return m def rvs(self, eigs, random_state=None, tol=1e-13, diag_tol=1e-7): """ Draw random correlation matrices Parameters ---------- eigs : 1d ndarray Eigenvalues of correlation matrix tol : float, optional Tolerance for input parameter checks diag_tol : float, optional Tolerance for deviation of the diagonal of the resulting matrix. Default: 1e-7 Raises ------ RuntimeError Floating point error prevented generating a valid correlation matrix. Returns ------- rvs : ndarray or scalar Random size N-dimensional matrices, dimension (size, dim, dim), each having eigenvalues eigs. """ dim, eigs = self._process_parameters(eigs, tol=tol) random_state = self._get_random_state(random_state) m = ortho_group.rvs(dim, random_state=random_state) m = np.dot(np.dot(m, np.diag(eigs)), m.T) # Set the trace of m m = self._to_corr(m) # Carefully rotate to unit diagonal # Check diagonal if abs(m.diagonal() - 1).max() > diag_tol: raise RuntimeError("Failed to generate a valid correlation matrix") return m random_correlation = random_correlation_gen()
gpl-3.0
jorge2703/scikit-learn
sklearn/ensemble/gradient_boosting.py
126
65552
"""Gradient Boosted Regression Trees This module contains methods for fitting gradient boosted regression trees for both classification and regression. The module structure is the following: - The ``BaseGradientBoosting`` base class implements a common ``fit`` method for all the estimators in the module. Regression and classification only differ in the concrete ``LossFunction`` used. - ``GradientBoostingClassifier`` implements gradient boosting for classification problems. - ``GradientBoostingRegressor`` implements gradient boosting for regression problems. """ # Authors: Peter Prettenhofer, Scott White, Gilles Louppe, Emanuele Olivetti, # Arnaud Joly # License: BSD 3 clause from __future__ import print_function from __future__ import division from abc import ABCMeta, abstractmethod from time import time import numbers import numpy as np from scipy import stats from .base import BaseEnsemble from ..base import BaseEstimator from ..base import ClassifierMixin from ..base import RegressorMixin from ..utils import check_random_state, check_array, check_X_y, column_or_1d from ..utils import check_consistent_length, deprecated from ..utils.extmath import logsumexp from ..utils.fixes import expit, bincount from ..utils.stats import _weighted_percentile from ..utils.validation import check_is_fitted, NotFittedError from ..externals import six from ..feature_selection.from_model import _LearntSelectorMixin from ..tree.tree import DecisionTreeRegressor from ..tree._tree import DTYPE, TREE_LEAF from ..tree._tree import PresortBestSplitter from ..tree._tree import FriedmanMSE from ._gradient_boosting import predict_stages from ._gradient_boosting import predict_stage from ._gradient_boosting import _random_sample_mask class QuantileEstimator(BaseEstimator): """An estimator predicting the alpha-quantile of the training targets.""" def __init__(self, alpha=0.9): if not 0 < alpha < 1.0: raise ValueError("`alpha` must be in (0, 1.0) but was %r" % alpha) self.alpha = alpha def fit(self, X, y, sample_weight=None): if sample_weight is None: self.quantile = stats.scoreatpercentile(y, self.alpha * 100.0) else: self.quantile = _weighted_percentile(y, sample_weight, self.alpha * 100.0) def predict(self, X): check_is_fitted(self, 'quantile') y = np.empty((X.shape[0], 1), dtype=np.float64) y.fill(self.quantile) return y class MeanEstimator(BaseEstimator): """An estimator predicting the mean of the training targets.""" def fit(self, X, y, sample_weight=None): if sample_weight is None: self.mean = np.mean(y) else: self.mean = np.average(y, weights=sample_weight) def predict(self, X): check_is_fitted(self, 'mean') y = np.empty((X.shape[0], 1), dtype=np.float64) y.fill(self.mean) return y class LogOddsEstimator(BaseEstimator): """An estimator predicting the log odds ratio.""" scale = 1.0 def fit(self, X, y, sample_weight=None): # pre-cond: pos, neg are encoded as 1, 0 if sample_weight is None: pos = np.sum(y) neg = y.shape[0] - pos else: pos = np.sum(sample_weight * y) neg = np.sum(sample_weight * (1 - y)) if neg == 0 or pos == 0: raise ValueError('y contains non binary labels.') self.prior = self.scale * np.log(pos / neg) def predict(self, X): check_is_fitted(self, 'prior') y = np.empty((X.shape[0], 1), dtype=np.float64) y.fill(self.prior) return y class ScaledLogOddsEstimator(LogOddsEstimator): """Log odds ratio scaled by 0.5 -- for exponential loss. """ scale = 0.5 class PriorProbabilityEstimator(BaseEstimator): """An estimator predicting the probability of each class in the training data. """ def fit(self, X, y, sample_weight=None): if sample_weight is None: sample_weight = np.ones_like(y, dtype=np.float64) class_counts = bincount(y, weights=sample_weight) self.priors = class_counts / class_counts.sum() def predict(self, X): check_is_fitted(self, 'priors') y = np.empty((X.shape[0], self.priors.shape[0]), dtype=np.float64) y[:] = self.priors return y class ZeroEstimator(BaseEstimator): """An estimator that simply predicts zero. """ def fit(self, X, y, sample_weight=None): if np.issubdtype(y.dtype, int): # classification self.n_classes = np.unique(y).shape[0] if self.n_classes == 2: self.n_classes = 1 else: # regression self.n_classes = 1 def predict(self, X): check_is_fitted(self, 'n_classes') y = np.empty((X.shape[0], self.n_classes), dtype=np.float64) y.fill(0.0) return y class LossFunction(six.with_metaclass(ABCMeta, object)): """Abstract base class for various loss functions. Attributes ---------- K : int The number of regression trees to be induced; 1 for regression and binary classification; ``n_classes`` for multi-class classification. """ is_multi_class = False def __init__(self, n_classes): self.K = n_classes def init_estimator(self): """Default ``init`` estimator for loss function. """ raise NotImplementedError() @abstractmethod def __call__(self, y, pred, sample_weight=None): """Compute the loss of prediction ``pred`` and ``y``. """ @abstractmethod def negative_gradient(self, y, y_pred, **kargs): """Compute the negative gradient. Parameters --------- y : np.ndarray, shape=(n,) The target labels. y_pred : np.ndarray, shape=(n,): The predictions. """ def update_terminal_regions(self, tree, X, y, residual, y_pred, sample_weight, sample_mask, learning_rate=1.0, k=0): """Update the terminal regions (=leaves) of the given tree and updates the current predictions of the model. Traverses tree and invokes template method `_update_terminal_region`. Parameters ---------- tree : tree.Tree The tree object. X : ndarray, shape=(n, m) The data array. y : ndarray, shape=(n,) The target labels. residual : ndarray, shape=(n,) The residuals (usually the negative gradient). y_pred : ndarray, shape=(n,) The predictions. sample_weight : ndarray, shape=(n,) The weight of each sample. sample_mask : ndarray, shape=(n,) The sample mask to be used. learning_rate : float, default=0.1 learning rate shrinks the contribution of each tree by ``learning_rate``. k : int, default 0 The index of the estimator being updated. """ # compute leaf for each sample in ``X``. terminal_regions = tree.apply(X) # mask all which are not in sample mask. masked_terminal_regions = terminal_regions.copy() masked_terminal_regions[~sample_mask] = -1 # update each leaf (= perform line search) for leaf in np.where(tree.children_left == TREE_LEAF)[0]: self._update_terminal_region(tree, masked_terminal_regions, leaf, X, y, residual, y_pred[:, k], sample_weight) # update predictions (both in-bag and out-of-bag) y_pred[:, k] += (learning_rate * tree.value[:, 0, 0].take(terminal_regions, axis=0)) @abstractmethod def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): """Template method for updating terminal regions (=leaves). """ class RegressionLossFunction(six.with_metaclass(ABCMeta, LossFunction)): """Base class for regression loss functions. """ def __init__(self, n_classes): if n_classes != 1: raise ValueError("``n_classes`` must be 1 for regression but " "was %r" % n_classes) super(RegressionLossFunction, self).__init__(n_classes) class LeastSquaresError(RegressionLossFunction): """Loss function for least squares (LS) estimation. Terminal regions need not to be updated for least squares. """ def init_estimator(self): return MeanEstimator() def __call__(self, y, pred, sample_weight=None): if sample_weight is None: return np.mean((y - pred.ravel()) ** 2.0) else: return (1.0 / sample_weight.sum() * np.sum(sample_weight * ((y - pred.ravel()) ** 2.0))) def negative_gradient(self, y, pred, **kargs): return y - pred.ravel() def update_terminal_regions(self, tree, X, y, residual, y_pred, sample_weight, sample_mask, learning_rate=1.0, k=0): """Least squares does not need to update terminal regions. But it has to update the predictions. """ # update predictions y_pred[:, k] += learning_rate * tree.predict(X).ravel() def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): pass class LeastAbsoluteError(RegressionLossFunction): """Loss function for least absolute deviation (LAD) regression. """ def init_estimator(self): return QuantileEstimator(alpha=0.5) def __call__(self, y, pred, sample_weight=None): if sample_weight is None: return np.abs(y - pred.ravel()).mean() else: return (1.0 / sample_weight.sum() * np.sum(sample_weight * np.abs(y - pred.ravel()))) def negative_gradient(self, y, pred, **kargs): """1.0 if y - pred > 0.0 else -1.0""" pred = pred.ravel() return 2.0 * (y - pred > 0.0) - 1.0 def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): """LAD updates terminal regions to median estimates. """ terminal_region = np.where(terminal_regions == leaf)[0] sample_weight = sample_weight.take(terminal_region, axis=0) diff = y.take(terminal_region, axis=0) - pred.take(terminal_region, axis=0) tree.value[leaf, 0, 0] = _weighted_percentile(diff, sample_weight, percentile=50) class HuberLossFunction(RegressionLossFunction): """Huber loss function for robust regression. M-Regression proposed in Friedman 2001. References ---------- J. Friedman, Greedy Function Approximation: A Gradient Boosting Machine, The Annals of Statistics, Vol. 29, No. 5, 2001. """ def __init__(self, n_classes, alpha=0.9): super(HuberLossFunction, self).__init__(n_classes) self.alpha = alpha self.gamma = None def init_estimator(self): return QuantileEstimator(alpha=0.5) def __call__(self, y, pred, sample_weight=None): pred = pred.ravel() diff = y - pred gamma = self.gamma if gamma is None: if sample_weight is None: gamma = stats.scoreatpercentile(np.abs(diff), self.alpha * 100) else: gamma = _weighted_percentile(np.abs(diff), sample_weight, self.alpha * 100) gamma_mask = np.abs(diff) <= gamma if sample_weight is None: sq_loss = np.sum(0.5 * diff[gamma_mask] ** 2.0) lin_loss = np.sum(gamma * (np.abs(diff[~gamma_mask]) - gamma / 2.0)) loss = (sq_loss + lin_loss) / y.shape[0] else: sq_loss = np.sum(0.5 * sample_weight[gamma_mask] * diff[gamma_mask] ** 2.0) lin_loss = np.sum(gamma * sample_weight[~gamma_mask] * (np.abs(diff[~gamma_mask]) - gamma / 2.0)) loss = (sq_loss + lin_loss) / sample_weight.sum() return loss def negative_gradient(self, y, pred, sample_weight=None, **kargs): pred = pred.ravel() diff = y - pred if sample_weight is None: gamma = stats.scoreatpercentile(np.abs(diff), self.alpha * 100) else: gamma = _weighted_percentile(np.abs(diff), sample_weight, self.alpha * 100) gamma_mask = np.abs(diff) <= gamma residual = np.zeros((y.shape[0],), dtype=np.float64) residual[gamma_mask] = diff[gamma_mask] residual[~gamma_mask] = gamma * np.sign(diff[~gamma_mask]) self.gamma = gamma return residual def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): terminal_region = np.where(terminal_regions == leaf)[0] sample_weight = sample_weight.take(terminal_region, axis=0) gamma = self.gamma diff = (y.take(terminal_region, axis=0) - pred.take(terminal_region, axis=0)) median = _weighted_percentile(diff, sample_weight, percentile=50) diff_minus_median = diff - median tree.value[leaf, 0] = median + np.mean( np.sign(diff_minus_median) * np.minimum(np.abs(diff_minus_median), gamma)) class QuantileLossFunction(RegressionLossFunction): """Loss function for quantile regression. Quantile regression allows to estimate the percentiles of the conditional distribution of the target. """ def __init__(self, n_classes, alpha=0.9): super(QuantileLossFunction, self).__init__(n_classes) assert 0 < alpha < 1.0 self.alpha = alpha self.percentile = alpha * 100.0 def init_estimator(self): return QuantileEstimator(self.alpha) def __call__(self, y, pred, sample_weight=None): pred = pred.ravel() diff = y - pred alpha = self.alpha mask = y > pred if sample_weight is None: loss = (alpha * diff[mask].sum() + (1.0 - alpha) * diff[~mask].sum()) / y.shape[0] else: loss = ((alpha * np.sum(sample_weight[mask] * diff[mask]) + (1.0 - alpha) * np.sum(sample_weight[~mask] * diff[~mask])) / sample_weight.sum()) return loss def negative_gradient(self, y, pred, **kargs): alpha = self.alpha pred = pred.ravel() mask = y > pred return (alpha * mask) - ((1.0 - alpha) * ~mask) def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): terminal_region = np.where(terminal_regions == leaf)[0] diff = (y.take(terminal_region, axis=0) - pred.take(terminal_region, axis=0)) sample_weight = sample_weight.take(terminal_region, axis=0) val = _weighted_percentile(diff, sample_weight, self.percentile) tree.value[leaf, 0] = val class ClassificationLossFunction(six.with_metaclass(ABCMeta, LossFunction)): """Base class for classification loss functions. """ def _score_to_proba(self, score): """Template method to convert scores to probabilities. the does not support probabilites raises AttributeError. """ raise TypeError('%s does not support predict_proba' % type(self).__name__) @abstractmethod def _score_to_decision(self, score): """Template method to convert scores to decisions. Returns int arrays. """ class BinomialDeviance(ClassificationLossFunction): """Binomial deviance loss function for binary classification. Binary classification is a special case; here, we only need to fit one tree instead of ``n_classes`` trees. """ def __init__(self, n_classes): if n_classes != 2: raise ValueError("{0:s} requires 2 classes.".format( self.__class__.__name__)) # we only need to fit one tree for binary clf. super(BinomialDeviance, self).__init__(1) def init_estimator(self): return LogOddsEstimator() def __call__(self, y, pred, sample_weight=None): """Compute the deviance (= 2 * negative log-likelihood). """ # logaddexp(0, v) == log(1.0 + exp(v)) pred = pred.ravel() if sample_weight is None: return -2.0 * np.mean((y * pred) - np.logaddexp(0.0, pred)) else: return (-2.0 / sample_weight.sum() * np.sum(sample_weight * ((y * pred) - np.logaddexp(0.0, pred)))) def negative_gradient(self, y, pred, **kargs): """Compute the residual (= negative gradient). """ return y - expit(pred.ravel()) def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): """Make a single Newton-Raphson step. our node estimate is given by: sum(w * (y - prob)) / sum(w * prob * (1 - prob)) we take advantage that: y - prob = residual """ terminal_region = np.where(terminal_regions == leaf)[0] residual = residual.take(terminal_region, axis=0) y = y.take(terminal_region, axis=0) sample_weight = sample_weight.take(terminal_region, axis=0) numerator = np.sum(sample_weight * residual) denominator = np.sum(sample_weight * (y - residual) * (1 - y + residual)) if denominator == 0.0: tree.value[leaf, 0, 0] = 0.0 else: tree.value[leaf, 0, 0] = numerator / denominator def _score_to_proba(self, score): proba = np.ones((score.shape[0], 2), dtype=np.float64) proba[:, 1] = expit(score.ravel()) proba[:, 0] -= proba[:, 1] return proba def _score_to_decision(self, score): proba = self._score_to_proba(score) return np.argmax(proba, axis=1) class MultinomialDeviance(ClassificationLossFunction): """Multinomial deviance loss function for multi-class classification. For multi-class classification we need to fit ``n_classes`` trees at each stage. """ is_multi_class = True def __init__(self, n_classes): if n_classes < 3: raise ValueError("{0:s} requires more than 2 classes.".format( self.__class__.__name__)) super(MultinomialDeviance, self).__init__(n_classes) def init_estimator(self): return PriorProbabilityEstimator() def __call__(self, y, pred, sample_weight=None): # create one-hot label encoding Y = np.zeros((y.shape[0], self.K), dtype=np.float64) for k in range(self.K): Y[:, k] = y == k if sample_weight is None: return np.sum(-1 * (Y * pred).sum(axis=1) + logsumexp(pred, axis=1)) else: return np.sum(-1 * sample_weight * (Y * pred).sum(axis=1) + logsumexp(pred, axis=1)) def negative_gradient(self, y, pred, k=0, **kwargs): """Compute negative gradient for the ``k``-th class. """ return y - np.nan_to_num(np.exp(pred[:, k] - logsumexp(pred, axis=1))) def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): """Make a single Newton-Raphson step. """ terminal_region = np.where(terminal_regions == leaf)[0] residual = residual.take(terminal_region, axis=0) y = y.take(terminal_region, axis=0) sample_weight = sample_weight.take(terminal_region, axis=0) numerator = np.sum(sample_weight * residual) numerator *= (self.K - 1) / self.K denominator = np.sum(sample_weight * (y - residual) * (1.0 - y + residual)) if denominator == 0.0: tree.value[leaf, 0, 0] = 0.0 else: tree.value[leaf, 0, 0] = numerator / denominator def _score_to_proba(self, score): return np.nan_to_num( np.exp(score - (logsumexp(score, axis=1)[:, np.newaxis]))) def _score_to_decision(self, score): proba = self._score_to_proba(score) return np.argmax(proba, axis=1) class ExponentialLoss(ClassificationLossFunction): """Exponential loss function for binary classification. Same loss as AdaBoost. References ---------- Greg Ridgeway, Generalized Boosted Models: A guide to the gbm package, 2007 """ def __init__(self, n_classes): if n_classes != 2: raise ValueError("{0:s} requires 2 classes.".format( self.__class__.__name__)) # we only need to fit one tree for binary clf. super(ExponentialLoss, self).__init__(1) def init_estimator(self): return ScaledLogOddsEstimator() def __call__(self, y, pred, sample_weight=None): pred = pred.ravel() if sample_weight is None: return np.mean(np.exp(-(2. * y - 1.) * pred)) else: return (1.0 / sample_weight.sum() * np.sum(sample_weight * np.exp(-(2 * y - 1) * pred))) def negative_gradient(self, y, pred, **kargs): y_ = -(2. * y - 1.) return y_ * np.exp(y_ * pred.ravel()) def _update_terminal_region(self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight): terminal_region = np.where(terminal_regions == leaf)[0] pred = pred.take(terminal_region, axis=0) y = y.take(terminal_region, axis=0) sample_weight = sample_weight.take(terminal_region, axis=0) y_ = 2. * y - 1. numerator = np.sum(y_ * sample_weight * np.exp(-y_ * pred)) denominator = np.sum(sample_weight * np.exp(-y_ * pred)) if denominator == 0.0: tree.value[leaf, 0, 0] = 0.0 else: tree.value[leaf, 0, 0] = numerator / denominator def _score_to_proba(self, score): proba = np.ones((score.shape[0], 2), dtype=np.float64) proba[:, 1] = expit(2.0 * score.ravel()) proba[:, 0] -= proba[:, 1] return proba def _score_to_decision(self, score): return (score.ravel() >= 0.0).astype(np.int) LOSS_FUNCTIONS = {'ls': LeastSquaresError, 'lad': LeastAbsoluteError, 'huber': HuberLossFunction, 'quantile': QuantileLossFunction, 'deviance': None, # for both, multinomial and binomial 'exponential': ExponentialLoss, } INIT_ESTIMATORS = {'zero': ZeroEstimator} class VerboseReporter(object): """Reports verbose output to stdout. If ``verbose==1`` output is printed once in a while (when iteration mod verbose_mod is zero).; if larger than 1 then output is printed for each update. """ def __init__(self, verbose): self.verbose = verbose def init(self, est, begin_at_stage=0): # header fields and line format str header_fields = ['Iter', 'Train Loss'] verbose_fmt = ['{iter:>10d}', '{train_score:>16.4f}'] # do oob? if est.subsample < 1: header_fields.append('OOB Improve') verbose_fmt.append('{oob_impr:>16.4f}') header_fields.append('Remaining Time') verbose_fmt.append('{remaining_time:>16s}') # print the header line print(('%10s ' + '%16s ' * (len(header_fields) - 1)) % tuple(header_fields)) self.verbose_fmt = ' '.join(verbose_fmt) # plot verbose info each time i % verbose_mod == 0 self.verbose_mod = 1 self.start_time = time() self.begin_at_stage = begin_at_stage def update(self, j, est): """Update reporter with new iteration. """ do_oob = est.subsample < 1 # we need to take into account if we fit additional estimators. i = j - self.begin_at_stage # iteration relative to the start iter if (i + 1) % self.verbose_mod == 0: oob_impr = est.oob_improvement_[j] if do_oob else 0 remaining_time = ((est.n_estimators - (j + 1)) * (time() - self.start_time) / float(i + 1)) if remaining_time > 60: remaining_time = '{0:.2f}m'.format(remaining_time / 60.0) else: remaining_time = '{0:.2f}s'.format(remaining_time) print(self.verbose_fmt.format(iter=j + 1, train_score=est.train_score_[j], oob_impr=oob_impr, remaining_time=remaining_time)) if self.verbose == 1 and ((i + 1) // (self.verbose_mod * 10) > 0): # adjust verbose frequency (powers of 10) self.verbose_mod *= 10 class BaseGradientBoosting(six.with_metaclass(ABCMeta, BaseEnsemble, _LearntSelectorMixin)): """Abstract base class for Gradient Boosting. """ @abstractmethod def __init__(self, loss, learning_rate, n_estimators, min_samples_split, min_samples_leaf, min_weight_fraction_leaf, max_depth, init, subsample, max_features, random_state, alpha=0.9, verbose=0, max_leaf_nodes=None, warm_start=False): self.n_estimators = n_estimators self.learning_rate = learning_rate self.loss = loss self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.subsample = subsample self.max_features = max_features self.max_depth = max_depth self.init = init self.random_state = random_state self.alpha = alpha self.verbose = verbose self.max_leaf_nodes = max_leaf_nodes self.warm_start = warm_start self.estimators_ = np.empty((0, 0), dtype=np.object) def _fit_stage(self, i, X, y, y_pred, sample_weight, sample_mask, criterion, splitter, random_state): """Fit another stage of ``n_classes_`` trees to the boosting model. """ assert sample_mask.dtype == np.bool loss = self.loss_ original_y = y for k in range(loss.K): if loss.is_multi_class: y = np.array(original_y == k, dtype=np.float64) residual = loss.negative_gradient(y, y_pred, k=k, sample_weight=sample_weight) # induce regression tree on residuals tree = DecisionTreeRegressor( criterion=criterion, splitter=splitter, max_depth=self.max_depth, min_samples_split=self.min_samples_split, min_samples_leaf=self.min_samples_leaf, min_weight_fraction_leaf=self.min_weight_fraction_leaf, max_features=self.max_features, max_leaf_nodes=self.max_leaf_nodes, random_state=random_state) if self.subsample < 1.0: # no inplace multiplication! sample_weight = sample_weight * sample_mask.astype(np.float64) tree.fit(X, residual, sample_weight=sample_weight, check_input=False) # update tree leaves loss.update_terminal_regions(tree.tree_, X, y, residual, y_pred, sample_weight, sample_mask, self.learning_rate, k=k) # add tree to ensemble self.estimators_[i, k] = tree return y_pred def _check_params(self): """Check validity of parameters and raise ValueError if not valid. """ if self.n_estimators <= 0: raise ValueError("n_estimators must be greater than 0 but " "was %r" % self.n_estimators) if self.learning_rate <= 0.0: raise ValueError("learning_rate must be greater than 0 but " "was %r" % self.learning_rate) if (self.loss not in self._SUPPORTED_LOSS or self.loss not in LOSS_FUNCTIONS): raise ValueError("Loss '{0:s}' not supported. ".format(self.loss)) if self.loss == 'deviance': loss_class = (MultinomialDeviance if len(self.classes_) > 2 else BinomialDeviance) else: loss_class = LOSS_FUNCTIONS[self.loss] if self.loss in ('huber', 'quantile'): self.loss_ = loss_class(self.n_classes_, self.alpha) else: self.loss_ = loss_class(self.n_classes_) if not (0.0 < self.subsample <= 1.0): raise ValueError("subsample must be in (0,1] but " "was %r" % self.subsample) if self.init is not None: if isinstance(self.init, six.string_types): if self.init not in INIT_ESTIMATORS: raise ValueError('init="%s" is not supported' % self.init) else: if (not hasattr(self.init, 'fit') or not hasattr(self.init, 'predict')): raise ValueError("init=%r must be valid BaseEstimator " "and support both fit and " "predict" % self.init) if not (0.0 < self.alpha < 1.0): raise ValueError("alpha must be in (0.0, 1.0) but " "was %r" % self.alpha) if isinstance(self.max_features, six.string_types): if self.max_features == "auto": # if is_classification if self.n_classes_ > 1: max_features = max(1, int(np.sqrt(self.n_features))) else: # is regression max_features = self.n_features elif self.max_features == "sqrt": max_features = max(1, int(np.sqrt(self.n_features))) elif self.max_features == "log2": max_features = max(1, int(np.log2(self.n_features))) else: raise ValueError("Invalid value for max_features: %r. " "Allowed string values are 'auto', 'sqrt' " "or 'log2'." % self.max_features) elif self.max_features is None: max_features = self.n_features elif isinstance(self.max_features, (numbers.Integral, np.integer)): max_features = self.max_features else: # float if 0. < self.max_features <= 1.: max_features = max(int(self.max_features * self.n_features), 1) else: raise ValueError("max_features must be in (0, n_features]") self.max_features_ = max_features def _init_state(self): """Initialize model state and allocate model state data structures. """ if self.init is None: self.init_ = self.loss_.init_estimator() elif isinstance(self.init, six.string_types): self.init_ = INIT_ESTIMATORS[self.init]() else: self.init_ = self.init self.estimators_ = np.empty((self.n_estimators, self.loss_.K), dtype=np.object) self.train_score_ = np.zeros((self.n_estimators,), dtype=np.float64) # do oob? if self.subsample < 1.0: self.oob_improvement_ = np.zeros((self.n_estimators), dtype=np.float64) def _clear_state(self): """Clear the state of the gradient boosting model. """ if hasattr(self, 'estimators_'): self.estimators_ = np.empty((0, 0), dtype=np.object) if hasattr(self, 'train_score_'): del self.train_score_ if hasattr(self, 'oob_improvement_'): del self.oob_improvement_ if hasattr(self, 'init_'): del self.init_ def _resize_state(self): """Add additional ``n_estimators`` entries to all attributes. """ # self.n_estimators is the number of additional est to fit total_n_estimators = self.n_estimators if total_n_estimators < self.estimators_.shape[0]: raise ValueError('resize with smaller n_estimators %d < %d' % (total_n_estimators, self.estimators_[0])) self.estimators_.resize((total_n_estimators, self.loss_.K)) self.train_score_.resize(total_n_estimators) if (self.subsample < 1 or hasattr(self, 'oob_improvement_')): # if do oob resize arrays or create new if not available if hasattr(self, 'oob_improvement_'): self.oob_improvement_.resize(total_n_estimators) else: self.oob_improvement_ = np.zeros((total_n_estimators,), dtype=np.float64) def _is_initialized(self): return len(getattr(self, 'estimators_', [])) > 0 def fit(self, X, y, sample_weight=None, monitor=None): """Fit the gradient boosting model. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values (integers in classification, real numbers in regression) For classification, labels must correspond to classes. sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. In the case of classification, splits are also ignored if they would result in any single class carrying a negative weight in either child node. monitor : callable, optional The monitor is called after each iteration with the current iteration, a reference to the estimator and the local variables of ``_fit_stages`` as keyword arguments ``callable(i, self, locals())``. If the callable returns ``True`` the fitting procedure is stopped. The monitor can be used for various things such as computing held-out estimates, early stopping, model introspect, and snapshoting. Returns ------- self : object Returns self. """ # if not warmstart - clear the estimator state if not self.warm_start: self._clear_state() # Check input X, y = check_X_y(X, y, dtype=DTYPE) n_samples, self.n_features = X.shape if sample_weight is None: sample_weight = np.ones(n_samples, dtype=np.float32) else: sample_weight = column_or_1d(sample_weight, warn=True) check_consistent_length(X, y, sample_weight) y = self._validate_y(y) random_state = check_random_state(self.random_state) self._check_params() if not self._is_initialized(): # init state self._init_state() # fit initial model - FIXME make sample_weight optional self.init_.fit(X, y, sample_weight) # init predictions y_pred = self.init_.predict(X) begin_at_stage = 0 else: # add more estimators to fitted model # invariant: warm_start = True if self.n_estimators < self.estimators_.shape[0]: raise ValueError('n_estimators=%d must be larger or equal to ' 'estimators_.shape[0]=%d when ' 'warm_start==True' % (self.n_estimators, self.estimators_.shape[0])) begin_at_stage = self.estimators_.shape[0] y_pred = self._decision_function(X) self._resize_state() # fit the boosting stages n_stages = self._fit_stages(X, y, y_pred, sample_weight, random_state, begin_at_stage, monitor) # change shape of arrays after fit (early-stopping or additional ests) if n_stages != self.estimators_.shape[0]: self.estimators_ = self.estimators_[:n_stages] self.train_score_ = self.train_score_[:n_stages] if hasattr(self, 'oob_improvement_'): self.oob_improvement_ = self.oob_improvement_[:n_stages] return self def _fit_stages(self, X, y, y_pred, sample_weight, random_state, begin_at_stage=0, monitor=None): """Iteratively fits the stages. For each stage it computes the progress (OOB, train score) and delegates to ``_fit_stage``. Returns the number of stages fit; might differ from ``n_estimators`` due to early stopping. """ n_samples = X.shape[0] do_oob = self.subsample < 1.0 sample_mask = np.ones((n_samples, ), dtype=np.bool) n_inbag = max(1, int(self.subsample * n_samples)) loss_ = self.loss_ # Set min_weight_leaf from min_weight_fraction_leaf if self.min_weight_fraction_leaf != 0. and sample_weight is not None: min_weight_leaf = (self.min_weight_fraction_leaf * np.sum(sample_weight)) else: min_weight_leaf = 0. # init criterion and splitter criterion = FriedmanMSE(1) splitter = PresortBestSplitter(criterion, self.max_features_, self.min_samples_leaf, min_weight_leaf, random_state) if self.verbose: verbose_reporter = VerboseReporter(self.verbose) verbose_reporter.init(self, begin_at_stage) # perform boosting iterations i = begin_at_stage for i in range(begin_at_stage, self.n_estimators): # subsampling if do_oob: sample_mask = _random_sample_mask(n_samples, n_inbag, random_state) # OOB score before adding this stage old_oob_score = loss_(y[~sample_mask], y_pred[~sample_mask], sample_weight[~sample_mask]) # fit next stage of trees y_pred = self._fit_stage(i, X, y, y_pred, sample_weight, sample_mask, criterion, splitter, random_state) # track deviance (= loss) if do_oob: self.train_score_[i] = loss_(y[sample_mask], y_pred[sample_mask], sample_weight[sample_mask]) self.oob_improvement_[i] = ( old_oob_score - loss_(y[~sample_mask], y_pred[~sample_mask], sample_weight[~sample_mask])) else: # no need to fancy index w/ no subsampling self.train_score_[i] = loss_(y, y_pred, sample_weight) if self.verbose > 0: verbose_reporter.update(i, self) if monitor is not None: early_stopping = monitor(i, self, locals()) if early_stopping: break return i + 1 def _make_estimator(self, append=True): # we don't need _make_estimator raise NotImplementedError() def _init_decision_function(self, X): """Check input and compute prediction of ``init``. """ if self.estimators_ is None or len(self.estimators_) == 0: raise NotFittedError("Estimator not fitted, call `fit`" " before making predictions`.") if X.shape[1] != self.n_features: raise ValueError("X.shape[1] should be {0:d}, not {1:d}.".format( self.n_features, X.shape[1])) score = self.init_.predict(X).astype(np.float64) return score def _decision_function(self, X): # for use in inner loop, not raveling the output in single-class case, # not doing input validation. score = self._init_decision_function(X) predict_stages(self.estimators_, X, self.learning_rate, score) return score @deprecated(" and will be removed in 0.19") def decision_function(self, X): """Compute the decision function of ``X``. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- score : array, shape = [n_samples, n_classes] or [n_samples] The decision function of the input samples. The order of the classes corresponds to that in the attribute `classes_`. Regression and binary classification produce an array of shape [n_samples]. """ X = check_array(X, dtype=DTYPE, order="C") score = self._decision_function(X) if score.shape[1] == 1: return score.ravel() return score def _staged_decision_function(self, X): """Compute decision function of ``X`` for each iteration. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- score : generator of array, shape = [n_samples, k] The decision function of the input samples. The order of the classes corresponds to that in the attribute `classes_`. Regression and binary classification are special cases with ``k == 1``, otherwise ``k==n_classes``. """ X = check_array(X, dtype=DTYPE, order="C") score = self._init_decision_function(X) for i in range(self.estimators_.shape[0]): predict_stage(self.estimators_, i, X, self.learning_rate, score) yield score.copy() @deprecated(" and will be removed in 0.19") def staged_decision_function(self, X): """Compute decision function of ``X`` for each iteration. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- score : generator of array, shape = [n_samples, k] The decision function of the input samples. The order of the classes corresponds to that in the attribute `classes_`. Regression and binary classification are special cases with ``k == 1``, otherwise ``k==n_classes``. """ for dec in self._staged_decision_function(X): # no yield from in Python2.X yield dec @property def feature_importances_(self): """Return the feature importances (the higher, the more important the feature). Returns ------- feature_importances_ : array, shape = [n_features] """ if self.estimators_ is None or len(self.estimators_) == 0: raise NotFittedError("Estimator not fitted, call `fit` before" " `feature_importances_`.") total_sum = np.zeros((self.n_features, ), dtype=np.float64) for stage in self.estimators_: stage_sum = sum(tree.feature_importances_ for tree in stage) / len(stage) total_sum += stage_sum importances = total_sum / len(self.estimators_) return importances def _validate_y(self, y): self.n_classes_ = 1 if y.dtype.kind == 'O': y = y.astype(np.float64) # Default implementation return y class GradientBoostingClassifier(BaseGradientBoosting, ClassifierMixin): """Gradient Boosting for classification. GB builds an additive model in a forward stage-wise fashion; it allows for the optimization of arbitrary differentiable loss functions. In each stage ``n_classes_`` regression trees are fit on the negative gradient of the binomial or multinomial deviance loss function. Binary classification is a special case where only a single regression tree is induced. Read more in the :ref:`User Guide <gradient_boosting>`. Parameters ---------- loss : {'deviance', 'exponential'}, optional (default='deviance') loss function to be optimized. 'deviance' refers to deviance (= logistic regression) for classification with probabilistic outputs. For loss 'exponential' gradient boosting recovers the AdaBoost algorithm. learning_rate : float, optional (default=0.1) learning rate shrinks the contribution of each tree by `learning_rate`. There is a trade-off between learning_rate and n_estimators. n_estimators : int (default=100) The number of boosting stages to perform. Gradient boosting is fairly robust to over-fitting so a large number usually results in better performance. max_depth : integer, optional (default=3) maximum depth of the individual regression estimators. The maximum depth limits the number of nodes in the tree. Tune this parameter for best performance; the best value depends on the interaction of the input variables. Ignored if ``max_leaf_nodes`` is not None. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. min_samples_leaf : integer, optional (default=1) The minimum number of samples required to be at a leaf node. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. subsample : float, optional (default=1.0) The fraction of samples to be used for fitting the individual base learners. If smaller than 1.0 this results in Stochastic Gradient Boosting. `subsample` interacts with the parameter `n_estimators`. Choosing `subsample < 1.0` leads to a reduction of variance and an increase in bias. max_features : int, float, string or None, optional (default=None) The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=sqrt(n_features)`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Choosing `max_features < n_features` leads to a reduction of variance and an increase in bias. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. If not None then ``max_depth`` will be ignored. init : BaseEstimator, None, optional (default=None) An estimator object that is used to compute the initial predictions. ``init`` has to provide ``fit`` and ``predict``. If None it uses ``loss.init_estimator``. verbose : int, default: 0 Enable verbose output. If 1 then it prints progress and performance once in a while (the more trees the lower the frequency). If greater than 1 then it prints progress and performance for every tree. warm_start : bool, default: False When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just erase the previous solution. 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 ---------- feature_importances_ : array, shape = [n_features] The feature importances (the higher, the more important the feature). oob_improvement_ : array, shape = [n_estimators] The improvement in loss (= deviance) on the out-of-bag samples relative to the previous iteration. ``oob_improvement_[0]`` is the improvement in loss of the first stage over the ``init`` estimator. train_score_ : array, shape = [n_estimators] The i-th score ``train_score_[i]`` is the deviance (= loss) of the model at iteration ``i`` on the in-bag sample. If ``subsample == 1`` this is the deviance on the training data. loss_ : LossFunction The concrete ``LossFunction`` object. init : BaseEstimator The estimator that provides the initial predictions. Set via the ``init`` argument or ``loss.init_estimator``. estimators_ : ndarray of DecisionTreeRegressor, shape = [n_estimators, loss_.K] The collection of fitted sub-estimators. ``loss_.K`` is 1 for binary classification, otherwise n_classes. See also -------- sklearn.tree.DecisionTreeClassifier, RandomForestClassifier AdaBoostClassifier References ---------- J. Friedman, Greedy Function Approximation: A Gradient Boosting Machine, The Annals of Statistics, Vol. 29, No. 5, 2001. J. Friedman, Stochastic Gradient Boosting, 1999 T. Hastie, R. Tibshirani and J. Friedman. Elements of Statistical Learning Ed. 2, Springer, 2009. """ _SUPPORTED_LOSS = ('deviance', 'exponential') def __init__(self, loss='deviance', learning_rate=0.1, n_estimators=100, subsample=1.0, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_depth=3, init=None, random_state=None, max_features=None, verbose=0, max_leaf_nodes=None, warm_start=False): super(GradientBoostingClassifier, self).__init__( loss=loss, learning_rate=learning_rate, n_estimators=n_estimators, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, min_weight_fraction_leaf=min_weight_fraction_leaf, max_depth=max_depth, init=init, subsample=subsample, max_features=max_features, random_state=random_state, verbose=verbose, max_leaf_nodes=max_leaf_nodes, warm_start=warm_start) def _validate_y(self, y): self.classes_, y = np.unique(y, return_inverse=True) self.n_classes_ = len(self.classes_) return y def decision_function(self, X): """Compute the decision function of ``X``. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- score : array, shape = [n_samples, n_classes] or [n_samples] The decision function of the input samples. The order of the classes corresponds to that in the attribute `classes_`. Regression and binary classification produce an array of shape [n_samples]. """ X = check_array(X, dtype=DTYPE, order="C") score = self._decision_function(X) if score.shape[1] == 1: return score.ravel() return score def staged_decision_function(self, X): """Compute decision function of ``X`` for each iteration. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- score : generator of array, shape = [n_samples, k] The decision function of the input samples. The order of the classes corresponds to that in the attribute `classes_`. Regression and binary classification are special cases with ``k == 1``, otherwise ``k==n_classes``. """ for dec in self._staged_decision_function(X): # no yield from in Python2.X yield dec def predict(self, X): """Predict class for X. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- y: array of shape = ["n_samples] The predicted values. """ score = self.decision_function(X) decisions = self.loss_._score_to_decision(score) return self.classes_.take(decisions, axis=0) def staged_predict(self, X): """Predict class at each stage for X. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- y : generator of array of shape = [n_samples] The predicted value of the input samples. """ for score in self._staged_decision_function(X): decisions = self.loss_._score_to_decision(score) yield self.classes_.take(decisions, axis=0) def predict_proba(self, X): """Predict class probabilities for X. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Raises ------ AttributeError If the ``loss`` does not support probabilities. Returns ------- p : array of shape = [n_samples] The class probabilities of the input samples. The order of the classes corresponds to that in the attribute `classes_`. """ score = self.decision_function(X) try: return self.loss_._score_to_proba(score) except NotFittedError: raise except AttributeError: raise AttributeError('loss=%r does not support predict_proba' % self.loss) def predict_log_proba(self, X): """Predict class log-probabilities for X. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Raises ------ AttributeError If the ``loss`` does not support probabilities. Returns ------- p : array of shape = [n_samples] The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute `classes_`. """ proba = self.predict_proba(X) return np.log(proba) def staged_predict_proba(self, X): """Predict class probabilities at each stage for X. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- y : generator of array of shape = [n_samples] The predicted value of the input samples. """ try: for score in self._staged_decision_function(X): yield self.loss_._score_to_proba(score) except NotFittedError: raise except AttributeError: raise AttributeError('loss=%r does not support predict_proba' % self.loss) class GradientBoostingRegressor(BaseGradientBoosting, RegressorMixin): """Gradient Boosting for regression. GB builds an additive model in a forward stage-wise fashion; it allows for the optimization of arbitrary differentiable loss functions. In each stage a regression tree is fit on the negative gradient of the given loss function. Read more in the :ref:`User Guide <gradient_boosting>`. Parameters ---------- loss : {'ls', 'lad', 'huber', 'quantile'}, optional (default='ls') loss function to be optimized. 'ls' refers to least squares regression. 'lad' (least absolute deviation) is a highly robust loss function solely based on order information of the input variables. 'huber' is a combination of the two. 'quantile' allows quantile regression (use `alpha` to specify the quantile). learning_rate : float, optional (default=0.1) learning rate shrinks the contribution of each tree by `learning_rate`. There is a trade-off between learning_rate and n_estimators. n_estimators : int (default=100) The number of boosting stages to perform. Gradient boosting is fairly robust to over-fitting so a large number usually results in better performance. max_depth : integer, optional (default=3) maximum depth of the individual regression estimators. The maximum depth limits the number of nodes in the tree. Tune this parameter for best performance; the best value depends on the interaction of the input variables. Ignored if ``max_leaf_nodes`` is not None. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. min_samples_leaf : integer, optional (default=1) The minimum number of samples required to be at a leaf node. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. subsample : float, optional (default=1.0) The fraction of samples to be used for fitting the individual base learners. If smaller than 1.0 this results in Stochastic Gradient Boosting. `subsample` interacts with the parameter `n_estimators`. Choosing `subsample < 1.0` leads to a reduction of variance and an increase in bias. max_features : int, float, string or None, optional (default=None) The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=n_features`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Choosing `max_features < n_features` leads to a reduction of variance and an increase in bias. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. alpha : float (default=0.9) The alpha-quantile of the huber loss function and the quantile loss function. Only if ``loss='huber'`` or ``loss='quantile'``. init : BaseEstimator, None, optional (default=None) An estimator object that is used to compute the initial predictions. ``init`` has to provide ``fit`` and ``predict``. If None it uses ``loss.init_estimator``. verbose : int, default: 0 Enable verbose output. If 1 then it prints progress and performance once in a while (the more trees the lower the frequency). If greater than 1 then it prints progress and performance for every tree. warm_start : bool, default: False When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just erase the previous solution. 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 ---------- feature_importances_ : array, shape = [n_features] The feature importances (the higher, the more important the feature). oob_improvement_ : array, shape = [n_estimators] The improvement in loss (= deviance) on the out-of-bag samples relative to the previous iteration. ``oob_improvement_[0]`` is the improvement in loss of the first stage over the ``init`` estimator. train_score_ : array, shape = [n_estimators] The i-th score ``train_score_[i]`` is the deviance (= loss) of the model at iteration ``i`` on the in-bag sample. If ``subsample == 1`` this is the deviance on the training data. loss_ : LossFunction The concrete ``LossFunction`` object. `init` : BaseEstimator The estimator that provides the initial predictions. Set via the ``init`` argument or ``loss.init_estimator``. estimators_ : ndarray of DecisionTreeRegressor, shape = [n_estimators, 1] The collection of fitted sub-estimators. See also -------- DecisionTreeRegressor, RandomForestRegressor References ---------- J. Friedman, Greedy Function Approximation: A Gradient Boosting Machine, The Annals of Statistics, Vol. 29, No. 5, 2001. J. Friedman, Stochastic Gradient Boosting, 1999 T. Hastie, R. Tibshirani and J. Friedman. Elements of Statistical Learning Ed. 2, Springer, 2009. """ _SUPPORTED_LOSS = ('ls', 'lad', 'huber', 'quantile') def __init__(self, loss='ls', learning_rate=0.1, n_estimators=100, subsample=1.0, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_depth=3, init=None, random_state=None, max_features=None, alpha=0.9, verbose=0, max_leaf_nodes=None, warm_start=False): super(GradientBoostingRegressor, self).__init__( loss=loss, learning_rate=learning_rate, n_estimators=n_estimators, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, min_weight_fraction_leaf=min_weight_fraction_leaf, max_depth=max_depth, init=init, subsample=subsample, max_features=max_features, random_state=random_state, alpha=alpha, verbose=verbose, max_leaf_nodes=max_leaf_nodes, warm_start=warm_start) def predict(self, X): """Predict regression target for X. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- y : array of shape = [n_samples] The predicted values. """ X = check_array(X, dtype=DTYPE, order="C") return self._decision_function(X).ravel() def staged_predict(self, X): """Predict regression target at each stage for X. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters ---------- X : array-like of shape = [n_samples, n_features] The input samples. Returns ------- y : generator of array of shape = [n_samples] The predicted value of the input samples. """ for y in self._staged_decision_function(X): yield y.ravel()
bsd-3-clause
dfroger/geomalgo
test/triangulation/test_triangulation.py
2
1509
import unittest import numpy as np import geomalgo as ga STEP = ga.data.step class TestTriangulation(unittest.TestCase): def test_get(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) triangle = TG[3] self.assertEqual(triangle.index, 3) self.assertEqual(triangle.A.x, 2.5) self.assertEqual(triangle.A.y, 10) self.assertEqual(triangle.B.x, 2.5) self.assertEqual(triangle.B.y, 11) self.assertEqual(triangle.C.x, 1) self.assertEqual(triangle.C.y, 11) self.assertAlmostEqual(triangle.area, 0.75) def test_to_numpy(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) x, y, trivtx = TG.to_numpy() def test_to_matplotlib(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) tri = TG.to_matplotlib() def test_x_y_have_different_length(self): x = np.array( STEP.x.tolist() + [0, ] ) msg = 'Vector x and y must have the same length, but got 9 and 8' with self.assertRaisesRegex(ValueError, msg): ga.Triangulation2D(x, STEP.y, STEP.trivtx) def test_trivtx_has_bad_shape(self): trivtx = STEP.trivtx.copy() NT = trivtx.shape[0] trivtx.shape = (3, NT) msg = 'trivtx must be an array of shape \(NT, 3\), but got: \(3, 6\)' with self.assertRaisesRegex(ValueError, msg): ga.Triangulation2D(STEP.x, STEP.y, trivtx) if __name__ == '__main__': unittest.main()
gpl-3.0
huzq/scikit-learn
doc/conf.py
1
15607
# -*- 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. import sys import os import warnings import re from packaging.version import parse from pathlib import Path from io import StringIO # 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 import sphinx_gallery # -- General configuration --------------------------------------------------- # 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', 'numpydoc', 'sphinx.ext.linkcode', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.imgconverter', 'sphinx_gallery.gen_gallery', 'sphinx_issues' ] # this is needed for some reason... # see https://github.com/numpy/numpydoc/issues/69 numpydoc_class_members_toctree = False # For maths, use mathjax by default and svg if NO_MATHJAX env variable is set # (useful for viewing the doc offline) if os.environ.get('NO_MATHJAX'): extensions.append('sphinx.ext.imgmath') imgmath_image_format = 'svg' mathjax_path = '' else: extensions.append('sphinx.ext.mathjax') mathjax_path = ('https://cdn.jsdelivr.net/npm/mathjax@3/es5/' 'tex-chtml.js') autodoc_default_options = { 'members': True, 'inherited-members': True } # 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' # The master toctree document. master_doc = 'contents' # General information about the project. project = 'scikit-learn' copyright = '2007 - 2020, 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 parsed_version = parse(sklearn.__version__) version = ".".join(parsed_version.base_version.split(".")[:2]) # The full version, including alpha/beta/rc tags. # Removes post from release name if parsed_version.is_postrelease: release = parsed_version.base_version else: 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 patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build', 'templates', 'includes', 'themes'] # The reST default role (used for this markup: `text`) to use for all # documents. default_role = 'literal' # 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-modern' # 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 = {'google_analytics': True, 'mathjax_path': mathjax_path} # 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' # 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 = { 'index': 'index.html', 'documentation': 'documentation.html'} # redirects to index # 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' # If true, the reST sources are included in the HTML build as _sources/name. html_copy_source = True # Adds variables into templates html_context = {} # finds latest release highlights and places it into HTML context for # index.html release_highlights_dir = Path("..") / "examples" / "release_highlights" # Finds the highlight with the latest version number latest_highlights = sorted(release_highlights_dir.glob( "plot_release_highlights_*.py"))[-1] latest_highlights = latest_highlights.with_suffix('').name html_context["release_highlights"] = \ f"auto_examples/release_highlights/{latest_highlights}" # get version from higlight name assuming highlights have the form # plot_release_highlights_0_22_0 highlight_version = ".".join(latest_highlights.split("_")[-3:-1]) html_context["release_highlights_version"] = highlight_version # -- Options for LaTeX output ------------------------------------------------ latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. 'preamble': r""" \usepackage{amsmath}\usepackage{amsfonts}\usepackage{bm} \usepackage{morefloats}\usepackage{enumitem} \setlistdepth{10} """ } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass # [howto/manual]). latex_documents = [('contents', 'user_guide.tex', 'scikit-learn user guide', '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" # 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 # intersphinx configuration intersphinx_mapping = { 'python': ('https://docs.python.org/{.major}'.format( sys.version_info), None), 'numpy': ('https://docs.scipy.org/doc/numpy/', None), 'scipy': ('https://docs.scipy.org/doc/scipy/reference', None), 'matplotlib': ('https://matplotlib.org/', None), 'pandas': ('https://pandas.pydata.org/pandas-docs/stable/', None), 'joblib': ('https://joblib.readthedocs.io/en/latest/', None), 'seaborn': ('https://seaborn.pydata.org/', None), } v = parse(release) if v.release is None: raise ValueError( 'Ill-formed version: {!r}. Version should follow ' 'PEP440'.format(version)) if v.is_devrelease: binder_branch = 'master' else: major, minor = v.release[:2] binder_branch = '{}.{}.X'.format(major, minor) class SubSectionTitleOrder: """Sort example gallery by title of subsection. Assumes README.txt exists for all subsections and uses the subsection with dashes, '---', as the adornment. """ def __init__(self, src_dir): self.src_dir = src_dir self.regex = re.compile(r"^([\w ]+)\n-", re.MULTILINE) def __repr__(self): return '<%s>' % (self.__class__.__name__,) def __call__(self, directory): src_path = os.path.normpath(os.path.join(self.src_dir, directory)) # Forces Release Highlights to the top if os.path.basename(src_path) == "release_highlights": return "0" readme = os.path.join(src_path, "README.txt") try: with open(readme, 'r') as f: content = f.read() except FileNotFoundError: return directory title_match = self.regex.search(content) if title_match is not None: return title_match.group(1) return directory sphinx_gallery_conf = { 'doc_module': 'sklearn', 'backreferences_dir': os.path.join('modules', 'generated'), 'show_memory': False, 'reference_url': { 'sklearn': None}, 'examples_dirs': ['../examples'], 'gallery_dirs': ['auto_examples'], 'subsection_order': SubSectionTitleOrder('../examples'), 'binder': { 'org': 'scikit-learn', 'repo': 'scikit-learn', 'binderhub_url': 'https://mybinder.org', 'branch': binder_branch, 'dependencies': './binder/requirements.txt', 'use_jupyter_lab': True }, # avoid generating too many cross links 'inspect_global_variables': False, 'remove_config_comments': True, } # The following dictionary contains the information used to create the # thumbnails for the front page of the scikit-learn home page. # key: first image in set # values: (number of plot in set, height of thumbnail) carousel_thumbs = {'sphx_glr_plot_classifier_comparison_001.png': 600} # enable experimental module so that experimental estimators can be # discovered properly by sphinx from sklearn.experimental import enable_hist_gradient_boosting # noqa from sklearn.experimental import enable_iterative_imputer # noqa def make_carousel_thumbs(app, exception): """produces the final resized carousel images""" if exception is not None: return print('Preparing carousel images') image_dir = os.path.join(app.builder.outdir, '_images') for glr_plot, max_width in carousel_thumbs.items(): image = os.path.join(image_dir, glr_plot) if os.path.exists(image): c_thumb = os.path.join(image_dir, glr_plot[:-4] + '_carousel.png') sphinx_gallery.gen_rst.scale_image(image, c_thumb, max_width, 190) def filter_search_index(app, exception): if exception is not None: return # searchindex only exist when generating html if app.builder.name != 'html': return print('Removing methods from search index') searchindex_path = os.path.join(app.builder.outdir, 'searchindex.js') with open(searchindex_path, 'r') as f: searchindex_text = f.read() searchindex_text = re.sub(r'{__init__.+?}', '{}', searchindex_text) searchindex_text = re.sub(r'{__call__.+?}', '{}', searchindex_text) with open(searchindex_path, 'w') as f: f.write(searchindex_text) def generate_min_dependency_table(app): """Generate min dependency table for docs.""" from sklearn._build_utils.min_dependencies import dependent_packages # get length of header package_header_len = max(len(package) for package in dependent_packages) + 4 version_header_len = len('Minimum Version') + 4 tags_header_len = max(len(tags) for _, tags in dependent_packages.values()) + 4 output = StringIO() output.write(' '.join(['=' * package_header_len, '=' * version_header_len, '=' * tags_header_len])) output.write('\n') dependency_title = "Dependency" version_title = "Minimum Version" tags_title = "Purpose" output.write(f'{dependency_title:<{package_header_len}} ' f'{version_title:<{version_header_len}} ' f'{tags_title}\n') output.write(' '.join(['=' * package_header_len, '=' * version_header_len, '=' * tags_header_len])) output.write('\n') for package, (version, tags) in dependent_packages.items(): output.write(f'{package:<{package_header_len}} ' f'{version:<{version_header_len}} ' f'{tags}\n') output.write(' '.join(['=' * package_header_len, '=' * version_header_len, '=' * tags_header_len])) output.write('\n') output = output.getvalue() with (Path('.') / 'min_dependency.rst').open('w') as f: f.write(output) # Config for sphinx_issues # we use the issues path for PRs since the issues URL will forward issues_github_path = 'scikit-learn/scikit-learn' def setup(app): app.connect('builder-inited', generate_min_dependency_table) # to hide/show the prompt in code examples: app.connect('build-finished', make_carousel_thumbs) app.connect('build-finished', filter_search_index) # The following is used by sphinx.ext.linkcode to provide links to github linkcode_resolve = make_linkcode_resolve('sklearn', 'https://github.com/scikit-learn/' 'scikit-learn/blob/{revision}/' '{package}/{path}#L{lineno}') warnings.filterwarnings("ignore", category=UserWarning, message='Matplotlib is currently using agg, which is a' ' non-GUI backend, so cannot show the figure.')
bsd-3-clause
herilalaina/scikit-learn
sklearn/linear_model/tests/test_ridge.py
21
29229
import numpy as np import scipy.sparse as sp from scipy import linalg from itertools import product from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_raise_message from sklearn.utils.testing import ignore_warnings from sklearn.utils.testing import assert_warns from sklearn import datasets from sklearn.metrics import mean_squared_error from sklearn.metrics import make_scorer from sklearn.metrics import get_scorer from sklearn.linear_model.base import LinearRegression from sklearn.linear_model.ridge import ridge_regression from sklearn.linear_model.ridge import Ridge from sklearn.linear_model.ridge import _RidgeGCV from sklearn.linear_model.ridge import RidgeCV from sklearn.linear_model.ridge import RidgeClassifier from sklearn.linear_model.ridge import RidgeClassifierCV from sklearn.linear_model.ridge import _solve_cholesky from sklearn.linear_model.ridge import _solve_cholesky_kernel from sklearn.datasets import make_regression from sklearn.model_selection import GridSearchCV from sklearn.model_selection import KFold from sklearn.utils import check_random_state from sklearn.datasets import make_multilabel_classification diabetes = datasets.load_diabetes() X_diabetes, y_diabetes = diabetes.data, diabetes.target ind = np.arange(X_diabetes.shape[0]) rng = np.random.RandomState(0) rng.shuffle(ind) ind = ind[:200] X_diabetes, y_diabetes = X_diabetes[ind], y_diabetes[ind] iris = datasets.load_iris() X_iris = sp.csr_matrix(iris.data) y_iris = iris.target DENSE_FILTER = lambda X: X SPARSE_FILTER = lambda X: sp.csr_matrix(X) def test_ridge(): # Ridge regression convergence test using score # TODO: for this test to be robust, we should use a dataset instead # of np.random. rng = np.random.RandomState(0) alpha = 1.0 for solver in ("svd", "sparse_cg", "cholesky", "lsqr", "sag"): # With more samples than features n_samples, n_features = 6, 5 y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) ridge = Ridge(alpha=alpha, solver=solver) ridge.fit(X, y) assert_equal(ridge.coef_.shape, (X.shape[1], )) assert_greater(ridge.score(X, y), 0.47) if solver in ("cholesky", "sag"): # Currently the only solvers to support sample_weight. ridge.fit(X, y, sample_weight=np.ones(n_samples)) assert_greater(ridge.score(X, y), 0.47) # With more features than samples n_samples, n_features = 5, 10 y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) ridge = Ridge(alpha=alpha, solver=solver) ridge.fit(X, y) assert_greater(ridge.score(X, y), .9) if solver in ("cholesky", "sag"): # Currently the only solvers to support sample_weight. ridge.fit(X, y, sample_weight=np.ones(n_samples)) assert_greater(ridge.score(X, y), 0.9) def test_primal_dual_relationship(): y = y_diabetes.reshape(-1, 1) coef = _solve_cholesky(X_diabetes, y, alpha=[1e-2]) K = np.dot(X_diabetes, X_diabetes.T) dual_coef = _solve_cholesky_kernel(K, y, alpha=[1e-2]) coef2 = np.dot(X_diabetes.T, dual_coef).T assert_array_almost_equal(coef, coef2) def test_ridge_singular(): # test on a singular matrix rng = np.random.RandomState(0) n_samples, n_features = 6, 6 y = rng.randn(n_samples // 2) y = np.concatenate((y, y)) X = rng.randn(n_samples // 2, n_features) X = np.concatenate((X, X), axis=0) ridge = Ridge(alpha=0) ridge.fit(X, y) assert_greater(ridge.score(X, y), 0.9) def test_ridge_regression_sample_weights(): rng = np.random.RandomState(0) for solver in ("cholesky", ): for n_samples, n_features in ((6, 5), (5, 10)): for alpha in (1.0, 1e-2): y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) sample_weight = 1.0 + rng.rand(n_samples) coefs = ridge_regression(X, y, alpha=alpha, sample_weight=sample_weight, solver=solver) # Sample weight can be implemented via a simple rescaling # for the square loss. coefs2 = ridge_regression( X * np.sqrt(sample_weight)[:, np.newaxis], y * np.sqrt(sample_weight), alpha=alpha, solver=solver) assert_array_almost_equal(coefs, coefs2) def test_ridge_sample_weights(): # TODO: loop over sparse data as well rng = np.random.RandomState(0) param_grid = product((1.0, 1e-2), (True, False), ('svd', 'cholesky', 'lsqr', 'sparse_cg')) for n_samples, n_features in ((6, 5), (5, 10)): y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) sample_weight = 1.0 + rng.rand(n_samples) for (alpha, intercept, solver) in param_grid: # Ridge with explicit sample_weight est = Ridge(alpha=alpha, fit_intercept=intercept, solver=solver) est.fit(X, y, sample_weight=sample_weight) coefs = est.coef_ inter = est.intercept_ # Closed form of the weighted regularized least square # theta = (X^T W X + alpha I)^(-1) * X^T W y W = np.diag(sample_weight) if intercept is False: X_aug = X I = np.eye(n_features) else: dummy_column = np.ones(shape=(n_samples, 1)) X_aug = np.concatenate((dummy_column, X), axis=1) I = np.eye(n_features + 1) I[0, 0] = 0 cf_coefs = linalg.solve(X_aug.T.dot(W).dot(X_aug) + alpha * I, X_aug.T.dot(W).dot(y)) if intercept is False: assert_array_almost_equal(coefs, cf_coefs) else: assert_array_almost_equal(coefs, cf_coefs[1:]) assert_almost_equal(inter, cf_coefs[0]) def test_ridge_shapes(): # Test shape of coef_ and intercept_ rng = np.random.RandomState(0) n_samples, n_features = 5, 10 X = rng.randn(n_samples, n_features) y = rng.randn(n_samples) Y1 = y[:, np.newaxis] Y = np.c_[y, 1 + y] ridge = Ridge() ridge.fit(X, y) assert_equal(ridge.coef_.shape, (n_features,)) assert_equal(ridge.intercept_.shape, ()) ridge.fit(X, Y1) assert_equal(ridge.coef_.shape, (1, n_features)) assert_equal(ridge.intercept_.shape, (1, )) ridge.fit(X, Y) assert_equal(ridge.coef_.shape, (2, n_features)) assert_equal(ridge.intercept_.shape, (2, )) def test_ridge_intercept(): # Test intercept with multiple targets GH issue #708 rng = np.random.RandomState(0) n_samples, n_features = 5, 10 X = rng.randn(n_samples, n_features) y = rng.randn(n_samples) Y = np.c_[y, 1. + y] ridge = Ridge() ridge.fit(X, y) intercept = ridge.intercept_ ridge.fit(X, Y) assert_almost_equal(ridge.intercept_[0], intercept) assert_almost_equal(ridge.intercept_[1], intercept + 1.) def test_toy_ridge_object(): # Test BayesianRegression ridge classifier # TODO: test also n_samples > n_features X = np.array([[1], [2]]) Y = np.array([1, 2]) reg = Ridge(alpha=0.0) reg.fit(X, Y) X_test = [[1], [2], [3], [4]] assert_almost_equal(reg.predict(X_test), [1., 2, 3, 4]) assert_equal(len(reg.coef_.shape), 1) assert_equal(type(reg.intercept_), np.float64) Y = np.vstack((Y, Y)).T reg.fit(X, Y) X_test = [[1], [2], [3], [4]] assert_equal(len(reg.coef_.shape), 2) assert_equal(type(reg.intercept_), np.ndarray) def test_ridge_vs_lstsq(): # On alpha=0., Ridge and OLS yield the same solution. rng = np.random.RandomState(0) # we need more samples than features n_samples, n_features = 5, 4 y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) ridge = Ridge(alpha=0., fit_intercept=False) ols = LinearRegression(fit_intercept=False) ridge.fit(X, y) ols.fit(X, y) assert_almost_equal(ridge.coef_, ols.coef_) ridge.fit(X, y) ols.fit(X, y) assert_almost_equal(ridge.coef_, ols.coef_) def test_ridge_individual_penalties(): # Tests the ridge object using individual penalties rng = np.random.RandomState(42) n_samples, n_features, n_targets = 20, 10, 5 X = rng.randn(n_samples, n_features) y = rng.randn(n_samples, n_targets) penalties = np.arange(n_targets) coef_cholesky = np.array([ Ridge(alpha=alpha, solver="cholesky").fit(X, target).coef_ for alpha, target in zip(penalties, y.T)]) coefs_indiv_pen = [ Ridge(alpha=penalties, solver=solver, tol=1e-8).fit(X, y).coef_ for solver in ['svd', 'sparse_cg', 'lsqr', 'cholesky', 'sag', 'saga']] for coef_indiv_pen in coefs_indiv_pen: assert_array_almost_equal(coef_cholesky, coef_indiv_pen) # Test error is raised when number of targets and penalties do not match. ridge = Ridge(alpha=penalties[:-1]) assert_raises(ValueError, ridge.fit, X, y) def _test_ridge_loo(filter_): # test that can work with both dense or sparse matrices n_samples = X_diabetes.shape[0] ret = [] fit_intercept = filter_ == DENSE_FILTER if fit_intercept: X_diabetes_ = X_diabetes - X_diabetes.mean(0) else: X_diabetes_ = X_diabetes ridge_gcv = _RidgeGCV(fit_intercept=fit_intercept) ridge = Ridge(alpha=1.0, fit_intercept=fit_intercept) # because fit_intercept is applied # generalized cross-validation (efficient leave-one-out) decomp = ridge_gcv._pre_compute(X_diabetes_, y_diabetes, fit_intercept) errors, c = ridge_gcv._errors(1.0, y_diabetes, *decomp) values, c = ridge_gcv._values(1.0, y_diabetes, *decomp) # brute-force leave-one-out: remove one example at a time errors2 = [] values2 = [] for i in range(n_samples): sel = np.arange(n_samples) != i X_new = X_diabetes_[sel] y_new = y_diabetes[sel] ridge.fit(X_new, y_new) value = ridge.predict([X_diabetes_[i]])[0] error = (y_diabetes[i] - value) ** 2 errors2.append(error) values2.append(value) # check that efficient and brute-force LOO give same results assert_almost_equal(errors, errors2) assert_almost_equal(values, values2) # generalized cross-validation (efficient leave-one-out, # SVD variation) decomp = ridge_gcv._pre_compute_svd(X_diabetes_, y_diabetes, fit_intercept) errors3, c = ridge_gcv._errors_svd(ridge.alpha, y_diabetes, *decomp) values3, c = ridge_gcv._values_svd(ridge.alpha, y_diabetes, *decomp) # check that efficient and SVD efficient LOO give same results assert_almost_equal(errors, errors3) assert_almost_equal(values, values3) # check best alpha ridge_gcv.fit(filter_(X_diabetes), y_diabetes) alpha_ = ridge_gcv.alpha_ ret.append(alpha_) # check that we get same best alpha with custom loss_func f = ignore_warnings scoring = make_scorer(mean_squared_error, greater_is_better=False) ridge_gcv2 = RidgeCV(fit_intercept=False, scoring=scoring) f(ridge_gcv2.fit)(filter_(X_diabetes), y_diabetes) assert_equal(ridge_gcv2.alpha_, alpha_) # check that we get same best alpha with custom score_func func = lambda x, y: -mean_squared_error(x, y) scoring = make_scorer(func) ridge_gcv3 = RidgeCV(fit_intercept=False, scoring=scoring) f(ridge_gcv3.fit)(filter_(X_diabetes), y_diabetes) assert_equal(ridge_gcv3.alpha_, alpha_) # check that we get same best alpha with a scorer scorer = get_scorer('neg_mean_squared_error') ridge_gcv4 = RidgeCV(fit_intercept=False, scoring=scorer) ridge_gcv4.fit(filter_(X_diabetes), y_diabetes) assert_equal(ridge_gcv4.alpha_, alpha_) # check that we get same best alpha with sample weights ridge_gcv.fit(filter_(X_diabetes), y_diabetes, sample_weight=np.ones(n_samples)) assert_equal(ridge_gcv.alpha_, alpha_) # simulate several responses Y = np.vstack((y_diabetes, y_diabetes)).T ridge_gcv.fit(filter_(X_diabetes), Y) Y_pred = ridge_gcv.predict(filter_(X_diabetes)) ridge_gcv.fit(filter_(X_diabetes), y_diabetes) y_pred = ridge_gcv.predict(filter_(X_diabetes)) assert_array_almost_equal(np.vstack((y_pred, y_pred)).T, Y_pred, decimal=5) return ret def _test_ridge_cv_normalize(filter_): ridge_cv = RidgeCV(normalize=True, cv=3) ridge_cv.fit(filter_(10. * X_diabetes), y_diabetes) gs = GridSearchCV(Ridge(normalize=True), cv=3, param_grid={'alpha': ridge_cv.alphas}) gs.fit(filter_(10. * X_diabetes), y_diabetes) assert_equal(gs.best_estimator_.alpha, ridge_cv.alpha_) def _test_ridge_cv(filter_): ridge_cv = RidgeCV() ridge_cv.fit(filter_(X_diabetes), y_diabetes) ridge_cv.predict(filter_(X_diabetes)) assert_equal(len(ridge_cv.coef_.shape), 1) assert_equal(type(ridge_cv.intercept_), np.float64) cv = KFold(5) ridge_cv.set_params(cv=cv) ridge_cv.fit(filter_(X_diabetes), y_diabetes) ridge_cv.predict(filter_(X_diabetes)) assert_equal(len(ridge_cv.coef_.shape), 1) assert_equal(type(ridge_cv.intercept_), np.float64) def _test_ridge_diabetes(filter_): ridge = Ridge(fit_intercept=False) ridge.fit(filter_(X_diabetes), y_diabetes) return np.round(ridge.score(filter_(X_diabetes), y_diabetes), 5) def _test_multi_ridge_diabetes(filter_): # simulate several responses Y = np.vstack((y_diabetes, y_diabetes)).T n_features = X_diabetes.shape[1] ridge = Ridge(fit_intercept=False) ridge.fit(filter_(X_diabetes), Y) assert_equal(ridge.coef_.shape, (2, n_features)) Y_pred = ridge.predict(filter_(X_diabetes)) ridge.fit(filter_(X_diabetes), y_diabetes) y_pred = ridge.predict(filter_(X_diabetes)) assert_array_almost_equal(np.vstack((y_pred, y_pred)).T, Y_pred, decimal=3) def _test_ridge_classifiers(filter_): n_classes = np.unique(y_iris).shape[0] n_features = X_iris.shape[1] for reg in (RidgeClassifier(), RidgeClassifierCV()): reg.fit(filter_(X_iris), y_iris) assert_equal(reg.coef_.shape, (n_classes, n_features)) y_pred = reg.predict(filter_(X_iris)) assert_greater(np.mean(y_iris == y_pred), .79) cv = KFold(5) reg = RidgeClassifierCV(cv=cv) reg.fit(filter_(X_iris), y_iris) y_pred = reg.predict(filter_(X_iris)) assert_true(np.mean(y_iris == y_pred) >= 0.8) def _test_tolerance(filter_): ridge = Ridge(tol=1e-5, fit_intercept=False) ridge.fit(filter_(X_diabetes), y_diabetes) score = ridge.score(filter_(X_diabetes), y_diabetes) ridge2 = Ridge(tol=1e-3, fit_intercept=False) ridge2.fit(filter_(X_diabetes), y_diabetes) score2 = ridge2.score(filter_(X_diabetes), y_diabetes) assert_true(score >= score2) def check_dense_sparse(test_func): # test dense matrix ret_dense = test_func(DENSE_FILTER) # test sparse matrix ret_sparse = test_func(SPARSE_FILTER) # test that the outputs are the same if ret_dense is not None and ret_sparse is not None: assert_array_almost_equal(ret_dense, ret_sparse, decimal=3) def test_dense_sparse(): for test_func in (_test_ridge_loo, _test_ridge_cv, _test_ridge_cv_normalize, _test_ridge_diabetes, _test_multi_ridge_diabetes, _test_ridge_classifiers, _test_tolerance): yield check_dense_sparse, test_func def test_ridge_cv_sparse_svd(): X = sp.csr_matrix(X_diabetes) ridge = RidgeCV(gcv_mode="svd") assert_raises(TypeError, ridge.fit, X) def test_ridge_sparse_svd(): X = sp.csc_matrix(rng.rand(100, 10)) y = rng.rand(100) ridge = Ridge(solver='svd', fit_intercept=False) assert_raises(TypeError, ridge.fit, X, y) def test_class_weights(): # Test class weights. X = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], [1.0, 1.0], [1.0, 0.0]]) y = [1, 1, 1, -1, -1] reg = RidgeClassifier(class_weight=None) reg.fit(X, y) assert_array_equal(reg.predict([[0.2, -1.0]]), np.array([1])) # we give a small weights to class 1 reg = RidgeClassifier(class_weight={1: 0.001}) reg.fit(X, y) # now the hyperplane should rotate clock-wise and # the prediction on this point should shift assert_array_equal(reg.predict([[0.2, -1.0]]), np.array([-1])) # check if class_weight = 'balanced' can handle negative labels. reg = RidgeClassifier(class_weight='balanced') reg.fit(X, y) assert_array_equal(reg.predict([[0.2, -1.0]]), np.array([1])) # class_weight = 'balanced', and class_weight = None should return # same values when y has equal number of all labels X = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], [1.0, 1.0]]) y = [1, 1, -1, -1] reg = RidgeClassifier(class_weight=None) reg.fit(X, y) rega = RidgeClassifier(class_weight='balanced') rega.fit(X, y) assert_equal(len(rega.classes_), 2) assert_array_almost_equal(reg.coef_, rega.coef_) assert_array_almost_equal(reg.intercept_, rega.intercept_) def test_class_weight_vs_sample_weight(): """Check class_weights resemble sample_weights behavior.""" for reg in (RidgeClassifier, RidgeClassifierCV): # Iris is balanced, so no effect expected for using 'balanced' weights reg1 = reg() reg1.fit(iris.data, iris.target) reg2 = reg(class_weight='balanced') reg2.fit(iris.data, iris.target) assert_almost_equal(reg1.coef_, reg2.coef_) # Inflate importance of class 1, check against user-defined weights sample_weight = np.ones(iris.target.shape) sample_weight[iris.target == 1] *= 100 class_weight = {0: 1., 1: 100., 2: 1.} reg1 = reg() reg1.fit(iris.data, iris.target, sample_weight) reg2 = reg(class_weight=class_weight) reg2.fit(iris.data, iris.target) assert_almost_equal(reg1.coef_, reg2.coef_) # Check that sample_weight and class_weight are multiplicative reg1 = reg() reg1.fit(iris.data, iris.target, sample_weight ** 2) reg2 = reg(class_weight=class_weight) reg2.fit(iris.data, iris.target, sample_weight) assert_almost_equal(reg1.coef_, reg2.coef_) def test_class_weights_cv(): # Test class weights for cross validated ridge classifier. X = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], [1.0, 1.0], [1.0, 0.0]]) y = [1, 1, 1, -1, -1] reg = RidgeClassifierCV(class_weight=None, alphas=[.01, .1, 1]) reg.fit(X, y) # we give a small weights to class 1 reg = RidgeClassifierCV(class_weight={1: 0.001}, alphas=[.01, .1, 1, 10]) reg.fit(X, y) assert_array_equal(reg.predict([[-.2, 2]]), np.array([-1])) def test_ridgecv_store_cv_values(): # Test _RidgeCV's store_cv_values attribute. rng = rng = np.random.RandomState(42) n_samples = 8 n_features = 5 x = rng.randn(n_samples, n_features) alphas = [1e-1, 1e0, 1e1] n_alphas = len(alphas) r = RidgeCV(alphas=alphas, store_cv_values=True) # with len(y.shape) == 1 y = rng.randn(n_samples) r.fit(x, y) assert_equal(r.cv_values_.shape, (n_samples, n_alphas)) # with len(y.shape) == 2 n_responses = 3 y = rng.randn(n_samples, n_responses) r.fit(x, y) assert_equal(r.cv_values_.shape, (n_samples, n_responses, n_alphas)) def test_ridgecv_sample_weight(): rng = np.random.RandomState(0) alphas = (0.1, 1.0, 10.0) # There are different algorithms for n_samples > n_features # and the opposite, so test them both. for n_samples, n_features in ((6, 5), (5, 10)): y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) sample_weight = 1.0 + rng.rand(n_samples) cv = KFold(5) ridgecv = RidgeCV(alphas=alphas, cv=cv) ridgecv.fit(X, y, sample_weight=sample_weight) # Check using GridSearchCV directly parameters = {'alpha': alphas} gs = GridSearchCV(Ridge(), parameters, cv=cv) gs.fit(X, y, sample_weight=sample_weight) assert_equal(ridgecv.alpha_, gs.best_estimator_.alpha) assert_array_almost_equal(ridgecv.coef_, gs.best_estimator_.coef_) def test_raises_value_error_if_sample_weights_greater_than_1d(): # Sample weights must be either scalar or 1D n_sampless = [2, 3] n_featuress = [3, 2] rng = np.random.RandomState(42) for n_samples, n_features in zip(n_sampless, n_featuress): X = rng.randn(n_samples, n_features) y = rng.randn(n_samples) sample_weights_OK = rng.randn(n_samples) ** 2 + 1 sample_weights_OK_1 = 1. sample_weights_OK_2 = 2. sample_weights_not_OK = sample_weights_OK[:, np.newaxis] sample_weights_not_OK_2 = sample_weights_OK[np.newaxis, :] ridge = Ridge(alpha=1) # make sure the "OK" sample weights actually work ridge.fit(X, y, sample_weights_OK) ridge.fit(X, y, sample_weights_OK_1) ridge.fit(X, y, sample_weights_OK_2) def fit_ridge_not_ok(): ridge.fit(X, y, sample_weights_not_OK) def fit_ridge_not_ok_2(): ridge.fit(X, y, sample_weights_not_OK_2) assert_raise_message(ValueError, "Sample weights must be 1D array or scalar", fit_ridge_not_ok) assert_raise_message(ValueError, "Sample weights must be 1D array or scalar", fit_ridge_not_ok_2) def test_sparse_design_with_sample_weights(): # Sample weights must work with sparse matrices n_sampless = [2, 3] n_featuress = [3, 2] rng = np.random.RandomState(42) sparse_matrix_converters = [sp.coo_matrix, sp.csr_matrix, sp.csc_matrix, sp.lil_matrix, sp.dok_matrix ] sparse_ridge = Ridge(alpha=1., fit_intercept=False) dense_ridge = Ridge(alpha=1., fit_intercept=False) for n_samples, n_features in zip(n_sampless, n_featuress): X = rng.randn(n_samples, n_features) y = rng.randn(n_samples) sample_weights = rng.randn(n_samples) ** 2 + 1 for sparse_converter in sparse_matrix_converters: X_sparse = sparse_converter(X) sparse_ridge.fit(X_sparse, y, sample_weight=sample_weights) dense_ridge.fit(X, y, sample_weight=sample_weights) assert_array_almost_equal(sparse_ridge.coef_, dense_ridge.coef_, decimal=6) def test_raises_value_error_if_solver_not_supported(): # Tests whether a ValueError is raised if a non-identified solver # is passed to ridge_regression wrong_solver = "This is not a solver (MagritteSolveCV QuantumBitcoin)" exception = ValueError message = "Solver %s not understood" % wrong_solver def func(): X = np.eye(3) y = np.ones(3) ridge_regression(X, y, alpha=1., solver=wrong_solver) assert_raise_message(exception, message, func) def test_sparse_cg_max_iter(): reg = Ridge(solver="sparse_cg", max_iter=1) reg.fit(X_diabetes, y_diabetes) assert_equal(reg.coef_.shape[0], X_diabetes.shape[1]) @ignore_warnings def test_n_iter(): # Test that self.n_iter_ is correct. n_targets = 2 X, y = X_diabetes, y_diabetes y_n = np.tile(y, (n_targets, 1)).T for max_iter in range(1, 4): for solver in ('sag', 'saga', 'lsqr'): reg = Ridge(solver=solver, max_iter=max_iter, tol=1e-12) reg.fit(X, y_n) assert_array_equal(reg.n_iter_, np.tile(max_iter, n_targets)) for solver in ('sparse_cg', 'svd', 'cholesky'): reg = Ridge(solver=solver, max_iter=1, tol=1e-1) reg.fit(X, y_n) assert_equal(reg.n_iter_, None) def test_ridge_fit_intercept_sparse(): X, y = make_regression(n_samples=1000, n_features=2, n_informative=2, bias=10., random_state=42) X_csr = sp.csr_matrix(X) for solver in ['saga', 'sag']: dense = Ridge(alpha=1., tol=1.e-15, solver=solver, fit_intercept=True) sparse = Ridge(alpha=1., tol=1.e-15, solver=solver, fit_intercept=True) dense.fit(X, y) sparse.fit(X_csr, y) assert_almost_equal(dense.intercept_, sparse.intercept_) assert_array_almost_equal(dense.coef_, sparse.coef_) # test the solver switch and the corresponding warning sparse = Ridge(alpha=1., tol=1.e-15, solver='lsqr', fit_intercept=True) assert_warns(UserWarning, sparse.fit, X_csr, y) assert_almost_equal(dense.intercept_, sparse.intercept_) assert_array_almost_equal(dense.coef_, sparse.coef_) def test_errors_and_values_helper(): ridgecv = _RidgeGCV() rng = check_random_state(42) alpha = 1. n = 5 y = rng.randn(n) v = rng.randn(n) Q = rng.randn(len(v), len(v)) QT_y = Q.T.dot(y) G_diag, c = ridgecv._errors_and_values_helper(alpha, y, v, Q, QT_y) # test that helper function behaves as expected out, c_ = ridgecv._errors(alpha, y, v, Q, QT_y) np.testing.assert_array_equal(out, (c / G_diag) ** 2) np.testing.assert_array_equal(c, c) out, c_ = ridgecv._values(alpha, y, v, Q, QT_y) np.testing.assert_array_equal(out, y - (c / G_diag)) np.testing.assert_array_equal(c_, c) def test_errors_and_values_svd_helper(): ridgecv = _RidgeGCV() rng = check_random_state(42) alpha = 1. for n, p in zip((5, 10), (12, 6)): y = rng.randn(n) v = rng.randn(p) U = rng.randn(n, p) UT_y = U.T.dot(y) G_diag, c = ridgecv._errors_and_values_svd_helper(alpha, y, v, U, UT_y) # test that helper function behaves as expected out, c_ = ridgecv._errors_svd(alpha, y, v, U, UT_y) np.testing.assert_array_equal(out, (c / G_diag) ** 2) np.testing.assert_array_equal(c, c) out, c_ = ridgecv._values_svd(alpha, y, v, U, UT_y) np.testing.assert_array_equal(out, y - (c / G_diag)) np.testing.assert_array_equal(c_, c) def test_ridge_classifier_no_support_multilabel(): X, y = make_multilabel_classification(n_samples=10, random_state=0) assert_raises(ValueError, RidgeClassifier().fit, X, y) def test_dtype_match(): rng = np.random.RandomState(0) alpha = 1.0 n_samples, n_features = 6, 5 X_64 = rng.randn(n_samples, n_features) y_64 = rng.randn(n_samples) X_32 = X_64.astype(np.float32) y_32 = y_64.astype(np.float32) solvers = ["svd", "sparse_cg", "cholesky", "lsqr"] for solver in solvers: # Check type consistency 32bits ridge_32 = Ridge(alpha=alpha, solver=solver) ridge_32.fit(X_32, y_32) coef_32 = ridge_32.coef_ # Check type consistency 64 bits ridge_64 = Ridge(alpha=alpha, solver=solver) ridge_64.fit(X_64, y_64) coef_64 = ridge_64.coef_ # Do the actual checks at once for easier debug assert coef_32.dtype == X_32.dtype assert coef_64.dtype == X_64.dtype assert ridge_32.predict(X_32).dtype == X_32.dtype assert ridge_64.predict(X_64).dtype == X_64.dtype assert_almost_equal(ridge_32.coef_, ridge_64.coef_, decimal=5) def test_dtype_match_cholesky(): # Test different alphas in cholesky solver to ensure full coverage. # This test is separated from test_dtype_match for clarity. rng = np.random.RandomState(0) alpha = (1.0, 0.5) n_samples, n_features, n_target = 6, 7, 2 X_64 = rng.randn(n_samples, n_features) y_64 = rng.randn(n_samples, n_target) X_32 = X_64.astype(np.float32) y_32 = y_64.astype(np.float32) # Check type consistency 32bits ridge_32 = Ridge(alpha=alpha, solver='cholesky') ridge_32.fit(X_32, y_32) coef_32 = ridge_32.coef_ # Check type consistency 64 bits ridge_64 = Ridge(alpha=alpha, solver='cholesky') ridge_64.fit(X_64, y_64) coef_64 = ridge_64.coef_ # Do all the checks at once, like this is easier to debug assert coef_32.dtype == X_32.dtype assert coef_64.dtype == X_64.dtype assert ridge_32.predict(X_32).dtype == X_32.dtype assert ridge_64.predict(X_64).dtype == X_64.dtype assert_almost_equal(ridge_32.coef_, ridge_64.coef_, decimal=5)
bsd-3-clause
AstroTech/workshop-python
data-visualization/src/matplotlib-radar-chart.py
1
7427
import numpy as np import matplotlib.pyplot as plt from matplotlib.path import Path from matplotlib.spines import Spine from matplotlib.projections.polar import PolarAxes from matplotlib.projections import register_projection def radar_factory(num_vars, frame='circle'): """Create a radar chart with `num_vars` axes. This function creates a RadarAxes projection and registers it. Parameters ---------- num_vars : int Number of variables for radar chart. frame : {'circle' | 'polygon'} Shape of frame surrounding axes. """ # calculate evenly-spaced axis angles theta = np.linspace(0, 2 * np.pi, num_vars, endpoint=False) def draw_poly_patch(self): # rotate theta such that the first axis is at the top verts = unit_poly_verts(theta + np.pi / 2) return plt.Polygon(verts, closed=True, edgecolor='k') def draw_circle_patch(self): # unit circle centered on (0.5, 0.5) return plt.Circle((0.5, 0.5), 0.5) patch_dict = {'polygon': draw_poly_patch, 'circle': draw_circle_patch} if frame not in patch_dict: raise ValueError('unknown value for `frame`: %s' % frame) class RadarAxes(PolarAxes): name = 'radar' # use 1 line segment to connect specified points RESOLUTION = 1 # define draw_frame method draw_patch = patch_dict[frame] def __init__(self, *args, **kwargs): super(RadarAxes, self).__init__(*args, **kwargs) # rotate plot such that the first axis is at the top self.set_theta_zero_location('N') def fill(self, *args, **kwargs): """Override fill so that line is closed by default""" closed = kwargs.pop('closed', True) return super(RadarAxes, self).fill(closed=closed, *args, **kwargs) def plot(self, *args, **kwargs): """Override plot so that line is closed by default""" lines = super(RadarAxes, self).plot(*args, **kwargs) for line in lines: self._close_line(line) def _close_line(self, line): x, y = line.get_data() # FIXME: markers at x[0], y[0] get doubled-up if x[0] != x[-1]: x = np.concatenate((x, [x[0]])) y = np.concatenate((y, [y[0]])) line.set_data(x, y) def set_varlabels(self, labels): self.set_thetagrids(np.degrees(theta), labels) def _gen_axes_patch(self): return self.draw_patch() def _gen_axes_spines(self): if frame == 'circle': return PolarAxes._gen_axes_spines(self) # The following is a hack to get the spines (i.e. the axes frame) # to draw correctly for a polygon frame. # spine_type must be 'left', 'right', 'top', 'bottom', or `circle`. spine_type = 'circle' verts = unit_poly_verts(theta + np.pi / 2) # close off polygon by repeating first vertex verts.append(verts[0]) path = Path(verts) spine = Spine(self, spine_type, path) spine.set_transform(self.transAxes) return {'polar': spine} register_projection(RadarAxes) return theta def unit_poly_verts(theta): """Return vertices of polygon for subplot axes. This polygon is circumscribed by a unit circle centered at (0.5, 0.5) """ x0, y0, r = [0.5] * 3 verts = [(r * np.cos(t) + x0, r * np.sin(t) + y0) for t in theta] return verts def example_data(): # The following data is from the Denver Aerosol Sources and Health study. # See doi:10.1016/j.atmosenv.2008.12.017 # # The data are pollution source profile estimates for five modeled # pollution sources (e.g., cars, wood-burning, etc) that emit 7-9 chemical # species. The radar charts are experimented with here to see if we can # nicely visualize how the modeled source profiles change across four # scenarios: # 1) No gas-phase species present, just seven particulate counts on # Sulfate # Nitrate # Elemental Carbon (EC) # Organic Carbon fraction 1 (OC) # Organic Carbon fraction 2 (OC2) # Organic Carbon fraction 3 (OC3) # Pyrolized Organic Carbon (OP) # 2)Inclusion of gas-phase specie carbon monoxide (CO) # 3)Inclusion of gas-phase specie ozone (O3). # 4)Inclusion of both gas-phase species is present... data = [ ['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP', 'CO', 'O3'], ('Basecase', [ [0.88, 0.01, 0.03, 0.03, 0.00, 0.06, 0.01, 0.00, 0.00], [0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00], [0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00], [0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00], [0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]]), ('With CO', [ [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00], [0.08, 0.94, 0.04, 0.02, 0.00, 0.01, 0.12, 0.04, 0.00], [0.01, 0.01, 0.79, 0.10, 0.00, 0.05, 0.00, 0.31, 0.00], [0.00, 0.02, 0.03, 0.38, 0.31, 0.31, 0.00, 0.59, 0.00], [0.02, 0.02, 0.11, 0.47, 0.69, 0.58, 0.88, 0.00, 0.00]]), ('With O3', [ [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03], [0.07, 0.95, 0.05, 0.04, 0.00, 0.02, 0.12, 0.00, 0.00], [0.01, 0.02, 0.86, 0.27, 0.16, 0.19, 0.00, 0.00, 0.00], [0.01, 0.03, 0.00, 0.32, 0.29, 0.27, 0.00, 0.00, 0.95], [0.02, 0.00, 0.03, 0.37, 0.56, 0.47, 0.87, 0.00, 0.00]]), ('CO & O3', [ [0.87, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01], [0.09, 0.95, 0.02, 0.03, 0.00, 0.01, 0.13, 0.06, 0.00], [0.01, 0.02, 0.71, 0.24, 0.13, 0.16, 0.00, 0.50, 0.00], [0.01, 0.03, 0.00, 0.28, 0.24, 0.23, 0.00, 0.44, 0.88], [0.02, 0.00, 0.18, 0.45, 0.64, 0.55, 0.86, 0.00, 0.16]]) ] return data if __name__ == '__main__': N = 9 theta = radar_factory(N, frame='polygon') data = example_data() spoke_labels = data.pop(0) fig, axes = plt.subplots(figsize=(9, 9), nrows=2, ncols=2, subplot_kw=dict(projection='radar')) fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05) colors = ['b', 'r', 'g', 'm', 'y'] # Plot the four cases from the example data on separate axes for ax, (title, case_data) in zip(axes.flatten(), data): ax.set_rgrids([0.2, 0.4, 0.6, 0.8]) ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1), horizontalalignment='center', verticalalignment='center') for d, color in zip(case_data, colors): ax.plot(theta, d, color=color) ax.fill(theta, d, facecolor=color, alpha=0.25) ax.set_varlabels(spoke_labels) # add legend relative to top-left plot ax = axes[0, 0] labels = ('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5') legend = ax.legend(labels, loc=(0.9, .95), labelspacing=0.1, fontsize='small') fig.text(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios', horizontalalignment='center', color='black', weight='bold', size='large') plt.show()
mit
abhishekgahlot/scikit-learn
sklearn/linear_model/tests/test_ransac.py
40
12814
import numpy as np from numpy.testing import assert_equal, assert_raises from numpy.testing import assert_array_almost_equal from scipy import sparse from sklearn.utils.testing import assert_less from sklearn.linear_model import LinearRegression, RANSACRegressor from sklearn.linear_model.ransac import _dynamic_max_trials # 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, 1))) 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) 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) 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.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) if __name__ == "__main__": np.testing.run_module_suite()
bsd-3-clause
xdnian/pyml
code/optional-py-scripts/ch13.py
2
11389
# Sebastian Raschka, 2015 (http://sebastianraschka.com) # Python Machine Learning - Code Examples # # Chapter 13 - Parallelizing Neural Network Training with Theano # # S. Raschka. Python Machine Learning. Packt Publishing Ltd., 2015. # GitHub Repo: https://github.com/rasbt/python-machine-learning-book # # License: MIT # https://github.com/rasbt/python-machine-learning-book/blob/master/LICENSE.txt import theano from theano import tensor as T import numpy as np import struct import matplotlib.pyplot as plt from keras.utils import np_utils from keras.models import Sequential from keras.layers.core import Dense from keras.optimizers import SGD ############################################################################# print(50 * '=') print('First steps with Theano') print(50 * '-') # initialize x1 = T.scalar() w1 = T.scalar() w0 = T.scalar() z1 = w1 * x1 + w0 # compile net_input = theano.function(inputs=[w1, x1, w0], outputs=z1) # execute net_input(2.0, 1.0, 0.5) ############################################################################# print(50 * '=') print('Configuring Theano') print(50 * '-') print('theano.config.floatX', theano.config.floatX) theano.config.floatX = 'float32' print('print(theano.config.device)', print(theano.config.device)) ############################################################################# print(50 * '=') print('Working with array structures') print(50 * '-') # initialize # if you are running Theano on 64 bit mode, # you need to use dmatrix instead of fmatrix x = T.fmatrix(name='x') x_sum = T.sum(x, axis=0) # compile calc_sum = theano.function(inputs=[x], outputs=x_sum) # execute (Python list) ary = [[1, 2, 3], [1, 2, 3]] print('Column sum:', calc_sum(ary)) # execute (NumPy array) ary = np.array([[1, 2, 3], [1, 2, 3]], dtype=theano.config.floatX) print('Column sum:', calc_sum(ary)) # initialize x = T.fmatrix(name='x') w = theano.shared(np.asarray([[0.0, 0.0, 0.0]], dtype=theano.config.floatX)) z = x.dot(w.T) update = [[w, w + 1.0]] # compile net_input = theano.function(inputs=[x], updates=update, outputs=z) # execute data = np.array([[1, 2, 3]], dtype=theano.config.floatX) for i in range(5): print('z%d:' % i, net_input(data)) """ We can use the `givens` variable to insert values into the graph before compiling it. Using this approach we can reduce the number of transfers from RAM (via CPUs) to GPUs to speed up learning with shared variables. If we use `inputs`, a datasets is transferred from the CPU to the GPU multiple times, for example, if we iterate over a dataset multiple times (epochs) during gradient descent. Via `givens`, we can keep the dataset on the GPU if it fits (e.g., a mini-batch). """ # initialize data = np.array([[1, 2, 3]], dtype=theano.config.floatX) x = T.fmatrix(name='x') w = theano.shared(np.asarray([[0.0, 0.0, 0.0]], dtype=theano.config.floatX)) z = x.dot(w.T) update = [[w, w + 1.0]] # compile net_input = theano.function(inputs=[], updates=update, givens={x: data}, outputs=z) # execute for i in range(5): print('z:', net_input()) ############################################################################# print(50 * '=') print('Wrapping things up: A linear regression example') print(50 * '-') X_train = np.asarray([[0.0], [1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0], [9.0]], dtype=theano.config.floatX) y_train = np.asarray([1.0, 1.3, 3.1, 2.0, 5.0, 6.3, 6.6, 7.4, 8.0, 9.0], dtype=theano.config.floatX) def train_linreg(X_train, y_train, eta, epochs): costs = [] # Initialize arrays eta0 = T.fscalar('eta0') y = T.fvector(name='y') X = T.fmatrix(name='X') w = theano.shared(np.zeros( shape=(X_train.shape[1] + 1), dtype=theano.config.floatX), name='w') # calculate cost net_input = T.dot(X, w[1:]) + w[0] errors = y - net_input cost = T.sum(T.pow(errors, 2)) # perform gradient update gradient = T.grad(cost, wrt=w) update = [(w, w - eta0 * gradient)] # compile model train = theano.function(inputs=[eta0], outputs=cost, updates=update, givens={X: X_train, y: y_train}) for _ in range(epochs): costs.append(train(eta)) return costs, w costs, w = train_linreg(X_train, y_train, eta=0.001, epochs=10) plt.plot(range(1, len(costs) + 1), costs) plt.tight_layout() plt.xlabel('Epoch') plt.ylabel('Cost') # plt.tight_layout() # plt.savefig('./figures/cost_convergence.png', dpi=300) plt.show() def predict_linreg(X, w): Xt = T.matrix(name='X') net_input = T.dot(Xt, w[1:]) + w[0] predict = theano.function(inputs=[Xt], givens={w: w}, outputs=net_input) return predict(X) plt.scatter(X_train, y_train, marker='s', s=50) plt.plot(range(X_train.shape[0]), predict_linreg(X_train, w), color='gray', marker='o', markersize=4, linewidth=3) plt.xlabel('x') plt.ylabel('y') # plt.tight_layout() # plt.savefig('./figures/linreg.png', dpi=300) plt.show() ############################################################################# print(50 * '=') print('Wrapping things up: A linear regression example') print(50 * '-') # note that first element (X[0] = 1) to denote bias unit X = np.array([[1, 1.4, 1.5]]) w = np.array([0.0, 0.2, 0.4]) def net_input(X, w): z = X.dot(w) return z def logistic(z): return 1.0 / (1.0 + np.exp(-z)) def logistic_activation(X, w): z = net_input(X, w) return logistic(z) print('P(y=1|x) = %.3f' % logistic_activation(X, w)[0]) # W : array, shape = [n_output_units, n_hidden_units+1] # Weight matrix for hidden layer -> output layer. # note that first column (A[:][0] = 1) are the bias units W = np.array([[1.1, 1.2, 1.3, 0.5], [0.1, 0.2, 0.4, 0.1], [0.2, 0.5, 2.1, 1.9]]) # A : array, shape = [n_hidden+1, n_samples] # Activation of hidden layer. # note that first element (A[0][0] = 1) is for the bias units A = np.array([[1.0], [0.1], [0.3], [0.7]]) # Z : array, shape = [n_output_units, n_samples] # Net input of output layer. Z = W.dot(A) y_probas = logistic(Z) print('Probabilities:\n', y_probas) y_class = np.argmax(Z, axis=0) print('predicted class label: %d' % y_class[0]) ############################################################################# print(50 * '=') print('Estimating probabilities in multi-class' ' classification via the softmax function') print(50 * '-') def softmax(z): return np.exp(z) / np.sum(np.exp(z)) def softmax_activation(X, w): z = net_input(X, w) return softmax(z) y_probas = softmax(Z) print('Probabilities:\n', y_probas) print('Sum of probabilities', y_probas.sum()) y_class = np.argmax(Z, axis=0) print('Predicted class', y_class) ############################################################################# print(50 * '=') print('Broadening the output spectrum using a hyperbolic tangent') print(50 * '-') def tanh(z): e_p = np.exp(z) e_m = np.exp(-z) return (e_p - e_m) / (e_p + e_m) z = np.arange(-5, 5, 0.005) log_act = logistic(z) tanh_act = tanh(z) # alternatives: # from scipy.special import expit # log_act = expit(z) # tanh_act = np.tanh(z) plt.ylim([-1.5, 1.5]) plt.xlabel('net input $z$') plt.ylabel('activation $\phi(z)$') plt.axhline(1, color='black', linestyle='--') plt.axhline(0.5, color='black', linestyle='--') plt.axhline(0, color='black', linestyle='--') plt.axhline(-1, color='black', linestyle='--') plt.plot(z, tanh_act, linewidth=2, color='black', label='tanh') plt.plot(z, log_act, linewidth=2, color='lightgreen', label='logistic') plt.legend(loc='lower right') # plt.tight_layout() # plt.savefig('./figures/activation.png', dpi=300) plt.show() ############################################################################# print(50 * '=') print('Broadening the output spectrum using a hyperbolic tangent') print(50 * '-') _ = input("Please make sure that you've downloaded and unzipped the" " MNIST dataset as described in the previous chapter. The following" " code assumes that you have created a mnist directory within" " this script's directory. Please hit 'enter' to continue.") def load_mnist(path, kind='train'): """Load MNIST data from `path`""" labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II', lbpath.read(8)) labels = np.fromfile(lbpath, dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16)) images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) return images, labels X_train, y_train = load_mnist('mnist', kind='train') print('Training rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1])) X_test, y_test = load_mnist('mnist', kind='t10k') print('Test rows: %d, columns: %d' % (X_test.shape[0], X_test.shape[1])) ############################################################################# print(50 * '=') print('Multi-layer Perceptron in Keras') print(50 * '-') theano.config.floatX = 'float32' X_train = X_train.astype(theano.config.floatX) X_test = X_test.astype(theano.config.floatX) print('First 3 labels: ', y_train[:3]) y_train_ohe = np_utils.to_categorical(y_train) print('\nFirst 3 labels (one-hot):\n', y_train_ohe[:3]) np.random.seed(1) model = Sequential() model.add(Dense(input_dim=X_train.shape[1], output_dim=50, init='uniform', activation='tanh')) model.add(Dense(input_dim=50, output_dim=50, init='uniform', activation='tanh')) model.add(Dense(input_dim=50, output_dim=y_train_ohe.shape[1], init='uniform', activation='softmax')) sgd = SGD(lr=0.001, decay=1e-7, momentum=.9) model.compile(loss='categorical_crossentropy', optimizer=sgd) model.fit(X_train, y_train_ohe, nb_epoch=50, batch_size=300, verbose=1, validation_split=0.1, show_accuracy=True) y_train_pred = model.predict_classes(X_train, verbose=0) print('First 3 predictions: ', y_train_pred[:3]) train_acc = np.sum(y_train == y_train_pred, axis=0) / X_train.shape[0] print('Training accuracy: %.2f%%' % (train_acc * 100)) y_test_pred = model.predict_classes(X_test, verbose=0) test_acc = np.sum(y_test == y_test_pred, axis=0) / X_test.shape[0] print('Test accuracy: %.2f%%' % (test_acc * 100))
mit
xuewei4d/scikit-learn
examples/mixture/plot_gmm_selection.py
15
3396
""" ================================ 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. """ import numpy as np import itertools from scipy import linalg import matplotlib.pyplot as plt import matplotlib as mpl from sklearn import mixture print(__doc__) # 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 Gaussian mixture with EM gmm = mixture.GaussianMixture(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(['navy', 'turquoise', 'cornflowerblue', 'darkorange']) clf = best_gmm bars = [] # Plot the BIC scores plt.figure(figsize=(8, 6)) 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, cov, color) in enumerate(zip(clf.means_, clf.covariances_, color_iter)): v, w = linalg.eigh(cov) 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 = 2. * np.sqrt(2.) * np.sqrt(v) 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.xticks(()) plt.yticks(()) plt.title(f'Selected GMM: {best_gmm.covariance_type} model, ' f'{best_gmm.n_components} components') plt.subplots_adjust(hspace=.35, bottom=.02) plt.show()
bsd-3-clause
zihua/scikit-learn
sklearn/linear_model/tests/test_bayes.py
299
1770
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Fabian Pedregosa <fabian.pedregosa@inria.fr> # # License: BSD 3 clause import numpy as np from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import SkipTest from sklearn.linear_model.bayes import BayesianRidge, ARDRegression from sklearn import datasets from sklearn.utils.testing import assert_array_almost_equal def test_bayesian_on_diabetes(): # Test BayesianRidge on diabetes raise SkipTest("XFailed Test") diabetes = datasets.load_diabetes() X, y = diabetes.data, diabetes.target clf = BayesianRidge(compute_score=True) # Test with more samples than features clf.fit(X, y) # Test that scores are increasing at each iteration assert_array_equal(np.diff(clf.scores_) > 0, True) # Test with more features than samples X = X[:5, :] y = y[:5] clf.fit(X, y) # Test that scores are increasing at each iteration assert_array_equal(np.diff(clf.scores_) > 0, True) def test_toy_bayesian_ridge_object(): # Test BayesianRidge on toy X = np.array([[1], [2], [6], [8], [10]]) Y = np.array([1, 2, 6, 8, 10]) clf = BayesianRidge(compute_score=True) clf.fit(X, Y) # Check that the model could approximately learn the identity function test = [[1], [3], [4]] assert_array_almost_equal(clf.predict(test), [1, 3, 4], 2) def test_toy_ard_object(): # Test BayesianRegression ARD classifier X = np.array([[1], [2], [3]]) Y = np.array([1, 2, 3]) clf = ARDRegression(compute_score=True) clf.fit(X, Y) # Check that the model could approximately learn the identity function test = [[1], [3], [4]] assert_array_almost_equal(clf.predict(test), [1, 3, 4], 2)
bsd-3-clause
CforED/Machine-Learning
examples/classification/plot_classifier_comparison.py
36
5123
#!/usr/bin/python # -*- coding: utf-8 -*- """ ===================== Classifier comparison ===================== A comparison of a several classifiers in scikit-learn on synthetic datasets. The point of this example is to illustrate the nature of decision boundaries of different classifiers. This should be taken with a grain of salt, as the intuition conveyed by these examples does not necessarily carry over to real datasets. Particularly in high-dimensional spaces, data can more easily be separated linearly and the simplicity of classifiers such as naive Bayes and linear SVMs might lead to better generalization than is achieved by other classifiers. The plots show training points in solid colors and testing points semi-transparent. The lower right shows the classification accuracy on the test set. """ print(__doc__) # Code source: Gaël Varoquaux # Andreas Müller # Modified for documentation by Jaques Grobler # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.datasets import make_moons, make_circles, make_classification from sklearn.neural_network import MLPClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.gaussian_process import GaussianProcessClassifier from sklearn.gaussian_process.kernels import RBF from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier from sklearn.naive_bayes import GaussianNB from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis h = .02 # step size in the mesh names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process", "Decision Tree", "Random Forest", "Neural Net", "AdaBoost", "Naive Bayes", "QDA"] classifiers = [ KNeighborsClassifier(3), SVC(kernel="linear", C=0.025), SVC(gamma=2, C=1), GaussianProcessClassifier(1.0 * RBF(1.0), warm_start=True), DecisionTreeClassifier(max_depth=5), RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1), MLPClassifier(alpha=1), AdaBoostClassifier(), GaussianNB(), QuadraticDiscriminantAnalysis()] X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=1) rng = np.random.RandomState(2) X += 2 * rng.uniform(size=X.shape) linearly_separable = (X, y) datasets = [make_moons(noise=0.3, random_state=0), make_circles(noise=0.2, factor=0.5, random_state=1), linearly_separable ] figure = plt.figure(figsize=(27, 9)) i = 1 # iterate over datasets for ds_cnt, ds in enumerate(datasets): # preprocess dataset, split into training and test part X, y = ds X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = \ train_test_split(X, y, test_size=.4, random_state=42) x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # just plot the dataset first cm = plt.cm.RdBu cm_bright = ListedColormap(['#FF0000', '#0000FF']) ax = plt.subplot(len(datasets), len(classifiers) + 1, i) if ds_cnt == 0: ax.set_title("Input data") # Plot the training points ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright) # and testing points ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6) ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) i += 1 # iterate over classifiers for name, clf in zip(names, classifiers): ax = plt.subplot(len(datasets), len(classifiers) + 1, i) clf.fit(X_train, y_train) score = clf.score(X_test, y_test) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. if hasattr(clf, "decision_function"): Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] # Put the result into a color plot Z = Z.reshape(xx.shape) ax.contourf(xx, yy, Z, cmap=cm, alpha=.8) # Plot also the training points ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright) # and testing points ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6) ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) if ds_cnt == 0: ax.set_title(name) ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'), size=15, horizontalalignment='right') i += 1 plt.tight_layout() plt.show()
bsd-3-clause
Clyde-fare/scikit-learn
sklearn/metrics/metrics.py
233
1262
import warnings warnings.warn("sklearn.metrics.metrics is deprecated and will be removed in " "0.18. Please import from sklearn.metrics", DeprecationWarning) from .ranking import auc from .ranking import average_precision_score from .ranking import label_ranking_average_precision_score from .ranking import precision_recall_curve from .ranking import roc_auc_score from .ranking import roc_curve from .classification import accuracy_score from .classification import classification_report from .classification import confusion_matrix from .classification import f1_score from .classification import fbeta_score from .classification import hamming_loss from .classification import hinge_loss from .classification import jaccard_similarity_score from .classification import log_loss from .classification import matthews_corrcoef from .classification import precision_recall_fscore_support from .classification import precision_score from .classification import recall_score from .classification import zero_one_loss from .regression import explained_variance_score from .regression import mean_absolute_error from .regression import mean_squared_error from .regression import median_absolute_error from .regression import r2_score
bsd-3-clause
macks22/gensim
gensim/sklearn_api/d2vmodel.py
1
3875
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (C) 2011 Radim Rehurek <radimrehurek@seznam.cz> # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html """ Scikit learn interface for gensim for easy use of gensim with scikit-learn Follows scikit-learn API conventions """ import numpy as np from six import string_types from sklearn.base import TransformerMixin, BaseEstimator from sklearn.exceptions import NotFittedError from gensim import models class D2VTransformer(TransformerMixin, BaseEstimator): """ Base Doc2Vec module """ def __init__(self, dm_mean=None, dm=1, dbow_words=0, dm_concat=0, dm_tag_count=1, docvecs=None, docvecs_mapfile=None, comment=None, trim_rule=None, size=100, alpha=0.025, window=5, min_count=5, max_vocab_size=None, sample=1e-3, seed=1, workers=3, min_alpha=0.0001, hs=0, negative=5, cbow_mean=1, hashfxn=hash, iter=5, sorted_vocab=1, batch_words=10000): """ Sklearn api for Doc2Vec model. See gensim.models.Doc2Vec and gensim.models.Word2Vec for parameter details. """ self.gensim_model = None self.dm_mean = dm_mean self.dm = dm self.dbow_words = dbow_words self.dm_concat = dm_concat self.dm_tag_count = dm_tag_count self.docvecs = docvecs self.docvecs_mapfile = docvecs_mapfile self.comment = comment self.trim_rule = trim_rule # attributes associated with gensim.models.Word2Vec self.size = size self.alpha = alpha self.window = window self.min_count = min_count self.max_vocab_size = max_vocab_size self.sample = sample self.seed = seed self.workers = workers self.min_alpha = min_alpha self.hs = hs self.negative = negative self.cbow_mean = int(cbow_mean) self.hashfxn = hashfxn self.iter = iter self.sorted_vocab = sorted_vocab self.batch_words = batch_words def fit(self, X, y=None): """ Fit the model according to the given training data. Calls gensim.models.Doc2Vec """ self.gensim_model = models.Doc2Vec( documents=X, dm_mean=self.dm_mean, dm=self.dm, dbow_words=self.dbow_words, dm_concat=self.dm_concat, dm_tag_count=self.dm_tag_count, docvecs=self.docvecs, docvecs_mapfile=self.docvecs_mapfile, comment=self.comment, trim_rule=self.trim_rule, size=self.size, alpha=self.alpha, window=self.window, min_count=self.min_count, max_vocab_size=self.max_vocab_size, sample=self.sample, seed=self.seed, workers=self.workers, min_alpha=self.min_alpha, hs=self.hs, negative=self.negative, cbow_mean=self.cbow_mean, hashfxn=self.hashfxn, iter=self.iter, sorted_vocab=self.sorted_vocab, batch_words=self.batch_words ) return self def transform(self, docs): """ Return the vector representations for the input documents. The input `docs` should be a list of lists like : [ ['calculus', 'mathematical'], ['geometry', 'operations', 'curves'] ] or a single document like : ['calculus', 'mathematical'] """ if self.gensim_model is None: raise NotFittedError( "This model has not been fitted yet. Call 'fit' with appropriate arguments before using this method." ) # The input as array of array check = lambda x: [x] if isinstance(x[0], string_types) else x docs = check(docs) X = [[] for _ in range(0, len(docs))] for k, v in enumerate(docs): doc_vec = self.gensim_model.infer_vector(v) X[k] = doc_vec return np.reshape(np.array(X), (len(docs), self.gensim_model.vector_size))
lgpl-2.1
alvarofierroclavero/scikit-learn
sklearn/grid_search.py
103
36232
""" The :mod:`sklearn.grid_search` includes utilities to fine-tune the parameters of an estimator. """ from __future__ import print_function # Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>, # Gael Varoquaux <gael.varoquaux@normalesup.org> # Andreas Mueller <amueller@ais.uni-bonn.de> # Olivier Grisel <olivier.grisel@ensta.org> # License: BSD 3 clause from abc import ABCMeta, abstractmethod from collections import Mapping, namedtuple, Sized from functools import partial, reduce from itertools import product import operator import warnings import numpy as np from .base import BaseEstimator, is_classifier, clone from .base import MetaEstimatorMixin, ChangedBehaviorWarning from .cross_validation import check_cv from .cross_validation import _fit_and_score from .externals.joblib import Parallel, delayed from .externals import six from .utils import check_random_state from .utils.random import sample_without_replacement from .utils.validation import _num_samples, indexable from .utils.metaestimators import if_delegate_has_method from .metrics.scorer import check_scoring __all__ = ['GridSearchCV', 'ParameterGrid', 'fit_grid_point', 'ParameterSampler', 'RandomizedSearchCV'] class ParameterGrid(object): """Grid of parameters with a discrete number of values for each. Can be used to iterate over parameter value combinations with the Python built-in function iter. Read more in the :ref:`User Guide <grid_search>`. Parameters ---------- param_grid : dict of string to sequence, or sequence of such The parameter grid to explore, as a dictionary mapping estimator parameters to sequences of allowed values. An empty dict signifies default parameters. A sequence of dicts signifies a sequence of grids to search, and is useful to avoid exploring parameter combinations that make no sense or have no effect. See the examples below. Examples -------- >>> from sklearn.grid_search import ParameterGrid >>> param_grid = {'a': [1, 2], 'b': [True, False]} >>> list(ParameterGrid(param_grid)) == ( ... [{'a': 1, 'b': True}, {'a': 1, 'b': False}, ... {'a': 2, 'b': True}, {'a': 2, 'b': False}]) True >>> grid = [{'kernel': ['linear']}, {'kernel': ['rbf'], 'gamma': [1, 10]}] >>> list(ParameterGrid(grid)) == [{'kernel': 'linear'}, ... {'kernel': 'rbf', 'gamma': 1}, ... {'kernel': 'rbf', 'gamma': 10}] True >>> ParameterGrid(grid)[1] == {'kernel': 'rbf', 'gamma': 1} True See also -------- :class:`GridSearchCV`: uses ``ParameterGrid`` to perform a full parallelized parameter search. """ def __init__(self, param_grid): if isinstance(param_grid, Mapping): # wrap dictionary in a singleton list to support either dict # or list of dicts param_grid = [param_grid] self.param_grid = param_grid def __iter__(self): """Iterate over the points in the grid. Returns ------- params : iterator over dict of string to any Yields dictionaries mapping each estimator parameter to one of its allowed values. """ for p in self.param_grid: # Always sort the keys of a dictionary, for reproducibility items = sorted(p.items()) if not items: yield {} else: keys, values = zip(*items) for v in product(*values): params = dict(zip(keys, v)) yield params def __len__(self): """Number of points on the grid.""" # Product function that can handle iterables (np.product can't). product = partial(reduce, operator.mul) return sum(product(len(v) for v in p.values()) if p else 1 for p in self.param_grid) def __getitem__(self, ind): """Get the parameters that would be ``ind``th in iteration Parameters ---------- ind : int The iteration index Returns ------- params : dict of string to any Equal to list(self)[ind] """ # This is used to make discrete sampling without replacement memory # efficient. for sub_grid in self.param_grid: # XXX: could memoize information used here if not sub_grid: if ind == 0: return {} else: ind -= 1 continue # Reverse so most frequent cycling parameter comes first keys, values_lists = zip(*sorted(sub_grid.items())[::-1]) sizes = [len(v_list) for v_list in values_lists] total = np.product(sizes) if ind >= total: # Try the next grid ind -= total else: out = {} for key, v_list, n in zip(keys, values_lists, sizes): ind, offset = divmod(ind, n) out[key] = v_list[offset] return out raise IndexError('ParameterGrid index out of range') class ParameterSampler(object): """Generator on parameters sampled from given distributions. Non-deterministic iterable over random candidate combinations for hyper- parameter search. If all parameters are presented as a list, sampling without replacement is performed. If at least one parameter is given as a distribution, sampling with replacement is used. It is highly recommended to use continuous distributions for continuous parameters. Note that as of SciPy 0.12, the ``scipy.stats.distributions`` do not accept a custom RNG instance and always use the singleton RNG from ``numpy.random``. Hence setting ``random_state`` will not guarantee a deterministic iteration whenever ``scipy.stats`` distributions are used to define the parameter search space. Read more in the :ref:`User Guide <grid_search>`. Parameters ---------- param_distributions : dict Dictionary where the keys are parameters and values are distributions from which a parameter is to be sampled. Distributions either have to provide a ``rvs`` function to sample from them, or can be given as a list of values, where a uniform distribution is assumed. n_iter : integer Number of parameter settings that are produced. random_state : int or RandomState Pseudo random number generator state used for random uniform sampling from lists of possible values instead of scipy.stats distributions. Returns ------- params : dict of string to any **Yields** dictionaries mapping each estimator parameter to as sampled value. Examples -------- >>> from sklearn.grid_search import ParameterSampler >>> from scipy.stats.distributions import expon >>> import numpy as np >>> np.random.seed(0) >>> param_grid = {'a':[1, 2], 'b': expon()} >>> param_list = list(ParameterSampler(param_grid, n_iter=4)) >>> rounded_list = [dict((k, round(v, 6)) for (k, v) in d.items()) ... for d in param_list] >>> rounded_list == [{'b': 0.89856, 'a': 1}, ... {'b': 0.923223, 'a': 1}, ... {'b': 1.878964, 'a': 2}, ... {'b': 1.038159, 'a': 2}] True """ def __init__(self, param_distributions, n_iter, random_state=None): self.param_distributions = param_distributions self.n_iter = n_iter self.random_state = random_state def __iter__(self): # check if all distributions are given as lists # in this case we want to sample without replacement all_lists = np.all([not hasattr(v, "rvs") for v in self.param_distributions.values()]) rnd = check_random_state(self.random_state) if all_lists: # look up sampled parameter settings in parameter grid param_grid = ParameterGrid(self.param_distributions) grid_size = len(param_grid) if grid_size < self.n_iter: raise ValueError( "The total space of parameters %d is smaller " "than n_iter=%d." % (grid_size, self.n_iter) + " For exhaustive searches, use GridSearchCV.") for i in sample_without_replacement(grid_size, self.n_iter, random_state=rnd): yield param_grid[i] else: # Always sort the keys of a dictionary, for reproducibility items = sorted(self.param_distributions.items()) for _ in six.moves.range(self.n_iter): params = dict() for k, v in items: if hasattr(v, "rvs"): params[k] = v.rvs() else: params[k] = v[rnd.randint(len(v))] yield params def __len__(self): """Number of points that will be sampled.""" return self.n_iter def fit_grid_point(X, y, estimator, parameters, train, test, scorer, verbose, error_score='raise', **fit_params): """Run fit on one set of parameters. Parameters ---------- X : array-like, sparse matrix or list Input data. y : array-like or None Targets for input data. estimator : estimator object This estimator will be cloned and then fitted. parameters : dict Parameters to be set on estimator for this grid point. train : ndarray, dtype int or bool Boolean mask or indices for training set. test : ndarray, dtype int or bool Boolean mask or indices for test set. scorer : callable or None. If provided must be a scorer callable object / function with signature ``scorer(estimator, X, y)``. verbose : int Verbosity level. **fit_params : kwargs Additional parameter passed to the fit function of the estimator. error_score : 'raise' (default) or numeric Value to assign to the score if an error occurs in estimator fitting. If set to 'raise', the error is raised. If a numeric value is given, FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error. Returns ------- score : float Score of this parameter setting on given training / test split. parameters : dict The parameters that have been evaluated. n_samples_test : int Number of test samples in this split. """ score, n_samples_test, _ = _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, error_score) return score, parameters, n_samples_test def _check_param_grid(param_grid): if hasattr(param_grid, 'items'): param_grid = [param_grid] for p in param_grid: for v in p.values(): if isinstance(v, np.ndarray) and v.ndim > 1: raise ValueError("Parameter array should be one-dimensional.") check = [isinstance(v, k) for k in (list, tuple, np.ndarray)] if True not in check: raise ValueError("Parameter values should be a list.") if len(v) == 0: raise ValueError("Parameter values should be a non-empty " "list.") class _CVScoreTuple (namedtuple('_CVScoreTuple', ('parameters', 'mean_validation_score', 'cv_validation_scores'))): # A raw namedtuple is very memory efficient as it packs the attributes # in a struct to get rid of the __dict__ of attributes in particular it # does not copy the string for the keys on each instance. # By deriving a namedtuple class just to introduce the __repr__ method we # would also reintroduce the __dict__ on the instance. By telling the # Python interpreter that this subclass uses static __slots__ instead of # dynamic attributes. Furthermore we don't need any additional slot in the # subclass so we set __slots__ to the empty tuple. __slots__ = () def __repr__(self): """Simple custom repr to summarize the main info""" return "mean: {0:.5f}, std: {1:.5f}, params: {2}".format( self.mean_validation_score, np.std(self.cv_validation_scores), self.parameters) class BaseSearchCV(six.with_metaclass(ABCMeta, BaseEstimator, MetaEstimatorMixin)): """Base class for hyper parameter search with cross-validation.""" @abstractmethod def __init__(self, estimator, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise'): self.scoring = scoring self.estimator = estimator self.n_jobs = n_jobs self.fit_params = fit_params if fit_params is not None else {} self.iid = iid self.refit = refit self.cv = cv self.verbose = verbose self.pre_dispatch = pre_dispatch self.error_score = error_score @property def _estimator_type(self): return self.estimator._estimator_type def score(self, X, y=None): """Returns the score on the given data, if the estimator has been refit This uses the score defined by ``scoring`` where provided, and the ``best_estimator_.score`` method otherwise. Parameters ---------- X : array-like, shape = [n_samples, n_features] Input data, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] or [n_samples, n_output], optional Target relative to X for classification or regression; None for unsupervised learning. Returns ------- score : float Notes ----- * The long-standing behavior of this method changed in version 0.16. * It no longer uses the metric provided by ``estimator.score`` if the ``scoring`` parameter was set when fitting. """ if self.scorer_ is None: raise ValueError("No score function explicitly defined, " "and the estimator doesn't provide one %s" % self.best_estimator_) if self.scoring is not None and hasattr(self.best_estimator_, 'score'): warnings.warn("The long-standing behavior to use the estimator's " "score function in {0}.score has changed. The " "scoring parameter is now used." "".format(self.__class__.__name__), ChangedBehaviorWarning) return self.scorer_(self.best_estimator_, X, y) @if_delegate_has_method(delegate='estimator') def predict(self, X): """Call predict on the estimator with the best found parameters. Only available if ``refit=True`` and the underlying estimator supports ``predict``. Parameters ----------- X : indexable, length n_samples Must fulfill the input assumptions of the underlying estimator. """ return self.best_estimator_.predict(X) @if_delegate_has_method(delegate='estimator') def predict_proba(self, X): """Call predict_proba on the estimator with the best found parameters. Only available if ``refit=True`` and the underlying estimator supports ``predict_proba``. Parameters ----------- X : indexable, length n_samples Must fulfill the input assumptions of the underlying estimator. """ return self.best_estimator_.predict_proba(X) @if_delegate_has_method(delegate='estimator') def predict_log_proba(self, X): """Call predict_log_proba on the estimator with the best found parameters. Only available if ``refit=True`` and the underlying estimator supports ``predict_log_proba``. Parameters ----------- X : indexable, length n_samples Must fulfill the input assumptions of the underlying estimator. """ return self.best_estimator_.predict_log_proba(X) @if_delegate_has_method(delegate='estimator') def decision_function(self, X): """Call decision_function on the estimator with the best found parameters. Only available if ``refit=True`` and the underlying estimator supports ``decision_function``. Parameters ----------- X : indexable, length n_samples Must fulfill the input assumptions of the underlying estimator. """ return self.best_estimator_.decision_function(X) @if_delegate_has_method(delegate='estimator') def transform(self, X): """Call transform on the estimator with the best found parameters. Only available if the underlying estimator supports ``transform`` and ``refit=True``. Parameters ----------- X : indexable, length n_samples Must fulfill the input assumptions of the underlying estimator. """ return self.best_estimator_.transform(X) @if_delegate_has_method(delegate='estimator') def inverse_transform(self, Xt): """Call inverse_transform on the estimator with the best found parameters. Only available if the underlying estimator implements ``inverse_transform`` and ``refit=True``. Parameters ----------- Xt : indexable, length n_samples Must fulfill the input assumptions of the underlying estimator. """ return self.best_estimator_.transform(Xt) def _fit(self, X, y, parameter_iterable): """Actual fitting, performing the search over parameters.""" estimator = self.estimator cv = self.cv self.scorer_ = check_scoring(self.estimator, scoring=self.scoring) n_samples = _num_samples(X) X, y = indexable(X, y) if y is not None: if len(y) != n_samples: raise ValueError('Target variable (y) has a different number ' 'of samples (%i) than data (X: %i samples)' % (len(y), n_samples)) cv = check_cv(cv, X, y, classifier=is_classifier(estimator)) if self.verbose > 0: if isinstance(parameter_iterable, Sized): n_candidates = len(parameter_iterable) print("Fitting {0} folds for each of {1} candidates, totalling" " {2} fits".format(len(cv), n_candidates, n_candidates * len(cv))) base_estimator = clone(self.estimator) pre_dispatch = self.pre_dispatch out = Parallel( n_jobs=self.n_jobs, verbose=self.verbose, pre_dispatch=pre_dispatch )( delayed(_fit_and_score)(clone(base_estimator), X, y, self.scorer_, train, test, self.verbose, parameters, self.fit_params, return_parameters=True, error_score=self.error_score) for parameters in parameter_iterable for train, test in cv) # Out is a list of triplet: score, estimator, n_test_samples n_fits = len(out) n_folds = len(cv) scores = list() grid_scores = list() for grid_start in range(0, n_fits, n_folds): n_test_samples = 0 score = 0 all_scores = [] for this_score, this_n_test_samples, _, parameters in \ out[grid_start:grid_start + n_folds]: all_scores.append(this_score) if self.iid: this_score *= this_n_test_samples n_test_samples += this_n_test_samples score += this_score if self.iid: score /= float(n_test_samples) else: score /= float(n_folds) scores.append((score, parameters)) # TODO: shall we also store the test_fold_sizes? grid_scores.append(_CVScoreTuple( parameters, score, np.array(all_scores))) # Store the computed scores self.grid_scores_ = grid_scores # Find the best parameters by comparing on the mean validation score: # note that `sorted` is deterministic in the way it breaks ties best = sorted(grid_scores, key=lambda x: x.mean_validation_score, reverse=True)[0] self.best_params_ = best.parameters self.best_score_ = best.mean_validation_score if self.refit: # fit the best estimator using the entire dataset # clone first to work around broken estimators best_estimator = clone(base_estimator).set_params( **best.parameters) if y is not None: best_estimator.fit(X, y, **self.fit_params) else: best_estimator.fit(X, **self.fit_params) self.best_estimator_ = best_estimator return self class GridSearchCV(BaseSearchCV): """Exhaustive search over specified parameter values for an estimator. Important members are fit, predict. GridSearchCV implements a "fit" method and a "predict" method like any classifier except that the parameters of the classifier used to predict is optimized by cross-validation. Read more in the :ref:`User Guide <grid_search>`. Parameters ---------- estimator : object type that implements the "fit" and "predict" methods A object of that type is instantiated for each grid point. param_grid : dict or list of dictionaries Dictionary with parameters names (string) as keys and lists of parameter settings to try as values, or a list of such dictionaries, in which case the grids spanned by each dictionary in the list are explored. This enables searching over any sequence of parameter settings. scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. fit_params : dict, optional Parameters to pass to the fit method. n_jobs : int, default 1 Number of jobs to run in parallel. pre_dispatch : int, or string, optional Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be: - None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs - An int, giving the exact number of total jobs that are spawned - A string, giving an expression as a function of n_jobs, as in '2*n_jobs' iid : boolean, default=True If True, the data is assumed to be identically distributed across the folds, and the loss minimized is the total loss per sample, and not the mean loss across the folds. cv : integer or cross-validation generator, default=3 If an integer is passed, it is the number of folds. Specific cross-validation objects can be passed, see sklearn.cross_validation module for the list of possible objects refit : boolean, default=True Refit the best estimator with the entire dataset. If "False", it is impossible to make predictions using this GridSearchCV instance after fitting. verbose : integer Controls the verbosity: the higher, the more messages. error_score : 'raise' (default) or numeric Value to assign to the score if an error occurs in estimator fitting. If set to 'raise', the error is raised. If a numeric value is given, FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error. Examples -------- >>> from sklearn import svm, grid_search, datasets >>> iris = datasets.load_iris() >>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]} >>> svr = svm.SVC() >>> clf = grid_search.GridSearchCV(svr, parameters) >>> clf.fit(iris.data, iris.target) ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS GridSearchCV(cv=None, error_score=..., estimator=SVC(C=1.0, cache_size=..., class_weight=..., coef0=..., decision_function_shape=None, degree=..., gamma=..., kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=..., verbose=False), fit_params={}, iid=..., n_jobs=1, param_grid=..., pre_dispatch=..., refit=..., scoring=..., verbose=...) Attributes ---------- grid_scores_ : list of named tuples Contains scores for all parameter combinations in param_grid. Each entry corresponds to one parameter setting. Each named tuple has the attributes: * ``parameters``, a dict of parameter settings * ``mean_validation_score``, the mean score over the cross-validation folds * ``cv_validation_scores``, the list of scores for each fold best_estimator_ : estimator Estimator that was chosen by the search, i.e. estimator which gave highest score (or smallest loss if specified) on the left out data. Not available if refit=False. best_score_ : float Score of best_estimator on the left out data. best_params_ : dict Parameter setting that gave the best results on the hold out data. scorer_ : function Scorer function used on the held out data to choose the best parameters for the model. Notes ------ The parameters selected are those that maximize the score of the left out data, unless an explicit score is passed in which case it is used instead. If `n_jobs` was set to a value higher than one, the data is copied for each point in the grid (and not `n_jobs` times). This is done for efficiency reasons if individual jobs take very little time, but may raise errors if the dataset is large and not enough memory is available. A workaround in this case is to set `pre_dispatch`. Then, the memory is copied only `pre_dispatch` many times. A reasonable value for `pre_dispatch` is `2 * n_jobs`. See Also --------- :class:`ParameterGrid`: generates all the combinations of a an hyperparameter grid. :func:`sklearn.cross_validation.train_test_split`: utility function to split the data into a development set usable for fitting a GridSearchCV instance and an evaluation set for its final evaluation. :func:`sklearn.metrics.make_scorer`: Make a scorer from a performance metric or loss function. """ def __init__(self, estimator, param_grid, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise'): super(GridSearchCV, self).__init__( estimator, scoring, fit_params, n_jobs, iid, refit, cv, verbose, pre_dispatch, error_score) self.param_grid = param_grid _check_param_grid(param_grid) def fit(self, X, y=None): """Run fit with all sets of parameters. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vector, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] or [n_samples, n_output], optional Target relative to X for classification or regression; None for unsupervised learning. """ return self._fit(X, y, ParameterGrid(self.param_grid)) class RandomizedSearchCV(BaseSearchCV): """Randomized search on hyper parameters. RandomizedSearchCV implements a "fit" method and a "predict" method like any classifier except that the parameters of the classifier used to predict is optimized by cross-validation. In contrast to GridSearchCV, not all parameter values are tried out, but rather a fixed number of parameter settings is sampled from the specified distributions. The number of parameter settings that are tried is given by n_iter. If all parameters are presented as a list, sampling without replacement is performed. If at least one parameter is given as a distribution, sampling with replacement is used. It is highly recommended to use continuous distributions for continuous parameters. Read more in the :ref:`User Guide <randomized_parameter_search>`. Parameters ---------- estimator : object type that implements the "fit" and "predict" methods A object of that type is instantiated for each parameter setting. param_distributions : dict Dictionary with parameters names (string) as keys and distributions or lists of parameters to try. Distributions must provide a ``rvs`` method for sampling (such as those from scipy.stats.distributions). If a list is given, it is sampled uniformly. n_iter : int, default=10 Number of parameter settings that are sampled. n_iter trades off runtime vs quality of the solution. scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. fit_params : dict, optional Parameters to pass to the fit method. n_jobs : int, default=1 Number of jobs to run in parallel. pre_dispatch : int, or string, optional Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be: - None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs - An int, giving the exact number of total jobs that are spawned - A string, giving an expression as a function of n_jobs, as in '2*n_jobs' iid : boolean, default=True If True, the data is assumed to be identically distributed across the folds, and the loss minimized is the total loss per sample, and not the mean loss across the folds. cv : integer or cross-validation generator, optional If an integer is passed, it is the number of folds (default 3). Specific cross-validation objects can be passed, see sklearn.cross_validation module for the list of possible objects refit : boolean, default=True Refit the best estimator with the entire dataset. If "False", it is impossible to make predictions using this RandomizedSearchCV instance after fitting. verbose : integer Controls the verbosity: the higher, the more messages. random_state : int or RandomState Pseudo random number generator state used for random uniform sampling from lists of possible values instead of scipy.stats distributions. error_score : 'raise' (default) or numeric Value to assign to the score if an error occurs in estimator fitting. If set to 'raise', the error is raised. If a numeric value is given, FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error. Attributes ---------- grid_scores_ : list of named tuples Contains scores for all parameter combinations in param_grid. Each entry corresponds to one parameter setting. Each named tuple has the attributes: * ``parameters``, a dict of parameter settings * ``mean_validation_score``, the mean score over the cross-validation folds * ``cv_validation_scores``, the list of scores for each fold best_estimator_ : estimator Estimator that was chosen by the search, i.e. estimator which gave highest score (or smallest loss if specified) on the left out data. Not available if refit=False. best_score_ : float Score of best_estimator on the left out data. best_params_ : dict Parameter setting that gave the best results on the hold out data. Notes ----- The parameters selected are those that maximize the score of the held-out data, according to the scoring parameter. If `n_jobs` was set to a value higher than one, the data is copied for each parameter setting(and not `n_jobs` times). This is done for efficiency reasons if individual jobs take very little time, but may raise errors if the dataset is large and not enough memory is available. A workaround in this case is to set `pre_dispatch`. Then, the memory is copied only `pre_dispatch` many times. A reasonable value for `pre_dispatch` is `2 * n_jobs`. See Also -------- :class:`GridSearchCV`: Does exhaustive search over a grid of parameters. :class:`ParameterSampler`: A generator over parameter settins, constructed from param_distributions. """ def __init__(self, estimator, param_distributions, n_iter=10, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', random_state=None, error_score='raise'): self.param_distributions = param_distributions self.n_iter = n_iter self.random_state = random_state super(RandomizedSearchCV, self).__init__( estimator=estimator, scoring=scoring, fit_params=fit_params, n_jobs=n_jobs, iid=iid, refit=refit, cv=cv, verbose=verbose, pre_dispatch=pre_dispatch, error_score=error_score) def fit(self, X, y=None): """Run fit on the estimator with randomly drawn parameters. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vector, where n_samples in the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] or [n_samples, n_output], optional Target relative to X for classification or regression; None for unsupervised learning. """ sampled_params = ParameterSampler(self.param_distributions, self.n_iter, random_state=self.random_state) return self._fit(X, y, sampled_params)
bsd-3-clause
newville/scikit-image
doc/examples/plot_random_walker_segmentation.py
3
2461
""" ========================== Random walker segmentation ========================== The random walker algorithm [1]_ determines the segmentation of an image from a set of markers labeling several phases (2 or more). An anisotropic diffusion equation is solved with tracers initiated at the markers' position. The local diffusivity coefficient is greater if neighboring pixels have similar values, so that diffusion is difficult across high gradients. The label of each unknown pixel is attributed to the label of the known marker that has the highest probability to be reached first during this diffusion process. In this example, two phases are clearly visible, but the data are too noisy to perform the segmentation from the histogram only. We determine markers of the two phases from the extreme tails of the histogram of gray values, and use the random walker for the segmentation. .. [1] *Random walks for image segmentation*, Leo Grady, IEEE Trans. Pattern Anal. Mach. Intell. 2006 Nov; 28(11):1768-83 """ import numpy as np from scipy import ndimage import matplotlib.pyplot as plt from skimage.segmentation import random_walker def microstructure(l=256): """ Synthetic binary data: binary microstructure with blobs. Parameters ---------- l: int, optional linear size of the returned image """ n = 5 x, y = np.ogrid[0:l, 0:l] mask = np.zeros((l, l)) generator = np.random.RandomState(1) points = l * generator.rand(2, n ** 2) mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1 mask = ndimage.gaussian_filter(mask, sigma=l / (4. * n)) return (mask > mask.mean()).astype(np.float) # Generate noisy synthetic data data = microstructure(l=128) data += 0.35 * np.random.randn(*data.shape) markers = np.zeros(data.shape, dtype=np.uint) markers[data < -0.3] = 1 markers[data > 1.3] = 2 # Run random walker algorithm labels = random_walker(data, markers, beta=10, mode='bf') # Plot results fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 3.2)) ax1.imshow(data, cmap='gray', interpolation='nearest') ax1.axis('off') ax1.set_title('Noisy data') ax2.imshow(markers, cmap='hot', interpolation='nearest') ax2.axis('off') ax2.set_title('Markers') ax3.imshow(labels, cmap='gray', interpolation='nearest') ax3.axis('off') ax3.set_title('Segmentation') fig.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1) plt.show()
bsd-3-clause
INM-6/nest-git-migration
testsuite/manualtests/test_tsodyks_depr_fac.py
13
1136
# -*- coding: utf-8 -*- # # test_tsodyks_depr_fac.py # # This file is part of NEST. # # Copyright (C) 2004 The NEST Initiative # # NEST 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 2 of the License, or # (at your option) any later version. # # NEST 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 NEST. If not, see <http://www.gnu.org/licenses/>. from scipy import * from matplotlib.pylab import * from matplotlib.mlab import * def plot_spikes(): dt = 0.1 # time resolution nbins = 1000 N = 500 # number of neurons vm = load('voltmeter-0-0-4.dat') figure(1) clf() plot(vm[:,0], vm[:,1], 'r') xlabel('time / ms') ylabel('$V_m [mV]$') savefig('test_tsodyks_depressing.png') plot_spikes() show()
gpl-2.0
trouden/MultiMediaVerwerking
Labo02/opdracht2.py
1
2427
import cv2 from matplotlib import pyplot as plt import math import numpy as np def saltPepper(imgage, times, kernelWidth): kernelSize = (kernelWidth -1) / 2 img = np.copy(imgage) height, width = img.shape[:2] for n in range(0, times): newImage = np.copy(img) for h in range(kernelSize, height - kernelSize): for w in range(kernelSize, width - kernelSize): array = img[h-kernelSize: h+kernelSize+1, w-kernelSize:w+kernelSize+1] median = np.median(array) newImage.itemset((h,w), median) img = newImage return img def averaging(imgage, times, kernelWidth): img = np.copy(imgage) kernelSize = (kernelWidth - 1) / 2 img = cv2.copyMakeBorder(img, kernelSize, kernelSize, kernelSize, kernelSize, cv2.BORDER_REPLICATE) height, width = img.shape[:2] for n in range(0, times): newImage = np.copy(img) for h in range(kernelSize, height - kernelSize): for w in range(kernelSize, width - kernelSize): newV = 0.0 for i in range(-1 * kernelSize, kernelSize + 1): for j in range(-1 * kernelSize, kernelSize + 1): newV += (img.item(h - i, w - j) / math.pow(kernelWidth, 2)) newImage.itemset((h, w), newV) img = newImage return img[kernelSize:height-kernelSize, kernelSize:width-kernelSize] if __name__ == '__main__': orig = cv2.imread('SaltPepper.jpg') #orig = cv2.resize(orig, (0,0), fx=0.3, fy=0.3) orig = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY) sP1 = saltPepper(orig, 1, 7) sP2 = saltPepper(sP1, 1, 7) sP3 = saltPepper(sP2, 1, 7) avr1 = averaging(orig, 1, 7) avr2 = averaging(avr1, 1, 7) avr3 = averaging(avr2, 1, 7) plt.subplot(241),plt.imshow(orig, cmap='Greys_r'),plt.title('orig') plt.subplot(242),plt.imshow(avr1, cmap='Greys_r'),plt.title('average 1') plt.subplot(243),plt.imshow(avr2, cmap='Greys_r'),plt.title('average 2') plt.subplot(244),plt.imshow(avr3, cmap='Greys_r'),plt.title('average 3') plt.subplot(245),plt.imshow(orig, cmap='Greys_r'),plt.title('orig') plt.subplot(246),plt.imshow(sP1, cmap='Greys_r'),plt.title('median 1') plt.subplot(247),plt.imshow(sP2, cmap='Greys_r'),plt.title('median 2') plt.subplot(248),plt.imshow(sP3, cmap='Greys_r'),plt.title('median 3') plt.show()
mit
caskorg/cask
src/frontend/cask.py
1
18214
"""Implements the main DSE loop in spark.""" import maxbuild import argparse import itertools import json import os import pprint import re import shutil import subprocess import sys import pandas as pd from tabulate import tabulate from html import HTML from bs4 import BeautifulSoup from os import listdir from os.path import isfile, join from scipy import io, sparse from subprocess import call from termcolor import colored import utils PRJ = 'Spmv' TARGET_DFE_MOCK = 'dfe_mock' TARGET_DFE = 'dfe' TARGET_SIM = 'sim' BENCHMARK_NONE = 'none' BENCHMARK_BEST = 'best' BENCHMARK_ALL_TO_ALL = 'all' REP_CSV = 'csv' REP_HTML = 'html' DIR_PATH_RESULTS = 'results' DIR_PATH_LOG = 'logs' DIR_PATH_RUNS = 'runs' DSE_LOG_FILE = 'dse_run.log' PATH_TO_CASK_FILE = os.path.dirname(os.path.abspath(__file__)) PATH_TO_ROOT = os.path.abspath(os.path.join(PATH_TO_CASK_FILE, '../../')) WORKING_DIR = os.getcwd() BUILD_DIR = os.path.join(PATH_TO_ROOT, 'build') SOURCE_DIR = os.path.join(PATH_TO_ROOT, 'src') OUTPUT_DIR = WORKING_DIR pd.options.display.float_format = '{:.2f}'.format def build_path(path=''): print 'Build dir -->', BUILD_DIR return os.path.join(BUILD_DIR, path) def src_path(path=''): return os.path.join(SOURCE_DIR, path) def output_path(path=''): return os.path.join(OUTPUT_DIR, path) def preProcessBenchmark(benchDirPath): entries = [] for f in os.listdir(benchDirPath): info = io.mminfo(os.path.join(benchDirPath, f)) if info[0] == info[1]: info = list(info[1:]) info.append(info[1] / info[0]) info.insert(0, f.replace(r'.mtx', '')) info[1] = int(info[1]) info[2] = int(info[2]) entries.append(info) return sorted(entries, key=lambda x : x[-1], reverse=True) def print_from_iterator(lines_iterator, logfile=None): output = '' if logfile: with open(logfile, 'w') as log: for line in lines_iterator: log.write(line) log.flush() output += line else: for line in lines_iterator: print line output += line return output def runDse(benchFile, paramsFile, target, skipExecution=False): dseFile = "dse_out.json" if not skipExecution: utils.execute([build_path('main'), benchFile, paramsFile], DSE_LOG_FILE) else: print ' --> Skip DSE run, load results from', dseFile params = [] prjs = [] architectures = [] with open(dseFile) as f: data = json.load(f) for arch in data['best_architectures']: ps = arch['architecture_params'] est_impl_ps = arch['estimated_impl_params'] matrix = arch['matrices'][0] params.append(ps) # XXX Should check for identical architectures before assigning new ID prj_id = len(prjs) architectures.append( [ os.path.basename(matrix).replace('.mtx', ''), prj_id, int(ps['cache_size']), int(ps['input_width']), int(ps['num_pipes']), int(ps['num_controllers']), int(ps['max_rows']), # The model uses BRAM36, the McTools use BRAM18 int(est_impl_ps['BRAMs']), int(est_impl_ps['LUTs']), int(est_impl_ps['FFs']), int(est_impl_ps['DSPs']), float(est_impl_ps['memory_bandwidth']), float(arch['estimated_gflops']), ]) prjs.append(maxbuild.PrjConfig(ps, target, PRJ, prj_id, src_path('spmv/build/'))) return prjs, architectures def buildClient(target): print ' >> Building Client ----' utils.execute(['make', '-C', build_path(), 'test_spmv_' + target]) def runClient(benchmark, target, prj=None): print ' ---- Benchmarking Client ----' for p in benchmark: cmd = [] if target == TARGET_DFE: cmd = ['bash', src_path('frontend/spark_dfe_run.sh'), p] elif target == TARGET_SIM: cmd = ['bash', src_path('frontend/simrunner'), build_path('test_spmv_sim'), p] elif target == TARGET_DFE_MOCK: cmd = ['bash', src_path('frontend/mockrunner'), build_path('test_spmv_dfe_mock'), p] outF = 'runs/run_' + target + '_' if prj: cmd.append(str(prj.prj_id)) outF += prj.buildName() else: outF += 'benchmark_best' outF += '_' + os.path.basename(p) print ' -->', p, 'outFile =', outF try: out = subprocess.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: print ' ',e out = e.output mode = 'w' if prj: if os.path.exists(outF): os.remove(outF) mode = 'a' with open(outF, mode) as f: for line in out: f.write(line) class Spark: def __init__(self, target, prjs, cppCompiler='g++'): self.target = target self.prjs =prjs self.cppCompiler = cppCompiler def runLibraryBuild(self, prjs, libName): print ' >> Building Library' interfaceFile = 'GeneratedImplementations.cpp' deviceO = 'SmpvDeviceInterface.o' maxfileO = 'maxfile.o' prj_includes = [] obj_files = [] if self.target != TARGET_DFE_MOCK: for p in prjs: objFile = p.name + '.o' utils.execute( ['sliccompile', p.maxFileLocation(), objFile], logfile=p.logFile()) prj_includes.append('-I' + p.resultsDir()) obj_files.append(objFile) cmd =[ self.cppCompiler, '-c', '-Wall', '-std=c++11', '-fPIC', '-I' + src_path('runtime'), ] # TODO move these checks in an earlier phase mcdir = os.getenv('MAXCOMPILERDIR') maxosdir = os.getenv('MAXELEROSDIR') if mcdir and maxosdir and self.target != TARGET_DFE_MOCK: cmd.extend([ '-I' + mcdir + '/include', '-I' + mcdir + '/include/slic', '-I' + maxosdir + '/include']) cmd.extend(prj_includes) cmd.extend([ interfaceFile, '-o', deviceO ]) out = subprocess.check_output(cmd) cmd =[ self.cppCompiler, '-fPIC', '--std=c++11', '-shared', '-Wl,-soname,{0}.0'.format(libName), '-o', libName] cmd.extend(obj_files + [deviceO]) if mcdir and maxosdir and self.target != TARGET_DFE_MOCK: cmd.extend([ '-L' + os.path.join(mcdir, 'lib'), '-L' + os.path.join(maxosdir, 'lib'), '-lmaxeleros', '-lslic',]) cmd.extend(['-lm', '-lpthread']) utils.execute(cmd, 'lib_build.log') # copy the generated library libDir = 'lib-generated' if not os.path.exists(libDir): os.makedirs(libDir) shutil.copy(libName, libDir + '/{}.0'.format(libName)) shutil.copy(libName, libDir) def generateImplementationHeader(self, prjs): genFilePath = output_path('GeneratedImplementations.cpp') with open(genFilePath, 'w') as f: # Include maxfile headers if self.target != TARGET_DFE_MOCK: for p in prjs: f.write('#include <{0}.h>\n'.format(p.name)) # Defines struct formats f.write('#include "{0}"\n'.format('GeneratedImplSupport.hpp')) f.write('using namespace cask::runtime;\n') f.write(""" cask::runtime::SpmvImplementationLoader::SpmvImplementationLoader() { """) for i in range(len(prjs)): p = prjs[i] f.write('this->impls.push_back(') runFunction = p.name writeFunction = p.name + '_dramWrite' readFunction = p.name + '_dramRead' dramReductionEnabled = p.name + '_dramReductionEnabled' if self.target == TARGET_DFE_MOCK: runFunction = 'cask::runtime::spmvRunMock' writeFunction = 'cask::runtime::spmvWriteMock' readFunction = 'cask::runtime::spmvReadMock' dramReductionEnabled = 'false' f.write( 'new GeneratedSpmvImplementation({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}));'.format( p.prj_id, runFunction, writeFunction, readFunction, p.getParam('max_rows'), p.getParam('num_pipes'), p.getParam('cache_size'), p.getParam('input_width'), dramReductionEnabled, p.getParam('num_controllers'))) f.write('\n}') def runBuilds(self): print ' >> Building Hardware Implementations' if self.target != TARGET_DFE_MOCK: b = maxbuild.MaxBuildRunner(poolSize=6) b.runBuilds(self.prjs) # library generation is sequential self.generateImplementationHeader(self.prjs) self.runLibraryBuild(self.prjs, 'libSpmv_' + self.target + '.so') # buildClient(self.target) def runBenchmark(self, benchmark, benchmark_mode): if benchmark_mode == BENCHMARK_NONE: return if benchmark_mode == BENCHMARK_ALL_TO_ALL: for p in self.prjs: runClient(benchmark, self.target, p) else: runClient(benchmark, self.target) def logTexTable(entries, fpath): rows = [] float_prec = '.3f' # find maximum length length = 0 for e in itertools.chain.from_iterable(entries): l = len(str(e)) if type(e) is float: l = len(('{0:' + float_prec + '}').format(e)) length = max(length, l) fmt = '{0:' + str(length) + '}' float_fmt = '{0:' + str(length) + float_prec + '}' for entry in entries: row = fmt.format(entry[0]) for field in entry[1:]: f = fmt if type(field) is float: f = float_fmt row += ' &' + f.format(field) rows.append(row) table = '\\begin{{tabular}}{{{0}}} \n{1}\n\end{{tabular}}'.format( 'l' * len(entries[0]), ' \\\\\n'.join(rows) + r' \\' ) with open(fpath, 'w') as f: f.write(table) def logDseResults(benchmark_df, arch_df): pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000) df = pd.merge(benchmark_df, arch_df, left_on='Matrix', right_on='Matrix') write_result('dse_matrix_arch.tex', df.to_latex()) write_result('dse_matrix_arch.html', df.to_html()) return df def postProcessResults(prjs, benchmark, benchmark_df, arch_df, arch_build_df, dirpath): utils.info('Post-processing results') # need to reconstruct a (matrix, architecture) relation from run files; # this relation also stores execution results (e.g. bwidth, gflops) df = pd.DataFrame([], columns=['Id', 'Matrix', 'GFLOPs']) for p in os.listdir(dirpath): with open(os.path.join(dirpath, p)) as f: matrix = None archId = None gflops = None for l in f: m = re.match(r'Config ArchitectureId (\d*).*', l) if m: matrix = int(m.group(1)) m = re.match(r'Param MatrixPath ([\w/-]*)', l) if m: archId = os.path.basename(m.group(1)) m = re.match(r'Result Simple Gflops \(actual\)=(.*),', l) if m: gflops = float(m.group(1)) if gflops and matrix and archId is not None: new_df = pd.DataFrame([[matrix, archId, gflops]], columns=['Id', 'Matrix', 'GFLOPs']) df = df.append(new_df, ignore_index=True) break # build a table compare est and measured results df1 = pd.merge(benchmark_df, df, left_on='Matrix', right_on='Matrix') df2 = pd.merge(df1, arch_df, left_on='Id', right_on='Id') df2 = pd.merge(df2, arch_build_df, left_on='Id', right_on='Id') # keep only some interesting columns and reorderd them df2 = df2[['Matrix_x', 'Order', 'Nonzeros', 'Nnz/row', 'Cx', 'k', 'Np', 'Cb', 'Logic %', 'DSP %', 'BRAM %', 'BWidth', 'GFLOPs_x', 'GFLOPs_y']] write_result('matrix_arch_before_after.tex', df2.to_latex(index=False)) print arch_build_df print df2 def check_make_dir(dirname): if not os.path.exists(dirname): os.makedirs(dirname) def make_clean_dir(dirname): if os.path.exists(dirname): shutil.rmtree(dirname) os.makedirs(dirname) def write_result(fname, data): with open(os.path.join(DIR_PATH_RESULTS, fname), 'w') as f: f.write(data) def build_html(): matrices = [] check_make_dir('matrices_html') for root, dirs, files in os.walk('matrices'): h = HTML() matrix = os.path.basename(root) if not dirs: print root, dirs, files h.p('Matrix: ' + matrix) sparsity_plot = None for f in files: if not f.endswith('.png'): with open(os.path.join(root, f)) as fin: h.p(fin.read(), style='white-space: pre-wrap;') else: p = h.p() p.img(src=matrix + '.png') sparsity_plot = os.path.join(root, f) path = 'matrices_html/' + matrix + '.html' with open(path, 'w') as fout: matrices.append(matrix + '.html') fout.write(str(h)) shutil.copyfile(sparsity_plot, 'matrices_html/' + matrix + '.png') with open('matrices_html/index.html', 'w') as fout: h = HTML() h.p('matrices: ') l = h.ol for m in matrices: l.li.a(m, href=m) fout.write(str(h)) def main(): parser = argparse.ArgumentParser(description='Run Spark DSE flow') parser.add_argument('-d', '--dse', action='store_true', default=False) parser.add_argument('-ds', '--dse-skip', action='store_true', default=False) parser.add_argument('-t', '--target', choices=[TARGET_DFE, TARGET_SIM, TARGET_DFE_MOCK], required=True) parser.add_argument('-p', '--param-file', required=True) parser.add_argument('-b', '--benchmark-dir', required=True) parser.add_argument('-st', '--build_start', type=int, default=None) parser.add_argument('-en', '--build_end', type=int, default=None) parser.add_argument('-bmst', '--benchmark_start', type=int, default=None) parser.add_argument('-bmen', '--benchmark_end', type=int, default=None) parser.add_argument('-cpp', '--cpp_compiler', default='g++') parser.add_argument('-bm', '--benchmarking-mode', choices=[BENCHMARK_BEST, BENCHMARK_ALL_TO_ALL, BENCHMARK_NONE], default=BENCHMARK_NONE) parser.add_argument('-rb', '--run-builds', default=False, action='store_true') parser.add_argument('-rep', '--reporting', choices=[REP_CSV, REP_HTML], default=REP_CSV) args = parser.parse_args() buildName = PRJ + '_' + args.target prjs = [] ## Prepare some directories check_make_dir('results') check_make_dir('logs') if args.benchmarking_mode != BENCHMARK_NONE: make_clean_dir('runs') ## Run DSE pass prjs = [] benchmark_df = pd.DataFrame( preProcessBenchmark(args.benchmark_dir), columns = ['Matrix', 'Order', 'Nonzeros', 'Format', 'Type', 'Pattern', 'Nnz/row']) if args.dse: utils.info('Running DSE flow') # the DSE tool produces a JSON file with architectures to be built prjs, log_archs = runDse(args.benchmark_dir, args.param_file, args.target, args.dse_skip) else: # load default parameters values from param_file with open(args.param_file) as f: data = json.load(f) ps = {} for k, v in data['dse_params'].iteritems(): ps[k] = str(v['default']) # XXX prj_id is not defined at this point, how do we create a project with the default values? params = [maxbuild.PrjConfig(ps, args.target, PRJ, prj_id, '../spmv/build/')] arch_df = pd.DataFrame(log_archs, columns = ['Matrix', 'Id', 'Cx', 'k', 'Np', 'Nc', 'Cb', 'BRAMs', 'LUTs', 'FFs', 'DSPs', 'BWidth', 'GFLOPs']) merged_df = logDseResults(benchmark_df, arch_df) print merged_df p = os.path.abspath(args.benchmark_dir) benchmark = [ join(p, f) for f in listdir(p) if isfile(join(p,f)) ] if args.benchmark_start != None and args.benchmark_end != None: benchmark = benchmark[args.benchmark_start:args.benchmark_end] ps = prjs if args.build_start != None and args.build_end != None: ps = prjs[args.build_start:args.build_end] spark = Spark(args.target, ps, args.cpp_compiler) if args.run_builds: utils.info('Running builds') spark.runBuilds() if args.target == TARGET_DFE: prj_info = [] header = ['Id', 'Logic', 'Logic %', 'DSP', 'DSP %', 'BRAM', 'BRAM %'] for p in ps: resUsage = p.getBuildResourceUsage() logic = resUsage['Logic utilization'] dsps = resUsage['DSP blocks'] # XXX: block memory type depends on the device # brams = resUsage['Block memory (BRAM18)'] brams = resUsage['Block memory (M20K)'] prj_info.append([ p.prj_id, logic[0], logic[0] / float(logic[1]) * 100, dsps[0], dsps[0] / float(dsps[1]) * 100, brams[0], brams[0] / float(brams[1]) * 100 ]) arch_build_df = pd.DataFrame(prj_info, columns = header) if args.benchmarking_mode != BENCHMARK_NONE: utils.info('Running benchmark') spark.runBenchmark(benchmark, args.benchmarking_mode) # Post-process results if args.target == TARGET_DFE: postProcessResults(ps, benchmark, benchmark_df, arch_df, arch_build_df, DIR_PATH_RUNS) # Reporting if args.reporting == REP_HTML: utils.info('Generating HTML reports') for p in benchmark: out, out_err = utils.execute(['python', src_path('frontend/sparsegrind.py'), '-f', 'mm', '-a', 'summary', p], silent=False) outputDir = os.path.join('matrices', os.path.basename(p).replace('.mtx', '')) summaryFile = os.path.join(outputDir, 'summary.csv') check_make_dir(outputDir) with open(summaryFile, 'w') as f: f.write(out) utils.execute(['python', src_path('frontend/sparsegrind.py'), '-f', 'mm', '-a', 'plot', p], silent=False) shutil.copy('sparsity.png', outputDir) build_html() # TODO also need to add hardware / simulation results to report # matrix_sim_run=${matrix_dir}/sim_run.csv # cd scripts && bash simrunner ../build/test_spmv_sim ../${f} >> ../${matrix_sim_run} && cd .. bs = BeautifulSoup(merged_df.to_html(), 'html.parser') for row in bs.findAll('tr'): cols = row.findAll('td') if cols: matrixName = cols[0].string new_tag = bs.new_tag('a', href='matrices/' + matrixName + '.html') new_tag.string = matrixName cols[0].string = '' cols[0].append(new_tag) with open('matrices_html/matrix_index.html', 'w') as f: f.write(str(bs)) if __name__ == '__main__': main()
mit
hugobowne/scikit-learn
examples/semi_supervised/plot_label_propagation_digits.py
268
2723
""" =================================================== Label Propagation digits: Demonstrating performance =================================================== This example demonstrates the power of semisupervised learning by training a Label Spreading model to classify handwritten digits with sets of very few labels. The handwritten digit dataset has 1797 total points. The model will be trained using all points, but only 30 will be labeled. Results in the form of a confusion matrix and a series of metrics over each class will be very good. At the end, the top 10 most uncertain predictions will be shown. """ print(__doc__) # Authors: Clay Woolam <clay@woolam.org> # Licence: BSD import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn import datasets from sklearn.semi_supervised import label_propagation from sklearn.metrics import confusion_matrix, classification_report digits = datasets.load_digits() rng = np.random.RandomState(0) indices = np.arange(len(digits.data)) rng.shuffle(indices) X = digits.data[indices[:330]] y = digits.target[indices[:330]] images = digits.images[indices[:330]] n_total_samples = len(y) n_labeled_points = 30 indices = np.arange(n_total_samples) unlabeled_set = indices[n_labeled_points:] # shuffle everything around y_train = np.copy(y) y_train[unlabeled_set] = -1 ############################################################################### # Learn with LabelSpreading lp_model = label_propagation.LabelSpreading(gamma=0.25, max_iter=5) lp_model.fit(X, y_train) predicted_labels = lp_model.transduction_[unlabeled_set] true_labels = y[unlabeled_set] cm = confusion_matrix(true_labels, predicted_labels, labels=lp_model.classes_) print("Label Spreading model: %d labeled & %d unlabeled points (%d total)" % (n_labeled_points, n_total_samples - n_labeled_points, n_total_samples)) print(classification_report(true_labels, predicted_labels)) print("Confusion matrix") print(cm) # calculate uncertainty values for each transduced distribution pred_entropies = stats.distributions.entropy(lp_model.label_distributions_.T) # pick the top 10 most uncertain labels uncertainty_index = np.argsort(pred_entropies)[-10:] ############################################################################### # plot f = plt.figure(figsize=(7, 5)) for index, image_index in enumerate(uncertainty_index): image = images[image_index] sub = f.add_subplot(2, 5, index + 1) sub.imshow(image, cmap=plt.cm.gray_r) plt.xticks([]) plt.yticks([]) sub.set_title('predict: %i\ntrue: %i' % ( lp_model.transduction_[image_index], y[image_index])) f.suptitle('Learning with small amount of labeled data') plt.show()
bsd-3-clause
aitoralmeida/dl_activity_recognition
sensor2vec/casas_aruba_dataset/partial_dataset_creator.py
1
3064
# -*- coding: utf-8 -*- """ Created on Thu Aug 31 10:27:31 2017 @author: gazkune Script to generate several csv files for aruba dataset """ import sys import pandas as pd import numpy as np # The input dataset DATASET = "aruba_complete_dataset.csv" # Output datasets COMPLETE_NUMERIC = "aruba_complete_numeric.csv" NO_T = "aruba_no_t.csv" INCR = 2 # define the temperature range to be used COMPLETE_RANGES = "aruba_complete_ranges_" + str(INCR) + ".csv" # -------------------------------------------------------- # COMPLETE_NUMERIC case def storeCompleteNumeric(idf): sensors = idf["sensor"].values values = idf["value"].values try: assert(len(sensors) == len(values)) except AssertionError: print 'Number of sensors and values are not equal; sensors:', len(sensors), 'values:', len(values) actions = sensors + '_' + values idf["action"] = actions # Decision: I will not store 'sensor', 'value' and 'event' columns idf.to_csv(COMPLETE_NUMERIC, columns=['timestamp', 'action', 'activity'], header=False, index=False) # -------------------------------------------------------- # NO_T case def storeNoT(idf): auxdf = idf[np.logical_not(idf["sensor"].str.contains("T0"))] sensors = auxdf["sensor"].values values = auxdf["value"].values try: assert(len(sensors) == len(values)) except AssertionError: print 'Number of sensors and values are not equal; sensors:', len(sensors), 'values:', len(values) actions = sensors + '_' + values auxdf["action"] = actions # Decision: I will not store 'sensor', 'value' and 'event' columns auxdf.to_csv(NO_T, columns=['timestamp', 'action', 'activity'], header=False, index=False) # -------------------------------------------------------- # COMPLETE_RANGES def storeCompleteRanges(idf): tempdf = idf[idf["sensor"].str.contains("T0")] mintemp = round(float(min(tempdf["value"]))) maxtemp = round(float(max(tempdf["value"]))) bins = np.arange(mintemp, maxtemp, INCR) temperatures = tempdf["value"].values.astype(float) # Discretize temperatures inds = np.digitize(temperatures, bins) temperatures = bins[inds-1] tempdf["value"] = temperatures.astype(str) idf.ix[tempdf.index, "value"] = tempdf["value"] sensors = idf["sensor"].values values = idf["value"].values try: assert(len(sensors) == len(values)) except AssertionError: print 'Number of sensors and values are not equal; sensors:', len(sensors), 'values:', len(values) actions = sensors + '_' + values idf["action"] = actions # Decision: I will not store 'sensor', 'value' and 'event' columns idf.to_csv(COMPLETE_RANGES, columns=['timestamp', 'action', 'activity'], header=False, index=False) # Load Aruba dataset idf = pd.read_csv(DATASET, parse_dates=[0], header=None, sep=',') idf.columns = ["timestamp", 'sensor', 'value', 'activity', 'event'] # Generate the desired output storeCompleteNumeric(idf) storeNoT(idf) storeCompleteRanges(idf)
gpl-3.0
YihaoLu/statsmodels
examples/incomplete/wls_extended.py
33
16137
""" Weighted Least Squares example is extended to look at the meaning of rsquared in WLS, at outliers, compares with RLM and a short bootstrap """ from __future__ import print_function import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt data = sm.datasets.ccard.load() data.exog = sm.add_constant(data.exog, prepend=False) ols_fit = sm.OLS(data.endog, data.exog).fit() # perhaps the residuals from this fit depend on the square of income incomesq = data.exog[:,2] plt.scatter(incomesq, ols_fit.resid) #@savefig wls_resid_check.png plt.grid() # If we think that the variance is proportional to income**2 # we would want to weight the regression by income # the weights argument in WLS weights the regression by its square root # and since income enters the equation, if we have income/income # it becomes the constant, so we would want to perform # this type of regression without an explicit constant in the design #..data.exog = data.exog[:,:-1] wls_fit = sm.WLS(data.endog, data.exog[:,:-1], weights=1/incomesq).fit() # This however, leads to difficulties in interpreting the post-estimation # statistics. Statsmodels does not yet handle this elegantly, but # the following may be more appropriate # explained sum of squares ess = wls_fit.uncentered_tss - wls_fit.ssr # rsquared rsquared = ess/wls_fit.uncentered_tss # mean squared error of the model mse_model = ess/(wls_fit.df_model + 1) # add back the dof of the constant # f statistic fvalue = mse_model/wls_fit.mse_resid # adjusted r-squared rsquared_adj = 1 -(wls_fit.nobs)/(wls_fit.df_resid)*(1-rsquared) #Trying to figure out what's going on in this example #---------------------------------------------------- #JP: I need to look at this again. Even if I exclude the weight variable # from the regressors and keep the constant in then the reported rsquared # stays small. Below also compared using squared or sqrt of weight variable. # TODO: need to add 45 degree line to graphs wls_fit3 = sm.WLS(data.endog, data.exog[:,(0,1,3,4)], weights=1/incomesq).fit() print(wls_fit3.summary()) print('corrected rsquared') print((wls_fit3.uncentered_tss - wls_fit3.ssr)/wls_fit3.uncentered_tss) plt.figure(); plt.title('WLS dropping heteroscedasticity variable from regressors'); plt.plot(data.endog, wls_fit3.fittedvalues, 'o'); plt.xlim([0,2000]); #@savefig wls_drop_het.png plt.ylim([0,2000]); print('raw correlation of endog and fittedvalues') print(np.corrcoef(data.endog, wls_fit.fittedvalues)) print('raw correlation coefficient of endog and fittedvalues squared') print(np.corrcoef(data.endog, wls_fit.fittedvalues)[0,1]**2) # compare with robust regression, # heteroscedasticity correction downweights the outliers rlm_fit = sm.RLM(data.endog, data.exog).fit() plt.figure(); plt.title('using robust for comparison'); plt.plot(data.endog, rlm_fit.fittedvalues, 'o'); plt.xlim([0,2000]); #@savefig wls_robust_compare.png plt.ylim([0,2000]); #What is going on? A more systematic look at the data #---------------------------------------------------- # two helper functions def getrsq(fitresult): '''calculates rsquared residual, total and explained sums of squares Parameters ---------- fitresult : instance of Regression Result class, or tuple of (resid, endog) arrays regression residuals and endogenous variable Returns ------- rsquared residual sum of squares (centered) total sum of squares explained sum of squares (for centered) ''' if hasattr(fitresult, 'resid') and hasattr(fitresult, 'model'): resid = fitresult.resid endog = fitresult.model.endog nobs = fitresult.nobs else: resid = fitresult[0] endog = fitresult[1] nobs = resid.shape[0] rss = np.dot(resid, resid) tss = np.var(endog)*nobs return 1-rss/tss, rss, tss, tss-rss def index_trim_outlier(resid, k): '''returns indices to residual array with k outliers removed Parameters ---------- resid : array_like, 1d data vector, usually residuals of a regression k : int number of outliers to remove Returns ------- trimmed_index : array, 1d index array with k outliers removed outlier_index : array, 1d index array of k outliers Notes ----- Outliers are defined as the k observations with the largest absolute values. ''' sort_index = np.argsort(np.abs(resid)) # index of non-outlier trimmed_index = np.sort(sort_index[:-k]) outlier_index = np.sort(sort_index[-k:]) return trimmed_index, outlier_index #Comparing estimation results for ols, rlm and wls with and without outliers #--------------------------------------------------------------------------- #ols_test_fit = sm.OLS(data.endog, data.exog).fit() olskeep, olsoutl = index_trim_outlier(ols_fit.resid, 2) print('ols outliers', olsoutl, ols_fit.resid[olsoutl]) ols_fit_rm2 = sm.OLS(data.endog[olskeep], data.exog[olskeep,:]).fit() rlm_fit_rm2 = sm.RLM(data.endog[olskeep], data.exog[olskeep,:]).fit() #weights = 1/incomesq results = [ols_fit, ols_fit_rm2, rlm_fit, rlm_fit_rm2] #Note: I think incomesq is already square for weights in [1/incomesq, 1/incomesq**2, np.sqrt(incomesq)]: print('\nComparison OLS and WLS with and without outliers') wls_fit0 = sm.WLS(data.endog, data.exog, weights=weights).fit() wls_fit_rm2 = sm.WLS(data.endog[olskeep], data.exog[olskeep,:], weights=weights[olskeep]).fit() wlskeep, wlsoutl = index_trim_outlier(ols_fit.resid, 2) print('2 outliers candidates and residuals') print(wlsoutl, wls_fit.resid[olsoutl]) # redundant because ols and wls outliers are the same: ##wls_fit_rm2_ = sm.WLS(data.endog[wlskeep], data.exog[wlskeep,:], ## weights=1/incomesq[wlskeep]).fit() print('outliers ols, wls:', olsoutl, wlsoutl) print('rsquared') print('ols vs ols rm2', ols_fit.rsquared, ols_fit_rm2.rsquared) print('wls vs wls rm2', wls_fit0.rsquared, wls_fit_rm2.rsquared) #, wls_fit_rm2_.rsquared print('compare R2_resid versus R2_wresid') print('ols minus 2', getrsq(ols_fit_rm2)[0],) print(getrsq((ols_fit_rm2.wresid, ols_fit_rm2.model.wendog))[0]) print('wls ', getrsq(wls_fit)[0],) print(getrsq((wls_fit.wresid, wls_fit.model.wendog))[0]) print('wls minus 2', getrsq(wls_fit_rm2)[0]) # next is same as wls_fit_rm2.rsquared for cross checking print(getrsq((wls_fit_rm2.wresid, wls_fit_rm2.model.wendog))[0]) #print(getrsq(wls_fit_rm2_)[0], #print(getrsq((wls_fit_rm2_.wresid, wls_fit_rm2_.model.wendog))[0] results.extend([wls_fit0, wls_fit_rm2]) print(' ols ols_rm2 rlm rlm_rm2 wls (lin) wls_rm2 (lin) wls (squ) wls_rm2 (squ) wls (sqrt) wls_rm2 (sqrt)') print('Parameter estimates') print(np.column_stack([r.params for r in results])) print('R2 original data, next line R2 weighted data') print(np.column_stack([getattr(r, 'rsquared', None) for r in results])) print('Standard errors') print(np.column_stack([getattr(r, 'bse', None) for r in results])) print('Heteroscedasticity robust standard errors (with ols)') print('with outliers') print(np.column_stack([getattr(ols_fit, se, None) for se in ['HC0_se', 'HC1_se', 'HC2_se', 'HC3_se']])) #..''' #.. #.. ols ols_rm2 rlm rlm_rm2 wls (lin) wls_rm2 (lin) wls (squ) wls_rm2 (squ) wls (sqrt) wls_rm2 (sqrt) #..Parameter estimates #..[[ -3.08181404 -5.06103843 -4.98510966 -5.34410309 -2.69418516 -3.1305703 -1.43815462 -1.58893054 -3.57074829 -6.80053364] #.. [ 234.34702702 115.08753715 129.85391456 109.01433492 158.42697752 128.38182357 60.95113284 100.25000841 254.82166855 103.75834726] #.. [ -14.99684418 -5.77558429 -6.46204829 -4.77409191 -7.24928987 -7.41228893 6.84943071 -3.34972494 -16.40524256 -4.5924465 ] #.. [ 27.94090839 85.46566835 89.91389709 95.85086459 60.44877369 79.7759146 55.9884469 60.97199734 -3.8085159 84.69170048] #.. [-237.1465136 39.51639838 -15.50014814 31.39771833 -114.10886935 -40.04207242 -6.41976501 -38.83583228 -260.72084271 117.20540179]] #.. #..R2 original data, next line R2 weighted data #..[[ 0.24357792 0.31745994 0.19220308 0.30527648 0.22861236 0.3112333 0.06573949 0.29366904 0.24114325 0.31218669]] #..[[ 0.24357791 0.31745994 None None 0.05936888 0.0679071 0.06661848 0.12769654 0.35326686 0.54681225]] #.. #..-> R2 with weighted data is jumping all over #.. #..standard errors #..[[ 5.51471653 3.31028758 2.61580069 2.39537089 3.80730631 2.90027255 2.71141739 2.46959477 6.37593755 3.39477842] #.. [ 80.36595035 49.35949263 38.12005692 35.71722666 76.39115431 58.35231328 87.18452039 80.30086861 86.99568216 47.58202096] #.. [ 7.46933695 4.55366113 3.54293763 3.29509357 9.72433732 7.41259156 15.15205888 14.10674821 7.18302629 3.91640711] #.. [ 82.92232357 50.54681754 39.33262384 36.57639175 58.55088753 44.82218676 43.11017757 39.31097542 96.4077482 52.57314209] #.. [ 199.35166485 122.1287718 94.55866295 88.3741058 139.68749646 106.89445525 115.79258539 105.99258363 239.38105863 130.32619908]] #.. #..robust standard errors (with ols) #..with outliers #.. HC0_se HC1_se HC2_se HC3_se' #..[[ 3.30166123 3.42264107 3.4477148 3.60462409] #.. [ 88.86635165 92.12260235 92.08368378 95.48159869] #.. [ 6.94456348 7.19902694 7.19953754 7.47634779] #.. [ 92.18777672 95.56573144 95.67211143 99.31427277] #.. [ 212.9905298 220.79495237 221.08892661 229.57434782]] #.. #..removing 2 outliers #..[[ 2.57840843 2.67574088 2.68958007 2.80968452] #.. [ 36.21720995 37.58437497 37.69555106 39.51362437] #.. [ 3.1156149 3.23322638 3.27353882 3.49104794] #.. [ 50.09789409 51.98904166 51.89530067 53.79478834] #.. [ 94.27094886 97.82958699 98.25588281 102.60375381]] #.. #.. #..''' # a quick bootstrap analysis # -------------------------- # #(I didn't check whether this is fully correct statistically) #**With OLS on full sample** nobs, nvar = data.exog.shape niter = 2000 bootres = np.zeros((niter, nvar*2)) for it in range(niter): rind = np.random.randint(nobs, size=nobs) endog = data.endog[rind] exog = data.exog[rind,:] res = sm.OLS(endog, exog).fit() bootres[it, :nvar] = res.params bootres[it, nvar:] = res.bse np.set_print(options(linewidth=200)) print('Bootstrap Results of parameters and parameter standard deviation OLS') print('Parameter estimates') print('median', np.median(bootres[:,:5], 0)) print('mean ', np.mean(bootres[:,:5], 0)) print('std ', np.std(bootres[:,:5], 0)) print('Standard deviation of parameter estimates') print('median', np.median(bootres[:,5:], 0)) print('mean ', np.mean(bootres[:,5:], 0)) print('std ', np.std(bootres[:,5:], 0)) plt.figure() for i in range(4): plt.subplot(2,2,i+1) plt.hist(bootres[:,i],50) plt.title('var%d'%i) #@savefig wls_bootstrap.png plt.figtext(0.5, 0.935, 'OLS Bootstrap', ha='center', color='black', weight='bold', size='large') #**With WLS on sample with outliers removed** data_endog = data.endog[olskeep] data_exog = data.exog[olskeep,:] incomesq_rm2 = incomesq[olskeep] nobs, nvar = data_exog.shape niter = 500 # a bit slow bootreswls = np.zeros((niter, nvar*2)) for it in range(niter): rind = np.random.randint(nobs, size=nobs) endog = data_endog[rind] exog = data_exog[rind,:] res = sm.WLS(endog, exog, weights=1/incomesq[rind,:]).fit() bootreswls[it, :nvar] = res.params bootreswls[it, nvar:] = res.bse print('Bootstrap Results of parameters and parameter standard deviation',) print('WLS removed 2 outliers from sample') print('Parameter estimates') print('median', np.median(bootreswls[:,:5], 0)) print('mean ', np.mean(bootreswls[:,:5], 0)) print('std ', np.std(bootreswls[:,:5], 0)) print('Standard deviation of parameter estimates') print('median', np.median(bootreswls[:,5:], 0)) print('mean ', np.mean(bootreswls[:,5:], 0)) print('std ', np.std(bootreswls[:,5:], 0)) plt.figure() for i in range(4): plt.subplot(2,2,i+1) plt.hist(bootreswls[:,i],50) plt.title('var%d'%i) #@savefig wls_bootstrap_rm2.png plt.figtext(0.5, 0.935, 'WLS rm2 Bootstrap', ha='center', color='black', weight='bold', size='large') #..plt.show() #..plt.close('all') #:: # # The following a random variables not fixed by a seed # # Bootstrap Results of parameters and parameter standard deviation # OLS # # Parameter estimates # median [ -3.26216383 228.52546429 -14.57239967 34.27155426 -227.02816597] # mean [ -2.89855173 234.37139359 -14.98726881 27.96375666 -243.18361746] # std [ 3.78704907 97.35797802 9.16316538 94.65031973 221.79444244] # # Standard deviation of parameter estimates # median [ 5.44701033 81.96921398 7.58642431 80.64906783 200.19167735] # mean [ 5.44840542 86.02554883 8.56750041 80.41864084 201.81196849] # std [ 1.43425083 29.74806562 4.22063268 19.14973277 55.34848348] # # Bootstrap Results of parameters and parameter standard deviation # WLS removed 2 outliers from sample # # Parameter estimates # median [ -3.95876112 137.10419042 -9.29131131 88.40265447 -44.21091869] # mean [ -3.67485724 135.42681207 -8.7499235 89.74703443 -46.38622848] # std [ 2.96908679 56.36648967 7.03870751 48.51201918 106.92466097] # # Standard deviation of parameter estimates # median [ 2.89349748 59.19454402 6.70583332 45.40987953 119.05241283] # mean [ 2.97600894 60.14540249 6.92102065 45.66077486 121.35519673] # std [ 0.55378808 11.77831934 1.69289179 7.4911526 23.72821085] # # # #Conclusion: problem with outliers and possibly heteroscedasticity #----------------------------------------------------------------- # #in bootstrap results # #* bse in OLS underestimates the standard deviation of the parameters # compared to standard deviation in bootstrap #* OLS heteroscedasticity corrected standard errors for the original # data (above) are close to bootstrap std #* using WLS with 2 outliers removed has a relatively good match between # the mean or median bse and the std of the parameter estimates in the # bootstrap # #We could also include rsquared in bootstrap, and do it also for RLM. #The problems could also mean that the linearity assumption is violated, #e.g. try non-linear transformation of exog variables, but linear #in parameters. # # #for statsmodels # # * In this case rsquared for original data looks less random/arbitrary. # * Don't change definition of rsquared from centered tss to uncentered # tss when calculating rsquared in WLS if the original exog contains # a constant. The increase in rsquared because of a change in definition # will be very misleading. # * Whether there is a constant in the transformed exog, wexog, or not, # might affect also the degrees of freedom calculation, but I haven't # checked this. I would guess that the df_model should stay the same, # but needs to be verified with a textbook. # * df_model has to be adjusted if the original data does not have a # constant, e.g. when regressing an endog on a single exog variable # without constant. This case might require also a redefinition of # the rsquare and f statistic for the regression anova to use the # uncentered tss. # This can be done through keyword parameter to model.__init__ or # through autodedection with hasconst = (exog.var(0)<1e-10).any() # I'm not sure about fixed effects with a full dummy set but # without a constant. In this case autodedection wouldn't work this # way. Also, I'm not sure whether a ddof keyword parameter can also # handle the hasconst case.
bsd-3-clause
jeffery-do/Vizdoombot
doom/lib/python3.5/site-packages/matplotlib/transforms.py
7
96105
""" matplotlib includes a framework for arbitrary geometric transformations that is used determine the final position of all elements drawn on the canvas. Transforms are composed into trees of :class:`TransformNode` objects whose actual value depends on their children. When the contents of children change, their parents are automatically invalidated. The next time an invalidated transform is accessed, it is recomputed to reflect those changes. This invalidation/caching approach prevents unnecessary recomputations of transforms, and contributes to better interactive performance. For example, here is a graph of the transform tree used to plot data to the graph: .. image:: ../_static/transforms.png The framework can be used for both affine and non-affine transformations. However, for speed, we want use the backend renderers to perform affine transformations whenever possible. Therefore, it is possible to perform just the affine or non-affine part of a transformation on a set of data. The affine is always assumed to occur after the non-affine. For any transform:: full transform == non-affine part + affine part The backends are not expected to handle non-affine transformations themselves. """ from __future__ import (absolute_import, division, print_function, unicode_literals) from matplotlib.externals import six import numpy as np from numpy import ma from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox, update_path_extents) from numpy.linalg import inv from weakref import WeakValueDictionary import warnings try: set except NameError: from sets import Set as set from .path import Path DEBUG = False # we need this later, but this is very expensive to set up MINFLOAT = np.MachAr(float).xmin MaskedArray = ma.MaskedArray class TransformNode(object): """ :class:`TransformNode` is the base class for anything that participates in the transform tree and needs to invalidate its parents or be invalidated. This includes classes that are not really transforms, such as bounding boxes, since some transforms depend on bounding boxes to compute their values. """ _gid = 0 # Invalidation may affect only the affine part. If the # invalidation was "affine-only", the _invalid member is set to # INVALID_AFFINE_ONLY INVALID_NON_AFFINE = 1 INVALID_AFFINE = 2 INVALID = INVALID_NON_AFFINE | INVALID_AFFINE # Some metadata about the transform, used to determine whether an # invalidation is affine-only is_affine = False is_bbox = False pass_through = False """ If pass_through is True, all ancestors will always be invalidated, even if 'self' is already invalid. """ def __init__(self, shorthand_name=None): """ Creates a new :class:`TransformNode`. **shorthand_name** - a string representing the "name" of this transform. The name carries no significance other than to improve the readability of ``str(transform)`` when DEBUG=True. """ # Parents are stored in a WeakValueDictionary, so that if the # parents are deleted, references from the children won't keep # them alive. self._parents = WeakValueDictionary() # TransformNodes start out as invalid until their values are # computed for the first time. self._invalid = 1 self._shorthand_name = shorthand_name or '' if DEBUG: def __str__(self): # either just return the name of this TransformNode, or it's repr return self._shorthand_name or repr(self) def __getstate__(self): d = self.__dict__.copy() # turn the weakkey dictionary into a normal dictionary d['_parents'] = dict(six.iteritems(self._parents)) return d def __setstate__(self, data_dict): self.__dict__ = data_dict # turn the normal dictionary back into a WeakValueDictionary self._parents = WeakValueDictionary(self._parents) def __copy__(self, *args): raise NotImplementedError( "TransformNode instances can not be copied. " + "Consider using frozen() instead.") __deepcopy__ = __copy__ def invalidate(self): """ Invalidate this :class:`TransformNode` and triggers an invalidation of its ancestors. Should be called any time the transform changes. """ value = self.INVALID if self.is_affine: value = self.INVALID_AFFINE return self._invalidate_internal(value, invalidating_node=self) def _invalidate_internal(self, value, invalidating_node): """ Called by :meth:`invalidate` and subsequently ascends the transform stack calling each TransformNode's _invalidate_internal method. """ # determine if this call will be an extension to the invalidation # status. If not, then a shortcut means that we needn't invoke an # invalidation up the transform stack as it will already have been # invalidated. # N.B This makes the invalidation sticky, once a transform has been # invalidated as NON_AFFINE, then it will always be invalidated as # NON_AFFINE even when triggered with a AFFINE_ONLY invalidation. # In most cases this is not a problem (i.e. for interactive panning and # zooming) and the only side effect will be on performance. status_changed = self._invalid < value if self.pass_through or status_changed: self._invalid = value for parent in list(six.itervalues(self._parents)): parent._invalidate_internal(value=value, invalidating_node=self) def set_children(self, *children): """ Set the children of the transform, to let the invalidation system know which transforms can invalidate this transform. Should be called from the constructor of any transforms that depend on other transforms. """ for child in children: child._parents[id(self)] = self if DEBUG: _set_children = set_children def set_children(self, *children): self._set_children(*children) self._children = children set_children.__doc__ = _set_children.__doc__ def frozen(self): """ Returns a frozen copy of this transform node. The frozen copy will not update when its children change. Useful for storing a previously known state of a transform where ``copy.deepcopy()`` might normally be used. """ return self if DEBUG: def write_graphviz(self, fobj, highlight=[]): """ For debugging purposes. Writes the transform tree rooted at 'self' to a graphviz "dot" format file. This file can be run through the "dot" utility to produce a graph of the transform tree. Affine transforms are marked in blue. Bounding boxes are marked in yellow. *fobj*: A Python file-like object Once the "dot" file has been created, it can be turned into a png easily with:: $> dot -Tpng -o $OUTPUT_FILE $DOT_FILE """ seen = set() def recurse(root): if root in seen: return seen.add(root) props = {} label = root.__class__.__name__ if root._invalid: label = '[%s]' % label if root in highlight: props['style'] = 'bold' props['shape'] = 'box' props['label'] = '"%s"' % label props = ' '.join(['%s=%s' % (key, val) for key, val in six.iteritems(props)]) fobj.write('%s [%s];\n' % (hash(root), props)) if hasattr(root, '_children'): for child in root._children: name = '?' for key, val in six.iteritems(root.__dict__): if val is child: name = key break fobj.write('"%s" -> "%s" [label="%s", fontsize=10];\n' % (hash(root), hash(child), name)) recurse(child) fobj.write("digraph G {\n") recurse(self) fobj.write("}\n") class BboxBase(TransformNode): """ This is the base class of all bounding boxes, and provides read-only access to its data. A mutable bounding box is provided by the :class:`Bbox` class. The canonical representation is as two points, with no restrictions on their ordering. Convenience properties are provided to get the left, bottom, right and top edges and width and height, but these are not stored explicitly. """ is_bbox = True is_affine = True #* Redundant: Removed for performance # # def __init__(self): # TransformNode.__init__(self) if DEBUG: def _check(points): if ma.isMaskedArray(points): warnings.warn("Bbox bounds are a masked array.") points = np.asarray(points) if (points[1, 0] - points[0, 0] == 0 or points[1, 1] - points[0, 1] == 0): warnings.warn("Singular Bbox.") _check = staticmethod(_check) def frozen(self): return Bbox(self.get_points().copy()) frozen.__doc__ = TransformNode.__doc__ def __array__(self, *args, **kwargs): return self.get_points() def is_unit(self): """ Returns True if the :class:`Bbox` is the unit bounding box from (0, 0) to (1, 1). """ return list(self.get_points().flatten()) == [0., 0., 1., 1.] def _get_x0(self): return self.get_points()[0, 0] x0 = property(_get_x0, None, None, """ (property) :attr:`x0` is the first of the pair of *x* coordinates that define the bounding box. :attr:`x0` is not guaranteed to be less than :attr:`x1`. If you require that, use :attr:`xmin`.""") def _get_y0(self): return self.get_points()[0, 1] y0 = property(_get_y0, None, None, """ (property) :attr:`y0` is the first of the pair of *y* coordinates that define the bounding box. :attr:`y0` is not guaranteed to be less than :attr:`y1`. If you require that, use :attr:`ymin`.""") def _get_x1(self): return self.get_points()[1, 0] x1 = property(_get_x1, None, None, """ (property) :attr:`x1` is the second of the pair of *x* coordinates that define the bounding box. :attr:`x1` is not guaranteed to be greater than :attr:`x0`. If you require that, use :attr:`xmax`.""") def _get_y1(self): return self.get_points()[1, 1] y1 = property(_get_y1, None, None, """ (property) :attr:`y1` is the second of the pair of *y* coordinates that define the bounding box. :attr:`y1` is not guaranteed to be greater than :attr:`y0`. If you require that, use :attr:`ymax`.""") def _get_p0(self): return self.get_points()[0] p0 = property(_get_p0, None, None, """ (property) :attr:`p0` is the first pair of (*x*, *y*) coordinates that define the bounding box. It is not guaranteed to be the bottom-left corner. For that, use :attr:`min`.""") def _get_p1(self): return self.get_points()[1] p1 = property(_get_p1, None, None, """ (property) :attr:`p1` is the second pair of (*x*, *y*) coordinates that define the bounding box. It is not guaranteed to be the top-right corner. For that, use :attr:`max`.""") def _get_xmin(self): return min(self.get_points()[:, 0]) xmin = property(_get_xmin, None, None, """ (property) :attr:`xmin` is the left edge of the bounding box.""") def _get_ymin(self): return min(self.get_points()[:, 1]) ymin = property(_get_ymin, None, None, """ (property) :attr:`ymin` is the bottom edge of the bounding box.""") def _get_xmax(self): return max(self.get_points()[:, 0]) xmax = property(_get_xmax, None, None, """ (property) :attr:`xmax` is the right edge of the bounding box.""") def _get_ymax(self): return max(self.get_points()[:, 1]) ymax = property(_get_ymax, None, None, """ (property) :attr:`ymax` is the top edge of the bounding box.""") def _get_min(self): return [min(self.get_points()[:, 0]), min(self.get_points()[:, 1])] min = property(_get_min, None, None, """ (property) :attr:`min` is the bottom-left corner of the bounding box.""") def _get_max(self): return [max(self.get_points()[:, 0]), max(self.get_points()[:, 1])] max = property(_get_max, None, None, """ (property) :attr:`max` is the top-right corner of the bounding box.""") def _get_intervalx(self): return self.get_points()[:, 0] intervalx = property(_get_intervalx, None, None, """ (property) :attr:`intervalx` is the pair of *x* coordinates that define the bounding box. It is not guaranteed to be sorted from left to right.""") def _get_intervaly(self): return self.get_points()[:, 1] intervaly = property(_get_intervaly, None, None, """ (property) :attr:`intervaly` is the pair of *y* coordinates that define the bounding box. It is not guaranteed to be sorted from bottom to top.""") def _get_width(self): points = self.get_points() return points[1, 0] - points[0, 0] width = property(_get_width, None, None, """ (property) The width of the bounding box. It may be negative if :attr:`x1` < :attr:`x0`.""") def _get_height(self): points = self.get_points() return points[1, 1] - points[0, 1] height = property(_get_height, None, None, """ (property) The height of the bounding box. It may be negative if :attr:`y1` < :attr:`y0`.""") def _get_size(self): points = self.get_points() return points[1] - points[0] size = property(_get_size, None, None, """ (property) The width and height of the bounding box. May be negative, in the same way as :attr:`width` and :attr:`height`.""") def _get_bounds(self): x0, y0, x1, y1 = self.get_points().flatten() return (x0, y0, x1 - x0, y1 - y0) bounds = property(_get_bounds, None, None, """ (property) Returns (:attr:`x0`, :attr:`y0`, :attr:`width`, :attr:`height`).""") def _get_extents(self): return self.get_points().flatten().copy() extents = property(_get_extents, None, None, """ (property) Returns (:attr:`x0`, :attr:`y0`, :attr:`x1`, :attr:`y1`).""") def get_points(self): return NotImplementedError() def containsx(self, x): """ Returns True if *x* is between or equal to :attr:`x0` and :attr:`x1`. """ x0, x1 = self.intervalx return ((x0 < x1 and (x >= x0 and x <= x1)) or (x >= x1 and x <= x0)) def containsy(self, y): """ Returns True if *y* is between or equal to :attr:`y0` and :attr:`y1`. """ y0, y1 = self.intervaly return ((y0 < y1 and (y >= y0 and y <= y1)) or (y >= y1 and y <= y0)) def contains(self, x, y): """ Returns *True* if (*x*, *y*) is a coordinate inside the bounding box or on its edge. """ return self.containsx(x) and self.containsy(y) def overlaps(self, other): """ Returns True if this bounding box overlaps with the given bounding box *other*. """ ax1, ay1, ax2, ay2 = self._get_extents() bx1, by1, bx2, by2 = other._get_extents() if any(np.isnan(v) for v in [ax1, ay1, ax2, ay2, bx1, by1, bx2, by2]): return False if ax2 < ax1: ax2, ax1 = ax1, ax2 if ay2 < ay1: ay2, ay1 = ay1, ay2 if bx2 < bx1: bx2, bx1 = bx1, bx2 if by2 < by1: by2, by1 = by1, by2 return not ((bx2 < ax1) or (by2 < ay1) or (bx1 > ax2) or (by1 > ay2)) def fully_containsx(self, x): """ Returns True if *x* is between but not equal to :attr:`x0` and :attr:`x1`. """ x0, x1 = self.intervalx return ((x0 < x1 and (x > x0 and x < x1)) or (x > x1 and x < x0)) def fully_containsy(self, y): """ Returns True if *y* is between but not equal to :attr:`y0` and :attr:`y1`. """ y0, y1 = self.intervaly return ((y0 < y1 and (y > y0 and y < y1)) or (y > y1 and y < y0)) def fully_contains(self, x, y): """ Returns True if (*x*, *y*) is a coordinate inside the bounding box, but not on its edge. """ return self.fully_containsx(x) \ and self.fully_containsy(y) def fully_overlaps(self, other): """ Returns True if this bounding box overlaps with the given bounding box *other*, but not on its edge alone. """ ax1, ay1, ax2, ay2 = self._get_extents() bx1, by1, bx2, by2 = other._get_extents() if ax2 < ax1: ax2, ax1 = ax1, ax2 if ay2 < ay1: ay2, ay1 = ay1, ay2 if bx2 < bx1: bx2, bx1 = bx1, bx2 if by2 < by1: by2, by1 = by1, by2 return not ((bx2 <= ax1) or (by2 <= ay1) or (bx1 >= ax2) or (by1 >= ay2)) def transformed(self, transform): """ Return a new :class:`Bbox` object, statically transformed by the given transform. """ pts = self.get_points() ll, ul, lr = transform.transform(np.array([pts[0], [pts[0, 0], pts[1, 1]], [pts[1, 0], pts[0, 1]]])) return Bbox([ll, [lr[0], ul[1]]]) def inverse_transformed(self, transform): """ Return a new :class:`Bbox` object, statically transformed by the inverse of the given transform. """ return self.transformed(transform.inverted()) coefs = {'C': (0.5, 0.5), 'SW': (0, 0), 'S': (0.5, 0), 'SE': (1.0, 0), 'E': (1.0, 0.5), 'NE': (1.0, 1.0), 'N': (0.5, 1.0), 'NW': (0, 1.0), 'W': (0, 0.5)} def anchored(self, c, container=None): """ Return a copy of the :class:`Bbox`, shifted to position *c* within a container. *c*: may be either: * a sequence (*cx*, *cy*) where *cx* and *cy* range from 0 to 1, where 0 is left or bottom and 1 is right or top * a string: - 'C' for centered - 'S' for bottom-center - 'SE' for bottom-left - 'E' for left - etc. Optional argument *container* is the box within which the :class:`Bbox` is positioned; it defaults to the initial :class:`Bbox`. """ if container is None: container = self l, b, w, h = container.bounds if isinstance(c, six.string_types): cx, cy = self.coefs[c] else: cx, cy = c L, B, W, H = self.bounds return Bbox(self._points + [(l + cx * (w - W)) - L, (b + cy * (h - H)) - B]) def shrunk(self, mx, my): """ Return a copy of the :class:`Bbox`, shrunk by the factor *mx* in the *x* direction and the factor *my* in the *y* direction. The lower left corner of the box remains unchanged. Normally *mx* and *my* will be less than 1, but this is not enforced. """ w, h = self.size return Bbox([self._points[0], self._points[0] + [mx * w, my * h]]) def shrunk_to_aspect(self, box_aspect, container=None, fig_aspect=1.0): """ Return a copy of the :class:`Bbox`, shrunk so that it is as large as it can be while having the desired aspect ratio, *box_aspect*. If the box coordinates are relative---that is, fractions of a larger box such as a figure---then the physical aspect ratio of that figure is specified with *fig_aspect*, so that *box_aspect* can also be given as a ratio of the absolute dimensions, not the relative dimensions. """ if box_aspect <= 0 or fig_aspect <= 0: raise ValueError("'box_aspect' and 'fig_aspect' must be positive") if container is None: container = self w, h = container.size H = w * box_aspect / fig_aspect if H <= h: W = w else: W = h * fig_aspect / box_aspect H = h return Bbox([self._points[0], self._points[0] + (W, H)]) def splitx(self, *args): """ e.g., ``bbox.splitx(f1, f2, ...)`` Returns a list of new :class:`Bbox` objects formed by splitting the original one with vertical lines at fractional positions *f1*, *f2*, ... """ boxes = [] xf = [0] + list(args) + [1] x0, y0, x1, y1 = self._get_extents() w = x1 - x0 for xf0, xf1 in zip(xf[:-1], xf[1:]): boxes.append(Bbox([[x0 + xf0 * w, y0], [x0 + xf1 * w, y1]])) return boxes def splity(self, *args): """ e.g., ``bbox.splitx(f1, f2, ...)`` Returns a list of new :class:`Bbox` objects formed by splitting the original one with horizontal lines at fractional positions *f1*, *f2*, ... """ boxes = [] yf = [0] + list(args) + [1] x0, y0, x1, y1 = self._get_extents() h = y1 - y0 for yf0, yf1 in zip(yf[:-1], yf[1:]): boxes.append(Bbox([[x0, y0 + yf0 * h], [x1, y0 + yf1 * h]])) return boxes def count_contains(self, vertices): """ Count the number of vertices contained in the :class:`Bbox`. *vertices* is a Nx2 Numpy array. """ if len(vertices) == 0: return 0 vertices = np.asarray(vertices) x0, y0, x1, y1 = self._get_extents() with np.errstate(invalid='ignore'): dx0 = np.sign(vertices[:, 0] - x0) dy0 = np.sign(vertices[:, 1] - y0) dx1 = np.sign(vertices[:, 0] - x1) dy1 = np.sign(vertices[:, 1] - y1) inside = ((abs(dx0 + dx1) + abs(dy0 + dy1)) == 0) return np.sum(inside) def count_overlaps(self, bboxes): """ Count the number of bounding boxes that overlap this one. bboxes is a sequence of :class:`BboxBase` objects """ return count_bboxes_overlapping_bbox( self, np.atleast_3d([np.array(x) for x in bboxes])) def expanded(self, sw, sh): """ Return a new :class:`Bbox` which is this :class:`Bbox` expanded around its center by the given factors *sw* and *sh*. """ width = self.width height = self.height deltaw = (sw * width - width) / 2.0 deltah = (sh * height - height) / 2.0 a = np.array([[-deltaw, -deltah], [deltaw, deltah]]) return Bbox(self._points + a) def padded(self, p): """ Return a new :class:`Bbox` that is padded on all four sides by the given value. """ points = self.get_points() return Bbox(points + [[-p, -p], [p, p]]) def translated(self, tx, ty): """ Return a copy of the :class:`Bbox`, statically translated by *tx* and *ty*. """ return Bbox(self._points + (tx, ty)) def corners(self): """ Return an array of points which are the four corners of this rectangle. For example, if this :class:`Bbox` is defined by the points (*a*, *b*) and (*c*, *d*), :meth:`corners` returns (*a*, *b*), (*a*, *d*), (*c*, *b*) and (*c*, *d*). """ l, b, r, t = self.get_points().flatten() return np.array([[l, b], [l, t], [r, b], [r, t]]) def rotated(self, radians): """ Return a new bounding box that bounds a rotated version of this bounding box by the given radians. The new bounding box is still aligned with the axes, of course. """ corners = self.corners() corners_rotated = Affine2D().rotate(radians).transform(corners) bbox = Bbox.unit() bbox.update_from_data_xy(corners_rotated, ignore=True) return bbox @staticmethod def union(bboxes): """ Return a :class:`Bbox` that contains all of the given bboxes. """ if not len(bboxes): raise ValueError("'bboxes' cannot be empty") if len(bboxes) == 1: return bboxes[0] x0 = np.inf y0 = np.inf x1 = -np.inf y1 = -np.inf for bbox in bboxes: points = bbox.get_points() xs = points[:, 0] ys = points[:, 1] x0 = min(x0, np.min(xs)) y0 = min(y0, np.min(ys)) x1 = max(x1, np.max(xs)) y1 = max(y1, np.max(ys)) return Bbox.from_extents(x0, y0, x1, y1) @staticmethod def intersection(bbox1, bbox2): """ Return the intersection of the two bboxes or None if they do not intersect. Implements the algorithm described at: http://www.tekpool.com/node/2687 """ intersects = not (bbox2.xmin > bbox1.xmax or bbox2.xmax < bbox1.xmin or bbox2.ymin > bbox1.ymax or bbox2.ymax < bbox1.ymin) if intersects: x0 = max([bbox1.xmin, bbox2.xmin]) x1 = min([bbox1.xmax, bbox2.xmax]) y0 = max([bbox1.ymin, bbox2.ymin]) y1 = min([bbox1.ymax, bbox2.ymax]) return Bbox.from_extents(x0, y0, x1, y1) return None class Bbox(BboxBase): """ A mutable bounding box. """ def __init__(self, points, **kwargs): """ *points*: a 2x2 numpy array of the form [[x0, y0], [x1, y1]] If you need to create a :class:`Bbox` object from another form of data, consider the static methods :meth:`unit`, :meth:`from_bounds` and :meth:`from_extents`. """ BboxBase.__init__(self, **kwargs) points = np.asarray(points, np.float_) if points.shape != (2, 2): raise ValueError('Bbox points must be of the form ' '"[[x0, y0], [x1, y1]]".') self._points = points self._minpos = np.array([0.0000001, 0.0000001]) self._ignore = True # it is helpful in some contexts to know if the bbox is a # default or has been mutated; we store the orig points to # support the mutated methods self._points_orig = self._points.copy() if DEBUG: ___init__ = __init__ def __init__(self, points, **kwargs): self._check(points) self.___init__(points, **kwargs) def invalidate(self): self._check(self._points) TransformNode.invalidate(self) @staticmethod def unit(): """ (staticmethod) Create a new unit :class:`Bbox` from (0, 0) to (1, 1). """ return Bbox(np.array([[0.0, 0.0], [1.0, 1.0]], np.float)) @staticmethod def null(): """ (staticmethod) Create a new null :class:`Bbox` from (inf, inf) to (-inf, -inf). """ return Bbox(np.array([[np.inf, np.inf], [-np.inf, -np.inf]], np.float)) @staticmethod def from_bounds(x0, y0, width, height): """ (staticmethod) Create a new :class:`Bbox` from *x0*, *y0*, *width* and *height*. *width* and *height* may be negative. """ return Bbox.from_extents(x0, y0, x0 + width, y0 + height) @staticmethod def from_extents(*args): """ (staticmethod) Create a new Bbox from *left*, *bottom*, *right* and *top*. The *y*-axis increases upwards. """ points = np.array(args, dtype=np.float_).reshape(2, 2) return Bbox(points) def __format__(self, fmt): return ( 'Bbox(x0={0.x0:{1}}, y0={0.y0:{1}}, x1={0.x1:{1}}, y1={0.y1:{1}})'. format(self, fmt)) def __str__(self): return format(self, '') def __repr__(self): return 'Bbox([[{0.x0}, {0.y0}], [{0.x1}, {0.y1}]])'.format(self) def ignore(self, value): """ Set whether the existing bounds of the box should be ignored by subsequent calls to :meth:`update_from_data` or :meth:`update_from_data_xy`. *value*: - When True, subsequent calls to :meth:`update_from_data` will ignore the existing bounds of the :class:`Bbox`. - When False, subsequent calls to :meth:`update_from_data` will include the existing bounds of the :class:`Bbox`. """ self._ignore = value def update_from_data(self, x, y, ignore=None): """ Update the bounds of the :class:`Bbox` based on the passed in data. After updating, the bounds will have positive *width* and *height*; *x0* and *y0* will be the minimal values. *x*: a numpy array of *x*-values *y*: a numpy array of *y*-values *ignore*: - when True, ignore the existing bounds of the :class:`Bbox`. - when False, include the existing bounds of the :class:`Bbox`. - when None, use the last value passed to :meth:`ignore`. """ warnings.warn( "update_from_data requires a memory copy -- please replace with " "update_from_data_xy") xy = np.hstack((x.reshape((len(x), 1)), y.reshape((len(y), 1)))) return self.update_from_data_xy(xy, ignore) def update_from_path(self, path, ignore=None, updatex=True, updatey=True): """ Update the bounds of the :class:`Bbox` based on the passed in data. After updating, the bounds will have positive *width* and *height*; *x0* and *y0* will be the minimal values. *path*: a :class:`~matplotlib.path.Path` instance *ignore*: - when True, ignore the existing bounds of the :class:`Bbox`. - when False, include the existing bounds of the :class:`Bbox`. - when None, use the last value passed to :meth:`ignore`. *updatex*: when True, update the x values *updatey*: when True, update the y values """ if ignore is None: ignore = self._ignore if path.vertices.size == 0: return points, minpos, changed = update_path_extents( path, None, self._points, self._minpos, ignore) if changed: self.invalidate() if updatex: self._points[:, 0] = points[:, 0] self._minpos[0] = minpos[0] if updatey: self._points[:, 1] = points[:, 1] self._minpos[1] = minpos[1] def update_from_data_xy(self, xy, ignore=None, updatex=True, updatey=True): """ Update the bounds of the :class:`Bbox` based on the passed in data. After updating, the bounds will have positive *width* and *height*; *x0* and *y0* will be the minimal values. *xy*: a numpy array of 2D points *ignore*: - when True, ignore the existing bounds of the :class:`Bbox`. - when False, include the existing bounds of the :class:`Bbox`. - when None, use the last value passed to :meth:`ignore`. *updatex*: when True, update the x values *updatey*: when True, update the y values """ if len(xy) == 0: return path = Path(xy) self.update_from_path(path, ignore=ignore, updatex=updatex, updatey=updatey) def _set_x0(self, val): self._points[0, 0] = val self.invalidate() x0 = property(BboxBase._get_x0, _set_x0) def _set_y0(self, val): self._points[0, 1] = val self.invalidate() y0 = property(BboxBase._get_y0, _set_y0) def _set_x1(self, val): self._points[1, 0] = val self.invalidate() x1 = property(BboxBase._get_x1, _set_x1) def _set_y1(self, val): self._points[1, 1] = val self.invalidate() y1 = property(BboxBase._get_y1, _set_y1) def _set_p0(self, val): self._points[0] = val self.invalidate() p0 = property(BboxBase._get_p0, _set_p0) def _set_p1(self, val): self._points[1] = val self.invalidate() p1 = property(BboxBase._get_p1, _set_p1) def _set_intervalx(self, interval): self._points[:, 0] = interval self.invalidate() intervalx = property(BboxBase._get_intervalx, _set_intervalx) def _set_intervaly(self, interval): self._points[:, 1] = interval self.invalidate() intervaly = property(BboxBase._get_intervaly, _set_intervaly) def _set_bounds(self, bounds): l, b, w, h = bounds points = np.array([[l, b], [l + w, b + h]], np.float_) if np.any(self._points != points): self._points = points self.invalidate() bounds = property(BboxBase._get_bounds, _set_bounds) def _get_minpos(self): return self._minpos minpos = property(_get_minpos) def _get_minposx(self): return self._minpos[0] minposx = property(_get_minposx) def _get_minposy(self): return self._minpos[1] minposy = property(_get_minposy) def get_points(self): """ Get the points of the bounding box directly as a numpy array of the form: [[x0, y0], [x1, y1]]. """ self._invalid = 0 return self._points def set_points(self, points): """ Set the points of the bounding box directly from a numpy array of the form: [[x0, y0], [x1, y1]]. No error checking is performed, as this method is mainly for internal use. """ if np.any(self._points != points): self._points = points self.invalidate() def set(self, other): """ Set this bounding box from the "frozen" bounds of another :class:`Bbox`. """ if np.any(self._points != other.get_points()): self._points = other.get_points() self.invalidate() def mutated(self): 'return whether the bbox has changed since init' return self.mutatedx() or self.mutatedy() def mutatedx(self): 'return whether the x-limits have changed since init' return (self._points[0, 0] != self._points_orig[0, 0] or self._points[1, 0] != self._points_orig[1, 0]) def mutatedy(self): 'return whether the y-limits have changed since init' return (self._points[0, 1] != self._points_orig[0, 1] or self._points[1, 1] != self._points_orig[1, 1]) class TransformedBbox(BboxBase): """ A :class:`Bbox` that is automatically transformed by a given transform. When either the child bounding box or transform changes, the bounds of this bbox will update accordingly. """ def __init__(self, bbox, transform, **kwargs): """ *bbox*: a child :class:`Bbox` *transform*: a 2D :class:`Transform` """ if not bbox.is_bbox: raise ValueError("'bbox' is not a bbox") if not isinstance(transform, Transform): msg = ("'transform' must be an instance of" " 'matplotlib.transform.Transform'") raise ValueError(msg) if transform.input_dims != 2 or transform.output_dims != 2: msg = "The input and output dimensions of 'transform' must be 2" raise ValueError(msg) BboxBase.__init__(self, **kwargs) self._bbox = bbox self._transform = transform self.set_children(bbox, transform) self._points = None def __repr__(self): return "TransformedBbox(%r, %r)" % (self._bbox, self._transform) def get_points(self): if self._invalid: points = self._transform.transform(self._bbox.get_points()) points = np.ma.filled(points, 0.0) self._points = points self._invalid = 0 return self._points get_points.__doc__ = Bbox.get_points.__doc__ if DEBUG: _get_points = get_points def get_points(self): points = self._get_points() self._check(points) return points class Transform(TransformNode): """ The base class of all :class:`TransformNode` instances that actually perform a transformation. All non-affine transformations should be subclasses of this class. New affine transformations should be subclasses of :class:`Affine2D`. Subclasses of this class should override the following members (at minimum): - :attr:`input_dims` - :attr:`output_dims` - :meth:`transform` - :attr:`is_separable` - :attr:`has_inverse` - :meth:`inverted` (if :attr:`has_inverse` is True) If the transform needs to do something non-standard with :class:`matplotlib.path.Path` objects, such as adding curves where there were once line segments, it should override: - :meth:`transform_path` """ input_dims = None """ The number of input dimensions of this transform. Must be overridden (with integers) in the subclass. """ output_dims = None """ The number of output dimensions of this transform. Must be overridden (with integers) in the subclass. """ has_inverse = False """True if this transform has a corresponding inverse transform.""" is_separable = False """True if this transform is separable in the x- and y- dimensions.""" def __add__(self, other): """ Composes two transforms together such that *self* is followed by *other*. """ if isinstance(other, Transform): return composite_transform_factory(self, other) raise TypeError( "Can not add Transform to object of type '%s'" % type(other)) def __radd__(self, other): """ Composes two transforms together such that *self* is followed by *other*. """ if isinstance(other, Transform): return composite_transform_factory(other, self) raise TypeError( "Can not add Transform to object of type '%s'" % type(other)) def __eq__(self, other): # equality is based on transform object id. Hence: # Transform() != Transform(). # Some classes, such as TransformWrapper & AffineBase, will override. return self is other def _iter_break_from_left_to_right(self): """ Returns an iterator breaking down this transform stack from left to right recursively. If self == ((A, N), A) then the result will be an iterator which yields I : ((A, N), A), followed by A : (N, A), followed by (A, N) : (A), but not ((A, N), A) : I. This is equivalent to flattening the stack then yielding ``flat_stack[:i], flat_stack[i:]`` where i=0..(n-1). """ yield IdentityTransform(), self @property def depth(self): """ Returns the number of transforms which have been chained together to form this Transform instance. .. note:: For the special case of a Composite transform, the maximum depth of the two is returned. """ return 1 def contains_branch(self, other): """ Return whether the given transform is a sub-tree of this transform. This routine uses transform equality to identify sub-trees, therefore in many situations it is object id which will be used. For the case where the given transform represents the whole of this transform, returns True. """ if self.depth < other.depth: return False # check that a subtree is equal to other (starting from self) for _, sub_tree in self._iter_break_from_left_to_right(): if sub_tree == other: return True return False def contains_branch_seperately(self, other_transform): """ Returns whether the given branch is a sub-tree of this transform on each seperate dimension. A common use for this method is to identify if a transform is a blended transform containing an axes' data transform. e.g.:: x_isdata, y_isdata = trans.contains_branch_seperately(ax.transData) """ if self.output_dims != 2: raise ValueError('contains_branch_seperately only supports ' 'transforms with 2 output dimensions') # for a non-blended transform each seperate dimension is the same, so # just return the appropriate shape. return [self.contains_branch(other_transform)] * 2 def __sub__(self, other): """ Returns a transform stack which goes all the way down self's transform stack, and then ascends back up other's stack. If it can, this is optimised:: # normally A - B == a + b.inverted() # sometimes, when A contains the tree B there is no need to # descend all the way down to the base of A (via B), instead we # can just stop at B. (A + B) - (B)^-1 == A # similarly, when B contains tree A, we can avoid decending A at # all, basically: A - (A + B) == ((B + A) - A).inverted() or B^-1 For clarity, the result of ``(A + B) - B + B == (A + B)``. """ # we only know how to do this operation if other is a Transform. if not isinstance(other, Transform): return NotImplemented for remainder, sub_tree in self._iter_break_from_left_to_right(): if sub_tree == other: return remainder for remainder, sub_tree in other._iter_break_from_left_to_right(): if sub_tree == self: if not remainder.has_inverse: raise ValueError("The shortcut cannot be computed since " "other's transform includes a non-invertable component.") return remainder.inverted() # if we have got this far, then there was no shortcut possible if other.has_inverse: return self + other.inverted() else: raise ValueError('It is not possible to compute transA - transB ' 'since transB cannot be inverted and there is no ' 'shortcut possible.') def __array__(self, *args, **kwargs): """ Array interface to get at this Transform's affine matrix. """ return self.get_affine().get_matrix() def transform(self, values): """ Performs the transformation on the given array of values. Accepts a numpy array of shape (N x :attr:`input_dims`) and returns a numpy array of shape (N x :attr:`output_dims`). Alternatively, accepts a numpy array of length :attr:`input_dims` and returns a numpy array of length :attr:`output_dims`. """ # Ensure that values is a 2d array (but remember whether # we started with a 1d or 2d array). values = np.asanyarray(values) ndim = values.ndim values = values.reshape((-1, self.input_dims)) # Transform the values res = self.transform_affine(self.transform_non_affine(values)) # Convert the result back to the shape of the input values. if ndim == 0: assert not np.ma.is_masked(res) # just to be on the safe side return res[0, 0] if ndim == 1: return res.reshape(-1) elif ndim == 2: return res else: raise ValueError( "Input values must have shape (N x {dims}) " "or ({dims}).".format(dims=self.input_dims)) return res def transform_affine(self, values): """ Performs only the affine part of this transformation on the given array of values. ``transform(values)`` is always equivalent to ``transform_affine(transform_non_affine(values))``. In non-affine transformations, this is generally a no-op. In affine transformations, this is equivalent to ``transform(values)``. Accepts a numpy array of shape (N x :attr:`input_dims`) and returns a numpy array of shape (N x :attr:`output_dims`). Alternatively, accepts a numpy array of length :attr:`input_dims` and returns a numpy array of length :attr:`output_dims`. """ return self.get_affine().transform(values) def transform_non_affine(self, values): """ Performs only the non-affine part of the transformation. ``transform(values)`` is always equivalent to ``transform_affine(transform_non_affine(values))``. In non-affine transformations, this is generally equivalent to ``transform(values)``. In affine transformations, this is always a no-op. Accepts a numpy array of shape (N x :attr:`input_dims`) and returns a numpy array of shape (N x :attr:`output_dims`). Alternatively, accepts a numpy array of length :attr:`input_dims` and returns a numpy array of length :attr:`output_dims`. """ return values def transform_bbox(self, bbox): """ Transform the given bounding box. Note, for smarter transforms including caching (a common requirement for matplotlib figures), see :class:`TransformedBbox`. """ return Bbox(self.transform(bbox.get_points())) def get_affine(self): """ Get the affine part of this transform. """ return IdentityTransform() def get_matrix(self): """ Get the Affine transformation array for the affine part of this transform. """ return self.get_affine().get_matrix() def transform_point(self, point): """ A convenience function that returns the transformed copy of a single point. The point is given as a sequence of length :attr:`input_dims`. The transformed point is returned as a sequence of length :attr:`output_dims`. """ if len(point) != self.input_dims: msg = "The length of 'point' must be 'self.input_dims'" raise ValueError(msg) return self.transform(np.asarray([point]))[0] def transform_path(self, path): """ Returns a transformed path. *path*: a :class:`~matplotlib.path.Path` instance. In some cases, this transform may insert curves into the path that began as line segments. """ return self.transform_path_affine(self.transform_path_non_affine(path)) def transform_path_affine(self, path): """ Returns a path, transformed only by the affine part of this transform. *path*: a :class:`~matplotlib.path.Path` instance. ``transform_path(path)`` is equivalent to ``transform_path_affine(transform_path_non_affine(values))``. """ return self.get_affine().transform_path_affine(path) def transform_path_non_affine(self, path): """ Returns a path, transformed only by the non-affine part of this transform. *path*: a :class:`~matplotlib.path.Path` instance. ``transform_path(path)`` is equivalent to ``transform_path_affine(transform_path_non_affine(values))``. """ x = self.transform_non_affine(path.vertices) return Path._fast_from_codes_and_verts(x, path.codes, {'interpolation_steps': path._interpolation_steps, 'should_simplify': path.should_simplify}) def transform_angles(self, angles, pts, radians=False, pushoff=1e-5): """ Performs transformation on a set of angles anchored at specific locations. The *angles* must be a column vector (i.e., numpy array). The *pts* must be a two-column numpy array of x,y positions (angle transforms currently only work in 2D). This array must have the same number of rows as *angles*. *radians* indicates whether or not input angles are given in radians (True) or degrees (False; the default). *pushoff* is the distance to move away from *pts* for determining transformed angles (see discussion of method below). The transformed angles are returned in an array with the same size as *angles*. The generic version of this method uses a very generic algorithm that transforms *pts*, as well as locations very close to *pts*, to find the angle in the transformed system. """ # Must be 2D if self.input_dims != 2 or self.output_dims != 2: raise NotImplementedError('Only defined in 2D') if pts.shape[1] != 2: raise ValueError("'pts' must be array with 2 columns for x,y") if angles.ndim != 1 or angles.shape[0] != pts.shape[0]: msg = "'angles' must be a column vector and have same number of" msg += " rows as 'pts'" raise ValueError(msg) # Convert to radians if desired if not radians: angles = angles / 180.0 * np.pi # Move a short distance away pts2 = pts + pushoff * np.c_[np.cos(angles), np.sin(angles)] # Transform both sets of points tpts = self.transform(pts) tpts2 = self.transform(pts2) # Calculate transformed angles d = tpts2 - tpts a = np.arctan2(d[:, 1], d[:, 0]) # Convert back to degrees if desired if not radians: a = a * 180.0 / np.pi return a def inverted(self): """ Return the corresponding inverse transformation. The return value of this method should be treated as temporary. An update to *self* does not cause a corresponding update to its inverted copy. ``x === self.inverted().transform(self.transform(x))`` """ raise NotImplementedError() class TransformWrapper(Transform): """ A helper class that holds a single child transform and acts equivalently to it. This is useful if a node of the transform tree must be replaced at run time with a transform of a different type. This class allows that replacement to correctly trigger invalidation. Note that :class:`TransformWrapper` instances must have the same input and output dimensions during their entire lifetime, so the child transform may only be replaced with another child transform of the same dimensions. """ pass_through = True def __init__(self, child): """ *child*: A class:`Transform` instance. This child may later be replaced with :meth:`set`. """ if not isinstance(child, Transform): msg = ("'child' must be an instance of" " 'matplotlib.transform.Transform'") raise ValueError(msg) self._init(child) self.set_children(child) def _init(self, child): Transform.__init__(self) self.input_dims = child.input_dims self.output_dims = child.output_dims self._set(child) self._invalid = 0 def __eq__(self, other): return self._child.__eq__(other) if DEBUG: def __str__(self): return str(self._child) # NOTE: Transform.__[gs]etstate__ should be sufficient when using only # Python 3.4+. def __getstate__(self): # only store the child information and parents return { 'child': self._child, 'input_dims': self.input_dims, 'output_dims': self.output_dims, # turn the weakkey dictionary into a normal dictionary 'parents': dict(six.iteritems(self._parents)) } def __setstate__(self, state): # re-initialise the TransformWrapper with the state's child self._init(state['child']) # The child may not be unpickled yet, so restore its information. self.input_dims = state['input_dims'] self.output_dims = state['output_dims'] # turn the normal dictionary back into a WeakValueDictionary self._parents = WeakValueDictionary(state['parents']) def __repr__(self): return "TransformWrapper(%r)" % self._child def frozen(self): return self._child.frozen() frozen.__doc__ = Transform.frozen.__doc__ def _set(self, child): self._child = child self.transform = child.transform self.transform_affine = child.transform_affine self.transform_non_affine = child.transform_non_affine self.transform_path = child.transform_path self.transform_path_affine = child.transform_path_affine self.transform_path_non_affine = child.transform_path_non_affine self.get_affine = child.get_affine self.inverted = child.inverted self.get_matrix = child.get_matrix # note we do not wrap other properties here since the transform's # child can be changed with WrappedTransform.set and so checking # is_affine and other such properties may be dangerous. def set(self, child): """ Replace the current child of this transform with another one. The new child must have the same number of input and output dimensions as the current child. """ if (child.input_dims != self.input_dims or child.output_dims != self.output_dims): msg = ("The new child must have the same number of input and" " output dimensions as the current child.") raise ValueError(msg) self.set_children(child) self._set(child) self._invalid = 0 self.invalidate() self._invalid = 0 def _get_is_affine(self): return self._child.is_affine is_affine = property(_get_is_affine) def _get_is_separable(self): return self._child.is_separable is_separable = property(_get_is_separable) def _get_has_inverse(self): return self._child.has_inverse has_inverse = property(_get_has_inverse) class AffineBase(Transform): """ The base class of all affine transformations of any number of dimensions. """ is_affine = True def __init__(self, *args, **kwargs): Transform.__init__(self, *args, **kwargs) self._inverted = None def __array__(self, *args, **kwargs): # optimises the access of the transform matrix vs the superclass return self.get_matrix() @staticmethod def _concat(a, b): """ Concatenates two transformation matrices (represented as numpy arrays) together. """ return np.dot(b, a) def __eq__(self, other): if getattr(other, "is_affine", False): return np.all(self.get_matrix() == other.get_matrix()) return NotImplemented def transform(self, values): return self.transform_affine(values) transform.__doc__ = Transform.transform.__doc__ def transform_affine(self, values): raise NotImplementedError('Affine subclasses should override this ' 'method.') transform_affine.__doc__ = Transform.transform_affine.__doc__ def transform_non_affine(self, points): return points transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__ def transform_path(self, path): return self.transform_path_affine(path) transform_path.__doc__ = Transform.transform_path.__doc__ def transform_path_affine(self, path): return Path(self.transform_affine(path.vertices), path.codes, path._interpolation_steps) transform_path_affine.__doc__ = Transform.transform_path_affine.__doc__ def transform_path_non_affine(self, path): return path transform_path_non_affine.__doc__ = Transform.transform_path_non_affine.__doc__ def get_affine(self): return self get_affine.__doc__ = Transform.get_affine.__doc__ class Affine2DBase(AffineBase): """ The base class of all 2D affine transformations. 2D affine transformations are performed using a 3x3 numpy array:: a c e b d f 0 0 1 This class provides the read-only interface. For a mutable 2D affine transformation, use :class:`Affine2D`. Subclasses of this class will generally only need to override a constructor and :meth:`get_matrix` that generates a custom 3x3 matrix. """ has_inverse = True input_dims = 2 output_dims = 2 def frozen(self): return Affine2D(self.get_matrix().copy()) frozen.__doc__ = AffineBase.frozen.__doc__ def _get_is_separable(self): mtx = self.get_matrix() return mtx[0, 1] == 0.0 and mtx[1, 0] == 0.0 is_separable = property(_get_is_separable) def to_values(self): """ Return the values of the matrix as a sequence (a,b,c,d,e,f) """ mtx = self.get_matrix() return tuple(mtx[:2].swapaxes(0, 1).flatten()) @staticmethod def matrix_from_values(a, b, c, d, e, f): """ (staticmethod) Create a new transformation matrix as a 3x3 numpy array of the form:: a c e b d f 0 0 1 """ return np.array([[a, c, e], [b, d, f], [0.0, 0.0, 1.0]], np.float_) def transform_affine(self, points): mtx = self.get_matrix() if isinstance(points, MaskedArray): tpoints = affine_transform(points.data, mtx) return ma.MaskedArray(tpoints, mask=ma.getmask(points)) return affine_transform(points, mtx) def transform_point(self, point): mtx = self.get_matrix() return affine_transform([point], mtx)[0] transform_point.__doc__ = AffineBase.transform_point.__doc__ if DEBUG: _transform_affine = transform_affine def transform_affine(self, points): # The major speed trap here is just converting to the # points to an array in the first place. If we can use # more arrays upstream, that should help here. if (not ma.isMaskedArray(points) and not isinstance(points, np.ndarray)): warnings.warn( ('A non-numpy array of type %s was passed in for ' + 'transformation. Please correct this.') % type(points)) return self._transform_affine(points) transform_affine.__doc__ = AffineBase.transform_affine.__doc__ def inverted(self): if self._inverted is None or self._invalid: mtx = self.get_matrix() shorthand_name = None if self._shorthand_name: shorthand_name = '(%s)-1' % self._shorthand_name self._inverted = Affine2D(inv(mtx), shorthand_name=shorthand_name) self._invalid = 0 return self._inverted inverted.__doc__ = AffineBase.inverted.__doc__ class Affine2D(Affine2DBase): """ A mutable 2D affine transformation. """ def __init__(self, matrix=None, **kwargs): """ Initialize an Affine transform from a 3x3 numpy float array:: a c e b d f 0 0 1 If *matrix* is None, initialize with the identity transform. """ Affine2DBase.__init__(self, **kwargs) if matrix is None: matrix = np.identity(3) elif DEBUG: matrix = np.asarray(matrix, np.float_) assert matrix.shape == (3, 3) self._mtx = matrix self._invalid = 0 def __repr__(self): return "Affine2D(%s)" % repr(self._mtx) # def __cmp__(self, other): # # XXX redundant. this only tells us eq. # if (isinstance(other, Affine2D) and # (self.get_matrix() == other.get_matrix()).all()): # return 0 # return -1 @staticmethod def from_values(a, b, c, d, e, f): """ (staticmethod) Create a new Affine2D instance from the given values:: a c e b d f 0 0 1 . """ return Affine2D( np.array([a, c, e, b, d, f, 0.0, 0.0, 1.0], np.float_) .reshape((3, 3))) def get_matrix(self): """ Get the underlying transformation matrix as a 3x3 numpy array:: a c e b d f 0 0 1 . """ self._invalid = 0 return self._mtx def set_matrix(self, mtx): """ Set the underlying transformation matrix from a 3x3 numpy array:: a c e b d f 0 0 1 . """ self._mtx = mtx self.invalidate() def set(self, other): """ Set this transformation from the frozen copy of another :class:`Affine2DBase` object. """ if not isinstance(other, Affine2DBase): msg = ("'other' must be an instance of" " 'matplotlib.transform.Affine2DBase'") raise ValueError(msg) self._mtx = other.get_matrix() self.invalidate() @staticmethod def identity(): """ (staticmethod) Return a new :class:`Affine2D` object that is the identity transform. Unless this transform will be mutated later on, consider using the faster :class:`IdentityTransform` class instead. """ return Affine2D(np.identity(3)) def clear(self): """ Reset the underlying matrix to the identity transform. """ self._mtx = np.identity(3) self.invalidate() return self def rotate(self, theta): """ Add a rotation (in radians) to this transform in place. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ a = np.cos(theta) b = np.sin(theta) rotate_mtx = np.array( [[a, -b, 0.0], [b, a, 0.0], [0.0, 0.0, 1.0]], np.float_) self._mtx = np.dot(rotate_mtx, self._mtx) self.invalidate() return self def rotate_deg(self, degrees): """ Add a rotation (in degrees) to this transform in place. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ return self.rotate(degrees * np.pi / 180.) def rotate_around(self, x, y, theta): """ Add a rotation (in radians) around the point (x, y) in place. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ return self.translate(-x, -y).rotate(theta).translate(x, y) def rotate_deg_around(self, x, y, degrees): """ Add a rotation (in degrees) around the point (x, y) in place. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ return self.translate(-x, -y).rotate_deg(degrees).translate(x, y) def translate(self, tx, ty): """ Adds a translation in place. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ translate_mtx = np.array( [[1.0, 0.0, tx], [0.0, 1.0, ty], [0.0, 0.0, 1.0]], np.float_) self._mtx = np.dot(translate_mtx, self._mtx) self.invalidate() return self def scale(self, sx, sy=None): """ Adds a scale in place. If *sy* is None, the same scale is applied in both the *x*- and *y*-directions. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ if sy is None: sy = sx scale_mtx = np.array( [[sx, 0.0, 0.0], [0.0, sy, 0.0], [0.0, 0.0, 1.0]], np.float_) self._mtx = np.dot(scale_mtx, self._mtx) self.invalidate() return self def skew(self, xShear, yShear): """ Adds a skew in place. *xShear* and *yShear* are the shear angles along the *x*- and *y*-axes, respectively, in radians. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ rotX = np.tan(xShear) rotY = np.tan(yShear) skew_mtx = np.array( [[1.0, rotX, 0.0], [rotY, 1.0, 0.0], [0.0, 0.0, 1.0]], np.float_) self._mtx = np.dot(skew_mtx, self._mtx) self.invalidate() return self def skew_deg(self, xShear, yShear): """ Adds a skew in place. *xShear* and *yShear* are the shear angles along the *x*- and *y*-axes, respectively, in degrees. Returns *self*, so this method can easily be chained with more calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate` and :meth:`scale`. """ return self.skew(np.deg2rad(xShear), np.deg2rad(yShear)) def _get_is_separable(self): mtx = self.get_matrix() return mtx[0, 1] == 0.0 and mtx[1, 0] == 0.0 is_separable = property(_get_is_separable) class IdentityTransform(Affine2DBase): """ A special class that does on thing, the identity transform, in a fast way. """ _mtx = np.identity(3) def frozen(self): return self frozen.__doc__ = Affine2DBase.frozen.__doc__ def __repr__(self): return "IdentityTransform()" def get_matrix(self): return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ def transform(self, points): return np.asanyarray(points) transform.__doc__ = Affine2DBase.transform.__doc__ transform_affine = transform transform_affine.__doc__ = Affine2DBase.transform_affine.__doc__ transform_non_affine = transform transform_non_affine.__doc__ = Affine2DBase.transform_non_affine.__doc__ def transform_path(self, path): return path transform_path.__doc__ = Affine2DBase.transform_path.__doc__ transform_path_affine = transform_path transform_path_affine.__doc__ = Affine2DBase.transform_path_affine.__doc__ transform_path_non_affine = transform_path transform_path_non_affine.__doc__ = Affine2DBase.transform_path_non_affine.__doc__ def get_affine(self): return self get_affine.__doc__ = Affine2DBase.get_affine.__doc__ inverted = get_affine inverted.__doc__ = Affine2DBase.inverted.__doc__ class BlendedGenericTransform(Transform): """ A "blended" transform uses one transform for the *x*-direction, and another transform for the *y*-direction. This "generic" version can handle any given child transform in the *x*- and *y*-directions. """ input_dims = 2 output_dims = 2 is_separable = True pass_through = True def __init__(self, x_transform, y_transform, **kwargs): """ Create a new "blended" transform using *x_transform* to transform the *x*-axis and *y_transform* to transform the *y*-axis. You will generally not call this constructor directly but use the :func:`blended_transform_factory` function instead, which can determine automatically which kind of blended transform to create. """ # Here we ask: "Does it blend?" Transform.__init__(self, **kwargs) self._x = x_transform self._y = y_transform self.set_children(x_transform, y_transform) self._affine = None def __eq__(self, other): # Note, this is an exact copy of BlendedAffine2D.__eq__ if isinstance(other, (BlendedAffine2D, BlendedGenericTransform)): return (self._x == other._x) and (self._y == other._y) elif self._x == self._y: return self._x == other else: return NotImplemented def contains_branch_seperately(self, transform): # Note, this is an exact copy of BlendedAffine2D.contains_branch_seperately return self._x.contains_branch(transform), self._y.contains_branch(transform) @property def depth(self): return max([self._x.depth, self._y.depth]) def contains_branch(self, other): # a blended transform cannot possibly contain a branch from two different transforms. return False def _get_is_affine(self): return self._x.is_affine and self._y.is_affine is_affine = property(_get_is_affine) def _get_has_inverse(self): return self._x.has_inverse and self._y.has_inverse has_inverse = property(_get_has_inverse) def frozen(self): return blended_transform_factory(self._x.frozen(), self._y.frozen()) frozen.__doc__ = Transform.frozen.__doc__ def __repr__(self): return "BlendedGenericTransform(%s,%s)" % (self._x, self._y) def transform_non_affine(self, points): if self._x.is_affine and self._y.is_affine: return points x = self._x y = self._y if x == y and x.input_dims == 2: return x.transform_non_affine(points) if x.input_dims == 2: x_points = x.transform_non_affine(points)[:, 0:1] else: x_points = x.transform_non_affine(points[:, 0]) x_points = x_points.reshape((len(x_points), 1)) if y.input_dims == 2: y_points = y.transform_non_affine(points)[:, 1:] else: y_points = y.transform_non_affine(points[:, 1]) y_points = y_points.reshape((len(y_points), 1)) if isinstance(x_points, MaskedArray) or isinstance(y_points, MaskedArray): return ma.concatenate((x_points, y_points), 1) else: return np.concatenate((x_points, y_points), 1) transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__ def inverted(self): return BlendedGenericTransform(self._x.inverted(), self._y.inverted()) inverted.__doc__ = Transform.inverted.__doc__ def get_affine(self): if self._invalid or self._affine is None: if self._x == self._y: self._affine = self._x.get_affine() else: x_mtx = self._x.get_affine().get_matrix() y_mtx = self._y.get_affine().get_matrix() # This works because we already know the transforms are # separable, though normally one would want to set b and # c to zero. mtx = np.vstack((x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0])) self._affine = Affine2D(mtx) self._invalid = 0 return self._affine get_affine.__doc__ = Transform.get_affine.__doc__ class BlendedAffine2D(Affine2DBase): """ A "blended" transform uses one transform for the *x*-direction, and another transform for the *y*-direction. This version is an optimization for the case where both child transforms are of type :class:`Affine2DBase`. """ is_separable = True def __init__(self, x_transform, y_transform, **kwargs): """ Create a new "blended" transform using *x_transform* to transform the *x*-axis and *y_transform* to transform the *y*-axis. Both *x_transform* and *y_transform* must be 2D affine transforms. You will generally not call this constructor directly but use the :func:`blended_transform_factory` function instead, which can determine automatically which kind of blended transform to create. """ is_affine = x_transform.is_affine and y_transform.is_affine is_separable = x_transform.is_separable and y_transform.is_separable is_correct = is_affine and is_separable if not is_correct: msg = ("Both *x_transform* and *y_transform* must be 2D affine" " transforms.") raise ValueError(msg) Transform.__init__(self, **kwargs) self._x = x_transform self._y = y_transform self.set_children(x_transform, y_transform) Affine2DBase.__init__(self) self._mtx = None def __eq__(self, other): # Note, this is an exact copy of BlendedGenericTransform.__eq__ if isinstance(other, (BlendedAffine2D, BlendedGenericTransform)): return (self._x == other._x) and (self._y == other._y) elif self._x == self._y: return self._x == other else: return NotImplemented def contains_branch_seperately(self, transform): # Note, this is an exact copy of BlendedTransform.contains_branch_seperately return self._x.contains_branch(transform), self._y.contains_branch(transform) def __repr__(self): return "BlendedAffine2D(%s,%s)" % (self._x, self._y) def get_matrix(self): if self._invalid: if self._x == self._y: self._mtx = self._x.get_matrix() else: x_mtx = self._x.get_matrix() y_mtx = self._y.get_matrix() # This works because we already know the transforms are # separable, though normally one would want to set b and # c to zero. self._mtx = np.vstack((x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0])) self._inverted = None self._invalid = 0 return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ def blended_transform_factory(x_transform, y_transform): """ Create a new "blended" transform using *x_transform* to transform the *x*-axis and *y_transform* to transform the *y*-axis. A faster version of the blended transform is returned for the case where both child transforms are affine. """ if (isinstance(x_transform, Affine2DBase) and isinstance(y_transform, Affine2DBase)): return BlendedAffine2D(x_transform, y_transform) return BlendedGenericTransform(x_transform, y_transform) class CompositeGenericTransform(Transform): """ A composite transform formed by applying transform *a* then transform *b*. This "generic" version can handle any two arbitrary transformations. """ pass_through = True def __init__(self, a, b, **kwargs): """ Create a new composite transform that is the result of applying transform *a* then transform *b*. You will generally not call this constructor directly but use the :func:`composite_transform_factory` function instead, which can automatically choose the best kind of composite transform instance to create. """ if a.output_dims != b.input_dims: msg = ("The output dimension of 'a' must be equal to the input" " dimensions of 'b'") raise ValueError(msg) self.input_dims = a.input_dims self.output_dims = b.output_dims Transform.__init__(self, **kwargs) self._a = a self._b = b self.set_children(a, b) is_affine = property(lambda self: self._a.is_affine and self._b.is_affine) def frozen(self): self._invalid = 0 frozen = composite_transform_factory(self._a.frozen(), self._b.frozen()) if not isinstance(frozen, CompositeGenericTransform): return frozen.frozen() return frozen frozen.__doc__ = Transform.frozen.__doc__ def _invalidate_internal(self, value, invalidating_node): # In some cases for a composite transform, an invalidating call to AFFINE_ONLY needs # to be extended to invalidate the NON_AFFINE part too. These cases are when the right # hand transform is non-affine and either: # (a) the left hand transform is non affine # (b) it is the left hand node which has triggered the invalidation if value == Transform.INVALID_AFFINE \ and not self._b.is_affine \ and (not self._a.is_affine or invalidating_node is self._a): value = Transform.INVALID Transform._invalidate_internal(self, value=value, invalidating_node=invalidating_node) def __eq__(self, other): if isinstance(other, (CompositeGenericTransform, CompositeAffine2D)): return self is other or (self._a == other._a and self._b == other._b) else: return False def _iter_break_from_left_to_right(self): for lh_compliment, rh_compliment in self._a._iter_break_from_left_to_right(): yield lh_compliment, rh_compliment + self._b for lh_compliment, rh_compliment in self._b._iter_break_from_left_to_right(): yield self._a + lh_compliment, rh_compliment @property def depth(self): return self._a.depth + self._b.depth def _get_is_affine(self): return self._a.is_affine and self._b.is_affine is_affine = property(_get_is_affine) def _get_is_separable(self): return self._a.is_separable and self._b.is_separable is_separable = property(_get_is_separable) if DEBUG: def __str__(self): return '(%s, %s)' % (self._a, self._b) def __repr__(self): return "CompositeGenericTransform(%r, %r)" % (self._a, self._b) def transform_affine(self, points): return self.get_affine().transform(points) transform_affine.__doc__ = Transform.transform_affine.__doc__ def transform_non_affine(self, points): if self._a.is_affine and self._b.is_affine: return points elif not self._a.is_affine and self._b.is_affine: return self._a.transform_non_affine(points) else: return self._b.transform_non_affine( self._a.transform(points)) transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__ def transform_path_non_affine(self, path): if self._a.is_affine and self._b.is_affine: return path elif not self._a.is_affine and self._b.is_affine: return self._a.transform_path_non_affine(path) else: return self._b.transform_path_non_affine( self._a.transform_path(path)) transform_path_non_affine.__doc__ = Transform.transform_path_non_affine.__doc__ def get_affine(self): if not self._b.is_affine: return self._b.get_affine() else: return Affine2D(np.dot(self._b.get_affine().get_matrix(), self._a.get_affine().get_matrix())) get_affine.__doc__ = Transform.get_affine.__doc__ def inverted(self): return CompositeGenericTransform(self._b.inverted(), self._a.inverted()) inverted.__doc__ = Transform.inverted.__doc__ def _get_has_inverse(self): return self._a.has_inverse and self._b.has_inverse has_inverse = property(_get_has_inverse) class CompositeAffine2D(Affine2DBase): """ A composite transform formed by applying transform *a* then transform *b*. This version is an optimization that handles the case where both *a* and *b* are 2D affines. """ def __init__(self, a, b, **kwargs): """ Create a new composite transform that is the result of applying transform *a* then transform *b*. Both *a* and *b* must be instances of :class:`Affine2DBase`. You will generally not call this constructor directly but use the :func:`composite_transform_factory` function instead, which can automatically choose the best kind of composite transform instance to create. """ if not a.is_affine or not b.is_affine: raise ValueError("'a' and 'b' must be affine transforms") if a.output_dims != b.input_dims: msg = ("The output dimension of 'a' must be equal to the input" " dimensions of 'b'") raise ValueError(msg) self.input_dims = a.input_dims self.output_dims = b.output_dims Affine2DBase.__init__(self, **kwargs) self._a = a self._b = b self.set_children(a, b) self._mtx = None if DEBUG: def __str__(self): return '(%s, %s)' % (self._a, self._b) @property def depth(self): return self._a.depth + self._b.depth def _iter_break_from_left_to_right(self): for lh_compliment, rh_compliment in self._a._iter_break_from_left_to_right(): yield lh_compliment, rh_compliment + self._b for lh_compliment, rh_compliment in self._b._iter_break_from_left_to_right(): yield self._a + lh_compliment, rh_compliment def __repr__(self): return "CompositeAffine2D(%r, %r)" % (self._a, self._b) def get_matrix(self): if self._invalid: self._mtx = np.dot( self._b.get_matrix(), self._a.get_matrix()) self._inverted = None self._invalid = 0 return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ def composite_transform_factory(a, b): """ Create a new composite transform that is the result of applying transform a then transform b. Shortcut versions of the blended transform are provided for the case where both child transforms are affine, or one or the other is the identity transform. Composite transforms may also be created using the '+' operator, e.g.:: c = a + b """ # check to see if any of a or b are IdentityTransforms. We use # isinstance here to guarantee that the transforms will *always* # be IdentityTransforms. Since TransformWrappers are mutable, # use of equality here would be wrong. if isinstance(a, IdentityTransform): return b elif isinstance(b, IdentityTransform): return a elif isinstance(a, Affine2D) and isinstance(b, Affine2D): return CompositeAffine2D(a, b) return CompositeGenericTransform(a, b) class BboxTransform(Affine2DBase): """ :class:`BboxTransform` linearly transforms points from one :class:`Bbox` to another :class:`Bbox`. """ is_separable = True def __init__(self, boxin, boxout, **kwargs): """ Create a new :class:`BboxTransform` that linearly transforms points from *boxin* to *boxout*. """ if not boxin.is_bbox or not boxout.is_bbox: msg = "'boxin' and 'boxout' must be bbox" raise ValueError(msg) Affine2DBase.__init__(self, **kwargs) self._boxin = boxin self._boxout = boxout self.set_children(boxin, boxout) self._mtx = None self._inverted = None def __repr__(self): return "BboxTransform(%r, %r)" % (self._boxin, self._boxout) def get_matrix(self): if self._invalid: inl, inb, inw, inh = self._boxin.bounds outl, outb, outw, outh = self._boxout.bounds x_scale = outw / inw y_scale = outh / inh if DEBUG and (x_scale == 0 or y_scale == 0): raise ValueError("Transforming from or to a singular bounding box.") self._mtx = np.array([[x_scale, 0.0 , (-inl*x_scale+outl)], [0.0 , y_scale, (-inb*y_scale+outb)], [0.0 , 0.0 , 1.0 ]], np.float_) self._inverted = None self._invalid = 0 return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ class BboxTransformTo(Affine2DBase): """ :class:`BboxTransformTo` is a transformation that linearly transforms points from the unit bounding box to a given :class:`Bbox`. """ is_separable = True def __init__(self, boxout, **kwargs): """ Create a new :class:`BboxTransformTo` that linearly transforms points from the unit bounding box to *boxout*. """ if not boxout.is_bbox: raise ValueError("'boxout' must be bbox") Affine2DBase.__init__(self, **kwargs) self._boxout = boxout self.set_children(boxout) self._mtx = None self._inverted = None def __repr__(self): return "BboxTransformTo(%r)" % (self._boxout) def get_matrix(self): if self._invalid: outl, outb, outw, outh = self._boxout.bounds if DEBUG and (outw == 0 or outh == 0): raise ValueError("Transforming to a singular bounding box.") self._mtx = np.array([[outw, 0.0, outl], [ 0.0, outh, outb], [ 0.0, 0.0, 1.0]], np.float_) self._inverted = None self._invalid = 0 return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ class BboxTransformToMaxOnly(BboxTransformTo): """ :class:`BboxTransformTo` is a transformation that linearly transforms points from the unit bounding box to a given :class:`Bbox` with a fixed upper left of (0, 0). """ def __repr__(self): return "BboxTransformToMaxOnly(%r)" % (self._boxout) def get_matrix(self): if self._invalid: xmax, ymax = self._boxout.max if DEBUG and (xmax == 0 or ymax == 0): raise ValueError("Transforming to a singular bounding box.") self._mtx = np.array([[xmax, 0.0, 0.0], [ 0.0, ymax, 0.0], [ 0.0, 0.0, 1.0]], np.float_) self._inverted = None self._invalid = 0 return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ class BboxTransformFrom(Affine2DBase): """ :class:`BboxTransformFrom` linearly transforms points from a given :class:`Bbox` to the unit bounding box. """ is_separable = True def __init__(self, boxin, **kwargs): if not boxin.is_bbox: raise ValueError("'boxin' must be bbox") Affine2DBase.__init__(self, **kwargs) self._boxin = boxin self.set_children(boxin) self._mtx = None self._inverted = None def __repr__(self): return "BboxTransformFrom(%r)" % (self._boxin) def get_matrix(self): if self._invalid: inl, inb, inw, inh = self._boxin.bounds if DEBUG and (inw == 0 or inh == 0): raise ValueError("Transforming from a singular bounding box.") x_scale = 1.0 / inw y_scale = 1.0 / inh self._mtx = np.array([[x_scale, 0.0 , (-inl*x_scale)], [0.0 , y_scale, (-inb*y_scale)], [0.0 , 0.0 , 1.0 ]], np.float_) self._inverted = None self._invalid = 0 return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ class ScaledTranslation(Affine2DBase): """ A transformation that translates by *xt* and *yt*, after *xt* and *yt* have been transformad by the given transform *scale_trans*. """ def __init__(self, xt, yt, scale_trans, **kwargs): Affine2DBase.__init__(self, **kwargs) self._t = (xt, yt) self._scale_trans = scale_trans self.set_children(scale_trans) self._mtx = None self._inverted = None def __repr__(self): return "ScaledTranslation(%r)" % (self._t,) def get_matrix(self): if self._invalid: xt, yt = self._scale_trans.transform_point(self._t) self._mtx = np.array([[1.0, 0.0, xt], [0.0, 1.0, yt], [0.0, 0.0, 1.0]], np.float_) self._invalid = 0 self._inverted = None return self._mtx get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__ class TransformedPath(TransformNode): """ A :class:`TransformedPath` caches a non-affine transformed copy of the :class:`~matplotlib.path.Path`. This cached copy is automatically updated when the non-affine part of the transform changes. .. note:: Paths are considered immutable by this class. Any update to the path's vertices/codes will not trigger a transform recomputation. """ def __init__(self, path, transform): """ Create a new :class:`TransformedPath` from the given :class:`~matplotlib.path.Path` and :class:`Transform`. """ if not isinstance(transform, Transform): msg = ("'transform' must be an instance of" " 'matplotlib.transform.Transform'") raise ValueError(msg) TransformNode.__init__(self) self._path = path self._transform = transform self.set_children(transform) self._transformed_path = None self._transformed_points = None def _revalidate(self): # only recompute if the invalidation includes the non_affine part of the transform if ((self._invalid & self.INVALID_NON_AFFINE == self.INVALID_NON_AFFINE) or self._transformed_path is None): self._transformed_path = \ self._transform.transform_path_non_affine(self._path) self._transformed_points = \ Path._fast_from_codes_and_verts( self._transform.transform_non_affine(self._path.vertices), None, {'interpolation_steps': self._path._interpolation_steps, 'should_simplify': self._path.should_simplify}) self._invalid = 0 def get_transformed_points_and_affine(self): """ Return a copy of the child path, with the non-affine part of the transform already applied, along with the affine part of the path necessary to complete the transformation. Unlike :meth:`get_transformed_path_and_affine`, no interpolation will be performed. """ self._revalidate() return self._transformed_points, self.get_affine() def get_transformed_path_and_affine(self): """ Return a copy of the child path, with the non-affine part of the transform already applied, along with the affine part of the path necessary to complete the transformation. """ self._revalidate() return self._transformed_path, self.get_affine() def get_fully_transformed_path(self): """ Return a fully-transformed copy of the child path. """ self._revalidate() return self._transform.transform_path_affine(self._transformed_path) def get_affine(self): return self._transform.get_affine() def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True): ''' Modify the endpoints of a range as needed to avoid singularities. *vmin*, *vmax* the initial endpoints. *tiny* threshold for the ratio of the interval to the maximum absolute value of its endpoints. If the interval is smaller than this, it will be expanded. This value should be around 1e-15 or larger; otherwise the interval will be approaching the double precision resolution limit. *expander* fractional amount by which *vmin* and *vmax* are expanded if the original interval is too small, based on *tiny*. *increasing*: [True | False] If True (default), swap *vmin*, *vmax* if *vmin* > *vmax* Returns *vmin*, *vmax*, expanded and/or swapped if necessary. If either input is inf or NaN, or if both inputs are 0 or very close to zero, it returns -*expander*, *expander*. ''' if (not np.isfinite(vmin)) or (not np.isfinite(vmax)): return -expander, expander swapped = False if vmax < vmin: vmin, vmax = vmax, vmin swapped = True maxabsvalue = max(abs(vmin), abs(vmax)) if maxabsvalue < (1e6 / tiny) * MINFLOAT: vmin = -expander vmax = expander elif vmax - vmin <= maxabsvalue * tiny: if vmax == 0 and vmin == 0: vmin = -expander vmax = expander else: vmin -= expander*abs(vmin) vmax += expander*abs(vmax) if swapped and not increasing: vmin, vmax = vmax, vmin return vmin, vmax def interval_contains(interval, val): a, b = interval return ( ((a < b) and (a <= val and b >= val)) or (b <= val and a >= val)) def interval_contains_open(interval, val): a, b = interval return ( ((a < b) and (a < val and b > val)) or (b < val and a > val)) def offset_copy(trans, fig=None, x=0.0, y=0.0, units='inches'): ''' Return a new transform with an added offset. args: trans is any transform kwargs: fig is the current figure; it can be None if units are 'dots' x, y give the offset units is 'inches', 'points' or 'dots' ''' if units == 'dots': return trans + Affine2D().translate(x, y) if fig is None: raise ValueError('For units of inches or points a fig kwarg is needed') if units == 'points': x /= 72.0 y /= 72.0 elif not units == 'inches': raise ValueError('units must be dots, points, or inches') return trans + ScaledTranslation(x, y, fig.dpi_scale_trans)
mit
wathen/PhD
MHD/FEniCS/MHD/Stabilised/SaddlePointForm/Test/SplitMatrix/ScottTest/Decouple/MU.py
1
13311
#!/usr/bin/python # interpolate scalar gradient onto nedelec space import petsc4py import sys petsc4py.init(sys.argv) from petsc4py import PETSc from dolfin import * # from MatrixOperations import * import numpy as np import PETScIO as IO import common import scipy import scipy.io import time import scipy.sparse as sp import BiLinear as forms import IterOperations as Iter import MatrixOperations as MO import CheckPetsc4py as CP import Solver as S import MHDmatrixPrecondSetup as PrecondSetup import NSprecondSetup import MHDprec as MHDpreconditioner import gc import MHDmulti import MHDmatrixSetup as MHDsetup import HartmanChannel # import matplotlib.pyplot as plt #@profile m = 6 set_log_active(False) errL2u = np.zeros((m-1,1)) errH1u = np.zeros((m-1,1)) errL2p = np.zeros((m-1,1)) errL2b = np.zeros((m-1,1)) errCurlb = np.zeros((m-1,1)) errL2r = np.zeros((m-1,1)) errH1r = np.zeros((m-1,1)) l2uorder = np.zeros((m-1,1)) H1uorder =np.zeros((m-1,1)) l2porder = np.zeros((m-1,1)) l2border = np.zeros((m-1,1)) Curlborder = np.zeros((m-1,1)) l2rorder = np.zeros((m-1,1)) H1rorder = np.zeros((m-1,1)) NN = np.zeros((m-1,1)) DoF = np.zeros((m-1,1)) Velocitydim = np.zeros((m-1,1)) Magneticdim = np.zeros((m-1,1)) Pressuredim = np.zeros((m-1,1)) Lagrangedim = np.zeros((m-1,1)) Wdim = np.zeros((m-1,1)) iterations = np.zeros((m-1,1)) SolTime = np.zeros((m-1,1)) udiv = np.zeros((m-1,1)) MU = np.zeros((m-1,1)) level = np.zeros((m-1,1)) NSave = np.zeros((m-1,1)) Mave = np.zeros((m-1,1)) TotalTime = np.zeros((m-1,1)) DimSave = np.zeros((m-1,4)) dim = 2 ShowResultPlots = 'yes' split = 'Linear' MU[0] = 1e0 K = [1., 10., 100.] K = [1., 1./10, 1./100] TableValues = np.zeros((m-1,len(K)*3)) SolTime = np.zeros((m-1,len(K)*3)) Decouple = ["P", "MD", "CD"] print len(K) # ii = 0 for xx in xrange(1,m): ii = 0 for i in xrange(0,len(K)): MU = K[i] for j in range(0, 3): DecoupleType = Decouple[j] IterType = Decouple[j] print xx level[xx-1] = xx + 3 nn = 2**(level[xx-1]) # Create mesh and define function space nn = int(nn) NN[xx-1] = nn/2 L = 10. y0 = 2. z0 = 1. mesh, boundaries, domains = HartmanChannel.Domain(nn) parameters['form_compiler']['quadrature_degree'] = -1 order = 2 parameters['reorder_dofs_serial'] = False Velocity = VectorElement("CG", mesh.ufl_cell(), order) Pressure = FiniteElement("CG", mesh.ufl_cell(), order-1) Magnetic = FiniteElement("N1curl", mesh.ufl_cell(), order-1) Lagrange = FiniteElement("CG", mesh.ufl_cell(), order-1) VelocityF = VectorFunctionSpace(mesh, "CG", order) PressureF = FunctionSpace(mesh, "CG", order-1) MagneticF = FunctionSpace(mesh, "N1curl", order-1) LagrangeF = FunctionSpace(mesh, "CG", order-1) W = FunctionSpace(mesh, MixedElement([Velocity, Pressure, Magnetic,Lagrange])) Velocitydim[xx-1] = W.sub(0).dim() Pressuredim[xx-1] = W.sub(1).dim() Magneticdim[xx-1] = W.sub(2).dim() Lagrangedim[xx-1] = W.sub(3).dim() Wdim[xx-1] = W.dim() print "\n\nW: ",Wdim[xx-1],"Velocity: ",Velocitydim[xx-1],"Pressure: ",Pressuredim[xx-1],"Magnetic: ",Magneticdim[xx-1],"Lagrange: ",Lagrangedim[xx-1],"\n\n" dim = [W.sub(0).dim(), W.sub(1).dim(), W.sub(2).dim(), W.sub(3).dim()] def boundary(x, on_boundary): return on_boundary FSpaces = [VelocityF,PressureF,MagneticF,LagrangeF] DimSave[xx-1,:] = np.array(dim) kappa = 1.0 Mu_m = 10.0 # MU = 1.0 N = FacetNormal(mesh) # IterType = 'Full' params = [kappa,Mu_m,MU] n = FacetNormal(mesh) trunc = 4 u0, p0, b0, r0, pN, Laplacian, Advection, gradPres, NScouple, CurlCurl, gradLagr, Mcouple = HartmanChannel.ExactSolution(mesh, params) # kappa = 0.0 # params = [kappa,Mu_m,MU] MO.PrintStr("Seting up initial guess matricies",2,"=","\n\n","\n") BCtime = time.time() BC = MHDsetup.BoundaryIndices(mesh) MO.StrTimePrint("BC index function, time: ", time.time()-BCtime) Hiptmairtol = 1e-6 HiptmairMatrices = PrecondSetup.MagneticSetup(mesh, Magnetic, Lagrange, b0, r0, Hiptmairtol, params) MO.PrintStr("Setting up MHD initial guess",5,"+","\n\n","\n\n") print params F_NS = -MU*Laplacian + Advection + gradPres - kappa*NScouple if kappa == 0.0: F_M = Mu_m*CurlCurl + gradLagr - kappa*Mcouple else: F_M = Mu_m*kappa*CurlCurl + gradLagr - kappa*Mcouple u_k, p_k = HartmanChannel.Stokes(Velocity, Pressure, F_NS, u0, pN, params, mesh, boundaries, domains) b_k, r_k = HartmanChannel.Maxwell(Magnetic, Lagrange, F_M, b0, r0, params, mesh, HiptmairMatrices, Hiptmairtol) (u, p, b, r) = TrialFunctions(W) (v, q, c, s) = TestFunctions(W) if kappa == 0.0: m11 = params[1]*inner(curl(b),curl(c))*dx else: r m11 = params[1]*params[0]*inner(curl(b),curl(c))*dx m21 = inner(c,grad(r))*dx m12 = inner(b,grad(s))*dx a11 = params[2]*inner(grad(v), grad(u))*dx O = inner((grad(u)*u_k),v)*dx + (1./2)*div(u_k)*inner(u,v)*dx - (1./2)*inner(u_k,n)*inner(u,v)*ds a12 = -div(v)*p*dx a21 = -div(u)*q*dx CoupleT = params[0]*(v[0]*b_k[1]-v[1]*b_k[0])*curl(b)*dx Couple = -params[0]*(u[0]*b_k[1]-u[1]*b_k[0])*curl(c)*dx if DecoupleType == "P": a = m11 + m12 + m21 + a11 + O + a21 + a12 + Couple + CoupleT elif DecoupleType == "MD": a = m11 + m12 + m21 + a11 + O + a21 + a12 elif DecoupleType == "CD": a = m11 + m12 + m21 + a11 + a21 + a12 Lns = inner(v, F_NS)*dx #- inner(pN*n,v)*ds(2) Lmaxwell = inner(c, F_M)*dx if kappa == 0.0: m11 = params[1]*params[0]*inner(curl(b_k),curl(c))*dx else: m11 = params[1]*inner(curl(b_k),curl(c))*dx m21 = inner(c,grad(r_k))*dx m12 = inner(b_k,grad(s))*dx a11 = params[2]*inner(grad(v), grad(u_k))*dx + inner((grad(u_k)*u_k),v)*dx + (1./2)*div(u_k)*inner(u_k,v)*dx - (1./2)*inner(u_k,n)*inner(u_k,v)*ds a12 = -div(v)*p_k*dx a21 = -div(u_k)*q*dx CoupleT = params[0]*(v[0]*b_k[1]-v[1]*b_k[0])*curl(b_k)*dx Couple = -params[0]*(u_k[0]*b_k[1]-u_k[1]*b_k[0])*curl(c)*dx L = Lns + Lmaxwell - (m11 + m12 + m21 + a11 + a21 + a12 + Couple + CoupleT) ones = Function(PressureF) ones.vector()[:]=(0*ones.vector().array()+1) pConst = - assemble(p_k*dx)/assemble(ones*dx) p_k.vector()[:] += - assemble(p_k*dx)/assemble(ones*dx) x = Iter.u_prev(u_k,p_k,b_k,r_k) KSPlinearfluids, MatrixLinearFluids = PrecondSetup.FluidLinearSetup(PressureF, MU, mesh) kspFp, Fp = PrecondSetup.FluidNonLinearSetup(PressureF, MU, u_k, mesh) IS = MO.IndexSet(W, 'Blocks') eps = 1.0 # error measure ||u-u_k|| tol = 1.0E-4 # tolerance iter = 0 # iteration counter maxiter = 50 # max no of iterations allowed SolutionTime = 0 outer = 0 # parameters['linear_algebra_backend'] = 'uBLAS' u_is = PETSc.IS().createGeneral(W.sub(0).dofmap().dofs()) b_is = PETSc.IS().createGeneral(W.sub(2).dofmap().dofs()) NS_is = PETSc.IS().createGeneral(range(VelocityF.dim()+PressureF.dim())) M_is = PETSc.IS().createGeneral(range(VelocityF.dim()+PressureF.dim(),W.dim())) OuterTol = 1e-5 InnerTol = 1e-5 NSits = 0 Mits = 0 TotalStart = time.time() SolutionTime = 0 while eps > tol and iter < maxiter: iter += 1 MO.PrintStr("Iter "+str(iter),7,"=","\n\n","\n\n") bcu = DirichletBC(W.sub(0),Expression(("0.0","0.0"), degree=4), boundary) #bcu = DirichletBC(W.sub(0),Expression(("0.0","0.0")), boundary) bcb = DirichletBC(W.sub(2),Expression(("0.0","0.0"),degree=4), boundary) bcr = DirichletBC(W.sub(3),Expression("0.0",degree=4), boundary) bcs = [bcu,bcb,bcr] initial = Function(W) R = action(a,initial); DR = derivative(R, initial); A, b = assemble_system(a, L, bcs) A, b = CP.Assemble(A,b) u = b.duplicate() u.setRandom() print " Max rhs = ",np.max(b.array) kspFp, Fp = PrecondSetup.FluidNonLinearSetup(PressureF, MU, u_k, mesh) # b_t = TrialFunction(Velocity) # c_t = TestFunction(Velocity) # n = FacetNormal(mesh) # mat = as_matrix([[b_k[1]*b_k[1],-b_k[1]*b_k[0]],[-b_k[1]*b_k[0],b_k[0]*b_k[0]]]) # aa = params[2]*inner(grad(b_t), grad(c_t))*dx(W.mesh()) + inner((grad(b_t)*u_k),c_t)*dx(W.mesh()) +(1./2)*div(u_k)*inner(c_t,b_t)*dx(W.mesh()) - (1./2)*inner(u_k,n)*inner(c_t,b_t)*ds(W.mesh())+kappa/Mu_m*inner(mat*b_t,c_t)*dx(W.mesh()) # ShiftedMass = assemble(aa) # bcu.apply(ShiftedMass) # ShiftedMass = CP.Assemble(ShiftedMass) ShiftedMass = A.getSubMatrix(u_is, u_is) kspF = NSprecondSetup.LSCKSPnonlinear(ShiftedMass) Options = 'p4' stime = time.time() u, mits,nsits = S.solve(A,b,u,params,W,'Direct',IterType,OuterTol,InnerTol,HiptmairMatrices,Hiptmairtol,KSPlinearfluids, Fp,kspF) MO.PrintStr(str((u).norm()),70,"=","\n\n","\n\n") Soltime = time.time() - stime MO.StrTimePrint("MHD solve, time: ", Soltime) Mits += mits NSits += mits SolutionTime += Soltime # u = IO.arrayToVec( u) u1, p1, b1, r1, eps = Iter.PicardToleranceDecouple(u,x,FSpaces,dim,"2",iter) p1.vector()[:] += - assemble(p1*dx)/assemble(ones*dx) u_k.assign(u1) p_k.assign(p1) b_k.assign(b1) r_k.assign(r1) uOld = np.concatenate((u_k.vector().array(),p_k.vector().array(),b_k.vector().array(),r_k.vector().array()), axis=0) x = IO.arrayToVec(uOld) if eps > 1e10: iter = 100000 break # b = assemble(L) # for bc in bcs: # bc.apply(b) # print (b-A*u).norm() # sss # iter = 1 SolTime[xx-1, ii] = SolutionTime/iter NSave[xx-1] = (float(NSits)/iter) Mave[xx-1] = (float(Mits)/iter) iterations[xx-1] = iter TotalTime[xx-1] = time.time() - TotalStart XX= np.concatenate((u_k.vector().array(),p_k.vector().array(),b_k.vector().array(),r_k.vector().array()), axis=0) ExactSolution = [u0,p0,b0,r0] TableValues[xx-1, ii] = iter ii += 1 print TableValues print SolTime import pandas as pd print "\n\n Iterations" IterTitles = ["l","DoF"] + ["P", "MD", "CD"] + ["P", "MD", "CD"] + ["P", "MD", "CD"] IterValues = np.concatenate((level,Wdim,TableValues),axis=1) IterTable= pd.DataFrame(IterValues, columns = IterTitles) print IterTable.to_latex() print "\n\n Time" IterValues = np.concatenate((level,Wdim,SolTime),axis=1) IterTable= pd.DataFrame(IterValues, columns = IterTitles) print IterTable.to_latex() MO.StoreMatrix(DimSave, "dim") # {\setlength{\tabcolsep}{.21em} \begin{table} # \begin{tabular}{|cc|ccc|ccc|ccc|ccc|} # \hline # \multicolumn{2}{|c|}{} & \multicolumn{3}{|c|}{tol$_{\rm L}=$1e-6} & \multicolumn{3}{|c|}{tol$_{\rm L}=$1e-5} & \multicolumn{3}{|c|}{tol$_{\rm L}=$1e-4} & \multicolumn{3}{|c|}{tol$_{\rm L}=$1e-3} \\ # $\ell$ & DoF & it$_{\rm NL}$ & it$_{\rm L}$ & time & it$_{\rm NL}$ & it$_{\rm L}$ & time & it$_{\rm NL}$ & it$_{\rm L}$ & time & it$_{\rm NL}$ & tol$_{\rm L}$ & time \\ # \hline # 4 & 3,556 & 5 & 36.0 & 2.16 & 6 & 33.2 & 2.49 & - & 29.0 & 9.40 & - & 25.1 & 8.48 \\ # 5 & 13,764 & 5 & 42.0 & 13.58 & 5 & 37.8 & 12.12 & 12 & 33.7 & 26.69 & - & 29.8 & 48.09 \\ # 6 & 54,148 & 5 & 42.2 & 57.71 & 5 & 39.4 & 54.47 & 5 & 37.2 & 51.36 & - & 31.4 & 220.93 \\ # 7 & 214,788 & 5 & 51.6 & 299.48 & 5 & 41.2 & 240.10 & 5 & 38.6 & 231.42 & - & 32.9 & 985.58 \\ # 8 & 855,556 & 5 & 52.4 & 1485.20 & 5 & 42.0 & 1230.99 & 5 & 35.6 & 1126.70 & 20 & 35.0 & 4306.09 \\ # \hline # \end{tabular} # \caption{tol$_{\rm NL}=$1e-4} # \end{table}}
mit
evidation-health/bokeh
bokeh/_legacy_charts/builder/tests/test_horizon_builder.py
6
3422
""" This is the Bokeh charts testing interface. """ #----------------------------------------------------------------------------- # Copyright (c) 2012 - 2014, Continuum Analytics, Inc. All rights reserved. # # Powered by the Bokeh Development Team. # # The full license is in the file LICENSE.txt, distributed with this software. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- from __future__ import absolute_import from collections import OrderedDict import datetime import unittest from numpy.testing import assert_array_equal import pandas as pd from bokeh._legacy_charts import Horizon from ._utils import create_chart #----------------------------------------------------------------------------- # Classes and functions #----------------------------------------------------------------------------- class TestHorizon(unittest.TestCase): def test_supported_input(self): now = datetime.datetime.now() delta = datetime.timedelta(minutes=1) dts = [now + delta*i for i in range(6)] xyvalues = OrderedDict({'Date': dts}) # Repeat the starting and trailing points in order to xyvalues['python'] = [-120, -120, -30, 50, 100, 103] xyvalues['pypy'] = [-75, -75, -33, 15, 126, 126] xyvaluesdf = pd.DataFrame(xyvalues) groups = ['python', 'pypy'] for i, _xy in enumerate([xyvalues, xyvaluesdf]): ts = create_chart(Horizon, _xy, index='Date') builder = ts._builders[0] padded_date = [x for x in _xy['Date']] padded_date.insert(0, padded_date[0]) padded_date.append(padded_date[-1]) self.assertEqual(builder.num_folds, 3) self.assertEqual(builder._series, groups) self.assertEqual(builder._fold_height, 126.0 / 3) self.assertEqual(builder._groups, ['42.0', '-42.0', '84.0', '-84.0', '126.0', '-126.0']) assert_array_equal(builder._data['x_python'], padded_date) assert_array_equal(builder._data['x_pypy'], padded_date) assert_array_equal(builder._data['y_fold-3_python'], [63, 9, 9 ,63, 63, 63, 63, 63]) assert_array_equal(builder._data['y_fold-2_python'], [63, 0, 0, 63, 63, 63, 63, 63]) assert_array_equal(builder._data['y_fold-1_python'], [63, 0, 0, 18, 63, 63, 63, 63]) assert_array_equal(builder._data['y_fold1_python'], [0, 0, 0, 0, 63, 63, 63, 0]) assert_array_equal(builder._data['y_fold2_python'], [0, 0, 0, 0, 12, 63, 63, 0]) assert_array_equal(builder._data['y_fold3_python'], [0, 0, 0, 0, 0, 24, 28.5, 0]) assert_array_equal(builder._data['y_fold-3_pypy'], [126, 126, 126, 126, 126, 126, 126, 126]) assert_array_equal(builder._data['y_fold-2_pypy'], [126, 76.5, 76.5, 126, 126, 126, 126, 126]) assert_array_equal(builder._data['y_fold-1_pypy'], [126, 63, 63, 76.5, 126, 126, 126, 126]) assert_array_equal(builder._data['y_fold1_pypy'], [63, 63, 63, 63, 85.5, 126, 126, 63]) assert_array_equal(builder._data['y_fold2_pypy'], [63, 63, 63, 63, 63, 126, 126, 63]) assert_array_equal(builder._data['y_fold3_pypy'], [63, 63, 63, 63, 63, 126, 126, 63])
bsd-3-clause
jakobworldpeace/scikit-learn
sklearn/utils/estimator_checks.py
16
64623
from __future__ import print_function import types import warnings import sys import traceback import pickle from copy import deepcopy import numpy as np from scipy import sparse from scipy.stats import rankdata import struct from sklearn.externals.six.moves import zip from sklearn.externals.joblib import hash, Memory from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_raises_regex from sklearn.utils.testing import assert_raise_message from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_not_equal from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_false from sklearn.utils.testing import assert_in from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_warns_message from sklearn.utils.testing import META_ESTIMATORS from sklearn.utils.testing import set_random_state from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_greater_equal from sklearn.utils.testing import SkipTest from sklearn.utils.testing import ignore_warnings from sklearn.utils.testing import assert_dict_equal from sklearn.base import (clone, ClassifierMixin, RegressorMixin, TransformerMixin, ClusterMixin, BaseEstimator) from sklearn.metrics import accuracy_score, adjusted_rand_score, f1_score from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.random_projection import BaseRandomProjection from sklearn.feature_selection import SelectKBest from sklearn.svm.base import BaseLibSVM from sklearn.pipeline import make_pipeline from sklearn.exceptions import ConvergenceWarning from sklearn.exceptions import DataConversionWarning from sklearn.exceptions import SkipTestWarning from sklearn.model_selection import train_test_split from sklearn.utils import shuffle from sklearn.utils.fixes import signature from sklearn.utils.validation import has_fit_parameter from sklearn.preprocessing import StandardScaler from sklearn.datasets import load_iris, load_boston, make_blobs BOSTON = None CROSS_DECOMPOSITION = ['PLSCanonical', 'PLSRegression', 'CCA', 'PLSSVD'] MULTI_OUTPUT = ['CCA', 'DecisionTreeRegressor', 'ElasticNet', 'ExtraTreeRegressor', 'ExtraTreesRegressor', 'GaussianProcess', 'GaussianProcessRegressor', 'KNeighborsRegressor', 'KernelRidge', 'Lars', 'Lasso', 'LassoLars', 'LinearRegression', 'MultiTaskElasticNet', 'MultiTaskElasticNetCV', 'MultiTaskLasso', 'MultiTaskLassoCV', 'OrthogonalMatchingPursuit', 'PLSCanonical', 'PLSRegression', 'RANSACRegressor', 'RadiusNeighborsRegressor', 'RandomForestRegressor', 'Ridge', 'RidgeCV'] def _yield_non_meta_checks(name, Estimator): yield check_estimators_dtypes yield check_fit_score_takes_y yield check_dtype_object yield check_sample_weights_pandas_series yield check_sample_weights_list yield check_estimators_fit_returns_self # Check that all estimator yield informative messages when # trained on empty datasets yield check_estimators_empty_data_messages if name not in CROSS_DECOMPOSITION + ['SpectralEmbedding']: # SpectralEmbedding is non-deterministic, # see issue #4236 # cross-decomposition's "transform" returns X and Y yield check_pipeline_consistency if name not in ['Imputer']: # Test that all estimators check their input for NaN's and infs yield check_estimators_nan_inf if name not in ['GaussianProcess']: # FIXME! # in particular GaussianProcess! yield check_estimators_overwrite_params if hasattr(Estimator, 'sparsify'): yield check_sparsify_coefficients yield check_estimator_sparse_data # Test that estimators can be pickled, and once pickled # give the same answer as before. yield check_estimators_pickle def _yield_classifier_checks(name, Classifier): # test classifiers can handle non-array data yield check_classifier_data_not_an_array # test classifiers trained on a single label always return this label yield check_classifiers_one_label yield check_classifiers_classes yield check_estimators_partial_fit_n_features # basic consistency testing yield check_classifiers_train yield check_classifiers_regression_target if (name not in ["MultinomialNB", "LabelPropagation", "LabelSpreading"] and # TODO some complication with -1 label name not in ["DecisionTreeClassifier", "ExtraTreeClassifier"]): # We don't raise a warning in these classifiers, as # the column y interface is used by the forests. yield check_supervised_y_2d # test if NotFittedError is raised yield check_estimators_unfitted if 'class_weight' in Classifier().get_params().keys(): yield check_class_weight_classifiers yield check_non_transformer_estimators_n_iter # test if predict_proba is a monotonic transformation of decision_function yield check_decision_proba_consistency @ignore_warnings(category=DeprecationWarning) def check_supervised_y_no_nan(name, Estimator): # Checks that the Estimator targets are not NaN. rng = np.random.RandomState(888) X = rng.randn(10, 5) y = np.ones(10) * np.inf y = multioutput_estimator_convert_y_2d(name, y) errmsg = "Input contains NaN, infinity or a value too large for " \ "dtype('float64')." try: Estimator().fit(X, y) except ValueError as e: if str(e) != errmsg: raise ValueError("Estimator {0} raised warning as expected, but " "does not match expected error message" .format(name)) else: raise ValueError("Estimator {0} should have raised error on fitting " "array y with NaN value.".format(name)) def _yield_regressor_checks(name, Regressor): # TODO: test with intercept # TODO: test with multiple responses # basic testing yield check_regressors_train yield check_regressor_data_not_an_array yield check_estimators_partial_fit_n_features yield check_regressors_no_decision_function yield check_supervised_y_2d yield check_supervised_y_no_nan if name != 'CCA': # check that the regressor handles int input yield check_regressors_int if name != "GaussianProcessRegressor": # Test if NotFittedError is raised yield check_estimators_unfitted yield check_non_transformer_estimators_n_iter def _yield_transformer_checks(name, Transformer): # All transformers should either deal with sparse data or raise an # exception with type TypeError and an intelligible error message if name not in ['AdditiveChi2Sampler', 'Binarizer', 'Normalizer', 'PLSCanonical', 'PLSRegression', 'CCA', 'PLSSVD']: yield check_transformer_data_not_an_array # these don't actually fit the data, so don't raise errors if name not in ['AdditiveChi2Sampler', 'Binarizer', 'FunctionTransformer', 'Normalizer']: # basic tests yield check_transformer_general yield check_transformers_unfitted # Dependent on external solvers and hence accessing the iter # param is non-trivial. external_solver = ['Isomap', 'KernelPCA', 'LocallyLinearEmbedding', 'RandomizedLasso', 'LogisticRegressionCV'] if name not in external_solver: yield check_transformer_n_iter def _yield_clustering_checks(name, Clusterer): yield check_clusterer_compute_labels_predict if name not in ('WardAgglomeration', "FeatureAgglomeration"): # this is clustering on the features # let's not test that here. yield check_clustering yield check_estimators_partial_fit_n_features yield check_non_transformer_estimators_n_iter def _yield_all_checks(name, Estimator): for check in _yield_non_meta_checks(name, Estimator): yield check if issubclass(Estimator, ClassifierMixin): for check in _yield_classifier_checks(name, Estimator): yield check if issubclass(Estimator, RegressorMixin): for check in _yield_regressor_checks(name, Estimator): yield check if issubclass(Estimator, TransformerMixin): for check in _yield_transformer_checks(name, Estimator): yield check if issubclass(Estimator, ClusterMixin): for check in _yield_clustering_checks(name, Estimator): yield check yield check_fit2d_predict1d yield check_fit2d_1sample yield check_fit2d_1feature yield check_fit1d_1feature yield check_fit1d_1sample yield check_get_params_invariance yield check_dict_unchanged yield check_no_fit_attributes_set_in_init yield check_dont_overwrite_parameters def check_estimator(Estimator): """Check if estimator adheres to scikit-learn conventions. This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests for classifiers, regressors, clustering or transformers will be run if the Estimator class inherits from the corresponding mixin from sklearn.base. Parameters ---------- Estimator : class Class to check. Estimator is a class object (not an instance). """ name = Estimator.__name__ check_parameters_default_constructible(name, Estimator) for check in _yield_all_checks(name, Estimator): try: check(name, Estimator) except SkipTest as message: # the only SkipTest thrown currently results from not # being able to import pandas. warnings.warn(message, SkipTestWarning) def _boston_subset(n_samples=200): global BOSTON if BOSTON is None: boston = load_boston() X, y = boston.data, boston.target X, y = shuffle(X, y, random_state=0) X, y = X[:n_samples], y[:n_samples] X = StandardScaler().fit_transform(X) BOSTON = X, y return BOSTON def set_testing_parameters(estimator): # set parameters to speed up some estimators and # avoid deprecated behaviour params = estimator.get_params() if ("n_iter" in params and estimator.__class__.__name__ != "TSNE"): estimator.set_params(n_iter=5) if "max_iter" in params: warnings.simplefilter("ignore", ConvergenceWarning) if estimator.max_iter is not None: estimator.set_params(max_iter=min(5, estimator.max_iter)) # LinearSVR if estimator.__class__.__name__ == 'LinearSVR': estimator.set_params(max_iter=20) # NMF if estimator.__class__.__name__ == 'NMF': estimator.set_params(max_iter=100) # MLP if estimator.__class__.__name__ in ['MLPClassifier', 'MLPRegressor']: estimator.set_params(max_iter=100) if "n_resampling" in params: # randomized lasso estimator.set_params(n_resampling=5) if "n_estimators" in params: # especially gradient boosting with default 100 estimator.set_params(n_estimators=min(5, estimator.n_estimators)) if "max_trials" in params: # RANSAC estimator.set_params(max_trials=10) if "n_init" in params: # K-Means estimator.set_params(n_init=2) if "decision_function_shape" in params: # SVC estimator.set_params(decision_function_shape='ovo') if estimator.__class__.__name__ == "SelectFdr": # be tolerant of noisy datasets (not actually speed) estimator.set_params(alpha=.5) if estimator.__class__.__name__ == "TheilSenRegressor": estimator.max_subpopulation = 100 if isinstance(estimator, BaseRandomProjection): # Due to the jl lemma and often very few samples, the number # of components of the random matrix projection will be probably # greater than the number of features. # So we impose a smaller number (avoid "auto" mode) estimator.set_params(n_components=1) if isinstance(estimator, SelectKBest): # SelectKBest has a default of k=10 # which is more feature than we have in most case. estimator.set_params(k=1) class NotAnArray(object): " An object that is convertable to an array" def __init__(self, data): self.data = data def __array__(self, dtype=None): return self.data def _is_32bit(): """Detect if process is 32bit Python.""" return struct.calcsize('P') * 8 == 32 def check_estimator_sparse_data(name, Estimator): rng = np.random.RandomState(0) X = rng.rand(40, 10) X[X < .8] = 0 X_csr = sparse.csr_matrix(X) y = (4 * rng.rand(40)).astype(np.int) for sparse_format in ['csr', 'csc', 'dok', 'lil', 'coo', 'dia', 'bsr']: X = X_csr.asformat(sparse_format) # catch deprecation warnings with ignore_warnings(category=DeprecationWarning): if name in ['Scaler', 'StandardScaler']: estimator = Estimator(with_mean=False) else: estimator = Estimator() set_testing_parameters(estimator) # fit and predict try: with ignore_warnings(category=DeprecationWarning): estimator.fit(X, y) if hasattr(estimator, "predict"): pred = estimator.predict(X) assert_equal(pred.shape, (X.shape[0],)) if hasattr(estimator, 'predict_proba'): probs = estimator.predict_proba(X) assert_equal(probs.shape, (X.shape[0], 4)) except TypeError as e: if 'sparse' not in repr(e): print("Estimator %s doesn't seem to fail gracefully on " "sparse data: error message state explicitly that " "sparse input is not supported if this is not the case." % name) raise except Exception: print("Estimator %s doesn't seem to fail gracefully on " "sparse data: it should raise a TypeError if sparse input " "is explicitly not supported." % name) raise @ignore_warnings(category=DeprecationWarning) def check_sample_weights_pandas_series(name, Estimator): # check that estimators will accept a 'sample_weight' parameter of # type pandas.Series in the 'fit' function. estimator = Estimator() if has_fit_parameter(estimator, "sample_weight"): try: import pandas as pd X = pd.DataFrame([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]]) y = pd.Series([1, 1, 1, 2, 2, 2]) weights = pd.Series([1] * 6) try: estimator.fit(X, y, sample_weight=weights) except ValueError: raise ValueError("Estimator {0} raises error if " "'sample_weight' parameter is of " "type pandas.Series".format(name)) except ImportError: raise SkipTest("pandas is not installed: not testing for " "input of type pandas.Series to class weight.") @ignore_warnings(category=DeprecationWarning) def check_sample_weights_list(name, Estimator): # check that estimators will accept a 'sample_weight' parameter of # type list in the 'fit' function. estimator = Estimator() if has_fit_parameter(estimator, "sample_weight"): rnd = np.random.RandomState(0) X = rnd.uniform(size=(10, 3)) y = np.arange(10) % 3 y = multioutput_estimator_convert_y_2d(name, y) sample_weight = [3] * 10 # Test that estimators don't raise any exception estimator.fit(X, y, sample_weight=sample_weight) @ignore_warnings(category=(DeprecationWarning, UserWarning)) def check_dtype_object(name, Estimator): # check that estimators treat dtype object as numeric if possible rng = np.random.RandomState(0) X = rng.rand(40, 10).astype(object) y = (X[:, 0] * 4).astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) estimator.fit(X, y) if hasattr(estimator, "predict"): estimator.predict(X) if hasattr(estimator, "transform"): estimator.transform(X) try: estimator.fit(X, y.astype(object)) except Exception as e: if "Unknown label type" not in str(e): raise X[0, 0] = {'foo': 'bar'} msg = "argument must be a string or a number" assert_raises_regex(TypeError, msg, estimator.fit, X, y) @ignore_warnings def check_dict_unchanged(name, Estimator): # this estimator raises # ValueError: Found array with 0 feature(s) (shape=(23, 0)) # while a minimum of 1 is required. # error if name in ['SpectralCoclustering']: return rnd = np.random.RandomState(0) if name in ['RANSACRegressor']: X = 3 * rnd.uniform(size=(20, 3)) else: X = 2 * rnd.uniform(size=(20, 3)) y = X[:, 0].astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 if hasattr(estimator, "n_best"): estimator.n_best = 1 set_random_state(estimator, 1) # should be just `estimator.fit(X, y)` # after merging #6141 if name in ['SpectralBiclustering']: estimator.fit(X) else: estimator.fit(X, y) for method in ["predict", "transform", "decision_function", "predict_proba"]: if hasattr(estimator, method): dict_before = estimator.__dict__.copy() getattr(estimator, method)(X) assert_dict_equal(estimator.__dict__, dict_before, 'Estimator changes __dict__ during %s' % method) def is_public_parameter(attr): return not (attr.startswith('_') or attr.endswith('_')) def check_dont_overwrite_parameters(name, Estimator): # check that fit method only changes or sets private attributes if hasattr(Estimator.__init__, "deprecated_original"): # to not check deprecated classes return rnd = np.random.RandomState(0) X = 3 * rnd.uniform(size=(20, 3)) y = X[:, 0].astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 set_random_state(estimator, 1) dict_before_fit = estimator.__dict__.copy() estimator.fit(X, y) dict_after_fit = estimator.__dict__ public_keys_after_fit = [key for key in dict_after_fit.keys() if is_public_parameter(key)] attrs_added_by_fit = [key for key in public_keys_after_fit if key not in dict_before_fit.keys()] # check that fit doesn't add any public attribute assert_true(not attrs_added_by_fit, ('Estimator adds public attribute(s) during' ' the fit method.' ' Estimators are only allowed to add private attributes' ' either started with _ or ended' ' with _ but %s added' % ', '.join(attrs_added_by_fit))) # check that fit doesn't change any public attribute attrs_changed_by_fit = [key for key in public_keys_after_fit if (dict_before_fit[key] is not dict_after_fit[key])] assert_true(not attrs_changed_by_fit, ('Estimator changes public attribute(s) during' ' the fit method. Estimators are only allowed' ' to change attributes started' ' or ended with _, but' ' %s changed' % ', '.join(attrs_changed_by_fit))) def check_fit2d_predict1d(name, Estimator): # check by fitting a 2d array and predicting with a 1d array rnd = np.random.RandomState(0) X = 3 * rnd.uniform(size=(20, 3)) y = X[:, 0].astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 set_random_state(estimator, 1) estimator.fit(X, y) for method in ["predict", "transform", "decision_function", "predict_proba"]: if hasattr(estimator, method): assert_raise_message(ValueError, "Reshape your data", getattr(estimator, method), X[0]) @ignore_warnings def check_fit2d_1sample(name, Estimator): # check by fitting a 2d array and prediting with a 1d array rnd = np.random.RandomState(0) X = 3 * rnd.uniform(size=(1, 10)) y = X[:, 0].astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 set_random_state(estimator, 1) try: estimator.fit(X, y) except ValueError: pass @ignore_warnings def check_fit2d_1feature(name, Estimator): # check by fitting a 2d array and prediting with a 1d array rnd = np.random.RandomState(0) X = 3 * rnd.uniform(size=(10, 1)) y = X[:, 0].astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 set_random_state(estimator, 1) try: estimator.fit(X, y) except ValueError: pass @ignore_warnings def check_fit1d_1feature(name, Estimator): # check fitting 1d array with 1 feature rnd = np.random.RandomState(0) X = 3 * rnd.uniform(size=(20)) y = X.astype(np.int) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 set_random_state(estimator, 1) try: estimator.fit(X, y) except ValueError: pass @ignore_warnings def check_fit1d_1sample(name, Estimator): # check fitting 1d array with 1 feature rnd = np.random.RandomState(0) X = 3 * rnd.uniform(size=(20)) y = np.array([1]) y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) if hasattr(estimator, "n_components"): estimator.n_components = 1 if hasattr(estimator, "n_clusters"): estimator.n_clusters = 1 set_random_state(estimator, 1) try: estimator.fit(X, y) except ValueError: pass @ignore_warnings(category=DeprecationWarning) def check_transformer_general(name, Transformer): X, y = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) X = StandardScaler().fit_transform(X) X -= X.min() _check_transformer(name, Transformer, X, y) _check_transformer(name, Transformer, X.tolist(), y.tolist()) @ignore_warnings(category=DeprecationWarning) def check_transformer_data_not_an_array(name, Transformer): X, y = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) X = StandardScaler().fit_transform(X) # We need to make sure that we have non negative data, for things # like NMF X -= X.min() - .1 this_X = NotAnArray(X) this_y = NotAnArray(np.asarray(y)) _check_transformer(name, Transformer, this_X, this_y) def check_transformers_unfitted(name, Transformer): X, y = _boston_subset() with ignore_warnings(category=DeprecationWarning): transformer = Transformer() assert_raises((AttributeError, ValueError), transformer.transform, X) def _check_transformer(name, Transformer, X, y): if name in ('CCA', 'LocallyLinearEmbedding', 'KernelPCA') and _is_32bit(): # Those transformers yield non-deterministic output when executed on # a 32bit Python. The same transformers are stable on 64bit Python. # FIXME: try to isolate a minimalistic reproduction case only depending # on numpy & scipy and/or maybe generate a test dataset that does not # cause such unstable behaviors. msg = name + ' is non deterministic on 32bit Python' raise SkipTest(msg) n_samples, n_features = np.asarray(X).shape # catch deprecation warnings transformer = Transformer() set_random_state(transformer) set_testing_parameters(transformer) # fit if name in CROSS_DECOMPOSITION: y_ = np.c_[y, y] y_[::2, 1] *= 2 else: y_ = y transformer.fit(X, y_) # fit_transform method should work on non fitted estimator transformer_clone = clone(transformer) X_pred = transformer_clone.fit_transform(X, y=y_) if isinstance(X_pred, tuple): for x_pred in X_pred: assert_equal(x_pred.shape[0], n_samples) else: # check for consistent n_samples assert_equal(X_pred.shape[0], n_samples) if hasattr(transformer, 'transform'): if name in CROSS_DECOMPOSITION: X_pred2 = transformer.transform(X, y_) X_pred3 = transformer.fit_transform(X, y=y_) else: X_pred2 = transformer.transform(X) X_pred3 = transformer.fit_transform(X, y=y_) if isinstance(X_pred, tuple) and isinstance(X_pred2, tuple): for x_pred, x_pred2, x_pred3 in zip(X_pred, X_pred2, X_pred3): assert_array_almost_equal( x_pred, x_pred2, 2, "fit_transform and transform outcomes not consistent in %s" % Transformer) assert_array_almost_equal( x_pred, x_pred3, 2, "consecutive fit_transform outcomes not consistent in %s" % Transformer) else: assert_array_almost_equal( X_pred, X_pred2, 2, "fit_transform and transform outcomes not consistent in %s" % Transformer) assert_array_almost_equal( X_pred, X_pred3, 2, "consecutive fit_transform outcomes not consistent in %s" % Transformer) assert_equal(len(X_pred2), n_samples) assert_equal(len(X_pred3), n_samples) # raises error on malformed input for transform if hasattr(X, 'T'): # If it's not an array, it does not have a 'T' property assert_raises(ValueError, transformer.transform, X.T) @ignore_warnings def check_pipeline_consistency(name, Estimator): if name in ('CCA', 'LocallyLinearEmbedding', 'KernelPCA') and _is_32bit(): # Those transformers yield non-deterministic output when executed on # a 32bit Python. The same transformers are stable on 64bit Python. # FIXME: try to isolate a minimalistic reproduction case only depending # scipy and/or maybe generate a test dataset that does not # cause such unstable behaviors. msg = name + ' is non deterministic on 32bit Python' raise SkipTest(msg) # check that make_pipeline(est) gives same score as est X, y = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) X -= X.min() y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator) pipeline = make_pipeline(estimator) estimator.fit(X, y) pipeline.fit(X, y) funcs = ["score", "fit_transform"] for func_name in funcs: func = getattr(estimator, func_name, None) if func is not None: func_pipeline = getattr(pipeline, func_name) result = func(X, y) result_pipe = func_pipeline(X, y) assert_array_almost_equal(result, result_pipe) @ignore_warnings def check_fit_score_takes_y(name, Estimator): # check that all estimators accept an optional y # in fit and score so they can be used in pipelines rnd = np.random.RandomState(0) X = rnd.uniform(size=(10, 3)) y = np.arange(10) % 3 y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator) funcs = ["fit", "score", "partial_fit", "fit_predict", "fit_transform"] for func_name in funcs: func = getattr(estimator, func_name, None) if func is not None: func(X, y) args = [p.name for p in signature(func).parameters.values()] assert_true(args[1] in ["y", "Y"], "Expected y or Y as second argument for method " "%s of %s. Got arguments: %r." % (func_name, Estimator.__name__, args)) @ignore_warnings def check_estimators_dtypes(name, Estimator): rnd = np.random.RandomState(0) X_train_32 = 3 * rnd.uniform(size=(20, 5)).astype(np.float32) X_train_64 = X_train_32.astype(np.float64) X_train_int_64 = X_train_32.astype(np.int64) X_train_int_32 = X_train_32.astype(np.int32) y = X_train_int_64[:, 0] y = multioutput_estimator_convert_y_2d(name, y) methods = ["predict", "transform", "decision_function", "predict_proba"] for X_train in [X_train_32, X_train_64, X_train_int_64, X_train_int_32]: estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator, 1) estimator.fit(X_train, y) for method in methods: if hasattr(estimator, method): getattr(estimator, method)(X_train) @ignore_warnings(category=DeprecationWarning) def check_estimators_empty_data_messages(name, Estimator): e = Estimator() set_testing_parameters(e) set_random_state(e, 1) X_zero_samples = np.empty(0).reshape(0, 3) # The precise message can change depending on whether X or y is # validated first. Let us test the type of exception only: assert_raises(ValueError, e.fit, X_zero_samples, []) X_zero_features = np.empty(0).reshape(3, 0) # the following y should be accepted by both classifiers and regressors # and ignored by unsupervised models y = multioutput_estimator_convert_y_2d(name, np.array([1, 0, 1])) msg = ("0 feature\(s\) \(shape=\(3, 0\)\) while a minimum of \d* " "is required.") assert_raises_regex(ValueError, msg, e.fit, X_zero_features, y) def check_estimators_nan_inf(name, Estimator): # Checks that Estimator X's do not contain NaN or inf. rnd = np.random.RandomState(0) X_train_finite = rnd.uniform(size=(10, 3)) X_train_nan = rnd.uniform(size=(10, 3)) X_train_nan[0, 0] = np.nan X_train_inf = rnd.uniform(size=(10, 3)) X_train_inf[0, 0] = np.inf y = np.ones(10) y[:5] = 0 y = multioutput_estimator_convert_y_2d(name, y) error_string_fit = "Estimator doesn't check for NaN and inf in fit." error_string_predict = ("Estimator doesn't check for NaN and inf in" " predict.") error_string_transform = ("Estimator doesn't check for NaN and inf in" " transform.") for X_train in [X_train_nan, X_train_inf]: # catch deprecation warnings with ignore_warnings(category=DeprecationWarning): estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator, 1) # try to fit try: estimator.fit(X_train, y) except ValueError as e: if 'inf' not in repr(e) and 'NaN' not in repr(e): print(error_string_fit, Estimator, e) traceback.print_exc(file=sys.stdout) raise e except Exception as exc: print(error_string_fit, Estimator, exc) traceback.print_exc(file=sys.stdout) raise exc else: raise AssertionError(error_string_fit, Estimator) # actually fit estimator.fit(X_train_finite, y) # predict if hasattr(estimator, "predict"): try: estimator.predict(X_train) except ValueError as e: if 'inf' not in repr(e) and 'NaN' not in repr(e): print(error_string_predict, Estimator, e) traceback.print_exc(file=sys.stdout) raise e except Exception as exc: print(error_string_predict, Estimator, exc) traceback.print_exc(file=sys.stdout) else: raise AssertionError(error_string_predict, Estimator) # transform if hasattr(estimator, "transform"): try: estimator.transform(X_train) except ValueError as e: if 'inf' not in repr(e) and 'NaN' not in repr(e): print(error_string_transform, Estimator, e) traceback.print_exc(file=sys.stdout) raise e except Exception as exc: print(error_string_transform, Estimator, exc) traceback.print_exc(file=sys.stdout) else: raise AssertionError(error_string_transform, Estimator) @ignore_warnings def check_estimators_pickle(name, Estimator): """Test that we can pickle all estimators""" check_methods = ["predict", "transform", "decision_function", "predict_proba"] X, y = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) # some estimators can't do features less than 0 X -= X.min() # some estimators only take multioutputs y = multioutput_estimator_convert_y_2d(name, y) estimator = Estimator() set_random_state(estimator) set_testing_parameters(estimator) estimator.fit(X, y) result = dict() for method in check_methods: if hasattr(estimator, method): result[method] = getattr(estimator, method)(X) # pickle and unpickle! pickled_estimator = pickle.dumps(estimator) if Estimator.__module__.startswith('sklearn.'): assert_true(b"version" in pickled_estimator) unpickled_estimator = pickle.loads(pickled_estimator) for method in result: unpickled_result = getattr(unpickled_estimator, method)(X) assert_array_almost_equal(result[method], unpickled_result) def check_estimators_partial_fit_n_features(name, Alg): # check if number of features changes between calls to partial_fit. if not hasattr(Alg, 'partial_fit'): return X, y = make_blobs(n_samples=50, random_state=1) X -= X.min() with ignore_warnings(category=DeprecationWarning): alg = Alg() if not hasattr(alg, 'partial_fit'): # check again as for mlp this depends on algorithm return set_testing_parameters(alg) try: if isinstance(alg, ClassifierMixin): classes = np.unique(y) alg.partial_fit(X, y, classes=classes) else: alg.partial_fit(X, y) except NotImplementedError: return assert_raises(ValueError, alg.partial_fit, X[:, :-1], y) def check_clustering(name, Alg): X, y = make_blobs(n_samples=50, random_state=1) X, y = shuffle(X, y, random_state=7) X = StandardScaler().fit_transform(X) n_samples, n_features = X.shape # catch deprecation and neighbors warnings with ignore_warnings(category=DeprecationWarning): alg = Alg() set_testing_parameters(alg) if hasattr(alg, "n_clusters"): alg.set_params(n_clusters=3) set_random_state(alg) if name == 'AffinityPropagation': alg.set_params(preference=-100) alg.set_params(max_iter=100) # fit alg.fit(X) # with lists alg.fit(X.tolist()) assert_equal(alg.labels_.shape, (n_samples,)) pred = alg.labels_ assert_greater(adjusted_rand_score(pred, y), 0.4) # fit another time with ``fit_predict`` and compare results if name == 'SpectralClustering': # there is no way to make Spectral clustering deterministic :( return set_random_state(alg) with warnings.catch_warnings(record=True): pred2 = alg.fit_predict(X) assert_array_equal(pred, pred2) def check_clusterer_compute_labels_predict(name, Clusterer): """Check that predict is invariant of compute_labels""" X, y = make_blobs(n_samples=20, random_state=0) clusterer = Clusterer() if hasattr(clusterer, "compute_labels"): # MiniBatchKMeans if hasattr(clusterer, "random_state"): clusterer.set_params(random_state=0) X_pred1 = clusterer.fit(X).predict(X) clusterer.set_params(compute_labels=False) X_pred2 = clusterer.fit(X).predict(X) assert_array_equal(X_pred1, X_pred2) def check_classifiers_one_label(name, Classifier): error_string_fit = "Classifier can't train when only one class is present." error_string_predict = ("Classifier can't predict when only one class is " "present.") rnd = np.random.RandomState(0) X_train = rnd.uniform(size=(10, 3)) X_test = rnd.uniform(size=(10, 3)) y = np.ones(10) # catch deprecation warnings with ignore_warnings(category=DeprecationWarning): classifier = Classifier() set_testing_parameters(classifier) # try to fit try: classifier.fit(X_train, y) except ValueError as e: if 'class' not in repr(e): print(error_string_fit, Classifier, e) traceback.print_exc(file=sys.stdout) raise e else: return except Exception as exc: print(error_string_fit, Classifier, exc) traceback.print_exc(file=sys.stdout) raise exc # predict try: assert_array_equal(classifier.predict(X_test), y) except Exception as exc: print(error_string_predict, Classifier, exc) raise exc @ignore_warnings # Warnings are raised by decision function def check_classifiers_train(name, Classifier): X_m, y_m = make_blobs(n_samples=300, random_state=0) X_m, y_m = shuffle(X_m, y_m, random_state=7) X_m = StandardScaler().fit_transform(X_m) # generate binary problem from multi-class one y_b = y_m[y_m != 2] X_b = X_m[y_m != 2] for (X, y) in [(X_m, y_m), (X_b, y_b)]: classes = np.unique(y) n_classes = len(classes) n_samples, n_features = X.shape classifier = Classifier() if name in ['BernoulliNB', 'MultinomialNB']: X -= X.min() set_testing_parameters(classifier) set_random_state(classifier) # raises error on malformed input for fit assert_raises(ValueError, classifier.fit, X, y[:-1]) # fit classifier.fit(X, y) # with lists classifier.fit(X.tolist(), y.tolist()) assert_true(hasattr(classifier, "classes_")) y_pred = classifier.predict(X) assert_equal(y_pred.shape, (n_samples,)) # training set performance if name not in ['BernoulliNB', 'MultinomialNB']: assert_greater(accuracy_score(y, y_pred), 0.83) # raises error on malformed input for predict assert_raises(ValueError, classifier.predict, X.T) if hasattr(classifier, "decision_function"): try: # decision_function agrees with predict decision = classifier.decision_function(X) if n_classes is 2: assert_equal(decision.shape, (n_samples,)) dec_pred = (decision.ravel() > 0).astype(np.int) assert_array_equal(dec_pred, y_pred) if (n_classes is 3 and not isinstance(classifier, BaseLibSVM)): # 1on1 of LibSVM works differently assert_equal(decision.shape, (n_samples, n_classes)) assert_array_equal(np.argmax(decision, axis=1), y_pred) # raises error on malformed input assert_raises(ValueError, classifier.decision_function, X.T) # raises error on malformed input for decision_function assert_raises(ValueError, classifier.decision_function, X.T) except NotImplementedError: pass if hasattr(classifier, "predict_proba"): # predict_proba agrees with predict y_prob = classifier.predict_proba(X) assert_equal(y_prob.shape, (n_samples, n_classes)) assert_array_equal(np.argmax(y_prob, axis=1), y_pred) # check that probas for all classes sum to one assert_array_almost_equal(np.sum(y_prob, axis=1), np.ones(n_samples)) # raises error on malformed input assert_raises(ValueError, classifier.predict_proba, X.T) # raises error on malformed input for predict_proba assert_raises(ValueError, classifier.predict_proba, X.T) if hasattr(classifier, "predict_log_proba"): # predict_log_proba is a transformation of predict_proba y_log_prob = classifier.predict_log_proba(X) assert_array_almost_equal(y_log_prob, np.log(y_prob), 8) assert_array_equal(np.argsort(y_log_prob), np.argsort(y_prob)) @ignore_warnings(category=DeprecationWarning) def check_estimators_fit_returns_self(name, Estimator): """Check if self is returned when calling fit""" X, y = make_blobs(random_state=0, n_samples=9, n_features=4) y = multioutput_estimator_convert_y_2d(name, y) # some want non-negative input X -= X.min() estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator) assert_true(estimator.fit(X, y) is estimator) @ignore_warnings def check_estimators_unfitted(name, Estimator): """Check that predict raises an exception in an unfitted estimator. Unfitted estimators should raise either AttributeError or ValueError. The specific exception type NotFittedError inherits from both and can therefore be adequately raised for that purpose. """ # Common test for Regressors as well as Classifiers X, y = _boston_subset() est = Estimator() msg = "fit" if hasattr(est, 'predict'): assert_raise_message((AttributeError, ValueError), msg, est.predict, X) if hasattr(est, 'decision_function'): assert_raise_message((AttributeError, ValueError), msg, est.decision_function, X) if hasattr(est, 'predict_proba'): assert_raise_message((AttributeError, ValueError), msg, est.predict_proba, X) if hasattr(est, 'predict_log_proba'): assert_raise_message((AttributeError, ValueError), msg, est.predict_log_proba, X) @ignore_warnings(category=DeprecationWarning) def check_supervised_y_2d(name, Estimator): if "MultiTask" in name: # These only work on 2d, so this test makes no sense return rnd = np.random.RandomState(0) X = rnd.uniform(size=(10, 3)) y = np.arange(10) % 3 estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator) # fit estimator.fit(X, y) y_pred = estimator.predict(X) set_random_state(estimator) # Check that when a 2D y is given, a DataConversionWarning is # raised with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", DataConversionWarning) warnings.simplefilter("ignore", RuntimeWarning) estimator.fit(X, y[:, np.newaxis]) y_pred_2d = estimator.predict(X) msg = "expected 1 DataConversionWarning, got: %s" % ( ", ".join([str(w_x) for w_x in w])) if name not in MULTI_OUTPUT: # check that we warned if we don't support multi-output assert_greater(len(w), 0, msg) assert_true("DataConversionWarning('A column-vector y" " was passed when a 1d array was expected" in msg) assert_array_almost_equal(y_pred.ravel(), y_pred_2d.ravel()) def check_classifiers_classes(name, Classifier): X, y = make_blobs(n_samples=30, random_state=0, cluster_std=0.1) X, y = shuffle(X, y, random_state=7) X = StandardScaler().fit_transform(X) # We need to make sure that we have non negative data, for things # like NMF X -= X.min() - .1 y_names = np.array(["one", "two", "three"])[y] for y_names in [y_names, y_names.astype('O')]: if name in ["LabelPropagation", "LabelSpreading"]: # TODO some complication with -1 label y_ = y else: y_ = y_names classes = np.unique(y_) with ignore_warnings(category=DeprecationWarning): classifier = Classifier() if name == 'BernoulliNB': classifier.set_params(binarize=X.mean()) set_testing_parameters(classifier) set_random_state(classifier) # fit classifier.fit(X, y_) y_pred = classifier.predict(X) # training set performance assert_array_equal(np.unique(y_), np.unique(y_pred)) if np.any(classifier.classes_ != classes): print("Unexpected classes_ attribute for %r: " "expected %s, got %s" % (classifier, classes, classifier.classes_)) @ignore_warnings(category=DeprecationWarning) def check_regressors_int(name, Regressor): X, _ = _boston_subset() X = X[:50] rnd = np.random.RandomState(0) y = rnd.randint(3, size=X.shape[0]) y = multioutput_estimator_convert_y_2d(name, y) rnd = np.random.RandomState(0) # separate estimators to control random seeds regressor_1 = Regressor() regressor_2 = Regressor() set_testing_parameters(regressor_1) set_testing_parameters(regressor_2) set_random_state(regressor_1) set_random_state(regressor_2) if name in CROSS_DECOMPOSITION: y_ = np.vstack([y, 2 * y + rnd.randint(2, size=len(y))]) y_ = y_.T else: y_ = y # fit regressor_1.fit(X, y_) pred1 = regressor_1.predict(X) regressor_2.fit(X, y_.astype(np.float)) pred2 = regressor_2.predict(X) assert_array_almost_equal(pred1, pred2, 2, name) @ignore_warnings(category=DeprecationWarning) def check_regressors_train(name, Regressor): X, y = _boston_subset() y = StandardScaler().fit_transform(y.reshape(-1, 1)) # X is already scaled y = y.ravel() y = multioutput_estimator_convert_y_2d(name, y) rnd = np.random.RandomState(0) # catch deprecation warnings regressor = Regressor() set_testing_parameters(regressor) if not hasattr(regressor, 'alphas') and hasattr(regressor, 'alpha'): # linear regressors need to set alpha, but not generalized CV ones regressor.alpha = 0.01 if name == 'PassiveAggressiveRegressor': regressor.C = 0.01 # raises error on malformed input for fit assert_raises(ValueError, regressor.fit, X, y[:-1]) # fit if name in CROSS_DECOMPOSITION: y_ = np.vstack([y, 2 * y + rnd.randint(2, size=len(y))]) y_ = y_.T else: y_ = y set_random_state(regressor) regressor.fit(X, y_) regressor.fit(X.tolist(), y_.tolist()) y_pred = regressor.predict(X) assert_equal(y_pred.shape, y_.shape) # TODO: find out why PLS and CCA fail. RANSAC is random # and furthermore assumes the presence of outliers, hence # skipped if name not in ('PLSCanonical', 'CCA', 'RANSACRegressor'): assert_greater(regressor.score(X, y_), 0.5) @ignore_warnings def check_regressors_no_decision_function(name, Regressor): # checks whether regressors have decision_function or predict_proba rng = np.random.RandomState(0) X = rng.normal(size=(10, 4)) y = multioutput_estimator_convert_y_2d(name, X[:, 0]) regressor = Regressor() set_testing_parameters(regressor) if hasattr(regressor, "n_components"): # FIXME CCA, PLS is not robust to rank 1 effects regressor.n_components = 1 regressor.fit(X, y) funcs = ["decision_function", "predict_proba", "predict_log_proba"] for func_name in funcs: func = getattr(regressor, func_name, None) if func is None: # doesn't have function continue # has function. Should raise deprecation warning msg = func_name assert_warns_message(DeprecationWarning, msg, func, X) def check_class_weight_classifiers(name, Classifier): if name == "NuSVC": # the sparse version has a parameter that doesn't do anything raise SkipTest if name.endswith("NB"): # NaiveBayes classifiers have a somewhat different interface. # FIXME SOON! raise SkipTest for n_centers in [2, 3]: # create a very noisy dataset X, y = make_blobs(centers=n_centers, random_state=0, cluster_std=20) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=0) n_centers = len(np.unique(y_train)) if n_centers == 2: class_weight = {0: 1000, 1: 0.0001} else: class_weight = {0: 1000, 1: 0.0001, 2: 0.0001} with ignore_warnings(category=DeprecationWarning): classifier = Classifier(class_weight=class_weight) if hasattr(classifier, "n_iter"): classifier.set_params(n_iter=100) if hasattr(classifier, "min_weight_fraction_leaf"): classifier.set_params(min_weight_fraction_leaf=0.01) set_random_state(classifier) classifier.fit(X_train, y_train) y_pred = classifier.predict(X_test) assert_greater(np.mean(y_pred == 0), 0.89) def check_class_weight_balanced_classifiers(name, Classifier, X_train, y_train, X_test, y_test, weights): with ignore_warnings(category=DeprecationWarning): classifier = Classifier() if hasattr(classifier, "n_iter"): classifier.set_params(n_iter=100) set_random_state(classifier) classifier.fit(X_train, y_train) y_pred = classifier.predict(X_test) classifier.set_params(class_weight='balanced') classifier.fit(X_train, y_train) y_pred_balanced = classifier.predict(X_test) assert_greater(f1_score(y_test, y_pred_balanced, average='weighted'), f1_score(y_test, y_pred, average='weighted')) def check_class_weight_balanced_linear_classifier(name, Classifier): """Test class weights with non-contiguous class labels.""" X = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], [1.0, 1.0], [1.0, 0.0]]) y = np.array([1, 1, 1, -1, -1]) with ignore_warnings(category=DeprecationWarning): classifier = Classifier() if hasattr(classifier, "n_iter"): # This is a very small dataset, default n_iter are likely to prevent # convergence classifier.set_params(n_iter=1000) set_random_state(classifier) # Let the model compute the class frequencies classifier.set_params(class_weight='balanced') coef_balanced = classifier.fit(X, y).coef_.copy() # Count each label occurrence to reweight manually n_samples = len(y) n_classes = float(len(np.unique(y))) class_weight = {1: n_samples / (np.sum(y == 1) * n_classes), -1: n_samples / (np.sum(y == -1) * n_classes)} classifier.set_params(class_weight=class_weight) coef_manual = classifier.fit(X, y).coef_.copy() assert_array_almost_equal(coef_balanced, coef_manual) @ignore_warnings(category=DeprecationWarning) def check_estimators_overwrite_params(name, Estimator): X, y = make_blobs(random_state=0, n_samples=9) y = multioutput_estimator_convert_y_2d(name, y) # some want non-negative input X -= X.min() estimator = Estimator() set_testing_parameters(estimator) set_random_state(estimator) # Make a physical copy of the original estimator parameters before fitting. params = estimator.get_params() original_params = deepcopy(params) # Fit the model estimator.fit(X, y) # Compare the state of the model parameters with the original parameters new_params = estimator.get_params() for param_name, original_value in original_params.items(): new_value = new_params[param_name] # We should never change or mutate the internal state of input # parameters by default. To check this we use the joblib.hash function # that introspects recursively any subobjects to compute a checksum. # The only exception to this rule of immutable constructor parameters # is possible RandomState instance but in this check we explicitly # fixed the random_state params recursively to be integer seeds. assert_equal(hash(new_value), hash(original_value), "Estimator %s should not change or mutate " " the parameter %s from %s to %s during fit." % (name, param_name, original_value, new_value)) def check_no_fit_attributes_set_in_init(name, Estimator): """Check that Estimator.__init__ doesn't set trailing-_ attributes.""" estimator = Estimator() for attr in dir(estimator): if attr.endswith("_") and not attr.startswith("__"): # This check is for properties, they can be listed in dir # while at the same time have hasattr return False as long # as the property getter raises an AttributeError assert_false( hasattr(estimator, attr), "By convention, attributes ending with '_' are " 'estimated from data in scikit-learn. Consequently they ' 'should not be initialized in the constructor of an ' 'estimator but in the fit method. Attribute {!r} ' 'was found in estimator {}'.format(attr, name)) def check_sparsify_coefficients(name, Estimator): X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1], [-1, -2], [2, 2], [-2, -2]]) y = [1, 1, 1, 2, 2, 2, 3, 3, 3] est = Estimator() est.fit(X, y) pred_orig = est.predict(X) # test sparsify with dense inputs est.sparsify() assert_true(sparse.issparse(est.coef_)) pred = est.predict(X) assert_array_equal(pred, pred_orig) # pickle and unpickle with sparse coef_ est = pickle.loads(pickle.dumps(est)) assert_true(sparse.issparse(est.coef_)) pred = est.predict(X) assert_array_equal(pred, pred_orig) def check_classifier_data_not_an_array(name, Estimator): X = np.array([[3, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 1]]) y = [1, 1, 1, 2, 2, 2] y = multioutput_estimator_convert_y_2d(name, y) check_estimators_data_not_an_array(name, Estimator, X, y) def check_regressor_data_not_an_array(name, Estimator): X, y = _boston_subset(n_samples=50) y = multioutput_estimator_convert_y_2d(name, y) check_estimators_data_not_an_array(name, Estimator, X, y) @ignore_warnings(category=DeprecationWarning) def check_estimators_data_not_an_array(name, Estimator, X, y): if name in CROSS_DECOMPOSITION: raise SkipTest # separate estimators to control random seeds estimator_1 = Estimator() estimator_2 = Estimator() set_testing_parameters(estimator_1) set_testing_parameters(estimator_2) set_random_state(estimator_1) set_random_state(estimator_2) y_ = NotAnArray(np.asarray(y)) X_ = NotAnArray(np.asarray(X)) # fit estimator_1.fit(X_, y_) pred1 = estimator_1.predict(X_) estimator_2.fit(X, y) pred2 = estimator_2.predict(X) assert_array_almost_equal(pred1, pred2, 2, name) def check_parameters_default_constructible(name, Estimator): classifier = LinearDiscriminantAnalysis() # test default-constructibility # get rid of deprecation warnings with ignore_warnings(category=DeprecationWarning): if name in META_ESTIMATORS: estimator = Estimator(classifier) else: estimator = Estimator() # test cloning clone(estimator) # test __repr__ repr(estimator) # test that set_params returns self assert_true(estimator.set_params() is estimator) # test if init does nothing but set parameters # this is important for grid_search etc. # We get the default parameters from init and then # compare these against the actual values of the attributes. # this comes from getattr. Gets rid of deprecation decorator. init = getattr(estimator.__init__, 'deprecated_original', estimator.__init__) try: def param_filter(p): """Identify hyper parameters of an estimator""" return (p.name != 'self' and p.kind != p.VAR_KEYWORD and p.kind != p.VAR_POSITIONAL) init_params = [p for p in signature(init).parameters.values() if param_filter(p)] except (TypeError, ValueError): # init is not a python function. # true for mixins return params = estimator.get_params() if name in META_ESTIMATORS: # they can need a non-default argument init_params = init_params[1:] for init_param in init_params: assert_not_equal(init_param.default, init_param.empty, "parameter %s for %s has no default value" % (init_param.name, type(estimator).__name__)) assert_in(type(init_param.default), [str, int, float, bool, tuple, type(None), np.float64, types.FunctionType, Memory]) if init_param.name not in params.keys(): # deprecated parameter, not in get_params assert_true(init_param.default is None) continue param_value = params[init_param.name] if isinstance(param_value, np.ndarray): assert_array_equal(param_value, init_param.default) else: assert_equal(param_value, init_param.default) def multioutput_estimator_convert_y_2d(name, y): # Estimators in mono_output_task_error raise ValueError if y is of 1-D # Convert into a 2-D y for those estimators. if "MultiTask" in name: return np.reshape(y, (-1, 1)) return y @ignore_warnings(category=DeprecationWarning) def check_non_transformer_estimators_n_iter(name, Estimator): # Test that estimators that are not transformers with a parameter # max_iter, return the attribute of n_iter_ at least 1. # These models are dependent on external solvers like # libsvm and accessing the iter parameter is non-trivial. not_run_check_n_iter = ['Ridge', 'SVR', 'NuSVR', 'NuSVC', 'RidgeClassifier', 'SVC', 'RandomizedLasso', 'LogisticRegressionCV', 'LinearSVC', 'LogisticRegression'] # Tested in test_transformer_n_iter not_run_check_n_iter += CROSS_DECOMPOSITION if name in not_run_check_n_iter: return # LassoLars stops early for the default alpha=1.0 the iris dataset. if name == 'LassoLars': estimator = Estimator(alpha=0.) else: estimator = Estimator() if hasattr(estimator, 'max_iter'): iris = load_iris() X, y_ = iris.data, iris.target y_ = multioutput_estimator_convert_y_2d(name, y_) set_random_state(estimator, 0) if name == 'AffinityPropagation': estimator.fit(X) else: estimator.fit(X, y_) # HuberRegressor depends on scipy.optimize.fmin_l_bfgs_b # which doesn't return a n_iter for old versions of SciPy. if not (name == 'HuberRegressor' and estimator.n_iter_ is None): assert_greater_equal(estimator.n_iter_, 1) @ignore_warnings(category=DeprecationWarning) def check_transformer_n_iter(name, Estimator): # Test that transformers with a parameter max_iter, return the # attribute of n_iter_ at least 1. estimator = Estimator() if hasattr(estimator, "max_iter"): if name in CROSS_DECOMPOSITION: # Check using default data X = [[0., 0., 1.], [1., 0., 0.], [2., 2., 2.], [2., 5., 4.]] y_ = [[0.1, -0.2], [0.9, 1.1], [0.1, -0.5], [0.3, -0.2]] else: X, y_ = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) X -= X.min() - 0.1 set_random_state(estimator, 0) estimator.fit(X, y_) # These return a n_iter per component. if name in CROSS_DECOMPOSITION: for iter_ in estimator.n_iter_: assert_greater_equal(iter_, 1) else: assert_greater_equal(estimator.n_iter_, 1) @ignore_warnings(category=DeprecationWarning) def check_get_params_invariance(name, estimator): # Checks if get_params(deep=False) is a subset of get_params(deep=True) class T(BaseEstimator): """Mock classifier """ def __init__(self): pass def fit(self, X, y): return self def transform(self, X): return X if name in ('FeatureUnion', 'Pipeline'): e = estimator([('clf', T())]) elif name in ('GridSearchCV', 'RandomizedSearchCV', 'SelectFromModel'): return else: e = estimator() shallow_params = e.get_params(deep=False) deep_params = e.get_params(deep=True) assert_true(all(item in deep_params.items() for item in shallow_params.items())) def check_classifiers_regression_target(name, Estimator): # Check if classifier throws an exception when fed regression targets boston = load_boston() X, y = boston.data, boston.target e = Estimator() msg = 'Unknown label type: ' assert_raises_regex(ValueError, msg, e.fit, X, y) @ignore_warnings(category=DeprecationWarning) def check_decision_proba_consistency(name, Estimator): # Check whether an estimator having both decision_function and # predict_proba methods has outputs with perfect rank correlation. centers = [(2, 2), (4, 4)] X, y = make_blobs(n_samples=100, random_state=0, n_features=4, centers=centers, cluster_std=1.0, shuffle=True) X_test = np.random.randn(20, 2) + 4 estimator = Estimator() set_testing_parameters(estimator) if (hasattr(estimator, "decision_function") and hasattr(estimator, "predict_proba")): estimator.fit(X, y) a = estimator.predict_proba(X_test)[:, 1] b = estimator.decision_function(X_test) assert_array_equal(rankdata(a), rankdata(b))
bsd-3-clause
SiLab-Bonn/Scarce
scarce/examples/cce_3D.py
1
3333
''' Example that calculates the collected charge. The collected charge is calculated as a function of the position in the sensor. The drift field takes irradiation into account. ''' import matplotlib.pyplot as plt from matplotlib import cm from scarce import tools from scarce.examples import cc_3D if __name__ == '__main__': import logging logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S") n_eff_0 = 4.6e11 n_pixel_x, n_pixel_y = 3, 3 width_x = 250. width_y = 50. radius = 6. nD = 2 # Number of columns per pixels resolution = 80 smoothing = 0.1 temperature = 300 V_readout = 0 (edge_x, edge_y, charge), pot_descr = cc_3D.cc(n_eff=n_eff_0, bias=-20, V_readout=V_readout, temperature=temperature, t_e_trapping=None, t_h_trapping=None, n_pixel_x=n_pixel_x, n_pixel_y=n_pixel_y, width_x=width_x, width_y=width_y, radius=radius, nD=nD, resolution=resolution, smoothing=smoothing) tools.save(obj=(edge_x, edge_y, charge), filename='charge_3D_unirrad') (edge_x, edge_y, charge), pot_descr = cc_3D.cc(n_eff=9.1e12, bias=-180, V_readout=V_readout, temperature=temperature, t_e_trapping=2.6, t_h_trapping=2.6, n_pixel_x=n_pixel_x, n_pixel_y=n_pixel_y, width_x=width_x, width_y=width_y, radius=radius, nD=nD, resolution=resolution, smoothing=smoothing) tools.save(obj=(edge_x, edge_y, charge), filename='charge_3D_irrad') # Plot collected charge map _, _, charge_u = tools.load(filename='charge_3D_unirrad') plt.clf() plt.gca().set_aspect('equal') plt.gca().invert_yaxis() cmap = cm.get_cmap('inferno') cmap.set_bad('white') cmesh = plt.pcolormesh(edge_x, edge_y, charge/charge_u, cmap=cmap, vmin=0, vmax=1.05) plt.title('Charge collection') plt.grid() cax = plt.gcf().add_axes( [plt.gca().get_position().xmax, 0.1, 0.05, plt.gca().get_position().ymax - plt.gca().get_position().ymin]) plt.colorbar(cmesh, cax=cax, orientation='vertical') plt.grid() plt.savefig('CCE_3D.pdf', layout='tight') plt.show()
mit
shikhardb/scikit-learn
sklearn/metrics/tests/test_pairwise.py
16
22326
import numpy as np from numpy import linalg from scipy.sparse import dok_matrix, csr_matrix, issparse from scipy.spatial.distance import cosine, cityblock, minkowski, wminkowski from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_true from sklearn.externals.six import iteritems from sklearn.metrics.pairwise import euclidean_distances from sklearn.metrics.pairwise import manhattan_distances from sklearn.metrics.pairwise import linear_kernel from sklearn.metrics.pairwise import chi2_kernel, additive_chi2_kernel from sklearn.metrics.pairwise import polynomial_kernel from sklearn.metrics.pairwise import rbf_kernel from sklearn.metrics.pairwise import sigmoid_kernel from sklearn.metrics.pairwise import cosine_similarity from sklearn.metrics.pairwise import cosine_distances from sklearn.metrics.pairwise import pairwise_distances from sklearn.metrics.pairwise import pairwise_distances_argmin_min from sklearn.metrics.pairwise import pairwise_distances_argmin from sklearn.metrics.pairwise import pairwise_kernels from sklearn.metrics.pairwise import PAIRWISE_KERNEL_FUNCTIONS from sklearn.metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS from sklearn.metrics.pairwise import PAIRED_DISTANCES from sklearn.metrics.pairwise import check_pairwise_arrays from sklearn.metrics.pairwise import check_paired_arrays from sklearn.metrics.pairwise import _parallel_pairwise from sklearn.metrics.pairwise import paired_distances from sklearn.metrics.pairwise import paired_euclidean_distances from sklearn.metrics.pairwise import paired_manhattan_distances from sklearn.preprocessing import normalize def test_pairwise_distances(): # Test the pairwise_distance helper function. rng = np.random.RandomState(0) # Euclidean distance should be equivalent to calling the function. X = rng.random_sample((5, 4)) S = pairwise_distances(X, metric="euclidean") S2 = euclidean_distances(X) assert_array_almost_equal(S, S2) # Euclidean distance, with Y != X. Y = rng.random_sample((2, 4)) S = pairwise_distances(X, Y, metric="euclidean") S2 = euclidean_distances(X, Y) assert_array_almost_equal(S, S2) # Test with tuples as X and Y X_tuples = tuple([tuple([v for v in row]) for row in X]) Y_tuples = tuple([tuple([v for v in row]) for row in Y]) S2 = pairwise_distances(X_tuples, Y_tuples, metric="euclidean") assert_array_almost_equal(S, S2) # "cityblock" uses sklearn metric, cityblock (function) is scipy.spatial. S = pairwise_distances(X, metric="cityblock") S2 = pairwise_distances(X, metric=cityblock) assert_equal(S.shape[0], S.shape[1]) assert_equal(S.shape[0], X.shape[0]) assert_array_almost_equal(S, S2) # The manhattan metric should be equivalent to cityblock. S = pairwise_distances(X, Y, metric="manhattan") S2 = pairwise_distances(X, Y, metric=cityblock) assert_equal(S.shape[0], X.shape[0]) assert_equal(S.shape[1], Y.shape[0]) assert_array_almost_equal(S, S2) # Low-level function for manhattan can divide in blocks to avoid # using too much memory during the broadcasting S3 = manhattan_distances(X, Y, size_threshold=10) assert_array_almost_equal(S, S3) # Test cosine as a string metric versus cosine callable # "cosine" uses sklearn metric, cosine (function) is scipy.spatial S = pairwise_distances(X, Y, metric="cosine") S2 = pairwise_distances(X, Y, metric=cosine) assert_equal(S.shape[0], X.shape[0]) assert_equal(S.shape[1], Y.shape[0]) assert_array_almost_equal(S, S2) # Tests that precomputed metric returns pointer to, and not copy of, X. S = np.dot(X, X.T) S2 = pairwise_distances(S, metric="precomputed") assert_true(S is S2) # Test with sparse X and Y, # currently only supported for Euclidean, L1 and cosine. X_sparse = csr_matrix(X) Y_sparse = csr_matrix(Y) S = pairwise_distances(X_sparse, Y_sparse, metric="euclidean") S2 = euclidean_distances(X_sparse, Y_sparse) assert_array_almost_equal(S, S2) S = pairwise_distances(X_sparse, Y_sparse, metric="cosine") S2 = cosine_distances(X_sparse, Y_sparse) assert_array_almost_equal(S, S2) S = pairwise_distances(X_sparse, Y_sparse.tocsc(), metric="manhattan") S2 = manhattan_distances(X_sparse.tobsr(), Y_sparse.tocoo()) assert_array_almost_equal(S, S2) S2 = manhattan_distances(X, Y) assert_array_almost_equal(S, S2) # Test with scipy.spatial.distance metric, with a kwd kwds = {"p": 2.0} S = pairwise_distances(X, Y, metric="minkowski", **kwds) S2 = pairwise_distances(X, Y, metric=minkowski, **kwds) assert_array_almost_equal(S, S2) # same with Y = None kwds = {"p": 2.0} S = pairwise_distances(X, metric="minkowski", **kwds) S2 = pairwise_distances(X, metric=minkowski, **kwds) assert_array_almost_equal(S, S2) # Test that scipy distance metrics throw an error if sparse matrix given assert_raises(TypeError, pairwise_distances, X_sparse, metric="minkowski") assert_raises(TypeError, pairwise_distances, X, Y_sparse, metric="minkowski") # Test that a value error is raised if the metric is unkown assert_raises(ValueError, pairwise_distances, X, Y, metric="blah") def check_pairwise_parallel(func, metric, kwds): rng = np.random.RandomState(0) for make_data in (np.array, csr_matrix): X = make_data(rng.random_sample((5, 4))) Y = make_data(rng.random_sample((3, 4))) try: S = func(X, metric=metric, n_jobs=1, **kwds) except (TypeError, ValueError) as exc: # Not all metrics support sparse input # ValueError may be triggered by bad callable if make_data is csr_matrix: assert_raises(type(exc), func, X, metric=metric, n_jobs=2, **kwds) continue else: raise S2 = func(X, metric=metric, n_jobs=2, **kwds) assert_array_almost_equal(S, S2) S = func(X, Y, metric=metric, n_jobs=1, **kwds) S2 = func(X, Y, metric=metric, n_jobs=2, **kwds) assert_array_almost_equal(S, S2) def test_pairwise_parallel(): wminkowski_kwds = {'w': np.arange(1, 5).astype('double'), 'p': 1} metrics = [(pairwise_distances, 'euclidean', {}), (pairwise_distances, wminkowski, wminkowski_kwds), (pairwise_distances, 'wminkowski', wminkowski_kwds), (pairwise_kernels, 'polynomial', {'degree': 1}), (pairwise_kernels, callable_rbf_kernel, {'gamma': .1}), ] for func, metric, kwds in metrics: yield check_pairwise_parallel, func, metric, kwds def test_pairwise_callable_nonstrict_metric(): # paired_distances should allow callable metric where metric(x, x) != 0 # Knowing that the callable is a strict metric would allow the diagonal to # be left uncalculated and set to 0. assert_equal(pairwise_distances([[1]], metric=lambda x, y: 5)[0, 0], 5) def callable_rbf_kernel(x, y, **kwds): # Callable version of pairwise.rbf_kernel. K = rbf_kernel(np.atleast_2d(x), np.atleast_2d(y), **kwds) return K def test_pairwise_kernels(): # Test the pairwise_kernels helper function. rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) Y = rng.random_sample((2, 4)) # Test with all metrics that should be in PAIRWISE_KERNEL_FUNCTIONS. test_metrics = ["rbf", "sigmoid", "polynomial", "linear", "chi2", "additive_chi2"] for metric in test_metrics: function = PAIRWISE_KERNEL_FUNCTIONS[metric] # Test with Y=None K1 = pairwise_kernels(X, metric=metric) K2 = function(X) assert_array_almost_equal(K1, K2) # Test with Y=Y K1 = pairwise_kernels(X, Y=Y, metric=metric) K2 = function(X, Y=Y) assert_array_almost_equal(K1, K2) # Test with tuples as X and Y X_tuples = tuple([tuple([v for v in row]) for row in X]) Y_tuples = tuple([tuple([v for v in row]) for row in Y]) K2 = pairwise_kernels(X_tuples, Y_tuples, metric=metric) assert_array_almost_equal(K1, K2) # Test with sparse X and Y X_sparse = csr_matrix(X) Y_sparse = csr_matrix(Y) if metric in ["chi2", "additive_chi2"]: # these don't support sparse matrices yet assert_raises(ValueError, pairwise_kernels, X_sparse, Y=Y_sparse, metric=metric) continue K1 = pairwise_kernels(X_sparse, Y=Y_sparse, metric=metric) assert_array_almost_equal(K1, K2) # Test with a callable function, with given keywords. metric = callable_rbf_kernel kwds = {} kwds['gamma'] = 0.1 K1 = pairwise_kernels(X, Y=Y, metric=metric, **kwds) K2 = rbf_kernel(X, Y=Y, **kwds) assert_array_almost_equal(K1, K2) # callable function, X=Y K1 = pairwise_kernels(X, Y=X, metric=metric, **kwds) K2 = rbf_kernel(X, Y=X, **kwds) assert_array_almost_equal(K1, K2) def test_pairwise_kernels_filter_param(): rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) Y = rng.random_sample((2, 4)) K = rbf_kernel(X, Y, gamma=0.1) params = {"gamma": 0.1, "blabla": ":)"} K2 = pairwise_kernels(X, Y, metric="rbf", filter_params=True, **params) assert_array_almost_equal(K, K2) assert_raises(TypeError, pairwise_kernels, X, Y, "rbf", **params) def test_paired_distances(): # Test the pairwise_distance helper function. rng = np.random.RandomState(0) # Euclidean distance should be equivalent to calling the function. X = rng.random_sample((5, 4)) # Euclidean distance, with Y != X. Y = rng.random_sample((5, 4)) for metric, func in iteritems(PAIRED_DISTANCES): S = paired_distances(X, Y, metric=metric) S2 = func(X, Y) assert_array_almost_equal(S, S2) S3 = func(csr_matrix(X), csr_matrix(Y)) assert_array_almost_equal(S, S3) if metric in PAIRWISE_DISTANCE_FUNCTIONS: # Check the the pairwise_distances implementation # gives the same value distances = PAIRWISE_DISTANCE_FUNCTIONS[metric](X, Y) distances = np.diag(distances) assert_array_almost_equal(distances, S) # Check the callable implementation S = paired_distances(X, Y, metric='manhattan') S2 = paired_distances(X, Y, metric=lambda x, y: np.abs(x - y).sum(axis=0)) assert_array_almost_equal(S, S2) # Test that a value error is raised when the lengths of X and Y should not # differ Y = rng.random_sample((3, 4)) assert_raises(ValueError, paired_distances, X, Y) def test_pairwise_distances_argmin_min(): # Check pairwise minimum distances computation for any metric X = [[0], [1]] Y = [[-1], [2]] Xsp = dok_matrix(X) Ysp = csr_matrix(Y, dtype=np.float32) # euclidean metric D, E = pairwise_distances_argmin_min(X, Y, metric="euclidean") D2 = pairwise_distances_argmin(X, Y, metric="euclidean") assert_array_almost_equal(D, [0, 1]) assert_array_almost_equal(D2, [0, 1]) assert_array_almost_equal(D, [0, 1]) assert_array_almost_equal(E, [1., 1.]) # sparse matrix case Dsp, Esp = pairwise_distances_argmin_min(Xsp, Ysp, metric="euclidean") assert_array_equal(Dsp, D) assert_array_equal(Esp, E) # We don't want np.matrix here assert_equal(type(Dsp), np.ndarray) assert_equal(type(Esp), np.ndarray) # Non-euclidean sklearn metric D, E = pairwise_distances_argmin_min(X, Y, metric="manhattan") D2 = pairwise_distances_argmin(X, Y, metric="manhattan") assert_array_almost_equal(D, [0, 1]) assert_array_almost_equal(D2, [0, 1]) assert_array_almost_equal(E, [1., 1.]) D, E = pairwise_distances_argmin_min(Xsp, Ysp, metric="manhattan") D2 = pairwise_distances_argmin(Xsp, Ysp, metric="manhattan") assert_array_almost_equal(D, [0, 1]) assert_array_almost_equal(E, [1., 1.]) # Non-euclidean Scipy distance (callable) D, E = pairwise_distances_argmin_min(X, Y, metric=minkowski, metric_kwargs={"p": 2}) assert_array_almost_equal(D, [0, 1]) assert_array_almost_equal(E, [1., 1.]) # Non-euclidean Scipy distance (string) D, E = pairwise_distances_argmin_min(X, Y, metric="minkowski", metric_kwargs={"p": 2}) assert_array_almost_equal(D, [0, 1]) assert_array_almost_equal(E, [1., 1.]) # Compare with naive implementation rng = np.random.RandomState(0) X = rng.randn(97, 149) Y = rng.randn(111, 149) dist = pairwise_distances(X, Y, metric="manhattan") dist_orig_ind = dist.argmin(axis=0) dist_orig_val = dist[dist_orig_ind, range(len(dist_orig_ind))] dist_chunked_ind, dist_chunked_val = pairwise_distances_argmin_min( X, Y, axis=0, metric="manhattan", batch_size=50) np.testing.assert_almost_equal(dist_orig_ind, dist_chunked_ind, decimal=7) np.testing.assert_almost_equal(dist_orig_val, dist_chunked_val, decimal=7) def test_euclidean_distances(): # Check the pairwise Euclidean distances computation X = [[0]] Y = [[1], [2]] D = euclidean_distances(X, Y) assert_array_almost_equal(D, [[1., 2.]]) X = csr_matrix(X) Y = csr_matrix(Y) D = euclidean_distances(X, Y) assert_array_almost_equal(D, [[1., 2.]]) # Paired distances def test_paired_euclidean_distances(): # Check the paired Euclidean distances computation X = [[0], [0]] Y = [[1], [2]] D = paired_euclidean_distances(X, Y) assert_array_almost_equal(D, [1., 2.]) def test_paired_manhattan_distances(): # Check the paired manhattan distances computation X = [[0], [0]] Y = [[1], [2]] D = paired_manhattan_distances(X, Y) assert_array_almost_equal(D, [1., 2.]) def test_chi_square_kernel(): rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) Y = rng.random_sample((10, 4)) K_add = additive_chi2_kernel(X, Y) gamma = 0.1 K = chi2_kernel(X, Y, gamma=gamma) assert_equal(K.dtype, np.float) for i, x in enumerate(X): for j, y in enumerate(Y): chi2 = -np.sum((x - y) ** 2 / (x + y)) chi2_exp = np.exp(gamma * chi2) assert_almost_equal(K_add[i, j], chi2) assert_almost_equal(K[i, j], chi2_exp) # check diagonal is ones for data with itself K = chi2_kernel(Y) assert_array_equal(np.diag(K), 1) # check off-diagonal is < 1 but > 0: assert_true(np.all(K > 0)) assert_true(np.all(K - np.diag(np.diag(K)) < 1)) # check that float32 is preserved X = rng.random_sample((5, 4)).astype(np.float32) Y = rng.random_sample((10, 4)).astype(np.float32) K = chi2_kernel(X, Y) assert_equal(K.dtype, np.float32) # check integer type gets converted, # check that zeros are handled X = rng.random_sample((10, 4)).astype(np.int32) K = chi2_kernel(X, X) assert_true(np.isfinite(K).all()) assert_equal(K.dtype, np.float) # check that kernel of similar things is greater than dissimilar ones X = [[.3, .7], [1., 0]] Y = [[0, 1], [.9, .1]] K = chi2_kernel(X, Y) assert_greater(K[0, 0], K[0, 1]) assert_greater(K[1, 1], K[1, 0]) # test negative input assert_raises(ValueError, chi2_kernel, [[0, -1]]) assert_raises(ValueError, chi2_kernel, [[0, -1]], [[-1, -1]]) assert_raises(ValueError, chi2_kernel, [[0, 1]], [[-1, -1]]) # different n_features in X and Y assert_raises(ValueError, chi2_kernel, [[0, 1]], [[.2, .2, .6]]) # sparse matrices assert_raises(ValueError, chi2_kernel, csr_matrix(X), csr_matrix(Y)) assert_raises(ValueError, additive_chi2_kernel, csr_matrix(X), csr_matrix(Y)) def test_kernel_symmetry(): # Valid kernels should be symmetric rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) for kernel in (linear_kernel, polynomial_kernel, rbf_kernel, sigmoid_kernel, cosine_similarity): K = kernel(X, X) assert_array_almost_equal(K, K.T, 15) def test_kernel_sparse(): rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) X_sparse = csr_matrix(X) for kernel in (linear_kernel, polynomial_kernel, rbf_kernel, sigmoid_kernel, cosine_similarity): K = kernel(X, X) K2 = kernel(X_sparse, X_sparse) assert_array_almost_equal(K, K2) def test_linear_kernel(): rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) K = linear_kernel(X, X) # the diagonal elements of a linear kernel are their squared norm assert_array_almost_equal(K.flat[::6], [linalg.norm(x) ** 2 for x in X]) def test_rbf_kernel(): rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) K = rbf_kernel(X, X) # the diagonal elements of a rbf kernel are 1 assert_array_almost_equal(K.flat[::6], np.ones(5)) def test_cosine_similarity(): # Test the cosine_similarity. rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) Y = rng.random_sample((3, 4)) Xcsr = csr_matrix(X) Ycsr = csr_matrix(Y) for X_, Y_ in ((X, None), (X, Y), (Xcsr, None), (Xcsr, Ycsr)): # Test that the cosine is kernel is equal to a linear kernel when data # has been previously normalized by L2-norm. K1 = pairwise_kernels(X_, Y=Y_, metric="cosine") X_ = normalize(X_) if Y_ is not None: Y_ = normalize(Y_) K2 = pairwise_kernels(X_, Y=Y_, metric="linear") assert_array_almost_equal(K1, K2) def test_check_dense_matrices(): # Ensure that pairwise array check works for dense matrices. # Check that if XB is None, XB is returned as reference to XA XA = np.resize(np.arange(40), (5, 8)) XA_checked, XB_checked = check_pairwise_arrays(XA, None) assert_true(XA_checked is XB_checked) assert_array_equal(XA, XA_checked) def test_check_XB_returned(): # Ensure that if XA and XB are given correctly, they return as equal. # Check that if XB is not None, it is returned equal. # Note that the second dimension of XB is the same as XA. XA = np.resize(np.arange(40), (5, 8)) XB = np.resize(np.arange(32), (4, 8)) XA_checked, XB_checked = check_pairwise_arrays(XA, XB) assert_array_equal(XA, XA_checked) assert_array_equal(XB, XB_checked) XB = np.resize(np.arange(40), (5, 8)) XA_checked, XB_checked = check_paired_arrays(XA, XB) assert_array_equal(XA, XA_checked) assert_array_equal(XB, XB_checked) def test_check_different_dimensions(): # Ensure an error is raised if the dimensions are different. XA = np.resize(np.arange(45), (5, 9)) XB = np.resize(np.arange(32), (4, 8)) assert_raises(ValueError, check_pairwise_arrays, XA, XB) XB = np.resize(np.arange(4 * 9), (4, 9)) assert_raises(ValueError, check_paired_arrays, XA, XB) def test_check_invalid_dimensions(): # Ensure an error is raised on 1D input arrays. XA = np.arange(45) XB = np.resize(np.arange(32), (4, 8)) assert_raises(ValueError, check_pairwise_arrays, XA, XB) XA = np.resize(np.arange(45), (5, 9)) XB = np.arange(32) assert_raises(ValueError, check_pairwise_arrays, XA, XB) def test_check_sparse_arrays(): # Ensures that checks return valid sparse matrices. rng = np.random.RandomState(0) XA = rng.random_sample((5, 4)) XA_sparse = csr_matrix(XA) XB = rng.random_sample((5, 4)) XB_sparse = csr_matrix(XB) XA_checked, XB_checked = check_pairwise_arrays(XA_sparse, XB_sparse) # compare their difference because testing csr matrices for # equality with '==' does not work as expected. assert_true(issparse(XA_checked)) assert_equal(abs(XA_sparse - XA_checked).sum(), 0) assert_true(issparse(XB_checked)) assert_equal(abs(XB_sparse - XB_checked).sum(), 0) XA_checked, XA_2_checked = check_pairwise_arrays(XA_sparse, XA_sparse) assert_true(issparse(XA_checked)) assert_equal(abs(XA_sparse - XA_checked).sum(), 0) assert_true(issparse(XA_2_checked)) assert_equal(abs(XA_2_checked - XA_checked).sum(), 0) def tuplify(X): # Turns a numpy matrix (any n-dimensional array) into tuples. s = X.shape if len(s) > 1: # Tuplify each sub-array in the input. return tuple(tuplify(row) for row in X) else: # Single dimension input, just return tuple of contents. return tuple(r for r in X) def test_check_tuple_input(): # Ensures that checks return valid tuples. rng = np.random.RandomState(0) XA = rng.random_sample((5, 4)) XA_tuples = tuplify(XA) XB = rng.random_sample((5, 4)) XB_tuples = tuplify(XB) XA_checked, XB_checked = check_pairwise_arrays(XA_tuples, XB_tuples) assert_array_equal(XA_tuples, XA_checked) assert_array_equal(XB_tuples, XB_checked) def test_check_preserve_type(): # Ensures that type float32 is preserved. XA = np.resize(np.arange(40), (5, 8)).astype(np.float32) XB = np.resize(np.arange(40), (5, 8)).astype(np.float32) XA_checked, XB_checked = check_pairwise_arrays(XA, None) assert_equal(XA_checked.dtype, np.float32) # both float32 XA_checked, XB_checked = check_pairwise_arrays(XA, XB) assert_equal(XA_checked.dtype, np.float32) assert_equal(XB_checked.dtype, np.float32) # mismatched A XA_checked, XB_checked = check_pairwise_arrays(XA.astype(np.float), XB) assert_equal(XA_checked.dtype, np.float) assert_equal(XB_checked.dtype, np.float) # mismatched B XA_checked, XB_checked = check_pairwise_arrays(XA, XB.astype(np.float)) assert_equal(XA_checked.dtype, np.float) assert_equal(XB_checked.dtype, np.float)
bsd-3-clause
jreback/pandas
pandas/tests/plotting/test_misc.py
2
20162
""" Test cases for misc plot functions """ import numpy as np import pytest import pandas.util._test_decorators as td from pandas import DataFrame, Series import pandas._testing as tm from pandas.tests.plotting.common import TestPlotBase, _check_plot_works import pandas.plotting as plotting pytestmark = pytest.mark.slow @td.skip_if_mpl def test_import_error_message(): # GH-19810 df = DataFrame({"A": [1, 2]}) with pytest.raises(ImportError, match="matplotlib is required for plotting"): df.plot() def test_get_accessor_args(): func = plotting._core.PlotAccessor._get_call_args msg = "Called plot accessor for type list, expected Series or DataFrame" with pytest.raises(TypeError, match=msg): func(backend_name="", data=[], args=[], kwargs={}) msg = "should not be called with positional arguments" with pytest.raises(TypeError, match=msg): func(backend_name="", data=Series(dtype=object), args=["line", None], kwargs={}) x, y, kind, kwargs = func( backend_name="", data=DataFrame(), args=["x"], kwargs={"y": "y", "kind": "bar", "grid": False}, ) assert x == "x" assert y == "y" assert kind == "bar" assert kwargs == {"grid": False} x, y, kind, kwargs = func( backend_name="pandas.plotting._matplotlib", data=Series(dtype=object), args=[], kwargs={}, ) assert x is None assert y is None assert kind == "line" assert len(kwargs) == 24 @td.skip_if_no_mpl class TestSeriesPlots(TestPlotBase): def setup_method(self, method): TestPlotBase.setup_method(self, method) import matplotlib as mpl mpl.rcdefaults() self.ts = tm.makeTimeSeries() self.ts.name = "ts" def test_autocorrelation_plot(self): from pandas.plotting import autocorrelation_plot # Ensure no UserWarning when making plot with tm.assert_produces_warning(None): _check_plot_works(autocorrelation_plot, series=self.ts) _check_plot_works(autocorrelation_plot, series=self.ts.values) ax = autocorrelation_plot(self.ts, label="Test") self._check_legend_labels(ax, labels=["Test"]) def test_lag_plot(self): from pandas.plotting import lag_plot _check_plot_works(lag_plot, series=self.ts) _check_plot_works(lag_plot, series=self.ts, lag=5) def test_bootstrap_plot(self): from pandas.plotting import bootstrap_plot _check_plot_works(bootstrap_plot, series=self.ts, size=10) @td.skip_if_no_mpl class TestDataFramePlots(TestPlotBase): @td.skip_if_no_scipy @pytest.mark.parametrize("pass_axis", [False, True]) def test_scatter_matrix_axis(self, pass_axis): from pandas.plotting._matplotlib.compat import mpl_ge_3_0_0 scatter_matrix = plotting.scatter_matrix ax = None if pass_axis: _, ax = self.plt.subplots(3, 3) with tm.RNGContext(42): df = DataFrame(np.random.randn(100, 3)) # we are plotting multiples on a sub-plot with tm.assert_produces_warning( UserWarning, raise_on_extra_warnings=mpl_ge_3_0_0() ): axes = _check_plot_works( scatter_matrix, filterwarnings="always", frame=df, range_padding=0.1, ax=ax, ) axes0_labels = axes[0][0].yaxis.get_majorticklabels() # GH 5662 expected = ["-2", "0", "2"] self._check_text_labels(axes0_labels, expected) self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0) df[0] = (df[0] - 2) / 3 # we are plotting multiples on a sub-plot with tm.assert_produces_warning(UserWarning): axes = _check_plot_works( scatter_matrix, filterwarnings="always", frame=df, range_padding=0.1, ax=ax, ) axes0_labels = axes[0][0].yaxis.get_majorticklabels() expected = ["-1.0", "-0.5", "0.0"] self._check_text_labels(axes0_labels, expected) self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0) def test_andrews_curves(self, iris): from matplotlib import cm from pandas.plotting import andrews_curves df = iris # Ensure no UserWarning when making plot with tm.assert_produces_warning(None): _check_plot_works(andrews_curves, frame=df, class_column="Name") rgba = ("#556270", "#4ECDC4", "#C7F464") ax = _check_plot_works( andrews_curves, frame=df, class_column="Name", color=rgba ) self._check_colors( ax.get_lines()[:10], linecolors=rgba, mapping=df["Name"][:10] ) cnames = ["dodgerblue", "aquamarine", "seagreen"] ax = _check_plot_works( andrews_curves, frame=df, class_column="Name", color=cnames ) self._check_colors( ax.get_lines()[:10], linecolors=cnames, mapping=df["Name"][:10] ) ax = _check_plot_works( andrews_curves, frame=df, class_column="Name", colormap=cm.jet ) cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())] self._check_colors( ax.get_lines()[:10], linecolors=cmaps, mapping=df["Name"][:10] ) length = 10 df = DataFrame( { "A": np.random.rand(length), "B": np.random.rand(length), "C": np.random.rand(length), "Name": ["A"] * length, } ) _check_plot_works(andrews_curves, frame=df, class_column="Name") rgba = ("#556270", "#4ECDC4", "#C7F464") ax = _check_plot_works( andrews_curves, frame=df, class_column="Name", color=rgba ) self._check_colors( ax.get_lines()[:10], linecolors=rgba, mapping=df["Name"][:10] ) cnames = ["dodgerblue", "aquamarine", "seagreen"] ax = _check_plot_works( andrews_curves, frame=df, class_column="Name", color=cnames ) self._check_colors( ax.get_lines()[:10], linecolors=cnames, mapping=df["Name"][:10] ) ax = _check_plot_works( andrews_curves, frame=df, class_column="Name", colormap=cm.jet ) cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())] self._check_colors( ax.get_lines()[:10], linecolors=cmaps, mapping=df["Name"][:10] ) colors = ["b", "g", "r"] df = DataFrame({"A": [1, 2, 3], "B": [1, 2, 3], "C": [1, 2, 3], "Name": colors}) ax = andrews_curves(df, "Name", color=colors) handles, labels = ax.get_legend_handles_labels() self._check_colors(handles, linecolors=colors) def test_parallel_coordinates(self, iris): from matplotlib import cm from pandas.plotting import parallel_coordinates df = iris ax = _check_plot_works(parallel_coordinates, frame=df, class_column="Name") nlines = len(ax.get_lines()) nxticks = len(ax.xaxis.get_ticklabels()) rgba = ("#556270", "#4ECDC4", "#C7F464") ax = _check_plot_works( parallel_coordinates, frame=df, class_column="Name", color=rgba ) self._check_colors( ax.get_lines()[:10], linecolors=rgba, mapping=df["Name"][:10] ) cnames = ["dodgerblue", "aquamarine", "seagreen"] ax = _check_plot_works( parallel_coordinates, frame=df, class_column="Name", color=cnames ) self._check_colors( ax.get_lines()[:10], linecolors=cnames, mapping=df["Name"][:10] ) ax = _check_plot_works( parallel_coordinates, frame=df, class_column="Name", colormap=cm.jet ) cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())] self._check_colors( ax.get_lines()[:10], linecolors=cmaps, mapping=df["Name"][:10] ) ax = _check_plot_works( parallel_coordinates, frame=df, class_column="Name", axvlines=False ) assert len(ax.get_lines()) == (nlines - nxticks) colors = ["b", "g", "r"] df = DataFrame({"A": [1, 2, 3], "B": [1, 2, 3], "C": [1, 2, 3], "Name": colors}) ax = parallel_coordinates(df, "Name", color=colors) handles, labels = ax.get_legend_handles_labels() self._check_colors(handles, linecolors=colors) # not sure if this is indicative of a problem @pytest.mark.filterwarnings("ignore:Attempting to set:UserWarning") def test_parallel_coordinates_with_sorted_labels(self): """ For #15908 """ from pandas.plotting import parallel_coordinates df = DataFrame( { "feat": list(range(30)), "class": [2 for _ in range(10)] + [3 for _ in range(10)] + [1 for _ in range(10)], } ) ax = parallel_coordinates(df, "class", sort_labels=True) polylines, labels = ax.get_legend_handles_labels() color_label_tuples = zip( [polyline.get_color() for polyline in polylines], labels ) ordered_color_label_tuples = sorted(color_label_tuples, key=lambda x: x[1]) prev_next_tupels = zip( list(ordered_color_label_tuples[0:-1]), list(ordered_color_label_tuples[1:]) ) for prev, nxt in prev_next_tupels: # labels and colors are ordered strictly increasing assert prev[1] < nxt[1] and prev[0] < nxt[0] def test_radviz(self, iris): from matplotlib import cm from pandas.plotting import radviz df = iris # Ensure no UserWarning when making plot with tm.assert_produces_warning(None): _check_plot_works(radviz, frame=df, class_column="Name") rgba = ("#556270", "#4ECDC4", "#C7F464") ax = _check_plot_works(radviz, frame=df, class_column="Name", color=rgba) # skip Circle drawn as ticks patches = [p for p in ax.patches[:20] if p.get_label() != ""] self._check_colors(patches[:10], facecolors=rgba, mapping=df["Name"][:10]) cnames = ["dodgerblue", "aquamarine", "seagreen"] _check_plot_works(radviz, frame=df, class_column="Name", color=cnames) patches = [p for p in ax.patches[:20] if p.get_label() != ""] self._check_colors(patches, facecolors=cnames, mapping=df["Name"][:10]) _check_plot_works(radviz, frame=df, class_column="Name", colormap=cm.jet) cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())] patches = [p for p in ax.patches[:20] if p.get_label() != ""] self._check_colors(patches, facecolors=cmaps, mapping=df["Name"][:10]) colors = [[0.0, 0.0, 1.0, 1.0], [0.0, 0.5, 1.0, 1.0], [1.0, 0.0, 0.0, 1.0]] df = DataFrame( {"A": [1, 2, 3], "B": [2, 1, 3], "C": [3, 2, 1], "Name": ["b", "g", "r"]} ) ax = radviz(df, "Name", color=colors) handles, labels = ax.get_legend_handles_labels() self._check_colors(handles, facecolors=colors) def test_subplot_titles(self, iris): df = iris.drop("Name", axis=1).head() # Use the column names as the subplot titles title = list(df.columns) # Case len(title) == len(df) plot = df.plot(subplots=True, title=title) assert [p.get_title() for p in plot] == title # Case len(title) > len(df) msg = ( "The length of `title` must equal the number of columns if " "using `title` of type `list` and `subplots=True`" ) with pytest.raises(ValueError, match=msg): df.plot(subplots=True, title=title + ["kittens > puppies"]) # Case len(title) < len(df) with pytest.raises(ValueError, match=msg): df.plot(subplots=True, title=title[:2]) # Case subplots=False and title is of type list msg = ( "Using `title` of type `list` is not supported unless " "`subplots=True` is passed" ) with pytest.raises(ValueError, match=msg): df.plot(subplots=False, title=title) # Case df with 3 numeric columns but layout of (2,2) plot = df.drop("SepalWidth", axis=1).plot( subplots=True, layout=(2, 2), title=title[:-1] ) title_list = [ax.get_title() for sublist in plot for ax in sublist] assert title_list == title[:3] + [""] def test_get_standard_colors_random_seed(self): # GH17525 df = DataFrame(np.zeros((10, 10))) # Make sure that the np.random.seed isn't reset by get_standard_colors plotting.parallel_coordinates(df, 0) rand1 = np.random.random() plotting.parallel_coordinates(df, 0) rand2 = np.random.random() assert rand1 != rand2 # Make sure it produces the same colors every time it's called from pandas.plotting._matplotlib.style import get_standard_colors color1 = get_standard_colors(1, color_type="random") color2 = get_standard_colors(1, color_type="random") assert color1 == color2 def test_get_standard_colors_default_num_colors(self): from pandas.plotting._matplotlib.style import get_standard_colors # Make sure the default color_types returns the specified amount color1 = get_standard_colors(1, color_type="default") color2 = get_standard_colors(9, color_type="default") color3 = get_standard_colors(20, color_type="default") assert len(color1) == 1 assert len(color2) == 9 assert len(color3) == 20 def test_plot_single_color(self): # Example from #20585. All 3 bars should have the same color df = DataFrame( { "account-start": ["2017-02-03", "2017-03-03", "2017-01-01"], "client": ["Alice Anders", "Bob Baker", "Charlie Chaplin"], "balance": [-1432.32, 10.43, 30000.00], "db-id": [1234, 2424, 251], "proxy-id": [525, 1525, 2542], "rank": [52, 525, 32], } ) ax = df.client.value_counts().plot.bar() colors = [rect.get_facecolor() for rect in ax.get_children()[0:3]] assert all(color == colors[0] for color in colors) def test_get_standard_colors_no_appending(self): # GH20726 # Make sure not to add more colors so that matplotlib can cycle # correctly. from matplotlib import cm from pandas.plotting._matplotlib.style import get_standard_colors color_before = cm.gnuplot(range(5)) color_after = get_standard_colors(1, color=color_before) assert len(color_after) == len(color_before) df = DataFrame(np.random.randn(48, 4), columns=list("ABCD")) color_list = cm.gnuplot(np.linspace(0, 1, 16)) p = df.A.plot.bar(figsize=(16, 7), color=color_list) assert p.patches[1].get_facecolor() == p.patches[17].get_facecolor() def test_dictionary_color(self): # issue-8193 # Test plot color dictionary format data_files = ["a", "b"] expected = [(0.5, 0.24, 0.6), (0.3, 0.7, 0.7)] df1 = DataFrame(np.random.rand(2, 2), columns=data_files) dic_color = {"b": (0.3, 0.7, 0.7), "a": (0.5, 0.24, 0.6)} # Bar color test ax = df1.plot(kind="bar", color=dic_color) colors = [rect.get_facecolor()[0:-1] for rect in ax.get_children()[0:3:2]] assert all(color == expected[index] for index, color in enumerate(colors)) # Line color test ax = df1.plot(kind="line", color=dic_color) colors = [rect.get_color() for rect in ax.get_lines()[0:2]] assert all(color == expected[index] for index, color in enumerate(colors)) def test_has_externally_shared_axis_x_axis(self): # GH33819 # Test _has_externally_shared_axis() works for x-axis func = plotting._matplotlib.tools._has_externally_shared_axis fig = self.plt.figure() plots = fig.subplots(2, 4) # Create *externally* shared axes for first and third columns plots[0][0] = fig.add_subplot(231, sharex=plots[1][0]) plots[0][2] = fig.add_subplot(233, sharex=plots[1][2]) # Create *internally* shared axes for second and third columns plots[0][1].twinx() plots[0][2].twinx() # First column is only externally shared # Second column is only internally shared # Third column is both # Fourth column is neither assert func(plots[0][0], "x") assert not func(plots[0][1], "x") assert func(plots[0][2], "x") assert not func(plots[0][3], "x") def test_has_externally_shared_axis_y_axis(self): # GH33819 # Test _has_externally_shared_axis() works for y-axis func = plotting._matplotlib.tools._has_externally_shared_axis fig = self.plt.figure() plots = fig.subplots(4, 2) # Create *externally* shared axes for first and third rows plots[0][0] = fig.add_subplot(321, sharey=plots[0][1]) plots[2][0] = fig.add_subplot(325, sharey=plots[2][1]) # Create *internally* shared axes for second and third rows plots[1][0].twiny() plots[2][0].twiny() # First row is only externally shared # Second row is only internally shared # Third row is both # Fourth row is neither assert func(plots[0][0], "y") assert not func(plots[1][0], "y") assert func(plots[2][0], "y") assert not func(plots[3][0], "y") def test_has_externally_shared_axis_invalid_compare_axis(self): # GH33819 # Test _has_externally_shared_axis() raises an exception when # passed an invalid value as compare_axis parameter func = plotting._matplotlib.tools._has_externally_shared_axis fig = self.plt.figure() plots = fig.subplots(4, 2) # Create arbitrary axes plots[0][0] = fig.add_subplot(321, sharey=plots[0][1]) # Check that an invalid compare_axis value triggers the expected exception msg = "needs 'x' or 'y' as a second parameter" with pytest.raises(ValueError, match=msg): func(plots[0][0], "z") def test_externally_shared_axes(self): # Example from GH33819 # Create data df = DataFrame({"a": np.random.randn(1000), "b": np.random.randn(1000)}) # Create figure fig = self.plt.figure() plots = fig.subplots(2, 3) # Create *externally* shared axes plots[0][0] = fig.add_subplot(231, sharex=plots[1][0]) # note: no plots[0][1] that's the twin only case plots[0][2] = fig.add_subplot(233, sharex=plots[1][2]) # Create *internally* shared axes # note: no plots[0][0] that's the external only case twin_ax1 = plots[0][1].twinx() twin_ax2 = plots[0][2].twinx() # Plot data to primary axes df["a"].plot(ax=plots[0][0], title="External share only").set_xlabel( "this label should never be visible" ) df["a"].plot(ax=plots[1][0]) df["a"].plot(ax=plots[0][1], title="Internal share (twin) only").set_xlabel( "this label should always be visible" ) df["a"].plot(ax=plots[1][1]) df["a"].plot(ax=plots[0][2], title="Both").set_xlabel( "this label should never be visible" ) df["a"].plot(ax=plots[1][2]) # Plot data to twinned axes df["b"].plot(ax=twin_ax1, color="green") df["b"].plot(ax=twin_ax2, color="yellow") assert not plots[0][0].xaxis.get_label().get_visible() assert plots[0][1].xaxis.get_label().get_visible() assert not plots[0][2].xaxis.get_label().get_visible()
bsd-3-clause
kernelmilowill/PDMQBACKTEST
vn.how/tick2trade/vn.trader_t2t/ctaAlgo/strategyAtrRsi.py
10
11369
# encoding: UTF-8 """ 一个ATR-RSI指标结合的交易策略,适合用在股指的1分钟和5分钟线上。 注意事项: 1. 作者不对交易盈利做任何保证,策略代码仅供参考 2. 本策略需要用到talib,没有安装的用户请先参考www.vnpy.org上的教程安装 3. 将IF0000_1min.csv用ctaHistoryData.py导入MongoDB后,直接运行本文件即可回测策略 """ from ctaBase import * from ctaTemplate import CtaTemplate import talib import numpy as np ######################################################################## class AtrRsiStrategy(CtaTemplate): """结合ATR和RSI指标的一个分钟线交易策略""" className = 'AtrRsiStrategy' author = u'用Python的交易员' # 策略参数 atrLength = 22 # 计算ATR指标的窗口数 atrMaLength = 10 # 计算ATR均线的窗口数 rsiLength = 5 # 计算RSI的窗口数 rsiEntry = 16 # RSI的开仓信号 trailingPercent = 0.8 # 百分比移动止损 initDays = 10 # 初始化数据所用的天数 # 策略变量 bar = None # K线对象 barMinute = EMPTY_STRING # K线当前的分钟 bufferSize = 100 # 需要缓存的数据的大小 bufferCount = 0 # 目前已经缓存了的数据的计数 highArray = np.zeros(bufferSize) # K线最高价的数组 lowArray = np.zeros(bufferSize) # K线最低价的数组 closeArray = np.zeros(bufferSize) # K线收盘价的数组 atrCount = 0 # 目前已经缓存了的ATR的计数 atrArray = np.zeros(bufferSize) # ATR指标的数组 atrValue = 0 # 最新的ATR指标数值 atrMa = 0 # ATR移动平均的数值 rsiValue = 0 # RSI指标的数值 rsiBuy = 0 # RSI买开阈值 rsiSell = 0 # RSI卖开阈值 intraTradeHigh = 0 # 移动止损用的持仓期内最高价 intraTradeLow = 0 # 移动止损用的持仓期内最低价 orderList = [] # 保存委托代码的列表 # 参数列表,保存了参数的名称 paramList = ['name', 'className', 'author', 'vtSymbol', 'atrLength', 'atrMaLength', 'rsiLength', 'rsiEntry', 'trailingPercent'] # 变量列表,保存了变量的名称 varList = ['inited', 'trading', 'pos', 'atrValue', 'atrMa', 'rsiValue', 'rsiBuy', 'rsiSell'] #---------------------------------------------------------------------- def __init__(self, ctaEngine, setting): """Constructor""" super(AtrRsiStrategy, self).__init__(ctaEngine, setting) # 注意策略类中的可变对象属性(通常是list和dict等),在策略初始化时需要重新创建, # 否则会出现多个策略实例之间数据共享的情况,有可能导致潜在的策略逻辑错误风险, # 策略类中的这些可变对象属性可以选择不写,全都放在__init__下面,写主要是为了阅读 # 策略时方便(更多是个编程习惯的选择) #---------------------------------------------------------------------- def onInit(self): """初始化策略(必须由用户继承实现)""" self.writeCtaLog(u'%s策略初始化' %self.name) # 初始化RSI入场阈值 self.rsiBuy = 50 + self.rsiEntry self.rsiSell = 50 - self.rsiEntry # 载入历史数据,并采用回放计算的方式初始化策略数值 initData = self.loadBar(self.initDays) for bar in initData: self.onBar(bar) self.putEvent() #---------------------------------------------------------------------- def onStart(self): """启动策略(必须由用户继承实现)""" self.writeCtaLog(u'%s策略启动' %self.name) self.putEvent() #---------------------------------------------------------------------- def onStop(self): """停止策略(必须由用户继承实现)""" self.writeCtaLog(u'%s策略停止' %self.name) self.putEvent() #---------------------------------------------------------------------- def onTick(self, tick): """收到行情TICK推送(必须由用户继承实现)""" # 计算K线 tickMinute = tick.datetime.minute if tickMinute != self.barMinute: if self.bar: self.onBar(self.bar) bar = CtaBarData() bar.vtSymbol = tick.vtSymbol bar.symbol = tick.symbol bar.exchange = tick.exchange bar.open = tick.lastPrice bar.high = tick.lastPrice bar.low = tick.lastPrice bar.close = tick.lastPrice bar.date = tick.date bar.time = tick.time bar.datetime = tick.datetime # K线的时间设为第一个Tick的时间 self.bar = bar # 这种写法为了减少一层访问,加快速度 self.barMinute = tickMinute # 更新当前的分钟 else: # 否则继续累加新的K线 bar = self.bar # 写法同样为了加快速度 bar.high = max(bar.high, tick.lastPrice) bar.low = min(bar.low, tick.lastPrice) bar.close = tick.lastPrice #---------------------------------------------------------------------- def onBar(self, bar): """收到Bar推送(必须由用户继承实现)""" # 撤销之前发出的尚未成交的委托(包括限价单和停止单) for orderID in self.orderList: self.cancelOrder(orderID) self.orderList = [] # 保存K线数据 self.closeArray[0:self.bufferSize-1] = self.closeArray[1:self.bufferSize] self.highArray[0:self.bufferSize-1] = self.highArray[1:self.bufferSize] self.lowArray[0:self.bufferSize-1] = self.lowArray[1:self.bufferSize] self.closeArray[-1] = bar.close self.highArray[-1] = bar.high self.lowArray[-1] = bar.low self.bufferCount += 1 if self.bufferCount < self.bufferSize: return # 计算指标数值 self.atrValue = talib.ATR(self.highArray, self.lowArray, self.closeArray, self.atrLength)[-1] self.atrArray[0:self.bufferSize-1] = self.atrArray[1:self.bufferSize] self.atrArray[-1] = self.atrValue self.atrCount += 1 if self.atrCount < self.bufferSize: return self.atrMa = talib.MA(self.atrArray, self.atrMaLength)[-1] self.rsiValue = talib.RSI(self.closeArray, self.rsiLength)[-1] # 判断是否要进行交易 # 当前无仓位 if self.pos == 0: self.intraTradeHigh = bar.high self.intraTradeLow = bar.low # ATR数值上穿其移动平均线,说明行情短期内波动加大 # 即处于趋势的概率较大,适合CTA开仓 if self.atrValue > self.atrMa: # 使用RSI指标的趋势行情时,会在超买超卖区钝化特征,作为开仓信号 if self.rsiValue > self.rsiBuy: # 这里为了保证成交,选择超价5个整指数点下单 self.buy(bar.close+5, 1) elif self.rsiValue < self.rsiSell: self.short(bar.close-5, 1) # 持有多头仓位 elif self.pos > 0: # 计算多头持有期内的最高价,以及重置最低价 self.intraTradeHigh = max(self.intraTradeHigh, bar.high) self.intraTradeLow = bar.low # 计算多头移动止损 longStop = self.intraTradeHigh * (1-self.trailingPercent/100) # 发出本地止损委托,并且把委托号记录下来,用于后续撤单 orderID = self.sell(longStop, 1, stop=True) self.orderList.append(orderID) # 持有空头仓位 elif self.pos < 0: self.intraTradeLow = min(self.intraTradeLow, bar.low) self.intraTradeHigh = bar.high shortStop = self.intraTradeLow * (1+self.trailingPercent/100) orderID = self.cover(shortStop, 1, stop=True) self.orderList.append(orderID) # 发出状态更新事件 self.putEvent() #---------------------------------------------------------------------- def onOrder(self, order): """收到委托变化推送(必须由用户继承实现)""" pass #---------------------------------------------------------------------- def onTrade(self, trade): pass if __name__ == '__main__': # 提供直接双击回测的功能 # 导入PyQt4的包是为了保证matplotlib使用PyQt4而不是PySide,防止初始化出错 from ctaBacktesting import * from PyQt4 import QtCore, QtGui # 创建回测引擎 engine = BacktestingEngine() # 设置引擎的回测模式为K线 engine.setBacktestingMode(engine.BAR_MODE) # 设置回测用的数据起始日期 engine.setStartDate('20120101') # 设置产品相关参数 engine.setSlippage(0.2) # 股指1跳 engine.setRate(0.3/10000) # 万0.3 engine.setSize(300) # 股指合约大小 # 设置使用的历史数据库 engine.setDatabase(MINUTE_DB_NAME, 'IF0000') ## 在引擎中创建策略对象 #d = {'atrLength': 11} #engine.initStrategy(AtrRsiStrategy, d) ## 开始跑回测 ##engine.runBacktesting() ## 显示回测结果 ##engine.showBacktestingResult() # 跑优化 setting = OptimizationSetting() # 新建一个优化任务设置对象 setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利 setting.addParameter('atrLength', 12, 20, 2) # 增加第一个优化参数atrLength,起始11,结束12,步进1 setting.addParameter('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa,起始20,结束30,步进1 setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数 # 性能测试环境:I7-3770,主频3.4G, 8核心,内存16G,Windows 7 专业版 # 测试时还跑着一堆其他的程序,性能仅供参考 import time start = time.time() # 运行单进程优化函数,自动输出结果,耗时:359秒 engine.runOptimization(AtrRsiStrategy, setting) # 多进程优化,耗时:89秒 #engine.runParallelOptimization(AtrRsiStrategy, setting) print u'耗时:%s' %(time.time()-start)
mit
pkruskal/scikit-learn
examples/semi_supervised/plot_label_propagation_digits.py
268
2723
""" =================================================== Label Propagation digits: Demonstrating performance =================================================== This example demonstrates the power of semisupervised learning by training a Label Spreading model to classify handwritten digits with sets of very few labels. The handwritten digit dataset has 1797 total points. The model will be trained using all points, but only 30 will be labeled. Results in the form of a confusion matrix and a series of metrics over each class will be very good. At the end, the top 10 most uncertain predictions will be shown. """ print(__doc__) # Authors: Clay Woolam <clay@woolam.org> # Licence: BSD import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn import datasets from sklearn.semi_supervised import label_propagation from sklearn.metrics import confusion_matrix, classification_report digits = datasets.load_digits() rng = np.random.RandomState(0) indices = np.arange(len(digits.data)) rng.shuffle(indices) X = digits.data[indices[:330]] y = digits.target[indices[:330]] images = digits.images[indices[:330]] n_total_samples = len(y) n_labeled_points = 30 indices = np.arange(n_total_samples) unlabeled_set = indices[n_labeled_points:] # shuffle everything around y_train = np.copy(y) y_train[unlabeled_set] = -1 ############################################################################### # Learn with LabelSpreading lp_model = label_propagation.LabelSpreading(gamma=0.25, max_iter=5) lp_model.fit(X, y_train) predicted_labels = lp_model.transduction_[unlabeled_set] true_labels = y[unlabeled_set] cm = confusion_matrix(true_labels, predicted_labels, labels=lp_model.classes_) print("Label Spreading model: %d labeled & %d unlabeled points (%d total)" % (n_labeled_points, n_total_samples - n_labeled_points, n_total_samples)) print(classification_report(true_labels, predicted_labels)) print("Confusion matrix") print(cm) # calculate uncertainty values for each transduced distribution pred_entropies = stats.distributions.entropy(lp_model.label_distributions_.T) # pick the top 10 most uncertain labels uncertainty_index = np.argsort(pred_entropies)[-10:] ############################################################################### # plot f = plt.figure(figsize=(7, 5)) for index, image_index in enumerate(uncertainty_index): image = images[image_index] sub = f.add_subplot(2, 5, index + 1) sub.imshow(image, cmap=plt.cm.gray_r) plt.xticks([]) plt.yticks([]) sub.set_title('predict: %i\ntrue: %i' % ( lp_model.transduction_[image_index], y[image_index])) f.suptitle('Learning with small amount of labeled data') plt.show()
bsd-3-clause
jlegendary/scikit-learn
examples/ensemble/plot_forest_importances.py
241
1761
""" ========================================= Feature importances with forests of trees ========================================= This examples shows the use of forests of trees to evaluate the importance of features on an artificial classification task. The red bars are the feature importances of the forest, along with their inter-trees variability. As expected, the plot suggests that 3 features are informative, while the remaining are not. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_classification from sklearn.ensemble import ExtraTreesClassifier # Build a classification task using 3 informative features X, y = make_classification(n_samples=1000, n_features=10, n_informative=3, n_redundant=0, n_repeated=0, n_classes=2, random_state=0, shuffle=False) # Build a forest and compute the feature importances forest = ExtraTreesClassifier(n_estimators=250, random_state=0) forest.fit(X, y) importances = forest.feature_importances_ std = np.std([tree.feature_importances_ for tree in forest.estimators_], axis=0) indices = np.argsort(importances)[::-1] # Print the feature ranking print("Feature ranking:") for f in range(10): print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]])) # Plot the feature importances of the forest plt.figure() plt.title("Feature importances") plt.bar(range(10), importances[indices], color="r", yerr=std[indices], align="center") plt.xticks(range(10), indices) plt.xlim([-1, 10]) plt.show()
bsd-3-clause
alephu5/Soundbyte
environment/lib/python3.3/site-packages/IPython/lib/tests/test_latextools.py
10
4076
# encoding: utf-8 """Tests for IPython.utils.path.py""" #----------------------------------------------------------------------------- # Copyright (C) 2008-2011 The IPython Development Team # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import nose.tools as nt from IPython.lib import latextools from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib from IPython.testing.tools import monkeypatch from IPython.utils.process import FindCmdError def test_latex_to_png_dvipng_fails_when_no_cmd(): """ `latex_to_png_dvipng` should return None when there is no required command """ for command in ['latex', 'dvipng']: yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) def check_latex_to_png_dvipng_fails_when_no_cmd(command): def mock_find_cmd(arg): if arg == command: raise FindCmdError with monkeypatch(latextools, "find_cmd", mock_find_cmd): nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True), None) @onlyif_cmds_exist('latex', 'dvipng') def test_latex_to_png_dvipng_runs(): """ Test that latex_to_png_dvipng just runs without error. """ def mock_kpsewhich(filename): nt.assert_equals(filename, "breqn.sty") return None for (s, wrap) in [("$$x^2$$", False), ("x^2", True)]: yield (latextools.latex_to_png_dvipng, s, wrap) with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): yield (latextools.latex_to_png_dvipng, s, wrap) @skipif_not_matplotlib def test_latex_to_png_mpl_runs(): """ Test that latex_to_png_mpl just runs without error. """ def mock_kpsewhich(filename): nt.assert_equals(filename, "breqn.sty") return None for (s, wrap) in [("$x^2$", False), ("x^2", True)]: yield (latextools.latex_to_png_mpl, s, wrap) with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): yield (latextools.latex_to_png_mpl, s, wrap) @skipif_not_matplotlib def test_latex_to_html(): img = latextools.latex_to_html("$x^2$") nt.assert_in("data:image/png;base64,iVBOR", img) def test_genelatex_no_wrap(): """ Test genelatex with wrap=False. """ def mock_kpsewhich(filename): assert False, ("kpsewhich should not be called " "(called with {0})".format(filename)) with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): nt.assert_equals( '\n'.join(latextools.genelatex("body text", False)), r'''\documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} \usepackage{bm} \pagestyle{empty} \begin{document} body text \end{document}''') def test_genelatex_wrap_with_breqn(): """ Test genelatex with wrap=True for the case breqn.sty is installed. """ def mock_kpsewhich(filename): nt.assert_equals(filename, "breqn.sty") return "path/to/breqn.sty" with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): nt.assert_equals( '\n'.join(latextools.genelatex("x^2", True)), r'''\documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} \usepackage{bm} \usepackage{breqn} \pagestyle{empty} \begin{document} \begin{dmath*} x^2 \end{dmath*} \end{document}''') def test_genelatex_wrap_without_breqn(): """ Test genelatex with wrap=True for the case breqn.sty is not installed. """ def mock_kpsewhich(filename): nt.assert_equals(filename, "breqn.sty") return None with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): nt.assert_equals( '\n'.join(latextools.genelatex("x^2", True)), r'''\documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} \usepackage{bm} \pagestyle{empty} \begin{document} $$x^2$$ \end{document}''')
gpl-3.0
bgris/ODL_bgris
lib/python3.5/site-packages/scipy/interpolate/_cubic.py
10
29293
"""Interpolation algorithms using piecewise cubic polynomials.""" from __future__ import division, print_function, absolute_import import numpy as np from scipy._lib.six import string_types from . import BPoly, PPoly from .polyint import _isscalar from scipy._lib._util import _asarray_validated from scipy.linalg import solve_banded, solve __all__ = ["PchipInterpolator", "pchip_interpolate", "pchip", "Akima1DInterpolator", "CubicSpline"] class PchipInterpolator(BPoly): r"""PCHIP 1-d monotonic cubic interpolation. `x` and `y` are arrays of values used to approximate some function f, with ``y = f(x)``. The interpolant uses monotonic cubic splines to find the value of new points. (PCHIP stands for Piecewise Cubic Hermite Interpolating Polynomial). Parameters ---------- x : ndarray A 1-D array of monotonically increasing real values. `x` cannot include duplicate values (otherwise f is overspecified) y : ndarray A 1-D array of real values. `y`'s length along the interpolation axis must be equal to the length of `x`. If N-D array, use `axis` parameter to select correct axis. axis : int, optional Axis in the y array corresponding to the x-coordinate values. extrapolate : bool, optional Whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. Methods ------- __call__ derivative antiderivative roots See Also -------- Akima1DInterpolator CubicSpline BPoly Notes ----- The interpolator preserves monotonicity in the interpolation data and does not overshoot if the data is not smooth. The first derivatives are guaranteed to be continuous, but the second derivatives may jump at :math:`x_k`. Determines the derivatives at the points :math:`x_k`, :math:`f'_k`, by using PCHIP algorithm [1]_. Let :math:`h_k = x_{k+1} - x_k`, and :math:`d_k = (y_{k+1} - y_k) / h_k` are the slopes at internal points :math:`x_k`. If the signs of :math:`d_k` and :math:`d_{k-1}` are different or either of them equals zero, then :math:`f'_k = 0`. Otherwise, it is given by the weighted harmonic mean .. math:: \frac{w_1 + w_2}{f'_k} = \frac{w_1}{d_{k-1}} + \frac{w_2}{d_k} where :math:`w_1 = 2 h_k + h_{k-1}` and :math:`w_2 = h_k + 2 h_{k-1}`. The end slopes are set using a one-sided scheme [2]_. References ---------- .. [1] F. N. Fritsch and R. E. Carlson, Monotone Piecewise Cubic Interpolation, SIAM J. Numer. Anal., 17(2), 238 (1980). DOI:10.1137/0717021 .. [2] see, e.g., C. Moler, Numerical Computing with Matlab, 2004. DOI: http://dx.doi.org/10.1137/1.9780898717952 """ def __init__(self, x, y, axis=0, extrapolate=None): x = _asarray_validated(x, check_finite=False, as_inexact=True) y = _asarray_validated(y, check_finite=False, as_inexact=True) axis = axis % y.ndim xp = x.reshape((x.shape[0],) + (1,)*(y.ndim-1)) yp = np.rollaxis(y, axis) dk = self._find_derivatives(xp, yp) data = np.hstack((yp[:, None, ...], dk[:, None, ...])) _b = BPoly.from_derivatives(x, data, orders=None) super(PchipInterpolator, self).__init__(_b.c, _b.x, extrapolate=extrapolate) self.axis = axis def roots(self): """ Return the roots of the interpolated function. """ return (PPoly.from_bernstein_basis(self)).roots() @staticmethod def _edge_case(h0, h1, m0, m1): # one-sided three-point estimate for the derivative d = ((2*h0 + h1)*m0 - h0*m1) / (h0 + h1) # try to preserve shape mask = np.sign(d) != np.sign(m0) mask2 = (np.sign(m0) != np.sign(m1)) & (np.abs(d) > 3.*np.abs(m0)) mmm = (~mask) & mask2 d[mask] = 0. d[mmm] = 3.*m0[mmm] return d @staticmethod def _find_derivatives(x, y): # Determine the derivatives at the points y_k, d_k, by using # PCHIP algorithm is: # We choose the derivatives at the point x_k by # Let m_k be the slope of the kth segment (between k and k+1) # If m_k=0 or m_{k-1}=0 or sgn(m_k) != sgn(m_{k-1}) then d_k == 0 # else use weighted harmonic mean: # w_1 = 2h_k + h_{k-1}, w_2 = h_k + 2h_{k-1} # 1/d_k = 1/(w_1 + w_2)*(w_1 / m_k + w_2 / m_{k-1}) # where h_k is the spacing between x_k and x_{k+1} y_shape = y.shape if y.ndim == 1: # So that _edge_case doesn't end up assigning to scalars x = x[:, None] y = y[:, None] hk = x[1:] - x[:-1] mk = (y[1:] - y[:-1]) / hk if y.shape[0] == 2: # edge case: only have two points, use linear interpolation dk = np.zeros_like(y) dk[0] = mk dk[1] = mk return dk.reshape(y_shape) smk = np.sign(mk) condition = (smk[1:] != smk[:-1]) | (mk[1:] == 0) | (mk[:-1] == 0) w1 = 2*hk[1:] + hk[:-1] w2 = hk[1:] + 2*hk[:-1] # values where division by zero occurs will be excluded # by 'condition' afterwards with np.errstate(divide='ignore'): whmean = (w1/mk[:-1] + w2/mk[1:]) / (w1 + w2) dk = np.zeros_like(y) dk[1:-1][condition] = 0.0 dk[1:-1][~condition] = 1.0 / whmean[~condition] # special case endpoints, as suggested in # Cleve Moler, Numerical Computing with MATLAB, Chap 3.4 dk[0] = PchipInterpolator._edge_case(hk[0], hk[1], mk[0], mk[1]) dk[-1] = PchipInterpolator._edge_case(hk[-1], hk[-2], mk[-1], mk[-2]) return dk.reshape(y_shape) def pchip_interpolate(xi, yi, x, der=0, axis=0): """ Convenience function for pchip interpolation. xi and yi are arrays of values used to approximate some function f, with ``yi = f(xi)``. The interpolant uses monotonic cubic splines to find the value of new points x and the derivatives there. See `PchipInterpolator` for details. Parameters ---------- xi : array_like A sorted list of x-coordinates, of length N. yi : array_like A 1-D array of real values. `yi`'s length along the interpolation axis must be equal to the length of `xi`. If N-D array, use axis parameter to select correct axis. x : scalar or array_like Of length M. der : int or list, optional Derivatives to extract. The 0-th derivative can be included to return the function value. axis : int, optional Axis in the yi array corresponding to the x-coordinate values. See Also -------- PchipInterpolator Returns ------- y : scalar or array_like The result, of length R or length M or M by R, """ P = PchipInterpolator(xi, yi, axis=axis) if der == 0: return P(x) elif _isscalar(der): return P.derivative(der)(x) else: return [P.derivative(nu)(x) for nu in der] # Backwards compatibility pchip = PchipInterpolator class Akima1DInterpolator(PPoly): """ Akima interpolator Fit piecewise cubic polynomials, given vectors x and y. The interpolation method by Akima uses a continuously differentiable sub-spline built from piecewise cubic polynomials. The resultant curve passes through the given data points and will appear smooth and natural. Parameters ---------- x : ndarray, shape (m, ) 1-D array of monotonically increasing real values. y : ndarray, shape (m, ...) N-D array of real values. The length of `y` along the first axis must be equal to the length of `x`. axis : int, optional Specifies the axis of `y` along which to interpolate. Interpolation defaults to the first axis of `y`. Methods ------- __call__ derivative antiderivative roots See Also -------- PchipInterpolator CubicSpline PPoly Notes ----- .. versionadded:: 0.14 Use only for precise data, as the fitted curve passes through the given points exactly. This routine is useful for plotting a pleasingly smooth curve through a few given points for purposes of plotting. References ---------- [1] A new method of interpolation and smooth curve fitting based on local procedures. Hiroshi Akima, J. ACM, October 1970, 17(4), 589-602. """ def __init__(self, x, y, axis=0): # Original implementation in MATLAB by N. Shamsundar (BSD licensed), see # http://www.mathworks.de/matlabcentral/fileexchange/1814-akima-interpolation x, y = map(np.asarray, (x, y)) axis = axis % y.ndim if np.any(np.diff(x) < 0.): raise ValueError("x must be strictly ascending") if x.ndim != 1: raise ValueError("x must be 1-dimensional") if x.size < 2: raise ValueError("at least 2 breakpoints are needed") if x.size != y.shape[axis]: raise ValueError("x.shape must equal y.shape[%s]" % axis) # move interpolation axis to front y = np.rollaxis(y, axis) # determine slopes between breakpoints m = np.empty((x.size + 3, ) + y.shape[1:]) dx = np.diff(x) dx = dx[(slice(None), ) + (None, ) * (y.ndim - 1)] m[2:-2] = np.diff(y, axis=0) / dx # add two additional points on the left ... m[1] = 2. * m[2] - m[3] m[0] = 2. * m[1] - m[2] # ... and on the right m[-2] = 2. * m[-3] - m[-4] m[-1] = 2. * m[-2] - m[-3] # if m1 == m2 != m3 == m4, the slope at the breakpoint is not defined. # This is the fill value: t = .5 * (m[3:] + m[:-3]) # get the denominator of the slope t dm = np.abs(np.diff(m, axis=0)) f1 = dm[2:] f2 = dm[:-2] f12 = f1 + f2 # These are the mask of where the the slope at breakpoint is defined: ind = np.nonzero(f12 > 1e-9 * np.max(f12)) x_ind, y_ind = ind[0], ind[1:] # Set the slope at breakpoint t[ind] = (f1[ind] * m[(x_ind + 1,) + y_ind] + f2[ind] * m[(x_ind + 2,) + y_ind]) / f12[ind] # calculate the higher order coefficients c = (3. * m[2:-2] - 2. * t[:-1] - t[1:]) / dx d = (t[:-1] + t[1:] - 2. * m[2:-2]) / dx ** 2 coeff = np.zeros((4, x.size - 1) + y.shape[1:]) coeff[3] = y[:-1] coeff[2] = t[:-1] coeff[1] = c coeff[0] = d super(Akima1DInterpolator, self).__init__(coeff, x, extrapolate=False) self.axis = axis def extend(self, c, x, right=True): raise NotImplementedError("Extending a 1D Akima interpolator is not " "yet implemented") # These are inherited from PPoly, but they do not produce an Akima # interpolator. Hence stub them out. @classmethod def from_spline(cls, tck, extrapolate=None): raise NotImplementedError("This method does not make sense for " "an Akima interpolator.") @classmethod def from_bernstein_basis(cls, bp, extrapolate=None): raise NotImplementedError("This method does not make sense for " "an Akima interpolator.") class CubicSpline(PPoly): """Cubic spline data interpolator. Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable [1]_. The result is represented as a `PPoly` instance with breakpoints matching the given data. Parameters ---------- x : array_like, shape (n,) 1-d array containing values of the independent variable. Values must be real, finite and in strictly increasing order. y : array_like Array containing values of the dependent variable. It can have arbitrary number of dimensions, but the length along `axis` (see below) must match the length of `x`. Values must be finite. axis : int, optional Axis along which `y` is assumed to be varying. Meaning that for ``x[i]`` the corresponding values are ``np.take(y, i, axis=axis)``. Default is 0. bc_type : string or 2-tuple, optional Boundary condition type. Two additional equations, given by the boundary conditions, are required to determine all coefficients of polynomials on each segment [2]_. If `bc_type` is a string, then the specified condition will be applied at both ends of a spline. Available conditions are: * 'not-a-knot' (default): The first and second segment at a curve end are the same polynomial. It is a good default when there is no information on boundary conditions. * 'periodic': The interpolated functions is assumed to be periodic of period ``x[-1] - x[0]``. The first and last value of `y` must be identical: ``y[0] == y[-1]``. This boundary condition will result in ``y'[0] == y'[-1]`` and ``y''[0] == y''[-1]``. * 'clamped': The first derivative at curves ends are zero. Assuming a 1D `y`, ``bc_type=((1, 0.0), (1, 0.0))`` is the same condition. * 'natural': The second derivative at curve ends are zero. Assuming a 1D `y`, ``bc_type=((2, 0.0), (2, 0.0))`` is the same condition. If `bc_type` is a 2-tuple, the first and the second value will be applied at the curve start and end respectively. The tuple values can be one of the previously mentioned strings (except 'periodic') or a tuple `(order, deriv_values)` allowing to specify arbitrary derivatives at curve ends: * `order`: the derivative order, 1 or 2. * `deriv_value`: array_like containing derivative values, shape must be the same as `y`, excluding `axis` dimension. For example, if `y` is 1D, then `deriv_value` must be a scalar. If `y` is 3D with the shape (n0, n1, n2) and axis=2, then `deriv_value` must be 2D and have the shape (n0, n1). extrapolate : {bool, 'periodic', None}, optional If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If 'periodic', periodic extrapolation is used. If None (default), `extrapolate` is set to 'periodic' for ``bc_type='periodic'`` and to True otherwise. Attributes ---------- x : ndarray, shape (n,) Breakpoints. The same `x` which was passed to the constructor. c : ndarray, shape (4, n-1, ...) Coefficients of the polynomials on each segment. The trailing dimensions match the dimensions of `y`, excluding `axis`. For example, if `y` is 1-d, then ``c[k, i]`` is a coefficient for ``(x-x[i])**(3-k)`` on the segment between ``x[i]`` and ``x[i+1]``. axis : int Interpolation axis. The same `axis` which was passed to the constructor. Methods ------- __call__ derivative antiderivative integrate roots See Also -------- Akima1DInterpolator PchipInterpolator PPoly Notes ----- Parameters `bc_type` and `interpolate` work independently, i.e. the former controls only construction of a spline, and the latter only evaluation. When a boundary condition is 'not-a-knot' and n = 2, it is replaced by a condition that the first derivative is equal to the linear interpolant slope. When both boundary conditions are 'not-a-knot' and n = 3, the solution is sought as a parabola passing through given points. When 'not-a-knot' boundary conditions is applied to both ends, the resulting spline will be the same as returned by `splrep` (with ``s=0``) and `InterpolatedUnivariateSpline`, but these two methods use a representation in B-spline basis. .. versionadded:: 0.18.0 Examples -------- In this example the cubic spline is used to interpolate a sampled sinusoid. You can see that the spline continuity property holds for the first and second derivatives and violates only for the third derivative. >>> from scipy.interpolate import CubicSpline >>> import matplotlib.pyplot as plt >>> x = np.arange(10) >>> y = np.sin(x) >>> cs = CubicSpline(x, y) >>> xs = np.arange(-0.5, 9.6, 0.1) >>> plt.figure(figsize=(6.5, 4)) >>> plt.plot(x, y, 'o', label='data') >>> plt.plot(xs, np.sin(xs), label='true') >>> plt.plot(xs, cs(xs), label="S") >>> plt.plot(xs, cs(xs, 1), label="S'") >>> plt.plot(xs, cs(xs, 2), label="S''") >>> plt.plot(xs, cs(xs, 3), label="S'''") >>> plt.xlim(-0.5, 9.5) >>> plt.legend(loc='lower left', ncol=2) >>> plt.show() In the second example, the unit circle is interpolated with a spline. A periodic boundary condition is used. You can see that the first derivative values, ds/dx=0, ds/dy=1 at the periodic point (1, 0) are correctly computed. Note that a circle cannot be exactly represented by a cubic spline. To increase precision, more breakpoints would be required. >>> theta = 2 * np.pi * np.linspace(0, 1, 5) >>> y = np.c_[np.cos(theta), np.sin(theta)] >>> cs = CubicSpline(theta, y, bc_type='periodic') >>> print("ds/dx={:.1f} ds/dy={:.1f}".format(cs(0, 1)[0], cs(0, 1)[1])) ds/dx=0.0 ds/dy=1.0 >>> xs = 2 * np.pi * np.linspace(0, 1, 100) >>> plt.figure(figsize=(6.5, 4)) >>> plt.plot(y[:, 0], y[:, 1], 'o', label='data') >>> plt.plot(np.cos(xs), np.sin(xs), label='true') >>> plt.plot(cs(xs)[:, 0], cs(xs)[:, 1], label='spline') >>> plt.axes().set_aspect('equal') >>> plt.legend(loc='center') >>> plt.show() The third example is the interpolation of a polynomial y = x**3 on the interval 0 <= x<= 1. A cubic spline can represent this function exactly. To achieve that we need to specify values and first derivatives at endpoints of the interval. Note that y' = 3 * x**2 and thus y'(0) = 0 and y'(1) = 3. >>> cs = CubicSpline([0, 1], [0, 1], bc_type=((1, 0), (1, 3))) >>> x = np.linspace(0, 1) >>> np.allclose(x**3, cs(x)) True References ---------- .. [1] `Cubic Spline Interpolation <https://en.wikiversity.org/wiki/Cubic_Spline_Interpolation>`_ on Wikiversity. .. [2] Carl de Boor, "A Practical Guide to Splines", Springer-Verlag, 1978. """ def __init__(self, x, y, axis=0, bc_type='not-a-knot', extrapolate=None): x, y = map(np.asarray, (x, y)) if np.issubdtype(x.dtype, np.complexfloating): raise ValueError("`x` must contain real values.") if np.issubdtype(y.dtype, np.complexfloating): dtype = complex else: dtype = float y = y.astype(dtype, copy=False) axis = axis % y.ndim if x.ndim != 1: raise ValueError("`x` must be 1-dimensional.") if x.shape[0] < 2: raise ValueError("`x` must contain at least 2 elements.") if x.shape[0] != y.shape[axis]: raise ValueError("The length of `y` along `axis`={0} doesn't " "match the length of `x`".format(axis)) if not np.all(np.isfinite(x)): raise ValueError("`x` must contain only finite values.") if not np.all(np.isfinite(y)): raise ValueError("`y` must contain only finite values.") dx = np.diff(x) if np.any(dx <= 0): raise ValueError("`x` must be strictly increasing sequence.") n = x.shape[0] y = np.rollaxis(y, axis) bc, y = self._validate_bc(bc_type, y, y.shape[1:], axis) if extrapolate is None: if bc[0] == 'periodic': extrapolate = 'periodic' else: extrapolate = True dxr = dx.reshape([dx.shape[0]] + [1] * (y.ndim - 1)) slope = np.diff(y, axis=0) / dxr # If bc is 'not-a-knot' this change is just a convention. # If bc is 'periodic' then we already checked that y[0] == y[-1], # and the spline is just a constant, we handle this case in the same # way by setting the first derivatives to slope, which is 0. if n == 2: if bc[0] in ['not-a-knot', 'periodic']: bc[0] = (1, slope[0]) if bc[1] in ['not-a-knot', 'periodic']: bc[1] = (1, slope[0]) # This is a very special case, when both conditions are 'not-a-knot' # and n == 3. In this case 'not-a-knot' can't be handled regularly # as the both conditions are identical. We handle this case by # constructing a parabola passing through given points. if n == 3 and bc[0] == 'not-a-knot' and bc[1] == 'not-a-knot': A = np.zeros((3, 3)) # This is a standard matrix. b = np.empty((3,) + y.shape[1:], dtype=y.dtype) A[0, 0] = 1 A[0, 1] = 1 A[1, 0] = dx[1] A[1, 1] = 2 * (dx[0] + dx[1]) A[1, 2] = dx[0] A[2, 1] = 1 A[2, 2] = 1 b[0] = 2 * slope[0] b[1] = 3 * (dxr[0] * slope[1] + dxr[1] * slope[0]) b[2] = 2 * slope[1] s = solve(A, b, overwrite_a=True, overwrite_b=True, check_finite=False) else: # Find derivative values at each x[i] by solving a tridiagonal # system. A = np.zeros((3, n)) # This is a banded matrix representation. b = np.empty((n,) + y.shape[1:], dtype=y.dtype) # Filling the system for i=1..n-2 # (x[i-1] - x[i]) * s[i-1] +\ # 2 * ((x[i] - x[i-1]) + (x[i+1] - x[i])) * s[i] +\ # (x[i] - x[i-1]) * s[i+1] =\ # 3 * ((x[i+1] - x[i])*(y[i] - y[i-1])/(x[i] - x[i-1]) +\ # (x[i] - x[i-1])*(y[i+1] - y[i])/(x[i+1] - x[i])) A[1, 1:-1] = 2 * (dx[:-1] + dx[1:]) # The diagonal A[0, 2:] = dx[:-1] # The upper diagonal A[-1, :-2] = dx[1:] # The lower diagonal b[1:-1] = 3 * (dxr[1:] * slope[:-1] + dxr[:-1] * slope[1:]) bc_start, bc_end = bc if bc_start == 'periodic': # Due to the periodicity, and because y[-1] = y[0], the linear # system has (n-1) unknowns/equations instead of n: A = A[:, 0:-1] A[1, 0] = 2 * (dx[-1] + dx[0]) A[0, 1] = dx[-1] b = b[:-1] # Also, due to the periodicity, the system is not tri-diagonal. # We need to compute a "condensed" matrix of shape (n-2, n-2). # See http://www.cfm.brown.edu/people/gk/chap6/node14.html for # more explanations. # The condensed matrix is obtained by removing the last column # and last row of the (n-1, n-1) system matrix. The removed # values are saved in scalar variables with the (n-1, n-1) # system matrix indices forming their names: a_m1_0 = dx[-2] # lower left corner value: A[-1, 0] a_m1_m2 = dx[-1] a_m1_m1 = 2 * (dx[-1] + dx[-2]) a_m2_m1 = dx[-2] a_0_m1 = dx[0] b[0] = 3 * (dxr[0] * slope[-1] + dxr[-1] * slope[0]) b[-1] = 3 * (dxr[-1] * slope[-2] + dxr[-2] * slope[-1]) Ac = A[:, :-1] b1 = b[:-1] b2 = np.zeros_like(b1) b2[0] = -a_0_m1 b2[-1] = -a_m2_m1 # s1 and s2 are the solutions of (n-2, n-2) system s1 = solve_banded((1, 1), Ac, b1, overwrite_ab=False, overwrite_b=False, check_finite=False) s2 = solve_banded((1, 1), Ac, b2, overwrite_ab=False, overwrite_b=False, check_finite=False) # computing the s[n-2] solution: s_m1 = ((b[-1] - a_m1_0 * s1[0] - a_m1_m2 * s1[-1]) / (a_m1_m1 + a_m1_0 * s2[0] + a_m1_m2 * s2[-1])) # s is the solution of the (n, n) system: s = np.empty((n,) + y.shape[1:], dtype=y.dtype) s[:-2] = s1 + s_m1 * s2 s[-2] = s_m1 s[-1] = s[0] else: if bc_start == 'not-a-knot': A[1, 0] = dx[1] A[0, 1] = x[2] - x[0] d = x[2] - x[0] b[0] = ((dxr[0] + 2*d) * dxr[1] * slope[0] + dxr[0]**2 * slope[1]) / d elif bc_start[0] == 1: A[1, 0] = 1 A[0, 1] = 0 b[0] = bc_start[1] elif bc_start[0] == 2: A[1, 0] = 2 * dx[0] A[0, 1] = dx[0] b[0] = -0.5 * bc_start[1] * dx[0]**2 + 3 * (y[1] - y[0]) if bc_end == 'not-a-knot': A[1, -1] = dx[-2] A[-1, -2] = x[-1] - x[-3] d = x[-1] - x[-3] b[-1] = ((dxr[-1]**2*slope[-2] + (2*d + dxr[-1])*dxr[-2]*slope[-1]) / d) elif bc_end[0] == 1: A[1, -1] = 1 A[-1, -2] = 0 b[-1] = bc_end[1] elif bc_end[0] == 2: A[1, -1] = 2 * dx[-1] A[-1, -2] = dx[-1] b[-1] = 0.5 * bc_end[1] * dx[-1]**2 + 3 * (y[-1] - y[-2]) s = solve_banded((1, 1), A, b, overwrite_ab=True, overwrite_b=True, check_finite=False) # Compute coefficients in PPoly form. t = (s[:-1] + s[1:] - 2 * slope) / dxr c = np.empty((4, n - 1) + y.shape[1:], dtype=t.dtype) c[0] = t / dxr c[1] = (slope - s[:-1]) / dxr - t c[2] = s[:-1] c[3] = y[:-1] super(CubicSpline, self).__init__(c, x, extrapolate=extrapolate) self.axis = axis @staticmethod def _validate_bc(bc_type, y, expected_deriv_shape, axis): """Validate and prepare boundary conditions. Returns ------- validated_bc : 2-tuple Boundary conditions for a curve start and end. y : ndarray y casted to complex dtype if one of the boundary conditions has complex dtype. """ if isinstance(bc_type, string_types): if bc_type == 'periodic': if not np.allclose(y[0], y[-1], rtol=1e-15, atol=1e-15): raise ValueError( "The first and last `y` point along axis {} must " "be identical (within machine precision) when " "bc_type='periodic'.".format(axis)) bc_type = (bc_type, bc_type) else: if len(bc_type) != 2: raise ValueError("`bc_type` must contain 2 elements to " "specify start and end conditions.") if 'periodic' in bc_type: raise ValueError("'periodic' `bc_type` is defined for both " "curve ends and cannot be used with other " "boundary conditions.") validated_bc = [] for bc in bc_type: if isinstance(bc, string_types): if bc == 'clamped': validated_bc.append((1, np.zeros(expected_deriv_shape))) elif bc == 'natural': validated_bc.append((2, np.zeros(expected_deriv_shape))) elif bc in ['not-a-knot', 'periodic']: validated_bc.append(bc) else: raise ValueError("bc_type={} is not allowed.".format(bc)) else: try: deriv_order, deriv_value = bc except Exception: raise ValueError("A specified derivative value must be " "given in the form (order, value).") if deriv_order not in [1, 2]: raise ValueError("The specified derivative order must " "be 1 or 2.") deriv_value = np.asarray(deriv_value) if deriv_value.shape != expected_deriv_shape: raise ValueError( "`deriv_value` shape {} is not the expected one {}." .format(deriv_value.shape, expected_deriv_shape)) if np.issubdtype(deriv_value.dtype, np.complexfloating): y = y.astype(complex, copy=False) validated_bc.append((deriv_order, deriv_value)) return validated_bc, y
gpl-3.0
JohnCEarls/DataDirac
datadirac/utils/stat.py
1
1978
import nipy.algorithms.statistics.empirical_pvalue as pval import pandas from collections import defaultdict def get_fdr_cutoffs( tsv_file, index='networks', alphas=[.05, .01], dec_places=2 ): for a in alphas: if a < .01: raise Exception("Alphas only go to .01, easy to fix, but I have bigger fish to fry") b6 = pandas.read_csv( tsv_file , sep='\t') b6.set_index(index) cutoffs = defaultdict(dict) for alpha in alphas: for c in b6.columns: if c != index: cutoff = pval.fdr_threshold(b6[c].values, alpha=alpha) cutoffs[c][("{:.%if}"%dec_places ).format(alpha)] = cutoff return cutoffs from rpy2.robjects.packages import importr import numpy as np from rpy2.robjects import rinterface, r, IntVector, FloatVector, StrVector def get_qval_table( tsv_file, index='networks'): b6 = pandas.read_csv( tsv_file , sep='\t') b6.set_index(index) b6_out = b6.copy() for c in b6.columns: if c != index: _, qvals = qvalues( b6[c].values ) b6_out[c][:] = qvals return b6_out def qvalues( pvals ): """ See (https://gist.github.com/JohnCEarls/050543dd2d7a403a7dd3) for instructions to setup system. """ qvalue_obj = importr("qvalue") base = importr('base') kw = {'pi0.method':'bootstrap'} res = qvalue_obj.qvalue(FloatVector(pvals), **kw) try: _, pi0_r, qv_r, pv_r, lam_r = res except ValueError: raise pi0 = np.array(pi0_r)[0] qv = np.array(qv_r) return pi0, qv if __name__ == "__main__": import json #co = get_fdr_cutoffs( '../black_6_go_4-joined-2014.02.13.01:19:21.tsv') #print json.dumps(co) #converted = get_qval_table( '../black_6_go_4-joined-2014.02.13.01:19:21.tsv') #converted.to_csv('../black_6_go_4-joined-qvals-2014.02.13.01:19:21.tsv', # sep='\t', index_label='networks') import numpy.random qvalues(np.random.randn(100))
gpl-3.0
mjudsp/Tsallis
sklearn/utils/tests/test_seq_dataset.py
47
2486
# Author: Tom Dupre la Tour <tom.dupre-la-tour@m4x.org> # # License: BSD 3 clause import numpy as np import scipy.sparse as sp from sklearn.utils.seq_dataset import ArrayDataset, CSRDataset from sklearn.datasets import load_iris from numpy.testing import assert_array_equal from nose.tools import assert_equal iris = load_iris() X = iris.data.astype(np.float64) y = iris.target.astype(np.float64) X_csr = sp.csr_matrix(X) sample_weight = np.arange(y.size, dtype=np.float64) def assert_csr_equal(X, Y): X.eliminate_zeros() Y.eliminate_zeros() assert_equal(X.shape[0], Y.shape[0]) assert_equal(X.shape[1], Y.shape[1]) assert_array_equal(X.data, Y.data) assert_array_equal(X.indices, Y.indices) assert_array_equal(X.indptr, Y.indptr) def test_seq_dataset(): dataset1 = ArrayDataset(X, y, sample_weight, seed=42) dataset2 = CSRDataset(X_csr.data, X_csr.indptr, X_csr.indices, y, sample_weight, seed=42) for dataset in (dataset1, dataset2): for i in range(5): # next sample xi_, yi, swi, idx = dataset._next_py() xi = sp.csr_matrix((xi_), shape=(1, X.shape[1])) assert_csr_equal(xi, X_csr[idx]) assert_equal(yi, y[idx]) assert_equal(swi, sample_weight[idx]) # random sample xi_, yi, swi, idx = dataset._random_py() xi = sp.csr_matrix((xi_), shape=(1, X.shape[1])) assert_csr_equal(xi, X_csr[idx]) assert_equal(yi, y[idx]) assert_equal(swi, sample_weight[idx]) def test_seq_dataset_shuffle(): dataset1 = ArrayDataset(X, y, sample_weight, seed=42) dataset2 = CSRDataset(X_csr.data, X_csr.indptr, X_csr.indices, y, sample_weight, seed=42) # not shuffled for i in range(5): _, _, _, idx1 = dataset1._next_py() _, _, _, idx2 = dataset2._next_py() assert_equal(idx1, i) assert_equal(idx2, i) for i in range(5): _, _, _, idx1 = dataset1._random_py() _, _, _, idx2 = dataset2._random_py() assert_equal(idx1, idx2) seed = 77 dataset1._shuffle_py(seed) dataset2._shuffle_py(seed) for i in range(5): _, _, _, idx1 = dataset1._next_py() _, _, _, idx2 = dataset2._next_py() assert_equal(idx1, idx2) _, _, _, idx1 = dataset1._random_py() _, _, _, idx2 = dataset2._random_py() assert_equal(idx1, idx2)
bsd-3-clause
klocey/Image-Analysis
python/using_scikit_image.py
4
1477
from matplotlib import pyplot as plt from skimage import data from skimage.feature import blob_dog, blob_log, blob_doh from math import sqrt from skimage.color import rgb2gray import numpy as np import cv2 from PIL import Image import sys import os mydir = os.path.expanduser("~/GitHub/Image-Analysis") # Read image image = cv2.imread(mydir + '/photos/test.jpg', cv2.IMREAD_COLOR) image = cv2.resize(image, (0,0), fx=0.2, fy=0.2) #image = data.hubble_deep_field()[0:500, 0:500] image_gray = rgb2gray(image) blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.1) # Compute radii in the 3rd column. blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2) blobs_dog = blob_dog(image_gray, max_sigma=30, threshold=.1) blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2) blobs_doh = blob_doh(image_gray, max_sigma=30, threshold=.01) blobs_list = [blobs_log, blobs_dog, blobs_doh] colors = ['yellow', 'lime', 'red'] titles = ['Laplacian of Gaussian', 'Difference of Gaussian', 'Determinant of Hessian'] sequence = zip(blobs_list, colors, titles) for blobs, color, title in sequence: fig, ax = plt.subplots(1, 1) ax.set_title(title) ax.imshow(image, interpolation='nearest') for blob in blobs: y, x, r = blob c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False) ax.add_patch(c) #plt.imshow(img, cmap=plt.cm.gray) plt.savefig(mydir + '/results/photos/using_scikit_image'+title+'.png') #plt.show()
mit
Aasmi/scikit-learn
sklearn/tests/test_common.py
127
7665
""" General tests for all estimators in sklearn. """ # Authors: Andreas Mueller <amueller@ais.uni-bonn.de> # Gael Varoquaux gael.varoquaux@normalesup.org # License: BSD 3 clause from __future__ import print_function import os import warnings import sys import pkgutil from sklearn.externals.six import PY3 from sklearn.utils.testing import assert_false, clean_warning_registry from sklearn.utils.testing import all_estimators from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_in from sklearn.utils.testing import ignore_warnings import sklearn from sklearn.cluster.bicluster import BiclusterMixin from sklearn.linear_model.base import LinearClassifierMixin from sklearn.utils.estimator_checks import ( _yield_all_checks, CROSS_DECOMPOSITION, check_parameters_default_constructible, check_class_weight_balanced_linear_classifier, check_transformer_n_iter, check_non_transformer_estimators_n_iter, check_get_params_invariance) def test_all_estimator_no_base_class(): # test that all_estimators doesn't find abstract classes. for name, Estimator in all_estimators(): msg = ("Base estimators such as {0} should not be included" " in all_estimators").format(name) assert_false(name.lower().startswith('base'), msg=msg) def test_all_estimators(): # Test that estimators are default-constructible, clonable # and have working repr. estimators = all_estimators(include_meta_estimators=True) # Meta sanity-check to make sure that the estimator introspection runs # properly assert_greater(len(estimators), 0) for name, Estimator in estimators: # some can just not be sensibly default constructed yield check_parameters_default_constructible, name, Estimator def test_non_meta_estimators(): # input validation etc for non-meta estimators estimators = all_estimators() for name, Estimator in estimators: if issubclass(Estimator, BiclusterMixin): continue if name.startswith("_"): continue for check in _yield_all_checks(name, Estimator): yield check, name, Estimator def test_configure(): # Smoke test the 'configure' step of setup, this tests all the # 'configure' functions in the setup.pys in the scikit cwd = os.getcwd() setup_path = os.path.abspath(os.path.join(sklearn.__path__[0], '..')) setup_filename = os.path.join(setup_path, 'setup.py') if not os.path.exists(setup_filename): return try: os.chdir(setup_path) old_argv = sys.argv sys.argv = ['setup.py', 'config'] clean_warning_registry() with warnings.catch_warnings(): # The configuration spits out warnings when not finding # Blas/Atlas development headers warnings.simplefilter('ignore', UserWarning) if PY3: with open('setup.py') as f: exec(f.read(), dict(__name__='__main__')) else: execfile('setup.py', dict(__name__='__main__')) finally: sys.argv = old_argv os.chdir(cwd) def test_class_weight_balanced_linear_classifiers(): classifiers = all_estimators(type_filter='classifier') clean_warning_registry() with warnings.catch_warnings(record=True): linear_classifiers = [ (name, clazz) for name, clazz in classifiers if 'class_weight' in clazz().get_params().keys() and issubclass(clazz, LinearClassifierMixin)] for name, Classifier in linear_classifiers: if name == "LogisticRegressionCV": # Contrary to RidgeClassifierCV, LogisticRegressionCV use actual # CV folds and fit a model for each CV iteration before averaging # the coef. Therefore it is expected to not behave exactly as the # other linear model. continue yield check_class_weight_balanced_linear_classifier, name, Classifier @ignore_warnings def test_import_all_consistency(): # Smoke test to check that any name in a __all__ list is actually defined # in the namespace of the module or package. pkgs = pkgutil.walk_packages(path=sklearn.__path__, prefix='sklearn.', onerror=lambda _: None) submods = [modname for _, modname, _ in pkgs] for modname in submods + ['sklearn']: if ".tests." in modname: continue package = __import__(modname, fromlist="dummy") for name in getattr(package, '__all__', ()): if getattr(package, name, None) is None: raise AttributeError( "Module '{0}' has no attribute '{1}'".format( modname, name)) def test_root_import_all_completeness(): EXCEPTIONS = ('utils', 'tests', 'base', 'setup') for _, modname, _ in pkgutil.walk_packages(path=sklearn.__path__, onerror=lambda _: None): if '.' in modname or modname.startswith('_') or modname in EXCEPTIONS: continue assert_in(modname, sklearn.__all__) def test_non_transformer_estimators_n_iter(): # Test that all estimators of type which are non-transformer # and which have an attribute of max_iter, return the attribute # of n_iter atleast 1. for est_type in ['regressor', 'classifier', 'cluster']: regressors = all_estimators(type_filter=est_type) for name, Estimator in regressors: # LassoLars stops early for the default alpha=1.0 for # the iris dataset. if name == 'LassoLars': estimator = Estimator(alpha=0.) else: estimator = Estimator() if hasattr(estimator, "max_iter"): # These models are dependent on external solvers like # libsvm and accessing the iter parameter is non-trivial. if name in (['Ridge', 'SVR', 'NuSVR', 'NuSVC', 'RidgeClassifier', 'SVC', 'RandomizedLasso', 'LogisticRegressionCV']): continue # Tested in test_transformer_n_iter below elif (name in CROSS_DECOMPOSITION or name in ['LinearSVC', 'LogisticRegression']): continue else: # Multitask models related to ENet cannot handle # if y is mono-output. yield (check_non_transformer_estimators_n_iter, name, estimator, 'Multi' in name) def test_transformer_n_iter(): transformers = all_estimators(type_filter='transformer') for name, Estimator in transformers: estimator = Estimator() # Dependent on external solvers and hence accessing the iter # param is non-trivial. external_solver = ['Isomap', 'KernelPCA', 'LocallyLinearEmbedding', 'RandomizedLasso', 'LogisticRegressionCV'] if hasattr(estimator, "max_iter") and name not in external_solver: yield check_transformer_n_iter, name, estimator def test_get_params_invariance(): # Test for estimators that support get_params, that # get_params(deep=False) is a subset of get_params(deep=True) # Related to issue #4465 estimators = all_estimators(include_meta_estimators=False, include_other=True) for name, Estimator in estimators: if hasattr(Estimator, 'get_params'): yield check_get_params_invariance, name, Estimator
bsd-3-clause
zhoulingjun/zipline
zipline/utils/tradingcalendar_tse.py
24
10413
# # Copyright 2014 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pandas as pd import pytz from datetime import datetime from dateutil import rrule from zipline.utils.tradingcalendar import end, canonicalize_datetime start = pd.Timestamp('1994-01-01', tz='UTC') def get_non_trading_days(start, end): non_trading_rules = [] start = canonicalize_datetime(start) end = canonicalize_datetime(end) weekends = rrule.rrule( rrule.YEARLY, byweekday=(rrule.SA, rrule.SU), cache=True, dtstart=start, until=end ) non_trading_rules.append(weekends) new_years = rrule.rrule( rrule.MONTHLY, byyearday=1, cache=True, dtstart=start, until=end ) non_trading_rules.append(new_years) new_years_sunday = rrule.rrule( rrule.MONTHLY, byyearday=2, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(new_years_sunday) new_years_saturday = rrule.rrule( rrule.MONTHLY, byyearday=3, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(new_years_saturday) # Family day in Ontario, starting in 2008, third monday of February family_day = rrule.rrule( rrule.MONTHLY, bymonth=2, byweekday=(rrule.MO(3)), cache=True, dtstart=datetime(2008, 1, 1, tzinfo=pytz.utc), until=end ) non_trading_rules.append(family_day) good_friday = rrule.rrule( rrule.DAILY, byeaster=-2, cache=True, dtstart=start, until=end ) non_trading_rules.append(good_friday) # Monday prior to May 25th. victoria_day = rrule.rrule( rrule.MONTHLY, bymonth=5, byweekday=rrule.MO, bymonthday=[24, 23, 22, 21, 20, 19, 18], cache=True, dtstart=start, until=end ) non_trading_rules.append(victoria_day) july_1st = rrule.rrule( rrule.MONTHLY, bymonth=7, bymonthday=1, cache=True, dtstart=start, until=end ) non_trading_rules.append(july_1st) july_1st_sunday = rrule.rrule( rrule.MONTHLY, bymonth=7, bymonthday=2, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(july_1st_sunday) july_1st_saturday = rrule.rrule( rrule.MONTHLY, bymonth=7, bymonthday=3, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(july_1st_saturday) civic_holiday = rrule.rrule( rrule.MONTHLY, bymonth=8, byweekday=rrule.MO(1), cache=True, dtstart=start, until=end ) non_trading_rules.append(civic_holiday) labor_day = rrule.rrule( rrule.MONTHLY, bymonth=9, byweekday=(rrule.MO(1)), cache=True, dtstart=start, until=end ) non_trading_rules.append(labor_day) thanksgiving = rrule.rrule( rrule.MONTHLY, bymonth=10, byweekday=(rrule.MO(2)), cache=True, dtstart=start, until=end ) non_trading_rules.append(thanksgiving) christmas = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=25, cache=True, dtstart=start, until=end ) non_trading_rules.append(christmas) # If Christmas is a Sunday then the 26th, a Monday is observed. # (but that would be boxing day), so the 27th is also observed. christmas_sunday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=27, byweekday=rrule.TU, cache=True, dtstart=start, until=end ) non_trading_rules.append(christmas_sunday) # If Christmas is a Saturday then the 27th, a monday is observed. christmas_saturday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=27, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(christmas_saturday) boxing_day = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=26, cache=True, dtstart=start, until=end ) non_trading_rules.append(boxing_day) # if boxing day is a sunday, the Christmas was saturday. # Christmas is observed on the 27th, a month and boxing day is observed # on the 28th, a tuesday. boxing_day_sunday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=28, byweekday=rrule.TU, cache=True, dtstart=start, until=end ) non_trading_rules.append(boxing_day_sunday) # If boxing day is a Saturday then the 28th, a monday is observed. boxing_day_saturday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=28, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(boxing_day_saturday) non_trading_ruleset = rrule.rruleset() for rule in non_trading_rules: non_trading_ruleset.rrule(rule) non_trading_days = non_trading_ruleset.between(start, end, inc=True) # Add September 11th closings # The TSX was open for 71 minutes on September 11, 2011. # It was closed on the 12th and reopened on the 13th. # http://www.cbc.ca/news2/interactives/map-tsx/ # # September 2001 # Su Mo Tu We Th Fr Sa # 1 # 2 3 4 5 6 7 8 # 9 10 11 12 13 14 15 # 16 17 18 19 20 21 22 # 23 24 25 26 27 28 29 # 30 non_trading_days.append( datetime(2001, 9, 12, tzinfo=pytz.utc)) non_trading_days.sort() return pd.DatetimeIndex(non_trading_days) non_trading_days = get_non_trading_days(start, end) trading_day = pd.tseries.offsets.CDay(holidays=non_trading_days) def get_trading_days(start, end, trading_day=trading_day): return pd.date_range(start=start.date(), end=end.date(), freq=trading_day).tz_localize('UTC') trading_days = get_trading_days(start, end) # Days in Environment but not in Calendar (using ^GSPTSE as bm_symbol): # -------------------------------------------------------------------- # Used http://web.tmxmoney.com/pricehistory.php?qm_page=61468&qm_symbol=^TSX # to check whether exchange was open on these days. # 1994-07-01 - July 1st, Yahoo Finance has Volume = 0 # 1996-07-01 - July 1st, Yahoo Finance has Volume = 0 # 1996-08-05 - Civic Holiday, Yahoo Finance has Volume = 0 # 1997-07-01 - July 1st, Yahoo Finance has Volume = 0 # 1997-08-04 - Civic Holiday, Yahoo Finance has Volume = 0 # 2001-05-21 - Victoria day, Yahoo Finance has Volume = 0 # 2004-10-11 - Closed, Thanksgiving - Confirmed closed # 2004-12-28 - Closed, Boxing Day - Confirmed closed # 2012-10-08 - Closed, Thanksgiving - Confirmed closed # Days in Calendar but not in Environment using ^GSPTSE as bm_symbol: # -------------------------------------------------------------------- # Used http://web.tmxmoney.com/pricehistory.php?qm_page=61468&qm_symbol=^TSX # to check whether exchange was open on these days. # 2000-06-28 - No data this far back, can't confirm # 2000-08-28 - No data this far back, can't confirm # 2000-08-29 - No data this far back, can't confirm # 2001-09-11 - TSE Open for 71 min. # 2002-02-01 - Confirm TSE Open # 2002-06-14 - Confirm TSE Open # 2002-07-02 - Confirm TSE Open # 2002-11-11 - TSX website has no data for 2 weeks in 2002 # 2003-07-07 - Confirm TSE Open # 2003-12-16 - Confirm TSE Open def get_early_closes(start, end): # TSX closed at 1:00 PM on december 24th. start = canonicalize_datetime(start) end = canonicalize_datetime(end) start = max(start, datetime(1993, 1, 1, tzinfo=pytz.utc)) end = max(end, datetime(1993, 1, 1, tzinfo=pytz.utc)) # Not included here are early closes prior to 1993 # or unplanned early closes early_close_rules = [] christmas_eve = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=24, byweekday=(rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR), cache=True, dtstart=start, until=end ) early_close_rules.append(christmas_eve) early_close_ruleset = rrule.rruleset() for rule in early_close_rules: early_close_ruleset.rrule(rule) early_closes = early_close_ruleset.between(start, end, inc=True) early_closes.sort() return pd.DatetimeIndex(early_closes) early_closes = get_early_closes(start, end) def get_open_and_closes(trading_days, early_closes, tz='US/Eastern'): open_and_closes = pd.DataFrame(index=trading_days, columns=('market_open', 'market_close')) for day in trading_days: market_open = pd.Timestamp( datetime( year=day.year, month=day.month, day=day.day, hour=9, minute=31), tz='US/Eastern').tz_convert('UTC') # 1 PM if early close, 4 PM otherwise close_hour = 13 if day in early_closes else 16 market_close = pd.Timestamp( datetime( year=day.year, month=day.month, day=day.day, hour=close_hour), tz='US/Eastern').tz_convert('UTC') open_and_closes.loc[day, 'market_open'] = market_open open_and_closes.loc[day, 'market_close'] = market_close return open_and_closes open_and_closes = get_open_and_closes(trading_days, early_closes)
apache-2.0
pravsripad/jumeg
examples/connectivity/plot_brain_connectome.py
3
1329
#!/usr/bin/env python ''' Plot connectivity on a glass brain using 'plot_connectome' function from Nilearn (https://nilearn.github.io/). Author: Praveen Sripad <pravsripad@gmail.com> ''' import numpy as np import mne from mne.datasets import sample from nilearn import plotting import nibabel as nib import matplotlib.pyplot as plt data_path = sample.data_path() subjects_dir = data_path + '/subjects' subject = 'fsaverage' aparc = mne.read_labels_from_annot(subject, subjects_dir=subjects_dir, parc='aparc') # nodes in one hemisphere can be plotted as well aparc_lh = [lab for lab in aparc if lab.hemi == 'lh'] coords = [] # plot 10 nodes from left hemisphere only for better viz for lab in aparc_lh[:10]: if lab.name is 'unknown-lh': continue # get the center of mass com = lab.center_of_mass('fsaverage') # obtain mni coordinated to the vertex from left hemi coords_ = mne.vertex_to_mni(com, hemis=0, subject=subject, subjects_dir=subjects_dir)[0] coords.append(coords_) n_nodes = np.array(coords).shape[0] # make a random connectivity matrix con = np.random.random((n_nodes, n_nodes)) con[np.diag_indices(5)] = 0. con[np.triu_indices(5, k=1)] = 0. con += con.T con[con < 0.6] = 0. # plot the connectome on a glass brain background plotting.plot_connectome(con, coords) plt.show()
bsd-3-clause
grundgruen/powerline
powerline/utils/global_calendar.py
2
2177
import pandas as pd from dateutil import rrule from zipline.utils.tradingcalendar import end, canonicalize_datetime __author__ = 'Warren' start = pd.Timestamp('2013-01-01', tz='UTC') end_base = pd.Timestamp('today', tz='UTC') start = canonicalize_datetime(start) end = canonicalize_datetime(end) weekends = rrule.rrule( rrule.YEARLY, byweekday=(rrule.SA, rrule.SU), cache=True, dtstart=start, until=end ) # New Year's Day new_year = rrule.rrule( rrule.MONTHLY, byyearday=1, cache=True, dtstart=start, until=end ) # Easter Monday easter_monday = rrule.rrule( rrule.DAILY, byeaster=1, cache=True, dtstart=start, until=end ) # Christi Himmelfahrt ch_himm = rrule.rrule( rrule.DAILY, byeaster=39, cache=True, dtstart=pd.Timestamp('2013-01-01', tz='UTC'), until=pd.Timestamp('2013-12-31', tz='UTC') ) # Pfingstmontag pfinst_mon_13 = rrule.rrule( rrule.DAILY, byeaster=50, cache=True, dtstart=pd.Timestamp('2013-01-01', tz='UTC'), until=pd.Timestamp('2013-12-31', tz='UTC') ) pfinst_mon_15 = rrule.rrule( rrule.DAILY, byeaster=50, cache=True, dtstart=pd.Timestamp('2015-01-01', tz='UTC'), ) # Labour Day (1st of May) may_bank = rrule.rrule( rrule.MONTHLY, bymonth=5, bymonthday=1, cache=True, dtstart=start, until=end ) # Tag der Deutschen Einheit tde = rrule.rrule( rrule.MONTHLY, bymonth=10, bymonthday=3, cache=True, dtstart=pd.Timestamp('2011-01-01', tz='UTC'), until=pd.Timestamp('2013-12-31', tz='UTC') ) # Christmas Eve christmas_eve = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=24, cache=True, dtstart=start, until=end ) # Christmas Day christmas = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=25, cache=True, dtstart=start, until=end ) # Boxing Day boxing_day = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=26, cache=True, dtstart=start, until=end ) # New Year's Eve newyears_eve = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=31, cache=True, dtstart=start, until=end )
apache-2.0
leesavide/pythonista-docs
Documentation/matplotlib/mpl_examples/pie_and_polar_charts/pie_demo_features.py
3
1070
""" Demo of a basic pie chart plus a few additional features. In addition to the basic pie chart, this demo shows a few optional features: * slice labels * auto-labeling the percentage * offsetting a slice with "explode" * drop-shadow * custom start angle Note about the custom start angle: The default ``startangle`` is 0, which would start the "Frogs" slice on the positive x-axis. This example sets ``startangle = 90`` such that everything is rotated counter-clockwise by 90 degrees, and the frog slice starts on the positive y-axis. """ import matplotlib.pyplot as plt # The slices will be ordered and plotted counter-clockwise. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) # Set aspect ratio to be equal so that pie is drawn as a circle. plt.axis('equal') plt.show()
apache-2.0
AlexandreAbraham/brainhack2013
brainhack/datasets.py
1
2280
import os from nilearn.datasets import _get_dataset, _fetch_dataset from sklearn.datasets.base import Bunch def fetch_craddock_2012_test(n_subjects=None, data_dir=None, resume=True, verbose=0): """Download and load example data from Craddock 2012 work. Parameters ---------- n_subjects: int, optional The number of subjects to load. If None is given, all the 3 subjects are used. data_dir: string, optional Path of the data directory. Used to force data storage in a specified location. Default: None resume: boolean, optional Indicate if dataset fetching can be resumed from previous attempt. Returns ------- data: sklearn.datasets.base.Bunch Dictionary-like object, the interest attributes are : - 'func': string list. Paths to functional images - 'mask': string. Path to nifti mask file. References ---------- `A whole brain fMRI atlas generated via spatially constrained spectral clustering <http://www.ncbi.nlm.nih.gov/pubmed/21769991>`_ Craddock, R. C., James, G. A., Holtzheimer, P. E., Hu, X. P. & Mayberg, H. S. , Human Brain Mapping, 2012, 33, 1914-1928 doi: 10.1002/hbm.21333. Notes ----- Cameron Craddock provides his code for this work: https://github.com/ccraddock/cluster_roi """ # Dataset files file_names = ['gm_maskfile.nii.gz', 'subject1.nii.gz', 'subject2.nii.gz', 'subject3.nii.gz'] file_names = [os.path.join('pyClusterROI', fn) for fn in file_names] # load the dataset try: # Try to load the dataset files = _get_dataset("craddock_2012_test", file_names, data_dir=data_dir) except IOError: # If the dataset does not exists, we download it url = 'ftp://www.nitrc.org/home/groups/cluster_roi/htdocs/pyClusterROI/pyClusterROI_testdata.1.0.tar.gz' _fetch_dataset('craddock_2012_test', [url], data_dir=data_dir, resume=resume, verbose=verbose) files = _get_dataset('craddock_2012_test', file_names, data_dir=data_dir) # return the data return Bunch(mask=files[0], func=files[1:n_subjects])
bsd-3-clause
rbalda/neural_ocr
env/lib/python2.7/site-packages/matplotlib/backends/backend_pgf.py
7
36822
from __future__ import (absolute_import, division, print_function, unicode_literals) from matplotlib.externals import six import math import os import sys import errno import re import shutil import tempfile import codecs import atexit import weakref import warnings import numpy as np import matplotlib as mpl from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\ FigureManagerBase, FigureCanvasBase from matplotlib.backends.backend_mixed import MixedModeRenderer from matplotlib.figure import Figure from matplotlib.text import Text from matplotlib.path import Path from matplotlib import _png, rcParams from matplotlib.cbook import is_string_like, is_writable_file_like from matplotlib.compat import subprocess from matplotlib.compat.subprocess import check_output ############################################################################### # create a list of system fonts, all of these should work with xe/lua-latex system_fonts = [] if sys.platform.startswith('win'): from matplotlib import font_manager from matplotlib.ft2font import FT2Font for f in font_manager.win32InstalledFonts(): try: system_fonts.append(FT2Font(str(f)).family_name) except: pass # unknown error, skip this font else: # assuming fontconfig is installed and the command 'fc-list' exists try: # list scalable (non-bitmap) fonts fc_list = check_output(['fc-list', ':outline,scalable', 'family']) fc_list = fc_list.decode('utf8') system_fonts = [f.split(',')[0] for f in fc_list.splitlines()] system_fonts = list(set(system_fonts)) except: warnings.warn('error getting fonts from fc-list', UserWarning) def get_texcommand(): """Get chosen TeX system from rc.""" texsystem_options = ["xelatex", "lualatex", "pdflatex"] texsystem = rcParams.get("pgf.texsystem", "xelatex") return texsystem if texsystem in texsystem_options else "xelatex" def get_fontspec(): """Build fontspec preamble from rc.""" latex_fontspec = [] texcommand = get_texcommand() if texcommand != "pdflatex": latex_fontspec.append("\\usepackage{fontspec}") if texcommand != "pdflatex" and rcParams.get("pgf.rcfonts", True): # try to find fonts from rc parameters families = ["serif", "sans-serif", "monospace"] fontspecs = [r"\setmainfont{%s}", r"\setsansfont{%s}", r"\setmonofont{%s}"] for family, fontspec in zip(families, fontspecs): matches = [f for f in rcParams["font." + family] if f in system_fonts] if matches: latex_fontspec.append(fontspec % matches[0]) else: pass # no fonts found, fallback to LaTeX defaule return "\n".join(latex_fontspec) def get_preamble(): """Get LaTeX preamble from rc.""" latex_preamble = rcParams.get("pgf.preamble", "") if type(latex_preamble) == list: latex_preamble = "\n".join(latex_preamble) return latex_preamble ############################################################################### # This almost made me cry!!! # In the end, it's better to use only one unit for all coordinates, since the # arithmetic in latex seems to produce inaccurate conversions. latex_pt_to_in = 1. / 72.27 latex_in_to_pt = 1. / latex_pt_to_in mpl_pt_to_in = 1. / 72. mpl_in_to_pt = 1. / mpl_pt_to_in ############################################################################### # helper functions NO_ESCAPE = r"(?<!\\)(?:\\\\)*" re_mathsep = re.compile(NO_ESCAPE + r"\$") re_escapetext = re.compile(NO_ESCAPE + "([_^$%])") repl_escapetext = lambda m: "\\" + m.group(1) re_mathdefault = re.compile(NO_ESCAPE + r"(\\mathdefault)") repl_mathdefault = lambda m: m.group(0)[:-len(m.group(1))] def common_texification(text): """ Do some necessary and/or useful substitutions for texts to be included in LaTeX documents. """ # Sometimes, matplotlib adds the unknown command \mathdefault. # Not using \mathnormal instead since this looks odd for the latex cm font. text = re_mathdefault.sub(repl_mathdefault, text) # split text into normaltext and inline math parts parts = re_mathsep.split(text) for i, s in enumerate(parts): if not i % 2: # textmode replacements s = re_escapetext.sub(repl_escapetext, s) else: # mathmode replacements s = r"\(\displaystyle %s\)" % s parts[i] = s return "".join(parts) def writeln(fh, line): # every line of a file included with \input must be terminated with % # if not, latex will create additional vertical spaces for some reason fh.write(line) fh.write("%\n") def _font_properties_str(prop): # translate font properties to latex commands, return as string commands = [] families = {"serif": r"\rmfamily", "sans": r"\sffamily", "sans-serif": r"\sffamily", "monospace": r"\ttfamily"} family = prop.get_family()[0] if family in families: commands.append(families[family]) elif family in system_fonts and get_texcommand() != "pdflatex": commands.append(r"\setmainfont{%s}\rmfamily" % family) else: pass # print warning? size = prop.get_size_in_points() commands.append(r"\fontsize{%f}{%f}" % (size, size * 1.2)) styles = {"normal": r"", "italic": r"\itshape", "oblique": r"\slshape"} commands.append(styles[prop.get_style()]) boldstyles = ["semibold", "demibold", "demi", "bold", "heavy", "extra bold", "black"] if prop.get_weight() in boldstyles: commands.append(r"\bfseries") commands.append(r"\selectfont") return "".join(commands) def make_pdf_to_png_converter(): """ Returns a function that converts a pdf file to a png file. """ tools_available = [] # check for pdftocairo try: check_output(["pdftocairo", "-v"], stderr=subprocess.STDOUT) tools_available.append("pdftocairo") except: pass # check for ghostscript gs, ver = mpl.checkdep_ghostscript() if gs: tools_available.append("gs") # pick converter if "pdftocairo" in tools_available: def cairo_convert(pdffile, pngfile, dpi): cmd = ["pdftocairo", "-singlefile", "-png", "-r %d" % dpi, pdffile, os.path.splitext(pngfile)[0]] # for some reason this doesn't work without shell check_output(" ".join(cmd), shell=True, stderr=subprocess.STDOUT) return cairo_convert elif "gs" in tools_available: def gs_convert(pdffile, pngfile, dpi): cmd = [gs, '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT', '-sDEVICE=png16m', '-dUseCIEColor', '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4', '-dDOINTERPOLATE', '-sOutputFile=%s' % pngfile, '-r%d' % dpi, pdffile] check_output(cmd, stderr=subprocess.STDOUT) return gs_convert else: raise RuntimeError("No suitable pdf to png renderer found.") class LatexError(Exception): def __init__(self, message, latex_output=""): Exception.__init__(self, message) self.latex_output = latex_output class LatexManagerFactory(object): previous_instance = None @staticmethod def get_latex_manager(): texcommand = get_texcommand() latex_header = LatexManager._build_latex_header() prev = LatexManagerFactory.previous_instance # check if the previous instance of LatexManager can be reused if prev and prev.latex_header == latex_header and prev.texcommand == texcommand: if rcParams.get("pgf.debug", False): print("reusing LatexManager") return prev else: if rcParams.get("pgf.debug", False): print("creating LatexManager") new_inst = LatexManager() LatexManagerFactory.previous_instance = new_inst return new_inst class WeakSet(object): # TODO: Poor man's weakref.WeakSet. # Remove this once python 2.6 support is dropped from matplotlib. def __init__(self): self.weak_key_dict = weakref.WeakKeyDictionary() def add(self, item): self.weak_key_dict[item] = None def discard(self, item): if item in self.weak_key_dict: del self.weak_key_dict[item] def __iter__(self): return six.iterkeys(self.weak_key_dict) class LatexManager(object): """ The LatexManager opens an instance of the LaTeX application for determining the metrics of text elements. The LaTeX environment can be modified by setting fonts and/or a custem preamble in the rc parameters. """ _unclean_instances = WeakSet() @staticmethod def _build_latex_header(): latex_preamble = get_preamble() latex_fontspec = get_fontspec() # Create LaTeX header with some content, else LaTeX will load some # math fonts later when we don't expect the additional output on stdout. # TODO: is this sufficient? latex_header = [r"\documentclass{minimal}", latex_preamble, latex_fontspec, r"\begin{document}", r"text $math \mu$", # force latex to load fonts now r"\typeout{pgf_backend_query_start}"] return "\n".join(latex_header) @staticmethod def _cleanup_remaining_instances(): unclean_instances = list(LatexManager._unclean_instances) for latex_manager in unclean_instances: latex_manager._cleanup() def _stdin_writeln(self, s): self.latex_stdin_utf8.write(s) self.latex_stdin_utf8.write("\n") self.latex_stdin_utf8.flush() def _expect(self, s): exp = s.encode("utf8") buf = bytearray() while True: b = self.latex.stdout.read(1) buf += b if buf[-len(exp):] == exp: break if not len(b): raise LatexError("LaTeX process halted", buf.decode("utf8")) return buf.decode("utf8") def _expect_prompt(self): return self._expect("\n*") def __init__(self): # store references for __del__ self._os_path = os.path self._shutil = shutil self._debug = rcParams.get("pgf.debug", False) # create a tmp directory for running latex, remember to cleanup self.tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_lm_") LatexManager._unclean_instances.add(self) # test the LaTeX setup to ensure a clean startup of the subprocess self.texcommand = get_texcommand() self.latex_header = LatexManager._build_latex_header() latex_end = "\n\\makeatletter\n\\@@end\n" try: latex = subprocess.Popen([self.texcommand, "-halt-on-error"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=self.tmpdir) except OSError as e: if e.errno == errno.ENOENT: raise RuntimeError("Latex command not found. " "Install '%s' or change pgf.texsystem to the desired command." % self.texcommand ) else: raise RuntimeError("Error starting process '%s'" % self.texcommand) test_input = self.latex_header + latex_end stdout, stderr = latex.communicate(test_input.encode("utf-8")) if latex.returncode != 0: raise LatexError("LaTeX returned an error, probably missing font or error in preamble:\n%s" % stdout) # open LaTeX process for real work latex = subprocess.Popen([self.texcommand, "-halt-on-error"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=self.tmpdir) self.latex = latex self.latex_stdin_utf8 = codecs.getwriter("utf8")(self.latex.stdin) # write header with 'pgf_backend_query_start' token self._stdin_writeln(self._build_latex_header()) # read all lines until our 'pgf_backend_query_start' token appears self._expect("*pgf_backend_query_start") self._expect_prompt() # cache for strings already processed self.str_cache = {} def _cleanup(self): if not self._os_path.isdir(self.tmpdir): return try: self.latex.communicate() self.latex_stdin_utf8.close() self.latex.stdout.close() except: pass try: self._shutil.rmtree(self.tmpdir) LatexManager._unclean_instances.discard(self) except: sys.stderr.write("error deleting tmp directory %s\n" % self.tmpdir) def __del__(self): if self._debug: print("deleting LatexManager") self._cleanup() def get_width_height_descent(self, text, prop): """ Get the width, total height and descent for a text typesetted by the current LaTeX environment. """ # apply font properties and define textbox prop_cmds = _font_properties_str(prop) textbox = "\\sbox0{%s %s}" % (prop_cmds, text) # check cache if textbox in self.str_cache: return self.str_cache[textbox] # send textbox to LaTeX and wait for prompt self._stdin_writeln(textbox) try: self._expect_prompt() except LatexError as e: msg = "Error processing '%s'\nLaTeX Output:\n%s" raise ValueError(msg % (text, e.latex_output)) # typeout width, height and text offset of the last textbox self._stdin_writeln(r"\typeout{\the\wd0,\the\ht0,\the\dp0}") # read answer from latex and advance to the next prompt try: answer = self._expect_prompt() except LatexError as e: msg = "Error processing '%s'\nLaTeX Output:\n%s" raise ValueError(msg % (text, e.latex_output)) # parse metrics from the answer string try: width, height, offset = answer.splitlines()[0].split(",") except: msg = "Error processing '%s'\nLaTeX Output:\n%s" % (text, answer) raise ValueError(msg) w, h, o = float(width[:-2]), float(height[:-2]), float(offset[:-2]) # the height returned from LaTeX goes from base to top. # the height matplotlib expects goes from bottom to top. self.str_cache[textbox] = (w, h + o, o) return w, h + o, o class RendererPgf(RendererBase): def __init__(self, figure, fh, dummy=False): """ Creates a new PGF renderer that translates any drawing instruction into text commands to be interpreted in a latex pgfpicture environment. Attributes: * figure: Matplotlib figure to initialize height, width and dpi from. * fh: File handle for the output of the drawing commands. """ RendererBase.__init__(self) self.dpi = figure.dpi self.fh = fh self.figure = figure self.image_counter = 0 # get LatexManager instance self.latexManager = LatexManagerFactory.get_latex_manager() if dummy: # dummy==True deactivate all methods nop = lambda *args, **kwargs: None for m in RendererPgf.__dict__.keys(): if m.startswith("draw_"): self.__dict__[m] = nop else: # if fh does not belong to a filename, deactivate draw_image if not hasattr(fh, 'name') or not os.path.exists(fh.name): warnings.warn("streamed pgf-code does not support raster " "graphics, consider using the pgf-to-pdf option", UserWarning) self.__dict__["draw_image"] = lambda *args, **kwargs: None def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None): writeln(self.fh, r"\begin{pgfscope}") # convert from display units to in f = 1. / self.dpi # set style and clip self._print_pgf_clip(gc) self._print_pgf_path_styles(gc, rgbFace) # build marker definition bl, tr = marker_path.get_extents(marker_trans).get_points() coords = bl[0] * f, bl[1] * f, tr[0] * f, tr[1] * f writeln(self.fh, r"\pgfsys@defobject{currentmarker}{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{" % coords) self._print_pgf_path(None, marker_path, marker_trans) self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0, fill=rgbFace is not None) writeln(self.fh, r"}") # draw marker for each vertex for point, code in path.iter_segments(trans, simplify=False): x, y = point[0] * f, point[1] * f writeln(self.fh, r"\begin{pgfscope}") writeln(self.fh, r"\pgfsys@transformshift{%fin}{%fin}" % (x, y)) writeln(self.fh, r"\pgfsys@useobject{currentmarker}{}") writeln(self.fh, r"\end{pgfscope}") writeln(self.fh, r"\end{pgfscope}") def draw_path(self, gc, path, transform, rgbFace=None): writeln(self.fh, r"\begin{pgfscope}") # draw the path self._print_pgf_clip(gc) self._print_pgf_path_styles(gc, rgbFace) self._print_pgf_path(gc, path, transform, rgbFace) self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0, fill=rgbFace is not None) writeln(self.fh, r"\end{pgfscope}") # if present, draw pattern on top if gc.get_hatch(): writeln(self.fh, r"\begin{pgfscope}") self._print_pgf_path_styles(gc, rgbFace) # combine clip and path for clipping self._print_pgf_clip(gc) self._print_pgf_path(gc, path, transform, rgbFace) writeln(self.fh, r"\pgfusepath{clip}") # build pattern definition writeln(self.fh, r"\pgfsys@defobject{currentpattern}{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}{") writeln(self.fh, r"\begin{pgfscope}") writeln(self.fh, r"\pgfpathrectangle{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}") writeln(self.fh, r"\pgfusepath{clip}") scale = mpl.transforms.Affine2D().scale(self.dpi) self._print_pgf_path(None, gc.get_hatch_path(), scale) self._pgf_path_draw(stroke=True) writeln(self.fh, r"\end{pgfscope}") writeln(self.fh, r"}") # repeat pattern, filling the bounding rect of the path f = 1. / self.dpi (xmin, ymin), (xmax, ymax) = path.get_extents(transform).get_points() xmin, xmax = f * xmin, f * xmax ymin, ymax = f * ymin, f * ymax repx, repy = int(math.ceil(xmax-xmin)), int(math.ceil(ymax-ymin)) writeln(self.fh, r"\pgfsys@transformshift{%fin}{%fin}" % (xmin, ymin)) for iy in range(repy): for ix in range(repx): writeln(self.fh, r"\pgfsys@useobject{currentpattern}{}") writeln(self.fh, r"\pgfsys@transformshift{1in}{0in}") writeln(self.fh, r"\pgfsys@transformshift{-%din}{0in}" % repx) writeln(self.fh, r"\pgfsys@transformshift{0in}{1in}") writeln(self.fh, r"\end{pgfscope}") def _print_pgf_clip(self, gc): f = 1. / self.dpi # check for clip box bbox = gc.get_clip_rectangle() if bbox: p1, p2 = bbox.get_points() w, h = p2 - p1 coords = p1[0] * f, p1[1] * f, w * f, h * f writeln(self.fh, r"\pgfpathrectangle{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}} " % coords) writeln(self.fh, r"\pgfusepath{clip}") # check for clip path clippath, clippath_trans = gc.get_clip_path() if clippath is not None: self._print_pgf_path(gc, clippath, clippath_trans) writeln(self.fh, r"\pgfusepath{clip}") def _print_pgf_path_styles(self, gc, rgbFace): # cap style capstyles = {"butt": r"\pgfsetbuttcap", "round": r"\pgfsetroundcap", "projecting": r"\pgfsetrectcap"} writeln(self.fh, capstyles[gc.get_capstyle()]) # join style joinstyles = {"miter": r"\pgfsetmiterjoin", "round": r"\pgfsetroundjoin", "bevel": r"\pgfsetbeveljoin"} writeln(self.fh, joinstyles[gc.get_joinstyle()]) # filling has_fill = rgbFace is not None if gc.get_forced_alpha(): fillopacity = strokeopacity = gc.get_alpha() else: strokeopacity = gc.get_rgb()[3] fillopacity = rgbFace[3] if has_fill and len(rgbFace) > 3 else 1.0 if has_fill: writeln(self.fh, r"\definecolor{currentfill}{rgb}{%f,%f,%f}" % tuple(rgbFace[:3])) writeln(self.fh, r"\pgfsetfillcolor{currentfill}") if has_fill and fillopacity != 1.0: writeln(self.fh, r"\pgfsetfillopacity{%f}" % fillopacity) # linewidth and color lw = gc.get_linewidth() * mpl_pt_to_in * latex_in_to_pt stroke_rgba = gc.get_rgb() writeln(self.fh, r"\pgfsetlinewidth{%fpt}" % lw) writeln(self.fh, r"\definecolor{currentstroke}{rgb}{%f,%f,%f}" % stroke_rgba[:3]) writeln(self.fh, r"\pgfsetstrokecolor{currentstroke}") if strokeopacity != 1.0: writeln(self.fh, r"\pgfsetstrokeopacity{%f}" % strokeopacity) # line style dash_offset, dash_list = gc.get_dashes() if dash_list is None: writeln(self.fh, r"\pgfsetdash{}{0pt}") else: dash_str = r"\pgfsetdash{" for dash in dash_list: dash_str += r"{%fpt}" % dash dash_str += r"}{%fpt}" % dash_offset writeln(self.fh, dash_str) def _print_pgf_path(self, gc, path, transform, rgbFace=None): f = 1. / self.dpi # check for clip box / ignore clip for filled paths bbox = gc.get_clip_rectangle() if gc else None if bbox and (rgbFace is None): p1, p2 = bbox.get_points() clip = (p1[0], p1[1], p2[0], p2[1]) else: clip = None # build path for points, code in path.iter_segments(transform, clip=clip): if code == Path.MOVETO: x, y = tuple(points) writeln(self.fh, r"\pgfpathmoveto{\pgfqpoint{%fin}{%fin}}" % (f * x, f * y)) elif code == Path.CLOSEPOLY: writeln(self.fh, r"\pgfpathclose") elif code == Path.LINETO: x, y = tuple(points) writeln(self.fh, r"\pgfpathlineto{\pgfqpoint{%fin}{%fin}}" % (f * x, f * y)) elif code == Path.CURVE3: cx, cy, px, py = tuple(points) coords = cx * f, cy * f, px * f, py * f writeln(self.fh, r"\pgfpathquadraticcurveto{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}" % coords) elif code == Path.CURVE4: c1x, c1y, c2x, c2y, px, py = tuple(points) coords = c1x * f, c1y * f, c2x * f, c2y * f, px * f, py * f writeln(self.fh, r"\pgfpathcurveto{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}" % coords) def _pgf_path_draw(self, stroke=True, fill=False): actions = [] if stroke: actions.append("stroke") if fill: actions.append("fill") writeln(self.fh, r"\pgfusepath{%s}" % ",".join(actions)) def draw_image(self, gc, x, y, im): # TODO: Almost no documentation for the behavior of this function. # Something missing? # save the images to png files path = os.path.dirname(self.fh.name) fname = os.path.splitext(os.path.basename(self.fh.name))[0] fname_img = "%s-img%d.png" % (fname, self.image_counter) self.image_counter += 1 _png.write_png(np.array(im)[::-1], os.path.join(path, fname_img)) # reference the image in the pgf picture writeln(self.fh, r"\begin{pgfscope}") self._print_pgf_clip(gc) h, w = im.get_size_out() f = 1. / self.dpi # from display coords to inch writeln(self.fh, r"\pgftext[at=\pgfqpoint{%fin}{%fin},left,bottom]{\pgfimage[interpolate=true,width=%fin,height=%fin]{%s}}" % (x * f, y * f, w * f, h * f, fname_img)) writeln(self.fh, r"\end{pgfscope}") def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None): self.draw_text(gc, x, y, s, prop, angle, ismath, mtext) def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): # prepare string for tex s = common_texification(s) prop_cmds = _font_properties_str(prop) s = r"%s %s" % (prop_cmds, s) writeln(self.fh, r"\begin{pgfscope}") alpha = gc.get_alpha() if alpha != 1.0: writeln(self.fh, r"\pgfsetfillopacity{%f}" % alpha) writeln(self.fh, r"\pgfsetstrokeopacity{%f}" % alpha) rgb = tuple(gc.get_rgb())[:3] if rgb != (0, 0, 0): writeln(self.fh, r"\definecolor{textcolor}{rgb}{%f,%f,%f}" % rgb) writeln(self.fh, r"\pgfsetstrokecolor{textcolor}") writeln(self.fh, r"\pgfsetfillcolor{textcolor}") s = r"\color{textcolor}" + s f = 1.0 / self.figure.dpi text_args = [] if mtext and (angle == 0 or mtext.get_rotation_mode() == "anchor"): # if text anchoring can be supported, get the original coordinates # and add alignment information x, y = mtext.get_transform().transform_point(mtext.get_position()) text_args.append("x=%fin" % (x * f)) text_args.append("y=%fin" % (y * f)) halign = {"left": "left", "right": "right", "center": ""} valign = {"top": "top", "bottom": "bottom", "baseline": "base", "center": ""} text_args.append(halign[mtext.get_ha()]) text_args.append(valign[mtext.get_va()]) else: # if not, use the text layout provided by matplotlib text_args.append("x=%fin" % (x * f)) text_args.append("y=%fin" % (y * f)) text_args.append("left") text_args.append("base") if angle != 0: text_args.append("rotate=%f" % angle) writeln(self.fh, r"\pgftext[%s]{%s}" % (",".join(text_args), s)) writeln(self.fh, r"\end{pgfscope}") def get_text_width_height_descent(self, s, prop, ismath): # check if the math is supposed to be displaystyled s = common_texification(s) # get text metrics in units of latex pt, convert to display units w, h, d = self.latexManager.get_width_height_descent(s, prop) # TODO: this should be latex_pt_to_in instead of mpl_pt_to_in # but having a little bit more space around the text looks better, # plus the bounding box reported by LaTeX is VERY narrow f = mpl_pt_to_in * self.dpi return w * f, h * f, d * f def flipy(self): return False def get_canvas_width_height(self): return self.figure.get_figwidth(), self.figure.get_figheight() def points_to_pixels(self, points): return points * mpl_pt_to_in * self.dpi def new_gc(self): return GraphicsContextPgf() class GraphicsContextPgf(GraphicsContextBase): pass ######################################################################## def draw_if_interactive(): pass def new_figure_manager(num, *args, **kwargs): """ Create a new figure manager instance """ # if a main-level app must be created, this is the usual place to # do it -- see backend_wx, backend_wxagg and backend_tkagg for # examples. Not all GUIs require explicit instantiation of a # main-level app (egg backend_gtk, backend_gtkagg) for pylab FigureClass = kwargs.pop('FigureClass', Figure) thisFig = FigureClass(*args, **kwargs) return new_figure_manager_given_figure(num, thisFig) def new_figure_manager_given_figure(num, figure): """ Create a new figure manager instance for the given figure. """ canvas = FigureCanvasPgf(figure) manager = FigureManagerPgf(canvas, num) return manager class TmpDirCleaner(object): remaining_tmpdirs = set() @staticmethod def add(tmpdir): TmpDirCleaner.remaining_tmpdirs.add(tmpdir) @staticmethod def cleanup_remaining_tmpdirs(): for tmpdir in TmpDirCleaner.remaining_tmpdirs: try: shutil.rmtree(tmpdir) except: sys.stderr.write("error deleting tmp directory %s\n" % tmpdir) class FigureCanvasPgf(FigureCanvasBase): filetypes = {"pgf": "LaTeX PGF picture", "pdf": "LaTeX compiled PGF picture", "png": "Portable Network Graphics", } def get_default_filetype(self): return 'pdf' def _print_pgf_to_fh(self, fh, *args, **kwargs): if kwargs.get("dryrun", False): renderer = RendererPgf(self.figure, None, dummy=True) self.figure.draw(renderer) return header_text = """%% Creator: Matplotlib, PGF backend %% %% To include the figure in your LaTeX document, write %% \\input{<filename>.pgf} %% %% Make sure the required packages are loaded in your preamble %% \\usepackage{pgf} %% %% Figures using additional raster images can only be included by \input if %% they are in the same directory as the main LaTeX file. For loading figures %% from other directories you can use the `import` package %% \\usepackage{import} %% and then include the figures with %% \\import{<path to file>}{<filename>.pgf} %% """ # append the preamble used by the backend as a comment for debugging header_info_preamble = ["%% Matplotlib used the following preamble"] for line in get_preamble().splitlines(): header_info_preamble.append("%% " + line) for line in get_fontspec().splitlines(): header_info_preamble.append("%% " + line) header_info_preamble.append("%%") header_info_preamble = "\n".join(header_info_preamble) # get figure size in inch w, h = self.figure.get_figwidth(), self.figure.get_figheight() dpi = self.figure.get_dpi() # create pgfpicture environment and write the pgf code fh.write(header_text) fh.write(header_info_preamble) fh.write("\n") writeln(fh, r"\begingroup") writeln(fh, r"\makeatletter") writeln(fh, r"\begin{pgfpicture}") writeln(fh, r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}" % (w, h)) writeln(fh, r"\pgfusepath{use as bounding box, clip}") _bbox_inches_restore = kwargs.pop("bbox_inches_restore", None) renderer = MixedModeRenderer(self.figure, w, h, dpi, RendererPgf(self.figure, fh), bbox_inches_restore=_bbox_inches_restore) self.figure.draw(renderer) # end the pgfpicture environment writeln(fh, r"\end{pgfpicture}") writeln(fh, r"\makeatother") writeln(fh, r"\endgroup") def print_pgf(self, fname_or_fh, *args, **kwargs): """ Output pgf commands for drawing the figure so it can be included and rendered in latex documents. """ if kwargs.get("dryrun", False): self._print_pgf_to_fh(None, *args, **kwargs) return # figure out where the pgf is to be written to if is_string_like(fname_or_fh): with codecs.open(fname_or_fh, "w", encoding="utf-8") as fh: self._print_pgf_to_fh(fh, *args, **kwargs) elif is_writable_file_like(fname_or_fh): fh = codecs.getwriter("utf-8")(fname_or_fh) self._print_pgf_to_fh(fh, *args, **kwargs) else: raise ValueError("filename must be a path") def _print_pdf_to_fh(self, fh, *args, **kwargs): w, h = self.figure.get_figwidth(), self.figure.get_figheight() try: # create temporary directory for compiling the figure tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_") fname_pgf = os.path.join(tmpdir, "figure.pgf") fname_tex = os.path.join(tmpdir, "figure.tex") fname_pdf = os.path.join(tmpdir, "figure.pdf") # print figure to pgf and compile it with latex self.print_pgf(fname_pgf, *args, **kwargs) latex_preamble = get_preamble() latex_fontspec = get_fontspec() latexcode = """ \\documentclass[12pt]{minimal} \\usepackage[paperwidth=%fin, paperheight=%fin, margin=0in]{geometry} %s %s \\usepackage{pgf} \\begin{document} \\centering \\input{figure.pgf} \\end{document}""" % (w, h, latex_preamble, latex_fontspec) with codecs.open(fname_tex, "w", "utf-8") as fh_tex: fh_tex.write(latexcode) texcommand = get_texcommand() cmdargs = [texcommand, "-interaction=nonstopmode", "-halt-on-error", "figure.tex"] try: check_output(cmdargs, stderr=subprocess.STDOUT, cwd=tmpdir) except subprocess.CalledProcessError as e: raise RuntimeError("%s was not able to process your file.\n\nFull log:\n%s" % (texcommand, e.output)) # copy file contents to target with open(fname_pdf, "rb") as fh_src: shutil.copyfileobj(fh_src, fh) finally: try: shutil.rmtree(tmpdir) except: TmpDirCleaner.add(tmpdir) def print_pdf(self, fname_or_fh, *args, **kwargs): """ Use LaTeX to compile a Pgf generated figure to PDF. """ if kwargs.get("dryrun", False): self._print_pgf_to_fh(None, *args, **kwargs) return # figure out where the pdf is to be written to if is_string_like(fname_or_fh): with open(fname_or_fh, "wb") as fh: self._print_pdf_to_fh(fh, *args, **kwargs) elif is_writable_file_like(fname_or_fh): self._print_pdf_to_fh(fname_or_fh, *args, **kwargs) else: raise ValueError("filename must be a path or a file-like object") def _print_png_to_fh(self, fh, *args, **kwargs): converter = make_pdf_to_png_converter() try: # create temporary directory for pdf creation and png conversion tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_") fname_pdf = os.path.join(tmpdir, "figure.pdf") fname_png = os.path.join(tmpdir, "figure.png") # create pdf and try to convert it to png self.print_pdf(fname_pdf, *args, **kwargs) converter(fname_pdf, fname_png, dpi=self.figure.dpi) # copy file contents to target with open(fname_png, "rb") as fh_src: shutil.copyfileobj(fh_src, fh) finally: try: shutil.rmtree(tmpdir) except: TmpDirCleaner.add(tmpdir) def print_png(self, fname_or_fh, *args, **kwargs): """ Use LaTeX to compile a pgf figure to pdf and convert it to png. """ if kwargs.get("dryrun", False): self._print_pgf_to_fh(None, *args, **kwargs) return if is_string_like(fname_or_fh): with open(fname_or_fh, "wb") as fh: self._print_png_to_fh(fh, *args, **kwargs) elif is_writable_file_like(fname_or_fh): self._print_png_to_fh(fname_or_fh, *args, **kwargs) else: raise ValueError("filename must be a path or a file-like object") def get_renderer(self): return RendererPgf(self.figure, None, dummy=True) class FigureManagerPgf(FigureManagerBase): def __init__(self, *args): FigureManagerBase.__init__(self, *args) FigureCanvas = FigureCanvasPgf FigureManager = FigureManagerPgf def _cleanup_all(): LatexManager._cleanup_remaining_instances() TmpDirCleaner.cleanup_remaining_tmpdirs() atexit.register(_cleanup_all)
mit
hypergravity/bopy
setup.py
1
1310
import setuptools with open("README.md", "r") as fh: long_description = fh.read() setuptools.setup( name='bopy', version='0.4.0', author='Bo Zhang', author_email='bozhang@nao.cas.cn', description='Bo Zhang (@NAOC)''s python package.', # short description long_description=long_description, long_description_content_type="text/markdown", url='http://github.com/hypergravity/bopy', packages=setuptools.find_packages(), #packages=['song', 'twodspec'], license='New BSD', classifiers=["Development Status :: 5 - Production/Stable", "Intended Audience :: Science/Research", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3.7", "Topic :: Scientific/Engineering :: Physics", "Topic :: Scientific/Engineering :: Astronomy"], # package_dir={'song': 'song', # 'twodspec': 'twodspec'}, commented because it's wrong package_data={"bopy": ['bopy/data/test_spectra/*/*.fits', ], "": ["LICENSE"] }, # include_package_data=False, # commented to include data! requires=['numpy', 'scipy', 'matplotlib', 'astropy', 'emcee', 'joblib'] )
bsd-3-clause
enigmampc/catalyst
catalyst/examples/simple_loop.py
1
4210
import pandas as pd import talib from logbook import Logger, INFO from catalyst import run_algorithm from catalyst.api import symbol, record from catalyst.exchange.utils.stats_utils import get_pretty_stats, \ extract_transactions log = Logger('simple_loop', level=INFO) def initialize(context): log.info('initializing') context.asset = symbol('eth_btc') context.base_price = None def handle_data(context, data): log.info('handling bar: {}'.format(data.current_dt)) price = data.current(context.asset, 'close') log.info('got price {price}'.format(price=price)) prices = data.history( context.asset, fields='price', bar_count=20, frequency='30T' ) last_traded = prices.index[-1] log.info('last candle date: {}'.format(last_traded)) rsi = talib.RSI(prices.values, timeperiod=14)[-1] log.info('got rsi: {}'.format(rsi)) # If base_price is not set, we use the current value. This is the # price at the first bar which we reference to calculate price_change. if context.base_price is None: context.base_price = price price_change = (price - context.base_price) / context.base_price cash = context.portfolio.cash # Now that we've collected all current data for this frame, we use # the record() method to save it. This data will be available as # a parameter of the analyze() function for further analysis. record( price=price, price_change=price_change, cash=cash ) def analyze(context, perf): import matplotlib.pyplot as plt log.info('the stats: {}'.format(get_pretty_stats(perf))) # The quote currency of the algo exchange quote_currency = list(context.exchanges.values())[0].quote_currency.upper() # Plot the portfolio value over time. ax1 = plt.subplot(611) perf.loc[:, 'portfolio_value'].plot(ax=ax1) ax1.set_ylabel('Portfolio Value ({})'.format(quote_currency)) # Plot the price increase or decrease over time. ax2 = plt.subplot(612, sharex=ax1) perf.loc[:, 'price'].plot(ax=ax2, label='Price') ax2.set_ylabel('{asset} ({quote})'.format( asset=context.asset.symbol, quote=quote_currency )) transaction_df = extract_transactions(perf) if not transaction_df.empty: buy_df = transaction_df[transaction_df['amount'] > 0] sell_df = transaction_df[transaction_df['amount'] < 0] ax2.scatter( buy_df.index.to_pydatetime(), perf.loc[buy_df.index, 'price'], marker='^', s=100, c='green', label='' ) ax2.scatter( sell_df.index.to_pydatetime(), perf.loc[sell_df.index, 'price'], marker='v', s=100, c='red', label='' ) ax4 = plt.subplot(613, sharex=ax1) perf.loc[:, 'cash'].plot( ax=ax4, label='Quote Currency ({})'.format(quote_currency) ) ax4.set_ylabel('Cash ({})'.format(quote_currency)) perf['algorithm'] = perf.loc[:, 'algorithm_period_return'] ax5 = plt.subplot(614, sharex=ax1) perf.loc[:, ['algorithm', 'price_change']].plot(ax=ax5) ax5.set_ylabel('Percent Change') plt.legend(loc=3) # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() pass if __name__ == '__main__': mode = 'live' if mode == 'backtest': run_algorithm( capital_base=1, initialize=initialize, handle_data=handle_data, analyze=None, exchange_name='poloniex', algo_namespace='simple_loop', quote_currency='eth', data_frequency='minute', start=pd.to_datetime('2017-9-1', utc=True), end=pd.to_datetime('2017-12-1', utc=True), ) else: run_algorithm( capital_base=1, initialize=initialize, handle_data=handle_data, analyze=None, exchange_name='binance', live=True, algo_namespace='simple_loop', quote_currency='eth', live_graph=False, simulate_orders=True )
apache-2.0
rolando/theusual-kaggle-seeclickfix-ensemble
Bryan/data_io.py
2
4219
""" Functions for data IO """ __author__ = 'Bryan Gregory' __email__ = 'bryan.gregory1@gmail.com' __date__ = '09-06-2013' #Internal modules import utils #Start logger to record all info, warnings, and errors to Logs/logfile.log log = utils.start_logging(__name__) #External modules import json import csv import gc import pandas as pd import time import os from datetime import datetime from sklearn.externals import joblib #import JSON data into a dict def load_json(file_path): return [json.loads(line) for line in open(file_path)] #import delimited flat file into a list def load_flatfile(file_path, delimiter=''): temp_array = [] #if no delimiter is specified, try to use the built-in delimiter detection if delimiter == '': csv_reader = csv.reader(open(file_path)) else: csv_reader = csv.reader(open(file_path),delimiter) for line in csv_reader: temp_array += line return temp_array #[line for line in csv_reader] #import delimited flat file into a pandas dataframe def load_flatfile_to_df(file_path, delimiter=''): #if no delimiter is specified, try to use the built-in delimiter detection if delimiter == '': return pd.read_csv(file_path) else: return pd.read_csv(file_path, delimiter) def save_predictions(df,target,model_name='',directory='Submits/',estimator_class='',note=''): timestamp = datetime.now().strftime('%m-%d-%y_%H%M') filename = directory+timestamp+'--'+model_name+'_'+estimator_class+'_'+note+'.csv' #---Perform any manual predictions cleanup that may be necessary---# #Save predictions try: df[target] = [x[0] for x in df[target]] except IndexError: df[target] = [x for x in df[target]] df.ix[:,['id',target]].to_csv(filename, index=False) log.info('Submission file saved: %s' % filename) def save_combined_predictions(df,directory,filename,note=''): #If previous combined predictions already exist, archive existing ones by renaming to append datetime try: modified_date = time.strptime(time.ctime(os.path.getmtime(directory+filename)), '%a %b %d %H:%M:%S %Y') modified_date = datetime.fromtimestamp(time.mktime(modified_date)).strftime('%m-%d-%y_%H%M') archived_file = directory+'Archive/'+filename[:len(filename)-4]+'--'+modified_date+'.csv' os.rename(directory+filename,archived_file) log.info('File already exists with given filename, archiving old file to: '+ archived_file) except WindowsError: pass #Save predictions df.to_csv(directory+filename, index=False) log.info('Predictions saved: %s' % filename) def save_cached_object(object, filename, directory='Cache/'): """Save cached objects in pickel format using joblib compression. If a previous cached file exists, then get its modified date and append it to filename and archive it """ if filename[-4:] != '.pkl': filename = filename+'.pkl' try: modified_date = time.strptime(time.ctime(os.path.getmtime(directory+filename)), '%a %b %d %H:%M:%S %Y') modified_date = datetime.fromtimestamp(time.mktime(modified_date)).strftime('%m-%d-%y_%H%M') archived_file = directory+'Archive/'+filename[:len(filename)-4]+'--'+modified_date+'.pkl' os.rename(directory+filename,archived_file) log.info('Cached object already exists with given filename, archiving old object to: '+ archived_file) except WindowsError: pass joblib.dump(object, directory+filename, compress=9) log.info('New object cached to: '+directory+filename) def load_cached_object(filename, directory='Cache/'): if filename[-4:] != '.pkl': filename = filename+'.pkl' try: object = joblib.load(directory+filename) log.info('Successfully loaded object from: '+directory+filename) except IOError: log.info('Cached object does not exist: '+directory+filename) return object def save_text_features(output_file, feature_names): o_f = open( output_file, 'wb' ) feature_names = '\n'.join( feature_names ) o_f.write( feature_names )
bsd-3-clause
alexeyum/scikit-learn
benchmarks/bench_multilabel_metrics.py
276
7138
#!/usr/bin/env python """ A comparison of multilabel target formats and metrics over them """ from __future__ import division from __future__ import print_function from timeit import timeit from functools import partial import itertools import argparse import sys import matplotlib.pyplot as plt import scipy.sparse as sp import numpy as np from sklearn.datasets import make_multilabel_classification from sklearn.metrics import (f1_score, accuracy_score, hamming_loss, jaccard_similarity_score) from sklearn.utils.testing import ignore_warnings METRICS = { 'f1': partial(f1_score, average='micro'), 'f1-by-sample': partial(f1_score, average='samples'), 'accuracy': accuracy_score, 'hamming': hamming_loss, 'jaccard': jaccard_similarity_score, } FORMATS = { 'sequences': lambda y: [list(np.flatnonzero(s)) for s in y], 'dense': lambda y: y, 'csr': lambda y: sp.csr_matrix(y), 'csc': lambda y: sp.csc_matrix(y), } @ignore_warnings def benchmark(metrics=tuple(v for k, v in sorted(METRICS.items())), formats=tuple(v for k, v in sorted(FORMATS.items())), samples=1000, classes=4, density=.2, n_times=5): """Times metric calculations for a number of inputs Parameters ---------- metrics : array-like of callables (1d or 0d) The metric functions to time. formats : array-like of callables (1d or 0d) These may transform a dense indicator matrix into multilabel representation. samples : array-like of ints (1d or 0d) The number of samples to generate as input. classes : array-like of ints (1d or 0d) The number of classes in the input. density : array-like of ints (1d or 0d) The density of positive labels in the input. n_times : int Time calling the metric n_times times. Returns ------- array of floats shaped like (metrics, formats, samples, classes, density) Time in seconds. """ metrics = np.atleast_1d(metrics) samples = np.atleast_1d(samples) classes = np.atleast_1d(classes) density = np.atleast_1d(density) formats = np.atleast_1d(formats) out = np.zeros((len(metrics), len(formats), len(samples), len(classes), len(density)), dtype=float) it = itertools.product(samples, classes, density) for i, (s, c, d) in enumerate(it): _, y_true = make_multilabel_classification(n_samples=s, n_features=1, n_classes=c, n_labels=d * c, random_state=42) _, y_pred = make_multilabel_classification(n_samples=s, n_features=1, n_classes=c, n_labels=d * c, random_state=84) for j, f in enumerate(formats): f_true = f(y_true) f_pred = f(y_pred) for k, metric in enumerate(metrics): t = timeit(partial(metric, f_true, f_pred), number=n_times) out[k, j].flat[i] = t return out def _tabulate(results, metrics, formats): """Prints results by metric and format Uses the last ([-1]) value of other fields """ column_width = max(max(len(k) for k in formats) + 1, 8) first_width = max(len(k) for k in metrics) head_fmt = ('{:<{fw}s}' + '{:>{cw}s}' * len(formats)) row_fmt = ('{:<{fw}s}' + '{:>{cw}.3f}' * len(formats)) print(head_fmt.format('Metric', *formats, cw=column_width, fw=first_width)) for metric, row in zip(metrics, results[:, :, -1, -1, -1]): print(row_fmt.format(metric, *row, cw=column_width, fw=first_width)) def _plot(results, metrics, formats, title, x_ticks, x_label, format_markers=('x', '|', 'o', '+'), metric_colors=('c', 'm', 'y', 'k', 'g', 'r', 'b')): """ Plot the results by metric, format and some other variable given by x_label """ fig = plt.figure('scikit-learn multilabel metrics benchmarks') plt.title(title) ax = fig.add_subplot(111) for i, metric in enumerate(metrics): for j, format in enumerate(formats): ax.plot(x_ticks, results[i, j].flat, label='{}, {}'.format(metric, format), marker=format_markers[j], color=metric_colors[i % len(metric_colors)]) ax.set_xlabel(x_label) ax.set_ylabel('Time (s)') ax.legend() plt.show() if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument('metrics', nargs='*', default=sorted(METRICS), help='Specifies metrics to benchmark, defaults to all. ' 'Choices are: {}'.format(sorted(METRICS))) ap.add_argument('--formats', nargs='+', choices=sorted(FORMATS), help='Specifies multilabel formats to benchmark ' '(defaults to all).') ap.add_argument('--samples', type=int, default=1000, help='The number of samples to generate') ap.add_argument('--classes', type=int, default=10, help='The number of classes') ap.add_argument('--density', type=float, default=.2, help='The average density of labels per sample') ap.add_argument('--plot', choices=['classes', 'density', 'samples'], default=None, help='Plot time with respect to this parameter varying ' 'up to the specified value') ap.add_argument('--n-steps', default=10, type=int, help='Plot this many points for each metric') ap.add_argument('--n-times', default=5, type=int, help="Time performance over n_times trials") args = ap.parse_args() if args.plot is not None: max_val = getattr(args, args.plot) if args.plot in ('classes', 'samples'): min_val = 2 else: min_val = 0 steps = np.linspace(min_val, max_val, num=args.n_steps + 1)[1:] if args.plot in ('classes', 'samples'): steps = np.unique(np.round(steps).astype(int)) setattr(args, args.plot, steps) if args.metrics is None: args.metrics = sorted(METRICS) if args.formats is None: args.formats = sorted(FORMATS) results = benchmark([METRICS[k] for k in args.metrics], [FORMATS[k] for k in args.formats], args.samples, args.classes, args.density, args.n_times) _tabulate(results, args.metrics, args.formats) if args.plot is not None: print('Displaying plot', file=sys.stderr) title = ('Multilabel metrics with %s' % ', '.join('{0}={1}'.format(field, getattr(args, field)) for field in ['samples', 'classes', 'density'] if args.plot != field)) _plot(results, args.metrics, args.formats, title, steps, args.plot)
bsd-3-clause
shangwuhencc/scikit-learn
sklearn/feature_selection/rfe.py
64
17509
# Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Vincent Michel <vincent.michel@inria.fr> # Gilles Louppe <g.louppe@gmail.com> # # License: BSD 3 clause """Recursive feature elimination for feature ranking""" import warnings import numpy as np from ..utils import check_X_y, safe_sqr from ..utils.metaestimators import if_delegate_has_method from ..base import BaseEstimator from ..base import MetaEstimatorMixin from ..base import clone from ..base import is_classifier from ..cross_validation import check_cv from ..cross_validation import _safe_split, _score from ..metrics.scorer import check_scoring from .base import SelectorMixin class RFE(BaseEstimator, MetaEstimatorMixin, SelectorMixin): """Feature ranking with recursive feature elimination. Given an external estimator that assigns weights to features (e.g., the coefficients of a linear model), the goal of recursive feature elimination (RFE) is to select features by recursively considering smaller and smaller sets of features. First, the estimator is trained on the initial set of features and weights are assigned to each one of them. Then, features whose absolute weights are the smallest are pruned from the current set features. That procedure is recursively repeated on the pruned set until the desired number of features to select is eventually reached. Read more in the :ref:`User Guide <rfe>`. Parameters ---------- estimator : object A supervised learning estimator with a `fit` method that updates a `coef_` attribute that holds the fitted parameters. Important features must correspond to high absolute values in the `coef_` array. For instance, this is the case for most supervised learning algorithms such as Support Vector Classifiers and Generalized Linear Models from the `svm` and `linear_model` modules. n_features_to_select : int or None (default=None) The number of features to select. If `None`, half of the features are selected. step : int or float, optional (default=1) If greater than or equal to 1, then `step` corresponds to the (integer) number of features to remove at each iteration. If within (0.0, 1.0), then `step` corresponds to the percentage (rounded down) of features to remove at each iteration. estimator_params : dict Parameters for the external estimator. This attribute is deprecated as of version 0.16 and will be removed in 0.18. Use estimator initialisation or set_params method instead. verbose : int, default=0 Controls verbosity of output. Attributes ---------- n_features_ : int The number of selected features. support_ : array of shape [n_features] The mask of selected features. ranking_ : array of shape [n_features] The feature ranking, such that ``ranking_[i]`` corresponds to the ranking position of the i-th feature. Selected (i.e., estimated best) features are assigned rank 1. estimator_ : object The external estimator fit on the reduced dataset. Examples -------- The following example shows how to retrieve the 5 right informative features in the Friedman #1 dataset. >>> from sklearn.datasets import make_friedman1 >>> from sklearn.feature_selection import RFE >>> from sklearn.svm import SVR >>> X, y = make_friedman1(n_samples=50, n_features=10, random_state=0) >>> estimator = SVR(kernel="linear") >>> selector = RFE(estimator, 5, step=1) >>> selector = selector.fit(X, y) >>> selector.support_ # doctest: +NORMALIZE_WHITESPACE array([ True, True, True, True, True, False, False, False, False, False], dtype=bool) >>> selector.ranking_ array([1, 1, 1, 1, 1, 6, 4, 3, 2, 5]) References ---------- .. [1] Guyon, I., Weston, J., Barnhill, S., & Vapnik, V., "Gene selection for cancer classification using support vector machines", Mach. Learn., 46(1-3), 389--422, 2002. """ def __init__(self, estimator, n_features_to_select=None, step=1, estimator_params=None, verbose=0): self.estimator = estimator self.n_features_to_select = n_features_to_select self.step = step self.estimator_params = estimator_params self.verbose = verbose @property def _estimator_type(self): return self.estimator._estimator_type def fit(self, X, y): """Fit the RFE model and then the underlying estimator on the selected features. Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] The training input samples. y : array-like, shape = [n_samples] The target values. """ return self._fit(X, y) def _fit(self, X, y, step_score=None): X, y = check_X_y(X, y, "csc") # Initialization n_features = X.shape[1] if self.n_features_to_select is None: n_features_to_select = n_features / 2 else: n_features_to_select = self.n_features_to_select if 0.0 < self.step < 1.0: step = int(max(1, self.step * n_features)) else: step = int(self.step) if step <= 0: raise ValueError("Step must be >0") if self.estimator_params is not None: warnings.warn("The parameter 'estimator_params' is deprecated as " "of version 0.16 and will be removed in 0.18. The " "parameter is no longer necessary because the value " "is set via the estimator initialisation or " "set_params method.", DeprecationWarning) support_ = np.ones(n_features, dtype=np.bool) ranking_ = np.ones(n_features, dtype=np.int) if step_score: self.scores_ = [] # Elimination while np.sum(support_) > n_features_to_select: # Remaining features features = np.arange(n_features)[support_] # Rank the remaining features estimator = clone(self.estimator) if self.estimator_params: estimator.set_params(**self.estimator_params) if self.verbose > 0: print("Fitting estimator with %d features." % np.sum(support_)) estimator.fit(X[:, features], y) # Get coefs if hasattr(estimator, 'coef_'): coefs = estimator.coef_ elif hasattr(estimator, 'feature_importances_'): coefs = estimator.feature_importances_ else: raise RuntimeError('The classifier does not expose ' '"coef_" or "feature_importances_" ' 'attributes') # Get ranks if coefs.ndim > 1: ranks = np.argsort(safe_sqr(coefs).sum(axis=0)) else: ranks = np.argsort(safe_sqr(coefs)) # for sparse case ranks is matrix ranks = np.ravel(ranks) # Eliminate the worse features threshold = min(step, np.sum(support_) - n_features_to_select) # Compute step score on the previous selection iteration # because 'estimator' must use features # that have not been eliminated yet if step_score: self.scores_.append(step_score(estimator, features)) support_[features[ranks][:threshold]] = False ranking_[np.logical_not(support_)] += 1 # Set final attributes features = np.arange(n_features)[support_] self.estimator_ = clone(self.estimator) if self.estimator_params: self.estimator_.set_params(**self.estimator_params) self.estimator_.fit(X[:, features], y) # Compute step score when only n_features_to_select features left if step_score: self.scores_.append(step_score(self.estimator_, features)) self.n_features_ = support_.sum() self.support_ = support_ self.ranking_ = ranking_ return self @if_delegate_has_method(delegate='estimator') def predict(self, X): """Reduce X to the selected features and then predict using the underlying estimator. Parameters ---------- X : array of shape [n_samples, n_features] The input samples. Returns ------- y : array of shape [n_samples] The predicted target values. """ return self.estimator_.predict(self.transform(X)) @if_delegate_has_method(delegate='estimator') def score(self, X, y): """Reduce X to the selected features and then return the score of the underlying estimator. Parameters ---------- X : array of shape [n_samples, n_features] The input samples. y : array of shape [n_samples] The target values. """ return self.estimator_.score(self.transform(X), y) def _get_support_mask(self): return self.support_ @if_delegate_has_method(delegate='estimator') def decision_function(self, X): return self.estimator_.decision_function(self.transform(X)) @if_delegate_has_method(delegate='estimator') def predict_proba(self, X): return self.estimator_.predict_proba(self.transform(X)) @if_delegate_has_method(delegate='estimator') def predict_log_proba(self, X): return self.estimator_.predict_log_proba(self.transform(X)) class RFECV(RFE, MetaEstimatorMixin): """Feature ranking with recursive feature elimination and cross-validated selection of the best number of features. Read more in the :ref:`User Guide <rfe>`. Parameters ---------- estimator : object A supervised learning estimator with a `fit` method that updates a `coef_` attribute that holds the fitted parameters. Important features must correspond to high absolute values in the `coef_` array. For instance, this is the case for most supervised learning algorithms such as Support Vector Classifiers and Generalized Linear Models from the `svm` and `linear_model` modules. step : int or float, optional (default=1) If greater than or equal to 1, then `step` corresponds to the (integer) number of features to remove at each iteration. If within (0.0, 1.0), then `step` corresponds to the percentage (rounded down) of features to remove at each iteration. cv : int, cross-validation generator or an iterable, optional Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 3-fold cross-validation, - integer, to specify the number of folds. - An object to be used as a cross-validation generator. - An iterable yielding train/test splits. For integer/None inputs, if ``y`` is binary or multiclass, :class:`StratifiedKFold` used. If the estimator is a classifier or if ``y`` is neither binary nor multiclass, :class:`KFold` is used. Refer :ref:`User Guide <cross_validation>` for the various cross-validation strategies that can be used here. scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. estimator_params : dict Parameters for the external estimator. This attribute is deprecated as of version 0.16 and will be removed in 0.18. Use estimator initialisation or set_params method instead. verbose : int, default=0 Controls verbosity of output. Attributes ---------- n_features_ : int The number of selected features with cross-validation. support_ : array of shape [n_features] The mask of selected features. ranking_ : array of shape [n_features] The feature ranking, such that `ranking_[i]` corresponds to the ranking position of the i-th feature. Selected (i.e., estimated best) features are assigned rank 1. grid_scores_ : array of shape [n_subsets_of_features] The cross-validation scores such that ``grid_scores_[i]`` corresponds to the CV score of the i-th subset of features. estimator_ : object The external estimator fit on the reduced dataset. Notes ----- The size of ``grid_scores_`` is equal to ceil((n_features - 1) / step) + 1, where step is the number of features removed at each iteration. Examples -------- The following example shows how to retrieve the a-priori not known 5 informative features in the Friedman #1 dataset. >>> from sklearn.datasets import make_friedman1 >>> from sklearn.feature_selection import RFECV >>> from sklearn.svm import SVR >>> X, y = make_friedman1(n_samples=50, n_features=10, random_state=0) >>> estimator = SVR(kernel="linear") >>> selector = RFECV(estimator, step=1, cv=5) >>> selector = selector.fit(X, y) >>> selector.support_ # doctest: +NORMALIZE_WHITESPACE array([ True, True, True, True, True, False, False, False, False, False], dtype=bool) >>> selector.ranking_ array([1, 1, 1, 1, 1, 6, 4, 3, 2, 5]) References ---------- .. [1] Guyon, I., Weston, J., Barnhill, S., & Vapnik, V., "Gene selection for cancer classification using support vector machines", Mach. Learn., 46(1-3), 389--422, 2002. """ def __init__(self, estimator, step=1, cv=None, scoring=None, estimator_params=None, verbose=0): self.estimator = estimator self.step = step self.cv = cv self.scoring = scoring self.estimator_params = estimator_params self.verbose = verbose def fit(self, X, y): """Fit the RFE model and automatically tune the number of selected features. Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vector, where `n_samples` is the number of samples and `n_features` is the total number of features. y : array-like, shape = [n_samples] Target values (integers for classification, real numbers for regression). """ X, y = check_X_y(X, y, "csr") if self.estimator_params is not None: warnings.warn("The parameter 'estimator_params' is deprecated as " "of version 0.16 and will be removed in 0.18. " "The parameter is no longer necessary because the " "value is set via the estimator initialisation or " "set_params method.", DeprecationWarning) # Initialization cv = check_cv(self.cv, X, y, is_classifier(self.estimator)) scorer = check_scoring(self.estimator, scoring=self.scoring) n_features = X.shape[1] n_features_to_select = 1 # Determine the number of subsets of features scores = [] # Cross-validation for n, (train, test) in enumerate(cv): X_train, y_train = _safe_split(self.estimator, X, y, train) X_test, y_test = _safe_split(self.estimator, X, y, test, train) rfe = RFE(estimator=self.estimator, n_features_to_select=n_features_to_select, step=self.step, estimator_params=self.estimator_params, verbose=self.verbose - 1) rfe._fit(X_train, y_train, lambda estimator, features: _score(estimator, X_test[:, features], y_test, scorer)) scores.append(np.array(rfe.scores_[::-1]).reshape(1, -1)) scores = np.sum(np.concatenate(scores, 0), 0) # The index in 'scores' when 'n_features' features are selected n_feature_index = np.ceil((n_features - n_features_to_select) / float(self.step)) n_features_to_select = max(n_features_to_select, n_features - ((n_feature_index - np.argmax(scores)) * self.step)) # Re-execute an elimination with best_k over the whole set rfe = RFE(estimator=self.estimator, n_features_to_select=n_features_to_select, step=self.step, estimator_params=self.estimator_params) rfe.fit(X, y) # Set final attributes self.support_ = rfe.support_ self.n_features_ = rfe.n_features_ self.ranking_ = rfe.ranking_ self.estimator_ = clone(self.estimator) if self.estimator_params: self.estimator_.set_params(**self.estimator_params) self.estimator_.fit(self.transform(X), y) # Fixing a normalization error, n is equal to len(cv) - 1 # here, the scores are normalized by len(cv) self.grid_scores_ = scores / len(cv) return self
bsd-3-clause
CooperLuan/yoka_bot
yoka_bot/spiders/yoka_bot_spider.py
1
6117
# encoding: utf8 import re import pandas as pd from lxml import etree import scrapy from yoka_bot.items import YokaBotBrandListItem, YokaBotBrandItem, YokaBotProductListItem, YokaBotProductItem host = 'http://brand.yoka.com' def wrap_full_url(url): if url.startswith('http'): return url return host + url class YokaBotSpider(scrapy.Spider): name = "YokaBot" start_urls = [ "http://brand.yoka.com/brandlist.htm", ] def parse(self, response): html = response.body_as_unicode() tree = etree.HTML(html) nodes = tree.xpath("//a[@target='_blank']") nodes = [ (node.text.strip('\n\r\t '), node.attrib['href']) for node in nodes if node.text and node.text.strip('\n\r\t ') ] df = pd.DataFrame(nodes, columns=['name', 'link']) valid_links = df[df['link'].apply(lambda x: x != '/' and x.count('/') == 1)]['link'].unique() for link in valid_links: is_hot = False name = df[df['link'] == link]['name'].apply( lambda x: re.sub(r'[\n\r\t]+', r'', x.strip('\n\r\t '), re.S)).tolist() name = map(lambda x: re.sub(r'\s{2,}', r' ', x, re.S), name) if df[df['link'] == link]['name'].unique().size > 1: is_hot = True item = YokaBotBrandListItem(**{ 'item_name': 'YokaBotBrandListItem', 'name': name, 'link': wrap_full_url(link), 'is_hot': is_hot, }) yield item yield scrapy.Request(item['link'], callback=self.parse_brand_page) def parse_brand_page(self, response): url = response.url tree = etree.HTML(response.body_as_unicode()) avator = tree.xpath("//div[@class='m-product-show']/div//img/@src") attrs = [node.text for node in tree.xpath("//div[@class='m-product-show']/div[@class='pl']/div[@class='box']/dl//dd//li")] attrs = dict([attr.partition(u'\uff1a')[::2] for attr in attrs]) brand_cn = attrs.get(u'中文名') brand_en = attrs.get(u'英文名') country = attrs.get(u'国家') created = attrs.get(u'创建年代') official_url = tree.xpath("//div[@class='m-product-show']//h2/a/@href") story = '\n'.join(tree.xpath("//div[@class='m-product-show']//h3/following-sibling::p/text()")) product_list_url = tree.xpath("//div[@id='tabcn']//div[@class='more']/a/@href") product_list_url = wrap_full_url(url.rstrip('/') + '/productlist.htm?p=1') yield YokaBotBrandItem(**{ 'item_name': 'YokaBotBrandItem', 'url': url, 'avator': avator and avator[0] or None, 'brand_cn': brand_cn, 'brand_en': brand_en, 'country': country, 'created': created, 'official_url': official_url and official_url[0] or None, 'story': story, 'product_list_url': product_list_url, }) yield scrapy.Request(product_list_url, callback=self.parse_product_list_page) def parse_product_list_page(self, response): url = response.url page = int(re.search(r'p=(\d+)', url).group(1)) tree = etree.HTML(response.body_as_unicode()) nodes = tree.xpath("//div[@class='mask']/dl") # force_break = False for node in nodes: product_url = wrap_full_url(node.xpath(".//dt/a/@href")[0]) yield YokaBotProductListItem(**{ 'item_name': 'YokaBotProductListItem', 'url': url, 'page': page, 'product_url': product_url, 'img': node.xpath(".//dt/a/img/@src")[0], 'title': node.xpath(".//dt/a/img/@alt")[0], }) yield scrapy.Request(product_url, callback=self.parse_product_page) if nodes: page += 1 url = re.sub(r'p=(\d+)', r'p=%s' % page, url, re.S) yield scrapy.Request(url, callback=self.parse_product_list_page) def parse_product_page(self, response): url = response.url tree = etree.HTML(response.body_as_unicode()) if tree.xpath("//input[@id='proid']/@value"): pid = tree.xpath("//input[@id='proid']/@value") breadcrumb = [t.text.strip('\n\r\t ') for t in tree.xpath("//div[@class='zpyTitle']/a")] title = [t.strip('\n\r\t ') for t in tree.xpath("//span[@itemprop='name']/text()")] attrib = [(t.xpath(".//dt/text()"), [t.strip('\n\r\t ') for t in t.xpath(".//dd")[0].itertext() if t.strip('\n\r\t ')]) for t in tree.xpath("//div[@class='list']/dl")] img = tree.xpath("//dt[@id='products_big']//img/@src") yield YokaBotProductItem(**{ 'item_name': 'YokaBotProductItem', 'url': url, 'product_id': pid and pid[0] or None, 'breadcrumb': breadcrumb, 'title': title and title[0].strip('\n\r\t ') or None, 'attrib': attrib, 'img': img and img[0] or None, }) else: pid = tree.xpath("//input[@id='_productId']/@value") title = tree.xpath("//div[@class='gc-brand-profile']/h1/text()") breadcrumb = list(filter(lambda x: 'no ' not in x[0], [(node.xpath(".//./@class")[0], node.xpath(".//./text()")[0]) for node in tree.xpath("//div[@class='sub-nav']/div[@class='list']/a")])) breadcrumb = breadcrumb and [breadcrumb[0][1]] or None attrib = [tuple(node.itertext()) for node in tree.xpath("//div[@class='mask']/ul/li")] img = tree.xpath("//dl[@id='gl-brand-showbig']//img/@src") yield YokaBotProductItem(**{ 'item_name': 'YokaBotProductItem', 'url': url, 'product_id': pid and pid[0] or None, 'breadcrumb': breadcrumb, 'title': title and title[0].strip('\n\r\t ') or None, 'attrib': attrib, 'img': img and img[0] or None, })
mit
senthil10/scilifelab
scripts/RNA_analysis/plot_complexity_curves.py
4
5371
import sys import os import yaml import glob import subprocess import argparse import pandas as pd from matplotlib import pyplot as plt import numpy as np import math def main(args): ccurves = args.ccurves[0] x_min = args.x_min x_max = args.x_max if x_min < 0 or x_max <= x_min: sys.exit("problem with x-min or x-max ({}, {}). x-min must be equal or higher to 0 and less than x-max".format(x_min, x_max)) output_name = args.output_name legend = [[],[]] global_x_max_ccurve_limit = 0 global_y_max_ccurve_limit = 0 ax = plt.subplot(111) #Each ccurve will get a different color colormap = plt.cm.gist_ncar plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, len(ccurves))]) col=math.ceil(float(len(ccurves))/30) for ccurve in ccurves: sample=ccurve.split('/')[1].split('.')[0] print "processing {}".format(ccurve) ccurve_table = pd.io.parsers.read_csv(ccurve, sep='\t', header=0) ccurve_TOTAL_READS = [] ccurve_EXPECTED_DISTINCT = [] if "TOTAL_READS" in ccurve_table: ccurve_TOTAL_READS = ccurve_table["TOTAL_READS"].tolist() ccurve_EXPECTED_DISTINCT = ccurve_table["EXPECTED_DISTINCT"].tolist() elif "total_reads" in ccurve_table: ccurve_TOTAL_READS = ccurve_table["total_reads"].tolist() ccurve_EXPECTED_DISTINCT = ccurve_table["distinct_reads"].tolist() else: sys.exit("Error, table {} is not in the expected format... has been generated with preseq?".format(ccurve)) #I need to find the interpolation point to print the plots x_mim_ccurve_limit = computeLimit(x_min, ccurve_TOTAL_READS) x_max_ccurve_limit = computeLimit(x_max, ccurve_TOTAL_READS) if x_max_ccurve_limit > global_x_max_ccurve_limit: global_x_max_ccurve_limit = x_max_ccurve_limit if ccurve_EXPECTED_DISTINCT[x_max_ccurve_limit] > global_y_max_ccurve_limit: global_y_max_ccurve_limit = ccurve_EXPECTED_DISTINCT[x_max_ccurve_limit] p, = ax.plot(ccurve_TOTAL_READS[x_mim_ccurve_limit:x_max_ccurve_limit], ccurve_EXPECTED_DISTINCT[x_mim_ccurve_limit:x_max_ccurve_limit]) legend[0].append(p) legend[1].append(sample) #plot perfect library as dashed line plt.plot([0, x_max], [0, x_max], color='black', linestyle='--', linewidth=1) plt.ylim(0, global_y_max_ccurve_limit + global_y_max_ccurve_limit*0.2) #label the axis plt.ylabel('EXPECTED DISTINCT READS') plt.xlabel('TOTAL READS') plt.title("Complexity curve: preseq") #change the font in oredr to plot a nicer picture font = {'size' : 6} plt.rc('font', **font) #now resize the plot and add the legend on the right in order to avoid visualisation problems box = ax.get_position() ax.set_position([0.1, box.y0, box.width * 0.78, box.height]) ax.legend(legend[0], legend[1],loc='center left', bbox_to_anchor=(1, 0.5),ncol=int(col)) #ax.legend(legend[0], legend[1],loc='center left') #now save the plot plotname = output_name+".pdf" plt.savefig(plotname,format='pdf') plt.clf() return 0 def computeLimit(value, ccurve_TOTAL_READS): """This function returns the index of ccurve_TOTAL_READS containing the closest value to x_max""" if ccurve_TOTAL_READS[-1] < value: sys.exit("Attention: value is set to a value higher than the highest extrapolated point by preseq (value={}, ccurve_TOTAL_READS[-1]={}). Please specify a lower m-max.".format(value, ccurve_TOTAL_READS[-1])) first_point = 0 last_point = len(ccurve_TOTAL_READS) while first_point != last_point: middle_point = (first_point + last_point)/2 middle_value = ccurve_TOTAL_READS[middle_point] if middle_value == value: return middle_point elif middle_value > value: last_point = middle_point -1 else: first_point = middle_point +1 return first_point if __name__ == '__main__': parser = argparse.ArgumentParser("This script plots the complexity curves generated for one or several libraries. The script is designed to work using" " the output produced by preseq (http://smithlabresearch.org/software/preseq/). preseq version 1.0.0 is currenlty " "supported by this script (the script is compatible also with version 0.1.0). Preseq is a tool used to estimate the " "library complexity and/or to predict the library complexity. In the first case \"preseq c_curve\" should be use. In " "the second case \"preseq lc_extrap\" should be usued. Please, refer to preseq manual available at " "http://smithlabresearch.org/wp-content/uploads/manual.pdf for examples (pages 12 to 14 are the most informatives ones)") parser.add_argument('--ccurves' , type=str, required=True, action='append', nargs='+', help="ccurves generated by preseq (http://smithlabresearch.org/software/preseq/)") parser.add_argument('--x-min' , type=int, default=0 , help="lower x-limit (default 0)") parser.add_argument('--x-max' , type=int, default=500000000, help="upper x-limit (default 500M)") parser.add_argument('--output-name' , type=str, default='complexity_curves', help="output file name") args = parser.parse_args() main(args)
mit
vortex-ape/scikit-learn
sklearn/manifold/t_sne.py
2
36774
# Author: Alexander Fabisch -- <afabisch@informatik.uni-bremen.de> # Author: Christopher Moody <chrisemoody@gmail.com> # Author: Nick Travers <nickt@squareup.com> # License: BSD 3 clause (C) 2014 # This is the exact and Barnes-Hut t-SNE implementation. There are other # modifications of the algorithm: # * Fast Optimization for t-SNE: # http://cseweb.ucsd.edu/~lvdmaaten/workshops/nips2010/papers/vandermaaten.pdf from __future__ import division import warnings from time import time import numpy as np from scipy import linalg import scipy.sparse as sp from scipy.spatial.distance import pdist from scipy.spatial.distance import squareform from scipy.sparse import csr_matrix from ..neighbors import NearestNeighbors from ..base import BaseEstimator from ..utils import check_array from ..utils import check_random_state from ..decomposition import PCA from ..metrics.pairwise import pairwise_distances from . import _utils from . import _barnes_hut_tsne from ..externals.six import string_types from ..utils import deprecated MACHINE_EPSILON = np.finfo(np.double).eps def _joint_probabilities(distances, desired_perplexity, verbose): """Compute joint probabilities p_ij from distances. Parameters ---------- distances : array, shape (n_samples * (n_samples-1) / 2,) Distances of samples are stored as condensed matrices, i.e. we omit the diagonal and duplicate entries and store everything in a one-dimensional array. desired_perplexity : float Desired perplexity of the joint probability distributions. verbose : int Verbosity level. Returns ------- P : array, shape (n_samples * (n_samples-1) / 2,) Condensed joint probability matrix. """ # Compute conditional probabilities such that they approximately match # the desired perplexity distances = distances.astype(np.float32, copy=False) conditional_P = _utils._binary_search_perplexity( distances, None, desired_perplexity, verbose) P = conditional_P + conditional_P.T sum_P = np.maximum(np.sum(P), MACHINE_EPSILON) P = np.maximum(squareform(P) / sum_P, MACHINE_EPSILON) return P def _joint_probabilities_nn(distances, neighbors, desired_perplexity, verbose): """Compute joint probabilities p_ij from distances using just nearest neighbors. This method is approximately equal to _joint_probabilities. The latter is O(N), but limiting the joint probability to nearest neighbors improves this substantially to O(uN). Parameters ---------- distances : array, shape (n_samples, k) Distances of samples to its k nearest neighbors. neighbors : array, shape (n_samples, k) Indices of the k nearest-neighbors for each samples. desired_perplexity : float Desired perplexity of the joint probability distributions. verbose : int Verbosity level. Returns ------- P : csr sparse matrix, shape (n_samples, n_samples) Condensed joint probability matrix with only nearest neighbors. """ t0 = time() # Compute conditional probabilities such that they approximately match # the desired perplexity n_samples, k = neighbors.shape distances = distances.astype(np.float32, copy=False) neighbors = neighbors.astype(np.int64, copy=False) conditional_P = _utils._binary_search_perplexity( distances, neighbors, desired_perplexity, verbose) assert np.all(np.isfinite(conditional_P)), \ "All probabilities should be finite" # Symmetrize the joint probability distribution using sparse operations P = csr_matrix((conditional_P.ravel(), neighbors.ravel(), range(0, n_samples * k + 1, k)), shape=(n_samples, n_samples)) P = P + P.T # Normalize the joint probability distribution sum_P = np.maximum(P.sum(), MACHINE_EPSILON) P /= sum_P assert np.all(np.abs(P.data) <= 1.0) if verbose >= 2: duration = time() - t0 print("[t-SNE] Computed conditional probabilities in {:.3f}s" .format(duration)) return P def _kl_divergence(params, P, degrees_of_freedom, n_samples, n_components, skip_num_points=0, compute_error=True): """t-SNE objective function: gradient of the KL divergence of p_ijs and q_ijs and the absolute error. Parameters ---------- params : array, shape (n_params,) Unraveled embedding. P : array, shape (n_samples * (n_samples-1) / 2,) Condensed joint probability matrix. degrees_of_freedom : int Degrees of freedom of the Student's-t distribution. n_samples : int Number of samples. n_components : int Dimension of the embedded space. skip_num_points : int (optional, default:0) This does not compute the gradient for points with indices below `skip_num_points`. This is useful when computing transforms of new data where you'd like to keep the old data fixed. compute_error: bool (optional, default:True) If False, the kl_divergence is not computed and returns NaN. Returns ------- kl_divergence : float Kullback-Leibler divergence of p_ij and q_ij. grad : array, shape (n_params,) Unraveled gradient of the Kullback-Leibler divergence with respect to the embedding. """ X_embedded = params.reshape(n_samples, n_components) # Q is a heavy-tailed distribution: Student's t-distribution dist = pdist(X_embedded, "sqeuclidean") dist /= degrees_of_freedom dist += 1. dist **= (degrees_of_freedom + 1.0) / -2.0 Q = np.maximum(dist / (2.0 * np.sum(dist)), MACHINE_EPSILON) # Optimization trick below: np.dot(x, y) is faster than # np.sum(x * y) because it calls BLAS # Objective: C (Kullback-Leibler divergence of P and Q) if compute_error: kl_divergence = 2.0 * np.dot( P, np.log(np.maximum(P, MACHINE_EPSILON) / Q)) else: kl_divergence = np.nan # Gradient: dC/dY # pdist always returns double precision distances. Thus we need to take grad = np.ndarray((n_samples, n_components), dtype=params.dtype) PQd = squareform((P - Q) * dist) for i in range(skip_num_points, n_samples): grad[i] = np.dot(np.ravel(PQd[i], order='K'), X_embedded[i] - X_embedded) grad = grad.ravel() c = 2.0 * (degrees_of_freedom + 1.0) / degrees_of_freedom grad *= c return kl_divergence, grad def _kl_divergence_bh(params, P, degrees_of_freedom, n_samples, n_components, angle=0.5, skip_num_points=0, verbose=False, compute_error=True): """t-SNE objective function: KL divergence of p_ijs and q_ijs. Uses Barnes-Hut tree methods to calculate the gradient that runs in O(NlogN) instead of O(N^2) Parameters ---------- params : array, shape (n_params,) Unraveled embedding. P : csr sparse matrix, shape (n_samples, n_sample) Sparse approximate joint probability matrix, computed only for the k nearest-neighbors and symmetrized. degrees_of_freedom : int Degrees of freedom of the Student's-t distribution. n_samples : int Number of samples. n_components : int Dimension of the embedded space. angle : float (default: 0.5) This is the trade-off between speed and accuracy for Barnes-Hut T-SNE. 'angle' is the angular size (referred to as theta in [3]) of a distant node as measured from a point. If this size is below 'angle' then it is used as a summary node of all points contained within it. This method is not very sensitive to changes in this parameter in the range of 0.2 - 0.8. Angle less than 0.2 has quickly increasing computation time and angle greater 0.8 has quickly increasing error. skip_num_points : int (optional, default:0) This does not compute the gradient for points with indices below `skip_num_points`. This is useful when computing transforms of new data where you'd like to keep the old data fixed. verbose : int Verbosity level. compute_error: bool (optional, default:True) If False, the kl_divergence is not computed and returns NaN. Returns ------- kl_divergence : float Kullback-Leibler divergence of p_ij and q_ij. grad : array, shape (n_params,) Unraveled gradient of the Kullback-Leibler divergence with respect to the embedding. """ params = params.astype(np.float32, copy=False) X_embedded = params.reshape(n_samples, n_components) val_P = P.data.astype(np.float32, copy=False) neighbors = P.indices.astype(np.int64, copy=False) indptr = P.indptr.astype(np.int64, copy=False) grad = np.zeros(X_embedded.shape, dtype=np.float32) error = _barnes_hut_tsne.gradient(val_P, X_embedded, neighbors, indptr, grad, angle, n_components, verbose, dof=degrees_of_freedom, compute_error=compute_error) c = 2.0 * (degrees_of_freedom + 1.0) / degrees_of_freedom grad = grad.ravel() grad *= c return error, grad def _gradient_descent(objective, p0, it, n_iter, n_iter_check=1, n_iter_without_progress=300, momentum=0.8, learning_rate=200.0, min_gain=0.01, min_grad_norm=1e-7, verbose=0, args=None, kwargs=None): """Batch gradient descent with momentum and individual gains. Parameters ---------- objective : function or callable Should return a tuple of cost and gradient for a given parameter vector. When expensive to compute, the cost can optionally be None and can be computed every n_iter_check steps using the objective_error function. p0 : array-like, shape (n_params,) Initial parameter vector. it : int Current number of iterations (this function will be called more than once during the optimization). n_iter : int Maximum number of gradient descent iterations. n_iter_check : int Number of iterations before evaluating the global error. If the error is sufficiently low, we abort the optimization. n_iter_without_progress : int, optional (default: 300) Maximum number of iterations without progress before we abort the optimization. momentum : float, within (0.0, 1.0), optional (default: 0.8) The momentum generates a weight for previous gradients that decays exponentially. learning_rate : float, optional (default: 200.0) The learning rate for t-SNE is usually in the range [10.0, 1000.0]. If the learning rate is too high, the data may look like a 'ball' with any point approximately equidistant from its nearest neighbours. If the learning rate is too low, most points may look compressed in a dense cloud with few outliers. min_gain : float, optional (default: 0.01) Minimum individual gain for each parameter. min_grad_norm : float, optional (default: 1e-7) If the gradient norm is below this threshold, the optimization will be aborted. verbose : int, optional (default: 0) Verbosity level. args : sequence Arguments to pass to objective function. kwargs : dict Keyword arguments to pass to objective function. Returns ------- p : array, shape (n_params,) Optimum parameters. error : float Optimum. i : int Last iteration. """ if args is None: args = [] if kwargs is None: kwargs = {} p = p0.copy().ravel() update = np.zeros_like(p) gains = np.ones_like(p) error = np.finfo(np.float).max best_error = np.finfo(np.float).max best_iter = i = it tic = time() for i in range(it, n_iter): check_convergence = (i + 1) % n_iter_check == 0 # only compute the error when needed kwargs['compute_error'] = check_convergence or i == n_iter - 1 error, grad = objective(p, *args, **kwargs) grad_norm = linalg.norm(grad) inc = update * grad < 0.0 dec = np.invert(inc) gains[inc] += 0.2 gains[dec] *= 0.8 np.clip(gains, min_gain, np.inf, out=gains) grad *= gains update = momentum * update - learning_rate * grad p += update if check_convergence: toc = time() duration = toc - tic tic = toc if verbose >= 2: print("[t-SNE] Iteration %d: error = %.7f," " gradient norm = %.7f" " (%s iterations in %0.3fs)" % (i + 1, error, grad_norm, n_iter_check, duration)) if error < best_error: best_error = error best_iter = i elif i - best_iter > n_iter_without_progress: if verbose >= 2: print("[t-SNE] Iteration %d: did not make any progress " "during the last %d episodes. Finished." % (i + 1, n_iter_without_progress)) break if grad_norm <= min_grad_norm: if verbose >= 2: print("[t-SNE] Iteration %d: gradient norm %f. Finished." % (i + 1, grad_norm)) break return p, error, i def trustworthiness(X, X_embedded, n_neighbors=5, precomputed=False, metric='euclidean'): r"""Expresses to what extent the local structure is retained. The trustworthiness is within [0, 1]. It is defined as .. math:: T(k) = 1 - \frac{2}{nk (2n - 3k - 1)} \sum^n_{i=1} \sum_{j \in \mathcal{N}_{i}^{k}} \max(0, (r(i, j) - k)) where for each sample i, :math:`\mathcal{N}_{i}^{k}` are its k nearest neighbors in the output space, and every sample j is its :math:`r(i, j)`-th nearest neighbor in the input space. In other words, any unexpected nearest neighbors in the output space are penalised in proportion to their rank in the input space. * "Neighborhood Preservation in Nonlinear Projection Methods: An Experimental Study" J. Venna, S. Kaski * "Learning a Parametric Embedding by Preserving Local Structure" L.J.P. van der Maaten Parameters ---------- X : array, shape (n_samples, n_features) or (n_samples, n_samples) If the metric is 'precomputed' X must be a square distance matrix. Otherwise it contains a sample per row. X_embedded : array, shape (n_samples, n_components) Embedding of the training data in low-dimensional space. n_neighbors : int, optional (default: 5) Number of neighbors k that will be considered. precomputed : bool, optional (default: False) Set this flag if X is a precomputed square distance matrix. ..deprecated:: 0.20 ``precomputed`` has been deprecated in version 0.20 and will be removed in version 0.22. Use ``metric`` instead. metric : string, or callable, optional, default 'euclidean' Which metric to use for computing pairwise distances between samples from the original input space. If metric is 'precomputed', X must be a matrix of pairwise distances or squared distances. Otherwise, see the documentation of argument metric in sklearn.pairwise.pairwise_distances for a list of available metrics. Returns ------- trustworthiness : float Trustworthiness of the low-dimensional embedding. """ if precomputed: warnings.warn("The flag 'precomputed' has been deprecated in version " "0.20 and will be removed in 0.22. See 'metric' " "parameter instead.", DeprecationWarning) metric = 'precomputed' dist_X = pairwise_distances(X, metric=metric) ind_X = np.argsort(dist_X, axis=1) ind_X_embedded = NearestNeighbors(n_neighbors).fit(X_embedded).kneighbors( return_distance=False) n_samples = X.shape[0] t = 0.0 ranks = np.zeros(n_neighbors) for i in range(n_samples): for j in range(n_neighbors): ranks[j] = np.where(ind_X[i] == ind_X_embedded[i, j])[0][0] ranks -= n_neighbors t += np.sum(ranks[ranks > 0]) t = 1.0 - t * (2.0 / (n_samples * n_neighbors * (2.0 * n_samples - 3.0 * n_neighbors - 1.0))) return t class TSNE(BaseEstimator): """t-distributed Stochastic Neighbor Embedding. t-SNE [1] is a tool to visualize high-dimensional data. It converts similarities between data points to joint probabilities and tries to minimize the Kullback-Leibler divergence between the joint probabilities of the low-dimensional embedding and the high-dimensional data. t-SNE has a cost function that is not convex, i.e. with different initializations we can get different results. It is highly recommended to use another dimensionality reduction method (e.g. PCA for dense data or TruncatedSVD for sparse data) to reduce the number of dimensions to a reasonable amount (e.g. 50) if the number of features is very high. This will suppress some noise and speed up the computation of pairwise distances between samples. For more tips see Laurens van der Maaten's FAQ [2]. Read more in the :ref:`User Guide <t_sne>`. Parameters ---------- n_components : int, optional (default: 2) Dimension of the embedded space. perplexity : float, optional (default: 30) The perplexity is related to the number of nearest neighbors that is used in other manifold learning algorithms. Larger datasets usually require a larger perplexity. Consider selecting a value between 5 and 50. The choice is not extremely critical since t-SNE is quite insensitive to this parameter. early_exaggeration : float, optional (default: 12.0) Controls how tight natural clusters in the original space are in the embedded space and how much space will be between them. For larger values, the space between natural clusters will be larger in the embedded space. Again, the choice of this parameter is not very critical. If the cost function increases during initial optimization, the early exaggeration factor or the learning rate might be too high. learning_rate : float, optional (default: 200.0) The learning rate for t-SNE is usually in the range [10.0, 1000.0]. If the learning rate is too high, the data may look like a 'ball' with any point approximately equidistant from its nearest neighbours. If the learning rate is too low, most points may look compressed in a dense cloud with few outliers. If the cost function gets stuck in a bad local minimum increasing the learning rate may help. n_iter : int, optional (default: 1000) Maximum number of iterations for the optimization. Should be at least 250. n_iter_without_progress : int, optional (default: 300) Maximum number of iterations without progress before we abort the optimization, used after 250 initial iterations with early exaggeration. Note that progress is only checked every 50 iterations so this value is rounded to the next multiple of 50. .. versionadded:: 0.17 parameter *n_iter_without_progress* to control stopping criteria. min_grad_norm : float, optional (default: 1e-7) If the gradient norm is below this threshold, the optimization will be stopped. metric : string or callable, optional The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by scipy.spatial.distance.pdist for its metric parameter, or a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. If metric is "precomputed", X is assumed to be a distance matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. The default is "euclidean" which is interpreted as squared euclidean distance. init : string or numpy array, optional (default: "random") Initialization of embedding. Possible options are 'random', 'pca', and a numpy array of shape (n_samples, n_components). PCA initialization cannot be used with precomputed distances and is usually more globally stable than random initialization. verbose : int, optional (default: 0) Verbosity level. 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`. Note that different initializations might result in different local minima of the cost function. method : string (default: 'barnes_hut') By default the gradient calculation algorithm uses Barnes-Hut approximation running in O(NlogN) time. method='exact' will run on the slower, but exact, algorithm in O(N^2) time. The exact algorithm should be used when nearest-neighbor errors need to be better than 3%. However, the exact method cannot scale to millions of examples. .. versionadded:: 0.17 Approximate optimization *method* via the Barnes-Hut. angle : float (default: 0.5) Only used if method='barnes_hut' This is the trade-off between speed and accuracy for Barnes-Hut T-SNE. 'angle' is the angular size (referred to as theta in [3]) of a distant node as measured from a point. If this size is below 'angle' then it is used as a summary node of all points contained within it. This method is not very sensitive to changes in this parameter in the range of 0.2 - 0.8. Angle less than 0.2 has quickly increasing computation time and angle greater 0.8 has quickly increasing error. Attributes ---------- embedding_ : array-like, shape (n_samples, n_components) Stores the embedding vectors. kl_divergence_ : float Kullback-Leibler divergence after optimization. n_iter_ : int Number of iterations run. Examples -------- >>> import numpy as np >>> from sklearn.manifold import TSNE >>> X = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]]) >>> X_embedded = TSNE(n_components=2).fit_transform(X) >>> X_embedded.shape (4, 2) References ---------- [1] van der Maaten, L.J.P.; Hinton, G.E. Visualizing High-Dimensional Data Using t-SNE. Journal of Machine Learning Research 9:2579-2605, 2008. [2] van der Maaten, L.J.P. t-Distributed Stochastic Neighbor Embedding https://lvdmaaten.github.io/tsne/ [3] L.J.P. van der Maaten. Accelerating t-SNE using Tree-Based Algorithms. Journal of Machine Learning Research 15(Oct):3221-3245, 2014. http://lvdmaaten.github.io/publications/papers/JMLR_2014.pdf """ # Control the number of exploration iterations with early_exaggeration on _EXPLORATION_N_ITER = 250 # Control the number of iterations between progress checks _N_ITER_CHECK = 50 def __init__(self, n_components=2, perplexity=30.0, early_exaggeration=12.0, learning_rate=200.0, n_iter=1000, n_iter_without_progress=300, min_grad_norm=1e-7, metric="euclidean", init="random", verbose=0, random_state=None, method='barnes_hut', angle=0.5): self.n_components = n_components self.perplexity = perplexity self.early_exaggeration = early_exaggeration self.learning_rate = learning_rate self.n_iter = n_iter self.n_iter_without_progress = n_iter_without_progress self.min_grad_norm = min_grad_norm self.metric = metric self.init = init self.verbose = verbose self.random_state = random_state self.method = method self.angle = angle def _fit(self, X, skip_num_points=0): """Fit the model using X as training data. Note that sparse arrays can only be handled by method='exact'. It is recommended that you convert your sparse array to dense (e.g. `X.toarray()`) if it fits in memory, or otherwise using a dimensionality reduction technique (e.g. TruncatedSVD). Parameters ---------- X : array, shape (n_samples, n_features) or (n_samples, n_samples) If the metric is 'precomputed' X must be a square distance matrix. Otherwise it contains a sample per row. Note that this when method='barnes_hut', X cannot be a sparse array and if need be will be converted to a 32 bit float array. Method='exact' allows sparse arrays and 64bit floating point inputs. skip_num_points : int (optional, default:0) This does not compute the gradient for points with indices below `skip_num_points`. This is useful when computing transforms of new data where you'd like to keep the old data fixed. """ if self.method not in ['barnes_hut', 'exact']: raise ValueError("'method' must be 'barnes_hut' or 'exact'") if self.angle < 0.0 or self.angle > 1.0: raise ValueError("'angle' must be between 0.0 - 1.0") if self.metric == "precomputed": if isinstance(self.init, string_types) and self.init == 'pca': raise ValueError("The parameter init=\"pca\" cannot be " "used with metric=\"precomputed\".") if X.shape[0] != X.shape[1]: raise ValueError("X should be a square distance matrix") if np.any(X < 0): raise ValueError("All distances should be positive, the " "precomputed distances given as X is not " "correct") if self.method == 'barnes_hut' and sp.issparse(X): raise TypeError('A sparse matrix was passed, but dense ' 'data is required for method="barnes_hut". Use ' 'X.toarray() to convert to a dense numpy array if ' 'the array is small enough for it to fit in ' 'memory. Otherwise consider dimensionality ' 'reduction techniques (e.g. TruncatedSVD)') if self.method == 'barnes_hut': X = check_array(X, ensure_min_samples=2, dtype=[np.float32, np.float64]) else: X = check_array(X, accept_sparse=['csr', 'csc', 'coo'], dtype=[np.float32, np.float64]) if self.method == 'barnes_hut' and self.n_components > 3: raise ValueError("'n_components' should be inferior to 4 for the " "barnes_hut algorithm as it relies on " "quad-tree or oct-tree.") random_state = check_random_state(self.random_state) if self.early_exaggeration < 1.0: raise ValueError("early_exaggeration must be at least 1, but is {}" .format(self.early_exaggeration)) if self.n_iter < 250: raise ValueError("n_iter should be at least 250") n_samples = X.shape[0] neighbors_nn = None if self.method == "exact": # Retrieve the distance matrix, either using the precomputed one or # computing it. if self.metric == "precomputed": distances = X else: if self.verbose: print("[t-SNE] Computing pairwise distances...") if self.metric == "euclidean": distances = pairwise_distances(X, metric=self.metric, squared=True) else: distances = pairwise_distances(X, metric=self.metric) if np.any(distances < 0): raise ValueError("All distances should be positive, the " "metric given is not correct") # compute the joint probability distribution for the input space P = _joint_probabilities(distances, self.perplexity, self.verbose) assert np.all(np.isfinite(P)), "All probabilities should be finite" assert np.all(P >= 0), "All probabilities should be non-negative" assert np.all(P <= 1), ("All probabilities should be less " "or then equal to one") else: # Cpmpute the number of nearest neighbors to find. # LvdM uses 3 * perplexity as the number of neighbors. # In the event that we have very small # of points # set the neighbors to n - 1. k = min(n_samples - 1, int(3. * self.perplexity + 1)) if self.verbose: print("[t-SNE] Computing {} nearest neighbors...".format(k)) # Find the nearest neighbors for every point knn = NearestNeighbors(algorithm='auto', n_neighbors=k, metric=self.metric) t0 = time() knn.fit(X) duration = time() - t0 if self.verbose: print("[t-SNE] Indexed {} samples in {:.3f}s...".format( n_samples, duration)) t0 = time() distances_nn, neighbors_nn = knn.kneighbors( None, n_neighbors=k) duration = time() - t0 if self.verbose: print("[t-SNE] Computed neighbors for {} samples in {:.3f}s..." .format(n_samples, duration)) # Free the memory used by the ball_tree del knn if self.metric == "euclidean": # knn return the euclidean distance but we need it squared # to be consistent with the 'exact' method. Note that the # the method was derived using the euclidean method as in the # input space. Not sure of the implication of using a different # metric. distances_nn **= 2 # compute the joint probability distribution for the input space P = _joint_probabilities_nn(distances_nn, neighbors_nn, self.perplexity, self.verbose) if isinstance(self.init, np.ndarray): X_embedded = self.init elif self.init == 'pca': pca = PCA(n_components=self.n_components, svd_solver='randomized', random_state=random_state) X_embedded = pca.fit_transform(X).astype(np.float32, copy=False) elif self.init == 'random': # The embedding is initialized with iid samples from Gaussians with # standard deviation 1e-4. X_embedded = 1e-4 * random_state.randn( n_samples, self.n_components).astype(np.float32) else: raise ValueError("'init' must be 'pca', 'random', or " "a numpy array") # Degrees of freedom of the Student's t-distribution. The suggestion # degrees_of_freedom = n_components - 1 comes from # "Learning a Parametric Embedding by Preserving Local Structure" # Laurens van der Maaten, 2009. degrees_of_freedom = max(self.n_components - 1, 1) return self._tsne(P, degrees_of_freedom, n_samples, X_embedded=X_embedded, neighbors=neighbors_nn, skip_num_points=skip_num_points) @property @deprecated("Attribute n_iter_final was deprecated in version 0.19 and " "will be removed in 0.21. Use ``n_iter_`` instead") def n_iter_final(self): return self.n_iter_ def _tsne(self, P, degrees_of_freedom, n_samples, X_embedded, neighbors=None, skip_num_points=0): """Runs t-SNE.""" # t-SNE minimizes the Kullback-Leiber divergence of the Gaussians P # and the Student's t-distributions Q. The optimization algorithm that # we use is batch gradient descent with two stages: # * initial optimization with early exaggeration and momentum at 0.5 # * final optimization with momentum at 0.8 params = X_embedded.ravel() opt_args = { "it": 0, "n_iter_check": self._N_ITER_CHECK, "min_grad_norm": self.min_grad_norm, "learning_rate": self.learning_rate, "verbose": self.verbose, "kwargs": dict(skip_num_points=skip_num_points), "args": [P, degrees_of_freedom, n_samples, self.n_components], "n_iter_without_progress": self._EXPLORATION_N_ITER, "n_iter": self._EXPLORATION_N_ITER, "momentum": 0.5, } if self.method == 'barnes_hut': obj_func = _kl_divergence_bh opt_args['kwargs']['angle'] = self.angle # Repeat verbose argument for _kl_divergence_bh opt_args['kwargs']['verbose'] = self.verbose else: obj_func = _kl_divergence # Learning schedule (part 1): do 250 iteration with lower momentum but # higher learning rate controlled via the early exageration parameter P *= self.early_exaggeration params, kl_divergence, it = _gradient_descent(obj_func, params, **opt_args) if self.verbose: print("[t-SNE] KL divergence after %d iterations with early " "exaggeration: %f" % (it + 1, kl_divergence)) # Learning schedule (part 2): disable early exaggeration and finish # optimization with a higher momentum at 0.8 P /= self.early_exaggeration remaining = self.n_iter - self._EXPLORATION_N_ITER if it < self._EXPLORATION_N_ITER or remaining > 0: opt_args['n_iter'] = self.n_iter opt_args['it'] = it + 1 opt_args['momentum'] = 0.8 opt_args['n_iter_without_progress'] = self.n_iter_without_progress params, kl_divergence, it = _gradient_descent(obj_func, params, **opt_args) # Save the final number of iterations self.n_iter_ = it if self.verbose: print("[t-SNE] KL divergence after %d iterations: %f" % (it + 1, kl_divergence)) X_embedded = params.reshape(n_samples, self.n_components) self.kl_divergence_ = kl_divergence return X_embedded def fit_transform(self, X, y=None): """Fit X into an embedded space and return that transformed output. Parameters ---------- X : array, shape (n_samples, n_features) or (n_samples, n_samples) If the metric is 'precomputed' X must be a square distance matrix. Otherwise it contains a sample per row. y : Ignored Returns ------- X_new : array, shape (n_samples, n_components) Embedding of the training data in low-dimensional space. """ embedding = self._fit(X) self.embedding_ = embedding return self.embedding_ def fit(self, X, y=None): """Fit X into an embedded space. Parameters ---------- X : array, shape (n_samples, n_features) or (n_samples, n_samples) If the metric is 'precomputed' X must be a square distance matrix. Otherwise it contains a sample per row. If the method is 'exact', X may be a sparse matrix of type 'csr', 'csc' or 'coo'. y : Ignored """ self.fit_transform(X) return self
bsd-3-clause
GuessWhoSamFoo/pandas
pandas/tests/indexing/test_timedelta.py
4
3710
import numpy as np import pytest import pandas as pd from pandas.util import testing as tm class TestTimedeltaIndexing(object): def test_boolean_indexing(self): # GH 14946 df = pd.DataFrame({'x': range(10)}) df.index = pd.to_timedelta(range(10), unit='s') conditions = [df['x'] > 3, df['x'] == 3, df['x'] < 3] expected_data = [[0, 1, 2, 3, 10, 10, 10, 10, 10, 10], [0, 1, 2, 10, 4, 5, 6, 7, 8, 9], [10, 10, 10, 3, 4, 5, 6, 7, 8, 9]] for cond, data in zip(conditions, expected_data): result = df.assign(x=df.mask(cond, 10).astype('int64')) expected = pd.DataFrame(data, index=pd.to_timedelta(range(10), unit='s'), columns=['x'], dtype='int64') tm.assert_frame_equal(expected, result) @pytest.mark.parametrize( "indexer, expected", [(0, [20, 1, 2, 3, 4, 5, 6, 7, 8, 9]), (slice(4, 8), [0, 1, 2, 3, 20, 20, 20, 20, 8, 9]), ([3, 5], [0, 1, 2, 20, 4, 20, 6, 7, 8, 9])]) def test_list_like_indexing(self, indexer, expected): # GH 16637 df = pd.DataFrame({'x': range(10)}, dtype="int64") df.index = pd.to_timedelta(range(10), unit='s') df.loc[df.index[indexer], 'x'] = 20 expected = pd.DataFrame(expected, index=pd.to_timedelta(range(10), unit='s'), columns=['x'], dtype="int64") tm.assert_frame_equal(expected, df) def test_string_indexing(self): # GH 16896 df = pd.DataFrame({'x': range(3)}, index=pd.to_timedelta(range(3), unit='days')) expected = df.iloc[0] sliced = df.loc['0 days'] tm.assert_series_equal(sliced, expected) @pytest.mark.parametrize( "value", [None, pd.NaT, np.nan]) def test_masked_setitem(self, value): # issue (#18586) series = pd.Series([0, 1, 2], dtype='timedelta64[ns]') series[series == series[0]] = value expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]') tm.assert_series_equal(series, expected) @pytest.mark.parametrize( "value", [None, pd.NaT, np.nan]) def test_listlike_setitem(self, value): # issue (#18586) series = pd.Series([0, 1, 2], dtype='timedelta64[ns]') series.iloc[0] = value expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]') tm.assert_series_equal(series, expected) @pytest.mark.parametrize('start,stop, expected_slice', [ [np.timedelta64(0, 'ns'), None, slice(0, 11)], [np.timedelta64(1, 'D'), np.timedelta64(6, 'D'), slice(1, 7)], [None, np.timedelta64(4, 'D'), slice(0, 5)]]) def test_numpy_timedelta_scalar_indexing(self, start, stop, expected_slice): # GH 20393 s = pd.Series(range(11), pd.timedelta_range('0 days', '10 days')) result = s.loc[slice(start, stop)] expected = s.iloc[expected_slice] tm.assert_series_equal(result, expected) def test_roundtrip_thru_setitem(self): # PR 23462 dt1 = pd.Timedelta(0) dt2 = pd.Timedelta(28767471428571405) df = pd.DataFrame({'dt': pd.Series([dt1, dt2])}) df_copy = df.copy() s = pd.Series([dt1]) expected = df['dt'].iloc[1].value df.loc[[True, False]] = s result = df['dt'].iloc[1].value assert expected == result tm.assert_frame_equal(df, df_copy)
bsd-3-clause
kiyoto/statsmodels
statsmodels/examples/ex_kernel_test_functional.py
34
2246
# -*- coding: utf-8 -*- """ Created on Tue Jan 08 19:03:20 2013 Author: Josef Perktold """ from __future__ import print_function if __name__ == '__main__': import numpy as np from statsmodels.regression.linear_model import OLS #from statsmodels.nonparametric.api import KernelReg import statsmodels.sandbox.nonparametric.kernel_extras as smke seed = np.random.randint(999999) #seed = 661176 print(seed) np.random.seed(seed) sig_e = 0.5 #0.1 nobs, k_vars = 200, 1 x = np.random.uniform(-2, 2, size=(nobs, k_vars)) x.sort() order = 3 exog = x**np.arange(order + 1) beta = np.array([1, 1, 0.1, 0.0])[:order+1] # 1. / np.arange(1, order + 2) y_true = np.dot(exog, beta) y = y_true + sig_e * np.random.normal(size=nobs) endog = y print('DGP') print('nobs=%d, beta=%r, sig_e=%3.1f' % (nobs, beta, sig_e)) mod_ols = OLS(endog, exog[:,:2]) res_ols = mod_ols.fit() #'cv_ls'[1000, 0.5][0.01, 0.45] tst = smke.TestFForm(endog, exog[:,:2], bw=[0.01, 0.45], var_type='cc', fform=lambda x,p: mod_ols.predict(p,x), estimator=lambda y,x: OLS(y,x).fit().params, nboot=1000) print('bw', tst.bw) print('tst.test_stat', tst.test_stat) print(tst.sig) print('tst.boots_results mean, min, max', (tst.boots_results.mean(), tst.boots_results.min(), tst.boots_results.max())) print('lower tail bootstrap p-value', (tst.boots_results < tst.test_stat).mean()) print('upper tail bootstrap p-value', (tst.boots_results >= tst.test_stat).mean()) from scipy import stats print('aymp.normal p-value (2-sided)', stats.norm.sf(np.abs(tst.test_stat))*2) print('aymp.normal p-value (upper)', stats.norm.sf(tst.test_stat)) do_plot=True if do_plot: import matplotlib.pyplot as plt plt.figure() plt.plot(x, y, '.') plt.plot(x, res_ols.fittedvalues) plt.title('OLS fit') plt.figure() plt.hist(tst.boots_results.ravel(), bins=20) plt.title('bootstrap histogram or test statistic') plt.show()
bsd-3-clause
abimannans/scikit-learn
examples/tree/plot_tree_regression.py
206
1476
""" =================================================================== Decision Tree Regression =================================================================== A 1D regression with decision tree. The :ref:`decision trees <tree>` is used to fit a sine curve with addition noisy observation. As a result, it learns local linear regressions approximating the sine curve. 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 the necessary modules and libraries import numpy as np from sklearn.tree import DecisionTreeRegressor import matplotlib.pyplot as plt # Create a random dataset rng = np.random.RandomState(1) X = np.sort(5 * rng.rand(80, 1), axis=0) y = np.sin(X).ravel() y[::5] += 3 * (0.5 - rng.rand(16)) # Fit regression model regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5) regr_1.fit(X, y) regr_2.fit(X, y) # Predict X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test) # Plot the results plt.figure() plt.scatter(X, y, c="k", label="data") plt.plot(X_test, y_1, c="g", label="max_depth=2", linewidth=2) plt.plot(X_test, y_2, c="r", label="max_depth=5", linewidth=2) plt.xlabel("data") plt.ylabel("target") plt.title("Decision Tree Regression") plt.legend() plt.show()
bsd-3-clause
davidgbe/scikit-learn
sklearn/kernel_ridge.py
155
6545
"""Module :mod:`sklearn.kernel_ridge` implements kernel ridge regression.""" # Authors: Mathieu Blondel <mathieu@mblondel.org> # Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # License: BSD 3 clause import numpy as np from .base import BaseEstimator, RegressorMixin from .metrics.pairwise import pairwise_kernels from .linear_model.ridge import _solve_cholesky_kernel from .utils import check_X_y from .utils.validation import check_is_fitted class KernelRidge(BaseEstimator, RegressorMixin): """Kernel ridge regression. Kernel ridge regression (KRR) combines ridge regression (linear least squares with l2-norm regularization) with the kernel trick. It thus learns a linear function in the space induced by the respective kernel and the data. For non-linear kernels, this corresponds to a non-linear function in the original space. The form of the model learned by KRR is identical to support vector regression (SVR). However, different loss functions are used: KRR uses squared error loss while support vector regression uses epsilon-insensitive loss, both combined with l2 regularization. In contrast to SVR, fitting a KRR model can be done in closed-form and is typically faster for medium-sized datasets. On the other hand, the learned model is non-sparse and thus slower than SVR, which learns a sparse model for epsilon > 0, at prediction-time. This estimator has built-in support for multi-variate regression (i.e., when y is a 2d-array of shape [n_samples, n_targets]). Read more in the :ref:`User Guide <kernel_ridge>`. Parameters ---------- alpha : {float, array-like}, shape = [n_targets] Small positive values of alpha improve the conditioning of the problem and reduce the variance of the estimates. Alpha corresponds to ``(2*C)^-1`` in other linear models such as LogisticRegression or LinearSVC. If an array is passed, penalties are assumed to be specific to the targets. Hence they must correspond in number. kernel : string or callable, default="linear" Kernel mapping used internally. A callable should accept two arguments and the keyword arguments passed to this object as kernel_params, and should return a floating point number. gamma : float, default=None Gamma parameter for the RBF, polynomial, exponential chi2 and sigmoid kernels. Interpretation of the default value is left to the kernel; see the documentation for sklearn.metrics.pairwise. Ignored by other kernels. degree : float, default=3 Degree of the polynomial kernel. Ignored by other kernels. coef0 : float, default=1 Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels. kernel_params : mapping of string to any, optional Additional parameters (keyword arguments) for kernel function passed as callable object. Attributes ---------- dual_coef_ : array, shape = [n_features] or [n_targets, n_features] Weight vector(s) in kernel space X_fit_ : {array-like, sparse matrix}, shape = [n_samples, n_features] Training data, which is also required for prediction References ---------- * Kevin P. Murphy "Machine Learning: A Probabilistic Perspective", The MIT Press chapter 14.4.3, pp. 492-493 See also -------- Ridge Linear ridge regression. SVR Support Vector Regression implemented using libsvm. Examples -------- >>> from sklearn.kernel_ridge import KernelRidge >>> import numpy as np >>> n_samples, n_features = 10, 5 >>> rng = np.random.RandomState(0) >>> y = rng.randn(n_samples) >>> X = rng.randn(n_samples, n_features) >>> clf = KernelRidge(alpha=1.0) >>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE KernelRidge(alpha=1.0, coef0=1, degree=3, gamma=None, kernel='linear', kernel_params=None) """ def __init__(self, alpha=1, kernel="linear", gamma=None, degree=3, coef0=1, kernel_params=None): self.alpha = alpha self.kernel = kernel self.gamma = gamma self.degree = degree self.coef0 = coef0 self.kernel_params = kernel_params def _get_kernel(self, X, Y=None): if callable(self.kernel): params = self.kernel_params or {} else: params = {"gamma": self.gamma, "degree": self.degree, "coef0": self.coef0} return pairwise_kernels(X, Y, metric=self.kernel, filter_params=True, **params) @property def _pairwise(self): return self.kernel == "precomputed" def fit(self, X, y=None, sample_weight=None): """Fit Kernel Ridge regression model Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training data y : array-like, shape = [n_samples] or [n_samples, n_targets] Target values sample_weight : float or numpy array of shape [n_samples] Individual weights for each sample, ignored if None is passed. Returns ------- self : returns an instance of self. """ # Convert data X, y = check_X_y(X, y, accept_sparse=("csr", "csc"), multi_output=True, y_numeric=True) K = self._get_kernel(X) alpha = np.atleast_1d(self.alpha) ravel = False if len(y.shape) == 1: y = y.reshape(-1, 1) ravel = True copy = self.kernel == "precomputed" self.dual_coef_ = _solve_cholesky_kernel(K, y, alpha, sample_weight, copy) if ravel: self.dual_coef_ = self.dual_coef_.ravel() self.X_fit_ = X return self def predict(self, X): """Predict using the the kernel ridge model Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Samples. Returns ------- C : array, shape = [n_samples] or [n_samples, n_targets] Returns predicted values. """ check_is_fitted(self, ["X_fit_", "dual_coef_"]) K = self._get_kernel(X, self.X_fit_) return np.dot(K, self.dual_coef_)
bsd-3-clause
kenshay/ImageScripter
ProgramData/SystemFiles/Python/Lib/site-packages/skimage/viewer/canvastools/recttool.py
43
8886
from matplotlib.widgets import RectangleSelector from ...viewer.canvastools.base import CanvasToolBase from ...viewer.canvastools.base import ToolHandles __all__ = ['RectangleTool'] class RectangleTool(CanvasToolBase, RectangleSelector): """Widget for selecting a rectangular region in a plot. After making the desired selection, press "Enter" to accept the selection and call the `on_enter` callback function. Parameters ---------- manager : Viewer or PlotPlugin. Skimage viewer or plot plugin object. on_move : function Function called whenever a control handle is moved. This function must accept the rectangle extents as the only argument. on_release : function Function called whenever the control handle is released. on_enter : function Function called whenever the "enter" key is pressed. maxdist : float Maximum pixel distance allowed when selecting control handle. rect_props : dict Properties for :class:`matplotlib.patches.Rectangle`. This class redefines defaults in :class:`matplotlib.widgets.RectangleSelector`. Attributes ---------- extents : tuple Rectangle extents: (xmin, xmax, ymin, ymax). Examples ---------- >>> from skimage import data >>> from skimage.viewer import ImageViewer >>> from skimage.viewer.canvastools import RectangleTool >>> from skimage.draw import line >>> from skimage.draw import set_color >>> viewer = ImageViewer(data.coffee()) # doctest: +SKIP >>> def print_the_rect(extents): ... global viewer ... im = viewer.image ... coord = np.int64(extents) ... [rr1, cc1] = line(coord[2],coord[0],coord[2],coord[1]) ... [rr2, cc2] = line(coord[2],coord[1],coord[3],coord[1]) ... [rr3, cc3] = line(coord[3],coord[1],coord[3],coord[0]) ... [rr4, cc4] = line(coord[3],coord[0],coord[2],coord[0]) ... set_color(im, (rr1, cc1), [255, 255, 0]) ... set_color(im, (rr2, cc2), [0, 255, 255]) ... set_color(im, (rr3, cc3), [255, 0, 255]) ... set_color(im, (rr4, cc4), [0, 0, 0]) ... viewer.image=im >>> rect_tool = RectangleTool(viewer, on_enter=print_the_rect) # doctest: +SKIP >>> viewer.show() # doctest: +SKIP """ def __init__(self, manager, on_move=None, on_release=None, on_enter=None, maxdist=10, rect_props=None): self._rect = None props = dict(edgecolor=None, facecolor='r', alpha=0.15) props.update(rect_props if rect_props is not None else {}) if props['edgecolor'] is None: props['edgecolor'] = props['facecolor'] RectangleSelector.__init__(self, manager.ax, lambda *args: None, rectprops=props) CanvasToolBase.__init__(self, manager, on_move=on_move, on_enter=on_enter, on_release=on_release) # Events are handled by the viewer try: self.disconnect_events() except AttributeError: # disconnect the events manually (hack for older mpl versions) [self.canvas.mpl_disconnect(i) for i in range(10)] # Alias rectangle attribute, which is initialized in RectangleSelector. self._rect = self.to_draw self._rect.set_animated(True) self.maxdist = maxdist self.active_handle = None self._extents_on_press = None if on_enter is None: def on_enter(extents): print("(xmin=%.3g, xmax=%.3g, ymin=%.3g, ymax=%.3g)" % extents) self.callback_on_enter = on_enter props = dict(mec=props['edgecolor']) self._corner_order = ['NW', 'NE', 'SE', 'SW'] xc, yc = self.corners self._corner_handles = ToolHandles(self.ax, xc, yc, marker_props=props) self._edge_order = ['W', 'N', 'E', 'S'] xe, ye = self.edge_centers self._edge_handles = ToolHandles(self.ax, xe, ye, marker='s', marker_props=props) self.artists = [self._rect, self._corner_handles.artist, self._edge_handles.artist] self.manager.add_tool(self) @property def _rect_bbox(self): if not self._rect: return 0, 0, 0, 0 x0 = self._rect.get_x() y0 = self._rect.get_y() width = self._rect.get_width() height = self._rect.get_height() return x0, y0, width, height @property def corners(self): """Corners of rectangle from lower left, moving clockwise.""" x0, y0, width, height = self._rect_bbox xc = x0, x0 + width, x0 + width, x0 yc = y0, y0, y0 + height, y0 + height return xc, yc @property def edge_centers(self): """Midpoint of rectangle edges from left, moving clockwise.""" x0, y0, width, height = self._rect_bbox w = width / 2. h = height / 2. xe = x0, x0 + w, x0 + width, x0 + w ye = y0 + h, y0, y0 + h, y0 + height return xe, ye @property def extents(self): """Return (xmin, xmax, ymin, ymax).""" x0, y0, width, height = self._rect_bbox xmin, xmax = sorted([x0, x0 + width]) ymin, ymax = sorted([y0, y0 + height]) return xmin, xmax, ymin, ymax @extents.setter def extents(self, extents): x1, x2, y1, y2 = extents xmin, xmax = sorted([x1, x2]) ymin, ymax = sorted([y1, y2]) # Update displayed rectangle self._rect.set_x(xmin) self._rect.set_y(ymin) self._rect.set_width(xmax - xmin) self._rect.set_height(ymax - ymin) # Update displayed handles self._corner_handles.set_data(*self.corners) self._edge_handles.set_data(*self.edge_centers) self.set_visible(True) self.redraw() def on_mouse_release(self, event): if event.button != 1: return if not self.ax.in_axes(event): self.eventpress = None return RectangleSelector.release(self, event) self._extents_on_press = None # Undo hiding of rectangle and redraw. self.set_visible(True) self.redraw() self.callback_on_release(self.geometry) def on_mouse_press(self, event): if event.button != 1 or not self.ax.in_axes(event): return self._set_active_handle(event) if self.active_handle is None: # Clear previous rectangle before drawing new rectangle. self.set_visible(False) self.redraw() self.set_visible(True) RectangleSelector.press(self, event) def _set_active_handle(self, event): """Set active handle based on the location of the mouse event""" # Note: event.xdata/ydata in data coordinates, event.x/y in pixels c_idx, c_dist = self._corner_handles.closest(event.x, event.y) e_idx, e_dist = self._edge_handles.closest(event.x, event.y) # Set active handle as closest handle, if mouse click is close enough. if c_dist > self.maxdist and e_dist > self.maxdist: self.active_handle = None return elif c_dist < e_dist: self.active_handle = self._corner_order[c_idx] else: self.active_handle = self._edge_order[e_idx] # Save coordinates of rectangle at the start of handle movement. x1, x2, y1, y2 = self.extents # Switch variables so that only x2 and/or y2 are updated on move. if self.active_handle in ['W', 'SW', 'NW']: x1, x2 = x2, event.xdata if self.active_handle in ['N', 'NW', 'NE']: y1, y2 = y2, event.ydata self._extents_on_press = x1, x2, y1, y2 def on_move(self, event): if self.eventpress is None or not self.ax.in_axes(event): return if self.active_handle is None: # New rectangle x1 = self.eventpress.xdata y1 = self.eventpress.ydata x2, y2 = event.xdata, event.ydata else: x1, x2, y1, y2 = self._extents_on_press if self.active_handle in ['E', 'W'] + self._corner_order: x2 = event.xdata if self.active_handle in ['N', 'S'] + self._corner_order: y2 = event.ydata self.extents = (x1, x2, y1, y2) self.callback_on_move(self.geometry) @property def geometry(self): return self.extents if __name__ == '__main__': # pragma: no cover from ...viewer import ImageViewer from ... import data viewer = ImageViewer(data.camera()) rect_tool = RectangleTool(viewer) viewer.show() print("Final selection:") rect_tool.callback_on_enter(rect_tool.extents)
gpl-3.0
lin-credible/scikit-learn
examples/applications/plot_species_distribution_modeling.py
254
7434
""" ============================= Species distribution modeling ============================= Modeling species' geographic distributions is an important problem in conservation biology. In this example we model the geographic distribution of two south american mammals given past observations and 14 environmental variables. Since we have only positive examples (there are no unsuccessful observations), we cast this problem as a density estimation problem and use the `OneClassSVM` provided by the package `sklearn.svm` as our modeling tool. The dataset is provided by Phillips et. al. (2006). If available, the example uses `basemap <http://matplotlib.sourceforge.net/basemap/doc/html/>`_ to plot the coast lines and national boundaries of South America. The two species are: - `"Bradypus variegatus" <http://www.iucnredlist.org/apps/redlist/details/3038/0>`_ , the Brown-throated Sloth. - `"Microryzomys minutus" <http://www.iucnredlist.org/apps/redlist/details/13408/0>`_ , also known as the Forest Small Rice Rat, a rodent that lives in Peru, Colombia, Ecuador, Peru, and Venezuela. References ---------- * `"Maximum entropy modeling of species geographic distributions" <http://www.cs.princeton.edu/~schapire/papers/ecolmod.pdf>`_ S. J. Phillips, R. P. Anderson, R. E. Schapire - Ecological Modelling, 190:231-259, 2006. """ # Authors: Peter Prettenhofer <peter.prettenhofer@gmail.com> # Jake Vanderplas <vanderplas@astro.washington.edu> # # License: BSD 3 clause from __future__ import print_function from time import time import numpy as np import matplotlib.pyplot as plt from sklearn.datasets.base import Bunch from sklearn.datasets import fetch_species_distributions from sklearn.datasets.species_distributions import construct_grids from sklearn import svm, metrics # if basemap is available, we'll use it. # otherwise, we'll improvise later... try: from mpl_toolkits.basemap import Basemap basemap = True except ImportError: basemap = False print(__doc__) def create_species_bunch(species_name, train, test, coverages, xgrid, ygrid): """Create a bunch with information about a particular organism This will use the test/train record arrays to extract the data specific to the given species name. """ bunch = Bunch(name=' '.join(species_name.split("_")[:2])) species_name = species_name.encode('ascii') points = dict(test=test, train=train) for label, pts in points.items(): # choose points associated with the desired species pts = pts[pts['species'] == species_name] bunch['pts_%s' % label] = pts # determine coverage values for each of the training & testing points ix = np.searchsorted(xgrid, pts['dd long']) iy = np.searchsorted(ygrid, pts['dd lat']) bunch['cov_%s' % label] = coverages[:, -iy, ix].T return bunch def plot_species_distribution(species=("bradypus_variegatus_0", "microryzomys_minutus_0")): """ Plot the species distribution. """ if len(species) > 2: print("Note: when more than two species are provided," " only the first two will be used") t0 = time() # Load the compressed data data = fetch_species_distributions() # Set up the data grid xgrid, ygrid = construct_grids(data) # The grid in x,y coordinates X, Y = np.meshgrid(xgrid, ygrid[::-1]) # create a bunch for each species BV_bunch = create_species_bunch(species[0], data.train, data.test, data.coverages, xgrid, ygrid) MM_bunch = create_species_bunch(species[1], data.train, data.test, data.coverages, xgrid, ygrid) # background points (grid coordinates) for evaluation np.random.seed(13) background_points = np.c_[np.random.randint(low=0, high=data.Ny, size=10000), np.random.randint(low=0, high=data.Nx, size=10000)].T # We'll make use of the fact that coverages[6] has measurements at all # land points. This will help us decide between land and water. land_reference = data.coverages[6] # Fit, predict, and plot for each species. for i, species in enumerate([BV_bunch, MM_bunch]): print("_" * 80) print("Modeling distribution of species '%s'" % species.name) # Standardize features mean = species.cov_train.mean(axis=0) std = species.cov_train.std(axis=0) train_cover_std = (species.cov_train - mean) / std # Fit OneClassSVM print(" - fit OneClassSVM ... ", end='') clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.5) clf.fit(train_cover_std) print("done.") # Plot map of South America plt.subplot(1, 2, i + 1) if basemap: print(" - plot coastlines using basemap") m = Basemap(projection='cyl', llcrnrlat=Y.min(), urcrnrlat=Y.max(), llcrnrlon=X.min(), urcrnrlon=X.max(), resolution='c') m.drawcoastlines() m.drawcountries() else: print(" - plot coastlines from coverage") plt.contour(X, Y, land_reference, levels=[-9999], colors="k", linestyles="solid") plt.xticks([]) plt.yticks([]) print(" - predict species distribution") # Predict species distribution using the training data Z = np.ones((data.Ny, data.Nx), dtype=np.float64) # We'll predict only for the land points. idx = np.where(land_reference > -9999) coverages_land = data.coverages[:, idx[0], idx[1]].T pred = clf.decision_function((coverages_land - mean) / std)[:, 0] Z *= pred.min() Z[idx[0], idx[1]] = pred levels = np.linspace(Z.min(), Z.max(), 25) Z[land_reference == -9999] = -9999 # plot contours of the prediction plt.contourf(X, Y, Z, levels=levels, cmap=plt.cm.Reds) plt.colorbar(format='%.2f') # scatter training/testing points plt.scatter(species.pts_train['dd long'], species.pts_train['dd lat'], s=2 ** 2, c='black', marker='^', label='train') plt.scatter(species.pts_test['dd long'], species.pts_test['dd lat'], s=2 ** 2, c='black', marker='x', label='test') plt.legend() plt.title(species.name) plt.axis('equal') # Compute AUC with regards to background points pred_background = Z[background_points[0], background_points[1]] pred_test = clf.decision_function((species.cov_test - mean) / std)[:, 0] scores = np.r_[pred_test, pred_background] y = np.r_[np.ones(pred_test.shape), np.zeros(pred_background.shape)] fpr, tpr, thresholds = metrics.roc_curve(y, scores) roc_auc = metrics.auc(fpr, tpr) plt.text(-35, -70, "AUC: %.3f" % roc_auc, ha="right") print("\n Area under the ROC curve : %f" % roc_auc) print("\ntime elapsed: %.2fs" % (time() - t0)) plot_species_distribution() plt.show()
bsd-3-clause
akrherz/dep
scripts/cligen/qc_summarize.py
2
7378
"""Need something that prints diagnostics of our climate file""" import sys import datetime import numpy as np import netCDF4 import pytz import pandas as pd import requests from pyiem.dep import read_cli from pyiem.iemre import hourly_offset from pyiem.util import c2f, mm2inch def compute_stage4(lon, lat, year): """Build a daily dataframe for the stage4 data""" nc = netCDF4.Dataset("/mesonet/data/stage4/%s_stage4_hourly.nc" % (year,)) lons = nc.variables["lon"][:] lats = nc.variables["lat"][:] dist = ((lons - lon) ** 2 + (lats - lat) ** 2) ** 0.5 (yidx, xidx) = np.unravel_index(dist.argmin(), dist.shape) print( ("Computed stage4 nclon:%.2f nclat:%.2f yidx:%s xidx:%s ") % (lons[yidx, xidx], lats[yidx, xidx], yidx, xidx) ) p01i = mm2inch(nc.variables["p01m"][:, yidx, xidx]) nc.close() df = pd.DataFrame( {"precip": 0.0}, index=pd.date_range( "%s-01-01" % (year,), "%s-12-31" % (year,), tz="America/Chicago" ), ) for date in df.index.values: date2 = datetime.datetime.utcfromtimestamp(date.tolist() / 1e9) ts = datetime.datetime(date2.year, date2.month, date2.day, 6) ts = ts.replace(tzinfo=pytz.utc) ts = ts.astimezone(pytz.timezone("America/Chicago")) ts = ts.replace(hour=0) ts = ts.astimezone(pytz.utc) tidx = hourly_offset(ts) # values are in the rears val = np.ma.sum(p01i[tidx + 1 : tidx + 25]) if val > 0: df.at[date, "precip"] = val # close enough return df def fn2lonlat(filename): """Convert the filename to lon and lat""" tokens = filename.split("/")[-1].rsplit(".", 1)[0].split("x") return [0 - float(tokens[0]), float(tokens[1])] def do_qc(fn, df, year): """Run some checks on this dataframe""" (lon, lat) = fn2lonlat(fn) stage4 = compute_stage4(lon, lat, year) # Does the frame appear to have all dates? if len(df.index) != len(df.resample("D").mean().index): print("ERROR: Appears to be missing dates!") if open(fn).read()[-1] != "\n": print("ERROR: File does not end with \\n") print("--------- Summary stats from the .cli file") print("YEAR | RAIN | MAXRATE | MAXACC | #DAYS | #>1RT | RAD/D") print(" --- | --- | --- | --- | --- | --- | ---") for _year, gdf in df.groupby(by=df.index.year): print( ("%s | %6.2f | %7.2f | %7.2f | %6i | %6i | %6.0f") % ( _year, mm2inch(gdf["pcpn"].sum()), mm2inch(gdf["maxr"].max()), mm2inch(gdf["pcpn"].max()), len(gdf[gdf["pcpn"] > 0].index), len(gdf[gdf["maxr"] > 25.4].index), gdf["rad"].mean(), ) ) print("---- Months with < 0.05 precipitation ----") gdf = df.groupby(by=[df.index.year, df.index.month])["pcpn"].sum() print(gdf[gdf < 1.0]) print("----- Average high temperature -----") print("YEAR | Avg High F | Avg Low F | Days > 100F") print(" --- | --- | --- | ---") for _year, gdf in df.groupby(by=df.index.year): print( ("%s | %6.2f | %6.2f | %3i") % ( _year, c2f(gdf["tmax"].mean()), c2f(gdf["tmin"].mean()), len(gdf[gdf["tmax"] > 37.7].index), ) ) monthly = df[df.index.year == year]["pcpn"].resample("M").sum().copy() monthly = pd.DataFrame( {"dep": mm2inch(monthly.values)}, index=range(1, 13) ) # Get prism, for a bulk comparison prism = requests.get( ( "http://mesonet.agron.iastate.edu/json/prism/" "%.2f/%.2f/%s0101-%s1231" ) % (lon, lat, year, year) ).json() rows = [] for entry in prism["data"]: rows.append( { "date": datetime.datetime.strptime( entry["valid"][:10], "%Y-%m-%d" ), "precip": entry["precip_in"], } ) prismdf = pd.DataFrame(rows) prismdf.set_index("date", inplace=True) monthly["prism"] = prismdf["precip"].resample("M").sum().copy().values # Compare daily values iemjson = requests.get( ( "http://mesonet.agron.iastate.edu/iemre/multiday/" "%s-01-01/%s-12-31/%s/%s/json" ) % (year, year, lat, lon) ).json() rows = [] for entry in iemjson["data"]: rows.append( { "date": datetime.datetime.strptime(entry["date"], "%Y-%m-%d"), "precip": entry["daily_precip_in"], } ) iemdf = pd.DataFrame(rows) iemdf.set_index("date", inplace=True) print("PRISM %s precip is: %.2f" % (year, prismdf["precip"].sum())) print("IEMRE sum precip is: %.2f" % (iemdf["precip"].sum(),)) print("StageIV sum precip is: %.2f" % (stage4["precip"].sum(),)) monthly["stage4"] = stage4["precip"].resample("M").sum().copy().values monthly["iemre"] = iemdf["precip"].resample("M").sum().copy().values monthly["prism-dep"] = monthly["prism"] - monthly["dep"] monthly["iemre-dep"] = monthly["iemre"] - monthly["dep"] print(" --------- %s Monthly Totals --------" % (year,)) print(monthly) df.at[ slice(datetime.date(year, 1, 1), datetime.date(year, 12, 31)), "stage4_precip", ] = stage4["precip"].values df["iemre_precip"] = iemdf["precip"] df["diff_precip"] = df["pcpn_in"] - df["iemre_precip"] df["diff_stage4"] = df["pcpn_in"] - df["stage4_precip"] print(" --- Top 5 Largest DEP > IEMRE ----") print( df[ [ "diff_precip", "pcpn_in", "iemre_precip", "stage4_precip", "diff_stage4", ] ] .sort_values(by="diff_precip", ascending=False) .head() ) print(" --- Top 5 Largest IEMRE > DEP ----") print( df[ [ "diff_precip", "pcpn_in", "iemre_precip", "stage4_precip", "diff_stage4", ] ] .sort_values(by="diff_precip", ascending=True) .head() ) print(" --- Top 10 Largest Stage4 > DEP ----") print( df[ [ "diff_precip", "pcpn_in", "iemre_precip", "stage4_precip", "diff_stage4", ] ] .sort_values(by="diff_stage4", ascending=True) .head(10) ) print(" vvv job listing based on the above vvv") for dt in df.sort_values(by="diff_stage4", ascending=True).head(10).index: print( "python daily_clifile_editor.py 0 %s %s %s" % (dt.year, dt.month, dt.day) ) df2 = df.loc[slice(datetime.date(year, 1, 1), datetime.date(year, 1, 31))][ ["diff_precip", "pcpn_in", "iemre_precip", "stage4_precip"] ].sort_values(by="diff_precip") print(" --- Daily values for month " "") print(df2) def main(argv): """Do Stuff""" fn = argv[1] year = int(argv[2]) df = read_cli(fn) df["pcpn_in"] = mm2inch(df["pcpn"].values) do_qc(fn, df, year) if __name__ == "__main__": main(sys.argv)
mit
elenita1221/BDA_py_demos
demos_ch2/demo2_4.py
19
2780
"""Bayesian Data Analysis, 3rd ed Chapter 2, demo 4 Calculate the posterior distribution on a discrete grid of points by multiplying the likelihood and a non-conjugate prior at each point, and normalizing over the points. Simulate samples from the resulting non-standard posterior distribution using inverse cdf using the discrete grid. """ import numpy as np from scipy.stats import beta import matplotlib.pyplot as plt # Edit default plot settings (colours from colorbrewer2.org) plt.rc('font', size=14) plt.rc('lines', color='#377eb8', linewidth=2) plt.rc('axes', color_cycle=('#377eb8','#e41a1c','#4daf4a', '#984ea3','#ff7f00','#ffff33')) # Data (437,543) a = 437 b = 543 # Grid of nx points nx = 1000 x = np.linspace(0, 1, nx) # Compute density of non-conjugate prior in grid # This non-conjugate prior is same as in Figure 2.4 in the book pp = np.ones(nx) ascent = (0.385 <= x) & (x <= 0.485) descent = (0.485 <= x) & (x <= 0.585) pm = 11 pp[ascent] = np.linspace(1, pm, np.count_nonzero(ascent)) pp[descent] = np.linspace(pm, 1, np.count_nonzero(descent)) # Normalize the prior pp /= np.sum(pp) # Unnormalised non-conjugate posterior in grid po = beta.pdf(x, a, b)*pp po /= np.sum(po) # Cumulative pc = np.cumsum(po) # Inverse-cdf sampling # Get n uniform random numbers from [0,1] n = 10000 r = np.random.rand(n) # Map each r into corresponding grid point x: # [0, pc[0]) map into x[0] and [pc[i-1], pc[i]), i>0, map into x[i] rr = x[np.sum(pc[:,np.newaxis] < r, axis=0)] # Plot posteriors # Plot 3 subplots fig, axes = plt.subplots(nrows=3, ncols=1, sharex=True, figsize=(8, 12)) # Posterior with uniform prior Beta(1,1) axes[0].plot(x, beta.pdf(x, a+1, b+1)) axes[0].set_title('Poster with uniform prior') axes[0].set_yticks(()) # Non-conjugate prior axes[1].plot(x, pp) axes[1].set_title('Non-conjugate prior') axes[1].set_yticks(()) # Posterior with non-conjugate prior axes[2].plot(x, po) axes[2].set_title('Posterior with non-conjugate prior') axes[2].set_yticks(()) # Set custom limits for x-axis axes[0].set_xlim((0.35, 0.6)) fig.subplots_adjust(hspace=0.2) # Plot samples fig = plt.figure() # Plot cumulative posterior plt.plot(x, pc, color='#e41a1c') # Calculate histograms and scale them into the same figure hist_r = np.histogram(r, bins=30) hist_rr = np.histogram(rr, bins=30) plt.barh(hist_r[1][:-1], hist_r[0]*0.02/hist_r[0].max(), height=hist_r[1][1]-hist_r[1][0], left=0.35, color='#4daf4a') plt.bar(hist_rr[1][:-1], hist_rr[0]*0.2/hist_rr[0].max(), width=hist_rr[1][1]-hist_rr[1][0], color='#377eb8') plt.legend(('Cumulative posterior', 'Random uniform numbers', 'Posterior samples'), loc='best') # Set limits plt.xlim((0.35, 0.55)) plt.ylim((0,1)) # Display the figure plt.show()
gpl-3.0
kjung/scikit-learn
examples/model_selection/plot_precision_recall.py
74
6377
""" ================ Precision-Recall ================ Example of Precision-Recall metric to evaluate classifier output quality. In information retrieval, precision is a measure of result relevancy, while recall is a measure of how many truly relevant results are returned. A high area under the curve represents both high recall and high precision, where high precision relates to a low false positive rate, and high recall relates to a low false negative rate. High scores for both show that the classifier is returning accurate results (high precision), as well as returning a majority of all positive results (high recall). A system with high recall but low precision returns many results, but most of its predicted labels are incorrect when compared to the training labels. A system with high precision but low recall is just the opposite, returning very few results, but most of its predicted labels are correct when compared to the training labels. An ideal system with high precision and high recall will return many results, with all results labeled correctly. Precision (:math:`P`) is defined as the number of true positives (:math:`T_p`) over the number of true positives plus the number of false positives (:math:`F_p`). :math:`P = \\frac{T_p}{T_p+F_p}` Recall (:math:`R`) is defined as the number of true positives (:math:`T_p`) over the number of true positives plus the number of false negatives (:math:`F_n`). :math:`R = \\frac{T_p}{T_p + F_n}` These quantities are also related to the (:math:`F_1`) score, which is defined as the harmonic mean of precision and recall. :math:`F1 = 2\\frac{P \\times R}{P+R}` It is important to note that the precision may not decrease with recall. The definition of precision (:math:`\\frac{T_p}{T_p + F_p}`) shows that lowering the threshold of a classifier may increase the denominator, by increasing the number of results returned. If the threshold was previously set too high, the new results may all be true positives, which will increase precision. If the previous threshold was about right or too low, further lowering the threshold will introduce false positives, decreasing precision. Recall is defined as :math:`\\frac{T_p}{T_p+F_n}`, where :math:`T_p+F_n` does not depend on the classifier threshold. This means that lowering the classifier threshold may increase recall, by increasing the number of true positive results. It is also possible that lowering the threshold may leave recall unchanged, while the precision fluctuates. The relationship between recall and precision can be observed in the stairstep area of the plot - at the edges of these steps a small change in the threshold considerably reduces precision, with only a minor gain in recall. See the corner at recall = .59, precision = .8 for an example of this phenomenon. Precision-recall curves are typically used in binary classification to study the output of a classifier. In order to extend Precision-recall curve and average precision to multi-class or multi-label classification, it is necessary to binarize the output. One curve can be drawn per label, but one can also draw a precision-recall curve by considering each element of the label indicator matrix as a binary prediction (micro-averaging). .. note:: See also :func:`sklearn.metrics.average_precision_score`, :func:`sklearn.metrics.recall_score`, :func:`sklearn.metrics.precision_score`, :func:`sklearn.metrics.f1_score` """ print(__doc__) import matplotlib.pyplot as plt import numpy as np from itertools import cycle from sklearn import svm, datasets from sklearn.metrics import precision_recall_curve from sklearn.metrics import average_precision_score from sklearn.model_selection import train_test_split from sklearn.preprocessing import label_binarize from sklearn.multiclass import OneVsRestClassifier # import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target # setup plot details colors = cycle(['navy', 'turquoise', 'darkorange', 'cornflowerblue', 'teal']) lw = 2 # Binarize the output y = label_binarize(y, classes=[0, 1, 2]) n_classes = y.shape[1] # Add noisy features random_state = np.random.RandomState(0) n_samples, n_features = X.shape X = np.c_[X, random_state.randn(n_samples, 200 * n_features)] # Split into training and test X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=random_state) # Run classifier classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, random_state=random_state)) y_score = classifier.fit(X_train, y_train).decision_function(X_test) # Compute Precision-Recall and plot curve precision = dict() recall = dict() average_precision = dict() for i in range(n_classes): precision[i], recall[i], _ = precision_recall_curve(y_test[:, i], y_score[:, i]) average_precision[i] = average_precision_score(y_test[:, i], y_score[:, i]) # Compute micro-average ROC curve and ROC area precision["micro"], recall["micro"], _ = precision_recall_curve(y_test.ravel(), y_score.ravel()) average_precision["micro"] = average_precision_score(y_test, y_score, average="micro") # Plot Precision-Recall curve plt.clf() plt.plot(recall[0], precision[0], lw=lw, color='navy', label='Precision-Recall curve') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title('Precision-Recall example: AUC={0:0.2f}'.format(average_precision[0])) plt.legend(loc="lower left") plt.show() # Plot Precision-Recall curve for each class plt.clf() plt.plot(recall["micro"], precision["micro"], color='gold', lw=lw, label='micro-average Precision-recall curve (area = {0:0.2f})' ''.format(average_precision["micro"])) for i, color in zip(range(n_classes), colors): plt.plot(recall[i], precision[i], color=color, lw=lw, label='Precision-recall curve of class {0} (area = {1:0.2f})' ''.format(i, average_precision[i])) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('Recall') plt.ylabel('Precision') plt.title('Extension of Precision-Recall curve to multi-class') plt.legend(loc="lower right") plt.show()
bsd-3-clause
qifeigit/scikit-learn
sklearn/preprocessing/label.py
137
27165
# Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Mathieu Blondel <mathieu@mblondel.org> # Olivier Grisel <olivier.grisel@ensta.org> # Andreas Mueller <amueller@ais.uni-bonn.de> # Joel Nothman <joel.nothman@gmail.com> # Hamzeh Alsalhi <ha258@cornell.edu> # License: BSD 3 clause from collections import defaultdict import itertools import array import numpy as np import scipy.sparse as sp from ..base import BaseEstimator, TransformerMixin from ..utils.fixes import np_version from ..utils.fixes import sparse_min_max from ..utils.fixes import astype from ..utils.fixes import in1d from ..utils import column_or_1d from ..utils.validation import check_array from ..utils.validation import check_is_fitted from ..utils.validation import _num_samples from ..utils.multiclass import unique_labels from ..utils.multiclass import type_of_target from ..externals import six zip = six.moves.zip map = six.moves.map __all__ = [ 'label_binarize', 'LabelBinarizer', 'LabelEncoder', 'MultiLabelBinarizer', ] def _check_numpy_unicode_bug(labels): """Check that user is not subject to an old numpy bug Fixed in master before 1.7.0: https://github.com/numpy/numpy/pull/243 """ if np_version[:3] < (1, 7, 0) and labels.dtype.kind == 'U': raise RuntimeError("NumPy < 1.7.0 does not implement searchsorted" " on unicode data correctly. Please upgrade" " NumPy to use LabelEncoder with unicode inputs.") class LabelEncoder(BaseEstimator, TransformerMixin): """Encode labels with value between 0 and n_classes-1. Read more in the :ref:`User Guide <preprocessing_targets>`. Attributes ---------- classes_ : array of shape (n_class,) Holds the label for each class. Examples -------- `LabelEncoder` can be used to normalize labels. >>> from sklearn import preprocessing >>> le = preprocessing.LabelEncoder() >>> le.fit([1, 2, 2, 6]) LabelEncoder() >>> le.classes_ array([1, 2, 6]) >>> le.transform([1, 1, 2, 6]) #doctest: +ELLIPSIS array([0, 0, 1, 2]...) >>> le.inverse_transform([0, 0, 1, 2]) array([1, 1, 2, 6]) It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels. >>> le = preprocessing.LabelEncoder() >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) LabelEncoder() >>> list(le.classes_) ['amsterdam', 'paris', 'tokyo'] >>> le.transform(["tokyo", "tokyo", "paris"]) #doctest: +ELLIPSIS array([2, 2, 1]...) >>> list(le.inverse_transform([2, 2, 1])) ['tokyo', 'tokyo', 'paris'] """ def fit(self, y): """Fit label encoder Parameters ---------- y : array-like of shape (n_samples,) Target values. Returns ------- self : returns an instance of self. """ y = column_or_1d(y, warn=True) _check_numpy_unicode_bug(y) self.classes_ = np.unique(y) return self def fit_transform(self, y): """Fit label encoder and return encoded labels Parameters ---------- y : array-like of shape [n_samples] Target values. Returns ------- y : array-like of shape [n_samples] """ y = column_or_1d(y, warn=True) _check_numpy_unicode_bug(y) self.classes_, y = np.unique(y, return_inverse=True) return y def transform(self, y): """Transform labels to normalized encoding. Parameters ---------- y : array-like of shape [n_samples] Target values. Returns ------- y : array-like of shape [n_samples] """ check_is_fitted(self, 'classes_') classes = np.unique(y) _check_numpy_unicode_bug(classes) if len(np.intersect1d(classes, self.classes_)) < len(classes): diff = np.setdiff1d(classes, self.classes_) raise ValueError("y contains new labels: %s" % str(diff)) return np.searchsorted(self.classes_, y) def inverse_transform(self, y): """Transform labels back to original encoding. Parameters ---------- y : numpy array of shape [n_samples] Target values. Returns ------- y : numpy array of shape [n_samples] """ check_is_fitted(self, 'classes_') diff = np.setdiff1d(y, np.arange(len(self.classes_))) if diff: raise ValueError("y contains new labels: %s" % str(diff)) y = np.asarray(y) return self.classes_[y] class LabelBinarizer(BaseEstimator, TransformerMixin): """Binarize labels in a one-vs-all fashion Several regression and binary classification algorithms are available in the scikit. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme. At learning time, this simply consists in learning one regressor or binary classifier per class. In doing so, one needs to convert multi-class labels to binary labels (belong or does not belong to the class). LabelBinarizer makes this process easy with the transform method. At prediction time, one assigns the class for which the corresponding model gave the greatest confidence. LabelBinarizer makes this easy with the inverse_transform method. Read more in the :ref:`User Guide <preprocessing_targets>`. Parameters ---------- neg_label : int (default: 0) Value with which negative labels must be encoded. pos_label : int (default: 1) Value with which positive labels must be encoded. sparse_output : boolean (default: False) True if the returned array from transform is desired to be in sparse CSR format. Attributes ---------- classes_ : array of shape [n_class] Holds the label for each class. y_type_ : str, Represents the type of the target data as evaluated by utils.multiclass.type_of_target. Possible type are 'continuous', 'continuous-multioutput', 'binary', 'multiclass', 'mutliclass-multioutput', 'multilabel-indicator', and 'unknown'. multilabel_ : boolean True if the transformer was fitted on a multilabel rather than a multiclass set of labels. The ``multilabel_`` attribute is deprecated and will be removed in 0.18 sparse_input_ : boolean, True if the input data to transform is given as a sparse matrix, False otherwise. indicator_matrix_ : str 'sparse' when the input data to tansform is a multilable-indicator and is sparse, None otherwise. The ``indicator_matrix_`` attribute is deprecated as of version 0.16 and will be removed in 0.18 Examples -------- >>> from sklearn import preprocessing >>> lb = preprocessing.LabelBinarizer() >>> lb.fit([1, 2, 6, 4, 2]) LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False) >>> lb.classes_ array([1, 2, 4, 6]) >>> lb.transform([1, 6]) array([[1, 0, 0, 0], [0, 0, 0, 1]]) Binary targets transform to a column vector >>> lb = preprocessing.LabelBinarizer() >>> lb.fit_transform(['yes', 'no', 'no', 'yes']) array([[1], [0], [0], [1]]) Passing a 2D matrix for multilabel classification >>> import numpy as np >>> lb.fit(np.array([[0, 1, 1], [1, 0, 0]])) LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False) >>> lb.classes_ array([0, 1, 2]) >>> lb.transform([0, 1, 2, 1]) array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0]]) See also -------- label_binarize : function to perform the transform operation of LabelBinarizer with fixed classes. """ def __init__(self, neg_label=0, pos_label=1, sparse_output=False): if neg_label >= pos_label: raise ValueError("neg_label={0} must be strictly less than " "pos_label={1}.".format(neg_label, pos_label)) if sparse_output and (pos_label == 0 or neg_label != 0): raise ValueError("Sparse binarization is only supported with non " "zero pos_label and zero neg_label, got " "pos_label={0} and neg_label={1}" "".format(pos_label, neg_label)) self.neg_label = neg_label self.pos_label = pos_label self.sparse_output = sparse_output def fit(self, y): """Fit label binarizer Parameters ---------- y : numpy array of shape (n_samples,) or (n_samples, n_classes) Target values. The 2-d matrix should only contain 0 and 1, represents multilabel classification. Returns ------- self : returns an instance of self. """ self.y_type_ = type_of_target(y) if 'multioutput' in self.y_type_: raise ValueError("Multioutput target data is not supported with " "label binarization") if _num_samples(y) == 0: raise ValueError('y has 0 samples: %r' % y) self.sparse_input_ = sp.issparse(y) self.classes_ = unique_labels(y) return self def transform(self, y): """Transform multi-class labels to binary labels The output of transform is sometimes referred to by some authors as the 1-of-K coding scheme. Parameters ---------- y : numpy array or sparse matrix of shape (n_samples,) or (n_samples, n_classes) Target values. The 2-d matrix should only contain 0 and 1, represents multilabel classification. Sparse matrix can be CSR, CSC, COO, DOK, or LIL. Returns ------- Y : numpy array or CSR matrix of shape [n_samples, n_classes] Shape will be [n_samples, 1] for binary problems. """ check_is_fitted(self, 'classes_') y_is_multilabel = type_of_target(y).startswith('multilabel') if y_is_multilabel and not self.y_type_.startswith('multilabel'): raise ValueError("The object was not fitted with multilabel" " input.") return label_binarize(y, self.classes_, pos_label=self.pos_label, neg_label=self.neg_label, sparse_output=self.sparse_output) def inverse_transform(self, Y, threshold=None): """Transform binary labels back to multi-class labels Parameters ---------- Y : numpy array or sparse matrix with shape [n_samples, n_classes] Target values. All sparse matrices are converted to CSR before inverse transformation. threshold : float or None Threshold used in the binary and multi-label cases. Use 0 when: - Y contains the output of decision_function (classifier) Use 0.5 when: - Y contains the output of predict_proba If None, the threshold is assumed to be half way between neg_label and pos_label. Returns ------- y : numpy array or CSR matrix of shape [n_samples] Target values. Notes ----- In the case when the binary labels are fractional (probabilistic), inverse_transform chooses the class with the greatest value. Typically, this allows to use the output of a linear model's decision_function method directly as the input of inverse_transform. """ check_is_fitted(self, 'classes_') if threshold is None: threshold = (self.pos_label + self.neg_label) / 2. if self.y_type_ == "multiclass": y_inv = _inverse_binarize_multiclass(Y, self.classes_) else: y_inv = _inverse_binarize_thresholding(Y, self.y_type_, self.classes_, threshold) if self.sparse_input_: y_inv = sp.csr_matrix(y_inv) elif sp.issparse(y_inv): y_inv = y_inv.toarray() return y_inv def label_binarize(y, classes, neg_label=0, pos_label=1, sparse_output=False): """Binarize labels in a one-vs-all fashion Several regression and binary classification algorithms are available in the scikit. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme. This function makes it possible to compute this transformation for a fixed set of class labels known ahead of time. Parameters ---------- y : array-like Sequence of integer labels or multilabel data to encode. classes : array-like of shape [n_classes] Uniquely holds the label for each class. neg_label : int (default: 0) Value with which negative labels must be encoded. pos_label : int (default: 1) Value with which positive labels must be encoded. sparse_output : boolean (default: False), Set to true if output binary array is desired in CSR sparse format Returns ------- Y : numpy array or CSR matrix of shape [n_samples, n_classes] Shape will be [n_samples, 1] for binary problems. Examples -------- >>> from sklearn.preprocessing import label_binarize >>> label_binarize([1, 6], classes=[1, 2, 4, 6]) array([[1, 0, 0, 0], [0, 0, 0, 1]]) The class ordering is preserved: >>> label_binarize([1, 6], classes=[1, 6, 4, 2]) array([[1, 0, 0, 0], [0, 1, 0, 0]]) Binary targets transform to a column vector >>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes']) array([[1], [0], [0], [1]]) See also -------- LabelBinarizer : class used to wrap the functionality of label_binarize and allow for fitting to classes independently of the transform operation """ if not isinstance(y, list): # XXX Workaround that will be removed when list of list format is # dropped y = check_array(y, accept_sparse='csr', ensure_2d=False, dtype=None) else: if _num_samples(y) == 0: raise ValueError('y has 0 samples: %r' % y) if neg_label >= pos_label: raise ValueError("neg_label={0} must be strictly less than " "pos_label={1}.".format(neg_label, pos_label)) if (sparse_output and (pos_label == 0 or neg_label != 0)): raise ValueError("Sparse binarization is only supported with non " "zero pos_label and zero neg_label, got " "pos_label={0} and neg_label={1}" "".format(pos_label, neg_label)) # To account for pos_label == 0 in the dense case pos_switch = pos_label == 0 if pos_switch: pos_label = -neg_label y_type = type_of_target(y) if 'multioutput' in y_type: raise ValueError("Multioutput target data is not supported with label " "binarization") if y_type == 'unknown': raise ValueError("The type of target data is not known") n_samples = y.shape[0] if sp.issparse(y) else len(y) n_classes = len(classes) classes = np.asarray(classes) if y_type == "binary": if len(classes) == 1: Y = np.zeros((len(y), 1), dtype=np.int) Y += neg_label return Y elif len(classes) >= 3: y_type = "multiclass" sorted_class = np.sort(classes) if (y_type == "multilabel-indicator" and classes.size != y.shape[1]): raise ValueError("classes {0} missmatch with the labels {1}" "found in the data".format(classes, unique_labels(y))) if y_type in ("binary", "multiclass"): y = column_or_1d(y) # pick out the known labels from y y_in_classes = in1d(y, classes) y_seen = y[y_in_classes] indices = np.searchsorted(sorted_class, y_seen) indptr = np.hstack((0, np.cumsum(y_in_classes))) data = np.empty_like(indices) data.fill(pos_label) Y = sp.csr_matrix((data, indices, indptr), shape=(n_samples, n_classes)) elif y_type == "multilabel-indicator": Y = sp.csr_matrix(y) if pos_label != 1: data = np.empty_like(Y.data) data.fill(pos_label) Y.data = data else: raise ValueError("%s target data is not supported with label " "binarization" % y_type) if not sparse_output: Y = Y.toarray() Y = astype(Y, int, copy=False) if neg_label != 0: Y[Y == 0] = neg_label if pos_switch: Y[Y == pos_label] = 0 else: Y.data = astype(Y.data, int, copy=False) # preserve label ordering if np.any(classes != sorted_class): indices = np.searchsorted(sorted_class, classes) Y = Y[:, indices] if y_type == "binary": if sparse_output: Y = Y.getcol(-1) else: Y = Y[:, -1].reshape((-1, 1)) return Y def _inverse_binarize_multiclass(y, classes): """Inverse label binarization transformation for multiclass. Multiclass uses the maximal score instead of a threshold. """ classes = np.asarray(classes) if sp.issparse(y): # Find the argmax for each row in y where y is a CSR matrix y = y.tocsr() n_samples, n_outputs = y.shape outputs = np.arange(n_outputs) row_max = sparse_min_max(y, 1)[1] row_nnz = np.diff(y.indptr) y_data_repeated_max = np.repeat(row_max, row_nnz) # picks out all indices obtaining the maximum per row y_i_all_argmax = np.flatnonzero(y_data_repeated_max == y.data) # For corner case where last row has a max of 0 if row_max[-1] == 0: y_i_all_argmax = np.append(y_i_all_argmax, [len(y.data)]) # Gets the index of the first argmax in each row from y_i_all_argmax index_first_argmax = np.searchsorted(y_i_all_argmax, y.indptr[:-1]) # first argmax of each row y_ind_ext = np.append(y.indices, [0]) y_i_argmax = y_ind_ext[y_i_all_argmax[index_first_argmax]] # Handle rows of all 0 y_i_argmax[np.where(row_nnz == 0)[0]] = 0 # Handles rows with max of 0 that contain negative numbers samples = np.arange(n_samples)[(row_nnz > 0) & (row_max.ravel() == 0)] for i in samples: ind = y.indices[y.indptr[i]:y.indptr[i + 1]] y_i_argmax[i] = classes[np.setdiff1d(outputs, ind)][0] return classes[y_i_argmax] else: return classes.take(y.argmax(axis=1), mode="clip") def _inverse_binarize_thresholding(y, output_type, classes, threshold): """Inverse label binarization transformation using thresholding.""" if output_type == "binary" and y.ndim == 2 and y.shape[1] > 2: raise ValueError("output_type='binary', but y.shape = {0}". format(y.shape)) if output_type != "binary" and y.shape[1] != len(classes): raise ValueError("The number of class is not equal to the number of " "dimension of y.") classes = np.asarray(classes) # Perform thresholding if sp.issparse(y): if threshold > 0: if y.format not in ('csr', 'csc'): y = y.tocsr() y.data = np.array(y.data > threshold, dtype=np.int) y.eliminate_zeros() else: y = np.array(y.toarray() > threshold, dtype=np.int) else: y = np.array(y > threshold, dtype=np.int) # Inverse transform data if output_type == "binary": if sp.issparse(y): y = y.toarray() if y.ndim == 2 and y.shape[1] == 2: return classes[y[:, 1]] else: if len(classes) == 1: y = np.empty(len(y), dtype=classes.dtype) y.fill(classes[0]) return y else: return classes[y.ravel()] elif output_type == "multilabel-indicator": return y else: raise ValueError("{0} format is not supported".format(output_type)) class MultiLabelBinarizer(BaseEstimator, TransformerMixin): """Transform between iterable of iterables and a multilabel format Although a list of sets or tuples is a very intuitive format for multilabel data, it is unwieldy to process. This transformer converts between this intuitive format and the supported multilabel format: a (samples x classes) binary matrix indicating the presence of a class label. Parameters ---------- classes : array-like of shape [n_classes] (optional) Indicates an ordering for the class labels sparse_output : boolean (default: False), Set to true if output binary array is desired in CSR sparse format Attributes ---------- classes_ : array of labels A copy of the `classes` parameter where provided, or otherwise, the sorted set of classes found when fitting. Examples -------- >>> mlb = MultiLabelBinarizer() >>> mlb.fit_transform([(1, 2), (3,)]) array([[1, 1, 0], [0, 0, 1]]) >>> mlb.classes_ array([1, 2, 3]) >>> mlb.fit_transform([set(['sci-fi', 'thriller']), set(['comedy'])]) array([[0, 1, 1], [1, 0, 0]]) >>> list(mlb.classes_) ['comedy', 'sci-fi', 'thriller'] """ def __init__(self, classes=None, sparse_output=False): self.classes = classes self.sparse_output = sparse_output def fit(self, y): """Fit the label sets binarizer, storing `classes_` Parameters ---------- y : iterable of iterables A set of labels (any orderable and hashable object) for each sample. If the `classes` parameter is set, `y` will not be iterated. Returns ------- self : returns this MultiLabelBinarizer instance """ if self.classes is None: classes = sorted(set(itertools.chain.from_iterable(y))) else: classes = self.classes dtype = np.int if all(isinstance(c, int) for c in classes) else object self.classes_ = np.empty(len(classes), dtype=dtype) self.classes_[:] = classes return self def fit_transform(self, y): """Fit the label sets binarizer and transform the given label sets Parameters ---------- y : iterable of iterables A set of labels (any orderable and hashable object) for each sample. If the `classes` parameter is set, `y` will not be iterated. Returns ------- y_indicator : array or CSR matrix, shape (n_samples, n_classes) A matrix such that `y_indicator[i, j] = 1` iff `classes_[j]` is in `y[i]`, and 0 otherwise. """ if self.classes is not None: return self.fit(y).transform(y) # Automatically increment on new class class_mapping = defaultdict(int) class_mapping.default_factory = class_mapping.__len__ yt = self._transform(y, class_mapping) # sort classes and reorder columns tmp = sorted(class_mapping, key=class_mapping.get) # (make safe for tuples) dtype = np.int if all(isinstance(c, int) for c in tmp) else object class_mapping = np.empty(len(tmp), dtype=dtype) class_mapping[:] = tmp self.classes_, inverse = np.unique(class_mapping, return_inverse=True) yt.indices = np.take(inverse, yt.indices) if not self.sparse_output: yt = yt.toarray() return yt def transform(self, y): """Transform the given label sets Parameters ---------- y : iterable of iterables A set of labels (any orderable and hashable object) for each sample. If the `classes` parameter is set, `y` will not be iterated. Returns ------- y_indicator : array or CSR matrix, shape (n_samples, n_classes) A matrix such that `y_indicator[i, j] = 1` iff `classes_[j]` is in `y[i]`, and 0 otherwise. """ class_to_index = dict(zip(self.classes_, range(len(self.classes_)))) yt = self._transform(y, class_to_index) if not self.sparse_output: yt = yt.toarray() return yt def _transform(self, y, class_mapping): """Transforms the label sets with a given mapping Parameters ---------- y : iterable of iterables class_mapping : Mapping Maps from label to column index in label indicator matrix Returns ------- y_indicator : sparse CSR matrix, shape (n_samples, n_classes) Label indicator matrix """ indices = array.array('i') indptr = array.array('i', [0]) for labels in y: indices.extend(set(class_mapping[label] for label in labels)) indptr.append(len(indices)) data = np.ones(len(indices), dtype=int) return sp.csr_matrix((data, indices, indptr), shape=(len(indptr) - 1, len(class_mapping))) def inverse_transform(self, yt): """Transform the given indicator matrix into label sets Parameters ---------- yt : array or sparse matrix of shape (n_samples, n_classes) A matrix containing only 1s ands 0s. Returns ------- y : list of tuples The set of labels for each sample such that `y[i]` consists of `classes_[j]` for each `yt[i, j] == 1`. """ if yt.shape[1] != len(self.classes_): raise ValueError('Expected indicator for {0} classes, but got {1}' .format(len(self.classes_), yt.shape[1])) if sp.issparse(yt): yt = yt.tocsr() if len(yt.data) != 0 and len(np.setdiff1d(yt.data, [0, 1])) > 0: raise ValueError('Expected only 0s and 1s in label indicator.') return [tuple(self.classes_.take(yt.indices[start:end])) for start, end in zip(yt.indptr[:-1], yt.indptr[1:])] else: unexpected = np.setdiff1d(yt, [0, 1]) if len(unexpected) > 0: raise ValueError('Expected only 0s and 1s in label indicator. ' 'Also got {0}'.format(unexpected)) return [tuple(self.classes_.compress(indicators)) for indicators in yt]
bsd-3-clause
verashira/ml-python
classification/seeds_knn/knn.py
1
2577
import numpy as np def find_plurality(labels): ''' prediction = find_plurality(labels) Return the label of most votes. ''' from collections import defaultdict counts = defaultdict(int) for label in labels: counts[label] += 1 maxv = max(counts.values()) for k,v in counts.items(): if v == maxv: return k def apply_knn(new_examples, data, labels, k = 8): ''' predictions = apply_knn(new_examples, data, labels, k=8) Return the predicted label of new examples,based on data, labels. ''' results = [] for d in new_examples: dists = [] for d2,label in zip(data, labels): dists.append( (np.linalg.norm(d2-d), label) ) dists.sort(key = lambda di: di[0]) # di for norm(d2-d) above # print dists dists = dists[:k] results.append(find_plurality([label for _,label in dists])) # print len(results) return np.array(results).reshape(new_examples.shape[0]) def accuracy(labels, predictions): return np.sum(labels == predictions) / float(labels.shape[0]) def test(): ''' Debug ''' import load import matplotlib.pyplot as plt import os feature_names, data, target_names, targets = load.load_seeds() target_names = np.array(target_names) labels = target_names[targets] pred = apply_knn(data, data, targets) pred = target_names[pred] #label_names = np.unique(labels) # plot original data plt.subplot(1, 2, 1) for k,marker,c in zip(xrange(3), "<ox", "rgb"): plt.scatter( data[labels == target_names[k], 0], data[labels == target_names[k], 2], marker = marker, c = c ); plt.title("origin") # plot knn predicted data plt.subplot(1, 2, 2) for k,marker,c in zip(xrange(3), "<ox", "rgb"): plt.scatter( data[pred == target_names[k], 0], data[pred == target_names[k], 2], marker = marker, c = c) # re-plot misclassified data wrong = plt.scatter( data[pred != labels, 0], data[pred != labels, 2], marker = 's', c = 'y' ); plt.legend([wrong], ["Wrong"], loc='upper right') file_dir = os.path.dirname(os.path.realpath(__file__)) plt.savefig(os.path.join(file_dir, "test", "knn_test.png")) print "Accuracy: %f" % accuracy(labels, pred) if __name__ == '__main__': test()
mit
xwolf12/scikit-learn
benchmarks/bench_tree.py
297
3617
""" To run this, you'll need to have installed. * scikit-learn Does two benchmarks First, we fix a training set, increase the number of samples to classify and plot number of classified samples as a function of time. In the second benchmark, we increase the number of dimensions of the training set, classify a sample and plot the time taken as a function of the number of dimensions. """ import numpy as np import pylab as pl import gc from datetime import datetime # to store the results scikit_classifier_results = [] scikit_regressor_results = [] mu_second = 0.0 + 10 ** 6 # number of microseconds in a second def bench_scikit_tree_classifier(X, Y): """Benchmark with scikit-learn decision tree classifier""" from sklearn.tree import DecisionTreeClassifier gc.collect() # start time tstart = datetime.now() clf = DecisionTreeClassifier() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_classifier_results.append( delta.seconds + delta.microseconds / mu_second) def bench_scikit_tree_regressor(X, Y): """Benchmark with scikit-learn decision tree regressor""" from sklearn.tree import DecisionTreeRegressor gc.collect() # start time tstart = datetime.now() clf = DecisionTreeRegressor() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_regressor_results.append( delta.seconds + delta.microseconds / mu_second) if __name__ == '__main__': print('============================================') print('Warning: this is going to take a looong time') print('============================================') n = 10 step = 10000 n_samples = 10000 dim = 10 n_classes = 10 for i in range(n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') n_samples += step X = np.random.randn(n_samples, dim) Y = np.random.randint(0, n_classes, (n_samples,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(n_samples) bench_scikit_tree_regressor(X, Y) xx = range(0, n * step, step) pl.figure('scikit-learn tree benchmark results') pl.subplot(211) pl.title('Learning with varying number of samples') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of samples') pl.ylabel('Time (s)') scikit_classifier_results = [] scikit_regressor_results = [] n = 10 step = 500 start_dim = 500 n_classes = 10 dim = start_dim for i in range(0, n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') dim += step X = np.random.randn(100, dim) Y = np.random.randint(0, n_classes, (100,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(100) bench_scikit_tree_regressor(X, Y) xx = np.arange(start_dim, start_dim + n * step, step) pl.subplot(212) pl.title('Learning in high dimensional spaces') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of dimensions') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
DanHickstein/pyBASEX
examples/example_all_dribinski.py
2
3691
# -*- coding: utf-8 -*- # This example compares the available inverse Abel transform methods # for the Ominus sample image # # Note it transforms only the Q0 (top-right) quadrant # using the fundamental transform code from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import numpy as np import abel import collections import matplotlib.pylab as plt from time import time fig, (ax1,ax2) = plt.subplots(1, 2, figsize=(8,4)) # inverse Abel transform methods ----------------------------- # dictionary of method: function() transforms = { "direct": abel.direct.direct_transform, "hansenlaw": abel.hansenlaw.hansenlaw_transform, "onion": abel.dasch.onion_peeling_transform, "basex": abel.basex.basex_transform, "three_point": abel.dasch.three_point_transform, "two_point": abel.dasch.two_point_transform, } # sort dictionary: transforms = collections.OrderedDict(sorted(transforms.items())) # number of transforms: ntrans = np.size(transforms.keys()) IM = abel.tools.analytical.SampleImage(n=301, name="dribinski").image h, w = IM.shape # forward transform: fIM = abel.Transform(IM, direction="forward", method="hansenlaw").transform Q0, Q1, Q2, Q3 = abel.tools.symmetry.get_image_quadrants(fIM, reorient=True) Q0fresh = Q0.copy() # keep clean copy print ("quadrant shape {}".format(Q0.shape)) # process Q0 quadrant using each method -------------------- iabelQ = [] # keep inverse Abel transformed image for q, method in enumerate(transforms.keys()): Q0 = Q0fresh.copy() # top-right quadrant of O2- image print ("\n------- {:s} inverse ...".format(method)) t0 = time() # inverse Abel transform using 'method' IAQ0 = transforms[method](Q0, direction="inverse", basis_dir='bases') print (" {:.4f} sec".format(time()-t0)) iabelQ.append(IAQ0) # store for plot # polar projection and speed profile radial, speed = abel.tools.vmi.angular_integration(IAQ0, origin=(0, 0), Jacobian=False) # normalize image intensity and speed distribution IAQ0 /= IAQ0.max() speed /= speed.max() # method label for each quadrant annot_angle = -(45+q*90)*np.pi/180 # -ve because numpy coords from top if q > 3: annot_angle += 50*np.pi/180 # shared quadrant - move the label annot_coord = (h/2+(h*0.9)*np.cos(annot_angle)/2 -50, w/2+(w*0.9)*np.sin(annot_angle)/2) ax1.annotate(method, annot_coord, color="yellow") # plot speed distribution ax2.plot(radial, speed, label=method) # reassemble image, each quadrant a different method # for < 4 images pad using a blank quadrant blank = np.zeros(IAQ0.shape) for q in range(ntrans, 4): iabelQ.append(blank) # more than 4, split quadrant if ntrans == 5: # split last quadrant into 2 = upper and lower triangles tmp_img = np.tril(np.flipud(iabelQ[-2])) +\ np.triu(np.flipud(iabelQ[-1])) iabelQ[3] = np.flipud(tmp_img) im = abel.tools.symmetry.put_image_quadrants((iabelQ[0], iabelQ[1], iabelQ[2], iabelQ[3]), original_image_shape=IM.shape) ax1.imshow(im, vmin=0, vmax=0.15) ax1.set_title('Inverse Abel comparison') ax2.set_xlim(0, 200) ax2.set_ylim(-0.5,2) ax2.legend(loc=0, labelspacing=0.1, frameon=False) ax2.set_title('Angular integration') ax2.set_xlabel('Radial coordinate (pixel)') ax2.set_ylabel('Integrated intensity') plt.suptitle('Dribinski sample image') plt.tight_layout() plt.savefig('plot_example_all_dribinski.png', dpi=100) plt.show()
gpl-2.0
f3r/scikit-learn
sklearn/datasets/__init__.py
72
3807
""" The :mod:`sklearn.datasets` module includes utilities to load datasets, including methods to load and fetch popular reference datasets. It also features some artificial data generators. """ from .base import load_diabetes from .base import load_digits from .base import load_files from .base import load_iris from .base import load_breast_cancer from .base import load_linnerud from .base import load_boston from .base import get_data_home from .base import clear_data_home from .base import load_sample_images from .base import load_sample_image from .covtype import fetch_covtype from .kddcup99 import fetch_kddcup99 from .mlcomp import load_mlcomp from .lfw import load_lfw_pairs from .lfw import load_lfw_people from .lfw import fetch_lfw_pairs from .lfw import fetch_lfw_people from .twenty_newsgroups import fetch_20newsgroups from .twenty_newsgroups import fetch_20newsgroups_vectorized from .mldata import fetch_mldata, mldata_filename from .samples_generator import make_classification from .samples_generator import make_multilabel_classification from .samples_generator import make_hastie_10_2 from .samples_generator import make_regression from .samples_generator import make_blobs from .samples_generator import make_moons from .samples_generator import make_circles from .samples_generator import make_friedman1 from .samples_generator import make_friedman2 from .samples_generator import make_friedman3 from .samples_generator import make_low_rank_matrix from .samples_generator import make_sparse_coded_signal from .samples_generator import make_sparse_uncorrelated from .samples_generator import make_spd_matrix from .samples_generator import make_swiss_roll from .samples_generator import make_s_curve from .samples_generator import make_sparse_spd_matrix from .samples_generator import make_gaussian_quantiles from .samples_generator import make_biclusters from .samples_generator import make_checkerboard from .svmlight_format import load_svmlight_file from .svmlight_format import load_svmlight_files from .svmlight_format import dump_svmlight_file from .olivetti_faces import fetch_olivetti_faces from .species_distributions import fetch_species_distributions from .california_housing import fetch_california_housing from .rcv1 import fetch_rcv1 __all__ = ['clear_data_home', 'dump_svmlight_file', 'fetch_20newsgroups', 'fetch_20newsgroups_vectorized', 'fetch_lfw_pairs', 'fetch_lfw_people', 'fetch_mldata', 'fetch_olivetti_faces', 'fetch_species_distributions', 'fetch_california_housing', 'fetch_covtype', 'fetch_rcv1', 'fetch_kddcup99', 'get_data_home', 'load_boston', 'load_diabetes', 'load_digits', 'load_files', 'load_iris', 'load_breast_cancer', 'load_lfw_pairs', 'load_lfw_people', 'load_linnerud', 'load_mlcomp', 'load_sample_image', 'load_sample_images', 'load_svmlight_file', 'load_svmlight_files', 'make_biclusters', 'make_blobs', 'make_circles', 'make_classification', 'make_checkerboard', 'make_friedman1', 'make_friedman2', 'make_friedman3', 'make_gaussian_quantiles', 'make_hastie_10_2', 'make_low_rank_matrix', 'make_moons', 'make_multilabel_classification', 'make_regression', 'make_s_curve', 'make_sparse_coded_signal', 'make_sparse_spd_matrix', 'make_sparse_uncorrelated', 'make_spd_matrix', 'make_swiss_roll', 'mldata_filename']
bsd-3-clause
prheenan/Research
Perkins/Projects/Lipids/2017-1-negative-control-gallery/main_negative_gallery.py
1
1450
# force floating point division. Can still use integer with // from __future__ import division # This file is used for importing the common utilities classes. import numpy as np import matplotlib.pyplot as plt import sys sys.path.append("../../../../../") from GeneralUtil.python import GenUtilities,CheckpointUtilities,PlotUtilities from Research.Perkins.AnalysisUtil.ForceExtensionAnalysis import \ FEC_Util,FEC_Plot def run(): """ <Description> Args: param1: This is the first param. Returns: This is a description of what is returned. """ network = FEC_Util.default_data_root() base = network + "4Patrick/CuratedData/Lipids/DOPC/"+\ "NegativeControls/Representative_Gallery/" _,raw_data = FEC_Util.read_and_cache_pxp(base,force=False) processed = [FEC_Util.SplitAndProcess(r) for r in raw_data] inches_per_plot = 4.5 n_rows,n_cols = FEC_Plot._n_rows_and_cols(processed) fig_size = (n_cols*inches_per_plot,n_rows*inches_per_plot) fig = PlotUtilities.figure(figsize=(fig_size)) ylim_pN = [-20,75] xlim_nm = [-10,100] FEC_Plot.gallery_fec(processed,xlim_nm,ylim_pN) plt.suptitle("Negative Control Gallery",y=1.2,fontsize=25) PlotUtilities.savefig(fig,base + "out.png",close=False) PlotUtilities.savefig(fig,"./out.png") # # plot each of the force extension curves in a separate subplot if __name__ == "__main__": run()
gpl-3.0
ishanic/scikit-learn
examples/missing_values.py
233
3056
""" ====================================================== Imputing missing values before building an estimator ====================================================== This example shows that imputing the missing values can give better results than discarding the samples containing any missing value. Imputing does not always improve the predictions, so please check via cross-validation. Sometimes dropping rows or using marker values is more effective. Missing values can be replaced by the mean, the median or the most frequent value using the ``strategy`` hyper-parameter. The median is a more robust estimator for data with high magnitude variables which could dominate results (otherwise known as a 'long tail'). Script output:: Score with the entire dataset = 0.56 Score without the samples containing missing values = 0.48 Score after imputation of the missing values = 0.55 In this case, imputing helps the classifier get close to the original score. """ import numpy as np from sklearn.datasets import load_boston from sklearn.ensemble import RandomForestRegressor from sklearn.pipeline import Pipeline from sklearn.preprocessing import Imputer from sklearn.cross_validation import cross_val_score rng = np.random.RandomState(0) dataset = load_boston() X_full, y_full = dataset.data, dataset.target n_samples = X_full.shape[0] n_features = X_full.shape[1] # Estimate the score on the entire dataset, with no missing values estimator = RandomForestRegressor(random_state=0, n_estimators=100) score = cross_val_score(estimator, X_full, y_full).mean() print("Score with the entire dataset = %.2f" % score) # Add missing values in 75% of the lines missing_rate = 0.75 n_missing_samples = np.floor(n_samples * missing_rate) missing_samples = np.hstack((np.zeros(n_samples - n_missing_samples, dtype=np.bool), np.ones(n_missing_samples, dtype=np.bool))) rng.shuffle(missing_samples) missing_features = rng.randint(0, n_features, n_missing_samples) # Estimate the score without the lines containing missing values X_filtered = X_full[~missing_samples, :] y_filtered = y_full[~missing_samples] estimator = RandomForestRegressor(random_state=0, n_estimators=100) score = cross_val_score(estimator, X_filtered, y_filtered).mean() print("Score without the samples containing missing values = %.2f" % score) # Estimate the score after imputation of the missing values X_missing = X_full.copy() X_missing[np.where(missing_samples)[0], missing_features] = 0 y_missing = y_full.copy() estimator = Pipeline([("imputer", Imputer(missing_values=0, strategy="mean", axis=0)), ("forest", RandomForestRegressor(random_state=0, n_estimators=100))]) score = cross_val_score(estimator, X_missing, y_missing).mean() print("Score after imputation of the missing values = %.2f" % score)
bsd-3-clause
rohanp/scikit-learn
sklearn/cluster/tests/test_dbscan.py
176
12155
""" Tests for DBSCAN clustering algorithm """ import pickle import numpy as np from scipy.spatial import distance from scipy import sparse from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_in from sklearn.utils.testing import assert_not_in from sklearn.neighbors import NearestNeighbors from sklearn.cluster.dbscan_ import DBSCAN from sklearn.cluster.dbscan_ import dbscan from sklearn.cluster.tests.common import generate_clustered_data from sklearn.metrics.pairwise import pairwise_distances n_clusters = 3 X = generate_clustered_data(n_clusters=n_clusters) def test_dbscan_similarity(): # Tests the DBSCAN algorithm with a similarity array. # Parameters chosen specifically for this task. eps = 0.15 min_samples = 10 # Compute similarities D = distance.squareform(distance.pdist(X)) D /= np.max(D) # Compute DBSCAN core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - (1 if -1 in labels else 0) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric="precomputed", eps=eps, min_samples=min_samples) labels = db.fit(D).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_feature(): # Tests the DBSCAN algorithm with a feature vector array. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 metric = 'euclidean' # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples) labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_sparse(): core_sparse, labels_sparse = dbscan(sparse.lil_matrix(X), eps=.8, min_samples=10) core_dense, labels_dense = dbscan(X, eps=.8, min_samples=10) assert_array_equal(core_dense, core_sparse) assert_array_equal(labels_dense, labels_sparse) def test_dbscan_sparse_precomputed(): D = pairwise_distances(X) nn = NearestNeighbors(radius=.9).fit(X) D_sparse = nn.radius_neighbors_graph(mode='distance') # Ensure it is sparse not merely on diagonals: assert D_sparse.nnz < D.shape[0] * (D.shape[0] - 1) core_sparse, labels_sparse = dbscan(D_sparse, eps=.8, min_samples=10, metric='precomputed') core_dense, labels_dense = dbscan(D, eps=.8, min_samples=10, metric='precomputed') assert_array_equal(core_dense, core_sparse) assert_array_equal(labels_dense, labels_sparse) def test_dbscan_no_core_samples(): rng = np.random.RandomState(0) X = rng.rand(40, 10) X[X < .8] = 0 for X_ in [X, sparse.csr_matrix(X)]: db = DBSCAN(min_samples=6).fit(X_) assert_array_equal(db.components_, np.empty((0, X_.shape[1]))) assert_array_equal(db.labels_, -1) assert_equal(db.core_sample_indices_.shape, (0,)) def test_dbscan_callable(): # Tests the DBSCAN algorithm with a callable metric. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 # metric is the function reference, not the string key. metric = distance.euclidean # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_balltree(): # Tests the DBSCAN algorithm with balltree for neighbor calculation. eps = 0.8 min_samples = 10 D = pairwise_distances(X) core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='kd_tree') labels = db.fit(X).labels_ n_clusters_3 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_3, n_clusters) db = DBSCAN(p=1.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_4 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_4, n_clusters) db = DBSCAN(leaf_size=20, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_5 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_5, n_clusters) def test_input_validation(): # DBSCAN.fit should accept a list of lists. X = [[1., 2.], [3., 4.]] DBSCAN().fit(X) # must not raise exception def test_dbscan_badargs(): # Test bad argument values: these should all raise ValueErrors assert_raises(ValueError, dbscan, X, eps=-1.0) assert_raises(ValueError, dbscan, X, algorithm='blah') assert_raises(ValueError, dbscan, X, metric='blah') assert_raises(ValueError, dbscan, X, leaf_size=-1) assert_raises(ValueError, dbscan, X, p=-1) def test_pickle(): obj = DBSCAN() s = pickle.dumps(obj) assert_equal(type(pickle.loads(s)), obj.__class__) def test_boundaries(): # ensure min_samples is inclusive of core point core, _ = dbscan([[0], [1]], eps=2, min_samples=2) assert_in(0, core) # ensure eps is inclusive of circumference core, _ = dbscan([[0], [1], [1]], eps=1, min_samples=2) assert_in(0, core) core, _ = dbscan([[0], [1], [1]], eps=.99, min_samples=2) assert_not_in(0, core) def test_weighted_dbscan(): # ensure sample_weight is validated assert_raises(ValueError, dbscan, [[0], [1]], sample_weight=[2]) assert_raises(ValueError, dbscan, [[0], [1]], sample_weight=[2, 3, 4]) # ensure sample_weight has an effect assert_array_equal([], dbscan([[0], [1]], sample_weight=None, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 5], min_samples=6)[0]) assert_array_equal([0], dbscan([[0], [1]], sample_weight=[6, 5], min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 6], min_samples=6)[0]) # points within eps of each other: assert_array_equal([0, 1], dbscan([[0], [1]], eps=1.5, sample_weight=[5, 1], min_samples=6)[0]) # and effect of non-positive and non-integer sample_weight: assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[5.9, 0.1], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[6, -1], eps=1.5, min_samples=6)[0]) # for non-negative sample_weight, cores should be identical to repetition rng = np.random.RandomState(42) sample_weight = rng.randint(0, 5, X.shape[0]) core1, label1 = dbscan(X, sample_weight=sample_weight) assert_equal(len(label1), len(X)) X_repeated = np.repeat(X, sample_weight, axis=0) core_repeated, label_repeated = dbscan(X_repeated) core_repeated_mask = np.zeros(X_repeated.shape[0], dtype=bool) core_repeated_mask[core_repeated] = True core_mask = np.zeros(X.shape[0], dtype=bool) core_mask[core1] = True assert_array_equal(np.repeat(core_mask, sample_weight), core_repeated_mask) # sample_weight should work with precomputed distance matrix D = pairwise_distances(X) core3, label3 = dbscan(D, sample_weight=sample_weight, metric='precomputed') assert_array_equal(core1, core3) assert_array_equal(label1, label3) # sample_weight should work with estimator est = DBSCAN().fit(X, sample_weight=sample_weight) core4 = est.core_sample_indices_ label4 = est.labels_ assert_array_equal(core1, core4) assert_array_equal(label1, label4) est = DBSCAN() label5 = est.fit_predict(X, sample_weight=sample_weight) core5 = est.core_sample_indices_ assert_array_equal(core1, core5) assert_array_equal(label1, label5) assert_array_equal(label1, est.labels_) def test_dbscan_core_samples_toy(): X = [[0], [2], [3], [4], [6], [8], [10]] n_samples = len(X) for algorithm in ['brute', 'kd_tree', 'ball_tree']: # Degenerate case: every sample is a core sample, either with its own # cluster or including other close core samples. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=1) assert_array_equal(core_samples, np.arange(n_samples)) assert_array_equal(labels, [0, 1, 1, 1, 2, 3, 4]) # With eps=1 and min_samples=2 only the 3 samples from the denser area # are core samples. All other points are isolated and considered noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=2) assert_array_equal(core_samples, [1, 2, 3]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # Only the sample in the middle of the dense area is core. Its two # neighbors are edge samples. Remaining samples are noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=3) assert_array_equal(core_samples, [2]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # It's no longer possible to extract core samples with eps=1: # everything is noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=4) assert_array_equal(core_samples, []) assert_array_equal(labels, -np.ones(n_samples)) def test_dbscan_precomputed_metric_with_degenerate_input_arrays(): # see https://github.com/scikit-learn/scikit-learn/issues/4641 for # more details X = np.eye(10) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert_equal(len(set(labels)), 1) X = np.zeros((10, 10)) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert_equal(len(set(labels)), 1)
bsd-3-clause
cumc-dbmi/pmi_sprint_reporter
run_config.py
1
1638
""" Module contains runtime configuration variables """ import pandas from sqlalchemy import DateTime from sqlalchemy import create_engine import resources import settings from sqlalchemy.dialects.mssql import DATETIME2 engine = create_engine(settings.conn_str) all_hpos = pandas.read_csv(resources.hpo_csv_path) all_hpo_ids = all_hpos.hpo_id.unique() multi_schema_supported = engine.dialect.name in ['mssql', 'postgresql', 'oracle'] if settings.hpo_id == 'all': hpo_ids = all_hpo_ids else: if settings.hpo_id.lower() not in all_hpo_ids: raise RuntimeError('%s not a valid hpo_id' % settings.hpo_id) hpo_ids = [settings.hpo_id] if len(hpo_ids) > 1 and not multi_schema_supported: raise Exception('Cannot process. Multiple schemas not supported by configured engine.') use_multi_schemas = multi_schema_supported and (len(hpo_ids) > 1 or settings.force_multi_schema) datetime_tpe = DATETIME2 if 'mssql' in settings.conn_str else DateTime(True) cdm_dialect = None if 'mssql' in settings.conn_str: cdm_dialect = 'sql server' elif 'oracle' in settings.conn_str: cdm_dialect = 'oracle' elif 'postgres' in settings.conn_str: cdm_dialect = 'postgres' def permitted_file_names(): cdm_df = pandas.read_csv(resources.cdm_csv_path) included_tables = pandas.read_csv(resources.pmi_tables_csv_path).table_name.unique() tables = cdm_df[cdm_df['table_name'].isin(included_tables)].groupby(['table_name']) sprint_num = settings.sprint_num for hpo_id in all_hpo_ids: for table_name, _ in tables: yield '%(hpo_id)s_%(table_name)s_datasprint_%(sprint_num)s.csv' % locals()
mit
jorge2703/scikit-learn
examples/neighbors/plot_approximate_nearest_neighbors_scalability.py
225
5719
""" ============================================ Scalability of Approximate Nearest Neighbors ============================================ This example studies the scalability profile of approximate 10-neighbors queries using the LSHForest with ``n_estimators=20`` and ``n_candidates=200`` when varying the number of samples in the dataset. The first plot demonstrates the relationship between query time and index size of LSHForest. Query time is compared with the brute force method in exact nearest neighbor search for the same index sizes. The brute force queries have a very predictable linear scalability with the index (full scan). LSHForest index have sub-linear scalability profile but can be slower for small datasets. The second plot shows the speedup when using approximate queries vs brute force exact queries. The speedup tends to increase with the dataset size but should reach a plateau typically when doing queries on datasets with millions of samples and a few hundreds of dimensions. Higher dimensional datasets tends to benefit more from LSHForest indexing. The break even point (speedup = 1) depends on the dimensionality and structure of the indexed data and the parameters of the LSHForest index. The precision of approximate queries should decrease slowly with the dataset size. The speed of the decrease depends mostly on the LSHForest parameters and the dimensionality of the data. """ from __future__ import division print(__doc__) # Authors: Maheshakya Wijewardena <maheshakya.10@cse.mrt.ac.lk> # Olivier Grisel <olivier.grisel@ensta.org> # # License: BSD 3 clause ############################################################################### import time import numpy as np from sklearn.datasets.samples_generator import make_blobs from sklearn.neighbors import LSHForest from sklearn.neighbors import NearestNeighbors import matplotlib.pyplot as plt # Parameters of the study n_samples_min = int(1e3) n_samples_max = int(1e5) n_features = 100 n_centers = 100 n_queries = 100 n_steps = 6 n_iter = 5 # Initialize the range of `n_samples` n_samples_values = np.logspace(np.log10(n_samples_min), np.log10(n_samples_max), n_steps).astype(np.int) # Generate some structured data rng = np.random.RandomState(42) all_data, _ = make_blobs(n_samples=n_samples_max + n_queries, n_features=n_features, centers=n_centers, shuffle=True, random_state=0) queries = all_data[:n_queries] index_data = all_data[n_queries:] # Metrics to collect for the plots average_times_exact = [] average_times_approx = [] std_times_approx = [] accuracies = [] std_accuracies = [] average_speedups = [] std_speedups = [] # Calculate the average query time for n_samples in n_samples_values: X = index_data[:n_samples] # Initialize LSHForest for queries of a single neighbor lshf = LSHForest(n_estimators=20, n_candidates=200, n_neighbors=10).fit(X) nbrs = NearestNeighbors(algorithm='brute', metric='cosine', n_neighbors=10).fit(X) time_approx = [] time_exact = [] accuracy = [] for i in range(n_iter): # pick one query at random to study query time variability in LSHForest query = queries[rng.randint(0, n_queries)] t0 = time.time() exact_neighbors = nbrs.kneighbors(query, return_distance=False) time_exact.append(time.time() - t0) t0 = time.time() approx_neighbors = lshf.kneighbors(query, return_distance=False) time_approx.append(time.time() - t0) accuracy.append(np.in1d(approx_neighbors, exact_neighbors).mean()) average_time_exact = np.mean(time_exact) average_time_approx = np.mean(time_approx) speedup = np.array(time_exact) / np.array(time_approx) average_speedup = np.mean(speedup) mean_accuracy = np.mean(accuracy) std_accuracy = np.std(accuracy) print("Index size: %d, exact: %0.3fs, LSHF: %0.3fs, speedup: %0.1f, " "accuracy: %0.2f +/-%0.2f" % (n_samples, average_time_exact, average_time_approx, average_speedup, mean_accuracy, std_accuracy)) accuracies.append(mean_accuracy) std_accuracies.append(std_accuracy) average_times_exact.append(average_time_exact) average_times_approx.append(average_time_approx) std_times_approx.append(np.std(time_approx)) average_speedups.append(average_speedup) std_speedups.append(np.std(speedup)) # Plot average query time against n_samples plt.figure() plt.errorbar(n_samples_values, average_times_approx, yerr=std_times_approx, fmt='o-', c='r', label='LSHForest') plt.plot(n_samples_values, average_times_exact, c='b', label="NearestNeighbors(algorithm='brute', metric='cosine')") plt.legend(loc='upper left', fontsize='small') plt.ylim(0, None) plt.ylabel("Average query time in seconds") plt.xlabel("n_samples") plt.grid(which='both') plt.title("Impact of index size on response time for first " "nearest neighbors queries") # Plot average query speedup versus index size plt.figure() plt.errorbar(n_samples_values, average_speedups, yerr=std_speedups, fmt='o-', c='r') plt.ylim(0, None) plt.ylabel("Average speedup") plt.xlabel("n_samples") plt.grid(which='both') plt.title("Speedup of the approximate NN queries vs brute force") # Plot average precision versus index size plt.figure() plt.errorbar(n_samples_values, accuracies, std_accuracies, fmt='o-', c='c') plt.ylim(0, 1.1) plt.ylabel("precision@10") plt.xlabel("n_samples") plt.grid(which='both') plt.title("precision of 10-nearest-neighbors queries with index size") plt.show()
bsd-3-clause
hsiaoyi0504/scikit-learn
sklearn/linear_model/tests/test_sparse_coordinate_descent.py
244
9986
import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_less from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_greater from sklearn.utils.testing import ignore_warnings from sklearn.linear_model.coordinate_descent import (Lasso, ElasticNet, LassoCV, ElasticNetCV) def test_sparse_coef(): # Check that the sparse_coef propery works clf = ElasticNet() clf.coef_ = [1, 2, 3] assert_true(sp.isspmatrix(clf.sparse_coef_)) assert_equal(clf.sparse_coef_.toarray().tolist()[0], clf.coef_) def test_normalize_option(): # Check that the normalize option in enet works X = sp.csc_matrix([[-1], [0], [1]]) y = [-1, 0, 1] clf_dense = ElasticNet(fit_intercept=True, normalize=True) clf_sparse = ElasticNet(fit_intercept=True, normalize=True) clf_dense.fit(X, y) X = sp.csc_matrix(X) clf_sparse.fit(X, y) assert_almost_equal(clf_dense.dual_gap_, 0) assert_array_almost_equal(clf_dense.coef_, clf_sparse.coef_) def test_lasso_zero(): # Check that the sparse lasso can handle zero data without crashing X = sp.csc_matrix((3, 1)) y = [0, 0, 0] T = np.array([[1], [2], [3]]) clf = Lasso().fit(X, y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0]) assert_array_almost_equal(pred, [0, 0, 0]) assert_almost_equal(clf.dual_gap_, 0) def test_enet_toy_list_input(): # Test ElasticNet for various values of alpha and l1_ratio with list X X = np.array([[-1], [0], [1]]) X = sp.csc_matrix(X) Y = [-1, 0, 1] # just a straight line T = np.array([[2], [3], [4]]) # test sample # this should be the same as unregularized least squares clf = ElasticNet(alpha=0, l1_ratio=1.0) # catch warning about alpha=0. # this is discouraged but should work. ignore_warnings(clf.fit)(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [1]) assert_array_almost_equal(pred, [2, 3, 4]) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.3, max_iter=1000) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.50819], decimal=3) assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.5) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.45454], 3) assert_array_almost_equal(pred, [0.9090, 1.3636, 1.8181], 3) assert_almost_equal(clf.dual_gap_, 0) def test_enet_toy_explicit_sparse_input(): # Test ElasticNet for various values of alpha and l1_ratio with sparse X f = ignore_warnings # training samples X = sp.lil_matrix((3, 1)) X[0, 0] = -1 # X[1, 0] = 0 X[2, 0] = 1 Y = [-1, 0, 1] # just a straight line (the identity function) # test samples T = sp.lil_matrix((3, 1)) T[0, 0] = 2 T[1, 0] = 3 T[2, 0] = 4 # this should be the same as lasso clf = ElasticNet(alpha=0, l1_ratio=1.0) f(clf.fit)(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [1]) assert_array_almost_equal(pred, [2, 3, 4]) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.3, max_iter=1000) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.50819], decimal=3) assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.5) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.45454], 3) assert_array_almost_equal(pred, [0.9090, 1.3636, 1.8181], 3) assert_almost_equal(clf.dual_gap_, 0) def make_sparse_data(n_samples=100, n_features=100, n_informative=10, seed=42, positive=False, n_targets=1): random_state = np.random.RandomState(seed) # build an ill-posed linear regression problem with many noisy features and # comparatively few samples # generate a ground truth model w = random_state.randn(n_features, n_targets) w[n_informative:] = 0.0 # only the top features are impacting the model if positive: w = np.abs(w) X = random_state.randn(n_samples, n_features) rnd = random_state.uniform(size=(n_samples, n_features)) X[rnd > 0.5] = 0.0 # 50% of zeros in input signal # generate training ground truth labels y = np.dot(X, w) X = sp.csc_matrix(X) if n_targets == 1: y = np.ravel(y) return X, y def _test_sparse_enet_not_as_toy_dataset(alpha, fit_intercept, positive): n_samples, n_features, max_iter = 100, 100, 1000 n_informative = 10 X, y = make_sparse_data(n_samples, n_features, n_informative, positive=positive) X_train, X_test = X[n_samples // 2:], X[:n_samples // 2] y_train, y_test = y[n_samples // 2:], y[:n_samples // 2] s_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept, max_iter=max_iter, tol=1e-7, positive=positive, warm_start=True) s_clf.fit(X_train, y_train) assert_almost_equal(s_clf.dual_gap_, 0, 4) assert_greater(s_clf.score(X_test, y_test), 0.85) # check the convergence is the same as the dense version d_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept, max_iter=max_iter, tol=1e-7, positive=positive, warm_start=True) d_clf.fit(X_train.toarray(), y_train) assert_almost_equal(d_clf.dual_gap_, 0, 4) assert_greater(d_clf.score(X_test, y_test), 0.85) assert_almost_equal(s_clf.coef_, d_clf.coef_, 5) assert_almost_equal(s_clf.intercept_, d_clf.intercept_, 5) # check that the coefs are sparse assert_less(np.sum(s_clf.coef_ != 0.0), 2 * n_informative) def test_sparse_enet_not_as_toy_dataset(): _test_sparse_enet_not_as_toy_dataset(alpha=0.1, fit_intercept=False, positive=False) _test_sparse_enet_not_as_toy_dataset(alpha=0.1, fit_intercept=True, positive=False) _test_sparse_enet_not_as_toy_dataset(alpha=1e-3, fit_intercept=False, positive=True) _test_sparse_enet_not_as_toy_dataset(alpha=1e-3, fit_intercept=True, positive=True) def test_sparse_lasso_not_as_toy_dataset(): n_samples = 100 max_iter = 1000 n_informative = 10 X, y = make_sparse_data(n_samples=n_samples, n_informative=n_informative) X_train, X_test = X[n_samples // 2:], X[:n_samples // 2] y_train, y_test = y[n_samples // 2:], y[:n_samples // 2] s_clf = Lasso(alpha=0.1, fit_intercept=False, max_iter=max_iter, tol=1e-7) s_clf.fit(X_train, y_train) assert_almost_equal(s_clf.dual_gap_, 0, 4) assert_greater(s_clf.score(X_test, y_test), 0.85) # check the convergence is the same as the dense version d_clf = Lasso(alpha=0.1, fit_intercept=False, max_iter=max_iter, tol=1e-7) d_clf.fit(X_train.toarray(), y_train) assert_almost_equal(d_clf.dual_gap_, 0, 4) assert_greater(d_clf.score(X_test, y_test), 0.85) # check that the coefs are sparse assert_equal(np.sum(s_clf.coef_ != 0.0), n_informative) def test_enet_multitarget(): n_targets = 3 X, y = make_sparse_data(n_targets=n_targets) estimator = ElasticNet(alpha=0.01, fit_intercept=True, precompute=None) # XXX: There is a bug when precompute is not None! estimator.fit(X, y) coef, intercept, dual_gap = (estimator.coef_, estimator.intercept_, estimator.dual_gap_) for k in range(n_targets): estimator.fit(X, y[:, k]) assert_array_almost_equal(coef[k, :], estimator.coef_) assert_array_almost_equal(intercept[k], estimator.intercept_) assert_array_almost_equal(dual_gap[k], estimator.dual_gap_) def test_path_parameters(): X, y = make_sparse_data() max_iter = 50 n_alphas = 10 clf = ElasticNetCV(n_alphas=n_alphas, eps=1e-3, max_iter=max_iter, l1_ratio=0.5, fit_intercept=False) ignore_warnings(clf.fit)(X, y) # new params assert_almost_equal(0.5, clf.l1_ratio) assert_equal(n_alphas, clf.n_alphas) assert_equal(n_alphas, len(clf.alphas_)) sparse_mse_path = clf.mse_path_ ignore_warnings(clf.fit)(X.toarray(), y) # compare with dense data assert_almost_equal(clf.mse_path_, sparse_mse_path) def test_same_output_sparse_dense_lasso_and_enet_cv(): X, y = make_sparse_data(n_samples=40, n_features=10) for normalize in [True, False]: clfs = ElasticNetCV(max_iter=100, cv=5, normalize=normalize) ignore_warnings(clfs.fit)(X, y) clfd = ElasticNetCV(max_iter=100, cv=5, normalize=normalize) ignore_warnings(clfd.fit)(X.toarray(), y) assert_almost_equal(clfs.alpha_, clfd.alpha_, 7) assert_almost_equal(clfs.intercept_, clfd.intercept_, 7) assert_array_almost_equal(clfs.mse_path_, clfd.mse_path_) assert_array_almost_equal(clfs.alphas_, clfd.alphas_) clfs = LassoCV(max_iter=100, cv=4, normalize=normalize) ignore_warnings(clfs.fit)(X, y) clfd = LassoCV(max_iter=100, cv=4, normalize=normalize) ignore_warnings(clfd.fit)(X.toarray(), y) assert_almost_equal(clfs.alpha_, clfd.alpha_, 7) assert_almost_equal(clfs.intercept_, clfd.intercept_, 7) assert_array_almost_equal(clfs.mse_path_, clfd.mse_path_) assert_array_almost_equal(clfs.alphas_, clfd.alphas_)
bsd-3-clause
walterst/qiime
qiime/make_rarefaction_plots.py
6
65388
#!/usr/bin/env python # file make_rarefaction_plots.py from __future__ import division __author__ = "Meg Pirrung" __copyright__ = "Copyright 2011, The QIIME Project" __credits__ = ["Meg Pirrung", "Jesse Stombaugh", "Antonio Gonzalez Pena", "Will Van Treuren", "Yoshiki Vazquez Baeza", "Jai Ram Rideout", "Evan Bolyen"] __license__ = "GPL" __version__ = "1.9.1-dev" __maintainer__ = "Jesse Stombaugh" __email__ = "jesse.stombaugh@colorado.edu" from matplotlib import use use('Agg', warn=False) from sys import exit from qiime.parse import parse_rarefaction_data from matplotlib.pyplot import savefig, clf, gca, gcf, errorbar import matplotlib.pyplot as plt import os.path from os.path import splitext, split from qiime.colors import iter_color_groups from qiime.sort import natsort from qiime.util import create_dir, stderr from numpy import isnan, nan, array, transpose, mean, std, arange from StringIO import StringIO import urllib import base64 def save_ave_rarefaction_plots(xaxis, yvals, err, xmax, ymax, ops, mapping_category, imagetype, res, data_colors, colors, fpath, background_color, label_color, metric_name, output_type="file_creation"): '''This function creates the images, using matplotlib.''' # Create the plot image plt.clf() plt.title(metric_name + ": " + mapping_category) fig = plt.gcf() # Add the lines to the plot for o in ops: l = o plt.errorbar(xaxis[:len(yvals[o])], yvals[o], yerr=err[o][:len(yvals[o])], label=l, color= data_colors[colors[o]].toHex(), elinewidth=1, lw=2, capsize=4) # get the plot axis ax = plt.gca() ax.set_axis_bgcolor(background_color) # ax.set_yscale('log',basey=2,basex=2) # set tick colors and width for line in ax.yaxis.get_ticklines(): # line is a matplotlib.lines.Line2D instance line.set_color(label_color) line.set_markeredgewidth(1) for line in ax.xaxis.get_ticklines(): # line is a matplotlib.lines.Line2D instance line.set_color(label_color) line.set_markeredgewidth(1) # set x/y limits and labels for plot ax.set_axisbelow(True) ax.set_xlim(0, xmax) ax.set_ylim(0, ymax) ax.set_xlabel('Sequences Per Sample') ax.set_ylabel("Rarefaction Measure: " + metric_name) imgpath = fpath + mapping_category + '.' + imagetype if output_type == "file_creation": # Save the image plt.savefig(imgpath, format=imagetype, dpi=res) # Get the image name for the saved image relative to the main directory image_loc = imgpath plt.close() return elif (output_type == "memory"): imgdata = StringIO() plt.savefig(imgdata, format='png', dpi=res, transparent=True) imgdata.seek(0) plt.close() return {imgpath: imgdata} else: return None def save_single_ave_rarefaction_plots(xaxis, yvals, err, xmax, ymax, ops, mapping_category, imagetype, res, data_colors, colors, fpath, background_color, label_color, rarefaction_legend_mat, metric_name, mapping_lookup, output_type="file_creation"): '''This function creates the images, using matplotlib.''' avg_plots = {} # Add the lines to the plot for o in ops: rarefaction_legend_mat[metric_name]['groups'][mapping_category][o]['ave_link'] = \ os.path.join('html_plots', metric_name + mapping_lookup[mapping_category + '-' + o] + '_ave.' + imagetype) # Create the plot image plt.clf() plt.title(metric_name + ": " + mapping_category, weight='regular') fig = plt.gcf() l = o plt.errorbar(xaxis[:len(yvals[o])], yvals[o], yerr=err[o][:len(yvals[o])], label=l, color= data_colors[colors[o]].toHex(), elinewidth=1, lw=2, capsize=4 ) plt.alpha = (0) # get the plot axis ax = plt.gca() # set tick colors and width for line in ax.yaxis.get_ticklines(): # line is a matplotlib.lines.Line2D instance line.set_color('black') line.set_markeredgewidth(1) for line in ax.xaxis.get_ticklines(): # line is a matplotlib.lines.Line2D instance line.set_color('black') line.set_markeredgewidth(1) # set x/y limits and labels for plot ax.set_axisbelow(True) ax.set_xlim((0, xmax)) ax.set_ylim((0, ymax)) ax.set_xlabel('Sequences Per Sample') ax.set_ylabel("Rarefaction Measure: " + metric_name) x = ax.xaxis.get_label() x.set_weight('regular') # x.set_name('Arial') y = ax.yaxis.get_label() y.set_weight('regular') # y.set_name('Arial') imgpath = fpath + \ mapping_lookup[mapping_category + '-' + o] + \ '_ave.' + imagetype if output_type == "file_creation": # Save the image plt.savefig( imgpath, format=imagetype, dpi=res, transparent=True) # Get the image name for the saved image relative to the main # directory image_loc = imgpath plt.close() elif (output_type == "memory"): imgdata = StringIO() plt.savefig(imgdata, format='png', dpi=res, transparent=True) imgdata.seek(0) avg_plots[imgpath] = imgdata plt.close() if output_type == "file_creation": return rarefaction_legend_mat elif output_type == "memory": return rarefaction_legend_mat, avg_plots def save_single_rarefaction_plots(sample_dict, imagetype, metric_name, data_colors, colors, fpath, background_color, label_color, res, ymax, xmax, rarefaction_legend_mat, groups, mapping_category, group_id, mapping_lookup, output_type="file_creation"): '''This function creates the images, using matplotlib.''' # Create the plot image plt.clf() # plt.title(str(metric_name)) fig = plt.gcf() ax = fig.add_subplot(111) for o in groups: for i in sample_dict[o]: xaxis = [] # this creates duplicates of the xval, since there are several # iterations for t in range(len(sample_dict[o][i])): xaxis.append(i) # If all the yvals are nan at a particular xval, skip adding # it to the plot if not isnan(sample_dict[o][i])[0]: scplot = ax.scatter(xaxis, sample_dict[o][i], c=data_colors[colors[o]].toHex(), marker='s', edgecolors='none') # get the plot axis ax = plt.gca() ax.set_axis_bgcolor(background_color) # set tick colors and width for line in ax.yaxis.get_ticklines(): # line is a matplotlib.lines.Line2D instance line.set_color(label_color) line.set_markeredgewidth(1) for line in ax.xaxis.get_ticklines(): # line is a matplotlib.lines.Line2D instance line.set_color(label_color) line.set_markeredgewidth(1) # set x/y limits and labels for plot ax.set_axisbelow(False) ax.set_xlim((0, xmax)) ax.set_ylim((0, ymax)) ax.set_xlabel('Sequences Per Sample') ax.set_ylabel("Rarefaction Measure: " + str(metric_name)) x = ax.xaxis.get_label() x.set_weight('regular') # x.set_name('Arial') y = ax.yaxis.get_label() y.set_weight('regular') # y.set_name('Arial') # Create file for image imgpath = os.path.join( fpath, metric_name + mapping_lookup[ mapping_category + '-' + group_id] + '_raw.' + imagetype) # Since both the average and raw are saved the same way we will save the # raw link as well rarefaction_legend_mat[metric_name]['groups'][mapping_category][group_id]['raw_link'] = \ os.path.join('html_plots', metric_name + mapping_lookup[mapping_category + '-' + group_id] + '_raw.' + imagetype) if output_type == "file_creation": # Save the image plt.savefig(imgpath, format=imagetype, dpi=res, transparent=True) # Get the image name for the saved image relative to the main directory image_loc = imgpath plt.close() return rarefaction_legend_mat elif (output_type == "memory"): imgdata = StringIO() plt.savefig(imgdata, format='png', dpi=res, transparent=True) imgdata.seek(0) plt.close() return [rarefaction_legend_mat, {imgpath: imgdata}] def get_rarefaction_data(rarefaction_data, col_headers): '''This function takes a rarefaction file and converts it into an array''' rare_mat_raw = array(rarefaction_data) rare_mat_min = [rare_mat_raw[x][2:] for x in range(0, len(rare_mat_raw))] seqs_per_samp = [rare_mat_raw[x][0] for x in range(0, len(rare_mat_raw))] sampleIDs = col_headers[3:] # Need to transpose the array to be used in averaging rare_mat_trans = transpose(array(rare_mat_min)).tolist() return rare_mat_trans, seqs_per_samp, sampleIDs def ave_seqs_per_sample(matrix, seqs_per_samp, sampleIDs): """Calculate the average for each sampleID across each number of \ seqs/sample""" ave_ser = {} temp_dict = {} # Iterate through the samples id's and create a dictionary for i, sid in enumerate(sampleIDs): temp_dict[sid] = {} for j, seq in enumerate(seqs_per_samp): try: temp_dict[sid][seq].append(matrix[i][j]) except(KeyError): temp_dict[sid][seq] = [] temp_dict[sid][seq].append(matrix[i][j]) # create a dictionary for average data for sid in sampleIDs: ave_ser[sid] = [] keys = sorted(temp_dict[sid].keys()) for k in keys: ave_ser[sid].append(mean(array(temp_dict[sid][k]), 0)) return ave_ser def make_error_series(rare_mat, groups, std_type): """Create mean and error bar series for the supplied mapping category""" err_ser = dict() collapsed_ser = dict() seen = set() pre_err = {} ops = [k for k in groups] notfound = [] # Iterate through the groups for o in ops: pre_err[o] = [] # For each sample in group, create a row in a list for samID in groups[o]: pre_err[o].append(rare_mat[samID]) min_len = min([len(i) - i.count('nan') for i in pre_err[o]]) pre_err[o] = [x[:min_len] for x in pre_err[o]] # iterate through the groups and calculate std deviations and error for o in ops: opsarray = array(pre_err[o]) mn = mean(opsarray, 0) collapsed_ser[o] = mn.tolist() if std_type == 'stderr': # this calculates the standard error # (using sample standard deviation) stderr_result = stderr(opsarray, 0) err_ser[o] = stderr_result.tolist() else: # this calculates the population standard deviation stddev = std(opsarray, 0) err_ser[o] = stddev.tolist() return collapsed_ser, err_ser, ops def save_rarefaction_data(rare_mat, xaxis, xmax, mapping_category, colors, rare_type, data_colors, groups, std_type): '''This function formats the average data and writes it to the output directory''' # get the error data yaxis, err, ops = make_error_series(rare_mat, groups, std_type) lines = [] lines.append("# " + rare_type + '\n') lines.append("# " + mapping_category + '\n') line = '' line += 'xaxis: ' for v in xaxis: line += str(v) + '\t' line += '\n' lines.append(line) lines.append('xmax: ' + str(xmax) + '\n') for o in colors.keys(): lines.append(">> " + o + '\n') # write the color lines if colors is not None: try: lines.append("color " + data_colors[colors[o]].toHex() + '\n') except(KeyError): print 'Color reference is missing!' # write the rarefection series lines lines.append('series ') line = '' try: for v in yaxis[o]: line += str(v) + '\t' except(TypeError): line += str(yaxis[o]) line += '\n' lines.append(line) # write the rarefaction error lines lines.append('error ') line = '' try: for e in err[o]: if e == 0: line += str(nan) + '\t' else: line += str(e) + '\t' except(TypeError): line += str(err[o]) line += '\n' lines.append(line) return lines def make_averages(color_prefs, data, background_color, label_color, rares, output_dir, resolution, imagetype, ymax, suppress_webpage, std_type, output_type="file_creation", generate_per_sample_plots=True, generate_average_tables=True): '''This is the main function, which takes the rarefaction files, calls the functions to make plots and formatting the output html.''' rarelines = [] rarefaction_legend_mat = {} if ymax: user_ymax = True else: user_ymax = False if not suppress_webpage and output_type == "file_creation": # in this option the path must include the output directory ave_output_dir = os.path.join(output_dir, 'average_plots') if generate_per_sample_plots: all_output_dir = os.path.join(output_dir, 'html_plots') # Create the directories, where plots and data will be written create_dir(all_output_dir) else: all_output_dir = "" elif output_type == 'memory': # this is rather an artificial path to work with the javascript code ave_output_dir = 'plot/average_plots' if generate_per_sample_plots: all_output_dir = 'plot/html_plots' else: all_output_dir = "" ave_data_file_path = os.path.join(output_dir, 'average_tables') if output_type == "file_creation": create_dir(ave_output_dir) if generate_average_tables: create_dir(ave_data_file_path, False) metric_num = 0 rarefaction_legend_mat = {} rarefaction_data_mat = {} rare_num = 0 # this is a fix for the issue of writing field values as the filenames mapping_lookup = {} for i, column in enumerate(data['map'][0]): for j, row in enumerate(data['map'][1:]): mapping_lookup['%s-%s' % (column, row[i])] = 'col_%s_row_%s' % \ (str(i), str(j)) all_plots = [] # Iterate through the rarefaction files for r in natsort(rares): raredata = rares[r] metric_name = r.split('.')[0] # convert the rarefaction data into variables col_headers, comments, rarefaction_fn, rarefaction_data = rares[r] # Here we only need to perform these steps once, since the data is # the same for all rarefaction files if rare_num == 0: # Remove samples from the mapping file, which contain no data after # rarefaction updated_mapping = [] for j in data['map']: # Add the mapping header if j[0] == 'SampleID': updated_mapping.append(j) # Determine if the sample exists in the rarefaction file for i in col_headers[3:]: if j[0] == i: updated_mapping.append(j) # Get the groups and colors for the updated mapping file groups_and_colors = iter_color_groups(updated_mapping, color_prefs) groups_and_colors = list(groups_and_colors) # parse the rarefaction data rare_mat_trans, seqs_per_samp, sampleIDs = \ get_rarefaction_data(rarefaction_data, col_headers) rarefaction_legend_mat[metric_name] = {} # Create dictionary variables and get the colors for each Sample sample_colors = None rarefaction_legend_mat[metric_name]['groups'] = {} for i in range(len(groups_and_colors)): labelname = groups_and_colors[i][0] # Create a legend dictionary for html output rarefaction_legend_mat[metric_name]['groups'][labelname] = {} # If this is the first time iterating through the rarefaction data # create a data dictionary for html output if rare_num == 0: rarefaction_data_mat[labelname] = {} # If the labelname is SampleID, use the colors assigned if labelname == 'SampleID': sample_colors = groups_and_colors[i][2] sample_data_colors = groups_and_colors[i][3] rare_num = 1 # If sample colors were not assigned, create a list of sample colors if not sample_colors: samples_and_colors = iter_color_groups(updated_mapping, {'SampleID': {'column': 'SampleID', 'colors': (('red', (0, 100, 100)), ('blue', (240, 100, 100)))}}) samples_and_colors = list(samples_and_colors) sample_colors = samples_and_colors[0][2] sample_data_colors = samples_and_colors[0][3] sample_dict = {} # Create a dictionary containing the samples for i, sid in enumerate(sampleIDs): if sid in (i[0] for i in updated_mapping): sample_dict[sid] = {} for j, seq in enumerate(seqs_per_samp): try: sample_dict[sid][seq].append(rare_mat_trans[i][j]) except(KeyError): sample_dict[sid][seq] = [] sample_dict[sid][seq].append(rare_mat_trans[i][j]) # convert xvals to float xaxisvals = sorted([float(x) for x in set(seqs_per_samp)]) # get the rarefaction averages rare_mat_ave = ave_seqs_per_sample(rare_mat_trans, seqs_per_samp, sampleIDs) # calculate the max xval xmax = max(xaxisvals) + (xaxisvals[len(xaxisvals) - 1] - xaxisvals[len(xaxisvals) - 2]) if not user_ymax: ymax = 0 for i in range(len(groups_and_colors)): labelname = groups_and_colors[i][0] groups = groups_and_colors[i][1] colors = groups_and_colors[i][2] data_colors = groups_and_colors[i][3] if generate_average_tables: ave_file_path = os.path.join( ave_data_file_path, metric_name) # save the rarefaction averages rare_lines = save_rarefaction_data( rare_mat_ave, xaxisvals, xmax, labelname, colors, r, data_colors, groups, std_type) # write out the rarefaction average data if output_type == "file_creation" and generate_average_tables: open( ave_file_path + labelname + '.txt', 'w').writelines( rare_lines) # take the formatted rarefaction averages and format the # results rares_data = parse_rarefaction_data( ''.join(rare_lines[:]).split('\n')) # determine the ymax based on the average data # multiple the ymax, since the dots can end up on the border new_ymax = (max([max(v) for v in rares_data['series'].values()]) + max([max(e) for e in rares_data['error'].values()])) * 1.15 if isnan(new_ymax): new_ymax = (max([max(v) for v in rares_data['series'].values()])) * 1.15 if new_ymax > ymax: ymax = new_ymax iterator_num = 0 # iterate through the groups for i in range(len(groups_and_colors)): labelname = groups_and_colors[i][0] groups = groups_and_colors[i][1] colors = groups_and_colors[i][2] data_colors = groups_and_colors[i][3] data_color_order = groups_and_colors[i][4] # save the rarefaction averages rare_lines = save_rarefaction_data(rare_mat_ave, xaxisvals, xmax, labelname, colors, r, data_colors, groups, std_type) # take the formatted rarefaction averages and format the results rares_data = parse_rarefaction_data( ''.join(rare_lines[:]).split('\n')) if not suppress_webpage: if iterator_num == 0: rarefaction_legend_mat[metric_name]['samples'] = {} for o in sample_dict: rarefaction_legend_mat[metric_name]['samples'][o] = {} # Add values to the legend dictionary rarefaction_legend_mat[metric_name]['samples'][o][ 'color'] = sample_data_colors[sample_colors[o]].toHex() iterator_num = 1 # Iterate through the groups and create the legend dictionary for g in groups: # create a dictionary of samples and their colors rarefaction_legend_mat[ metric_name][ 'groups'][ labelname][ g] = { } rarefaction_legend_mat[metric_name]['groups'][ labelname][g]['groupsamples'] = groups[g] rarefaction_legend_mat[metric_name]['groups'][labelname][g]['groupcolor'] =\ data_colors[colors[g]].toHex() # Create the individual category average plots if output_type == "file_creation": rarefaction_data_mat, rarefaction_legend_mat = make_plots( background_color, label_color, rares_data, ymax, xmax, all_output_dir, resolution, imagetype, groups, colors, data_colors, metric_name, labelname, rarefaction_data_mat, rarefaction_legend_mat, sample_dict, sample_data_colors, sample_colors, mapping_lookup, output_type, generate_per_sample_plots) elif output_type == "memory": rarefaction_data_mat, rarefaction_legend_mat, all_plots_single, \ all_plots_ave = make_plots( background_color, label_color, rares_data, ymax, xmax, all_output_dir, resolution, imagetype, groups, colors, data_colors, metric_name, labelname, rarefaction_data_mat, rarefaction_legend_mat, sample_dict, sample_data_colors, sample_colors, mapping_lookup, output_type, generate_per_sample_plots) # generate the filepath for the image file file_path = os.path.join(ave_output_dir, splitext(split(rares_data['headers'][0])[1])[0]) # Create the average plots categories = [k for k in groups] all_plots_rare = save_ave_rarefaction_plots( rares_data['xaxis'], rares_data['series'], rares_data[ 'error'], xmax, ymax, categories, labelname, imagetype, resolution, data_colors, colors, file_path, background_color, label_color, metric_name, output_type) if output_type == "memory": all_plots.append(all_plots_rare) all_plots.extend(all_plots_single) all_plots.append(all_plots_ave) else: # generate the filepath for the image file file_path = os.path.join(ave_output_dir, splitext(split(rares_data['headers'][0])[1])[0]) categories = [k for k in groups] all_plots_rare = save_ave_rarefaction_plots( rares_data['xaxis'], rares_data['series'], rares_data[ 'error'], xmax, ymax, categories, labelname, imagetype, resolution, data_colors, colors, file_path, background_color, label_color, metric_name, output_type) if not suppress_webpage: # format the html output html_output = make_html(rarefaction_legend_mat, rarefaction_data_mat, xaxisvals, imagetype, mapping_lookup, output_type, all_plots, generate_per_sample_plots) else: html_output = None return html_output def make_html(rarefaction_legend_mat, rarefaction_data_mat, xaxisvals, imagetype, mapping_lookup, output_type="file_creation", all_plots=None, generate_per_sample_plots=True): rarefaction_legend_mat legend_td = [ '<b>Legend</b><div STYLE="border: thin black solid; height: 300px; width: 200px; font-size: 12px; overflow: auto;"><table>'] summarized_table = [] metric_select_html = [] category_select_html = [] data_table_html = [] metrics = [] category_colors = {} cat_iter = 0 # iterate the legend dictionary for m in natsort(rarefaction_legend_mat): # Create the metric select box options metric_select_html.append('<option value="%s">%s</option>' % (m, m)) metrics.append(m) # iterate through the categories in the legend dictionary for category in natsort(rarefaction_legend_mat[m]['groups']): category_colors[category] = {} # Create the select box options if cat_iter == 0: cat_links = [] for i in rarefaction_legend_mat[m]['groups'][category]: cat_links.append(mapping_lookup[category + '-' + i]) category_select_html.append('<option value="%s">%s</option>' % (category + '$#!' + '$#!'.join(cat_links), category)) plot_iterator = 0 # iterate through the groups in the legend dictionary and create # the html formatted rows for each category and group for group in natsort(rarefaction_legend_mat[m]['groups'][category]): sample_list = [] category_colors[category][group] =\ rarefaction_legend_mat[m]['groups'][ category][group]['groupcolor'] for sample in natsort(rarefaction_legend_mat[m]['groups'][category][group]['groupsamples']): sample_list.append('\'' + sample + '\'') plot_iterator = plot_iterator + 1 if generate_per_sample_plots: legend_td.append( '<tr id="%s" name="%s" style="display: none;"><td class="data" onmouseover="document.body.style.cursor=\'pointer\'" onmouseout="document.body.style.cursor=\'default\'" onclick="toggle(%s)" id="%s" name="%s">&#x25B6;</td><td><input name="%s" type="checkbox" checked="True" onclick="show_hide_category(this)"></td><td style="color:%s">&#x25A0;&nbsp;</td><td class="data"><b>%s</b></td></tr>' % (m + category, m + category, "'" + m + mapping_lookup[ category + '-' + group] + "'", m + mapping_lookup[ category + '-' + group], ','.join( sample_list), m + mapping_lookup[category + '-' + group] + '_raw.' + imagetype, rarefaction_legend_mat[m]['groups'][ category][ group][ 'groupcolor'], group)) else: legend_td.append( '<tr id="%s" name="%s" style="display: none;"><td class="data" onmouseover="document.body.style.cursor=\'pointer\'" onmouseout="document.body.style.cursor=\'default\'" onclick="toggle(%s)" id="%s" name="%s">&#x25B6;</td><td>&nbsp;</td><td style="color:%s">&#x25A0;&nbsp;</td><td class="data"><b>%s</b></td></tr>' % (m + category, m + category, "'" + m + mapping_lookup[ category + '-' + group] + "'", m + mapping_lookup[ category + '-' + group], ','.join( sample_list), rarefaction_legend_mat[m]['groups'][ category][ group][ 'groupcolor'], group)) for sample in natsort(rarefaction_legend_mat[m]['groups'][category][group]['groupsamples']): sample = str(sample) legend_td.append( '<tr id="%s" name="%s" style="display: none;"><td class="data" align="right">&#x221F;</td><td></td><td style="color:%s">&#x25C6;</td><td class="data" align="left"><b>%s</b></td></tr>' % (m + mapping_lookup[category + '-' + group] + '_raw', m + mapping_lookup[category + '-' + group], rarefaction_legend_mat[m]['samples'][sample]['color'], sample)) cat_iter = 1 # iterate through the data dictionary and format the rows for the html # data table for category in rarefaction_data_mat: data_table_html.append( '<tr name="%s" style="display: none;"><td class="headers">%s</td><td class="headers">Seqs/Sample</td>' % (category, category)) for j in metrics: data_table_html.append( '<td class="headers">%s Ave.</td><td class="headers">%s Err.</td>' % (j, j)) data_table_html.append('</tr>') #data_table_html.append('<tr name="%s" style="display: none;"></tr>' % (category)) for g in natsort(rarefaction_data_mat[category]): for i in range(len(xaxisvals)): data_table_html.append( '<tr name="%s" style="display: none;">' % (category)) data_table_html.append( '<td class="data" bgcolor="%s">%s</td><td class="data">%s</td>' % (category_colors[category][g], g, xaxisvals[i])) # bugfix, was rarefaction_data_mat[category][g] for m in metrics: data_table_html.append( '<td class="data">%s</td><td class="data">%s</td>' % (rarefaction_data_mat[category][g][m]['ave'][i], rarefaction_data_mat[category][g][m]['err'][i])) data_table_html.append('</tr>') legend_td.append('</table></div></div>') # Create the table that contains the plots and table plot_html = '%s' % ('\n'.join(legend_td)) if output_type == "file_creation": # insert the formatted rows into the html string at the bottom of this # file if generate_per_sample_plots: html_output = HTML % ('', "img.setAttribute('src',\"./html_plots/\"+SelObject.value+array[i]+'_ave'+imagetype)", "img.setAttribute('src',\"./html_plots/\"+metric+array[i]+'_ave'+imagetype)", "img.setAttribute('src',\"./html_plots/\"+arguments[0]+'_raw'+imagetype)", '.' + imagetype, '\n'.join(metric_select_html), '\n'.join(category_select_html), plot_html, '\n'.join(data_table_html)) else: html_output = HTML % ('', "img.setAttribute('src',\"./average_plots/\"+SelObject.value+array[0]+imagetype)", "img.setAttribute('src',\"./average_plots/\"+metric+array[0]+imagetype)", "", '.' + imagetype, '\n'.join(metric_select_html), '\n'.join(category_select_html), plot_html, '\n'.join(data_table_html)) elif output_type == "memory": plots_html = ['all_plots = {}'] for elements in all_plots: for k, v in elements.items(): # the path is compatible with the javascript, see make_averages plots_html.append('all_plots["%s"] = "%s"' % (k, "data:image/png;base64," + urllib.quote(base64.b64encode(v.buf)))) # insert the formatted rows into the html string at the bottom of this # file if generate_per_sample_plots: html_output = HTML % ('\n'.join(plots_html), "img.setAttribute('src',all_plots[\"plot/html_plots/\"+SelObject.value+array[i]+'_ave'+imagetype])", "img.setAttribute('src',all_plots[\"plot/html_plots/\"+metric+array[i]+'_ave'+imagetype])", "img.setAttribute('src',all_plots[\"plot/html_plots/\"+arguments[0]+'_raw'+imagetype])", '.' + imagetype, '\n'.join(metric_select_html), '\n'.join(category_select_html), plot_html, '\n'.join(data_table_html)) else: html_output = HTML % ('', "img.setAttribute('src',\"./average_plots/\"+SelObject.value+array[0]+imagetype)", "img.setAttribute('src',\"./average_plots/\"+metric+array[0]+imagetype)", "", '.' + imagetype, '\n'.join(metric_select_html), '\n'.join(category_select_html), plot_html, '\n'.join(data_table_html)) return html_output def make_plots(background_color, label_color, rares, ymax, xmax, output_dir, resolution, imagetype, groups, colors, data_colors, metric_name, labelname, rarefaction_data_mat, rarefaction_legend_mat, sample_dict, sample_data_colors, sample_colors, mapping_lookup, output_type="file_creation", generate_per_sample_plots=True): '''This is the main function for generating the rarefaction plots and html file.''' # Get the alpha rare data raredata = rares # generate the filepath for the image file file_path = os.path.join(output_dir, splitext(split(raredata['headers'][0])[1])[0]) all_plots_single = [] # Sort and iterate through the groups for i in natsort(groups): # for k in groups[i]: for j in range(len(raredata['xaxis'])): group_field = i seq_per_sample_field = int(raredata['xaxis'][j]) color_field = data_colors[colors[group_field]].toHex() # If a field is missing, then it means that one of the # samples did not contain enough sequences. # For this case, we will assign the value as n.a. try: average_field = raredata['series'][i][j] error_field = raredata['error'][i][j] if isnan(average_field): error_field = nan except: average_field = nan error_field = nan # Add context to the data dictionary, which will be used in the # html if i in rarefaction_data_mat[labelname]: if metric_name in rarefaction_data_mat[labelname][i]: rarefaction_data_mat[labelname][i][metric_name]['ave'].append( ''.join('%10.3f' % ((raredata['series'][i][j])))) rarefaction_data_mat[labelname][i][metric_name]['err'].append( ''.join('%10.3f' % ((raredata['error'][i][j])))) else: rarefaction_data_mat[labelname][i][metric_name] = {} rarefaction_data_mat[labelname][i][metric_name]['ave'] = [] rarefaction_data_mat[labelname][i][metric_name]['err'] = [] rarefaction_data_mat[labelname][i][metric_name]['ave'].append( ''.join('%10.3f' % ((raredata['series'][i][j])))) rarefaction_data_mat[labelname][i][metric_name]['err'].append( ''.join('%10.3f' % ((raredata['error'][i][j])))) else: rarefaction_data_mat[labelname][i] = {} rarefaction_data_mat[labelname][i][metric_name] = {} rarefaction_data_mat[labelname][i][metric_name]['ave'] = [] rarefaction_data_mat[labelname][i][metric_name]['err'] = [] rarefaction_data_mat[labelname][i][metric_name]['ave'].append( ''.join('%10.3f' % ((raredata['series'][i][j])))) rarefaction_data_mat[labelname][i][metric_name]['err'].append( ''.join('%10.3f' % ((raredata['error'][i][j])))) # Create raw plots for each group in a category if generate_per_sample_plots: if output_type == "file_creation": rarefaction_legend_mat = save_single_rarefaction_plots( sample_dict, imagetype, metric_name, sample_data_colors, sample_colors, output_dir, background_color, label_color, resolution, ymax, xmax, rarefaction_legend_mat, groups[i], labelname, i, mapping_lookup, output_type) elif output_type == "memory": rarefaction_legend_mat, rare_plot_for_all = save_single_rarefaction_plots( sample_dict, imagetype, metric_name, sample_data_colors, sample_colors, output_dir, background_color, label_color, resolution, ymax, xmax, rarefaction_legend_mat, groups[i], labelname, i, mapping_lookup, output_type) all_plots_single.append(rare_plot_for_all) all_plots_ave = {} if generate_per_sample_plots: # Create the rarefaction average plot and get updated legend information categories = [k for k in groups] if output_type == "file_creation": rarefaction_legend_mat = save_single_ave_rarefaction_plots( raredata['xaxis'], raredata['series'], raredata[ 'error'], xmax, ymax, categories, labelname, imagetype, resolution, data_colors, colors, file_path, background_color, label_color, rarefaction_legend_mat, metric_name, mapping_lookup, output_type) elif output_type == "memory": rarefaction_legend_mat, all_plots_ave = save_single_ave_rarefaction_plots( raredata['xaxis'], raredata['series'], raredata[ 'error'], xmax, ymax, categories, labelname, imagetype, resolution, data_colors, colors, file_path, background_color, label_color, rarefaction_legend_mat, metric_name, mapping_lookup, output_type) if output_type == "file_creation": return rarefaction_data_mat, rarefaction_legend_mat elif output_type == "memory": return ( rarefaction_data_mat, rarefaction_legend_mat, all_plots_single, all_plots_ave ) HTML = ''' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html;"> <title>Rarefaction Curves</title> <style type="text/css"> td.data{font-size:10px;border-spacing:0px 10px;text-align:center;} td.headers{font-size:12px;font-weight:bold;text-align:center;} table{border-spacing:0px;} .removed{display:none;} .expands{cursor:pointer; cursor:hand;} .child1 td:first-child{padding-left: 3px;} </style> <script language="javascript" type="text/javascript"> %s function show_hide_category(checkobject){ var imagetype=document.getElementById('imagetype').value; img=document.getElementById(checkobject.name.replace('_raw'+imagetype,'_ave'+imagetype)) if (checkobject.checked==false){ img.style.display='none'; }else{ img.style.display=''; } } function reset_tree(){ var category=document.getElementById('category').value; var metric=document.getElementById('metric').value; var old_all_categories=document.getElementById('all_categories'); var imagetype=document.getElementById('imagetype').value; cat_list=old_all_categories.value.split('$#!') if (metric!='' && category != ''){ for (var i=1, il=cat_list.length; i<il; i++){ group=metric+category+cat_list[i] main_class=metric+category var exp_item=document.getElementById(group); if (exp_item!=null){ if (exp_item.innerHTML=='\u25BC'){ exp_item.innerHTML='\u25B6' var rows=document.getElementsByName(group); for (var j=0, jl=rows.length; j<jl; j++){ rows[j].style.display="none"; } } var rows=document.getElementsByName(group+'_raw'+imagetype); for (var j=0, jl=rows.length; j<jl; j++){ if (rows[j].checked==false){ rows[j].checked=true; } } } } } } function changeMetric(SelObject){ var category=document.getElementById('category'); var old_metric=document.getElementById('metric'); var imagetype=document.getElementById('imagetype').value; var legend=document.getElementById('legend'); var array=document.getElementById('all_categories').value.split('$#!') var plots=document.getElementById('plots'); plots.style.display='none' reset_tree(); if (category.value != ''){ legend.style.display=""; cat=SelObject.value+category.value data_display=document.getElementsByName(cat) for (var i=0, il=data_display.length; i<il; i++){ data_display[i].style.display=""; } cat=old_metric.value+category.value data_hide=document.getElementsByName(cat) for (var i=0, il=data_hide.length; i<il; i++){ data_hide[i].style.display="none"; } data_display=document.getElementsByName(category.value) for (var i=0, il=data_display.length; i<il; i++){ data_display[i].style.display=""; } new_cat=SelObject.value+category.value plots.innerHTML='' for (var i=1, il=array.length; i<il; i++){ img=document.createElement('img') img.setAttribute('width',"600px") img.setAttribute('id',array[i]+'_ave'+imagetype) img.setAttribute('style','position:absolute;z-index:0') %s plots.appendChild(img) } plots.style.display='' } old_metric.value=SelObject.value; // If both combo boxes have changed the value, display a disclaimer if (document.getElementById('select_metric_combo').selectedIndex !== 0 && document.getElementById('select_category_combo').selectedIndex !== 0) { document.getElementById('nan_disclaimer').style.display='inline'; } } function changeCategory(SelObject){ var old_category=document.getElementById('category'); var metric=document.getElementById('metric').value; var imagetype=document.getElementById('imagetype').value; var legend=document.getElementById('legend'); var plots=document.getElementById('plots'); var array=SelObject.value.split('$#!') var old_all_categories=document.getElementById('all_categories') category=array[0] plots.style.display='none' reset_tree(); if (metric != ''){ legend.style.display=""; data_display=document.getElementsByName(category) for (var i=0, il=data_display.length; i<il; i++){ data_display[i].style.display=""; } data_hide=document.getElementsByName(old_category.value) for (var i=0, il=data_hide.length; i<il; i++){ data_hide[i].style.display="none"; } cat=metric+category data_display=document.getElementsByName(cat) for (var i=0, il=data_display.length; i<il; i++){ data_display[i].style.display=""; } cat=metric+old_category.value data_hide=document.getElementsByName(cat) for (var i=0, il=data_hide.length; i<il; i++){ data_hide[i].style.display="none"; } cat=metric+category plots.innerHTML='' for (var i=1, il=array.length; i<il; i++){ img=document.createElement('img') img.setAttribute('width',"600px") img.setAttribute('id',metric+array[i]+'_ave'+imagetype) img.setAttribute('style','position:absolute;z-index:0') %s plots.appendChild(img) } plots.style.display='' } old_all_categories.value=SelObject.value; old_category.value=category; // If both combo boxes have changed the value, display a disclaimer if (document.getElementById('select_metric_combo').selectedIndex !== 0 && document.getElementById('select_category_combo').selectedIndex !== 0) { document.getElementById('nan_disclaimer').style.display='inline'; } } function toggle(){ var plots=document.getElementById('plots'); var imagetype=document.getElementById('imagetype').value; var plot_str=''; var category=document.getElementById('category'); var metric=document.getElementById('metric'); expansion_element=document.getElementById(arguments[0]); rows=document.getElementsByName(arguments[0]); if (expansion_element.innerHTML=='\u25B6'){ expansion_element.innerHTML='\u25BC' show_row=arguments[0]+'_raw'+imagetype if (document.getElementById(show_row)==null){ img=document.createElement('img') img.setAttribute('width',"600px") img.setAttribute('id',arguments[0]+'_raw'+imagetype) img.setAttribute('style','position:absolute;z-index:0') %s plots.appendChild(img) }else{ document.getElementById(arguments[0]+'_raw'+imagetype).style.display='' } for (var i=0, il=rows.length;i<il;i++){ rows[i].style.display=''; } }else{ expansion_element.innerHTML='\u25B6' document.getElementById(arguments[0]+'_raw'+imagetype).style.display='none' for (var i=0, il=rows.length;i<il;i++){ rows[i].style.display='none'; } } } function show_hide_categories(SelObject){ var all_categories=document.getElementById('all_categories').value.split('$#!') var category=document.getElementById('category').value; var imagetype=document.getElementById('imagetype').value; var metric=document.getElementById('metric').value; for (var i=1, il=all_categories.length; i<il; i++){ basename=metric+category+all_categories[i] raw_image=basename+'_raw'+imagetype ave_image=basename+'_ave'+imagetype checkbox=document.getElementsByName(raw_image) if (SelObject.value=='All'){ if (checkbox[0].checked==false){ checkbox[0].checked=true document.getElementById(ave_image).style.display='' } }else if (SelObject.value=='None'){ if (checkbox[0].checked==true){ checkbox[0].checked=false document.getElementById(ave_image).style.display='none' } }else if (SelObject.value=='Invert'){ if (checkbox[0].checked==true){ checkbox[0].checked=false document.getElementById(ave_image).style.display='none' }else if (checkbox[0].checked==false){ checkbox[0].checked=true document.getElementById(ave_image).style.display='' } } } document.getElementById('show_category').selectedIndex=0; } </script> </head> <body> <form action=''> <input id="metric" type="hidden"> <input id="category" type="hidden"> <input id="imagetype" type="hidden" value="%s"> <input id="all_categories" type="hidden"> </form> <table><tr> <td><b>Select a Metric:</b></td> <td> <select onchange="javascript:changeMetric(this)" id="select_metric_combo"> <option>&nbsp;</option> %s </select> </td> <td><b>&nbsp;&nbsp;Select a Category:</b></td> <td> <select onchange="javascript:changeCategory(this)" id="select_category_combo"> <option>&nbsp;</option> %s </select> </td> </table> <br> <div style="width:950px"> <div id="plots" style="width:650px;height:550px;float:left;"></div> <div id="legend" style="width:300px;height:550px;float:right;display:none;"> <p><b>Show Categories: <select id="show_category" onchange="show_hide_categories(this);"> <option value="">&nbsp;</option> <option value="All">All</option> <option value="None">None</option> <option value="Invert">Invert</option> </select> </b></p> %s <div style="position:relative;clear:both;"> <div style="position:relative;clear:both;display:none;" class="strong" id="nan_disclaimer"> <b>If the lines for some categories do not extend all the way to the right end of the x-axis, that means that at least one of the samples in that category does not have that many sequences.</b> </div> <br><br> <table id="rare_data" border="1px"> %s </table> </div> </div> </body> </html> '''
gpl-2.0
dopplershift/MetPy
src/metpy/plots/declarative.py
1
65191
# Copyright (c) 2018,2019 MetPy Developers. # Distributed under the terms of the BSD 3-Clause License. # SPDX-License-Identifier: BSD-3-Clause """Declarative plotting tools.""" import contextlib import copy from datetime import datetime, timedelta import re import matplotlib.pyplot as plt import numpy as np import pandas as pd from traitlets import (Any, Bool, Float, HasTraits, Instance, Int, List, observe, TraitError, Tuple, Unicode, Union, validate) from . import ctables from . import wx_symbols from .cartopy_utils import import_cartopy from .station_plot import StationPlot from ..calc import reduce_point_density from ..package_tools import Exporter from ..units import units ccrs = import_cartopy() exporter = Exporter(globals()) _areas = { '105': (-129.3, -22.37, 17.52, 53.78), 'local': (-92., -64., 28.5, 48.5), 'wvaac': (120.86, -15.07, -53.6, 89.74), 'tropsfc': (-100., -55., 8., 33.), 'epacsfc': (-155., -75., -20., 33.), 'ofagx': (-100., -80., 20., 35.), 'ahsf': (-105., -30., -5., 35.), 'ehsf': (-145., -75., -5., 35.), 'shsf': (-125., -75., -20., 5.), 'tropful': (-160., 0., -20., 50.), 'tropatl': (-115., 10., 0., 40.), 'subtrop': (-90., -20., 20., 60.), 'troppac': (-165., -80., -25., 45.), 'gulf': (-105., -70., 10., 40.), 'carib': (-100., -50., 0., 40.), 'sthepac': (-170., -70., -60., 0.), 'opcahsf': (-102., -20., 0., 45.), 'opcphsf': (175., -70., -28., 45.), 'wwe': (-106., -50., 18., 54.), 'world': (-24., -24., -90., 90.), 'nwwrd1': (-180., 180., -90., 90.), 'nwwrd2': (0., 0., -90., 90.), 'afna': (-135.02, -23.04, 10.43, 40.31), 'awna': (-141.03, -18.58, 7.84, 35.62), 'medr': (-178., -25., -15., 5.), 'pacsfc': (129., -95., -5., 18.), 'saudi': (4.6, 92.5, -13.2, 60.3), 'natlmed': (-30., 70., 0., 65.), 'ncna': (-135.5, -19.25, 8., 37.7), 'ncna2': (-133.5, -20.5, 10., 42.), 'hpcsfc': (-124., -26., 15., 53.), 'atlhur': (-96., -6., 4., 3.), 'nam': (-134., 3., -4., 39.), 'sam': (-120., -20., -60., 20.), 'samps': (-148., -36., -28., 12.), 'eur': (-16., 80., 24., 52.), 'afnh': (-155.19, 18.76, -6.8, -3.58), 'awnh': (-158.94, 15.35, -11.55, -8.98), 'wwwus': (-127.7, -59., 19.8, 56.6), 'ccfp': (-130., -65., 22., 52.), 'llvl': (-119.6, -59.5, 19.9, 44.5), 'llvl2': (-125., -32.5, 5., 46.), 'llvl_e': (-89., -59.5, 23.5, 44.5), 'llvl_c': (-102.4, -81.25, 23.8, 51.6), 'llvl_w': (-119.8, -106.5, 19.75, 52.8), 'ak_artc': (163.7, -65.3, 17.5, 52.6), 'fxpswna': (-80.5, 135., -1., 79.), 'fxpsnna': (-80.5, 54., -1., 25.5), 'fxpsna': (-72.6, 31.4, -3.6, 31.), 'natl_ps': (-80.5, 54., -1., 25.5), 'fxpsena': (-45., 54., 11., 25.5), 'fxpsnp': (155.5, -106.5, 22.5, 47.), 'npac_ps': (155.5, -106.5, 22.5, 47.), 'fxpsus': (-120., -59., 20., 44.5), 'fxmrwrd': (58., 58., -70., 70.), 'fxmrwr2': (-131., -131., -70., 70.), 'nwmrwrd': (70., 70., -70., 70.), 'wrld_mr': (58., 58., -70., 70.), 'fxmr110': (-180., -110., -20., 50.5), 'fxmr180': (110., -180., -20., 50.5), 'fxmrswp': (97.5, -147.5, -36., 45.5), 'fxmrus': (-162.5, -37.5, -28., 51.2), 'fxmrea': (-40., 20., -20., 54.2), 'fxmrjp': (100., -160., 0., 45.), 'icao_a': (-137.4, -12.6, -54., 67.), 'icao_b': (-52.5, -16., -62.5, 77.5), 'icao_b1': (-125., 40., -45.5, 62.7), 'icao_c': (-35., 70., -45., 75.), 'icao_d': (-15., 132., -27., 63.), 'icao_e': (25., 180., -54., 40.), 'icao_f': (100., -110., -52.7, 50.), 'icao_g': (34.8, 157.2, -0.8, 13.7), 'icao_h': (-79.1, 56.7, 1.6, 25.2), 'icao_i': (166.24, -60.62, -6.74, 33.32), 'icao_j': (106.8, -101.1, -27.6, 0.8), 'icao_k': (3.3, 129.1, -11.1, 6.7), 'icao_m': (100., -110., -10., 70.), 'icao_eu': (-21.6, 68.4, 21.4, 58.7), 'icao_me': (17., 70., 10., 44.), 'icao_as': (53., 108., 00., 36.), 'icao_na': (-54.1, 60.3, 17.2, 50.7), 'nhem': (-135., 45., -15., -15.), 'nhem_ps': (-135., 45., -15., -15.), 'nhem180': (135., -45., -15., -15.), 'nhem155': (160., -20., -15., -15.), 'nhem165': (150., -30., -15., -15.), 'nh45_ps': (-90., 90., -15., -15.), 'nhem0': (-45., 135., -15., -15.), 'shem_ps': (88., -92., 30., 30.), 'hfo_gu': (160., -130., -30., 40.), 'natl': (-110., 20.1, 15., 70.), 'watl': (-84., -38., 25., 46.), 'tatl': (-90., -15., -10., 35.), 'npac': (102., -110., -12., 60.), 'spac': (102., -70., -60., 20.), 'tpac': (-165., -75., -10., 40.), 'epac': (-134., -110., 12., 75.), 'wpac': (130., -120., 0., 63.), 'mpac': (128., -108., 15., 71.95), 'opcsfp': (128.89, -105.3, 3.37, 16.77), 'opcsfa': (-55.5, 75., -8.5, 52.6), 'opchur': (-99., -15., 1., 50.05), 'us': (-119., -56., 19., 47.), 'spcus': (-116.4, -63.9, 22.1, 47.2), 'afus': (-119.04, -63.44, 23.1, 44.63), 'ncus': (-124.2, -40.98, 17.89, 47.39), 'nwus': (-118., -55.5, 17., 46.5), 'awips': (-127., -59., 20., 50.), 'bwus': (-124.6, -46.7, 13.1, 43.1), 'usa': (-118., -62., 22.8, 45.), 'usnps': (-118., -62., 18., 51.), 'uslcc': (-118., -62., 20., 51.), 'uswn': (-129., -45., 17., 53.), 'ussf': (-123.5, -44.5, 13., 32.1), 'ussp': (-126., -49., 13., 54.), 'whlf': (-123.8, -85.9, 22.9, 50.2), 'chlf': (-111., -79., 27.5, 50.5), 'centus': (-105.4, -77., 24.7, 47.6), 'ehlf': (-96.2, -62.7, 22., 49.), 'mehlf': (-89.9, -66.6, 23.8, 49.1), 'bosfa': (-87.5, -63.5, 34.5, 50.5), 'miafa': (-88., -72., 23., 39.), 'chifa': (-108., -75., 34., 50.), 'dfwfa': (-106.5, -80.5, 22., 40.), 'slcfa': (-126., -98., 29.5, 50.5), 'sfofa': (-129., -111., 30., 50.), 'g8us': (-116., -58., 19., 56.), 'wsig': (155., -115., 18., 58.), 'esig': (-80., -30., 25., 51.), 'eg8': (-79., -13., 24., 52.), 'west': (-125., -90., 25., 55.), 'cent': (-107.4, -75.3, 24.3, 49.7), 'east': (-100.55, -65.42, 24.57, 47.2), 'nwse': (-126., -102., 38.25, 50.25), 'swse': (-126., -100., 28.25, 40.25), 'ncse': (-108., -84., 38.25, 50.25), 'scse': (-108.9, -84., 24., 40.25), 'nese': (-89., -64., 37.25, 47.25), 'sese': (-90., -66., 28.25, 40.25), 'afwh': (170.7, 15.4, -48.6, 69.4), 'afeh': (-9.3, -164.6, -48.6, 69.4), 'afpc': (80.7, -74.6, -48.6, 69.4), 'ak': (-179., -116.4, 49., 69.), 'ak2': (-180., -106., 42., 73.), 'nwak': (-180., -110., 50., 60.), 'al': (-95., -79., 27., 38.), 'ar': (-100.75, -84.75, 29.5, 40.5), 'ca': (-127.75, -111.75, 31.5, 42.5), 'co': (-114., -98., 33.5, 44.5), 'ct': (-81.25, -65.25, 36., 47.), 'dc': (-85., -69., 33.35, 44.35), 'de': (-83.75, -67.75, 33.25, 44.25), 'fl': (-90., -74., 23., 34.), 'ga': (-92., -76., 27.5, 38.5), 'hi': (-161.5, -152.5, 17., 23.), 'nwxhi': (-166., -148., 14., 26.), 'ia': (-102., -86., 36.5, 47.5), 'id': (-123., -107., 39.25, 50.25), 'il': (-97.75, -81.75, 34.5, 45.5), 'in': (-94.5, -78.5, 34.5, 45.5), 'ks': (-106.5, -90.5, 33.25, 44.25), 'ky': (-93., -77., 31.75, 42.75), 'la': (-100.75, -84.75, 25.75, 36.75), 'ma': (-80.25, -64.25, 36.75, 47.75), 'md': (-85.25, -69.25, 33.75, 44.75), 'me': (-77.75, -61.75, 39.5, 50.5), 'mi': (-93., -77., 37.75, 48.75), 'mn': (-102., -86., 40.5, 51.5), 'mo': (-101., -85., 33., 44.), 'ms': (-98., -82., 27., 38.), 'mt': (-117., -101., 41.5, 52.5), 'nc': (-87.25, -71.25, 30., 41.), 'nd': (-107.5, -91.5, 42.25, 53.25), 'ne': (-107.5, -91.5, 36.25, 47.25), 'nh': (-79.5, -63.5, 38.25, 49.25), 'nj': (-82.5, -66.5, 34.75, 45.75), 'nm': (-114.25, -98.25, 29., 40.), 'nv': (-125., -109., 34., 45.), 'ny': (-84., -68., 37.25, 48.25), 'oh': (-91., -75., 34.5, 45.5), 'ok': (-105.25, -89.25, 30.25, 41.25), 'or': (-128., -112., 38.75, 49.75), 'pa': (-86., -70., 35.5, 46.5), 'ri': (-79.75, -63.75, 36., 47.), 'sc': (-89., -73., 28.5, 39.5), 'sd': (-107.5, -91.5, 39., 50.), 'tn': (-95., -79., 30., 41.), 'tx': (-107., -91., 25.4, 36.5), 'ut': (-119., -103., 34., 45.), 'va': (-86.5, -70.5, 32.25, 43.25), 'vt': (-80.75, -64.75, 38.25, 49.25), 'wi': (-98., -82., 38.5, 49.5), 'wv': (-89., -73., 33., 44.), 'wy': (-116., -100., 37.75, 48.75), 'az': (-119., -103., 29., 40.), 'wa': (-128., -112., 41.75, 52.75), 'abrfc': (-108., -88., 30., 42.), 'ab10': (-106.53, -90.28, 31.69, 40.01), 'cbrfc': (-117., -103., 28., 46.), 'cb10': (-115.69, -104.41, 29.47, 44.71), 'lmrfc': (-100., -77., 26., 40.), 'lm10': (-97.17, -80.07, 28.09, 38.02), 'marfc': (-83.5, -70., 35.5, 44.), 'ma10': (-81.27, -72.73, 36.68, 43.1), 'mbrfc': (-116., -86., 33., 53.), 'mb10': (-112.8, -89.33, 35.49, 50.72), 'ncrfc': (-108., -76., 34., 53.), 'nc10': (-104.75, -80.05, 35.88, 50.6), 'nerfc': (-84., -61., 39., 49.), 'ne10': (-80.11, -64.02, 40.95, 47.62), 'nwrfc': (-128., -105., 35., 55.), 'nw10': (-125.85, -109.99, 38.41, 54.46), 'ohrfc': (-92., -75., 34., 44.), 'oh10': (-90.05, -77.32, 35.2, 42.9), 'serfc': (-94., -70., 22., 40.), 'se10': (-90.6, -73.94, 24.12, 37.91), 'wgrfc': (-112., -88., 21., 42.), 'wg10': (-108.82, -92.38, 23.99, 39.18), 'nwcn': (-133.5, -10.5, 32., 56.), 'cn': (-120.4, -14., 37.9, 58.6), 'ab': (-119.6, -108.2, 48.6, 60.4), 'bc': (-134.5, -109., 47.2, 60.7), 'mb': (-102.4, -86.1, 48.3, 60.2), 'nb': (-75.7, -57.6, 42.7, 49.6), 'nf': (-68., -47., 45., 62.), 'ns': (-67., -59., 43., 47.5), 'nt': (-131.8, -33.3, 57.3, 67.8), 'on': (-94.5, -68.2, 41.9, 55.), 'pe': (-64.6, -61.7, 45.8, 47.1), 'qb': (-80., -49.2, 44.1, 60.9), 'sa': (-111.2, -97.8, 48.5, 60.3), 'yt': (-142., -117., 59., 70.5), 'ag': (-80., -53., -56., -20.), 'ah': (60., 77., 27., 40.), 'afrca': (-25., 59.4, -36., 41.), 'ai': (-14.3, -14.1, -8., -7.8), 'alba': (18., 23., 39., 43.), 'alge': (-9., 12., 15., 38.), 'an': (10., 25., -20., -5.), 'antl': (-70., -58., 11., 19.), 'antg': (-86., -65., 17., 25.), 'atg': (-62., -61.6, 16.9, 17.75), 'au': (101., 148., -45., -6.5), 'azor': (-27.6, -23., 36., 41.), 'ba': (-80.5, -72.5, 22.5, 28.5), 'be': (-64.9, -64.5, 32.2, 32.6), 'bel': (2.5, 6.5, 49.4, 51.6), 'bf': (113., 116., 4., 5.5), 'bfa': (-6., 3., 9., 15.1), 'bh': (-89.3, -88.1, 15.7, 18.5), 'bi': (29., 30.9, -4.6, -2.2), 'bj': (0., 5., 6., 12.6), 'bn': (50., 51., 25.5, 27.1), 'bo': (-72., -50., -24., -8.), 'bots': (19., 29.6, -27., -17.), 'br': (-62.5, -56.5, 12.45, 13.85), 'bt': (71.25, 72.6, -7.5, -5.), 'bu': (22., 30., 40., 45.), 'bv': (3., 4., -55., -54.), 'bw': (87., 93., 20.8, 27.), 'by': (19., 33., 51., 60.), 'bz': (-75., -30., -35., 5.), 'cais': (-172., -171., -3., -2.), 'nwcar': (-120., -50., -15., 35.), 'cari': (-103., -53., 3., 36.), 'cb': (13., 25., 7., 24.), 'ce': (14., 29., 2., 11.5), 'cg': (10., 20., -6., 5.), 'ch': (-80., -66., -56., -15.), 'ci': (85., 145., 14., 48.5), 'cm': (7.5, 17.1, 1., 14.), 'colm': (-81., -65., -5., 14.), 'cr': (-19., -13., 27., 30.), 'cs': (-86.5, -81.5, 8.2, 11.6), 'cu': (-85., -74., 19., 24.), 'cv': (-26., -22., 14., 18.), 'cy': (32., 35., 34., 36.), 'cz': (8.9, 22.9, 47.4, 52.4), 'dj': (41.5, 44.1, 10.5, 13.1), 'dl': (4.8, 16.8, 47., 55.), 'dn': (8., 11., 54., 58.6), 'do': (-61.6, -61.2, 15.2, 15.8), 'dr': (-72.2, -68., 17.5, 20.2), 'eg': (24., 37., 21., 33.), 'eq': (-85., -74., -7., 3.), 'er': (50., 57., 22., 26.6), 'es': (-90.3, -87.5, 13., 14.6), 'et': (33., 49., 2., 19.), 'fa': (-8., -6., 61., 63.), 'fg': (-55., -49., 1., 7.), 'fi': (20.9, 35.1, 59., 70.6), 'fj': (176., -179., 16., 19.), 'fk': (-61.3, -57.5, -53., -51.), 'fn': (0., 17., 11., 24.), 'fr': (-5., 11., 41., 51.5), 'gb': (-17.1, -13.5, 13., 14.6), 'gc': (-82.8, -77.6, 17.9, 21.1), 'gh': (-4.5, 1.5, 4., 12.), 'gi': (-8., -4., 35., 38.), 'gl': (-56.7, 14., 58.3, 79.7), 'glp': (-64.2, -59.8, 14.8, 19.2), 'gm': (144.5, 145.1, 13., 14.), 'gn': (2., 16., 3.5, 15.5), 'go': (8., 14.5, -4.6, 3.), 'gr': (20., 27.6, 34., 42.), 'gu': (-95.6, -85., 10.5, 21.1), 'gw': (-17.5, -13.5, 10.8, 12.8), 'gy': (-62., -55., 0., 10.), 'ha': (-75., -71., 18., 20.), 'he': (-6.1, -5.5, -16.3, -15.5), 'hk': (113.5, 114.7, 22., 23.), 'ho': (-90., -83., 13., 16.6), 'hu': (16., 23., 45.5, 49.1), 'ic': (43., 45., -13.2, -11.), 'icel': (-24.1, -11.5, 63., 67.5), 'ie': (-11.1, -4.5, 50., 55.6), 'inda': (67., 92., 4.2, 36.), 'indo': (95., 141., -8., 6.), 'iq': (38., 50., 29., 38.), 'ir': (44., 65., 25., 40.), 'is': (34., 37., 29., 34.), 'iv': (-9., -2., 4., 11.), 'iw': (34.8, 35.6, 31.2, 32.6), 'iy': (6.6, 20.6, 35.6, 47.2), 'jd': (34., 39.6, 29., 33.6), 'jm': (-80., -76., 16., 19.), 'jp': (123., 155., 24., 47.), 'ka': (131., 155., 1., 9.6), 'kash': (74., 78., 32., 35.), 'kb': (172., 177., -3., 3.2), 'khm': (102., 108., 10., 15.), 'ki': (105.2, 106.2, -11., -10.), 'kn': (32.5, 42.1, -6., 6.), 'kna': (-62.9, -62.4, 17., 17.5), 'ko': (124., 131.5, 33., 43.5), 'ku': (-168., -155., -24.1, -6.1), 'kw': (46.5, 48.5, 28.5, 30.5), 'laos': (100., 108., 13.5, 23.1), 'lb': (34.5, 37.1, 33., 35.), 'lc': (60.9, 61.3, 13.25, 14.45), 'li': (-12., -7., 4., 9.), 'ln': (-162.1, -154.9, -4.2, 6.), 'ls': (27., 29.6, -30.6, -28.), 'lt': (9.3, 9.9, 47., 47.6), 'lux': (5.6, 6.6, 49.35, 50.25), 'ly': (8., 26., 19., 35.), 'maar': (-63.9, -62.3, 17., 18.6), 'made': (-17.3, -16.5, 32.6, 33.), 'mala': (100., 119.6, 1., 8.), 'mali': (-12.5, 6., 8.5, 25.5), 'maur': (57.2, 57.8, -20.7, -19.9), 'maut': (-17.1, -4.5, 14.5, 28.1), 'mc': (-13., -1., 25., 36.), 'mg': (43., 50.6, -25.6, -12.), 'mh': (160., 172., 4.5, 12.1), 'ml': (14.3, 14.7, 35.8, 36.), 'mmr': (92., 102., 7.5, 28.5), 'mong': (87.5, 123.1, 38.5, 52.6), 'mr': (-61.2, -60.8, 14.3, 15.1), 'mu': (113., 114., 22., 23.), 'mv': (70.1, 76.1, -6., 10.), 'mw': (32.5, 36.1, -17., -9.), 'mx': (-119., -83., 13., 34.), 'my': (142.5, 148.5, 9., 25.), 'mz': (29., 41., -26.5, -9.5), 'nama': (11., 25., -29.5, -16.5), 'ncal': (158., 172., -23., -18.), 'ng': (130., 152., -11., 0.), 'ni': (2., 14.6, 3., 14.), 'nk': (-88., -83., 10.5, 15.1), 'nl': (3.5, 7.5, 50.5, 54.1), 'no': (3., 35., 57., 71.5), 'np': (80., 89., 25., 31.), 'nw': (166.4, 167.4, -1., 0.), 'nz': (165., 179., -48., -33.), 'om': (52., 60., 16., 25.6), 'os': (9., 18., 46., 50.), 'pf': (-154., -134., -28., -8.), 'ph': (116., 127., 4., 21.), 'pi': (-177.5, -167.5, -9., 1.), 'pk': (60., 78., 23., 37.), 'pl': (14., 25., 48.5, 55.), 'pm': (-83., -77., 7., 10.), 'po': (-10., -4., 36.5, 42.5), 'pr': (-82., -68., -20., 5.), 'pt': (-130.6, -129.6, -25.56, -24.56), 'pu': (-67.5, -65.5, 17.5, 18.5), 'py': (-65., -54., -32., -17.), 'qg': (7., 12., -2., 3.), 'qt': (50., 52., 24., 27.), 'ra': (60., -165., 25., 55.), 're': (55., 56., -21.5, -20.5), 'riro': (-18., -12., 17.5, 27.5), 'ro': (19., 31., 42.5, 48.5), 'rw': (29., 31., -3., -1.), 'saud': (34.5, 56.1, 15., 32.6), 'sb': (79., 83., 5., 10.), 'seyc': (55., 56., -5., -4.), 'sg': (-18., -10., 12., 17.), 'si': (39.5, 52.1, -4.5, 13.5), 'sk': (109.5, 119.3, 1., 7.), 'sl': (-13.6, -10.2, 6.9, 10.1), 'sm': (-59., -53., 1., 6.), 'sn': (10., 25., 55., 69.6), 'so': (156., 167., -12., -6.), 'sp': (-10., 6., 35., 44.), 'sr': (103., 105., 1., 2.), 'su': (21.5, 38.5, 3.5, 23.5), 'sv': (30.5, 33.1, -27.5, -25.3), 'sw': (5.9, 10.5, 45.8, 48.), 'sy': (35., 42.6, 32., 37.6), 'tanz': (29., 40.6, -13., 0.), 'td': (-62.1, -60.5, 10., 11.6), 'tg': (-0.5, 2.5, 5., 12.), 'th': (97., 106., 5., 21.), 'ti': (-71.6, -70.6, 21., 22.), 'tk': (-173., -171., -11.5, -7.5), 'to': (-178.5, -170.5, -22., -15.), 'tp': (6., 7.6, 0., 2.), 'ts': (7., 13., 30., 38.), 'tu': (25., 48., 34.1, 42.1), 'tv': (176., 180., -11., -5.), 'tw': (120., 122., 21.9, 25.3), 'ug': (29., 35., -3.5, 5.5), 'uk': (-11., 5., 49., 60.), 'ur': (24., 41., 44., 55.), 'uy': (-60., -52., -35.5, -29.5), 'vanu': (167., 170., -21., -13.), 'vi': (-65.5, -64., 16.6, 19.6), 'vk': (13.8, 25.8, 46.75, 50.75), 'vn': (-75., -60., -2., 14.), 'vs': (102., 110., 8., 24.), 'wk': (166.1, 167.1, 18.8, 19.8), 'ye': (42.5, 54.1, 12.5, 19.1), 'yg': (13.5, 24.6, 40., 47.), 'za': (16., 34., -36., -22.), 'zb': (21., 35., -20., -7.), 'zm': (170.5, 173.5, -15., -13.), 'zr': (12., 31.6, -14., 6.), 'zw': (25., 34., -22.9, -15.5) } def lookup_projection(projection_code): """Get a Cartopy projection based on a short abbreviation.""" import cartopy.crs as ccrs projections = {'lcc': ccrs.LambertConformal(central_latitude=40, central_longitude=-100, standard_parallels=[30, 60]), 'ps': ccrs.NorthPolarStereo(central_longitude=-100), 'mer': ccrs.Mercator()} return projections[projection_code] def lookup_map_feature(feature_name): """Get a Cartopy map feature based on a name.""" import cartopy.feature as cfeature from . import cartopy_utils name = feature_name.upper() try: feat = getattr(cfeature, name) scaler = cfeature.AdaptiveScaler('110m', (('50m', 50), ('10m', 15))) except AttributeError: feat = getattr(cartopy_utils, name) scaler = cfeature.AdaptiveScaler('20m', (('5m', 5), ('500k', 1))) return feat.with_scale(scaler) class Panel(HasTraits): """Draw one or more plots.""" @exporter.export class PanelContainer(HasTraits): """Collects panels and set complete figure related settings (e.g., size).""" size = Union([Tuple(Union([Int(), Float()]), Union([Int(), Float()])), Instance(type(None))], default_value=None) size.__doc__ = """This trait takes a tuple of (width, height) to set the size of the figure. This trait defaults to None and will assume the default `matplotlib.pyplot.figure` size. """ panels = List(Instance(Panel)) panels.__doc__ = """A list of panels to plot on the figure. This trait must contain at least one panel to plot on the figure.""" @property def panel(self): """Provide simple access for a single panel.""" return self.panels[0] @panel.setter def panel(self, val): self.panels = [val] @observe('panels') def _panels_changed(self, change): for panel in change.new: panel.parent = self panel.observe(self.refresh, names=('_need_redraw')) @property def figure(self): """Provide access to the underlying figure object.""" if not hasattr(self, '_fig'): self._fig = plt.figure(figsize=self.size) return self._fig def refresh(self, _): """Refresh the rendering of all panels.""" # First make sure everything is properly constructed self.draw() # Trigger the graphics refresh self.figure.canvas.draw() # Flush out interactive events--only ok on Agg for newer matplotlib with contextlib.suppress(NotImplementedError): self.figure.canvas.flush_events() def draw(self): """Draw the collection of panels.""" for panel in self.panels: with panel.hold_trait_notifications(): panel.draw() def save(self, *args, **kwargs): """Save the constructed graphic as an image file. This method takes a string for saved file name. Additionally, the same arguments and keyword arguments that `matplotlib.pyplot.savefig` does. """ self.draw() self.figure.savefig(*args, **kwargs) def show(self): """Show the constructed graphic on the screen.""" self.draw() plt.show() def copy(self): """Return a copy of the panel container.""" return copy.copy(self) @exporter.export class MapPanel(Panel): """Set figure related elements for an individual panel. Parameters that need to be set include collecting all plotting types (e.g., contours, wind barbs, etc.) that are desired to be in a given panel. Additionally, traits can be set to plot map related features (e.g., coastlines, borders), projection, graphics area, and title. """ parent = Instance(PanelContainer, allow_none=True) layout = Tuple(Int(), Int(), Int(), default_value=(1, 1, 1)) layout.__doc__ = """A tuple that contains the description (nrows, ncols, index) of the panel position; default value is (1, 1, 1). This trait is set to describe the panel position and the default is for a single panel. For example, a four-panel plot will have two rows and two columns with the tuple setting for the upper-left panel as (2, 2, 1), upper-right as (2, 2, 2), lower-left as (2, 2, 3), and lower-right as (2, 2, 4). For more details see the documentation for `matplotlib.figure.Figure.add_subplot`. """ plots = List(Any()) plots.__doc__ = """A list of handles that represent the plots (e.g., `ContourPlot`, `FilledContourPlot`, `ImagePlot`) to put on a given panel. This trait collects the different plots, including contours and images, that are intended for a given panel. """ _need_redraw = Bool(default_value=True) area = Union([Unicode(), Tuple(Float(), Float(), Float(), Float())], allow_none=True, default_value=None) area.__doc__ = """A tuple or string value that indicates the graphical area of the plot. The tuple value corresponds to longitude/latitude box based on the projection of the map with the format (west-most longitude, east-most longitude, south-most latitude, north-most latitude). This tuple defines a box from the lower-left to the upper-right corner. This trait can also be set with a string value associated with the named geographic regions within MetPy. The tuples associated with the names are based on a PlatteCarree projection. For a CONUS region, the following strings can be used: 'us', 'spcus', 'ncus', and 'afus'. For regional plots, US postal state abbreviations can be used, such as 'co', 'ny', 'ca', et cetera. Providing a '+' or '-' suffix to the string value will zoom in or out, respectively. Providing multiple '+' or '-' characters will zoom in or out further. """ projection = Union([Unicode(), Instance('cartopy.crs.Projection')], default_value='data') projection.__doc__ = """A string for a pre-defined projection or a Cartopy projection object. There are three pre-defined projections that can be called with a short name: Lambert conformal conic ('lcc'), Mercator ('mer'), or polar-stereographic ('ps'). Additionally, this trait can be set to a Cartopy projection object. """ layers = List(Union([Unicode(), Instance('cartopy.feature.Feature')]), default_value=['coastline']) layers.__doc__ = """A list of strings for a pre-defined feature layer or a Cartopy Feature object. Like the projection, there are a couple of pre-defined feature layers that can be called using a short name. The pre-defined layers are: 'coastline', 'states', 'borders', 'lakes', 'land', 'ocean', 'rivers', 'usstates', and 'uscounties'. Additionally, this can accept Cartopy Feature objects. """ title = Unicode() title.__doc__ = """A string to set a title for the figure. This trait sets a user-defined title that will plot at the top center of the figure. """ title_fontsize = Union([Int(), Float(), Unicode()], allow_none=True, default_value=None) title_fontsize.__doc__ = """An integer or string value for the font size of the title of the figure. This trait sets the font size for the title that will plot at the top center of the figure. Accepts size in points or relative size. Allowed relative sizes are those of Matplotlib: 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'. """ @validate('area') def _valid_area(self, proposal): """Check that proposed string or tuple is valid and turn string into a tuple extent.""" area = proposal['value'] # Parse string, check that string is valid, and determine extent based on string if isinstance(area, str): match = re.match(r'(\w+)([-+]*)$', area) if match is None: raise TraitError(f'"{area}" is not a valid string area.') region, modifier = match.groups() region = region.lower() if region == 'global': extent = 'global' elif region in _areas: extent = _areas[region] zoom = modifier.count('+') - modifier.count('-') extent = self._zoom_extent(extent, zoom) else: raise TraitError(f'"{area}" is not a valid string area.') # Otherwise, assume area is a tuple and check that latitudes/longitudes are valid else: west_lon, east_lon, south_lat, north_lat = area valid_west = -180 <= west_lon <= 180 valid_east = -180 <= east_lon <= 180 valid_south = -90 <= south_lat <= 90 valid_north = -90 <= north_lat <= 90 if not (valid_west and valid_east and valid_south and valid_north): raise TraitError(f'"{area}" is not a valid string area.') extent = area return extent @observe('plots') def _plots_changed(self, change): """Handle when our collection of plots changes.""" for plot in change.new: plot.parent = self plot.observe(self.refresh, names=('_need_redraw')) self._need_redraw = True @observe('parent') def _parent_changed(self, _): """Handle when the parent is changed.""" self.ax = None @property def _proj_obj(self): """Return the projection as a Cartopy object. Handles looking up a string for the projection, or if the projection is set to ``'data'`` looks at the data for the projection. """ if isinstance(self.projection, str): if self.projection == 'data': if isinstance(self.plots[0].griddata, tuple): return self.plots[0].griddata[0].metpy.cartopy_crs else: return self.plots[0].griddata.metpy.cartopy_crs else: return lookup_projection(self.projection) else: return self.projection @property def _layer_features(self): """Iterate over all map features and return as Cartopy objects. Handle converting names of maps to auto-scaling map features. """ for item in self.layers: if isinstance(item, str): feat = lookup_map_feature(item) else: feat = item yield feat @observe('area') def _set_need_redraw(self, _): """Watch traits and set the need redraw flag as necessary.""" self._need_redraw = True @staticmethod def _zoom_extent(extent, zoom): """Calculate new bounds for zooming in or out of a given extent. ``extent`` is given as a tuple with four numeric values, in the same format as the ``area`` trait. If ``zoom`` = 0, the extent will not be changed from what was provided to the method If ``zoom`` > 0, the returned extent will be smaller (zoomed in) If ``zoom`` < 0, the returned extent will be larger (zoomed out) """ west_lon, east_lon, south_lat, north_lat = extent # Turn number of pluses and minuses into a number than can scale the latitudes and # longitudes of our extent zoom_multiplier = (1 - 2**-zoom) / 2 # Calculate bounds for new, zoomed extent new_north_lat = north_lat + (south_lat - north_lat) * zoom_multiplier new_south_lat = south_lat - (south_lat - north_lat) * zoom_multiplier new_east_lon = east_lon + (west_lon - east_lon) * zoom_multiplier new_west_lon = west_lon - (west_lon - east_lon) * zoom_multiplier return (new_west_lon, new_east_lon, new_south_lat, new_north_lat) @property def ax(self): """Get the :class:`matplotlib.axes.Axes` to draw on. Creates a new instance if necessary. """ # If we haven't actually made an instance yet, make one with the right size and # map projection. if getattr(self, '_ax', None) is None: self._ax = self.parent.figure.add_subplot(*self.layout, projection=self._proj_obj) return self._ax @ax.setter def ax(self, val): """Set the :class:`matplotlib.axes.Axes` to draw on. Clears existing state as necessary. """ if getattr(self, '_ax', None) is not None: self._ax.cla() self._ax = val def refresh(self, changed): """Refresh the drawing if necessary.""" self._need_redraw = changed.new def draw(self): """Draw the panel.""" # Only need to run if we've actually changed. if self._need_redraw: # Set the extent as appropriate based on the area. One special case for 'global'. if self.area == 'global': self.ax.set_global() elif self.area is not None: self.ax.set_extent(self.area, ccrs.PlateCarree()) # Draw all of the plots. for p in self.plots: with p.hold_trait_notifications(): p.draw() # Add all of the maps for feat in self._layer_features: self.ax.add_feature(feat) # Use the set title or generate one. title = self.title or ',\n'.join(plot.name for plot in self.plots) self.ax.set_title(title, fontsize=self.title_fontsize) self._need_redraw = False def __copy__(self): """Return a copy of this MapPanel.""" # Create new, blank instance of MapPanel cls = self.__class__ obj = cls.__new__(cls) # Copy each attribute from current MapPanel to new MapPanel for name in self.trait_names(): # The 'plots' attribute is a list. # A copy must be made for each plot in the list. if name == 'plots': obj.plots = [copy.copy(plot) for plot in self.plots] else: setattr(obj, name, getattr(self, name)) return obj def copy(self): """Return a copy of the panel.""" return copy.copy(self) @exporter.export class Plots2D(HasTraits): """The highest level class related to plotting 2D data. This class collects all common methods no matter whether plotting a scalar variable or vector. Primary settings common to all types of 2D plots are time and level. """ parent = Instance(Panel) _need_redraw = Bool(default_value=True) level = Union([Int(allow_none=True, default_value=None), Instance(units.Quantity)]) level.__doc__ = """The level of the field to be plotted. This is a value with units to choose the desired plot level. For example, selecting the 850-hPa level, set this parameter to ``850 * units.hPa`` """ time = Instance(datetime, allow_none=True) time.__doc__ = """Set the valid time to be plotted as a datetime object. If a forecast hour is to be plotted the time should be set to the valid future time, which can be done using the `~datetime.datetime` and `~datetime.timedelta` objects from the Python standard library. """ plot_units = Unicode(allow_none=True, default_value=None) plot_units.__doc__ = """The desired units to plot the field in. Setting this attribute will convert the units of the field variable to the given units for plotting using the MetPy Units module. """ scale = Float(default_value=1e0) scale.__doc__ = """Scale the field to be plotted by the value given. This attribute will scale the field by multiplying by the scale. For example, to scale vorticity to be whole values for contouring you could set the scale to 1e5, such that the data values will be scaled by 10^5. """ @property def _cmap_obj(self): """Return the colormap object. Handle convert the name of the colormap to an object from matplotlib or metpy. """ try: return ctables.registry.get_colortable(self.colormap) except KeyError: return plt.get_cmap(self.colormap) @property def _norm_obj(self): """Return the normalization object. Converts the tuple image range to a matplotlib normalization instance. """ return plt.Normalize(*self.image_range) def clear(self): """Clear the plot. Resets all internal state and sets need for redraw. """ if getattr(self, 'handle', None) is not None: if getattr(self.handle, 'collections', None) is not None: self.clear_collections() else: self.clear_handle() self._need_redraw = True def clear_handle(self): """Clear the handle to the plot instance.""" self.handle.remove() self.handle = None def clear_collections(self): """Clear the handle collections to the plot instance.""" for col in self.handle.collections: col.remove() self.handle = None @observe('parent') def _parent_changed(self, _): """Handle setting the parent object for the plot.""" self.clear() @observe('level', 'time') def _update_data(self, _=None): """Handle updating the internal cache of data. Responds to changes in various subsetting parameters. """ self._griddata = None self.clear() # Can't be a Traitlet because notifications don't work with arrays for traits # notification never happens @property def data(self): """Xarray dataset that contains the field to be plotted.""" return self._data @data.setter def data(self, val): self._data = val self._update_data() @property def name(self): """Generate a name for the plot.""" if isinstance(self.field, tuple): ret = '' ret += ' and '.join(f for f in self.field) else: ret = self.field if self.level is not None: ret += f'@{self.level:d}' return ret def copy(self): """Return a copy of the plot.""" return copy.copy(self) @exporter.export class PlotScalar(Plots2D): """Defines the common elements of 2D scalar plots for single scalar value fields. Most of the other traits here are for one or more of the specific plots. Currently this allows too many options for `ContourPlot` since it does not user image_range, for example. Similar issues for `ImagePlot` and `FilledContourPlot`. """ field = Unicode() field.__doc__ = """Name of the field to be plotted. This is the name of the variable from the dataset that is to be plotted. An example, from a model grid file that uses the THREDDS convention for naming would be `Geopotential_height_isobaric` or `Temperature_isobaric`. For GOES-16/17 satellite data it might be `Sectorized_CMI`. To check for the variables available within a dataset, list the variables with the following command assuming the dataset was read using xarray as `ds`, `list(ds)` """ @observe('field') def _update_data(self, _=None): """Handle updating the internal cache of data. Responds to changes in various subsetting parameters. """ self._griddata = None self.clear() @property def griddata(self): """Return the internal cached data.""" if getattr(self, '_griddata', None) is None: if self.field: data = self.data.metpy.parse_cf(self.field) elif not hasattr(self.data.metpy, 'x'): # Handles the case where we have a dataset but no specified field raise ValueError('field attribute has not been set.') else: data = self.data subset = {'method': 'nearest'} if self.level is not None: subset[data.metpy.vertical.name] = self.level if self.time is not None: subset[data.metpy.time.name] = self.time data_subset = data.metpy.sel(**subset).squeeze() if self.plot_units is not None: data_subset = data_subset.metpy.convert_units(self.plot_units) self._griddata = data_subset * self.scale return self._griddata @property def plotdata(self): """Return the data for plotting. The data array, x coordinates, and y coordinates. """ x = self.griddata.metpy.x y = self.griddata.metpy.y return x, y, self.griddata def draw(self): """Draw the plot.""" if self._need_redraw: if getattr(self, 'handle', None) is None: self._build() if getattr(self, 'colorbar', None) is not None: cbar = self.parent.ax.figure.colorbar( self.handle, orientation=self.colorbar, pad=0, aspect=50) cbar.ax.tick_params(labelsize=self.colorbar_fontsize) self._need_redraw = False class ContourTraits(HasTraits): """Represents common contour traits.""" contours = Union([List(Float()), Int()], default_value=25) contours.__doc__ = """A list of values to contour or an integer number of contour levels. This parameter sets contour or colorfill values for a plot. Values can be entered either as a list of values or as an integer with the number of contours to be plotted (as per matplotlib documentation). A list can be generated by using square brackets or creating a numpy 1D array and converting it to a list with the `~numpy.ndarray.tolist` method. """ clabels = Bool(default_value=False) clabels.__doc__ = """A boolean (True/False) on whether to plot contour labels. To plot contour labels set this trait to ``True``, the default value is ``False``. """ label_fontsize = Union([Int(), Float(), Unicode()], allow_none=True, default_value=None) label_fontsize.__doc__ = """An integer, float, or string value to set the font size of labels for contours. This trait sets the font size for labels that will plot along contour lines. Accepts size in points or relative size. Allowed relative sizes are those of Matplotlib: 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'. """ class ColorfillTraits(HasTraits): """Represent common colorfill traits.""" colormap = Unicode(allow_none=True, default_value=None) colormap.__doc__ = """The string name for a Matplolib or MetPy colormap. For example, the Blue-Purple colormap from Matplotlib can be accessed using 'BuPu'. """ image_range = Union([Tuple(Int(allow_none=True), Int(allow_none=True)), Instance(plt.Normalize)], default_value=(None, None)) image_range.__doc__ = """A tuple of min and max values that represent the range of values to color the rasterized image. The min and max values entered as a tuple will be converted to a `matplotlib.colors.Normalize` instance for plotting. """ colorbar = Unicode(default_value=None, allow_none=True) colorbar.__doc__ = """A string (horizontal/vertical) on whether to add a colorbar to the plot. To add a colorbar associated with the plot, set the trait to ``horizontal`` or ``vertical``,specifying the orientation of the produced colorbar. The default value is ``None``. """ colorbar_fontsize = Union([Int(), Float(), Unicode()], allow_none=True, default_value=None) colorbar_fontsize.__doc__ = """An integer, float, or string value to set the font size of labels for the colorbar. This trait sets the font size of labels for the colorbar. Accepts size in points or relative size. Allowed relative sizes are those of Matplotlib: 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'. """ @exporter.export class ImagePlot(PlotScalar, ColorfillTraits): """Make raster image using `~matplotlib.pyplot.imshow` for satellite or colored image.""" @observe('colormap', 'image_range') def _set_need_redraw(self, _): """Handle changes to attributes that just need a simple redraw.""" if hasattr(self, 'handle'): self.handle.set_cmap(self._cmap_obj) self.handle.set_norm(self._norm_obj) self._need_redraw = True @observe('colorbar') def _set_need_rebuild(self, _): """Handle changes to attributes that need to regenerate everything.""" # Because matplotlib doesn't let you just change these properties, we need # to trigger a clear and re-call of contour() self.clear() @property def plotdata(self): """Return the data for plotting. The data array, x coordinates, and y coordinates. """ x = self.griddata.metpy.x y = self.griddata.metpy.y # At least currently imshow with cartopy does not like this if 'degree' in x.units: x = x.data x[x > 180] -= 360 return x, y, self.griddata def _build(self): """Build the plot by calling any plotting methods as necessary.""" x, y, imdata = self.plotdata # We use min/max for y and manually figure out origin to try to avoid upside down # images created by images where y[0] > y[-1] extents = (x[0], x[-1], y.min(), y.max()) origin = 'upper' if y[0] > y[-1] else 'lower' self.handle = self.parent.ax.imshow(imdata, extent=extents, origin=origin, cmap=self._cmap_obj, norm=self._norm_obj, transform=imdata.metpy.cartopy_crs) @exporter.export class ContourPlot(PlotScalar, ContourTraits): """Make contour plots by defining specific traits.""" linecolor = Unicode('black') linecolor.__doc__ = """A string value to set the color of plotted contours; default is black. This trait can be set to any Matplotlib color (https://matplotlib.org/3.1.0/gallery/color/named_colors.html) """ linewidth = Int(2) linewidth.__doc__ = """An integer value to set the width of plotted contours; default value is 2. This trait changes the thickness of contour lines with a higher value plotting a thicker line. """ linestyle = Unicode('solid', allow_none=True) linestyle.__doc__ = """A string value to set the linestyle (e.g., dashed); default is solid. The valid string values are those of Matplotlib which are solid, dashed, dotted, and dashdot. """ @observe('contours', 'linecolor', 'linewidth', 'linestyle', 'clabels', 'label_fontsize') def _set_need_rebuild(self, _): """Handle changes to attributes that need to regenerate everything.""" # Because matplotlib doesn't let you just change these properties, we need # to trigger a clear and re-call of contour() self.clear() def _build(self): """Build the plot by calling any plotting methods as necessary.""" x, y, imdata = self.plotdata self.handle = self.parent.ax.contour(x, y, imdata, self.contours, colors=self.linecolor, linewidths=self.linewidth, linestyles=self.linestyle, transform=imdata.metpy.cartopy_crs) if self.clabels: self.handle.clabel(inline=1, fmt='%.0f', inline_spacing=8, use_clabeltext=True, fontsize=self.label_fontsize) @exporter.export class FilledContourPlot(PlotScalar, ColorfillTraits, ContourTraits): """Make color-filled contours plots by defining appropriate traits.""" @observe('contours', 'colorbar', 'colormap') def _set_need_rebuild(self, _): """Handle changes to attributes that need to regenerate everything.""" # Because matplotlib doesn't let you just change these properties, we need # to trigger a clear and re-call of contour() self.clear() def _build(self): """Build the plot by calling any plotting methods as necessary.""" x, y, imdata = self.plotdata self.handle = self.parent.ax.contourf(x, y, imdata, self.contours, cmap=self._cmap_obj, norm=self._norm_obj, transform=imdata.metpy.cartopy_crs) @exporter.export class PlotVector(Plots2D): """Defines common elements for 2D vector plots. This class collects common elements including the field trait, which is a tuple argument accepting two strings, for plotting 2D vector fields. """ field = Tuple(Unicode(), Unicode()) field.__doc__ = """A tuple containing the two components of the vector field from the dataset in the form (east-west component, north-south component). For a wind barb plot each component of the wind must be specified and should be of the form (u-wind, v-wind). """ pivot = Unicode('middle') pivot.__doc__ = """A string setting the pivot point of the vector. Default value is 'middle'. This trait takes the values of the keyword argument from `matplotlin.pyplot.barbs`: 'tip' or 'middle'. """ skip = Tuple(Int(), Int(), default_value=(1, 1)) skip.__doc__ = """A tuple of integers to indicate the number of grid points to skip between plotting vectors. Default is (1, 1). This trait is to be used to reduce the number of vectors plotted in the (east-west, north-south) components. The two values can be set to the same or different integer values depending on what is desired. """ earth_relative = Bool(default_value=True) earth_relative.__doc__ = """A boolean value to indicate whether the vector to be plotted is earth- or grid-relative. Default value is `True`, indicating that vectors are earth-relative. Common gridded meteorological datasets including GFS and NARR output contain wind components that are earth-relative. The primary exception is NAM output with wind components that are grid-relative. For any grid-relative vectors set this trait to `False`. """ color = Unicode(default_value='black') color.__doc__ = """A string value that controls the color of the vectors. Default value is black. This trait can be set to any named color from `Matplotlibs Colors <https://matplotlib.org/3.1.0/gallery/color/named_colors.html>` """ @observe('field') def _update_data(self, _=None): """Handle updating the internal cache of data. Responds to changes in various subsetting parameters. """ self._griddata_u = None self._griddata_v = None self.clear() @property def griddata(self): """Return the internal cached data.""" if getattr(self, '_griddata_u', None) is None: if self.field[0]: u = self.data.metpy.parse_cf(self.field[0]) v = self.data.metpy.parse_cf(self.field[1]) else: raise ValueError('field attribute not set correctly') subset = {'method': 'nearest'} if self.level is not None: subset[u.metpy.vertical.name] = self.level if self.time is not None: subset[u.metpy.time.name] = self.time data_subset_u = u.metpy.sel(**subset).squeeze() data_subset_v = v.metpy.sel(**subset).squeeze() if self.plot_units is not None: data_subset_u = data_subset_u.metpy.convert_units(self.plot_units) data_subset_v = data_subset_v.metpy.convert_units(self.plot_units) self._griddata_u = data_subset_u self._griddata_v = data_subset_v return (self._griddata_u, self._griddata_v) @property def plotdata(self): """Return the data for plotting. The data array, x coordinates, and y coordinates. """ x = self.griddata[0].metpy.x y = self.griddata[0].metpy.y if self.earth_relative: x, y, _ = ccrs.PlateCarree().transform_points(self.griddata[0].metpy.cartopy_crs, *np.meshgrid(x, y)).T x = x.T y = y.T else: if 'degree' in x.units: x, y, _ = self.griddata[0].metpy.cartopy_crs.transform_points( ccrs.PlateCarree(), *np.meshgrid(x, y)).T x = x.T y = y.T if x.ndim == 1: xx, yy = np.meshgrid(x, y) else: xx, yy = x, y return xx, yy, self.griddata[0], self.griddata[1] def draw(self): """Draw the plot.""" if self._need_redraw: if getattr(self, 'handle', None) is None: self._build() self._need_redraw = False @exporter.export class BarbPlot(PlotVector): """Make plots of wind barbs on a map with traits to refine the look of plotted elements.""" barblength = Float(default_value=7) barblength.__doc__ = """A float value that changes the length of the wind barbs. Default value is 7. This trait corresponds to the keyword length in `matplotlib.pyplot.barbs`. """ @observe('barblength', 'pivot', 'skip', 'earth_relative', 'color') def _set_need_rebuild(self, _): """Handle changes to attributes that need to regenerate everything.""" # Because matplotlib doesn't let you just change these properties, we need # to trigger a clear and re-call of contour() self.clear() def _build(self): """Build the plot by calling needed plotting methods as necessary.""" x, y, u, v = self.plotdata if self.earth_relative: transform = ccrs.PlateCarree() else: transform = u.metpy.cartopy_crs wind_slice = (slice(None, None, self.skip[0]), slice(None, None, self.skip[1])) self.handle = self.parent.ax.barbs( x[wind_slice], y[wind_slice], u.values[wind_slice], v.values[wind_slice], color=self.color, pivot=self.pivot, length=self.barblength, transform=transform) @exporter.export class PlotObs(HasTraits): """The highest level class related to plotting observed surface and upperair data. This class collects all common methods no matter whether plotting a upper-level or surface data using station plots. List of Traits: * level * time * fields * locations (optional) * time_window (optional) * formats (optional) * colors (optional) * plot_units (optional) * vector_field (optional) * vector_field_color (optional) * vector_field_length (optional) * vector_plot_units (optional) * reduce_points (optional) * fontsize (optional) """ parent = Instance(Panel) _need_redraw = Bool(default_value=True) level = Union([Int(allow_none=True), Instance(units.Quantity)], default_value=None) level.__doc__ = """The level of the field to be plotted. This is a value with units to choose the desired plot level. For example, selecting the 850-hPa level, set this parameter to ``850 * units.hPa``. For surface data, parameter must be set to `None`. """ time = Instance(datetime, allow_none=True) time.__doc__ = """Set the valid time to be plotted as a datetime object. If a forecast hour is to be plotted the time should be set to the valid future time, which can be done using the `~datetime.datetime` and `~datetime.timedelta` objects from the Python standard library. """ time_window = Instance(timedelta, default_value=timedelta(minutes=0), allow_none=True) time_window.__doc__ = """Set a range to look for data to plot as a timedelta object. If this parameter is set, it will subset the data provided to be within the time and plus or minus the range value given. If there is more than one observation from a given station then it will keep only the most recent one for plotting purposes. Default value is to have no range. (optional) """ fields = List(Unicode()) fields.__doc__ = """Name of the scalar or symbol fields to be plotted. List of parameters to be plotted around station plot (e.g., temperature, dewpoint, skyc). """ locations = List(default_value=['C']) locations.__doc__ = """List of strings for scalar or symbol field plotting locations. List of parameters locations for plotting parameters around the station plot (e.g., NW, NE, SW, SE, W, C). (optional) """ formats = List(default_value=[None]) formats.__doc__ = """List of the scalar, symbol, and text field data formats. (optional) List of scalar parameters formmaters or mapping values (if symbol) for plotting text and/or symbols around the station plot (e.g., for pressure variable ```lambda v: format(10 * v, '.0f')[-3:]```). For symbol mapping the following options are available to be put in as a string: current_weather, sky_cover, low_clouds, mid_clouds, high_clouds, and pressure_tendency. For plotting text, use the format setting of 'text'. """ colors = List(Unicode(), default_value=['black']) colors.__doc__ = """List of the scalar and symbol field colors. List of strings that represent the colors to be used for the variable being plotted. (optional) """ vector_field = List(default_value=[None], allow_none=True) vector_field.__doc__ = """List of the vector field to be plotted. List of vector components to combined and plotted from the center of the station plot (e.g., wind components). (optional) """ vector_field_color = Unicode('black', allow_none=True) vector_field_color.__doc__ = """String color name to plot the vector. (optional)""" vector_field_length = Int(default_value=None, allow_none=True) vector_field_length.__doc__ = """Integer value to set the length of the plotted vector. (optional) """ reduce_points = Float(default_value=0) reduce_points.__doc__ = """Float to reduce number of points plotted. (optional)""" plot_units = List(default_value=[None], allow_none=True) plot_units.__doc__ = """A list of the desired units to plot the fields in. Setting this attribute will convert the units of the field variable to the given units for plotting using the MetPy Units module, provided that units are attached to the DataFrame. """ vector_plot_units = Unicode(default_value=None, allow_none=True) vector_plot_units.__doc__ = """The desired units to plot the vector field in. Setting this attribute will convert the units of the field variable to the given units for plotting using the MetPy Units module, provided that units are attached to the DataFrame. """ fontsize = Int(10) fontsize.__doc__ = """An integer value to set the font size of station plots. Default is 10 pt.""" def clear(self): """Clear the plot. Resets all internal state and sets need for redraw. """ if getattr(self, 'handle', None) is not None: self.handle.ax.cla() self.handle = None self._need_redraw = True @observe('parent') def _parent_changed(self, _): """Handle setting the parent object for the plot.""" self.clear() @observe('fields', 'level', 'time', 'vector_field', 'time_window') def _update_data(self, _=None): """Handle updating the internal cache of data. Responds to changes in various subsetting parameters. """ self._obsdata = None self.clear() # Can't be a Traitlet because notifications don't work with arrays for traits # notification never happens @property def data(self): """Pandas dataframe that contains the fields to be plotted.""" return self._data @data.setter def data(self, val): self._data = val self._update_data() @property def name(self): """Generate a name for the plot.""" ret = '' ret += ' and '.join(f for f in self.fields) if self.level is not None: ret += f'@{self.level:d}' return ret @property def obsdata(self): """Return the internal cached data.""" if getattr(self, '_obsdata', None) is None: # Use a copy of data so we retain all of the original data passed in unmodified data = self.data # Subset for a particular level if given if self.level is not None: mag = getattr(self.level, 'magnitude', self.level) data = data[data.pressure == mag] # Subset for our particular time if self.time is not None: # If data are not currently indexed by time, we need to do so choosing one of # the columns we're looking for if not isinstance(data.index, pd.DatetimeIndex): time_vars = ['valid', 'time', 'valid_time', 'date_time', 'date'] dim_times = [time_var for time_var in time_vars if time_var in list(self.data)] if not dim_times: raise AttributeError( 'Time variable not found. Valid variable names are:' f'{time_vars}') data = data.set_index(dim_times[0]) if not isinstance(data.index, pd.DatetimeIndex): # Convert our column of interest to a datetime data = data.reset_index() time_index = pd.to_datetime(data[dim_times[0]]) data = data.set_index(time_index) # Works around the fact that traitlets 4.3 insists on sending us None by # default because timedelta(0) is Falsey. window = timedelta(minutes=0) if self.time_window is None else self.time_window # Indexes need to be properly sorted for the slicing below to work; the # error you get if that's not the case really convoluted, which is why # we don't rely on users doing it. data = data.sort_index() data = data[self.time - window:self.time + window] # Look for the station column stn_vars = ['station', 'stn', 'station_id', 'stid'] dim_stns = [stn_var for stn_var in stn_vars if stn_var in list(self.data)] if not dim_stns: raise AttributeError('Station variable not found. Valid variable names are: ' f'{stn_vars}') else: dim_stn = dim_stns[0] # Make sure we only use one observation per station self._obsdata = data.groupby(dim_stn).tail(1) return self._obsdata @property def plotdata(self): """Return the data for plotting. The data arrays, x coordinates, and y coordinates. """ plot_data = {} for dim_name in list(self.obsdata): if dim_name.find('lat') != -1: lat = self.obsdata[dim_name] elif dim_name.find('lon') != -1: lon = self.obsdata[dim_name] else: plot_data[dim_name] = self.obsdata[dim_name] return lon.values, lat.values, plot_data def draw(self): """Draw the plot.""" if self._need_redraw: if getattr(self, 'handle', None) is None: self._build() self._need_redraw = False @observe('colors', 'formats', 'locations', 'reduce_points', 'vector_field_color') def _set_need_rebuild(self, _): """Handle changes to attributes that need to regenerate everything.""" # Because matplotlib doesn't let you just change these properties, we need # to trigger a clear and re-call of contour() self.clear() def _build(self): """Build the plot by calling needed plotting methods as necessary.""" lon, lat, data = self.plotdata # Use the cartopy map projection to transform station locations to the map and # then refine the number of stations plotted by setting a radius if self.parent._proj_obj == ccrs.PlateCarree(): scale = 1. else: scale = 100000. point_locs = self.parent._proj_obj.transform_points(ccrs.PlateCarree(), lon, lat) subset = reduce_point_density(point_locs, self.reduce_points * scale) self.handle = StationPlot(self.parent.ax, lon[subset], lat[subset], clip_on=True, transform=ccrs.PlateCarree(), fontsize=self.fontsize) for i, ob_type in enumerate(self.fields): field_kwargs = {} if len(self.locations) > 1: location = self.locations[i] else: location = self.locations[0] if len(self.colors) > 1: field_kwargs['color'] = self.colors[i] else: field_kwargs['color'] = self.colors[0] if len(self.formats) > 1: field_kwargs['formatter'] = self.formats[i] else: field_kwargs['formatter'] = self.formats[0] if len(self.plot_units) > 1: field_kwargs['plot_units'] = self.plot_units[i] else: field_kwargs['plot_units'] = self.plot_units[0] if hasattr(self.data, 'units') and (field_kwargs['plot_units'] is not None): parameter = units.Quantity(data[ob_type][subset].values, self.data.units[ob_type]) else: parameter = data[ob_type][subset] if field_kwargs['formatter'] is not None: mapper = getattr(wx_symbols, str(field_kwargs['formatter']), None) if mapper is not None: field_kwargs.pop('formatter') self.handle.plot_symbol(location, parameter, mapper, **field_kwargs) else: if self.formats[i] == 'text': self.handle.plot_text(location, parameter, color=field_kwargs['color']) else: self.handle.plot_parameter(location, parameter, **field_kwargs) else: field_kwargs.pop('formatter') self.handle.plot_parameter(location, parameter, **field_kwargs) if self.vector_field[0] is not None: vector_kwargs = {} vector_kwargs['color'] = self.vector_field_color vector_kwargs['plot_units'] = self.vector_plot_units if hasattr(self.data, 'units') and (vector_kwargs['plot_units'] is not None): u = units.Quantity(data[self.vector_field[0]][subset].values, self.data.units[self.vector_field[0]]) v = units.Quantity(data[self.vector_field[1]][subset].values, self.data.units[self.vector_field[1]]) else: vector_kwargs.pop('plot_units') u = data[self.vector_field[0]][subset] v = data[self.vector_field[1]][subset] if self.vector_field_length is not None: vector_kwargs['length'] = self.vector_field_length self.handle.plot_barb(u, v, **vector_kwargs) def copy(self): """Return a copy of the plot.""" return copy.copy(self)
bsd-3-clause
jburos/survivalstan
test/test_byo-gamma_survival_model_sim.py
1
3465
import matplotlib as mpl mpl.use('Agg') import survivalstan from stancache import stancache import numpy as np from functools import partial from nose.tools import ok_ num_iter = 500 from .test_datasets import sim_test_dataset model_code = ''' functions { int count_value(vector a, real val) { int s; s = 0; for (i in 1:num_elements(a)) if (a[i] == val) s = s + 1; return s; } // Defines the log survival real surv_gamma_lpdf (vector t, vector d, real shape, vector rate, int num_cens, int num_obs) { vector[2] log_lik; int idx_obs[num_obs]; int idx_cens[num_cens]; real prob; int i_cens; int i_obs; i_cens = 1; i_obs = 1; for (i in 1:num_elements(t)) { if (d[i] == 1) { idx_obs[i_obs] = i; i_obs = i_obs+1; } else { idx_cens[i_cens] = i; i_cens = i_cens+1; } } print(idx_obs); log_lik[1] = gamma_lpdf(t[idx_obs] | shape, rate[idx_obs]); log_lik[2] = gamma_lccdf(t[idx_cens] | shape, rate[idx_cens]); prob = sum(log_lik); return prob; } } data { int N; // number of observations vector<lower=0>[N] y; // observed times vector<lower=0,upper=1>[N] event; // censoring indicator (1=observed, 0=censored) int M; // number of covariates matrix[N, M] x; // matrix of covariates (with n rows and H columns) } transformed data { int num_cens; int num_obs; num_obs = count_value(event, 1); num_cens = N - num_obs; } parameters { vector[M] beta; // Coefficients in the linear predictor (including intercept) real<lower=0> alpha; // shape parameter } transformed parameters { vector[N] linpred; vector[N] mu; linpred = x*beta; mu = exp(linpred); } model { alpha ~ gamma(0.01,0.01); beta ~ normal(0,5); y ~ surv_gamma(event, alpha, mu, num_cens, num_obs); } ''' make_inits = None def test_null_model(**kwargs): ''' Test weibull survival model on simulated dataset ''' d = sim_test_dataset() testfit = survivalstan.fit_stan_survival_model( model_cohort = 'test model', model_code = model_code, df = d, time_col = 't', event_col = 'event', formula = '~ 1', iter = num_iter, chains = 2, seed = 9001, make_inits = make_inits, FIT_FUN = stancache.cached_stan_fit, drop_intercept = False, **kwargs ) ok_('fit' in testfit) ok_('coefs' in testfit) survivalstan.utils.plot_coefs([testfit]) survivalstan.utils.plot_coefs([testfit], trans=np.exp) return(testfit) def test_model(**kwargs): ''' Test weibull survival model on simulated dataset ''' d = sim_test_dataset() testfit = survivalstan.fit_stan_survival_model( model_cohort = 'test model', model_code = model_code, df = d, time_col = 't', event_col = 'event', formula = '~ age + sex', iter = num_iter, chains = 2, seed = 9001, make_inits = make_inits, FIT_FUN = stancache.cached_stan_fit, drop_intercept = False, **kwargs ) ok_('fit' in testfit) ok_('coefs' in testfit) survivalstan.utils.plot_coefs([testfit]) survivalstan.utils.plot_coefs([testfit], trans=np.exp) return(testfit)
apache-2.0