repo_name
stringlengths
6
112
path
stringlengths
4
204
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
714
810k
license
stringclasses
15 values
eriklindernoren/Keras-GAN
cgan/cgan.py
1
6521
from __future__ import print_function, division from keras.datasets import mnist from keras.layers import Input, Dense, Reshape, Flatten, Dropout, multiply from keras.layers import BatchNormalization, Activation, Embedding, ZeroPadding2D from keras.layers.advanced_activations import LeakyReLU from keras.layers.convolutional import UpSampling2D, Conv2D from keras.models import Sequential, Model from keras.optimizers import Adam import matplotlib.pyplot as plt import numpy as np class CGAN(): def __init__(self): # Input shape self.img_rows = 28 self.img_cols = 28 self.channels = 1 self.img_shape = (self.img_rows, self.img_cols, self.channels) self.num_classes = 10 self.latent_dim = 100 optimizer = Adam(0.0002, 0.5) # Build and compile the discriminator self.discriminator = self.build_discriminator() self.discriminator.compile(loss=['binary_crossentropy'], optimizer=optimizer, metrics=['accuracy']) # Build the generator self.generator = self.build_generator() # The generator takes noise and the target label as input # and generates the corresponding digit of that label noise = Input(shape=(self.latent_dim,)) label = Input(shape=(1,)) img = self.generator([noise, label]) # For the combined model we will only train the generator self.discriminator.trainable = False # The discriminator takes generated image as input and determines validity # and the label of that image valid = self.discriminator([img, label]) # The combined model (stacked generator and discriminator) # Trains generator to fool discriminator self.combined = Model([noise, label], valid) self.combined.compile(loss=['binary_crossentropy'], optimizer=optimizer) def build_generator(self): model = Sequential() model.add(Dense(256, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(1024)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() noise = Input(shape=(self.latent_dim,)) label = Input(shape=(1,), dtype='int32') label_embedding = Flatten()(Embedding(self.num_classes, self.latent_dim)(label)) model_input = multiply([noise, label_embedding]) img = model(model_input) return Model([noise, label], img) def build_discriminator(self): model = Sequential() model.add(Dense(512, input_dim=np.prod(self.img_shape))) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.4)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.4)) model.add(Dense(1, activation='sigmoid')) model.summary() img = Input(shape=self.img_shape) label = Input(shape=(1,), dtype='int32') label_embedding = Flatten()(Embedding(self.num_classes, np.prod(self.img_shape))(label)) flat_img = Flatten()(img) model_input = multiply([flat_img, label_embedding]) validity = model(model_input) return Model([img, label], validity) def train(self, epochs, batch_size=128, sample_interval=50): # Load the dataset (X_train, y_train), (_, _) = mnist.load_data() # Configure input X_train = (X_train.astype(np.float32) - 127.5) / 127.5 X_train = np.expand_dims(X_train, axis=3) y_train = y_train.reshape(-1, 1) # Adversarial ground truths valid = np.ones((batch_size, 1)) fake = np.zeros((batch_size, 1)) for epoch in range(epochs): # --------------------- # Train Discriminator # --------------------- # Select a random half batch of images idx = np.random.randint(0, X_train.shape[0], batch_size) imgs, labels = X_train[idx], y_train[idx] # Sample noise as generator input noise = np.random.normal(0, 1, (batch_size, 100)) # Generate a half batch of new images gen_imgs = self.generator.predict([noise, labels]) # Train the discriminator d_loss_real = self.discriminator.train_on_batch([imgs, labels], valid) d_loss_fake = self.discriminator.train_on_batch([gen_imgs, labels], fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # --------------------- # Train Generator # --------------------- # Condition on labels sampled_labels = np.random.randint(0, 10, batch_size).reshape(-1, 1) # Train the generator g_loss = self.combined.train_on_batch([noise, sampled_labels], valid) # Plot the progress print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss)) # If at save interval => save generated image samples if epoch % sample_interval == 0: self.sample_images(epoch) def sample_images(self, epoch): r, c = 2, 5 noise = np.random.normal(0, 1, (r * c, 100)) sampled_labels = np.arange(0, 10).reshape(-1, 1) gen_imgs = self.generator.predict([noise, sampled_labels]) # Rescale images 0 - 1 gen_imgs = 0.5 * gen_imgs + 0.5 fig, axs = plt.subplots(r, c) cnt = 0 for i in range(r): for j in range(c): axs[i,j].imshow(gen_imgs[cnt,:,:,0], cmap='gray') axs[i,j].set_title("Digit: %d" % sampled_labels[cnt]) axs[i,j].axis('off') cnt += 1 fig.savefig("images/%d.png" % epoch) plt.close() if __name__ == '__main__': cgan = CGAN() cgan.train(epochs=20000, batch_size=32, sample_interval=200)
mit
rexshihaoren/scikit-learn
examples/ensemble/plot_gradient_boosting_regularization.py
355
2843
""" ================================ Gradient Boosting regularization ================================ Illustration of the effect of different regularization strategies for Gradient Boosting. The example is taken from Hastie et al 2009. The loss function used is binomial deviance. Regularization via shrinkage (``learning_rate < 1.0``) improves performance considerably. In combination with shrinkage, stochastic gradient boosting (``subsample < 1.0``) can produce more accurate models by reducing the variance via bagging. Subsampling without shrinkage usually does poorly. Another strategy to reduce the variance is by subsampling the features analogous to the random splits in Random Forests (via the ``max_features`` parameter). .. [1] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical Learning Ed. 2", Springer, 2009. """ print(__doc__) # Author: Peter Prettenhofer <[email protected]> # # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn import ensemble from sklearn import datasets X, y = datasets.make_hastie_10_2(n_samples=12000, random_state=1) X = X.astype(np.float32) # map labels from {-1, 1} to {0, 1} labels, y = np.unique(y, return_inverse=True) X_train, X_test = X[:2000], X[2000:] y_train, y_test = y[:2000], y[2000:] original_params = {'n_estimators': 1000, 'max_leaf_nodes': 4, 'max_depth': None, 'random_state': 2, 'min_samples_split': 5} plt.figure() for label, color, setting in [('No shrinkage', 'orange', {'learning_rate': 1.0, 'subsample': 1.0}), ('learning_rate=0.1', 'turquoise', {'learning_rate': 0.1, 'subsample': 1.0}), ('subsample=0.5', 'blue', {'learning_rate': 1.0, 'subsample': 0.5}), ('learning_rate=0.1, subsample=0.5', 'gray', {'learning_rate': 0.1, 'subsample': 0.5}), ('learning_rate=0.1, max_features=2', 'magenta', {'learning_rate': 0.1, 'max_features': 2})]: params = dict(original_params) params.update(setting) clf = ensemble.GradientBoostingClassifier(**params) clf.fit(X_train, y_train) # compute test set deviance test_deviance = np.zeros((params['n_estimators'],), dtype=np.float64) for i, y_pred in enumerate(clf.staged_decision_function(X_test)): # clf.loss_ assumes that y_test[i] in {0, 1} test_deviance[i] = clf.loss_(y_test, y_pred) plt.plot((np.arange(test_deviance.shape[0]) + 1)[::5], test_deviance[::5], '-', color=color, label=label) plt.legend(loc='upper left') plt.xlabel('Boosting Iterations') plt.ylabel('Test Set Deviance') plt.show()
bsd-3-clause
nicproulx/mne-python
mne/evoked.py
2
51316
# -*- coding: utf-8 -*- # Authors: Alexandre Gramfort <[email protected]> # Matti Hamalainen <[email protected]> # Denis Engemann <[email protected]> # Andrew Dykstra <[email protected]> # Mads Jensen <[email protected]> # # License: BSD (3-clause) from copy import deepcopy import numpy as np from .baseline import rescale from .channels.channels import (ContainsMixin, UpdateChannelsMixin, SetChannelsMixin, InterpolationMixin, equalize_channels) from .channels.layout import _merge_grad_data, _pair_grad_sensors from .filter import resample, detrend, FilterMixin from .utils import (check_fname, logger, verbose, _time_mask, warn, sizeof_fmt, SizeMixin, copy_function_doc_to_method_doc) from .viz import (plot_evoked, plot_evoked_topomap, plot_evoked_field, plot_evoked_image, plot_evoked_topo) from .viz.evoked import (_plot_evoked_white, plot_evoked_joint, _animate_evoked_topomap) from .externals.six import string_types from .io.constants import FIFF from .io.open import fiff_open from .io.tag import read_tag from .io.tree import dir_tree_find from .io.pick import channel_type, pick_types, _pick_data_channels from .io.meas_info import read_meas_info, write_meas_info from .io.proj import ProjMixin from .io.write import (start_file, start_block, end_file, end_block, write_int, write_string, write_float_matrix, write_id) from .io.base import ToDataFrameMixin, TimeMixin, _check_maxshield _aspect_dict = {'average': FIFF.FIFFV_ASPECT_AVERAGE, 'standard_error': FIFF.FIFFV_ASPECT_STD_ERR} _aspect_rev = {str(FIFF.FIFFV_ASPECT_AVERAGE): 'average', str(FIFF.FIFFV_ASPECT_STD_ERR): 'standard_error'} class Evoked(ProjMixin, ContainsMixin, UpdateChannelsMixin, SetChannelsMixin, InterpolationMixin, FilterMixin, ToDataFrameMixin, TimeMixin, SizeMixin): """Evoked data. Parameters ---------- fname : string Name of evoked/average FIF file to load. If None no data is loaded. condition : int, or str Dataset ID number (int) or comment/name (str). Optional if there is only one data set in file. proj : bool, optional Apply SSP projection vectors kind : str Either 'average' or 'standard_error'. The type of data to read. Only used if 'condition' is a str. allow_maxshield : bool | str (default False) If True, allow loading of data that has been recorded with internal active compensation (MaxShield). Data recorded with MaxShield should generally not be loaded directly, but should first be processed using SSS/tSSS to remove the compensation signals that may also affect brain activity. Can also be "yes" to load without eliciting a warning. verbose : bool, str, int, or None If not None, override default verbose level (see :func:`mne.verbose` and :ref:`Logging documentation <tut_logging>` for more). Attributes ---------- info : dict Measurement info. ch_names : list of string List of channels' names. nave : int Number of averaged epochs. kind : str Type of data, either average or standard_error. first : int First time sample. last : int Last time sample. comment : string Comment on dataset. Can be the condition. times : array Array of time instants in seconds. data : array of shape (n_channels, n_times) Evoked response. verbose : bool, str, int, or None. See above. Notes ----- Evoked objects contain a single condition only. """ @verbose def __init__(self, fname, condition=None, proj=True, kind='average', allow_maxshield=False, verbose=None): # noqa: D102 if not isinstance(proj, bool): raise ValueError(r"'proj' must be 'True' or 'False'") # Read the requested data self.info, self.nave, self._aspect_kind, self.first, self.last, \ self.comment, self.times, self.data = _read_evoked( fname, condition, kind, allow_maxshield) self.kind = _aspect_rev.get(str(self._aspect_kind), 'Unknown') self.verbose = verbose self.preload = True # project and baseline correct if proj: self.apply_proj() @property def data(self): """The data matrix.""" return self._data @data.setter def data(self, data): """Set the data matrix.""" self._data = data @verbose def apply_baseline(self, baseline=(None, 0), verbose=None): """Baseline correct evoked data. Parameters ---------- baseline : tuple of length 2 The time interval to apply baseline correction. If None do not apply it. If baseline is (a, b) the interval is between "a (s)" and "b (s)". If a is None the beginning of the data is used and if b is None then b is set to the end of the interval. If baseline is equal to (None, None) all the time interval is used. Correction is applied by computing mean of the baseline period and subtracting it from the data. The baseline (a, b) includes both endpoints, i.e. all timepoints t such that a <= t <= b. verbose : bool, str, int, or None If not None, override default verbose level (see :func:`mne.verbose` and :ref:`Logging documentation <tut_logging>` for more). Returns ------- evoked : instance of Evoked The baseline-corrected Evoked object. Notes ----- Baseline correction can be done multiple times. .. versionadded:: 0.13.0 """ self.data = rescale(self.data, self.times, baseline, copy=False) return self def save(self, fname): """Save dataset to file. Parameters ---------- fname : string Name of the file where to save the data. Notes ----- To write multiple conditions into a single file, use :func:`mne.write_evokeds`. """ write_evokeds(fname, self) def __repr__(self): # noqa: D105 s = "comment : '%s'" % self.comment s += ', kind : %s' % self.kind s += ", time : [%f, %f]" % (self.times[0], self.times[-1]) s += ", n_epochs : %d" % self.nave s += ", n_channels x n_times : %s x %s" % self.data.shape s += ", ~%s" % (sizeof_fmt(self._size),) return "<Evoked | %s>" % s @property def ch_names(self): """Channel names.""" return self.info['ch_names'] def crop(self, tmin=None, tmax=None): """Crop data to a given time interval. Parameters ---------- tmin : float | None Start time of selection in seconds. tmax : float | None End time of selection in seconds. Returns ------- evoked : instance of Evoked The cropped Evoked object. Notes ----- Unlike Python slices, MNE time intervals include both their end points; crop(tmin, tmax) returns the interval tmin <= t <= tmax. """ mask = _time_mask(self.times, tmin, tmax, sfreq=self.info['sfreq']) self.times = self.times[mask] self.first = int(self.times[0] * self.info['sfreq']) self.last = len(self.times) + self.first - 1 self.data = self.data[:, mask] return self def decimate(self, decim, offset=0): """Decimate the evoked data. .. note:: No filtering is performed. To avoid aliasing, ensure your data are properly lowpassed. Parameters ---------- decim : int The amount to decimate data. offset : int Apply an offset to where the decimation starts relative to the sample corresponding to t=0. The offset is in samples at the current sampling rate. Returns ------- evoked : instance of Evoked The decimated Evoked object. See Also -------- Epochs.decimate Epochs.resample mne.io.Raw.resample Notes ----- Decimation can be done multiple times. For example, ``evoked.decimate(2).decimate(2)`` will be the same as ``evoked.decimate(4)``. .. versionadded:: 0.13.0 """ decim, offset, new_sfreq = _check_decim(self.info, decim, offset) start_idx = int(round(self.times[0] * (self.info['sfreq'] * decim))) i_start = start_idx % decim + offset decim_slice = slice(i_start, None, decim) self.info['sfreq'] = new_sfreq self.data = self.data[:, decim_slice].copy() self.times = self.times[decim_slice].copy() return self def shift_time(self, tshift, relative=True): """Shift time scale in evoked data. Parameters ---------- tshift : float The amount of time shift to be applied if relative is True else the first time point. When relative is True, positive value of tshift moves the data forward while negative tshift moves it backward. relative : bool If true, move the time backwards or forwards by specified amount. Else, set the starting time point to the value of tshift. Notes ----- Maximum accuracy of time shift is 1 / evoked.info['sfreq'] """ times = self.times sfreq = self.info['sfreq'] offset = self.first if relative else 0 self.first = int(tshift * sfreq) + offset self.last = self.first + len(times) - 1 self.times = np.arange(self.first, self.last + 1, dtype=np.float) / sfreq @copy_function_doc_to_method_doc(plot_evoked) def plot(self, picks=None, exclude='bads', unit=True, show=True, ylim=None, xlim='tight', proj=False, hline=None, units=None, scalings=None, titles=None, axes=None, gfp=False, window_title=None, spatial_colors=False, zorder='unsorted', selectable=True): return plot_evoked( self, picks=picks, exclude=exclude, unit=unit, show=show, ylim=ylim, proj=proj, xlim=xlim, hline=hline, units=units, scalings=scalings, titles=titles, axes=axes, gfp=gfp, window_title=window_title, spatial_colors=spatial_colors, zorder=zorder, selectable=selectable) @copy_function_doc_to_method_doc(plot_evoked_image) def plot_image(self, picks=None, exclude='bads', unit=True, show=True, clim=None, xlim='tight', proj=False, units=None, scalings=None, titles=None, axes=None, cmap='RdBu_r'): return plot_evoked_image(self, picks=picks, exclude=exclude, unit=unit, show=show, clim=clim, proj=proj, xlim=xlim, units=units, scalings=scalings, titles=titles, axes=axes, cmap=cmap) @copy_function_doc_to_method_doc(plot_evoked_topo) def plot_topo(self, layout=None, layout_scale=0.945, color=None, border='none', ylim=None, scalings=None, title=None, proj=False, vline=[0.0], fig_facecolor='k', fig_background=None, axis_facecolor='k', font_color='w', merge_grads=False, legend=True, show=True): """ Notes ----- .. versionadded:: 0.10.0 """ return plot_evoked_topo(self, layout=layout, layout_scale=layout_scale, color=color, border=border, ylim=ylim, scalings=scalings, title=title, proj=proj, vline=vline, fig_facecolor=fig_facecolor, fig_background=fig_background, axis_facecolor=axis_facecolor, font_color=font_color, merge_grads=merge_grads, legend=legend, show=show) @copy_function_doc_to_method_doc(plot_evoked_topomap) def plot_topomap(self, times="auto", ch_type=None, layout=None, vmin=None, vmax=None, cmap=None, sensors=True, colorbar=True, scale=None, scale_time=1e3, unit=None, res=64, size=1, cbar_fmt="%3.1f", time_format='%01d ms', proj=False, show=True, show_names=False, title=None, mask=None, mask_params=None, outlines='head', contours=6, image_interp='bilinear', average=None, head_pos=None, axes=None): return plot_evoked_topomap(self, times=times, ch_type=ch_type, layout=layout, vmin=vmin, vmax=vmax, cmap=cmap, sensors=sensors, colorbar=colorbar, scale=scale, scale_time=scale_time, unit=unit, res=res, proj=proj, size=size, cbar_fmt=cbar_fmt, time_format=time_format, show=show, show_names=show_names, title=title, mask=mask, mask_params=mask_params, outlines=outlines, contours=contours, image_interp=image_interp, average=average, head_pos=head_pos, axes=axes) @copy_function_doc_to_method_doc(plot_evoked_field) def plot_field(self, surf_maps, time=None, time_label='t = %0.0f ms', n_jobs=1): return plot_evoked_field(self, surf_maps, time=time, time_label=time_label, n_jobs=n_jobs) def plot_white(self, noise_cov, show=True): """Plot whitened evoked response. Plots the whitened evoked response and the whitened GFP as described in [1]_. If one single covariance object is passed, the GFP panel (bottom) will depict different sensor types. If multiple covariance objects are passed as a list, the left column will display the whitened evoked responses for each channel based on the whitener from the noise covariance that has the highest log-likelihood. The left column will depict the whitened GFPs based on each estimator separately for each sensor type. Instead of numbers of channels the GFP display shows the estimated rank. The rank estimation will be printed by the logger for each noise covariance estimator that is passed. Parameters ---------- noise_cov : list | instance of Covariance | str The noise covariance as computed by ``mne.cov.compute_covariance``. show : bool Whether to show the figure or not. Defaults to True. Returns ------- fig : instance of matplotlib.figure.Figure The figure object containing the plot. References ---------- .. [1] Engemann D. and Gramfort A. (2015) Automated model selection in covariance estimation and spatial whitening of MEG and EEG signals, vol. 108, 328-342, NeuroImage. Notes ----- .. versionadded:: 0.9.0 """ return _plot_evoked_white(self, noise_cov=noise_cov, scalings=None, rank=None, show=show) @copy_function_doc_to_method_doc(plot_evoked_joint) def plot_joint(self, times="peaks", title='', picks=None, exclude='bads', show=True, ts_args=None, topomap_args=None): return plot_evoked_joint(self, times=times, title=title, picks=picks, exclude=exclude, show=show, ts_args=ts_args, topomap_args=topomap_args) def animate_topomap(self, ch_type='mag', times=None, frame_rate=None, butterfly=False, blit=True, show=True): """Make animation of evoked data as topomap timeseries. The animation can be paused/resumed with left mouse button. Left and right arrow keys can be used to move backward or forward in time. Parameters ---------- ch_type : str | None Channel type to plot. Accepted data types: 'mag', 'grad', 'eeg'. If None, first available channel type from ('mag', 'grad', 'eeg') is used. Defaults to None. times : array of floats | None The time points to plot. If None, 10 evenly spaced samples are calculated over the evoked time series. Defaults to None. frame_rate : int | None Frame rate for the animation in Hz. If None, frame rate = sfreq / 10. Defaults to None. butterfly : bool Whether to plot the data as butterfly plot under the topomap. Defaults to False. blit : bool Whether to use blit to optimize drawing. In general, it is recommended to use blit in combination with ``show=True``. If you intend to save the animation it is better to disable blit. Defaults to True. show : bool Whether to show the animation. Defaults to True. Returns ------- fig : instance of matplotlib figure The figure. anim : instance of matplotlib FuncAnimation Animation of the topomap. Notes ----- .. versionadded:: 0.12.0 """ return _animate_evoked_topomap(self, ch_type=ch_type, times=times, frame_rate=frame_rate, butterfly=butterfly, blit=blit, show=show) def as_type(self, ch_type='grad', mode='fast'): """Compute virtual evoked using interpolated fields. .. Warning:: Using virtual evoked to compute inverse can yield unexpected results. The virtual channels have `'_virtual'` appended at the end of the names to emphasize that the data contained in them are interpolated. Parameters ---------- ch_type : str The destination channel type. It can be 'mag' or 'grad'. mode : str Either `'accurate'` or `'fast'`, determines the quality of the Legendre polynomial expansion used. `'fast'` should be sufficient for most applications. Returns ------- evoked : instance of mne.Evoked The transformed evoked object containing only virtual channels. Notes ----- .. versionadded:: 0.9.0 """ from .forward import _as_meg_type_evoked return _as_meg_type_evoked(self, ch_type=ch_type, mode=mode) def resample(self, sfreq, npad='auto', window='boxcar'): """Resample data. This function operates in-place. Parameters ---------- sfreq : float New sample rate to use npad : int | str Amount to pad the start and end of the data. Can also be "auto" to use a padding that will result in a power-of-two size (can be much faster). window : string or tuple Window to use in resampling. See scipy.signal.resample. Returns ------- evoked : instance of mne.Evoked The resampled evoked object. """ sfreq = float(sfreq) o_sfreq = self.info['sfreq'] self.data = resample(self.data, sfreq, o_sfreq, npad, -1, window) # adjust indirectly affected variables self.info['sfreq'] = sfreq self.times = (np.arange(self.data.shape[1], dtype=np.float) / sfreq + self.times[0]) self.first = int(self.times[0] * self.info['sfreq']) self.last = len(self.times) + self.first - 1 return self def detrend(self, order=1, picks=None): """Detrend data. This function operates in-place. Parameters ---------- order : int Either 0 or 1, the order of the detrending. 0 is a constant (DC) detrend, 1 is a linear detrend. picks : array-like of int | None If None only MEG, EEG, SEEG, ECoG and fNIRS channels are detrended. Returns ------- evoked : instance of Evoked The detrended evoked object. """ if picks is None: picks = _pick_data_channels(self.info) self.data[picks] = detrend(self.data[picks], order, axis=-1) return self def copy(self): """Copy the instance of evoked. Returns ------- evoked : instance of Evoked """ evoked = deepcopy(self) return evoked def __neg__(self): """Negate channel responses. Returns ------- evoked_neg : instance of Evoked The Evoked instance with channel data negated and '-' prepended to the comment. """ out = self.copy() out.data *= -1 out.comment = '-' + (out.comment or 'unknown') return out def get_peak(self, ch_type=None, tmin=None, tmax=None, mode='abs', time_as_index=False, merge_grads=False): """Get location and latency of peak amplitude. Parameters ---------- ch_type : 'mag', 'grad', 'eeg', 'seeg', 'ecog', 'hbo', hbr', 'misc', None # noqa The channel type to use. Defaults to None. If more than one sensor Type is present in the data the channel type has to be explicitly set. tmin : float | None The minimum point in time to be considered for peak getting. If None (default), the beginning of the data is used. tmax : float | None The maximum point in time to be considered for peak getting. If None (default), the end of the data is used. mode : {'pos', 'neg', 'abs'} How to deal with the sign of the data. If 'pos' only positive values will be considered. If 'neg' only negative values will be considered. If 'abs' absolute values will be considered. Defaults to 'abs'. time_as_index : bool Whether to return the time index instead of the latency in seconds. merge_grads : bool If True, compute peak from merged gradiometer data. Returns ------- ch_name : str The channel exhibiting the maximum response. latency : float | int The time point of the maximum response, either latency in seconds or index. """ supported = ('mag', 'grad', 'eeg', 'seeg', 'ecog', 'misc', 'hbo', 'hbr', 'None') data_picks = _pick_data_channels(self.info, with_ref_meg=False) types_used = set([channel_type(self.info, idx) for idx in data_picks]) if str(ch_type) not in supported: raise ValueError('Channel type must be `{supported}`. You gave me ' '`{ch_type}` instead.' .format(ch_type=ch_type, supported='` or `'.join(supported))) elif ch_type is not None and ch_type not in types_used: raise ValueError('Channel type `{ch_type}` not found in this ' 'evoked object.'.format(ch_type=ch_type)) elif len(types_used) > 1 and ch_type is None: raise RuntimeError('More than one sensor type found. `ch_type` ' 'must not be `None`, pass a sensor type ' 'value instead') if merge_grads: if ch_type != 'grad': raise ValueError('Channel type must be grad for merge_grads') elif mode == 'neg': raise ValueError('Negative mode (mode=neg) does not make ' 'sense with merge_grads=True') meg = eeg = misc = seeg = ecog = fnirs = False picks = None if ch_type in ('mag', 'grad'): meg = ch_type elif ch_type == 'eeg': eeg = True elif ch_type == 'misc': misc = True elif ch_type == 'seeg': seeg = True elif ch_type == 'ecog': ecog = True elif ch_type in ('hbo', 'hbr'): fnirs = ch_type if ch_type is not None: if merge_grads: picks = _pair_grad_sensors(self.info, topomap_coords=False) else: picks = pick_types(self.info, meg=meg, eeg=eeg, misc=misc, seeg=seeg, ecog=ecog, ref_meg=False, fnirs=fnirs) data = self.data ch_names = self.ch_names if picks is not None: data = data[picks] ch_names = [ch_names[k] for k in picks] if merge_grads: data = _merge_grad_data(data) ch_names = [ch_name[:-1] + 'X' for ch_name in ch_names[::2]] ch_idx, time_idx = _get_peak(data, self.times, tmin, tmax, mode) return (ch_names[ch_idx], time_idx if time_as_index else self.times[time_idx]) def _check_decim(info, decim, offset): """Check decimation parameters.""" if decim < 1 or decim != int(decim): raise ValueError('decim must be an integer > 0') decim = int(decim) new_sfreq = info['sfreq'] / float(decim) lowpass = info['lowpass'] if decim > 1 and lowpass is None: warn('The measurement information indicates data is not low-pass ' 'filtered. The decim=%i parameter will result in a sampling ' 'frequency of %g Hz, which can cause aliasing artifacts.' % (decim, new_sfreq)) elif decim > 1 and new_sfreq < 2.5 * lowpass: warn('The measurement information indicates a low-pass frequency ' 'of %g Hz. The decim=%i parameter will result in a sampling ' 'frequency of %g Hz, which can cause aliasing artifacts.' % (lowpass, decim, new_sfreq)) # > 50% nyquist lim offset = int(offset) if not 0 <= offset < decim: raise ValueError('decim must be at least 0 and less than %s, got ' '%s' % (decim, offset)) return decim, offset, new_sfreq class EvokedArray(Evoked): """Evoked object from numpy array. Parameters ---------- data : array of shape (n_channels, n_times) The channels' evoked response. See notes for proper units of measure. info : instance of Info Info dictionary. Consider using ``create_info`` to populate this structure. tmin : float Start time before event. Defaults to 0. comment : string Comment on dataset. Can be the condition. Defaults to ''. nave : int Number of averaged epochs. Defaults to 1. kind : str Type of data, either average or standard_error. Defaults to 'average'. verbose : bool, str, int, or None If not None, override default verbose level (see :func:`mne.verbose` and :ref:`Logging documentation <tut_logging>` for more). Notes ----- Proper units of measure: * V: eeg, eog, seeg, emg, ecg, bio, ecog * T: mag * T/m: grad * M: hbo, hbr * Am: dipole * AU: misc See Also -------- EpochsArray, io.RawArray, create_info """ @verbose def __init__(self, data, info, tmin=0., comment='', nave=1, kind='average', verbose=None): # noqa: D102 dtype = np.complex128 if np.any(np.iscomplex(data)) else np.float64 data = np.asanyarray(data, dtype=dtype) if data.ndim != 2: raise ValueError('Data must be a 2D array of shape (n_channels, ' 'n_samples)') if len(info['ch_names']) != np.shape(data)[0]: raise ValueError('Info (%s) and data (%s) must have same number ' 'of channels.' % (len(info['ch_names']), np.shape(data)[0])) self.data = data # XXX: this should use round and be tested self.first = int(tmin * info['sfreq']) self.last = self.first + np.shape(data)[-1] - 1 self.times = np.arange(self.first, self.last + 1, dtype=np.float) / info['sfreq'] self.info = info.copy() # do not modify original info self.nave = nave self.kind = kind self.comment = comment self.picks = None self.verbose = verbose self.preload = True self._projector = None if not isinstance(self.kind, string_types): raise TypeError('kind must be a string, not "%s"' % (type(kind),)) if self.kind not in _aspect_dict: raise ValueError('unknown kind "%s", should be "average" or ' '"standard_error"' % (self.kind,)) self._aspect_kind = _aspect_dict[self.kind] def _get_entries(fid, evoked_node, allow_maxshield=False): """Get all evoked entries.""" comments = list() aspect_kinds = list() for ev in evoked_node: for k in range(ev['nent']): my_kind = ev['directory'][k].kind pos = ev['directory'][k].pos if my_kind == FIFF.FIFF_COMMENT: tag = read_tag(fid, pos) comments.append(tag.data) my_aspect = _get_aspect(ev, allow_maxshield)[0] for k in range(my_aspect['nent']): my_kind = my_aspect['directory'][k].kind pos = my_aspect['directory'][k].pos if my_kind == FIFF.FIFF_ASPECT_KIND: tag = read_tag(fid, pos) aspect_kinds.append(int(tag.data)) comments = np.atleast_1d(comments) aspect_kinds = np.atleast_1d(aspect_kinds) if len(comments) != len(aspect_kinds) or len(comments) == 0: fid.close() raise ValueError('Dataset names in FIF file ' 'could not be found.') t = [_aspect_rev.get(str(a), 'Unknown') for a in aspect_kinds] t = ['"' + c + '" (' + tt + ')' for tt, c in zip(t, comments)] t = ' ' + '\n '.join(t) return comments, aspect_kinds, t def _get_aspect(evoked, allow_maxshield): """Get Evoked data aspect.""" is_maxshield = False aspect = dir_tree_find(evoked, FIFF.FIFFB_ASPECT) if len(aspect) == 0: _check_maxshield(allow_maxshield) aspect = dir_tree_find(evoked, FIFF.FIFFB_SMSH_ASPECT) is_maxshield = True if len(aspect) > 1: logger.info('Multiple data aspects found. Taking first one.') return aspect[0], is_maxshield def _get_evoked_node(fname): """Get info in evoked file.""" f, tree, _ = fiff_open(fname) with f as fid: _, meas = read_meas_info(fid, tree, verbose=False) evoked_node = dir_tree_find(meas, FIFF.FIFFB_EVOKED) return evoked_node def grand_average(all_evoked, interpolate_bads=True): """Make grand average of a list evoked data. The function interpolates bad channels based on `interpolate_bads` parameter. If `interpolate_bads` is True, the grand average file will contain good channels and the bad channels interpolated from the good MEG/EEG channels. The grand_average.nave attribute will be equal the number of evoked datasets used to calculate the grand average. Note: Grand average evoked shall not be used for source localization. Parameters ---------- all_evoked : list of Evoked data The evoked datasets. interpolate_bads : bool If True, bad MEG and EEG channels are interpolated. Returns ------- grand_average : Evoked The grand average data. Notes ----- .. versionadded:: 0.9.0 """ # check if all elements in the given list are evoked data if not all(isinstance(e, Evoked) for e in all_evoked): raise ValueError("Not all the elements in list are evoked data") # Copy channels to leave the original evoked datasets intact. all_evoked = [e.copy() for e in all_evoked] # Interpolates if necessary if interpolate_bads: all_evoked = [e.interpolate_bads() if len(e.info['bads']) > 0 else e for e in all_evoked] equalize_channels(all_evoked) # apply equalize_channels # make grand_average object using combine_evoked grand_average = combine_evoked(all_evoked, weights='equal') # change the grand_average.nave to the number of Evokeds grand_average.nave = len(all_evoked) # change comment field grand_average.comment = "Grand average (n = %d)" % grand_average.nave return grand_average def combine_evoked(all_evoked, weights): """Merge evoked data by weighted addition or subtraction. Data should have the same channels and the same time instants. Subtraction can be performed by passing negative weights (e.g., [1, -1]). Parameters ---------- all_evoked : list of Evoked The evoked datasets. weights : list of float | str The weights to apply to the data of each evoked instance. Can also be ``'nave'`` to weight according to evoked.nave, or ``"equal"`` to use equal weighting (each weighted as ``1/N``). Returns ------- evoked : Evoked The new evoked data. Notes ----- .. versionadded:: 0.9.0 """ evoked = all_evoked[0].copy() if isinstance(weights, string_types): if weights not in ('nave', 'equal'): raise ValueError('weights must be a list of float, or "nave" or ' '"equal"') if weights == 'nave': weights = np.array([e.nave for e in all_evoked], float) weights /= weights.sum() else: # == 'equal' weights = [1. / len(all_evoked)] * len(all_evoked) weights = np.array(weights, float) if weights.ndim != 1 or weights.size != len(all_evoked): raise ValueError('weights must be the same size as all_evoked') ch_names = evoked.ch_names for e in all_evoked[1:]: assert e.ch_names == ch_names, ValueError("%s and %s do not contain " "the same channels" % (evoked, e)) assert np.max(np.abs(e.times - evoked.times)) < 1e-7, \ ValueError("%s and %s do not contain the same time instants" % (evoked, e)) # use union of bad channels bads = list(set(evoked.info['bads']).union(*(ev.info['bads'] for ev in all_evoked[1:]))) evoked.info['bads'] = bads evoked.data = sum(w * e.data for w, e in zip(weights, all_evoked)) # We should set nave based on how variances change when summing Gaussian # random variables. From: # # https://en.wikipedia.org/wiki/Weighted_arithmetic_mean # # We know that the variance of a weighted sample mean is: # # σ^2 = w_1^2 σ_1^2 + w_2^2 σ_2^2 + ... + w_n^2 σ_n^2 # # We estimate the variance of each evoked instance as 1 / nave to get: # # σ^2 = w_1^2 / nave_1 + w_2^2 / nave_2 + ... + w_n^2 / nave_n # # And our resulting nave is the reciprocal of this: evoked.nave = max(int(round( 1. / sum(w ** 2 / e.nave for w, e in zip(weights, all_evoked)))), 1) evoked.comment = ' + '.join('%0.3f * %s' % (w, e.comment or 'unknown') for w, e in zip(weights, all_evoked)) return evoked @verbose def read_evokeds(fname, condition=None, baseline=None, kind='average', proj=True, allow_maxshield=False, verbose=None): """Read evoked dataset(s). Parameters ---------- fname : string The file name, which should end with -ave.fif or -ave.fif.gz. condition : int or str | list of int or str | None The index or list of indices of the evoked dataset to read. FIF files can contain multiple datasets. If None, all datasets are returned as a list. baseline : None (default) or tuple of length 2 The time interval to apply baseline correction. If None do not apply it. If baseline is (a, b) the interval is between "a (s)" and "b (s)". If a is None the beginning of the data is used and if b is None then b is set to the end of the interval. If baseline is equal to (None, None) all the time interval is used. Correction is applied by computing mean of the baseline period and subtracting it from the data. The baseline (a, b) includes both endpoints, i.e. all timepoints t such that a <= t <= b. kind : str Either 'average' or 'standard_error', the type of data to read. proj : bool If False, available projectors won't be applied to the data. allow_maxshield : bool | str (default False) If True, allow loading of data that has been recorded with internal active compensation (MaxShield). Data recorded with MaxShield should generally not be loaded directly, but should first be processed using SSS/tSSS to remove the compensation signals that may also affect brain activity. Can also be "yes" to load without eliciting a warning. verbose : bool, str, int, or None If not None, override default verbose level (see :func:`mne.verbose` and :ref:`Logging documentation <tut_logging>` for more). Returns ------- evoked : Evoked (if condition is int or str) or list of Evoked (if condition is None or list) The evoked dataset(s). See Also -------- write_evokeds """ check_fname(fname, 'evoked', ('-ave.fif', '-ave.fif.gz')) logger.info('Reading %s ...' % fname) return_list = True if condition is None: evoked_node = _get_evoked_node(fname) condition = range(len(evoked_node)) elif not isinstance(condition, list): condition = [condition] return_list = False out = [Evoked(fname, c, kind=kind, proj=proj, allow_maxshield=allow_maxshield, verbose=verbose).apply_baseline(baseline) for c in condition] return out if return_list else out[0] def _read_evoked(fname, condition=None, kind='average', allow_maxshield=False): """Read evoked data from a FIF file.""" if fname is None: raise ValueError('No evoked filename specified') f, tree, _ = fiff_open(fname) with f as fid: # Read the measurement info info, meas = read_meas_info(fid, tree, clean_bads=True) # Locate the data of interest processed = dir_tree_find(meas, FIFF.FIFFB_PROCESSED_DATA) if len(processed) == 0: raise ValueError('Could not find processed data') evoked_node = dir_tree_find(meas, FIFF.FIFFB_EVOKED) if len(evoked_node) == 0: raise ValueError('Could not find evoked data') # find string-based entry if isinstance(condition, string_types): if kind not in _aspect_dict.keys(): raise ValueError('kind must be "average" or ' '"standard_error"') comments, aspect_kinds, t = _get_entries(fid, evoked_node, allow_maxshield) goods = (np.in1d(comments, [condition]) & np.in1d(aspect_kinds, [_aspect_dict[kind]])) found_cond = np.where(goods)[0] if len(found_cond) != 1: raise ValueError('condition "%s" (%s) not found, out of ' 'found datasets:\n %s' % (condition, kind, t)) condition = found_cond[0] elif condition is None: if len(evoked_node) > 1: _, _, conditions = _get_entries(fid, evoked_node, allow_maxshield) raise TypeError("Evoked file has more than one " "conditions, the condition parameters " "must be specified from:\n%s" % conditions) else: condition = 0 if condition >= len(evoked_node) or condition < 0: raise ValueError('Data set selector out of range') my_evoked = evoked_node[condition] # Identify the aspects my_aspect, info['maxshield'] = _get_aspect(my_evoked, allow_maxshield) # Now find the data in the evoked block nchan = 0 sfreq = -1 chs = [] comment = last = first = first_time = nsamp = None for k in range(my_evoked['nent']): my_kind = my_evoked['directory'][k].kind pos = my_evoked['directory'][k].pos if my_kind == FIFF.FIFF_COMMENT: tag = read_tag(fid, pos) comment = tag.data elif my_kind == FIFF.FIFF_FIRST_SAMPLE: tag = read_tag(fid, pos) first = int(tag.data) elif my_kind == FIFF.FIFF_LAST_SAMPLE: tag = read_tag(fid, pos) last = int(tag.data) elif my_kind == FIFF.FIFF_NCHAN: tag = read_tag(fid, pos) nchan = int(tag.data) elif my_kind == FIFF.FIFF_SFREQ: tag = read_tag(fid, pos) sfreq = float(tag.data) elif my_kind == FIFF.FIFF_CH_INFO: tag = read_tag(fid, pos) chs.append(tag.data) elif my_kind == FIFF.FIFF_FIRST_TIME: tag = read_tag(fid, pos) first_time = float(tag.data) elif my_kind == FIFF.FIFF_NO_SAMPLES: tag = read_tag(fid, pos) nsamp = int(tag.data) if comment is None: comment = 'No comment' # Local channel information? if nchan > 0: if chs is None: raise ValueError('Local channel information was not found ' 'when it was expected.') if len(chs) != nchan: raise ValueError('Number of channels and number of ' 'channel definitions are different') info['chs'] = chs logger.info(' Found channel information in evoked data. ' 'nchan = %d' % nchan) if sfreq > 0: info['sfreq'] = sfreq # Read the data in the aspect block nave = 1 epoch = [] for k in range(my_aspect['nent']): kind = my_aspect['directory'][k].kind pos = my_aspect['directory'][k].pos if kind == FIFF.FIFF_COMMENT: tag = read_tag(fid, pos) comment = tag.data elif kind == FIFF.FIFF_ASPECT_KIND: tag = read_tag(fid, pos) aspect_kind = int(tag.data) elif kind == FIFF.FIFF_NAVE: tag = read_tag(fid, pos) nave = int(tag.data) elif kind == FIFF.FIFF_EPOCH: tag = read_tag(fid, pos) epoch.append(tag) nepoch = len(epoch) if nepoch != 1 and nepoch != info['nchan']: raise ValueError('Number of epoch tags is unreasonable ' '(nepoch = %d nchan = %d)' % (nepoch, info['nchan'])) if nepoch == 1: # Only one epoch data = epoch[0].data # May need a transpose if the number of channels is one if data.shape[1] == 1 and info['nchan'] == 1: data = data.T else: # Put the old style epochs together data = np.concatenate([e.data[None, :] for e in epoch], axis=0) data = data.astype(np.float) if first is not None: nsamp = last - first + 1 elif first_time is not None: first = int(round(first_time * info['sfreq'])) last = first + nsamp else: raise RuntimeError('Could not read time parameters') if nsamp is not None and data.shape[1] != nsamp: raise ValueError('Incorrect number of samples (%d instead of ' ' %d)' % (data.shape[1], nsamp)) nsamp = data.shape[1] last = first + nsamp - 1 logger.info(' Found the data of interest:') logger.info(' t = %10.2f ... %10.2f ms (%s)' % (1000 * first / info['sfreq'], 1000 * last / info['sfreq'], comment)) if info['comps'] is not None: logger.info(' %d CTF compensation matrices available' % len(info['comps'])) logger.info(' nave = %d - aspect type = %d' % (nave, aspect_kind)) # Calibrate cals = np.array([info['chs'][k]['cal'] * info['chs'][k].get('scale', 1.0) for k in range(info['nchan'])]) data *= cals[:, np.newaxis] times = np.arange(first, last + 1, dtype=np.float) / info['sfreq'] return info, nave, aspect_kind, first, last, comment, times, data def write_evokeds(fname, evoked): """Write an evoked dataset to a file. Parameters ---------- fname : string The file name, which should end with -ave.fif or -ave.fif.gz. evoked : Evoked instance, or list of Evoked instances The evoked dataset, or list of evoked datasets, to save in one file. Note that the measurement info from the first evoked instance is used, so be sure that information matches. See Also -------- read_evokeds """ _write_evokeds(fname, evoked) def _write_evokeds(fname, evoked, check=True): """Write evoked data.""" if check: check_fname(fname, 'evoked', ('-ave.fif', '-ave.fif.gz')) if not isinstance(evoked, list): evoked = [evoked] # Create the file and save the essentials with start_file(fname) as fid: start_block(fid, FIFF.FIFFB_MEAS) write_id(fid, FIFF.FIFF_BLOCK_ID) if evoked[0].info['meas_id'] is not None: write_id(fid, FIFF.FIFF_PARENT_BLOCK_ID, evoked[0].info['meas_id']) # Write measurement info write_meas_info(fid, evoked[0].info) # One or more evoked data sets start_block(fid, FIFF.FIFFB_PROCESSED_DATA) for e in evoked: start_block(fid, FIFF.FIFFB_EVOKED) # Comment is optional if e.comment is not None and len(e.comment) > 0: write_string(fid, FIFF.FIFF_COMMENT, e.comment) # First and last sample write_int(fid, FIFF.FIFF_FIRST_SAMPLE, e.first) write_int(fid, FIFF.FIFF_LAST_SAMPLE, e.last) # The epoch itself if e.info.get('maxshield'): aspect = FIFF.FIFFB_SMSH_ASPECT else: aspect = FIFF.FIFFB_ASPECT start_block(fid, aspect) write_int(fid, FIFF.FIFF_ASPECT_KIND, e._aspect_kind) write_int(fid, FIFF.FIFF_NAVE, e.nave) decal = np.zeros((e.info['nchan'], 1)) for k in range(e.info['nchan']): decal[k] = 1.0 / (e.info['chs'][k]['cal'] * e.info['chs'][k].get('scale', 1.0)) write_float_matrix(fid, FIFF.FIFF_EPOCH, decal * e.data) end_block(fid, aspect) end_block(fid, FIFF.FIFFB_EVOKED) end_block(fid, FIFF.FIFFB_PROCESSED_DATA) end_block(fid, FIFF.FIFFB_MEAS) end_file(fid) def _get_peak(data, times, tmin=None, tmax=None, mode='abs'): """Get feature-index and time of maximum signal from 2D array. Note. This is a 'getter', not a 'finder'. For non-evoked type data and continuous signals, please use proper peak detection algorithms. Parameters ---------- data : instance of numpy.ndarray (n_locations, n_times) The data, either evoked in sensor or source space. times : instance of numpy.ndarray (n_times) The times in seconds. tmin : float | None The minimum point in time to be considered for peak getting. tmax : float | None The maximum point in time to be considered for peak getting. mode : {'pos', 'neg', 'abs'} How to deal with the sign of the data. If 'pos' only positive values will be considered. If 'neg' only negative values will be considered. If 'abs' absolute values will be considered. Defaults to 'abs'. Returns ------- max_loc : int The index of the feature with the maximum value. max_time : int The time point of the maximum response, index. """ modes = ('abs', 'neg', 'pos') if mode not in modes: raise ValueError('The `mode` parameter must be `{modes}`. You gave ' 'me `{mode}`'.format(modes='` or `'.join(modes), mode=mode)) if tmin is None: tmin = times[0] if tmax is None: tmax = times[-1] if tmin < times.min(): raise ValueError('The tmin value is out of bounds. It must be ' 'within {0} and {1}'.format(times.min(), times.max())) if tmax > times.max(): raise ValueError('The tmin value is out of bounds. It must be ' 'within {0} and {1}'.format(times.min(), times.max())) if tmin >= tmax: raise ValueError('The tmin must be smaller than tmax') time_win = (times >= tmin) & (times <= tmax) mask = np.ones_like(data).astype(np.bool) mask[:, time_win] = False maxfun = np.argmax if mode == 'pos': if not np.any(data > 0): raise ValueError('No positive values encountered. Cannot ' 'operate in pos mode.') elif mode == 'neg': if not np.any(data < 0): raise ValueError('No negative values encountered. Cannot ' 'operate in neg mode.') maxfun = np.argmin masked_index = np.ma.array(np.abs(data) if mode == 'abs' else data, mask=mask) max_loc, max_time = np.unravel_index(maxfun(masked_index), data.shape) return max_loc, max_time
bsd-3-clause
cython-testbed/pandas
pandas/tests/frame/test_to_csv.py
1
44233
# -*- coding: utf-8 -*- from __future__ import print_function import csv import pytest from numpy import nan import numpy as np from pandas.compat import (lmap, range, lrange, StringIO, u) from pandas.io.common import _get_handle import pandas.core.common as com from pandas.errors import ParserError from pandas import (DataFrame, Index, Series, MultiIndex, Timestamp, date_range, read_csv, compat, to_datetime) import pandas as pd from pandas.util.testing import (assert_almost_equal, assert_series_equal, assert_frame_equal, ensure_clean, makeCustomDataframe as mkdf) import pandas.util.testing as tm from pandas.tests.frame.common import TestData MIXED_FLOAT_DTYPES = ['float16', 'float32', 'float64'] MIXED_INT_DTYPES = ['uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64'] class TestDataFrameToCSV(TestData): def read_csv(self, path, **kwargs): params = dict(index_col=0, parse_dates=True) params.update(**kwargs) return pd.read_csv(path, **params) def test_from_csv_deprecation(self): # see gh-17812 with ensure_clean('__tmp_from_csv_deprecation__') as path: self.tsframe.to_csv(path) with tm.assert_produces_warning(FutureWarning): depr_recons = DataFrame.from_csv(path) assert_frame_equal(self.tsframe, depr_recons) def test_to_csv_from_csv1(self): with ensure_clean('__tmp_to_csv_from_csv1__') as path: self.frame['A'][:5] = nan self.frame.to_csv(path) self.frame.to_csv(path, columns=['A', 'B']) self.frame.to_csv(path, header=False) self.frame.to_csv(path, index=False) # test roundtrip self.tsframe.to_csv(path) recons = self.read_csv(path) assert_frame_equal(self.tsframe, recons) self.tsframe.to_csv(path, index_label='index') recons = self.read_csv(path, index_col=None) assert(len(recons.columns) == len(self.tsframe.columns) + 1) # no index self.tsframe.to_csv(path, index=False) recons = self.read_csv(path, index_col=None) assert_almost_equal(self.tsframe.values, recons.values) # corner case dm = DataFrame({'s1': Series(lrange(3), lrange(3)), 's2': Series(lrange(2), lrange(2))}) dm.to_csv(path) recons = self.read_csv(path) assert_frame_equal(dm, recons) def test_to_csv_from_csv2(self): with ensure_clean('__tmp_to_csv_from_csv2__') as path: # duplicate index df = DataFrame(np.random.randn(3, 3), index=['a', 'a', 'b'], columns=['x', 'y', 'z']) df.to_csv(path) result = self.read_csv(path) assert_frame_equal(result, df) midx = MultiIndex.from_tuples( [('A', 1, 2), ('A', 1, 2), ('B', 1, 2)]) df = DataFrame(np.random.randn(3, 3), index=midx, columns=['x', 'y', 'z']) df.to_csv(path) result = self.read_csv(path, index_col=[0, 1, 2], parse_dates=False) assert_frame_equal(result, df, check_names=False) # column aliases col_aliases = Index(['AA', 'X', 'Y', 'Z']) self.frame2.to_csv(path, header=col_aliases) rs = self.read_csv(path) xp = self.frame2.copy() xp.columns = col_aliases assert_frame_equal(xp, rs) pytest.raises(ValueError, self.frame2.to_csv, path, header=['AA', 'X']) def test_to_csv_from_csv3(self): with ensure_clean('__tmp_to_csv_from_csv3__') as path: df1 = DataFrame(np.random.randn(3, 1)) df2 = DataFrame(np.random.randn(3, 1)) df1.to_csv(path) df2.to_csv(path, mode='a', header=False) xp = pd.concat([df1, df2]) rs = pd.read_csv(path, index_col=0) rs.columns = lmap(int, rs.columns) xp.columns = lmap(int, xp.columns) assert_frame_equal(xp, rs) def test_to_csv_from_csv4(self): with ensure_clean('__tmp_to_csv_from_csv4__') as path: # GH 10833 (TimedeltaIndex formatting) dt = pd.Timedelta(seconds=1) df = pd.DataFrame({'dt_data': [i * dt for i in range(3)]}, index=pd.Index([i * dt for i in range(3)], name='dt_index')) df.to_csv(path) result = pd.read_csv(path, index_col='dt_index') result.index = pd.to_timedelta(result.index) # TODO: remove renaming when GH 10875 is solved result.index = result.index.rename('dt_index') result['dt_data'] = pd.to_timedelta(result['dt_data']) assert_frame_equal(df, result, check_index_type=True) def test_to_csv_from_csv5(self): # tz, 8260 with ensure_clean('__tmp_to_csv_from_csv5__') as path: self.tzframe.to_csv(path) result = pd.read_csv(path, index_col=0, parse_dates=['A']) converter = lambda c: to_datetime(result[c]).dt.tz_convert( 'UTC').dt.tz_convert(self.tzframe[c].dt.tz) result['B'] = converter('B') result['C'] = converter('C') assert_frame_equal(result, self.tzframe) def test_to_csv_cols_reordering(self): # GH3454 import pandas as pd chunksize = 5 N = int(chunksize * 2.5) df = mkdf(N, 3) cs = df.columns cols = [cs[2], cs[0]] with ensure_clean() as path: df.to_csv(path, columns=cols, chunksize=chunksize) rs_c = pd.read_csv(path, index_col=0) assert_frame_equal(df[cols], rs_c, check_names=False) def test_to_csv_new_dupe_cols(self): import pandas as pd def _check_df(df, cols=None): with ensure_clean() as path: df.to_csv(path, columns=cols, chunksize=chunksize) rs_c = pd.read_csv(path, index_col=0) # we wrote them in a different order # so compare them in that order if cols is not None: if df.columns.is_unique: rs_c.columns = cols else: indexer, missing = df.columns.get_indexer_non_unique( cols) rs_c.columns = df.columns.take(indexer) for c in cols: obj_df = df[c] obj_rs = rs_c[c] if isinstance(obj_df, Series): assert_series_equal(obj_df, obj_rs) else: assert_frame_equal( obj_df, obj_rs, check_names=False) # wrote in the same order else: rs_c.columns = df.columns assert_frame_equal(df, rs_c, check_names=False) chunksize = 5 N = int(chunksize * 2.5) # dupe cols df = mkdf(N, 3) df.columns = ['a', 'a', 'b'] _check_df(df, None) # dupe cols with selection cols = ['b', 'a'] _check_df(df, cols) @pytest.mark.slow def test_to_csv_dtnat(self): # GH3437 from pandas import NaT def make_dtnat_arr(n, nnat=None): if nnat is None: nnat = int(n * 0.1) # 10% s = list(date_range('2000', freq='5min', periods=n)) if nnat: for i in np.random.randint(0, len(s), nnat): s[i] = NaT i = np.random.randint(100) s[-i] = NaT s[i] = NaT return s chunksize = 1000 # N=35000 s1 = make_dtnat_arr(chunksize + 5) s2 = make_dtnat_arr(chunksize + 5, 0) # s3=make_dtnjat_arr(chunksize+5,0) with ensure_clean('1.csv') as pth: df = DataFrame(dict(a=s1, b=s2)) df.to_csv(pth, chunksize=chunksize) recons = self.read_csv(pth)._convert(datetime=True, coerce=True) assert_frame_equal(df, recons, check_names=False, check_less_precise=True) @pytest.mark.slow def test_to_csv_moar(self): def _do_test(df, r_dtype=None, c_dtype=None, rnlvl=None, cnlvl=None, dupe_col=False): kwargs = dict(parse_dates=False) if cnlvl: if rnlvl is not None: kwargs['index_col'] = lrange(rnlvl) kwargs['header'] = lrange(cnlvl) with ensure_clean('__tmp_to_csv_moar__') as path: df.to_csv(path, encoding='utf8', chunksize=chunksize) recons = self.read_csv(path, **kwargs) else: kwargs['header'] = 0 with ensure_clean('__tmp_to_csv_moar__') as path: df.to_csv(path, encoding='utf8', chunksize=chunksize) recons = self.read_csv(path, **kwargs) def _to_uni(x): if not isinstance(x, compat.text_type): return x.decode('utf8') return x if dupe_col: # read_Csv disambiguates the columns by # labeling them dupe.1,dupe.2, etc'. monkey patch columns recons.columns = df.columns if rnlvl and not cnlvl: delta_lvl = [recons.iloc[ :, i].values for i in range(rnlvl - 1)] ix = MultiIndex.from_arrays([list(recons.index)] + delta_lvl) recons.index = ix recons = recons.iloc[:, rnlvl - 1:] type_map = dict(i='i', f='f', s='O', u='O', dt='O', p='O') if r_dtype: if r_dtype == 'u': # unicode r_dtype = 'O' recons.index = np.array(lmap(_to_uni, recons.index), dtype=r_dtype) df.index = np.array(lmap(_to_uni, df.index), dtype=r_dtype) elif r_dtype == 'dt': # unicode r_dtype = 'O' recons.index = np.array(lmap(Timestamp, recons.index), dtype=r_dtype) df.index = np.array( lmap(Timestamp, df.index), dtype=r_dtype) elif r_dtype == 'p': r_dtype = 'O' recons.index = np.array( list(map(Timestamp, to_datetime(recons.index))), dtype=r_dtype) df.index = np.array( list(map(Timestamp, df.index.to_timestamp())), dtype=r_dtype) else: r_dtype = type_map.get(r_dtype) recons.index = np.array(recons.index, dtype=r_dtype) df.index = np.array(df.index, dtype=r_dtype) if c_dtype: if c_dtype == 'u': c_dtype = 'O' recons.columns = np.array(lmap(_to_uni, recons.columns), dtype=c_dtype) df.columns = np.array( lmap(_to_uni, df.columns), dtype=c_dtype) elif c_dtype == 'dt': c_dtype = 'O' recons.columns = np.array(lmap(Timestamp, recons.columns), dtype=c_dtype) df.columns = np.array( lmap(Timestamp, df.columns), dtype=c_dtype) elif c_dtype == 'p': c_dtype = 'O' recons.columns = np.array( lmap(Timestamp, to_datetime(recons.columns)), dtype=c_dtype) df.columns = np.array( lmap(Timestamp, df.columns.to_timestamp()), dtype=c_dtype) else: c_dtype = type_map.get(c_dtype) recons.columns = np.array(recons.columns, dtype=c_dtype) df.columns = np.array(df.columns, dtype=c_dtype) assert_frame_equal(df, recons, check_names=False, check_less_precise=True) N = 100 chunksize = 1000 for ncols in [4]: base = int((chunksize // ncols or 1) or 1) for nrows in [2, 10, N - 1, N, N + 1, N + 2, 2 * N - 2, 2 * N - 1, 2 * N, 2 * N + 1, 2 * N + 2, base - 1, base, base + 1]: _do_test(mkdf(nrows, ncols, r_idx_type='dt', c_idx_type='s'), 'dt', 's') for ncols in [4]: base = int((chunksize // ncols or 1) or 1) for nrows in [2, 10, N - 1, N, N + 1, N + 2, 2 * N - 2, 2 * N - 1, 2 * N, 2 * N + 1, 2 * N + 2, base - 1, base, base + 1]: _do_test(mkdf(nrows, ncols, r_idx_type='dt', c_idx_type='s'), 'dt', 's') pass for r_idx_type, c_idx_type in [('i', 'i'), ('s', 's'), ('u', 'dt'), ('p', 'p')]: for ncols in [1, 2, 3, 4]: base = int((chunksize // ncols or 1) or 1) for nrows in [2, 10, N - 1, N, N + 1, N + 2, 2 * N - 2, 2 * N - 1, 2 * N, 2 * N + 1, 2 * N + 2, base - 1, base, base + 1]: _do_test(mkdf(nrows, ncols, r_idx_type=r_idx_type, c_idx_type=c_idx_type), r_idx_type, c_idx_type) for ncols in [1, 2, 3, 4]: base = int((chunksize // ncols or 1) or 1) for nrows in [10, N - 2, N - 1, N, N + 1, N + 2, 2 * N - 2, 2 * N - 1, 2 * N, 2 * N + 1, 2 * N + 2, base - 1, base, base + 1]: _do_test(mkdf(nrows, ncols)) for nrows in [10, N - 2, N - 1, N, N + 1, N + 2]: df = mkdf(nrows, 3) cols = list(df.columns) cols[:2] = ["dupe", "dupe"] cols[-2:] = ["dupe", "dupe"] ix = list(df.index) ix[:2] = ["rdupe", "rdupe"] ix[-2:] = ["rdupe", "rdupe"] df.index = ix df.columns = cols _do_test(df, dupe_col=True) _do_test(DataFrame(index=lrange(10))) _do_test(mkdf(chunksize // 2 + 1, 2, r_idx_nlevels=2), rnlvl=2) for ncols in [2, 3, 4]: base = int(chunksize // ncols) for nrows in [10, N - 2, N - 1, N, N + 1, N + 2, 2 * N - 2, 2 * N - 1, 2 * N, 2 * N + 1, 2 * N + 2, base - 1, base, base + 1]: _do_test(mkdf(nrows, ncols, r_idx_nlevels=2), rnlvl=2) _do_test(mkdf(nrows, ncols, c_idx_nlevels=2), cnlvl=2) _do_test(mkdf(nrows, ncols, r_idx_nlevels=2, c_idx_nlevels=2), rnlvl=2, cnlvl=2) def test_to_csv_from_csv_w_some_infs(self): # test roundtrip with inf, -inf, nan, as full columns and mix self.frame['G'] = np.nan f = lambda x: [np.inf, np.nan][np.random.rand() < .5] self.frame['H'] = self.frame.index.map(f) with ensure_clean() as path: self.frame.to_csv(path) recons = self.read_csv(path) # TODO to_csv drops column name assert_frame_equal(self.frame, recons, check_names=False) assert_frame_equal(np.isinf(self.frame), np.isinf(recons), check_names=False) def test_to_csv_from_csv_w_all_infs(self): # test roundtrip with inf, -inf, nan, as full columns and mix self.frame['E'] = np.inf self.frame['F'] = -np.inf with ensure_clean() as path: self.frame.to_csv(path) recons = self.read_csv(path) # TODO to_csv drops column name assert_frame_equal(self.frame, recons, check_names=False) assert_frame_equal(np.isinf(self.frame), np.isinf(recons), check_names=False) def test_to_csv_no_index(self): # GH 3624, after appending columns, to_csv fails with ensure_clean('__tmp_to_csv_no_index__') as path: df = DataFrame({'c1': [1, 2, 3], 'c2': [4, 5, 6]}) df.to_csv(path, index=False) result = read_csv(path) assert_frame_equal(df, result) df['c3'] = Series([7, 8, 9], dtype='int64') df.to_csv(path, index=False) result = read_csv(path) assert_frame_equal(df, result) def test_to_csv_with_mix_columns(self): # gh-11637: incorrect output when a mix of integer and string column # names passed as columns parameter in to_csv df = DataFrame({0: ['a', 'b', 'c'], 1: ['aa', 'bb', 'cc']}) df['test'] = 'txt' assert df.to_csv() == df.to_csv(columns=[0, 1, 'test']) def test_to_csv_headers(self): # GH6186, the presence or absence of `index` incorrectly # causes to_csv to have different header semantics. from_df = DataFrame([[1, 2], [3, 4]], columns=['A', 'B']) to_df = DataFrame([[1, 2], [3, 4]], columns=['X', 'Y']) with ensure_clean('__tmp_to_csv_headers__') as path: from_df.to_csv(path, header=['X', 'Y']) recons = self.read_csv(path) assert_frame_equal(to_df, recons) from_df.to_csv(path, index=False, header=['X', 'Y']) recons = self.read_csv(path) recons.reset_index(inplace=True) assert_frame_equal(to_df, recons) def test_to_csv_multiindex(self): frame = self.frame old_index = frame.index arrays = np.arange(len(old_index) * 2).reshape(2, -1) new_index = MultiIndex.from_arrays(arrays, names=['first', 'second']) frame.index = new_index with ensure_clean('__tmp_to_csv_multiindex__') as path: frame.to_csv(path, header=False) frame.to_csv(path, columns=['A', 'B']) # round trip frame.to_csv(path) df = self.read_csv(path, index_col=[0, 1], parse_dates=False) # TODO to_csv drops column name assert_frame_equal(frame, df, check_names=False) assert frame.index.names == df.index.names # needed if setUp becomes a class method self.frame.index = old_index # try multiindex with dates tsframe = self.tsframe old_index = tsframe.index new_index = [old_index, np.arange(len(old_index))] tsframe.index = MultiIndex.from_arrays(new_index) tsframe.to_csv(path, index_label=['time', 'foo']) recons = self.read_csv(path, index_col=[0, 1]) # TODO to_csv drops column name assert_frame_equal(tsframe, recons, check_names=False) # do not load index tsframe.to_csv(path) recons = self.read_csv(path, index_col=None) assert len(recons.columns) == len(tsframe.columns) + 2 # no index tsframe.to_csv(path, index=False) recons = self.read_csv(path, index_col=None) assert_almost_equal(recons.values, self.tsframe.values) # needed if setUp becomes class method self.tsframe.index = old_index with ensure_clean('__tmp_to_csv_multiindex__') as path: # GH3571, GH1651, GH3141 def _make_frame(names=None): if names is True: names = ['first', 'second'] return DataFrame(np.random.randint(0, 10, size=(3, 3)), columns=MultiIndex.from_tuples( [('bah', 'foo'), ('bah', 'bar'), ('ban', 'baz')], names=names), dtype='int64') # column & index are multi-index df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) df.to_csv(path) result = read_csv(path, header=[0, 1, 2, 3], index_col=[0, 1]) assert_frame_equal(df, result) # column is mi df = mkdf(5, 3, r_idx_nlevels=1, c_idx_nlevels=4) df.to_csv(path) result = read_csv( path, header=[0, 1, 2, 3], index_col=0) assert_frame_equal(df, result) # dup column names? df = mkdf(5, 3, r_idx_nlevels=3, c_idx_nlevels=4) df.to_csv(path) result = read_csv(path, header=[0, 1, 2, 3], index_col=[0, 1, 2]) assert_frame_equal(df, result) # writing with no index df = _make_frame() df.to_csv(path, index=False) result = read_csv(path, header=[0, 1]) assert_frame_equal(df, result) # we lose the names here df = _make_frame(True) df.to_csv(path, index=False) result = read_csv(path, header=[0, 1]) assert com._all_none(*result.columns.names) result.columns.names = df.columns.names assert_frame_equal(df, result) # tupleize_cols=True and index=False df = _make_frame(True) with tm.assert_produces_warning(FutureWarning): df.to_csv(path, tupleize_cols=True, index=False) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): result = read_csv(path, header=0, tupleize_cols=True, index_col=None) result.columns = df.columns assert_frame_equal(df, result) # whatsnew example df = _make_frame() df.to_csv(path) result = read_csv(path, header=[0, 1], index_col=[0]) assert_frame_equal(df, result) df = _make_frame(True) df.to_csv(path) result = read_csv(path, header=[0, 1], index_col=[0]) assert_frame_equal(df, result) # column & index are multi-index (compatibility) df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) with tm.assert_produces_warning(FutureWarning): df.to_csv(path, tupleize_cols=True) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): result = read_csv(path, header=0, index_col=[0, 1], tupleize_cols=True) result.columns = df.columns assert_frame_equal(df, result) # invalid options df = _make_frame(True) df.to_csv(path) for i in [6, 7]: msg = 'len of {i}, but only 5 lines in file'.format(i=i) with tm.assert_raises_regex(ParserError, msg): read_csv(path, header=lrange(i), index_col=0) # write with cols with tm.assert_raises_regex(TypeError, 'cannot specify cols ' 'with a MultiIndex'): df.to_csv(path, columns=['foo', 'bar']) with ensure_clean('__tmp_to_csv_multiindex__') as path: # empty tsframe[:0].to_csv(path) recons = self.read_csv(path) exp = tsframe[:0] exp.index = [] tm.assert_index_equal(recons.columns, exp.columns) assert len(recons) == 0 def test_to_csv_float32_nanrep(self): df = DataFrame(np.random.randn(1, 4).astype(np.float32)) df[1] = np.nan with ensure_clean('__tmp_to_csv_float32_nanrep__.csv') as path: df.to_csv(path, na_rep=999) with open(path) as f: lines = f.readlines() assert lines[1].split(',')[2] == '999' def test_to_csv_withcommas(self): # Commas inside fields should be correctly escaped when saving as CSV. df = DataFrame({'A': [1, 2, 3], 'B': ['5,6', '7,8', '9,0']}) with ensure_clean('__tmp_to_csv_withcommas__.csv') as path: df.to_csv(path) df2 = self.read_csv(path) assert_frame_equal(df2, df) def test_to_csv_mixed(self): def create_cols(name): return ["%s%03d" % (name, i) for i in range(5)] df_float = DataFrame(np.random.randn( 100, 5), dtype='float64', columns=create_cols('float')) df_int = DataFrame(np.random.randn(100, 5), dtype='int64', columns=create_cols('int')) df_bool = DataFrame(True, index=df_float.index, columns=create_cols('bool')) df_object = DataFrame('foo', index=df_float.index, columns=create_cols('object')) df_dt = DataFrame(Timestamp('20010101'), index=df_float.index, columns=create_cols('date')) # add in some nans df_float.loc[30:50, 1:3] = np.nan # ## this is a bug in read_csv right now #### # df_dt.loc[30:50,1:3] = np.nan df = pd.concat([df_float, df_int, df_bool, df_object, df_dt], axis=1) # dtype dtypes = dict() for n, dtype in [('float', np.float64), ('int', np.int64), ('bool', np.bool), ('object', np.object)]: for c in create_cols(n): dtypes[c] = dtype with ensure_clean() as filename: df.to_csv(filename) rs = read_csv(filename, index_col=0, dtype=dtypes, parse_dates=create_cols('date')) assert_frame_equal(rs, df) def test_to_csv_dups_cols(self): df = DataFrame(np.random.randn(1000, 30), columns=lrange( 15) + lrange(15), dtype='float64') with ensure_clean() as filename: df.to_csv(filename) # single dtype, fine result = read_csv(filename, index_col=0) result.columns = df.columns assert_frame_equal(result, df) df_float = DataFrame(np.random.randn(1000, 3), dtype='float64') df_int = DataFrame(np.random.randn(1000, 3), dtype='int64') df_bool = DataFrame(True, index=df_float.index, columns=lrange(3)) df_object = DataFrame('foo', index=df_float.index, columns=lrange(3)) df_dt = DataFrame(Timestamp('20010101'), index=df_float.index, columns=lrange(3)) df = pd.concat([df_float, df_int, df_bool, df_object, df_dt], axis=1, ignore_index=True) cols = [] for i in range(5): cols.extend([0, 1, 2]) df.columns = cols with ensure_clean() as filename: df.to_csv(filename) result = read_csv(filename, index_col=0) # date cols for i in ['0.4', '1.4', '2.4']: result[i] = to_datetime(result[i]) result.columns = df.columns assert_frame_equal(result, df) # GH3457 from pandas.util.testing import makeCustomDataframe as mkdf N = 10 df = mkdf(N, 3) df.columns = ['a', 'a', 'b'] with ensure_clean() as filename: df.to_csv(filename) # read_csv will rename the dups columns result = read_csv(filename, index_col=0) result = result.rename(columns={'a.1': 'a'}) assert_frame_equal(result, df) def test_to_csv_chunking(self): aa = DataFrame({'A': lrange(100000)}) aa['B'] = aa.A + 1.0 aa['C'] = aa.A + 2.0 aa['D'] = aa.A + 3.0 for chunksize in [10000, 50000, 100000]: with ensure_clean() as filename: aa.to_csv(filename, chunksize=chunksize) rs = read_csv(filename, index_col=0) assert_frame_equal(rs, aa) @pytest.mark.slow def test_to_csv_wide_frame_formatting(self): # Issue #8621 df = DataFrame(np.random.randn(1, 100010), columns=None, index=None) with ensure_clean() as filename: df.to_csv(filename, header=False, index=False) rs = read_csv(filename, header=None) assert_frame_equal(rs, df) def test_to_csv_bug(self): f1 = StringIO('a,1.0\nb,2.0') df = self.read_csv(f1, header=None) newdf = DataFrame({'t': df[df.columns[0]]}) with ensure_clean() as path: newdf.to_csv(path) recons = read_csv(path, index_col=0) # don't check_names as t != 1 assert_frame_equal(recons, newdf, check_names=False) def test_to_csv_unicode(self): df = DataFrame({u('c/\u03c3'): [1, 2, 3]}) with ensure_clean() as path: df.to_csv(path, encoding='UTF-8') df2 = read_csv(path, index_col=0, encoding='UTF-8') assert_frame_equal(df, df2) df.to_csv(path, encoding='UTF-8', index=False) df2 = read_csv(path, index_col=None, encoding='UTF-8') assert_frame_equal(df, df2) def test_to_csv_unicode_index_col(self): buf = StringIO('') df = DataFrame( [[u("\u05d0"), "d2", "d3", "d4"], ["a1", "a2", "a3", "a4"]], columns=[u("\u05d0"), u("\u05d1"), u("\u05d2"), u("\u05d3")], index=[u("\u05d0"), u("\u05d1")]) df.to_csv(buf, encoding='UTF-8') buf.seek(0) df2 = read_csv(buf, index_col=0, encoding='UTF-8') assert_frame_equal(df, df2) def test_to_csv_stringio(self): buf = StringIO() self.frame.to_csv(buf) buf.seek(0) recons = read_csv(buf, index_col=0) # TODO to_csv drops column name assert_frame_equal(recons, self.frame, check_names=False) def test_to_csv_float_format(self): df = DataFrame([[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]], index=['A', 'B'], columns=['X', 'Y', 'Z']) with ensure_clean() as filename: df.to_csv(filename, float_format='%.2f') rs = read_csv(filename, index_col=0) xp = DataFrame([[0.12, 0.23, 0.57], [12.32, 123123.20, 321321.20]], index=['A', 'B'], columns=['X', 'Y', 'Z']) assert_frame_equal(rs, xp) def test_to_csv_unicodewriter_quoting(self): df = DataFrame({'A': [1, 2, 3], 'B': ['foo', 'bar', 'baz']}) buf = StringIO() df.to_csv(buf, index=False, quoting=csv.QUOTE_NONNUMERIC, encoding='utf-8') result = buf.getvalue() expected = ('"A","B"\n' '1,"foo"\n' '2,"bar"\n' '3,"baz"\n') assert result == expected def test_to_csv_quote_none(self): # GH4328 df = DataFrame({'A': ['hello', '{"hello"}']}) for encoding in (None, 'utf-8'): buf = StringIO() df.to_csv(buf, quoting=csv.QUOTE_NONE, encoding=encoding, index=False) result = buf.getvalue() expected = 'A\nhello\n{"hello"}\n' assert result == expected def test_to_csv_index_no_leading_comma(self): df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['one', 'two', 'three']) buf = StringIO() df.to_csv(buf, index_label=False) expected = ('A,B\n' 'one,1,4\n' 'two,2,5\n' 'three,3,6\n') assert buf.getvalue() == expected def test_to_csv_line_terminators(self): df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['one', 'two', 'three']) buf = StringIO() df.to_csv(buf, line_terminator='\r\n') expected = (',A,B\r\n' 'one,1,4\r\n' 'two,2,5\r\n' 'three,3,6\r\n') assert buf.getvalue() == expected buf = StringIO() df.to_csv(buf) # The default line terminator remains \n expected = (',A,B\n' 'one,1,4\n' 'two,2,5\n' 'three,3,6\n') assert buf.getvalue() == expected def test_to_csv_from_csv_categorical(self): # CSV with categoricals should result in the same output # as when one would add a "normal" Series/DataFrame. s = Series(pd.Categorical(["a", "b", "b", "a", "a", "c", "c", "c"])) s2 = Series(["a", "b", "b", "a", "a", "c", "c", "c"]) res = StringIO() s.to_csv(res, header=False) exp = StringIO() s2.to_csv(exp, header=False) assert res.getvalue() == exp.getvalue() df = DataFrame({"s": s}) df2 = DataFrame({"s": s2}) res = StringIO() df.to_csv(res) exp = StringIO() df2.to_csv(exp) assert res.getvalue() == exp.getvalue() def test_to_csv_path_is_none(self): # GH 8215 # Make sure we return string for consistency with # Series.to_csv() csv_str = self.frame.to_csv(path_or_buf=None) assert isinstance(csv_str, str) recons = pd.read_csv(StringIO(csv_str), index_col=0) assert_frame_equal(self.frame, recons) @pytest.mark.parametrize('df,encoding', [ (DataFrame([[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]], index=['A', 'B'], columns=['X', 'Y', 'Z']), None), # GH 21241, 21118 (DataFrame([['abc', 'def', 'ghi']], columns=['X', 'Y', 'Z']), 'ascii'), (DataFrame(5 * [[123, u"你好", u"世界"]], columns=['X', 'Y', 'Z']), 'gb2312'), (DataFrame(5 * [[123, u"Γειά σου", u"Κόσμε"]], columns=['X', 'Y', 'Z']), 'cp737') ]) def test_to_csv_compression(self, df, encoding, compression): with ensure_clean() as filename: df.to_csv(filename, compression=compression, encoding=encoding) # test the round trip - to_csv -> read_csv result = read_csv(filename, compression=compression, index_col=0, encoding=encoding) assert_frame_equal(df, result) # test the round trip using file handle - to_csv -> read_csv f, _handles = _get_handle(filename, 'w', compression=compression, encoding=encoding) with f: df.to_csv(f, encoding=encoding) result = pd.read_csv(filename, compression=compression, encoding=encoding, index_col=0, squeeze=True) assert_frame_equal(df, result) # explicitly make sure file is compressed with tm.decompress_file(filename, compression) as fh: text = fh.read().decode(encoding or 'utf8') for col in df.columns: assert col in text with tm.decompress_file(filename, compression) as fh: assert_frame_equal(df, read_csv(fh, index_col=0, encoding=encoding)) def test_to_csv_date_format(self): with ensure_clean('__tmp_to_csv_date_format__') as path: dt_index = self.tsframe.index datetime_frame = DataFrame( {'A': dt_index, 'B': dt_index.shift(1)}, index=dt_index) datetime_frame.to_csv(path, date_format='%Y%m%d') # Check that the data was put in the specified format test = read_csv(path, index_col=0) datetime_frame_int = datetime_frame.applymap( lambda x: int(x.strftime('%Y%m%d'))) datetime_frame_int.index = datetime_frame_int.index.map( lambda x: int(x.strftime('%Y%m%d'))) assert_frame_equal(test, datetime_frame_int) datetime_frame.to_csv(path, date_format='%Y-%m-%d') # Check that the data was put in the specified format test = read_csv(path, index_col=0) datetime_frame_str = datetime_frame.applymap( lambda x: x.strftime('%Y-%m-%d')) datetime_frame_str.index = datetime_frame_str.index.map( lambda x: x.strftime('%Y-%m-%d')) assert_frame_equal(test, datetime_frame_str) # Check that columns get converted datetime_frame_columns = datetime_frame.T datetime_frame_columns.to_csv(path, date_format='%Y%m%d') test = read_csv(path, index_col=0) datetime_frame_columns = datetime_frame_columns.applymap( lambda x: int(x.strftime('%Y%m%d'))) # Columns don't get converted to ints by read_csv datetime_frame_columns.columns = ( datetime_frame_columns.columns .map(lambda x: x.strftime('%Y%m%d'))) assert_frame_equal(test, datetime_frame_columns) # test NaTs nat_index = to_datetime( ['NaT'] * 10 + ['2000-01-01', '1/1/2000', '1-1-2000']) nat_frame = DataFrame({'A': nat_index}, index=nat_index) nat_frame.to_csv(path, date_format='%Y-%m-%d') test = read_csv(path, parse_dates=[0, 1], index_col=0) assert_frame_equal(test, nat_frame) def test_to_csv_with_dst_transitions(self): with ensure_clean('csv_date_format_with_dst') as path: # make sure we are not failing on transitions times = pd.date_range("2013-10-26 23:00", "2013-10-27 01:00", tz="Europe/London", freq="H", ambiguous='infer') for i in [times, times + pd.Timedelta('10s')]: time_range = np.array(range(len(i)), dtype='int64') df = DataFrame({'A': time_range}, index=i) df.to_csv(path, index=True) # we have to reconvert the index as we # don't parse the tz's result = read_csv(path, index_col=0) result.index = to_datetime(result.index, utc=True).tz_convert( 'Europe/London') assert_frame_equal(result, df) # GH11619 idx = pd.date_range('2015-01-01', '2015-12-31', freq='H', tz='Europe/Paris') df = DataFrame({'values': 1, 'idx': idx}, index=idx) with ensure_clean('csv_date_format_with_dst') as path: df.to_csv(path, index=True) result = read_csv(path, index_col=0) result.index = to_datetime(result.index, utc=True).tz_convert( 'Europe/Paris') result['idx'] = to_datetime(result['idx'], utc=True).astype( 'datetime64[ns, Europe/Paris]') assert_frame_equal(result, df) # assert working df.astype(str) with ensure_clean('csv_date_format_with_dst') as path: df.to_pickle(path) result = pd.read_pickle(path) assert_frame_equal(result, df) def test_to_csv_quoting(self): df = DataFrame({ 'c_bool': [True, False], 'c_float': [1.0, 3.2], 'c_int': [42, np.nan], 'c_string': ['a', 'b,c'], }) expected = """\ ,c_bool,c_float,c_int,c_string 0,True,1.0,42.0,a 1,False,3.2,,"b,c" """ result = df.to_csv() assert result == expected result = df.to_csv(quoting=None) assert result == expected result = df.to_csv(quoting=csv.QUOTE_MINIMAL) assert result == expected expected = """\ "","c_bool","c_float","c_int","c_string" "0","True","1.0","42.0","a" "1","False","3.2","","b,c" """ result = df.to_csv(quoting=csv.QUOTE_ALL) assert result == expected # see gh-12922, gh-13259: make sure changes to # the formatters do not break this behaviour expected = """\ "","c_bool","c_float","c_int","c_string" 0,True,1.0,42.0,"a" 1,False,3.2,"","b,c" """ result = df.to_csv(quoting=csv.QUOTE_NONNUMERIC) assert result == expected msg = "need to escape, but no escapechar set" tm.assert_raises_regex(csv.Error, msg, df.to_csv, quoting=csv.QUOTE_NONE) tm.assert_raises_regex(csv.Error, msg, df.to_csv, quoting=csv.QUOTE_NONE, escapechar=None) expected = """\ ,c_bool,c_float,c_int,c_string 0,True,1.0,42.0,a 1,False,3.2,,b!,c """ result = df.to_csv(quoting=csv.QUOTE_NONE, escapechar='!') assert result == expected expected = """\ ,c_bool,c_ffloat,c_int,c_string 0,True,1.0,42.0,a 1,False,3.2,,bf,c """ result = df.to_csv(quoting=csv.QUOTE_NONE, escapechar='f') assert result == expected # see gh-3503: quoting Windows line terminators # presents with encoding? text = 'a,b,c\n1,"test \r\n",3\n' df = pd.read_csv(StringIO(text)) buf = StringIO() df.to_csv(buf, encoding='utf-8', index=False) assert buf.getvalue() == text # xref gh-7791: make sure the quoting parameter is passed through # with multi-indexes df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]}) df = df.set_index(['a', 'b']) expected = '"a","b","c"\n"1","3","5"\n"2","4","6"\n' assert df.to_csv(quoting=csv.QUOTE_ALL) == expected def test_period_index_date_overflow(self): # see gh-15982 dates = ["1990-01-01", "2000-01-01", "3005-01-01"] index = pd.PeriodIndex(dates, freq="D") df = pd.DataFrame([4, 5, 6], index=index) result = df.to_csv() expected = ',0\n1990-01-01,4\n2000-01-01,5\n3005-01-01,6\n' assert result == expected date_format = "%m-%d-%Y" result = df.to_csv(date_format=date_format) expected = ',0\n01-01-1990,4\n01-01-2000,5\n01-01-3005,6\n' assert result == expected # Overflow with pd.NaT dates = ["1990-01-01", pd.NaT, "3005-01-01"] index = pd.PeriodIndex(dates, freq="D") df = pd.DataFrame([4, 5, 6], index=index) result = df.to_csv() expected = ',0\n1990-01-01,4\n,5\n3005-01-01,6\n' assert result == expected def test_multi_index_header(self): # see gh-5539 columns = pd.MultiIndex.from_tuples([("a", 1), ("a", 2), ("b", 1), ("b", 2)]) df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]]) df.columns = columns header = ["a", "b", "c", "d"] result = df.to_csv(header=header) expected = ",a,b,c,d\n0,1,2,3,4\n1,5,6,7,8\n" assert result == expected
bsd-3-clause
Lawrence-Liu/scikit-learn
examples/mixture/plot_gmm_sin.py
248
2747
""" ================================= Gaussian Mixture Model Sine Curve ================================= This example highlights the advantages of the Dirichlet Process: complexity control and dealing with sparse data. The dataset is formed by 100 points loosely spaced following a noisy sine curve. The fit by the GMM class, using the expectation-maximization algorithm to fit a mixture of 10 Gaussian components, finds too-small components and very little structure. The fits by the Dirichlet process, however, show that the model can either learn a global structure for the data (small alpha) or easily interpolate to finding relevant local structure (large alpha), never falling into the problems shown by the GMM class. """ import itertools import numpy as np from scipy import linalg import matplotlib.pyplot as plt import matplotlib as mpl from sklearn import mixture from sklearn.externals.six.moves import xrange # Number of samples per component n_samples = 100 # Generate random sample following a sine curve np.random.seed(0) X = np.zeros((n_samples, 2)) step = 4 * np.pi / n_samples for i in xrange(X.shape[0]): x = i * step - 6 X[i, 0] = x + np.random.normal(0, 0.1) X[i, 1] = 3 * (np.sin(x) + np.random.normal(0, .2)) color_iter = itertools.cycle(['r', 'g', 'b', 'c', 'm']) for i, (clf, title) in enumerate([ (mixture.GMM(n_components=10, covariance_type='full', n_iter=100), "Expectation-maximization"), (mixture.DPGMM(n_components=10, covariance_type='full', alpha=0.01, n_iter=100), "Dirichlet Process,alpha=0.01"), (mixture.DPGMM(n_components=10, covariance_type='diag', alpha=100., n_iter=100), "Dirichlet Process,alpha=100.")]): clf.fit(X) splot = plt.subplot(3, 1, 1 + i) Y_ = clf.predict(X) for i, (mean, covar, color) in enumerate(zip( clf.means_, clf._get_covars(), color_iter)): v, w = linalg.eigh(covar) u = w[0] / linalg.norm(w[0]) # as the DP will not use every component it has access to # unless it needs it, we shouldn't plot the redundant # components. 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.arctan(u[1] / u[0]) angle = 180 * angle / np.pi # convert to degrees ell = mpl.patches.Ellipse(mean, v[0], v[1], 180 + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(0.5) splot.add_artist(ell) plt.xlim(-6, 4 * np.pi - 6) plt.ylim(-5, 5) plt.title(title) plt.xticks(()) plt.yticks(()) plt.show()
bsd-3-clause
elijah513/scikit-learn
examples/linear_model/plot_sgd_separating_hyperplane.py
260
1219
""" ========================================= SGD: Maximum margin separating hyperplane ========================================= Plot the maximum margin separating hyperplane within a two-class separable dataset using a linear Support Vector Machines classifier trained using SGD. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import SGDClassifier from sklearn.datasets.samples_generator import make_blobs # we create 50 separable points X, Y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60) # fit the model clf = SGDClassifier(loss="hinge", alpha=0.01, n_iter=200, fit_intercept=True) clf.fit(X, Y) # plot the line, the points, and the nearest vectors to the plane xx = np.linspace(-1, 5, 10) yy = np.linspace(-1, 5, 10) X1, X2 = np.meshgrid(xx, yy) Z = np.empty(X1.shape) for (i, j), val in np.ndenumerate(X1): x1 = val x2 = X2[i, j] p = clf.decision_function([x1, x2]) Z[i, j] = p[0] levels = [-1.0, 0.0, 1.0] linestyles = ['dashed', 'solid', 'dashed'] colors = 'k' plt.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles) plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired) plt.axis('tight') plt.show()
bsd-3-clause
untom/scikit-learn
sklearn/datasets/lfw.py
38
19042
"""Loader for the Labeled Faces in the Wild (LFW) dataset This dataset is a collection of JPEG pictures of famous people collected over the internet, all details are available on the official website: http://vis-www.cs.umass.edu/lfw/ Each picture is centered on a single face. The typical task is called Face Verification: given a pair of two pictures, a binary classifier must predict whether the two images are from the same person. An alternative task, Face Recognition or Face Identification is: given the picture of the face of an unknown person, identify the name of the person by referring to a gallery of previously seen pictures of identified persons. Both Face Verification and Face Recognition are tasks that are typically performed on the output of a model trained to perform Face Detection. The most popular model for Face Detection is called Viola-Johns and is implemented in the OpenCV library. The LFW faces were extracted by this face detector from various online websites. """ # Copyright (c) 2011 Olivier Grisel <[email protected]> # License: BSD 3 clause from os import listdir, makedirs, remove from os.path import join, exists, isdir from sklearn.utils import deprecated import logging import numpy as np try: import urllib.request as urllib # for backwards compatibility except ImportError: import urllib from .base import get_data_home, Bunch from ..externals.joblib import Memory from ..externals.six import b logger = logging.getLogger(__name__) BASE_URL = "http://vis-www.cs.umass.edu/lfw/" ARCHIVE_NAME = "lfw.tgz" FUNNELED_ARCHIVE_NAME = "lfw-funneled.tgz" TARGET_FILENAMES = [ 'pairsDevTrain.txt', 'pairsDevTest.txt', 'pairs.txt', ] def scale_face(face): """Scale back to 0-1 range in case of normalization for plotting""" scaled = face - face.min() scaled /= scaled.max() return scaled # # Common private utilities for data fetching from the original LFW website # local disk caching, and image decoding. # def check_fetch_lfw(data_home=None, funneled=True, download_if_missing=True): """Helper function to download any missing LFW data""" data_home = get_data_home(data_home=data_home) lfw_home = join(data_home, "lfw_home") if funneled: archive_path = join(lfw_home, FUNNELED_ARCHIVE_NAME) data_folder_path = join(lfw_home, "lfw_funneled") archive_url = BASE_URL + FUNNELED_ARCHIVE_NAME else: archive_path = join(lfw_home, ARCHIVE_NAME) data_folder_path = join(lfw_home, "lfw") archive_url = BASE_URL + ARCHIVE_NAME if not exists(lfw_home): makedirs(lfw_home) for target_filename in TARGET_FILENAMES: target_filepath = join(lfw_home, target_filename) if not exists(target_filepath): if download_if_missing: url = BASE_URL + target_filename logger.warn("Downloading LFW metadata: %s", url) urllib.urlretrieve(url, target_filepath) else: raise IOError("%s is missing" % target_filepath) if not exists(data_folder_path): if not exists(archive_path): if download_if_missing: logger.warn("Downloading LFW data (~200MB): %s", archive_url) urllib.urlretrieve(archive_url, archive_path) else: raise IOError("%s is missing" % target_filepath) import tarfile logger.info("Decompressing the data archive to %s", data_folder_path) tarfile.open(archive_path, "r:gz").extractall(path=lfw_home) remove(archive_path) return lfw_home, data_folder_path def _load_imgs(file_paths, slice_, color, resize): """Internally used to load images""" # Try to import imread and imresize from PIL. We do this here to prevent # the whole sklearn.datasets module from depending on PIL. try: try: from scipy.misc import imread except ImportError: from scipy.misc.pilutil import imread from scipy.misc import imresize except ImportError: raise ImportError("The Python Imaging Library (PIL)" " is required to load data from jpeg files") # compute the portion of the images to load to respect the slice_ parameter # given by the caller default_slice = (slice(0, 250), slice(0, 250)) if slice_ is None: slice_ = default_slice else: slice_ = tuple(s or ds for s, ds in zip(slice_, default_slice)) h_slice, w_slice = slice_ h = (h_slice.stop - h_slice.start) // (h_slice.step or 1) w = (w_slice.stop - w_slice.start) // (w_slice.step or 1) if resize is not None: resize = float(resize) h = int(resize * h) w = int(resize * w) # allocate some contiguous memory to host the decoded image slices n_faces = len(file_paths) if not color: faces = np.zeros((n_faces, h, w), dtype=np.float32) else: faces = np.zeros((n_faces, h, w, 3), dtype=np.float32) # iterate over the collected file path to load the jpeg files as numpy # arrays for i, file_path in enumerate(file_paths): if i % 1000 == 0: logger.info("Loading face #%05d / %05d", i + 1, n_faces) face = np.asarray(imread(file_path)[slice_], dtype=np.float32) face /= 255.0 # scale uint8 coded colors to the [0.0, 1.0] floats if resize is not None: face = imresize(face, resize) if not color: # average the color channels to compute a gray levels # representaion face = face.mean(axis=2) faces[i, ...] = face return faces # # Task #1: Face Identification on picture with names # def _fetch_lfw_people(data_folder_path, slice_=None, color=False, resize=None, min_faces_per_person=0): """Perform the actual data loading for the lfw people dataset This operation is meant to be cached by a joblib wrapper. """ # scan the data folder content to retain people with more that # `min_faces_per_person` face pictures person_names, file_paths = [], [] for person_name in sorted(listdir(data_folder_path)): folder_path = join(data_folder_path, person_name) if not isdir(folder_path): continue paths = [join(folder_path, f) for f in listdir(folder_path)] n_pictures = len(paths) if n_pictures >= min_faces_per_person: person_name = person_name.replace('_', ' ') person_names.extend([person_name] * n_pictures) file_paths.extend(paths) n_faces = len(file_paths) if n_faces == 0: raise ValueError("min_faces_per_person=%d is too restrictive" % min_faces_per_person) target_names = np.unique(person_names) target = np.searchsorted(target_names, person_names) faces = _load_imgs(file_paths, slice_, color, resize) # shuffle the faces with a deterministic RNG scheme to avoid having # all faces of the same person in a row, as it would break some # cross validation and learning algorithms such as SGD and online # k-means that make an IID assumption indices = np.arange(n_faces) np.random.RandomState(42).shuffle(indices) faces, target = faces[indices], target[indices] return faces, target, target_names def fetch_lfw_people(data_home=None, funneled=True, resize=0.5, min_faces_per_person=0, color=False, slice_=(slice(70, 195), slice(78, 172)), download_if_missing=True): """Loader for the Labeled Faces in the Wild (LFW) people dataset This dataset is a collection of JPEG pictures of famous people collected on the internet, all details are available on the official website: http://vis-www.cs.umass.edu/lfw/ Each picture is centered on a single face. Each pixel of each channel (color in RGB) is encoded by a float in range 0.0 - 1.0. The task is called Face Recognition (or Identification): given the picture of a face, find the name of the person given a training set (gallery). The original images are 250 x 250 pixels, but the default slice and resize arguments reduce them to 62 x 74. 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. funneled : boolean, optional, default: True Download and use the funneled variant of the dataset. resize : float, optional, default 0.5 Ratio used to resize the each face picture. min_faces_per_person : int, optional, default None The extracted dataset will only retain pictures of people that have at least `min_faces_per_person` different pictures. color : boolean, optional, default False Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than than the shape with color = False. slice_ : optional Provide a custom 2D slice (height, width) to extract the 'interesting' part of the jpeg files and avoid use statistical correlation from the background 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. Returns ------- dataset : dict-like object with the following attributes: dataset.data : numpy array of shape (13233, 2914) Each row corresponds to a ravelled face image of original size 62 x 47 pixels. Changing the ``slice_`` or resize parameters will change the shape of the output. dataset.images : numpy array of shape (13233, 62, 47) Each row is a face image corresponding to one of the 5749 people in the dataset. Changing the ``slice_`` or resize parameters will change the shape of the output. dataset.target : numpy array of shape (13233,) Labels associated to each face image. Those labels range from 0-5748 and correspond to the person IDs. dataset.DESCR : string Description of the Labeled Faces in the Wild (LFW) dataset. """ lfw_home, data_folder_path = check_fetch_lfw( data_home=data_home, funneled=funneled, download_if_missing=download_if_missing) logger.info('Loading LFW people faces from %s', lfw_home) # wrap the loader in a memoizing function that will return memmaped data # arrays for optimal memory usage m = Memory(cachedir=lfw_home, compress=6, verbose=0) load_func = m.cache(_fetch_lfw_people) # load and memoize the pairs as np arrays faces, target, target_names = load_func( data_folder_path, resize=resize, min_faces_per_person=min_faces_per_person, color=color, slice_=slice_) # pack the results as a Bunch instance return Bunch(data=faces.reshape(len(faces), -1), images=faces, target=target, target_names=target_names, DESCR="LFW faces dataset") # # Task #2: Face Verification on pairs of face pictures # def _fetch_lfw_pairs(index_file_path, data_folder_path, slice_=None, color=False, resize=None): """Perform the actual data loading for the LFW pairs dataset This operation is meant to be cached by a joblib wrapper. """ # parse the index file to find the number of pairs to be able to allocate # the right amount of memory before starting to decode the jpeg files with open(index_file_path, 'rb') as index_file: split_lines = [ln.strip().split(b('\t')) for ln in index_file] pair_specs = [sl for sl in split_lines if len(sl) > 2] n_pairs = len(pair_specs) # interating over the metadata lines for each pair to find the filename to # decode and load in memory target = np.zeros(n_pairs, dtype=np.int) file_paths = list() for i, components in enumerate(pair_specs): if len(components) == 3: target[i] = 1 pair = ( (components[0], int(components[1]) - 1), (components[0], int(components[2]) - 1), ) elif len(components) == 4: target[i] = 0 pair = ( (components[0], int(components[1]) - 1), (components[2], int(components[3]) - 1), ) else: raise ValueError("invalid line %d: %r" % (i + 1, components)) for j, (name, idx) in enumerate(pair): try: person_folder = join(data_folder_path, name) except TypeError: person_folder = join(data_folder_path, str(name, 'UTF-8')) filenames = list(sorted(listdir(person_folder))) file_path = join(person_folder, filenames[idx]) file_paths.append(file_path) pairs = _load_imgs(file_paths, slice_, color, resize) shape = list(pairs.shape) n_faces = shape.pop(0) shape.insert(0, 2) shape.insert(0, n_faces // 2) pairs.shape = shape return pairs, target, np.array(['Different persons', 'Same person']) @deprecated("Function 'load_lfw_people' has been deprecated in 0.17 and will be " "removed in 0.19." "Use fetch_lfw_people(download_if_missing=False) instead.") def load_lfw_people(download_if_missing=False, **kwargs): """Alias for fetch_lfw_people(download_if_missing=False) Check fetch_lfw_people.__doc__ for the documentation and parameter list. """ return fetch_lfw_people(download_if_missing=download_if_missing, **kwargs) def fetch_lfw_pairs(subset='train', data_home=None, funneled=True, resize=0.5, color=False, slice_=(slice(70, 195), slice(78, 172)), download_if_missing=True): """Loader for the Labeled Faces in the Wild (LFW) pairs dataset This dataset is a collection of JPEG pictures of famous people collected on the internet, all details are available on the official website: http://vis-www.cs.umass.edu/lfw/ Each picture is centered on a single face. Each pixel of each channel (color in RGB) is encoded by a float in range 0.0 - 1.0. The task is called Face Verification: given a pair of two pictures, a binary classifier must predict whether the two images are from the same person. In the official `README.txt`_ this task is described as the "Restricted" task. As I am not sure as to implement the "Unrestricted" variant correctly, I left it as unsupported for now. .. _`README.txt`: http://vis-www.cs.umass.edu/lfw/README.txt The original images are 250 x 250 pixels, but the default slice and resize arguments reduce them to 62 x 74. Read more in the :ref:`User Guide <labeled_faces_in_the_wild>`. Parameters ---------- subset : optional, default: 'train' Select the dataset to load: 'train' for the development training set, 'test' for the development test set, and '10_folds' for the official evaluation set that is meant to be used with a 10-folds cross validation. 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. funneled : boolean, optional, default: True Download and use the funneled variant of the dataset. resize : float, optional, default 0.5 Ratio used to resize the each face picture. color : boolean, optional, default False Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than than the shape with color = False. slice_ : optional Provide a custom 2D slice (height, width) to extract the 'interesting' part of the jpeg files and avoid use statistical correlation from the background 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. Returns ------- The data is returned as a Bunch object with the following attributes: data : numpy array of shape (2200, 5828) Each row corresponds to 2 ravel'd face images of original size 62 x 47 pixels. Changing the ``slice_`` or resize parameters will change the shape of the output. pairs : numpy array of shape (2200, 2, 62, 47) Each row has 2 face images corresponding to same or different person from the dataset containing 5749 people. Changing the ``slice_`` or resize parameters will change the shape of the output. target : numpy array of shape (13233,) Labels associated to each pair of images. The two label values being different persons or the same person. DESCR : string Description of the Labeled Faces in the Wild (LFW) dataset. """ lfw_home, data_folder_path = check_fetch_lfw( data_home=data_home, funneled=funneled, download_if_missing=download_if_missing) logger.info('Loading %s LFW pairs from %s', subset, lfw_home) # wrap the loader in a memoizing function that will return memmaped data # arrays for optimal memory usage m = Memory(cachedir=lfw_home, compress=6, verbose=0) load_func = m.cache(_fetch_lfw_pairs) # select the right metadata file according to the requested subset label_filenames = { 'train': 'pairsDevTrain.txt', 'test': 'pairsDevTest.txt', '10_folds': 'pairs.txt', } if subset not in label_filenames: raise ValueError("subset='%s' is invalid: should be one of %r" % ( subset, list(sorted(label_filenames.keys())))) index_file_path = join(lfw_home, label_filenames[subset]) # load and memoize the pairs as np arrays pairs, target, target_names = load_func( index_file_path, data_folder_path, resize=resize, color=color, slice_=slice_) # pack the results as a Bunch instance return Bunch(data=pairs.reshape(len(pairs), -1), pairs=pairs, target=target, target_names=target_names, DESCR="'%s' segment of the LFW pairs dataset" % subset) @deprecated("Function 'load_lfw_pairs' has been deprecated in 0.17 and will be " "removed in 0.19." "Use fetch_lfw_pairs(download_if_missing=False) instead.") def load_lfw_pairs(download_if_missing=False, **kwargs): """Alias for fetch_lfw_pairs(download_if_missing=False) Check fetch_lfw_pairs.__doc__ for the documentation and parameter list. """ return fetch_lfw_pairs(download_if_missing=download_if_missing, **kwargs)
bsd-3-clause
ldirer/scikit-learn
sklearn/feature_extraction/image.py
21
18105
""" The :mod:`sklearn.feature_extraction.image` submodule gathers utilities to extract features from images. """ # Authors: Emmanuelle Gouillart <[email protected]> # Gael Varoquaux <[email protected]> # Olivier Grisel # Vlad Niculae # License: BSD 3 clause from itertools import product import numbers import numpy as np from scipy import sparse from numpy.lib.stride_tricks import as_strided from ..utils import check_array, check_random_state from ..base import BaseEstimator __all__ = ['PatchExtractor', 'extract_patches_2d', 'grid_to_graph', 'img_to_graph', 'reconstruct_from_patches_2d'] ############################################################################### # From an image to a graph def _make_edges_3d(n_x, n_y, n_z=1): """Returns a list of edges for a 3D image. Parameters =========== n_x : integer The size of the grid in the x direction. n_y : integer The size of the grid in the y direction. n_z : integer, optional The size of the grid in the z direction, defaults to 1 """ vertices = np.arange(n_x * n_y * n_z).reshape((n_x, n_y, n_z)) edges_deep = np.vstack((vertices[:, :, :-1].ravel(), vertices[:, :, 1:].ravel())) edges_right = np.vstack((vertices[:, :-1].ravel(), vertices[:, 1:].ravel())) edges_down = np.vstack((vertices[:-1].ravel(), vertices[1:].ravel())) edges = np.hstack((edges_deep, edges_right, edges_down)) return edges def _compute_gradient_3d(edges, img): n_x, n_y, n_z = img.shape gradient = np.abs(img[edges[0] // (n_y * n_z), (edges[0] % (n_y * n_z)) // n_z, (edges[0] % (n_y * n_z)) % n_z] - img[edges[1] // (n_y * n_z), (edges[1] % (n_y * n_z)) // n_z, (edges[1] % (n_y * n_z)) % n_z]) return gradient # XXX: Why mask the image after computing the weights? def _mask_edges_weights(mask, edges, weights=None): """Apply a mask to edges (weighted or not)""" inds = np.arange(mask.size) inds = inds[mask.ravel()] ind_mask = np.logical_and(np.in1d(edges[0], inds), np.in1d(edges[1], inds)) edges = edges[:, ind_mask] if weights is not None: weights = weights[ind_mask] if len(edges.ravel()): maxval = edges.max() else: maxval = 0 order = np.searchsorted(np.unique(edges.ravel()), np.arange(maxval + 1)) edges = order[edges] if weights is None: return edges else: return edges, weights def _to_graph(n_x, n_y, n_z, mask=None, img=None, return_as=sparse.coo_matrix, dtype=None): """Auxiliary function for img_to_graph and grid_to_graph """ edges = _make_edges_3d(n_x, n_y, n_z) if dtype is None: if img is None: dtype = np.int else: dtype = img.dtype if img is not None: img = np.atleast_3d(img) weights = _compute_gradient_3d(edges, img) if mask is not None: edges, weights = _mask_edges_weights(mask, edges, weights) diag = img.squeeze()[mask] else: diag = img.ravel() n_voxels = diag.size else: if mask is not None: mask = mask.astype(dtype=np.bool, copy=False) mask = np.asarray(mask, dtype=np.bool) edges = _mask_edges_weights(mask, edges) n_voxels = np.sum(mask) else: n_voxels = n_x * n_y * n_z weights = np.ones(edges.shape[1], dtype=dtype) diag = np.ones(n_voxels, dtype=dtype) diag_idx = np.arange(n_voxels) i_idx = np.hstack((edges[0], edges[1])) j_idx = np.hstack((edges[1], edges[0])) graph = sparse.coo_matrix((np.hstack((weights, weights, diag)), (np.hstack((i_idx, diag_idx)), np.hstack((j_idx, diag_idx)))), (n_voxels, n_voxels), dtype=dtype) if return_as is np.ndarray: return graph.toarray() return return_as(graph) def img_to_graph(img, mask=None, return_as=sparse.coo_matrix, dtype=None): """Graph of the pixel-to-pixel gradient connections Edges are weighted with the gradient values. Read more in the :ref:`User Guide <image_feature_extraction>`. Parameters ---------- img : ndarray, 2D or 3D 2D or 3D image mask : ndarray of booleans, optional An optional mask of the image, to consider only part of the pixels. return_as : np.ndarray or a sparse matrix class, optional The class to use to build the returned adjacency matrix. dtype : None or dtype, optional The data of the returned sparse matrix. By default it is the dtype of img Notes ----- For scikit-learn versions 0.14.1 and prior, return_as=np.ndarray was handled by returning a dense np.matrix instance. Going forward, np.ndarray returns an np.ndarray, as expected. For compatibility, user code relying on this method should wrap its calls in ``np.asarray`` to avoid type issues. """ img = np.atleast_3d(img) n_x, n_y, n_z = img.shape return _to_graph(n_x, n_y, n_z, mask, img, return_as, dtype) def grid_to_graph(n_x, n_y, n_z=1, mask=None, return_as=sparse.coo_matrix, dtype=np.int): """Graph of the pixel-to-pixel connections Edges exist if 2 voxels are connected. Parameters ---------- n_x : int Dimension in x axis n_y : int Dimension in y axis n_z : int, optional, default 1 Dimension in z axis mask : ndarray of booleans, optional An optional mask of the image, to consider only part of the pixels. return_as : np.ndarray or a sparse matrix class, optional The class to use to build the returned adjacency matrix. dtype : dtype, optional, default int The data of the returned sparse matrix. By default it is int Notes ----- For scikit-learn versions 0.14.1 and prior, return_as=np.ndarray was handled by returning a dense np.matrix instance. Going forward, np.ndarray returns an np.ndarray, as expected. For compatibility, user code relying on this method should wrap its calls in ``np.asarray`` to avoid type issues. """ return _to_graph(n_x, n_y, n_z, mask=mask, return_as=return_as, dtype=dtype) ############################################################################### # From an image to a set of small image patches def _compute_n_patches(i_h, i_w, p_h, p_w, max_patches=None): """Compute the number of patches that will be extracted in an image. Read more in the :ref:`User Guide <image_feature_extraction>`. Parameters ---------- i_h : int The image height i_w : int The image with p_h : int The height of a patch p_w : int The width of a patch max_patches : integer or float, optional default is None The maximum number of patches to extract. If max_patches is a float between 0 and 1, it is taken to be a proportion of the total number of patches. """ n_h = i_h - p_h + 1 n_w = i_w - p_w + 1 all_patches = n_h * n_w if max_patches: if (isinstance(max_patches, (numbers.Integral)) and max_patches < all_patches): return max_patches elif (isinstance(max_patches, (numbers.Real)) and 0 < max_patches < 1): return int(max_patches * all_patches) else: raise ValueError("Invalid value for max_patches: %r" % max_patches) else: return all_patches def extract_patches(arr, patch_shape=8, extraction_step=1): """Extracts patches of any n-dimensional array in place using strides. Given an n-dimensional array it will return a 2n-dimensional array with the first n dimensions indexing patch position and the last n indexing the patch content. This operation is immediate (O(1)). A reshape performed on the first n dimensions will cause numpy to copy data, leading to a list of extracted patches. Read more in the :ref:`User Guide <image_feature_extraction>`. Parameters ---------- arr : ndarray n-dimensional array of which patches are to be extracted patch_shape : integer or tuple of length arr.ndim Indicates the shape of the patches to be extracted. If an integer is given, the shape will be a hypercube of sidelength given by its value. extraction_step : integer or tuple of length arr.ndim Indicates step size at which extraction shall be performed. If integer is given, then the step is uniform in all dimensions. Returns ------- patches : strided ndarray 2n-dimensional array indexing patches on first n dimensions and containing patches on the last n dimensions. These dimensions are fake, but this way no data is copied. A simple reshape invokes a copying operation to obtain a list of patches: result.reshape([-1] + list(patch_shape)) """ arr_ndim = arr.ndim if isinstance(patch_shape, numbers.Number): patch_shape = tuple([patch_shape] * arr_ndim) if isinstance(extraction_step, numbers.Number): extraction_step = tuple([extraction_step] * arr_ndim) patch_strides = arr.strides slices = [slice(None, None, st) for st in extraction_step] indexing_strides = arr[slices].strides patch_indices_shape = ((np.array(arr.shape) - np.array(patch_shape)) // np.array(extraction_step)) + 1 shape = tuple(list(patch_indices_shape) + list(patch_shape)) strides = tuple(list(indexing_strides) + list(patch_strides)) patches = as_strided(arr, shape=shape, strides=strides) return patches def extract_patches_2d(image, patch_size, max_patches=None, random_state=None): """Reshape a 2D image into a collection of patches The resulting patches are allocated in a dedicated array. Read more in the :ref:`User Guide <image_feature_extraction>`. Parameters ---------- image : array, shape = (image_height, image_width) or (image_height, image_width, n_channels) The original image data. For color images, the last dimension specifies the channel: a RGB image would have `n_channels=3`. patch_size : tuple of ints (patch_height, patch_width) the dimensions of one patch max_patches : integer or float, optional default is None The maximum number of patches to extract. If max_patches is a float between 0 and 1, it is taken to be a proportion of the total number of patches. random_state : int, RandomState instance or None, optional (default=None) Pseudo number generator state used for random sampling to use if `max_patches` is not 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`. Returns ------- patches : array, shape = (n_patches, patch_height, patch_width) or (n_patches, patch_height, patch_width, n_channels) The collection of patches extracted from the image, where `n_patches` is either `max_patches` or the total number of patches that can be extracted. Examples -------- >>> from sklearn.feature_extraction import image >>> one_image = np.arange(16).reshape((4, 4)) >>> one_image array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) >>> patches = image.extract_patches_2d(one_image, (2, 2)) >>> print(patches.shape) (9, 2, 2) >>> patches[0] array([[0, 1], [4, 5]]) >>> patches[1] array([[1, 2], [5, 6]]) >>> patches[8] array([[10, 11], [14, 15]]) """ i_h, i_w = image.shape[:2] p_h, p_w = patch_size if p_h > i_h: raise ValueError("Height of the patch should be less than the height" " of the image.") if p_w > i_w: raise ValueError("Width of the patch should be less than the width" " of the image.") image = check_array(image, allow_nd=True) image = image.reshape((i_h, i_w, -1)) n_colors = image.shape[-1] extracted_patches = extract_patches(image, patch_shape=(p_h, p_w, n_colors), extraction_step=1) n_patches = _compute_n_patches(i_h, i_w, p_h, p_w, max_patches) if max_patches: rng = check_random_state(random_state) i_s = rng.randint(i_h - p_h + 1, size=n_patches) j_s = rng.randint(i_w - p_w + 1, size=n_patches) patches = extracted_patches[i_s, j_s, 0] else: patches = extracted_patches patches = patches.reshape(-1, p_h, p_w, n_colors) # remove the color dimension if useless if patches.shape[-1] == 1: return patches.reshape((n_patches, p_h, p_w)) else: return patches def reconstruct_from_patches_2d(patches, image_size): """Reconstruct the image from all of its patches. Patches are assumed to overlap and the image is constructed by filling in the patches from left to right, top to bottom, averaging the overlapping regions. Read more in the :ref:`User Guide <image_feature_extraction>`. Parameters ---------- patches : array, shape = (n_patches, patch_height, patch_width) or (n_patches, patch_height, patch_width, n_channels) The complete set of patches. If the patches contain colour information, channels are indexed along the last dimension: RGB patches would have `n_channels=3`. image_size : tuple of ints (image_height, image_width) or (image_height, image_width, n_channels) the size of the image that will be reconstructed Returns ------- image : array, shape = image_size the reconstructed image """ i_h, i_w = image_size[:2] p_h, p_w = patches.shape[1:3] img = np.zeros(image_size) # compute the dimensions of the patches array n_h = i_h - p_h + 1 n_w = i_w - p_w + 1 for p, (i, j) in zip(patches, product(range(n_h), range(n_w))): img[i:i + p_h, j:j + p_w] += p for i in range(i_h): for j in range(i_w): # divide by the amount of overlap # XXX: is this the most efficient way? memory-wise yes, cpu wise? img[i, j] /= float(min(i + 1, p_h, i_h - i) * min(j + 1, p_w, i_w - j)) return img class PatchExtractor(BaseEstimator): """Extracts patches from a collection of images Read more in the :ref:`User Guide <image_feature_extraction>`. Parameters ---------- patch_size : tuple of ints (patch_height, patch_width) the dimensions of one patch max_patches : integer or float, optional default is None The maximum number of patches per image to extract. If max_patches is a float in (0, 1), it is taken to mean a proportion of the total number of patches. 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`. """ def __init__(self, patch_size=None, max_patches=None, random_state=None): self.patch_size = patch_size self.max_patches = max_patches self.random_state = random_state def fit(self, X, y=None): """Do nothing and return the estimator unchanged This method is just there to implement the usual API and hence work in pipelines. """ return self def transform(self, X): """Transforms the image samples in X into a matrix of patch data. Parameters ---------- X : array, shape = (n_samples, image_height, image_width) or (n_samples, image_height, image_width, n_channels) Array of images from which to extract patches. For color images, the last dimension specifies the channel: a RGB image would have `n_channels=3`. Returns ------- patches : array, shape = (n_patches, patch_height, patch_width) or (n_patches, patch_height, patch_width, n_channels) The collection of patches extracted from the images, where `n_patches` is either `n_samples * max_patches` or the total number of patches that can be extracted. """ self.random_state = check_random_state(self.random_state) n_images, i_h, i_w = X.shape[:3] X = np.reshape(X, (n_images, i_h, i_w, -1)) n_channels = X.shape[-1] if self.patch_size is None: patch_size = i_h // 10, i_w // 10 else: patch_size = self.patch_size # compute the dimensions of the patches array p_h, p_w = patch_size n_patches = _compute_n_patches(i_h, i_w, p_h, p_w, self.max_patches) patches_shape = (n_images * n_patches,) + patch_size if n_channels > 1: patches_shape += (n_channels,) # extract the patches patches = np.empty(patches_shape) for ii, image in enumerate(X): patches[ii * n_patches:(ii + 1) * n_patches] = extract_patches_2d( image, patch_size, self.max_patches, self.random_state) return patches
bsd-3-clause
tlby/mxnet
example/named_entity_recognition/src/preprocess.py
10
2002
# !/usr/bin/env python # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # -*- coding: utf-8 -*- import pandas as pd import numpy as np #read in csv of NER training data df = pd.read_csv("../data/ner_dataset.csv", encoding="ISO-8859-1") #rename columns df = df.rename(columns = {"Sentence #" : "utterance_id", "Word" : "token", "POS" : "POS_tag", "Tag" : "BILOU_tag"}) #clean utterance_id column df.loc[:, "utterance_id"] = df["utterance_id"].str.replace('Sentence: ', '') #fill np.nan utterance ID's with the last valid entry df = df.fillna(method='ffill') df.loc[:, "utterance_id"] = df["utterance_id"].apply(int) #melt BILOU tags and tokens into an array per utterance df1 = df.groupby("utterance_id")["BILOU_tag"].apply(lambda x: np.array(x)).to_frame().reset_index() df2 = df.groupby("utterance_id")["token"].apply(lambda x: np.array(x)).to_frame().reset_index() df3 = df.groupby("utterance_id")["POS_tag"].apply(lambda x: np.array(x)).to_frame().reset_index() #join the results on utterance id df = df1.merge(df2.merge(df3, how = "left", on = "utterance_id"), how = "left", on = "utterance_id") #save the dataframe to a csv file df.to_pickle("../data/ner_data.pkl")
apache-2.0
winklerand/pandas
pandas/tests/series/test_operators.py
1
71940
# coding=utf-8 # pylint: disable-msg=E1101,W0612 import pytest import pytz from collections import Iterable from datetime import datetime, timedelta import operator from itertools import product, starmap from numpy import nan, inf import numpy as np import pandas as pd from pandas import (Index, Series, DataFrame, isna, bdate_range, NaT, date_range, timedelta_range) from pandas.core.indexes.datetimes import Timestamp from pandas.core.indexes.timedeltas import Timedelta import pandas.core.nanops as nanops from pandas.compat import range, zip from pandas import compat from pandas.util.testing import (assert_series_equal, assert_almost_equal, assert_frame_equal, assert_index_equal) import pandas.util.testing as tm from .common import TestData class TestSeriesOperators(TestData): def test_series_comparison_scalars(self): series = Series(date_range('1/1/2000', periods=10)) val = datetime(2000, 1, 4) result = series > val expected = Series([x > val for x in series]) tm.assert_series_equal(result, expected) val = series[5] result = series > val expected = Series([x > val for x in series]) tm.assert_series_equal(result, expected) def test_comparisons(self): left = np.random.randn(10) right = np.random.randn(10) left[:3] = np.nan result = nanops.nangt(left, right) with np.errstate(invalid='ignore'): expected = (left > right).astype('O') expected[:3] = np.nan assert_almost_equal(result, expected) s = Series(['a', 'b', 'c']) s2 = Series([False, True, False]) # it works! exp = Series([False, False, False]) assert_series_equal(s == s2, exp) assert_series_equal(s2 == s, exp) def test_op_method(self): def check(series, other, check_reverse=False): simple_ops = ['add', 'sub', 'mul', 'floordiv', 'truediv', 'pow'] if not compat.PY3: simple_ops.append('div') for opname in simple_ops: op = getattr(Series, opname) if op == 'div': alt = operator.truediv else: alt = getattr(operator, opname) result = op(series, other) expected = alt(series, other) assert_almost_equal(result, expected) if check_reverse: rop = getattr(Series, "r" + opname) result = rop(series, other) expected = alt(other, series) assert_almost_equal(result, expected) check(self.ts, self.ts * 2) check(self.ts, self.ts[::2]) check(self.ts, 5, check_reverse=True) check(tm.makeFloatSeries(), tm.makeFloatSeries(), check_reverse=True) def test_neg(self): assert_series_equal(-self.series, -1 * self.series) def test_invert(self): assert_series_equal(-(self.series < 0), ~(self.series < 0)) def test_div(self): with np.errstate(all='ignore'): # no longer do integer div for any ops, but deal with the 0's p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) result = p['first'] / p['second'] expected = Series( p['first'].values.astype(float) / p['second'].values, dtype='float64') expected.iloc[0:3] = np.inf assert_series_equal(result, expected) result = p['first'] / 0 expected = Series(np.inf, index=p.index, name='first') assert_series_equal(result, expected) p = p.astype('float64') result = p['first'] / p['second'] expected = Series(p['first'].values / p['second'].values) assert_series_equal(result, expected) p = DataFrame({'first': [3, 4, 5, 8], 'second': [1, 1, 1, 1]}) result = p['first'] / p['second'] assert_series_equal(result, p['first'].astype('float64'), check_names=False) assert result.name is None assert not result.equals(p['second'] / p['first']) # inf signing s = Series([np.nan, 1., -1.]) result = s / 0 expected = Series([np.nan, np.inf, -np.inf]) assert_series_equal(result, expected) # float/integer issue # GH 7785 p = DataFrame({'first': (1, 0), 'second': (-0.01, -0.02)}) expected = Series([-0.01, -np.inf]) result = p['second'].div(p['first']) assert_series_equal(result, expected, check_names=False) result = p['second'] / p['first'] assert_series_equal(result, expected) # GH 9144 s = Series([-1, 0, 1]) result = 0 / s expected = Series([0.0, nan, 0.0]) assert_series_equal(result, expected) result = s / 0 expected = Series([-inf, nan, inf]) assert_series_equal(result, expected) result = s // 0 expected = Series([-inf, nan, inf]) assert_series_equal(result, expected) # GH 8674 zero_array = np.array([0] * 5) data = np.random.randn(5) expected = pd.Series([0.] * 5) result = zero_array / pd.Series(data) assert_series_equal(result, expected) result = pd.Series(zero_array) / data assert_series_equal(result, expected) result = pd.Series(zero_array) / pd.Series(data) assert_series_equal(result, expected) def test_operators(self): def _check_op(series, other, op, pos_only=False, check_dtype=True): left = np.abs(series) if pos_only else series right = np.abs(other) if pos_only else other cython_or_numpy = op(left, right) python = left.combine(right, op) assert_series_equal(cython_or_numpy, python, check_dtype=check_dtype) def check(series, other): simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod'] for opname in simple_ops: _check_op(series, other, getattr(operator, opname)) _check_op(series, other, operator.pow, pos_only=True) _check_op(series, other, lambda x, y: operator.add(y, x)) _check_op(series, other, lambda x, y: operator.sub(y, x)) _check_op(series, other, lambda x, y: operator.truediv(y, x)) _check_op(series, other, lambda x, y: operator.floordiv(y, x)) _check_op(series, other, lambda x, y: operator.mul(y, x)) _check_op(series, other, lambda x, y: operator.pow(y, x), pos_only=True) _check_op(series, other, lambda x, y: operator.mod(y, x)) check(self.ts, self.ts * 2) check(self.ts, self.ts * 0) check(self.ts, self.ts[::2]) check(self.ts, 5) def check_comparators(series, other, check_dtype=True): _check_op(series, other, operator.gt, check_dtype=check_dtype) _check_op(series, other, operator.ge, check_dtype=check_dtype) _check_op(series, other, operator.eq, check_dtype=check_dtype) _check_op(series, other, operator.lt, check_dtype=check_dtype) _check_op(series, other, operator.le, check_dtype=check_dtype) check_comparators(self.ts, 5) check_comparators(self.ts, self.ts + 1, check_dtype=False) def test_divmod(self): def check(series, other): results = divmod(series, other) if isinstance(other, Iterable) and len(series) != len(other): # if the lengths don't match, this is the test where we use # `self.ts[::2]`. Pad every other value in `other_np` with nan. other_np = [] for n in other: other_np.append(n) other_np.append(np.nan) else: other_np = other other_np = np.asarray(other_np) with np.errstate(all='ignore'): expecteds = divmod(series.values, np.asarray(other_np)) for result, expected in zip(results, expecteds): # check the values, name, and index separatly assert_almost_equal(np.asarray(result), expected) assert result.name == series.name assert_index_equal(result.index, series.index) check(self.ts, self.ts * 2) check(self.ts, self.ts * 0) check(self.ts, self.ts[::2]) check(self.ts, 5) def test_operators_empty_int_corner(self): s1 = Series([], [], dtype=np.int32) s2 = Series({'x': 0.}) assert_series_equal(s1 * s2, Series([np.nan], index=['x'])) def test_operators_timedelta64(self): # invalid ops pytest.raises(Exception, self.objSeries.__add__, 1) pytest.raises(Exception, self.objSeries.__add__, np.array(1, dtype=np.int64)) pytest.raises(Exception, self.objSeries.__sub__, 1) pytest.raises(Exception, self.objSeries.__sub__, np.array(1, dtype=np.int64)) # seriese ops v1 = date_range('2012-1-1', periods=3, freq='D') v2 = date_range('2012-1-2', periods=3, freq='D') rs = Series(v2) - Series(v1) xp = Series(1e9 * 3600 * 24, rs.index).astype('int64').astype('timedelta64[ns]') assert_series_equal(rs, xp) assert rs.dtype == 'timedelta64[ns]' df = DataFrame(dict(A=v1)) td = Series([timedelta(days=i) for i in range(3)]) assert td.dtype == 'timedelta64[ns]' # series on the rhs result = df['A'] - df['A'].shift() assert result.dtype == 'timedelta64[ns]' result = df['A'] + td assert result.dtype == 'M8[ns]' # scalar Timestamp on rhs maxa = df['A'].max() assert isinstance(maxa, Timestamp) resultb = df['A'] - df['A'].max() assert resultb.dtype == 'timedelta64[ns]' # timestamp on lhs result = resultb + df['A'] values = [Timestamp('20111230'), Timestamp('20120101'), Timestamp('20120103')] expected = Series(values, name='A') assert_series_equal(result, expected) # datetimes on rhs result = df['A'] - datetime(2001, 1, 1) expected = Series( [timedelta(days=4017 + i) for i in range(3)], name='A') assert_series_equal(result, expected) assert result.dtype == 'm8[ns]' d = datetime(2001, 1, 1, 3, 4) resulta = df['A'] - d assert resulta.dtype == 'm8[ns]' # roundtrip resultb = resulta + d assert_series_equal(df['A'], resultb) # timedeltas on rhs td = timedelta(days=1) resulta = df['A'] + td resultb = resulta - td assert_series_equal(resultb, df['A']) assert resultb.dtype == 'M8[ns]' # roundtrip td = timedelta(minutes=5, seconds=3) resulta = df['A'] + td resultb = resulta - td assert_series_equal(df['A'], resultb) assert resultb.dtype == 'M8[ns]' # inplace value = rs[2] + np.timedelta64(timedelta(minutes=5, seconds=1)) rs[2] += np.timedelta64(timedelta(minutes=5, seconds=1)) assert rs[2] == value def test_operator_series_comparison_zerorank(self): # GH 13006 result = np.float64(0) > pd.Series([1, 2, 3]) expected = 0.0 > pd.Series([1, 2, 3]) tm.assert_series_equal(result, expected) result = pd.Series([1, 2, 3]) < np.float64(0) expected = pd.Series([1, 2, 3]) < 0.0 tm.assert_series_equal(result, expected) result = np.array([0, 1, 2])[0] > pd.Series([0, 1, 2]) expected = 0.0 > pd.Series([1, 2, 3]) tm.assert_series_equal(result, expected) def test_timedeltas_with_DateOffset(self): # GH 4532 # operate with pd.offsets s = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) result = s + pd.offsets.Second(5) result2 = pd.offsets.Second(5) + s expected = Series([Timestamp('20130101 9:01:05'), Timestamp( '20130101 9:02:05')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s - pd.offsets.Second(5) result2 = -pd.offsets.Second(5) + s expected = Series([Timestamp('20130101 9:00:55'), Timestamp( '20130101 9:01:55')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s + pd.offsets.Milli(5) result2 = pd.offsets.Milli(5) + s expected = Series([Timestamp('20130101 9:01:00.005'), Timestamp( '20130101 9:02:00.005')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s + pd.offsets.Minute(5) + pd.offsets.Milli(5) expected = Series([Timestamp('20130101 9:06:00.005'), Timestamp( '20130101 9:07:00.005')]) assert_series_equal(result, expected) # operate with np.timedelta64 correctly result = s + np.timedelta64(1, 's') result2 = np.timedelta64(1, 's') + s expected = Series([Timestamp('20130101 9:01:01'), Timestamp( '20130101 9:02:01')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s + np.timedelta64(5, 'ms') result2 = np.timedelta64(5, 'ms') + s expected = Series([Timestamp('20130101 9:01:00.005'), Timestamp( '20130101 9:02:00.005')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) # valid DateOffsets for do in ['Hour', 'Minute', 'Second', 'Day', 'Micro', 'Milli', 'Nano']: op = getattr(pd.offsets, do) s + op(5) op(5) + s def test_timedelta_series_ops(self): # GH11925 s = Series(timedelta_range('1 day', periods=3)) ts = Timestamp('2012-01-01') expected = Series(date_range('2012-01-02', periods=3)) assert_series_equal(ts + s, expected) assert_series_equal(s + ts, expected) expected2 = Series(date_range('2011-12-31', periods=3, freq='-1D')) assert_series_equal(ts - s, expected2) assert_series_equal(ts + (-s), expected2) def test_timedelta64_operations_with_DateOffset(self): # GH 10699 td = Series([timedelta(minutes=5, seconds=3)] * 3) result = td + pd.offsets.Minute(1) expected = Series([timedelta(minutes=6, seconds=3)] * 3) assert_series_equal(result, expected) result = td - pd.offsets.Minute(1) expected = Series([timedelta(minutes=4, seconds=3)] * 3) assert_series_equal(result, expected) result = td + Series([pd.offsets.Minute(1), pd.offsets.Second(3), pd.offsets.Hour(2)]) expected = Series([timedelta(minutes=6, seconds=3), timedelta( minutes=5, seconds=6), timedelta(hours=2, minutes=5, seconds=3)]) assert_series_equal(result, expected) result = td + pd.offsets.Minute(1) + pd.offsets.Second(12) expected = Series([timedelta(minutes=6, seconds=15)] * 3) assert_series_equal(result, expected) # valid DateOffsets for do in ['Hour', 'Minute', 'Second', 'Day', 'Micro', 'Milli', 'Nano']: op = getattr(pd.offsets, do) td + op(5) op(5) + td td - op(5) op(5) - td def test_timedelta64_operations_with_timedeltas(self): # td operate with td td1 = Series([timedelta(minutes=5, seconds=3)] * 3) td2 = timedelta(minutes=5, seconds=4) result = td1 - td2 expected = Series([timedelta(seconds=0)] * 3) - Series([timedelta( seconds=1)] * 3) assert result.dtype == 'm8[ns]' assert_series_equal(result, expected) result2 = td2 - td1 expected = (Series([timedelta(seconds=1)] * 3) - Series([timedelta( seconds=0)] * 3)) assert_series_equal(result2, expected) # roundtrip assert_series_equal(result + td2, td1) # Now again, using pd.to_timedelta, which should build # a Series or a scalar, depending on input. td1 = Series(pd.to_timedelta(['00:05:03'] * 3)) td2 = pd.to_timedelta('00:05:04') result = td1 - td2 expected = Series([timedelta(seconds=0)] * 3) - Series([timedelta( seconds=1)] * 3) assert result.dtype == 'm8[ns]' assert_series_equal(result, expected) result2 = td2 - td1 expected = (Series([timedelta(seconds=1)] * 3) - Series([timedelta( seconds=0)] * 3)) assert_series_equal(result2, expected) # roundtrip assert_series_equal(result + td2, td1) def test_timedelta64_operations_with_integers(self): # GH 4521 # divide/multiply by integers startdate = Series(date_range('2013-01-01', '2013-01-03')) enddate = Series(date_range('2013-03-01', '2013-03-03')) s1 = enddate - startdate s1[2] = np.nan s2 = Series([2, 3, 4]) expected = Series(s1.values.astype(np.int64) / s2, dtype='m8[ns]') expected[2] = np.nan result = s1 / s2 assert_series_equal(result, expected) s2 = Series([20, 30, 40]) expected = Series(s1.values.astype(np.int64) / s2, dtype='m8[ns]') expected[2] = np.nan result = s1 / s2 assert_series_equal(result, expected) result = s1 / 2 expected = Series(s1.values.astype(np.int64) / 2, dtype='m8[ns]') expected[2] = np.nan assert_series_equal(result, expected) s2 = Series([20, 30, 40]) expected = Series(s1.values.astype(np.int64) * s2, dtype='m8[ns]') expected[2] = np.nan result = s1 * s2 assert_series_equal(result, expected) for dtype in ['int32', 'int16', 'uint32', 'uint64', 'uint32', 'uint16', 'uint8']: s2 = Series([20, 30, 40], dtype=dtype) expected = Series( s1.values.astype(np.int64) * s2.astype(np.int64), dtype='m8[ns]') expected[2] = np.nan result = s1 * s2 assert_series_equal(result, expected) result = s1 * 2 expected = Series(s1.values.astype(np.int64) * 2, dtype='m8[ns]') expected[2] = np.nan assert_series_equal(result, expected) result = s1 * -1 expected = Series(s1.values.astype(np.int64) * -1, dtype='m8[ns]') expected[2] = np.nan assert_series_equal(result, expected) # invalid ops assert_series_equal(s1 / s2.astype(float), Series([Timedelta('2 days 22:48:00'), Timedelta( '1 days 23:12:00'), Timedelta('NaT')])) assert_series_equal(s1 / 2.0, Series([Timedelta('29 days 12:00:00'), Timedelta( '29 days 12:00:00'), Timedelta('NaT')])) for op in ['__add__', '__sub__']: sop = getattr(s1, op, None) if sop is not None: pytest.raises(TypeError, sop, 1) pytest.raises(TypeError, sop, s2.values) def test_timedelta64_conversions(self): startdate = Series(date_range('2013-01-01', '2013-01-03')) enddate = Series(date_range('2013-03-01', '2013-03-03')) s1 = enddate - startdate s1[2] = np.nan for m in [1, 3, 10]: for unit in ['D', 'h', 'm', 's', 'ms', 'us', 'ns']: # op expected = s1.apply(lambda x: x / np.timedelta64(m, unit)) result = s1 / np.timedelta64(m, unit) assert_series_equal(result, expected) if m == 1 and unit != 'ns': # astype result = s1.astype("timedelta64[{0}]".format(unit)) assert_series_equal(result, expected) # reverse op expected = s1.apply( lambda x: Timedelta(np.timedelta64(m, unit)) / x) result = np.timedelta64(m, unit) / s1 # astype s = Series(date_range('20130101', periods=3)) result = s.astype(object) assert isinstance(result.iloc[0], datetime) assert result.dtype == np.object_ result = s1.astype(object) assert isinstance(result.iloc[0], timedelta) assert result.dtype == np.object_ def test_timedelta64_equal_timedelta_supported_ops(self): ser = Series([Timestamp('20130301'), Timestamp('20130228 23:00:00'), Timestamp('20130228 22:00:00'), Timestamp( '20130228 21:00:00')]) intervals = 'D', 'h', 'm', 's', 'us' # TODO: unused # npy16_mappings = {'D': 24 * 60 * 60 * 1000000, # 'h': 60 * 60 * 1000000, # 'm': 60 * 1000000, # 's': 1000000, # 'us': 1} def timedelta64(*args): return sum(starmap(np.timedelta64, zip(args, intervals))) for op, d, h, m, s, us in product([operator.add, operator.sub], *([range(2)] * 5)): nptd = timedelta64(d, h, m, s, us) pytd = timedelta(days=d, hours=h, minutes=m, seconds=s, microseconds=us) lhs = op(ser, nptd) rhs = op(ser, pytd) try: assert_series_equal(lhs, rhs) except: raise AssertionError( "invalid comparsion [op->{0},d->{1},h->{2},m->{3}," "s->{4},us->{5}]\n{6}\n{7}\n".format(op, d, h, m, s, us, lhs, rhs)) def test_operators_datetimelike(self): def run_ops(ops, get_ser, test_ser): # check that we are getting a TypeError # with 'operate' (from core/ops.py) for the ops that are not # defined for op_str in ops: op = getattr(get_ser, op_str, None) with tm.assert_raises_regex(TypeError, 'operate'): op(test_ser) # ## timedelta64 ### td1 = Series([timedelta(minutes=5, seconds=3)] * 3) td1.iloc[2] = np.nan td2 = timedelta(minutes=5, seconds=4) ops = ['__mul__', '__floordiv__', '__pow__', '__rmul__', '__rfloordiv__', '__rpow__'] run_ops(ops, td1, td2) td1 + td2 td2 + td1 td1 - td2 td2 - td1 td1 / td2 td2 / td1 # ## datetime64 ### dt1 = Series([Timestamp('20111230'), Timestamp('20120101'), Timestamp('20120103')]) dt1.iloc[2] = np.nan dt2 = Series([Timestamp('20111231'), Timestamp('20120102'), Timestamp('20120104')]) ops = ['__add__', '__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__radd__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] run_ops(ops, dt1, dt2) dt1 - dt2 dt2 - dt1 # ## datetime64 with timetimedelta ### ops = ['__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] run_ops(ops, dt1, td1) dt1 + td1 td1 + dt1 dt1 - td1 # TODO: Decide if this ought to work. # td1 - dt1 # ## timetimedelta with datetime64 ### ops = ['__sub__', '__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] run_ops(ops, td1, dt1) td1 + dt1 dt1 + td1 # 8260, 10763 # datetime64 with tz ops = ['__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] tz = 'US/Eastern' dt1 = Series(date_range('2000-01-01 09:00:00', periods=5, tz=tz), name='foo') dt2 = dt1.copy() dt2.iloc[2] = np.nan td1 = Series(timedelta_range('1 days 1 min', periods=5, freq='H')) td2 = td1.copy() td2.iloc[1] = np.nan run_ops(ops, dt1, td1) result = dt1 + td1[0] exp = (dt1.dt.tz_localize(None) + td1[0]).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt2 + td2[0] exp = (dt2.dt.tz_localize(None) + td2[0]).dt.tz_localize(tz) assert_series_equal(result, exp) # odd numpy behavior with scalar timedeltas result = td1[0] + dt1 exp = (dt1.dt.tz_localize(None) + td1[0]).dt.tz_localize(tz) assert_series_equal(result, exp) result = td2[0] + dt2 exp = (dt2.dt.tz_localize(None) + td2[0]).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt1 - td1[0] exp = (dt1.dt.tz_localize(None) - td1[0]).dt.tz_localize(tz) assert_series_equal(result, exp) pytest.raises(TypeError, lambda: td1[0] - dt1) result = dt2 - td2[0] exp = (dt2.dt.tz_localize(None) - td2[0]).dt.tz_localize(tz) assert_series_equal(result, exp) pytest.raises(TypeError, lambda: td2[0] - dt2) result = dt1 + td1 exp = (dt1.dt.tz_localize(None) + td1).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt2 + td2 exp = (dt2.dt.tz_localize(None) + td2).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt1 - td1 exp = (dt1.dt.tz_localize(None) - td1).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt2 - td2 exp = (dt2.dt.tz_localize(None) - td2).dt.tz_localize(tz) assert_series_equal(result, exp) pytest.raises(TypeError, lambda: td1 - dt1) pytest.raises(TypeError, lambda: td2 - dt2) def test_sub_datetime_compat(self): # see gh-14088 s = Series([datetime(2016, 8, 23, 12, tzinfo=pytz.utc), pd.NaT]) dt = datetime(2016, 8, 22, 12, tzinfo=pytz.utc) exp = Series([Timedelta('1 days'), pd.NaT]) assert_series_equal(s - dt, exp) assert_series_equal(s - Timestamp(dt), exp) def test_sub_single_tz(self): # GH12290 s1 = Series([pd.Timestamp('2016-02-10', tz='America/Sao_Paulo')]) s2 = Series([pd.Timestamp('2016-02-08', tz='America/Sao_Paulo')]) result = s1 - s2 expected = Series([Timedelta('2days')]) assert_series_equal(result, expected) result = s2 - s1 expected = Series([Timedelta('-2days')]) assert_series_equal(result, expected) def test_ops_nat(self): # GH 11349 timedelta_series = Series([NaT, Timedelta('1s')]) datetime_series = Series([NaT, Timestamp('19900315')]) nat_series_dtype_timedelta = Series( [NaT, NaT], dtype='timedelta64[ns]') nat_series_dtype_timestamp = Series([NaT, NaT], dtype='datetime64[ns]') single_nat_dtype_datetime = Series([NaT], dtype='datetime64[ns]') single_nat_dtype_timedelta = Series([NaT], dtype='timedelta64[ns]') # subtraction assert_series_equal(timedelta_series - NaT, nat_series_dtype_timedelta) assert_series_equal(-NaT + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(timedelta_series - single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(-single_nat_dtype_timedelta + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(datetime_series - NaT, nat_series_dtype_timestamp) assert_series_equal(-NaT + datetime_series, nat_series_dtype_timestamp) assert_series_equal(datetime_series - single_nat_dtype_datetime, nat_series_dtype_timedelta) with pytest.raises(TypeError): -single_nat_dtype_datetime + datetime_series assert_series_equal(datetime_series - single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(-single_nat_dtype_timedelta + datetime_series, nat_series_dtype_timestamp) # without a Series wrapping the NaT, it is ambiguous # whether it is a datetime64 or timedelta64 # defaults to interpreting it as timedelta64 assert_series_equal(nat_series_dtype_timestamp - NaT, nat_series_dtype_timestamp) assert_series_equal(-NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timestamp - single_nat_dtype_datetime, nat_series_dtype_timedelta) with pytest.raises(TypeError): -single_nat_dtype_datetime + nat_series_dtype_timestamp assert_series_equal(nat_series_dtype_timestamp - single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(-single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) with pytest.raises(TypeError): timedelta_series - single_nat_dtype_datetime # addition assert_series_equal(nat_series_dtype_timestamp + NaT, nat_series_dtype_timestamp) assert_series_equal(NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timestamp + single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timedelta + NaT, nat_series_dtype_timedelta) assert_series_equal(NaT + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timedelta + single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(timedelta_series + NaT, nat_series_dtype_timedelta) assert_series_equal(NaT + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(timedelta_series + single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(single_nat_dtype_timedelta + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timestamp + NaT, nat_series_dtype_timestamp) assert_series_equal(NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timestamp + single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timedelta + NaT, nat_series_dtype_timedelta) assert_series_equal(NaT + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timedelta + single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timedelta + single_nat_dtype_datetime, nat_series_dtype_timestamp) assert_series_equal(single_nat_dtype_datetime + nat_series_dtype_timedelta, nat_series_dtype_timestamp) # multiplication assert_series_equal(nat_series_dtype_timedelta * 1.0, nat_series_dtype_timedelta) assert_series_equal(1.0 * nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(timedelta_series * 1, timedelta_series) assert_series_equal(1 * timedelta_series, timedelta_series) assert_series_equal(timedelta_series * 1.5, Series([NaT, Timedelta('1.5s')])) assert_series_equal(1.5 * timedelta_series, Series([NaT, Timedelta('1.5s')])) assert_series_equal(timedelta_series * nan, nat_series_dtype_timedelta) assert_series_equal(nan * timedelta_series, nat_series_dtype_timedelta) with pytest.raises(TypeError): datetime_series * 1 with pytest.raises(TypeError): nat_series_dtype_timestamp * 1 with pytest.raises(TypeError): datetime_series * 1.0 with pytest.raises(TypeError): nat_series_dtype_timestamp * 1.0 # division assert_series_equal(timedelta_series / 2, Series([NaT, Timedelta('0.5s')])) assert_series_equal(timedelta_series / 2.0, Series([NaT, Timedelta('0.5s')])) assert_series_equal(timedelta_series / nan, nat_series_dtype_timedelta) with pytest.raises(TypeError): nat_series_dtype_timestamp / 1.0 with pytest.raises(TypeError): nat_series_dtype_timestamp / 1 def test_ops_datetimelike_align(self): # GH 7500 # datetimelike ops need to align dt = Series(date_range('2012-1-1', periods=3, freq='D')) dt.iloc[2] = np.nan dt2 = dt[::-1] expected = Series([timedelta(0), timedelta(0), pd.NaT]) # name is reset result = dt2 - dt assert_series_equal(result, expected) expected = Series(expected, name=0) result = (dt2.to_frame() - dt.to_frame())[0] assert_series_equal(result, expected) def test_object_comparisons(self): s = Series(['a', 'b', np.nan, 'c', 'a']) result = s == 'a' expected = Series([True, False, False, False, True]) assert_series_equal(result, expected) result = s < 'a' expected = Series([False, False, False, False, False]) assert_series_equal(result, expected) result = s != 'a' expected = -(s == 'a') assert_series_equal(result, expected) def test_comparison_tuples(self): # GH11339 # comparisons vs tuple s = Series([(1, 1), (1, 2)]) result = s == (1, 2) expected = Series([False, True]) assert_series_equal(result, expected) result = s != (1, 2) expected = Series([True, False]) assert_series_equal(result, expected) result = s == (0, 0) expected = Series([False, False]) assert_series_equal(result, expected) result = s != (0, 0) expected = Series([True, True]) assert_series_equal(result, expected) s = Series([(1, 1), (1, 1)]) result = s == (1, 1) expected = Series([True, True]) assert_series_equal(result, expected) result = s != (1, 1) expected = Series([False, False]) assert_series_equal(result, expected) s = Series([frozenset([1]), frozenset([1, 2])]) result = s == frozenset([1]) expected = Series([True, False]) assert_series_equal(result, expected) def test_comparison_operators_with_nas(self): s = Series(bdate_range('1/1/2000', periods=10), dtype=object) s[::2] = np.nan # test that comparisons work ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne'] for op in ops: val = s[5] f = getattr(operator, op) result = f(s, val) expected = f(s.dropna(), val).reindex(s.index) if op == 'ne': expected = expected.fillna(True).astype(bool) else: expected = expected.fillna(False).astype(bool) assert_series_equal(result, expected) # fffffffuuuuuuuuuuuu # result = f(val, s) # expected = f(val, s.dropna()).reindex(s.index) # assert_series_equal(result, expected) # boolean &, |, ^ should work with object arrays and propagate NAs ops = ['and_', 'or_', 'xor'] mask = s.isna() for bool_op in ops: f = getattr(operator, bool_op) filled = s.fillna(s[0]) result = f(s < s[9], s > s[3]) expected = f(filled < filled[9], filled > filled[3]) expected[mask] = False assert_series_equal(result, expected) def test_comparison_object_numeric_nas(self): s = Series(np.random.randn(10), dtype=object) shifted = s.shift(2) ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne'] for op in ops: f = getattr(operator, op) result = f(s, shifted) expected = f(s.astype(float), shifted.astype(float)) assert_series_equal(result, expected) def test_comparison_invalid(self): # GH4968 # invalid date/int comparisons s = Series(range(5)) s2 = Series(date_range('20010101', periods=5)) for (x, y) in [(s, s2), (s2, s)]: pytest.raises(TypeError, lambda: x == y) pytest.raises(TypeError, lambda: x != y) pytest.raises(TypeError, lambda: x >= y) pytest.raises(TypeError, lambda: x > y) pytest.raises(TypeError, lambda: x < y) pytest.raises(TypeError, lambda: x <= y) def test_more_na_comparisons(self): for dtype in [None, object]: left = Series(['a', np.nan, 'c'], dtype=dtype) right = Series(['a', np.nan, 'd'], dtype=dtype) result = left == right expected = Series([True, False, False]) assert_series_equal(result, expected) result = left != right expected = Series([False, True, True]) assert_series_equal(result, expected) result = left == np.nan expected = Series([False, False, False]) assert_series_equal(result, expected) result = left != np.nan expected = Series([True, True, True]) assert_series_equal(result, expected) def test_nat_comparisons(self): data = [([pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')], [pd.NaT, pd.NaT, pd.Timestamp('2011-01-03')]), ([pd.Timedelta('1 days'), pd.NaT, pd.Timedelta('3 days')], [pd.NaT, pd.NaT, pd.Timedelta('3 days')]), ([pd.Period('2011-01', freq='M'), pd.NaT, pd.Period('2011-03', freq='M')], [pd.NaT, pd.NaT, pd.Period('2011-03', freq='M')])] # add lhs / rhs switched data data = data + [(r, l) for l, r in data] for l, r in data: for dtype in [None, object]: left = Series(l, dtype=dtype) # Series, Index for right in [Series(r, dtype=dtype), Index(r, dtype=dtype)]: expected = Series([False, False, True]) assert_series_equal(left == right, expected) expected = Series([True, True, False]) assert_series_equal(left != right, expected) expected = Series([False, False, False]) assert_series_equal(left < right, expected) expected = Series([False, False, False]) assert_series_equal(left > right, expected) expected = Series([False, False, True]) assert_series_equal(left >= right, expected) expected = Series([False, False, True]) assert_series_equal(left <= right, expected) def test_nat_comparisons_scalar(self): data = [[pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')], [pd.Timedelta('1 days'), pd.NaT, pd.Timedelta('3 days')], [pd.Period('2011-01', freq='M'), pd.NaT, pd.Period('2011-03', freq='M')]] for l in data: for dtype in [None, object]: left = Series(l, dtype=dtype) expected = Series([False, False, False]) assert_series_equal(left == pd.NaT, expected) assert_series_equal(pd.NaT == left, expected) expected = Series([True, True, True]) assert_series_equal(left != pd.NaT, expected) assert_series_equal(pd.NaT != left, expected) expected = Series([False, False, False]) assert_series_equal(left < pd.NaT, expected) assert_series_equal(pd.NaT > left, expected) assert_series_equal(left <= pd.NaT, expected) assert_series_equal(pd.NaT >= left, expected) assert_series_equal(left > pd.NaT, expected) assert_series_equal(pd.NaT < left, expected) assert_series_equal(left >= pd.NaT, expected) assert_series_equal(pd.NaT <= left, expected) def test_comparison_different_length(self): a = Series(['a', 'b', 'c']) b = Series(['b', 'a']) pytest.raises(ValueError, a.__lt__, b) a = Series([1, 2]) b = Series([2, 3, 4]) pytest.raises(ValueError, a.__eq__, b) def test_comparison_label_based(self): # GH 4947 # comparisons should be label based a = Series([True, False, True], list('bca')) b = Series([False, True, False], list('abc')) expected = Series([False, True, False], list('abc')) result = a & b assert_series_equal(result, expected) expected = Series([True, True, False], list('abc')) result = a | b assert_series_equal(result, expected) expected = Series([True, False, False], list('abc')) result = a ^ b assert_series_equal(result, expected) # rhs is bigger a = Series([True, False, True], list('bca')) b = Series([False, True, False, True], list('abcd')) expected = Series([False, True, False, False], list('abcd')) result = a & b assert_series_equal(result, expected) expected = Series([True, True, False, False], list('abcd')) result = a | b assert_series_equal(result, expected) # filling # vs empty result = a & Series([]) expected = Series([False, False, False], list('bca')) assert_series_equal(result, expected) result = a | Series([]) expected = Series([True, False, True], list('bca')) assert_series_equal(result, expected) # vs non-matching result = a & Series([1], ['z']) expected = Series([False, False, False, False], list('abcz')) assert_series_equal(result, expected) result = a | Series([1], ['z']) expected = Series([True, True, False, False], list('abcz')) assert_series_equal(result, expected) # identity # we would like s[s|e] == s to hold for any e, whether empty or not for e in [Series([]), Series([1], ['z']), Series(np.nan, b.index), Series(np.nan, a.index)]: result = a[a | e] assert_series_equal(result, a[a]) for e in [Series(['z'])]: if compat.PY3: with tm.assert_produces_warning(RuntimeWarning): result = a[a | e] else: result = a[a | e] assert_series_equal(result, a[a]) # vs scalars index = list('bca') t = Series([True, False, True]) for v in [True, 1, 2]: result = Series([True, False, True], index=index) | v expected = Series([True, True, True], index=index) assert_series_equal(result, expected) for v in [np.nan, 'foo']: pytest.raises(TypeError, lambda: t | v) for v in [False, 0]: result = Series([True, False, True], index=index) | v expected = Series([True, False, True], index=index) assert_series_equal(result, expected) for v in [True, 1]: result = Series([True, False, True], index=index) & v expected = Series([True, False, True], index=index) assert_series_equal(result, expected) for v in [False, 0]: result = Series([True, False, True], index=index) & v expected = Series([False, False, False], index=index) assert_series_equal(result, expected) for v in [np.nan]: pytest.raises(TypeError, lambda: t & v) def test_comparison_flex_basic(self): left = pd.Series(np.random.randn(10)) right = pd.Series(np.random.randn(10)) assert_series_equal(left.eq(right), left == right) assert_series_equal(left.ne(right), left != right) assert_series_equal(left.le(right), left < right) assert_series_equal(left.lt(right), left <= right) assert_series_equal(left.gt(right), left > right) assert_series_equal(left.ge(right), left >= right) # axis for axis in [0, None, 'index']: assert_series_equal(left.eq(right, axis=axis), left == right) assert_series_equal(left.ne(right, axis=axis), left != right) assert_series_equal(left.le(right, axis=axis), left < right) assert_series_equal(left.lt(right, axis=axis), left <= right) assert_series_equal(left.gt(right, axis=axis), left > right) assert_series_equal(left.ge(right, axis=axis), left >= right) # msg = 'No axis named 1 for object type' for op in ['eq', 'ne', 'le', 'le', 'gt', 'ge']: with tm.assert_raises_regex(ValueError, msg): getattr(left, op)(right, axis=1) def test_comparison_flex_alignment(self): left = Series([1, 3, 2], index=list('abc')) right = Series([2, 2, 2], index=list('bcd')) exp = pd.Series([False, False, True, False], index=list('abcd')) assert_series_equal(left.eq(right), exp) exp = pd.Series([True, True, False, True], index=list('abcd')) assert_series_equal(left.ne(right), exp) exp = pd.Series([False, False, True, False], index=list('abcd')) assert_series_equal(left.le(right), exp) exp = pd.Series([False, False, False, False], index=list('abcd')) assert_series_equal(left.lt(right), exp) exp = pd.Series([False, True, True, False], index=list('abcd')) assert_series_equal(left.ge(right), exp) exp = pd.Series([False, True, False, False], index=list('abcd')) assert_series_equal(left.gt(right), exp) def test_comparison_flex_alignment_fill(self): left = Series([1, 3, 2], index=list('abc')) right = Series([2, 2, 2], index=list('bcd')) exp = pd.Series([False, False, True, True], index=list('abcd')) assert_series_equal(left.eq(right, fill_value=2), exp) exp = pd.Series([True, True, False, False], index=list('abcd')) assert_series_equal(left.ne(right, fill_value=2), exp) exp = pd.Series([False, False, True, True], index=list('abcd')) assert_series_equal(left.le(right, fill_value=0), exp) exp = pd.Series([False, False, False, True], index=list('abcd')) assert_series_equal(left.lt(right, fill_value=0), exp) exp = pd.Series([True, True, True, False], index=list('abcd')) assert_series_equal(left.ge(right, fill_value=0), exp) exp = pd.Series([True, True, False, False], index=list('abcd')) assert_series_equal(left.gt(right, fill_value=0), exp) def test_return_dtypes_bool_op_costant(self): # gh15115 s = pd.Series([1, 3, 2], index=range(3)) const = 2 for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']: result = getattr(s, op)(const).get_dtype_counts() tm.assert_series_equal(result, Series([1], ['bool'])) # empty Series empty = s.iloc[:0] for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']: result = getattr(empty, op)(const).get_dtype_counts() tm.assert_series_equal(result, Series([1], ['bool'])) def test_operators_bitwise(self): # GH 9016: support bitwise op for integer types index = list('bca') s_tft = Series([True, False, True], index=index) s_fff = Series([False, False, False], index=index) s_tff = Series([True, False, False], index=index) s_empty = Series([]) # TODO: unused # s_0101 = Series([0, 1, 0, 1]) s_0123 = Series(range(4), dtype='int64') s_3333 = Series([3] * 4) s_4444 = Series([4] * 4) res = s_tft & s_empty expected = s_fff assert_series_equal(res, expected) res = s_tft | s_empty expected = s_tft assert_series_equal(res, expected) res = s_0123 & s_3333 expected = Series(range(4), dtype='int64') assert_series_equal(res, expected) res = s_0123 | s_4444 expected = Series(range(4, 8), dtype='int64') assert_series_equal(res, expected) s_a0b1c0 = Series([1], list('b')) res = s_tft & s_a0b1c0 expected = s_tff.reindex(list('abc')) assert_series_equal(res, expected) res = s_tft | s_a0b1c0 expected = s_tft.reindex(list('abc')) assert_series_equal(res, expected) n0 = 0 res = s_tft & n0 expected = s_fff assert_series_equal(res, expected) res = s_0123 & n0 expected = Series([0] * 4) assert_series_equal(res, expected) n1 = 1 res = s_tft & n1 expected = s_tft assert_series_equal(res, expected) res = s_0123 & n1 expected = Series([0, 1, 0, 1]) assert_series_equal(res, expected) s_1111 = Series([1] * 4, dtype='int8') res = s_0123 & s_1111 expected = Series([0, 1, 0, 1], dtype='int64') assert_series_equal(res, expected) res = s_0123.astype(np.int16) | s_1111.astype(np.int32) expected = Series([1, 1, 3, 3], dtype='int32') assert_series_equal(res, expected) pytest.raises(TypeError, lambda: s_1111 & 'a') pytest.raises(TypeError, lambda: s_1111 & ['a', 'b', 'c', 'd']) pytest.raises(TypeError, lambda: s_0123 & np.NaN) pytest.raises(TypeError, lambda: s_0123 & 3.14) pytest.raises(TypeError, lambda: s_0123 & [0.1, 4, 3.14, 2]) # s_0123 will be all false now because of reindexing like s_tft if compat.PY3: # unable to sort incompatible object via .union. exp = Series([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3]) with tm.assert_produces_warning(RuntimeWarning): assert_series_equal(s_tft & s_0123, exp) else: exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) assert_series_equal(s_tft & s_0123, exp) # s_tft will be all false now because of reindexing like s_0123 if compat.PY3: # unable to sort incompatible object via .union. exp = Series([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a']) with tm.assert_produces_warning(RuntimeWarning): assert_series_equal(s_0123 & s_tft, exp) else: exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) assert_series_equal(s_0123 & s_tft, exp) assert_series_equal(s_0123 & False, Series([False] * 4)) assert_series_equal(s_0123 ^ False, Series([False, True, True, True])) assert_series_equal(s_0123 & [False], Series([False] * 4)) assert_series_equal(s_0123 & (False), Series([False] * 4)) assert_series_equal(s_0123 & Series([False, np.NaN, False, False]), Series([False] * 4)) s_ftft = Series([False, True, False, True]) assert_series_equal(s_0123 & Series([0.1, 4, -3.14, 2]), s_ftft) s_abNd = Series(['a', 'b', np.NaN, 'd']) res = s_0123 & s_abNd expected = s_ftft assert_series_equal(res, expected) def test_scalar_na_cmp_corners(self): s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10]) def tester(a, b): return a & b pytest.raises(TypeError, tester, s, datetime(2005, 1, 1)) s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)]) s[::2] = np.nan expected = Series(True, index=s.index) expected[::2] = False assert_series_equal(tester(s, list(s)), expected) d = DataFrame({'A': s}) # TODO: Fix this exception - needs to be fixed! (see GH5035) # (previously this was a TypeError because series returned # NotImplemented # this is an alignment issue; these are equivalent # https://github.com/pandas-dev/pandas/issues/5284 pytest.raises(ValueError, lambda: d.__and__(s, axis='columns')) pytest.raises(ValueError, tester, s, d) # this is wrong as its not a boolean result # result = d.__and__(s,axis='index') def test_operators_corner(self): series = self.ts empty = Series([], index=Index([])) result = series + empty assert np.isnan(result).all() result = empty + Series([], index=Index([])) assert len(result) == 0 # TODO: this returned NotImplemented earlier, what to do? # deltas = Series([timedelta(1)] * 5, index=np.arange(5)) # sub_deltas = deltas[::2] # deltas5 = deltas * 5 # deltas = deltas + sub_deltas # float + int int_ts = self.ts.astype(int)[:-5] added = self.ts + int_ts expected = Series(self.ts.values[:-5] + int_ts.values, index=self.ts.index[:-5], name='ts') tm.assert_series_equal(added[:-5], expected) def test_operators_reverse_object(self): # GH 56 arr = Series(np.random.randn(10), index=np.arange(10), dtype=object) def _check_op(arr, op): result = op(1., arr) expected = op(1., arr.astype(float)) assert_series_equal(result.astype(float), expected) _check_op(arr, operator.add) _check_op(arr, operator.sub) _check_op(arr, operator.mul) _check_op(arr, operator.truediv) _check_op(arr, operator.floordiv) def test_arith_ops_df_compat(self): # GH 1134 s1 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s2 = pd.Series([2, 2, 2], index=list('ABD'), name='x') exp = pd.Series([3.0, 4.0, np.nan, np.nan], index=list('ABCD'), name='x') assert_series_equal(s1 + s2, exp) assert_series_equal(s2 + s1, exp) exp = pd.DataFrame({'x': [3.0, 4.0, np.nan, np.nan]}, index=list('ABCD')) assert_frame_equal(s1.to_frame() + s2.to_frame(), exp) assert_frame_equal(s2.to_frame() + s1.to_frame(), exp) # different length s3 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s4 = pd.Series([2, 2, 2, 2], index=list('ABCD'), name='x') exp = pd.Series([3, 4, 5, np.nan], index=list('ABCD'), name='x') assert_series_equal(s3 + s4, exp) assert_series_equal(s4 + s3, exp) exp = pd.DataFrame({'x': [3, 4, 5, np.nan]}, index=list('ABCD')) assert_frame_equal(s3.to_frame() + s4.to_frame(), exp) assert_frame_equal(s4.to_frame() + s3.to_frame(), exp) def test_comp_ops_df_compat(self): # GH 1134 s1 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s2 = pd.Series([2, 2, 2], index=list('ABD'), name='x') s3 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s4 = pd.Series([2, 2, 2, 2], index=list('ABCD'), name='x') for l, r in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]: msg = "Can only compare identically-labeled Series objects" with tm.assert_raises_regex(ValueError, msg): l == r with tm.assert_raises_regex(ValueError, msg): l != r with tm.assert_raises_regex(ValueError, msg): l < r msg = "Can only compare identically-labeled DataFrame objects" with tm.assert_raises_regex(ValueError, msg): l.to_frame() == r.to_frame() with tm.assert_raises_regex(ValueError, msg): l.to_frame() != r.to_frame() with tm.assert_raises_regex(ValueError, msg): l.to_frame() < r.to_frame() def test_bool_ops_df_compat(self): # GH 1134 s1 = pd.Series([True, False, True], index=list('ABC'), name='x') s2 = pd.Series([True, True, False], index=list('ABD'), name='x') exp = pd.Series([True, False, False, False], index=list('ABCD'), name='x') assert_series_equal(s1 & s2, exp) assert_series_equal(s2 & s1, exp) # True | np.nan => True exp = pd.Series([True, True, True, False], index=list('ABCD'), name='x') assert_series_equal(s1 | s2, exp) # np.nan | True => np.nan, filled with False exp = pd.Series([True, True, False, False], index=list('ABCD'), name='x') assert_series_equal(s2 | s1, exp) # DataFrame doesn't fill nan with False exp = pd.DataFrame({'x': [True, False, np.nan, np.nan]}, index=list('ABCD')) assert_frame_equal(s1.to_frame() & s2.to_frame(), exp) assert_frame_equal(s2.to_frame() & s1.to_frame(), exp) exp = pd.DataFrame({'x': [True, True, np.nan, np.nan]}, index=list('ABCD')) assert_frame_equal(s1.to_frame() | s2.to_frame(), exp) assert_frame_equal(s2.to_frame() | s1.to_frame(), exp) # different length s3 = pd.Series([True, False, True], index=list('ABC'), name='x') s4 = pd.Series([True, True, True, True], index=list('ABCD'), name='x') exp = pd.Series([True, False, True, False], index=list('ABCD'), name='x') assert_series_equal(s3 & s4, exp) assert_series_equal(s4 & s3, exp) # np.nan | True => np.nan, filled with False exp = pd.Series([True, True, True, False], index=list('ABCD'), name='x') assert_series_equal(s3 | s4, exp) # True | np.nan => True exp = pd.Series([True, True, True, True], index=list('ABCD'), name='x') assert_series_equal(s4 | s3, exp) exp = pd.DataFrame({'x': [True, False, True, np.nan]}, index=list('ABCD')) assert_frame_equal(s3.to_frame() & s4.to_frame(), exp) assert_frame_equal(s4.to_frame() & s3.to_frame(), exp) exp = pd.DataFrame({'x': [True, True, True, np.nan]}, index=list('ABCD')) assert_frame_equal(s3.to_frame() | s4.to_frame(), exp) assert_frame_equal(s4.to_frame() | s3.to_frame(), exp) def test_series_frame_radd_bug(self): # GH 353 vals = Series(tm.rands_array(5, 10)) result = 'foo_' + vals expected = vals.map(lambda x: 'foo_' + x) assert_series_equal(result, expected) frame = DataFrame({'vals': vals}) result = 'foo_' + frame expected = DataFrame({'vals': vals.map(lambda x: 'foo_' + x)}) assert_frame_equal(result, expected) # really raise this time with pytest.raises(TypeError): datetime.now() + self.ts with pytest.raises(TypeError): self.ts + datetime.now() def test_series_radd_more(self): data = [[1, 2, 3], [1.1, 2.2, 3.3], [pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02'), pd.NaT], ['x', 'y', 1]] for d in data: for dtype in [None, object]: s = Series(d, dtype=dtype) with pytest.raises(TypeError): 'foo_' + s for dtype in [None, object]: res = 1 + pd.Series([1, 2, 3], dtype=dtype) exp = pd.Series([2, 3, 4], dtype=dtype) assert_series_equal(res, exp) res = pd.Series([1, 2, 3], dtype=dtype) + 1 assert_series_equal(res, exp) res = np.nan + pd.Series([1, 2, 3], dtype=dtype) exp = pd.Series([np.nan, np.nan, np.nan], dtype=dtype) assert_series_equal(res, exp) res = pd.Series([1, 2, 3], dtype=dtype) + np.nan assert_series_equal(res, exp) s = pd.Series([pd.Timedelta('1 days'), pd.Timedelta('2 days'), pd.Timedelta('3 days')], dtype=dtype) exp = pd.Series([pd.Timedelta('4 days'), pd.Timedelta('5 days'), pd.Timedelta('6 days')]) assert_series_equal(pd.Timedelta('3 days') + s, exp) assert_series_equal(s + pd.Timedelta('3 days'), exp) s = pd.Series(['x', np.nan, 'x']) assert_series_equal('a' + s, pd.Series(['ax', np.nan, 'ax'])) assert_series_equal(s + 'a', pd.Series(['xa', np.nan, 'xa'])) def test_frame_radd_more(self): data = [[1, 2, 3], [1.1, 2.2, 3.3], [pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02'), pd.NaT], ['x', 'y', 1]] for d in data: for dtype in [None, object]: s = DataFrame(d, dtype=dtype) with pytest.raises(TypeError): 'foo_' + s for dtype in [None, object]: res = 1 + pd.DataFrame([1, 2, 3], dtype=dtype) exp = pd.DataFrame([2, 3, 4], dtype=dtype) assert_frame_equal(res, exp) res = pd.DataFrame([1, 2, 3], dtype=dtype) + 1 assert_frame_equal(res, exp) res = np.nan + pd.DataFrame([1, 2, 3], dtype=dtype) exp = pd.DataFrame([np.nan, np.nan, np.nan], dtype=dtype) assert_frame_equal(res, exp) res = pd.DataFrame([1, 2, 3], dtype=dtype) + np.nan assert_frame_equal(res, exp) df = pd.DataFrame(['x', np.nan, 'x']) assert_frame_equal('a' + df, pd.DataFrame(['ax', np.nan, 'ax'])) assert_frame_equal(df + 'a', pd.DataFrame(['xa', np.nan, 'xa'])) def test_operators_frame(self): # rpow does not work with DataFrame df = DataFrame({'A': self.ts}) assert_series_equal(self.ts + self.ts, self.ts + df['A'], check_names=False) assert_series_equal(self.ts ** self.ts, self.ts ** df['A'], check_names=False) assert_series_equal(self.ts < self.ts, self.ts < df['A'], check_names=False) assert_series_equal(self.ts / self.ts, self.ts / df['A'], check_names=False) def test_operators_combine(self): def _check_fill(meth, op, a, b, fill_value=0): exp_index = a.index.union(b.index) a = a.reindex(exp_index) b = b.reindex(exp_index) amask = isna(a) bmask = isna(b) exp_values = [] for i in range(len(exp_index)): with np.errstate(all='ignore'): if amask[i]: if bmask[i]: exp_values.append(nan) continue exp_values.append(op(fill_value, b[i])) elif bmask[i]: if amask[i]: exp_values.append(nan) continue exp_values.append(op(a[i], fill_value)) else: exp_values.append(op(a[i], b[i])) result = meth(a, b, fill_value=fill_value) expected = Series(exp_values, exp_index) assert_series_equal(result, expected) a = Series([nan, 1., 2., 3., nan], index=np.arange(5)) b = Series([nan, 1, nan, 3, nan, 4.], index=np.arange(6)) pairings = [] for op in ['add', 'sub', 'mul', 'pow', 'truediv', 'floordiv']: fv = 0 lop = getattr(Series, op) lequiv = getattr(operator, op) rop = getattr(Series, 'r' + op) # bind op at definition time... requiv = lambda x, y, op=op: getattr(operator, op)(y, x) pairings.append((lop, lequiv, fv)) pairings.append((rop, requiv, fv)) if compat.PY3: pairings.append((Series.div, operator.truediv, 1)) pairings.append((Series.rdiv, lambda x, y: operator.truediv(y, x), 1)) else: pairings.append((Series.div, operator.div, 1)) pairings.append((Series.rdiv, lambda x, y: operator.div(y, x), 1)) for op, equiv_op, fv in pairings: result = op(a, b) exp = equiv_op(a, b) assert_series_equal(result, exp) _check_fill(op, equiv_op, a, b, fill_value=fv) # should accept axis=0 or axis='rows' op(a, b, axis=0) def test_ne(self): ts = Series([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float) expected = [True, True, False, True, True] assert tm.equalContents(ts.index != 5, expected) assert tm.equalContents(~(ts.index == 5), expected) def test_operators_na_handling(self): from decimal import Decimal from datetime import date s = Series([Decimal('1.3'), Decimal('2.3')], index=[date(2012, 1, 1), date(2012, 1, 2)]) result = s + s.shift(1) result2 = s.shift(1) + s assert isna(result[0]) assert isna(result2[0]) s = Series(['foo', 'bar', 'baz', np.nan]) result = 'prefix_' + s expected = Series(['prefix_foo', 'prefix_bar', 'prefix_baz', np.nan]) assert_series_equal(result, expected) result = s + '_suffix' expected = Series(['foo_suffix', 'bar_suffix', 'baz_suffix', np.nan]) assert_series_equal(result, expected) def test_divide_decimal(self): """ resolves issue #9787 """ from decimal import Decimal expected = Series([Decimal(5)]) s = Series([Decimal(10)]) s = s / Decimal(2) assert_series_equal(expected, s) s = Series([Decimal(10)]) s = s // Decimal(2) assert_series_equal(expected, s) def test_datetime64_with_index(self): # arithmetic integer ops with an index s = Series(np.random.randn(5)) expected = s - s.index.to_series() result = s - s.index assert_series_equal(result, expected) # GH 4629 # arithmetic datetime64 ops with an index s = Series(date_range('20130101', periods=5), index=date_range('20130101', periods=5)) expected = s - s.index.to_series() result = s - s.index assert_series_equal(result, expected) result = s - s.index.to_period() assert_series_equal(result, expected) df = DataFrame(np.random.randn(5, 2), index=date_range('20130101', periods=5)) df['date'] = Timestamp('20130102') df['expected'] = df['date'] - df.index.to_series() df['result'] = df['date'] - df.index assert_series_equal(df['result'], df['expected'], check_names=False) def test_dti_tz_convert_to_utc(self): base = pd.DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], tz='UTC') idx1 = base.tz_convert('Asia/Tokyo')[:2] idx2 = base.tz_convert('US/Eastern')[1:] res = Series([1, 2], index=idx1) + Series([1, 1], index=idx2) assert_series_equal(res, Series([np.nan, 3, np.nan], index=base)) def test_op_duplicate_index(self): # GH14227 s1 = Series([1, 2], index=[1, 1]) s2 = Series([10, 10], index=[1, 2]) result = s1 + s2 expected = pd.Series([11, 12, np.nan], index=[1, 1, 2]) assert_series_equal(result, expected) @pytest.mark.parametrize( "test_input,error_type", [ (pd.Series([]), ValueError), # For strings, or any Series with dtype 'O' (pd.Series(['foo', 'bar', 'baz']), TypeError), (pd.Series([(1,), (2,)]), TypeError), # For mixed data types ( pd.Series(['foo', 'foo', 'bar', 'bar', None, np.nan, 'baz']), TypeError ), ] ) def test_assert_idxminmax_raises(self, test_input, error_type): """ Cases where ``Series.argmax`` and related should raise an exception """ with pytest.raises(error_type): test_input.idxmin() with pytest.raises(error_type): test_input.idxmin(skipna=False) with pytest.raises(error_type): test_input.idxmax() with pytest.raises(error_type): test_input.idxmax(skipna=False) def test_idxminmax_with_inf(self): # For numeric data with NA and Inf (GH #13595) s = pd.Series([0, -np.inf, np.inf, np.nan]) assert s.idxmin() == 1 assert np.isnan(s.idxmin(skipna=False)) assert s.idxmax() == 2 assert np.isnan(s.idxmax(skipna=False)) # Using old-style behavior that treats floating point nan, -inf, and # +inf as missing with pd.option_context('mode.use_inf_as_na', True): assert s.idxmin() == 0 assert np.isnan(s.idxmin(skipna=False)) assert s.idxmax() == 0 np.isnan(s.idxmax(skipna=False))
bsd-3-clause
petosegan/scikit-learn
examples/semi_supervised/plot_label_propagation_digits_active_learning.py
294
3417
""" ======================================== Label Propagation digits active learning ======================================== Demonstrates an active learning technique to learn handwritten digits using label propagation. We start by training a label propagation model with only 10 labeled points, then we select the top five most uncertain points to label. Next, we train with 15 labeled points (original 10 + 5 new ones). We repeat this process four times to have a model trained with 30 labeled examples. A plot will appear showing the top 5 most uncertain digits for each iteration of training. These may or may not contain mistakes, but we will train the next model with their true labels. """ print(__doc__) # Authors: Clay Woolam <[email protected]> # 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 classification_report, confusion_matrix 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 = 10 unlabeled_indices = np.arange(n_total_samples)[n_labeled_points:] f = plt.figure() for i in range(5): y_train = np.copy(y) y_train[unlabeled_indices] = -1 lp_model = label_propagation.LabelSpreading(gamma=0.25, max_iter=5) lp_model.fit(X, y_train) predicted_labels = lp_model.transduction_[unlabeled_indices] true_labels = y[unlabeled_indices] cm = confusion_matrix(true_labels, predicted_labels, labels=lp_model.classes_) print('Iteration %i %s' % (i, 70 * '_')) print("Label Spreading model: %d labeled & %d unlabeled (%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) # compute the entropies of transduced label distributions pred_entropies = stats.distributions.entropy( lp_model.label_distributions_.T) # select five digit examples that the classifier is most uncertain about uncertainty_index = uncertainty_index = np.argsort(pred_entropies)[-5:] # keep track of indices that we get labels for delete_indices = np.array([]) f.text(.05, (1 - (i + 1) * .183), "model %d\n\nfit with\n%d labels" % ((i + 1), i * 5 + 10), size=10) for index, image_index in enumerate(uncertainty_index): image = images[image_index] sub = f.add_subplot(5, 5, index + 1 + (5 * i)) sub.imshow(image, cmap=plt.cm.gray_r) sub.set_title('predict: %i\ntrue: %i' % ( lp_model.transduction_[image_index], y[image_index]), size=10) sub.axis('off') # labeling 5 points, remote from labeled set delete_index, = np.where(unlabeled_indices == image_index) delete_indices = np.concatenate((delete_indices, delete_index)) unlabeled_indices = np.delete(unlabeled_indices, delete_indices) n_labeled_points += 5 f.suptitle("Active learning with Label Propagation.\nRows show 5 most " "uncertain labels to learn with the next model.") plt.subplots_adjust(0.12, 0.03, 0.9, 0.8, 0.2, 0.45) plt.show()
bsd-3-clause
spiralx/custom-web
settings/ipython/ipython_config.py
1
19852
# Configuration file for ipython. c = get_config() #------------------------------------------------------------------------------ # InteractiveShellApp configuration #------------------------------------------------------------------------------ # A Mixin for applications that start InteractiveShell instances. # # Provides configurables for loading extensions and executing files as part of # configuring a Shell environment. # # The following methods should be called by the :meth:`initialize` method of the # subclass: # # - :meth:`init_path` # - :meth:`init_shell` (to be implemented by the subclass) # - :meth:`init_gui_pylab` # - :meth:`init_extensions` # - :meth:`init_code` # Execute the given command string. # c.InteractiveShellApp.code_to_run = '' # Run the file referenced by the PYTHONSTARTUP environment variable at IPython # startup. # c.InteractiveShellApp.exec_PYTHONSTARTUP = True # lines of code to run at IPython startup. # c.InteractiveShellApp.exec_lines = [] # Enable GUI event loop integration with any of ('glut', 'gtk', 'gtk3', 'none', # 'osx', 'pyglet', 'qt', 'qt4', 'tk', 'wx'). # c.InteractiveShellApp.gui = None # Pre-load matplotlib and numpy for interactive use, selecting a particular # matplotlib backend and loop integration. # c.InteractiveShellApp.pylab = None # Configure matplotlib for interactive use with the default matplotlib backend. # c.InteractiveShellApp.matplotlib = None # If true, IPython will populate the user namespace with numpy, pylab, etc. and # an ``import *`` is done from numpy and pylab, when using pylab mode. # # When False, pylab mode should not import any names into the user namespace. # c.InteractiveShellApp.pylab_import_all = True # A list of dotted module names of IPython extensions to load. # c.InteractiveShellApp.extensions = [] # Run the module as a script. # c.InteractiveShellApp.module_to_run = '' # Should variables loaded at startup (by startup files, exec_lines, etc.) be # hidden from tools like %who? # c.InteractiveShellApp.hide_initial_ns = True # dotted module name of an IPython extension to load. # c.InteractiveShellApp.extra_extension = '' # List of files to run at IPython startup. # c.InteractiveShellApp.exec_files = [] # A file to be run # c.InteractiveShellApp.file_to_run = '' #------------------------------------------------------------------------------ # TerminalIPythonApp configuration #------------------------------------------------------------------------------ # TerminalIPythonApp will inherit config from: BaseIPythonApplication, # Application, InteractiveShellApp # Run the file referenced by the PYTHONSTARTUP environment variable at IPython # startup. # c.TerminalIPythonApp.exec_PYTHONSTARTUP = True # Pre-load matplotlib and numpy for interactive use, selecting a particular # matplotlib backend and loop integration. # c.TerminalIPythonApp.pylab = None # Create a massive crash report when IPython encounters what may be an internal # error. The default is to append a short message to the usual traceback # c.TerminalIPythonApp.verbose_crash = False # Run the module as a script. # c.TerminalIPythonApp.module_to_run = '' # The date format used by logging formatters for %(asctime)s # c.TerminalIPythonApp.log_datefmt = '%Y-%m-%d %H:%M:%S' # Whether to overwrite existing config files when copying # c.TerminalIPythonApp.overwrite = False # Execute the given command string. # c.TerminalIPythonApp.code_to_run = '' # Set the log level by value or name. # c.TerminalIPythonApp.log_level = 30 # lines of code to run at IPython startup. c.TerminalIPythonApp.exec_lines = [ "import os, re, sys" ] # Suppress warning messages about legacy config files # c.TerminalIPythonApp.ignore_old_config = False # Path to an extra config file to load. # # If specified, load this config file in addition to any other IPython config. # c.TerminalIPythonApp.extra_config_file = u'' # Should variables loaded at startup (by startup files, exec_lines, etc.) be # hidden from tools like %who? # c.TerminalIPythonApp.hide_initial_ns = True # dotted module name of an IPython extension to load. # c.TerminalIPythonApp.extra_extension = '' # A file to be run # c.TerminalIPythonApp.file_to_run = '' # The IPython profile to use. # c.TerminalIPythonApp.profile = u'default' # Configure matplotlib for interactive use with the default matplotlib backend. # c.TerminalIPythonApp.matplotlib = None # If a command or file is given via the command-line, e.g. 'ipython foo.py', # start an interactive shell after executing the file or command. # c.TerminalIPythonApp.force_interact = False # If true, IPython will populate the user namespace with numpy, pylab, etc. and # an ``import *`` is done from numpy and pylab, when using pylab mode. # # When False, pylab mode should not import any names into the user namespace. # c.TerminalIPythonApp.pylab_import_all = True # The name of the IPython directory. This directory is used for logging # configuration (through profiles), history storage, etc. The default is usually # $HOME/.ipython. This options can also be specified through the environment # variable IPYTHONDIR. # c.TerminalIPythonApp.ipython_dir = u'' # Whether to display a banner upon starting IPython. c.TerminalIPythonApp.display_banner = False # Whether to install the default config files into the profile dir. If a new # profile is being created, and IPython contains config files for that profile, # then they will be staged into the new directory. Otherwise, default config # files will be automatically generated. # c.TerminalIPythonApp.copy_config_files = False # List of files to run at IPython startup. # c.TerminalIPythonApp.exec_files = [] # Enable GUI event loop integration with any of ('glut', 'gtk', 'gtk3', 'none', # 'osx', 'pyglet', 'qt', 'qt4', 'tk', 'wx'). # c.TerminalIPythonApp.gui = None # A list of dotted module names of IPython extensions to load. # c.TerminalIPythonApp.extensions = [] # Start IPython quickly by skipping the loading of config files. # c.TerminalIPythonApp.quick = False # The Logging format template # c.TerminalIPythonApp.log_format = '[%(name)s]%(highlevel)s %(message)s' #------------------------------------------------------------------------------ # TerminalInteractiveShell configuration #------------------------------------------------------------------------------ # TerminalInteractiveShell will inherit config from: InteractiveShell # auto editing of files with syntax errors. # c.TerminalInteractiveShell.autoedit_syntax = False # Use colors for displaying information about objects. Because this information # is passed through a pager (like 'less'), and some pagers get confused with # color codes, this capability can be turned off. # c.TerminalInteractiveShell.color_info = True # A list of ast.NodeTransformer subclass instances, which will be applied to # user input before code is run. # c.TerminalInteractiveShell.ast_transformers = [] # # c.TerminalInteractiveShell.history_length = 10000 # Don't call post-execute functions that have failed in the past. # c.TerminalInteractiveShell.disable_failing_post_execute = False # Show rewritten input, e.g. for autocall. # c.TerminalInteractiveShell.show_rewritten_input = True # Set the color scheme (NoColor, Linux, or LightBG). c.TerminalInteractiveShell.colors = 'Linux' # Autoindent IPython code entered interactively. # c.TerminalInteractiveShell.autoindent = True # # c.TerminalInteractiveShell.separate_in = '\n' # Deprecated, use PromptManager.in2_template # c.TerminalInteractiveShell.prompt_in2 = ' .\\D.: ' # # c.TerminalInteractiveShell.separate_out = '' # Deprecated, use PromptManager.in_template # c.TerminalInteractiveShell.prompt_in1 = 'In [\\#]: ' # Make IPython automatically call any callable object even if you didn't type # explicit parentheses. For example, 'str 43' becomes 'str(43)' automatically. # The value can be '0' to disable the feature, '1' for 'smart' autocall, where # it is not applied if there are no more arguments on the line, and '2' for # 'full' autocall, where all callable objects are automatically called (even if # no arguments are present). c.TerminalInteractiveShell.autocall = 1 # Number of lines of your screen, used to control printing of very long strings. # Strings longer than this number of lines will be sent through a pager instead # of directly printed. The default value for this is 0, which means IPython # will auto-detect your screen size every time it needs to print certain # potentially long strings (this doesn't change the behavior of the 'print' # keyword, it's only triggered internally). If for some reason this isn't # working well (it needs curses support), specify it yourself. Otherwise don't # change the default. # c.TerminalInteractiveShell.screen_length = 0 # Set the editor used by IPython (default to $EDITOR/vi/notepad). # c.TerminalInteractiveShell.editor = u'subl' # Deprecated, use PromptManager.justify # c.TerminalInteractiveShell.prompts_pad_left = True # The part of the banner to be printed before the profile # c.TerminalInteractiveShell.banner1 = 'Python 2.7.5 (default, Mar 9 2014, 22:15:05) \nType "copyright", "credits" or "license" for more information.\n\nIPython 2.4.1 -- An enhanced Interactive Python.\n? -> Introduction and overview of IPython\'s features.\n%quickref -> Quick reference.\nhelp -> Python\'s own help system.\nobject? -> Details about \'object\', use \'object??\' for extra details.\n' # # c.TerminalInteractiveShell.readline_parse_and_bind = ['tab: complete', '"\\C-l": clear-screen', 'set show-all-if-ambiguous on', '"\\C-o": tab-insert', '"\\C-r": reverse-search-history', '"\\C-s": forward-search-history', '"\\C-p": history-search-backward', '"\\C-n": history-search-forward', '"\\e[A": history-search-backward', '"\\e[B": history-search-forward', '"\\C-k": kill-line', '"\\C-u": unix-line-discard'] # The part of the banner to be printed after the profile # c.TerminalInteractiveShell.banner2 = '' # # c.TerminalInteractiveShell.separate_out2 = '' # # c.TerminalInteractiveShell.wildcards_case_sensitive = True # # c.TerminalInteractiveShell.debug = False # Set to confirm when you try to exit IPython with an EOF (Control-D in Unix, # Control-Z/Enter in Windows). By typing 'exit' or 'quit', you can force a # direct exit without any confirmation. c.TerminalInteractiveShell.confirm_exit = False # # c.TerminalInteractiveShell.ipython_dir = '' # # c.TerminalInteractiveShell.readline_remove_delims = '-/~' # Start logging to the default log file. # c.TerminalInteractiveShell.logstart = False # The name of the logfile to use. # c.TerminalInteractiveShell.logfile = '' c.TerminalInteractiveShell.display_page = False # The shell program to be used for paging. # c.TerminalInteractiveShell.pager = 'less' # Enable magic commands to be called without the leading %. # c.TerminalInteractiveShell.automagic = True # Save multi-line entries as one entry in readline history # c.TerminalInteractiveShell.multiline_history = True # # c.TerminalInteractiveShell.readline_use = True # Enable deep (recursive) reloading by default. IPython can use the deep_reload # module which reloads changes in modules recursively (it replaces the reload() # function, so you don't need to change anything to use it). deep_reload() # forces a full reload of modules whose code may have changed, which the default # reload() function does not. When deep_reload is off, IPython will use the # normal reload(), but deep_reload will still be available as dreload(). # c.TerminalInteractiveShell.deep_reload = False # Start logging to the given file in append mode. # c.TerminalInteractiveShell.logappend = '' # # c.TerminalInteractiveShell.xmode = 'Context' # # c.TerminalInteractiveShell.quiet = False # Enable auto setting the terminal title. # c.TerminalInteractiveShell.term_title = False # # c.TerminalInteractiveShell.object_info_string_level = 0 # Deprecated, use PromptManager.out_template # c.TerminalInteractiveShell.prompt_out = 'Out[\\#]: ' # Set the size of the output cache. The default is 1000, you can change it # permanently in your config file. Setting it to 0 completely disables the # caching system, and the minimum value accepted is 20 (if you provide a value # less than 20, it is reset to 0 and a warning is issued). This limit is # defined because otherwise you'll spend more time re-flushing a too small cache # than working # c.TerminalInteractiveShell.cache_size = 1000 # 'all', 'last', 'last_expr' or 'none', specifying which nodes should be run # interactively (displaying output from expressions). # c.TerminalInteractiveShell.ast_node_interactivity = 'last_expr' # Automatically call the pdb debugger after every exception. # c.TerminalInteractiveShell.pdb = False #------------------------------------------------------------------------------ # PromptManager configuration #------------------------------------------------------------------------------ # This is the primary interface for producing IPython's prompts. # Output prompt. '\#' will be transformed to the prompt number # c.PromptManager.out_template = 'Out[\\#]: ' # Continuation prompt. # c.PromptManager.in2_template = ' .\\D.: ' # If True (default), each prompt will be right-aligned with the preceding one. # c.PromptManager.justify = True # Input prompt. '\#' will be transformed to the prompt number # c.PromptManager.in_template = 'In [\\#]: ' # # c.PromptManager.color_scheme = 'Linux' #------------------------------------------------------------------------------ # HistoryManager configuration #------------------------------------------------------------------------------ # A class to organize all history-related functionality in one place. # HistoryManager will inherit config from: HistoryAccessor # Should the history database include output? (default: no) # c.HistoryManager.db_log_output = False # Write to database every x commands (higher values save disk access & power). # Values of 1 or less effectively disable caching. # c.HistoryManager.db_cache_size = 0 # Path to file to use for SQLite history database. # # By default, IPython will put the history database in the IPython profile # directory. If you would rather share one history among profiles, you can set # this value in each, so that they are consistent. # # Due to an issue with fcntl, SQLite is known to misbehave on some NFS mounts. # If you see IPython hanging, try setting this to something on a local disk, # e.g:: # # ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite # c.HistoryManager.hist_file = u'' # Options for configuring the SQLite connection # # These options are passed as keyword args to sqlite3.connect when establishing # database conenctions. # c.HistoryManager.connection_options = {} # enable the SQLite history # # set enabled=False to disable the SQLite history, in which case there will be # no stored history, no SQLite connection, and no background saving thread. # This may be necessary in some threaded environments where IPython is embedded. # c.HistoryManager.enabled = True #------------------------------------------------------------------------------ # ProfileDir configuration #------------------------------------------------------------------------------ # An object to manage the profile directory and its resources. # # The profile directory is used by all IPython applications, to manage # configuration, logging and security. # # This object knows how to find, create and manage these directories. This # should be used by any code that wants to handle profiles. # Set the profile location directly. This overrides the logic used by the # `profile` option. # c.ProfileDir.location = u'' #------------------------------------------------------------------------------ # PlainTextFormatter configuration #------------------------------------------------------------------------------ # The default pretty-printer. # # This uses :mod:`IPython.lib.pretty` to compute the format data of the object. # If the object cannot be pretty printed, :func:`repr` is used. See the # documentation of :mod:`IPython.lib.pretty` for details on how to write pretty # printers. Here is a simple example:: # # def dtype_pprinter(obj, p, cycle): # if cycle: # return p.text('dtype(...)') # if hasattr(obj, 'fields'): # if obj.fields is None: # p.text(repr(obj)) # else: # p.begin_group(7, 'dtype([') # for i, field in enumerate(obj.descr): # if i > 0: # p.text(',') # p.breakable() # p.pretty(field) # p.end_group(7, '])') # PlainTextFormatter will inherit config from: BaseFormatter # # c.PlainTextFormatter.type_printers = {} # # c.PlainTextFormatter.newline = '\n' # # c.PlainTextFormatter.float_precision = '' # # c.PlainTextFormatter.verbose = False # # c.PlainTextFormatter.deferred_printers = {} # # c.PlainTextFormatter.pprint = True # c.PlainTextFormatter.max_width = 99 # # c.PlainTextFormatter.singleton_printers = {} #------------------------------------------------------------------------------ # IPCompleter configuration #------------------------------------------------------------------------------ # Extension of the completer class with IPython-specific features # IPCompleter will inherit config from: Completer # Instruct the completer to omit private method names # # Specifically, when completing on ``object.<tab>``. # # When 2 [default]: all names that start with '_' will be excluded. # # When 1: all 'magic' names (``__foo__``) will be excluded. # # When 0: nothing will be excluded. # c.IPCompleter.omit__names = 2 # Whether to merge completion results into a single list # # If False, only the completion results from the first non-empty completer will # be returned. # c.IPCompleter.merge_completions = True # Instruct the completer to use __all__ for the completion # # Specifically, when completing on ``object.<tab>``. # # When True: only those names in obj.__all__ will be included. # # When False [default]: the __all__ attribute is ignored # c.IPCompleter.limit_to__all__ = False # Activate greedy completion # # This will enable completion on elements of lists, results of function calls, # etc., but can be unsafe because the code is actually evaluated on TAB. # c.IPCompleter.greedy = False #------------------------------------------------------------------------------ # ScriptMagics configuration #------------------------------------------------------------------------------ # Magics for talking to scripts # # This defines a base `%%script` cell magic for running a cell with a program in # a subprocess, and registers a few top-level magics that call %%script with # common interpreters. # Extra script cell magics to define # # This generates simple wrappers of `%%script foo` as `%%foo`. # # If you want to add script magics that aren't on your path, specify them in # script_paths # c.ScriptMagics.script_magics = [] # Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby' # # Only necessary for items in script_magics where the default path will not find # the right interpreter. # c.ScriptMagics.script_paths = {} #------------------------------------------------------------------------------ # StoreMagics configuration #------------------------------------------------------------------------------ # Lightweight persistence for python variables. # # Provides the %store magic. # If True, any %store-d variables will be automatically restored when IPython # starts. # c.StoreMagics.autorestore = False c.AliasManager.user_aliases = [ ('la', 'ls -al'), ('md', 'mkdir') ]
mit
ddtm/dl-course
Seminar1/zebrafish_drawing_factory.py
3
2387
import numpy import matplotlib.pyplot as plt import matplotlib.cm as cm def preparePlot(xticks, yticks, figsize=(10.5, 6), hideLabels=False, gridColor='#999999', gridWidth=1.0): """Template for generating the plot layout.""" plt.close() fig, ax = plt.subplots(figsize=figsize, facecolor='white', edgecolor='white') ax.axes.tick_params(labelcolor='#999999', labelsize='10') for axis, ticks in [(ax.get_xaxis(), xticks), (ax.get_yaxis(), yticks)]: axis.set_ticks_position('none') axis.set_ticks(ticks) axis.label.set_color('#999999') if hideLabels: axis.set_ticklabels([]) plt.grid(color=gridColor, linewidth=gridWidth, linestyle='-') map(lambda position: ax.spines[position].set_visible(False), ['bottom', 'top', 'left', 'right']) return fig, ax def draw_component(component): image = component.reshape(230, 202).T fig, ax = preparePlot(numpy.arange(0, 10, 1), numpy.arange(0, 10, 1), figsize=(9.0, 7.2), hideLabels=True) ax.grid(False) image = plt.imshow(image,interpolation='nearest', aspect='auto', cmap=cm.gray) plt.show() # Adapted from python-thunder's Colorize.transform where cmap='polar'. # Checkout the library at: https://github.com/thunder-project/thunder and # http://thunder-project.org/ import numpy as np def polarTransform(scale, img): """Convert points from cartesian to polar coordinates and map to colors.""" from matplotlib.colors import hsv_to_rgb img = np.asarray(img) dims = img.shape phi = ((np.arctan2(-img[0], -img[1]) + np.pi/2) % (np.pi*2)) / (2 * np.pi) rho = np.sqrt(img[0]**2 + img[1]**2) saturation = np.ones((dims[1], dims[2])) out = hsv_to_rgb(np.dstack((phi, saturation, scale * rho))) return np.clip(out * scale, 0, 1) def draw_components(*components): assert len(components)==2,"this method only accepts 2 components at once" components = [i.reshape(230, 202).T for i in components] # Use the same transformation on the image data # Try changing the first parameter to lower values brainmap = polarTransform(2.0, components) # generate layout and plot data fig, ax = preparePlot(np.arange(0, 10, 1), np.arange(0, 10, 1), figsize=(9.0, 7.2), hideLabels=True) ax.grid(False) image = plt.imshow(brainmap,interpolation='nearest', aspect='auto') plt.show()
mit
fanshi118/Time-Out-NY-with-ML-Revisited
ml_models/ada_train.py
1
4381
import numpy as np, scipy.sparse as sp import random, sys from collections import defaultdict from sklearn.feature_extraction import FeatureHasher from sklearn.ensemble import AdaBoostClassifier from sklearn.metrics import roc_curve, roc_auc_score, confusion_matrix from sklearn.externals import joblib import data_helper import matplotlib.pyplot as plt plt.style.use("fivethirtyeight") # paths to data _tr = "../data/train_data.csv" _vv = "../data/valid_visible.csv" _vp = "../data/valid_predict.csv" def generate_interaction(_tr, _vv, _vp): print "Creating user venue-interaction lists" _, all_venues = data_helper.get_unique(_tr, users=False, venues=True) train_pairs, valid_pairs = data_helper.get_user_venue_pairs(_tr, _vv, _vp) return all_venues, train_pairs, valid_pairs def generate_features(all_venues, yay_venues): yay_pairs, nay_pairs = [], [] # positive examples for venue1 in yay_venues: venue_pairs = dict() for venue2 in yay_venues: # skip itself to avoid overfitting if venue1 != venue2: venue_pairs["%s_%s" % (venue1, venue2)] = 1. yay_pairs.append(venue_pairs) # negative examples nay_venues = all_venues - yay_venues for venue1 in random.sample(nay_venues, len(yay_venues)): venue_pairs = dict() for venue2 in yay_venues: venue_pairs["%s_%s" % (venue1, venue2)] = 1. nay_pairs.append(venue_pairs) labels = np.hstack([np.ones(len(yay_pairs)), np.zeros(len(nay_pairs))]) return labels, yay_pairs, nay_pairs def train_and_score(_tr, _vv, _vp, model_sizes, colors=None): all_venues, train_pairs, valid_pairs = generate_interaction(_tr, _vv, _vp) print "Creating models" # plt.figure(figsize=(10,10)); lw = 2 roc_aucs = [] for size in model_sizes: extractor = FeatureHasher(n_features=2**size) model = AdaBoostClassifier(n_estimators=200, learning_rate=0.5) all_labels, all_paris = [], [] print "Training" for i, (user, yay_venues) in enumerate(train_pairs.iteritems()): print "Training on user", i, user labels, yay_pairs, nay_pairs = generate_features(all_venues, yay_venues) all_labels.extend(labels), all_paris.extend(yay_pairs+nay_pairs) all_features = extractor.transform(all_paris) model.fit(all_features, all_labels) print "Testing" all_labels, all_paris, all_preds, all_probas = [], [], [], [] for i, (user, yay_venues) in enumerate(valid_pairs.iteritems()): print "Testing on user", i, user labels, yay_pairs, nay_pairs = generate_features(all_venues, yay_venues) all_labels.extend(labels), all_paris.extend(yay_pairs+nay_pairs) all_features = extractor.transform(all_paris) all_preds, all_probas = model.predict(all_features), model.predict_proba(all_features)[:, 1] print "Scoring" roc_auc = roc_auc_score(all_labels, all_probas) cm = confusion_matrix(all_labels, all_preds) print "Model size", size, "AUC", roc_auc print cm roc_aucs.append(roc_auc) # fpr, tpr, _ = roc_curve(all_labels, all_probas) # plt.plot(fpr, tpr, color=color, # lw=lw, label='Model %d (area = %0.2f)' % (size, roc_auc)) ''' joblib.dump(model, 'model_rf_size%d.pkl' % size) np.save("labels_rf_size%d.npy" % size, all_labels) np.save("probas_rf_size%d.npy" % size, all_probas) plt.plot([0, 1], [0, 1], color='navy', lw=lw, ls='--', label='Luck') plt.xlim([-.05, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic for different model sizes') plt.legend(loc="lower right") plt.savefig('../plots/model_rf_200.png') plt.tight_layout() plt.show() ''' plt.figure(figsize=(15,9)) plt.plot(model_sizes, roc_aucs, c='gray', ls='dashed', lw=1) for model_size, roc_auc in zip(model_sizes, roc_aucs): plt.plot(model_size, roc_auc, "*", markersize=12) plt.xlim([model_sizes[0]-.1, model_sizes[-1]+.1]) plt.ylim([0.5, 0.85]) plt.xlabel("Model Size") plt.ylabel("ROC AUC Score") plt.title('ROC AUC score for different model sizes') plt.savefig('../plots/auc_by_model_size_ada.png') plt.tight_layout() plt.show() def main(): # model size by number of bits # model_size = int(sys.argv[1]) model_sizes = range(10, 22) # model_sizes = [20] # colors = ['darkorange', 'skyblue', 'forestgreen'] # colors = ['darkorange', 'skyblue', 'forestgreen', 'darkslategray', 'firebrick'] train_and_score(_tr, _vv, _vp, model_sizes) if __name__=="__main__": main()
mit
jaidevd/scikit-learn
examples/exercises/plot_cv_digits.py
135
1223
""" ============================================= Cross-validation on Digits Dataset Exercise ============================================= A tutorial exercise using Cross-validation with an SVM on the Digits dataset. This exercise is used in the :ref:`cv_generators_tut` part of the :ref:`model_selection_tut` section of the :ref:`stat_learn_tut_index`. """ print(__doc__) import numpy as np from sklearn.model_selection import cross_val_score from sklearn import datasets, svm digits = datasets.load_digits() X = digits.data y = digits.target svc = svm.SVC(kernel='linear') C_s = np.logspace(-10, 0, 10) scores = list() scores_std = list() for C in C_s: svc.C = C this_scores = cross_val_score(svc, X, y, n_jobs=1) scores.append(np.mean(this_scores)) scores_std.append(np.std(this_scores)) # Do the plotting import matplotlib.pyplot as plt plt.figure(1, figsize=(4, 3)) plt.clf() plt.semilogx(C_s, scores) plt.semilogx(C_s, np.array(scores) + np.array(scores_std), 'b--') plt.semilogx(C_s, np.array(scores) - np.array(scores_std), 'b--') locs, labels = plt.yticks() plt.yticks(locs, list(map(lambda x: "%g" % x, locs))) plt.ylabel('CV score') plt.xlabel('Parameter C') plt.ylim(0, 1.1) plt.show()
bsd-3-clause
GoogleCloudPlatform/hpc-monte-carlo
model/randomwalk.py
1
4240
#!/usr/bin/env python # Copyright 2018 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import argparse import csv import math import pickle from os import path, makedirs from random import randint from sys import exit, stdout, stderr from time import sleep import numpy import pandas TRADING_DAYS = 252 # Number of trading days on stock class DataModel(object): def __init__(self, company, start_date): self.verbose = False self.company = company self.start_date = start_date self._raw_data = None self.data = None self.iter_count = 1000 self.from_csv = None def _from_csv(self, file_path): data = {} with open(file_path, 'rb') as csv_file: csv_reader = csv.DictReader(csv_file) for line in csv_reader: if line['ticker'] == self.company: data[pandas.Timestamp(line['date'])] = line line['Close'] = numpy.float64(line['close']) del line['ticker'] del line['date'] self._raw_data = pandas.DataFrame.from_dict(data, orient='index') def _write_csv(self): csv_writer = csv.writer(stdout) for row in self.data: csv_writer.writerow(row) def _get_data(self): marketd = self._raw_data # # calculate the compound annual growth rate (CAGR) which will give us # our mean return input (mu) # days = (marketd.index[-1] - marketd.index[0]).days cagr = (marketd['Close'][-1] / marketd['Close'][1]) ** (365.0 / days)-1 # # create a series of percentage returns and calculate the annual # volatility of returns # marketd['Returns'] = marketd['Close'].pct_change() vol = marketd['Returns'].std() * numpy.sqrt(TRADING_DAYS) data = [] starting_price = marketd['Close'][-1] position = randint(10, 1000) * 10 for i in xrange(self.iter_count): daily_returns = numpy.random.normal(cagr / TRADING_DAYS, vol / math.sqrt(TRADING_DAYS), TRADING_DAYS) + 1 price_list = [self.company, position, i, starting_price] for x in daily_returns: price_list.append(price_list[-1] * x) data.append(price_list) self.data = data def run(self): if self.from_csv: self._from_csv(self.from_csv) self._get_data() self._write_csv() def _parse_args(): parser = argparse.ArgumentParser('randomwalk', description='Monte-Carlo simulation of stock prices ' 'behavior based on data from quandl') parser.add_argument('-n', '--snum', type=int, default=1000, help='number of simulations (default:%(default)s)') parser.add_argument('-c', '--company', required=True, help='company symbol on stock (i. e. WDC)') parser.add_argument('--from-csv', help='path to wiki csv file') parser.add_argument('-s', '--start-date', default='2018-01-01', help='example: %(default)s') return parser.parse_args() def main(): args = _parse_args() data_model = DataModel(args.company, args.start_date) data_model.from_csv = args.from_csv data_model.run() if __name__ == '__main__': main()
apache-2.0
rsheftel/pandas_market_calendars
pandas_market_calendars/exchange_calendar_cme.py
1
9791
# # Copyright 2016 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. from datetime import time from itertools import chain from pandas import Timestamp from pandas.tseries.holiday import AbstractHolidayCalendar, GoodFriday, USLaborDay, USPresidentsDay, USThanksgivingDay from pytz import timezone from .exchange_calendar_nyse import NYSEExchangeCalendar from .holidays_us import (Christmas, ChristmasEveBefore1993, ChristmasEveInOrAfter1993, USBlackFridayInOrAfter1993, USIndependenceDay, USMartinLutherKingJrAfter1998, USMemorialDay, USNationalDaysofMourning, USNewYearsDay) from .market_calendar import MarketCalendar # Useful resources for making changes to this file: http://www.cmegroup.com/tools-information/holiday-calendar.html # The CME has different holiday rules depending on the type of instrument. # For example, http://www.cmegroup.com/tools-information/holiday-calendar/files/2016-4th-of-july-holiday-schedule.pdf # noqa # shows that Equity, Interest Rate, FX, Energy, Metals & DME Products close at 1200 CT on July 4, 2016, while Grain, # Oilseed & MGEX Products and Livestock, Dairy & Lumber products are completely closed. class CMEEquityExchangeCalendar(MarketCalendar): """ Exchange calendar for CME for Equity products Open Time: 6:00 PM, America/New_York / 5:00 PM Chicago Close Time: 5:00 PM, America/New_York / 4:00 PM Chicago Break: 4:15 - 4:30pm America/New_York / 3:15 - 3:30 PM Chicago """ aliases = ['CME_Equity', 'CBOT_Equity'] @property def name(self): return "CME_Equity" @property def tz(self): return timezone('America/Chicago') @property def open_time_default(self): return time(17, 0, tzinfo=self.tz) @property def close_time_default(self): return time(16, 0, tzinfo=self.tz) @property def open_offset(self): return -1 @property def break_start(self): return time(15, 15) @property def break_end(self): return time(15, 30) @property def regular_holidays(self): # Many days that are holidays for the NYSE are an early close day for CME return AbstractHolidayCalendar(rules=[ USNewYearsDay, GoodFriday, Christmas, ]) @property def adhoc_holidays(self): return USNationalDaysofMourning @property def special_closes(self): return [( time(12), AbstractHolidayCalendar(rules=[ USMartinLutherKingJrAfter1998, USPresidentsDay, USMemorialDay, USLaborDay, USIndependenceDay, USThanksgivingDay, USBlackFridayInOrAfter1993, ChristmasEveBefore1993, ChristmasEveInOrAfter1993, ]) )] class CMEAgricultureExchangeCalendar(MarketCalendar): """ Exchange calendar for CME for Agriculture products Open Time: 5:00 PM, America/Chicago Close Time: 5:00 PM, America/Chicago Regularly-Observed Holidays: - New Years Day - Good Friday - Christmas """ aliases = ['CME_Agriculture', 'CBOT_Agriculture', 'COMEX_Agriculture', 'NYMEX_Agriculture'] @property def name(self): return "CME_Agriculture" @property def tz(self): return timezone('America/Chicago') @property def open_time_default(self): return time(17, 1, tzinfo=self.tz) @property def close_time_default(self): return time(17, tzinfo=self.tz) @property def open_offset(self): return -1 @property def regular_holidays(self): # Ignore gap between 13:20 CST and 14:30 CST for regular trading hours # # The CME has different holiday rules depending on the type of # instrument. For example, http://www.cmegroup.com/tools-information/holiday-calendar/files/2016-4th-of-july-holiday-schedule.pdf # noqa # shows that Equity, Interest Rate, FX, Energy, Metals & DME Products # close at 1200 CT on July 4, 2016, while Grain, Oilseed & MGEX # Products and Livestock, Dairy & Lumber products are completely # closed. return AbstractHolidayCalendar(rules=[ USNewYearsDay, USMartinLutherKingJrAfter1998, USPresidentsDay, GoodFriday, USMemorialDay, USIndependenceDay, USLaborDay, USThanksgivingDay, Christmas, ]) @property def adhoc_holidays(self): return USNationalDaysofMourning @property def special_closes(self): return [( time(12), AbstractHolidayCalendar(rules=[ USBlackFridayInOrAfter1993, ChristmasEveBefore1993, ChristmasEveInOrAfter1993, ]) )] # For the bond market Good Friday that coincides with the release of NFP on the first friday of the month is an open day goodFridayClosed = ['1970-03-27', '1971-04-09', '1972-03-31', '1973-04-20', '1974-04-12', '1975-03-28', '1976-04-16', '1977-04-08', '1978-03-24', '1979-04-13', '1981-04-17', '1982-04-09', '1984-04-20', '1986-03-28', '1987-04-17', '1989-03-24', '1990-04-13', '1991-03-29', '1992-04-17', '1993-04-09', '1995-04-14', '1997-03-28', '1998-04-10', '2000-04-21', '2001-04-13', '2002-03-29', '2003-04-18', '2004-04-09', '2005-03-25', '2006-04-14', '2008-03-21', '2009-04-10', '2011-04-22', '2013-03-29', '2014-04-18', '2016-03-25', '2017-04-14', '2018-03-30', '2019-04-19', '2020-04-10', '2022-04-15', '2024-03-29', '2025-04-18', '2027-03-26', '2028-04-14', '2029-03-30', '2030-04-19', '2031-04-11', '2032-03-26', '2033-04-15', '2035-03-23', '2036-04-11', '2038-04-23', '2039-04-08', '2040-03-30', '2041-04-19', '2043-03-27', '2044-04-15', '2046-03-23', '2047-04-12', '2049-04-16', '2050-04-08', '2051-03-31', '2052-04-19', '2054-03-27', '2055-04-16', '2056-03-31', '2057-04-20', '2058-04-12', '2059-03-28', '2060-04-16', '2061-04-08', '2062-03-24', '2063-04-13', '2065-03-27', '2066-04-09', '2068-04-20', '2069-04-12', '2070-03-28', '2071-04-17', '2072-04-08', '2073-03-24', '2074-04-13', '2076-04-17', '2077-04-09', '2079-04-21', '2081-03-28', '2082-04-17', '2084-03-24', '2085-04-13', '2086-03-29', '2087-04-18', '2088-04-09', '2090-04-14', '2092-03-28', '2093-04-10', '2095-04-22', '2096-04-13', '2097-03-29', '2098-04-18', '2099-04-10'] BondsGoodFridayClosed = [Timestamp(x, tz='UTC') for x in goodFridayClosed] goodFridayOpen = ['1980-04-04', '1983-04-01', '1985-04-05', '1988-04-01', '1994-04-01', '1996-04-05', '1999-04-02', '2007-04-06', '2010-04-02', '2012-04-06', '2015-04-03', '2021-04-02', '2023-04-07', '2026-04-03', '2034-04-07', '2037-04-03', '2042-04-04', '2045-04-07', '2048-04-03', '2053-04-04', '2064-04-04', '2067-04-01', '2075-04-05', '2078-04-01', '2080-04-05', '2083-04-02', '2089-04-01', '2091-04-06', '2094-04-02'] BondsGoodFridayOpen = [Timestamp(x, tz='UTC') for x in goodFridayOpen] class CMEBondExchangeCalendar(MarketCalendar): """ Exchange calendar for CME for Interest Rate and Bond products The Holiday calendar is different between the open outcry trading floor hours and GLOBEX electronic trading hours. This calendar attempts to be accurate for the GLOBEX holidays and hours from approx 2010 onward. """ aliases = ['CME_Rate', 'CBOT_Rate', 'CME_InterestRate', 'CBOT_InterestRate', 'CME_Bond', 'CBOT_Bond'] @property def name(self): return "CME_Bond" @property def tz(self): return timezone('America/Chicago') @property def open_time_default(self): return time(17, tzinfo=self.tz) @property def close_time_default(self): return time(16, tzinfo=self.tz) @property def open_offset(self): return -1 @property def regular_holidays(self): return AbstractHolidayCalendar(rules=[ USNewYearsDay, Christmas, ]) @property def adhoc_holidays(self): return list(chain(USNationalDaysofMourning, BondsGoodFridayClosed)) @property def special_closes(self): return [ (time(12), AbstractHolidayCalendar(rules=[ USMartinLutherKingJrAfter1998, USPresidentsDay, USMemorialDay, USIndependenceDay, USLaborDay, USThanksgivingDay, ])), (time(12, 15), AbstractHolidayCalendar(rules=[ USBlackFridayInOrAfter1993, ChristmasEveBefore1993, ChristmasEveInOrAfter1993, ])) ] @property def special_closes_adhoc(self): return [ (time(10, tzinfo=self.tz), BondsGoodFridayOpen) ]
mit
biolab/orange
Orange/regression/pls.py
6
16929
"""\ ########################################## Partial least sqaures regression (``PLS``) ########################################## .. index:: regression .. _`Parital Least Squares Regression`: http://en.wikipedia.org/wiki/Partial_least_squares_regression `Partial least squares <http://en.wikipedia.org/wiki/Partial_least_squares_regression>`_ regression is a statistical method for simultaneous prediction of multiple response variables. Orange's implementation is based on `Scikit learn python implementation <https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/pls.py>`_. The following code shows how to fit a PLS regression model on a multi-target data set. .. literalinclude:: code/pls-example.py :lines: 7,9,13,14 .. autoclass:: PLSRegressionLearner :members: .. autoclass:: PLSRegression :members: Utility functions ----------------- .. autofunction:: normalize_matrix .. autofunction:: nipals_xy .. autofunction:: svd_xy ======== Examples ======== The following code predicts the values of output variables for the first two instances in ``data``. .. literalinclude:: code/pls-example.py :lines: 16-20 :: Actual [<orange.Value 'Y1'='0.490'>, <orange.Value 'Y2'='1.237'>, <orange.Value 'Y3'='1.808'>, <orange.Value 'Y4'='0.422'>] Predicted [<orange.Value 'Y1'='0.613'>, <orange.Value 'Y2'='0.826'>, <orange.Value 'Y3'='1.084'>, <orange.Value 'Y4'='0.534'>] Actual [<orange.Value 'Y1'='0.167'>, <orange.Value 'Y2'='-0.664'>, <orange.Value 'Y3'='-1.378'>, <orange.Value 'Y4'='0.589'>] Predicted [<orange.Value 'Y1'='0.058'>, <orange.Value 'Y2'='-0.706'>, <orange.Value 'Y3'='-1.420'>, <orange.Value 'Y4'='0.599'>] To see the coefficient of the model, print the model: .. literalinclude:: code/pls-example.py :lines: 22 :: Regression coefficients: Y1 Y2 Y3 Y4 X1 0.714 2.153 3.590 -0.078 X2 -0.238 -2.500 -4.797 -0.036 X3 0.230 -0.314 -0.880 -0.060 Note that coefficients are stored in a matrix since the model predicts values of multiple outputs. """ import Orange import numpy from Orange.regression import base from numpy import dot, zeros from numpy import linalg from numpy.linalg import svd, pinv from Orange.utils import deprecated_members, deprecated_keywords def normalize_matrix(X): """ Normalize a matrix column-wise: subtract the means and divide by standard deviations. Returns the standardized matrix, sample mean and standard deviation :param X: data matrix :type X: :class:`numpy.array` """ mu_x, sigma_x = numpy.mean(X, axis=0), numpy.std(X, axis=0) sigma_x[sigma_x == 0] = 1. return (X - mu_x)/sigma_x, mu_x, sigma_x @deprecated_keywords({"maxIter": "max_iter"}) def nipals_xy(X, Y, mode="PLS", max_iter=500, tol=1e-06): """ NIPALS algorithm; returns the first left and rigth singular vectors of X'Y. :param X, Y: data matrix :type X, Y: :class:`numpy.array` :param mode: possible values "PLS" (default) or "CCA" :type mode: string :param max_iter: maximal number of iterations (default: 500) :type max_iter: int :param tol: tolerance parameter; if norm of difference between two successive left singular vectors is less than tol, iteration is stopped :type tol: a not negative float """ yScore, uOld, ite = Y[:, [0]], 0, 1 Xpinv = Ypinv = None # Inner loop of the Wold algo. while True and ite < max_iter: # Update u: the X weights if mode == "CCA": if Xpinv is None: Xpinv = linalg.pinv(X) # compute once pinv(X) u = dot(Xpinv, yScore) else: # mode PLS # Mode PLS regress each X column on yScore u = dot(X.T, yScore) / dot(yScore.T, yScore) # Normalize u u /= numpy.sqrt(dot(u.T, u)) # Update xScore: the X latent scores xScore = dot(X, u) # Update v: the Y weights if mode == "CCA": if Ypinv is None: Ypinv = linalg.pinv(Y) # compute once pinv(Y) v = dot(Ypinv, xScore) else: # Mode PLS regress each X column on yScore v = dot(Y.T, xScore) / dot(xScore.T, xScore) # Normalize v v /= numpy.sqrt(dot(v.T, v)) # Update yScore: the Y latent scores yScore = dot(Y, v) uDiff = u - uOld if dot(uDiff.T, uDiff) < tol or Y.shape[1] == 1: break uOld = u ite += 1 return u, v def svd_xy(X, Y): """ Return the first left and right singular vectors of X'Y. :param X, Y: data matrix :type X, Y: :class:`numpy.array` """ U, s, V = svd(dot(X.T, Y), full_matrices=False) u = U[:, [0]] v = V.T[:, [0]] return u, v def select_attrs(table, attributes, class_var=None, metas=None): """ Select ``attributes`` from the ``table`` and return a new data table. """ domain = Orange.data.Domain(attributes, class_var) if metas: domain.add_metas(metas) return Orange.data.Table(domain, table) @deprecated_members( {"nComp": "n_comp", "deflationMode": "deflation_mode", "maxIter": "max_iter"}, wrap_methods=["__init__"]) class PLSRegressionLearner(base.BaseRegressionLearner): """ Fit the partial least squares regression model, i.e. learn the regression parameters. The implementation is based on `Scikit learn python implementation`_ The class is derived from :class:`Orange.regression.base.BaseRegressionLearner` that is used for preprocessing the data (continuization and imputation) before fitting the regression parameters """ def __init__(self, n_comp=2, deflation_mode="regression", mode="PLS", algorithm="nipals", max_iter=500, imputer=None, continuizer=None, **kwds): """ .. attribute:: n_comp number of components to keep (default: 2) .. attribute:: deflation_mode "canonical" or "regression" (default) .. attribute:: mode "CCA" or "PLS" (default) .. attribute:: algorithm The algorithm for estimating the weights: "nipals" or "svd" (default) """ self.n_comp = n_comp self.deflation_mode = deflation_mode self.mode = mode self.algorithm = algorithm self.max_iter = max_iter self.set_imputer(imputer=imputer) self.set_continuizer(continuizer=continuizer) self.__dict__.update(kwds) @deprecated_keywords({"xVars": "x_vars", "yVars": "y_vars"}) def __call__(self, table, weight_id=None, x_vars=None, y_vars=None): """ :param table: data instances. :type table: :class:`Orange.data.Table` :param x_vars, y_vars: List of input and response variables (:obj:`Orange.feature.Continuous` or :obj:`Orange.feature.Discrete`). If ``None`` (default) it is assumed that the data domain provides information which variables are reponses and which are not. If data has :obj:`~Orange.data.Domain.class_var` defined in its domain, a single-target regression learner is constructed. Otherwise a multi-target learner predicting response variables defined by :obj:`~Orange.data.Domain.class_vars` is constructed. :type x_vars, y_vars: list """ domain = table.domain multitarget = False if x_vars is None and y_vars is None: # Response variables are defined in the table. x_vars = domain.features if domain.class_var: y_vars = [domain.class_var] elif domain.class_vars: y_vars = domain.class_vars multitarget = True else: raise TypeError('Class-less domain (x-vars and y-vars needed).') elif not (x_vars and y_vars): raise ValueError("Both x_vars and y_vars must be defined.") else: multitarget = True x_table = select_attrs(table, x_vars) y_table = select_attrs(table, y_vars) # dicrete values are continuized x_table = self.continuize_table(x_table) y_table = self.continuize_table(y_table) # missing values are imputed x_table = self.impute_table(x_table) y_table = self.impute_table(y_table) # Collect the new transformed x_vars/y_vars x_vars = list(x_table.domain.variables) y_vars = list(y_table.domain.variables) domain = Orange.data.Domain(x_vars + y_vars, False) x = x_table.to_numpy()[0] y = y_table.to_numpy()[0] kwargs = self.fit(x, y) return PLSRegression(domain=domain, x_vars=x_vars, y_vars=y_vars, multitarget=multitarget, **kwargs) def fit(self, X, Y): """ Fit all unknown parameters, i.e. weights, scores, loadings (for x and y) and regression coefficients. Return a dict with all of the parameters. """ # copy since this will contain the residuals (deflated) matrices X, Y = X.copy(), Y.copy() if Y.ndim == 1: Y = Y.reshape((Y.size, 1)) n, p = X.shape q = Y.shape[1] # normalization of data matrices X, muX, sigmaX = normalize_matrix(X) Y, muY, sigmaY = normalize_matrix(Y) # Residuals (deflated) matrices Xk, Yk = X, Y # Results matrices T, U = zeros((n, self.n_comp)), zeros((n, self.n_comp)) W, C = zeros((p, self.n_comp)), zeros((q, self.n_comp)) P, Q = zeros((p, self.n_comp)), zeros((q, self.n_comp)) # NIPALS over components for k in xrange(self.n_comp): # Weights estimation (inner loop) if self.algorithm == "nipals": u, v = nipals_xy(X=Xk, Y=Yk, mode=self.mode, max_iter=self.max_iter) elif self.algorithm == "svd": u, v = svd_xy(X=Xk, Y=Yk) # compute scores xScore, yScore = dot(Xk, u), dot(Yk, v) # Deflation (in place) # - regress Xk's on xScore xLoadings = dot(Xk.T, xScore) / dot(xScore.T, xScore) # - substract rank-one approximations to obtain remainder matrix Xk -= dot(xScore, xLoadings.T) if self.deflation_mode == "canonical": # - regress Yk's on yScore, then substract rank-one approx. yLoadings = dot(Yk.T, yScore) / dot(yScore.T, yScore) Yk -= dot(yScore, yLoadings.T) if self.deflation_mode == "regression": # - regress Yk's on xScore, then substract rank-one approx. yLoadings = dot(Yk.T, xScore) / dot(xScore.T, xScore) Yk -= dot(xScore, yLoadings.T) # Store weights, scores and loadings T[:, k] = xScore.ravel() # x-scores U[:, k] = yScore.ravel() # y-scores W[:, k] = u.ravel() # x-weights C[:, k] = v.ravel() # y-weights P[:, k] = xLoadings.ravel() # x-loadings Q[:, k] = yLoadings.ravel() # y-loadings # X = TP' + E and Y = UQ' + E # Rotations from input space to transformed space (scores) # T = X W(P'W)^-1 = XW* (W* : p x k matrix) # U = Y C(Q'C)^-1 = YC* (W* : q x k matrix) xRotations = dot(W, pinv(dot(P.T, W))) if Y.shape[1] > 1: yRotations = dot(C, pinv(dot(Q.T, C))) else: yRotations = numpy.ones(1) if True or self.deflation_mode == "regression": # Estimate regression coefficient # Y = TQ' + E = X W(P'W)^-1Q' + E = XB + E # => B = W*Q' (p x q) coefs = dot(xRotations, Q.T) coefs = 1. / sigmaX.reshape((p, 1)) * \ coefs * sigmaY return {"mu_x": muX, "mu_y": muY, "sigma_x": sigmaX, "sigma_y": sigmaY, "T": T, "U":U, "W":U, "C": C, "P":P, "Q":Q, "x_rotations": xRotations, "y_rotations": yRotations, "coefs": coefs} @deprecated_members( {"xVars": "x_vars", "yVars": "y_vars", "muX": "mu_x", "muY": "mu_y", "sigmaX": "sigma_x", "sigmaY": "sigma_y"}, wrap_methods=["__init__"]) class PLSRegression(Orange.classification.Classifier): """ Predict values of the response variables based on the values of independent variables. Basic notations: n - number of data instances p - number of independent variables q - number of reponse variables .. attribute:: T A n x n_comp numpy array of x-scores .. attribute:: U A n x n_comp numpy array of y-scores .. attribute:: W A p x n_comp numpy array of x-weights .. attribute:: C A q x n_comp numpy array of y-weights .. attribute:: P A p x n_comp numpy array of x-loadings .. attribute:: Q A q x n_comp numpy array of y-loading .. attribute:: coefs A p x q numpy array coefficients of the linear model: Y = X coefs + E .. attribute:: x_vars Predictor variables .. attribute:: y_vars Response variables """ def __init__(self, domain=None, multitarget=False, coefs=None, sigma_x=None, sigma_y=None, mu_x=None, mu_y=None, x_vars=None, y_vars=None, **kwargs): self.domain = domain self.multitarget = multitarget if multitarget and y_vars: self.class_vars = y_vars elif y_vars: self.class_var = y_vars[0] self.coefs = coefs self.mu_x, self.mu_y = mu_x, mu_y self.sigma_x, self.sigma_y = sigma_x, sigma_y self.x_vars, self.y_vars = x_vars, y_vars for name, val in kwargs.items(): setattr(self, name, val) def __call__(self, instance, result_type=Orange.core.GetValue): """ :param instance: data instance for which the value of the response variable will be predicted :type instance: :class:`Orange.data.Instance` """ instance = Orange.data.Instance(self.domain, instance) ins = [instance[v].native() for v in self.x_vars] if "?" in ins: # missing value -> corresponding coefficient omitted def miss_2_0(x): return x if x != "?" else 0 ins = map(miss_2_0, ins) ins = numpy.array(ins) xc = (ins - self.mu_x) predicted = dot(xc, self.coefs) + self.mu_y y_hat = [var(val) for var, val in zip(self.y_vars, predicted)] if result_type == Orange.core.GetValue: return y_hat if self.multitarget else y_hat[0] else: from Orange.statistics.distribution import Distribution probs = [] for var, val in zip(self.y_vars, y_hat): dist = Distribution(var) dist[val] = 1.0 probs.append(dist) if result_type == Orange.core.GetBoth: return (y_hat, probs) if self.multitarget else (y_hat[0], probs[0]) else: return probs if self.multitarget else probs[0] def to_string(self): """ Pretty-prints the coefficient of the PLS regression model. """ x_vars, y_vars = [x.name for x in self.x_vars], [y.name for y in self.y_vars] fmt = "%8s " + "%12.3f " * len(y_vars) first = [" " * 8 + "%13s" * len(y_vars) % tuple(y_vars)] lines = [fmt % tuple([x_vars[i]] + list(coef)) for i, coef in enumerate(self.coefs)] return '\n'.join(first + lines) def __str__(self): return self.to_string() """ def transform(self, X, Y=None): # Normalize Xc = (X - self.muX) / self.sigmaX if Y is not None: Yc = (Y - self.muY) / self.sigmaY # Apply rotation xScores = dot(Xc, self.xRotations) if Y is not None: yScores = dot(Yc, self.yRotations) return xScores, yScores return xScores """ if __name__ == "__main__": import Orange from Orange.regression import pls data = Orange.data.Table("multitarget-synthetic") l = pls.PLSRegressionLearner() x = data.domain.features y = data.domain.class_vars print x, y # c = l(data, x_vars=x, y_vars=y) c = l(data) print c
gpl-3.0
great-expectations/great_expectations
tests/cli/v012/upgrade_helpers/test_upgrade_helper_pre_v013.py
1
17400
import json import os import shutil from click.testing import CliRunner from freezegun import freeze_time from moto import mock_s3 import great_expectations from great_expectations import DataContext from great_expectations.cli.v012 import cli from great_expectations.data_context.util import file_relative_path from great_expectations.util import gen_directory_tree_str from tests.cli.v012.utils import ( VALIDATION_OPERATORS_DEPRECATION_MESSAGE, assert_no_logging_messages_or_tracebacks, ) try: from unittest import mock except ImportError: from unittest import mock def test_project_upgrade_already_up_to_date(v10_project_directory, caplog): # test great_expectations project upgrade command with project with config_version 2 # copy v2 yml shutil.copy( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/great_expectations_v2.yml" ), os.path.join(v10_project_directory, "great_expectations.yml"), ) runner = CliRunner(mix_stderr=False) result = runner.invoke( cli, ["project", "upgrade", "-d", v10_project_directory], input="\n", catch_exceptions=False, ) stdout = result.stdout assert "Checking project..." in stdout assert "Your project is up-to-date - no further upgrade is necessary." in stdout assert_no_logging_messages_or_tracebacks( my_caplog=caplog, click_result=result, allowed_deprecation_message=VALIDATION_OPERATORS_DEPRECATION_MESSAGE, ) def test_upgrade_helper_intervention_on_cli_command(v10_project_directory, caplog): # test if cli detects out of date project and asks to run upgrade helper # decline upgrade and ensure config version was not modified runner = CliRunner(mix_stderr=False) result = runner.invoke( cli, ["suite", "list", "-d", v10_project_directory], input="n\n", catch_exceptions=False, ) stdout = result.stdout assert ( "Your project appears to have an out-of-date config version (1.0) - the version number must be at least 3." in stdout ) assert "In order to proceed, your project must be upgraded." in stdout assert ( "Would you like to run the Upgrade Helper to bring your project up-to-date? [Y/n]:" in stdout ) assert ( "Ok, exiting now. To upgrade at a later time, use the following command: great_expectations project " "upgrade" in stdout ) assert ( "To learn more about the upgrade process, visit [" "36mhttps://docs.greatexpectations.io/en/latest/how_to_guides/migrating_versions.html" in stdout ) assert_no_logging_messages_or_tracebacks(caplog, result) # make sure config version unchanged assert ( DataContext.get_ge_config_version(context_root_dir=v10_project_directory) == 1 ) expected_project_tree_str = """\ great_expectations/ .gitignore great_expectations.yml checkpoints/ .gitkeep expectations/ .gitkeep notebooks/ .gitkeep plugins/ custom_store_backends/ __init__.py my_custom_store_backend.py uncommitted/ config_variables.yml data_docs/ local_site/ expectations/ .gitkeep static/ .gitkeep validations/ diabetic_data/ warning/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.html validations/ diabetic_data/ warning/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.json """ obs_project_tree_str = gen_directory_tree_str(v10_project_directory) assert obs_project_tree_str == expected_project_tree_str @freeze_time("09/26/2019 13:42:41") def test_basic_project_upgrade(v10_project_directory, caplog): # test project upgrade that requires no manual steps runner = CliRunner(mix_stderr=False) result = runner.invoke( cli, ["project", "upgrade", "-d", v10_project_directory], input="\n", catch_exceptions=False, ) stdout = result.stdout with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/test_basic_project_upgrade_expected_v012_stdout.fixture", ) ) as f: expected_stdout = f.read() expected_stdout = expected_stdout.replace( "GE_PROJECT_DIR", v10_project_directory ) assert stdout == expected_stdout expected_project_tree_str = """\ great_expectations/ .gitignore great_expectations.yml checkpoints/ .gitkeep expectations/ .ge_store_backend_id .gitkeep notebooks/ .gitkeep plugins/ custom_store_backends/ __init__.py my_custom_store_backend.py uncommitted/ config_variables.yml data_docs/ local_site/ expectations/ .gitkeep static/ .gitkeep validations/ diabetic_data/ warning/ 20200430T191246.763896Z/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.html logs/ project_upgrades/ UpgradeHelperV11_20190926T134241.000000Z.json UpgradeHelperV13_20190926T134241.000000Z.json validations/ .ge_store_backend_id diabetic_data/ warning/ 20200430T191246.763896Z/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.json """ obs_project_tree_str = gen_directory_tree_str(v10_project_directory) assert obs_project_tree_str == expected_project_tree_str # make sure config number incremented assert ( DataContext.get_ge_config_version(context_root_dir=v10_project_directory) == 3 ) with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/UpgradeHelperV11_basic_upgrade_log.json", ) ) as f: expected_upgrade_log_dict = json.load(f) expected_upgrade_log_str = json.dumps(expected_upgrade_log_dict) expected_upgrade_log_str = expected_upgrade_log_str.replace( "GE_PROJECT_DIR", v10_project_directory ) expected_upgrade_log_dict = json.loads(expected_upgrade_log_str) with open( f"{v10_project_directory}/uncommitted/logs/project_upgrades/UpgradeHelperV11_20190926T134241.000000Z.json" ) as f: obs_upgrade_log_dict = json.load(f) assert obs_upgrade_log_dict == expected_upgrade_log_dict @freeze_time("09/26/2019 13:42:41") def test_project_upgrade_with_manual_steps( v10_project_directory, caplog, sa, postgresql_engine ): # This test requires sqlalchemy because it includes database backends configured # test project upgrade that requires manual steps # copy v2 yml shutil.copy( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/great_expectations_v1_needs_manual_upgrade.yml", ), os.path.join(v10_project_directory, "great_expectations.yml"), ) runner = CliRunner(mix_stderr=False) result = runner.invoke( cli, ["project", "upgrade", "-d", v10_project_directory], input="\n", catch_exceptions=False, ) stdout = result.stdout with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/test_project_upgrade_with_manual_steps_expected_v012_stdout.fixture", ) ) as f: expected_stdout = f.read() expected_stdout = expected_stdout.replace( "GE_PROJECT_DIR", v10_project_directory ) assert stdout == expected_stdout pycache_dir_path = os.path.join( v10_project_directory, "plugins", "custom_store_backends", "__pycache__" ) try: shutil.rmtree(pycache_dir_path) except FileNotFoundError: pass expected_project_tree_str = """\ great_expectations/ .gitignore great_expectations.yml checkpoints/ .gitkeep expectations/ .ge_store_backend_id .gitkeep notebooks/ .gitkeep plugins/ custom_store_backends/ __init__.py my_custom_store_backend.py uncommitted/ config_variables.yml data_docs/ local_site/ expectations/ .gitkeep static/ .gitkeep validations/ diabetic_data/ warning/ 20200430T191246.763896Z/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.html logs/ project_upgrades/ UpgradeHelperV11_20190926T134241.000000Z.json validations/ .ge_store_backend_id diabetic_data/ warning/ 20200430T191246.763896Z/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.json """ obs_project_tree_str = gen_directory_tree_str(v10_project_directory) assert obs_project_tree_str == expected_project_tree_str # make sure config number not incremented assert ( DataContext.get_ge_config_version(context_root_dir=v10_project_directory) == 1 ) with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/UpgradeHelperV11_manual_steps_upgrade_log.json", ) ) as f: expected_upgrade_log_dict = json.load(f) expected_upgrade_log_str = json.dumps(expected_upgrade_log_dict) expected_upgrade_log_str = expected_upgrade_log_str.replace( "GE_PROJECT_DIR", v10_project_directory ) expected_upgrade_log_dict = json.loads(expected_upgrade_log_str) with open( f"{v10_project_directory}/uncommitted/logs/project_upgrades/UpgradeHelperV11_20190926T134241.000000Z.json" ) as f: obs_upgrade_log_dict = json.load(f) assert obs_upgrade_log_dict == expected_upgrade_log_dict @freeze_time("09/26/2019 13:42:41") @mock_s3 def test_project_upgrade_with_exception(v10_project_directory, caplog): # test project upgrade that requires manual steps # copy v2 yml shutil.copy( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/great_expectations_v1_basic_with_exception.yml", ), os.path.join(v10_project_directory, "great_expectations.yml"), ) runner = CliRunner(mix_stderr=False) result = runner.invoke( cli, ["project", "upgrade", "-d", v10_project_directory], input="\n", catch_exceptions=False, ) stdout = result.stdout with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/test_project_upgrade_with_exception_expected_v012_stdout.fixture", ) ) as f: expected_stdout = f.read() expected_stdout = expected_stdout.replace( "GE_PROJECT_DIR", v10_project_directory ) assert stdout == expected_stdout expected_project_tree_str = """\ great_expectations/ .gitignore great_expectations.yml checkpoints/ .gitkeep expectations/ .ge_store_backend_id .gitkeep notebooks/ .gitkeep plugins/ custom_store_backends/ __init__.py my_custom_store_backend.py uncommitted/ config_variables.yml data_docs/ local_site/ expectations/ .gitkeep static/ .gitkeep validations/ diabetic_data/ warning/ 20200430T191246.763896Z/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.html logs/ project_upgrades/ UpgradeHelperV11_20190926T134241.000000Z.json validations/ .ge_store_backend_id diabetic_data/ warning/ 20200430T191246.763896Z/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.json """ obs_project_tree_str = gen_directory_tree_str(v10_project_directory) assert obs_project_tree_str == expected_project_tree_str # make sure config number not incremented assert ( DataContext.get_ge_config_version(context_root_dir=v10_project_directory) == 1 ) with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/UpgradeHelperV11_basic_upgrade_with_exception_log.json", ) ) as f: expected_upgrade_log_dict = json.load(f) expected_upgrade_log_str = json.dumps(expected_upgrade_log_dict) expected_upgrade_log_str = expected_upgrade_log_str.replace( "GE_PROJECT_DIR", v10_project_directory ) expected_upgrade_log_str = expected_upgrade_log_str.replace( "GE_PATH", os.path.split(great_expectations.__file__)[0] ) expected_upgrade_log_dict = json.loads(expected_upgrade_log_str) with open( f"{v10_project_directory}/uncommitted/logs/project_upgrades/UpgradeHelperV11_20190926T134241.000000Z.json" ) as f: obs_upgrade_log_dict = json.load(f) obs_upgrade_log_dict["exceptions"][0]["exception_message"] = "" assert obs_upgrade_log_dict == expected_upgrade_log_dict @freeze_time("01/19/2021 13:26:39") def test_v2_to_v3_project_upgrade(v20_project_directory, caplog): # test project upgrade that requires no manual steps runner = CliRunner(mix_stderr=False) result = runner.invoke( cli, ["project", "upgrade", "-d", v20_project_directory], input="\n", catch_exceptions=False, ) stdout = result.stdout with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/test_v2_to_v3_project_upgrade_expected_v012_stdout.fixture", ) ) as f: expected_stdout = f.read() expected_stdout = expected_stdout.replace( "GE_PROJECT_DIR", v20_project_directory ) assert stdout == expected_stdout expected_project_tree_str = """\ great_expectations/ .gitignore great_expectations.yml checkpoints/ .gitkeep my_checkpoint.yml titanic_checkpoint_0.yml titanic_checkpoint_1.yml titanic_checkpoint_2.yml expectations/ .ge_store_backend_id .gitkeep notebooks/ .gitkeep pandas/ validation_playground.ipynb spark/ validation_playground.ipynb sql/ validation_playground.ipynb plugins/ custom_data_docs/ styles/ data_docs_custom_styles.css uncommitted/ config_variables.yml data_docs/ local_site/ expectations/ .gitkeep static/ .gitkeep validations/ diabetic_data/ warning/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.html logs/ project_upgrades/ UpgradeHelperV13_20210119T132639.000000Z.json validations/ .ge_store_backend_id diabetic_data/ warning/ 20200430T191246.763896Z/ c3b4c5df224fef4b1a056a0f3b93aba5.json """ obs_project_tree_str = gen_directory_tree_str(v20_project_directory) assert obs_project_tree_str == expected_project_tree_str # make sure config number incremented assert ( DataContext.get_ge_config_version(context_root_dir=v20_project_directory) == 3 ) with open( file_relative_path( __file__, "../../../test_fixtures/upgrade_helper/UpgradeHelperV13_basic_upgrade_log.json", ) ) as f: expected_upgrade_log_dict = json.load(f) expected_upgrade_log_str = json.dumps(expected_upgrade_log_dict) expected_upgrade_log_str = expected_upgrade_log_str.replace( "GE_PROJECT_DIR", v20_project_directory ) expected_upgrade_log_dict = json.loads(expected_upgrade_log_str) with open( f"{v20_project_directory}/uncommitted/logs/project_upgrades/UpgradeHelperV13_20210119T132639.000000Z.json" ) as f: obs_upgrade_log_dict = json.load(f) assert obs_upgrade_log_dict == expected_upgrade_log_dict
apache-2.0
UCSD-CCAL/ccal
ccal/compute_mutational_signature_enrichment.py
1
3940
from pprint import pprint from pandas import DataFrame from pyfaidx import Fasta from ._count import _count from ._identify_what_to_count import _identify_what_to_count def compute_mutational_signature_enrichment( mutation_file_paths, reference_file_path, upper_fasta=True, span=20, contig_format="", contig_intervals=None, ): signature_component_weight = { "TCA ==> TGA": 1, "TCA ==> TTA": 1, "TCT ==> TGT": 1, "TCT ==> TTT": 1, "TGA ==> TCA": 1, "TGA ==> TAA": 1, "AGA ==> ACA": 1, "AGA ==> AAA": 1, } signature_component_dict, signature_component_differing_dict, signature_component_before_dict, signature_component_before_differing_dict = _identify_what_to_count( signature_component_weight ) print("signature_component_dict:") pprint(signature_component_dict) print("signature_component_differing_dict:") pprint(signature_component_differing_dict) print("signature_component_before_dict:") pprint(signature_component_before_dict) print("signature_component_before_differing_dict:") pprint(signature_component_before_differing_dict) samples = {} n_sample = len(mutation_file_paths) fasta_handle = Fasta(reference_file_path, sequence_always_upper=upper_fasta) print("Reference sequence:") pprint(fasta_handle.keys()) for i, mutation_file_path in enumerate(mutation_file_paths): sample = mutation_file_path.split("/")[-1] if sample in samples: raise ValueError("{} duplicated.".format(sample)) print("({}/{}) {} ...".format(i + 1, n_sample, sample)) samples[sample] = _count( mutation_file_path, fasta_handle, span, signature_component_dict, signature_component_differing_dict, signature_component_before_dict, signature_component_before_differing_dict, contig_format, contig_intervals, ) df = DataFrame(samples) df.columns.name = "Sample" df.loc["TCW"] = df.loc[["TCA", "TCT"]].sum() df.loc["TCW ==> TGW"] = df.loc[["TCA ==> TGA", "TCT ==> TGT"]].sum() df.loc["TCW ==> TTW"] = df.loc[["TCA ==> TTA", "TCT ==> TTT"]].sum() df.loc["WGA"] = df.loc[["AGA", "TGA"]].sum() df.loc["WGA ==> WCA"] = df.loc[["AGA ==> ACA", "TGA ==> TCA"]].sum() df.loc["WGA ==> WAA"] = df.loc[["AGA ==> AAA", "TGA ==> TAA"]].sum() signature_component_weight = ( df.loc[signature_component_dict.keys()] .apply( lambda series: series * signature_component_dict[series.name]["weight"], axis=1, ) .sum() ) signature_component_differing_weight = ( df.loc[signature_component_differing_dict.keys()] .apply( lambda series: series * signature_component_differing_dict[series.name]["weight"], axis=1, ) .sum() ) signature_component_before_weight = ( df.loc[signature_component_before_dict.keys()] .apply( lambda series: series * signature_component_before_dict[series.name]["weight"], axis=1, ) .sum() ) signature_component_before_differing_weight = ( df.loc[signature_component_before_differing_dict.keys()] .apply( lambda series: series * signature_component_before_differing_dict[series.name]["weight"], axis=1, ) .sum() ) mutational_signature_enrichment = ( signature_component_weight / signature_component_before_weight ) / ( signature_component_differing_weight / signature_component_before_differing_weight ) df.loc[ "Mutational Signature Enrichment" ] = mutational_signature_enrichment # .fillna(value=0) return df
mit
mattuuh7/incubator-airflow
docs/conf.py
33
8957
# -*- coding: utf-8 -*- # # Airflow documentation build configuration file, created by # sphinx-quickstart on Thu Oct 9 20:50:01 2014. # # 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 os import sys import mock MOCK_MODULES = [ 'apiclient', 'apiclient.discovery', 'apiclient.http', 'mesos', 'mesos.interface', 'mesos.native', 'oauth2client.service_account', 'pandas.io.gbq', ] for mod_name in MOCK_MODULES: sys.modules[mod_name] = mock.Mock() # Hack to allow changing for piece of the code to behave differently while # the docs are being built. The main objective was to alter the # behavior of the utils.apply_default that was hiding function headers os.environ['BUILDING_AIRFLOW_DOCS'] = 'TRUE' from airflow import settings # 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. # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # 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.coverage', 'sphinx.ext.viewcode', 'sphinxarg.ext', ] viewcode_import = True # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'Airflow' #copyright = u'' # 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. #version = '1.0.0' # The full version, including alpha/beta/rc tags. #release = '1.0.0' # 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'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'sphinx_rtd_theme' # 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 = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] import sphinx_rtd_theme html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". html_title = "Airflow Documentation" # A shorter title for the navigation bar. Default is the same as html_title. html_short_title = "" # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # 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 = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. #html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. html_use_index = True # 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, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. html_show_copyright = False # 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 = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'Airflowdoc' # -- 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': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ ('index', 'Airflow.tex', u'Airflow Documentation', u'Maxime Beauchemin', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'airflow', u'Airflow Documentation', [u'Maxime Beauchemin'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [( 'index', 'Airflow', u'Airflow Documentation', u'Maxime Beauchemin', 'Airflow', 'Airflow is a system to programmaticaly author, schedule and monitor data pipelines.', 'Miscellaneous' ),] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False
apache-2.0
drammock/pyeparse
pyeparse/epochs.py
1
23836
# -*- coding: utf-8 -*- # Authors: Denis Engemann <[email protected]> # # License: BSD (3-clause) from copy import deepcopy import numpy as np from scipy.optimize import fmin_slsqp import warnings from ._event import Discrete from .viz import plot_epochs from .utils import pupil_kernel from ._fixes import string_types, nanmean, nanstd from .parallel import parallel_func class Epochs(object): """ Create epoched data Parameters ---------- raw : instance of Raw | list The raw instance to create epochs from. Can also be a list of raw instances to use. events : ndarray (n_epochs) | list The events to construct epochs around. Can also be a list of arrays. event_id : int | dict The event ID to use. Can be a dict to supply multiple event types by name. tmin : float The time window before a particular event in seconds. tmax : float The time window after a particular event in seconds. ignore_missing : bool If True, do not warn if no events were found. Returns ------- epochs : instance of Epochs The epoched dataset. """ def __init__(self, raw, events, event_id, tmin, tmax, ignore_missing=False): if np.isscalar(event_id) and not isinstance(event_id, string_types): if event_id != int(event_id): raise RuntimeError('event_id must be an integer') event_id = int(event_id) event_id = {str(event_id): event_id} if not isinstance(event_id, dict): raise RuntimeError('event_id must be an int or dict') self.event_id = deepcopy(event_id) self.tmin = tmin self.tmax = tmax self._current = 0 if not isinstance(raw, list): raw = [raw] if not isinstance(events, list): events = [events] if len(raw) != len(events): raise ValueError('raw and events must match') event_keys = dict() my_event_id = event_id.values() for k, v in event_id.items(): if (not ignore_missing and v not in np.concatenate(events)[:, 1]): warnings.warn('Did not find event id %i' % v, RuntimeWarning) event_keys[v] = k assert len(raw) > 0 # figure out parameters to use idx_offsets = raw[0].time_as_index([self.tmin, self.tmax]) n_times = idx_offsets[1] - idx_offsets[0] self._n_times = n_times self.info = dict(sfreq=raw[0].info['sfreq'], data_cols=deepcopy(raw[0].info['sample_fields']), ps_units=raw[0].info['ps_units']) for r in raw[1:]: if r.info['sfreq'] != raw[0].info['sfreq']: raise RuntimeError('incompatible raw files') # process each raw file outs = [self._process_raw_events(rr, ee, my_event_id, event_keys, idx_offsets) for rr, ee in zip(raw, events)] _samples, _discretes, _events = zip(*outs) t_off = np.cumsum(np.concatenate(([0], [r.n_samples for r in raw]))) t_off = t_off[:-1] for ev, off in zip(_events, t_off): ev[:, 0] += off # Calculate offsets for epoch indices e_off = np.cumsum(np.concatenate(([0], [len(e) for e in _events]))) _events = np.concatenate(_events) self.events = _events self._data = np.empty((e_off[-1], len(self.info['data_cols']), self.n_times)) # Need to add offsets to our epoch indices for _samp, e1, e2, t in zip(_samples, e_off[:-1], e_off[1:], t_off): for ii in range(len(_samp)): self._data[e1:e2, ii] = _samp[ii] # deal with discretes for kind in _discretes[0].keys(): this_discrete = Discrete() for d in _discretes: this_discrete.extend(d[kind]) assert len(this_discrete) == len(self.events) setattr(self, kind, this_discrete) self.info['discretes'] = list(_discretes[0].keys()) def _process_raw_events(self, raw, events, my_event_id, event_keys, idx_offsets): times = raw.times.copy() sample_inds = [] keep_idx = [] # prevent the evil events = events[events[:, 0].argsort()] discretes = dict() for kind in raw.discrete.keys(): discretes[kind] = Discrete() for ii, (event, this_id) in enumerate(events): if this_id not in my_event_id: continue this_time = times[event] this_tmin, this_tmax = this_time + self.tmin, this_time + self.tmax inds_min, inds_max = raw.time_as_index(this_time)[0] + idx_offsets if max([inds_min, inds_max]) >= len(raw): continue if min([inds_min, inds_max]) < 0: continue inds = np.arange(inds_min, inds_max) sample_inds.append(inds) keep_idx.append(ii) for kind in raw.discrete.keys(): df = raw.discrete[kind] names = df.dtype.names comp_1 = df['stime'] comp_2 = df['etime'] if 'etime' in names else df['stime'] idx = np.where((comp_1 >= this_tmin) & (comp_2 <= this_tmax))[0] subarray = df[idx] subarray['stime'] -= this_time if 'etime' in subarray.dtype.names: subarray['etime'] -= this_time discretes[kind].append(subarray) events = events[keep_idx] sample_inds = np.array(sample_inds) samples = raw[:, sample_inds][0] for kind in raw.discrete.keys(): assert len(discretes[kind]) == len(events) return samples, discretes, events def __len__(self): return len(self.events) def __repr__(self): s = '<Epochs | {0} events | tmin: {1} tmax: {2}>' return s.format(len(self), self.tmin, self.tmax) def __iter__(self): """To make iteration over epochs easy. """ self._current = 0 return self def next(self, return_event_id=False): """To make iteration over epochs easy. """ if self._current >= len(self): raise StopIteration epoch = self._data[self._current] event = self.events[self._current, -1] self._current += 1 if not return_event_id: out = epoch else: out = (epoch, event) return out def __next__(self, *args, **kwargs): return self.next(*args, **kwargs) @property def data(self): return self._data def get_data(self, kind): """Get data of a particular kind Parameters ---------- kind : str Kind of data go obtain. Must be one of ``self.info['data_cols']``. Returns ------- data : array Array of data, n_epochs x n_times. """ if kind not in self.info['data_cols']: raise ValueError('kind "%s" must be one of %s' % (kind, self.info['data_cols'])) return self._data[:, self.info['data_cols'].index(kind)].copy() @property def data_frame(self): raise NotImplementedError @property def ch_names(self): return self.info['data_cols'] @property def n_times(self): return self._n_times @property def times(self): return (np.arange(self.n_times).astype(float) / self.info['sfreq'] + self.tmin) def _str_to_idx(self, string): """Convert epoch string label to set of indices""" if string not in self.event_id: raise IndexError('ID "%s" not found, must be one of %s' % (string, list(self.event_id.keys()))) idx = np.where(self.events[:, -1] == self.event_id[string])[0] return idx def __getitem__(self, idx): out = self.copy() if isinstance(idx, string_types): idx = self._str_to_idx(idx) elif isinstance(idx, list): if all([isinstance(ii, string_types) for ii in idx]): idx = np.concatenate([self._str_to_idx(ii) for ii in idx]) if isinstance(idx, slice): idx = np.arange(len(self))[idx] else: idx = np.atleast_1d(idx) idx = np.atleast_1d(np.sort(idx)) out._data = out._data[idx] out.events = out.events[idx] for discrete in self.info['discretes']: disc = getattr(self, discrete) setattr(out, discrete, Discrete(disc[k] for k in idx)) return out def time_as_index(self, times): """Convert time to indices Parameters ---------- times : list-like | float | int List of numbers or a number representing points in time. Returns ------- index : ndarray Indices corresponding to the times supplied. """ index = (np.atleast_1d(times) - self.times[0]) * self.info['sfreq'] return index.astype(int) def copy(self): """Return a copy of Epochs. """ return deepcopy(self) def plot(self, epoch_idx=None, picks=None, n_chunks=20, title_str='#%003i', show=True, draw_discrete=None, discrete_colors=None, block=False): """ Visualize single trials using Trellis plot. Parameters ---------- epoch_idx : array-like | int | None The epochs to visualize. If None, the first 20 epochs are shown. Defaults to None. n_chunks : int The number of chunks to use for display. picks : array-like | None Channels to be included. If None only good data channels are used. Defaults to None lines : array-like | list of tuple Events to draw as vertical lines title_str : None | str The string formatting to use for axes titles. If None, no titles will be shown. Defaults expand to ``#001, #002, ...`` show : bool Whether to show the figure or not. draw_discrete : {saccades, blinks, fixations} | list-like | None | The events to draw as vertical lines. discrete_colors: : list-like | None list of str or color objects with length of discrete events drawn. block : bool Whether to halt program execution until the figure is closed. Useful for rejecting bad trials on the fly by clicking on a sub plot. Returns ------- fig : Instance of matplotlib.figure.Figure The figure. """ return plot_epochs(epochs=self, epoch_idx=epoch_idx, picks=picks, n_chunks=n_chunks, title_str=title_str, show=show, draw_discrete=draw_discrete, discrete_colors=discrete_colors, block=block) def combine_event_ids(self, old_event_ids, new_event_id): """Collapse event_ids into a new event_id Parameters ---------- old_event_ids : str, or list Conditions to collapse together. new_event_id : dict, or int A one-element dict (or a single integer) for the new condition. Note that for safety, this cannot be any existing id (in epochs.event_id.values()). Notes ----- This For example (if epochs.event_id was {'Left': 1, 'Right': 2}: combine_event_ids(epochs, ['Left', 'Right'], {'Directional': 12}) would create a 'Directional' entry in epochs.event_id replacing 'Left' and 'Right' (combining their trials). """ old_event_ids = np.asanyarray(old_event_ids) if isinstance(new_event_id, int): new_event_id = {str(new_event_id): new_event_id} else: if not isinstance(new_event_id, dict): raise ValueError('new_event_id must be a dict or int') if not len(list(new_event_id.keys())) == 1: raise ValueError('new_event_id dict must have one entry') new_event_num = list(new_event_id.values())[0] if not isinstance(new_event_num, int): raise ValueError('new_event_id value must be an integer') if new_event_num in self.event_id.values(): raise ValueError('new_event_id value must not already exist') old_event_nums = np.array([self.event_id[key] for key in old_event_ids]) # find the ones to replace inds = np.any(self.events[:, 1][:, np.newaxis] == old_event_nums[np.newaxis, :], axis=1) # replace the event numbers in the events list self.events[inds, 1] = new_event_num # delete old entries for key in old_event_ids: self.event_id.pop(key) # add the new entry self.event_id.update(new_event_id) def _key_match(self, key): """Helper function for event dict use""" if key not in self.event_id: raise KeyError('Event "%s" is not in Epochs.' % key) return self.events[:, 1] == self.event_id[key] def drop_epochs(self, indices): """Drop epochs based on indices or boolean mask Parameters ---------- indices : array of ints or bools Set epochs to remove by specifying indices to remove or a boolean mask to apply (where True values get removed). Events are correspondingly modified. """ indices = np.atleast_1d(indices) if indices.ndim > 1: raise ValueError("indices must be a scalar or a 1-d array") if indices.dtype == bool: indices = np.where(indices)[0] out_of_bounds = (indices < 0) | (indices >= len(self.events)) if out_of_bounds.any(): raise IndexError("Epoch index %d is out of bounds" % indices[out_of_bounds][0]) old_idx = np.delete(np.arange(len(self)), indices) self.events = np.delete(self.events, indices, axis=0) self._data = np.delete(self._data, indices, axis=0) for key in self.info['discretes']: val = getattr(self, key) setattr(self, key, [val[ii] for ii in old_idx]) def equalize_event_counts(self, event_ids, method='mintime'): """Equalize the number of trials in each condition Parameters ---------- event_ids : list The event types to equalize. Each entry in the list can either be a str (single event) or a list of str. In the case where one of the entries is a list of str, event_ids in that list will be grouped together before equalizing trial counts across conditions. method : str If 'truncate', events will be truncated from the end of each event list. If 'mintime', timing differences between each event list will be minimized. Returns ------- epochs : instance of Epochs The modified Epochs instance. indices : array of int Indices from the original events list that were dropped. Notes ---- This method operates in-place. """ epochs = self if len(event_ids) == 0: raise ValueError('event_ids must have at least one element') # figure out how to equalize eq_inds = list() for eq in event_ids: eq = np.atleast_1d(eq) # eq is now a list of types key_match = np.zeros(epochs.events.shape[0]) for key in eq: key_match = np.logical_or(key_match, epochs._key_match(key)) eq_inds.append(np.where(key_match)[0]) event_times = [epochs.events[eqi, 0] for eqi in eq_inds] indices = _get_drop_indices(event_times, method) # need to re-index indices indices = np.concatenate([eqi[inds] for eqi, inds in zip(eq_inds, indices)]) epochs.drop_epochs(indices) # actually remove the indices return epochs, indices def pupil_zscores(self, baseline=(None, 0)): """Get normalized pupil data Parameters ---------- baseline : list 2-element list of time points to use as baseline. The default is (None, 0), which uses all negative time. Returns ------- pupil_data : array An n_epochs x n_time array of pupil size data. """ if 'ps' not in self.info['data_cols']: raise RuntimeError('no pupil data') if len(baseline) != 2: raise RuntimeError('baseline must be a 2-element list') baseline = np.array(baseline) if baseline[0] is None: baseline[0] = self.times[0] if baseline[1] is None: baseline[1] = self.times[-1] baseline = self.time_as_index(baseline) zs = self.get_data('ps') std = nanstd(zs.flat) bl = nanmean(zs[:, baseline[0]:baseline[1] + 1], axis=1) zs -= bl[:, np.newaxis] zs /= std return zs def deconvolve(self, spacing=0.1, baseline=(None, 0), bounds=None, max_iter=500, kernel=None, n_jobs=1, acc=1e-6): """Deconvolve pupillary responses Parameters ---------- spacing : float | array Spacing of time points to use for deconvolution. Can also be an array to directly specify time points to use. baseline : list 2-element list of time points to use as baseline. The default is (None, 0), which uses all negative time. This is passed to pupil_zscores(). bounds : 2-element array | None Limits for deconvolution values. Can be, e.g. (0, np.inf) to constrain to positive values. max_iter : int Maximum number of iterations of minimization algorithm. kernel : array | None Kernel to assume when doing deconvolution. If None, the Hoeks and Levelt (1993) kernel will be used. n_jobs : array Number of jobs to run in parallel. acc : float The requested accuracy. Lower accuracy generally means smoother fits. Returns ------- fit : array Array of fits, of size n_epochs x n_fit_times. times : array The array of times at which points were fit. Notes ----- This method is adapted from: Wierda et al., 2012, "Pupil dilation deconvolution reveals the dynamics of attention at high temporal resolution." See: http://www.pnas.org/content/109/22/8456.long Our implementation does not, by default, force all weights to be greater than zero. It also does not do first-order detrending, which the Wierda paper discusses implementing. """ if bounds is not None: bounds = np.array(bounds) if bounds.ndim != 1 or bounds.size != 2: raise RuntimeError('bounds must be 2-element array or None') if kernel is None: kernel = pupil_kernel(self.info['sfreq']) else: kernel = np.array(kernel, np.float64) if kernel.ndim != 1: raise TypeError('kernel must be 1D') # get the data (and make sure it exists) pupil_data = self.pupil_zscores(baseline) # set up parallel function (and check n_jobs) parallel, p_fun, n_jobs = parallel_func(_do_deconv, n_jobs) # figure out where the samples go n_samp = self.n_times if not isinstance(spacing, (np.ndarray, tuple, list)): times = np.arange(self.times[0], self.times[-1], spacing) times = np.unique(times) else: times = np.asanyarray(spacing) samples = self.time_as_index(times) if len(samples) == 0: warnings.warn('No usable samples') return np.array([]), np.array([]) # convert bounds to slsqp representation if bounds is not None: bounds = np.array([bounds for _ in range(len(samples))]) else: bounds = [] # compatible with old version of scipy # Build the convolution matrix conv_mat = np.zeros((n_samp, len(samples))) for li, loc in enumerate(samples): eidx = min(loc + len(kernel), n_samp) conv_mat[loc:eidx, li] = kernel[:eidx-loc] # do the fitting fit_fails = parallel(p_fun(data, conv_mat, bounds, max_iter, acc) for data in np.array_split(pupil_data, n_jobs)) fit = np.concatenate([f[0] for f in fit_fails]) fails = np.concatenate([f[1] for f in fit_fails]) if np.any(fails): reasons = ', '.join(str(r) for r in np.setdiff1d(np.unique(fails), [0])) warnings.warn('%i/%i fits did not converge (reasons: %s)' % (np.sum(fails != 0), len(fails), reasons)) return fit, times def _do_deconv(pupil_data, conv_mat, bounds, max_iter, acc): """Helper to parallelize deconvolution""" x0 = np.zeros(conv_mat.shape[1]) fit = np.empty((len(pupil_data), conv_mat.shape[1])) failed = np.empty(len(pupil_data)) for di, data in enumerate(pupil_data): out = fmin_slsqp(_score, x0, args=(data, conv_mat), epsilon=1e-4, bounds=bounds, disp=False, full_output=True, iter=max_iter, acc=acc) fit[di, :] = out[0] failed[di] = out[3] return fit, failed def _score(vals, x_0, conv_mat): return np.mean((x_0 - conv_mat.dot(vals)) ** 2) def _get_drop_indices(event_times, method): """Helper to get indices to drop from multiple event timing lists""" small_idx = np.argmin([e.shape[0] for e in event_times]) small_e_times = event_times[small_idx] if method not in ['mintime', 'truncate']: raise ValueError('method must be either mintime or truncate, not ' '%s' % method) indices = list() for e in event_times: if method == 'mintime': mask = _minimize_time_diff(small_e_times, e) else: mask = np.ones(e.shape[0], dtype=bool) mask[small_e_times.shape[0]:] = False indices.append(np.where(np.logical_not(mask))[0]) return indices def _minimize_time_diff(t_shorter, t_longer): """Find a boolean mask to minimize timing differences""" keep = np.ones((len(t_longer)), dtype=bool) scores = np.ones((len(t_longer))) for iter in range(len(t_longer) - len(t_shorter)): scores.fill(np.inf) # Check every possible removal to see if it minimizes for idx in np.where(keep)[0]: keep[idx] = False scores[idx] = _area_between_times(t_shorter, t_longer[keep]) keep[idx] = True keep[np.argmin(scores)] = False return keep def _area_between_times(t1, t2): """Quantify the difference between two timing sets""" x1 = list(range(len(t1))) x2 = list(range(len(t2))) xs = np.concatenate((x1, x2)) return np.sum(np.abs(np.interp(xs, x1, t1) - np.interp(xs, x2, t2)))
bsd-3-clause
Srisai85/scikit-learn
sklearn/__check_build/__init__.py
345
1671
""" Module to give helpful messages to the user that did not compile the scikit properly. """ import os INPLACE_MSG = """ It appears that you are importing a local scikit-learn source tree. For this, you need to have an inplace install. Maybe you are in the source directory and you need to try from another location.""" STANDARD_MSG = """ If you have used an installer, please check that it is suited for your Python version, your operating system and your platform.""" def raise_build_error(e): # Raise a comprehensible error and list the contents of the # directory to help debugging on the mailing list. local_dir = os.path.split(__file__)[0] msg = STANDARD_MSG if local_dir == "sklearn/__check_build": # Picking up the local install: this will work only if the # install is an 'inplace build' msg = INPLACE_MSG dir_content = list() for i, filename in enumerate(os.listdir(local_dir)): if ((i + 1) % 3): dir_content.append(filename.ljust(26)) else: dir_content.append(filename + '\n') raise ImportError("""%s ___________________________________________________________________________ Contents of %s: %s ___________________________________________________________________________ It seems that scikit-learn has not been built correctly. If you have installed scikit-learn from source, please do not forget to build the package before using it: run `python setup.py install` or `make` in the source directory. %s""" % (e, local_dir, ''.join(dir_content).strip(), msg)) try: from ._check_build import check_build except ImportError as e: raise_build_error(e)
bsd-3-clause
jenshnielsen/basemap
doc/conf.py
4
5643
# -*- coding: utf-8 -*- # # Basemap documentation build configuration file, created by # sphinx-quickstart on Fri May 2 12:33:25 2008. # # This file is execfile()d with the current directory set to its containing dir. # # The contents of this file are pickled, so don't put values in the namespace # that aren't pickleable (module imports are okay, they're removed automatically). # # All configuration values have a default value; values that are commented out # serve to show the default value. import sys, os # If your extensions are in another directory, add it here. If the directory # is relative to the documentation root, use os.path.abspath to make it # absolute, like shown here. #sys.path.append(os.path.abspath('sphinxext')) # 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', #extensions = ['matplotlib.sphinxext.mathmpl', # 'sphinx.ext.autodoc', 'matplotlib.sphinxext.only_directives', 'matplotlib.sphinxext.plot_directive', # 'sphinx.ext.inheritance_diagram', # 'matplotlib.sphinxext.ipython_console_highlighting', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The master toctree document. master_doc = 'index' # General substitutions. project = 'Basemap Matplotlib Toolkit' copyright = '2011, Jeffrey Whitaker' # The default replacements for |version| and |release|, also used in various # other places throughout the built documents. # # The short X.Y version. from mpl_toolkits.basemap import __version__ as bmversion version = bmversion # The full version, including alpha/beta/rc tags. release = bmversion # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. today_fmt = '%B %d, %Y' # List of documents that shouldn't be included in the build. unused_docs = [] # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # Options for HTML output # ----------------------- # The style sheet to use for HTML and HTML Help pages. A file of that name # must exist either in Sphinx' static/ path, or in one of the custom paths # given in html_static_path. #html_style = 'mpl.css' # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". #html_title = None # The name of an image file (within the static path) to place at the top of # the sidebar. #html_logo = 'logo.png' # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". #html_static_path = ['_static'] # If nonempty, this is the file name suffix for generated HTML files. The # default is ``".html"``. #html_file_suffix = '.xhtml' # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_use_modindex = True # If true, the reST sources are included in the HTML build as _sources/<name>. #html_copy_source = True # If true, an OpenSearch description file will be output, and all pages will # contain a <link> tag referring to it. html_use_opensearch = 'False' # Output file base name for HTML help builder. htmlhelp_basename = 'Basemapdoc' # Options for LaTeX output # ------------------------ # The paper size ('letter' or 'a4'). latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). latex_font_size = '11pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, document class [howto/manual]). latex_documents = [ ('index', 'Basemap.tex', 'Basemap', 'Jeffrey Whitaker', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. latex_logo = None # Additional stuff for the LaTeX preamble. latex_preamble = '' # Documents to append as an appendix to all manuals. latex_appendices = [] # If false, no module index is generated. latex_use_modindex = True latex_use_parts = True # Show both class-level docstring and __init__ docstring in class # documentation autoclass_content = 'both' ################ plot directive configurations ##################### plot_html_show_formats = False plot_include_source = True plot_rcparams = {'figure.figsize':[8, 6]} plot_formats = [('png', 100), # pngs for html building ('pdf', 72), # pdfs for latex building ]
gpl-2.0
danithaca/mxnet
example/reinforcement-learning/ddpg/strategies.py
15
1705
import numpy as np class BaseStrategy(object): """ Base class of exploration strategy. """ def get_action(self, obs, policy): raise NotImplementedError def reset(self): pass class OUStrategy(BaseStrategy): """ Ornstein-Uhlenbeck process: dxt = theta * (mu - xt) * dt + sigma * dWt where Wt denotes the Wiener process. """ def __init__(self, env_spec, mu=0, theta=0.15, sigma=0.3): self.mu = mu self.theta = theta self.sigma = sigma self.action_space = env_spec.action_space self.state = np.ones(self.action_space.flat_dim) * self.mu def evolve_state(self): x = self.state dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(len(x)) self.state = x + dx return self.state def reset(self): self.state = np.ones(self.action_space.flat_dim) * self.mu def get_action(self, obs, policy): # get_action accepts a 2D tensor with one row obs = obs.reshape((1, -1)) action = policy.get_action(obs) increment = self.evolve_state() return np.clip(action + increment, self.action_space.low, self.action_space.high) if __name__ == "__main__": class Env1(object): def __init__(self): self.action_space = Env2() class Env2(object): def __init__(self): self.flat_dim = 2 env_spec = Env1() test = OUStrategy(env_spec) states = [] for i in range(1000): states.append(test.evolve_state()[0]) import matplotlib.pyplot as plt plt.plot(states) plt.show()
apache-2.0
orlandi/connectomicsPerspectivesPaper
participants_codes/aaagv/ranks.py
1
1970
import numpy as np from scipy.sparse import coo_matrix from scipy.linalg import svd import matplotlib.pyplot as plt from sklearn.decomposition import PCA # Path to network files if they are not in the same folder path = '' n_nodes = 1000 for dataset in ["normal-1", "normal-2", "normal-3","normal-4", "lowcc", "lowcon", "highcon","highcc","normal-3-highrate","normal-4-lownoise" ]: name = path + 'network_' + dataset + '.txt' raw_graph = np.loadtxt(name, delimiter=",") row = raw_graph[:, 0] - 1 col = raw_graph[:, 1] - 1 data = raw_graph[:, 2] valid_index = data > 0 net = coo_matrix((data[valid_index], (row[valid_index], col[valid_index])), shape=(n_nodes, n_nodes)) graph = net.toarray() _, singular_values, _ = svd(graph) singular_values /= singular_values.sum() plt.plot(np.sort(singular_values)[::-1], label=dataset) plt.legend(loc="best", prop={'size':12}).draw_frame(False) plt.ylabel('Singular values',size=12) plt.xlabel('Components',size=12) plt.savefig("singular_values_all.pdf", bbox_inches = 'tight') plt.close() for dataset in ["normal-1", "normal-2", "normal-3","normal-4", "lowcc", "lowcon", "highcon","highcc","normal-3-highrate","normal-4-lownoise" ]: name = path + 'network_' + dataset + '.txt' raw_graph = np.loadtxt(name, delimiter=",") row = raw_graph[:, 0] - 1 col = raw_graph[:, 1] - 1 data = raw_graph[:, 2] valid_index = data > 0 net = coo_matrix((data[valid_index], (row[valid_index], col[valid_index])), shape=(n_nodes, n_nodes)) graph = net.toarray() clf = PCA(whiten = True) clf = clf.fit(graph) plt.plot(np.sort(clf.explained_variance_ratio_[::])[::-1], label=dataset) plt.legend(loc="best", prop={'size':12}).draw_frame(False) plt.ylabel('Explained variance ratio',size=12) plt.xlabel('Components',size=12) plt.savefig("explained_variance_all.pdf", bbox_inches = 'tight')
mit
lorenzo-desantis/mne-python
examples/decoding/plot_decoding_sensors.py
16
1976
""" ========================== Decoding sensor space data ========================== Decoding, a.k.a MVPA or supervised machine learning applied to MEG data in sensor space. Here the classifier is applied to every time point. """ # Authors: Alexandre Gramfort <[email protected]> # Jean-Remi King <[email protected]> # # License: BSD (3-clause) import matplotlib.pyplot as plt import mne from mne import io from mne.datasets import sample from mne.decoding import TimeDecoding print(__doc__) data_path = sample.data_path() plt.close('all') ############################################################################### # Set parameters raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif' tmin, tmax = -0.2, 0.5 event_id = dict(aud_l=1, vis_l=3) # Setup for reading the raw data raw = io.Raw(raw_fname, preload=True) raw.filter(2, None, method='iir') # replace baselining with high-pass events = mne.read_events(event_fname) # Set up pick list: EEG + MEG - bad channels (modify to your needs) raw.info['bads'] += ['MEG 2443', 'EEG 053'] # bads + 2 more picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=True, eog=True, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True, reject=dict(grad=4000e-13, eog=150e-6)) epochs_list = [epochs[k] for k in event_id] mne.epochs.equalize_epoch_counts(epochs_list) data_picks = mne.pick_types(epochs.info, meg=True, exclude='bads') ############################################################################### # Setup decoding: default is linear SVC td = TimeDecoding(predict_mode='cross-validation', n_jobs=1) # Fit td.fit(epochs) # Compute accuracy td.score(epochs) # Plot scores across time td.plot(title='Sensor space decoding')
bsd-3-clause
Sklearn-HMM/scikit-learn-HMM
sklean-hmm/datasets/tests/test_20news.py
42
2416
"""Test the 20news downloader, if the data is available.""" import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_true from sklearn.utils.testing import SkipTest from sklearn import datasets def test_20news(): try: data = datasets.fetch_20newsgroups( subset='all', download_if_missing=False, shuffle=False) except IOError: raise SkipTest("Download 20 newsgroups to run this test") # Extract a reduced dataset data2cats = datasets.fetch_20newsgroups( subset='all', categories=data.target_names[-1:-3:-1], shuffle=False) # Check that the ordering of the target_names is the same # as the ordering in the full dataset assert_equal(data2cats.target_names, data.target_names[-2:]) # Assert that we have only 0 and 1 as labels assert_equal(np.unique(data2cats.target).tolist(), [0, 1]) # Check that the number of filenames is consistent with data/target assert_equal(len(data2cats.filenames), len(data2cats.target)) assert_equal(len(data2cats.filenames), len(data2cats.data)) # Check that the first entry of the reduced dataset corresponds to # the first entry of the corresponding category in the full dataset entry1 = data2cats.data[0] category = data2cats.target_names[data2cats.target[0]] label = data.target_names.index(category) entry2 = data.data[np.where(data.target == label)[0][0]] assert_equal(entry1, entry2) def test_20news_vectorized(): # This test is slow. raise SkipTest("Test too slow.") bunch = datasets.fetch_20newsgroups_vectorized(subset="train") assert_true(sp.isspmatrix_csr(bunch.data)) assert_equal(bunch.data.shape, (11314, 107428)) assert_equal(bunch.target.shape[0], 11314) assert_equal(bunch.data.dtype, np.float64) bunch = datasets.fetch_20newsgroups_vectorized(subset="test") assert_true(sp.isspmatrix_csr(bunch.data)) assert_equal(bunch.data.shape, (7532, 107428)) assert_equal(bunch.target.shape[0], 7532) assert_equal(bunch.data.dtype, np.float64) bunch = datasets.fetch_20newsgroups_vectorized(subset="all") assert_true(sp.isspmatrix_csr(bunch.data)) assert_equal(bunch.data.shape, (11314 + 7532, 107428)) assert_equal(bunch.target.shape[0], 11314 + 7532) assert_equal(bunch.data.dtype, np.float64)
bsd-3-clause
kwailamchan/programming-languages
python/crawlers/crawler/catalogs/lowes/lowes_catalogs_cleaning.py
3
2867
__author__ = "Kelly Chan" __date__ = "Sept 9 2014" __version__ = "1.0.0" import re import pandas as pd def loadContent(dataFile): with open(dataFile, 'rb') as f: content = f.read() f.close() return content def filterRE(content, pattern): return re.findall(re.compile(pattern), str(content)) def extractAttrs(content): names = [] #ids = [] itemIDs = [] cateIDs = [] modelIDs = [] prodIDs = [] depts = [] # names pattern = r".*,(.*),http://www.lowes.com/.*" results = filterRE(content, pattern) for result in results: names.append(result) # ids #pattern = r"/pd_(.*)_.*__\??" #results = filterRE(content, pattern) #for result in results: # ids.append(result) # itemIDs pattern = r"/pd_(\d+)-?" results = filterRE(content, pattern) for result in results: itemIDs.append(result) # cateIDs pattern = r"/pd_\d+-(\d+)-?" results = filterRE(content, pattern) for result in results: cateIDs.append(result) # modelIDs pattern = r"/pd_\d+-\d+-(.*)_.*__\??" results = filterRE(content, pattern) for result in results: modelIDs.append(result.replace('+', ' ')) # prodIDs #pattern = r"productId=(\d+)&amp;" pattern = r"productId=(\d+)" results = filterRE(content, pattern) for result in results: prodIDs.append(result) # depts pattern = r"http://www.lowes.com/(.*)/_/N-.*" results = filterRE(content, pattern) for result in results: depts.append(result.replace('-', ' ')) cates = [] for dept in depts: cates.append(dept.split('/')) #print len(names) #print len(itemIDs) #print len(cateIDs) #print len(modelIDs) #print len(prodIDs) #print len(cates) attrs = pd.DataFrame({'prodName': names, 'itemID': itemIDs, 'cateID': cateIDs, 'modelID': modelIDs, 'prodID': prodIDs, 'cates': cates}) return attrs def main(): dataPath = "G:/vimFiles/freelance/20140903-eCatalog/data/lowes/products/" outPath = "G:/vimFiles/freelance/20140903-eCatalog/src/outputs/" #for i in range(21): # dataFile = "products-lowes-catalogs-fullurls-%s.csv" % str(i) # content = loadContent(dataPath+dataFile) # attrs = extractAttrs(content) # attrs.to_csv(outPath+"clean-"+dataFile, header=True, index=False) #dataFile = "products-error-one-page.csv" #dataFile = "products-error-more-pages.csv" dataFile = "products-error-no-products.csv" content = loadContent(dataPath+dataFile) attrs = extractAttrs(content) attrs.to_csv(outPath+"clean-"+dataFile, header=True, index=False) if __name__ == '__main__': main()
mit
etkirsch/scikit-learn
sklearn/gaussian_process/tests/test_gaussian_process.py
267
6813
""" Testing for Gaussian Process module (sklearn.gaussian_process) """ # Author: Vincent Dubourg <[email protected]> # Licence: BSD 3 clause from nose.tools import raises from nose.tools import assert_true import numpy as np from sklearn.gaussian_process import GaussianProcess from sklearn.gaussian_process import regression_models as regression from sklearn.gaussian_process import correlation_models as correlation from sklearn.datasets import make_regression from sklearn.utils.testing import assert_greater f = lambda x: x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T X2 = np.atleast_2d([2., 4., 5.5, 6.5, 7.5]).T y = f(X).ravel() def test_1d(regr=regression.constant, corr=correlation.squared_exponential, random_start=10, beta0=None): # MLE estimation of a one-dimensional Gaussian Process model. # Check random start optimization. # Test the interpolating property. gp = GaussianProcess(regr=regr, corr=corr, beta0=beta0, theta0=1e-2, thetaL=1e-4, thetaU=1e-1, random_start=random_start, verbose=False).fit(X, y) y_pred, MSE = gp.predict(X, eval_MSE=True) y2_pred, MSE2 = gp.predict(X2, eval_MSE=True) assert_true(np.allclose(y_pred, y) and np.allclose(MSE, 0.) and np.allclose(MSE2, 0., atol=10)) def test_2d(regr=regression.constant, corr=correlation.squared_exponential, random_start=10, beta0=None): # MLE estimation of a two-dimensional Gaussian Process model accounting for # anisotropy. Check random start optimization. # Test the interpolating property. b, kappa, e = 5., .5, .1 g = lambda x: b - x[:, 1] - kappa * (x[:, 0] - e) ** 2. X = np.array([[-4.61611719, -6.00099547], [4.10469096, 5.32782448], [0.00000000, -0.50000000], [-6.17289014, -4.6984743], [1.3109306, -6.93271427], [-5.03823144, 3.10584743], [-2.87600388, 6.74310541], [5.21301203, 4.26386883]]) y = g(X).ravel() thetaL = [1e-4] * 2 thetaU = [1e-1] * 2 gp = GaussianProcess(regr=regr, corr=corr, beta0=beta0, theta0=[1e-2] * 2, thetaL=thetaL, thetaU=thetaU, random_start=random_start, verbose=False) gp.fit(X, y) y_pred, MSE = gp.predict(X, eval_MSE=True) assert_true(np.allclose(y_pred, y) and np.allclose(MSE, 0.)) eps = np.finfo(gp.theta_.dtype).eps assert_true(np.all(gp.theta_ >= thetaL - eps)) # Lower bounds of hyperparameters assert_true(np.all(gp.theta_ <= thetaU + eps)) # Upper bounds of hyperparameters def test_2d_2d(regr=regression.constant, corr=correlation.squared_exponential, random_start=10, beta0=None): # MLE estimation of a two-dimensional Gaussian Process model accounting for # anisotropy. Check random start optimization. # Test the GP interpolation for 2D output b, kappa, e = 5., .5, .1 g = lambda x: b - x[:, 1] - kappa * (x[:, 0] - e) ** 2. f = lambda x: np.vstack((g(x), g(x))).T X = np.array([[-4.61611719, -6.00099547], [4.10469096, 5.32782448], [0.00000000, -0.50000000], [-6.17289014, -4.6984743], [1.3109306, -6.93271427], [-5.03823144, 3.10584743], [-2.87600388, 6.74310541], [5.21301203, 4.26386883]]) y = f(X) gp = GaussianProcess(regr=regr, corr=corr, beta0=beta0, theta0=[1e-2] * 2, thetaL=[1e-4] * 2, thetaU=[1e-1] * 2, random_start=random_start, verbose=False) gp.fit(X, y) y_pred, MSE = gp.predict(X, eval_MSE=True) assert_true(np.allclose(y_pred, y) and np.allclose(MSE, 0.)) @raises(ValueError) def test_wrong_number_of_outputs(): gp = GaussianProcess() gp.fit([[1, 2, 3], [4, 5, 6]], [1, 2, 3]) def test_more_builtin_correlation_models(random_start=1): # Repeat test_1d and test_2d for several built-in correlation # models specified as strings. all_corr = ['absolute_exponential', 'squared_exponential', 'cubic', 'linear'] for corr in all_corr: test_1d(regr='constant', corr=corr, random_start=random_start) test_2d(regr='constant', corr=corr, random_start=random_start) test_2d_2d(regr='constant', corr=corr, random_start=random_start) def test_ordinary_kriging(): # Repeat test_1d and test_2d with given regression weights (beta0) for # different regression models (Ordinary Kriging). test_1d(regr='linear', beta0=[0., 0.5]) test_1d(regr='quadratic', beta0=[0., 0.5, 0.5]) test_2d(regr='linear', beta0=[0., 0.5, 0.5]) test_2d(regr='quadratic', beta0=[0., 0.5, 0.5, 0.5, 0.5, 0.5]) test_2d_2d(regr='linear', beta0=[0., 0.5, 0.5]) test_2d_2d(regr='quadratic', beta0=[0., 0.5, 0.5, 0.5, 0.5, 0.5]) def test_no_normalize(): gp = GaussianProcess(normalize=False).fit(X, y) y_pred = gp.predict(X) assert_true(np.allclose(y_pred, y)) def test_random_starts(): # Test that an increasing number of random-starts of GP fitting only # increases the reduced likelihood function of the optimal theta. n_samples, n_features = 50, 3 np.random.seed(0) rng = np.random.RandomState(0) X = rng.randn(n_samples, n_features) * 2 - 1 y = np.sin(X).sum(axis=1) + np.sin(3 * X).sum(axis=1) best_likelihood = -np.inf for random_start in range(1, 5): gp = GaussianProcess(regr="constant", corr="squared_exponential", theta0=[1e-0] * n_features, thetaL=[1e-4] * n_features, thetaU=[1e+1] * n_features, random_start=random_start, random_state=0, verbose=False).fit(X, y) rlf = gp.reduced_likelihood_function()[0] assert_greater(rlf, best_likelihood - np.finfo(np.float32).eps) best_likelihood = rlf def test_mse_solving(): # test the MSE estimate to be sane. # non-regression test for ignoring off-diagonals of feature covariance, # testing with nugget that renders covariance useless, only # using the mean function, with low effective rank of data gp = GaussianProcess(corr='absolute_exponential', theta0=1e-4, thetaL=1e-12, thetaU=1e-2, nugget=1e-2, optimizer='Welch', regr="linear", random_state=0) X, y = make_regression(n_informative=3, n_features=60, noise=50, random_state=0, effective_rank=1) gp.fit(X, y) assert_greater(1000, gp.predict(X, eval_MSE=True)[1].mean())
bsd-3-clause
mblondel/scikit-learn
sklearn/decomposition/tests/test_sparse_pca.py
31
6002
# Author: Vlad Niculae # License: BSD 3 clause import sys import numpy as np 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 SkipTest from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_false from sklearn.utils.testing import if_not_mac_os from sklearn.decomposition import SparsePCA, MiniBatchSparsePCA from sklearn.utils import check_random_state def generate_toy_data(n_components, n_samples, image_size, random_state=None): n_features = image_size[0] * image_size[1] rng = check_random_state(random_state) U = rng.randn(n_samples, n_components) V = rng.randn(n_components, n_features) centers = [(3, 3), (6, 7), (8, 1)] sz = [1, 2, 1] for k in range(n_components): img = np.zeros(image_size) xmin, xmax = centers[k][0] - sz[k], centers[k][0] + sz[k] ymin, ymax = centers[k][1] - sz[k], centers[k][1] + sz[k] img[xmin:xmax][:, ymin:ymax] = 1.0 V[k, :] = img.ravel() # Y is defined by : Y = UV + noise Y = np.dot(U, V) Y += 0.1 * rng.randn(Y.shape[0], Y.shape[1]) # Add noise return Y, U, V # SparsePCA can be a bit slow. To avoid having test times go up, we # test different aspects of the code in the same test def test_correct_shapes(): rng = np.random.RandomState(0) X = rng.randn(12, 10) spca = SparsePCA(n_components=8, random_state=rng) U = spca.fit_transform(X) assert_equal(spca.components_.shape, (8, 10)) assert_equal(U.shape, (12, 8)) # test overcomplete decomposition spca = SparsePCA(n_components=13, random_state=rng) U = spca.fit_transform(X) assert_equal(spca.components_.shape, (13, 10)) assert_equal(U.shape, (12, 13)) def test_fit_transform(): alpha = 1 rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array spca_lars = SparsePCA(n_components=3, method='lars', alpha=alpha, random_state=0) spca_lars.fit(Y) # Test that CD gives similar results spca_lasso = SparsePCA(n_components=3, method='cd', random_state=0, alpha=alpha) spca_lasso.fit(Y) assert_array_almost_equal(spca_lasso.components_, spca_lars.components_) @if_not_mac_os() def test_fit_transform_parallel(): alpha = 1 rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array spca_lars = SparsePCA(n_components=3, method='lars', alpha=alpha, random_state=0) spca_lars.fit(Y) U1 = spca_lars.transform(Y) # Test multiple CPUs spca = SparsePCA(n_components=3, n_jobs=2, method='lars', alpha=alpha, random_state=0).fit(Y) U2 = spca.transform(Y) assert_true(not np.all(spca_lars.components_ == 0)) assert_array_almost_equal(U1, U2) def test_transform_nan(): """ Test that SparsePCA won't return NaN when there is 0 feature in all samples. """ rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array Y[:, 0] = 0 estimator = SparsePCA(n_components=8) assert_false(np.any(np.isnan(estimator.fit_transform(Y)))) def test_fit_transform_tall(): rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 65, (8, 8), random_state=rng) # tall array spca_lars = SparsePCA(n_components=3, method='lars', random_state=rng) U1 = spca_lars.fit_transform(Y) spca_lasso = SparsePCA(n_components=3, method='cd', random_state=rng) U2 = spca_lasso.fit(Y).transform(Y) assert_array_almost_equal(U1, U2) def test_initialization(): rng = np.random.RandomState(0) U_init = rng.randn(5, 3) V_init = rng.randn(3, 4) model = SparsePCA(n_components=3, U_init=U_init, V_init=V_init, max_iter=0, random_state=rng) model.fit(rng.randn(5, 4)) assert_array_equal(model.components_, V_init) def test_mini_batch_correct_shapes(): rng = np.random.RandomState(0) X = rng.randn(12, 10) pca = MiniBatchSparsePCA(n_components=8, random_state=rng) U = pca.fit_transform(X) assert_equal(pca.components_.shape, (8, 10)) assert_equal(U.shape, (12, 8)) # test overcomplete decomposition pca = MiniBatchSparsePCA(n_components=13, random_state=rng) U = pca.fit_transform(X) assert_equal(pca.components_.shape, (13, 10)) assert_equal(U.shape, (12, 13)) def test_mini_batch_fit_transform(): raise SkipTest("skipping mini_batch_fit_transform.") alpha = 1 rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array spca_lars = MiniBatchSparsePCA(n_components=3, random_state=0, alpha=alpha).fit(Y) U1 = spca_lars.transform(Y) # Test multiple CPUs if sys.platform == 'win32': # fake parallelism for win32 import sklearn.externals.joblib.parallel as joblib_par _mp = joblib_par.multiprocessing joblib_par.multiprocessing = None try: U2 = MiniBatchSparsePCA(n_components=3, n_jobs=2, alpha=alpha, random_state=0).fit(Y).transform(Y) finally: joblib_par.multiprocessing = _mp else: # we can efficiently use parallelism U2 = MiniBatchSparsePCA(n_components=3, n_jobs=2, alpha=alpha, random_state=0).fit(Y).transform(Y) assert_true(not np.all(spca_lars.components_ == 0)) assert_array_almost_equal(U1, U2) # Test that CD gives similar results spca_lasso = MiniBatchSparsePCA(n_components=3, method='cd', alpha=alpha, random_state=0).fit(Y) assert_array_almost_equal(spca_lasso.components_, spca_lars.components_)
bsd-3-clause
sarahgrogan/scikit-learn
sklearn/cluster/tests/test_birch.py
342
5603
""" Tests for the birch clustering algorithm. """ from scipy import sparse import numpy as np from sklearn.cluster.tests.common import generate_clustered_data from sklearn.cluster.birch import Birch from sklearn.cluster.hierarchical import AgglomerativeClustering from sklearn.datasets import make_blobs from sklearn.linear_model import ElasticNet from sklearn.metrics import pairwise_distances_argmin, v_measure_score from sklearn.utils.testing import assert_greater_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_warns def test_n_samples_leaves_roots(): # Sanity check for the number of samples in leaves and roots X, y = make_blobs(n_samples=10) brc = Birch() brc.fit(X) n_samples_root = sum([sc.n_samples_ for sc in brc.root_.subclusters_]) n_samples_leaves = sum([sc.n_samples_ for leaf in brc._get_leaves() for sc in leaf.subclusters_]) assert_equal(n_samples_leaves, X.shape[0]) assert_equal(n_samples_root, X.shape[0]) def test_partial_fit(): # Test that fit is equivalent to calling partial_fit multiple times X, y = make_blobs(n_samples=100) brc = Birch(n_clusters=3) brc.fit(X) brc_partial = Birch(n_clusters=None) brc_partial.partial_fit(X[:50]) brc_partial.partial_fit(X[50:]) assert_array_equal(brc_partial.subcluster_centers_, brc.subcluster_centers_) # Test that same global labels are obtained after calling partial_fit # with None brc_partial.set_params(n_clusters=3) brc_partial.partial_fit(None) assert_array_equal(brc_partial.subcluster_labels_, brc.subcluster_labels_) def test_birch_predict(): # Test the predict method predicts the nearest centroid. rng = np.random.RandomState(0) X = generate_clustered_data(n_clusters=3, n_features=3, n_samples_per_cluster=10) # n_samples * n_samples_per_cluster shuffle_indices = np.arange(30) rng.shuffle(shuffle_indices) X_shuffle = X[shuffle_indices, :] brc = Birch(n_clusters=4, threshold=1.) brc.fit(X_shuffle) centroids = brc.subcluster_centers_ assert_array_equal(brc.labels_, brc.predict(X_shuffle)) nearest_centroid = pairwise_distances_argmin(X_shuffle, centroids) assert_almost_equal(v_measure_score(nearest_centroid, brc.labels_), 1.0) def test_n_clusters(): # Test that n_clusters param works properly X, y = make_blobs(n_samples=100, centers=10) brc1 = Birch(n_clusters=10) brc1.fit(X) assert_greater(len(brc1.subcluster_centers_), 10) assert_equal(len(np.unique(brc1.labels_)), 10) # Test that n_clusters = Agglomerative Clustering gives # the same results. gc = AgglomerativeClustering(n_clusters=10) brc2 = Birch(n_clusters=gc) brc2.fit(X) assert_array_equal(brc1.subcluster_labels_, brc2.subcluster_labels_) assert_array_equal(brc1.labels_, brc2.labels_) # Test that the wrong global clustering step raises an Error. clf = ElasticNet() brc3 = Birch(n_clusters=clf) assert_raises(ValueError, brc3.fit, X) # Test that a small number of clusters raises a warning. brc4 = Birch(threshold=10000.) assert_warns(UserWarning, brc4.fit, X) def test_sparse_X(): # Test that sparse and dense data give same results X, y = make_blobs(n_samples=100, centers=10) brc = Birch(n_clusters=10) brc.fit(X) csr = sparse.csr_matrix(X) brc_sparse = Birch(n_clusters=10) brc_sparse.fit(csr) assert_array_equal(brc.labels_, brc_sparse.labels_) assert_array_equal(brc.subcluster_centers_, brc_sparse.subcluster_centers_) def check_branching_factor(node, branching_factor): subclusters = node.subclusters_ assert_greater_equal(branching_factor, len(subclusters)) for cluster in subclusters: if cluster.child_: check_branching_factor(cluster.child_, branching_factor) def test_branching_factor(): # Test that nodes have at max branching_factor number of subclusters X, y = make_blobs() branching_factor = 9 # Purposefully set a low threshold to maximize the subclusters. brc = Birch(n_clusters=None, branching_factor=branching_factor, threshold=0.01) brc.fit(X) check_branching_factor(brc.root_, branching_factor) brc = Birch(n_clusters=3, branching_factor=branching_factor, threshold=0.01) brc.fit(X) check_branching_factor(brc.root_, branching_factor) # Raises error when branching_factor is set to one. brc = Birch(n_clusters=None, branching_factor=1, threshold=0.01) assert_raises(ValueError, brc.fit, X) def check_threshold(birch_instance, threshold): """Use the leaf linked list for traversal""" current_leaf = birch_instance.dummy_leaf_.next_leaf_ while current_leaf: subclusters = current_leaf.subclusters_ for sc in subclusters: assert_greater_equal(threshold, sc.radius) current_leaf = current_leaf.next_leaf_ def test_threshold(): # Test that the leaf subclusters have a threshold lesser than radius X, y = make_blobs(n_samples=80, centers=4) brc = Birch(threshold=0.5, n_clusters=None) brc.fit(X) check_threshold(brc, 0.5) brc = Birch(threshold=5.0, n_clusters=None) brc.fit(X) check_threshold(brc, 5.)
bsd-3-clause
brentjm/Impurity-Predictions
server/package/container.py
1
5967
""" Container class Brent Maranzano 2016-02-16 """ import pandas as pd import numpy as np class Container(object): """ Define the type of the container and necessary information, such as volume, area, ... used for calculating transport through the container material. Class variables: ID : string - unique identification string to lookup parameters name : string - name of the container (e.g. LDPE) volume : float - total volume of container (cm^3) area : float - surface area of package (cm^2) thickness : float - thickness of container used for flux calculation (mil) Only used for LDPE. seal : string - key work identifier describing the permeability of the seal transport_coef : dictionary {"slope": float, "intercept": float} - used for calculating the permeability of the package. temperature : float - container environment temperature (K) permeability : permeability of the container at the set temperature (mg / day / delta RH) Class methods: set_properties : Set the properties of the container. recalc_properties : Calculate and set all the container properties based on the current object variable values. set_temperature : Set the temperature of the container. set_seal : Set the type of seal for the container. calc_area : Calculate the area of the container calc_permeability : Calculate the permeability """ def __init__(self, ID, **kwargs): """ Set the class attributes. """ self.set_properties(ID, **kwargs) def set_properties(self, ID, temperature=298.15, **kwargs): """ Set all the properties of the package. Parameters: ID: string - Unique identification of the container temperature: float - temperature of environment (K) optional kwargs: seal : string - value for the seal type volume : float - volume of the container thickness : float - thickness for LDPE area : float - area of LDPE temperaure : float - temperature of container environment """ store = pd.HDFStore("simulation_constants.hdf", mode="r") package_properties = store["package_properties"] store.close() if ID in package_properties.index.values: self.ID = ID self.name = package_properties.loc[ID]["name"] self.transport_coef = package_properties.loc[ID][["slope", "intercept"]].to_dict() if ID != "LDPE": self.volume = package_properties.loc[ID]["volume"] else: raise ValueError("No container with identification {} is known.".format(ID)) if ID != "LDPE": if "seal" in kwargs: self.seal = kwargs["seal"] else: self.seal = "ACTIVATED" elif ID == "LDPE": if "volume" not in kwargs or "thickness" not in kwargs: raise ValueError("Must supply volume and thickness for LDPE.") else: self.volume = float("volume") self.thickness = float("thickness") if "area" in kwargs: self.area = float(kwargs["area"]) else: self.area = self.calc_area() self.temperature = float(temperature) self.permeability = self.calc_permeability() def recalc_properties(self): """ Recalculate and set all the container properties based on the new object variable values. """ self.permeability = self.calc_permeability() def set_temperature(self, temperature): """ Set the environment temperature for the container and recalculate container parameters. Parameters temperature: float - temperature of the environment (K) """ self.temperature = float(temperature) self.recalc_properties() def set_seal(self, seal): """ Set the seal conditions and recalculate the container parameters. Parameters seal : string - type of seals ["ACTIVATED" | "UNACTIVATED" | "BROACHED"] """ if seal in ["ACTIVATED", "UNACTIVATED", "BROACHED"]: self.seal = seal else: raise ValueError("Unknown seal type") self.recalc_properties() def calc_area(self): """ Calculate the package area assuming the volume is a sphere. return : float - Area of package in cm^2. """ area = 4 * np.pi * (3 * self.volume / (4 * np.pi))**(2./3.) return area def calc_permeability(self): """ Calculate the permeability of the package. return : float - permeabilty mg / day / delta RH """ if self.ID == "LDPE": permeability = np.exp(self.transport_coef["intercept"] + self.transport_coef["slope"] / self.temperature) * \ self.area * 4. / (10. * self.thickness) else: if self.seal == "UNACTIVATED": f1 = 1.20923913 f2 = 0. elif self.seal == "BROACHED": f1 = 1. f2 = 0.02072 else: f1 = 0. f2 = 0. permeability = np.exp(self.transport_coef["intercept"] + self.transport_coef["slope"] / self.temperature) * f1 + f2 return permeability @classmethod def make_container(cls, config): """ Make the container object using the dictionary for parameters. config : dictionary - Dictionary of parameters that define the container. """ ID = config["ID"] del config["ID"] container = cls(ID, **config) return container ##
bsd-2-clause
ltiao/scikit-learn
doc/datasets/mldata_fixture.py
367
1183
"""Fixture module to skip the datasets loading when offline Mock urllib2 access to mldata.org and create a temporary data folder. """ from os import makedirs from os.path import join import numpy as np import tempfile import shutil from sklearn import datasets from sklearn.utils.testing import install_mldata_mock from sklearn.utils.testing import uninstall_mldata_mock def globs(globs): # Create a temporary folder for the data fetcher global custom_data_home custom_data_home = tempfile.mkdtemp() makedirs(join(custom_data_home, 'mldata')) globs['custom_data_home'] = custom_data_home return globs def setup_module(): # setup mock urllib2 module to avoid downloading from mldata.org install_mldata_mock({ 'mnist-original': { 'data': np.empty((70000, 784)), 'label': np.repeat(np.arange(10, dtype='d'), 7000), }, 'iris': { 'data': np.empty((150, 4)), }, 'datasets-uci-iris': { 'double0': np.empty((150, 4)), 'class': np.empty((150,)), }, }) def teardown_module(): uninstall_mldata_mock() shutil.rmtree(custom_data_home)
bsd-3-clause
RouxRC/gazouilleur
gazouilleur/lib/tests.py
1
8866
#!/usr/bin/env python # -*- coding: utf-8 -*- from sys import stderr from traceback import format_exc # Check dependencies try: from gazouilleur.lib.colorize import colorize colorize('a', style='bold') except (ImportError, TypeError) as e: stderr.write("ERROR: Could not load module colorize.\nERROR: Something obviously wrong here...\n") exit(1) try: import pymongo, txmongo, txmongo.connection, lxml, twisted, twitter, feedparser, pypump, zope.interface, stevedore, urllib3, cffi, cryptography, OpenSSL, w3lib except ImportError as e: stderr.write(colorize("ERROR: Could not load module%s.\nERROR: Please check your install or run `./bin/update_requirements.sh` to update the dependencies.\n" % str(e).replace('No module named', ''), 'red', style='bold')) exit(1) # Check config.py try: from gazouilleur import config except ImportError: stderr.write(colorize("Could not find `gazouilleur/config.py`.\nERROR: Please run `bash bin/configure.sh` to create it, then edit it to prepare your bot.\n", 'red', style='bold')) exit(1) except SyntaxError as e: stderr.write(colorize("Could not read `gazouilleur/config.py`.\nERROR: Please edit it to fix the following syntax issue:\nERROR: %s\n%s\n" % (e, "\n".join(format_exc().splitlines()[-3:-1])), 'red', style='bold')) exit(1) #Load decorator from gazouilleur.lib.log import logerr try: config.BOTNAME, config.BOTPASS, config.HOST, config.PORT, config.MONGODB, config.GLOBAL_USERS, config.BACK_HOURS, config.COMMAND_CHARACTER, config.CHANNELS, config.DEBUG, config.ADMINS, config.EXTRA_COMMANDS [config.MONGODB[k] for k in ['HOST', 'PORT', 'DATABASE', 'USER', 'PSWD']] except AttributeError as e: logerr("Some field is missing from `gazouilleur/config.py`.\nERROR: Please edit it to fix the following issue:\nERROR: %s" % str(e).replace("'module' object", 'config')) exit(1) except KeyError as e: logerr("A field is missing from MONGODB config in `gazouilleur/config.py`: %s." % e) exit(1) try: assert(len([1 for c in config.CHANNELS.values() if "MASTER" in c and c["MASTER"]]) == 1) [c[k] for k in ['USERS', 'DISPLAY_RT'] for c in config.CHANNELS.values()] except AssertionError: logerr("One and only one channel must be set as MASTER in `gazouilleur/config.py`.\nERROR: Please edit it to fix this issue.") exit(1) except KeyError as e: logerr("A field is missing from one channel set in `gazouilleur/config.py`: %s." % e) exit(1) try: [c['IDENTICA']['USER'] for c in config.CHANNELS.values() if "IDENTICA" in c] except KeyError: logerr("USER field is missing from IDENTICA config in `gazouilleur/config.py`.") exit(1) try: [c['TWITTER'][k] for k in ['USER', 'DISPLAY_RT', 'KEY', 'SECRET', 'OAUTH_TOKEN', 'OAUTH_SECRET'] for c in config.CHANNELS.values() if "TWITTER" in c] except KeyError as e: logerr("A field is missing from TWITTER config in `gazouilleur/config.py`: %s." % e) exit(1) try: [e[k] for k in ['command', 'help', 'helpset', 'return', 'none'] for e in config.EXTRA_COMMANDS] except KeyError as e: logerr("A field is missing from EXTRA_COMMANDS in `gazouilleur/config.py`: %s." % e) exit(1) # Check image dependencies if Manet screenshots activated if hasattr(config, 'URL_MANET'): try: import wand except (NameError, ImportError) as e: logerr("Could not load module%s.\nERROR: This module is required to activate the Manet screenshots set with URL_MANET in `gazouilleur/config.py`: %s\nERROR: Please check your install or run `pip install Wand` in gazouilleur's virtualenv.\n" % (str(e).replace('No module named', ''), config.URL_STATS)) exit(1) try: from gazouilleur.lib import ircclient_with_names, irccolors, feeds, filelogger, httpget, log, microblog, mongo, resolver, stats, utils, templater, webmonitor except Exception as e: logerr("Oups, looks like something is wrong somewhere in the code, shouldn't be committed...") logerr("%s\n%s" % (e, "\n".join(format_exc().splitlines()[-3:-1]))) exit(1) # Check plotting dependencies if webstats activated if hasattr(config, 'URL_STATS'): try: import pystache, pylab, matplotlib import gazouilleur.lib.plots except (NameError, ImportError) as e: logerr("Could not load module%s.\nERROR: This module is required to activate the Twitter web stats set in URL_STATS in `gazouilleur/config.py`: %s\nERROR: Please check your installl or run `./bin/update_requirements.sh` to update the dependencies.\n" % (str(e).replace('No module named', ''), config.URL_STATS)) exit(1) # Check Color Configs try: irccolors.ColorConf(config.FORMAT) except TypeError as e: logerr("Global FORMAT conf is broken in `gazouilleur/config.py`:\n%s" % e) exit(1) except: pass for chan, conf in config.CHANNELS.iteritems(): if "FORMAT" not in conf: continue try: irccolors.ColorConf(conf['FORMAT']) except TypeError as e: logerr("Conf FORMAT of channel %s is broken in `gazouilleur/config.py`:\n%s" % (chan, e)) exit(1) # Check MongoDB try: db = pymongo.MongoClient(config.MONGODB['HOST'], config.MONGODB['PORT'])[config.MONGODB['DATABASE']] assert(db.authenticate(config.MONGODB['USER'], config.MONGODB['PSWD'])) except (pymongo.errors.AutoReconnect, pymongo.errors.ConnectionFailure) as e: logerr("MongoDB is unreachable, %s \nERROR: Please check `mongo` is installed and restart it with `sudo /etc/init.d/mongodb restart`\nERROR: You may need to repair your database, run `tail -n 30 /var/log/mongodb/mongodb.log` for more details.\nERROR: Classic cleaning would be: `sudo service mongodb stop; sudo rm /var/lib/mongodb/mongod.lock; sudo -u mongodb mongod --dbpath /var/lib/mongodb --repair --repairpath /var/lib/mongodb/%s; sudo service mongodb start`\n" % (e, config.BOTNAME)) exit(1) except (AssertionError, pymongo.errors.OperationFailure) as e: logerr("Cannot connect to database %s in MongoDB.\nERROR: Please check the database and its users are created,\nERROR: or run `bash bin/configureDB.sh` to create or update them automatically (or configureDB-mongo3.sh when using MongoDB v3+).\n%s\n" % (config.MONGODB['DATABASE'], e)) exit(1) # Check Identi.ca config if [1 for c in config.CHANNELS.values() if "IDENTICA" in c]: try: from gazouilleur.identica_auth_config import identica_auth [identica_auth[conf['IDENTICA']['USER'].lower()] for conf in config.CHANNELS.values() if "IDENTICA" in conf] except (ImportError, KeyError) as e: logerr("Could not find `gazouilleur/identica_auth_config.py` with configuration for %s.\nERROR: Please run `python bin/auth_identica.py` to generate your OAuth Identi.ca keys and create it automatically.\n" % e) exit(1) from gazouilleur.lib.microblog import Microblog for chan, conf in config.CHANNELS.iteritems(): if "IDENTICA" not in conf: continue conn = Microblog("identica", conf) try: from urllib2 import urlopen urlopen("https://identi.ca", timeout=15) if not conn.ping(): logerr("Cannot connect to Identi.ca with the auth configuration provided in `gazouilleur/identica_auth_config.py` for channel %s and user @%s.\nERROR: Please rerun `python bin/auth_identica.py` to generate your OAuth Identi.ca keys.\n" % (chan, conf["IDENTICA"]["USER"].lower())) exit(1) except: stderr.write(colorize("WARNING: Identi.ca seems down, bypassing related tests.\n", 'red', style='bold')) # Check Twitter config for chan, conf in config.CHANNELS.iteritems(): if "TWITTER" not in conf: continue conn = Microblog("twitter", conf) if not conn.ping(): logerr("Cannot connect to Twitter with the auth configuration provided in `gazouilleur/config.py` for channel %s and user @%s.\nERROR: Please check you properly set the 4 auth fields and gave \"Read, write, and direct messages\" rights to gazouilleur's app on https://dev.twitter.com and wait at most 15 minutes\n" % (chan, conf["TWITTER"]["USER"])) exit(1) # Check IRC server from twisted.internet import reactor, protocol, ssl from twisted.words.protocols.irc import IRCClient class IRCBotTest(IRCClient): def connectionMade(self): self.factory.doStop() class IRCBotTester(protocol.ClientFactory): protocol = IRCBotTest def clientConnectionFailed(self, connector, reason): self.doStop() logerr("Cannot connect to IRC server %s on port %d: %s.\nERROR: Please check your configuration in `gazouilleur/config.py`.\n" % (config.HOST, config.PORT, reason.getErrorMessage())) reactor.stop() if utils.is_ssl(config): d = reactor.connectSSL(config.HOST, config.PORT, IRCBotTester(), ssl.ClientContextFactory()) else: d = reactor.connectTCP(config.HOST, config.PORT, IRCBotTester())
agpl-3.0
UltracoldAtomsLab/labhardware
projects/beamprofile/beamprofile.py
2
2353
from __future__ import division import pydc1394 as fw from time import sleep, time import numpy as np import pylab as pl from matplotlib import cm from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter import matplotlib import fastfit import interface def sizetext(sx, sy): """ Check if the difference between the two axes are similar or different Returns displayable text """ if abs(sx - sy)/(sx+sy) < 0.05: csign = '~' elif (sx > sy): csign = '>' else: csign = '<' ctext = "wx | wy\n%.1f %s %.1f" %(sx, csign, sy) return ctext if __name__ == "__main__": l = fw.DC1394Library() cams = l.enumerate_cameras() channel = int(raw_input("Which camera you want to see (0/1)? ")) cam0 = fw.Camera(l, cams[channel]['guid'], isospeed=800) print "Connected to: %s / %s" %(cam0.vendor, cam0.model) # Settings cam0.framerate.mode = 'manual' cam0.framerate.val = 25 cam0.exposure.mode = 'manual' cam0.exposure.val = cam0.exposure.range[0] cam0.shutter.mode = 'manual' cam0.shutter.val = cam0.shutter.range[0] print "\nFeatures\n", "="*30 for feat in cam0.features: try: val = cam0.__getattribute__(feat).val except: val = '??' try: mode = cam0.__getattribute__(feat).mode except: mode = '??' print "%s : %s (mode: %s)" %(feat, val, mode) print "Camera modes:", cam0.modes cam0.mode = "640x480_Y8" # the Y16 mode does not seem to work print "Used camera mode: %s" %(cam0.mode) matplotlib.interactive(True) fs = 12.5 fig = pl.figure(num=1, figsize=(fs, fs)) ax = fig.add_subplot(111) cam0.start(interactive=True) elements = None dimx, dimy = 640, 480 pixelsize = 5.6 while True: # image collection and display try: data = np.array(cam0.current_image, dtype='f') if elements is None: # First display, set up output screen elements = interface.createiface(data) else: # Every other iteration just update data interface.updateiface(data, elements) pl.draw() except KeyboardInterrupt: print "Stopping" break except: break cam0.stop()
mit
wzbozon/statsmodels
examples/python/interactions_anova.py
25
10584
## Interactions and ANOVA # Note: This script is based heavily on Jonathan Taylor's class notes http://www.stanford.edu/class/stats191/interactions.html # # Download and format data: from __future__ import print_function from statsmodels.compat import urlopen import numpy as np np.set_printoptions(precision=4, suppress=True) import statsmodels.api as sm import pandas as pd import matplotlib.pyplot as plt from statsmodels.formula.api import ols from statsmodels.graphics.api import interaction_plot, abline_plot from statsmodels.stats.anova import anova_lm try: salary_table = pd.read_csv('salary.table') except: # recent pandas can read URL without urlopen url = 'http://stats191.stanford.edu/data/salary.table' fh = urlopen(url) salary_table = pd.read_table(fh) salary_table.to_csv('salary.table') E = salary_table.E M = salary_table.M X = salary_table.X S = salary_table.S # Take a look at the data: plt.figure(figsize=(6,6)) symbols = ['D', '^'] colors = ['r', 'g', 'blue'] factor_groups = salary_table.groupby(['E','M']) for values, group in factor_groups: i,j = values plt.scatter(group['X'], group['S'], marker=symbols[j], color=colors[i-1], s=144) plt.xlabel('Experience'); plt.ylabel('Salary'); # Fit a linear model: formula = 'S ~ C(E) + C(M) + X' lm = ols(formula, salary_table).fit() print(lm.summary()) # Have a look at the created design matrix: lm.model.exog[:5] # Or since we initially passed in a DataFrame, we have a DataFrame available in lm.model.data.orig_exog[:5] # We keep a reference to the original untouched data in lm.model.data.frame[:5] # Influence statistics infl = lm.get_influence() print(infl.summary_table()) # or get a dataframe df_infl = infl.summary_frame() df_infl[:5] # Now plot the reiduals within the groups separately: resid = lm.resid plt.figure(figsize=(6,6)); for values, group in factor_groups: i,j = values group_num = i*2 + j - 1 # for plotting purposes x = [group_num] * len(group) plt.scatter(x, resid[group.index], marker=symbols[j], color=colors[i-1], s=144, edgecolors='black') plt.xlabel('Group') plt.ylabel('Residuals') # Now we will test some interactions using anova or f_test interX_lm = ols("S ~ C(E) * X + C(M)", salary_table).fit() print(interX_lm.summary()) # Do an ANOVA check from statsmodels.stats.api import anova_lm table1 = anova_lm(lm, interX_lm) print(table1) interM_lm = ols("S ~ X + C(E)*C(M)", data=salary_table).fit() print(interM_lm.summary()) table2 = anova_lm(lm, interM_lm) print(table2) # The design matrix as a DataFrame interM_lm.model.data.orig_exog[:5] # The design matrix as an ndarray interM_lm.model.exog interM_lm.model.exog_names infl = interM_lm.get_influence() resid = infl.resid_studentized_internal plt.figure(figsize=(6,6)) for values, group in factor_groups: i,j = values idx = group.index plt.scatter(X[idx], resid[idx], marker=symbols[j], color=colors[i-1], s=144, edgecolors='black') plt.xlabel('X'); plt.ylabel('standardized resids'); # Looks like one observation is an outlier. drop_idx = abs(resid).argmax() print(drop_idx) # zero-based index idx = salary_table.index.drop(drop_idx) lm32 = ols('S ~ C(E) + X + C(M)', data=salary_table, subset=idx).fit() print(lm32.summary()) print('\n') interX_lm32 = ols('S ~ C(E) * X + C(M)', data=salary_table, subset=idx).fit() print(interX_lm32.summary()) print('\n') table3 = anova_lm(lm32, interX_lm32) print(table3) print('\n') interM_lm32 = ols('S ~ X + C(E) * C(M)', data=salary_table, subset=idx).fit() table4 = anova_lm(lm32, interM_lm32) print(table4) print('\n') # Replot the residuals try: resid = interM_lm32.get_influence().summary_frame()['standard_resid'] except: resid = interM_lm32.get_influence().summary_frame()['standard_resid'] plt.figure(figsize=(6,6)) for values, group in factor_groups: i,j = values idx = group.index plt.scatter(X[idx], resid[idx], marker=symbols[j], color=colors[i-1], s=144, edgecolors='black') plt.xlabel('X[~[32]]'); plt.ylabel('standardized resids'); # Plot the fitted values lm_final = ols('S ~ X + C(E)*C(M)', data = salary_table.drop([drop_idx])).fit() mf = lm_final.model.data.orig_exog lstyle = ['-','--'] plt.figure(figsize=(6,6)) for values, group in factor_groups: i,j = values idx = group.index plt.scatter(X[idx], S[idx], marker=symbols[j], color=colors[i-1], s=144, edgecolors='black') # drop NA because there is no idx 32 in the final model plt.plot(mf.X[idx].dropna(), lm_final.fittedvalues[idx].dropna(), ls=lstyle[j], color=colors[i-1]) plt.xlabel('Experience'); plt.ylabel('Salary'); # From our first look at the data, the difference between Master's and PhD in the management group is different than in the non-management group. This is an interaction between the two qualitative variables management,M and education,E. We can visualize this by first removing the effect of experience, then plotting the means within each of the 6 groups using interaction.plot. U = S - X * interX_lm32.params['X'] plt.figure(figsize=(6,6)) interaction_plot(E, M, U, colors=['red','blue'], markers=['^','D'], markersize=10, ax=plt.gca()) # ## Minority Employment Data try: jobtest_table = pd.read_table('jobtest.table') except: # don't have data already url = 'http://stats191.stanford.edu/data/jobtest.table' jobtest_table = pd.read_table(url) factor_group = jobtest_table.groupby(['ETHN']) plt.figure(figsize=(6,6)) colors = ['purple', 'green'] markers = ['o', 'v'] for factor, group in factor_group: plt.scatter(group['TEST'], group['JPERF'], color=colors[factor], marker=markers[factor], s=12**2) plt.xlabel('TEST'); plt.ylabel('JPERF'); min_lm = ols('JPERF ~ TEST', data=jobtest_table).fit() print(min_lm.summary()) plt.figure(figsize=(6,6)); for factor, group in factor_group: plt.scatter(group['TEST'], group['JPERF'], color=colors[factor], marker=markers[factor], s=12**2) plt.xlabel('TEST') plt.ylabel('JPERF') abline_plot(model_results = min_lm, ax=plt.gca()); min_lm2 = ols('JPERF ~ TEST + TEST:ETHN', data=jobtest_table).fit() print(min_lm2.summary()) plt.figure(figsize=(6,6)); for factor, group in factor_group: plt.scatter(group['TEST'], group['JPERF'], color=colors[factor], marker=markers[factor], s=12**2) abline_plot(intercept = min_lm2.params['Intercept'], slope = min_lm2.params['TEST'], ax=plt.gca(), color='purple'); abline_plot(intercept = min_lm2.params['Intercept'], slope = min_lm2.params['TEST'] + min_lm2.params['TEST:ETHN'], ax=plt.gca(), color='green'); min_lm3 = ols('JPERF ~ TEST + ETHN', data = jobtest_table).fit() print(min_lm3.summary()) plt.figure(figsize=(6,6)); for factor, group in factor_group: plt.scatter(group['TEST'], group['JPERF'], color=colors[factor], marker=markers[factor], s=12**2) abline_plot(intercept = min_lm3.params['Intercept'], slope = min_lm3.params['TEST'], ax=plt.gca(), color='purple'); abline_plot(intercept = min_lm3.params['Intercept'] + min_lm3.params['ETHN'], slope = min_lm3.params['TEST'], ax=plt.gca(), color='green'); min_lm4 = ols('JPERF ~ TEST * ETHN', data = jobtest_table).fit() print(min_lm4.summary()) plt.figure(figsize=(6,6)); for factor, group in factor_group: plt.scatter(group['TEST'], group['JPERF'], color=colors[factor], marker=markers[factor], s=12**2) abline_plot(intercept = min_lm4.params['Intercept'], slope = min_lm4.params['TEST'], ax=plt.gca(), color='purple'); abline_plot(intercept = min_lm4.params['Intercept'] + min_lm4.params['ETHN'], slope = min_lm4.params['TEST'] + min_lm4.params['TEST:ETHN'], ax=plt.gca(), color='green'); # is there any effect of ETHN on slope or intercept? table5 = anova_lm(min_lm, min_lm4) print(table5) # is there any effect of ETHN on intercept table6 = anova_lm(min_lm, min_lm3) print(table6) # is there any effect of ETHN on slope table7 = anova_lm(min_lm, min_lm2) print(table7) # is it just the slope or both? table8 = anova_lm(min_lm2, min_lm4) print(table8) # ## One-way ANOVA try: rehab_table = pd.read_csv('rehab.table') except: url = 'http://stats191.stanford.edu/data/rehab.csv' rehab_table = pd.read_table(url, delimiter=",") rehab_table.to_csv('rehab.table') plt.figure(figsize=(6,6)) rehab_table.boxplot('Time', 'Fitness', ax=plt.gca()) rehab_lm = ols('Time ~ C(Fitness)', data=rehab_table).fit() table9 = anova_lm(rehab_lm) print(table9) print(rehab_lm.model.data.orig_exog) print(rehab_lm.summary()) # ## Two-way ANOVA try: kidney_table = pd.read_table('./kidney.table') except: url = 'http://stats191.stanford.edu/data/kidney.table' kidney_table = pd.read_table(url, delimiter=" *") # Explore the dataset kidney_table.groupby(['Weight', 'Duration']).size() # Balanced panel kt = kidney_table plt.figure(figsize=(6,6)) interaction_plot(kt['Weight'], kt['Duration'], np.log(kt['Days']+1), colors=['red', 'blue'], markers=['D','^'], ms=10, ax=plt.gca()) # You have things available in the calling namespace available in the formula evaluation namespace kidney_lm = ols('np.log(Days+1) ~ C(Duration) * C(Weight)', data=kt).fit() table10 = anova_lm(kidney_lm) print(anova_lm(ols('np.log(Days+1) ~ C(Duration) + C(Weight)', data=kt).fit(), kidney_lm)) print(anova_lm(ols('np.log(Days+1) ~ C(Duration)', data=kt).fit(), ols('np.log(Days+1) ~ C(Duration) + C(Weight, Sum)', data=kt).fit())) print(anova_lm(ols('np.log(Days+1) ~ C(Weight)', data=kt).fit(), ols('np.log(Days+1) ~ C(Duration) + C(Weight, Sum)', data=kt).fit())) # ## Sum of squares # # Illustrates the use of different types of sums of squares (I,II,II) # and how the Sum contrast can be used to produce the same output between # the 3. # # Types I and II are equivalent under a balanced design. # # Don't use Type III with non-orthogonal contrast - ie., Treatment sum_lm = ols('np.log(Days+1) ~ C(Duration, Sum) * C(Weight, Sum)', data=kt).fit() print(anova_lm(sum_lm)) print(anova_lm(sum_lm, typ=2)) print(anova_lm(sum_lm, typ=3)) nosum_lm = ols('np.log(Days+1) ~ C(Duration, Treatment) * C(Weight, Treatment)', data=kt).fit() print(anova_lm(nosum_lm)) print(anova_lm(nosum_lm, typ=2)) print(anova_lm(nosum_lm, typ=3))
bsd-3-clause
eickenberg/scikit-learn
examples/svm/plot_separating_hyperplane_unbalanced.py
329
1850
""" ================================================= SVM: Separating hyperplane for unbalanced classes ================================================= Find the optimal separating hyperplane using an SVC for classes that are unbalanced. We first find the separating plane with a plain SVC and then plot (dashed) the separating hyperplane with automatically correction for unbalanced classes. .. currentmodule:: sklearn.linear_model .. note:: This example will also work by replacing ``SVC(kernel="linear")`` with ``SGDClassifier(loss="hinge")``. Setting the ``loss`` parameter of the :class:`SGDClassifier` equal to ``hinge`` will yield behaviour such as that of a SVC with a linear kernel. For example try instead of the ``SVC``:: clf = SGDClassifier(n_iter=100, alpha=0.01) """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm #from sklearn.linear_model import SGDClassifier # we create 40 separable points rng = np.random.RandomState(0) n_samples_1 = 1000 n_samples_2 = 100 X = np.r_[1.5 * rng.randn(n_samples_1, 2), 0.5 * rng.randn(n_samples_2, 2) + [2, 2]] y = [0] * (n_samples_1) + [1] * (n_samples_2) # fit the model and get the separating hyperplane clf = svm.SVC(kernel='linear', C=1.0) clf.fit(X, y) w = clf.coef_[0] a = -w[0] / w[1] xx = np.linspace(-5, 5) yy = a * xx - clf.intercept_[0] / w[1] # get the separating hyperplane using weighted classes wclf = svm.SVC(kernel='linear', class_weight={1: 10}) wclf.fit(X, y) ww = wclf.coef_[0] wa = -ww[0] / ww[1] wyy = wa * xx - wclf.intercept_[0] / ww[1] # plot separating hyperplanes and samples h0 = plt.plot(xx, yy, 'k-', label='no weights') h1 = plt.plot(xx, wyy, 'k--', label='with weights') plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired) plt.legend() plt.axis('tight') plt.show()
bsd-3-clause
adazey/Muzez
libs/nltk/tokenize/texttiling.py
1
17310
# Natural Language Toolkit: TextTiling # # Copyright (C) 2001-2016 NLTK Project # Author: George Boutsioukis # # URL: <http://nltk.org/> # For license information, see LICENSE.TXT import re import math try: import numpy except ImportError: pass from nltk.tokenize.api import TokenizerI BLOCK_COMPARISON, VOCABULARY_INTRODUCTION = 0, 1 LC, HC = 0, 1 DEFAULT_SMOOTHING = [0] class TextTilingTokenizer(TokenizerI): """Tokenize a document into topical sections using the TextTiling algorithm. This algorithm detects subtopic shifts based on the analysis of lexical co-occurrence patterns. The process starts by tokenizing the text into pseudosentences of a fixed size w. Then, depending on the method used, similarity scores are assigned at sentence gaps. The algorithm proceeds by detecting the peak differences between these scores and marking them as boundaries. The boundaries are normalized to the closest paragraph break and the segmented text is returned. :param w: Pseudosentence size :type w: int :param k: Size (in sentences) of the block used in the block comparison method :type k: int :param similarity_method: The method used for determining similarity scores: `BLOCK_COMPARISON` (default) or `VOCABULARY_INTRODUCTION`. :type similarity_method: constant :param stopwords: A list of stopwords that are filtered out (defaults to NLTK's stopwords corpus) :type stopwords: list(str) :param smoothing_method: The method used for smoothing the score plot: `DEFAULT_SMOOTHING` (default) :type smoothing_method: constant :param smoothing_width: The width of the window used by the smoothing method :type smoothing_width: int :param smoothing_rounds: The number of smoothing passes :type smoothing_rounds: int :param cutoff_policy: The policy used to determine the number of boundaries: `HC` (default) or `LC` :type cutoff_policy: constant >>> from nltk.corpus import brown >>> tt = TextTilingTokenizer(demo_mode=True) >>> text = brown.raw()[:10000] >>> s, ss, d, b = tt.tokenize(text) >>> b [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0] """ def __init__(self, w=20, k=10, similarity_method=BLOCK_COMPARISON, stopwords=None, smoothing_method=DEFAULT_SMOOTHING, smoothing_width=2, smoothing_rounds=1, cutoff_policy=HC, demo_mode=False): if stopwords is None: from nltk.corpus import stopwords stopwords = stopwords.words('english') self.__dict__.update(locals()) del self.__dict__['self'] def tokenize(self, text): """Return a tokenized copy of *text*, where each "token" represents a separate topic.""" lowercase_text = text.lower() paragraph_breaks = self._mark_paragraph_breaks(text) text_length = len(lowercase_text) # Tokenization step starts here # Remove punctuation nopunct_text = ''.join(c for c in lowercase_text if re.match("[a-z\-\' \n\t]", c)) nopunct_par_breaks = self._mark_paragraph_breaks(nopunct_text) tokseqs = self._divide_to_tokensequences(nopunct_text) # The morphological stemming step mentioned in the TextTile # paper is not implemented. A comment in the original C # implementation states that it offers no benefit to the # process. It might be interesting to test the existing # stemmers though. #words = _stem_words(words) # Filter stopwords for ts in tokseqs: ts.wrdindex_list = [wi for wi in ts.wrdindex_list if wi[0] not in self.stopwords] token_table = self._create_token_table(tokseqs, nopunct_par_breaks) # End of the Tokenization step # Lexical score determination if self.similarity_method == BLOCK_COMPARISON: gap_scores = self._block_comparison(tokseqs, token_table) elif self.similarity_method == VOCABULARY_INTRODUCTION: raise NotImplementedError("Vocabulary introduction not implemented") if self.smoothing_method == DEFAULT_SMOOTHING: smooth_scores = self._smooth_scores(gap_scores) # End of Lexical score Determination # Boundary identification depth_scores = self._depth_scores(smooth_scores) segment_boundaries = self._identify_boundaries(depth_scores) normalized_boundaries = self._normalize_boundaries(text, segment_boundaries, paragraph_breaks) # End of Boundary Identification segmented_text = [] prevb = 0 for b in normalized_boundaries: if b == 0: continue segmented_text.append(text[prevb:b]) prevb = b if prevb < text_length: # append any text that may be remaining segmented_text.append(text[prevb:]) if not segmented_text: segmented_text = [text] if self.demo_mode: return gap_scores, smooth_scores, depth_scores, segment_boundaries return segmented_text def _block_comparison(self, tokseqs, token_table): "Implements the block comparison method" def blk_frq(tok, block): ts_occs = filter(lambda o: o[0] in block, token_table[tok].ts_occurences) freq = sum([tsocc[1] for tsocc in ts_occs]) return freq gap_scores = [] numgaps = len(tokseqs)-1 for curr_gap in range(numgaps): score_dividend, score_divisor_b1, score_divisor_b2 = 0.0, 0.0, 0.0 score = 0.0 #adjust window size for boundary conditions if curr_gap < self.k-1: window_size = curr_gap + 1 elif curr_gap > numgaps-self.k: window_size = numgaps - curr_gap else: window_size = self.k b1 = [ts.index for ts in tokseqs[curr_gap-window_size+1 : curr_gap+1]] b2 = [ts.index for ts in tokseqs[curr_gap+1 : curr_gap+window_size+1]] for t in token_table: score_dividend += blk_frq(t, b1)*blk_frq(t, b2) score_divisor_b1 += blk_frq(t, b1)**2 score_divisor_b2 += blk_frq(t, b2)**2 try: score = score_dividend/math.sqrt(score_divisor_b1* score_divisor_b2) except ZeroDivisionError: pass # score += 0.0 gap_scores.append(score) return gap_scores def _smooth_scores(self, gap_scores): "Wraps the smooth function from the SciPy Cookbook" return list(smooth(numpy.array(gap_scores[:]), window_len = self.smoothing_width+1)) def _mark_paragraph_breaks(self, text): """Identifies indented text or line breaks as the beginning of paragraphs""" MIN_PARAGRAPH = 100 pattern = re.compile("[ \t\r\f\v]*\n[ \t\r\f\v]*\n[ \t\r\f\v]*") matches = pattern.finditer(text) last_break = 0 pbreaks = [0] for pb in matches: if pb.start()-last_break < MIN_PARAGRAPH: continue else: pbreaks.append(pb.start()) last_break = pb.start() return pbreaks def _divide_to_tokensequences(self, text): "Divides the text into pseudosentences of fixed size" w = self.w wrdindex_list = [] matches = re.finditer("\w+", text) for match in matches: wrdindex_list.append((match.group(), match.start())) return [TokenSequence(i/w, wrdindex_list[i:i+w]) for i in range(0, len(wrdindex_list), w)] def _create_token_table(self, token_sequences, par_breaks): "Creates a table of TokenTableFields" token_table = {} current_par = 0 current_tok_seq = 0 pb_iter = par_breaks.__iter__() current_par_break = next(pb_iter) if current_par_break == 0: try: current_par_break = next(pb_iter) #skip break at 0 except StopIteration: raise ValueError( "No paragraph breaks were found(text too short perhaps?)" ) for ts in token_sequences: for word, index in ts.wrdindex_list: try: while index > current_par_break: current_par_break = next(pb_iter) current_par += 1 except StopIteration: #hit bottom pass if word in token_table: token_table[word].total_count += 1 if token_table[word].last_par != current_par: token_table[word].last_par = current_par token_table[word].par_count += 1 if token_table[word].last_tok_seq != current_tok_seq: token_table[word].last_tok_seq = current_tok_seq token_table[word]\ .ts_occurences.append([current_tok_seq,1]) else: token_table[word].ts_occurences[-1][1] += 1 else: #new word token_table[word] = TokenTableField(first_pos=index, ts_occurences= \ [[current_tok_seq,1]], total_count=1, par_count=1, last_par=current_par, last_tok_seq= \ current_tok_seq) current_tok_seq += 1 return token_table def _identify_boundaries(self, depth_scores): """Identifies boundaries at the peaks of similarity score differences""" boundaries = [0 for x in depth_scores] avg = sum(depth_scores)/len(depth_scores) stdev = numpy.std(depth_scores) #SB: what is the purpose of this conditional? if self.cutoff_policy == LC: cutoff = avg-stdev/2.0 else: cutoff = avg-stdev/2.0 depth_tuples = sorted(zip(depth_scores, range(len(depth_scores)))) depth_tuples.reverse() hp = list(filter(lambda x:x[0]>cutoff, depth_tuples)) for dt in hp: boundaries[dt[1]] = 1 for dt2 in hp: #undo if there is a boundary close already if dt[1] != dt2[1] and abs(dt2[1]-dt[1]) < 4 \ and boundaries[dt2[1]] == 1: boundaries[dt[1]] = 0 return boundaries def _depth_scores(self, scores): """Calculates the depth of each gap, i.e. the average difference between the left and right peaks and the gap's score""" depth_scores = [0 for x in scores] #clip boundaries: this holds on the rule of thumb(my thumb) #that a section shouldn't be smaller than at least 2 #pseudosentences for small texts and around 5 for larger ones. clip = min(max(len(scores)/10, 2), 5) index = clip for gapscore in scores[clip:-clip]: lpeak = gapscore for score in scores[index::-1]: if score >= lpeak: lpeak = score else: break rpeak = gapscore for score in scores[index:]: if score >= rpeak: rpeak = score else: break depth_scores[index] = lpeak + rpeak - 2 * gapscore index += 1 return depth_scores def _normalize_boundaries(self, text, boundaries, paragraph_breaks): """Normalize the boundaries identified to the original text's paragraph breaks""" norm_boundaries = [] char_count, word_count, gaps_seen = 0, 0, 0 seen_word = False for char in text: char_count += 1 if char in " \t\n" and seen_word: seen_word = False word_count += 1 if char not in " \t\n" and not seen_word: seen_word=True if gaps_seen < len(boundaries) and word_count > \ (max(gaps_seen*self.w, self.w)): if boundaries[gaps_seen] == 1: #find closest paragraph break best_fit = len(text) for br in paragraph_breaks: if best_fit > abs(br-char_count): best_fit = abs(br-char_count) bestbr = br else: break if bestbr not in norm_boundaries: #avoid duplicates norm_boundaries.append(bestbr) gaps_seen += 1 return norm_boundaries class TokenTableField(object): """A field in the token table holding parameters for each token, used later in the process""" def __init__(self, first_pos, ts_occurences, total_count=1, par_count=1, last_par=0, last_tok_seq=None): self.__dict__.update(locals()) del self.__dict__['self'] class TokenSequence(object): "A token list with its original length and its index" def __init__(self, index, wrdindex_list, original_length=None): original_length=original_length or len(wrdindex_list) self.__dict__.update(locals()) del self.__dict__['self'] #Pasted from the SciPy cookbook: http://www.scipy.org/Cookbook/SignalSmooth def smooth(x,window_len=11,window='flat'): """smooth the data using a window with requested size. This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the beginning and end part of the output signal. :param x: the input signal :param window_len: the dimension of the smoothing window; should be an odd integer :param window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' flat window will produce a moving average smoothing. :return: the smoothed signal example:: t=linspace(-2,2,0.1) x=sin(t)+randn(len(t))*0.1 y=smooth(x) :see also: numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve, scipy.signal.lfilter TODO: the window parameter could be the window itself if an array instead of a string """ if x.ndim != 1: raise ValueError("smooth only accepts 1 dimension arrays.") if x.size < window_len: raise ValueError("Input vector needs to be bigger than window size.") if window_len < 3: return x if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']: raise ValueError("Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'") s=numpy.r_[2*x[0]-x[window_len:1:-1],x,2*x[-1]-x[-1:-window_len:-1]] #print(len(s)) if window == 'flat': #moving average w = numpy.ones(window_len,'d') else: w = eval('numpy.' + window + '(window_len)') y = numpy.convolve(w/w.sum(), s, mode='same') return y[window_len-1:-window_len+1] def demo(text=None): from nltk.corpus import brown from matplotlib import pylab tt = TextTilingTokenizer(demo_mode=True) if text is None: text = brown.raw()[:10000] s, ss, d, b = tt.tokenize(text) pylab.xlabel("Sentence Gap index") pylab.ylabel("Gap Scores") pylab.plot(range(len(s)), s, label="Gap Scores") pylab.plot(range(len(ss)), ss, label="Smoothed Gap scores") pylab.plot(range(len(d)), d, label="Depth scores") pylab.stem(range(len(b)), b) pylab.legend() pylab.show()
gpl-3.0
padraic-padraic/MPHYSG001_CW1
greengraph/test/test_google_map.py
1
3480
from ..google_map import Map from mock import patch from nose.tools import assert_equal from nose.tools import assert_raises from .colors import colors import numpy as np @patch('requests.get') @patch('matplotlib.image.imread') def test_map_init(mock_imread,mock_get): m = Map(10.,10.) mock_get.assert_called_with('http://maps.googleapis.com/maps/api/staticmap?', params={'style': 'feature:all|element:labels|visibility:off', 'center': '10.0,10.0', 'zoom': 10, 'maptype': 'satellite', 'sensor': 'false', 'size': '400x400'}) m = Map(10.,10., satellite=False) mock_get.assert_called_with('http://maps.googleapis.com/maps/api/staticmap?', params={'style': 'feature:all|element:labels|visibility:off', 'center': '10.0,10.0', 'zoom': 10, 'sensor': 'false', 'size': '400x400'}) m = Map(10.,10., sensor=True) mock_get.assert_called_with('http://maps.googleapis.com/maps/api/staticmap?', params={'style': 'feature:all|element:labels|visibility:off', 'center': '10.0,10.0', 'zoom': 10, 'maptype': 'satellite', 'sensor': 'true', 'size': '400x400'}) m = Map(10.,10., size=(200,300)) mock_get.assert_called_with('http://maps.googleapis.com/maps/api/staticmap?', params={'style': 'feature:all|element:labels|visibility:off', 'center': '10.0,10.0', 'zoom': 10, 'maptype': 'satellite', 'sensor': 'false', 'size': '200x300'}) m = Map(10.,10., zoom=5) mock_get.assert_called_with('http://maps.googleapis.com/maps/api/staticmap?', params={'style': 'feature:all|element:labels|visibility:off', 'center': '10.0,10.0', 'zoom': 5, 'maptype': 'satellite', 'sensor': 'false', 'size': '400x400'}) def test_green_detection(): #Colour RGB values taken from 500 colors list, http://cloford.com/resources/colours/500col.htm m = Map(10,10) trues = [] for color in colors: pixel = np.array([[[color[0]/255.,color[1]/255.,color[2]/255.]]]) m.pixels = pixel trues.append(m.green(1.1)[0,0]) assert np.sum(trues) == 54 def test_green_count(): vals = range(1,100) m = Map(10.,15.) for val in vals: pixels = ([[0.,1.,0.]] * val) + ([[1.,1.,1.]] * (100-val)) pixels = np.array(pixels, dtype='float32') pixels = pixels.reshape(10,10,3) m.pixels = pixels assert_equal(m.count_green(), val) @patch('matplotlib.image.imsave') def test_green_save(mock_imsave): vals = range(1,100) m = Map(10.,20.) for val in vals: pixels = ([[0,1,0]] * val) + ([[0,0,0]] * (100-val)) pixels = np.array(pixels) pixels = pixels.reshape(10,10,3) m.pixels = pixels m.show_green() assert np.array_equal(mock_imsave.call_args[0][1],pixels) assert_equal(mock_imsave.call_args[1], {'format':'png'})
gpl-2.0
olologin/scikit-learn
sklearn/utils/metaestimators.py
283
2353
"""Utilities for meta-estimators""" # Author: Joel Nothman # Andreas Mueller # Licence: BSD from operator import attrgetter from functools import update_wrapper __all__ = ['if_delegate_has_method'] class _IffHasAttrDescriptor(object): """Implements a conditional property using the descriptor protocol. Using this class to create a decorator will raise an ``AttributeError`` if the ``attribute_name`` is not present on the base object. This allows ducktyping of the decorated method based on ``attribute_name``. See https://docs.python.org/3/howto/descriptor.html for an explanation of descriptors. """ def __init__(self, fn, attribute_name): self.fn = fn self.get_attribute = attrgetter(attribute_name) # update the docstring of the descriptor update_wrapper(self, fn) def __get__(self, obj, type=None): # raise an AttributeError if the attribute is not present on the object if obj is not None: # delegate only on instances, not the classes. # this is to allow access to the docstrings. self.get_attribute(obj) # lambda, but not partial, allows help() to work with update_wrapper out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) # update the docstring of the returned function update_wrapper(out, self.fn) return out def if_delegate_has_method(delegate): """Create a decorator for methods that are delegated to a sub-estimator This enables ducktyping by hasattr returning True according to the sub-estimator. >>> from sklearn.utils.metaestimators import if_delegate_has_method >>> >>> >>> class MetaEst(object): ... def __init__(self, sub_est): ... self.sub_est = sub_est ... ... @if_delegate_has_method(delegate='sub_est') ... def predict(self, X): ... return self.sub_est.predict(X) ... >>> class HasPredict(object): ... def predict(self, X): ... return X.sum(axis=1) ... >>> class HasNoPredict(object): ... pass ... >>> hasattr(MetaEst(HasPredict()), 'predict') True >>> hasattr(MetaEst(HasNoPredict()), 'predict') False """ return lambda fn: _IffHasAttrDescriptor(fn, '%s.%s' % (delegate, fn.__name__))
bsd-3-clause
pythonvietnam/scikit-learn
sklearn/tests/test_naive_bayes.py
70
17509
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:1]).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
danielballan/dataportal
dataportal/broker/simple_broker.py
1
13435
from __future__ import print_function import warnings import six # noqa from collections import Iterable, deque import pandas as pd import tzlocal from metadatastore.commands import (find_last, find_run_starts, find_descriptors, get_events_generator, get_events_table) import metadatastore.doc as doc import metadatastore.commands as mc import filestore.api as fs import logging logger = logging.getLogger(__name__) TZ = str(tzlocal.get_localzone()) class _DataBrokerClass(object): # A singleton is instantiated in broker/__init__.py. # You probably do not want to instantiate this; use # broker.DataBroker instead. def __getitem__(self, key): """DWIM slicing Some more docs go here """ if isinstance(key, slice): # Interpret key as a slice into previous scans. if key.start is not None and key.start > -1: raise ValueError("Slices must be negative. The most recent " "run is referred to as -1.") if key.stop is not None and key.stop > 0: raise ValueError("Slices must be negative. The most recent " "run is referred to as -1.") if key.stop is not None: stop = -key.stop else: stop = None if key.start is None: raise ValueError("Cannot slice infinitely into the past; " "the result could become too large.") start = -key.start result = list(find_last(start))[stop::key.step] header = [Header.from_run_start(h) for h in result] elif isinstance(key, int): if key > -1: # Interpret key as a scan_id. gen = find_run_starts(scan_id=key) try: result = next(gen) # most recent match except StopIteration: raise ValueError("No such run found.") header = Header.from_run_start(result) else: # Interpret key as the Nth last scan. gen = find_last(-key) for i in range(-key): try: result = next(gen) except StopIteration: raise IndexError( "There are only {0} runs.".format(i)) header = Header.from_run_start(result) elif isinstance(key, six.string_types): # Interpret key as a uid (or the few several characters of one). # First try searching as if we have the full uid. results = list(find_run_starts(uid=key)) if len(results) == 0: # No dice? Try searching as if we have a partial uid. gen = find_run_starts(uid={'$regex': '{0}.*'.format(key)}) results = list(gen) if len(results) < 1: raise ValueError("No such run found.") if len(results) > 1: raise ValueError("That partial uid matches multiple runs. " "Provide more characters.") result, = results header = Header.from_run_start(result) elif isinstance(key, Iterable): # Interpret key as a list of several keys. If it is a string # we will never get this far. return [self.__getitem__(k) for k in key] else: raise ValueError("Must give an integer scan ID like [6], a slice " "into past scans like [-5], [-5:], or [-5:-9:2], " "a list like [1, 7, 13], or a (partial) uid " "like ['a23jslk'].") return header def __call__(self, **kwargs): """Given search criteria, find Headers describing runs. This function returns a list of dictionary-like objects encapsulating the metadata for a run -- start time, instruments uses, and so on. In addition to the Parameters below, advanced users can specifiy arbitrary queries that are passed through to mongodb. Parameters ---------- start_time : time-like, optional Include Headers for runs started after this time. Valid "time-like" representations are: - float timestamps (seconds since 1970), such as time.time() - '2015' - '2015-01' - '2015-01-30' - '2015-03-30 03:00:00' - Python datetime objects, such as datetime.datetime.now() stop_time: time-like, optional Include Headers for runs started before this time. See `start_time` above for examples. beamline_id : str, optional String identifier for a specific beamline project : str, optional Project name owner : str, optional The username of the logged-in user when the scan was performed scan_id : int, optional Integer scan identifier uid : str, optional Globally unique id string provided to metadatastore data_key : str, optional The alias (e.g., 'motor1') or PV identifier of data source Returns ------- data : list Header objects Examples -------- >>> DataBroker(start_time='2015-03-05', stop_time='2015-03-10') >>> DataBroker(data_key='motor1') >>> DataBroker(data_key='motor1', start_time='2015-03-05') """ data_key = kwargs.pop('data_key', None) run_start = find_run_starts(**kwargs) if data_key is not None: node_name = 'data_keys.{0}'.format(data_key) query = {node_name: {'$exists': True}} descriptors = [] for rs in run_start: descriptor = find_descriptors(run_start=rs, **query) for d in descriptor: descriptors.append(d) # query = {node_name: {'$exists': True}, # 'run_start_id': {'$in': [ObjectId(rs.id) for rs in run_start]}} # descriptors = find_descriptors(**query) result = [] known_uids = deque() for descriptor in descriptors: if descriptor['run_start']['uid'] not in known_uids: rs = descriptor['run_start'] known_uids.append(rs['uid']) result.append(rs) run_start = result result = [] for rs in run_start: result.append(Header.from_run_start(rs)) return result def find_headers(self, **kwargs): "This function is deprecated. Use DataBroker() instead." warnings.warn("Use DataBroker() instead of " "DataBroker.find_headers()", UserWarning) return self(**kwargs) def fetch_events(self, headers, fill=True): "This function is deprecated. Use top-level function get_events." warnings.warn("Use top-level function " "get_events() instead.", UserWarning) return get_events(headers, None, fill) DataBroker = _DataBrokerClass() # singleton, used by pims_readers import below def _inspect_descriptor(descriptor): """ Return a dict with the data keys mapped to boolean answering whether data is external. """ # TODO memoize to cache these results data_keys = descriptor.data_keys is_external = dict() for data_key, data_key_dict in data_keys.items(): is_external[data_key] = data_key_dict.get('external', False) return is_external def fill_event(event): """ Populate events with externally stored data. """ is_external = _inspect_descriptor(event.descriptor) for data_key, value in six.iteritems(event.data): if is_external[data_key]: # Retrieve a numpy array from filestore event.data[data_key] = fs.retrieve(value) class Header(doc.Document): """A dictionary-like object summarizing metadata for a run.""" @classmethod def from_run_start(cls, run_start, verify_integrity=False): """ Build a Header from a RunStart Document. Parameters ---------- run_start : metadatastore.document.Document or str RunStart Document or uid Returns ------- header : dataportal.broker.Header """ run_start_uid = mc.doc_or_uid_to_uid(run_start) run_start = mc.run_start_given_uid(run_start_uid) try: run_stop = doc.ref_doc_to_uid(mc.stop_by_start(run_start_uid), 'run_start') except mc.NoRunStop: run_stop = None try: ev_descs = [doc.ref_doc_to_uid(ev_desc, 'run_start') for ev_desc in mc.descriptors_by_start(run_start_uid)] except mc.NoEventDescriptors: ev_descs = [] d = {'start': run_start, 'stop': run_stop, 'descriptors': ev_descs} return cls('header', d) def get_events(headers, fields=None, fill=True): """ Get Events from given run(s). Parameters ---------- headers : Header or iterable of Headers The headers to fetch the events for fields : list, optional whitelist of field names of interest; if None, all are returned fill : bool, optional Whether externally-stored data should be filled in. Defaults to True Yields ------ event : Event The event, optionally with non-scalar data filled in """ # A word about the 'fields' argument: # Notice that we assume that the same field name cannot occur in # more than one descriptor. We could relax this assumption, but # we current enforce it in bluesky, so it is safe for now. try: headers.items() except AttributeError: pass else: headers = [headers] if fields is None: fields = [] fields = set(fields) for header in headers: descriptors = find_descriptors(header['start']['uid']) for descriptor in descriptors: all_fields = set(descriptor['data_keys']) if fields: discard_fields = all_fields - fields else: discard_fields = [] if discard_fields == all_fields: continue for event in get_events_generator(descriptor): for field in discard_fields: del event.data[field] del event.timestamps[field] if fill: fill_event(event) yield event def get_table(headers, fields=None, fill=True, convert_times=True): """ Make a table (pandas.DataFrame) from given run(s). Parameters ---------- headers : Header or iterable of Headers The headers to fetch the events for fields : list, optional whitelist of field names of interest; if None, all are returned fill : bool, optional Whether externally-stored data should be filled in. Defaults to True convert_times : bool, optional Whether to convert times from float (seconds since 1970) to numpy datetime64, using pandas. True by default. Returns ------- table : pandas.DataFrame """ # A word about the 'fields' argument: # Notice that we assume that the same field name cannot occur in # more than one descriptor. We could relax this assumption, but # we current enforce it in bluesky, so it is safe for now. try: headers.items() except AttributeError: pass else: headers = [headers] if fields is None: fields = [] fields = set(fields) dfs = [] for header in headers: descriptors = find_descriptors(header['start']['uid']) for descriptor in descriptors: all_fields = set(descriptor['data_keys']) if fields: discard_fields = all_fields - fields else: discard_fields = [] if discard_fields == all_fields: continue is_external = _inspect_descriptor(descriptor) payload = get_events_table(descriptor) descriptor, data, seq_nums, times, uids, timestamps = payload df = pd.DataFrame(index=seq_nums) if convert_times: times = pd.to_datetime( pd.Series(times), unit='s', utc=True).dt.tz_localize(TZ) df['time'] = times for field, values in six.iteritems(data): if field in discard_fields: logger.debug('Discarding field %s', field) continue if is_external[field] and fill: logger.debug('filling data for %s', field) # TODO someday we will have bulk retrieve in FS values = [fs.retrieve(value) for value in values] df[field] = values dfs.append(df) if dfs: return pd.concat(dfs) else: # edge case: no data return pd.DataFrame()
bsd-3-clause
Monika319/EWEF-1
Cw2Rezonans/Karolina/Oscyloskop/Fourier moduł i faza/OscyloskopZ9W3Fourier.py
1
1966
# -*- coding: utf-8 -*- """ Plot oscilloscope files from MultiSim """ import numpy as np import matplotlib.pyplot as plt import sys import os from matplotlib import rc rc('font',family="Consolas") files=["real_zad9_033f.txt"] for NazwaPliku in files: print NazwaPliku Plik=open(NazwaPliku) #print DeltaT Dane=Plik.readlines()#[4:] DeltaT=float(Dane[2].split()[3].replace(",",".")) #M=len(Dane[4].split())/2 M=2 Dane=Dane[5:] Plik.close() print M Ys=[np.zeros(len(Dane)) for i in range(M)] for m in range(M): for i in range(len(Dane)): try: Ys[m][i]=float(Dane[i].split()[2+3*m].replace(",",".")) except: print m, i, 2+3*m, len(Dane[i].split()), Dane[i].split() #print i, Y[i] X=np.zeros_like(Ys[1]) for i in range(len(X)): X[i]=i*DeltaT w0= np.fft.rfft(Ys[0]) w1= np.fft.rfft(Ys[1]) m0=np.abs(w0) m1=np.abs(w1) p0=np.angle(w0) p1=np.angle(w1) #freqs = np.abs(np.fft.rfftfreq(len(Ys[1]))/DeltaT) freqs = np.fft.rfftfreq(len(Ys[1]))/DeltaT Opis=u"Układ równoległy\nJedna trzecia częstotliwości rezonansowej" Nazwa=u"Z9W3Fourier" plt.title(u"Transformata Fouriera sygnału wyjściowego\n"+Opis) plt.xlabel(u"Częstotliwość [Hz]") plt.ylabel(u"Moduł amplitudy") plt.plot(freqs,m0, "-", label=u"Wejście") plt.plot(freqs,m1, "-", label=u"Wyjście") plt.xlim(0,2e5) plt.grid() plt.legend(loc="best") plt.savefig(Nazwa + "Modul.png", bbox_inches='tight') plt.show() plt.title(u"Transformata Fouriera sygnału wyjściowego\n"+Opis) plt.xlabel(u"Częstotliwość [Hz]") plt.ylabel(u"Przesunięcie fazowe") plt.plot(freqs,p0, "-", label=u"Wejście") plt.plot(freqs,p1, "-", label=u"Wyjście") plt.xlim(0,2e5) plt.grid() plt.legend(loc="best") plt.savefig(Nazwa + "Faza.png", bbox_inches='tight') plt.show()
gpl-2.0
soulmachine/scikit-learn
examples/svm/plot_svm_regression.py
249
1451
""" =================================================================== Support Vector Regression (SVR) using linear and non-linear kernels =================================================================== Toy example of 1D regression using linear, polynomial and RBF kernels. """ print(__doc__) import numpy as np from sklearn.svm import SVR import matplotlib.pyplot as plt ############################################################################### # Generate sample data X = np.sort(5 * np.random.rand(40, 1), axis=0) y = np.sin(X).ravel() ############################################################################### # Add noise to targets y[::5] += 3 * (0.5 - np.random.rand(8)) ############################################################################### # Fit regression model svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) svr_lin = SVR(kernel='linear', C=1e3) svr_poly = SVR(kernel='poly', C=1e3, degree=2) y_rbf = svr_rbf.fit(X, y).predict(X) y_lin = svr_lin.fit(X, y).predict(X) y_poly = svr_poly.fit(X, y).predict(X) ############################################################################### # look at the results plt.scatter(X, y, c='k', label='data') plt.hold('on') plt.plot(X, y_rbf, c='g', label='RBF model') plt.plot(X, y_lin, c='r', label='Linear model') plt.plot(X, y_poly, c='b', label='Polynomial model') plt.xlabel('data') plt.ylabel('target') plt.title('Support Vector Regression') plt.legend() plt.show()
bsd-3-clause
kevalds51/sympy
examples/intermediate/sample.py
107
3494
""" Utility functions for plotting sympy functions. See examples\mplot2d.py and examples\mplot3d.py for usable 2d and 3d graphing functions using matplotlib. """ from sympy.core.sympify import sympify, SympifyError from sympy.external import import_module np = import_module('numpy') def sample2d(f, x_args): """ Samples a 2d function f over specified intervals and returns two arrays (X, Y) suitable for plotting with matlab (matplotlib) syntax. See examples\mplot2d.py. f is a function of one variable, such as x**2. x_args is an interval given in the form (var, min, max, n) """ try: f = sympify(f) except SympifyError: raise ValueError("f could not be interpretted as a SymPy function") try: x, x_min, x_max, x_n = x_args except AttributeError: raise ValueError("x_args must be a tuple of the form (var, min, max, n)") x_l = float(x_max - x_min) x_d = x_l/float(x_n) X = np.arange(float(x_min), float(x_max) + x_d, x_d) Y = np.empty(len(X)) for i in range(len(X)): try: Y[i] = float(f.subs(x, X[i])) except TypeError: Y[i] = None return X, Y def sample3d(f, x_args, y_args): """ Samples a 3d function f over specified intervals and returns three 2d arrays (X, Y, Z) suitable for plotting with matlab (matplotlib) syntax. See examples\mplot3d.py. f is a function of two variables, such as x**2 + y**2. x_args and y_args are intervals given in the form (var, min, max, n) """ x, x_min, x_max, x_n = None, None, None, None y, y_min, y_max, y_n = None, None, None, None try: f = sympify(f) except SympifyError: raise ValueError("f could not be interpreted as a SymPy function") try: x, x_min, x_max, x_n = x_args y, y_min, y_max, y_n = y_args except AttributeError: raise ValueError("x_args and y_args must be tuples of the form (var, min, max, intervals)") x_l = float(x_max - x_min) x_d = x_l/float(x_n) x_a = np.arange(float(x_min), float(x_max) + x_d, x_d) y_l = float(y_max - y_min) y_d = y_l/float(y_n) y_a = np.arange(float(y_min), float(y_max) + y_d, y_d) def meshgrid(x, y): """ Taken from matplotlib.mlab.meshgrid. """ x = np.array(x) y = np.array(y) numRows, numCols = len(y), len(x) x.shape = 1, numCols X = np.repeat(x, numRows, 0) y.shape = numRows, 1 Y = np.repeat(y, numCols, 1) return X, Y X, Y = np.meshgrid(x_a, y_a) Z = np.ndarray((len(X), len(X[0]))) for j in range(len(X)): for k in range(len(X[0])): try: Z[j][k] = float(f.subs(x, X[j][k]).subs(y, Y[j][k])) except (TypeError, NotImplementedError): Z[j][k] = 0 return X, Y, Z def sample(f, *var_args): """ Samples a 2d or 3d function over specified intervals and returns a dataset suitable for plotting with matlab (matplotlib) syntax. Wrapper for sample2d and sample3d. f is a function of one or two variables, such as x**2. var_args are intervals for each variable given in the form (var, min, max, n) """ if len(var_args) == 1: return sample2d(f, var_args[0]) elif len(var_args) == 2: return sample3d(f, var_args[0], var_args[1]) else: raise ValueError("Only 2d and 3d sampling are supported at this time.")
bsd-3-clause
tmhm/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
jpzk/evopy
evopy/examples/experiments/constraints_dses_dsessvcal/simulate.py
2
2819
''' This file is part of evopy. Copyright 2012 - 2013, Jendrik Poloczek evopy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. evopy 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 evopy. If not, see <http://www.gnu.org/licenses/>. ''' from sys import path path.append("../../../..") from pickle import dump from copy import deepcopy from numpy import matrix, log10 from evopy.strategies.ori_dses_svc_repair import ORIDSESSVCR from evopy.strategies.ori_dses_svc import ORIDSESSVC from evopy.strategies.ori_dses import ORIDSES from evopy.simulators.simulator import Simulator from evopy.problems.sphere_problem_origin_r1 import SphereProblemOriginR1 from evopy.problems.sphere_problem_origin_r2 import SphereProblemOriginR2 from evopy.problems.schwefels_problem_26 import SchwefelsProblem26 from evopy.problems.tr_problem import TRProblem from evopy.metamodel.dses_svc_linear_meta_model import DSESSVCLinearMetaModel from sklearn.cross_validation import KFold from evopy.operators.scaling.scaling_standardscore import ScalingStandardscore from evopy.operators.scaling.scaling_dummy import ScalingDummy from evopy.metamodel.cv.svc_cv_sklearn_grid_linear import SVCCVSkGridLinear from evopy.operators.termination.or_combinator import ORCombinator from evopy.operators.termination.accuracy import Accuracy from evopy.operators.termination.generations import Generations from evopy.operators.termination.convergence import Convergence from evopy.external.playdoh import map as pmap from os.path import exists from os import mkdir from setup import * # create simulators for problem in problems: for optimizer in optimizers[problem]: simulators_op = [] for i in range(0, samples): simulator = Simulator(optimizer(), problem(), termination) simulators_op.append(simulator) simulators[problem][optimizer] = simulators_op simulate = lambda simulator : simulator.simulate() # run simulators for problem in problems: for optimizer, simulators_ in simulators[problem].iteritems(): resulting_simulators = pmap(simulate, simulators_) for simulator in resulting_simulators: cfc = simulator.logger.all()['count_cfc'] cfcs[problem][optimizer].append(cfc) if not exists("output/"): mkdir("output/") cfc_file = open("output/cfcs_file.save", "w") dump(cfcs, cfc_file) cfc_file.close()
gpl-3.0
jyeatman/dipy
dipy/tests/test_scripts.py
9
4292
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: """ Test scripts Run scripts and check outputs """ from __future__ import division, print_function, absolute_import import os import shutil from os.path import (dirname, join as pjoin, abspath) from nose.tools import assert_true, assert_false, assert_equal import numpy.testing as nt import nibabel as nib from nibabel.tmpdirs import InTemporaryDirectory from dipy.data import get_data # Quickbundles command-line requires matplotlib: try: import matplotlib no_mpl = False except ImportError: no_mpl = True from .scriptrunner import ScriptRunner runner = ScriptRunner( script_sdir = 'bin', debug_print_var = 'NIPY_DEBUG_PRINT') run_command = runner.run_command DATA_PATH = abspath(pjoin(dirname(__file__), 'data')) def test_dipy_peak_extraction(): # test dipy_peak_extraction script cmd = 'dipy_peak_extraction' code, stdout, stderr = run_command(cmd, check_code=False) assert_equal(code, 2) def test_dipy_fit_tensor(): # test dipy_fit_tensor script cmd = 'dipy_fit_tensor' code, stdout, stderr = run_command(cmd, check_code=False) assert_equal(code, 2) def test_dipy_sh_estimate(): # test dipy_sh_estimate script cmd = 'dipy_sh_estimate' code, stdout, stderr = run_command(cmd, check_code=False) assert_equal(code, 2) def assert_image_shape_affine(filename, shape, affine): assert_true(os.path.isfile(filename)) image = nib.load(filename) assert_equal(image.shape, shape) nt.assert_array_almost_equal(image.get_affine(), affine) def test_dipy_fit_tensor_again(): with InTemporaryDirectory(): dwi, bval, bvec = get_data("small_25") # Copy data to tmp directory shutil.copyfile(dwi, "small_25.nii.gz") shutil.copyfile(bval, "small_25.bval") shutil.copyfile(bvec, "small_25.bvec") # Call script cmd = ["dipy_fit_tensor", "--mask=none", "small_25.nii.gz"] out = run_command(cmd) assert_equal(out[0], 0) # Get expected values img = nib.load("small_25.nii.gz") affine = img.get_affine() shape = img.shape[:-1] # Check expected outputs assert_image_shape_affine("small_25_fa.nii.gz", shape, affine) assert_image_shape_affine("small_25_t2di.nii.gz", shape, affine) assert_image_shape_affine("small_25_dirFA.nii.gz", shape, affine) assert_image_shape_affine("small_25_ad.nii.gz", shape, affine) assert_image_shape_affine("small_25_md.nii.gz", shape, affine) assert_image_shape_affine("small_25_rd.nii.gz", shape, affine) with InTemporaryDirectory(): dwi, bval, bvec = get_data("small_25") # Copy data to tmp directory shutil.copyfile(dwi, "small_25.nii.gz") shutil.copyfile(bval, "small_25.bval") shutil.copyfile(bvec, "small_25.bvec") # Call script cmd = ["dipy_fit_tensor", "--save-tensor", "--mask=none", "small_25.nii.gz"] out = run_command(cmd) assert_equal(out[0], 0) # Get expected values img = nib.load("small_25.nii.gz") affine = img.get_affine() shape = img.shape[:-1] # Check expected outputs assert_image_shape_affine("small_25_fa.nii.gz", shape, affine) assert_image_shape_affine("small_25_t2di.nii.gz", shape, affine) assert_image_shape_affine("small_25_dirFA.nii.gz", shape, affine) assert_image_shape_affine("small_25_ad.nii.gz", shape, affine) assert_image_shape_affine("small_25_md.nii.gz", shape, affine) assert_image_shape_affine("small_25_rd.nii.gz", shape, affine) # small_25_tensor saves the tensor as a symmetric matrix following # the nifti standard. ten_shape = shape + (1, 6) assert_image_shape_affine("small_25_tensor.nii.gz", ten_shape, affine) @nt.dec.skipif(no_mpl) def test_qb_commandline(): with InTemporaryDirectory(): tracks_file = get_data('fornix') cmd = ["dipy_quickbundles", tracks_file, '--pkl_file', 'mypickle.pkl', '--out_file', 'tracks300.trk'] out = run_command(cmd) assert_equal(out[0], 0)
bsd-3-clause
trachelr/mne-python
mne/viz/_3d.py
10
33467
"""Functions to make 3D plots with M/EEG data """ from __future__ import print_function # Authors: Alexandre Gramfort <[email protected]> # Denis Engemann <[email protected]> # Martin Luessi <[email protected]> # Eric Larson <[email protected]> # Mainak Jas <[email protected]> # Mark Wronkiewicz <[email protected]> # # License: Simplified BSD from ..externals.six import string_types, advance_iterator import os.path as op import inspect import warnings from itertools import cycle import base64 import numpy as np from scipy import linalg from ..io.constants import FIFF from ..io.pick import pick_types from ..surface import (get_head_surf, get_meg_helmet_surf, read_surface, transform_surface_to) from ..transforms import (read_trans, _find_trans, apply_trans, combine_transforms, _get_mri_head_t) from ..utils import get_subjects_dir, logger, _check_subject from ..defaults import _handle_default from .utils import mne_analyze_colormap, _prepare_trellis, COLORS from ..externals.six import BytesIO def plot_evoked_field(evoked, surf_maps, time=None, time_label='t = %0.0f ms', n_jobs=1): """Plot MEG/EEG fields on head surface and helmet in 3D Parameters ---------- evoked : instance of mne.Evoked The evoked object. surf_maps : list The surface mapping information obtained with make_field_map. time : float | None The time point at which the field map shall be displayed. If None, the average peak latency (across sensor types) is used. time_label : str How to print info about the time instant visualized. n_jobs : int Number of jobs to run in parallel. Returns ------- fig : instance of mlab.Figure The mayavi figure. """ types = [t for t in ['eeg', 'grad', 'mag'] if t in evoked] time_idx = None if time is None: time = np.mean([evoked.get_peak(ch_type=t)[1] for t in types]) if not evoked.times[0] <= time <= evoked.times[-1]: raise ValueError('`time` (%0.3f) must be inside `evoked.times`' % time) time_idx = np.argmin(np.abs(evoked.times - time)) types = [sm['kind'] for sm in surf_maps] # Plot them from mayavi import mlab alphas = [1.0, 0.5] colors = [(0.6, 0.6, 0.6), (1.0, 1.0, 1.0)] colormap = mne_analyze_colormap(format='mayavi') colormap_lines = np.concatenate([np.tile([0., 0., 255., 255.], (127, 1)), np.tile([0., 0., 0., 255.], (2, 1)), np.tile([255., 0., 0., 255.], (127, 1))]) fig = mlab.figure(bgcolor=(0.0, 0.0, 0.0), size=(600, 600)) for ii, this_map in enumerate(surf_maps): surf = this_map['surf'] map_data = this_map['data'] map_type = this_map['kind'] map_ch_names = this_map['ch_names'] if map_type == 'eeg': pick = pick_types(evoked.info, meg=False, eeg=True) else: pick = pick_types(evoked.info, meg=True, eeg=False, ref_meg=False) ch_names = [evoked.ch_names[k] for k in pick] set_ch_names = set(ch_names) set_map_ch_names = set(map_ch_names) if set_ch_names != set_map_ch_names: message = ['Channels in map and data do not match.'] diff = set_map_ch_names - set_ch_names if len(diff): message += ['%s not in data file. ' % list(diff)] diff = set_ch_names - set_map_ch_names if len(diff): message += ['%s not in map file.' % list(diff)] raise RuntimeError(' '.join(message)) data = np.dot(map_data, evoked.data[pick, time_idx]) x, y, z = surf['rr'].T nn = surf['nn'] # make absolutely sure these are normalized for Mayavi nn = nn / np.sum(nn * nn, axis=1)[:, np.newaxis] # Make a solid surface vlim = np.max(np.abs(data)) alpha = alphas[ii] with warnings.catch_warnings(record=True): # traits mesh = mlab.pipeline.triangular_mesh_source(x, y, z, surf['tris']) mesh.data.point_data.normals = nn mesh.data.cell_data.normals = None mlab.pipeline.surface(mesh, color=colors[ii], opacity=alpha) # Now show our field pattern with warnings.catch_warnings(record=True): # traits mesh = mlab.pipeline.triangular_mesh_source(x, y, z, surf['tris'], scalars=data) mesh.data.point_data.normals = nn mesh.data.cell_data.normals = None with warnings.catch_warnings(record=True): # traits fsurf = mlab.pipeline.surface(mesh, vmin=-vlim, vmax=vlim) fsurf.module_manager.scalar_lut_manager.lut.table = colormap # And the field lines on top with warnings.catch_warnings(record=True): # traits mesh = mlab.pipeline.triangular_mesh_source(x, y, z, surf['tris'], scalars=data) mesh.data.point_data.normals = nn mesh.data.cell_data.normals = None with warnings.catch_warnings(record=True): # traits cont = mlab.pipeline.contour_surface(mesh, contours=21, line_width=1.0, vmin=-vlim, vmax=vlim, opacity=alpha) cont.module_manager.scalar_lut_manager.lut.table = colormap_lines if '%' in time_label: time_label %= (1e3 * evoked.times[time_idx]) mlab.text(0.01, 0.01, time_label, width=0.4) mlab.view(10, 60) return fig def _plot_mri_contours(mri_fname, surf_fnames, orientation='coronal', slices=None, show=True, img_output=False): """Plot BEM contours on anatomical slices. Parameters ---------- mri_fname : str The name of the file containing anatomical data. surf_fnames : list of str The filenames for the BEM surfaces in the format ['inner_skull.surf', 'outer_skull.surf', 'outer_skin.surf']. orientation : str 'coronal' or 'transverse' or 'sagittal' slices : list of int Slice indices. show : bool Call pyplot.show() at the end. img_output : None | tuple If tuple (width and height), images will be produced instead of a single figure with many axes. This mode is designed to reduce the (substantial) overhead associated with making tens to hundreds of matplotlib axes, instead opting to re-use a single Axes instance. Returns ------- fig : Instance of matplotlib.figure.Figure | list The figure. Will instead be a list of png images if img_output is a tuple. """ import matplotlib.pyplot as plt import nibabel as nib if orientation not in ['coronal', 'axial', 'sagittal']: raise ValueError("Orientation must be 'coronal', 'axial' or " "'sagittal'. Got %s." % orientation) # Load the T1 data nim = nib.load(mri_fname) data = nim.get_data() affine = nim.get_affine() n_sag, n_axi, n_cor = data.shape orientation_name2axis = dict(sagittal=0, axial=1, coronal=2) orientation_axis = orientation_name2axis[orientation] if slices is None: n_slices = data.shape[orientation_axis] slices = np.linspace(0, n_slices, 12, endpoint=False).astype(np.int) # create of list of surfaces surfs = list() trans = linalg.inv(affine) # XXX : next line is a hack don't ask why trans[:3, -1] = [n_sag // 2, n_axi // 2, n_cor // 2] for surf_fname in surf_fnames: surf = dict() surf['rr'], surf['tris'] = read_surface(surf_fname) # move back surface to MRI coordinate system surf['rr'] = nib.affines.apply_affine(trans, surf['rr']) surfs.append(surf) if img_output is None: fig, axs = _prepare_trellis(len(slices), 4) else: fig, ax = plt.subplots(1, 1, figsize=(7.0, 7.0)) axs = [ax] * len(slices) fig_size = fig.get_size_inches() w, h = img_output[0], img_output[1] w2 = fig_size[0] fig.set_size_inches([(w2 / float(w)) * w, (w2 / float(w)) * h]) plt.close(fig) inds = dict(coronal=[0, 1, 2], axial=[2, 0, 1], sagittal=[2, 1, 0])[orientation] outs = [] for ax, sl in zip(axs, slices): # adjust the orientations for good view if orientation == 'coronal': dat = data[:, :, sl].transpose() elif orientation == 'axial': dat = data[:, sl, :] elif orientation == 'sagittal': dat = data[sl, :, :] # First plot the anatomical data if img_output is not None: ax.clear() ax.imshow(dat, cmap=plt.cm.gray) ax.axis('off') # and then plot the contours on top for surf in surfs: ax.tricontour(surf['rr'][:, inds[0]], surf['rr'][:, inds[1]], surf['tris'], surf['rr'][:, inds[2]], levels=[sl], colors='yellow', linewidths=2.0) if img_output is not None: ax.set_xticks([]) ax.set_yticks([]) ax.set_xlim(0, img_output[1]) ax.set_ylim(img_output[0], 0) output = BytesIO() fig.savefig(output, bbox_inches='tight', pad_inches=0, format='png') outs.append(base64.b64encode(output.getvalue()).decode('ascii')) if show: plt.subplots_adjust(left=0., bottom=0., right=1., top=1., wspace=0., hspace=0.) plt.show() return fig if img_output is None else outs def plot_trans(info, trans='auto', subject=None, subjects_dir=None, ch_type=None, source=('bem', 'head'), coord_frame='head'): """Plot MEG/EEG head surface and helmet in 3D. Parameters ---------- info : dict The measurement info. trans : str | 'auto' The full path to the `*-trans.fif` file produced during coregistration. subject : str | None The subject name corresponding to FreeSurfer environment variable SUBJECT. subjects_dir : str The path to the freesurfer subjects reconstructions. It corresponds to Freesurfer environment variable SUBJECTS_DIR. ch_type : None | 'eeg' | 'meg' If None, both the MEG helmet and EEG electrodes will be shown. If 'meg', only the MEG helmet will be shown. If 'eeg', only the EEG electrodes will be shown. source : str Type to load. Common choices would be `'bem'` or `'head'`. We first try loading `'$SUBJECTS_DIR/$SUBJECT/bem/$SUBJECT-$SOURCE.fif'`, and then look for `'$SUBJECT*$SOURCE.fif'` in the same directory. Defaults to 'bem'. Note. For single layer bems it is recommended to use 'head'. coord_frame : str Coordinate frame to use. Returns ------- fig : instance of mlab.Figure The mayavi figure. """ if coord_frame not in ['head', 'meg']: raise ValueError('coord_frame must be "head" or "meg"') if ch_type not in [None, 'eeg', 'meg']: raise ValueError('Argument ch_type must be None | eeg | meg. Got %s.' % ch_type) if trans == 'auto': # let's try to do this in MRI coordinates so they're easy to plot trans = _find_trans(subject, subjects_dir) trans = read_trans(trans) surfs = [get_head_surf(subject, source=source, subjects_dir=subjects_dir)] if ch_type is None or ch_type == 'meg': surfs.append(get_meg_helmet_surf(info, trans)) if coord_frame == 'meg': meg_trans = combine_transforms(info['dev_head_t'], trans, FIFF.FIFFV_COORD_DEVICE, FIFF.FIFFV_COORD_MRI) surfs = [transform_surface_to(surf, 'meg', meg_trans) for surf in surfs] # Plot them from mayavi import mlab alphas = [1.0, 0.5] colors = [(0.6, 0.6, 0.6), (0.0, 0.0, 0.6)] fig = mlab.figure(bgcolor=(0.0, 0.0, 0.0), size=(600, 600)) for ii, surf in enumerate(surfs): x, y, z = surf['rr'].T nn = surf['nn'] # make absolutely sure these are normalized for Mayavi nn = nn / np.sum(nn * nn, axis=1)[:, np.newaxis] # Make a solid surface alpha = alphas[ii] with warnings.catch_warnings(record=True): # traits mesh = mlab.pipeline.triangular_mesh_source(x, y, z, surf['tris']) mesh.data.point_data.normals = nn mesh.data.cell_data.normals = None mlab.pipeline.surface(mesh, color=colors[ii], opacity=alpha) if ch_type is None or ch_type == 'eeg': eeg_locs = [l['eeg_loc'][:, 0] for l in info['chs'] if l['eeg_loc'] is not None] if len(eeg_locs) > 0: eeg_loc = np.array(eeg_locs) # Transform EEG electrodes to MRI coordinates eeg_loc = apply_trans(trans['trans'], eeg_loc) with warnings.catch_warnings(record=True): # traits mlab.points3d(eeg_loc[:, 0], eeg_loc[:, 1], eeg_loc[:, 2], color=(1.0, 0.0, 0.0), scale_factor=0.005) else: warnings.warn('EEG electrode locations not found. ' 'Cannot plot EEG electrodes.') mlab.view(90, 90) return fig def _limits_to_control_points(clim, stc_data, colormap): """Private helper function to convert limits (values or percentiles) to control points. Note: If using 'mne', generate cmap control points for a directly mirrored cmap for simplicity (i.e., no normalization is computed to account for a 2-tailed mne cmap). Parameters ---------- clim : str | dict Desired limits use to set cmap control points. Returns ------- ctrl_pts : list (length 3) Array of floats corresponding to values to use as cmap control points. colormap : str The colormap. """ # Based on type of limits specified, get cmap control points if colormap == 'auto': if clim == 'auto': colormap = 'mne' if (stc_data < 0).any() else 'hot' else: if 'lims' in clim: colormap = 'hot' else: # 'pos_lims' in clim colormap = 'mne' if clim == 'auto': # Set upper and lower bound based on percent, and get average between ctrl_pts = np.percentile(np.abs(stc_data), [96, 97.5, 99.95]) elif isinstance(clim, dict): # Get appropriate key for clim if it's a dict limit_key = ['lims', 'pos_lims'][colormap in ('mne', 'mne_analyze')] if colormap != 'auto' and limit_key not in clim.keys(): raise KeyError('"pos_lims" must be used with "mne" colormap') clim['kind'] = clim.get('kind', 'percent') if clim['kind'] == 'percent': ctrl_pts = np.percentile(np.abs(stc_data), list(np.abs(clim[limit_key]))) elif clim['kind'] == 'value': ctrl_pts = np.array(clim[limit_key]) if (np.diff(ctrl_pts) < 0).any(): raise ValueError('value colormap limits must be strictly ' 'nondecreasing') else: raise ValueError('If clim is a dict, clim[kind] must be ' ' "value" or "percent"') else: raise ValueError('"clim" must be "auto" or dict') if len(ctrl_pts) != 3: raise ValueError('"lims" or "pos_lims" is length %i. It must be length' ' 3' % len(ctrl_pts)) ctrl_pts = np.array(ctrl_pts, float) if len(set(ctrl_pts)) != 3: if len(set(ctrl_pts)) == 1: # three points match if ctrl_pts[0] == 0: # all are zero warnings.warn('All data were zero') ctrl_pts = np.arange(3, dtype=float) else: ctrl_pts *= [0., 0.5, 1] # all nonzero pts == max else: # two points match # if points one and two are identical, add a tiny bit to the # control point two; if points two and three are identical, # subtract a tiny bit from point two. bump = 1e-5 if ctrl_pts[0] == ctrl_pts[1] else -1e-5 ctrl_pts[1] = ctrl_pts[0] + bump * (ctrl_pts[2] - ctrl_pts[0]) return ctrl_pts, colormap def plot_source_estimates(stc, subject=None, surface='inflated', hemi='lh', colormap='auto', time_label='time=%0.2f ms', smoothing_steps=10, transparent=None, alpha=1.0, time_viewer=False, config_opts=None, subjects_dir=None, figure=None, views='lat', colorbar=True, clim='auto'): """Plot SourceEstimates with PySurfer Note: PySurfer currently needs the SUBJECTS_DIR environment variable, which will automatically be set by this function. Plotting multiple SourceEstimates with different values for subjects_dir will cause PySurfer to use the wrong FreeSurfer surfaces when using methods of the returned Brain object. It is therefore recommended to set the SUBJECTS_DIR environment variable or always use the same value for subjects_dir (within the same Python session). Parameters ---------- stc : SourceEstimates The source estimates to plot. subject : str | None The subject name corresponding to FreeSurfer environment variable SUBJECT. If None stc.subject will be used. If that is None, the environment will be used. surface : str The type of surface (inflated, white etc.). hemi : str, 'lh' | 'rh' | 'split' | 'both' The hemisphere to display. colormap : str | np.ndarray of float, shape(n_colors, 3 | 4) Name of colormap to use or a custom look up table. If array, must be (n x 3) or (n x 4) array for with RGB or RGBA values between 0 and 255. If 'auto', either 'hot' or 'mne' will be chosen based on whether 'lims' or 'pos_lims' are specified in `clim`. time_label : str How to print info about the time instant visualized. smoothing_steps : int The amount of smoothing transparent : bool | None If True, use a linear transparency between fmin and fmid. None will choose automatically based on colormap type. alpha : float Alpha value to apply globally to the overlay. time_viewer : bool Display time viewer GUI. config_opts : dict Keyword arguments for Brain initialization. See pysurfer.viz.Brain. subjects_dir : str The path to the freesurfer subjects reconstructions. It corresponds to Freesurfer environment variable SUBJECTS_DIR. figure : instance of mayavi.core.scene.Scene | list | int | None If None, a new figure will be created. If multiple views or a split view is requested, this must be a list of the appropriate length. If int is provided it will be used to identify the Mayavi figure by it's id or create a new figure with the given id. views : str | list View to use. See surfer.Brain(). colorbar : bool If True, display colorbar on scene. clim : str | dict Colorbar properties specification. If 'auto', set clim automatically based on data percentiles. If dict, should contain: ``kind`` : str Flag to specify type of limits. 'value' or 'percent'. ``lims`` : list | np.ndarray | tuple of float, 3 elements Note: Only use this if 'colormap' is not 'mne'. Left, middle, and right bound for colormap. ``pos_lims`` : list | np.ndarray | tuple of float, 3 elements Note: Only use this if 'colormap' is 'mne'. Left, middle, and right bound for colormap. Positive values will be mirrored directly across zero during colormap construction to obtain negative control points. Returns ------- brain : Brain A instance of surfer.viz.Brain from PySurfer. """ from surfer import Brain, TimeViewer config_opts = _handle_default('config_opts', config_opts) import mayavi from mayavi import mlab # import here to avoid circular import problem from ..source_estimate import SourceEstimate if not isinstance(stc, SourceEstimate): raise ValueError('stc has to be a surface source estimate') if hemi not in ['lh', 'rh', 'split', 'both']: raise ValueError('hemi has to be either "lh", "rh", "split", ' 'or "both"') n_split = 2 if hemi == 'split' else 1 n_views = 1 if isinstance(views, string_types) else len(views) if figure is not None: # use figure with specified id or create new figure if isinstance(figure, int): figure = mlab.figure(figure, size=(600, 600)) # make sure it is of the correct type if not isinstance(figure, list): figure = [figure] if not all(isinstance(f, mayavi.core.scene.Scene) for f in figure): raise TypeError('figure must be a mayavi scene or list of scenes') # make sure we have the right number of figures n_fig = len(figure) if not n_fig == n_split * n_views: raise RuntimeError('`figure` must be a list with the same ' 'number of elements as PySurfer plots that ' 'will be created (%s)' % n_split * n_views) # convert control points to locations in colormap ctrl_pts, colormap = _limits_to_control_points(clim, stc.data, colormap) # Construct cmap manually if 'mne' and get cmap bounds # and triage transparent argument if colormap in ('mne', 'mne_analyze'): colormap = mne_analyze_colormap(ctrl_pts) scale_pts = [-1 * ctrl_pts[-1], 0, ctrl_pts[-1]] transparent = False if transparent is None else transparent else: scale_pts = ctrl_pts transparent = True if transparent is None else transparent subjects_dir = get_subjects_dir(subjects_dir=subjects_dir, raise_error=True) subject = _check_subject(stc.subject, subject, True) if hemi in ['both', 'split']: hemis = ['lh', 'rh'] else: hemis = [hemi] title = subject if len(hemis) > 1 else '%s - %s' % (subject, hemis[0]) args = inspect.getargspec(Brain.__init__)[0] kwargs = dict(title=title, figure=figure, config_opts=config_opts, subjects_dir=subjects_dir) if 'views' in args: kwargs['views'] = views with warnings.catch_warnings(record=True): # traits warnings brain = Brain(subject, hemi, surface, **kwargs) for hemi in hemis: hemi_idx = 0 if hemi == 'lh' else 1 if hemi_idx == 0: data = stc.data[:len(stc.vertices[0])] else: data = stc.data[len(stc.vertices[0]):] vertices = stc.vertices[hemi_idx] time = 1e3 * stc.times with warnings.catch_warnings(record=True): # traits warnings brain.add_data(data, colormap=colormap, vertices=vertices, smoothing_steps=smoothing_steps, time=time, time_label=time_label, alpha=alpha, hemi=hemi, colorbar=colorbar) # scale colormap and set time (index) to display brain.scale_data_colormap(fmin=scale_pts[0], fmid=scale_pts[1], fmax=scale_pts[2], transparent=transparent) if time_viewer: TimeViewer(brain) return brain def plot_sparse_source_estimates(src, stcs, colors=None, linewidth=2, fontsize=18, bgcolor=(.05, 0, .1), opacity=0.2, brain_color=(0.7,) * 3, show=True, high_resolution=False, fig_name=None, fig_number=None, labels=None, modes=('cone', 'sphere'), scale_factors=(1, 0.6), verbose=None, **kwargs): """Plot source estimates obtained with sparse solver Active dipoles are represented in a "Glass" brain. If the same source is active in multiple source estimates it is displayed with a sphere otherwise with a cone in 3D. Parameters ---------- src : dict The source space. stcs : instance of SourceEstimate or list of instances of SourceEstimate The source estimates (up to 3). colors : list List of colors linewidth : int Line width in 2D plot. fontsize : int Font size. bgcolor : tuple of length 3 Background color in 3D. opacity : float in [0, 1] Opacity of brain mesh. brain_color : tuple of length 3 Brain color. show : bool Show figures if True. high_resolution : bool If True, plot on the original (non-downsampled) cortical mesh. fig_name : Mayavi figure name. fig_number : Matplotlib figure number. labels : ndarray or list of ndarrays Labels to show sources in clusters. Sources with the same label and the waveforms within each cluster are presented in the same color. labels should be a list of ndarrays when stcs is a list ie. one label for each stc. modes : list Should be a list, with each entry being ``'cone'`` or ``'sphere'`` to specify how the dipoles should be shown. scale_factors : list List of floating point scale factors for the markers. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). **kwargs : kwargs Keyword arguments to pass to mlab.triangular_mesh. """ known_modes = ['cone', 'sphere'] if not isinstance(modes, (list, tuple)) or \ not all(mode in known_modes for mode in modes): raise ValueError('mode must be a list containing only ' '"cone" or "sphere"') if not isinstance(stcs, list): stcs = [stcs] if labels is not None and not isinstance(labels, list): labels = [labels] if colors is None: colors = COLORS linestyles = ['-', '--', ':'] # Show 3D lh_points = src[0]['rr'] rh_points = src[1]['rr'] points = np.r_[lh_points, rh_points] lh_normals = src[0]['nn'] rh_normals = src[1]['nn'] normals = np.r_[lh_normals, rh_normals] if high_resolution: use_lh_faces = src[0]['tris'] use_rh_faces = src[1]['tris'] else: use_lh_faces = src[0]['use_tris'] use_rh_faces = src[1]['use_tris'] use_faces = np.r_[use_lh_faces, lh_points.shape[0] + use_rh_faces] points *= 170 vertnos = [np.r_[stc.lh_vertno, lh_points.shape[0] + stc.rh_vertno] for stc in stcs] unique_vertnos = np.unique(np.concatenate(vertnos).ravel()) from mayavi import mlab from matplotlib.colors import ColorConverter color_converter = ColorConverter() f = mlab.figure(figure=fig_name, bgcolor=bgcolor, size=(600, 600)) mlab.clf() if mlab.options.backend != 'test': f.scene.disable_render = True with warnings.catch_warnings(record=True): # traits warnings surface = mlab.triangular_mesh(points[:, 0], points[:, 1], points[:, 2], use_faces, color=brain_color, opacity=opacity, **kwargs) import matplotlib.pyplot as plt # Show time courses plt.figure(fig_number) plt.clf() colors = cycle(colors) logger.info("Total number of active sources: %d" % len(unique_vertnos)) if labels is not None: colors = [advance_iterator(colors) for _ in range(np.unique(np.concatenate(labels).ravel()).size)] for idx, v in enumerate(unique_vertnos): # get indices of stcs it belongs to ind = [k for k, vertno in enumerate(vertnos) if v in vertno] is_common = len(ind) > 1 if labels is None: c = advance_iterator(colors) else: # if vertex is in different stcs than take label from first one c = colors[labels[ind[0]][vertnos[ind[0]] == v]] mode = modes[1] if is_common else modes[0] scale_factor = scale_factors[1] if is_common else scale_factors[0] if (isinstance(scale_factor, (np.ndarray, list, tuple)) and len(unique_vertnos) == len(scale_factor)): scale_factor = scale_factor[idx] x, y, z = points[v] nx, ny, nz = normals[v] with warnings.catch_warnings(record=True): # traits mlab.quiver3d(x, y, z, nx, ny, nz, color=color_converter.to_rgb(c), mode=mode, scale_factor=scale_factor) for k in ind: vertno = vertnos[k] mask = (vertno == v) assert np.sum(mask) == 1 linestyle = linestyles[k] plt.plot(1e3 * stc.times, 1e9 * stcs[k].data[mask].ravel(), c=c, linewidth=linewidth, linestyle=linestyle) plt.xlabel('Time (ms)', fontsize=18) plt.ylabel('Source amplitude (nAm)', fontsize=18) if fig_name is not None: plt.title(fig_name) if show: plt.show() surface.actor.property.backface_culling = True surface.actor.property.shading = True return surface def plot_dipole_locations(dipoles, trans, subject, subjects_dir=None, bgcolor=(1, 1, 1), opacity=0.3, brain_color=(0.7, 0.7, 0.7), mesh_color=(1, 1, 0), fig_name=None, fig_size=(600, 600), mode='cone', scale_factor=0.1e-1, colors=None, verbose=None): """Plot dipole locations Only the location of the first time point of each dipole is shown. Parameters ---------- dipoles : list of instances of Dipole | Dipole The dipoles to plot. trans : dict The mri to head trans. subject : str The subject name corresponding to FreeSurfer environment variable SUBJECT. subjects_dir : None | str The path to the freesurfer subjects reconstructions. It corresponds to Freesurfer environment variable SUBJECTS_DIR. The default is None. bgcolor : tuple of length 3 Background color in 3D. opacity : float in [0, 1] Opacity of brain mesh. brain_color : tuple of length 3 Brain color. mesh_color : tuple of length 3 Mesh color. fig_name : str Mayavi figure name. fig_size : tuple of length 2 Mayavi figure size. mode : str Should be ``'cone'`` or ``'sphere'`` to specify how the dipoles should be shown. scale_factor : float The scaling applied to amplitudes for the plot. colors: list of colors | None Color to plot with each dipole. If None default colors are used. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). Returns ------- fig : instance of mlab.Figure The mayavi figure. Notes ----- .. versionadded:: 0.9.0 """ from mayavi import mlab from matplotlib.colors import ColorConverter color_converter = ColorConverter() trans = _get_mri_head_t(trans)[0] subjects_dir = get_subjects_dir(subjects_dir=subjects_dir, raise_error=True) fname = op.join(subjects_dir, subject, 'bem', 'inner_skull.surf') points, faces = read_surface(fname) points = apply_trans(trans['trans'], points * 1e-3) from .. import Dipole if isinstance(dipoles, Dipole): dipoles = [dipoles] if mode not in ['cone', 'sphere']: raise ValueError('mode must be in "cone" or "sphere"') if colors is None: colors = cycle(COLORS) fig = mlab.figure(size=fig_size, bgcolor=bgcolor, fgcolor=(0, 0, 0)) with warnings.catch_warnings(record=True): # FutureWarning in traits mlab.triangular_mesh(points[:, 0], points[:, 1], points[:, 2], faces, color=mesh_color, opacity=opacity) for dip, color in zip(dipoles, colors): rgb_color = color_converter.to_rgb(color) with warnings.catch_warnings(record=True): # FutureWarning in traits mlab.quiver3d(dip.pos[0, 0], dip.pos[0, 1], dip.pos[0, 2], dip.ori[0, 0], dip.ori[0, 1], dip.ori[0, 2], opacity=1., mode=mode, color=rgb_color, scalars=dip.amplitude.max(), scale_factor=scale_factor) if fig_name is not None: mlab.title(fig_name) if fig.scene is not None: # safe for Travis fig.scene.x_plus_view() return fig
bsd-3-clause
nrhine1/scikit-learn
benchmarks/bench_glmnet.py
297
3848
""" To run this, you'll need to have installed. * glmnet-python * scikit-learn (of course) Does two benchmarks First, we fix a training set and increase the number of samples. Then we plot the computation time as function of the number of samples. In the second benchmark, we increase the number of dimensions of the training set. Then we plot the computation time as function of the number of dimensions. In both cases, only 10% of the features are informative. """ import numpy as np import gc from time import time from sklearn.datasets.samples_generator import make_regression alpha = 0.1 # alpha = 0.01 def rmse(a, b): return np.sqrt(np.mean((a - b) ** 2)) def bench(factory, X, Y, X_test, Y_test, ref_coef): gc.collect() # start time tstart = time() clf = factory(alpha=alpha).fit(X, Y) delta = (time() - tstart) # stop time print("duration: %0.3fs" % delta) print("rmse: %f" % rmse(Y_test, clf.predict(X_test))) print("mean coef abs diff: %f" % abs(ref_coef - clf.coef_.ravel()).mean()) return delta if __name__ == '__main__': from glmnet.elastic_net import Lasso as GlmnetLasso from sklearn.linear_model import Lasso as ScikitLasso # Delayed import of pylab import pylab as pl scikit_results = [] glmnet_results = [] n = 20 step = 500 n_features = 1000 n_informative = n_features / 10 n_test_samples = 1000 for i in range(1, n + 1): print('==================') print('Iteration %s of %s' % (i, n)) print('==================') X, Y, coef_ = make_regression( n_samples=(i * step) + n_test_samples, n_features=n_features, noise=0.1, n_informative=n_informative, coef=True) X_test = X[-n_test_samples:] Y_test = Y[-n_test_samples:] X = X[:(i * step)] Y = Y[:(i * step)] print("benchmarking scikit-learn: ") scikit_results.append(bench(ScikitLasso, X, Y, X_test, Y_test, coef_)) print("benchmarking glmnet: ") glmnet_results.append(bench(GlmnetLasso, X, Y, X_test, Y_test, coef_)) pl.clf() xx = range(0, n * step, step) pl.title('Lasso regression on sample dataset (%d features)' % n_features) pl.plot(xx, scikit_results, 'b-', label='scikit-learn') pl.plot(xx, glmnet_results, 'r-', label='glmnet') pl.legend() pl.xlabel('number of samples to classify') pl.ylabel('Time (s)') pl.show() # now do a benchmark where the number of points is fixed # and the variable is the number of features scikit_results = [] glmnet_results = [] n = 20 step = 100 n_samples = 500 for i in range(1, n + 1): print('==================') print('Iteration %02d of %02d' % (i, n)) print('==================') n_features = i * step n_informative = n_features / 10 X, Y, coef_ = make_regression( n_samples=(i * step) + n_test_samples, n_features=n_features, noise=0.1, n_informative=n_informative, coef=True) X_test = X[-n_test_samples:] Y_test = Y[-n_test_samples:] X = X[:n_samples] Y = Y[:n_samples] print("benchmarking scikit-learn: ") scikit_results.append(bench(ScikitLasso, X, Y, X_test, Y_test, coef_)) print("benchmarking glmnet: ") glmnet_results.append(bench(GlmnetLasso, X, Y, X_test, Y_test, coef_)) xx = np.arange(100, 100 + n * step, step) pl.figure('scikit-learn vs. glmnet benchmark results') pl.title('Regression in high dimensional spaces (%d samples)' % n_samples) pl.plot(xx, scikit_results, 'b-', label='scikit-learn') pl.plot(xx, glmnet_results, 'r-', label='glmnet') pl.legend() pl.xlabel('number of features') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
petewarden/tensorflow
tensorflow/python/kernel_tests/constant_op_eager_test.py
6
22281
# Copyright 2015 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for ConstantOp.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from tensorflow.python.eager import context from tensorflow.python.eager import test from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes as dtypes_lib from tensorflow.python.framework import errors_impl from tensorflow.python.framework import ops from tensorflow.python.framework import test_util from tensorflow.python.ops import array_ops from tensorflow.python.util import compat # TODO(josh11b): add tests with lists/tuples, Shape. # TODO(ashankar): Collapse with tests in constant_op_test.py and use something # like the test_util.run_in_graph_and_eager_modes decorator to confirm # equivalence between graph and eager execution. class ConstantTest(test.TestCase): def _testCpu(self, x): np_ans = np.array(x) with context.device("/device:CPU:0"): tf_ans = ops.convert_to_tensor(x).numpy() if np_ans.dtype in [np.float32, np.float64, np.complex64, np.complex128]: self.assertAllClose(np_ans, tf_ans) else: self.assertAllEqual(np_ans, tf_ans) def _testGpu(self, x): device = test_util.gpu_device_name() if device: np_ans = np.array(x) with context.device(device): tf_ans = ops.convert_to_tensor(x).numpy() if np_ans.dtype in [np.float32, np.float64, np.complex64, np.complex128]: self.assertAllClose(np_ans, tf_ans) else: self.assertAllEqual(np_ans, tf_ans) def _testAll(self, x): self._testCpu(x) self._testGpu(x) def testFloat(self): self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32)) self._testAll( np.random.normal(size=30).reshape([2, 3, 5]).astype(np.float32)) self._testAll(np.empty((2, 0, 5)).astype(np.float32)) orig = [-1.0, 2.0, 0.0] tf_ans = constant_op.constant(orig) self.assertEqual(dtypes_lib.float32, tf_ans.dtype) self.assertAllClose(np.array(orig), tf_ans.numpy()) # Mix floats and ints orig = [-1.5, 2, 0] tf_ans = constant_op.constant(orig) self.assertEqual(dtypes_lib.float32, tf_ans.dtype) self.assertAllClose(np.array(orig), tf_ans.numpy()) orig = [-5, 2.5, 0] tf_ans = constant_op.constant(orig) self.assertEqual(dtypes_lib.float32, tf_ans.dtype) self.assertAllClose(np.array(orig), tf_ans.numpy()) # Mix floats and ints that don't fit in int32 orig = [1, 2**42, 0.5] tf_ans = constant_op.constant(orig) self.assertEqual(dtypes_lib.float32, tf_ans.dtype) self.assertAllClose(np.array(orig), tf_ans.numpy()) def testDouble(self): self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float64)) self._testAll( np.random.normal(size=30).reshape([2, 3, 5]).astype(np.float64)) self._testAll(np.empty((2, 0, 5)).astype(np.float64)) orig = [-5, 2.5, 0] tf_ans = constant_op.constant(orig, dtypes_lib.float64) self.assertEqual(dtypes_lib.float64, tf_ans.dtype) self.assertAllClose(np.array(orig), tf_ans.numpy()) # This integer is not exactly representable as a double, gets rounded. tf_ans = constant_op.constant(2**54 + 1, dtypes_lib.float64) self.assertEqual(2**54, tf_ans.numpy()) # This integer is larger than all non-infinite numbers representable # by a double, raises an exception. with self.assertRaisesRegex(ValueError, "out-of-range integer"): constant_op.constant(10**310, dtypes_lib.float64) def testInt32(self): self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.int32)) self._testAll( (100 * np.random.normal(size=30)).reshape([2, 3, 5]).astype(np.int32)) self._testAll(np.empty((2, 0, 5)).astype(np.int32)) self._testAll([-1, 2]) def testInt64(self): self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.int64)) self._testAll( (100 * np.random.normal(size=30)).reshape([2, 3, 5]).astype(np.int64)) self._testAll(np.empty((2, 0, 5)).astype(np.int64)) # Should detect out of range for int32 and use int64 instead. orig = [2, 2**48, -2**48] tf_ans = constant_op.constant(orig) self.assertEqual(dtypes_lib.int64, tf_ans.dtype) self.assertAllClose(np.array(orig), tf_ans.numpy()) # Out of range for an int64 with self.assertRaisesRegex(ValueError, "out-of-range integer"): constant_op.constant([2**72]) def testComplex64(self): self._testAll( np.complex(1, 2) * np.arange(-15, 15).reshape([2, 3, 5]).astype(np.complex64)) self._testAll( np.complex(1, 2) * np.random.normal(size=30).reshape([2, 3, 5]).astype(np.complex64)) self._testAll(np.empty((2, 0, 5)).astype(np.complex64)) def testComplex128(self): self._testAll( np.complex(1, 2) * np.arange(-15, 15).reshape([2, 3, 5 ]).astype(np.complex128)) self._testAll( np.complex(1, 2) * np.random.normal(size=30).reshape( [2, 3, 5]).astype(np.complex128)) self._testAll(np.empty((2, 0, 5)).astype(np.complex128)) @test_util.disable_tfrt("support creating string tensors from empty " "numpy arrays.") def testString(self): val = [compat.as_bytes(str(x)) for x in np.arange(-15, 15)] self._testCpu(np.array(val).reshape([2, 3, 5])) self._testCpu(np.empty((2, 0, 5)).astype(np.str_)) def testStringWithNulls(self): val = ops.convert_to_tensor(b"\0\0\0\0").numpy() self.assertEqual(len(val), 4) self.assertEqual(val, b"\0\0\0\0") val = ops.convert_to_tensor(b"xx\0xx").numpy() self.assertEqual(len(val), 5) self.assertAllEqual(val, b"xx\0xx") nested = [[b"\0\0\0\0", b"xx\0xx"], [b"\0_\0_\0_\0", b"\0"]] val = ops.convert_to_tensor(nested).numpy() # NOTE(mrry): Do not use assertAllEqual, because it converts nested to a # numpy array, which loses the null terminators. self.assertEqual(val.tolist(), nested) def testStringConstantOp(self): s = constant_op.constant("uiuc") self.assertEqual(s.numpy().decode("utf-8"), "uiuc") s_array = constant_op.constant(["mit", "stanford"]) self.assertAllEqual(s_array.numpy(), ["mit", "stanford"]) with ops.device("/cpu:0"): s = constant_op.constant("cmu") self.assertEqual(s.numpy().decode("utf-8"), "cmu") s_array = constant_op.constant(["berkeley", "ucla"]) self.assertAllEqual(s_array.numpy(), ["berkeley", "ucla"]) def testExplicitShapeNumPy(self): c = constant_op.constant( np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32), shape=[2, 3, 5]) self.assertEqual(c.get_shape(), [2, 3, 5]) def testImplicitShapeNumPy(self): c = constant_op.constant( np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32)) self.assertEqual(c.get_shape(), [2, 3, 5]) def testExplicitShapeList(self): c = constant_op.constant([1, 2, 3, 4, 5, 6, 7], shape=[7]) self.assertEqual(c.get_shape(), [7]) def testExplicitShapeFill(self): c = constant_op.constant(12, shape=[7]) self.assertEqual(c.get_shape(), [7]) self.assertAllEqual([12, 12, 12, 12, 12, 12, 12], c.numpy()) def testExplicitShapeReshape(self): c = constant_op.constant( np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32), shape=[5, 2, 3]) self.assertEqual(c.get_shape(), [5, 2, 3]) def testImplicitShapeList(self): c = constant_op.constant([1, 2, 3, 4, 5, 6, 7]) self.assertEqual(c.get_shape(), [7]) def testExplicitShapeNumber(self): c = constant_op.constant(1, shape=[1]) self.assertEqual(c.get_shape(), [1]) def testImplicitShapeNumber(self): c = constant_op.constant(1) self.assertEqual(c.get_shape(), []) def testShapeTooBig(self): with self.assertRaises(TypeError): constant_op.constant([1, 2, 3, 4, 5, 6, 7], shape=[10]) def testShapeTooSmall(self): with self.assertRaises(TypeError): constant_op.constant([1, 2, 3, 4, 5, 6, 7], shape=[5]) def testShapeWrong(self): with self.assertRaisesRegex(TypeError, None): constant_op.constant([1, 2, 3, 4, 5, 6, 7], shape=[5]) def testShape(self): self._testAll(constant_op.constant([1]).get_shape()) def testDimension(self): x = constant_op.constant([1]).shape[0] self._testAll(x) def testDimensionList(self): x = [constant_op.constant([1]).shape[0]] self._testAll(x) # Mixing with regular integers is fine too self._testAll([1] + x) self._testAll(x + [1]) def testDimensionTuple(self): x = constant_op.constant([1]).shape[0] self._testAll((x,)) self._testAll((1, x)) self._testAll((x, 1)) def testInvalidLength(self): class BadList(list): def __init__(self): super(BadList, self).__init__([1, 2, 3]) # pylint: disable=invalid-length-returned def __len__(self): return -1 with self.assertRaisesRegex(ValueError, "should return >= 0"): constant_op.constant([BadList()]) with self.assertRaisesRegex(ValueError, "mixed types"): constant_op.constant([1, 2, BadList()]) with self.assertRaisesRegex(ValueError, "should return >= 0"): constant_op.constant(BadList()) with self.assertRaisesRegex(ValueError, "should return >= 0"): constant_op.constant([[BadList(), 2], 3]) with self.assertRaisesRegex(ValueError, "should return >= 0"): constant_op.constant([BadList(), [1, 2, 3]]) with self.assertRaisesRegex(ValueError, "should return >= 0"): constant_op.constant([BadList(), []]) # TODO(allenl, josh11b): These cases should return exceptions rather than # working (currently shape checking only checks the first element of each # sequence recursively). Maybe the first one is fine, but the second one # silently truncating is rather bad. # with self.assertRaisesRegex(ValueError, "should return >= 0"): # constant_op.constant([[3, 2, 1], BadList()]) # with self.assertRaisesRegex(ValueError, "should return >= 0"): # constant_op.constant([[], BadList()]) def testSparseValuesRaiseErrors(self): with self.assertRaisesRegex(ValueError, "non-rectangular Python sequence"): constant_op.constant([[1, 2], [3]], dtype=dtypes_lib.int32) with self.assertRaisesRegex(ValueError, None): constant_op.constant([[1, 2], [3]]) with self.assertRaisesRegex(ValueError, None): constant_op.constant([[1, 2], [3], [4, 5]]) # TODO(ashankar): This test fails with graph construction since # tensor_util.make_tensor_proto (invoked from constant_op.constant) # does not handle iterables (it relies on numpy conversion). # For consistency, should graph construction handle Python objects # that implement the sequence protocol (but not numpy conversion), # or should eager execution fail on such sequences? def testCustomSequence(self): # This is inspired by how many objects in pandas are implemented: # - They implement the Python sequence protocol # - But may raise a KeyError on __getitem__(self, 0) # See https://github.com/tensorflow/tensorflow/issues/20347 class MySeq(object): def __getitem__(self, key): if key != 1 and key != 3: raise KeyError(key) return key def __len__(self): return 2 def __iter__(self): l = list([1, 3]) return l.__iter__() self.assertAllEqual([1, 3], self.evaluate(constant_op.constant(MySeq()))) class AsTensorTest(test.TestCase): def testAsTensorForTensorInput(self): t = constant_op.constant(10.0) x = ops.convert_to_tensor(t) self.assertIs(t, x) def testAsTensorForNonTensorInput(self): x = ops.convert_to_tensor(10.0) self.assertTrue(isinstance(x, ops.EagerTensor)) class ZerosTest(test.TestCase): def _Zeros(self, shape): ret = array_ops.zeros(shape) self.assertEqual(shape, ret.get_shape()) return ret.numpy() def testConst(self): self.assertTrue( np.array_equal(self._Zeros([2, 3]), np.array([[0] * 3] * 2))) def testScalar(self): self.assertEqual(0, self._Zeros([])) self.assertEqual(0, self._Zeros(())) scalar = array_ops.zeros(constant_op.constant([], dtype=dtypes_lib.int32)) self.assertEqual(0, scalar.numpy()) def testDynamicSizes(self): np_ans = np.array([[0] * 3] * 2) # Creates a tensor of 2 x 3. d = array_ops.fill([2, 3], 12., name="fill") # Constructs a tensor of zeros of the same dimensions as "d". z = array_ops.zeros(array_ops.shape(d)) out = z.numpy() self.assertAllEqual(np_ans, out) self.assertShapeEqual(np_ans, d) self.assertShapeEqual(np_ans, z) def testDtype(self): d = array_ops.fill([2, 3], 12., name="fill") self.assertEqual(d.get_shape(), [2, 3]) # Test default type for both constant size and dynamic size z = array_ops.zeros([2, 3]) self.assertEqual(z.dtype, dtypes_lib.float32) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.numpy(), np.zeros([2, 3])) z = array_ops.zeros(array_ops.shape(d)) self.assertEqual(z.dtype, dtypes_lib.float32) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.numpy(), np.zeros([2, 3])) # Test explicit type control for dtype in [ dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.uint8, dtypes_lib.int16, dtypes_lib.int8, dtypes_lib.complex64, dtypes_lib.complex128, dtypes_lib.int64, dtypes_lib.bool, # TODO(josh11b): Support string type here. # dtypes_lib.string ]: z = array_ops.zeros([2, 3], dtype=dtype) self.assertEqual(z.dtype, dtype) self.assertEqual([2, 3], z.get_shape()) z_value = z.numpy() self.assertFalse(np.any(z_value)) self.assertEqual((2, 3), z_value.shape) z = array_ops.zeros(array_ops.shape(d), dtype=dtype) self.assertEqual(z.dtype, dtype) self.assertEqual([2, 3], z.get_shape()) z_value = z.numpy() self.assertFalse(np.any(z_value)) self.assertEqual((2, 3), z_value.shape) class ZerosLikeTest(test.TestCase): def _compareZeros(self, dtype, use_gpu): # Creates a tensor of non-zero values with shape 2 x 3. # NOTE(kearnes): The default numpy dtype associated with tf.string is # np.object (and can't be changed without breaking a lot things), which # causes a TypeError in constant_op.constant below. Here we catch the # special case of tf.string and set the numpy dtype appropriately. if dtype == dtypes_lib.string: numpy_dtype = np.string_ else: numpy_dtype = dtype.as_numpy_dtype d = constant_op.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype) # Constructs a tensor of zeros of the same dimensions and type as "d". z_var = array_ops.zeros_like(d) # Test that the type is correct self.assertEqual(z_var.dtype, dtype) # Test that the shape is correct self.assertEqual([2, 3], z_var.get_shape()) # Test that the value is correct z_value = z_var.numpy() self.assertFalse(np.any(z_value)) self.assertEqual((2, 3), z_value.shape) @test_util.disable_tfrt("b/169112823: unsupported dtype for Op:ZerosLike.") def testZerosLikeCPU(self): for dtype in [ dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.uint8, dtypes_lib.int16, dtypes_lib.int8, dtypes_lib.complex64, dtypes_lib.complex128, dtypes_lib.int64, # TODO(josh11b): Support string type here. # dtypes_lib.string ]: self._compareZeros(dtype, use_gpu=False) @test_util.disable_tfrt("b/169112823: unsupported dtype for Op:ZerosLike.") def testZerosLikeGPU(self): for dtype in [ dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.bool, dtypes_lib.int64, # TODO(josh11b): Support string type here. # dtypes_lib.string ]: self._compareZeros(dtype, use_gpu=True) @test_util.disable_tfrt("b/169112823: unsupported dtype for Op:ZerosLike.") def testZerosLikeDtype(self): # Make sure zeros_like works even for dtypes that cannot be cast between shape = (3, 5) dtypes = np.float32, np.complex64 for in_type in dtypes: x = np.arange(15).astype(in_type).reshape(*shape) for out_type in dtypes: y = array_ops.zeros_like(x, dtype=out_type).numpy() self.assertEqual(y.dtype, out_type) self.assertEqual(y.shape, shape) self.assertAllEqual(y, np.zeros(shape, dtype=out_type)) class OnesTest(test.TestCase): def _Ones(self, shape): ret = array_ops.ones(shape) self.assertEqual(shape, ret.get_shape()) return ret.numpy() def testConst(self): self.assertTrue(np.array_equal(self._Ones([2, 3]), np.array([[1] * 3] * 2))) def testScalar(self): self.assertEqual(1, self._Ones([])) self.assertEqual(1, self._Ones(())) scalar = array_ops.ones(constant_op.constant([], dtype=dtypes_lib.int32)) self.assertEqual(1, scalar.numpy()) def testDynamicSizes(self): np_ans = np.array([[1] * 3] * 2) # Creates a tensor of 2 x 3. d = array_ops.fill([2, 3], 12., name="fill") # Constructs a tensor of ones of the same dimensions as "d". z = array_ops.ones(array_ops.shape(d)) out = z.numpy() self.assertAllEqual(np_ans, out) self.assertShapeEqual(np_ans, d) self.assertShapeEqual(np_ans, z) def testDtype(self): d = array_ops.fill([2, 3], 12., name="fill") self.assertEqual(d.get_shape(), [2, 3]) # Test default type for both constant size and dynamic size z = array_ops.ones([2, 3]) self.assertEqual(z.dtype, dtypes_lib.float32) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.numpy(), np.ones([2, 3])) z = array_ops.ones(array_ops.shape(d)) self.assertEqual(z.dtype, dtypes_lib.float32) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.numpy(), np.ones([2, 3])) # Test explicit type control for dtype in (dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.uint8, dtypes_lib.int16, dtypes_lib.int8, dtypes_lib.complex64, dtypes_lib.complex128, dtypes_lib.int64, dtypes_lib.bool): z = array_ops.ones([2, 3], dtype=dtype) self.assertEqual(z.dtype, dtype) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.numpy(), np.ones([2, 3])) z = array_ops.ones(array_ops.shape(d), dtype=dtype) self.assertEqual(z.dtype, dtype) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.numpy(), np.ones([2, 3])) class OnesLikeTest(test.TestCase): def testOnesLike(self): for dtype in [ dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.uint8, dtypes_lib.int16, dtypes_lib.int8, dtypes_lib.complex64, dtypes_lib.complex128, dtypes_lib.int64 ]: numpy_dtype = dtype.as_numpy_dtype # Creates a tensor of non-zero values with shape 2 x 3. d = constant_op.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype) # Constructs a tensor of zeros of the same dimensions and type as "d". z_var = array_ops.ones_like(d) # Test that the type is correct self.assertEqual(z_var.dtype, dtype) z_value = z_var.numpy() # Test that the value is correct self.assertTrue(np.array_equal(z_value, np.array([[1] * 3] * 2))) self.assertEqual([2, 3], z_var.get_shape()) class FillTest(test.TestCase): def _compare(self, dims, val, np_ans, use_gpu): ctx = context.context() device = "GPU:0" if (use_gpu and ctx.num_gpus()) else "CPU:0" with ops.device(device): tf_ans = array_ops.fill(dims, val, name="fill") out = tf_ans.numpy() self.assertAllClose(np_ans, out) def _compareAll(self, dims, val, np_ans): self._compare(dims, val, np_ans, False) self._compare(dims, val, np_ans, True) def testFillFloat(self): np_ans = np.array([[3.1415] * 3] * 2).astype(np.float32) self._compareAll([2, 3], np_ans[0][0], np_ans) def testFillDouble(self): np_ans = np.array([[3.1415] * 3] * 2).astype(np.float64) self._compareAll([2, 3], np_ans[0][0], np_ans) def testFillInt32(self): np_ans = np.array([[42] * 3] * 2).astype(np.int32) self._compareAll([2, 3], np_ans[0][0], np_ans) def testFillInt64(self): np_ans = np.array([[-42] * 3] * 2).astype(np.int64) self._compareAll([2, 3], np_ans[0][0], np_ans) def testFillComplex64(self): np_ans = np.array([[0.15] * 3] * 2).astype(np.complex64) self._compare([2, 3], np_ans[0][0], np_ans, use_gpu=False) def testFillComplex128(self): np_ans = np.array([[0.15] * 3] * 2).astype(np.complex128) self._compare([2, 3], np_ans[0][0], np_ans, use_gpu=False) def testFillString(self): np_ans = np.array([[b"yolo"] * 3] * 2) tf_ans = array_ops.fill([2, 3], np_ans[0][0], name="fill").numpy() self.assertAllEqual(np_ans, tf_ans) def testFillNegative(self): for shape in (-1,), (2, -1), (-1, 2), (-2), (-3): with self.assertRaises(errors_impl.InvalidArgumentError): array_ops.fill(shape, 7) def testShapeFunctionEdgeCases(self): # Non-vector dimensions. with self.assertRaises(errors_impl.InvalidArgumentError): array_ops.fill([[0, 1], [2, 3]], 1.0) # Non-scalar value. with self.assertRaises(errors_impl.InvalidArgumentError): array_ops.fill([3, 2], [1.0, 2.0]) if __name__ == "__main__": test.main()
apache-2.0
ahnqirage/spark
python/pyspark/worker.py
4
16532
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ Worker that receives input from Piped RDD. """ from __future__ import print_function import os import sys import time import resource import socket import traceback from pyspark.accumulators import _accumulatorRegistry from pyspark.broadcast import Broadcast, _broadcastRegistry from pyspark.java_gateway import local_connect_and_auth from pyspark.taskcontext import BarrierTaskContext, TaskContext from pyspark.files import SparkFiles from pyspark.rdd import PythonEvalType from pyspark.serializers import write_with_length, write_int, read_long, read_bool, \ write_long, read_int, SpecialLengths, UTF8Deserializer, PickleSerializer, \ BatchedSerializer, ArrowStreamPandasSerializer from pyspark.sql.types import to_arrow_type from pyspark.util import _get_argspec, fail_on_stopiteration from pyspark import shuffle if sys.version >= '3': basestring = str pickleSer = PickleSerializer() utf8_deserializer = UTF8Deserializer() def report_times(outfile, boot, init, finish): write_int(SpecialLengths.TIMING_DATA, outfile) write_long(int(1000 * boot), outfile) write_long(int(1000 * init), outfile) write_long(int(1000 * finish), outfile) def add_path(path): # worker can be used, so donot add path multiple times if path not in sys.path: # overwrite system packages sys.path.insert(1, path) def read_command(serializer, file): command = serializer._read_with_length(file) if isinstance(command, Broadcast): command = serializer.loads(command.value) return command def chain(f, g): """chain two functions together """ return lambda *a: g(f(*a)) def wrap_udf(f, return_type): if return_type.needConversion(): toInternal = return_type.toInternal return lambda *a: toInternal(f(*a)) else: return lambda *a: f(*a) def wrap_scalar_pandas_udf(f, return_type): arrow_return_type = to_arrow_type(return_type) def verify_result_length(*a): result = f(*a) if not hasattr(result, "__len__"): raise TypeError("Return type of the user-defined function should be " "Pandas.Series, but is {}".format(type(result))) if len(result) != len(a[0]): raise RuntimeError("Result vector from pandas_udf was not the required length: " "expected %d, got %d" % (len(a[0]), len(result))) return result return lambda *a: (verify_result_length(*a), arrow_return_type) def wrap_grouped_map_pandas_udf(f, return_type, argspec, runner_conf): assign_cols_by_name = runner_conf.get( "spark.sql.legacy.execution.pandas.groupedMap.assignColumnsByName", "true") assign_cols_by_name = assign_cols_by_name.lower() == "true" def wrapped(key_series, value_series): import pandas as pd if len(argspec.args) == 1: result = f(pd.concat(value_series, axis=1)) elif len(argspec.args) == 2: key = tuple(s[0] for s in key_series) result = f(key, pd.concat(value_series, axis=1)) if not isinstance(result, pd.DataFrame): raise TypeError("Return type of the user-defined function should be " "pandas.DataFrame, but is {}".format(type(result))) if not len(result.columns) == len(return_type): raise RuntimeError( "Number of columns of the returned pandas.DataFrame " "doesn't match specified schema. " "Expected: {} Actual: {}".format(len(return_type), len(result.columns))) # Assign result columns by schema name if user labeled with strings, else use position if assign_cols_by_name and any(isinstance(name, basestring) for name in result.columns): return [(result[field.name], to_arrow_type(field.dataType)) for field in return_type] else: return [(result[result.columns[i]], to_arrow_type(field.dataType)) for i, field in enumerate(return_type)] return wrapped def wrap_grouped_agg_pandas_udf(f, return_type): arrow_return_type = to_arrow_type(return_type) def wrapped(*series): import pandas as pd result = f(*series) return pd.Series([result]) return lambda *a: (wrapped(*a), arrow_return_type) def wrap_window_agg_pandas_udf(f, return_type): # This is similar to grouped_agg_pandas_udf, the only difference # is that window_agg_pandas_udf needs to repeat the return value # to match window length, where grouped_agg_pandas_udf just returns # the scalar value. arrow_return_type = to_arrow_type(return_type) def wrapped(*series): import pandas as pd result = f(*series) return pd.Series([result]).repeat(len(series[0])) return lambda *a: (wrapped(*a), arrow_return_type) def read_single_udf(pickleSer, infile, eval_type, runner_conf): num_arg = read_int(infile) arg_offsets = [read_int(infile) for i in range(num_arg)] row_func = None for i in range(read_int(infile)): f, return_type = read_command(pickleSer, infile) if row_func is None: row_func = f else: row_func = chain(row_func, f) # make sure StopIteration's raised in the user code are not ignored # when they are processed in a for loop, raise them as RuntimeError's instead func = fail_on_stopiteration(row_func) # the last returnType will be the return type of UDF if eval_type == PythonEvalType.SQL_SCALAR_PANDAS_UDF: return arg_offsets, wrap_scalar_pandas_udf(func, return_type) elif eval_type == PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF: argspec = _get_argspec(row_func) # signature was lost when wrapping it return arg_offsets, wrap_grouped_map_pandas_udf(func, return_type, argspec, runner_conf) elif eval_type == PythonEvalType.SQL_GROUPED_AGG_PANDAS_UDF: return arg_offsets, wrap_grouped_agg_pandas_udf(func, return_type) elif eval_type == PythonEvalType.SQL_WINDOW_AGG_PANDAS_UDF: return arg_offsets, wrap_window_agg_pandas_udf(func, return_type) elif eval_type == PythonEvalType.SQL_BATCHED_UDF: return arg_offsets, wrap_udf(func, return_type) else: raise ValueError("Unknown eval type: {}".format(eval_type)) def read_udfs(pickleSer, infile, eval_type): runner_conf = {} if eval_type in (PythonEvalType.SQL_SCALAR_PANDAS_UDF, PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF, PythonEvalType.SQL_GROUPED_AGG_PANDAS_UDF, PythonEvalType.SQL_WINDOW_AGG_PANDAS_UDF): # Load conf used for pandas_udf evaluation num_conf = read_int(infile) for i in range(num_conf): k = utf8_deserializer.loads(infile) v = utf8_deserializer.loads(infile) runner_conf[k] = v # NOTE: if timezone is set here, that implies respectSessionTimeZone is True timezone = runner_conf.get("spark.sql.session.timeZone", None) ser = ArrowStreamPandasSerializer(timezone) else: ser = BatchedSerializer(PickleSerializer(), 100) num_udfs = read_int(infile) udfs = {} call_udf = [] mapper_str = "" if eval_type == PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF: # Create function like this: # lambda a: f([a[0]], [a[0], a[1]]) # We assume there is only one UDF here because grouped map doesn't # support combining multiple UDFs. assert num_udfs == 1 # See FlatMapGroupsInPandasExec for how arg_offsets are used to # distinguish between grouping attributes and data attributes arg_offsets, udf = read_single_udf(pickleSer, infile, eval_type, runner_conf) udfs['f'] = udf split_offset = arg_offsets[0] + 1 arg0 = ["a[%d]" % o for o in arg_offsets[1: split_offset]] arg1 = ["a[%d]" % o for o in arg_offsets[split_offset:]] mapper_str = "lambda a: f([%s], [%s])" % (", ".join(arg0), ", ".join(arg1)) else: # Create function like this: # lambda a: (f0(a[0]), f1(a[1], a[2]), f2(a[3])) # In the special case of a single UDF this will return a single result rather # than a tuple of results; this is the format that the JVM side expects. for i in range(num_udfs): arg_offsets, udf = read_single_udf(pickleSer, infile, eval_type, runner_conf) udfs['f%d' % i] = udf args = ["a[%d]" % o for o in arg_offsets] call_udf.append("f%d(%s)" % (i, ", ".join(args))) mapper_str = "lambda a: (%s)" % (", ".join(call_udf)) mapper = eval(mapper_str, udfs) func = lambda _, it: map(mapper, it) # profiling is not supported for UDF return func, None, ser, ser def main(infile, outfile): try: boot_time = time.time() split_index = read_int(infile) if split_index == -1: # for unit tests sys.exit(-1) version = utf8_deserializer.loads(infile) if version != "%d.%d" % sys.version_info[:2]: raise Exception(("Python in worker has different version %s than that in " + "driver %s, PySpark cannot run with different minor versions." + "Please check environment variables PYSPARK_PYTHON and " + "PYSPARK_DRIVER_PYTHON are correctly set.") % ("%d.%d" % sys.version_info[:2], version)) # read inputs only for a barrier task isBarrier = read_bool(infile) boundPort = read_int(infile) secret = UTF8Deserializer().loads(infile) # set up memory limits memory_limit_mb = int(os.environ.get('PYSPARK_EXECUTOR_MEMORY_MB', "-1")) total_memory = resource.RLIMIT_AS try: if memory_limit_mb > 0: (soft_limit, hard_limit) = resource.getrlimit(total_memory) msg = "Current mem limits: {0} of max {1}\n".format(soft_limit, hard_limit) print(msg, file=sys.stderr) # convert to bytes new_limit = memory_limit_mb * 1024 * 1024 if soft_limit == resource.RLIM_INFINITY or new_limit < soft_limit: msg = "Setting mem limits to {0} of max {1}\n".format(new_limit, new_limit) print(msg, file=sys.stderr) resource.setrlimit(total_memory, (new_limit, new_limit)) except (resource.error, OSError, ValueError) as e: # not all systems support resource limits, so warn instead of failing print("WARN: Failed to set memory limit: {0}\n".format(e), file=sys.stderr) # initialize global state taskContext = None if isBarrier: taskContext = BarrierTaskContext._getOrCreate() BarrierTaskContext._initialize(boundPort, secret) else: taskContext = TaskContext._getOrCreate() # read inputs for TaskContext info taskContext._stageId = read_int(infile) taskContext._partitionId = read_int(infile) taskContext._attemptNumber = read_int(infile) taskContext._taskAttemptId = read_long(infile) taskContext._localProperties = dict() for i in range(read_int(infile)): k = utf8_deserializer.loads(infile) v = utf8_deserializer.loads(infile) taskContext._localProperties[k] = v shuffle.MemoryBytesSpilled = 0 shuffle.DiskBytesSpilled = 0 _accumulatorRegistry.clear() # fetch name of workdir spark_files_dir = utf8_deserializer.loads(infile) SparkFiles._root_directory = spark_files_dir SparkFiles._is_running_on_worker = True # fetch names of includes (*.zip and *.egg files) and construct PYTHONPATH add_path(spark_files_dir) # *.py files that were added will be copied here num_python_includes = read_int(infile) for _ in range(num_python_includes): filename = utf8_deserializer.loads(infile) add_path(os.path.join(spark_files_dir, filename)) if sys.version > '3': import importlib importlib.invalidate_caches() # fetch names and values of broadcast variables needs_broadcast_decryption_server = read_bool(infile) num_broadcast_variables = read_int(infile) if needs_broadcast_decryption_server: # read the decrypted data from a server in the jvm port = read_int(infile) auth_secret = utf8_deserializer.loads(infile) (broadcast_sock_file, _) = local_connect_and_auth(port, auth_secret) for _ in range(num_broadcast_variables): bid = read_long(infile) if bid >= 0: if needs_broadcast_decryption_server: read_bid = read_long(broadcast_sock_file) assert(read_bid == bid) _broadcastRegistry[bid] = \ Broadcast(sock_file=broadcast_sock_file) else: path = utf8_deserializer.loads(infile) _broadcastRegistry[bid] = Broadcast(path=path) else: bid = - bid - 1 _broadcastRegistry.pop(bid) if needs_broadcast_decryption_server: broadcast_sock_file.write(b'1') broadcast_sock_file.close() _accumulatorRegistry.clear() eval_type = read_int(infile) if eval_type == PythonEvalType.NON_UDF: func, profiler, deserializer, serializer = read_command(pickleSer, infile) else: func, profiler, deserializer, serializer = read_udfs(pickleSer, infile, eval_type) init_time = time.time() def process(): iterator = deserializer.load_stream(infile) serializer.dump_stream(func(split_index, iterator), outfile) if profiler: profiler.profile(process) else: process() except Exception: try: write_int(SpecialLengths.PYTHON_EXCEPTION_THROWN, outfile) write_with_length(traceback.format_exc().encode("utf-8"), outfile) except IOError: # JVM close the socket pass except Exception: # Write the error to stderr if it happened while serializing print("PySpark worker failed with exception:", file=sys.stderr) print(traceback.format_exc(), file=sys.stderr) sys.exit(-1) finish_time = time.time() report_times(outfile, boot_time, init_time, finish_time) write_long(shuffle.MemoryBytesSpilled, outfile) write_long(shuffle.DiskBytesSpilled, outfile) # Mark the beginning of the accumulators section of the output write_int(SpecialLengths.END_OF_DATA_SECTION, outfile) write_int(len(_accumulatorRegistry), outfile) for (aid, accum) in _accumulatorRegistry.items(): pickleSer._write_with_length((aid, accum._value), outfile) # check end of stream if read_int(infile) == SpecialLengths.END_OF_STREAM: write_int(SpecialLengths.END_OF_STREAM, outfile) else: # write a different value to tell JVM to not reuse this worker write_int(SpecialLengths.END_OF_DATA_SECTION, outfile) sys.exit(-1) if __name__ == '__main__': # Read information about how to connect back to the JVM from the environment. java_port = int(os.environ["PYTHON_WORKER_FACTORY_PORT"]) auth_secret = os.environ["PYTHON_WORKER_FACTORY_SECRET"] (sock_file, _) = local_connect_and_auth(java_port, auth_secret) main(sock_file, sock_file)
apache-2.0
russel1237/scikit-learn
sklearn/linear_model/logistic.py
57
65098
""" Logistic Regression """ # Author: Gael Varoquaux <[email protected]> # Fabian Pedregosa <[email protected]> # Alexandre Gramfort <[email protected]> # Manoj Kumar <[email protected]> # Lars Buitinck # Simon Wu <[email protected]> import numbers import warnings import numpy as np from scipy import optimize, sparse from .base import LinearClassifierMixin, SparseCoefMixin, BaseEstimator from .sag import sag_solver from .sag_fast import get_max_squared_sum from ..feature_selection.from_model import _LearntSelectorMixin from ..preprocessing import LabelEncoder, LabelBinarizer from ..svm.base import _fit_liblinear from ..utils import check_array, check_consistent_length, compute_class_weight from ..utils import check_random_state from ..utils.extmath import (logsumexp, log_logistic, safe_sparse_dot, softmax, squared_norm) from ..utils.optimize import newton_cg from ..utils.validation import (DataConversionWarning, check_X_y, NotFittedError) from ..utils.fixes import expit from ..externals.joblib import Parallel, delayed from ..cross_validation import check_cv from ..externals import six from ..metrics import SCORERS # .. some helper functions for logistic_regression_path .. def _intercept_dot(w, X, y): """Computes y * np.dot(X, w). It takes into consideration if the intercept should be fit or not. Parameters ---------- w : ndarray, shape (n_features,) or (n_features + 1,) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. y : ndarray, shape (n_samples,) Array of labels. """ c = 0. if w.size == X.shape[1] + 1: c = w[-1] w = w[:-1] z = safe_sparse_dot(X, w) + c return w, c, y * z def _logistic_loss_and_grad(w, X, y, alpha, sample_weight=None): """Computes the logistic loss and gradient. Parameters ---------- w : ndarray, shape (n_features,) or (n_features + 1,) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. y : ndarray, shape (n_samples,) Array of labels. alpha : float Regularization parameter. alpha is equal to 1 / C. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- out : float Logistic loss. grad : ndarray, shape (n_features,) or (n_features + 1,) Logistic gradient. """ _, n_features = X.shape grad = np.empty_like(w) w, c, yz = _intercept_dot(w, X, y) if sample_weight is None: sample_weight = np.ones(y.shape[0]) # Logistic loss is the negative of the log of the logistic function. out = -np.sum(sample_weight * log_logistic(yz)) + .5 * alpha * np.dot(w, w) z = expit(yz) z0 = sample_weight * (z - 1) * y grad[:n_features] = safe_sparse_dot(X.T, z0) + alpha * w # Case where we fit the intercept. if grad.shape[0] > n_features: grad[-1] = z0.sum() return out, grad def _logistic_loss(w, X, y, alpha, sample_weight=None): """Computes the logistic loss. Parameters ---------- w : ndarray, shape (n_features,) or (n_features + 1,) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. y : ndarray, shape (n_samples,) Array of labels. alpha : float Regularization parameter. alpha is equal to 1 / C. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- out : float Logistic loss. """ w, c, yz = _intercept_dot(w, X, y) if sample_weight is None: sample_weight = np.ones(y.shape[0]) # Logistic loss is the negative of the log of the logistic function. out = -np.sum(sample_weight * log_logistic(yz)) + .5 * alpha * np.dot(w, w) return out def _logistic_grad_hess(w, X, y, alpha, sample_weight=None): """Computes the gradient and the Hessian, in the case of a logistic loss. Parameters ---------- w : ndarray, shape (n_features,) or (n_features + 1,) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. y : ndarray, shape (n_samples,) Array of labels. alpha : float Regularization parameter. alpha is equal to 1 / C. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- grad : ndarray, shape (n_features,) or (n_features + 1,) Logistic gradient. Hs : callable Function that takes the gradient as a parameter and returns the matrix product of the Hessian and gradient. """ n_samples, n_features = X.shape grad = np.empty_like(w) fit_intercept = grad.shape[0] > n_features w, c, yz = _intercept_dot(w, X, y) if sample_weight is None: sample_weight = np.ones(y.shape[0]) z = expit(yz) z0 = sample_weight * (z - 1) * y grad[:n_features] = safe_sparse_dot(X.T, z0) + alpha * w # Case where we fit the intercept. if fit_intercept: grad[-1] = z0.sum() # The mat-vec product of the Hessian d = sample_weight * z * (1 - z) if sparse.issparse(X): dX = safe_sparse_dot(sparse.dia_matrix((d, 0), shape=(n_samples, n_samples)), X) else: # Precompute as much as possible dX = d[:, np.newaxis] * X if fit_intercept: # Calculate the double derivative with respect to intercept # In the case of sparse matrices this returns a matrix object. dd_intercept = np.squeeze(np.array(dX.sum(axis=0))) def Hs(s): ret = np.empty_like(s) ret[:n_features] = X.T.dot(dX.dot(s[:n_features])) ret[:n_features] += alpha * s[:n_features] # For the fit intercept case. if fit_intercept: ret[:n_features] += s[-1] * dd_intercept ret[-1] = dd_intercept.dot(s[:n_features]) ret[-1] += d.sum() * s[-1] return ret return grad, Hs def _multinomial_loss(w, X, Y, alpha, sample_weight): """Computes multinomial loss and class probabilities. Parameters ---------- w : ndarray, shape (n_classes * n_features,) or (n_classes * (n_features + 1),) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. Y : ndarray, shape (n_samples, n_classes) Transformed labels according to the output of LabelBinarizer. alpha : float Regularization parameter. alpha is equal to 1 / C. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- loss : float Multinomial loss. p : ndarray, shape (n_samples, n_classes) Estimated class probabilities. w : ndarray, shape (n_classes, n_features) Reshaped param vector excluding intercept terms. """ n_classes = Y.shape[1] n_features = X.shape[1] fit_intercept = w.size == (n_classes * (n_features + 1)) w = w.reshape(n_classes, -1) sample_weight = sample_weight[:, np.newaxis] if fit_intercept: intercept = w[:, -1] w = w[:, :-1] else: intercept = 0 p = safe_sparse_dot(X, w.T) p += intercept p -= logsumexp(p, axis=1)[:, np.newaxis] loss = -(sample_weight * Y * p).sum() loss += 0.5 * alpha * squared_norm(w) p = np.exp(p, p) return loss, p, w def _multinomial_loss_grad(w, X, Y, alpha, sample_weight): """Computes the multinomial loss, gradient and class probabilities. Parameters ---------- w : ndarray, shape (n_classes * n_features,) or (n_classes * (n_features + 1),) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. Y : ndarray, shape (n_samples, n_classes) Transformed labels according to the output of LabelBinarizer. alpha : float Regularization parameter. alpha is equal to 1 / C. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. Returns ------- loss : float Multinomial loss. grad : ndarray, shape (n_classes * n_features,) or (n_classes * (n_features + 1),) Ravelled gradient of the multinomial loss. p : ndarray, shape (n_samples, n_classes) Estimated class probabilities """ n_classes = Y.shape[1] n_features = X.shape[1] fit_intercept = (w.size == n_classes * (n_features + 1)) grad = np.zeros((n_classes, n_features + bool(fit_intercept))) loss, p, w = _multinomial_loss(w, X, Y, alpha, sample_weight) sample_weight = sample_weight[:, np.newaxis] diff = sample_weight * (p - Y) grad[:, :n_features] = safe_sparse_dot(diff.T, X) grad[:, :n_features] += alpha * w if fit_intercept: grad[:, -1] = diff.sum(axis=0) return loss, grad.ravel(), p def _multinomial_grad_hess(w, X, Y, alpha, sample_weight): """ Computes the gradient and the Hessian, in the case of a multinomial loss. Parameters ---------- w : ndarray, shape (n_classes * n_features,) or (n_classes * (n_features + 1),) Coefficient vector. X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. Y : ndarray, shape (n_samples, n_classes) Transformed labels according to the output of LabelBinarizer. alpha : float Regularization parameter. alpha is equal to 1 / C. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. Returns ------- grad : array, shape (n_classes * n_features,) or (n_classes * (n_features + 1),) Ravelled gradient of the multinomial loss. hessp : callable Function that takes in a vector input of shape (n_classes * n_features) or (n_classes * (n_features + 1)) and returns matrix-vector product with hessian. References ---------- Barak A. Pearlmutter (1993). Fast Exact Multiplication by the Hessian. http://www.bcl.hamilton.ie/~barak/papers/nc-hessian.pdf """ n_features = X.shape[1] n_classes = Y.shape[1] fit_intercept = w.size == (n_classes * (n_features + 1)) # `loss` is unused. Refactoring to avoid computing it does not # significantly speed up the computation and decreases readability loss, grad, p = _multinomial_loss_grad(w, X, Y, alpha, sample_weight) sample_weight = sample_weight[:, np.newaxis] # Hessian-vector product derived by applying the R-operator on the gradient # of the multinomial loss function. def hessp(v): v = v.reshape(n_classes, -1) if fit_intercept: inter_terms = v[:, -1] v = v[:, :-1] else: inter_terms = 0 # r_yhat holds the result of applying the R-operator on the multinomial # estimator. r_yhat = safe_sparse_dot(X, v.T) r_yhat += inter_terms r_yhat += (-p * r_yhat).sum(axis=1)[:, np.newaxis] r_yhat *= p r_yhat *= sample_weight hessProd = np.zeros((n_classes, n_features + bool(fit_intercept))) hessProd[:, :n_features] = safe_sparse_dot(r_yhat.T, X) hessProd[:, :n_features] += v * alpha if fit_intercept: hessProd[:, -1] = r_yhat.sum(axis=0) return hessProd.ravel() return grad, hessp def _check_solver_option(solver, multi_class, penalty, dual, sample_weight): if solver not in ['liblinear', 'newton-cg', 'lbfgs', 'sag']: raise ValueError("Logistic Regression supports only liblinear," " newton-cg, lbfgs and sag solvers, got %s" % solver) if multi_class not in ['multinomial', 'ovr']: raise ValueError("multi_class should be either multinomial or " "ovr, got %s" % multi_class) if multi_class == 'multinomial' and solver in ['liblinear', 'sag']: raise ValueError("Solver %s does not support " "a multinomial backend." % solver) if solver != 'liblinear': if penalty != 'l2': raise ValueError("Solver %s supports only l2 penalties, " "got %s penalty." % (solver, penalty)) if dual: raise ValueError("Solver %s supports only " "dual=False, got dual=%s" % (solver, dual)) if solver == 'liblinear' and sample_weight is not None: raise ValueError("Solver %s does not support " "sample weights." % solver) def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True, max_iter=100, tol=1e-4, verbose=0, solver='lbfgs', coef=None, copy=False, class_weight=None, dual=False, penalty='l2', intercept_scaling=1., multi_class='ovr', random_state=None, check_input=True, max_squared_sum=None, sample_weight=None): """Compute a Logistic Regression model for a list of regularization parameters. This is an implementation that uses the result of the previous model to speed up computations along the set of solutions, making it faster than sequentially calling LogisticRegression for the different parameters. Read more in the :ref:`User Guide <logistic_regression>`. Parameters ---------- X : array-like or sparse matrix, shape (n_samples, n_features) Input data. y : array-like, shape (n_samples,) Input data, target values. Cs : int | array-like, shape (n_cs,) List of values for the regularization parameter or integer specifying the number of regularization parameters that should be used. In this case, the parameters will be chosen in a logarithmic scale between 1e-4 and 1e4. pos_class : int, None The class with respect to which we perform a one-vs-all fit. If None, then it is assumed that the given problem is binary. fit_intercept : bool Whether to fit an intercept for the model. In this case the shape of the returned array is (n_cs, n_features + 1). max_iter : int Maximum number of iterations for the solver. tol : float Stopping criterion. For the newton-cg and lbfgs solvers, the iteration will stop when ``max{|g_i | i = 1, ..., n} <= tol`` where ``g_i`` is the i-th component of the gradient. verbose : int For the liblinear and lbfgs solvers set verbose to any positive number for verbosity. solver : {'lbfgs', 'newton-cg', 'liblinear', 'sag'} Numerical solver to use. coef : array-like, shape (n_features,), default None Initialization value for coefficients of logistic regression. Useless for liblinear solver. copy : bool, default False Whether or not to produce a copy of the data. A copy is not required anymore. This parameter is deprecated and will be removed in 0.19. class_weight : dict or 'balanced', optional Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. dual : bool Dual or primal formulation. Dual formulation is only implemented for l2 penalty with liblinear solver. Prefer dual=False when n_samples > n_features. penalty : str, 'l1' or 'l2' Used to specify the norm used in the penalization. The 'newton-cg', 'sag' and 'lbfgs' solvers support only l2 penalties. intercept_scaling : float, default 1. This parameter is useful only when the solver 'liblinear' is used and self.fit_intercept is set to True. In this case, x becomes [x, self.intercept_scaling], i.e. a "synthetic" feature with constant value equals to intercept_scaling is appended to the instance vector. The intercept becomes intercept_scaling * synthetic feature weight Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. multi_class : str, {'ovr', 'multinomial'} Multiclass option can be either 'ovr' or 'multinomial'. If the option chosen is 'ovr', then a binary problem is fit for each label. Else the loss minimised is the multinomial loss fit across the entire probability distribution. Works only for the 'lbfgs' and 'newton-cg' solvers. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. check_input : bool, default True If False, the input arrays X and y will not be checked. max_squared_sum : float, default None Maximum squared sum of X over samples. Used only in SAG solver. If None, it will be computed, going through all the samples. The value should be precomputed to speed up cross validation. sample_weight : array-like, shape(n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- coefs : ndarray, shape (n_cs, n_features) or (n_cs, n_features + 1) List of coefficients for the Logistic Regression model. If fit_intercept is set to True then the second dimension will be n_features + 1, where the last item represents the intercept. Cs : ndarray Grid of Cs used for cross-validation. n_iter : array, shape (n_cs,) Actual number of iteration for each Cs. Notes ----- You might get slighly different results with the solver liblinear than with the others since this uses LIBLINEAR which penalizes the intercept. """ if copy: warnings.warn("A copy is not required anymore. The 'copy' parameter " "is deprecated and will be removed in 0.19.", DeprecationWarning) if isinstance(Cs, numbers.Integral): Cs = np.logspace(-4, 4, Cs) _check_solver_option(solver, multi_class, penalty, dual, sample_weight) # Preprocessing. if check_input or copy: X = check_array(X, accept_sparse='csr', dtype=np.float64) y = check_array(y, ensure_2d=False, copy=copy, dtype=None) check_consistent_length(X, y) _, n_features = X.shape classes = np.unique(y) random_state = check_random_state(random_state) if pos_class is None and multi_class != 'multinomial': if (classes.size > 2): raise ValueError('To fit OvR, use the pos_class argument') # np.unique(y) gives labels in sorted order. pos_class = classes[1] # If sample weights exist, convert them to array (support for lists) # and check length # Otherwise set them to 1 for all examples if sample_weight is not None: sample_weight = np.array(sample_weight, dtype=np.float64, order='C') check_consistent_length(y, sample_weight) else: sample_weight = np.ones(X.shape[0]) # If class_weights is a dict (provided by the user), the weights # are assigned to the original labels. If it is "auto", then # the class_weights are assigned after masking the labels with a OvR. le = LabelEncoder() if isinstance(class_weight, dict): if solver == "liblinear": if classes.size == 2: # Reconstruct the weights with keys 1 and -1 temp = {1: class_weight[pos_class], -1: class_weight[classes[0]]} class_weight = temp.copy() else: raise ValueError("In LogisticRegressionCV the liblinear " "solver cannot handle multiclass with " "class_weight of type dict. Use the lbfgs, " "newton-cg or sag solvers or set " "class_weight='auto'") else: class_weight_ = compute_class_weight(class_weight, classes, y) sample_weight *= class_weight_[le.fit_transform(y)] # For doing a ovr, we need to mask the labels first. for the # multinomial case this is not necessary. if multi_class == 'ovr': w0 = np.zeros(n_features + int(fit_intercept)) mask_classes = np.array([-1, 1]) mask = (y == pos_class) y_bin = np.ones(y.shape, dtype=np.float64) y_bin[~mask] = -1. else: lbin = LabelBinarizer() Y_bin = lbin.fit_transform(y) if Y_bin.shape[1] == 1: Y_bin = np.hstack([1 - Y_bin, Y_bin]) w0 = np.zeros((Y_bin.shape[1], n_features + int(fit_intercept)), order='F') mask_classes = classes if class_weight == "auto": class_weight_ = compute_class_weight(class_weight, mask_classes, y_bin) sample_weight *= class_weight_[le.fit_transform(y_bin)] if coef is not None: # it must work both giving the bias term and not if multi_class == 'ovr': if coef.size not in (n_features, w0.size): raise ValueError( 'Initialization coef is of shape %d, expected shape ' '%d or %d' % (coef.size, n_features, w0.size)) w0[:coef.size] = coef else: # For binary problems coef.shape[0] should be 1, otherwise it # should be classes.size. n_vectors = classes.size if n_vectors == 2: n_vectors = 1 if (coef.shape[0] != n_vectors or coef.shape[1] not in (n_features, n_features + 1)): raise ValueError( 'Initialization coef is of shape (%d, %d), expected ' 'shape (%d, %d) or (%d, %d)' % ( coef.shape[0], coef.shape[1], classes.size, n_features, classes.size, n_features + 1)) w0[:, :coef.shape[1]] = coef if multi_class == 'multinomial': # fmin_l_bfgs_b and newton-cg accepts only ravelled parameters. w0 = w0.ravel() target = Y_bin if solver == 'lbfgs': func = lambda x, *args: _multinomial_loss_grad(x, *args)[0:2] elif solver == 'newton-cg': func = lambda x, *args: _multinomial_loss(x, *args)[0] grad = lambda x, *args: _multinomial_loss_grad(x, *args)[1] hess = _multinomial_grad_hess else: target = y_bin if solver == 'lbfgs': func = _logistic_loss_and_grad elif solver == 'newton-cg': func = _logistic_loss grad = lambda x, *args: _logistic_loss_and_grad(x, *args)[1] hess = _logistic_grad_hess coefs = list() warm_start_sag = {'coef': w0} n_iter = np.zeros(len(Cs), dtype=np.int32) for i, C in enumerate(Cs): if solver == 'lbfgs': try: w0, loss, info = optimize.fmin_l_bfgs_b( func, w0, fprime=None, args=(X, target, 1. / C, sample_weight), iprint=(verbose > 0) - 1, pgtol=tol, maxiter=max_iter) except TypeError: # old scipy doesn't have maxiter w0, loss, info = optimize.fmin_l_bfgs_b( func, w0, fprime=None, args=(X, target, 1. / C, sample_weight), iprint=(verbose > 0) - 1, pgtol=tol) if info["warnflag"] == 1 and verbose > 0: warnings.warn("lbfgs failed to converge. Increase the number " "of iterations.") try: n_iter_i = info['nit'] - 1 except: n_iter_i = info['funcalls'] - 1 elif solver == 'newton-cg': args = (X, target, 1. / C, sample_weight) w0, n_iter_i = newton_cg(hess, func, grad, w0, args=args, maxiter=max_iter, tol=tol) elif solver == 'liblinear': coef_, intercept_, n_iter_i, = _fit_liblinear( X, target, C, fit_intercept, intercept_scaling, class_weight, penalty, dual, verbose, max_iter, tol, random_state) if fit_intercept: w0 = np.concatenate([coef_.ravel(), intercept_]) else: w0 = coef_.ravel() elif solver == 'sag': w0, n_iter_i, warm_start_sag = sag_solver( X, target, sample_weight, 'log', 1. / C, max_iter, tol, verbose, random_state, False, max_squared_sum, warm_start_sag) else: raise ValueError("solver must be one of {'liblinear', 'lbfgs', " "'newton-cg', 'sag'}, got '%s' instead" % solver) if multi_class == 'multinomial': multi_w0 = np.reshape(w0, (classes.size, -1)) if classes.size == 2: multi_w0 = multi_w0[1][np.newaxis, :] coefs.append(multi_w0) else: coefs.append(w0.copy()) n_iter[i] = n_iter_i return coefs, np.array(Cs), n_iter # helper function for LogisticCV def _log_reg_scoring_path(X, y, train, test, pos_class=None, Cs=10, scoring=None, fit_intercept=False, max_iter=100, tol=1e-4, class_weight=None, verbose=0, solver='lbfgs', penalty='l2', dual=False, intercept_scaling=1., multi_class='ovr', random_state=None, max_squared_sum=None, sample_weight=None): """Computes scores across logistic_regression_path 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 labels. train : list of indices The indices of the train set. test : list of indices The indices of the test set. pos_class : int, None The class with respect to which we perform a one-vs-all fit. If None, then it is assumed that the given problem is binary. Cs : list of floats | int Each of the values in Cs describes the inverse of regularization strength. If Cs is as an int, then a grid of Cs values are chosen in a logarithmic scale between 1e-4 and 1e4. If not provided, then a fixed set of values for Cs are used. scoring : callable For a list of scoring functions that can be used, look at :mod:`sklearn.metrics`. The default scoring option used is accuracy_score. fit_intercept : bool If False, then the bias term is set to zero. Else the last term of each coef_ gives us the intercept. max_iter : int Maximum number of iterations for the solver. tol : float Tolerance for stopping criteria. class_weight : dict or 'balanced', optional Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. verbose : int For the liblinear and lbfgs solvers set verbose to any positive number for verbosity. solver : {'lbfgs', 'newton-cg', 'liblinear', 'sag'} Decides which solver to use. penalty : str, 'l1' or 'l2' Used to specify the norm used in the penalization. The newton-cg and lbfgs solvers support only l2 penalties. dual : bool Dual or primal formulation. Dual formulation is only implemented for l2 penalty with liblinear solver. Prefer dual=False when n_samples > n_features. intercept_scaling : float, default 1. This parameter is useful only when the solver 'liblinear' is used and self.fit_intercept is set to True. In this case, x becomes [x, self.intercept_scaling], i.e. a "synthetic" feature with constant value equals to intercept_scaling is appended to the instance vector. The intercept becomes intercept_scaling * synthetic feature weight Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. multi_class : str, {'ovr', 'multinomial'} Multiclass option can be either 'ovr' or 'multinomial'. If the option chosen is 'ovr', then a binary problem is fit for each label. Else the loss minimised is the multinomial loss fit across the entire probability distribution. Works only for the 'lbfgs' and 'newton-cg' solver. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. max_squared_sum : float, default None Maximum squared sum of X over samples. Used only in SAG solver. If None, it will be computed, going through all the samples. The value should be precomputed to speed up cross validation. sample_weight : array-like, shape(n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- coefs : ndarray, shape (n_cs, n_features) or (n_cs, n_features + 1) List of coefficients for the Logistic Regression model. If fit_intercept is set to True then the second dimension will be n_features + 1, where the last item represents the intercept. Cs : ndarray Grid of Cs used for cross-validation. scores : ndarray, shape (n_cs,) Scores obtained for each Cs. n_iter : array, shape(n_cs,) Actual number of iteration for each Cs. """ _check_solver_option(solver, multi_class, penalty, dual, sample_weight) X_train = X[train] X_test = X[test] y_train = y[train] y_test = y[test] if sample_weight is not None: sample_weight = sample_weight[train] coefs, Cs, n_iter = logistic_regression_path( X_train, y_train, Cs=Cs, fit_intercept=fit_intercept, solver=solver, max_iter=max_iter, class_weight=class_weight, pos_class=pos_class, multi_class=multi_class, tol=tol, verbose=verbose, dual=dual, penalty=penalty, intercept_scaling=intercept_scaling, random_state=random_state, check_input=False, max_squared_sum=max_squared_sum, sample_weight=sample_weight) log_reg = LogisticRegression(fit_intercept=fit_intercept) # The score method of Logistic Regression has a classes_ attribute. if multi_class == 'ovr': log_reg.classes_ = np.array([-1, 1]) elif multi_class == 'multinomial': log_reg.classes_ = np.unique(y_train) else: raise ValueError("multi_class should be either multinomial or ovr, " "got %d" % multi_class) if pos_class is not None: mask = (y_test == pos_class) y_test = np.ones(y_test.shape, dtype=np.float64) y_test[~mask] = -1. # To deal with object dtypes, we need to convert into an array of floats. y_test = check_array(y_test, dtype=np.float64, ensure_2d=False) scores = list() if isinstance(scoring, six.string_types): scoring = SCORERS[scoring] for w in coefs: if multi_class == 'ovr': w = w[np.newaxis, :] if fit_intercept: log_reg.coef_ = w[:, :-1] log_reg.intercept_ = w[:, -1] else: log_reg.coef_ = w log_reg.intercept_ = 0. if scoring is None: scores.append(log_reg.score(X_test, y_test)) else: scores.append(scoring(log_reg, X_test, y_test)) return coefs, Cs, np.array(scores), n_iter class LogisticRegression(BaseEstimator, LinearClassifierMixin, _LearntSelectorMixin, SparseCoefMixin): """Logistic Regression (aka logit, MaxEnt) classifier. In the multiclass case, the training algorithm uses the one-vs-rest (OvR) scheme if the 'multi_class' option is set to 'ovr' and uses the cross-entropy loss, if the 'multi_class' option is set to 'multinomial'. (Currently the 'multinomial' option is supported only by the 'lbfgs' and 'newton-cg' solvers.) This class implements regularized logistic regression using the `liblinear` library, newton-cg and lbfgs solvers. It can handle both dense and sparse input. Use C-ordered arrays or CSR matrices containing 64-bit floats for optimal performance; any other input format will be converted (and copied). The newton-cg and lbfgs solvers support only L2 regularization with primal formulation. The liblinear solver supports both L1 and L2 regularization, with a dual formulation only for the L2 penalty. Read more in the :ref:`User Guide <logistic_regression>`. Parameters ---------- penalty : str, 'l1' or 'l2' Used to specify the norm used in the penalization. The newton-cg and lbfgs solvers support only l2 penalties. dual : bool Dual or primal formulation. Dual formulation is only implemented for l2 penalty with liblinear solver. Prefer dual=False when n_samples > n_features. C : float, optional (default=1.0) Inverse of regularization strength; must be a positive float. Like in support vector machines, smaller values specify stronger regularization. fit_intercept : bool, default: True Specifies if a constant (a.k.a. bias or intercept) should be added to the decision function. intercept_scaling : float, default: 1 Useful only if solver is liblinear. when self.fit_intercept is True, instance vector x becomes [x, self.intercept_scaling], i.e. a "synthetic" feature with constant value equals to intercept_scaling is appended to the instance vector. The intercept becomes intercept_scaling * synthetic feature weight Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. class_weight : dict or 'balanced', optional Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. max_iter : int Useful only for the newton-cg, sag and lbfgs solvers. Maximum number of iterations taken for the solvers to converge. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. solver : {'newton-cg', 'lbfgs', 'liblinear', 'sag'} Algorithm to use in the optimization problem. - For small datasets, 'liblinear' is a good choice, whereas 'sag' is faster for large ones. - For multiclass problems, only 'newton-cg' and 'lbfgs' handle multinomial loss; 'sag' and 'liblinear' are limited to one-versus-rest schemes. - 'newton-cg', 'lbfgs' and 'sag' only handle L2 penalty. Note that 'sag' fast convergence is only guaranteed on features with approximately the same scale. You can preprocess the data with a scaler from sklearn.preprocessing. tol : float, optional Tolerance for stopping criteria. multi_class : str, {'ovr', 'multinomial'} Multiclass option can be either 'ovr' or 'multinomial'. If the option chosen is 'ovr', then a binary problem is fit for each label. Else the loss minimised is the multinomial loss fit across the entire probability distribution. Works only for the 'lbfgs' solver. verbose : int For the liblinear and lbfgs solvers set verbose to any positive number for verbosity. warm_start : bool, optional When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. Useless for liblinear solver. n_jobs : int, optional Number of CPU cores used during the cross-validation loop. If given a value of -1, all cores are used. Attributes ---------- coef_ : array, shape (n_classes, n_features) Coefficient of the features in the decision function. intercept_ : array, shape (n_classes,) Intercept (a.k.a. bias) added to the decision function. If `fit_intercept` is set to False, the intercept is set to zero. n_iter_ : array, shape (n_classes,) or (1, ) Actual number of iterations for all classes. If binary or multinomial, it returns only 1 element. For liblinear solver, only the maximum number of iteration across all classes is given. See also -------- SGDClassifier : incrementally trained logistic regression (when given the parameter ``loss="log"``). sklearn.svm.LinearSVC : learns SVM models using the same algorithm. Notes ----- The underlying C implementation uses a random number generator to select features when fitting the model. It is thus not uncommon, to have slightly different results for the same input data. If that happens, try with a smaller tol parameter. Predict output may not match that of standalone liblinear in certain cases. See :ref:`differences from liblinear <liblinear_differences>` in the narrative documentation. References ---------- LIBLINEAR -- A Library for Large Linear Classification http://www.csie.ntu.edu.tw/~cjlin/liblinear/ Hsiang-Fu Yu, Fang-Lan Huang, Chih-Jen Lin (2011). Dual coordinate descent methods for logistic regression and maximum entropy models. Machine Learning 85(1-2):41-75. http://www.csie.ntu.edu.tw/~cjlin/papers/maxent_dual.pdf """ def __init__(self, penalty='l2', dual=False, tol=1e-4, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None, solver='liblinear', max_iter=100, multi_class='ovr', verbose=0, warm_start=False, n_jobs=1): self.penalty = penalty self.dual = dual self.tol = tol self.C = C self.fit_intercept = fit_intercept self.intercept_scaling = intercept_scaling self.class_weight = class_weight self.random_state = random_state self.solver = solver self.max_iter = max_iter self.multi_class = multi_class self.verbose = verbose self.warm_start = warm_start self.n_jobs = n_jobs def fit(self, X, y, sample_weight=None): """Fit the model according to the given training data. Parameters ---------- X : {array-like, sparse matrix}, 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,) Target vector relative to X. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- self : object Returns self. """ if not isinstance(self.C, numbers.Number) or self.C < 0: raise ValueError("Penalty term must be positive; got (C=%r)" % self.C) if not isinstance(self.max_iter, numbers.Number) or self.max_iter < 0: raise ValueError("Maximum number of iteration must be positive;" " got (max_iter=%r)" % self.max_iter) if not isinstance(self.tol, numbers.Number) or self.tol < 0: raise ValueError("Tolerance for stopping criteria must be " "positive; got (tol=%r)" % self.tol) X, y = check_X_y(X, y, accept_sparse='csr', dtype=np.float64, order="C") self.classes_ = np.unique(y) n_samples, n_features = X.shape _check_solver_option(self.solver, self.multi_class, self.penalty, self.dual, sample_weight) if self.solver == 'liblinear': self.coef_, self.intercept_, n_iter_ = _fit_liblinear( X, y, self.C, self.fit_intercept, self.intercept_scaling, self.class_weight, self.penalty, self.dual, self.verbose, self.max_iter, self.tol, self.random_state) self.n_iter_ = np.array([n_iter_]) return self max_squared_sum = get_max_squared_sum(X) if self.solver == 'sag' \ else None n_classes = len(self.classes_) classes_ = self.classes_ if n_classes < 2: raise ValueError("This solver needs samples of at least 2 classes" " in the data, but the data contains only one" " class: %r" % classes_[0]) if len(self.classes_) == 2: n_classes = 1 classes_ = classes_[1:] if self.warm_start: warm_start_coef = getattr(self, 'coef_', None) else: warm_start_coef = None if warm_start_coef is not None and self.fit_intercept: warm_start_coef = np.append(warm_start_coef, self.intercept_[:, np.newaxis], axis=1) self.coef_ = list() self.intercept_ = np.zeros(n_classes) # Hack so that we iterate only once for the multinomial case. if self.multi_class == 'multinomial': classes_ = [None] warm_start_coef = [warm_start_coef] if warm_start_coef is None: warm_start_coef = [None] * n_classes path_func = delayed(logistic_regression_path) # The SAG solver releases the GIL so it's more efficient to use # threads for this solver. backend = 'threading' if self.solver == 'sag' else 'multiprocessing' fold_coefs_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend=backend)( path_func(X, y, pos_class=class_, Cs=[self.C], fit_intercept=self.fit_intercept, tol=self.tol, verbose=self.verbose, solver=self.solver, copy=False, multi_class=self.multi_class, max_iter=self.max_iter, class_weight=self.class_weight, check_input=False, random_state=self.random_state, coef=warm_start_coef_, max_squared_sum=max_squared_sum, sample_weight=sample_weight) for (class_, warm_start_coef_) in zip(classes_, warm_start_coef)) fold_coefs_, _, n_iter_ = zip(*fold_coefs_) self.n_iter_ = np.asarray(n_iter_, dtype=np.int32)[:, 0] if self.multi_class == 'multinomial': self.coef_ = fold_coefs_[0][0] else: self.coef_ = np.asarray(fold_coefs_) self.coef_ = self.coef_.reshape(n_classes, n_features + int(self.fit_intercept)) if self.fit_intercept: self.intercept_ = self.coef_[:, -1] self.coef_ = self.coef_[:, :-1] return self def predict_proba(self, X): """Probability estimates. The returned estimates for all classes are ordered by the label of classes. For a multi_class problem, if multi_class is set to be "multinomial" the softmax function is used to find the predicted probability of each class. Else use a one-vs-rest approach, i.e calculate the probability of each class assuming it to be positive using the logistic function. and normalize these values across all the classes. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- T : array-like, shape = [n_samples, n_classes] Returns the probability of the sample for each class in the model, where classes are ordered as they are in ``self.classes_``. """ if not hasattr(self, "coef_"): raise NotFittedError("Call fit before prediction") calculate_ovr = self.coef_.shape[0] == 1 or self.multi_class == "ovr" if calculate_ovr: return super(LogisticRegression, self)._predict_proba_lr(X) else: return softmax(self.decision_function(X), copy=False) def predict_log_proba(self, X): """Log of probability estimates. The returned estimates for all classes are ordered by the label of classes. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- T : array-like, shape = [n_samples, n_classes] Returns the log-probability of the sample for each class in the model, where classes are ordered as they are in ``self.classes_``. """ return np.log(self.predict_proba(X)) class LogisticRegressionCV(LogisticRegression, BaseEstimator, LinearClassifierMixin, _LearntSelectorMixin): """Logistic Regression CV (aka logit, MaxEnt) classifier. This class implements logistic regression using liblinear, newton-cg, sag of lbfgs optimizer. The newton-cg, sag and lbfgs solvers support only L2 regularization with primal formulation. The liblinear solver supports both L1 and L2 regularization, with a dual formulation only for the L2 penalty. For the grid of Cs values (that are set by default to be ten values in a logarithmic scale between 1e-4 and 1e4), the best hyperparameter is selected by the cross-validator StratifiedKFold, but it can be changed using the cv parameter. In the case of newton-cg and lbfgs solvers, we warm start along the path i.e guess the initial coefficients of the present fit to be the coefficients got after convergence in the previous fit, so it is supposed to be faster for high-dimensional dense data. For a multiclass problem, the hyperparameters for each class are computed using the best scores got by doing a one-vs-rest in parallel across all folds and classes. Hence this is not the true multinomial loss. Read more in the :ref:`User Guide <logistic_regression>`. Parameters ---------- Cs : list of floats | int Each of the values in Cs describes the inverse of regularization strength. If Cs is as an int, then a grid of Cs values are chosen in a logarithmic scale between 1e-4 and 1e4. Like in support vector machines, smaller values specify stronger regularization. fit_intercept : bool, default: True Specifies if a constant (a.k.a. bias or intercept) should be added to the decision function. class_weight : dict or 'balanced', optional Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. cv : integer or cross-validation generator The default cross-validation generator used is Stratified K-Folds. If an integer is provided, then it is the number of folds used. See the module :mod:`sklearn.cross_validation` module for the list of possible cross-validation objects. penalty : str, 'l1' or 'l2' Used to specify the norm used in the penalization. The newton-cg and lbfgs solvers support only l2 penalties. dual : bool Dual or primal formulation. Dual formulation is only implemented for l2 penalty with liblinear solver. Prefer dual=False when n_samples > n_features. scoring : callabale Scoring function to use as cross-validation criteria. For a list of scoring functions that can be used, look at :mod:`sklearn.metrics`. The default scoring option used is accuracy_score. solver : {'newton-cg', 'lbfgs', 'liblinear', 'sag'} Algorithm to use in the optimization problem. - For small datasets, 'liblinear' is a good choice, whereas 'sag' is faster for large ones. - For multiclass problems, only 'newton-cg' and 'lbfgs' handle multinomial loss; 'sag' and 'liblinear' are limited to one-versus-rest schemes. - 'newton-cg', 'lbfgs' and 'sag' only handle L2 penalty. - 'liblinear' might be slower in LogisticRegressionCV because it does not handle warm-starting. tol : float, optional Tolerance for stopping criteria. max_iter : int, optional Maximum number of iterations of the optimization algorithm. n_jobs : int, optional Number of CPU cores used during the cross-validation loop. If given a value of -1, all cores are used. verbose : int For the 'liblinear', 'sag' and 'lbfgs' solvers set verbose to any positive number for verbosity. refit : bool If set to True, the scores are averaged across all folds, and the coefs and the C that corresponds to the best score is taken, and a final refit is done using these parameters. Otherwise the coefs, intercepts and C that correspond to the best scores across folds are averaged. multi_class : str, {'ovr', 'multinomial'} Multiclass option can be either 'ovr' or 'multinomial'. If the option chosen is 'ovr', then a binary problem is fit for each label. Else the loss minimised is the multinomial loss fit across the entire probability distribution. Works only for 'lbfgs' and 'newton-cg' solvers. intercept_scaling : float, default 1. Useful only if solver is liblinear. This parameter is useful only when the solver 'liblinear' is used and self.fit_intercept is set to True. In this case, x becomes [x, self.intercept_scaling], i.e. a "synthetic" feature with constant value equals to intercept_scaling is appended to the instance vector. The intercept becomes intercept_scaling * synthetic feature weight Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. Attributes ---------- coef_ : array, shape (1, n_features) or (n_classes, n_features) Coefficient of the features in the decision function. `coef_` is of shape (1, n_features) when the given problem is binary. `coef_` is readonly property derived from `raw_coef_` that follows the internal memory layout of liblinear. intercept_ : array, shape (1,) or (n_classes,) Intercept (a.k.a. bias) added to the decision function. It is available only when parameter intercept is set to True and is of shape(1,) when the problem is binary. Cs_ : array Array of C i.e. inverse of regularization parameter values used for cross-validation. coefs_paths_ : array, shape ``(n_folds, len(Cs_), n_features)`` or \ ``(n_folds, len(Cs_), n_features + 1)`` dict with classes as the keys, and the path of coefficients obtained during cross-validating across each fold and then across each Cs after doing an OvR for the corresponding class as values. If the 'multi_class' option is set to 'multinomial', then the coefs_paths are the coefficients corresponding to each class. Each dict value has shape ``(n_folds, len(Cs_), n_features)`` or ``(n_folds, len(Cs_), n_features + 1)`` depending on whether the intercept is fit or not. scores_ : dict dict with classes as the keys, and the values as the grid of scores obtained during cross-validating each fold, after doing an OvR for the corresponding class. If the 'multi_class' option given is 'multinomial' then the same scores are repeated across all classes, since this is the multinomial class. Each dict value has shape (n_folds, len(Cs)) C_ : array, shape (n_classes,) or (n_classes - 1,) Array of C that maps to the best scores across every class. If refit is set to False, then for each class, the best C is the average of the C's that correspond to the best scores for each fold. n_iter_ : array, shape (n_classes, n_folds, n_cs) or (1, n_folds, n_cs) Actual number of iterations for all classes, folds and Cs. In the binary or multinomial cases, the first dimension is equal to 1. See also -------- LogisticRegression """ def __init__(self, Cs=10, fit_intercept=True, cv=None, dual=False, penalty='l2', scoring=None, solver='lbfgs', tol=1e-4, max_iter=100, class_weight=None, n_jobs=1, verbose=0, refit=True, intercept_scaling=1., multi_class='ovr', random_state=None): self.Cs = Cs self.fit_intercept = fit_intercept self.cv = cv self.dual = dual self.penalty = penalty self.scoring = scoring self.tol = tol self.max_iter = max_iter self.class_weight = class_weight self.n_jobs = n_jobs self.verbose = verbose self.solver = solver self.refit = refit self.intercept_scaling = intercept_scaling self.multi_class = multi_class self.random_state = random_state def fit(self, X, y, sample_weight=None): """Fit the model according to the given training data. Parameters ---------- X : {array-like, sparse matrix}, 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,) Target vector relative to X. sample_weight : array-like, shape (n_samples,) optional Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Returns ------- self : object Returns self. """ _check_solver_option(self.solver, self.multi_class, self.penalty, self.dual, sample_weight) if not isinstance(self.max_iter, numbers.Number) or self.max_iter < 0: raise ValueError("Maximum number of iteration must be positive;" " got (max_iter=%r)" % self.max_iter) if not isinstance(self.tol, numbers.Number) or self.tol < 0: raise ValueError("Tolerance for stopping criteria must be " "positive; got (tol=%r)" % self.tol) X, y = check_X_y(X, y, accept_sparse='csr', dtype=np.float64, order="C") max_squared_sum = get_max_squared_sum(X) if self.solver == 'sag' \ else None if y.ndim == 2 and y.shape[1] == 1: warnings.warn( "A column-vector y was passed when a 1d array was" " expected. Please change the shape of y to " "(n_samples, ), for example using ravel().", DataConversionWarning) y = np.ravel(y) check_consistent_length(X, y) # init cross-validation generator cv = check_cv(self.cv, X, y, classifier=True) folds = list(cv) self._enc = LabelEncoder() self._enc.fit(y) labels = self.classes_ = np.unique(y) n_classes = len(labels) if n_classes < 2: raise ValueError("This solver needs samples of at least 2 classes" " in the data, but the data contains only one" " class: %r" % self.classes_[0]) if n_classes == 2: # OvR in case of binary problems is as good as fitting # the higher label n_classes = 1 labels = labels[1:] # We need this hack to iterate only once over labels, in the case of # multi_class = multinomial, without changing the value of the labels. iter_labels = labels if self.multi_class == 'multinomial': iter_labels = [None] if self.class_weight and not(isinstance(self.class_weight, dict) or self.class_weight in ['balanced', 'auto']): raise ValueError("class_weight provided should be a " "dict or 'balanced'") path_func = delayed(_log_reg_scoring_path) # The SAG solver releases the GIL so it's more efficient to use # threads for this solver. backend = 'threading' if self.solver == 'sag' else 'multiprocessing' fold_coefs_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend=backend)( path_func(X, y, train, test, pos_class=label, Cs=self.Cs, fit_intercept=self.fit_intercept, penalty=self.penalty, dual=self.dual, solver=self.solver, tol=self.tol, max_iter=self.max_iter, verbose=self.verbose, class_weight=self.class_weight, scoring=self.scoring, multi_class=self.multi_class, intercept_scaling=self.intercept_scaling, random_state=self.random_state, max_squared_sum=max_squared_sum, sample_weight=sample_weight ) for label in iter_labels for train, test in folds) if self.multi_class == 'multinomial': multi_coefs_paths, Cs, multi_scores, n_iter_ = zip(*fold_coefs_) multi_coefs_paths = np.asarray(multi_coefs_paths) multi_scores = np.asarray(multi_scores) # This is just to maintain API similarity between the ovr and # multinomial option. # Coefs_paths in now n_folds X len(Cs) X n_classes X n_features # we need it to be n_classes X len(Cs) X n_folds X n_features # to be similar to "ovr". coefs_paths = np.rollaxis(multi_coefs_paths, 2, 0) # Multinomial has a true score across all labels. Hence the # shape is n_folds X len(Cs). We need to repeat this score # across all labels for API similarity. scores = np.tile(multi_scores, (n_classes, 1, 1)) self.Cs_ = Cs[0] self.n_iter_ = np.reshape(n_iter_, (1, len(folds), len(self.Cs_))) else: coefs_paths, Cs, scores, n_iter_ = zip(*fold_coefs_) self.Cs_ = Cs[0] coefs_paths = np.reshape(coefs_paths, (n_classes, len(folds), len(self.Cs_), -1)) self.n_iter_ = np.reshape(n_iter_, (n_classes, len(folds), len(self.Cs_))) self.coefs_paths_ = dict(zip(labels, coefs_paths)) scores = np.reshape(scores, (n_classes, len(folds), -1)) self.scores_ = dict(zip(labels, scores)) self.C_ = list() self.coef_ = np.empty((n_classes, X.shape[1])) self.intercept_ = np.zeros(n_classes) # hack to iterate only once for multinomial case. if self.multi_class == 'multinomial': scores = multi_scores coefs_paths = multi_coefs_paths for index, label in enumerate(iter_labels): if self.multi_class == 'ovr': scores = self.scores_[label] coefs_paths = self.coefs_paths_[label] if self.refit: best_index = scores.sum(axis=0).argmax() C_ = self.Cs_[best_index] self.C_.append(C_) if self.multi_class == 'multinomial': coef_init = np.mean(coefs_paths[:, best_index, :, :], axis=0) else: coef_init = np.mean(coefs_paths[:, best_index, :], axis=0) w, _, _ = logistic_regression_path( X, y, pos_class=label, Cs=[C_], solver=self.solver, fit_intercept=self.fit_intercept, coef=coef_init, max_iter=self.max_iter, tol=self.tol, penalty=self.penalty, copy=False, class_weight=self.class_weight, multi_class=self.multi_class, verbose=max(0, self.verbose - 1), random_state=self.random_state, check_input=False, max_squared_sum=max_squared_sum, sample_weight=sample_weight) w = w[0] else: # Take the best scores across every fold and the average of all # coefficients corresponding to the best scores. best_indices = np.argmax(scores, axis=1) w = np.mean([coefs_paths[i][best_indices[i]] for i in range(len(folds))], axis=0) self.C_.append(np.mean(self.Cs_[best_indices])) if self.multi_class == 'multinomial': self.C_ = np.tile(self.C_, n_classes) self.coef_ = w[:, :X.shape[1]] if self.fit_intercept: self.intercept_ = w[:, -1] else: self.coef_[index] = w[: X.shape[1]] if self.fit_intercept: self.intercept_[index] = w[-1] self.C_ = np.asarray(self.C_) return self
bsd-3-clause
kalvdans/scipy
scipy/optimize/minpack.py
9
33126
from __future__ import division, print_function, absolute_import import warnings from . import _minpack import numpy as np from numpy import (atleast_1d, dot, take, triu, shape, eye, transpose, zeros, product, greater, array, all, where, isscalar, asarray, inf, abs, finfo, inexact, issubdtype, dtype) from scipy.linalg import svd, cholesky, solve_triangular, LinAlgError from scipy._lib._util import _asarray_validated, _lazywhere from .optimize import OptimizeResult, _check_unknown_options, OptimizeWarning from ._lsq import least_squares from ._lsq.common import make_strictly_feasible from ._lsq.least_squares import prepare_bounds error = _minpack.error __all__ = ['fsolve', 'leastsq', 'fixed_point', 'curve_fit'] def _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape=None): res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) if (output_shape is not None) and (shape(res) != output_shape): if (output_shape[0] != 1): if len(output_shape) > 1: if output_shape[1] == 1: return shape(res) msg = "%s: there is a mismatch between the input and output " \ "shape of the '%s' argument" % (checker, argname) func_name = getattr(thefunc, '__name__', None) if func_name: msg += " '%s'." % func_name else: msg += "." msg += 'Shape should be %s but it is %s.' % (output_shape, shape(res)) raise TypeError(msg) if issubdtype(res.dtype, inexact): dt = res.dtype else: dt = dtype(float) return shape(res), dt def fsolve(func, x0, args=(), fprime=None, full_output=0, col_deriv=0, xtol=1.49012e-8, maxfev=0, band=None, epsfcn=None, factor=100, diag=None): """ Find the roots of a function. Return the roots of the (non-linear) equations defined by ``func(x) = 0`` given a starting estimate. Parameters ---------- func : callable ``f(x, *args)`` A function that takes at least one (possibly vector) argument. x0 : ndarray The starting estimate for the roots of ``func(x) = 0``. args : tuple, optional Any extra arguments to `func`. fprime : callable(x), optional A function to compute the Jacobian of `func` with derivatives across the rows. By default, the Jacobian will be estimated. full_output : bool, optional If True, return optional outputs. col_deriv : bool, optional Specify whether the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation). xtol : float, optional The calculation will terminate if the relative error between two consecutive iterates is at most `xtol`. maxfev : int, optional The maximum number of calls to the function. If zero, then ``100*(N+1)`` is the maximum where N is the number of elements in `x0`. band : tuple, optional If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for ``fprime=None``). epsfcn : float, optional A suitable step length for the forward-difference approximation of the Jacobian (for ``fprime=None``). If `epsfcn` is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision. factor : float, optional A parameter determining the initial step bound (``factor * || diag * x||``). Should be in the interval ``(0.1, 100)``. diag : sequence, optional N positive entries that serve as a scale factors for the variables. Returns ------- x : ndarray The solution (or the result of the last iteration for an unsuccessful call). infodict : dict A dictionary of optional outputs with the keys: ``nfev`` number of function calls ``njev`` number of Jacobian calls ``fvec`` function evaluated at the output ``fjac`` the orthogonal matrix, q, produced by the QR factorization of the final approximate Jacobian matrix, stored column wise ``r`` upper triangular matrix produced by QR factorization of the same matrix ``qtf`` the vector ``(transpose(q) * fvec)`` ier : int An integer flag. Set to 1 if a solution was found, otherwise refer to `mesg` for more information. mesg : str If no solution is found, `mesg` details the cause of failure. See Also -------- root : Interface to root finding algorithms for multivariate functions. See the 'hybr' `method` in particular. Notes ----- ``fsolve`` is a wrapper around MINPACK's hybrd and hybrj algorithms. """ options = {'col_deriv': col_deriv, 'xtol': xtol, 'maxfev': maxfev, 'band': band, 'eps': epsfcn, 'factor': factor, 'diag': diag} res = _root_hybr(func, x0, args, jac=fprime, **options) if full_output: x = res['x'] info = dict((k, res.get(k)) for k in ('nfev', 'njev', 'fjac', 'r', 'qtf') if k in res) info['fvec'] = res['fun'] return x, info, res['status'], res['message'] else: status = res['status'] msg = res['message'] if status == 0: raise TypeError(msg) elif status == 1: pass elif status in [2, 3, 4, 5]: warnings.warn(msg, RuntimeWarning) else: raise TypeError(msg) return res['x'] def _root_hybr(func, x0, args=(), jac=None, col_deriv=0, xtol=1.49012e-08, maxfev=0, band=None, eps=None, factor=100, diag=None, **unknown_options): """ Find the roots of a multivariate function using MINPACK's hybrd and hybrj routines (modified Powell method). Options ------- col_deriv : bool Specify whether the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation). xtol : float The calculation will terminate if the relative error between two consecutive iterates is at most `xtol`. maxfev : int The maximum number of calls to the function. If zero, then ``100*(N+1)`` is the maximum where N is the number of elements in `x0`. band : tuple If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for ``fprime=None``). eps : float A suitable step length for the forward-difference approximation of the Jacobian (for ``fprime=None``). If `eps` is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision. factor : float A parameter determining the initial step bound (``factor * || diag * x||``). Should be in the interval ``(0.1, 100)``. diag : sequence N positive entries that serve as a scale factors for the variables. """ _check_unknown_options(unknown_options) epsfcn = eps x0 = asarray(x0).flatten() n = len(x0) if not isinstance(args, tuple): args = (args,) shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,)) if epsfcn is None: epsfcn = finfo(dtype).eps Dfun = jac if Dfun is None: if band is None: ml, mu = -10, -10 else: ml, mu = band[:2] if maxfev == 0: maxfev = 200 * (n + 1) retval = _minpack._hybrd(func, x0, args, 1, xtol, maxfev, ml, mu, epsfcn, factor, diag) else: _check_func('fsolve', 'fprime', Dfun, x0, args, n, (n, n)) if (maxfev == 0): maxfev = 100 * (n + 1) retval = _minpack._hybrj(func, Dfun, x0, args, 1, col_deriv, xtol, maxfev, factor, diag) x, status = retval[0], retval[-1] errors = {0: "Improper input parameters were entered.", 1: "The solution converged.", 2: "The number of calls to function has " "reached maxfev = %d." % maxfev, 3: "xtol=%f is too small, no further improvement " "in the approximate\n solution " "is possible." % xtol, 4: "The iteration is not making good progress, as measured " "by the \n improvement from the last five " "Jacobian evaluations.", 5: "The iteration is not making good progress, " "as measured by the \n improvement from the last " "ten iterations.", 'unknown': "An error occurred."} info = retval[1] info['fun'] = info.pop('fvec') sol = OptimizeResult(x=x, success=(status == 1), status=status) sol.update(info) try: sol['message'] = errors[status] except KeyError: info['message'] = errors['unknown'] return sol def leastsq(func, x0, args=(), Dfun=None, full_output=0, col_deriv=0, ftol=1.49012e-8, xtol=1.49012e-8, gtol=0.0, maxfev=0, epsfcn=None, factor=100, diag=None): """ Minimize the sum of squares of a set of equations. :: x = arg min(sum(func(y)**2,axis=0)) y Parameters ---------- func : callable should take at least one (possibly length N vector) argument and returns M floating point numbers. It must not return NaNs or fitting might fail. x0 : ndarray The starting estimate for the minimization. args : tuple, optional Any extra arguments to func are placed in this tuple. Dfun : callable, optional A function or method to compute the Jacobian of func with derivatives across the rows. If this is None, the Jacobian will be estimated. full_output : bool, optional non-zero to return all optional outputs. col_deriv : bool, optional non-zero to specify that the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation). ftol : float, optional Relative error desired in the sum of squares. xtol : float, optional Relative error desired in the approximate solution. gtol : float, optional Orthogonality desired between the function vector and the columns of the Jacobian. maxfev : int, optional The maximum number of calls to the function. If `Dfun` is provided then the default `maxfev` is 100*(N+1) where N is the number of elements in x0, otherwise the default `maxfev` is 200*(N+1). epsfcn : float, optional A variable used in determining a suitable step length for the forward- difference approximation of the Jacobian (for Dfun=None). Normally the actual step length will be sqrt(epsfcn)*x If epsfcn is less than the machine precision, it is assumed that the relative errors are of the order of the machine precision. factor : float, optional A parameter determining the initial step bound (``factor * || diag * x||``). Should be in interval ``(0.1, 100)``. diag : sequence, optional N positive entries that serve as a scale factors for the variables. Returns ------- x : ndarray The solution (or the result of the last iteration for an unsuccessful call). cov_x : ndarray Uses the fjac and ipvt optional outputs to construct an estimate of the jacobian around the solution. None if a singular matrix encountered (indicates very flat curvature in some direction). This matrix must be multiplied by the residual variance to get the covariance of the parameter estimates -- see curve_fit. infodict : dict a dictionary of optional outputs with the key s: ``nfev`` The number of function calls ``fvec`` The function evaluated at the output ``fjac`` A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated. ``ipvt`` An integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix. ``qtf`` The vector (transpose(q) * fvec). mesg : str A string message giving information about the cause of failure. ier : int An integer flag. If it is equal to 1, 2, 3 or 4, the solution was found. Otherwise, the solution was not found. In either case, the optional output variable 'mesg' gives more information. Notes ----- "leastsq" is a wrapper around MINPACK's lmdif and lmder algorithms. cov_x is a Jacobian approximation to the Hessian of the least squares objective function. This approximation assumes that the objective function is based on the difference between some observed target data (ydata) and a (non-linear) function of the parameters `f(xdata, params)` :: func(params) = ydata - f(xdata, params) so that the objective function is :: min sum((ydata - f(xdata, params))**2, axis=0) params """ x0 = asarray(x0).flatten() n = len(x0) if not isinstance(args, tuple): args = (args,) shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) m = shape[0] if n > m: raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m)) if epsfcn is None: epsfcn = finfo(dtype).eps if Dfun is None: if maxfev == 0: maxfev = 200*(n + 1) retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) else: if col_deriv: _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (n, m)) else: _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (m, n)) if maxfev == 0: maxfev = 100 * (n + 1) retval = _minpack._lmder(func, Dfun, x0, args, full_output, col_deriv, ftol, xtol, gtol, maxfev, factor, diag) errors = {0: ["Improper input parameters.", TypeError], 1: ["Both actual and predicted relative reductions " "in the sum of squares\n are at most %f" % ftol, None], 2: ["The relative error between two consecutive " "iterates is at most %f" % xtol, None], 3: ["Both actual and predicted relative reductions in " "the sum of squares\n are at most %f and the " "relative error between two consecutive " "iterates is at \n most %f" % (ftol, xtol), None], 4: ["The cosine of the angle between func(x) and any " "column of the\n Jacobian is at most %f in " "absolute value" % gtol, None], 5: ["Number of calls to function has reached " "maxfev = %d." % maxfev, ValueError], 6: ["ftol=%f is too small, no further reduction " "in the sum of squares\n is possible.""" % ftol, ValueError], 7: ["xtol=%f is too small, no further improvement in " "the approximate\n solution is possible." % xtol, ValueError], 8: ["gtol=%f is too small, func(x) is orthogonal to the " "columns of\n the Jacobian to machine " "precision." % gtol, ValueError], 'unknown': ["Unknown error.", TypeError]} info = retval[-1] # The FORTRAN return value if info not in [1, 2, 3, 4] and not full_output: if info in [5, 6, 7, 8]: warnings.warn(errors[info][0], RuntimeWarning) else: try: raise errors[info][1](errors[info][0]) except KeyError: raise errors['unknown'][1](errors['unknown'][0]) mesg = errors[info][0] if full_output: cov_x = None if info in [1, 2, 3, 4]: from numpy.dual import inv perm = take(eye(n), retval[1]['ipvt'] - 1, 0) r = triu(transpose(retval[1]['fjac'])[:n, :]) R = dot(r, perm) try: cov_x = inv(dot(transpose(R), R)) except (LinAlgError, ValueError): pass return (retval[0], cov_x) + retval[1:-1] + (mesg, info) else: return (retval[0], info) def _wrap_func(func, xdata, ydata, transform): if transform is None: def func_wrapped(params): return func(xdata, *params) - ydata elif transform.ndim == 1: def func_wrapped(params): return transform * (func(xdata, *params) - ydata) else: # Chisq = (y - yd)^T C^{-1} (y-yd) # transform = L such that C = L L^T # C^{-1} = L^{-T} L^{-1} # Chisq = (y - yd)^T L^{-T} L^{-1} (y-yd) # Define (y-yd)' = L^{-1} (y-yd) # by solving # L (y-yd)' = (y-yd) # and minimize (y-yd)'^T (y-yd)' def func_wrapped(params): return solve_triangular(transform, func(xdata, *params) - ydata, lower=True) return func_wrapped def _wrap_jac(jac, xdata, transform): if transform is None: def jac_wrapped(params): return jac(xdata, *params) elif transform.ndim == 1: def jac_wrapped(params): return transform[:, np.newaxis] * np.asarray(jac(xdata, *params)) else: def jac_wrapped(params): return solve_triangular(transform, np.asarray(jac(xdata, *params)), lower=True) return jac_wrapped def _initialize_feasible(lb, ub): p0 = np.ones_like(lb) lb_finite = np.isfinite(lb) ub_finite = np.isfinite(ub) mask = lb_finite & ub_finite p0[mask] = 0.5 * (lb[mask] + ub[mask]) mask = lb_finite & ~ub_finite p0[mask] = lb[mask] + 1 mask = ~lb_finite & ub_finite p0[mask] = ub[mask] - 1 return p0 def curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(-np.inf, np.inf), method=None, jac=None, **kwargs): """ Use non-linear least squares to fit a function, f, to data. Assumes ``ydata = f(xdata, *params) + eps`` Parameters ---------- f : callable The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments. xdata : An M-length sequence or an (k,M)-shaped array for functions with k predictors The independent variable where the data is measured. ydata : M-length sequence The dependent data --- nominally f(xdata, ...) p0 : None, scalar, or N-length sequence, optional Initial guess for the parameters. If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised). sigma : None or M-length sequence or MxM array, optional Determines the uncertainty in `ydata`. If we define residuals as ``r = ydata - f(xdata, *popt)``, then the interpretation of `sigma` depends on its number of dimensions: - A 1-d `sigma` should contain values of standard deviations of errors in `ydata`. In this case, the optimized function is ``chisq = sum((r / sigma) ** 2)``. - A 2-d `sigma` should contain the covariance matrix of errors in `ydata`. In this case, the optimized function is ``chisq = r.T @ inv(sigma) @ r``. .. versionadded:: 0.19 None (default) is equivalent of 1-d `sigma` filled with ones. absolute_sigma : bool, optional If True, `sigma` is used in an absolute sense and the estimated parameter covariance `pcov` reflects these absolute values. If False, only the relative magnitudes of the `sigma` values matter. The returned parameter covariance matrix `pcov` is based on scaling `sigma` by a constant factor. This constant is set by demanding that the reduced `chisq` for the optimal parameters `popt` when using the *scaled* `sigma` equals unity. In other words, `sigma` is scaled to match the sample variance of the residuals after the fit. Mathematically, ``pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(M-N)`` check_finite : bool, optional If True, check that the input arrays do not contain nans of infs, and raise a ValueError if they do. Setting this parameter to False may silently produce nonsensical results if the input arrays do contain nans. Default is True. bounds : 2-tuple of array_like, optional Lower and upper bounds on independent variables. Defaults to no bounds. Each element of the tuple must be either an array with the length equal to the number of parameters, or a scalar (in which case the bound is taken to be the same for all parameters.) Use ``np.inf`` with an appropriate sign to disable bounds on all or some parameters. .. versionadded:: 0.17 method : {'lm', 'trf', 'dogbox'}, optional Method to use for optimization. See `least_squares` for more details. Default is 'lm' for unconstrained problems and 'trf' if `bounds` are provided. The method 'lm' won't work when the number of observations is less than the number of variables, use 'trf' or 'dogbox' in this case. .. versionadded:: 0.17 jac : callable, string or None, optional Function with signature ``jac(x, ...)`` which computes the Jacobian matrix of the model function with respect to parameters as a dense array_like structure. It will be scaled according to provided `sigma`. If None (default), the Jacobian will be estimated numerically. String keywords for 'trf' and 'dogbox' methods can be used to select a finite difference scheme, see `least_squares`. .. versionadded:: 0.18 kwargs Keyword arguments passed to `leastsq` for ``method='lm'`` or `least_squares` otherwise. Returns ------- popt : array Optimal values for the parameters so that the sum of the squared residuals of ``f(xdata, *popt) - ydata`` is minimized pcov : 2d array The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use ``perr = np.sqrt(np.diag(pcov))``. How the `sigma` parameter affects the estimated covariance depends on `absolute_sigma` argument, as described above. If the Jacobian matrix at the solution doesn't have a full rank, then 'lm' method returns a matrix filled with ``np.inf``, on the other hand 'trf' and 'dogbox' methods use Moore-Penrose pseudoinverse to compute the covariance matrix. Raises ------ ValueError if either `ydata` or `xdata` contain NaNs, or if incompatible options are used. RuntimeError if the least-squares minimization fails. OptimizeWarning if covariance of the parameters can not be estimated. See Also -------- least_squares : Minimize the sum of squares of nonlinear functions. scipy.stats.linregress : Calculate a linear least squares regression for two sets of measurements. Notes ----- With ``method='lm'``, the algorithm uses the Levenberg-Marquardt algorithm through `leastsq`. Note that this algorithm can only deal with unconstrained problems. Box constraints can be handled by methods 'trf' and 'dogbox'. Refer to the docstring of `least_squares` for more information. Examples -------- >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from scipy.optimize import curve_fit >>> def func(x, a, b, c): ... return a * np.exp(-b * x) + c define the data to be fit with some noise >>> xdata = np.linspace(0, 4, 50) >>> y = func(xdata, 2.5, 1.3, 0.5) >>> y_noise = 0.2 * np.random.normal(size=xdata.size) >>> ydata = y + y_noise >>> plt.plot(xdata, ydata, 'b-', label='data') Fit for the parameters a, b, c of the function `func` >>> popt, pcov = curve_fit(func, xdata, ydata) >>> plt.plot(xdata, func(xdata, *popt), 'r-', label='fit') Constrain the optimization to the region of ``0 < a < 3``, ``0 < b < 2`` and ``0 < c < 1``: >>> popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 2., 1.])) >>> plt.plot(xdata, func(xdata, *popt), 'g--', label='fit-with-bounds') >>> plt.xlabel('x') >>> plt.ylabel('y') >>> plt.legend() >>> plt.show() """ if p0 is None: # determine number of parameters by inspecting the function from scipy._lib._util import getargspec_no_self as _getargspec args, varargs, varkw, defaults = _getargspec(f) if len(args) < 2: raise ValueError("Unable to determine number of fit parameters.") n = len(args) - 1 else: p0 = np.atleast_1d(p0) n = p0.size lb, ub = prepare_bounds(bounds, n) if p0 is None: p0 = _initialize_feasible(lb, ub) bounded_problem = np.any((lb > -np.inf) | (ub < np.inf)) if method is None: if bounded_problem: method = 'trf' else: method = 'lm' if method == 'lm' and bounded_problem: raise ValueError("Method 'lm' only works for unconstrained problems. " "Use 'trf' or 'dogbox' instead.") # NaNs can not be handled if check_finite: ydata = np.asarray_chkfinite(ydata) else: ydata = np.asarray(ydata) if isinstance(xdata, (list, tuple, np.ndarray)): # `xdata` is passed straight to the user-defined `f`, so allow # non-array_like `xdata`. if check_finite: xdata = np.asarray_chkfinite(xdata) else: xdata = np.asarray(xdata) # Determine type of sigma if sigma is not None: sigma = np.asarray(sigma) # if 1-d, sigma are errors, define transform = 1/sigma if sigma.shape == (ydata.size, ): transform = 1.0 / sigma # if 2-d, sigma is the covariance matrix, # define transform = L such that L L^T = C elif sigma.shape == (ydata.size, ydata.size): try: # scipy.linalg.cholesky requires lower=True to return L L^T = A transform = cholesky(sigma, lower=True) except LinAlgError: raise ValueError("`sigma` must be positive definite.") else: raise ValueError("`sigma` has incorrect shape.") else: transform = None func = _wrap_func(f, xdata, ydata, transform) if callable(jac): jac = _wrap_jac(jac, xdata, transform) elif jac is None and method != 'lm': jac = '2-point' if method == 'lm': # Remove full_output from kwargs, otherwise we're passing it in twice. return_full = kwargs.pop('full_output', False) res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs) popt, pcov, infodict, errmsg, ier = res cost = np.sum(infodict['fvec'] ** 2) if ier not in [1, 2, 3, 4]: raise RuntimeError("Optimal parameters not found: " + errmsg) else: # Rename maxfev (leastsq) to max_nfev (least_squares), if specified. if 'max_nfev' not in kwargs: kwargs['max_nfev'] = kwargs.pop('maxfev', None) res = least_squares(func, p0, jac=jac, bounds=bounds, method=method, **kwargs) if not res.success: raise RuntimeError("Optimal parameters not found: " + res.message) cost = 2 * res.cost # res.cost is half sum of squares! popt = res.x # Do Moore-Penrose inverse discarding zero singular values. _, s, VT = svd(res.jac, full_matrices=False) threshold = np.finfo(float).eps * max(res.jac.shape) * s[0] s = s[s > threshold] VT = VT[:s.size] pcov = np.dot(VT.T / s**2, VT) return_full = False warn_cov = False if pcov is None: # indeterminate covariance pcov = zeros((len(popt), len(popt)), dtype=float) pcov.fill(inf) warn_cov = True elif not absolute_sigma: if ydata.size > p0.size: s_sq = cost / (ydata.size - p0.size) pcov = pcov * s_sq else: pcov.fill(inf) warn_cov = True if warn_cov: warnings.warn('Covariance of the parameters could not be estimated', category=OptimizeWarning) if return_full: return popt, pcov, infodict, errmsg, ier else: return popt, pcov def check_gradient(fcn, Dfcn, x0, args=(), col_deriv=0): """Perform a simple check on the gradient for correctness. """ x = atleast_1d(x0) n = len(x) x = x.reshape((n,)) fvec = atleast_1d(fcn(x, *args)) m = len(fvec) fvec = fvec.reshape((m,)) ldfjac = m fjac = atleast_1d(Dfcn(x, *args)) fjac = fjac.reshape((m, n)) if col_deriv == 0: fjac = transpose(fjac) xp = zeros((n,), float) err = zeros((m,), float) fvecp = None _minpack._chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, 1, err) fvecp = atleast_1d(fcn(xp, *args)) fvecp = fvecp.reshape((m,)) _minpack._chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, 2, err) good = (product(greater(err, 0.5), axis=0)) return (good, err) def _del2(p0, p1, d): return p0 - np.square(p1 - p0) / d def _relerr(actual, desired): return (actual - desired) / desired def _fixed_point_helper(func, x0, args, xtol, maxiter, use_accel): p0 = x0 for i in range(maxiter): p1 = func(p0, *args) if use_accel: p2 = func(p1, *args) d = p2 - 2.0 * p1 + p0 p = _lazywhere(d != 0, (p0, p1, d), f=_del2, fillvalue=p2) else: p = p1 relerr = _lazywhere(p0 != 0, (p, p0), f=_relerr, fillvalue=p) if np.all(np.abs(relerr) < xtol): return p p0 = p msg = "Failed to converge after %d iterations, value is %s" % (maxiter, p) raise RuntimeError(msg) def fixed_point(func, x0, args=(), xtol=1e-8, maxiter=500, method='del2'): """ Find a fixed point of the function. Given a function of one or more variables and a starting point, find a fixed-point of the function: i.e. where ``func(x0) == x0``. Parameters ---------- func : function Function to evaluate. x0 : array_like Fixed point of function. args : tuple, optional Extra arguments to `func`. xtol : float, optional Convergence tolerance, defaults to 1e-08. maxiter : int, optional Maximum number of iterations, defaults to 500. method : {"del2", "iteration"}, optional Method of finding the fixed-point, defaults to "del2" which uses Steffensen's Method with Aitken's ``Del^2`` convergence acceleration [1]_. The "iteration" method simply iterates the function until convergence is detected, without attempting to accelerate the convergence. References ---------- .. [1] Burden, Faires, "Numerical Analysis", 5th edition, pg. 80 Examples -------- >>> from scipy import optimize >>> def func(x, c1, c2): ... return np.sqrt(c1/(x+c2)) >>> c1 = np.array([10,12.]) >>> c2 = np.array([3, 5.]) >>> optimize.fixed_point(func, [1.2, 1.3], args=(c1,c2)) array([ 1.4920333 , 1.37228132]) """ use_accel = {'del2': True, 'iteration': False}[method] x0 = _asarray_validated(x0, as_inexact=True) return _fixed_point_helper(func, x0, args, xtol, maxiter, use_accel)
bsd-3-clause
aewhatley/scikit-learn
examples/gaussian_process/gp_diabetes_dataset.py
223
1976
#!/usr/bin/python # -*- coding: utf-8 -*- """ ======================================================================== Gaussian Processes regression: goodness-of-fit on the 'diabetes' dataset ======================================================================== In this example, we fit a Gaussian Process model onto the diabetes dataset. We determine the correlation parameters with maximum likelihood estimation (MLE). We use an anisotropic squared exponential correlation model with a constant regression model. We also use a nugget of 1e-2 to account for the (strong) noise in the targets. We compute a cross-validation estimate of the coefficient of determination (R2) without reperforming MLE, using the set of correlation parameters found on the whole dataset. """ print(__doc__) # Author: Vincent Dubourg <[email protected]> # Licence: BSD 3 clause from sklearn import datasets from sklearn.gaussian_process import GaussianProcess from sklearn.cross_validation import cross_val_score, KFold # Load the dataset from scikit's data sets diabetes = datasets.load_diabetes() X, y = diabetes.data, diabetes.target # Instanciate a GP model gp = GaussianProcess(regr='constant', corr='absolute_exponential', theta0=[1e-4] * 10, thetaL=[1e-12] * 10, thetaU=[1e-2] * 10, nugget=1e-2, optimizer='Welch') # Fit the GP model to the data performing maximum likelihood estimation gp.fit(X, y) # Deactivate maximum likelihood estimation for the cross-validation loop gp.theta0 = gp.theta_ # Given correlation parameter = MLE gp.thetaL, gp.thetaU = None, None # None bounds deactivate MLE # Perform a cross-validation estimate of the coefficient of determination using # the cross_validation module using all CPUs available on the machine K = 20 # folds R2 = cross_val_score(gp, X, y=y, cv=KFold(y.size, K), n_jobs=1).mean() print("The %d-Folds estimate of the coefficient of determination is R2 = %s" % (K, R2))
bsd-3-clause
joshbohde/scikit-learn
sklearn/manifold/isomap.py
2
6453
"""Isomap for manifold learning""" # Author: Jake Vanderplas -- <[email protected]> # License: BSD, (C) 2011 import numpy as np from ..base import BaseEstimator from ..neighbors import BallTree, kneighbors_graph from ..utils.graph_shortest_path import graph_shortest_path from ..decomposition import KernelPCA from ..preprocessing import KernelCenterer class Isomap(BaseEstimator): """Isomap Embedding Non-linear dimensionality reduction through Isometric Mapping Parameters ---------- n_neighbors : integer number of neighbors to consider for each point. out_dim : integer number of coordinates for the manifold eigen_solver : ['auto'|'arpack'|'dense'] 'auto' : attempt to choose the most efficient solver for the given problem. 'arpack' : use Arnoldi decomposition to find the eigenvalues and eigenvectors. Note that arpack can handle both dense and sparse data efficiently 'dense' : use a direct solver (i.e. LAPACK) for the eigenvalue decomposition. tol : float convergence tolerance passed to arpack or lobpcg. not used if eigen_solver == 'dense' max_iter : integer maximum number of iterations for the arpack solver. not used if eigen_solver == 'dense' path_method : string ['auto'|'FW'|'D'] method to use in finding shortest path. 'auto' : attempt to choose the best algorithm automatically 'FW' : Floyd-Warshall algorithm 'D' : Dijkstra algorithm with Fibonacci Heaps Attributes ---------- `embedding_` : array-like, shape (n_samples, out_dim) Stores the embedding vectors `kernel_pca_` : `KernelPCA` object used to implement the embedding `training_data_` : array-like, shape (n_samples, n_features) Stores the training data `ball_tree_` : sklearn.neighbors.BallTree instance Stores ball tree of training data for faster transform `dist_matrix_` : array-like, shape (n_samples, n_samples) Stores the geodesic distance matrix of training data References ---------- [1] Tenenbaum, J.B.; De Silva, V.; & Langford, J.C. A global geometric framework for nonlinear dimensionality reduction. Science 290 (5500) """ def __init__(self, n_neighbors=5, out_dim=2, eigen_solver='auto', tol=0, max_iter=None, path_method='auto'): self.n_neighbors = n_neighbors self.out_dim = out_dim self.eigen_solver = eigen_solver self.tol = tol self.max_iter = max_iter self.path_method = path_method def _fit_transform(self, X): self.training_data_ = X self.kernel_pca_ = KernelPCA(n_components=self.out_dim, kernel="precomputed", eigen_solver=self.eigen_solver, tol=self.tol, max_iter=self.max_iter) self.ball_tree_ = BallTree(X) kng = kneighbors_graph(self.ball_tree_, self.n_neighbors, mode='distance') self.dist_matrix_ = graph_shortest_path(kng, method=self.path_method, directed=False) G = self.dist_matrix_ ** 2 G *= -0.5 self.embedding_ = self.kernel_pca_.fit_transform(G) def reconstruction_error(self): """Compute the reconstruction error for the embedding. Returns ------- reconstruction_error : float Details ------- The cost function of an isomap embedding is E = frobenius_norm[K(D) - K(D_fit)] / n_samples Where D is the matrix of distances for the input data X, D_fit is the matrix of distances for the output embedding X_fit, and K is the isomap kernel: K(D) = -0.5 * (I - 1/n_samples) * D^2 * (I - 1/n_samples) """ G = -0.5 * self.dist_matrix_ ** 2 G_center = KernelCenterer().fit_transform(G) evals = self.kernel_pca_.lambdas_ return np.sqrt(np.sum(G_center ** 2) - np.sum(evals ** 2)) / G.shape[0] def fit(self, X, y=None): """Compute the embedding vectors for data X Parameters ---------- X : array-like of shape (n_samples, n_features) training set. Returns ------- self : returns an instance of self. """ self._fit_transform(X) return self def fit_transform(self, X, y=None): """Fit the model from data in X and transform X. 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. Returns ------- X_new: array-like, shape (n_samples, out_dim) """ self._fit_transform(X) return self.embedding_ def transform(self, X): """Transform X. This is implemented by linking the points X into the graph of geodesic distances of the training data. First the `n_neighbors` nearest neighbors of X are found in the training data, and from these the shortest geodesic distances from each point in X to each point in the training data are computed in order to construct the kernel. The embedding of X is the projection of this kernel onto the embedding vectors of the training set. Parameters ---------- X: array-like, shape (n_samples, n_features) Returns ------- X_new: array-like, shape (n_samples, out_dim) """ distances, indices = self.ball_tree_.query(X, return_distance=True) #Create the graph of shortest distances from X to self.training_data_ # via the nearest neighbors of X. #This can be done as a single array operation, but it potentially # takes a lot of memory. To avoid that, use a loop: G_X = np.zeros((X.shape[0], self.training_data_.shape[0])) for i in range(X.shape[0]): G_X[i] = np.min((self.dist_matrix_[indices[i]] + distances[i][:, None]), 0) G_X **= 2 G_X *= -0.5 return self.kernel_pca_.transform(G_X)
bsd-3-clause
mne-tools/mne-tools.github.io
0.13/_downloads/plot_visualize_epochs.py
4
3018
""" .. _tut_viz_epochs: Visualize Epochs data ===================== """ import os.path as op import mne data_path = op.join(mne.datasets.sample.data_path(), 'MEG', 'sample') raw = mne.io.read_raw_fif(op.join(data_path, 'sample_audvis_raw.fif'), add_eeg_ref=False) raw.set_eeg_reference() # set EEG average reference events = mne.read_events(op.join(data_path, 'sample_audvis_raw-eve.fif')) picks = mne.pick_types(raw.info, meg='grad') epochs = mne.Epochs(raw, events, [1, 2], picks=picks, add_eeg_ref=False) ############################################################################### # This tutorial focuses on visualization of epoched data. All of the functions # introduced here are basically high level matplotlib functions with built in # intelligence to work with epoched data. All the methods return a handle to # matplotlib figure instance. # # All plotting functions start with ``plot``. Let's start with the most # obvious. :func:`mne.Epochs.plot` offers an interactive browser that allows # rejection by hand when called in combination with a keyword ``block=True``. # This blocks the execution of the script until the browser window is closed. epochs.plot(block=True) ############################################################################### # The numbers at the top refer to the event id of the epoch. We only have # events with id numbers of 1 and 2 since we included only those when # constructing the epochs. # # Since we did no artifact correction or rejection, there are epochs # contaminated with blinks and saccades. For instance, epoch number 9 (see # numbering at the bottom) seems to be contaminated by a blink (scroll to the # bottom to view the EOG channel). This epoch can be marked for rejection by # clicking on top of the browser window. The epoch should turn red when you # click it. This means that it will be dropped as the browser window is closed. # You should check out `help` at the lower left corner of the window for more # information about the interactive features. # # To plot individual channels as an image, where you see all the epochs at one # glance, you can use function :func:`mne.Epochs.plot_image`. It shows the # amplitude of the signal over all the epochs plus an average of the # activation. We explicitly set interactive colorbar on (it is also on by # default for plotting functions with a colorbar except the topo plots). In # interactive mode you can scale and change the colormap with mouse scroll and # up/down arrow keys. You can also drag the colorbar with left/right mouse # button. Hitting space bar resets the scale. epochs.plot_image(97, cmap='interactive') # You also have functions for plotting channelwise information arranged into a # shape of the channel array. The image plotting uses automatic scaling by # default, but noisy channels and different channel types can cause the scaling # to be a bit off. Here we define the limits by hand. epochs.plot_topo_image(vmin=-200, vmax=200, title='ERF images')
bsd-3-clause
DSLituiev/scikit-learn
sklearn/preprocessing/__init__.py
268
1319
""" The :mod:`sklearn.preprocessing` module includes scaling, centering, normalization, binarization and imputation methods. """ from ._function_transformer import FunctionTransformer from .data import Binarizer from .data import KernelCenterer from .data import MinMaxScaler from .data import MaxAbsScaler from .data import Normalizer from .data import RobustScaler from .data import StandardScaler from .data import add_dummy_feature from .data import binarize from .data import normalize from .data import scale from .data import robust_scale from .data import maxabs_scale from .data import minmax_scale from .data import OneHotEncoder from .data import PolynomialFeatures from .label import label_binarize from .label import LabelBinarizer from .label import LabelEncoder from .label import MultiLabelBinarizer from .imputation import Imputer __all__ = [ 'Binarizer', 'FunctionTransformer', 'Imputer', 'KernelCenterer', 'LabelBinarizer', 'LabelEncoder', 'MultiLabelBinarizer', 'MinMaxScaler', 'MaxAbsScaler', 'Normalizer', 'OneHotEncoder', 'RobustScaler', 'StandardScaler', 'add_dummy_feature', 'PolynomialFeatures', 'binarize', 'normalize', 'scale', 'robust_scale', 'maxabs_scale', 'minmax_scale', 'label_binarize', ]
bsd-3-clause
sillvan/hyperspy
setup.py
1
8023
# -*- coding: utf-8 -*- # Copyright 2007-2011 The HyperSpy developers # # This file is part of HyperSpy. # # HyperSpy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # HyperSpy 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 HyperSpy. If not, see <http://www.gnu.org/licenses/>. from distutils.core import setup import distutils.dir_util import os import subprocess import sys import fileinput import hyperspy.Release as Release # clean the build directory so we aren't mixing Windows and Linux # installations carelessly. if os.path.exists('build'): distutils.dir_util.remove_tree('build') install_req = ['scipy', 'ipython', 'matplotlib (>= 1.2)', 'numpy', 'traits', 'traitsui', ] def are_we_building4windows(): for arg in sys.argv: if 'wininst' in arg: return True scripts = ['bin/hyperspy', ] if are_we_building4windows() or os.name in ['nt', 'dos']: # In the Windows command prompt we can't execute Python scripts # without a .py extension. A solution is to create batch files # that runs the different scripts. # (code adapted from scitools) scripts.extend(('bin/win_post_installation.py', 'bin/install_hyperspy_here.py', 'bin/uninstall_hyperspy_here.py')) batch_files = [] for script in scripts: batch_file = os.path.splitext(script)[0] + '.bat' f = open(batch_file, "w") f.write('set path=%~dp0;%~dp0\..\;%PATH%\n') f.write('python "%%~dp0\%s" %%*\n' % os.path.split(script)[1]) f.close() batch_files.append(batch_file) if script in ('bin/hyperspy'): for env in ('qtconsole', 'notebook'): batch_file = os.path.splitext(script)[0] + '_%s' % env + '.bat' f = open(batch_file, "w") f.write('set path=%~dp0;%~dp0\..\;%PATH%\n') f.write('cd %1\n') if env == "qtconsole": f.write('start pythonw "%%~dp0\%s " %s \n' % ( os.path.split(script)[1], env)) else: f.write('python "%%~dp0\%s" %s \n' % (os.path.split(script)[1], env)) batch_files.append(batch_file) scripts.extend(batch_files) class update_version_when_dev: def __enter__(self): self.release_version = Release.version # Get the hash from the git repository if available self.restore_version = False git_master_path = ".git/refs/heads/master" if "+dev" in self.release_version and \ os.path.isfile(git_master_path): try: p = subprocess.Popen(["git", "describe", "--tags", "--dirty", "--always"], stdout=subprocess.PIPE) stdout = p.communicate()[0] if p.returncode != 0: raise EnvironmentError else: version = stdout[1:].strip() if str(self.release_version[:-4] + '-') in version: version = version.replace( self.release_version[:-4] + '-', self.release_version[:-4] + '+git') self.version = version except EnvironmentError: # Git is not available, but the .git directory exists # Therefore we can get just the master hash with open(git_master_path) as f: masterhash = f.readline() self.version = self.release_version.replace( "+dev", "+git-%s" % masterhash[:7]) for line in fileinput.FileInput("hyperspy/Release.py", inplace=1): if line.startswith('version = '): print "version = \"%s\"" % self.version else: print line, self.restore_version = True else: self.version = self.release_version return self.version def __exit__(self, type, value, traceback): if self.restore_version is True: for line in fileinput.FileInput("hyperspy/Release.py", inplace=1): if line.startswith('version = '): print "version = \"%s\"" % self.release_version else: print line, with update_version_when_dev() as version: setup( name="hyperspy", package_dir={'hyperspy': 'hyperspy'}, version=version, packages=['hyperspy', 'hyperspy._components', 'hyperspy.io_plugins', 'hyperspy.drawing', 'hyperspy.drawing._markers', 'hyperspy.learn', 'hyperspy._signals', 'hyperspy.gui', 'hyperspy.utils', 'hyperspy.tests', 'hyperspy.tests.axes', 'hyperspy.tests.component', 'hyperspy.tests.drawing', 'hyperspy.tests.io', 'hyperspy.tests.model', 'hyperspy.tests.mva', 'hyperspy.tests.signal', 'hyperspy.tests.utils', 'hyperspy.models', 'hyperspy.misc', 'hyperspy.misc.eels', 'hyperspy.misc.eds', 'hyperspy.misc.io', 'hyperspy.misc.machine_learning', 'hyperspy.misc.mpfit', 'hyperspy.misc.mpfit.tests', 'hyperspy.misc.borrowed', 'hyperspy.misc.borrowed.astroML', ], requires=install_req, scripts=scripts, package_data= { 'hyperspy': ['bin/*.py', 'ipython_profile/*', 'data/*.ico', 'misc/eds/example_signals/*.hdf5', 'tests/io/dm3_1D_data/*.dm3', 'tests/io/dm3_2D_data/*.dm3', 'tests/io/dm3_3D_data/*.dm3', 'tests/io/dm4_1D_data/*.dm4', 'tests/io/dm4_2D_data/*.dm4', 'tests/io/dm4_3D_data/*.dm4', 'tests/io/msa_files/*.msa', 'tests/io/hdf5_files/*.hdf5', 'tests/io/tiff_files/*.tif', 'tests/io/npy_files/*.npy', 'tests/drawing/*.ipynb', ], }, author=Release.authors['all'][0], author_email=Release.authors['all'][1], maintainer='Francisco de la Peña', maintainer_email='[email protected]', description=Release.description, long_description=open('README.rst').read(), license=Release.license, platforms=Release.platforms, url=Release.url, #~ test_suite = 'nose.collector', keywords=Release.keywords, classifiers=[ "Programming Language :: Python :: 2.7", "Development Status :: 4 - Beta", "Environment :: Console", "Intended Audience :: Science/Research", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Natural Language :: English", "Operating System :: OS Independent", "Topic :: Scientific/Engineering", "Topic :: Scientific/Engineering :: Physics", ], )
gpl-3.0
jobovy/apogee-maps
py/plot_distanceintegral_final.py
1
2876
############################################################################### # plot_distanceintegral_final.py: make a final overview plot of the distance # integral ############################################################################### import sys import pickle import numpy from scipy import signal, integrate, interpolate import matplotlib matplotlib.use('Agg') from matplotlib import pyplot from galpy.util import bovy_plot import dust def plot_distanceintegral_final(plotname): # Reload full area with open('../savs/distInt.sav','rb') as savefile: area_full= pickle.load(savefile) # Reload full area w/o center with open('../savs/distIntRmcenter.sav','rb') as savefile: area_rmcenter= pickle.load(savefile) # Calculate PSD of each psdx_full, psd_full= \ signal.periodogram(area_full*dust._GREEN15DISTS**3./numpy.sum(area_full*dust._GREEN15DISTS**3.), fs=1./(dust._GREEN15DISTMODS[1]-dust._GREEN15DISTMODS[0]), detrend=lambda x: x,scaling='spectrum') psdx_rmcenter, psd_rmcenter= \ signal.periodogram(area_rmcenter*dust._GREEN15DISTS**3./numpy.sum(area_rmcenter*dust._GREEN15DISTS**3.), fs=1./(dust._GREEN15DISTMODS[1]-dust._GREEN15DISTMODS[0]), detrend=lambda x: x,scaling='spectrum') bovy_plot.bovy_print(fig_height=5.5) matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{yfonts}"] line1= bovy_plot.bovy_plot(psdx_full[1:],numpy.sqrt(psd_full[1:]), 'k-',loglog=True, xlabel=r'$\mathrm{distance\ resolution}\ k_\mu\,(\mathrm{mag}^{-1})$', ylabel=r'$\mathrm{effective\ volume\ error}\ \sqrt{P_k}$', xrange=[0.04,20.], yrange=[10**-11.,5.]) line2= bovy_plot.bovy_plot(psdx_rmcenter[1:],numpy.sqrt(psd_rmcenter[1:]), 'r-',overplot=True) bovy_plot.bovy_plot([1.,10.],[6.*10.**-4.,6.*10.**-7.],'k--',overplot=True) bovy_plot.bovy_plot([1.,10.],[2.*10.**-5.,2.*10.**-10.], 'r--',overplot=True) pyplot.legend((line1[0],line2[0]), (r'$\mathrm{full\ sky}$', r'$\mathrm{excluding}\ |180^\circ-l| > 155^\circ, |b| < 25^\circ$'), loc='upper right',#bbox_to_anchor=(.02,.02), numpoints=8, prop={'size':14}, frameon=False) bovy_plot.bovy_text(r'$\mathrm{normalized}\ D^3\,\nu_*(\mu|\theta)\,\textswab{S}(\mu)$', bottom_left=True,size=16.) bovy_plot.bovy_end_print(plotname) return None if __name__ == '__main__': plot_distanceintegral_final(sys.argv[1])
bsd-3-clause
ywcui1990/nupic
external/linux32/lib/python2.6/site-packages/matplotlib/backends/backend_gdk.py
69
15968
from __future__ import division import math import os import sys import warnings def fn_name(): return sys._getframe(1).f_code.co_name import gobject import gtk; gdk = gtk.gdk import pango pygtk_version_required = (2,2,0) if gtk.pygtk_version < pygtk_version_required: raise ImportError ("PyGTK %d.%d.%d is installed\n" "PyGTK %d.%d.%d or later is required" % (gtk.pygtk_version + pygtk_version_required)) del pygtk_version_required import numpy as npy import matplotlib from matplotlib._pylab_helpers import Gcf from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \ FigureManagerBase, FigureCanvasBase from matplotlib.cbook import is_string_like from matplotlib.figure import Figure from matplotlib.mathtext import MathTextParser from matplotlib.transforms import Affine2D from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array backend_version = "%d.%d.%d" % gtk.pygtk_version _debug = False # Image formats that this backend supports - for FileChooser and print_figure() IMAGE_FORMAT = ['eps', 'jpg', 'png', 'ps', 'svg'] + ['bmp'] # , 'raw', 'rgb'] IMAGE_FORMAT.sort() IMAGE_FORMAT_DEFAULT = 'png' class RendererGDK(RendererBase): fontweights = { 100 : pango.WEIGHT_ULTRALIGHT, 200 : pango.WEIGHT_LIGHT, 300 : pango.WEIGHT_LIGHT, 400 : pango.WEIGHT_NORMAL, 500 : pango.WEIGHT_NORMAL, 600 : pango.WEIGHT_BOLD, 700 : pango.WEIGHT_BOLD, 800 : pango.WEIGHT_HEAVY, 900 : pango.WEIGHT_ULTRABOLD, 'ultralight' : pango.WEIGHT_ULTRALIGHT, 'light' : pango.WEIGHT_LIGHT, 'normal' : pango.WEIGHT_NORMAL, 'medium' : pango.WEIGHT_NORMAL, 'semibold' : pango.WEIGHT_BOLD, 'bold' : pango.WEIGHT_BOLD, 'heavy' : pango.WEIGHT_HEAVY, 'ultrabold' : pango.WEIGHT_ULTRABOLD, 'black' : pango.WEIGHT_ULTRABOLD, } # cache for efficiency, these must be at class, not instance level layoutd = {} # a map from text prop tups to pango layouts rotated = {} # a map from text prop tups to rotated text pixbufs def __init__(self, gtkDA, dpi): # widget gtkDA is used for: # '<widget>.create_pango_layout(s)' # cmap line below) self.gtkDA = gtkDA self.dpi = dpi self._cmap = gtkDA.get_colormap() self.mathtext_parser = MathTextParser("Agg") def set_pixmap (self, pixmap): self.gdkDrawable = pixmap def set_width_height (self, width, height): """w,h is the figure w,h not the pixmap w,h """ self.width, self.height = width, height def draw_path(self, gc, path, transform, rgbFace=None): transform = transform + Affine2D(). \ scale(1.0, -1.0).translate(0, self.height) polygons = path.to_polygons(transform, self.width, self.height) for polygon in polygons: # draw_polygon won't take an arbitrary sequence -- it must be a list # of tuples polygon = [(int(round(x)), int(round(y))) for x, y in polygon] if rgbFace is not None: saveColor = gc.gdkGC.foreground gc.gdkGC.foreground = gc.rgb_to_gdk_color(rgbFace) self.gdkDrawable.draw_polygon(gc.gdkGC, True, polygon) gc.gdkGC.foreground = saveColor if gc.gdkGC.line_width > 0: self.gdkDrawable.draw_lines(gc.gdkGC, polygon) def draw_image(self, x, y, im, bbox, clippath=None, clippath_trans=None): if bbox != None: l,b,w,h = bbox.bounds #rectangle = (int(l), self.height-int(b+h), # int(w), int(h)) # set clip rect? im.flipud_out() rows, cols, image_str = im.as_rgba_str() image_array = npy.fromstring(image_str, npy.uint8) image_array.shape = rows, cols, 4 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=cols, height=rows) array = pixbuf_get_pixels_array(pixbuf) array[:,:,:] = image_array gc = self.new_gc() y = self.height-y-rows try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf (gc.gdkGC, pixbuf, 0, 0, int(x), int(y), cols, rows, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), cols, rows, gdk.RGB_DITHER_NONE, 0, 0) # unflip im.flipud_out() def draw_text(self, gc, x, y, s, prop, angle, ismath): x, y = int(x), int(y) if x <0 or y <0: # window has shrunk and text is off the edge return if angle not in (0,90): warnings.warn('backend_gdk: unable to draw text at angles ' + 'other than 0 or 90') elif ismath: self._draw_mathtext(gc, x, y, s, prop, angle) elif angle==90: self._draw_rotated_text(gc, x, y, s, prop, angle) else: layout, inkRect, logicalRect = self._get_pango_layout(s, prop) l, b, w, h = inkRect self.gdkDrawable.draw_layout(gc.gdkGC, x, y-h-b, layout) def _draw_mathtext(self, gc, x, y, s, prop, angle): ox, oy, width, height, descent, font_image, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) if angle==90: width, height = height, width x -= width y -= height imw = font_image.get_width() imh = font_image.get_height() N = imw * imh # a numpixels by num fonts array Xall = npy.zeros((N,1), npy.uint8) image_str = font_image.as_str() Xall[:,0] = npy.fromstring(image_str, npy.uint8) # get the max alpha at each pixel Xs = npy.amax(Xall,axis=1) # convert it to it's proper shape Xs.shape = imh, imw pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=imw, height=imh) array = pixbuf_get_pixels_array(pixbuf) rgb = gc.get_rgb() array[:,:,0]=int(rgb[0]*255) array[:,:,1]=int(rgb[1]*255) array[:,:,2]=int(rgb[2]*255) array[:,:,3]=Xs try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf (gc.gdkGC, pixbuf, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0) def _draw_rotated_text(self, gc, x, y, s, prop, angle): """ Draw the text rotated 90 degrees, other angles are not supported """ # this function (and its called functions) is a bottleneck # Pango 1.6 supports rotated text, but pygtk 2.4.0 does not yet have # wrapper functions # GTK+ 2.6 pixbufs support rotation gdrawable = self.gdkDrawable ggc = gc.gdkGC layout, inkRect, logicalRect = self._get_pango_layout(s, prop) l, b, w, h = inkRect x = int(x-h) y = int(y-w) if x < 0 or y < 0: # window has shrunk and text is off the edge return key = (x,y,s,angle,hash(prop)) imageVert = self.rotated.get(key) if imageVert != None: gdrawable.draw_image(ggc, imageVert, 0, 0, x, y, h, w) return imageBack = gdrawable.get_image(x, y, w, h) imageVert = gdrawable.get_image(x, y, h, w) imageFlip = gtk.gdk.Image(type=gdk.IMAGE_FASTEST, visual=gdrawable.get_visual(), width=w, height=h) if imageFlip == None or imageBack == None or imageVert == None: warnings.warn("Could not renderer vertical text") return imageFlip.set_colormap(self._cmap) for i in range(w): for j in range(h): imageFlip.put_pixel(i, j, imageVert.get_pixel(j,w-i-1) ) gdrawable.draw_image(ggc, imageFlip, 0, 0, x, y, w, h) gdrawable.draw_layout(ggc, x, y-b, layout) imageIn = gdrawable.get_image(x, y, w, h) for i in range(w): for j in range(h): imageVert.put_pixel(j, i, imageIn.get_pixel(w-i-1,j) ) gdrawable.draw_image(ggc, imageBack, 0, 0, x, y, w, h) gdrawable.draw_image(ggc, imageVert, 0, 0, x, y, h, w) self.rotated[key] = imageVert def _get_pango_layout(self, s, prop): """ Create a pango layout instance for Text 's' with properties 'prop'. Return - pango layout (from cache if already exists) Note that pango assumes a logical DPI of 96 Ref: pango/fonts.c/pango_font_description_set_size() manual page """ # problem? - cache gets bigger and bigger, is never cleared out # two (not one) layouts are created for every text item s (then they # are cached) - why? key = self.dpi, s, hash(prop) value = self.layoutd.get(key) if value != None: return value size = prop.get_size_in_points() * self.dpi / 96.0 size = round(size) font_str = '%s, %s %i' % (prop.get_name(), prop.get_style(), size,) font = pango.FontDescription(font_str) # later - add fontweight to font_str font.set_weight(self.fontweights[prop.get_weight()]) layout = self.gtkDA.create_pango_layout(s) layout.set_font_description(font) inkRect, logicalRect = layout.get_pixel_extents() self.layoutd[key] = layout, inkRect, logicalRect return layout, inkRect, logicalRect def flipy(self): return True def get_canvas_width_height(self): return self.width, self.height def get_text_width_height_descent(self, s, prop, ismath): if ismath: ox, oy, width, height, descent, font_image, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) return width, height, descent layout, inkRect, logicalRect = self._get_pango_layout(s, prop) l, b, w, h = inkRect return w, h+1, h + 1 def new_gc(self): return GraphicsContextGDK(renderer=self) def points_to_pixels(self, points): return points/72.0 * self.dpi class GraphicsContextGDK(GraphicsContextBase): # a cache shared by all class instances _cached = {} # map: rgb color -> gdk.Color _joind = { 'bevel' : gdk.JOIN_BEVEL, 'miter' : gdk.JOIN_MITER, 'round' : gdk.JOIN_ROUND, } _capd = { 'butt' : gdk.CAP_BUTT, 'projecting' : gdk.CAP_PROJECTING, 'round' : gdk.CAP_ROUND, } def __init__(self, renderer): GraphicsContextBase.__init__(self) self.renderer = renderer self.gdkGC = gtk.gdk.GC(renderer.gdkDrawable) self._cmap = renderer._cmap def rgb_to_gdk_color(self, rgb): """ rgb - an RGB tuple (three 0.0-1.0 values) return an allocated gtk.gdk.Color """ try: return self._cached[tuple(rgb)] except KeyError: color = self._cached[tuple(rgb)] = \ self._cmap.alloc_color( int(rgb[0]*65535),int(rgb[1]*65535),int(rgb[2]*65535)) return color #def set_antialiased(self, b): # anti-aliasing is not supported by GDK def set_capstyle(self, cs): GraphicsContextBase.set_capstyle(self, cs) self.gdkGC.cap_style = self._capd[self._capstyle] def set_clip_rectangle(self, rectangle): GraphicsContextBase.set_clip_rectangle(self, rectangle) if rectangle is None: return l,b,w,h = rectangle.bounds rectangle = (int(l), self.renderer.height-int(b+h)+1, int(w), int(h)) #rectangle = (int(l), self.renderer.height-int(b+h), # int(w+1), int(h+2)) self.gdkGC.set_clip_rectangle(rectangle) def set_dashes(self, dash_offset, dash_list): GraphicsContextBase.set_dashes(self, dash_offset, dash_list) if dash_list == None: self.gdkGC.line_style = gdk.LINE_SOLID else: pixels = self.renderer.points_to_pixels(npy.asarray(dash_list)) dl = [max(1, int(round(val))) for val in pixels] self.gdkGC.set_dashes(dash_offset, dl) self.gdkGC.line_style = gdk.LINE_ON_OFF_DASH def set_foreground(self, fg, isRGB=False): GraphicsContextBase.set_foreground(self, fg, isRGB) self.gdkGC.foreground = self.rgb_to_gdk_color(self.get_rgb()) def set_graylevel(self, frac): GraphicsContextBase.set_graylevel(self, frac) self.gdkGC.foreground = self.rgb_to_gdk_color(self.get_rgb()) def set_joinstyle(self, js): GraphicsContextBase.set_joinstyle(self, js) self.gdkGC.join_style = self._joind[self._joinstyle] def set_linewidth(self, w): GraphicsContextBase.set_linewidth(self, w) if w == 0: self.gdkGC.line_width = 0 else: pixels = self.renderer.points_to_pixels(w) self.gdkGC.line_width = max(1, int(round(pixels))) def new_figure_manager(num, *args, **kwargs): """ Create a new figure manager instance """ FigureClass = kwargs.pop('FigureClass', Figure) thisFig = FigureClass(*args, **kwargs) canvas = FigureCanvasGDK(thisFig) manager = FigureManagerBase(canvas, num) # equals: #manager = FigureManagerBase (FigureCanvasGDK (Figure(*args, **kwargs), # num) return manager class FigureCanvasGDK (FigureCanvasBase): def __init__(self, figure): FigureCanvasBase.__init__(self, figure) self._renderer_init() def _renderer_init(self): self._renderer = RendererGDK (gtk.DrawingArea(), self.figure.dpi) def _render_figure(self, pixmap, width, height): self._renderer.set_pixmap (pixmap) self._renderer.set_width_height (width, height) self.figure.draw (self._renderer) filetypes = FigureCanvasBase.filetypes.copy() filetypes['jpg'] = 'JPEG' filetypes['jpeg'] = 'JPEG' def print_jpeg(self, filename, *args, **kwargs): return self._print_image(filename, 'jpeg') print_jpg = print_jpeg def print_png(self, filename, *args, **kwargs): return self._print_image(filename, 'png') def _print_image(self, filename, format, *args, **kwargs): width, height = self.get_width_height() pixmap = gtk.gdk.Pixmap (None, width, height, depth=24) self._render_figure(pixmap, width, height) # jpg colors don't match the display very well, png colors match # better pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, width, height) pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, width, height) pixbuf.save(filename, format) def get_default_filetype(self): return 'png'
agpl-3.0
santis19/fatiando
gallery/gridder/padding.py
6
1944
""" Pad the edges of grids using various methods ============================================= Sometimes it is useful to add some padding points to the edges of grids, for example during FFT-based processing to avoid edge effects. Function :func:`fatiando.gridder.pad_array` does this using various padding methods. Functions :func:`fatiando.gridder.unpad_array` (to remove padding) and :func:`fatiando.gridder.pad_coords` (to created padded coordinate arrays) offer support for common operations done while padding. """ import matplotlib.pyplot as plt import numpy as np from fatiando import gridder # Generate some synthetic data area = (-100, 100, -60, 60) shape = (101, 172) # The padding functions need data to be on a regular grid and represented by a # 2D numpy array. So I'll convert the outputs to 2D. x, y = gridder.regular(area, shape) x = x.reshape(shape) y = y.reshape(shape) data = np.sin(0.1*x)*np.cos(0.09*y) + 0.001*(x**2 + y**2) # Pad arrays with all the padding options and make a single figure with all of # them. fig, axes = plt.subplots(2, 4, figsize=(10, 6), sharex=True, sharey=True) ax = axes[0, 0] ax.set_title('Original') # Keep all plots on the same color scale of the original data vmin, vmax = data.min(), data.max() ax.pcolormesh(y, x, data, cmap='RdBu_r', vmin=vmin, vmax=vmax) padtypes = ['0', 'mean', 'edge', 'lintaper', 'reflection', 'oddreflection', 'oddreflectiontaper'] for padtype, ax in zip(padtypes, axes.ravel()[1:]): padded_data, nps = gridder.pad_array(data, padtype=padtype) # Get coordinate vectors pad_x, pad_y = gridder.pad_coords([x, y], shape, nps) padshape = padded_data.shape ax.set_title(padtype) ax.pcolormesh(pad_y.reshape(padshape), pad_x.reshape(padshape), padded_data, cmap='RdBu_r', vmin=vmin, vmax=vmax) ax.set_xlim(pad_y.min(), pad_y.max()) ax.set_ylim(pad_x.min(), pad_x.max()) plt.tight_layout(w_pad=0) plt.show()
bsd-3-clause
opcon/plutokore
scripts/plot-jet-injection.py
2
2019
#!/bin/env python3 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plot import argparse def plot_injection(t1, t2, iw, r, c, ir): l = ir+1 x = np.linspace(-l, l, num=r) z = np.linspace(-l, l, num=r) v = np.zeros((x.shape[0], z.shape[0])) for i in range(v.shape[0]): for k in range(v.shape[1]): r = np.sqrt(x[i]**2 + z[k]**2) if z[k] > 0: off = iw / np.tan(t1) ih = ir * np.cos(t1) else: off = iw / np.tan(np.pi-t2) ih = ir * np.cos(t2) th = np.arccos((z[k] + off) / np.sqrt(x[i]**2 + (z[k] + off)**2)) h = np.abs(r * np.cos(th)) if (r <= ir and c == 'cap') or (h <= ih and c == 'nocap'): if z[k] > 0 and th < t1: v[i,k] = 1 elif z[k] < 0 and th > np.pi - t2: v[i,k] = -1 fig,ax = plot.subplots(1, 1, subplot_kw={'aspect': 'equal'}) ax.pcolormesh(x, z, v.T) plot.show() return def main(): parser = argparse.ArgumentParser( description = 'Plot jet injection region' ) parser.add_argument('theta1', type=float, help='First jet opening angle (degrees)') parser.add_argument('theta2', type=float, help='Second jet opening angle (degrees)') parser.add_argument('initial_width', type=float, help='Initial width of jet injection cone') parser.add_argument('-r', '--resolution', help='Grid cell count (default: %(default)s)', type=int, default=100) parser.add_argument('-c', '--conetype', help='Cone type (with cap or without)', choices=['cap', 'nocap'], default='cap') parser.add_argument('-i', '--injectionradius', help='Injection radius (default: %(default)s)', type=float, default=1.0) args = parser.parse_args() plot_injection(np.deg2rad(args.theta1), np.deg2rad(args.theta2), args.initial_width, args.resolution, args.conetype, args.injectionradius) if __name__ == "__main__": main()
mit
laurensstoop/HiSPARC-BONZ
egg/eggs_v6.py
1
16596
# -*- coding: utf-8 -*- # ############################################################################ # # Program for analysing HiSPARC data # # This software is made under the GNU General Public License, version 3 (GPL-3.0) # ############################################################################ """ =================================== Created on Thu Mar 24 13:17:57 2016 @author: Laurens Stoop =================================== """ ################################## HEADER ################################## """ Import of Packages """ print "POKING eggs, NOW IMPORTING PYTHON FEED (ETC = 18s)" import sapphire # The HiSparc Python Framework import tables # A HDF5 python module that allows to store data import datetime # A package to decode the timeformat of HiSparc data import matplotlib.pyplot as plt # Plotting functionality of MatPlotLib import numpy as np # This is NumPy import rootpy # Get the pythonesc version of ROOT import os.path # To check if files exist (so you don't do stuff again) import rootpy.interactive # Get some option like a wait() from rootpy.plotting import root2matplotlib from matplotlib.colors import LogNorm import ROOT import array """ Getting the data file and setting the variables """ # Time between which the data is downloaded (jjjj,mm,dd,[hh]) START = datetime.datetime(2016,01,01) END = datetime.datetime(2016,01,02) # Give the list of stations STATIONS = [501]#,503,1006,1101,3001,13002,14001,20003] # Do not show the figures plt.ioff() ################################## STYLE ################################## def style(name='hisparc', shape='rect', orientation='landscape'): # The style we make STYLE = rootpy.plotting.Style(name, 'HiSPARC') # We do not want borders STYLE.SetCanvasBorderMode(0) STYLE.SetFrameBorderMode(0) # The style of the line STYLE.SetHistLineColor(1) STYLE.SetHistLineStyle(0) STYLE.SetHistLineWidth(2) #For the fit/function information STYLE.SetOptFit(1111) STYLE.SetFitFormat("5.4g") STYLE.SetFuncColor(2) STYLE.SetFuncStyle(1) STYLE.SetFuncWidth(2) STYLE.SetOptStat(0) # Change for log plots: STYLE.SetOptLogx(0) STYLE.SetOptLogy(1) STYLE.SetOptLogz(0) # Whit background STYLE.SetFrameFillColor(0) STYLE.SetCanvasColor(0) STYLE.SetPadColor(0) STYLE.SetStatColor(0) return STYLE ################################## FUNC ################################## print "GETTING THE FUNC OUT" # This function gets the data into a file with obvious naming structure def eggs_data_download( self, station_number=501, begin=datetime.datetime(2016,01,01), final = datetime.datetime(2016,01,02) ): # Data is downloaded if '/s%d' %station_number not in self: # Let them know what we do print "\nGetting event data from station %d " % station # Now retrieve the event data sapphire.esd.download_data( self, # File (as opened above) '/s%d' %station_number, # Group name (/s..station..) station_number, # Station number begin, # Start data date final, # End data date 'events', # Download events (or 'weather') True) # Show progress # Let them know what we do print "\nGetting wheater data from station %d " % station # Now retrieve the wheater data sapphire.esd.download_data( self, # File (as opened above) '/s%d' %station_number, # Group name (/s..station..) station_number, # Station number begin, # Start data date final, # End data date 'weather', # Download wheater True) # Show progress # If the datafile has the group we do not download them data else: print "All data present for station %d" % station # Do the dataconversion of the ADC def eggs_adc_conversion( self, station_number = 501 ): # Get the unconverted data base data_set = self.get_node( '/s%d' %station_number, # From the group (/s..station..) 'events') # Get the node with events # Get the column we want to convert data_converted_ph = 0.57*data_set.col('pulseheights')- 113 # If no new group present then create one if ('/s%d/handled/converted' %station_number) not in self: # Create a group for the converted data ## Should be assigned in data_download handled = self.create_group('/s%d/' % station_number, 'handled', 'Handled data') # Write the new data set to the File new_data_set = data_set.copy(handled,'converted') ####################################### # Modify the pulseheight data column new_data_set.modify_column(column=data_converted_ph, colname='pulseheights') # Load a histogram def eggs_load_pulseheight( self, station_number=501, detector=0, number_bins=200, range_start=0., range_end=4500): # Get event data event_data = self.get_node( '/s%d' %station_number, # From the group (/s..station..) 'events') # Get the node with events # Get the pulseheight from all events data_ph = event_data.col('pulseheights') # col takes all data from events # Create a histogram ph_histo = rootpy.plotting.Hist(number_bins, range_start, range_end, drawstyle='hist') # Fill it with data ph_histo.fill_array(data_ph[:,detector]) return ph_histo # This function plots the pulse_histograms depending on number of bins and range def eggs_plot_pulseheight( self, station_number=501, detector=0, number_bins=200, range_start=0., range_end=4500 ): # If the plot exist we skip the plotting if os.path.isfile('./img/pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)): # Say if the plot is present print "Plot already present for station %d" % station_number # If there is no plot we make it else: # Now transform the ROOT histogram to a python figure rootpy.plotting.root2matplotlib.hist(ph_histo) # Setting the limits on the axis plt.ylim((pow(10,-1),pow(10,7))) plt.xlim((range_start, range_end)) plt.yscale('log') # Setting the plot labels and title plt.xlabel("Pulseheight [ADC]") plt.ylabel("Counts") plt.title("Pulseheight histogram (log scale) for station (%d)" %station_number) # Saving them Pica plt.savefig( './img/pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector) , # Name of the file bbox_inches='tight') # Use less whitespace def eggs_fitplot_pulseheight( self , station_number, detector=0): # If the plot exist we skip the plotting if os.path.isfile('./img/fitted_pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)): # Say if the plot is present print "Fitted plot already present for station %d, detector %d" % (station_number, detector) # If there is no plot we make it else: # Set the plot style rootpy.plotting.set_style(style('hisparc')) # We work on a invisible canvas and fit the histogram and plot it with rootpy.context.invisible_canvas() as canv: # Properties of the canvas canv.axes(xlimits=[100,4500],ylimits=[pow(10,-1),pow(10,7)], xbins=200) # The fitfunction definitions ph_fitf_signal = ROOT.TF1( 'ph_fitf_signal', 'landau',120,2500) ph_fitf_bkg = ROOT.TF1( 'ph_fitf_bkg', 'expo',0,200) ph_fitf_total = ROOT.TF1('ph_fitf_total','expo(0)+landau(2)',120,2500) # THe fitting of the pre-functions self.Fit(ph_fitf_signal,'MQR') self.Fit(ph_fitf_bkg,'MQR+') # Retrieving the fitparameters for final fit ph_par_signal= ph_fitf_signal.GetParameters() ph_par_bkg = ph_fitf_bkg.GetParameters() # Making an empty array in which the fitparameters can be loaded ph_par_total = array.array( 'd', 5*[0.] ) # Loading of the fitparameters ph_par_total[0], ph_par_total[1] = ph_par_bkg[0], ph_par_bkg[1] ph_par_total[2], ph_par_total[3], ph_par_total[4] = ph_par_signal[0], ph_par_signal[1], ph_par_signal[2] # Set the fitparameters and their names for final fit ph_fitf_total.SetParameters( ph_par_total ) ph_fitf_total.SetParName(0,'Exp. decay offset') ph_fitf_total.SetParName(1,'Exp. decay const') ph_fitf_total.SetParName(2,'#mu_{peak}') ph_fitf_total.SetParName(3,'#sigma (scale parameter)') ph_fitf_total.SetParName(4,'Normalization') # Fit the full function self.Fit(ph_fitf_total,'MR') # Get the parameters for later use ph_par = ph_fitf_total.GetParameters() # set visual attributes self.linecolor = 'blue' self.markercolor = 'blue' self.xaxis.SetTitle("Pulseheight [ADC]") self.yaxis.SetTitle("Count") # Draw the histo gram and the fitfunction self.Draw() ph_fitf_total.Draw('same hisparc') # Save the image for thesis canv.SaveAs('./img/fitted_pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)) # Return the found fitparameters for the total fit function return [ph_par] def eggs_cut_pulseheight( self, station_number): # If no pulseheight cut present then create one if ('/s%d/handled/pulseheight_cut' %station_number) not in self: # Make a empty table with the correct properties data_cut_ph = self.create_table( '/s%d/handled' % station_number, # Destination group 'pulseheight_cut' , # Destination filename sapphire.storage.EventObservables, # Format of the table "Converted data") # Title of the table else: data_cut_ph = self.get_node( '/s%d/handled' %station_number, # From the group (/s..station..) 'pulseheight_cut') # Get the uncut converted data base data_set = self.get_node( '/s%d/handled' %station_number, # From the group (/s..station..) 'converted') # Get the node with events # print data_set.cols.pulseheights[1:5] # cut_condition_pulseheight = '(pulseheights[:] > 0)' # # number_of_rows = data_set.append_where(data_cut_ph, cut_condition_pulseheight) # print "%d" % number_of_rows result = [ row[:] for row in data_set if row['pulseheights'] <= 20 ] print("Values that pass the cuts:", result) def eggs_plot_pmt(self, station_number): # If the plot exist we skip the plotting if os.path.isfile('./img/pmt_saturation_s%d.pdf' %station_number): # Say if the plot is present print "PMT saturation histogram already present for station %d" % station_number # If there is no plot we make it else: # Get event data event_data = self.get_node( '/s%d' %station, # From the group (/s..station..) 'events') # Get the node with events # Get the pulseheight from all events data_phs = event_data.col('pulseheights') # col takes all data from events (this improves the speed) # Get the integral from all events data_ints = event_data.col('integrals') # col takes all data from events # Make a figure so it can be closed figure_combo, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex = 'col', sharey = 'row') # Setting the plot titles ax1.set_title('Detector 1') ax2.set_title('Detector 2') ax3.set_title('Detector 3') ax4.set_title('Detector 4') # Setting the plot labels ax1.set_ylabel('Pulseheight [ADC]') ax3.set_ylabel('Pulseheight [ADC]') ax3.set_xlabel('Pulse integral [ADC.ns]') ax4.set_xlabel('Pulse integral [ADC.ns]') # Now we plot the data of every detector for detector in range(0,4): # Select the detector data data_ph_detector = data_phs[:,detector] data_int_detector = data_ints[:,detector] # Combine the detector data data_combo = np.stack( (data_int_detector, # The pulse integral on y axis data_ph_detector), # The pulseheight on x axis axis=-1) # To get the direction correct # Initiate a 2D histogram (ROOT style) histo_combo_detector = rootpy.plotting.Hist2D(100, 0, 150000, 100, 0, 4500) # Fill the Histogram histo_combo_detector.fill_array(data_combo) # Plot the histogram with logarithmic colors in correct place if detector == 0: root2matplotlib.hist2d(histo_combo_detector, norm=LogNorm(), axes=ax1) elif detector == 1: root2matplotlib.hist2d(histo_combo_detector, norm=LogNorm(), axes=ax2) elif detector == 2: root2matplotlib.hist2d(histo_combo_detector, norm=LogNorm(), axes=ax3) elif detector == 3: root2matplotlib.hist2d(histo_combo_detector, norm=LogNorm(), axes=ax4) # Save the file figure_combo.savefig( './img/pmt_saturation_s%d.pdf' %station_number) # Name of the file # Close the figure plt.close(figure_combo) # Now we go to the next detector detector +1 def eggs_clean(self, station_number): # Cleans the pica's if os.path.isfile('./img/pmt_saturation_s%d.pdf' %station_number): os.remove('./img/pmt_saturation_s%d.pdf' %station_number) detector=0 if os.path.isfile('./img/fitted_pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)): os.remove('./img/fitted_pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)) if os.path.isfile('./img/pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)): os.remove('./img/pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)) ################################## BODY ################################## """ Data acquisition """ print "SPARCS ENABLED, TREE ROOTS ACTIVATED, TIMING ADJUSTED" # Open a data file (automatic close) with tables.open_file('nest_of_eggs.h5','a') as data_file: # Retrieve for every station the data and plot a pulsehisto for station in STATIONS: data_file.eggs_clean(station) # Getting the data data_file.eggs_data_download( station, START, END) # Set the conversion for adc data_file.eggs_adc_conversion( station) # Create the histogram ph_histo = data_file.eggs_load_pulseheight( station, 0, 200, 0., 4500) # Fit the functions ph_histo.eggs_fitplot_pulseheight(station,0) # Plot some data data_file.eggs_plot_pulseheight( station, 0, 200, 0., 4500) # Plot the PMT curves data_file.eggs_plot_pmt( station ) data_file.eggs_cut_pulseheight( station) print "####### I'm Done Bitches! #######" ################################## FOOTER ################################## """ Clean up shit """
gpl-3.0
jadecastro/LTLMoP
src/lib/handlers/motionControl/OMPLController.py
1
33988
#!/usr/bin/env python ###################################################################### # Software License Agreement (BSD License) # # Copyright (c) 2010, Rice University # All rights reserved. ###################################################################### """ =================================================================== OMPLController.py - Open Motion Planning Library Motion Controller =================================================================== Uses Open Motion Planning Library developed by Rice University to generate paths. """ try: from ompl import util as ou from ompl import base as ob from ompl import control as oc from ompl import geometric as og except: # if the ompl module is not in the PYTHONPATH assume it is installed in a # subdirectory of the parent directory called "py-bindings." from os.path import basename, abspath, dirname, join import sys sys.path.insert(0, join(dirname(dirname(abspath(__file__))),'py-bindings')) from ompl import util as ou from ompl import base as ob from ompl import control as oc from ompl import geometric as og from functools import partial from time import sleep from math import fabs from numpy import * from __is_inside import * import math import sys,os, time from scipy.linalg import norm from numpy.matlib import zeros import scipy as Sci import scipy.linalg import Polygon, Polygon.IO import Polygon.Utils as PolyUtils import Polygon.Shapes as PolyShapes import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.figure import Figure import Tkinter as Tk from mpl_toolkits.mplot3d import Axes3D from math import sqrt, fabs , pi import random import thread import threading class motionControlHandler: def __init__(self, proj, shared_data,Space_Dimension,planner,robot_type,Geometric_Control,plotting): """ Space_Dimension(int): dimension of the space operating in. Enter 2 for 2D and 3 for 3D. Only quadrotor in ROS is supported for 3D now.(default=2) planner(string): Planner to be used. Enter RRT,KPIECE1 or PRM, RRTConnect. (default='PRM') robot_type (int): Which robot is used for execution. BasicSim is 1, ODE is 2, ROS is 3, Nao is 4, Pioneer is 5(default=1) Geometric_Control(string): Specify if you want to planner to sample in geometric or control space. G for geometric and C for control. (default='G') plotting (bool): Check the box to enable plotting (default=True) """ #Parameters self.system_print = False self.currentRegionPoly = None # polygon of the current region self.nextRegionPoly = None # polygon of the next region self.map = {} # dictionary of polygons of different regions self.all = Polygon.Polygon() # polygon of the boundary self.OMPLpath = None self.trans_matrix = mat([[0,1],[-1,0]]) # transformation matrix for find the normal to the vector # Get references to handlers we'll need to communicate with self.drive_handler = proj.h_instance['drive'] self.pose_handler = proj.h_instance['pose'] # Get information about regions self.proj = proj self.coordmap_map2lab = proj.coordmap_map2lab self.coordmap_lab2map = proj.coordmap_lab2map self.last_warning = 0 self.previous_next_reg = None # Information about the spoce dimension if Space_Dimension not in [2,3]: Space_Dimension = 2 self.Space_Dimension = Space_Dimension # Information about the planner if planner not in ['RRT','KPIECE1','PRM','RRTConnect','EST']: planner = 'PRM' self.planner = planner # Information about the geometric or control space if Geometric_Control not in ['G','C']: Geometric_Control = 'G' self.Geometric_Control = Geometric_Control # Information about the robot (default set to ODE) if robot_type not in [1,2,3,4]: robot_type = 3 self.system = robot_type # Information about whether plotting is enabled. if plotting is True: self.plotting = True else: self.plotting = False # Operate_system (int): Which operating system is used for execution. # Ubuntu and Mac is 1, Windows is 2 if sys.platform in ['win32', 'cygwin']: self.operate_system = 2 else: self.operate_system = 1 print "Operate_system: "+ str(self.operate_system) print "Planner: " + str(self.planner) print "Geometric/Control space: " + str(self.Geometric_Control) print "Space Dimension: " + str(self.Space_Dimension) # Generate polygon for regions in the map # getting raw region inputs from user. To be used for 3D path planning self.original_regions = self.proj.loadRegionFile() if self.system_print is True: print "MAXHEIGHT:" +str(self.original_regions.getMaximumHeight()) # Information about the maximum height in z direction if self.Space_Dimension == 2: self.maxHeight = 0.5 else: self.maxHeight = self.original_regions.getMaximumHeight() self.map = {'polygon':{},'original_name':{},'height':{}} for region in self.proj.rfi.regions: self.map['polygon'][region.name] = self.createRegionPolygon(region) for n in range(len(region.holeList)): # no of holes self.map['polygon'][region.name] -= self.createRegionPolygon(region,n) # map the names back to the old original names specified by the user for rname, rlist in self.proj.regionMapping.iteritems(): self.map['original_name'][rlist[0]] = rname #store the height of the regions for region in self.original_regions.regions: if region.name.lower() == rname.lower(): self.map['height'][rlist[0]] = region.height self.original_map = {'polygon':{},'height':{},'isObstacle':{}} for region in self.original_regions.regions: self.original_map['polygon'][region.name] = self.createRegionPolygon(region) self.original_map['height'][region.name] = region.height self.original_map['isObstacle'][region.name] = region.isObstacle # building the planner dictionary self.planner_dictionary = {'G':{},'C':{}} self.planner_dictionary['G']['PRM'] = og.PRM self.planner_dictionary['G']['RRT'] = og.RRT self.planner_dictionary['G']['KPIECE1'] = og.KPIECE1 self.planner_dictionary['G']['RRTConnect'] = og.RRTConnect self.planner_dictionary['G']['EST'] = og.EST self.planner_dictionary['C']['RRT'] = oc.RRT self.planner_dictionary['C']['KPIECE1'] = oc.KPIECE1 # Generate the boundary polygon for regionName,regionPoly in self.map['polygon'].iteritems(): self.all += regionPoly # Specify the size of the robot # 1: basicSim; 2: ODE; 3: ROS 4: Nao; 5: Pioneer # self.radius: radius of the robot (m) if self.system == 1: self.radius = 5 elif self.system == 2: self.radius = 5 elif self.system == 3: self.ROSInitHandler = shared_data['ROS_INIT_HANDLER'] self.radius = self.ROSInitHandler.robotPhysicalWidth if self.ROSInitHandler.modelName == 'quadrotor': self.height = 0.40*1.2 #(m) height of the robot elif self.system == 4: self.radius = 0.15*1.2 elif self.system == 5: self.radius = 0.15 if self.plotting == True: app = _MyTkApp() app.start() self.TkApp = app.getSharedData() self.fig = self.TkApp.fig self.ax = self.TkApp.ax self.ax.legend() self.BoundaryMaxMin = self.all.boundingBox() #0-3:xmin, xmax, ymin and ymax self.plotMap() self.setPlotLimitXYZ() self.current_reg = None self.next_reg = None def gotoRegion(self, current_reg, next_reg, last=False): """ If ``last`` is True, we will move to the center of the destination region. Returns ``True`` if we've reached the destination region. """ # Find our current configuration pose = self.pose_handler.getPose() self.current_reg = current_reg self.next_reg = next_reg if self.plotting == True: if self.operate_system == 1: if self.Space_Dimension == 3: self.ax.plot([pose[0]],[pose[1]],[pose[3]],'ko') else: self.ax.plot([pose[0]],[pose[1]],'ko') self.setPlotLimitXYZ() self.ax.get_figure().canvas.draw() if current_reg == next_reg and not last: # No need to move! self.drive_handler.setVelocity(0, 0) # So let's stop return True # Check if Vicon has cut out # TODO: this should probably go in posehandler? if math.isnan(pose[2]): print "WARNING: No Vicon data! Pausing." self.drive_handler.setVelocity(0, 0) # So let's stop time.sleep(1) return False ###This part will be run when the robot goes to a new region, otherwise, the original tree will be used. if not self.previous_next_reg == next_reg: # plotting current pose and the map if self.operate_system == 1 and self.plotting == True: self.ax.cla() self.plotMap() if self.Space_Dimension == 3: self.ax.plot([pose[0]],[pose[1]],[pose[3]],'ko') else: self.ax.plot([pose[0]],[pose[1]],'ko') # Entered a new region. New tree should be formed. if self.Space_Dimension == 3: self.nextRegionPoly = self.original_map['polygon'][self.map['original_name'][self.proj.rfi.regions[next_reg].name]] self.currentRegionPoly = self.original_map['polygon'][self.map['original_name'][self.proj.rfi.regions[current_reg].name]] self.nextAndcurrentRegionPoly = self.nextRegionPoly+self.currentRegionPoly else: self.nextRegionPoly = self.map['polygon'][self.proj.rfi.regions[next_reg].name] self.currentRegionPoly = self.map['polygon'][self.proj.rfi.regions[current_reg].name] self.nextAndcurrentRegionPoly = self.nextRegionPoly+self.currentRegionPoly #just to make sure a path can be generated self.nextAndcurrentRegionPoly += Polygon.Shapes.Circle(self.radius*2,(pose[0],pose[1])) if self.system_print == True: print "next Region is " + str(self.proj.rfi.regions[next_reg].name) print "Current Region is " + str(self.proj.rfi.regions[current_reg].name) #set to zero velocity before tree is generated self.drive_handler.setVelocity(0, 0) if last: transFace = None else: # Determine the mid points on the faces connecting to the next region (one goal point will be picked among all the mid points later in buildTree) transFace = None goalPoints = [[],[]] # list of goal points (midpoints of transition faces) face_normal = [[],[]] # normal of the trnasition faces for i in range(len(self.proj.rfi.transitions[current_reg][next_reg])): pointArray_transface = [x for x in self.proj.rfi.transitions[current_reg][next_reg][i]] transFace = asarray(map(self.coordmap_map2lab,pointArray_transface)) coord_x = (transFace[0,0] +transFace[1,0])/2 #mid-point coordinate x coord_y = (transFace[0,1] +transFace[1,1])/2 #mid-point coordinate y goalPoints = hstack((goalPoints,vstack((coord_x,coord_y)))) #find the normal vector to the face face = transFace[0,:] - transFace[1,:] distance_face = norm(face) normal = face/distance_face * self.trans_matrix face_normal = hstack((face_normal,vstack((normal[0,0],normal[0,1])))) # move the goal points to the next region q_gBundle = mat(goalPoints) face_normal = mat(face_normal) for i in range(q_gBundle.shape[1]): q_g = q_gBundle[:,i]+face_normal[:,i]*1.5*self.radius ##original 2*self.radius if not self.nextRegionPoly.isInside(q_g[0],q_g[1]): q_g = q_gBundle[:,i]-face_normal[:,i]*1.5*self.radius ##original 2*self.radius goalPoints[0,i] = q_g[0,0] goalPoints[1,i] = q_g[1,0] if transFace is None: print "ERROR: Unable to find transition face between regions %s and %s. Please check the decomposition (try viewing projectname_decomposed.regions in RegionEditor or a text editor)." % (self.proj.rfi.regions[current_reg].name, self.proj.rfi.regions[next_reg].name) self.OMPLpath = self.plan(goalPoints,self.proj.rfi.regions[current_reg].name,self.proj.rfi.regions[next_reg].name,0) self.currentState = 1 # Run algorithm to find a velocity vector (global frame) to take the robot to the next region if self.Space_Dimension == 3: self.Velocity = self.getVelocity([pose[0],pose[1],pose[3]], self.OMPLpath) else: self.Velocity = self.getVelocity([pose[0], pose[1]], self.OMPLpath) self.previous_next_reg = next_reg """ # FOR ROBERT self.Node = self.getNode([pose[0], pose[1]], self.OMPLpath) print "self.Node:" + str(self.Node) self.drive_handler.setDestination(self.Node[0,0], self.Node[1,0], pose[2]) """ # Pass this desired velocity on to the drive handler if self.Space_Dimension == 3: self.drive_handler.setVelocity(self.Velocity[0,0], self.Velocity[1,0],pose[2],self.Velocity[2,0]) else: self.drive_handler.setVelocity(self.Velocity[0,0], self.Velocity[1,0],pose[2]) RobotPoly = Polygon.Shapes.Circle(self.radius,(pose[0],pose[1])) # check if robot is inside the current region departed = not self.currentRegionPoly.overlaps(RobotPoly) pose = self.pose_handler.getPose() arrived = self.nextRegionPoly.isInside(pose[0],pose[1]) #arrived = self.nextRegionPoly.covers(RobotPoly) if departed and (not arrived) and (time.time()-self.last_warning) > 0.5: # Figure out what region we think we stumbled into for r in self.proj.rfi.regions: pointArray = [self.coordmap_map2lab(x) for x in r.getPoints()] vertices = mat(pointArray).T if is_inside([pose[0], pose[1]], vertices): print "I think I'm in " + r.name print pose break self.last_warning = time.time() #print "arrived:"+str(arrived) return arrived def getVelocity(self,p, OMPLpath, last=False): """ This function calculates the velocity for the robot with RRT. The inputs are (given in order): p = the current x-y position of the robot OMPLpath = path information of the planner last = True, if the current region is the last region = False, if the current region is NOT the last region """ pose = mat(p).T """ if self.system_print == True: print (OMPLpath.getSolutionPath().getState(self.currentState))[0] # x-coordinate of the current state """ if self.Space_Dimension == 3: dis_cur = vstack(((OMPLpath.getSolutionPath().getState(self.currentState)).getX(),(OMPLpath.getSolutionPath().getState(self.currentState)).getY(),(OMPLpath.getSolutionPath().getState(self.currentState)).getZ()))- pose if norm(dis_cur) < 1.5*self.radius: # go to next point if not (self.currentState+1) == OMPLpath.getSolutionPath().getStateCount(): self.currentState = self.currentState + 1 dis_cur = vstack(((OMPLpath.getSolutionPath().getState(self.currentState)).getX(),(OMPLpath.getSolutionPath().getState(self.currentState)).getY(),(OMPLpath.getSolutionPath().getState(self.currentState)).getZ()))- pose Vel = zeros([3,1]) Vel[0:3,0] = dis_cur/norm(dis_cur)*0.3 #TUNE THE SPEED LATER else: dis_cur = vstack(((OMPLpath.getSolutionPath().getState(self.currentState)).getX(),(OMPLpath.getSolutionPath().getState(self.currentState)).getY()))- pose[0:2] if norm(dis_cur) < 1.5*self.radius: # go to next point if not (self.currentState+1) == OMPLpath.getSolutionPath().getStateCount(): # head to the next node self.currentState = self.currentState + 1 dis_cur = vstack(((OMPLpath.getSolutionPath().getState(self.currentState)).getX(),(OMPLpath.getSolutionPath().getState(self.currentState)).getY()))- pose[0:2] Vel = zeros([2,1]) # set different speed for basicSim Vel[0:2,0] = dis_cur/norm(dis_cur)*0.5 #TUNE THE SPEED LATER return Vel def getNode(self,p, OMPLpath, last=False): """ This function return the heading node of the robot. (for 2D only now) The inputs are (given in order): p = the current x-y position of the robot E = edges of the tree (2 x No. of nodes on the tree) V = points of the tree (2 x No. of vertices) last = True, if the current region is the last region = False, if the current region is NOT the last region """ pose = mat(p).T #dis_cur = distance between current position and the next point dis_cur = vstack(((OMPLpath.getSolutionPath().getState(self.currentState)).getX(),(OMPLpath.getSolutionPath().getState(self.currentState)).getY()))- pose[0:2] if norm(dis_cur) < 1.5*self.radius: # go to next point if not (self.currentState+1) == OMPLpath.getSolutionPath().getStateCount(): # head to the next node self.currentState = self.currentState + 1 Node = zeros([2,1]) Node[0,0] = (OMPLpath.getSolutionPath().getState(self.currentState)).getX() Node[1,0] = (OMPLpath.getSolutionPath().getState(self.currentState)).getY() return Node def createRegionPolygon(self,region,hole = None): """ This function takes in the region points and make it a Polygon. """ if hole == None: pointArray = [x for x in region.getPoints()] else: pointArray = [x for x in region.getPoints(hole_id = hole)] pointArray = map(self.coordmap_map2lab, pointArray) regionPoints = [(pt[0],pt[1]) for pt in pointArray] formedPolygon= Polygon.Polygon(regionPoints) return formedPolygon def plotMap(self): """ Plotting regions and obstacles with matplotlib.pyplot number: figure number (see on top) """ if self.operate_system == 1: for regionName,regionPoly in self.map['polygon'].iteritems(): self.plotPoly(regionPoly,'k') def setPlotLimitXYZ(self): """ Set the limits for the plot on x, y and z axis """ if not self.system == 1: self.ax.set_xlim3d(self.BoundaryMaxMin[0], self.BoundaryMaxMin[1]) self.ax.set_ylim3d(self.BoundaryMaxMin[2], self.BoundaryMaxMin[3]) else: self.ax.set_xlim3d(self.BoundaryMaxMin[0], self.BoundaryMaxMin[1]) self.ax.set_ylim3d(self.BoundaryMaxMin[3], self.BoundaryMaxMin[2]) self.ax.set_zlim3d(-0.05,self.maxHeight) def plotPoly(self,c,string,w = 1): """ Plot polygons inside the boundary c = polygon to be plotted with matlabplot string = string that specify color w = width of the line plotting """ if bool(c): for i in range(len(c)): toPlot = Polygon.Polygon(c.contour(i)) & self.all if bool(toPlot): for j in range(len(toPlot)): BoundPolyPoints = asarray(PolyUtils.pointList(Polygon.Polygon(toPlot.contour(j)))) if self.operate_system == 2: self.ax.plot(BoundPolyPoints[:,0],BoundPolyPoints[:,1],string,linewidth=w) self.ax.plot([BoundPolyPoints[-1,0],BoundPolyPoints[0,0]],[BoundPolyPoints[-1,1],BoundPolyPoints[0,1]],string,linewidth=w) else: self.ax.plot(BoundPolyPoints[:,0],BoundPolyPoints[:,1],string,linewidth=w) self.ax.plot([BoundPolyPoints[-1,0],BoundPolyPoints[0,0]],[BoundPolyPoints[-1,1],BoundPolyPoints[0,1]],string,linewidth=w) self.setPlotLimitXYZ() #self.ax.get_figure().canvas.draw() # return an obstacle-based sampler def allocOBValidStateSampler(self,si): # we can perform any additional setup / configuration of a sampler here, # but there is nothing to tweak in case of the ObstacleBasedValidStateSampler. return ob.ObstacleBasedValidStateSampler(si) # return an instance of my sampler def alloc_MyValidStateSampler(self,si): return _MyValidStateSampler(si) # This function is needed, even when we can write a sampler like the one # above, because we need to check path segments for validity def isStateValid(self,state): # Let's pretend that the validity check is computationally relatively # expensive to emphasize the benefit of explicitly generating valid # samples sleep(.001) # Valid states satisfy the following constraints: # inside the current region and the next region if self.Space_Dimension == 3: region_considered = Polygon.Polygon(self.nextAndcurrentRegionPoly) bottom = state.getZ()-self.height/2 # bottom of the robot for i, name in enumerate(self.original_map['polygon']): if self.original_map['isObstacle'][name] is True: if self.original_map['height'][name] >= bottom: region_considered -=self.original_map['polygon'][name] state_polygon = PolyShapes.Circle(self.radius,(state.getX(),state.getY())) current_region = self.proj.rfi.regions[self.current_reg].name next_region = self.proj.rfi.regions[self.next_reg].name if self.currentRegionPoly.covers(state_polygon): height = self.map['height'][current_region] elif self.nextRegionPoly.covers(state_polygon): height = self.map['height'][next_region] else: height = min(self.map['height'][current_region],self.map['height'][next_region]) return region_considered.covers(state_polygon) and (state.getZ()+self.height/2) < height and (state.getZ()-self.height/2) > 0 else: return self.nextAndcurrentRegionPoly.covers(PolyShapes.Circle(self.radius,(state.getX(),state.getY()))) # This function generates control space needed information # control[0] for velocity # control[1] for angular velocity def propagate(self, start, control, duration, state): if self.system_print is True: print >>sys.__stdout__,"control[0]: " + str(control[0]) print >>sys.__stdout__,"control[1]: " + str(control[1]) print >>sys.__stdout__,"duration: " + str(duration) state.setX( start.getX() + control[0] * duration * cos(start.getYaw()) ) state.setY( start.getY() + control[0] * duration * sin(start.getYaw()) ) state.setYaw(start.getYaw() + control[1] * duration) def plan(self,goalPoints,current_region,next_region,samplerIndex): """ goal points: array that contains the coordinates of all the possible goal states current_reg: name of the current region (p1 etc) next_reg : name of the next region (p1 etc) """ # construct the state space we are planning in if self.Space_Dimension == 2: space = ob.SE2StateSpace() else: space = ob.SE3StateSpace() # set the bounds bounds = ob.RealVectorBounds(self.Space_Dimension) BoundaryMaxMin = self.all.boundingBox() #0-3:xmin, xmax, ymin and ymax bounds.setLow(0,BoundaryMaxMin[0]) # 0 stands for x axis bounds.setHigh(0,BoundaryMaxMin[1]) bounds.setLow(1,BoundaryMaxMin[2]) # 1 stands for y axis bounds.setHigh(1,BoundaryMaxMin[3]) if self.Space_Dimension == 3: bounds.setLow(2,0) # 2 stands for z axis bounds.setHigh(2,self.maxHeight) space.setBounds(bounds) if self.system_print == True: print "The bounding box of the boundary is: " + str(self.all.boundingBox() ) print "The volume of the bounding box is: " + str(bounds.getVolume()) if self.Geometric_Control == 'C': # create a control space cspace = oc.RealVectorControlSpace(space, self.Space_Dimension) # set the bounds for the control space # cbounds[0] for velocity # cbounds[1] for angular velocity cbounds = ob.RealVectorBounds(self.Space_Dimension) cbounds.setLow(0,0) cbounds.setHigh(0,max((BoundaryMaxMin[1]-BoundaryMaxMin[0]),(BoundaryMaxMin[3]-BoundaryMaxMin[2]))/100) cbounds.setLow(1,-pi/5) cbounds.setHigh(1,pi/5) cspace.setBounds(cbounds) if self.system_print == True: print cspace.settings() if self.Geometric_Control == 'G': # define a simple setup class ss = og.SimpleSetup(space) else: # define a simple setup class ss = oc.SimpleSetup(cspace) # set state validity checking for this space ss.setStatePropagator(oc.StatePropagatorFn(self.propagate)) ss.setStateValidityChecker(ob.StateValidityCheckerFn(self.isStateValid)) # create a start state start = ob.State(space) pose = self.pose_handler.getPose() #x,y,w,(z if using ROS quadrotor) start().setX(pose[0]) start().setY(pose[1]) if self.Space_Dimension == 2: while pose[2] > pi or pose[2] < -pi: if pose[2]> pi: pose[2] = pose[2] - pi else: pose[2] = pose[2] + pi start().setYaw(pose[2]) # else: start().setZ(pose[3]) start().rotation().setIdentity() if self.system_print is True and self.Space_Dimension == 3: print "start:" + str(start().getX())+","+str(start().getY()) +"," + str(start().getZ()) print goalPoints # create goal states goalStates = ob.GoalStates(ss.getSpaceInformation()) for i in range(shape(goalPoints)[1]): goal = ob.State(space) goal().setX(goalPoints[0,i]) goal().setY(goalPoints[1,i]) if self.Space_Dimension == 3: if self.system_print is True: print current_region,next_region print self.map['height'][current_region],self.map['height'][next_region] z_goalPoint = min(self.map['height'][current_region]/2,self.map['height'][next_region]/2) goal().setZ(z_goalPoint) goal().rotation().setIdentity() else: goal().setYaw(0.0) if self.plotting == True: if self.Space_Dimension == 3: self.ax.plot([goalPoints[0,i]],[goalPoints[1,i]],[z_goalPoint],'ro') else: self.ax.plot([goalPoints[0,i]],[goalPoints[1,i]],'ro') self.setPlotLimitXYZ() self.ax.get_figure().canvas.draw() goalStates.addState(goal) if self.system_print is True: print goalStates # set the start and goal states; ss.setGoal(goalStates) ss.setStartState(start) # set sampler (optional; the default is uniform sampling) si = ss.getSpaceInformation() # set planner planner_prep = self.planner_dictionary[self.Geometric_Control][self.planner] planner = planner_prep(si) ss.setPlanner(planner) if self.Geometric_Control == 'G': if not self.planner == 'PRM': planner.setRange(self.radius*2) if self.system_print is True: print "planner.getRange():" + str(planner.getRange()) #if not self.planner == 'RRTConnect': # planner.setGoalBias(0.5) else: # (optionally) set propagation step size si.setPropagationStepSize(1) #actually is the duration in propagate si.setMinMaxControlDuration(3,3) # is the no of steps taken with the same velocity and omega if self.system_print is True: print "radius: " +str(self.radius) print "si.getPropagationStepSize():" + str(si.getPropagationStepSize()) planner.setGoalBias(0.5) ss.setup() # attempt to solve the problem within ten seconds of planning time solved = ss.solve(1000.0) #10 if (solved): print("Found solution:") # print the path to screen print >>sys.__stdout__,(ss.getSolutionPath()) else: print("No solution found") if self.plotting == True : self.ax.set_title('Map with Geo/Control: '+str(self.Geometric_Control) + ",Planner:" +str(self.planner), fontsize=12) self.ax.set_xlabel('x') self.ax.set_ylabel('y') for i in range(ss.getSolutionPath().getStateCount()-1): if self.Space_Dimension == 3: self.ax.plot(((ss.getSolutionPath().getState(i)).getX(),(ss.getSolutionPath().getState(i+1)).getX()),((ss.getSolutionPath().getState(i)).getY(),(ss.getSolutionPath().getState(i+1)).getY()),((ss.getSolutionPath().getState(i)).getZ(),(ss.getSolutionPath().getState(i+1)).getZ()),'b') ro=Polygon.Shapes.Circle (self.radius,((ss.getSolutionPath().getState(i)).getX(),(ss.getSolutionPath().getState(i)).getY())) self.plotPoly(ro,'r') else: self.ax.plot(((ss.getSolutionPath().getState(i)).getX(),(ss.getSolutionPath().getState(i+1)).getX()),((ss.getSolutionPath().getState(i)).getY(),(ss.getSolutionPath().getState(i+1)).getY()),0,'b') self.setPlotLimitXYZ() #self.ax.get_figure().canvas.draw() return ss class _MyTkApp(threading.Thread): def __init__(self): self.fig = Figure(figsize=(5,4), dpi=100) self.ax = Axes3D(self.fig) threading.Thread.__init__(self) def getSharedData(self): # A dictionary of any objects that will need to be shared with other handlers return self def _quit(self): self.root.quit() # stops mainloop self.root.destroy() # this is necessary on Windows to prevent def run(self): self.root=Tk.Tk() #root self.root.wm_title("Embedding in TK") canvas = FigureCanvasTkAgg(self.fig, master=self.root) self.ax.mouse_init() canvas.show() canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) toolbar = NavigationToolbar2TkAgg( canvas, self.root ) toolbar.update() canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) button = Tk.Button(master=self.root, text='Quit', command=self._quit) button.pack(side=Tk.BOTTOM) self.root.mainloop()
gpl-3.0
nOkuda/classtm
check/cooccurrences/plot_cooccurrences.py
1
1755
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.ticker import FixedLocator from mpl_toolkits.axes_grid1.inset_locator import mark_inset, inset_axes import numpy as np def get_values(filename): """Get row normalized cooccurrences matrix""" with open(filename, 'rb') as ifh: result = np.load(ifh) return (result.T / result.sum(axis=1).T).T def plot_heat(ax, data): """Plot heatmap""" return ax.matshow(data, cmap='viridis') def plot_inset(ax, data, loc, loc1, loc2, limits): """Plot inset""" axins = inset_axes(ax, '25%', '25%', loc=loc) plot_heat(axins, data) axins.axis(limits) mark_inset(ax, axins, loc1=loc1, loc2=loc2, fc="none") axins.xaxis.set_major_locator(FixedLocator([limits[0], limits[1]])) axins.yaxis.set_major_locator(FixedLocator([limits[2], limits[3]])) def _main(): """Plot histograms of iteration data""" data = [ ('supervised', get_values('sup.Q')), ('overwatched', get_values('supnormed.Q')), ('free', get_values('projected.Q')), ] for datum in data: fig, ax = plt.subplots() # fig.set_size_inches(4, 4) caxins = plot_heat(ax, datum[1]) fig.colorbar(caxins) height, width = datum[1].shape # far right (since we're using matshow, the y axes limits need to be # flipped for the inset axes to match the diretion of the rest of the # plot) plot_inset(ax, datum[1], 7, 2, 4, [width-21, width-1, 20, 0]) # bottom corner plot_inset(ax, datum[1], 8, 1, 3, [width-21, width-1, height-1, height-21]) fig.savefig(datum[0]+'_cooccurrences.pdf', bbox_inches='tight') if __name__ == '__main__': _main()
gpl-3.0
ch3ll0v3k/scikit-learn
sklearn/tests/test_cross_validation.py
70
41943
"""Test the cross_validation module""" from __future__ import division import warnings import numpy as np from scipy.sparse import coo_matrix from scipy import stats from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_false from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_less from sklearn.utils.testing import assert_not_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_warns_message from sklearn.utils.testing import ignore_warnings from sklearn.utils.mocking import CheckingClassifier, MockDataFrame from sklearn import cross_validation as cval from sklearn.datasets import make_regression from sklearn.datasets import load_boston from sklearn.datasets import load_digits from sklearn.datasets import load_iris from sklearn.metrics import explained_variance_score from sklearn.metrics import make_scorer from sklearn.metrics import precision_score from sklearn.externals import six from sklearn.externals.six.moves import zip from sklearn.linear_model import Ridge from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.cluster import KMeans from sklearn.preprocessing import Imputer, LabelBinarizer from sklearn.pipeline import Pipeline class MockClassifier(object): """Dummy classifier to test the cross-validation""" def __init__(self, a=0, allow_nd=False): self.a = a self.allow_nd = allow_nd def fit(self, X, Y=None, sample_weight=None, class_prior=None, sparse_sample_weight=None, sparse_param=None, dummy_int=None, dummy_str=None, dummy_obj=None, callback=None): """The dummy arguments are to test that this fit function can accept non-array arguments through cross-validation, such as: - int - str (this is actually array-like) - object - function """ self.dummy_int = dummy_int self.dummy_str = dummy_str self.dummy_obj = dummy_obj if callback is not None: callback(self) if self.allow_nd: X = X.reshape(len(X), -1) if X.ndim >= 3 and not self.allow_nd: raise ValueError('X cannot be d') if sample_weight is not None: assert_true(sample_weight.shape[0] == X.shape[0], 'MockClassifier extra fit_param sample_weight.shape[0]' ' is {0}, should be {1}'.format(sample_weight.shape[0], X.shape[0])) if class_prior is not None: assert_true(class_prior.shape[0] == len(np.unique(y)), 'MockClassifier extra fit_param class_prior.shape[0]' ' is {0}, should be {1}'.format(class_prior.shape[0], len(np.unique(y)))) if sparse_sample_weight is not None: fmt = ('MockClassifier extra fit_param sparse_sample_weight' '.shape[0] is {0}, should be {1}') assert_true(sparse_sample_weight.shape[0] == X.shape[0], fmt.format(sparse_sample_weight.shape[0], X.shape[0])) if sparse_param is not None: fmt = ('MockClassifier extra fit_param sparse_param.shape ' 'is ({0}, {1}), should be ({2}, {3})') assert_true(sparse_param.shape == P_sparse.shape, fmt.format(sparse_param.shape[0], sparse_param.shape[1], P_sparse.shape[0], P_sparse.shape[1])) return self def predict(self, T): if self.allow_nd: T = T.reshape(len(T), -1) return T[:, 0] def score(self, X=None, Y=None): return 1. / (1 + np.abs(self.a)) def get_params(self, deep=False): return {'a': self.a, 'allow_nd': self.allow_nd} X = np.ones((10, 2)) X_sparse = coo_matrix(X) W_sparse = coo_matrix((np.array([1]), (np.array([1]), np.array([0]))), shape=(10, 1)) P_sparse = coo_matrix(np.eye(5)) y = np.arange(10) // 2 ############################################################################## # Tests def check_valid_split(train, test, n_samples=None): # Use python sets to get more informative assertion failure messages train, test = set(train), set(test) # Train and test split should not overlap assert_equal(train.intersection(test), set()) if n_samples is not None: # Check that the union of train an test split cover all the indices assert_equal(train.union(test), set(range(n_samples))) def check_cv_coverage(cv, expected_n_iter=None, n_samples=None): # Check that a all the samples appear at least once in a test fold if expected_n_iter is not None: assert_equal(len(cv), expected_n_iter) else: expected_n_iter = len(cv) collected_test_samples = set() iterations = 0 for train, test in cv: check_valid_split(train, test, n_samples=n_samples) iterations += 1 collected_test_samples.update(test) # Check that the accumulated test samples cover the whole dataset assert_equal(iterations, expected_n_iter) if n_samples is not None: assert_equal(collected_test_samples, set(range(n_samples))) def test_kfold_valueerrors(): # Check that errors are raised if there is not enough samples assert_raises(ValueError, cval.KFold, 3, 4) # Check that a warning is raised if the least populated class has too few # members. y = [3, 3, -1, -1, 2] cv = assert_warns_message(Warning, "The least populated class", cval.StratifiedKFold, y, 3) # Check that despite the warning the folds are still computed even # though all the classes are not necessarily represented at on each # side of the split at each split check_cv_coverage(cv, expected_n_iter=3, n_samples=len(y)) # Error when number of folds is <= 1 assert_raises(ValueError, cval.KFold, 2, 0) assert_raises(ValueError, cval.KFold, 2, 1) assert_raises(ValueError, cval.StratifiedKFold, y, 0) assert_raises(ValueError, cval.StratifiedKFold, y, 1) # When n is not integer: assert_raises(ValueError, cval.KFold, 2.5, 2) # When n_folds is not integer: assert_raises(ValueError, cval.KFold, 5, 1.5) assert_raises(ValueError, cval.StratifiedKFold, y, 1.5) def test_kfold_indices(): # Check all indices are returned in the test folds kf = cval.KFold(300, 3) check_cv_coverage(kf, expected_n_iter=3, n_samples=300) # Check all indices are returned in the test folds even when equal-sized # folds are not possible kf = cval.KFold(17, 3) check_cv_coverage(kf, expected_n_iter=3, n_samples=17) def test_kfold_no_shuffle(): # Manually check that KFold preserves the data ordering on toy datasets splits = iter(cval.KFold(4, 2)) train, test = next(splits) assert_array_equal(test, [0, 1]) assert_array_equal(train, [2, 3]) train, test = next(splits) assert_array_equal(test, [2, 3]) assert_array_equal(train, [0, 1]) splits = iter(cval.KFold(5, 2)) train, test = next(splits) assert_array_equal(test, [0, 1, 2]) assert_array_equal(train, [3, 4]) train, test = next(splits) assert_array_equal(test, [3, 4]) assert_array_equal(train, [0, 1, 2]) def test_stratified_kfold_no_shuffle(): # Manually check that StratifiedKFold preserves the data ordering as much # as possible on toy datasets in order to avoid hiding sample dependencies # when possible splits = iter(cval.StratifiedKFold([1, 1, 0, 0], 2)) train, test = next(splits) assert_array_equal(test, [0, 2]) assert_array_equal(train, [1, 3]) train, test = next(splits) assert_array_equal(test, [1, 3]) assert_array_equal(train, [0, 2]) splits = iter(cval.StratifiedKFold([1, 1, 1, 0, 0, 0, 0], 2)) train, test = next(splits) assert_array_equal(test, [0, 1, 3, 4]) assert_array_equal(train, [2, 5, 6]) train, test = next(splits) assert_array_equal(test, [2, 5, 6]) assert_array_equal(train, [0, 1, 3, 4]) def test_stratified_kfold_ratios(): # Check that stratified kfold preserves label ratios in individual splits # Repeat with shuffling turned off and on n_samples = 1000 labels = np.array([4] * int(0.10 * n_samples) + [0] * int(0.89 * n_samples) + [1] * int(0.01 * n_samples)) for shuffle in [False, True]: for train, test in cval.StratifiedKFold(labels, 5, shuffle=shuffle): assert_almost_equal(np.sum(labels[train] == 4) / len(train), 0.10, 2) assert_almost_equal(np.sum(labels[train] == 0) / len(train), 0.89, 2) assert_almost_equal(np.sum(labels[train] == 1) / len(train), 0.01, 2) assert_almost_equal(np.sum(labels[test] == 4) / len(test), 0.10, 2) assert_almost_equal(np.sum(labels[test] == 0) / len(test), 0.89, 2) assert_almost_equal(np.sum(labels[test] == 1) / len(test), 0.01, 2) def test_kfold_balance(): # Check that KFold returns folds with balanced sizes for kf in [cval.KFold(i, 5) for i in range(11, 17)]: sizes = [] for _, test in kf: sizes.append(len(test)) assert_true((np.max(sizes) - np.min(sizes)) <= 1) assert_equal(np.sum(sizes), kf.n) def test_stratifiedkfold_balance(): # Check that KFold returns folds with balanced sizes (only when # stratification is possible) # Repeat with shuffling turned off and on labels = [0] * 3 + [1] * 14 for shuffle in [False, True]: for skf in [cval.StratifiedKFold(labels[:i], 3, shuffle=shuffle) for i in range(11, 17)]: sizes = [] for _, test in skf: sizes.append(len(test)) assert_true((np.max(sizes) - np.min(sizes)) <= 1) assert_equal(np.sum(sizes), skf.n) def test_shuffle_kfold(): # Check the indices are shuffled properly, and that all indices are # returned in the different test folds kf = cval.KFold(300, 3, shuffle=True, random_state=0) ind = np.arange(300) all_folds = None for train, test in kf: sorted_array = np.arange(100) assert_true(np.any(sorted_array != ind[train])) sorted_array = np.arange(101, 200) assert_true(np.any(sorted_array != ind[train])) sorted_array = np.arange(201, 300) assert_true(np.any(sorted_array != ind[train])) if all_folds is None: all_folds = ind[test].copy() else: all_folds = np.concatenate((all_folds, ind[test])) all_folds.sort() assert_array_equal(all_folds, ind) def test_shuffle_stratifiedkfold(): # Check that shuffling is happening when requested, and for proper # sample coverage labels = [0] * 20 + [1] * 20 kf0 = list(cval.StratifiedKFold(labels, 5, shuffle=True, random_state=0)) kf1 = list(cval.StratifiedKFold(labels, 5, shuffle=True, random_state=1)) for (_, test0), (_, test1) in zip(kf0, kf1): assert_true(set(test0) != set(test1)) check_cv_coverage(kf0, expected_n_iter=5, n_samples=40) def test_kfold_can_detect_dependent_samples_on_digits(): # see #2372 # The digits samples are dependent: they are apparently grouped by authors # although we don't have any information on the groups segment locations # for this data. We can highlight this fact be computing k-fold cross- # validation with and without shuffling: we observe that the shuffling case # wrongly makes the IID assumption and is therefore too optimistic: it # estimates a much higher accuracy (around 0.96) than than the non # shuffling variant (around 0.86). digits = load_digits() X, y = digits.data[:800], digits.target[:800] model = SVC(C=10, gamma=0.005) n = len(y) cv = cval.KFold(n, 5, shuffle=False) mean_score = cval.cross_val_score(model, X, y, cv=cv).mean() assert_greater(0.88, mean_score) assert_greater(mean_score, 0.85) # Shuffling the data artificially breaks the dependency and hides the # overfitting of the model with regards to the writing style of the authors # by yielding a seriously overestimated score: cv = cval.KFold(n, 5, shuffle=True, random_state=0) mean_score = cval.cross_val_score(model, X, y, cv=cv).mean() assert_greater(mean_score, 0.95) cv = cval.KFold(n, 5, shuffle=True, random_state=1) mean_score = cval.cross_val_score(model, X, y, cv=cv).mean() assert_greater(mean_score, 0.95) # Similarly, StratifiedKFold should try to shuffle the data as little # as possible (while respecting the balanced class constraints) # and thus be able to detect the dependency by not overestimating # the CV score either. As the digits dataset is approximately balanced # the estimated mean score is close to the score measured with # non-shuffled KFold cv = cval.StratifiedKFold(y, 5) mean_score = cval.cross_val_score(model, X, y, cv=cv).mean() assert_greater(0.88, mean_score) assert_greater(mean_score, 0.85) def test_shuffle_split(): ss1 = cval.ShuffleSplit(10, test_size=0.2, random_state=0) ss2 = cval.ShuffleSplit(10, test_size=2, random_state=0) ss3 = cval.ShuffleSplit(10, test_size=np.int32(2), random_state=0) for typ in six.integer_types: ss4 = cval.ShuffleSplit(10, test_size=typ(2), random_state=0) for t1, t2, t3, t4 in zip(ss1, ss2, ss3, ss4): assert_array_equal(t1[0], t2[0]) assert_array_equal(t2[0], t3[0]) assert_array_equal(t3[0], t4[0]) assert_array_equal(t1[1], t2[1]) assert_array_equal(t2[1], t3[1]) assert_array_equal(t3[1], t4[1]) def test_stratified_shuffle_split_init(): y = np.asarray([0, 1, 1, 1, 2, 2, 2]) # Check that error is raised if there is a class with only one sample assert_raises(ValueError, cval.StratifiedShuffleSplit, y, 3, 0.2) # Check that error is raised if the test set size is smaller than n_classes assert_raises(ValueError, cval.StratifiedShuffleSplit, y, 3, 2) # Check that error is raised if the train set size is smaller than # n_classes assert_raises(ValueError, cval.StratifiedShuffleSplit, y, 3, 3, 2) y = np.asarray([0, 0, 0, 1, 1, 1, 2, 2, 2]) # Check that errors are raised if there is not enough samples assert_raises(ValueError, cval.StratifiedShuffleSplit, y, 3, 0.5, 0.6) assert_raises(ValueError, cval.StratifiedShuffleSplit, y, 3, 8, 0.6) assert_raises(ValueError, cval.StratifiedShuffleSplit, y, 3, 0.6, 8) # Train size or test size too small assert_raises(ValueError, cval.StratifiedShuffleSplit, y, train_size=2) assert_raises(ValueError, cval.StratifiedShuffleSplit, y, test_size=2) def test_stratified_shuffle_split_iter(): ys = [np.array([1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]), np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]), np.array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2]), np.array([1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4]), np.array([-1] * 800 + [1] * 50) ] for y in ys: sss = cval.StratifiedShuffleSplit(y, 6, test_size=0.33, random_state=0) for train, test in sss: assert_array_equal(np.unique(y[train]), np.unique(y[test])) # Checks if folds keep classes proportions p_train = (np.bincount(np.unique(y[train], return_inverse=True)[1]) / float(len(y[train]))) p_test = (np.bincount(np.unique(y[test], return_inverse=True)[1]) / float(len(y[test]))) assert_array_almost_equal(p_train, p_test, 1) assert_equal(y[train].size + y[test].size, y.size) assert_array_equal(np.lib.arraysetops.intersect1d(train, test), []) def test_stratified_shuffle_split_even(): # Test the StratifiedShuffleSplit, indices are drawn with a # equal chance n_folds = 5 n_iter = 1000 def assert_counts_are_ok(idx_counts, p): # Here we test that the distribution of the counts # per index is close enough to a binomial threshold = 0.05 / n_splits bf = stats.binom(n_splits, p) for count in idx_counts: p = bf.pmf(count) assert_true(p > threshold, "An index is not drawn with chance corresponding " "to even draws") for n_samples in (6, 22): labels = np.array((n_samples // 2) * [0, 1]) splits = cval.StratifiedShuffleSplit(labels, n_iter=n_iter, test_size=1. / n_folds, random_state=0) train_counts = [0] * n_samples test_counts = [0] * n_samples n_splits = 0 for train, test in splits: n_splits += 1 for counter, ids in [(train_counts, train), (test_counts, test)]: for id in ids: counter[id] += 1 assert_equal(n_splits, n_iter) assert_equal(len(train), splits.n_train) assert_equal(len(test), splits.n_test) assert_equal(len(set(train).intersection(test)), 0) label_counts = np.unique(labels) assert_equal(splits.test_size, 1.0 / n_folds) assert_equal(splits.n_train + splits.n_test, len(labels)) assert_equal(len(label_counts), 2) ex_test_p = float(splits.n_test) / n_samples ex_train_p = float(splits.n_train) / n_samples assert_counts_are_ok(train_counts, ex_train_p) assert_counts_are_ok(test_counts, ex_test_p) def test_predefinedsplit_with_kfold_split(): # Check that PredefinedSplit can reproduce a split generated by Kfold. folds = -1 * np.ones(10) kf_train = [] kf_test = [] for i, (train_ind, test_ind) in enumerate(cval.KFold(10, 5, shuffle=True)): kf_train.append(train_ind) kf_test.append(test_ind) folds[test_ind] = i ps_train = [] ps_test = [] ps = cval.PredefinedSplit(folds) for train_ind, test_ind in ps: ps_train.append(train_ind) ps_test.append(test_ind) assert_array_equal(ps_train, kf_train) assert_array_equal(ps_test, kf_test) def test_leave_label_out_changing_labels(): # Check that LeaveOneLabelOut and LeavePLabelOut work normally if # the labels variable is changed before calling __iter__ labels = np.array([0, 1, 2, 1, 1, 2, 0, 0]) labels_changing = np.array(labels, copy=True) lolo = cval.LeaveOneLabelOut(labels) lolo_changing = cval.LeaveOneLabelOut(labels_changing) lplo = cval.LeavePLabelOut(labels, p=2) lplo_changing = cval.LeavePLabelOut(labels_changing, p=2) labels_changing[:] = 0 for llo, llo_changing in [(lolo, lolo_changing), (lplo, lplo_changing)]: for (train, test), (train_chan, test_chan) in zip(llo, llo_changing): assert_array_equal(train, train_chan) assert_array_equal(test, test_chan) def test_cross_val_score(): clf = MockClassifier() for a in range(-10, 10): clf.a = a # Smoke test scores = cval.cross_val_score(clf, X, y) assert_array_equal(scores, clf.score(X, y)) # test with multioutput y scores = cval.cross_val_score(clf, X_sparse, X) assert_array_equal(scores, clf.score(X_sparse, X)) scores = cval.cross_val_score(clf, X_sparse, y) assert_array_equal(scores, clf.score(X_sparse, y)) # test with multioutput y scores = cval.cross_val_score(clf, X_sparse, X) assert_array_equal(scores, clf.score(X_sparse, X)) # test with X and y as list list_check = lambda x: isinstance(x, list) clf = CheckingClassifier(check_X=list_check) scores = cval.cross_val_score(clf, X.tolist(), y.tolist()) clf = CheckingClassifier(check_y=list_check) scores = cval.cross_val_score(clf, X, y.tolist()) assert_raises(ValueError, cval.cross_val_score, clf, X, y, scoring="sklearn") # test with 3d X and X_3d = X[:, :, np.newaxis] clf = MockClassifier(allow_nd=True) scores = cval.cross_val_score(clf, X_3d, y) clf = MockClassifier(allow_nd=False) assert_raises(ValueError, cval.cross_val_score, clf, X_3d, y) def test_cross_val_score_pandas(): # check cross_val_score doesn't destroy pandas dataframe types = [(MockDataFrame, MockDataFrame)] try: from pandas import Series, DataFrame types.append((Series, DataFrame)) except ImportError: pass for TargetType, InputFeatureType in types: # X dataframe, y series X_df, y_ser = InputFeatureType(X), TargetType(y) check_df = lambda x: isinstance(x, InputFeatureType) check_series = lambda x: isinstance(x, TargetType) clf = CheckingClassifier(check_X=check_df, check_y=check_series) cval.cross_val_score(clf, X_df, y_ser) def test_cross_val_score_mask(): # test that cross_val_score works with boolean masks svm = SVC(kernel="linear") iris = load_iris() X, y = iris.data, iris.target cv_indices = cval.KFold(len(y), 5) scores_indices = cval.cross_val_score(svm, X, y, cv=cv_indices) cv_indices = cval.KFold(len(y), 5) cv_masks = [] for train, test in cv_indices: mask_train = np.zeros(len(y), dtype=np.bool) mask_test = np.zeros(len(y), dtype=np.bool) mask_train[train] = 1 mask_test[test] = 1 cv_masks.append((train, test)) scores_masks = cval.cross_val_score(svm, X, y, cv=cv_masks) assert_array_equal(scores_indices, scores_masks) def test_cross_val_score_precomputed(): # test for svm with precomputed kernel svm = SVC(kernel="precomputed") iris = load_iris() X, y = iris.data, iris.target linear_kernel = np.dot(X, X.T) score_precomputed = cval.cross_val_score(svm, linear_kernel, y) svm = SVC(kernel="linear") score_linear = cval.cross_val_score(svm, X, y) assert_array_equal(score_precomputed, score_linear) # Error raised for non-square X svm = SVC(kernel="precomputed") assert_raises(ValueError, cval.cross_val_score, svm, X, y) # test error is raised when the precomputed kernel is not array-like # or sparse assert_raises(ValueError, cval.cross_val_score, svm, linear_kernel.tolist(), y) def test_cross_val_score_fit_params(): clf = MockClassifier() n_samples = X.shape[0] n_classes = len(np.unique(y)) DUMMY_INT = 42 DUMMY_STR = '42' DUMMY_OBJ = object() def assert_fit_params(clf): # Function to test that the values are passed correctly to the # classifier arguments for non-array type assert_equal(clf.dummy_int, DUMMY_INT) assert_equal(clf.dummy_str, DUMMY_STR) assert_equal(clf.dummy_obj, DUMMY_OBJ) fit_params = {'sample_weight': np.ones(n_samples), 'class_prior': np.ones(n_classes) / n_classes, 'sparse_sample_weight': W_sparse, 'sparse_param': P_sparse, 'dummy_int': DUMMY_INT, 'dummy_str': DUMMY_STR, 'dummy_obj': DUMMY_OBJ, 'callback': assert_fit_params} cval.cross_val_score(clf, X, y, fit_params=fit_params) def test_cross_val_score_score_func(): clf = MockClassifier() _score_func_args = [] def score_func(y_test, y_predict): _score_func_args.append((y_test, y_predict)) return 1.0 with warnings.catch_warnings(record=True): scoring = make_scorer(score_func) score = cval.cross_val_score(clf, X, y, scoring=scoring) assert_array_equal(score, [1.0, 1.0, 1.0]) assert len(_score_func_args) == 3 def test_cross_val_score_errors(): class BrokenEstimator: pass assert_raises(TypeError, cval.cross_val_score, BrokenEstimator(), X) def test_train_test_split_errors(): assert_raises(ValueError, cval.train_test_split) assert_raises(ValueError, cval.train_test_split, range(3), train_size=1.1) assert_raises(ValueError, cval.train_test_split, range(3), test_size=0.6, train_size=0.6) assert_raises(ValueError, cval.train_test_split, range(3), test_size=np.float32(0.6), train_size=np.float32(0.6)) assert_raises(ValueError, cval.train_test_split, range(3), test_size="wrong_type") assert_raises(ValueError, cval.train_test_split, range(3), test_size=2, train_size=4) assert_raises(TypeError, cval.train_test_split, range(3), some_argument=1.1) assert_raises(ValueError, cval.train_test_split, range(3), range(42)) def test_train_test_split(): X = np.arange(100).reshape((10, 10)) X_s = coo_matrix(X) y = np.arange(10) # simple test split = cval.train_test_split(X, y, test_size=None, train_size=.5) X_train, X_test, y_train, y_test = split assert_equal(len(y_test), len(y_train)) # test correspondence of X and y assert_array_equal(X_train[:, 0], y_train * 10) assert_array_equal(X_test[:, 0], y_test * 10) # conversion of lists to arrays (deprecated?) with warnings.catch_warnings(record=True): split = cval.train_test_split(X, X_s, y.tolist(), allow_lists=False) X_train, X_test, X_s_train, X_s_test, y_train, y_test = split assert_array_equal(X_train, X_s_train.toarray()) assert_array_equal(X_test, X_s_test.toarray()) # don't convert lists to anything else by default split = cval.train_test_split(X, X_s, y.tolist()) X_train, X_test, X_s_train, X_s_test, y_train, y_test = split assert_true(isinstance(y_train, list)) assert_true(isinstance(y_test, list)) # allow nd-arrays X_4d = np.arange(10 * 5 * 3 * 2).reshape(10, 5, 3, 2) y_3d = np.arange(10 * 7 * 11).reshape(10, 7, 11) split = cval.train_test_split(X_4d, y_3d) assert_equal(split[0].shape, (7, 5, 3, 2)) assert_equal(split[1].shape, (3, 5, 3, 2)) assert_equal(split[2].shape, (7, 7, 11)) assert_equal(split[3].shape, (3, 7, 11)) # test stratification option y = np.array([1, 1, 1, 1, 2, 2, 2, 2]) for test_size, exp_test_size in zip([2, 4, 0.25, 0.5, 0.75], [2, 4, 2, 4, 6]): train, test = cval.train_test_split(y, test_size=test_size, stratify=y, random_state=0) assert_equal(len(test), exp_test_size) assert_equal(len(test) + len(train), len(y)) # check the 1:1 ratio of ones and twos in the data is preserved assert_equal(np.sum(train == 1), np.sum(train == 2)) def train_test_split_pandas(): # check cross_val_score doesn't destroy pandas dataframe types = [MockDataFrame] try: from pandas import DataFrame types.append(DataFrame) except ImportError: pass for InputFeatureType in types: # X dataframe X_df = InputFeatureType(X) X_train, X_test = cval.train_test_split(X_df) assert_true(isinstance(X_train, InputFeatureType)) assert_true(isinstance(X_test, InputFeatureType)) def train_test_split_mock_pandas(): # X mock dataframe X_df = MockDataFrame(X) X_train, X_test = cval.train_test_split(X_df) assert_true(isinstance(X_train, MockDataFrame)) assert_true(isinstance(X_test, MockDataFrame)) X_train_arr, X_test_arr = cval.train_test_split(X_df, allow_lists=False) assert_true(isinstance(X_train_arr, np.ndarray)) assert_true(isinstance(X_test_arr, np.ndarray)) def test_cross_val_score_with_score_func_classification(): iris = load_iris() clf = SVC(kernel='linear') # Default score (should be the accuracy score) scores = cval.cross_val_score(clf, iris.data, iris.target, cv=5) assert_array_almost_equal(scores, [0.97, 1., 0.97, 0.97, 1.], 2) # Correct classification score (aka. zero / one score) - should be the # same as the default estimator score zo_scores = cval.cross_val_score(clf, iris.data, iris.target, scoring="accuracy", cv=5) assert_array_almost_equal(zo_scores, [0.97, 1., 0.97, 0.97, 1.], 2) # F1 score (class are balanced so f1_score should be equal to zero/one # score f1_scores = cval.cross_val_score(clf, iris.data, iris.target, scoring="f1_weighted", cv=5) assert_array_almost_equal(f1_scores, [0.97, 1., 0.97, 0.97, 1.], 2) def test_cross_val_score_with_score_func_regression(): X, y = make_regression(n_samples=30, n_features=20, n_informative=5, random_state=0) reg = Ridge() # Default score of the Ridge regression estimator scores = cval.cross_val_score(reg, X, y, cv=5) assert_array_almost_equal(scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) # R2 score (aka. determination coefficient) - should be the # same as the default estimator score r2_scores = cval.cross_val_score(reg, X, y, scoring="r2", cv=5) assert_array_almost_equal(r2_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) # Mean squared error; this is a loss function, so "scores" are negative mse_scores = cval.cross_val_score(reg, X, y, cv=5, scoring="mean_squared_error") expected_mse = np.array([-763.07, -553.16, -274.38, -273.26, -1681.99]) assert_array_almost_equal(mse_scores, expected_mse, 2) # Explained variance scoring = make_scorer(explained_variance_score) ev_scores = cval.cross_val_score(reg, X, y, cv=5, scoring=scoring) assert_array_almost_equal(ev_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) def test_permutation_score(): iris = load_iris() X = iris.data X_sparse = coo_matrix(X) y = iris.target svm = SVC(kernel='linear') cv = cval.StratifiedKFold(y, 2) score, scores, pvalue = cval.permutation_test_score( svm, X, y, n_permutations=30, cv=cv, scoring="accuracy") assert_greater(score, 0.9) assert_almost_equal(pvalue, 0.0, 1) score_label, _, pvalue_label = cval.permutation_test_score( svm, X, y, n_permutations=30, cv=cv, scoring="accuracy", labels=np.ones(y.size), random_state=0) assert_true(score_label == score) assert_true(pvalue_label == pvalue) # check that we obtain the same results with a sparse representation svm_sparse = SVC(kernel='linear') cv_sparse = cval.StratifiedKFold(y, 2) score_label, _, pvalue_label = cval.permutation_test_score( svm_sparse, X_sparse, y, n_permutations=30, cv=cv_sparse, scoring="accuracy", labels=np.ones(y.size), random_state=0) assert_true(score_label == score) assert_true(pvalue_label == pvalue) # test with custom scoring object def custom_score(y_true, y_pred): return (((y_true == y_pred).sum() - (y_true != y_pred).sum()) / y_true.shape[0]) scorer = make_scorer(custom_score) score, _, pvalue = cval.permutation_test_score( svm, X, y, n_permutations=100, scoring=scorer, cv=cv, random_state=0) assert_almost_equal(score, .93, 2) assert_almost_equal(pvalue, 0.01, 3) # set random y y = np.mod(np.arange(len(y)), 3) score, scores, pvalue = cval.permutation_test_score( svm, X, y, n_permutations=30, cv=cv, scoring="accuracy") assert_less(score, 0.5) assert_greater(pvalue, 0.2) def test_cross_val_generator_with_indices(): X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) y = np.array([1, 1, 2, 2]) labels = np.array([1, 2, 3, 4]) # explicitly passing indices value is deprecated loo = cval.LeaveOneOut(4) lpo = cval.LeavePOut(4, 2) kf = cval.KFold(4, 2) skf = cval.StratifiedKFold(y, 2) lolo = cval.LeaveOneLabelOut(labels) lopo = cval.LeavePLabelOut(labels, 2) ps = cval.PredefinedSplit([1, 1, 2, 2]) ss = cval.ShuffleSplit(2) for cv in [loo, lpo, kf, skf, lolo, lopo, ss, ps]: for train, test in cv: assert_not_equal(np.asarray(train).dtype.kind, 'b') assert_not_equal(np.asarray(train).dtype.kind, 'b') X[train], X[test] y[train], y[test] @ignore_warnings def test_cross_val_generator_with_default_indices(): X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) y = np.array([1, 1, 2, 2]) labels = np.array([1, 2, 3, 4]) loo = cval.LeaveOneOut(4) lpo = cval.LeavePOut(4, 2) kf = cval.KFold(4, 2) skf = cval.StratifiedKFold(y, 2) lolo = cval.LeaveOneLabelOut(labels) lopo = cval.LeavePLabelOut(labels, 2) ss = cval.ShuffleSplit(2) ps = cval.PredefinedSplit([1, 1, 2, 2]) for cv in [loo, lpo, kf, skf, lolo, lopo, ss, ps]: for train, test in cv: assert_not_equal(np.asarray(train).dtype.kind, 'b') assert_not_equal(np.asarray(train).dtype.kind, 'b') X[train], X[test] y[train], y[test] def test_shufflesplit_errors(): assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=2.0) assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=1.0) assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=0.1, train_size=0.95) assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=11) assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=10) assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=8, train_size=3) assert_raises(ValueError, cval.ShuffleSplit, 10, train_size=1j) assert_raises(ValueError, cval.ShuffleSplit, 10, test_size=None, train_size=None) def test_shufflesplit_reproducible(): # Check that iterating twice on the ShuffleSplit gives the same # sequence of train-test when the random_state is given ss = cval.ShuffleSplit(10, random_state=21) assert_array_equal(list(a for a, b in ss), list(a for a, b in ss)) def test_safe_split_with_precomputed_kernel(): clf = SVC() clfp = SVC(kernel="precomputed") iris = load_iris() X, y = iris.data, iris.target K = np.dot(X, X.T) cv = cval.ShuffleSplit(X.shape[0], test_size=0.25, random_state=0) tr, te = list(cv)[0] X_tr, y_tr = cval._safe_split(clf, X, y, tr) K_tr, y_tr2 = cval._safe_split(clfp, K, y, tr) assert_array_almost_equal(K_tr, np.dot(X_tr, X_tr.T)) X_te, y_te = cval._safe_split(clf, X, y, te, tr) K_te, y_te2 = cval._safe_split(clfp, K, y, te, tr) assert_array_almost_equal(K_te, np.dot(X_te, X_tr.T)) def test_cross_val_score_allow_nans(): # Check that cross_val_score allows input data with NaNs X = np.arange(200, dtype=np.float64).reshape(10, -1) X[2, :] = np.nan y = np.repeat([0, 1], X.shape[0] / 2) p = Pipeline([ ('imputer', Imputer(strategy='mean', missing_values='NaN')), ('classifier', MockClassifier()), ]) cval.cross_val_score(p, X, y, cv=5) def test_train_test_split_allow_nans(): # Check that train_test_split allows input data with NaNs X = np.arange(200, dtype=np.float64).reshape(10, -1) X[2, :] = np.nan y = np.repeat([0, 1], X.shape[0] / 2) cval.train_test_split(X, y, test_size=0.2, random_state=42) def test_permutation_test_score_allow_nans(): # Check that permutation_test_score allows input data with NaNs X = np.arange(200, dtype=np.float64).reshape(10, -1) X[2, :] = np.nan y = np.repeat([0, 1], X.shape[0] / 2) p = Pipeline([ ('imputer', Imputer(strategy='mean', missing_values='NaN')), ('classifier', MockClassifier()), ]) cval.permutation_test_score(p, X, y, cv=5) def test_check_cv_return_types(): X = np.ones((9, 2)) cv = cval.check_cv(3, X, classifier=False) assert_true(isinstance(cv, cval.KFold)) y_binary = np.array([0, 1, 0, 1, 0, 0, 1, 1, 1]) cv = cval.check_cv(3, X, y_binary, classifier=True) assert_true(isinstance(cv, cval.StratifiedKFold)) y_multiclass = np.array([0, 1, 0, 1, 2, 1, 2, 0, 2]) cv = cval.check_cv(3, X, y_multiclass, classifier=True) assert_true(isinstance(cv, cval.StratifiedKFold)) X = np.ones((5, 2)) y_seq_of_seqs = [[], [1, 2], [3], [0, 1, 3], [2]] with warnings.catch_warnings(record=True): # deprecated sequence of sequence format cv = cval.check_cv(3, X, y_seq_of_seqs, classifier=True) assert_true(isinstance(cv, cval.KFold)) y_indicator_matrix = LabelBinarizer().fit_transform(y_seq_of_seqs) cv = cval.check_cv(3, X, y_indicator_matrix, classifier=True) assert_true(isinstance(cv, cval.KFold)) y_multioutput = np.array([[1, 2], [0, 3], [0, 0], [3, 1], [2, 0]]) cv = cval.check_cv(3, X, y_multioutput, classifier=True) assert_true(isinstance(cv, cval.KFold)) def test_cross_val_score_multilabel(): X = np.array([[-3, 4], [2, 4], [3, 3], [0, 2], [-3, 1], [-2, 1], [0, 0], [-2, -1], [-1, -2], [1, -2]]) y = np.array([[1, 1], [0, 1], [0, 1], [0, 1], [1, 1], [0, 1], [1, 0], [1, 1], [1, 0], [0, 0]]) clf = KNeighborsClassifier(n_neighbors=1) scoring_micro = make_scorer(precision_score, average='micro') scoring_macro = make_scorer(precision_score, average='macro') scoring_samples = make_scorer(precision_score, average='samples') score_micro = cval.cross_val_score(clf, X, y, scoring=scoring_micro, cv=5) score_macro = cval.cross_val_score(clf, X, y, scoring=scoring_macro, cv=5) score_samples = cval.cross_val_score(clf, X, y, scoring=scoring_samples, cv=5) assert_almost_equal(score_micro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 3]) assert_almost_equal(score_macro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4]) assert_almost_equal(score_samples, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4]) def test_cross_val_predict(): boston = load_boston() X, y = boston.data, boston.target cv = cval.KFold(len(boston.target)) est = Ridge() # Naive loop (should be same as cross_val_predict): preds2 = np.zeros_like(y) for train, test in cv: est.fit(X[train], y[train]) preds2[test] = est.predict(X[test]) preds = cval.cross_val_predict(est, X, y, cv=cv) assert_array_almost_equal(preds, preds2) preds = cval.cross_val_predict(est, X, y) assert_equal(len(preds), len(y)) cv = cval.LeaveOneOut(len(y)) preds = cval.cross_val_predict(est, X, y, cv=cv) assert_equal(len(preds), len(y)) Xsp = X.copy() Xsp *= (Xsp > np.median(Xsp)) Xsp = coo_matrix(Xsp) preds = cval.cross_val_predict(est, Xsp, y) assert_array_almost_equal(len(preds), len(y)) preds = cval.cross_val_predict(KMeans(), X) assert_equal(len(preds), len(y)) def bad_cv(): for i in range(4): yield np.array([0, 1, 2, 3]), np.array([4, 5, 6, 7, 8]) assert_raises(ValueError, cval.cross_val_predict, est, X, y, cv=bad_cv()) def test_cross_val_predict_input_types(): clf = Ridge() # Smoke test predictions = cval.cross_val_predict(clf, X, y) assert_equal(predictions.shape, (10,)) # test with multioutput y predictions = cval.cross_val_predict(clf, X_sparse, X) assert_equal(predictions.shape, (10, 2)) predictions = cval.cross_val_predict(clf, X_sparse, y) assert_array_equal(predictions.shape, (10,)) # test with multioutput y predictions = cval.cross_val_predict(clf, X_sparse, X) assert_array_equal(predictions.shape, (10, 2)) # test with X and y as list list_check = lambda x: isinstance(x, list) clf = CheckingClassifier(check_X=list_check) predictions = cval.cross_val_predict(clf, X.tolist(), y.tolist()) clf = CheckingClassifier(check_y=list_check) predictions = cval.cross_val_predict(clf, X, y.tolist()) # test with 3d X and X_3d = X[:, :, np.newaxis] check_3d = lambda x: x.ndim == 3 clf = CheckingClassifier(check_X=check_3d) predictions = cval.cross_val_predict(clf, X_3d, y) assert_array_equal(predictions.shape, (10,)) def test_cross_val_predict_pandas(): # check cross_val_score doesn't destroy pandas dataframe types = [(MockDataFrame, MockDataFrame)] try: from pandas import Series, DataFrame types.append((Series, DataFrame)) except ImportError: pass for TargetType, InputFeatureType in types: # X dataframe, y series X_df, y_ser = InputFeatureType(X), TargetType(y) check_df = lambda x: isinstance(x, InputFeatureType) check_series = lambda x: isinstance(x, TargetType) clf = CheckingClassifier(check_X=check_df, check_y=check_series) cval.cross_val_predict(clf, X_df, y_ser) def test_sparse_fit_params(): iris = load_iris() X, y = iris.data, iris.target clf = MockClassifier() fit_params = {'sparse_sample_weight': coo_matrix(np.eye(X.shape[0]))} a = cval.cross_val_score(clf, X, y, fit_params=fit_params) assert_array_equal(a, np.ones(3)) def test_check_is_partition(): p = np.arange(100) assert_true(cval._check_is_partition(p, 100)) assert_false(cval._check_is_partition(np.delete(p, 23), 100)) p[0] = 23 assert_false(cval._check_is_partition(p, 100))
bsd-3-clause
phoebe-project/phoebe2-docs
development/examples/inverse_paper_examples.py
2
19902
#!/usr/bin/env python # coding: utf-8 # # Inverse Problem: General Workflow and Examples # # In this example script, we'll reproduce many of the plots from the fitting release paper ([Conroy et al. 2020](http://phoebe-project.org/publications/2020Conroy+)). # # For the few figures not included here, see the following: # # * Figure 4: [Propagating Distributions through Constraints](distribution_constraints.ipynb) # * Figure 6: [Comparing PHOEBE 2, Legacy, jktebop, ellc](backends_compare_legacy_jktebop_ellc.ipynb) # * FIgure 7: [Minimal Gaussian Processes](minimal_GPs.ipynb) # # Setup # # Let's first make sure we have the latest version of PHOEBE 2.3 installed (uncomment this line if running in an online notebook session such as colab). # In[1]: #!pip install -I "phoebe>=2.3,<2.4" # First we'll import, set our plotting options, and set a fixed random seed so that our noise model is reproducible between runs. # In[2]: import matplotlib.pyplot as plt plt.rc('font', family='serif', size=14, serif='STIXGeneral') plt.rc('mathtext', fontset='stix') # In[3]: import phoebe import numpy as np logger = phoebe.logger('error') # we'll set the random seed so that the noise model is reproducible np.random.seed(123456789) # # Create fake "observations" # # Now we'll create a fake set of observations by setting some parameter values, running a forward model, and adding some simple random noise on both the fluxes and RVs. # In[4]: b = phoebe.default_binary() b.set_value('ecc', 0.2) b.set_value('per0', 25) b.set_value('teff@primary', 7000) b.set_value('teff@secondary', 6000) b.set_value('sma@binary', 7) b.set_value('incl@binary', 80) b.set_value('q', 0.3) b.set_value('t0_supconj', 0.1) b.set_value('requiv@primary', 2.0) b.set_value('vgamma', 80) lctimes = phoebe.linspace(0, 10, 1005) rvtimes = phoebe.linspace(0, 10, 105) b.add_dataset('lc', compute_times=lctimes) b.add_dataset('rv', compute_times=rvtimes) b.add_compute('ellc', compute='fastcompute') b.set_value_all('ld_mode', 'lookup') b.run_compute(compute='fastcompute') fluxes = b.get_value('fluxes@model') + np.random.normal(size=lctimes.shape) * 0.01 fsigmas = np.ones_like(lctimes) * 0.02 rvsA = b.get_value('rvs@primary@model') + np.random.normal(size=rvtimes.shape) * 10 rvsB = b.get_value('rvs@secondary@model') + np.random.normal(size=rvtimes.shape) * 10 rvsigmas = np.ones_like(rvtimes) * 20 # # Create a new bundle/system # # Now we'll start over "blind" with a fresh bundle and import our "fake" observations in datasets. # In[5]: b = phoebe.default_binary() b.set_value('latex_repr', component='binary', value='orb') b.set_value('latex_repr', component='primary', value='1') b.set_value('latex_repr', component='secondary', value='2') b.add_dataset('lc', compute_phases=phoebe.linspace(0,1,201), times=lctimes, fluxes=fluxes, sigmas=fsigmas, dataset='lc01') b.add_dataset('rv', compute_phases=phoebe.linspace(0,1,201), times=rvtimes, rvs={'primary': rvsA, 'secondary': rvsB}, sigmas=rvsigmas, dataset='rv01') b.set_value_all('ld_mode', 'lookup') # For the sake of this example, we'll assume that we know the orbital period *exactly*, and so can see that our observations phase nicely. # In[6]: afig, mplfig = b.plot(x='phases', show=True) # # Run rv_geometry estimator # # First we'll run the [rv_geometry estimator](../api/phoebe.parameters.solver.estimator.rv_geometry.md) via [b.add_solver](../api/phoebe.frontend.bundle.Bundle.add_solver.md) and [b.run_solver](../api/phoebe.frontend.bundle.Bundle.run_solver.md). # In[7]: b.add_solver('estimator.rv_geometry', rv_datasets='rv01') # In[8]: b.run_solver(kind='rv_geometry', solution='rv_geom_sol') # By calling [b.adopt_solution](../api/phoebe.frontend.bundle.Bundle.adopt_solution.md) with `trial_run=True`, we can see the proposed values by the estimator. # In[9]: print(b.adopt_solution('rv_geom_sol', trial_run=True)) # And by plotting the solution, we can see the underlying Keplerian orbit that was fitted to the RVs to determine these values. # # This reproduces Figure 1 # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig1.png" id="fig1" alt="Figure 1" width="800px"/> # In[10]: afig, mplfig = b.plot(solution='rv_geom_sol', show=True, save='figure_rv_geometry.pdf') # # Run lc_geometry estimator # # Next we'll run the [lc_geometry estimator](../api/phoebe.parameters.solver.estimator.lc_geometry.md). # In[11]: b.add_solver('estimator.lc_geometry', lc_datasets='lc01') # In[12]: b.run_solver(kind='lc_geometry', solution='lc_geom_sol') # Again, calling [b.adopt_solution](../api/phoebe.frontend.bundle.Bundle.adopt_solution.md) with `trial_run=True` shows the proposed values. # In[13]: print(b.adopt_solution('lc_geom_sol', trial_run=True)) # By plotting the solution, we get Figure 2, which shows the best two gaussian model as well as the detected positions of mid-eclipse, ingress, and egress which were used to compute the proposed values. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig2.png" id="fig2" alt="Figure 2" width="800px"/> # In[14]: afig, mplfig = b.plot(solution='lc_geom_sol', show=True, save='figure_lc_geometry.pdf') # Figure 5 exhibits eclipse masking by adopting `mask_phases` from the `lc_geometry` solution. Note that by default, `mask_phases` is not included in `adopt_parameters`, which is why it was not included when calling [b.adopt_solution](../api/phoebe.frontend.bundle.Bundle.adopt_solution.md) with `trial_mode=True` (all available proposed parameters could be shown by passing `adopt_parameters='*'`. For the sake of this figure, we'll only adopt the `mask_phases`, plot the dataset with that mask applied, but then disable the mask for the rest of this example script. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig5.png" id="fig5" alt="Figure 5" width="800px"/> # In[15]: b.adopt_solution('lc_geom_sol', adopt_parameters='mask_phases') # In[16]: _ = b.plot(context='dataset', dataset='lc01', x='phases', xlim=(-0.55,0.55), save='figure_lc_geometry_mask.pdf', show=True) # In[17]: b.set_value('mask_enabled@lc01', False) # # Run ebai estimator # # And finally, we'll do the same for the [ebai estimator](../api/phoebe.parameters.solver.estimator.ebai.md). # In[18]: b.add_solver('estimator.ebai', lc_datasets='lc01') # In[19]: b.run_solver(kind='ebai', solution='ebai_sol') # In[20]: print(b.adopt_solution('ebai_sol', trial_run=True)) # By plotting the `ebai` solution, we reproduce Figure 3, which shows the normalized light curve observations and the resulting sample two gaussian model that is sent to the neural network. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig3.png" id="fig3" alt="Figure 3" width="800px"/> # In[21]: afig, mplfig = b.plot(solution='ebai_sol', show=True, save='figure_ebai.pdf') # # Adopt from estimators # # Now we'll adopt the proposed values from the two geometry estimators. # In[22]: b.flip_constraint('asini@binary', solve_for='sma@binary') b.adopt_solution('rv_geom_sol') # In[23]: b.adopt_solution('lc_geom_sol') # We'll keep the eccentricity and per0 estimates from the lc geometry, but use the ebai results to adopt the values for the temperature ratio, sum of fractional radii, and inclination. Note that since we flipped the asini constraint earlier, that value from the rv geometry will remain fixed and the semi-major axis will be adjusted based on asini from rv geometry and incl from ebai. # In[24]: b.flip_constraint('teffratio', solve_for='teff@primary') b.flip_constraint('requivsumfrac', solve_for='requiv@primary') b.adopt_solution('ebai_sol', adopt_parameters=['teffratio', 'requivsumfrac', 'incl']) # In[25]: print(b.filter(qualifier=['ecc', 'per0', 'teff', 'sma', 'incl', 'q', 'requiv'], context='component')) # Now we can run a forward model with these adopted parameters to see how well the results from the estimators agree with the observations. We'll also set the synthetic light curve to automatically scale to the flux-levels of the observations. # In[26]: b.set_value_all('pblum_mode', 'dataset-scaled') # In[27]: b.run_compute(irrad_method='none', model='after_estimators', overwrite=True) # In[28]: _ = b.plot(x='phases', m='.', show=True) # # Optimize with nelder_mead using ellc # # To avoid a long burnin during sampling, we'll use the [nelder_mead optimizer](../api/phoebe.parameters.solver.optimizer.nelder_mead.md) to try to achieve better agreement with the observations. # # We'll use [ellc](../api/phoebe.parameters.compute.ellc.md) as our forward-model just for the sake of computational efficiency. # In[29]: b.add_compute('ellc', compute='fastcompute') # For the sake of optimizing, we'll keep `pblum_mode='dataset-scaled'` which will automatically re-scale the light curve to the observations at each iteration - we'll disable this later for sampling to make sure we account for any degeneracies between the luminosity and other parameters. # In[30]: b.add_solver('optimizer.nelder_mead', fit_parameters=['teffratio', 'requivsumfrac', 'incl@binary', 'q', 'ecc', 'per0'], compute='fastcompute') # In[31]: print(b.get_solver(kind='nelder_mead')) # In[32]: b.run_solver(kind='nelder_mead', maxiter=10000, solution='nm_sol') # In[33]: print(b.get_solution('nm_sol').filter(qualifier=['message', 'nfev', 'niter', 'success'])) # In[34]: print(b.adopt_solution('nm_sol', trial_run=True)) # We'll adopt all the proposed values, and run the forward model with a new `model` tag so that we can overplot the "before" and "after". # In[35]: b.adopt_solution('nm_sol') # In[36]: b.run_compute(compute='fastcompute', model='after_nm') # Figure 8 shows the forward-models from the parameters we adopted after estimators to those after optimization. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig8.png" id="fig8" alt="Figure 8" width="800px"/> # In[37]: _ = b.plot(x='phases', c={'after_estimators': 'red', 'after_nm': 'green', 'dataset': 'black'}, linestyle={'after_estimators': 'dashed', 'after_nm': 'solid'}, marker={'dataset': '.'}, save='figure_optimizer_nm.pdf', show=True) # It's also always a good idea to check to see if our model agrees between different backends and approximations. So we'll compute the same forward-model using PHOEBE and overplot ellc and PHOEBE (note there are some minor differences... if this were a real system that we were publishing we may want to switch to using PHOEBE for determining the final uncertainties) # In[38]: b.run_compute(compute='phoebe01', model='after_nm_phoebe') # In[39]: _ = b.plot(x='phases', model='after_nm*', show=True) # # Determine uncertainties with emcee # So that we don't ignore any degeneracies between parameters and the luminosities, we'll turn off the dataset-scaling we used for optimizing and make sure we have a reasonable value of `pblum@primary` set to roughly obtain the out-of-eclipse flux levels of the observations. To get a good rough guess for `pblum@primary`, we'll use the flux-scaling from `pblum_mode='dataset-scaled'` (see [compute_pblums API docs](../api/phoebe.frontend.bundle.Bundle.compute_pblums.md) for details). # In[40]: pblums_scaled = b.compute_pblums(compute='fastcompute', model='after_nm') # In[41]: print(pblums_scaled) # In[42]: b.set_value_all('pblum_mode', 'component-coupled') # **IMPORTANT NOTE**: it is important that you only apply this automatically scaled pblum value with the same `pblum_method` as was originally used. See [pblum method comparison](pblum_method_compare.ipynb). Also note that if we marginalize over `pblum` using `pblum_method = 'stefan-boltzmann'` that the luminosities themselves should not be trusted - here we're just marginalizing as a nuisance parameter to account for any degeneracies but will not report the actual values themselves, so we can use the cheaper method. If we wanted to switch to `pblum_method='phoebe'` at this point (or to use the phoebe backend), we could re-run a single forward model with `pblum_method='phoebe'` and `pblum_mode='dataset-scaled'` first, and then make the call to `b.compute_pblums` using the resulting model. # In[43]: b.set_value('pblum', dataset='lc01', component='primary', value=pblums_scaled['pblum@primary@lc01']) # In[44]: print(b.compute_pblums(compute='fastcompute', dataset='lc01', pbflux=True)) # And although it doesn't really matter, let's marginalize over 'sma' and 'incl' instead of 'asini' and 'incl'. # In[45]: b.flip_constraint('sma@binary', solve_for='asini') # We'll now create our initializing distribution, including gaussian "balls" around all of the optimized values and a uniform boxcar on `pblum@primary`. # In[46]: b.add_distribution({'teffratio': phoebe.gaussian_around(0.1), 'requivsumfrac': phoebe.gaussian_around(0.1), 'incl@binary': phoebe.gaussian_around(3), 'sma@binary': phoebe.gaussian_around(2), 'q': phoebe.gaussian_around(0.1), 'ecc': phoebe.gaussian_around(0.05), 'per0': phoebe.gaussian_around(5), 'pblum': phoebe.uniform_around(0.5)}, distribution='ball_around_optimized_solution') # We can look at this combined set of distributions, which will be used to sample the initial values of our walkers in [emcee](../api/phoebe.parameters.solver.sampler.emcee.md). # In[47]: _ = b.plot_distribution_collection('ball_around_optimized_solution', show=True) # In[48]: b.add_solver('sampler.emcee', init_from='ball_around_optimized_solution', compute='fastcompute', solver='emcee_solver') # Since we'll need a lot of iterations, we'll export the solver to an HPC cluster (with [b.export_solver](../api/phoebe.frontend.bundle.Bundle.export_solver.md)) and import the solution (with [b.import_solution](../api/phoebe.frontend.bundle.Bundle.import_solution.md)). We'll [save](../api/phoebe.parameters.ParameterSet.save.md) the bundle first so that we can interrupt the notebook and return to the following line, if needed. # # For 2000 iteration on 72 processors, this should take about 2 hours. # In[49]: b.save('inverse_paper_examples_before_emcee.bundle') b.export_solver('inverse_paper_examples_run_emcee.py', solver='emcee_solver', niters=2000, progress_every_niters=100, nwalkers=16, solution='emcee_sol', log_level='warning', pause=True) # In[1]: # only needed if starting script from here import matplotlib.pyplot as plt plt.rc('font', family='serif', size=14, serif='STIXGeneral') plt.rc('mathtext', fontset='stix') import phoebe import numpy as np logger = phoebe.logger('error') b = phoebe.load('inverse_paper_examples_before_emcee.bundle') # In[2]: # NOTE: append .progress to view any of the following plots before the run has completed b.import_solution('inverse_paper_examples_run_emcee.py.out', solution='emcee_sol') # To get as "clean" of posterior distributions as possible, we'll override the proposed thinning value and set it to 1 (effectively disabling thinning). # In[3]: print(b.get_value('thin', solution='emcee_sol')) # In[4]: b.set_value('thin', solution='emcee_sol', value=1) # Alternatively, we could run the solver locally as we've seen before, but probably would want to run less iterations: # # ``` # b.run_solver('emcee_solver', niters=300, nwalkers=16, solution='emcee_sol') # ``` # # in which case calling `b.import_solution` is not necessary. # # Figure 9 shows the relation of any failed or rejected samples with respect to the final posteriors. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig9.png" id="fig9" alt="Figure 9" width="800px"/> # In[5]: plt.rc('font', size=18) _ = b.plot('emcee_sol', style='failed', save='figure_emcee_failed_samples.pdf', show=True) plt.rc('font', size=14) # # Accessing posteriors from emcee run # In[6]: plt.rc('font', size=18) _ = b.plot('emcee_sol', style='corner', show=True) plt.rc('font', size=14) # Figure 10 compares posteriors directly from the samples to those converted to a multivariate gaussian. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig10.png" id="fig10" alt="Figure 10" width="800px"/> # In[7]: _ = b.plot('emcee_sol', style='corner', parameters=['teffratio', 'requivsumfrac', 'incl@binary'], save='figure_posteriors_mvsamples.pdf', show=True) # In[8]: _ = b.plot('emcee_sol', style='corner', parameters=['teffratio', 'requivsumfrac', 'incl@binary'], distributions_convert='mvgaussian', save='figure_posteriors_mvgaussian.pdf', show=True) # Figure 11 demonstrates how posteriors can be propagated through constraints. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig11.png" id="fig11" alt="Figure 11" width="800px"/> # In[9]: _ = b.plot('emcee_sol', style='corner', parameters=['ecc', 'per0'], save='figure_posteriors_ew.pdf', show=True) # In[10]: _ = b.plot('emcee_sol', style='corner', parameters=['esinw', 'ecosw'], save='figure_posteriors_ecs.pdf', show=True) # ## Accessing Uncertainty Estimates from Posteriors # A nice latex representation of the asymmetric uncertainties can be exposed via [b.uncertainties_from_distribution_collection](../api/phoebe.frontend.bundle.Bundle.uncertainties_from_distribution_collection.md) for any distribution collection - but this is particularly useful for acting on posterior distributions. # In[11]: b.uncertainties_from_distribution_collection(solution='emcee_sol', tex=True) # As with the corner plots, these can also be accessed with distributions propagated through constraints into any parameterization. # In[12]: b.uncertainties_from_distribution_collection(solution='emcee_sol', parameters=['esinw', 'ecosw'], tex=True) # ## Propagating Posteriors through Forward-Model # In[13]: b.run_compute(compute='fastcompute', sample_from='emcee_sol', sample_num=500, sample_mode='3-sigma', model='emcee_posts', progressbar=False) # In[14]: b.save('inverse_paper_examples_after_sample_from.bundle') # In[15]: # only needed if starting script from here import matplotlib.pyplot as plt plt.rc('font', family='serif', size=14, serif='STIXGeneral') plt.rc('mathtext', fontset='stix') import phoebe import numpy as np logger = phoebe.logger('error') b = phoebe.load('inverse_paper_examples_after_sample_from.bundle') # And lastly, Figure 12 demonstrates posteriors propagated through the forward model. # # <img src="http://phoebe-project.org/images/figures/2020Conroy+_fig12.png" id="fig12" alt="Figure 12" width="800px"/> # In[16]: _ = b.plot(kind='lc', model='emcee_posts', x='phases', y='fluxes', s={'dataset': 0.005}, marker={'dataset': '.'}) _ = b.plot(kind='lc', model='emcee_posts', x='phases', y='residuals', z={'dataset': 0, 'model': 1}, save='figure_posteriors_sample_from_lc.pdf', show=True) # In[17]: _ = b.plot(kind='rv', model='emcee_posts', x='phases', y='rvs', marker={'dataset': '.'}) _ = b.plot(kind='rv', model='emcee_posts', x='phases', y='residuals', z={'dataset': 0, 'model': 1}, save='figure_posteriors_sample_from_rv.pdf', show=True) # In[ ]:
gpl-3.0
arjunkhode/ASP
software/transformations_interface/hpsTransformations_function.py
23
6610
# function call to the transformation functions of relevance for the hpsModel import numpy as np import matplotlib.pyplot as plt from scipy.signal import get_window import sys, os sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../models/')) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../transformations/')) import hpsModel as HPS import hpsTransformations as HPST import harmonicTransformations as HT import utilFunctions as UF def analysis(inputFile='../../sounds/sax-phrase-short.wav', window='blackman', M=601, N=1024, t=-100, minSineDur=0.1, nH=100, minf0=350, maxf0=700, f0et=5, harmDevSlope=0.01, stocf=0.1): """ Analyze a sound with the harmonic plus stochastic model inputFile: input sound file (monophonic with sampling rate of 44100) window: analysis window type (rectangular, hanning, hamming, blackman, blackmanharris) M: analysis window size N: fft size (power of two, bigger or equal than M) t: magnitude threshold of spectral peaks minSineDur: minimum duration of sinusoidal tracks nH: maximum number of harmonics minf0: minimum fundamental frequency in sound maxf0: maximum fundamental frequency in sound f0et: maximum error accepted in f0 detection algorithm harmDevSlope: allowed deviation of harmonic tracks, higher harmonics have higher allowed deviation stocf: decimation factor used for the stochastic approximation returns inputFile: input file name; fs: sampling rate of input file, hfreq, hmag: harmonic frequencies, magnitude; mYst: stochastic residual """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # read input sound (fs, x) = UF.wavread(inputFile) # compute analysis window w = get_window(window, M) # compute the harmonic plus stochastic model of the whole sound hfreq, hmag, hphase, mYst = HPS.hpsModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope, minSineDur, Ns, stocf) # synthesize the harmonic plus stochastic model without original phases y, yh, yst = HPS.hpsModelSynth(hfreq, hmag, np.array([]), mYst, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_hpsModel.wav' UF.wavwrite(y,fs, outputFile) # create figure to plot plt.figure(figsize=(12, 9)) # frequency range to plot maxplotfreq = 15000.0 # plot the input sound plt.subplot(3,1,1) plt.plot(np.arange(x.size)/float(fs), x) plt.axis([0, x.size/float(fs), min(x), max(x)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('input sound: x') # plot spectrogram stochastic compoment plt.subplot(3,1,2) numFrames = int(mYst[:,0].size) sizeEnv = int(mYst[0,:].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = (.5*fs)*np.arange(sizeEnv*maxplotfreq/(.5*fs))/sizeEnv plt.pcolormesh(frmTime, binFreq, np.transpose(mYst[:,:sizeEnv*maxplotfreq/(.5*fs)+1])) plt.autoscale(tight=True) # plot harmonic on top of stochastic spectrogram if (hfreq.shape[1] > 0): harms = hfreq*np.less(hfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram') # plot the output sound plt.subplot(3,1,3) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show(block=False) return inputFile, fs, hfreq, hmag, mYst def transformation_synthesis(inputFile, fs, hfreq, hmag, mYst, freqScaling = np.array([0, 1.2, 2.01, 1.2, 2.679, .7, 3.146, .7]), freqStretching = np.array([0, 1, 2.01, 1, 2.679, 1.5, 3.146, 1.5]), timbrePreservation = 1, timeScaling = np.array([0, 0, 2.138, 2.138-1.0, 3.146, 3.146])): """ transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file fs: sampling rate of input file hfreq, hmag: harmonic frequencies and magnitudes mYst: stochastic residual freqScaling: frequency scaling factors, in time-value pairs (value of 1 no scaling) freqStretching: frequency stretching factors, in time-value pairs (value of 1 no stretching) timbrePreservation: 1 preserves original timbre, 0 it does not timeScaling: time scaling factors, in time-value pairs """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # frequency scaling of the harmonics hfreqt, hmagt = HT.harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs) # time scaling the sound yhfreq, yhmag, ystocEnv = HPST.hpsTimeScale(hfreqt, hmagt, mYst, timeScaling) # synthesis from the trasformed hps representation y, yh, yst = HPS.hpsModelSynth(yhfreq, yhmag, np.array([]), ystocEnv, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_hpsModelTransformation.wav' UF.wavwrite(y,fs, outputFile) # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 # plot spectrogram of transformed stochastic compoment plt.subplot(2,1,1) numFrames = int(ystocEnv[:,0].size) sizeEnv = int(ystocEnv[0,:].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = (.5*fs)*np.arange(sizeEnv*maxplotfreq/(.5*fs))/sizeEnv plt.pcolormesh(frmTime, binFreq, np.transpose(ystocEnv[:,:sizeEnv*maxplotfreq/(.5*fs)+1])) plt.autoscale(tight=True) # plot transformed harmonic on top of stochastic spectrogram if (yhfreq.shape[1] > 0): harms = yhfreq*np.less(yhfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram') # plot the output sound plt.subplot(2,1,2) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show() if __name__ == "__main__": # analysis inputFile, fs, hfreq, hmag, mYst = analysis() # transformation and synthesis transformation_synthesis(inputFile, fs, hfreq, hmag, mYst) plt.show()
agpl-3.0
rinze/kaggle-public
instacart/most_common_items_simple_weight.py
1
2090
import sqlite3 import pandas as pd import csv import gzip from collections import defaultdict if __name__ == '__main__': conn = sqlite3.connect('data/instacart.db') c = conn.cursor() # This does the same (?; re-check) and is much faster q = """ SELECT user_id, MIN(n_items) AS min_items, AVG(n_items) AS avg_items, MAX(n_items) AS max_items FROM orders o INNER JOIN (SELECT order_id, COUNT(*) AS n_items FROM order_products__prior GROUP BY order_id) avg ON avg.order_id = o.order_id GROUP BY user_id """ print "Getting order stats..." c.execute(q) order_stats = dict() print "Assigning to dictionary..." for row in c: order_stats[row[0]] = (row[1], row[2], row[3]) # For every customer, sort the bought items in descending popularity q = """ SELECT o.user_id AS user_id, opp.product_id AS product_id, sum(w.w * w.w) AS n -- improve here, probably FROM order_products__prior opp JOIN orders o ON o.order_id = opp.order_id JOIN order_weights w ON w.user_id = o.user_id AND w.order_id = o.order_id GROUP BY o.user_id, opp.product_id ORDER BY o.user_id, n DESC """ print "Getting product frequency..." c.execute(q) print "Assigning next order per user..." next_order = defaultdict(list) for row in c: if len(next_order[row[0]]) < round(order_stats[row[0]][1]): # more than the average next_order[row[0]].append(row[1]) # Now just let's assign orders print "Generating CSV file..." q = "SELECT order_id, user_id FROM orders WHERE eval_set = 'test'" c.execute(q) result = [] result.append(['order_id', 'products']) for row in c: result.append([row[0], " ".join([str(x) for x in next_order[row[1]]])]) # Write compressed CSV file with gzip.open('/tmp/submission.csv.gz', 'wb') as f: csvwriter = csv.writer(f, delimiter = ',', quotechar = '"') for row in result: csvwriter.writerow(row)
gpl-2.0
procoder317/scikit-learn
examples/decomposition/plot_ica_blind_source_separation.py
349
2228
""" ===================================== Blind source separation using FastICA ===================================== An example of estimating sources from noisy data. :ref:`ICA` is used to estimate sources given noisy measurements. Imagine 3 instruments playing simultaneously and 3 microphones recording the mixed signals. ICA is used to recover the sources ie. what is played by each instrument. Importantly, PCA fails at recovering our `instruments` since the related signals reflect non-Gaussian processes. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from scipy import signal from sklearn.decomposition import FastICA, PCA ############################################################################### # Generate sample data np.random.seed(0) n_samples = 2000 time = np.linspace(0, 8, n_samples) s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal s3 = signal.sawtooth(2 * np.pi * time) # Signal 3: saw tooth signal S = np.c_[s1, s2, s3] S += 0.2 * np.random.normal(size=S.shape) # Add noise S /= S.std(axis=0) # Standardize data # Mix data A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]]) # Mixing matrix X = np.dot(S, A.T) # Generate observations # Compute ICA ica = FastICA(n_components=3) S_ = ica.fit_transform(X) # Reconstruct signals A_ = ica.mixing_ # Get estimated mixing matrix # We can `prove` that the ICA model applies by reverting the unmixing. assert np.allclose(X, np.dot(S_, A_.T) + ica.mean_) # For comparison, compute PCA pca = PCA(n_components=3) H = pca.fit_transform(X) # Reconstruct signals based on orthogonal components ############################################################################### # Plot results plt.figure() models = [X, S, S_, H] names = ['Observations (mixed signal)', 'True Sources', 'ICA recovered signals', 'PCA recovered signals'] colors = ['red', 'steelblue', 'orange'] for ii, (model, name) in enumerate(zip(models, names), 1): plt.subplot(4, 1, ii) plt.title(name) for sig, color in zip(model.T, colors): plt.plot(sig, color=color) plt.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.46) plt.show()
bsd-3-clause
rr1964/Caterpillar
JSS Exploration.py
1
4242
# -*- coding: utf-8 -*- """ Created on Thu Jun 01 11:26:48 2017 @author: reeserd2 """ print "Entering JSS Analysis mode." ####All of the following is now done by my module simpleRead #import csv # #with open('C:/Users/reeserd2/Desktop/JSS Analysis/data/JSS_fixed_CIM_CapIQ_20170525_fixed.csv', mode = 'r') as f: # readIn = csv.reader(f, delimiter = ',', skipinitialspace=True) # # lineData = list() # # cols = next(readIn) # print(cols) # # for col in cols: # # Create a list in lineData for each column of data. # lineData.append(list()) # # # for line in readIn: # for i in xrange(0, len(lineData)): # # Copy the data from the line into the correct columns. # lineData[i].append(line[i]) # # data = dict() # # for i in xrange(0, len(cols)): ## Create each key in the dict with the data in its column. # data[cols[i]] = lineData[i] # #print(data) # #f.close() import simpleRead as sr###A crude module I personally wrote for reading in raw csv files. import numpy as np import math as m import matplotlib import pylab as pl import pandas print matplotlib.__version__ """ I am learning some work from 'Python for Data Analysis'. """ ####One way of reading in the data. A bit choppy, but you can get at it at a more "raw" level. #raw_data = sr.simpleReadCSV('C:/Users/reeserd2/Desktop/JSS Analysis/data/JSS_fixed_CIM_CapIQ_20170525_fixed.csv') #my_data.keys() #print my_data["ISWON"] def make_float(s): s = s.strip() return float(s) if s else 'NA' #raw_data["RoAPer"] = map(make_float, raw_data["RoAPer"]) #print my_data["RoAPer"] ###dataf = pd.DataFrame(raw_data, columns = []) ####pandas.read_csv() most closely resembles R's ability to intelligently read in a csv file. JSS_Data = pandas.read_csv('../JSS Analysis/data/JSS_fixed_CIM_CapIQ_20170525_fixed.csv') print JSS_Data.keys() JSS_Data["Rev"] JSS_Data.count() JSS_Data.sum()##Who knows what this does for strings.....But I believe it ignores NaN. It also seems to ignore string coulmns. #%% import json path = "C:/Users/reeserd2/Documents/bitlyData.txt" bitly =[json.loads(line) for line in open(path)] ###Note the list constructor using a for loop. #print bitly[2]["tz"] time_zones = [record['tz'] for record in bitly if "tz" in record] ###Again, the list constructor using a for loop. time_zones[:15]###The index is not inclusive remember. from collections import defaultdict def get_counts(seq): counts = defaultdict(int)###Initializes all values to 0. for rec in seq: counts[rec] += 1 return counts tzCounts = get_counts(time_zones) tzCounts['America/New_York'] #%%% ###We can find the top 5 most common time zones, but it requires us "flipping" the dictionary so to speak. def top_counts(count_dict, n = 5): value_key = [(count,tz) for tz,count in count_dict.items()] value_key.sort(reverse = True) #value_key.sort() ###sorts based on the FIRST value in the tuple. ###The value_key list now remains sorted. No need to cache. To not modify the list, use sorted(LIST) return value_key[-n:] top_counts(tzCounts, n = 10) ###This same thing can be done using some tools that are importable from the collections module. from collections import Counter tzCounts_simple = Counter(time_zones) ###A single function to cover the last 20 lines or so. tzCounts_simple.most_common(10) #Also presents these in a top to bottom format. #%% ###All of the time zone stuff can be done by using DataFrame in pandas. import pandas as pd df = pd.DataFrame(bitly) df['tz'][:10] ##print df["tz"].value_counts()[:10] clean_tz = df['tz'].fillna('Missing') clean_tz[clean_tz == ''] = 'Unknown' tz_counts = clean_tz.value_counts() tz_counts[:10] #%% import matplotlib.pyplot as plt plt.figure(figsize=(10, 4)) tz_counts[:10].plot(kind='barh', rot=0) df['a'][1] df['a'][50] df['a'][51] results = pd.Series([x.split()[0] for x in df.a.dropna()]) results[:5] #%% #%% #%% #%% #%% #%% #%%
gpl-3.0
lancezlin/ml_template_py
lib/python2.7/site-packages/pandas/compat/numpy/function.py
7
12445
""" For compatibility with numpy libraries, pandas functions or methods have to accept '*args' and '**kwargs' parameters to accommodate numpy arguments that are not actually used or respected in the pandas implementation. To ensure that users do not abuse these parameters, validation is performed in 'validators.py' to make sure that any extra parameters passed correspond ONLY to those in the numpy signature. Part of that validation includes whether or not the user attempted to pass in non-default values for these extraneous parameters. As we want to discourage users from relying on these parameters when calling the pandas implementation, we want them only to pass in the default values for these parameters. This module provides a set of commonly used default arguments for functions and methods that are spread throughout the codebase. This module will make it easier to adjust to future upstream changes in the analogous numpy signatures. """ from numpy import ndarray from pandas.util.validators import (validate_args, validate_kwargs, validate_args_and_kwargs) from pandas.core.common import UnsupportedFunctionCall from pandas.types.common import is_integer, is_bool from pandas.compat import OrderedDict class CompatValidator(object): def __init__(self, defaults, fname=None, method=None, max_fname_arg_count=None): self.fname = fname self.method = method self.defaults = defaults self.max_fname_arg_count = max_fname_arg_count def __call__(self, args, kwargs, fname=None, max_fname_arg_count=None, method=None): fname = self.fname if fname is None else fname max_fname_arg_count = (self.max_fname_arg_count if max_fname_arg_count is None else max_fname_arg_count) method = self.method if method is None else method if method == 'args': validate_args(fname, args, max_fname_arg_count, self.defaults) elif method == 'kwargs': validate_kwargs(fname, kwargs, self.defaults) elif method == 'both': validate_args_and_kwargs(fname, args, kwargs, max_fname_arg_count, self.defaults) else: raise ValueError("invalid validation method " "'{method}'".format(method=method)) ARGMINMAX_DEFAULTS = dict(out=None) validate_argmin = CompatValidator(ARGMINMAX_DEFAULTS, fname='argmin', method='both', max_fname_arg_count=1) validate_argmax = CompatValidator(ARGMINMAX_DEFAULTS, fname='argmax', method='both', max_fname_arg_count=1) def process_skipna(skipna, args): if isinstance(skipna, ndarray) or skipna is None: args = (skipna,) + args skipna = True return skipna, args def validate_argmin_with_skipna(skipna, args, kwargs): """ If 'Series.argmin' is called via the 'numpy' library, the third parameter in its signature is 'out', which takes either an ndarray or 'None', so check if the 'skipna' parameter is either an instance of ndarray or is None, since 'skipna' itself should be a boolean """ skipna, args = process_skipna(skipna, args) validate_argmin(args, kwargs) return skipna def validate_argmax_with_skipna(skipna, args, kwargs): """ If 'Series.argmax' is called via the 'numpy' library, the third parameter in its signature is 'out', which takes either an ndarray or 'None', so check if the 'skipna' parameter is either an instance of ndarray or is None, since 'skipna' itself should be a boolean """ skipna, args = process_skipna(skipna, args) validate_argmax(args, kwargs) return skipna ARGSORT_DEFAULTS = OrderedDict() ARGSORT_DEFAULTS['axis'] = -1 ARGSORT_DEFAULTS['kind'] = 'quicksort' ARGSORT_DEFAULTS['order'] = None validate_argsort = CompatValidator(ARGSORT_DEFAULTS, fname='argsort', max_fname_arg_count=0, method='both') def validate_argsort_with_ascending(ascending, args, kwargs): """ If 'Categorical.argsort' is called via the 'numpy' library, the first parameter in its signature is 'axis', which takes either an integer or 'None', so check if the 'ascending' parameter has either integer type or is None, since 'ascending' itself should be a boolean """ if is_integer(ascending) or ascending is None: args = (ascending,) + args ascending = True validate_argsort(args, kwargs, max_fname_arg_count=1) return ascending CLIP_DEFAULTS = dict(out=None) validate_clip = CompatValidator(CLIP_DEFAULTS, fname='clip', method='both', max_fname_arg_count=3) def validate_clip_with_axis(axis, args, kwargs): """ If 'NDFrame.clip' is called via the numpy library, the third parameter in its signature is 'out', which can takes an ndarray, so check if the 'axis' parameter is an instance of ndarray, since 'axis' itself should either be an integer or None """ if isinstance(axis, ndarray): args = (axis,) + args axis = None validate_clip(args, kwargs) return axis COMPRESS_DEFAULTS = OrderedDict() COMPRESS_DEFAULTS['axis'] = None COMPRESS_DEFAULTS['out'] = None validate_compress = CompatValidator(COMPRESS_DEFAULTS, fname='compress', method='both', max_fname_arg_count=1) CUM_FUNC_DEFAULTS = OrderedDict() CUM_FUNC_DEFAULTS['dtype'] = None CUM_FUNC_DEFAULTS['out'] = None validate_cum_func = CompatValidator(CUM_FUNC_DEFAULTS, method='both', max_fname_arg_count=1) validate_cumsum = CompatValidator(CUM_FUNC_DEFAULTS, fname='cumsum', method='both', max_fname_arg_count=1) def validate_cum_func_with_skipna(skipna, args, kwargs, name): """ If this function is called via the 'numpy' library, the third parameter in its signature is 'dtype', which takes either a 'numpy' dtype or 'None', so check if the 'skipna' parameter is a boolean or not """ if not is_bool(skipna): args = (skipna,) + args skipna = True validate_cum_func(args, kwargs, fname=name) return skipna LOGICAL_FUNC_DEFAULTS = dict(out=None) validate_logical_func = CompatValidator(LOGICAL_FUNC_DEFAULTS, method='kwargs') MINMAX_DEFAULTS = dict(out=None) validate_min = CompatValidator(MINMAX_DEFAULTS, fname='min', method='both', max_fname_arg_count=1) validate_max = CompatValidator(MINMAX_DEFAULTS, fname='max', method='both', max_fname_arg_count=1) RESHAPE_DEFAULTS = dict(order='C') validate_reshape = CompatValidator(RESHAPE_DEFAULTS, fname='reshape', method='both', max_fname_arg_count=1) REPEAT_DEFAULTS = dict(axis=None) validate_repeat = CompatValidator(REPEAT_DEFAULTS, fname='repeat', method='both', max_fname_arg_count=1) ROUND_DEFAULTS = dict(out=None) validate_round = CompatValidator(ROUND_DEFAULTS, fname='round', method='both', max_fname_arg_count=1) SORT_DEFAULTS = OrderedDict() SORT_DEFAULTS['axis'] = -1 SORT_DEFAULTS['kind'] = 'quicksort' SORT_DEFAULTS['order'] = None validate_sort = CompatValidator(SORT_DEFAULTS, fname='sort', method='kwargs') STAT_FUNC_DEFAULTS = OrderedDict() STAT_FUNC_DEFAULTS['dtype'] = None STAT_FUNC_DEFAULTS['out'] = None validate_stat_func = CompatValidator(STAT_FUNC_DEFAULTS, method='kwargs') validate_sum = CompatValidator(STAT_FUNC_DEFAULTS, fname='sort', method='both', max_fname_arg_count=1) validate_mean = CompatValidator(STAT_FUNC_DEFAULTS, fname='mean', method='both', max_fname_arg_count=1) STAT_DDOF_FUNC_DEFAULTS = OrderedDict() STAT_DDOF_FUNC_DEFAULTS['dtype'] = None STAT_DDOF_FUNC_DEFAULTS['out'] = None validate_stat_ddof_func = CompatValidator(STAT_DDOF_FUNC_DEFAULTS, method='kwargs') # Currently, numpy (v1.11) has backwards compatibility checks # in place so that this 'kwargs' parameter is technically # unnecessary, but in the long-run, this will be needed. SQUEEZE_DEFAULTS = dict(axis=None) validate_squeeze = CompatValidator(SQUEEZE_DEFAULTS, fname='squeeze', method='kwargs') TAKE_DEFAULTS = OrderedDict() TAKE_DEFAULTS['out'] = None TAKE_DEFAULTS['mode'] = 'raise' validate_take = CompatValidator(TAKE_DEFAULTS, fname='take', method='kwargs') def validate_take_with_convert(convert, args, kwargs): """ If this function is called via the 'numpy' library, the third parameter in its signature is 'axis', which takes either an ndarray or 'None', so check if the 'convert' parameter is either an instance of ndarray or is None """ if isinstance(convert, ndarray) or convert is None: args = (convert,) + args convert = True validate_take(args, kwargs, max_fname_arg_count=3, method='both') return convert TRANSPOSE_DEFAULTS = dict(axes=None) validate_transpose = CompatValidator(TRANSPOSE_DEFAULTS, fname='transpose', method='both', max_fname_arg_count=0) def validate_transpose_for_generic(inst, kwargs): try: validate_transpose(tuple(), kwargs) except ValueError as e: klass = type(inst).__name__ msg = str(e) # the Panel class actual relies on the 'axes' parameter if called # via the 'numpy' library, so let's make sure the error is specific # about saying that the parameter is not supported for particular # implementations of 'transpose' if "the 'axes' parameter is not supported" in msg: msg += " for {klass} instances".format(klass=klass) raise ValueError(msg) def validate_window_func(name, args, kwargs): numpy_args = ('axis', 'dtype', 'out') msg = ("numpy operations are not " "valid with window objects. " "Use .{func}() directly instead ".format(func=name)) if len(args) > 0: raise UnsupportedFunctionCall(msg) for arg in numpy_args: if arg in kwargs: raise UnsupportedFunctionCall(msg) def validate_rolling_func(name, args, kwargs): numpy_args = ('axis', 'dtype', 'out') msg = ("numpy operations are not " "valid with window objects. " "Use .rolling(...).{func}() instead ".format(func=name)) if len(args) > 0: raise UnsupportedFunctionCall(msg) for arg in numpy_args: if arg in kwargs: raise UnsupportedFunctionCall(msg) def validate_expanding_func(name, args, kwargs): numpy_args = ('axis', 'dtype', 'out') msg = ("numpy operations are not " "valid with window objects. " "Use .expanding(...).{func}() instead ".format(func=name)) if len(args) > 0: raise UnsupportedFunctionCall(msg) for arg in numpy_args: if arg in kwargs: raise UnsupportedFunctionCall(msg) def validate_groupby_func(name, args, kwargs): """ 'args' and 'kwargs' should be empty because all of their necessary parameters are explicitly listed in the function signature """ if len(args) + len(kwargs) > 0: raise UnsupportedFunctionCall(( "numpy operations are not valid " "with groupby. Use .groupby(...)." "{func}() instead".format(func=name))) RESAMPLER_NUMPY_OPS = ('min', 'max', 'sum', 'prod', 'mean', 'std', 'var') def validate_resampler_func(method, args, kwargs): """ 'args' and 'kwargs' should be empty because all of their necessary parameters are explicitly listed in the function signature """ if len(args) + len(kwargs) > 0: if method in RESAMPLER_NUMPY_OPS: raise UnsupportedFunctionCall(( "numpy operations are not valid " "with resample. Use .resample(...)." "{func}() instead".format(func=method))) else: raise TypeError("too many arguments passed in")
mit
cg31/tensorflow
tensorflow/examples/learn/iris.py
25
1649
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Example of DNNClassifier for Iris plant dataset.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from sklearn import cross_validation from sklearn import metrics import tensorflow as tf from tensorflow.contrib import learn def main(unused_argv): # Load dataset. iris = learn.datasets.load_dataset('iris') x_train, x_test, y_train, y_test = cross_validation.train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) # Build 3 layer DNN with 10, 20, 10 units respectively. feature_columns = learn.infer_real_valued_columns_from_input(x_train) classifier = learn.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3) # Fit and predict. classifier.fit(x_train, y_train, steps=200) predictions = list(classifier.predict(x_test, as_iterable=True)) score = metrics.accuracy_score(y_test, predictions) print('Accuracy: {0:f}'.format(score)) if __name__ == '__main__': tf.app.run()
apache-2.0
SergioLezama/LC3-INMEGEN.github.io
scripts/antropometria/antropometria_csv2json.py
3
1677
import pandas as pd render = {} df = pd.read_csv(path+"visualizacion/antro_freq_nutri_adulto_2012.csv") df = df.fillna(0) max_ = 160 imc_cat_index = { 0:"ND", 1:"Bajo Peso", 2:"Normal", 3:"Sobrepeso", 4:"Obesidad"} def proccess_data(genero): ob = {} sp = {} nm = {} bp = {} for d in df[df['sexo'] == genero].iterrows(): try: index = int(d[1]['imc_cat']) except ValueError: index = 0 for i in xrange(1, max_): if index == 4: if not i in ob: ob[i] = d[1][str(i)] else: ob[i] = ob[i] + d[1][str(i)] elif index == 3: if not i in sp: sp[i] = d[1][str(i)] else: sp[i] = sp[i] + d[1][str(i)] elif index == 2: if not i in nm: nm[i] = d[1][str(i)] else: nm[i] = nm[i] + d[1][str(i)] elif index == 1: if not i in bp: bp[i] = d[1][str(i)] else: bp[i] = bp[i] + d[1][str(i)] n_ob, n_sp, n_nm, n_bp = [], [], [], [] for k, v in ob.items(): print k n_ob.append({"y": v, "x": k}) for k, v in sp.items(): n_sp.append({"y": v, "x": k}) for k, v in nm.items(): n_nm.append({"y": v, "x": k}) for k, v in bp.items(): n_bp.append({"y": v, "x": k}) t = min(n_ob, n_sp, n_nm, n_bp) tl = len(t) return n_ob[:tl], n_sp[:tl], n_nm[:tl], n_bp[:tl] n_ob_H, n_sp_H, n_nm_H, n_bp_H = proccess_data(1) n_ob_M, n_sp_M, n_nm_M, n_bp_M = proccess_data(2)
gpl-3.0
kashif/scikit-learn
sklearn/tests/test_learning_curve.py
59
10869
# Author: Alexander Fabisch <[email protected]> # # License: BSD 3 clause import sys from sklearn.externals.six.moves import cStringIO as StringIO import numpy as np import warnings from sklearn.base import BaseEstimator from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_warns from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.datasets import make_classification with warnings.catch_warnings(): warnings.simplefilter('ignore') from sklearn.learning_curve import learning_curve, validation_curve from sklearn.cross_validation import KFold from sklearn.linear_model import PassiveAggressiveClassifier class MockImprovingEstimator(BaseEstimator): """Dummy classifier to test the learning curve""" def __init__(self, n_max_train_sizes): self.n_max_train_sizes = n_max_train_sizes self.train_sizes = 0 self.X_subset = None def fit(self, X_subset, y_subset=None): self.X_subset = X_subset self.train_sizes = X_subset.shape[0] return self def predict(self, X): raise NotImplementedError def score(self, X=None, Y=None): # training score becomes worse (2 -> 1), test error better (0 -> 1) if self._is_training_data(X): return 2. - float(self.train_sizes) / self.n_max_train_sizes else: return float(self.train_sizes) / self.n_max_train_sizes def _is_training_data(self, X): return X is self.X_subset class MockIncrementalImprovingEstimator(MockImprovingEstimator): """Dummy classifier that provides partial_fit""" def __init__(self, n_max_train_sizes): super(MockIncrementalImprovingEstimator, self).__init__(n_max_train_sizes) self.x = None def _is_training_data(self, X): return self.x in X def partial_fit(self, X, y=None, **params): self.train_sizes += X.shape[0] self.x = X[0] class MockEstimatorWithParameter(BaseEstimator): """Dummy classifier to test the validation curve""" def __init__(self, param=0.5): self.X_subset = None self.param = param def fit(self, X_subset, y_subset): self.X_subset = X_subset self.train_sizes = X_subset.shape[0] return self def predict(self, X): raise NotImplementedError def score(self, X=None, y=None): return self.param if self._is_training_data(X) else 1 - self.param def _is_training_data(self, X): return X is self.X_subset def test_learning_curve(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) with warnings.catch_warnings(record=True) as w: train_sizes, train_scores, test_scores = learning_curve( estimator, X, y, cv=3, train_sizes=np.linspace(0.1, 1.0, 10)) if len(w) > 0: raise RuntimeError("Unexpected warning: %r" % w[0].message) assert_equal(train_scores.shape, (10, 3)) assert_equal(test_scores.shape, (10, 3)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_unsupervised(): X, _ = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y=None, cv=3, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_verbose(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) old_stdout = sys.stdout sys.stdout = StringIO() try: train_sizes, train_scores, test_scores = \ learning_curve(estimator, X, y, cv=3, verbose=1) finally: out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = old_stdout assert("[learning_curve]" in out) def test_learning_curve_incremental_learning_not_possible(): X, y = make_classification(n_samples=2, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) # The mockup does not have partial_fit() estimator = MockImprovingEstimator(1) assert_raises(ValueError, learning_curve, estimator, X, y, exploit_incremental_learning=True) def test_learning_curve_incremental_learning(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockIncrementalImprovingEstimator(20) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y, cv=3, exploit_incremental_learning=True, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_incremental_learning_unsupervised(): X, _ = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockIncrementalImprovingEstimator(20) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y=None, cv=3, exploit_incremental_learning=True, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_learning_curve_batch_and_incremental_learning_are_equal(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) train_sizes = np.linspace(0.2, 1.0, 5) estimator = PassiveAggressiveClassifier(n_iter=1, shuffle=False) train_sizes_inc, train_scores_inc, test_scores_inc = \ learning_curve( estimator, X, y, train_sizes=train_sizes, cv=3, exploit_incremental_learning=True) train_sizes_batch, train_scores_batch, test_scores_batch = \ learning_curve( estimator, X, y, cv=3, train_sizes=train_sizes, exploit_incremental_learning=False) assert_array_equal(train_sizes_inc, train_sizes_batch) assert_array_almost_equal(train_scores_inc.mean(axis=1), train_scores_batch.mean(axis=1)) assert_array_almost_equal(test_scores_inc.mean(axis=1), test_scores_batch.mean(axis=1)) def test_learning_curve_n_sample_range_out_of_bounds(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0, 1]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0.0, 1.0]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0.1, 1.1]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[0, 20]) assert_raises(ValueError, learning_curve, estimator, X, y, cv=3, train_sizes=[1, 21]) def test_learning_curve_remove_duplicate_sample_sizes(): X, y = make_classification(n_samples=3, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(2) train_sizes, _, _ = assert_warns( RuntimeWarning, learning_curve, estimator, X, y, cv=3, train_sizes=np.linspace(0.33, 1.0, 3)) assert_array_equal(train_sizes, [1, 2]) def test_learning_curve_with_boolean_indices(): X, y = make_classification(n_samples=30, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) estimator = MockImprovingEstimator(20) cv = KFold(n=30, n_folds=3) train_sizes, train_scores, test_scores = learning_curve( estimator, X, y, cv=cv, train_sizes=np.linspace(0.1, 1.0, 10)) assert_array_equal(train_sizes, np.linspace(2, 20, 10)) assert_array_almost_equal(train_scores.mean(axis=1), np.linspace(1.9, 1.0, 10)) assert_array_almost_equal(test_scores.mean(axis=1), np.linspace(0.1, 1.0, 10)) def test_validation_curve(): X, y = make_classification(n_samples=2, n_features=1, n_informative=1, n_redundant=0, n_classes=2, n_clusters_per_class=1, random_state=0) param_range = np.linspace(0, 1, 10) with warnings.catch_warnings(record=True) as w: train_scores, test_scores = validation_curve( MockEstimatorWithParameter(), X, y, param_name="param", param_range=param_range, cv=2 ) if len(w) > 0: raise RuntimeError("Unexpected warning: %r" % w[0].message) assert_array_almost_equal(train_scores.mean(axis=1), param_range) assert_array_almost_equal(test_scores.mean(axis=1), 1 - param_range)
bsd-3-clause
cowlicks/blaze
blaze/expr/collections.py
2
20951
from __future__ import absolute_import, division, print_function import numbers import numpy as np from functools import partial from itertools import chain import datashape from datashape import ( DataShape, Option, Record, Unit, dshape, var, Fixed, Var, promote, object_, ) from datashape.predicates import isscalar, iscollection, isrecord from toolz import ( isdistinct, frequencies, concat as tconcat, unique, get, first, compose, keymap ) import toolz.curried.operator as op from odo.utils import copydoc from .core import common_subexpression from .expressions import Expr, ElemWise, label, Field from .expressions import dshape_method_list from ..compatibility import zip_longest, _strtypes from ..utils import listpack __all__ = ['Sort', 'Distinct', 'Head', 'Merge', 'IsIn', 'isin', 'distinct', 'merge', 'head', 'sort', 'Join', 'join', 'transform', 'Concat', 'concat', 'Tail', 'tail', 'Shift', 'shift'] class Sort(Expr): """ Table in sorted order Examples -------- >>> from blaze import symbol >>> accounts = symbol('accounts', 'var * {name: string, amount: int}') >>> accounts.sort('amount', ascending=False).schema dshape("{name: string, amount: int32}") Some backends support sorting by arbitrary rowwise tables, e.g. >>> accounts.sort(-accounts.amount) # doctest: +SKIP """ __slots__ = '_hash', '_child', '_key', 'ascending' def _dshape(self): return self._child.dshape @property def key(self): if self._key is () or self._key is None: return self._child.fields[0] if isinstance(self._key, tuple): return list(self._key) else: return self._key def _len(self): return self._child._len() @property def _name(self): return self._child._name def __str__(self): return "%s.sort(%s, ascending=%s)" % (self._child, repr(self._key), self.ascending) def sort(child, key=None, ascending=True): """ Sort a collection Parameters ---------- key : str, list of str, or Expr Defines by what you want to sort. * A single column string: ``t.sort('amount')`` * A list of column strings: ``t.sort(['name', 'amount'])`` * An expression: ``t.sort(-t.amount)`` ascending : bool, optional Determines order of the sort """ if not isrecord(child.dshape.measure): key = None if isinstance(key, list): key = tuple(key) return Sort(child, key, ascending) class Distinct(Expr): """ Remove duplicate elements from an expression Parameters ---------- on : tuple of :class:`~blaze.expr.expressions.Field` The subset of fields or names of fields to be distinct on. Examples -------- >>> from blaze import symbol >>> t = symbol('t', 'var * {name: string, amount: int, id: int}') >>> e = distinct(t) >>> data = [('Alice', 100, 1), ... ('Bob', 200, 2), ... ('Alice', 100, 1)] >>> from blaze.compute.python import compute >>> sorted(compute(e, data)) [('Alice', 100, 1), ('Bob', 200, 2)] Use a subset by passing `on`: >>> import pandas as pd >>> e = distinct(t, 'name') >>> data = pd.DataFrame([['Alice', 100, 1], ... ['Alice', 200, 2], ... ['Bob', 100, 1], ... ['Bob', 200, 2]], ... columns=['name', 'amount', 'id']) >>> compute(e, data) name amount id 0 Alice 100 1 1 Bob 100 1 """ __slots__ = '_hash', '_child', 'on' def _dshape(self): return datashape.var * self._child.dshape.measure @property def fields(self): return self._child.fields @property def _name(self): return self._child._name def __str__(self): return 'distinct({child}{on})'.format( child=self._child, on=(', ' if self.on else '') + ', '.join(map(str, self.on)) ) @copydoc(Distinct) def distinct(expr, *on): fields = frozenset(expr.fields) _on = [] append = _on.append for n in on: if isinstance(n, Field): if n._child.isidentical(expr): n = n._name else: raise ValueError('{0} is not a field of {1}'.format(n, expr)) if not isinstance(n, _strtypes): raise TypeError('on must be a name or field, not: {0}'.format(n)) elif n not in fields: raise ValueError('{0} is not a field of {1}'.format(n, expr)) append(n) return Distinct(expr, tuple(_on)) class _HeadOrTail(Expr): __slots__ = '_hash', '_child', 'n' def _dshape(self): return self.n * self._child.dshape.subshape[0] def _len(self): return min(self._child._len(), self.n) @property def _name(self): return self._child._name def __str__(self): return '%s.%s(%d)' % (self._child, type(self).__name__.lower(), self.n) class Head(_HeadOrTail): """ First `n` elements of collection Examples -------- >>> from blaze import symbol >>> accounts = symbol('accounts', 'var * {name: string, amount: int}') >>> accounts.head(5).dshape dshape("5 * {name: string, amount: int32}") See Also -------- blaze.expr.collections.Tail """ pass @copydoc(Head) def head(child, n=10): return Head(child, n) class Tail(_HeadOrTail): """ Last `n` elements of collection Examples -------- >>> from blaze import symbol >>> accounts = symbol('accounts', 'var * {name: string, amount: int}') >>> accounts.tail(5).dshape dshape("5 * {name: string, amount: int32}") See Also -------- blaze.expr.collections.Head """ pass @copydoc(Tail) def tail(child, n=10): return Tail(child, n) def transform(t, replace=True, **kwargs): """ Add named columns to table >>> from blaze import symbol >>> t = symbol('t', 'var * {x: int, y: int}') >>> transform(t, z=t.x + t.y).fields ['x', 'y', 'z'] """ if replace and set(t.fields).intersection(set(kwargs)): t = t[[c for c in t.fields if c not in kwargs]] args = [t] + [v.label(k) for k, v in sorted(kwargs.items(), key=first)] return merge(*args) def schema_concat(exprs): """ Concatenate schemas together. Supporting both Records and Units In the case of Units, the name is taken from expr.name """ new_fields = [] for c in exprs: schema = c.schema[0] if isinstance(schema, Record): new_fields.extend(schema.fields) elif isinstance(schema, (Unit, Option)): new_fields.append((c._name, schema)) else: raise TypeError("All schemas must have Record or Unit shape." "\nGot %s" % schema) return dshape(Record(new_fields)) class Merge(ElemWise): """ Merge many fields together Examples -------- >>> from blaze import symbol >>> accounts = symbol('accounts', 'var * {name: string, x: int, y: real}') >>> merge(accounts.name, z=accounts.x + accounts.y).fields ['name', 'z'] """ __slots__ = '_hash', '_child', 'children' def _schema(self): return schema_concat(self.children) @property def fields(self): return list(tconcat(child.fields for child in self.children)) def _subterms(self): yield self for i in self.children: for node in i._subterms(): yield node def _get_field(self, key): for child in self.children: if key in child.fields: if isscalar(child.dshape.measure): return child else: return child[key] def _project(self, key): if not isinstance(key, (tuple, list)): raise TypeError("Expected tuple or list, got %s" % key) return merge(*[self[c] for c in key]) def _leaves(self): return list(unique(tconcat(i._leaves() for i in self.children))) @copydoc(Merge) def merge(*exprs, **kwargs): if len(exprs) + len(kwargs) == 1: if exprs: return exprs[0] if kwargs: [(k, v)] = kwargs.items() return v.label(k) # Get common sub expression exprs += tuple(label(v, k) for k, v in sorted(kwargs.items(), key=first)) child = common_subexpression(*exprs) result = Merge(child, exprs) if not isdistinct(result.fields): raise ValueError( "Repeated columns found: " + ', '.join( k for k, v in frequencies(result.fields).items() if v > 1 ), ) return result def unpack(l): """ Unpack items from collections of nelements 1 >>> unpack('hello') 'hello' >>> unpack(['hello']) 'hello' """ if isinstance(l, (tuple, list, set)) and len(l) == 1: return next(iter(l)) else: return l class Join(Expr): """ Join two tables on common columns Parameters ---------- lhs, rhs : Expr Expressions to join on_left : str, optional The fields from the left side to join on. If no ``on_right`` is passed, then these are the fields for both sides. on_right : str, optional The fields from the right side to join on. how : {'inner', 'outer', 'left', 'right'} What type of join to perform. suffixes: pair of str The suffixes to be applied to the left and right sides in order to resolve duplicate field names. Examples -------- >>> from blaze import symbol >>> names = symbol('names', 'var * {name: string, id: int}') >>> amounts = symbol('amounts', 'var * {amount: int, id: int}') Join tables based on shared column name >>> joined = join(names, amounts, 'id') Join based on different column names >>> amounts = symbol('amounts', 'var * {amount: int, acctNumber: int}') >>> joined = join(names, amounts, 'id', 'acctNumber') See Also -------- blaze.expr.collections.Merge """ __slots__ = ( '_hash', 'lhs', 'rhs', '_on_left', '_on_right', 'how', 'suffixes' ) __inputs__ = 'lhs', 'rhs' @property def on_left(self): on_left = self._on_left if isinstance(on_left, tuple): return list(on_left) return on_left @property def on_right(self): on_right = self._on_right if isinstance(on_right, tuple): return list(on_right) return on_right def _schema(self): """ Examples -------- >>> from blaze import symbol >>> t = symbol('t', 'var * {name: string, amount: int}') >>> s = symbol('t', 'var * {name: string, id: int}') >>> join(t, s).schema dshape("{name: string, amount: int32, id: int32}") >>> join(t, s, how='left').schema dshape("{name: string, amount: int32, id: ?int32}") Overlapping but non-joined fields append _left, _right >>> a = symbol('a', 'var * {x: int, y: int}') >>> b = symbol('b', 'var * {x: int, y: int}') >>> join(a, b, 'x').fields ['x', 'y_left', 'y_right'] """ option = lambda dt: dt if isinstance(dt, Option) else Option(dt) on_left = self.on_left if not isinstance(on_left, list): on_left = on_left, on_right = self.on_right if not isinstance(on_right, list): on_right = on_right, right_types = keymap( dict(zip(on_right, on_left)).get, self.rhs.dshape.measure.dict, ) joined = ( (name, promote(dt, right_types[name], promote_option=False)) for n, (name, dt) in enumerate(filter( compose(op.contains(on_left), first), self.lhs.dshape.measure.fields, )) ) left = [ (name, dt) for name, dt in zip( self.lhs.fields, types_of_fields(self.lhs.fields, self.lhs) ) if name not in on_left ] right = [ (name, dt) for name, dt in zip( self.rhs.fields, types_of_fields(self.rhs.fields, self.rhs) ) if name not in on_right ] # Handle overlapping but non-joined case, e.g. left_other = set(name for name, dt in left if name not in on_left) right_other = set(name for name, dt in right if name not in on_right) overlap = left_other & right_other left_suffix, right_suffix = self.suffixes left = ((name + left_suffix if name in overlap else name, dt) for name, dt in left) right = ((name + right_suffix if name in overlap else name, dt) for name, dt in right) if self.how in ('right', 'outer'): left = ((name, option(dt)) for name, dt in left) if self.how in ('left', 'outer'): right = ((name, option(dt)) for name, dt in right) return dshape(Record(chain(joined, left, right))) def _dshape(self): # TODO: think if this can be generalized return var * self.schema def types_of_fields(fields, expr): """ Get the types of fields in an expression Examples -------- >>> from blaze import symbol >>> expr = symbol('e', 'var * {x: int64, y: float32}') >>> types_of_fields('y', expr) ctype("float32") >>> types_of_fields(['y', 'x'], expr) (ctype("float32"), ctype("int64")) >>> types_of_fields('x', expr.x) ctype("int64") """ if isinstance(expr.dshape.measure, Record): return get(fields, expr.dshape.measure) else: if isinstance(fields, (tuple, list, set)): assert len(fields) == 1 fields, = fields assert fields == expr._name return expr.dshape.measure @copydoc(Join) def join(lhs, rhs, on_left=None, on_right=None, how='inner', suffixes=('_left', '_right')): if not on_left and not on_right: on_left = on_right = unpack(list(sorted( set(lhs.fields) & set(rhs.fields), key=lhs.fields.index))) if not on_right: on_right = on_left if isinstance(on_left, tuple): on_left = list(on_left) if isinstance(on_right, tuple): on_right = list(on_right) if not on_left or not on_right: raise ValueError( "Can not Join. No shared columns between %s and %s" % (lhs, rhs), ) left_types = listpack(types_of_fields(on_left, lhs)) right_types = listpack(types_of_fields(on_right, rhs)) if len(left_types) != len(right_types): raise ValueError( 'Length of on_left=%d not equal to length of on_right=%d' % ( len(left_types), len(right_types), ), ) for n, promotion in enumerate(map(partial(promote, promote_option=False), left_types, right_types)): if promotion == object_: raise TypeError( 'Schemata of joining columns do not match,' ' no promotion found for %s=%s and %s=%s' % ( on_left[n], left_types[n], on_right[n], right_types[n], ), ) _on_left = tuple(on_left) if isinstance(on_left, list) else on_left _on_right = (tuple(on_right) if isinstance(on_right, list) else on_right) how = how.lower() if how not in ('inner', 'outer', 'left', 'right'): raise ValueError("How parameter should be one of " "\n\tinner, outer, left, right." "\nGot: %s" % how) return Join(lhs, rhs, _on_left, _on_right, how, suffixes) class Concat(Expr): """ Stack tables on common columns Parameters ---------- lhs, rhs : Expr Collections to concatenate axis : int, optional The axis to concatenate on. Examples -------- >>> from blaze import symbol Vertically stack tables: >>> names = symbol('names', '5 * {name: string, id: int32}') >>> more_names = symbol('more_names', '7 * {name: string, id: int32}') >>> stacked = concat(names, more_names) >>> stacked.dshape dshape("12 * {name: string, id: int32}") Vertically stack matrices: >>> mat_a = symbol('a', '3 * 5 * int32') >>> mat_b = symbol('b', '3 * 5 * int32') >>> vstacked = concat(mat_a, mat_b, axis=0) >>> vstacked.dshape dshape("6 * 5 * int32") Horizontally stack matrices: >>> hstacked = concat(mat_a, mat_b, axis=1) >>> hstacked.dshape dshape("3 * 10 * int32") See Also -------- blaze.expr.collections.Merge """ __slots__ = '_hash', 'lhs', 'rhs', 'axis' __inputs__ = 'lhs', 'rhs' def _dshape(self): axis = self.axis ldshape = self.lhs.dshape lshape = ldshape.shape return DataShape( *(lshape[:axis] + ( _shape_add(lshape[axis], self.rhs.dshape.shape[axis]), ) + lshape[axis + 1:] + (ldshape.measure,)) ) def _shape_add(a, b): if isinstance(a, Var) or isinstance(b, Var): return var return Fixed(a.val + b.val) @copydoc(Concat) def concat(lhs, rhs, axis=0): ldshape = lhs.dshape rdshape = rhs.dshape if ldshape.measure != rdshape.measure: raise TypeError( 'Mismatched measures: {l} != {r}'.format( l=ldshape.measure, r=rdshape.measure ), ) lshape = ldshape.shape rshape = rdshape.shape for n, (a, b) in enumerate(zip_longest(lshape, rshape, fillvalue=None)): if n != axis and a != b: raise TypeError( 'Shapes are not equal along axis {n}: {a} != {b}'.format( n=n, a=a, b=b, ), ) if axis < 0 or 0 < len(lshape) <= axis: raise ValueError( "Invalid axis '{a}', must be in range: [0, {n})".format( a=axis, n=len(lshape) ), ) return Concat(lhs, rhs, axis) class IsIn(ElemWise): """Check if an expression contains values from a set. Return a boolean expression indicating whether another expression contains values that are members of a collection. Parameters ---------- expr : Expr Expression whose elements to check for membership in `keys` keys : Sequence Elements to test against. Blaze stores this as a ``frozenset``. Examples -------- Check if a vector contains any of 1, 2 or 3: >>> from blaze import symbol >>> t = symbol('t', '10 * int64') >>> expr = t.isin([1, 2, 3]) >>> expr.dshape dshape("10 * bool") """ __slots__ = '_hash', '_child', '_keys' def _schema(self): return datashape.bool_ def __str__(self): return '%s.%s(%s)' % (self._child, type(self).__name__.lower(), self._keys) @copydoc(IsIn) def isin(expr, keys): if isinstance(keys, Expr): raise TypeError('keys argument cannot be an expression, ' 'it must be an iterable object such as a list, ' 'tuple or set') return IsIn(expr, frozenset(keys)) class Shift(Expr): """ Shift a column backward or forward by N elements Parameters ---------- expr : Expr The expression to shift. This expression's dshape should be columnar n : int The number of elements to shift by. If n < 0 then shift backward, if n == 0 do nothing, else shift forward. """ __slots__ = '_hash', '_child', 'n' def _schema(self): measure = self._child.schema.measure # if we are not shifting or we are already an Option type then return # the child's schema if not self.n or isinstance(measure, Option): return measure else: return Option(measure) def _dshape(self): return DataShape(*(self._child.dshape.shape + tuple(self.schema))) def __str__(self): return '%s(%s, n=%d)' % ( type(self).__name__.lower(), self._child, self.n ) @copydoc(Shift) def shift(expr, n): if not isinstance(n, (numbers.Integral, np.integer)): raise TypeError('n must be an integer') return Shift(expr, n) dshape_method_list.extend([ (iscollection, set([sort, head, tail])), (lambda ds: len(ds.shape) == 1, set([distinct, shift])), (lambda ds: (len(ds.shape) == 1 and isscalar(getattr(ds.measure, 'key', ds.measure))), set([isin])), (lambda ds: len(ds.shape) == 1 and isscalar(ds.measure), set([isin])), ])
bsd-3-clause
jm-begon/scikit-learn
examples/mixture/plot_gmm_pdf.py
284
1528
""" ============================================= Density Estimation for a mixture of Gaussians ============================================= Plot the density estimation of a mixture of two Gaussians. Data is generated from two Gaussians with different centers and covariance matrices. """ import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LogNorm from sklearn import mixture n_samples = 300 # generate random sample, two components np.random.seed(0) # generate spherical data centered on (20, 20) shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 20]) # generate zero centered stretched Gaussian data C = np.array([[0., -0.7], [3.5, .7]]) stretched_gaussian = np.dot(np.random.randn(n_samples, 2), C) # concatenate the two datasets into the final training set X_train = np.vstack([shifted_gaussian, stretched_gaussian]) # fit a Gaussian Mixture Model with two components clf = mixture.GMM(n_components=2, covariance_type='full') clf.fit(X_train) # display predicted scores by the model as a contour plot x = np.linspace(-20.0, 30.0) y = np.linspace(-20.0, 40.0) X, Y = np.meshgrid(x, y) XX = np.array([X.ravel(), Y.ravel()]).T Z = -clf.score_samples(XX)[0] Z = Z.reshape(X.shape) CS = plt.contour(X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0), levels=np.logspace(0, 3, 10)) CB = plt.colorbar(CS, shrink=0.8, extend='both') plt.scatter(X_train[:, 0], X_train[:, 1], .8) plt.title('Negative log-likelihood predicted by a GMM') plt.axis('tight') plt.show()
bsd-3-clause
murali-munna/scikit-learn
examples/calibration/plot_calibration_multiclass.py
272
6972
""" ================================================== Probability Calibration for 3-class classification ================================================== This example illustrates how sigmoid calibration changes predicted probabilities for a 3-class classification problem. Illustrated is the standard 2-simplex, where the three corners correspond to the three classes. Arrows point from the probability vectors predicted by an uncalibrated classifier to the probability vectors predicted by the same classifier after sigmoid calibration on a hold-out validation set. Colors indicate the true class of an instance (red: class 1, green: class 2, blue: class 3). The base classifier is a random forest classifier with 25 base estimators (trees). If this classifier is trained on all 800 training datapoints, it is overly confident in its predictions and thus incurs a large log-loss. Calibrating an identical classifier, which was trained on 600 datapoints, with method='sigmoid' on the remaining 200 datapoints reduces the confidence of the predictions, i.e., moves the probability vectors from the edges of the simplex towards the center. This calibration results in a lower log-loss. Note that an alternative would have been to increase the number of base estimators which would have resulted in a similar decrease in log-loss. """ print(__doc__) # Author: Jan Hendrik Metzen <[email protected]> # License: BSD Style. import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_blobs from sklearn.ensemble import RandomForestClassifier from sklearn.calibration import CalibratedClassifierCV from sklearn.metrics import log_loss np.random.seed(0) # Generate data X, y = make_blobs(n_samples=1000, n_features=2, random_state=42, cluster_std=5.0) X_train, y_train = X[:600], y[:600] X_valid, y_valid = X[600:800], y[600:800] X_train_valid, y_train_valid = X[:800], y[:800] X_test, y_test = X[800:], y[800:] # Train uncalibrated random forest classifier on whole train and validation # data and evaluate on test data clf = RandomForestClassifier(n_estimators=25) clf.fit(X_train_valid, y_train_valid) clf_probs = clf.predict_proba(X_test) score = log_loss(y_test, clf_probs) # Train random forest classifier, calibrate on validation data and evaluate # on test data clf = RandomForestClassifier(n_estimators=25) clf.fit(X_train, y_train) clf_probs = clf.predict_proba(X_test) sig_clf = CalibratedClassifierCV(clf, method="sigmoid", cv="prefit") sig_clf.fit(X_valid, y_valid) sig_clf_probs = sig_clf.predict_proba(X_test) sig_score = log_loss(y_test, sig_clf_probs) # Plot changes in predicted probabilities via arrows plt.figure(0) colors = ["r", "g", "b"] for i in range(clf_probs.shape[0]): plt.arrow(clf_probs[i, 0], clf_probs[i, 1], sig_clf_probs[i, 0] - clf_probs[i, 0], sig_clf_probs[i, 1] - clf_probs[i, 1], color=colors[y_test[i]], head_width=1e-2) # Plot perfect predictions plt.plot([1.0], [0.0], 'ro', ms=20, label="Class 1") plt.plot([0.0], [1.0], 'go', ms=20, label="Class 2") plt.plot([0.0], [0.0], 'bo', ms=20, label="Class 3") # Plot boundaries of unit simplex plt.plot([0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], 'k', label="Simplex") # Annotate points on the simplex plt.annotate(r'($\frac{1}{3}$, $\frac{1}{3}$, $\frac{1}{3}$)', xy=(1.0/3, 1.0/3), xytext=(1.0/3, .23), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') plt.plot([1.0/3], [1.0/3], 'ko', ms=5) plt.annotate(r'($\frac{1}{2}$, $0$, $\frac{1}{2}$)', xy=(.5, .0), xytext=(.5, .1), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') plt.annotate(r'($0$, $\frac{1}{2}$, $\frac{1}{2}$)', xy=(.0, .5), xytext=(.1, .5), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') plt.annotate(r'($\frac{1}{2}$, $\frac{1}{2}$, $0$)', xy=(.5, .5), xytext=(.6, .6), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') plt.annotate(r'($0$, $0$, $1$)', xy=(0, 0), xytext=(.1, .1), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') plt.annotate(r'($1$, $0$, $0$)', xy=(1, 0), xytext=(1, .1), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') plt.annotate(r'($0$, $1$, $0$)', xy=(0, 1), xytext=(.1, 1), xycoords='data', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='center', verticalalignment='center') # Add grid plt.grid("off") for x in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]: plt.plot([0, x], [x, 0], 'k', alpha=0.2) plt.plot([0, 0 + (1-x)/2], [x, x + (1-x)/2], 'k', alpha=0.2) plt.plot([x, x + (1-x)/2], [0, 0 + (1-x)/2], 'k', alpha=0.2) plt.title("Change of predicted probabilities after sigmoid calibration") plt.xlabel("Probability class 1") plt.ylabel("Probability class 2") plt.xlim(-0.05, 1.05) plt.ylim(-0.05, 1.05) plt.legend(loc="best") print("Log-loss of") print(" * uncalibrated classifier trained on 800 datapoints: %.3f " % score) print(" * classifier trained on 600 datapoints and calibrated on " "200 datapoint: %.3f" % sig_score) # Illustrate calibrator plt.figure(1) # generate grid over 2-simplex p1d = np.linspace(0, 1, 20) p0, p1 = np.meshgrid(p1d, p1d) p2 = 1 - p0 - p1 p = np.c_[p0.ravel(), p1.ravel(), p2.ravel()] p = p[p[:, 2] >= 0] calibrated_classifier = sig_clf.calibrated_classifiers_[0] prediction = np.vstack([calibrator.predict(this_p) for calibrator, this_p in zip(calibrated_classifier.calibrators_, p.T)]).T prediction /= prediction.sum(axis=1)[:, None] # Ploit modifications of calibrator for i in range(prediction.shape[0]): plt.arrow(p[i, 0], p[i, 1], prediction[i, 0] - p[i, 0], prediction[i, 1] - p[i, 1], head_width=1e-2, color=colors[np.argmax(p[i])]) # Plot boundaries of unit simplex plt.plot([0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], 'k', label="Simplex") plt.grid("off") for x in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]: plt.plot([0, x], [x, 0], 'k', alpha=0.2) plt.plot([0, 0 + (1-x)/2], [x, x + (1-x)/2], 'k', alpha=0.2) plt.plot([x, x + (1-x)/2], [0, 0 + (1-x)/2], 'k', alpha=0.2) plt.title("Illustration of sigmoid calibrator") plt.xlabel("Probability class 1") plt.ylabel("Probability class 2") plt.xlim(-0.05, 1.05) plt.ylim(-0.05, 1.05) plt.show()
bsd-3-clause
valexandersaulys/prudential_insurance_kaggle
venv/lib/python2.7/site-packages/numpy/lib/npyio.py
42
71218
from __future__ import division, absolute_import, print_function import sys import os import re import itertools import warnings import weakref from operator import itemgetter import numpy as np from . import format from ._datasource import DataSource from numpy.core.multiarray import packbits, unpackbits from ._iotools import ( LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, has_nested_fields, flatten_dtype, easy_dtype, _bytes_to_name ) from numpy.compat import ( asbytes, asstr, asbytes_nested, bytes, basestring, unicode ) if sys.version_info[0] >= 3: import pickle else: import cPickle as pickle from future_builtins import map loads = pickle.loads __all__ = [ 'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource' ] class BagObj(object): """ BagObj(obj) Convert attribute look-ups to getitems on the object passed in. Parameters ---------- obj : class instance Object on which attribute look-up is performed. Examples -------- >>> from numpy.lib.npyio import BagObj as BO >>> class BagDemo(object): ... def __getitem__(self, key): # An instance of BagObj(BagDemo) ... # will call this method when any ... # attribute look-up is required ... result = "Doesn't matter what you want, " ... return result + "you're gonna get this" ... >>> demo_obj = BagDemo() >>> bagobj = BO(demo_obj) >>> bagobj.hello_there "Doesn't matter what you want, you're gonna get this" >>> bagobj.I_can_be_anything "Doesn't matter what you want, you're gonna get this" """ def __init__(self, obj): # Use weakref to make NpzFile objects collectable by refcount self._obj = weakref.proxy(obj) def __getattribute__(self, key): try: return object.__getattribute__(self, '_obj')[key] except KeyError: raise AttributeError(key) def __dir__(self): """ Enables dir(bagobj) to list the files in an NpzFile. This also enables tab-completion in an interpreter or IPython. """ return object.__getattribute__(self, '_obj').keys() def zipfile_factory(*args, **kwargs): import zipfile kwargs['allowZip64'] = True return zipfile.ZipFile(*args, **kwargs) class NpzFile(object): """ NpzFile(fid) A dictionary-like object with lazy-loading of files in the zipped archive provided on construction. `NpzFile` is used to load files in the NumPy ``.npz`` data archive format. It assumes that files in the archive have a ``.npy`` extension, other files are ignored. The arrays and file strings are lazily loaded on either getitem access using ``obj['key']`` or attribute lookup using ``obj.f.key``. A list of all files (without ``.npy`` extensions) can be obtained with ``obj.files`` and the ZipFile object itself using ``obj.zip``. Attributes ---------- files : list of str List of all files in the archive with a ``.npy`` extension. zip : ZipFile instance The ZipFile object initialized with the zipped archive. f : BagObj instance An object on which attribute can be performed as an alternative to getitem access on the `NpzFile` instance itself. allow_pickle : bool, optional Allow loading pickled data. Default: True pickle_kwargs : dict, optional Additional keyword arguments to pass on to pickle.load. These are only useful when loading object arrays saved on Python 2 when using Python 3. Parameters ---------- fid : file or str The zipped archive to open. This is either a file-like object or a string containing the path to the archive. own_fid : bool, optional Whether NpzFile should close the file handle. Requires that `fid` is a file-like object. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npz = np.load(outfile) >>> isinstance(npz, np.lib.io.NpzFile) True >>> npz.files ['y', 'x'] >>> npz['x'] # getitem access array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> npz.f.x # attribute lookup array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ def __init__(self, fid, own_fid=False, allow_pickle=True, pickle_kwargs=None): # Import is postponed to here since zipfile depends on gzip, an # optional component of the so-called standard library. _zip = zipfile_factory(fid) self._files = _zip.namelist() self.files = [] self.allow_pickle = allow_pickle self.pickle_kwargs = pickle_kwargs for x in self._files: if x.endswith('.npy'): self.files.append(x[:-4]) else: self.files.append(x) self.zip = _zip self.f = BagObj(self) if own_fid: self.fid = fid else: self.fid = None def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): """ Close the file. """ if self.zip is not None: self.zip.close() self.zip = None if self.fid is not None: self.fid.close() self.fid = None self.f = None # break reference cycle def __del__(self): self.close() def __getitem__(self, key): # FIXME: This seems like it will copy strings around # more than is strictly necessary. The zipfile # will read the string and then # the format.read_array will copy the string # to another place in memory. # It would be better if the zipfile could read # (or at least uncompress) the data # directly into the array memory. member = 0 if key in self._files: member = 1 elif key in self.files: member = 1 key += '.npy' if member: bytes = self.zip.open(key) magic = bytes.read(len(format.MAGIC_PREFIX)) bytes.close() if magic == format.MAGIC_PREFIX: bytes = self.zip.open(key) return format.read_array(bytes, allow_pickle=self.allow_pickle, pickle_kwargs=self.pickle_kwargs) else: return self.zip.read(key) else: raise KeyError("%s is not a file in the archive" % key) def __iter__(self): return iter(self.files) def items(self): """ Return a list of tuples, with each tuple (filename, array in file). """ return [(f, self[f]) for f in self.files] def iteritems(self): """Generator that returns tuples (filename, array in file).""" for f in self.files: yield (f, self[f]) def keys(self): """Return files in the archive with a ``.npy`` extension.""" return self.files def iterkeys(self): """Return an iterator over the files in the archive.""" return self.__iter__() def __contains__(self, key): return self.files.__contains__(key) def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII'): """ Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. Parameters ---------- file : file-like object or string The file to read. File-like objects must support the ``seek()`` and ``read()`` methods. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see `numpy.memmap` for a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. allow_pickle : bool, optional Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: True fix_imports : bool, optional Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. If `fix_imports` is True, pickle will try to map the old Python 2 names to the new names used in Python 3. encoding : str, optional What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than 'latin1', 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical data. Default: 'ASCII' Returns ------- result : array, tuple, dict, etc. Data stored in the file. For ``.npz`` files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors. Raises ------ IOError If the input file does not exist or cannot be read. ValueError The file contains an object array, but allow_pickle=False given. See Also -------- save, savez, savez_compressed, loadtxt memmap : Create a memory-map to an array stored in a file on disk. Notes ----- - If the file contains pickle data, then whatever object is stored in the pickle is returned. - If the file is a ``.npy`` file, then a single array is returned. - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. - If the file is a ``.npz`` file, the returned value supports the context manager protocol in a similar fashion to the open function:: with load('foo.npz') as data: a = data['a'] The underlying file descriptor is closed when exiting the 'with' block. Examples -------- Store data to disk, and load it again: >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) >>> np.load('/tmp/123.npy') array([[1, 2, 3], [4, 5, 6]]) Store compressed data to disk, and load it again: >>> a=np.array([[1, 2, 3], [4, 5, 6]]) >>> b=np.array([1, 2]) >>> np.savez('/tmp/123.npz', a=a, b=b) >>> data = np.load('/tmp/123.npz') >>> data['a'] array([[1, 2, 3], [4, 5, 6]]) >>> data['b'] array([1, 2]) >>> data.close() Mem-map the stored array, and then access the second row directly from disk: >>> X = np.load('/tmp/123.npy', mmap_mode='r') >>> X[1, :] memmap([4, 5, 6]) """ import gzip own_fid = False if isinstance(file, basestring): fid = open(file, "rb") own_fid = True else: fid = file if encoding not in ('ASCII', 'latin1', 'bytes'): # The 'encoding' value for pickle also affects what encoding # the serialized binary data of Numpy arrays is loaded # in. Pickle does not pass on the encoding information to # Numpy. The unpickling code in numpy.core.multiarray is # written to assume that unicode data appearing where binary # should be is in 'latin1'. 'bytes' is also safe, as is 'ASCII'. # # Other encoding values can corrupt binary data, and we # purposefully disallow them. For the same reason, the errors= # argument is not exposed, as values other than 'strict' # result can similarly silently corrupt numerical data. raise ValueError("encoding must be 'ASCII', 'latin1', or 'bytes'") if sys.version_info[0] >= 3: pickle_kwargs = dict(encoding=encoding, fix_imports=fix_imports) else: # Nothing to do on Python 2 pickle_kwargs = {} try: # Code to distinguish from NumPy binary files and pickles. _ZIP_PREFIX = asbytes('PK\x03\x04') N = len(format.MAGIC_PREFIX) magic = fid.read(N) fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) # Transfer file ownership to NpzFile tmp = own_fid own_fid = False return NpzFile(fid, own_fid=tmp, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) else: return format.read_array(fid, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) else: # Try a pickle if not allow_pickle: raise ValueError("allow_pickle=False, but file does not contain " "non-pickled data") try: return pickle.load(fid, **pickle_kwargs) except: raise IOError( "Failed to interpret file %s as a pickle" % repr(file)) finally: if own_fid: fid.close() def save(file, arr, allow_pickle=True, fix_imports=True): """ Save an array to a binary file in NumPy ``.npy`` format. Parameters ---------- file : file or str File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string, a ``.npy`` extension will be appended to the file name if it does not already have one. allow_pickle : bool, optional Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example if the stored objects require libraries that are not available, and not all pickled data is compatible between Python 2 and Python 3). Default: True fix_imports : bool, optional Only useful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way. If `fix_imports` is True, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2. arr : array_like Array data to be saved. See Also -------- savez : Save several arrays into a ``.npz`` archive savetxt, load Notes ----- For a description of the ``.npy`` format, see the module docstring of `numpy.lib.format` or the Numpy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ own_fid = False if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") own_fid = True else: fid = file if sys.version_info[0] >= 3: pickle_kwargs = dict(fix_imports=fix_imports) else: # Nothing to do on Python 2 pickle_kwargs = None try: arr = np.asanyarray(arr) format.write_array(fid, arr, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) finally: if own_fid: fid.close() def savez(file, *args, **kwds): """ Save several arrays into a single file in uncompressed ``.npz`` format. If arguments are passed in with no keywords, the corresponding variable names, in the ``.npz`` file, are 'arr_0', 'arr_1', etc. If keyword arguments are given, the corresponding variable names, in the ``.npz`` file will match the keyword names. Parameters ---------- file : str or file Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the ``.npz`` extension will be appended to the file name if it is not already there. args : Arguments, optional Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside `savez`, the arrays will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments, optional Arrays to save to the file. Arrays will be saved in the file with the keyword names. Returns ------- None See Also -------- save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in ``.npy`` format. For a description of the ``.npy`` format, see `numpy.lib.format` or the Numpy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the ``.files`` attribute), and for the arrays themselves. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) Using `savez` with \\*args, the arrays are saved with default names. >>> np.savez(outfile, x, y) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_1', 'arr_0'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Using `savez` with \\**kwds, the arrays are saved with the keyword names. >>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npzfile = np.load(outfile) >>> npzfile.files ['y', 'x'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ _savez(file, args, kwds, False) def savez_compressed(file, *args, **kwds): """ Save several arrays into a single file in compressed ``.npz`` format. If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : str File name of ``.npz`` file. args : Arguments Function arguments. kwds : Keyword arguments Keywords. See Also -------- numpy.savez : Save several arrays into an uncompressed ``.npz`` file format numpy.load : Load the files created by savez_compressed. """ _savez(file, args, kwds, True) def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None): # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile # Import deferred for startup time improvement import tempfile if isinstance(file, basestring): if not file.endswith('.npz'): file = file + '.npz' namedict = kwds for i, val in enumerate(args): key = 'arr_%d' % i if key in namedict.keys(): raise ValueError( "Cannot use un-named variables and keyword %s" % key) namedict[key] = val if compress: compression = zipfile.ZIP_DEFLATED else: compression = zipfile.ZIP_STORED zipf = zipfile_factory(file, mode="w", compression=compression) # Stage arrays in a temporary file on disk, before writing to zip. fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy') os.close(fd) try: for key, val in namedict.items(): fname = key + '.npy' fid = open(tmpfile, 'wb') try: format.write_array(fid, np.asanyarray(val), allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) fid.close() fid = None zipf.write(tmpfile, arcname=fname) finally: if fid: fid.close() finally: os.remove(tmpfile) zipf.close() def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ def floatconv(x): x.lower() if b'0x' in x: return float.fromhex(asstr(x)) return float(x) typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) if issubclass(typ, np.uint64): return np.uint64 if issubclass(typ, np.int64): return np.int64 if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.floating): return floatconv elif issubclass(typ, np.complex): return lambda x: complex(asstr(x)) elif issubclass(typ, np.bytes_): return bytes else: return str def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0): """ Load data from a text file. Each row in the text file must have the same number of values. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings for Python 3k. dtype : data-type, optional Data-type of the resulting array; default: float. If this is a structured data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type. comments : str or sequence, optional The characters or list of characters used to indicate the start of a comment; default: '#'. delimiter : str, optional The string used to separate values. By default, this is any whitespace. converters : dict, optional A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None. skiprows : int, optional Skip the first `skiprows` lines; default: 0. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)``. When used with a structured data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 Returns ------- out : ndarray Data read from the text file. See Also -------- load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files Notes ----- This function aims to be a fast reader for simply formatted files. The `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. .. versionadded:: 1.10.0 The strings produced by the Python float.hex method can be used as input for floats. Examples -------- >>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\\n2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72\\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([('M', 21, 72.0), ('F', 35, 58.0)], dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')]) >>> c = StringIO("1,0,2\\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([ 1., 3.]) >>> y array([ 2., 4.]) """ # Type conversions for Py3 convenience if comments is not None: if isinstance(comments, (basestring, bytes)): comments = [asbytes(comments)] else: comments = [asbytes(comment) for comment in comments] # Compile regex for comments beforehand comments = (re.escape(comment) for comment in comments) regex_comments = re.compile(asbytes('|').join(comments)) user_converters = converters if delimiter is not None: delimiter = asbytes(delimiter) if usecols is not None: usecols = list(usecols) fown = False try: if _is_string_like(fname): fown = True if fname.endswith('.gz'): import gzip fh = iter(gzip.GzipFile(fname)) elif fname.endswith('.bz2'): import bz2 fh = iter(bz2.BZ2File(fname)) elif sys.version_info[0] == 2: fh = iter(open(fname, 'U')) else: fh = iter(open(fname)) else: fh = iter(fname) except TypeError: raise ValueError('fname must be a string, file handle, or generator') X = [] def flatten_dtype(dt): """Unpack a structured data-type, and produce re-packing info.""" if dt.names is None: # If the dtype is flattened, return. # If the dtype has a shape, the dtype occurs # in the list more than once. shape = dt.shape if len(shape) == 0: return ([dt.base], None) else: packing = [(shape[-1], list)] if len(shape) > 1: for dim in dt.shape[-2::-1]: packing = [(dim*packing[0][0], packing*dim)] return ([dt.base] * int(np.prod(dt.shape)), packing) else: types = [] packing = [] for field in dt.names: tp, bytes = dt.fields[field] flat_dt, flat_packing = flatten_dtype(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays if len(tp.shape) > 0: packing.extend(flat_packing) else: packing.append((len(flat_dt), flat_packing)) return (types, packing) def pack_items(items, packing): """Pack items into nested lists based on re-packing info.""" if packing is None: return items[0] elif packing is tuple: return tuple(items) elif packing is list: return list(items) else: start = 0 ret = [] for length, subpacking in packing: ret.append(pack_items(items[start:start+length], subpacking)) start += length return tuple(ret) def split_line(line): """Chop off comments, strip, and split at delimiter. Note that although the file is opened as text, this function returns bytes. """ line = asbytes(line) if comments is not None: line = regex_comments.split(asbytes(line), maxsplit=1)[0] line = line.strip(asbytes('\r\n')) if line: return line.split(delimiter) else: return [] try: # Make sure we're dealing with a proper dtype dtype = np.dtype(dtype) defconv = _getconv(dtype) # Skip the first `skiprows` lines for i in range(skiprows): next(fh) # Read until we find a line with some values, and use # it to estimate the number of columns, N. first_vals = None try: while not first_vals: first_line = next(fh) first_vals = split_line(first_line) except StopIteration: # End of lines reached first_line = '' first_vals = [] warnings.warn('loadtxt: Empty input file: "%s"' % fname) N = len(usecols or first_vals) dtype_types, packing = flatten_dtype(dtype) if len(dtype_types) > 1: # We're dealing with a structured array, each field of # the dtype matches a column converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype converters = [defconv for i in range(N)] if N > 1: packing = [(N, tuple)] # By preference, use the converters specified by the user for i, conv in (user_converters or {}).items(): if usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue converters[i] = conv # Parse each line, including the first for i, line in enumerate(itertools.chain([first_line], fh)): vals = split_line(line) if len(vals) == 0: continue if usecols: vals = [vals[i] for i in usecols] if len(vals) != N: line_num = i + skiprows + 1 raise ValueError("Wrong number of columns at line %d" % line_num) # Convert each value according to its column and store items = [conv(val) for (conv, val) in zip(converters, vals)] # Then pack it according to the dtype's nesting items = pack_items(items, packing) X.append(items) finally: if fown: fh.close() X = np.array(X, dtype) # Multicolumn data are returned with shape (1, N, M), i.e. # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) # Verify that the array has at least dimensions `ndmin`. # Check correctness of the values of `ndmin` if ndmin not in [0, 1, 2]: raise ValueError('Illegal value of ndmin keyword: %s' % ndmin) # Tweak the size and shape of the arrays - remove extraneous dimensions if X.ndim > ndmin: X = np.squeeze(X) # and ensure we have the minimum number of dimensions asked for # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 if X.ndim < ndmin: if ndmin == 1: X = np.atleast_1d(X) elif ndmin == 2: X = np.atleast_2d(X).T if unpack: if len(dtype_types) > 1: # For structured arrays, return an array for each field. return [X[field] for field in dtype.names] else: return X.T else: return X def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '): """ Save an array to a text file. Parameters ---------- fname : filename or file handle If the filename ends in ``.gz``, the file is automatically saved in compressed gzip format. `loadtxt` understands gzipped files transparently. X : array_like Data to be saved to a text file. fmt : str or sequence of strs, optional A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. For complex `X`, the legal options for `fmt` are: a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted like `' (%s+%sj)' % (fmt, fmt)` b) a full string specifying every real and imaginary part, e.g. `' %.4e %+.4j %.4e %+.4j %.4e %+.4j'` for 3 columns c) a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns delimiter : str, optional String or character separating columns. newline : str, optional String or character separating lines. .. versionadded:: 1.5.0 header : str, optional String that will be written at the beginning of the file. .. versionadded:: 1.7.0 footer : str, optional String that will be written at the end of the file. .. versionadded:: 1.7.0 comments : str, optional String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# ', as expected by e.g. ``numpy.loadtxt``. .. versionadded:: 1.7.0 See Also -------- save : Save an array to a binary file in NumPy ``.npy`` format savez : Save several arrays into an uncompressed ``.npz`` archive savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- Further explanation of the `fmt` parameter (``%[flag]width[.precision]specifier``): flags: ``-`` : left justify ``+`` : Forces to precede result with + or -. ``0`` : Left pad the number with zeros instead of space (see width). width: Minimum number of characters to be printed. The value is not truncated if it has more characters. precision: - For integer specifiers (eg. ``d,i,o,x``), the minimum number of digits. - For ``e, E`` and ``f`` specifiers, the number of digits to print after the decimal point. - For ``g`` and ``G``, the maximum number of significant digits. - For ``s``, the maximum number of characters. specifiers: ``c`` : character ``d`` or ``i`` : signed decimal integer ``e`` or ``E`` : scientific notation with ``e`` or ``E``. ``f`` : decimal floating point ``g,G`` : use the shorter of ``e,E`` or ``f`` ``o`` : signed octal ``s`` : string of characters ``u`` : unsigned decimal integer ``x,X`` : unsigned hexadecimal integer This explanation of ``fmt`` is not complete, for an exhaustive specification see [1]_. References ---------- .. [1] `Format Specification Mini-Language <http://docs.python.org/library/string.html# format-specification-mini-language>`_, Python Documentation. Examples -------- >>> x = y = z = np.arange(0.0,5.0,1.0) >>> np.savetxt('test.out', x, delimiter=',') # X is an array >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation """ # Py3 conversions first if isinstance(fmt, bytes): fmt = asstr(fmt) delimiter = asstr(delimiter) own_fh = False if _is_string_like(fname): own_fh = True if fname.endswith('.gz'): import gzip fh = gzip.open(fname, 'wb') else: if sys.version_info[0] >= 3: fh = open(fname, 'wb') else: fh = open(fname, 'w') elif hasattr(fname, 'write'): fh = fname else: raise ValueError('fname must be a string or file handle') try: X = np.asarray(X) # Handle 1-dimensional arrays if X.ndim == 1: # Common case -- 1d array of numbers if X.dtype.names is None: X = np.atleast_2d(X).T ncol = 1 # Complex dtype -- each field indicates a separate column else: ncol = len(X.dtype.descr) else: ncol = X.shape[1] iscomplex_X = np.iscomplexobj(X) # `fmt` can be a string with multiple insertion points or a # list of formats. E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') if type(fmt) in (list, tuple): if len(fmt) != ncol: raise AttributeError('fmt has wrong shape. %s' % str(fmt)) format = asstr(delimiter).join(map(asstr, fmt)) elif isinstance(fmt, str): n_fmt_chars = fmt.count('%') error = ValueError('fmt has wrong number of %% formats: %s' % fmt) if n_fmt_chars == 1: if iscomplex_X: fmt = [' (%s+%sj)' % (fmt, fmt), ] * ncol else: fmt = [fmt, ] * ncol format = delimiter.join(fmt) elif iscomplex_X and n_fmt_chars != (2 * ncol): raise error elif ((not iscomplex_X) and n_fmt_chars != ncol): raise error else: format = fmt else: raise ValueError('invalid fmt: %r' % (fmt,)) if len(header) > 0: header = header.replace('\n', '\n' + comments) fh.write(asbytes(comments + header + newline)) if iscomplex_X: for row in X: row2 = [] for number in row: row2.append(number.real) row2.append(number.imag) fh.write(asbytes(format % tuple(row2) + newline)) else: for row in X: try: fh.write(asbytes(format % tuple(row) + newline)) except TypeError: raise TypeError("Mismatch between array dtype ('%s') and " "format specifier ('%s')" % (str(X.dtype), format)) if len(footer) > 0: footer = footer.replace('\n', '\n' + comments) fh.write(asbytes(comments + footer + newline)) finally: if own_fh: fh.close() def fromregex(file, regexp, dtype): """ Construct an array from a text file, using regular expression parsing. The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array. Parameters ---------- file : str or file File name or file object to read. regexp : str or regexp Regular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype. dtype : dtype or list of dtypes Dtype for the structured array. Returns ------- output : ndarray The output array, containing the part of the content of `file` that was matched by `regexp`. `output` is always a structured array. Raises ------ TypeError When `dtype` is not a valid dtype for a structured array. See Also -------- fromstring, loadtxt Notes ----- Dtypes for structured arrays can be specified in several forms, but all forms specify at least the data type and field name. For details see `doc.structured_arrays`. Examples -------- >>> f = open('test.dat', 'w') >>> f.write("1312 foo\\n1534 bar\\n444 qux") >>> f.close() >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex('test.dat', regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], dtype=[('num', '<i8'), ('key', '|S3')]) >>> output['num'] array([1312, 1534, 444], dtype=int64) """ own_fh = False if not hasattr(file, "read"): file = open(file, 'rb') own_fh = True try: if not hasattr(regexp, 'match'): regexp = re.compile(asbytes(regexp)) if not isinstance(dtype, np.dtype): dtype = np.dtype(dtype) seq = regexp.findall(file.read()) if seq and not isinstance(seq[0], tuple): # Only one group is in the regexp. # Create the new array as a single data-type and then # re-interpret as a single-field structured array. newdtype = np.dtype(dtype[dtype.names[0]]) output = np.array(seq, dtype=newdtype) output.dtype = dtype else: output = np.array(seq, dtype=dtype) return output finally: if own_fh: file.close() #####-------------------------------------------------------------------------- #---- --- ASCII functions --- #####-------------------------------------------------------------------------- def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None): """ Load data from a text file, with missing values handled as specified. Each line past the first `skip_header` lines is split at the `delimiter` character, and characters following the `comments` character are discarded. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is `.gz` or `.bz2`, the file is first decompressed. Note that generators must return byte strings in Python 3k. dtype : dtype, optional Data type of the resulting array. If None, the dtypes will be determined by the contents of each column, individually. comments : str, optional The character used to indicate the start of a comment. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive whitespaces act as delimiter. An integer or sequence of integers can also be provided as width(s) of each field. skiprows : int, optional `skiprows` was removed in numpy 1.10. Please use `skip_header` instead. skip_header : int, optional The number of lines to skip at the beginning of the file. skip_footer : int, optional The number of lines to skip at the end of the file. converters : variable, optional The set of functions that convert the data of a column to a value. The converters can also be used to provide a default value for missing data: ``converters = {3: lambda s: float(s or 0)}``. missing : variable, optional `missing` was removed in numpy 1.10. Please use `missing_values` instead. missing_values : variable, optional The set of strings corresponding to missing data. filling_values : variable, optional The set of values to be used as default when the data are missing. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. names : {None, True, str, sequence}, optional If `names` is True, the field names are read from the first valid line after the first `skip_header` lines. If `names` is a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. If `names` is None, the names of the dtype fields will be used, if any. excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended an underscore: for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional Whether to automatically strip white spaces from the variables. replace_space : char, optional Character(s) used in replacement of white spaces in the variables names. By default, use a '_'. case_sensitive : {True, False, 'upper', 'lower'}, optional If True, field names are case sensitive. If False or 'upper', field names are converted to upper case. If 'lower', field names are converted to lower case. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)`` usemask : bool, optional If True, return a masked array. If False, return a regular array. loose : bool, optional If True, do not raise errors for invalid values. invalid_raise : bool, optional If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped. max_rows : int, optional The maximum number of rows to read. Must not be used with skip_footer at the same time. If given, the value must be at least 1. Default is to read the entire file. .. versionadded:: 1.10.0 Returns ------- out : ndarray Data read from the text file. If `usemask` is True, this is a masked array. See Also -------- numpy.loadtxt : equivalent function when no data is missing. Notes ----- * When spaces are used as delimiters, or when no delimiter has been given as input, there should not be any missing data between two fields. * When the variables are named (either by a flexible dtype or with `names`, there must not be any header in the file (else a ValueError exception is raised). * Individual values are not stripped of spaces by default. When using a custom converter, make sure the function does remove spaces. References ---------- .. [1] Numpy User Guide, section `I/O with Numpy <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_. Examples --------- >>> from io import StringIO >>> import numpy as np Comma delimited file with mixed dtype >>> s = StringIO("1,1.3,abcde") >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ... ('mystring','S5')], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Using dtype = None >>> s.seek(0) # needed for StringIO example only >>> data = np.genfromtxt(s, dtype=None, ... names = ['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Specifying dtype and names >>> s.seek(0) >>> data = np.genfromtxt(s, dtype="i8,f8,S5", ... names=['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) An example with fixed-width columns >>> s = StringIO("11.3abcde") >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], ... delimiter=[1,3,5]) >>> data array((1, 1.3, 'abcde'), dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')]) """ if max_rows is not None: if skip_footer: raise ValueError( "The keywords 'skip_footer' and 'max_rows' can not be " "specified at the same time.") if max_rows < 1: raise ValueError("'max_rows' must be at least 1.") # Py3 data conversions to bytes, for convenience if comments is not None: comments = asbytes(comments) if isinstance(delimiter, unicode): delimiter = asbytes(delimiter) if isinstance(missing_values, (unicode, list, tuple)): missing_values = asbytes_nested(missing_values) # if usemask: from numpy.ma import MaskedArray, make_mask_descr # Check the input dictionary of converters user_converters = converters or {} if not isinstance(user_converters, dict): raise TypeError( "The input argument 'converter' should be a valid dictionary " "(got '%s' instead)" % type(user_converters)) # Initialize the filehandle, the LineSplitter and the NameValidator own_fhd = False try: if isinstance(fname, basestring): if sys.version_info[0] == 2: fhd = iter(np.lib._datasource.open(fname, 'rbU')) else: fhd = iter(np.lib._datasource.open(fname, 'rb')) own_fhd = True else: fhd = iter(fname) except TypeError: raise TypeError( "fname must be a string, filehandle, or generator. " "(got %s instead)" % type(fname)) split_line = LineSplitter(delimiter=delimiter, comments=comments, autostrip=autostrip)._handyman validate_names = NameValidator(excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Skip the first `skip_header` rows for i in range(skip_header): next(fhd) # Keep on until we find the first valid values first_values = None try: while not first_values: first_line = next(fhd) if names is True: if comments in first_line: first_line = ( asbytes('').join(first_line.split(comments)[1:])) first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty first_line = asbytes('') first_values = [] warnings.warn('genfromtxt: Empty input file: "%s"' % fname) # Should we take the first values as names ? if names is True: fval = first_values[0].strip() if fval in comments: del first_values[0] # Check the columns to use: make sure `usecols` is a list if usecols is not None: try: usecols = [_.strip() for _ in usecols.split(",")] except AttributeError: try: usecols = list(usecols) except TypeError: usecols = [usecols, ] nbcols = len(usecols or first_values) # Check the names and overwrite the dtype.names if needed if names is True: names = validate_names([_bytes_to_name(_.strip()) for _ in first_values]) first_line = asbytes('') elif _is_string_like(names): names = validate_names([_.strip() for _ in names.split(',')]) elif names: names = validate_names(names) # Get the dtype if dtype is not None: dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names, excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Make sure the names is a list (for 2.5) if names is not None: names = list(names) if usecols: for (i, current) in enumerate(usecols): # if usecols is a list of names, convert to a list of indices if _is_string_like(current): usecols[i] = names.index(current) elif current < 0: usecols[i] = current + len(first_values) # If the dtype is not None, make sure we update it if (dtype is not None) and (len(dtype) > nbcols): descr = dtype.descr dtype = np.dtype([descr[_] for _ in usecols]) names = list(dtype.names) # If `names` is not None, update the names elif (names is not None) and (len(names) > nbcols): names = [names[_] for _ in usecols] elif (names is not None) and (dtype is not None): names = list(dtype.names) # Process the missing values ............................... # Rename missing_values for convenience user_missing_values = missing_values or () # Define the list of missing_values (one column: one list) missing_values = [list([asbytes('')]) for _ in range(nbcols)] # We have a dictionary: process it field by field if isinstance(user_missing_values, dict): # Loop on the items for (key, val) in user_missing_values.items(): # Is the key a string ? if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped continue # Redefine the key as needed if it's a column number if usecols: try: key = usecols.index(key) except ValueError: pass # Transform the value as a list of string if isinstance(val, (list, tuple)): val = [str(_) for _ in val] else: val = [str(val), ] # Add the value(s) to the current list of missing if key is None: # None acts as default for miss in missing_values: miss.extend(val) else: missing_values[key].extend(val) # We have a sequence : each item matches a column elif isinstance(user_missing_values, (list, tuple)): for (value, entry) in zip(user_missing_values, missing_values): value = str(value) if value not in entry: entry.append(value) # We have a string : apply it to all entries elif isinstance(user_missing_values, bytes): user_value = user_missing_values.split(asbytes(",")) for entry in missing_values: entry.extend(user_value) # We have something else: apply it to all entries else: for entry in missing_values: entry.extend([str(user_missing_values)]) # Process the filling_values ............................... # Rename the input for convenience user_filling_values = filling_values if user_filling_values is None: user_filling_values = [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually if isinstance(user_filling_values, dict): for (key, val) in user_filling_values.items(): if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped, continue # Redefine the key if it's a column number and usecols is defined if usecols: try: key = usecols.index(key) except ValueError: pass # Add the value to the list filling_values[key] = val # We have a sequence : update on a one-to-one basis elif isinstance(user_filling_values, (list, tuple)): n = len(user_filling_values) if (n <= nbcols): filling_values[:n] = user_filling_values else: filling_values = user_filling_values[:nbcols] # We have something else : use it for all entries else: filling_values = [user_filling_values] * nbcols # Initialize the converters ................................ if dtype is None: # Note: we can't use a [...]*nbcols, as we would have 3 times the same # ... converter, instead of 3 different converters. converters = [StringConverter(None, missing_values=miss, default=fill) for (miss, fill) in zip(missing_values, filling_values)] else: dtype_flat = flatten_dtype(dtype, flatten_base=True) # Initialize the converters if len(dtype_flat) > 1: # Flexible type : get a converter from each dtype zipit = zip(dtype_flat, missing_values, filling_values) converters = [StringConverter(dt, locked=True, missing_values=miss, default=fill) for (dt, miss, fill) in zipit] else: # Set to a default converter (but w/ different missing values) zipit = zip(missing_values, filling_values) converters = [StringConverter(dtype, locked=True, missing_values=miss, default=fill) for (miss, fill) in zipit] # Update the converters to use the user-defined ones uc_update = [] for (j, conv) in user_converters.items(): # If the converter is specified by column names, use the index instead if _is_string_like(j): try: j = names.index(j) i = j except ValueError: continue elif usecols: try: i = usecols.index(j) except ValueError: # Unused converter specified continue else: i = j # Find the value to test - first_line is not filtered by usecols: if len(first_line): testing_value = first_values[j] else: testing_value = None converters[i].update(conv, locked=True, testing_value=testing_value, default=filling_values[i], missing_values=missing_values[i],) uc_update.append((i, conv)) # Make sure we have the corrected keys in user_converters... user_converters.update(uc_update) # Fixme: possible error as following variable never used. #miss_chars = [_.missing_values for _ in converters] # Initialize the output lists ... # ... rows rows = [] append_to_rows = rows.append # ... masks if usemask: masks = [] append_to_masks = masks.append # ... invalid invalid = [] append_to_invalid = invalid.append # Parse each line for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): values = split_line(line) nbvalues = len(values) # Skip an empty line if nbvalues == 0: continue if usecols: # Select only the columns we need try: values = [values[_] for _ in usecols] except IndexError: append_to_invalid((i + skip_header + 1, nbvalues)) continue elif nbvalues != nbcols: append_to_invalid((i + skip_header + 1, nbvalues)) continue # Store the values append_to_rows(tuple(values)) if usemask: append_to_masks(tuple([v.strip() in m for (v, m) in zip(values, missing_values)])) if len(rows) == max_rows: break if own_fhd: fhd.close() # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): current_column = [itemgetter(i)(_m) for _m in rows] try: converter.iterupgrade(current_column) except ConverterLockError: errmsg = "Converter #%i is locked and cannot be upgraded: " % i current_column = map(itemgetter(i), rows) for (j, value) in enumerate(current_column): try: converter.upgrade(value) except (ConverterError, ValueError): errmsg += "(occurred line #%i for value '%s')" errmsg %= (j + 1 + skip_header, value) raise ConverterError(errmsg) # Check that we don't have invalid values nbinvalid = len(invalid) if nbinvalid > 0: nbrows = len(rows) + nbinvalid - skip_footer # Construct the error message template = " Line #%%i (got %%i columns instead of %i)" % nbcols if skip_footer > 0: nbinvalid_skipped = len([_ for _ in invalid if _[0] > nbrows + skip_header]) invalid = invalid[:nbinvalid - nbinvalid_skipped] skip_footer -= nbinvalid_skipped # # nbrows -= skip_footer # errmsg = [template % (i, nb) # for (i, nb) in invalid if i < nbrows] # else: errmsg = [template % (i, nb) for (i, nb) in invalid] if len(errmsg): errmsg.insert(0, "Some errors were detected !") errmsg = "\n".join(errmsg) # Raise an exception ? if invalid_raise: raise ValueError(errmsg) # Issue a warning ? else: warnings.warn(errmsg, ConversionWarning) # Strip the last skip_footer data if skip_footer > 0: rows = rows[:-skip_footer] if usemask: masks = masks[:-skip_footer] # Convert each value according to the converter: # We want to modify the list in place to avoid creating a new one... if loose: rows = list( zip(*[[conv._loose_call(_r) for _r in map(itemgetter(i), rows)] for (i, conv) in enumerate(converters)])) else: rows = list( zip(*[[conv._strict_call(_r) for _r in map(itemgetter(i), rows)] for (i, conv) in enumerate(converters)])) # Reset the dtype data = rows if dtype is None: # Get the dtypes from the types of the converters column_types = [conv.type for conv in converters] # Find the columns with strings... strcolidx = [i for (i, v) in enumerate(column_types) if v in (type('S'), np.string_)] # ... and take the largest number of chars. for i in strcolidx: column_types[i] = "|S%i" % max(len(row[i]) for row in data) # if names is None: # If the dtype is uniform, don't define names, else use '' base = set([c.type for c in converters if c._checked]) if len(base) == 1: (ddtype, mdtype) = (list(base)[0], np.bool) else: ddtype = [(defaultfmt % i, dt) for (i, dt) in enumerate(column_types)] if usemask: mdtype = [(defaultfmt % i, np.bool) for (i, dt) in enumerate(column_types)] else: ddtype = list(zip(names, column_types)) mdtype = list(zip(names, [np.bool] * len(column_types))) output = np.array(data, dtype=ddtype) if usemask: outputmask = np.array(masks, dtype=mdtype) else: # Overwrite the initial dtype names if needed if names and dtype.names: dtype.names = names # Case 1. We have a structured type if len(dtype_flat) > 1: # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] # First, create the array using a flattened dtype: # [('a', int), ('b1', int), ('b2', float)] # Then, view the array using the specified dtype. if 'O' in (_.char for _ in dtype_flat): if has_nested_fields(dtype): raise NotImplementedError( "Nested fields involving objects are not supported...") else: output = np.array(data, dtype=dtype) else: rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) output = rows.view(dtype) # Now, process the rowmasks the same way if usemask: rowmasks = np.array( masks, dtype=np.dtype([('', np.bool) for t in dtype_flat])) # Construct the new dtype mdtype = make_mask_descr(dtype) outputmask = rowmasks.view(mdtype) # Case #2. We have a basic dtype else: # We used some user-defined converters if user_converters: ishomogeneous = True descr = [] for i, ttype in enumerate([conv.type for conv in converters]): # Keep the dtype of the current converter if i in user_converters: ishomogeneous &= (ttype == dtype.type) if ttype == np.string_: ttype = "|S%i" % max(len(row[i]) for row in data) descr.append(('', ttype)) else: descr.append(('', dtype)) # So we changed the dtype ? if not ishomogeneous: # We have more than one field if len(descr) > 1: dtype = np.dtype(descr) # We have only one field: drop the name if not needed. else: dtype = np.dtype(ttype) # output = np.array(data, dtype) if usemask: if dtype.names: mdtype = [(_, np.bool) for _ in dtype.names] else: mdtype = np.bool outputmask = np.array(masks, dtype=mdtype) # Try to take care of the missing data we missed names = output.dtype.names if usemask and names: for (name, conv) in zip(names or (), converters): missing_values = [conv(_) for _ in conv.missing_values if _ != asbytes('')] for mval in missing_values: outputmask[name] |= (output[name] == mval) # Construct the final array if usemask: output = output.view(MaskedArray) output._mask = outputmask if unpack: return output.squeeze().T return output.squeeze() def ndfromtxt(fname, **kwargs): """ Load ASCII data stored in a file and return it as a single array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function. """ kwargs['usemask'] = False return genfromtxt(fname, **kwargs) def mafromtxt(fname, **kwargs): """ Load ASCII data stored in a text file and return a masked array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ kwargs['usemask'] = True return genfromtxt(fname, **kwargs) def recfromtxt(fname, **kwargs): """ Load ASCII data from a file and return it in a record array. If ``usemask=False`` a standard `recarray` is returned, if ``usemask=True`` a MaskedRecords array is returned. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ kwargs.setdefault("dtype", None) usemask = kwargs.get('usemask', False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output def recfromcsv(fname, **kwargs): """ Load ASCII data stored in a comma-separated file. The returned array is a record array (if ``usemask=False``, see `recarray`) or a masked record array (if ``usemask=True``, see `ma.mrecords.MaskedRecords`). Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ # Set default kwargs for genfromtxt as relevant to csv import. kwargs.setdefault("case_sensitive", "lower") kwargs.setdefault("names", True) kwargs.setdefault("delimiter", ",") kwargs.setdefault("dtype", None) output = genfromtxt(fname, **kwargs) usemask = kwargs.get("usemask", False) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output
gpl-2.0
kmike/scikit-learn
examples/svm/plot_svm_scale_c.py
3
5402
""" ========================================================================= Support Vector Classification (SVC): scaling the regularization parameter ========================================================================= The following example illustrates the effect of scaling the regularization parameter when using :ref:`svm` for :ref:`classification <svm_classification>`. For SVC classification, we are interested in a risk minimization for the equation: .. math:: C \sum_{i=1, n} \mathcal{L} (f(x_i), y_i) + \Omega (w) where - :math:`C` is used to set the amount of regularization - :math:`\mathcal{L}` is a `loss` function of our samples and our model parameters. - :math:`\Omega` is a `penalty` function of our model parameters If we consider the loss function to be the individual error per sample, then the data-fit term, or the sum of the error for each sample, will increase as we add more samples. The penalization term, however, will not increase. When using, for example, :ref:`cross validation <cross_validation>`, to set the amount of regularization with `C`, there will be a different amount of samples between the main problem and the smaller problems withing the folds of the cross validation. Since our loss function is dependant on the amount of samples, the latter will influence the selected value of `C`. The question that arises is `How do we optimally adjust C to account for the different amount of training samples?` The figures below are used to illustrate the effect of scaling our `C` to compensate for the change in the number of samples, in the case of using an `L1` penalty, as well as the `L2` penalty. L1-penalty case ----------------- In the `L1` case, theory says that prediction consistency (i.e. that under given hypothesis, the estimator learned predicts as well as a model knowing the true distribution) is not possible because of the bias of the `L1`. It does say, however, that model consistency, in terms of finding the right set of non-zero parameters as well as their signs, can be achieved by scaling `C1`. L2-penalty case ----------------- The theory says that in order to achieve prediction consistency, the penalty parameter should be kept constant as the number of samples grow. Simulations ------------ The two figures below plot the values of `C` on the `x-axis` and the corresponding cross-validation scores on the `y-axis`, for several different fractions of a generated data-set. In the `L1` penalty case, the cross-validation-error correlates best with the test-error, when scaling our `C` with the number of samples, `n`, which can be seen in the first figure. For the `L2` penalty case, the best result comes from the case where `C` is not scaled. .. topic:: Note: Two seperate datasets are used for the two different plots. The reason behind this is the `L1` case works better on sparse data, while `L2` is better suited to the non-sparse case. """ print(__doc__) # Author: Andreas Mueller <[email protected]> # Jaques Grobler <[email protected]> # License: BSD import numpy as np import pylab as pl from sklearn.svm import LinearSVC from sklearn.cross_validation import ShuffleSplit from sklearn.grid_search import GridSearchCV from sklearn.utils import check_random_state from sklearn import datasets rnd = check_random_state(1) # set up dataset n_samples = 100 n_features = 300 # L1 data (only 5 informative features) X_1, y_1 = datasets.make_classification(n_samples=n_samples, n_features=n_features, n_informative=5, random_state=1) # L2 data: non sparse, but less features y_2 = np.sign(.5 - rnd.rand(n_samples)) X_2 = rnd.randn(n_samples, n_features / 5) + y_2[:, np.newaxis] X_2 += 5 * rnd.randn(n_samples, n_features / 5) clf_sets = [(LinearSVC(penalty='L1', loss='L2', dual=False, tol=1e-3), np.logspace(-2.3, -1.3, 10), X_1, y_1), (LinearSVC(penalty='L2', loss='L2', dual=True, tol=1e-4), np.logspace(-4.5, -2, 10), X_2, y_2)] colors = ['b', 'g', 'r', 'c'] for fignum, (clf, cs, X, y) in enumerate(clf_sets): # set up the plot for each regressor pl.figure(fignum, figsize=(9, 10)) for k, train_size in enumerate(np.linspace(0.3, 0.7, 3)[::-1]): param_grid = dict(C=cs) # To get nice curve, we need a large number of iterations to # reduce the variance grid = GridSearchCV(clf, refit=False, param_grid=param_grid, cv=ShuffleSplit(n=n_samples, train_size=train_size, n_iter=250, random_state=1)) grid.fit(X, y) scores = [x[1] for x in grid.cv_scores_] scales = [(1, 'No scaling'), ((n_samples * train_size), '1/n_samples'), ] for subplotnum, (scaler, name) in enumerate(scales): pl.subplot(2, 1, subplotnum + 1) pl.xlabel('C') pl.ylabel('CV Score') grid_cs = cs * float(scaler) # scale the C's pl.semilogx(grid_cs, scores, label="fraction %.2f" % train_size) pl.title('scaling=%s, penalty=%s, loss=%s' % (name, clf.penalty, clf.loss)) pl.legend(loc="best") pl.show()
bsd-3-clause
aabadie/scikit-learn
sklearn/metrics/tests/test_pairwise.py
13
26241
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_raises_regexp from sklearn.utils.testing import assert_true from sklearn.utils.testing import ignore_warnings 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 laplacian_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 PAIRWISE_BOOLEAN_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 paired_distances from sklearn.metrics.pairwise import paired_euclidean_distances from sklearn.metrics.pairwise import paired_manhattan_distances from sklearn.preprocessing import normalize from sklearn.exceptions import DataConversionWarning 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 scikit-learn 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 # The string "cosine" uses sklearn.metric, # while the function cosine 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) # 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 unknown assert_raises(ValueError, pairwise_distances, X, Y, metric="blah") # ignore conversion to boolean in pairwise_distances @ignore_warnings(category=DataConversionWarning) def test_pairwise_boolean_distance(): # test that we convert to boolean arrays for boolean distances rng = np.random.RandomState(0) X = rng.randn(5, 4) Y = X.copy() Y[0, 0] = 1 - Y[0, 0] for metric in PAIRWISE_BOOLEAN_FUNCTIONS: for Z in [Y, None]: res = pairwise_distances(X, Z, metric=metric) res[np.isnan(res)] = 0 assert_true(np.sum(res != 0) == 0) def test_pairwise_precomputed(): for func in [pairwise_distances, pairwise_kernels]: # Test correct shape assert_raises_regexp(ValueError, '.* shape .*', func, np.zeros((5, 3)), metric='precomputed') # with two args assert_raises_regexp(ValueError, '.* shape .*', func, np.zeros((5, 3)), np.zeros((4, 4)), metric='precomputed') # even if shape[1] agrees (although thus second arg is spurious) assert_raises_regexp(ValueError, '.* shape .*', func, np.zeros((5, 3)), np.zeros((4, 3)), metric='precomputed') # Test not copied (if appropriate dtype) S = np.zeros((5, 5)) S2 = func(S, metric="precomputed") assert_true(S is S2) # with two args S = np.zeros((5, 3)) S2 = func(S, np.zeros((3, 3)), metric="precomputed") assert_true(S is S2) # Test always returns float dtype S = func(np.array([[1]], dtype='int'), metric='precomputed') assert_equal('f', S.dtype.kind) # Test converts list to array-like S = func([[1.]], metric='precomputed') assert_true(isinstance(S, np.ndarray)) 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", "laplacian", "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 = {'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 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 scikit-learn 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.]]) rng = np.random.RandomState(0) X = rng.random_sample((10, 4)) Y = rng.random_sample((20, 4)) X_norm_sq = (X ** 2).sum(axis=1).reshape(1, -1) Y_norm_sq = (Y ** 2).sum(axis=1).reshape(1, -1) # check that we still get the right answers with {X,Y}_norm_squared D1 = euclidean_distances(X, Y) D2 = euclidean_distances(X, Y, X_norm_squared=X_norm_sq) D3 = euclidean_distances(X, Y, Y_norm_squared=Y_norm_sq) D4 = euclidean_distances(X, Y, X_norm_squared=X_norm_sq, Y_norm_squared=Y_norm_sq) assert_array_almost_equal(D2, D1) assert_array_almost_equal(D3, D1) assert_array_almost_equal(D4, D1) # check we get the wrong answer with wrong {X,Y}_norm_squared X_norm_sq *= 0.5 Y_norm_sq *= 0.5 wrong_D = euclidean_distances(X, Y, X_norm_squared=np.zeros_like(X_norm_sq), Y_norm_squared=np.zeros_like(Y_norm_sq)) assert_greater(np.max(np.abs(wrong_D - D1)), .01) # 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, laplacian_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, laplacian_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_laplacian_kernel(): rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) K = laplacian_kernel(X, X) # the diagonal elements of a laplacian kernel are 1 assert_array_almost_equal(np.diag(K), np.ones(5)) # off-diagonal elements are < 1 but > 0: assert_true(np.all(K > 0)) assert_true(np.all(K - np.diag(np.diag(K)) < 1)) def test_cosine_similarity_sparse_output(): # Test if cosine_similarity correctly produces sparse output. 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) K1 = cosine_similarity(Xcsr, Ycsr, dense_output=False) assert_true(issparse(K1)) K2 = pairwise_kernels(Xcsr, Y=Ycsr, metric="cosine") assert_array_almost_equal(K1.todense(), K2) 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. # The modified tests are not 1D. In the old test, the array was internally # converted to 2D anyways XA = np.arange(45).reshape(9, 5) XB = np.arange(32).reshape(4, 8) assert_raises(ValueError, check_pairwise_arrays, XA, XB) XA = np.arange(45).reshape(9, 5) XB = np.arange(32).reshape(4, 8) 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
lthurlow/Boolean-Constrained-Routing
networkx-1.8.1/examples/multigraph/chess_masters.py
11
5140
#!/usr/bin/env python """ An example of the MultiDiGraph clas The function chess_pgn_graph reads a collection of chess matches stored in the specified PGN file (PGN ="Portable Game Notation") Here the (compressed) default file --- chess_masters_WCC.pgn.bz2 --- contains all 685 World Chess Championship matches from 1886 - 1985. (data from http://chessproblem.my-free-games.com/chess/games/Download-PGN.php) The chess_pgn_graph() function returns a MultiDiGraph with multiple edges. Each node is the last name of a chess master. Each edge is directed from white to black and contains selected game info. The key statement in chess_pgn_graph below is G.add_edge(white, black, game_info) where game_info is a dict describing each game. """ # Copyright (C) 2006-2010 by # Aric Hagberg <[email protected]> # Dan Schult <[email protected]> # Pieter Swart <[email protected]> # All rights reserved. # BSD license. import networkx as nx # tag names specifying what game info should be # stored in the dict on each digraph edge game_details=["Event", "Date", "Result", "ECO", "Site"] def chess_pgn_graph(pgn_file="chess_masters_WCC.pgn.bz2"): """Read chess games in pgn format in pgn_file. Filenames ending in .gz or .bz2 will be uncompressed. Return the MultiDiGraph of players connected by a chess game. Edges contain game data in a dict. """ import bz2 G=nx.MultiDiGraph() game={} datafile = bz2.BZ2File(pgn_file) lines = (line.decode().rstrip('\r\n') for line in datafile) for line in lines: if line.startswith('['): tag,value=line[1:-1].split(' ',1) game[str(tag)]=value.strip('"') else: # empty line after tag set indicates # we finished reading game info if game: white=game.pop('White') black=game.pop('Black') G.add_edge(white, black, **game) game={} return G if __name__ == '__main__': import networkx as nx G=chess_pgn_graph() ngames=G.number_of_edges() nplayers=G.number_of_nodes() print("Loaded %d chess games between %d players\n"\ % (ngames,nplayers)) # identify connected components # of the undirected version Gcc=nx.connected_component_subgraphs(G.to_undirected()) if len(Gcc)>1: print("Note the disconnected component consisting of:") print(Gcc[1].nodes()) # find all games with B97 opening (as described in ECO) openings=set([game_info['ECO'] for (white,black,game_info) in G.edges(data=True)]) print("\nFrom a total of %d different openings,"%len(openings)) print('the following games used the Sicilian opening') print('with the Najdorff 7...Qb6 "Poisoned Pawn" variation.\n') for (white,black,game_info) in G.edges(data=True): if game_info['ECO']=='B97': print(white,"vs",black) for k,v in game_info.items(): print(" ",k,": ",v) print("\n") try: import matplotlib.pyplot as plt except ImportError: import sys print("Matplotlib needed for drawing. Skipping") sys.exit(0) # make new undirected graph H without multi-edges H=nx.Graph(G) # edge width is proportional number of games played edgewidth=[] for (u,v,d) in H.edges(data=True): edgewidth.append(len(G.get_edge_data(u,v))) # node size is proportional to number of games won wins=dict.fromkeys(G.nodes(),0.0) for (u,v,d) in G.edges(data=True): r=d['Result'].split('-') if r[0]=='1': wins[u]+=1.0 elif r[0]=='1/2': wins[u]+=0.5 wins[v]+=0.5 else: wins[v]+=1.0 try: pos=nx.graphviz_layout(H) except: pos=nx.spring_layout(H,iterations=20) plt.rcParams['text.usetex'] = False plt.figure(figsize=(8,8)) nx.draw_networkx_edges(H,pos,alpha=0.3,width=edgewidth, edge_color='m') nodesize=[wins[v]*50 for v in H] nx.draw_networkx_nodes(H,pos,node_size=nodesize,node_color='w',alpha=0.4) nx.draw_networkx_edges(H,pos,alpha=0.4,node_size=0,width=1,edge_color='k') nx.draw_networkx_labels(H,pos,fontsize=14) font = {'fontname' : 'Helvetica', 'color' : 'k', 'fontweight' : 'bold', 'fontsize' : 14} plt.title("World Chess Championship Games: 1886 - 1985", font) # change font and write text (using data coordinates) font = {'fontname' : 'Helvetica', 'color' : 'r', 'fontweight' : 'bold', 'fontsize' : 14} plt.text(0.5, 0.97, "edge width = # games played", horizontalalignment='center', transform=plt.gca().transAxes) plt.text(0.5, 0.94, "node size = # games won", horizontalalignment='center', transform=plt.gca().transAxes) plt.axis('off') plt.savefig("chess_masters.png",dpi=75) print("Wrote chess_masters.png") plt.show() # display
mit
bosmanoglu/adore-doris
lib/python/basic/__init__.py
1
24798
# -*- coding: utf-8 -*- """ Created on Fri Sep 10 13:47:54 2010 @author: bosmanoglu basic library includes several functions to simplify programming. confirm(prompt=None, resp=False) ind2sub(shp, idx) isarray(a) isint(x) isfloat(f) peaks(n=49) maskshow(array, mask=None) mdot(listIn) nonan(array) progress(p=1,t=1,f="%.3f ") progresstime(t0=0,timeSpan=120) rescale(arr, lim) shallIStop(t0,timeSpan=120) sub2ind(shap, sub) tic() toc(t) validIndex(arrSize, arrIdx) wrapToPi(x) wrapToInt(x, period) colorbarFigure(cmap,norm,label="") fillNan cdiff div """ import operator import sys # import exceptions import builtins as exceptions from numpy import * import pylab as plt import time from .graphics import graphics class rkdict(dict): #return (missing) key dict def __missing__(self, key): return key class DictObj(object): def __init__(self, **entries): if entries: for e in entries: #No space and dot for attribute name et="_".join(e.split()) et=et.replace('.','') if isinstance(d[e], dict): self.__dict__[et]=DictObj(d[e]) else: self.__dict__[et]=d[e] def _add_property(self, name, func): setattr(self.__class__, name, property(func)) def __missing__(self, key): return None #def list_methods(): # import basic # for m in dir(basic): # try: # if "basic" in basic.__dict__[m]: # print m # except: # pass def confirm(prompt=None, resp=False): """prompts for yes or no response from the user. Returns True for yes and False for no. 'resp' should be set to the default value assumed by the caller when user simply types ENTER. >>> confirm(prompt='Create Directory?', resp=True) Create Directory? [y]|n: True >>> confirm(prompt='Create Directory?', resp=False) Create Directory? [n]|y: False >>> confirm(prompt='Create Directory?', resp=False) Create Directory? [n]|y: y True Reference:http://code.activestate.com/recipes/541096-prompt-the-user-for-confirmation/ """ if prompt is None: prompt = 'Confirm' if resp: prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n') else: prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y') while True: ans = raw_input(prompt) if not ans: return resp if ans not in ['y', 'Y', 'n', 'N']: print('please enter y or n.') continue if ans == 'y' or ans == 'Y': return True if ans == 'n' or ans == 'N': return False def corr2(x,y,w): '''correlate(x, y, w): input is master and slave complex images (tested for 1D only) w is the calculation window. ''' import scipy scipy.pkgload('signal') cor=zeros(size(x)) corrFilter= ones(w) nfilt=corrFilter.size corrFilter=corrFilter/nfilt # Em=scipy.ndimage.filters.correlate(m*conj(m),corrFilter,mode='nearest') # Es=scipy.ndimage.filters.correlate(s*conj(s),corrFilter,mode='nearest') # Ems=scipy.ndimage.filters.correlate(m*conj(s),corrFilter,mode='nearest') Ex=scipy.signal.signaltools.correlate(x, corrFilter, mode='same') Ey=scipy.signal.signaltools.correlate(y, corrFilter, mode='same') cor=scipy.signal.signaltools.correlate((x-Ex)*(y-Ey)/sqrt((x-Ex)**2*(y-Ey)**2), corrFilter, mode='same') #Vy=scipy.signal.signaltools.correlate((y-Ey)**2, corrFilter, mode='same') #cor=abs( (x-Ex)*(y-Ey) / sqrt(Vx*Vy) ) return cor def nancorr2(x,y,w): '''nancorr2(x, y, w): input is master and slave complex images (tested for 1D only) w is the calculation window. ''' import scipy scipy.pkgload('signal') w=array(w); cor=zeros(x.shape) Ex=empty(x.shape) Ey=empty(y.shape) for k,l in ( (k,l) for k in range(x.shape[0]) for l in range(x.shape[1]) ): idx=[ (kk,ll) for kk in range(k-w[0],k+w[0]) for ll in range(l-w[1], l+w[1]) ] vidx=validIndex(x.shape, idx); Ex[k,l]=nonan(x[vidx[:,0],vidx[:,1]]).mean() Ey[k,l]=nonan(y[vidx[:,0],vidx[:,1]]).mean() corrFilter= ones(2*w+1) nfilt=corrFilter.size corrFilter=corrFilter/nfilt #Ex=scipy.signal.signaltools.correlate(x, corrFilter, mode='same') #Ey=scipy.signal.signaltools.correlate(y, corrFilter, mode='same') cor=scipy.signal.signaltools.correlate((x-Ex)*(y-Ey)/sqrt((x-Ex)**2*(y-Ey)**2), corrFilter, mode='same') return cor def cdiff(A, axis=0): """cdiff, returns the center difference for Array A in given axis """ singleDim=False if len(A.shape)==1: A=atleast_2d(A).T singleDim=True A=rollaxis(A, axis) out=zeros(A.shape); out[0,:]=A[0,:]-A[1,:] for i in range(1,A.shape[0]-1): out[i,:] = (A[i+1,:] - A[i-1,:])/2 out[-1,:] = A[-1,:]-A[-2,:] if singleDim==True: return squeeze(rollaxis(out, -axis)) else: return rollaxis(out, -axis) def div(U,V,spacing=[1.,1.]): """div(U,V,spacing=[1.,1.]) Divergence of a 2D array. http://en.wikipedia.org/wiki/Divergence """ return cdiff(U,0)/spacing[0]+cdiff(V,1)/spacing[1]; def fillNan(arr, copy=True, method='quick', maxiter=None): """ outData=basic.fillNan(data, copy=True) Fills nan's of an ND-array with a nearest neighbor like algorithm. copy: If true creates a copy of the input array and returns it filled. If false works on the input array, without creating a copy. For algorithm details see the authors explanation on the following page. Source: http://stackoverflow.com/questions/5551286/filling-gaps-in-a-numpy-array """ if method=='quick': # -- setup -- if copy: data=arr.copy() else: data=arr shape = data.shape dim = len(shape) #data = np.random.random(shape) flag = ~isnan(data);#zeros(shape, dtype=bool) t_ct = int(data.size/5) #flag.flat[random.randint(0, flag.size, t_ct)] = True # True flags the data # -- end setup -- slcs = [slice(None)]*dim k=0 while any(~flag): # as long as there are any False's in flag if maxiter is not None: k=k+1 if k>= maxiter: break for i in range(dim): # do each axis # make slices to shift view one element along the axis slcs1 = slcs[:] slcs2 = slcs[:] slcs1[i] = slice(0, -1) slcs2[i] = slice(1, None) # replace from the right repmask = logical_and(~flag[slcs1], flag[slcs2]) data[slcs1][repmask] = data[slcs2][repmask] flag[slcs1][repmask] = True # replace from the left repmask = logical_and(~flag[slcs2], flag[slcs1]) data[slcs2][repmask] = data[slcs1][repmask] flag[slcs2][repmask] = True return data else: import scipy import scipy.interpolate X, Y= meshgrid(r_[0:arr.shape[1]], r_[0:arr.shape[0]]) m=~isnan(arr) z=arr[m] x=X[m] y=Y[m] return scipy.interpolate.griddata((x,y), z, (X,Y), method=method); def ind2sub(shp, idx): """ind2sub(shp, idx) where shp is shape, and idx is index. returns np.unravel_index(idx,shap) """ return unravel_index(idx, shp) # ''' # DO NOT USE NOT CORRECT!!! # I1, I2, I3, ..., Idim = ind2sub(shape, idx) # Input: # shap - shape of the array # idx - list of indicies # Output: # list of subscripts # r = array([[0,1,0,0],[0,0,1,1],[1,1,1,0],[1,0,0,1]]) # l = find(r==1) # [cols,rows] = ind2sub(r.shape, l) # # The element in coulmn cols[i], and row rows[i] is 1. # ''' ## if len(shap) <= dim: ## shap = shap + tuple(zeros((dim - len(shap),))) ## else: ## shap = shap[0:dim-1] + (prod(shap[(dim-1):]),) # if not isinstance(idx, ndarray): # idx=array(idx) # # n = len(shap) # k = array([1] + cumprod(shap[0:(n-1)]).tolist()) # # argout = [zeros((len(idx),))]*n # # for i in xrange(n-1,-1,-1): # vi = (idx)%k[-i] # vj = (idx-vi)/k[-i] # argout[i] = vj # idx = vi # print argout # return argout def isarray(a): """ Test for arrayobjects. Can also handle UserArray instances http://mail.python.org/pipermail/matrix-sig/1998-March/002155.html """ try: sh = list(a.shape) except AttributeError: return 0 try: sh[0] = sh[0]+1 a.shape = sh except ValueError: return 1 except IndexError: return 1 # ? this is a scalar array return 0 def isint(x): #http://drj11.wordpress.com/2009/02/27/python-integer-int-float/ try: return int(x) == x except: return False def isfloat(f): if not operator.isNumberType(f): return 0 if f % 1: return 1 else: return 0 def peaks(n=49): xx=linspace(-3,3,n) yy=linspace(-3,3,n) [x,y] = meshgrid(xx,yy) z = 3*(1-x)**2*exp(-x**2 - (y+1)**2) \ - 10*(x/5 - x**3 - y**5)*exp(-x**2-y**2) \ - 1/3*exp(-(x+1)**2 - y**2) return z def maskshow(array, mask=None, **kwargs): ''' maskshow(array, mask=None) Ex: maskshow(kum.topoA[:,:,0], mask<0.5) ''' if array.ndim==4: # use imshow instead of matshow... maskedArray=array.copy(); if mask != None: mask=255*abs(double(mask)-1); maskedArray[:,:,3] =mask; return plt.imshow(maskedArray, **kwargs); elif array.ndim==3: # use imshow instead of matshow... maskedArray=zeros((array.shape[0], array.shape[1], 4), dtype=array.dtype) maskedArray[:,:,0:3]=array; if mask != None: mask=255*abs(double(mask)-1); maskedArray[:,:,3] =mask; plt.imshow(maskedArray, **kwargs); return maskedArray elif array.ndim==2: maskedArray=array.copy(); if mask is None: maskedArray[mask]=nan; return plt.matshow(maskedArray, **kwargs); def mdot(listIn): ''' out=mdot([A,B,C]) Simulates multiple dot operations. Ex: mdot([A,B,C]) is equal to dot(dot(A,B),C) ''' out=listIn[0]; for k in r_[1:len(listIn)]: out=dot(out, listIn[k]) return out; def nonan(A, rows=False): if rows: return A[isnan(A).sum(1)==0]; else: return A[~isnan(A)]; def nonaninf(A, rows=False): m=isnan(A) | isinf(A) if rows: return A[m.sum(1)==0]; else: return A[~m]; def progress(p=1,t=1,f="%.3f "): """progress(position=1,totalCount=1) display progress in ratio (position/totalCount) """ sys.stdout.write(f % (float(p)/t)) sys.stdout.flush() #print '%.3f' % (float(p)/t) def progressbar(percentage,interval=10.0,character='.'): """progressbar(percentage,interval=10, character='.') display progressbar based on the ratio... """ if percentage%interval == 0: sys.stdout.write(str(percentage)+" ") sys.stdout.flush() def progresstime(t0=0,timeSpan=30): ''' t1=progresstime(t0=0,timeSpan=30) Returns True time if currentTime-t0>timeSpan. Otherwise returns False. ''' t1=time.time() if t1-t0>timeSpan: return True; else: return False; def rescale(arr, lim, trim=False, arrlim=None, quiet=False): """rescale(array, limits, trim=False, arrlim=None, quiet=False) scale the values of the array to new limits ([min, max]) Trim: With this option set to a number, the limits are stretced between [mean-TRIM*stdev:mean+TRIM*stdev] arrlim: If given the limits are not calculated (useful if array has nan/inf values). quiet: If True, won't print the min/max values for the array. """ if arrlim is not None: minarr=arrlim[0]; maxarr=arrlim[1]; if trim: m=arr.mean() s=arr.std() minarr=m-trim*s; maxarr=m+trim*s; elif (trim==False) & (arrlim is None): minarr=arr.min() maxarr=arr.max() if not quiet: print([minarr, maxarr]) newarr=(arr-minarr)/(maxarr-minarr)*(lim[1]-lim[0])+lim[0] newarr[newarr<lim[0]]=lim[0] newarr[newarr>lim[1]]=lim[1] return newarr def shallIStop(t0,timeSpan=120): ''' t1=shallIStop(t0,timeSpan) returns time.time() if a long time (timeSpan) has passed since the given time (t0). Otherwise returns t0. ''' t1=time.time() if (t1-t0)>timeSpan: if confirm("Shall I stop?", resp=False): return 0 else: return t1 else: return t0 def sub2ind(shap, sub): """sub2ind(shap, sub): Changes a subscript to indices """ try: nSub=size(sub,1); for s in xrange(nSub): subs=sub[s,] for k in xrange(len(shap)): if subs[k]<0: raise exceptions.IndexError; # subscript can't be negative if (shap[k]-subs[k])<= 0: raise exceptions.IndexError; #subscript can't be bigger than shape except IndexError: for k in xrange(len(shap)): if sub[k]<0: raise exceptions.IndexError; # subscript can't be negative if (shap[k]-sub[k])<= 0: raise exceptions.IndexError; #subscript can't be bigger than shape shap=hstack([shap[1:], 1]); ind=dot(shap,sub); return ind def tic(): ''' returns current time as float ''' return time.time() def transect(x,y,z,x0,y0,x1,y1,plots=0): ''' (xi, yi, zi)=transect(x,y,z,x0,y0,x1,y1,plots=0) x: 2-D array of x coordinates y: 2-D array of y coordinates z: 2-D array of z values x0,y0,x1,y1: scalar coordinates plots=0: do not show plots outputs xi,yi,zi: vectors of x,y coordinates and z values along transect. ''' #convert coord to pixel coord d0=sqrt( (x-x0)**2+ (y-y0)**2 ); i0=d0.argmin(); x0,y0=unravel_index(i0,x.shape); #overwrite x0,y0 d1=plt.np.sqrt( (x-x1)**2+ (y-y1)**2 ); i1=d1.argmin(); x1,y1=unravel_index(i1,x.shape); #overwrite x1,y1 #-- Extract the line... # Make a line with "num" points... length = int(plt.np.hypot(x1-x0, y1-y0)) xi, yi = plt.np.linspace(x0, x1, length), plt.np.linspace(y0, y1, length) # Extract the values along the line #y is the first dimension and x is the second, row,col zi = z[xi.astype(plt.np.int), yi.astype(plt.np.int)] mz=nonaninf(z.ravel()).mean() sz=nonaninf(z.ravel()).std() if plots==1: plt.matshow(z);plt.clim([mz-2*sz,mz+2*sz]);plt.colorbar();plt.title('transect: (' + str(x0) + ',' + str(y0) + ') (' +str(x1) + ',' +str(y1) + ')' ); plt.scatter(yi,xi,5,c='r',edgecolors='none') plt.figure();plt.scatter(sqrt( (xi-xi[0])**2 + (yi-yi[0])**2 ) , zi) #plt.figure();plt.scatter(xi, zi) #plt.figure();plt.scatter(yi, zi) return (xi, yi, zi); def toc(t): ''' subtracts current time from given, and displays result ''' print(time.time()-t, "sec.") return def validIndex(arrSize, arrIdx): """validIndex(arrSize, arrIdx): Returns validIndex values given the array size. Ex: dims=array.shape a=0;r=1; idx=mgrid[a-1:a+2,r-1:r+2].reshape(2,9) vidx=basic.validIndex(dims, idx.T) """ if not isinstance(arrSize, ndarray): arrSize=array(arrSize) if not isinstance(arrIdx, ndarray): arrIdx=array(arrIdx) arrSize=arrSize-1 elements=r_[0:arrIdx.shape[0]] for k in elements: if any((arrSize-arrIdx[k])*arrIdx[k]<0): elements[k]=-1 return arrIdx[elements>-1,] def wrapToPi(x): return mod(x+pi,2*pi)-pi def wrapToInt(x, period): return mod(x+period,2*period)-period def writeToKml(filename, arr2d, NSEW, rotation=0.0, vmin=None, vmax=None, cmap=None, format=None, origin=None, dpi=72): """ writeToKml(filename, arr2d, NSEW, rotation=0.0, vmin=None, vmax=None, cmap=None, format=None, origin=None, dpi=None): NSEW=[north, south, east, west] """ import os #check if filename has extension base,ext=os.path.splitext(filename); if len(ext)==0: ext='.kml' kmlFile=base+ext; pngFile=base+'.png'; f=open(kmlFile,'w'); f.write('<kml xmlns="http://earth.google.com/kml/2.1">\n') f.write('<Document>\n') f.write('<GroundOverlay>\n') f.write(' <visibility>1</visibility>\n') f.write(' <LatLonBox>\n') f.write(' <north>%(#)3.4f</north>\n' % {"#":NSEW[0]}) f.write(' <south>%(#)3.4f</south>\n'% {"#":NSEW[1]}) f.write(' <east>%(#)3.4f</east>\n'% {"#":NSEW[2]}) f.write(' <west>%(#)3.4f</west>\n'% {"#":NSEW[3]}) f.write(' <rotation>%(#)3.4f</rotation>\n' % {"#":rotation}) f.write(' </LatLonBox>') f.write(' <Icon>') f.write(' <href>%(pngFile)s</href>' % {'pngFile':pngFile}) f.write(' </Icon>') f.write('</GroundOverlay>') f.write('</Document>') f.write('</kml>') f.close(); #Now write the image plt.imsave(pngFile, arr2d,vmin=vmin, vmax=vmax, cmap=cmap, format=format, origin=origin, dpi=dpi) def colorbarFigure(cmap,norm,label=""): '''colorbarFigure(cmap,norm,label="") basic.colorbarFigure(jet(),normalize(vmin=1,vmax=10),'Label') ''' fig = plt.figure(figsize=(1,5)) ax1 = fig.add_axes([0.05, 0.1, 0.05, 0.8]) cb1 = plt.matplotlib.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm, orientation='vertical') cb1.set_label(label) return fig def findAndReplace(fileList,searchExp,replaceExp): """findAndReplace(fileList,searchExp,replaceExp): """ import fileinput if not isinstance(fileList, list): fileList=[fileList]; for line in fileinput.input(fileList, inplace=1): if searchExp in line: line = line.replace(searchExp,replaceExp) sys.stdout.write(line) def gridSearch2(fun, bounds,tol=1., goal='min'): """gridSearch(fun, args, bounds): #for now bounds are actual sampling points (i.e. r_[xmin:xmax]) Divide and conquer brute-force grid search """ #Main idea is that we want to do only a handful of operations at each time on the multidimensional grid. #Slowly zone in on the lowest score. if goal == 'min': fun_z=+inf goalfun= lambda x: x<fun_z elif goal == 'max': fun_z=-inf goalfun= lambda x: x>fun_z else: fun_z=0 goalfun=goal #select spacing s=[int(round((max(b)-min(b)))/10.) for b in bounds ] for k in xrange(len(s)): if s[k]<1: s[k]=1 print(s) #create solution lists x=[] y=[] z=[] #start infinite loop breakLoop=False b0=[] while True: #create sampling grid for k in xrange(len(bounds)): b0.append(bounds[k][::s[k]]) X,Y=meshgrid(b0[0], b0[1]) print(X) print(Y) for xy in zip(X.ravel(),Y.ravel()): #if (xy[0] in x) and (xy[1] in y): if any( (x==xy[0]) & (y==xy[1]) ): print([xy[0], xy[1], 0]) #raise NameError("LogicError") continue x.append(xy[0]) y.append(xy[1]) z.append(fun(xy)) if goalfun(z[-1]): # z[-1] < fun_z: fun_z=z[-1] x0=x[-1]; y0=y[-1]; print([99999, x[-1], y[-1], z[-1]]) else: print([x[-1], y[-1], z[-1]]) #Z=griddata(x,y,z,X,Y); # The first one is actually not necessary #set new bounds #xy0=Z.argmin() #set new bounds if breakLoop: print("Reached lowest grid resolution.") break if all([sk==1 for sk in s]): #Do one more loop then break. print("BreakLoop is ON") breakLoop=True if all(bounds[0]==r_[x0-s[0]:x0+s[0]]) and all(bounds[1]==r_[y0-s[1]:y0+s[1]] ): print("Breaking to avoid infinite loop.") break#if the bounds are the same then break bounds[0]=r_[x0-s[0]:x0+s[0]] bounds[1]=r_[y0-s[1]:y0+s[1]] b0=[] #re-initialize #select spacing #s=[max(1,int(round(sk/5.))) for sk in s] s=[int(round((max(b)-min(b)))/10.) for b in bounds ] for k in xrange(len(s)): if s[k]<1: s[k]=1 return [x0,y0,x,y,z] def moving_window(arr, window_size=[3,3], func=mean): """moving_window(array, window_size, func=mean) """ import scipy return scipy.ndimage.filters.generic_filter(arr, func, size=window_size) def reload_package(root_module): """reload_package(module) Reloads module and loaded sub-modules. It clears sys.modules for the given module. http://stackoverflow.com/questions/2918898/prevent-python-from-caching-the-imported-modules """ import types package_name = root_module.__name__ # get a reference to each loaded module loaded_package_modules = dict([ (key, value) for key, value in sys.modules.items() if key.startswith(package_name) and isinstance(value, types.ModuleType)]) # delete references to these loaded modules from sys.modules for key in loaded_package_modules: del sys.modules[key] # load each of the modules again; # make old modules share state with new modules for key in loaded_package_modules: print('loading %s' % key) newmodule = __import__(key) oldmodule = loaded_package_modules[key] oldmodule.__dict__.clear() oldmodule.__dict__.update(newmodule.__dict__) def clear_sys_module(root_module): """clear_sys_module(module) Deletes module and loaded sub-modules from sys.modules for the given module. http://stackoverflow.com/questions/2918898/prevent-python-from-caching-the-imported-modules """ import types package_name = root_module.__name__ # get a reference to each loaded module loaded_package_modules = dict([ (key, value) for key, value in sys.modules.items() if key.startswith(package_name) and isinstance(value, types.ModuleType)]) # delete references to these loaded modules from sys.modules for key in loaded_package_modules: del sys.modules[key] def numel(x): import numpy as np if isinstance(x, np.int): return 1 elif isinstance(x, np.double): return 1 elif isinstance(x, np.float): return 1 elif isinstance(x, list) or isinstance(x, tuple): return len(x) elif isinstance(x, np.ndarray): return x.size else: print('Unknown type {}.'.format(type(x))) return None def resize(a, new_shape, stretch=True,method='linear'): """ Returns a in the new_shape. stretch=True: Interpolate as necessary. If false, use numpy.resize method= 'linear' ==> interp2d kind. %Only supports 2D at the moment """ import numpy as np if stretch==False: return np.resize(a, new_shape) import scipy import scipy.interpolate #import scipy.misc #return scipy.misc.imresize(a,new_shape) #X, Y= meshgrid(x, y) x=arange(a.shape[0]) y=arange(a.shape[1]) interpFun=scipy.interpolate.interp2d(x,y,a ) X=arange(new_shape[0])*a.shape[0]/new_shape[0] Y=arange(new_shape[1])*a.shape[1]/new_shape[1] return interpFun(X,Y) def rmse(predictions, targets): """rmse(predictions, targets) """ return sqrt(mean((predictions-targets)**2.)) def r_squared(predictions, targets, ignore_nan=True): """r_squared(predictions, targets) """ import scipy scipy.pkgload('stats') if ignore_nan: m=bitwise_not(bitwise_or(isnan(predictions), isnan(targets))) print("{} nan elements masked.".format(m.sum())) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(predictions[m], targets[m]) else: slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(predictions, targets) return r_value**2.
gpl-2.0
rishibarve/incubator-airflow
tests/contrib/hooks/test_bigquery_hook.py
4
7872
# -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import unittest from airflow.contrib.hooks import bigquery_hook as hook from oauth2client.contrib.gce import HttpAccessTokenRefreshError bq_available = True try: hook.BigQueryHook().get_service() except HttpAccessTokenRefreshError: bq_available = False class TestBigQueryDataframeResults(unittest.TestCase): def setUp(self): self.instance = hook.BigQueryHook() @unittest.skipIf(not bq_available, 'BQ is not available to run tests') def test_output_is_dataframe_with_valid_query(self): import pandas as pd df = self.instance.get_pandas_df('select 1') self.assertIsInstance(df, pd.DataFrame) @unittest.skipIf(not bq_available, 'BQ is not available to run tests') def test_throws_exception_with_invalid_query(self): with self.assertRaises(Exception) as context: self.instance.get_pandas_df('from `1`') self.assertIn('pandas_gbq.gbq.GenericGBQException: Reason: invalidQuery', str(context.exception), "") @unittest.skipIf(not bq_available, 'BQ is not available to run tests') def test_suceeds_with_explicit_legacy_query(self): df = self.instance.get_pandas_df('select 1', dialect='legacy') self.assertEqual(df.iloc(0)[0][0], 1) @unittest.skipIf(not bq_available, 'BQ is not available to run tests') def test_suceeds_with_explicit_std_query(self): df = self.instance.get_pandas_df('select * except(b) from (select 1 a, 2 b)', dialect='standard') self.assertEqual(df.iloc(0)[0][0], 1) @unittest.skipIf(not bq_available, 'BQ is not available to run tests') def test_throws_exception_with_incompatible_syntax(self): with self.assertRaises(Exception) as context: self.instance.get_pandas_df('select * except(b) from (select 1 a, 2 b)', dialect='legacy') self.assertIn('pandas_gbq.gbq.GenericGBQException: Reason: invalidQuery', str(context.exception), "") class TestBigQueryTableSplitter(unittest.TestCase): def test_internal_need_default_project(self): with self.assertRaises(Exception) as context: hook._split_tablename('dataset.table', None) self.assertIn('INTERNAL: No default project is specified', str(context.exception), "") def test_split_dataset_table(self): project, dataset, table = hook._split_tablename('dataset.table', 'project') self.assertEqual("project", project) self.assertEqual("dataset", dataset) self.assertEqual("table", table) def test_split_project_dataset_table(self): project, dataset, table = hook._split_tablename('alternative:dataset.table', 'project') self.assertEqual("alternative", project) self.assertEqual("dataset", dataset) self.assertEqual("table", table) def test_sql_split_project_dataset_table(self): project, dataset, table = hook._split_tablename('alternative.dataset.table', 'project') self.assertEqual("alternative", project) self.assertEqual("dataset", dataset) self.assertEqual("table", table) def test_invalid_syntax_column_double_project(self): with self.assertRaises(Exception) as context: hook._split_tablename('alt1:alt.dataset.table', 'project') self.assertIn('Use either : or . to specify project', str(context.exception), "") self.assertFalse('Format exception for' in str(context.exception)) def test_invalid_syntax_double_column(self): with self.assertRaises(Exception) as context: hook._split_tablename('alt1:alt:dataset.table', 'project') self.assertIn('Expect format of (<project:)<dataset>.<table>', str(context.exception), "") self.assertFalse('Format exception for' in str(context.exception)) def test_invalid_syntax_tiple_dot(self): with self.assertRaises(Exception) as context: hook._split_tablename('alt1.alt.dataset.table', 'project') self.assertIn('Expect format of (<project.|<project:)<dataset>.<table>', str(context.exception), "") self.assertFalse('Format exception for' in str(context.exception)) def test_invalid_syntax_column_double_project_var(self): with self.assertRaises(Exception) as context: hook._split_tablename('alt1:alt.dataset.table', 'project', 'var_x') self.assertIn('Use either : or . to specify project', str(context.exception), "") self.assertIn('Format exception for var_x:', str(context.exception), "") def test_invalid_syntax_double_column_var(self): with self.assertRaises(Exception) as context: hook._split_tablename('alt1:alt:dataset.table', 'project', 'var_x') self.assertIn('Expect format of (<project:)<dataset>.<table>', str(context.exception), "") self.assertIn('Format exception for var_x:', str(context.exception), "") def test_invalid_syntax_tiple_dot_var(self): with self.assertRaises(Exception) as context: hook._split_tablename('alt1.alt.dataset.table', 'project', 'var_x') self.assertIn('Expect format of (<project.|<project:)<dataset>.<table>', str(context.exception), "") self.assertIn('Format exception for var_x:', str(context.exception), "") class TestBigQueryHookSourceFormat(unittest.TestCase): def test_invalid_source_format(self): with self.assertRaises(Exception) as context: hook.BigQueryBaseCursor("test", "test").run_load("test.test", "test_schema.json", ["test_data.json"], source_format="json") # since we passed 'json' in, and it's not valid, make sure it's present in the error string. self.assertIn("JSON", str(context.exception)) class TestBigQueryBaseCursor(unittest.TestCase): def test_invalid_schema_update_options(self): with self.assertRaises(Exception) as context: hook.BigQueryBaseCursor("test", "test").run_load( "test.test", "test_schema.json", ["test_data.json"], schema_update_options=["THIS IS NOT VALID"] ) self.assertIn("THIS IS NOT VALID", str(context.exception)) def test_invalid_schema_update_and_write_disposition(self): with self.assertRaises(Exception) as context: hook.BigQueryBaseCursor("test", "test").run_load( "test.test", "test_schema.json", ["test_data.json"], schema_update_options=['ALLOW_FIELD_ADDITION'], write_disposition='WRITE_EMPTY' ) self.assertIn("schema_update_options is only", str(context.exception)) if __name__ == '__main__': unittest.main()
apache-2.0
kcavagnolo/astroML
book_figures/chapter5/fig_posterior_binomial.py
3
2399
""" Binomial Posterior ------------------ Figure 5.9 The solid line in the left panel shows the posterior pdf p(b|k, N) described by eq. 5.71, for k = 4 and N = 10. The dashed line shows a Gaussian approximation described in Section 3.3.3. The right panel shows the corresponding cumulative distributions. A value of 0.1 is marginally likely according to the Gaussian approximation (p_approx(< 0.1) ~ 0.03) but strongly rejected by the true distribution (p_true(< 0.1) ~ 0.003). """ # Author: Jake VanderPlas # License: BSD # The figure produced by this code is published in the textbook # "Statistics, Data Mining, and Machine Learning in Astronomy" (2013) # For more information, see http://astroML.github.com # To report a bug or issue, use the following forum: # https://groups.google.com/forum/#!forum/astroml-general import numpy as np from scipy.stats import norm, binom from matplotlib import pyplot as plt #---------------------------------------------------------------------- # This function adjusts matplotlib settings for a uniform feel in the textbook. # Note that with usetex=True, fonts are rendered with LaTeX. This may # result in an error if LaTeX is not installed on your system. In that case, # you can set usetex to False. from astroML.plotting import setup_text_plots setup_text_plots(fontsize=8, usetex=True) #------------------------------------------------------------ # Plot posterior as a function of b n = 10 # number of points k = 4 # number of successes from n draws b = np.linspace(0, 1, 100) db = b[1] - b[0] # compute the probability p(b) (eqn. 5.70) p_b = b ** k * (1 - b) ** (n - k) p_b /= p_b.sum() p_b /= db cuml_p_b = p_b.cumsum() cuml_p_b /= cuml_p_b[-1] # compute the gaussian approximation (eqn. 5.71) p_g = norm(k * 1. / n, 0.16).pdf(b) cuml_p_g = p_g.cumsum() cuml_p_g /= cuml_p_g[-1] #------------------------------------------------------------ # Plot the results fig = plt.figure(figsize=(5, 2.5)) fig.subplots_adjust(left=0.11, right=0.95, wspace=0.35, bottom=0.18) ax = fig.add_subplot(121) ax.plot(b, p_b, '-b') ax.plot(b, p_g, '--r') ax.set_ylim(-0.05, 3) ax.set_xlabel('$b$') ax.set_ylabel('$p(b|x,I)$') ax = fig.add_subplot(122, yscale='log') ax.plot(b, cuml_p_b, '-b') ax.plot(b, cuml_p_g, '--r') ax.plot([0.1, 0.1], [1E-6, 2], ':k') ax.set_xlabel('$b$') ax.set_ylabel('$P(<b|x,I)$') ax.set_ylim(1E-6, 2) plt.show()
bsd-2-clause
bmazin/ARCONS-pipeline
util/test/TestHg.py
1
2125
import unittest import numpy as np import time import matplotlib.pyplot as plt from util import hgPlot import inspect class TestHg(unittest.TestCase): """ Test options for filling and plotting histograms """ def testCompare(self): """ Demonstrate that np.bincounts is at least 10 times faster than np.histogram """ nPixels = 100 values = [] for i in range(nPixels): values.append(np.random.random_integers(0,1000000, 100)) values[i].sort() # measure time for np.histogram begin = time.time() for i in range(nPixels): hg = np.histogram(values[i], 1000000, range=(0,1000000)) # 3.8 sec end = time.time() deltaHg = end - begin # measure time for np.bincount begin = time.time() for i in range(nPixels): hg = np.bincount(values[i],minlength=1000000) # 0.097 sec/100 end = time.time() deltaBc = end - begin if deltaBc*10 > deltaHg: print "np.histogram elapsed time is",deltaHg print "np.bincount elapsed time is",deltaBc self.assertTrue(deltaBc*10 < deltaHg) def testHgPlot1(self): xmax = 5 hg = np.histogram([0,1,1,1,1,1,2,2],bins=xmax, range=(-0.5,xmax-0.5)) x,y = hgPlot.getPlotValues(hg, ylog=False) plt.clf() plt.plot(x,y) plt.margins(0.1, 0.1) tfn = inspect.stack()[0][3] plt.savefig(tfn) def testHgPlot2(self): hg = [0,0,1,8,4,0] x,y = hgPlot.getPlotValues(hg, ylog=False) plt.clf() plt.plot(x,y) plt.margins(0.1, 0.1) tfn = inspect.stack()[0][3] plt.savefig(tfn) def testHgPlot3(self): hg = [10000,0.1,0,1] x,y = hgPlot.getPlotValues(hg, ylog=False) #for i in range(len(x)): # print i, x[i],y[i] plt.clf() plt.plot(x,y) plt.margins(0.1, 0.1) plt.yscale('log') tfn = inspect.stack()[0][3] plt.savefig(tfn) if __name__ == '__main__': unittest.main()
gpl-2.0
ElDeveloper/scikit-learn
examples/classification/plot_digits_classification.py
289
2397
""" ================================ Recognizing hand-written digits ================================ An example showing how the scikit-learn can be used to recognize images of hand-written digits. This example is commented in the :ref:`tutorial section of the user manual <introduction>`. """ print(__doc__) # Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org> # License: BSD 3 clause # Standard scientific Python imports import matplotlib.pyplot as plt # Import datasets, classifiers and performance metrics from sklearn import datasets, svm, metrics # The digits dataset digits = datasets.load_digits() # The data that we are interested in is made of 8x8 images of digits, let's # have a look at the first 3 images, stored in the `images` attribute of the # dataset. If we were working from image files, we could load them using # pylab.imread. Note that each image must have the same size. For these # images, we know which digit they represent: it is given in the 'target' of # the dataset. images_and_labels = list(zip(digits.images, digits.target)) for index, (image, label) in enumerate(images_and_labels[:4]): plt.subplot(2, 4, index + 1) plt.axis('off') plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.title('Training: %i' % label) # To apply a classifier on this data, we need to flatten the image, to # turn the data in a (samples, feature) matrix: n_samples = len(digits.images) data = digits.images.reshape((n_samples, -1)) # Create a classifier: a support vector classifier classifier = svm.SVC(gamma=0.001) # We learn the digits on the first half of the digits classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2]) # Now predict the value of the digit on the second half: expected = digits.target[n_samples / 2:] predicted = classifier.predict(data[n_samples / 2:]) print("Classification report for classifier %s:\n%s\n" % (classifier, metrics.classification_report(expected, predicted))) print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted)) images_and_predictions = list(zip(digits.images[n_samples / 2:], predicted)) for index, (image, prediction) in enumerate(images_and_predictions[:4]): plt.subplot(2, 4, index + 5) plt.axis('off') plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.title('Prediction: %i' % prediction) plt.show()
bsd-3-clause
rbrecheisen/arff-utils
arff_utils/arff_utils.py
1
17913
# -*- coding: utf-8 -*- __author__ = 'Ralph' import arff import numpy as np import pandas as pd class ARFF(object): @staticmethod def read(file_name, missing=None): """ Loads ARFF file into data dictionary. Missing values indicated by '?' are automatically converted to None. If you want some other value to be treated as missing, specify them in the missing parameter. :param file_name: File name :param missing: List of missing value representations :return: Data dictionary """ data = arff.load(open(file_name)) if missing is not None: for i in range(len(data['data'])): for j in range(len(data['data'][i])): if type(missing) is str: if data['data'][i][j] == missing: data['data'][i][j] = None elif type(missing) is list: for m in missing: if data['data'][i][j] == m: data['data'][i][j] = None else: raise RuntimeError('Invalid type for \'missing\' parameter ' + str(type(missing))) return data @staticmethod def read_from_csv(file_name): """ Loads CSV file and converts it to an ARFF data dictionary. This function assumes the following: (1) First line contains a header with column names (2) First column contains IDs (interpreted as string values) (3) Remaining columns contain numeric values :param file_name: CSV file path """ attributes = [] f = open(file_name, 'r') header = f.readline().strip().split(',') header = [item.strip() for item in header] attributes.append((header[0], 'STRING')) for item in header[1:]: attributes.append((item, 'NUMERIC')) data = [] for line in f.readlines(): line = line.strip() if line.startswith('#') or line == '': continue parts = line.split(',') parts = [part.strip() for part in parts] parts[0] = str(parts[0]) parts[1:] = [float(part) for part in parts[1:]] data.append(parts) f.close() return { 'relation': 'unknown', 'attributes': attributes, 'data': data, 'description': '' } @staticmethod def to_data_frame(data, index_col=None): """ Converts ARFF data dictionary to Pandas data frame. :param data: Data dictionary :param index_col: Column name to use as index :return: Data frame """ # Create data frame by by taking rows and attributes from # ARFF data. Data types should be automatically inferred rows = data['data'] columns = [attribute[0] for attribute in data['attributes']] # Get categorical-type columns categoricals = [] for attribute in data['attributes']: column = attribute[0] if type(attribute[1]) is list: categoricals.append(column) # Create data frame from ARFF dictionary data_frame = pd.DataFrame(rows, columns=columns) for categorical in categoricals: data_frame[categorical] = data_frame[categorical].astype('category') # If index column specified, set it if index_col is not None: if index_col not in data_frame.columns: raise RuntimeError('Index column ' + index_col + ' not found') data_frame.set_index(index_col, drop=True, inplace=True, verify_integrity=True) return data_frame @staticmethod def from_data_frame(relation, attributes, data_frame, description=''): """ Converts Pandas data frame to ARFF dictionary. This is only possible if the data frame was previously converted from ARFF data because we need the specific attribute information. :param relation: Relation :param attributes: ARFF attributes :param data_frame: Data frame :param description: Optional description :return: ARFF data dictionary """ data = [] for row in data_frame.to_records(index=False): data.append(list(row)) return { 'relation': relation, 'attributes': attributes, 'data': data, 'description': description } @staticmethod def from_data_frame(relation, data_frame): """ Converts Pandas data frame to ARFF dictionary. Attribute types are automatically inferred. :param relation: Relation name :param data_frame: Data frame :return: ARFF data dictionary """ attributes = [] for name in data_frame.columns: column = data_frame[name] if column.dtype is np.dtype('int') or column.dtype is np.dtype('float'): attributes.append((name, 'NUMERIC')) elif column.dtype is np.dtype('object'): attributes.append((name, 'STRING')) elif column.dtype.name is 'category': attributes.append((name, list(column.cat.categories))) data = [] for row in data_frame.to_records(index=False): data.append(list(row)) return { 'relation': relation, 'attributes': attributes, 'data': data, 'description': 'Converted from Pandas data frame' } @staticmethod def test(): data = ARFF.to_data_frame( ARFF.read('/Users/Ralph/datasets/pyminer/features.arff')) ARFF.from_data_frame(data) @staticmethod def write(file_name, data): """ Writes ARFF data dictionary to file. :param file_name: File name :param data: Data dictionary :return: """ f = open(file_name, 'w') arff.dump(data, f) f.close() @staticmethod def write_csv(file_name, data): """ Writes ARFF data dictionary to CSV file. Note that this will cause loss of attribute type information. The approach we take here is to first convert to a Pandas data frame and then use the Pandas built-in function to export to CSV. :param file_name: CSV file name :param data: Data dictionary :return: """ data_frame = ARFF.to_data_frame(data) data_frame.to_csv(file_name, na_rep='?', header=True, index=False, sep=',') @staticmethod def append(data1, data2): """ Appends contents of ARFF data dictionary 'data2' to the contents of data dictionary 'data1'. Obviously, the attributes and types must correspond exactly. :param data1: Base data dictionary. :param data2: Dictionary to append :return: Updated dictionary """ # Use description of data1 description = data1['description'] # Check whether we have matching attributes attributes1 = data1['attributes'] attributes2 = data2['attributes'] if not len(attributes1) == len(attributes2): raise RuntimeError('Mismatch number of attributes') for i in range(len(attributes1)): attribute1 = attributes1[i] attribute2 = attributes2[i] if not len(attribute1) == 2: raise RuntimeError('Number of attribute1 items != 2') if not len(attribute2) == 2: raise RuntimeError('Number of attribute2 items != 2') if not unicode(attribute1[0]) == unicode(attribute2[0]): raise RuntimeError('Mismatching names at ' + str(i) + ' (' + attribute1[0] + ' vs ' + attribute2[0] + ')') if type(attribute1[1]) is list and type(attribute2[1]) is list: for j in range(len(attribute1[1])): if not unicode(attribute1[1][j]) == unicode(attribute2[1][j]): raise RuntimeError('Mismatching nominal values at (' + str(i) + ',' + str(j) + ') (' + unicode(attribute1[1][j]) + ' vs ' + unicode(attribute2[1][j]) + ')') elif not unicode(attribute1[1]) == unicode(attribute2[1]): raise RuntimeError('Mismatching attribute types (' + unicode(attribute1[1]) + ' vs ' + unicode(attribute2[1]) + ')') # Append rows of data2 to rows of data1 data = [] data.extend(data1['data']) data.extend(data2['data']) return { 'relation': relation1, 'attributes': attributes1, 'data': data, 'description': description } @staticmethod def merge(data1, data2, join_by, attributes): """ Merges two data sets by appending the columns of data2 associated with given attributes to data1. Rows are matched based on the join_by attribute. :param data1: Original data set :param data2: Data set whose columns to add :param join_by: Attribute for matching data rows :param attributes: Attributes to add :return: New data set """ # Check that both data sets have the merge attribute otherwise we # can never match rows from one with rows from the other if not ARFF.contains(data1, join_by): raise RuntimeError('Attribute ' + join_by + ' missing from data1') if not ARFF.contains(data2, join_by): raise RuntimeError('Attribute ' + join_by + ' missing from data2') # Check that data2 has the given attributes for attribute in attributes: if not ARFF.contains(data2, attribute): raise RuntimeError('Attribute ' + attribute + ' missing from data2') # Check that data1 does not have the given attributes for attribute in attributes: if ARFF.contains(data1, attribute): raise RuntimeError('Attribute ' + attribute + ' already exists in data1') # Get index of join_by attribute in both data sets. Then, create a # lookup table for data2 based on join_by attribute as key. This # allows quick access to data rows of data2. If we iterate through # the rows of data1, we can get the join_by attribute value using # the join_idx1 index. Using the attribute value we can then lookup # the corresponding data row in data2. join_idx1 = ARFF.index_of(data1, join_by) join_idx2 = ARFF.index_of(data2, join_by) data2_lookup = {} for data_row1 in data2['data']: data2_lookup[data_row1[join_idx2]] = data_row1 # Get indexes associated with given attributes in data2. We need this # to efficiently access specific values in the rows of data2 attribute_indexes = [] for attribute in attributes: attribute_indexes.append(ARFF.index_of(data2, attribute)) # Create new attribute set by appending the attributes of # data set data2. We already checked there are no duplicates. attributes_extended = data1['attributes'] for i in attribute_indexes: attribute = data2['attributes'][i] attributes_extended.append(attribute) # Create new data rows by taking the original data row and # appending the values corresponding to the attribute columns from # data2. We can do this efficiently because of the lookup table we # created earlier. data = [] for i in range(len(data1['data'])): data_row = data1['data'][i] key = data_row[join_idx1] if key not in data2_lookup: print('WARNING: row with id {} not present in data2'.format(key)) continue data_row2 = data2_lookup[data_row[join_idx1]] for j in attribute_indexes: data_row.append(data_row2[j]) data.append(data_row) return { 'relation': data1['relation'], 'attributes': attributes_extended, 'data': data, 'description': '' } @staticmethod def dummy_encode(data, attribute): """ Applies a 1-of-k dummy encoding to the given attribute and replaces the associated column with two or more dummy columns. Note that if there are only two levels, they are just converted to zero and one instead of creating new columns for them. :param data: ARFF data dictionary :param attribute: Nominal attribute :return: Dummy encoded data dictionary, new attributes """ # Check that the attribute is actually nominal. If not, just # return the data unchanged if not ARFF.is_nominal(data, attribute): return data # Get index of given attribute. We need it when we insert # additional dummy columns. idx = ARFF.index_of(data, attribute) # Get attribute values attr_values = data['attributes'][idx][1] if len(attr_values) == 2: # If we're dealing with a binominal attribute there's no need # to split it up in separate dummy columns. Just convert the # values to 0's and 1's. data['attributes'][idx] = (attribute, 'NUMERIC') data_rows = data['data'] for i in range(len(data_rows)): value = data_rows[i][idx] if value == attr_values[0]: data_rows[i][idx] = 0 else: data_rows[i][idx] = 1 return data, [attribute] else: # Next, delete the original attribute and insert new attributes # for each attribute value we encounter del data['attributes'][idx] for attr_value in reversed(attr_values): data['attributes'].insert(idx, (attr_value, 'NUMERIC')) # Insert dummy values into each data row depending on its # original value in the attribute column data_rows = data['data'] for i in range(len(data_rows)): value = data_rows[i][idx] first = True for attr_value in reversed(attr_values): if first: data_rows[i][idx] = 0 first = False else: data_rows[i].insert(idx, 0) if value == attr_value: data_rows[i][idx] = 1 return data, attr_values @staticmethod def contains(data, attribute): """ Checks whether given attribute is in data dictionary. :param data: Data dictionary :param attribute: Attribute to check :return: True/False """ return ARFF.index_of(data, attribute) > -1 @staticmethod def index_of(data, attribute): """ Returns index of given attribute or -1 if not found. :param data: Data dictionary :param attribute: Attribute to search :return: Index or -1 """ for i in range(len(data['attributes'])): item = data['attributes'][i][0] if item == attribute: return i return -1 @staticmethod def type_of(data, attribute): """ Returns type of given attribute or None if attribute is of nominal type. In that case, use labels_of() :param data: Data dictionary :param attribute: Attribute to return type of :return: Attribute type """ i = ARFF.index_of(data, attribute) if i < 0: return None attribute_value = data['attributes'][i][1] if isinstance(attribute_value, list): print('WARNING: attribute value is nominal') return None else: return attribute_value @staticmethod def labels_of(data, attribute): """ Returns labels of given nominal attribute or None if attribute is not of nominal type. :param data: Data dictionary :param attribute: Attribute to return labels of :return: Labels """ i = ARFF.index_of(data, attribute) if i < 0: return None attribute_values = data['attributes'][i][1] if not isinstance(attribute_values, list): print('WARNING: attribute value is not of type nominal') return None else: return attribute_values @staticmethod def sort_by(data, attribute): """ Sorts data by given attribute. :param data: ARFF data dictionary :param attribute: Attribute to sort by :return: Sorted dictionary """ i = ARFF.index_of(data, attribute) if i < 0: raise RuntimeError('Attribute not found') data['data'].sort(key=lambda tup: tup[i]) return data @staticmethod def is_nominal(data, attribute): """ Checks whether given attribute name corresponds to nominal attribute or not. :param data: ARFF data dictionary :param attribute: Attribute to check """ i = ARFF.index_of(data, attribute) if i < 0: raise RuntimeError('Attribute not found') attribute_value = data['attributes'][i][1] if type(attribute_value) is list: return True return False if __name__ == '__main__': ARFF.test()
apache-2.0
nmartensen/pandas
pandas/tests/io/json/test_pandas.py
11
44634
# -*- coding: utf-8 -*- # pylint: disable-msg=W0612,E1101 import pytest from pandas.compat import (range, lrange, StringIO, OrderedDict, is_platform_32bit) import os import numpy as np from pandas import (Series, DataFrame, DatetimeIndex, Timestamp, read_json, compat) from datetime import timedelta import pandas as pd from pandas.util.testing import (assert_almost_equal, assert_frame_equal, assert_series_equal, network, ensure_clean, assert_index_equal) import pandas.util.testing as tm _seriesd = tm.getSeriesData() _tsd = tm.getTimeSeriesData() _frame = DataFrame(_seriesd) _frame2 = DataFrame(_seriesd, columns=['D', 'C', 'B', 'A']) _intframe = DataFrame(dict((k, v.astype(np.int64)) for k, v in compat.iteritems(_seriesd))) _tsframe = DataFrame(_tsd) _cat_frame = _frame.copy() cat = ['bah'] * 5 + ['bar'] * 5 + ['baz'] * \ 5 + ['foo'] * (len(_cat_frame) - 15) _cat_frame.index = pd.CategoricalIndex(cat, name='E') _cat_frame['E'] = list(reversed(cat)) _cat_frame['sort'] = np.arange(len(_cat_frame), dtype='int64') _mixed_frame = _frame.copy() class TestPandasContainer(object): def setup_method(self, method): self.dirpath = tm.get_data_path() self.ts = tm.makeTimeSeries() self.ts.name = 'ts' self.series = tm.makeStringSeries() self.series.name = 'series' self.objSeries = tm.makeObjectSeries() self.objSeries.name = 'objects' self.empty_series = Series([], index=[]) self.empty_frame = DataFrame({}) self.frame = _frame.copy() self.frame2 = _frame2.copy() self.intframe = _intframe.copy() self.tsframe = _tsframe.copy() self.mixed_frame = _mixed_frame.copy() self.categorical = _cat_frame.copy() def teardown_method(self, method): del self.dirpath del self.ts del self.series del self.objSeries del self.empty_series del self.empty_frame del self.frame del self.frame2 del self.intframe del self.tsframe del self.mixed_frame def test_frame_double_encoded_labels(self): df = DataFrame([['a', 'b'], ['c', 'd']], index=['index " 1', 'index / 2'], columns=['a \\ b', 'y / z']) assert_frame_equal(df, read_json(df.to_json(orient='split'), orient='split')) assert_frame_equal(df, read_json(df.to_json(orient='columns'), orient='columns')) assert_frame_equal(df, read_json(df.to_json(orient='index'), orient='index')) df_unser = read_json(df.to_json(orient='records'), orient='records') assert_index_equal(df.columns, df_unser.columns) tm.assert_numpy_array_equal(df.values, df_unser.values) def test_frame_non_unique_index(self): df = DataFrame([['a', 'b'], ['c', 'd']], index=[1, 1], columns=['x', 'y']) pytest.raises(ValueError, df.to_json, orient='index') pytest.raises(ValueError, df.to_json, orient='columns') assert_frame_equal(df, read_json(df.to_json(orient='split'), orient='split')) unser = read_json(df.to_json(orient='records'), orient='records') tm.assert_index_equal(df.columns, unser.columns) tm.assert_almost_equal(df.values, unser.values) unser = read_json(df.to_json(orient='values'), orient='values') tm.assert_numpy_array_equal(df.values, unser.values) def test_frame_non_unique_columns(self): df = DataFrame([['a', 'b'], ['c', 'd']], index=[1, 2], columns=['x', 'x']) pytest.raises(ValueError, df.to_json, orient='index') pytest.raises(ValueError, df.to_json, orient='columns') pytest.raises(ValueError, df.to_json, orient='records') assert_frame_equal(df, read_json(df.to_json(orient='split'), orient='split', dtype=False)) unser = read_json(df.to_json(orient='values'), orient='values') tm.assert_numpy_array_equal(df.values, unser.values) # GH4377; duplicate columns not processing correctly df = DataFrame([['a', 'b'], ['c', 'd']], index=[ 1, 2], columns=['x', 'y']) result = read_json(df.to_json(orient='split'), orient='split') assert_frame_equal(result, df) def _check(df): result = read_json(df.to_json(orient='split'), orient='split', convert_dates=['x']) assert_frame_equal(result, df) for o in [[['a', 'b'], ['c', 'd']], [[1.5, 2.5], [3.5, 4.5]], [[1, 2.5], [3, 4.5]], [[Timestamp('20130101'), 3.5], [Timestamp('20130102'), 4.5]]]: _check(DataFrame(o, index=[1, 2], columns=['x', 'x'])) def test_frame_from_json_to_json(self): def _check_orient(df, orient, dtype=None, numpy=False, convert_axes=True, check_dtype=True, raise_ok=None, sort=None, check_index_type=True, check_column_type=True, check_numpy_dtype=False): if sort is not None: df = df.sort_values(sort) else: df = df.sort_index() # if we are not unique, then check that we are raising ValueError # for the appropriate orients if not df.index.is_unique and orient in ['index', 'columns']: pytest.raises( ValueError, lambda: df.to_json(orient=orient)) return if (not df.columns.is_unique and orient in ['index', 'columns', 'records']): pytest.raises( ValueError, lambda: df.to_json(orient=orient)) return dfjson = df.to_json(orient=orient) try: unser = read_json(dfjson, orient=orient, dtype=dtype, numpy=numpy, convert_axes=convert_axes) except Exception as detail: if raise_ok is not None: if isinstance(detail, raise_ok): return raise if sort is not None and sort in unser.columns: unser = unser.sort_values(sort) else: unser = unser.sort_index() if dtype is False: check_dtype = False if not convert_axes and df.index.dtype.type == np.datetime64: unser.index = DatetimeIndex( unser.index.values.astype('i8') * 1e6) if orient == "records": # index is not captured in this orientation tm.assert_almost_equal(df.values, unser.values, check_dtype=check_numpy_dtype) tm.assert_index_equal(df.columns, unser.columns, exact=check_column_type) elif orient == "values": # index and cols are not captured in this orientation if numpy is True and df.shape == (0, 0): assert unser.shape[0] == 0 else: tm.assert_almost_equal(df.values, unser.values, check_dtype=check_numpy_dtype) elif orient == "split": # index and col labels might not be strings unser.index = [str(i) for i in unser.index] unser.columns = [str(i) for i in unser.columns] if sort is None: unser = unser.sort_index() tm.assert_almost_equal(df.values, unser.values, check_dtype=check_numpy_dtype) else: if convert_axes: tm.assert_frame_equal(df, unser, check_dtype=check_dtype, check_index_type=check_index_type, check_column_type=check_column_type) else: tm.assert_frame_equal(df, unser, check_less_precise=False, check_dtype=check_dtype) def _check_all_orients(df, dtype=None, convert_axes=True, raise_ok=None, sort=None, check_index_type=True, check_column_type=True): # numpy=False if convert_axes: _check_orient(df, "columns", dtype=dtype, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "records", dtype=dtype, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "split", dtype=dtype, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "index", dtype=dtype, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "values", dtype=dtype, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "columns", dtype=dtype, convert_axes=False, sort=sort) _check_orient(df, "records", dtype=dtype, convert_axes=False, sort=sort) _check_orient(df, "split", dtype=dtype, convert_axes=False, sort=sort) _check_orient(df, "index", dtype=dtype, convert_axes=False, sort=sort) _check_orient(df, "values", dtype=dtype, convert_axes=False, sort=sort) # numpy=True and raise_ok might be not None, so ignore the error if convert_axes: _check_orient(df, "columns", dtype=dtype, numpy=True, raise_ok=raise_ok, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "records", dtype=dtype, numpy=True, raise_ok=raise_ok, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "split", dtype=dtype, numpy=True, raise_ok=raise_ok, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "index", dtype=dtype, numpy=True, raise_ok=raise_ok, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "values", dtype=dtype, numpy=True, raise_ok=raise_ok, sort=sort, check_index_type=False, check_column_type=False) _check_orient(df, "columns", dtype=dtype, numpy=True, convert_axes=False, raise_ok=raise_ok, sort=sort) _check_orient(df, "records", dtype=dtype, numpy=True, convert_axes=False, raise_ok=raise_ok, sort=sort) _check_orient(df, "split", dtype=dtype, numpy=True, convert_axes=False, raise_ok=raise_ok, sort=sort) _check_orient(df, "index", dtype=dtype, numpy=True, convert_axes=False, raise_ok=raise_ok, sort=sort) _check_orient(df, "values", dtype=dtype, numpy=True, convert_axes=False, raise_ok=raise_ok, sort=sort) # basic _check_all_orients(self.frame) assert self.frame.to_json() == self.frame.to_json(orient="columns") _check_all_orients(self.intframe, dtype=self.intframe.values.dtype) _check_all_orients(self.intframe, dtype=False) # big one # index and columns are strings as all unserialised JSON object keys # are assumed to be strings biggie = DataFrame(np.zeros((200, 4)), columns=[str(i) for i in range(4)], index=[str(i) for i in range(200)]) _check_all_orients(biggie, dtype=False, convert_axes=False) # dtypes _check_all_orients(DataFrame(biggie, dtype=np.float64), dtype=np.float64, convert_axes=False) _check_all_orients(DataFrame(biggie, dtype=np.int), dtype=np.int, convert_axes=False) _check_all_orients(DataFrame(biggie, dtype='U3'), dtype='U3', convert_axes=False, raise_ok=ValueError) # categorical _check_all_orients(self.categorical, sort='sort', raise_ok=ValueError) # empty _check_all_orients(self.empty_frame, check_index_type=False, check_column_type=False) # time series data _check_all_orients(self.tsframe) # mixed data index = pd.Index(['a', 'b', 'c', 'd', 'e']) data = {'A': [0., 1., 2., 3., 4.], 'B': [0., 1., 0., 1., 0.], 'C': ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], 'D': [True, False, True, False, True]} df = DataFrame(data=data, index=index) _check_orient(df, "split", check_dtype=False) _check_orient(df, "records", check_dtype=False) _check_orient(df, "values", check_dtype=False) _check_orient(df, "columns", check_dtype=False) # index oriented is problematic as it is read back in in a transposed # state, so the columns are interpreted as having mixed data and # given object dtypes. # force everything to have object dtype beforehand _check_orient(df.transpose().transpose(), "index", dtype=False) def test_frame_from_json_bad_data(self): pytest.raises(ValueError, read_json, StringIO('{"key":b:a:d}')) # too few indices json = StringIO('{"columns":["A","B"],' '"index":["2","3"],' '"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}') pytest.raises(ValueError, read_json, json, orient="split") # too many columns json = StringIO('{"columns":["A","B","C"],' '"index":["1","2","3"],' '"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}') pytest.raises(AssertionError, read_json, json, orient="split") # bad key json = StringIO('{"badkey":["A","B"],' '"index":["2","3"],' '"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}') with tm.assert_raises_regex(ValueError, r"unexpected key\(s\): badkey"): read_json(json, orient="split") def test_frame_from_json_nones(self): df = DataFrame([[1, 2], [4, 5, 6]]) unser = read_json(df.to_json()) assert np.isnan(unser[2][0]) df = DataFrame([['1', '2'], ['4', '5', '6']]) unser = read_json(df.to_json()) assert np.isnan(unser[2][0]) unser = read_json(df.to_json(), dtype=False) assert unser[2][0] is None unser = read_json(df.to_json(), convert_axes=False, dtype=False) assert unser['2']['0'] is None unser = read_json(df.to_json(), numpy=False) assert np.isnan(unser[2][0]) unser = read_json(df.to_json(), numpy=False, dtype=False) assert unser[2][0] is None unser = read_json(df.to_json(), numpy=False, convert_axes=False, dtype=False) assert unser['2']['0'] is None # infinities get mapped to nulls which get mapped to NaNs during # deserialisation df = DataFrame([[1, 2], [4, 5, 6]]) df.loc[0, 2] = np.inf unser = read_json(df.to_json()) assert np.isnan(unser[2][0]) unser = read_json(df.to_json(), dtype=False) assert np.isnan(unser[2][0]) df.loc[0, 2] = np.NINF unser = read_json(df.to_json()) assert np.isnan(unser[2][0]) unser = read_json(df.to_json(), dtype=False) assert np.isnan(unser[2][0]) @pytest.mark.skipif(is_platform_32bit(), reason="not compliant on 32-bit, xref #15865") def test_frame_to_json_float_precision(self): df = pd.DataFrame([dict(a_float=0.95)]) encoded = df.to_json(double_precision=1) assert encoded == '{"a_float":{"0":1.0}}' df = pd.DataFrame([dict(a_float=1.95)]) encoded = df.to_json(double_precision=1) assert encoded == '{"a_float":{"0":2.0}}' df = pd.DataFrame([dict(a_float=-1.95)]) encoded = df.to_json(double_precision=1) assert encoded == '{"a_float":{"0":-2.0}}' df = pd.DataFrame([dict(a_float=0.995)]) encoded = df.to_json(double_precision=2) assert encoded == '{"a_float":{"0":1.0}}' df = pd.DataFrame([dict(a_float=0.9995)]) encoded = df.to_json(double_precision=3) assert encoded == '{"a_float":{"0":1.0}}' df = pd.DataFrame([dict(a_float=0.99999999999999944)]) encoded = df.to_json(double_precision=15) assert encoded == '{"a_float":{"0":1.0}}' def test_frame_to_json_except(self): df = DataFrame([1, 2, 3]) pytest.raises(ValueError, df.to_json, orient="garbage") def test_frame_empty(self): df = DataFrame(columns=['jim', 'joe']) assert not df._is_mixed_type assert_frame_equal(read_json(df.to_json(), dtype=dict(df.dtypes)), df, check_index_type=False) # GH 7445 result = pd.DataFrame({'test': []}, index=[]).to_json(orient='columns') expected = '{"test":{}}' assert result == expected def test_frame_empty_mixedtype(self): # mixed type df = DataFrame(columns=['jim', 'joe']) df['joe'] = df['joe'].astype('i8') assert df._is_mixed_type assert_frame_equal(read_json(df.to_json(), dtype=dict(df.dtypes)), df, check_index_type=False) def test_frame_mixedtype_orient(self): # GH10289 vals = [[10, 1, 'foo', .1, .01], [20, 2, 'bar', .2, .02], [30, 3, 'baz', .3, .03], [40, 4, 'qux', .4, .04]] df = DataFrame(vals, index=list('abcd'), columns=['1st', '2nd', '3rd', '4th', '5th']) assert df._is_mixed_type right = df.copy() for orient in ['split', 'index', 'columns']: inp = df.to_json(orient=orient) left = read_json(inp, orient=orient, convert_axes=False) assert_frame_equal(left, right) right.index = np.arange(len(df)) inp = df.to_json(orient='records') left = read_json(inp, orient='records', convert_axes=False) assert_frame_equal(left, right) right.columns = np.arange(df.shape[1]) inp = df.to_json(orient='values') left = read_json(inp, orient='values', convert_axes=False) assert_frame_equal(left, right) def test_v12_compat(self): df = DataFrame( [[1.56808523, 0.65727391, 1.81021139, -0.17251653], [-0.2550111, -0.08072427, -0.03202878, -0.17581665], [1.51493992, 0.11805825, 1.629455, -1.31506612], [-0.02765498, 0.44679743, 0.33192641, -0.27885413], [0.05951614, -2.69652057, 1.28163262, 0.34703478]], columns=['A', 'B', 'C', 'D'], index=pd.date_range('2000-01-03', '2000-01-07')) df['date'] = pd.Timestamp('19920106 18:21:32.12') df.iloc[3, df.columns.get_loc('date')] = pd.Timestamp('20130101') df['modified'] = df['date'] df.iloc[1, df.columns.get_loc('modified')] = pd.NaT v12_json = os.path.join(self.dirpath, 'tsframe_v012.json') df_unser = pd.read_json(v12_json) assert_frame_equal(df, df_unser) df_iso = df.drop(['modified'], axis=1) v12_iso_json = os.path.join(self.dirpath, 'tsframe_iso_v012.json') df_unser_iso = pd.read_json(v12_iso_json) assert_frame_equal(df_iso, df_unser_iso) def test_blocks_compat_GH9037(self): index = pd.date_range('20000101', periods=10, freq='H') df_mixed = DataFrame(OrderedDict( float_1=[-0.92077639, 0.77434435, 1.25234727, 0.61485564, -0.60316077, 0.24653374, 0.28668979, -2.51969012, 0.95748401, -1.02970536], int_1=[19680418, 75337055, 99973684, 65103179, 79373900, 40314334, 21290235, 4991321, 41903419, 16008365], str_1=['78c608f1', '64a99743', '13d2ff52', 'ca7f4af2', '97236474', 'bde7e214', '1a6bde47', 'b1190be5', '7a669144', '8d64d068'], float_2=[-0.0428278, -1.80872357, 3.36042349, -0.7573685, -0.48217572, 0.86229683, 1.08935819, 0.93898739, -0.03030452, 1.43366348], str_2=['14f04af9', 'd085da90', '4bcfac83', '81504caf', '2ffef4a9', '08e2f5c4', '07e1af03', 'addbd4a7', '1f6a09ba', '4bfc4d87'], int_2=[86967717, 98098830, 51927505, 20372254, 12601730, 20884027, 34193846, 10561746, 24867120, 76131025] ), index=index) # JSON deserialisation always creates unicode strings df_mixed.columns = df_mixed.columns.astype('unicode') df_roundtrip = pd.read_json(df_mixed.to_json(orient='split'), orient='split') assert_frame_equal(df_mixed, df_roundtrip, check_index_type=True, check_column_type=True, check_frame_type=True, by_blocks=True, check_exact=True) def test_series_non_unique_index(self): s = Series(['a', 'b'], index=[1, 1]) pytest.raises(ValueError, s.to_json, orient='index') assert_series_equal(s, read_json(s.to_json(orient='split'), orient='split', typ='series')) unser = read_json(s.to_json(orient='records'), orient='records', typ='series') tm.assert_numpy_array_equal(s.values, unser.values) def test_series_from_json_to_json(self): def _check_orient(series, orient, dtype=None, numpy=False, check_index_type=True): series = series.sort_index() unser = read_json(series.to_json(orient=orient), typ='series', orient=orient, numpy=numpy, dtype=dtype) unser = unser.sort_index() if orient == "records" or orient == "values": assert_almost_equal(series.values, unser.values) else: if orient == "split": assert_series_equal(series, unser, check_index_type=check_index_type) else: assert_series_equal(series, unser, check_names=False, check_index_type=check_index_type) def _check_all_orients(series, dtype=None, check_index_type=True): _check_orient(series, "columns", dtype=dtype, check_index_type=check_index_type) _check_orient(series, "records", dtype=dtype, check_index_type=check_index_type) _check_orient(series, "split", dtype=dtype, check_index_type=check_index_type) _check_orient(series, "index", dtype=dtype, check_index_type=check_index_type) _check_orient(series, "values", dtype=dtype) _check_orient(series, "columns", dtype=dtype, numpy=True, check_index_type=check_index_type) _check_orient(series, "records", dtype=dtype, numpy=True, check_index_type=check_index_type) _check_orient(series, "split", dtype=dtype, numpy=True, check_index_type=check_index_type) _check_orient(series, "index", dtype=dtype, numpy=True, check_index_type=check_index_type) _check_orient(series, "values", dtype=dtype, numpy=True, check_index_type=check_index_type) # basic _check_all_orients(self.series) assert self.series.to_json() == self.series.to_json(orient="index") objSeries = Series([str(d) for d in self.objSeries], index=self.objSeries.index, name=self.objSeries.name) _check_all_orients(objSeries, dtype=False) # empty_series has empty index with object dtype # which cannot be revert assert self.empty_series.index.dtype == np.object_ _check_all_orients(self.empty_series, check_index_type=False) _check_all_orients(self.ts) # dtype s = Series(lrange(6), index=['a', 'b', 'c', 'd', 'e', 'f']) _check_all_orients(Series(s, dtype=np.float64), dtype=np.float64) _check_all_orients(Series(s, dtype=np.int), dtype=np.int) def test_series_to_json_except(self): s = Series([1, 2, 3]) pytest.raises(ValueError, s.to_json, orient="garbage") def test_series_from_json_precise_float(self): s = Series([4.56, 4.56, 4.56]) result = read_json(s.to_json(), typ='series', precise_float=True) assert_series_equal(result, s, check_index_type=False) def test_frame_from_json_precise_float(self): df = DataFrame([[4.56, 4.56, 4.56], [4.56, 4.56, 4.56]]) result = read_json(df.to_json(), precise_float=True) assert_frame_equal(result, df, check_index_type=False, check_column_type=False) def test_typ(self): s = Series(lrange(6), index=['a', 'b', 'c', 'd', 'e', 'f'], dtype='int64') result = read_json(s.to_json(), typ=None) assert_series_equal(result, s) def test_reconstruction_index(self): df = DataFrame([[1, 2, 3], [4, 5, 6]]) result = read_json(df.to_json()) assert_frame_equal(result, df) df = DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=['A', 'B', 'C']) result = read_json(df.to_json()) assert_frame_equal(result, df) def test_path(self): with ensure_clean('test.json') as path: for df in [self.frame, self.frame2, self.intframe, self.tsframe, self.mixed_frame]: df.to_json(path) read_json(path) def test_axis_dates(self): # frame json = self.tsframe.to_json() result = read_json(json) assert_frame_equal(result, self.tsframe) # series json = self.ts.to_json() result = read_json(json, typ='series') assert_series_equal(result, self.ts, check_names=False) assert result.name is None def test_convert_dates(self): # frame df = self.tsframe.copy() df['date'] = Timestamp('20130101') json = df.to_json() result = read_json(json) assert_frame_equal(result, df) df['foo'] = 1. json = df.to_json(date_unit='ns') result = read_json(json, convert_dates=False) expected = df.copy() expected['date'] = expected['date'].values.view('i8') expected['foo'] = expected['foo'].astype('int64') assert_frame_equal(result, expected) # series ts = Series(Timestamp('20130101'), index=self.ts.index) json = ts.to_json() result = read_json(json, typ='series') assert_series_equal(result, ts) def test_convert_dates_infer(self): # GH10747 from pandas.io.json import dumps infer_words = ['trade_time', 'date', 'datetime', 'sold_at', 'modified', 'timestamp', 'timestamps'] for infer_word in infer_words: data = [{'id': 1, infer_word: 1036713600000}, {'id': 2}] expected = DataFrame([[1, Timestamp('2002-11-08')], [2, pd.NaT]], columns=['id', infer_word]) result = read_json(dumps(data))[['id', infer_word]] assert_frame_equal(result, expected) def test_date_format_frame(self): df = self.tsframe.copy() def test_w_date(date, date_unit=None): df['date'] = Timestamp(date) df.iloc[1, df.columns.get_loc('date')] = pd.NaT df.iloc[5, df.columns.get_loc('date')] = pd.NaT if date_unit: json = df.to_json(date_format='iso', date_unit=date_unit) else: json = df.to_json(date_format='iso') result = read_json(json) assert_frame_equal(result, df) test_w_date('20130101 20:43:42.123') test_w_date('20130101 20:43:42', date_unit='s') test_w_date('20130101 20:43:42.123', date_unit='ms') test_w_date('20130101 20:43:42.123456', date_unit='us') test_w_date('20130101 20:43:42.123456789', date_unit='ns') pytest.raises(ValueError, df.to_json, date_format='iso', date_unit='foo') def test_date_format_series(self): def test_w_date(date, date_unit=None): ts = Series(Timestamp(date), index=self.ts.index) ts.iloc[1] = pd.NaT ts.iloc[5] = pd.NaT if date_unit: json = ts.to_json(date_format='iso', date_unit=date_unit) else: json = ts.to_json(date_format='iso') result = read_json(json, typ='series') assert_series_equal(result, ts) test_w_date('20130101 20:43:42.123') test_w_date('20130101 20:43:42', date_unit='s') test_w_date('20130101 20:43:42.123', date_unit='ms') test_w_date('20130101 20:43:42.123456', date_unit='us') test_w_date('20130101 20:43:42.123456789', date_unit='ns') ts = Series(Timestamp('20130101 20:43:42.123'), index=self.ts.index) pytest.raises(ValueError, ts.to_json, date_format='iso', date_unit='foo') def test_date_unit(self): df = self.tsframe.copy() df['date'] = Timestamp('20130101 20:43:42') dl = df.columns.get_loc('date') df.iloc[1, dl] = Timestamp('19710101 20:43:42') df.iloc[2, dl] = Timestamp('21460101 20:43:42') df.iloc[4, dl] = pd.NaT for unit in ('s', 'ms', 'us', 'ns'): json = df.to_json(date_format='epoch', date_unit=unit) # force date unit result = read_json(json, date_unit=unit) assert_frame_equal(result, df) # detect date unit result = read_json(json, date_unit=None) assert_frame_equal(result, df) def test_weird_nested_json(self): # this used to core dump the parser s = r'''{ "status": "success", "data": { "posts": [ { "id": 1, "title": "A blog post", "body": "Some useful content" }, { "id": 2, "title": "Another blog post", "body": "More content" } ] } }''' read_json(s) def test_doc_example(self): dfj2 = DataFrame(np.random.randn(5, 2), columns=list('AB')) dfj2['date'] = Timestamp('20130101') dfj2['ints'] = lrange(5) dfj2['bools'] = True dfj2.index = pd.date_range('20130101', periods=5) json = dfj2.to_json() result = read_json(json, dtype={'ints': np.int64, 'bools': np.bool_}) assert_frame_equal(result, result) def test_misc_example(self): # parsing unordered input fails result = read_json('[{"a": 1, "b": 2}, {"b":2, "a" :1}]', numpy=True) expected = DataFrame([[1, 2], [1, 2]], columns=['a', 'b']) error_msg = """DataFrame\\.index are different DataFrame\\.index values are different \\(100\\.0 %\\) \\[left\\]: Index\\(\\[u?'a', u?'b'\\], dtype='object'\\) \\[right\\]: RangeIndex\\(start=0, stop=2, step=1\\)""" with tm.assert_raises_regex(AssertionError, error_msg): assert_frame_equal(result, expected, check_index_type=False) result = read_json('[{"a": 1, "b": 2}, {"b":2, "a" :1}]') expected = DataFrame([[1, 2], [1, 2]], columns=['a', 'b']) assert_frame_equal(result, expected) @network def test_round_trip_exception_(self): # GH 3867 csv = 'https://raw.github.com/hayd/lahman2012/master/csvs/Teams.csv' df = pd.read_csv(csv) s = df.to_json() result = pd.read_json(s) assert_frame_equal(result.reindex( index=df.index, columns=df.columns), df) @network def test_url(self): url = 'https://api.github.com/repos/pandas-dev/pandas/issues?per_page=5' # noqa result = read_json(url, convert_dates=True) for c in ['created_at', 'closed_at', 'updated_at']: assert result[c].dtype == 'datetime64[ns]' def test_timedelta(self): converter = lambda x: pd.to_timedelta(x, unit='ms') s = Series([timedelta(23), timedelta(seconds=5)]) assert s.dtype == 'timedelta64[ns]' result = pd.read_json(s.to_json(), typ='series').apply(converter) assert_series_equal(result, s) s = Series([timedelta(23), timedelta(seconds=5)], index=pd.Index([0, 1])) assert s.dtype == 'timedelta64[ns]' result = pd.read_json(s.to_json(), typ='series').apply(converter) assert_series_equal(result, s) frame = DataFrame([timedelta(23), timedelta(seconds=5)]) assert frame[0].dtype == 'timedelta64[ns]' assert_frame_equal(frame, pd.read_json(frame.to_json()) .apply(converter)) frame = DataFrame({'a': [timedelta(days=23), timedelta(seconds=5)], 'b': [1, 2], 'c': pd.date_range(start='20130101', periods=2)}) result = pd.read_json(frame.to_json(date_unit='ns')) result['a'] = pd.to_timedelta(result.a, unit='ns') result['c'] = pd.to_datetime(result.c) assert_frame_equal(frame, result) def test_mixed_timedelta_datetime(self): frame = DataFrame({'a': [timedelta(23), pd.Timestamp('20130101')]}, dtype=object) expected = DataFrame({'a': [pd.Timedelta(frame.a[0]).value, pd.Timestamp(frame.a[1]).value]}) result = pd.read_json(frame.to_json(date_unit='ns'), dtype={'a': 'int64'}) assert_frame_equal(result, expected, check_index_type=False) def test_default_handler(self): value = object() frame = DataFrame({'a': [7, value]}) expected = DataFrame({'a': [7, str(value)]}) result = pd.read_json(frame.to_json(default_handler=str)) assert_frame_equal(expected, result, check_index_type=False) def test_default_handler_indirect(self): from pandas.io.json import dumps def default(obj): if isinstance(obj, complex): return [('mathjs', 'Complex'), ('re', obj.real), ('im', obj.imag)] return str(obj) df_list = [9, DataFrame({'a': [1, 'STR', complex(4, -5)], 'b': [float('nan'), None, 'N/A']}, columns=['a', 'b'])] expected = ('[9,[[1,null],["STR",null],[[["mathjs","Complex"],' '["re",4.0],["im",-5.0]],"N\\/A"]]]') assert dumps(df_list, default_handler=default, orient="values") == expected def test_default_handler_numpy_unsupported_dtype(self): # GH12554 to_json raises 'Unhandled numpy dtype 15' df = DataFrame({'a': [1, 2.3, complex(4, -5)], 'b': [float('nan'), None, complex(1.2, 0)]}, columns=['a', 'b']) expected = ('[["(1+0j)","(nan+0j)"],' '["(2.3+0j)","(nan+0j)"],' '["(4-5j)","(1.2+0j)"]]') assert df.to_json(default_handler=str, orient="values") == expected def test_default_handler_raises(self): def my_handler_raises(obj): raise TypeError("raisin") pytest.raises(TypeError, DataFrame({'a': [1, 2, object()]}).to_json, default_handler=my_handler_raises) pytest.raises(TypeError, DataFrame({'a': [1, 2, complex(4, -5)]}).to_json, default_handler=my_handler_raises) def test_categorical(self): # GH4377 df.to_json segfaults with non-ndarray blocks df = DataFrame({"A": ["a", "b", "c", "a", "b", "b", "a"]}) df["B"] = df["A"] expected = df.to_json() df["B"] = df["A"].astype('category') assert expected == df.to_json() s = df["A"] sc = df["B"] assert s.to_json() == sc.to_json() def test_datetime_tz(self): # GH4377 df.to_json segfaults with non-ndarray blocks tz_range = pd.date_range('20130101', periods=3, tz='US/Eastern') tz_naive = tz_range.tz_convert('utc').tz_localize(None) df = DataFrame({ 'A': tz_range, 'B': pd.date_range('20130101', periods=3)}) df_naive = df.copy() df_naive['A'] = tz_naive expected = df_naive.to_json() assert expected == df.to_json() stz = Series(tz_range) s_naive = Series(tz_naive) assert stz.to_json() == s_naive.to_json() def test_sparse(self): # GH4377 df.to_json segfaults with non-ndarray blocks df = pd.DataFrame(np.random.randn(10, 4)) df.loc[:8] = np.nan sdf = df.to_sparse() expected = df.to_json() assert expected == sdf.to_json() s = pd.Series(np.random.randn(10)) s.loc[:8] = np.nan ss = s.to_sparse() expected = s.to_json() assert expected == ss.to_json() def test_tz_is_utc(self): from pandas.io.json import dumps exp = '"2013-01-10T05:00:00.000Z"' ts = Timestamp('2013-01-10 05:00:00Z') assert dumps(ts, iso_dates=True) == exp dt = ts.to_pydatetime() assert dumps(dt, iso_dates=True) == exp ts = Timestamp('2013-01-10 00:00:00', tz='US/Eastern') assert dumps(ts, iso_dates=True) == exp dt = ts.to_pydatetime() assert dumps(dt, iso_dates=True) == exp ts = Timestamp('2013-01-10 00:00:00-0500') assert dumps(ts, iso_dates=True) == exp dt = ts.to_pydatetime() assert dumps(dt, iso_dates=True) == exp def test_tz_range_is_utc(self): from pandas.io.json import dumps exp = '["2013-01-01T05:00:00.000Z","2013-01-02T05:00:00.000Z"]' dfexp = ('{"DT":{' '"0":"2013-01-01T05:00:00.000Z",' '"1":"2013-01-02T05:00:00.000Z"}}') tz_range = pd.date_range('2013-01-01 05:00:00Z', periods=2) assert dumps(tz_range, iso_dates=True) == exp dti = pd.DatetimeIndex(tz_range) assert dumps(dti, iso_dates=True) == exp df = DataFrame({'DT': dti}) assert dumps(df, iso_dates=True) == dfexp tz_range = pd.date_range('2013-01-01 00:00:00', periods=2, tz='US/Eastern') assert dumps(tz_range, iso_dates=True) == exp dti = pd.DatetimeIndex(tz_range) assert dumps(dti, iso_dates=True) == exp df = DataFrame({'DT': dti}) assert dumps(df, iso_dates=True) == dfexp tz_range = pd.date_range('2013-01-01 00:00:00-0500', periods=2) assert dumps(tz_range, iso_dates=True) == exp dti = pd.DatetimeIndex(tz_range) assert dumps(dti, iso_dates=True) == exp df = DataFrame({'DT': dti}) assert dumps(df, iso_dates=True) == dfexp def test_read_jsonl(self): # GH9180 result = read_json('{"a": 1, "b": 2}\n{"b":2, "a" :1}\n', lines=True) expected = DataFrame([[1, 2], [1, 2]], columns=['a', 'b']) assert_frame_equal(result, expected) def test_read_jsonl_unicode_chars(self): # GH15132: non-ascii unicode characters # \u201d == RIGHT DOUBLE QUOTATION MARK # simulate file handle json = '{"a": "foo”", "b": "bar"}\n{"a": "foo", "b": "bar"}\n' json = StringIO(json) result = read_json(json, lines=True) expected = DataFrame([[u"foo\u201d", "bar"], ["foo", "bar"]], columns=['a', 'b']) assert_frame_equal(result, expected) # simulate string json = '{"a": "foo”", "b": "bar"}\n{"a": "foo", "b": "bar"}\n' result = read_json(json, lines=True) expected = DataFrame([[u"foo\u201d", "bar"], ["foo", "bar"]], columns=['a', 'b']) assert_frame_equal(result, expected) def test_to_jsonl(self): # GH9180 df = DataFrame([[1, 2], [1, 2]], columns=['a', 'b']) result = df.to_json(orient="records", lines=True) expected = '{"a":1,"b":2}\n{"a":1,"b":2}' assert result == expected df = DataFrame([["foo}", "bar"], ['foo"', "bar"]], columns=['a', 'b']) result = df.to_json(orient="records", lines=True) expected = '{"a":"foo}","b":"bar"}\n{"a":"foo\\"","b":"bar"}' assert result == expected assert_frame_equal(pd.read_json(result, lines=True), df) # GH15096: escaped characters in columns and data df = DataFrame([["foo\\", "bar"], ['foo"', "bar"]], columns=["a\\", 'b']) result = df.to_json(orient="records", lines=True) expected = ('{"a\\\\":"foo\\\\","b":"bar"}\n' '{"a\\\\":"foo\\"","b":"bar"}') assert result == expected assert_frame_equal(pd.read_json(result, lines=True), df) def test_latin_encoding(self): if compat.PY2: tm.assert_raises_regex( TypeError, r'\[unicode\] is not implemented as a table column') return # GH 13774 pytest.skip("encoding not implemented in .to_json(), " "xref #13774") values = [[b'E\xc9, 17', b'', b'a', b'b', b'c'], [b'E\xc9, 17', b'a', b'b', b'c'], [b'EE, 17', b'', b'a', b'b', b'c'], [b'E\xc9, 17', b'\xf8\xfc', b'a', b'b', b'c'], [b'', b'a', b'b', b'c'], [b'\xf8\xfc', b'a', b'b', b'c'], [b'A\xf8\xfc', b'', b'a', b'b', b'c'], [np.nan, b'', b'b', b'c'], [b'A\xf8\xfc', np.nan, b'', b'b', b'c']] def _try_decode(x, encoding='latin-1'): try: return x.decode(encoding) except AttributeError: return x # not sure how to remove latin-1 from code in python 2 and 3 values = [[_try_decode(x) for x in y] for y in values] examples = [] for dtype in ['category', object]: for val in values: examples.append(Series(val, dtype=dtype)) def roundtrip(s, encoding='latin-1'): with ensure_clean('test.json') as path: s.to_json(path, encoding=encoding) retr = read_json(path, encoding=encoding) assert_series_equal(s, retr, check_categorical=False) for s in examples: roundtrip(s) def test_data_frame_size_after_to_json(self): # GH15344 df = DataFrame({'a': [str(1)]}) size_before = df.memory_usage(index=True, deep=True).sum() df.to_json() size_after = df.memory_usage(index=True, deep=True).sum() assert size_before == size_after
bsd-3-clause
aarongarrett/inspyred
recipes/constraint_selection.py
1
3392
import random from inspyred import ec from inspyred.ec import variators from inspyred.ec import replacers from inspyred.ec import terminators from inspyred.ec import observers def my_constraint_function(candidate): """Return the number of constraints that candidate violates.""" # In this case, we'll just say that the point has to lie # within a circle centered at (0, 0) of radius 1. if candidate[0]**2 + candidate[1]**2 > 1: return 1 else: return 0 def my_generator(random, args): # Create pairs in the range [-2, 2]. return [random.uniform(-2.0, 2.0) for i in range(2)] def my_evaluator(candidates, args): # The fitness will be how far the point is from # the origin. (We're maximizing, in this case.) # Note that the constraint heavily punishes individuals # who go beyond the unit circle. Therefore, these # two functions combined focus the evolution toward # finding individual who lie ON the circle. fitness = [] for c in candidates: if my_constraint_function(c) > 0: fitness.append(-1) else: fitness.append(c[0]**2 + c[1]**2) return fitness def constrained_tournament_selection(random, population, args): num_selected = args.setdefault('num_selected', 1) constraint_func = args.setdefault('constraint_function', None) tournament_size = 2 pop = list(population) selected = [] for _ in range(num_selected): tournament = random.sample(pop, tournament_size) # If there is not a constraint function, # just do regular tournament selection. if constraint_func is None: selected.append(max(tournament)) else: cons = [constraint_func(t.candidate) for t in tournament] # If no constraints are violated, just do # regular tournament selection. if max(cons) == 0: selected.append(max(tournament)) # Otherwise, choose the least violator # (which may be a non-violator). else: selected.append(tournament[cons.index(min(cons))]) return selected r = random.Random() myec = ec.EvolutionaryComputation(r) myec.selector = constrained_tournament_selection myec.variator = variators.gaussian_mutation myec.replacer = replacers.generational_replacement myec.terminator = terminators.evaluation_termination myec.observer = observers.stats_observer pop = myec.evolve(my_generator, my_evaluator, pop_size=100, bounder=ec.Bounder(-2, 2), num_selected=100, constraint_func=my_constraint_function, mutation_rate=0.5, max_evaluations=2000) import matplotlib.pyplot as plt import numpy x = [] y = [] c = [] pop.sort() num_feasible = len([p for p in pop if p.fitness >= 0]) feasible_count = 0 for i, p in enumerate(pop): x.append(p.candidate[0]) y.append(p.candidate[1]) if i == len(pop) - 1: c.append('r') elif p.fitness < 0: c.append('0.98') else: c.append(str(1 - feasible_count / float(num_feasible))) feasible_count += 1 angles = numpy.linspace(0, 2*numpy.pi, 100) plt.plot(numpy.cos(angles), numpy.sin(angles), color='b') plt.scatter(x, y, color=c) plt.savefig('constraint_example.pdf', format='pdf')
mit
nlaanait/pyxrim
pyxrim/PlotLib.py
1
6186
# -*- coding: utf-8 -*- """ Created on Fri Oct 9 10:23:05 2015 @author: nouamanelaanait """ import numpy as np from matplotlib import pyplot as plt def imageGallery(n_col, n_row, Title , images, **kwargs): ''' Plots a bunch of images. num := figure num n_col := # of columns n_rows := ... titleList := list of axes titles images := list of images **kwargs of plt.imshow ''' fig, axes = plt.subplots(n_row, n_col, squeeze=True, figsize = (12, 6), sharex = True, sharey =True) plt.suptitle(Title, size=16) for ax, imp in zip(axes.flat, images): ax.imshow(imp, **kwargs) ax.axis('off') def plotGallery(num, subtitles, xlist, ylist, n_col,n_row, Maintitle = '',**kwargs): ''' Function for multiple (x,y) scatter plots. ''' plt.figure(num, figsize=(5 * n_col, 5 * n_row)) plt.suptitle(Maintitle, size=16) lst = [xlist, ylist, subtitles] # for i, elem in zip(range(0,len(lst)+1),lst): for i in range(0,len(lst)+1): ax = plt.subplot(n_row, n_col, i+1 ) plt.plot(lst[0][i], lst[1][i],**kwargs) ax.set_title(str(lst[2][i])) def imageFeaturesGallery(n_col, n_row, Title, images ,keypts, **kwargs): ''' Plots a bunch of images and features . num := figure num n_col := # of columns n_rows := ... titleList := list of axes titles images := list of images **kwargs of plt.imshow ''' fig, axes = plt.subplots(n_row, n_col, squeeze=True, figsize = (8, 8), sharex = True, sharey =True) plt.suptitle(Title, size=16) # plt.figure(num, figsize=( n_col, n_row)) if len(keypts) != 0 : for ax, imp, key in zip(axes.flat, images, keypts): ax.imshow(imp) ax.scatter(key[:, 1], key[:, 0],marker ='D',c = 'k', s =15 ) ax.axis('off') else: for ax, imp in zip(axes.flat, images): ax.imshow(imp, **kwargs) ax.axis('off') plt.tight_layout(pad = 0.5, h_pad = 0.01, w_pad =0.01) def plotMatches(ax, image1, image2, keypoints1, keypoints2, matches, keypoints_color='k', matches_color=None, only_matches=False, **kwargs): """Plot matched features. Parameters ---------- ax : matplotlib.axes.Axes Matches and image are drawn in this ax. image1 : (N, M [, 3]) array First grayscale or color image. image2 : (N, M [, 3]) array Second grayscale or color image. keypoints1 : (K1, 2) array First keypoint coordinates as ``(row, col)``. keypoints2 : (K2, 2) array Second keypoint coordinates as ``(row, col)``. matches : (Q, 2) array Indices of corresponding matches in first and second set of descriptors, where ``matches[:, 0]`` denote the indices in the first and ``matches[:, 1]`` the indices in the second set of descriptors. keypoints_color : matplotlib color, optional Color for keypoint locations. matches_color : matplotlib color, optional Color for lines which connect keypoint matches. By default the color is chosen randomly. only_matches : bool, optional Whether to only plot matches and not plot the keypoint locations. **kwargs of imshow """ image1 = img_as_float(image1) image2 = img_as_float(image2) new_shape1 = list(image1.shape) new_shape2 = list(image2.shape) if image1.shape[0] < image2.shape[0]: new_shape1[0] = image2.shape[0] elif image1.shape[0] > image2.shape[0]: new_shape2[0] = image1.shape[0] if image1.shape[1] < image2.shape[1]: new_shape1[1] = image2.shape[1] elif image1.shape[1] > image2.shape[1]: new_shape2[1] = image1.shape[1] if new_shape1 != image1.shape: new_image1 = np.zeros(new_shape1, dtype=image1.dtype) new_image1[:image1.shape[0], :image1.shape[1]] = image1 image1 = new_image1 if new_shape2 != image2.shape: new_image2 = np.zeros(new_shape2, dtype=image2.dtype) new_image2[:image2.shape[0], :image2.shape[1]] = image2 image2 = new_image2 image = np.concatenate([image1, image2], axis=1) offset = image1.shape if not only_matches: ax.scatter(keypoints1[:, 1], keypoints1[:, 0], facecolors='none', edgecolors=keypoints_color) ax.scatter(keypoints2[:, 1] + offset[1], keypoints2[:, 0], facecolors='none', edgecolors=keypoints_color) ax.imshow(image, cmap='jet', vmin= np.amin(image1), vmax=np.amax(image2)/6.5089) # ax.imshow(image, **kwargs) ax.axis((0, 2 * offset[1], offset[0], 0)) for i in range(matches.shape[0]): idx1 = matches[i, 0] idx2 = matches[i, 1] if matches_color is None: color = np.random.rand(3, 1) else: color = matches_color ax.plot((keypoints1[idx1, 1], keypoints2[idx2, 1] + offset[1]), (keypoints1[idx1, 0], keypoints2[idx2, 0]), '-', color=color) def imageTile(data, padsize=1, padval=0, figsize=(12,12),**kwargs): ''' Function to tile n-images into a single image Input: data: np.ndarray. Must be of dimension 3. padsize: size in pixels of borders between different images. Default is 1. padval: value by which to pad. Default is 0. figsize: size of the figure passed to matplotlib.pyplot.figure. Default is (12,12). **kwargs: extra arguments to be passed to pyplot.imshow() function. ''' # force the number of images to be square n = int(np.ceil(np.sqrt(data.shape[0]))) padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3) data = np.pad(data, padding, mode='constant', constant_values=(padval, padval)) # tile all the images into a single image data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) fig,ax = plt.subplots(1,1,figsize=figsize) ax.imshow(data,**kwargs) ax.axis('off')
mit
sujitmhj/devanagari-handwritting-recognition
dbn.py
1
2177
# import the necessary packages from sklearn.cross_validation import train_test_split from sklearn.metrics import classification_report from sklearn import datasets from nolearn.dbn import DBN import numpy as np import cv2 import scipy.io as sio # grab the MNIST dataset (if this is the first time you are running # this script, this make take a minute -- the 55mb MNIST digit dataset # will be downloaded) print "[X] downloading data..." dataset = datasets.fetch_mldata("MNIST Original") # scale the data to the range [0, 1] and then construct the training # and testing splits # (trainX, testX, trainY, testY) = train_test_split( # dataset.data / 255.0, dataset.target.astype("U7"), test_size = 0.33) # dataset.data = dataset.data/255.0 dataset = sio.loadmat("/home/sujit/projects/personal/dbn/mnist-original.mat") # dataset = sio.loadmat("/home/sujit/scikit_learn_data/mldata/mnist-original.mat") trainX = dataset['data'].T[0:20000,0:]/255.0 trainY = dataset['label'][0][0:20000] testX = dataset['data'].T[2000:,0:]/255.0 testY = dataset['label'][0][20000:] # train the Deep Belief Network with 784 input units (the flattened, # 28x28 grayscale image), 300 hidden units, 10 output units (one for # each possible output classification, which are the digits 1-10) print trainX.shape, trainY.shape # trainY = np.array(range(2000)) print type(trainY), trainY dbn = DBN( [trainX.shape[0], 300, 10], learn_rates = 0.3, learn_rate_decays = 0.9, epochs = 10, verbose = 1) dbn.fit(trainX, trainY) # # compute the predictions for the test data and show a classification # # report # preds = dbn.predict(testX) # print classification_report(testY, preds) # randomly select a few of the test instances for i in np.random.choice(np.arange(0, len(testY)), size = (10,)): # classify the digit # pred = dbn.predict(np.atleast_2d(testX[i])) # reshape the feature vector to be a 28x28 pixel image, then change # the data type to be an unsigned 8-bit integer image = (testX[i] * 255).reshape((28, 28)).astype("uint8") # show the image and prediction # print "Actual digit is {0}, predicted {1}".format(testY[i], pred[0]) cv2.imshow("Digit", image) cv2.waitKey(0)
mit
boland1992/seissuite_iran
build/lib/ambient/spectrum/heatinterpolate.py
8
3048
#!/usr/bin/env python # combining density estimation and delaunay interpolation for confidence-weighted value mapping # Dan Stowell, April 2013 import numpy as np from numpy import random #from math import exp, log from scipy import stats, mgrid, c_, reshape, rot90 import matplotlib.delaunay #import matplotlib.tri as tri import matplotlib.delaunay.interpolate from matplotlib.colors import LogNorm import matplotlib.pyplot as plt import matplotlib.cm as cm #from colorsys import hls_to_rgb import pickle pickle_file = '/storage/ANT/spectral_density/station_pds_maxima/\ S Network 2014/noise_info0_SNetwork2014.pickle' f = open(name=pickle_file, mode='rb') data = pickle.load(f) f.close() ############################# # user settings n = 100 gridsize = 100 fontsize = 'xx-small' ############################# # first generate some random [x,y,z] data -- random locations but closest to the middle, and random z-values # we will add some correlation to the z-values data[:,2] += data[:,1] data[:,2] += data[:,0] # scale the z-values to 0--1 for convenience zmin = np.min(data[:,2]) zmax = np.max(data[:,2]) xmin = np.min(data[:,0]) xmax = np.max(data[:,0]) ymin = np.min(data[:,1]) ymax = np.max(data[:,1]) zmin = np.min(data[:,2]) zmax = np.max(data[:,2]) ################################################## # plot it simply plt.figure() ################################################## # now make a KDE of it and plot that kdeX, kdeY = mgrid[xmin:xmax:gridsize*1j, ymin:ymax:gridsize*1j] positions = c_[kdeX.ravel(), kdeY.ravel()] values = c_[data[:,0], data[:,1]] kernel = stats.kde.gaussian_kde(values.T) kdeZ = reshape(kernel(positions.T).T, kdeX.T.shape) ################################################## # now make a delaunay triangulation of it and plot that tt = matplotlib.delaunay.triangulate.Triangulation(data[:,0], data[:,1]) print xmin, xmax, ymin, ymax print gridsize extrap = tt.nn_extrapolator(data[:,2]) interped = extrap[xmin:xmax:gridsize*1j, ymin:ymax:gridsize*1j] ################################################## # now combine delaunay with KDE colours = np.zeros((gridsize, gridsize, 4)) kdeZmin = np.min(kdeZ) kdeZmax = np.max(kdeZ) confdepth = 0.45 for x in range(gridsize): for y in range(gridsize): conf = (kdeZ[x,y] - kdeZmin) / (kdeZmax - kdeZmin) val = min(1., max(0., interped[x,y])) colour = list(cm.rainbow(val)) # now fade it out to white according to conf for index in [0,1,2]: colour[index] = (colour[index] * conf) + (1.0 * (1. -conf)) colours[x,y,:] = colour #colours[x,y,:] = np.hstack((hls_to_rgb(val, 0.5 + confdepth - (confdepth * conf), 1.0), 1.0)) #colours[x,y,:] = [conf, conf, 1.0-conf, val] print colours plt.imshow(rot90(colours), cmap=cm.rainbow, norm=LogNorm(\ vmin=zmin, vmax=zmax)) plt.title("interpolated & confidence-shaded") plt.ylim([ymin,ymax]) plt.xlim([xmin,xmax]) plt.xticks(fontsize=fontsize) plt.yticks(fontsize=fontsize) ############################################ plt.savefig("plot_heati_simple.svg", format='SVG')
gpl-3.0
shyamalschandra/scikit-learn
sklearn/naive_bayes.py
29
28917
# -*- coding: utf-8 -*- """ The :mod:`sklearn.naive_bayes` module implements Naive Bayes algorithms. These are supervised learning methods based on applying Bayes' theorem with strong (naive) feature independence assumptions. """ # Author: Vincent Michel <[email protected]> # Minor fixes by Fabian Pedregosa # Amit Aides <[email protected]> # Yehuda Finkelstein <[email protected]> # Lars Buitinck <[email protected]> # Jan Hendrik Metzen <[email protected]> # (parts based on earlier work by Mathieu Blondel) # # License: BSD 3 clause from abc import ABCMeta, abstractmethod import numpy as np from scipy.sparse import issparse from .base import BaseEstimator, ClassifierMixin from .preprocessing import binarize from .preprocessing import LabelBinarizer from .preprocessing import label_binarize from .utils import check_X_y, check_array from .utils.extmath import safe_sparse_dot, logsumexp from .utils.multiclass import _check_partial_fit_first_call from .utils.fixes import in1d from .utils.validation import check_is_fitted from .externals import six __all__ = ['BernoulliNB', 'GaussianNB', 'MultinomialNB'] class BaseNB(six.with_metaclass(ABCMeta, BaseEstimator, ClassifierMixin)): """Abstract base class for naive Bayes estimators""" @abstractmethod def _joint_log_likelihood(self, X): """Compute the unnormalized posterior log probability of X I.e. ``log P(c) + log P(x|c)`` for all rows x of X, as an array-like of shape [n_classes, n_samples]. Input is passed to _joint_log_likelihood as-is by predict, predict_proba and predict_log_proba. """ def predict(self, X): """ Perform classification on an array of test vectors X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array, shape = [n_samples] Predicted target values for X """ jll = self._joint_log_likelihood(X) return self.classes_[np.argmax(jll, axis=1)] def predict_log_proba(self, X): """ Return log-probability estimates for the test vector X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array-like, shape = [n_samples, n_classes] Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute `classes_`. """ jll = self._joint_log_likelihood(X) # normalize by P(x) = P(f_1, ..., f_n) log_prob_x = logsumexp(jll, axis=1) return jll - np.atleast_2d(log_prob_x).T def predict_proba(self, X): """ Return probability estimates for the test vector X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array-like, shape = [n_samples, n_classes] Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute `classes_`. """ return np.exp(self.predict_log_proba(X)) class GaussianNB(BaseNB): """ Gaussian Naive Bayes (GaussianNB) Can perform online updates to model parameters via `partial_fit` method. For details on algorithm used to update feature means and variance online, see Stanford CS tech report STAN-CS-79-773 by Chan, Golub, and LeVeque: http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf Read more in the :ref:`User Guide <gaussian_naive_bayes>`. Attributes ---------- class_prior_ : array, shape (n_classes,) probability of each class. class_count_ : array, shape (n_classes,) number of training samples observed in each class. theta_ : array, shape (n_classes, n_features) mean of each feature per class sigma_ : array, shape (n_classes, n_features) variance of each feature per class Examples -------- >>> import numpy as np >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> Y = np.array([1, 1, 1, 2, 2, 2]) >>> from sklearn.naive_bayes import GaussianNB >>> clf = GaussianNB() >>> clf.fit(X, Y) GaussianNB() >>> print(clf.predict([[-0.8, -1]])) [1] >>> clf_pf = GaussianNB() >>> clf_pf.partial_fit(X, Y, np.unique(Y)) GaussianNB() >>> print(clf_pf.predict([[-0.8, -1]])) [1] """ def fit(self, X, y, sample_weight=None): """Fit Gaussian Naive Bayes according to X, y Parameters ---------- X : array-like, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples,) Target values. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). .. versionadded:: 0.17 Gaussian Naive Bayes supports fitting with *sample_weight*. Returns ------- self : object Returns self. """ X, y = check_X_y(X, y) return self._partial_fit(X, y, np.unique(y), _refit=True, sample_weight=sample_weight) @staticmethod def _update_mean_variance(n_past, mu, var, X, sample_weight=None): """Compute online update of Gaussian mean and variance. Given starting sample count, mean, and variance, a new set of points X, and optionally sample weights, return the updated mean and variance. (NB - each dimension (column) in X is treated as independent -- you get variance, not covariance). Can take scalar mean and variance, or vector mean and variance to simultaneously update a number of independent Gaussians. See Stanford CS tech report STAN-CS-79-773 by Chan, Golub, and LeVeque: http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf Parameters ---------- n_past : int Number of samples represented in old mean and variance. If sample weights were given, this should contain the sum of sample weights represented in old mean and variance. mu : array-like, shape (number of Gaussians,) Means for Gaussians in original set. var : array-like, shape (number of Gaussians,) Variances for Gaussians in original set. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- total_mu : array-like, shape (number of Gaussians,) Updated mean for each Gaussian over the combined set. total_var : array-like, shape (number of Gaussians,) Updated variance for each Gaussian over the combined set. """ if X.shape[0] == 0: return mu, var # Compute (potentially weighted) mean and variance of new datapoints if sample_weight is not None: n_new = float(sample_weight.sum()) new_mu = np.average(X, axis=0, weights=sample_weight / n_new) new_var = np.average((X - new_mu) ** 2, axis=0, weights=sample_weight / n_new) else: n_new = X.shape[0] new_var = np.var(X, axis=0) new_mu = np.mean(X, axis=0) if n_past == 0: return new_mu, new_var n_total = float(n_past + n_new) # Combine mean of old and new data, taking into consideration # (weighted) number of observations total_mu = (n_new * new_mu + n_past * mu) / n_total # Combine variance of old and new data, taking into consideration # (weighted) number of observations. This is achieved by combining # the sum-of-squared-differences (ssd) old_ssd = n_past * var new_ssd = n_new * new_var total_ssd = (old_ssd + new_ssd + (n_past / float(n_new * n_total)) * (n_new * mu - n_new * new_mu) ** 2) total_var = total_ssd / n_total return total_mu, total_var def partial_fit(self, X, y, classes=None, sample_weight=None): """Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance and numerical stability overhead, hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples,) Target values. classes : array-like, shape (n_classes,) List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). .. versionadded:: 0.17 Returns ------- self : object Returns self. """ return self._partial_fit(X, y, classes, _refit=False, sample_weight=sample_weight) def _partial_fit(self, X, y, classes=None, _refit=False, sample_weight=None): """Actual implementation of Gaussian NB fitting. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples,) Target values. classes : array-like, shape (n_classes,) List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. _refit: bool If true, act as though this were the first time we called _partial_fit (ie, throw away any past fitting and start over). sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X, y = check_X_y(X, y) # If the ratio of data variance between dimensions is too small, it # will cause numerical errors. To address this, we artificially # boost the variance by epsilon, a small fraction of the standard # deviation of the largest dimension. epsilon = 1e-9 * np.var(X, axis=0).max() if _refit: self.classes_ = None if _check_partial_fit_first_call(self, classes): # This is the first call to partial_fit: # initialize various cumulative counters n_features = X.shape[1] n_classes = len(self.classes_) self.theta_ = np.zeros((n_classes, n_features)) self.sigma_ = np.zeros((n_classes, n_features)) self.class_prior_ = np.zeros(n_classes) self.class_count_ = np.zeros(n_classes) else: if X.shape[1] != self.theta_.shape[1]: msg = "Number of features %d does not match previous data %d." raise ValueError(msg % (X.shape[1], self.theta_.shape[1])) # Put epsilon back in each time self.sigma_[:, :] -= epsilon classes = self.classes_ unique_y = np.unique(y) unique_y_in_classes = in1d(unique_y, classes) if not np.all(unique_y_in_classes): raise ValueError("The target label(s) %s in y do not exist in the " "initial classes %s" % (y[~unique_y_in_classes], classes)) for y_i in unique_y: i = classes.searchsorted(y_i) X_i = X[y == y_i, :] if sample_weight is not None: sw_i = sample_weight[y == y_i] N_i = sw_i.sum() else: sw_i = None N_i = X_i.shape[0] new_theta, new_sigma = self._update_mean_variance( self.class_count_[i], self.theta_[i, :], self.sigma_[i, :], X_i, sw_i) self.theta_[i, :] = new_theta self.sigma_[i, :] = new_sigma self.class_count_[i] += N_i self.sigma_[:, :] += epsilon self.class_prior_[:] = self.class_count_ / np.sum(self.class_count_) return self def _joint_log_likelihood(self, X): check_is_fitted(self, "classes_") X = check_array(X) joint_log_likelihood = [] for i in range(np.size(self.classes_)): jointi = np.log(self.class_prior_[i]) n_ij = - 0.5 * np.sum(np.log(2. * np.pi * self.sigma_[i, :])) n_ij -= 0.5 * np.sum(((X - self.theta_[i, :]) ** 2) / (self.sigma_[i, :]), 1) joint_log_likelihood.append(jointi + n_ij) joint_log_likelihood = np.array(joint_log_likelihood).T return joint_log_likelihood class BaseDiscreteNB(BaseNB): """Abstract base class for naive Bayes on discrete/categorical data Any estimator based on this class should provide: __init__ _joint_log_likelihood(X) as per BaseNB """ def _update_class_log_prior(self, class_prior=None): n_classes = len(self.classes_) if class_prior is not None: if len(class_prior) != n_classes: raise ValueError("Number of priors must match number of" " classes.") self.class_log_prior_ = np.log(class_prior) elif self.fit_prior: # empirical prior, with sample_weight taken into account self.class_log_prior_ = (np.log(self.class_count_) - np.log(self.class_count_.sum())) else: self.class_log_prior_ = np.zeros(n_classes) - np.log(n_classes) def partial_fit(self, X, y, classes=None, sample_weight=None): """Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance overhead hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead. Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. classes : array-like, shape = [n_classes] List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. sample_weight : array-like, shape = [n_samples], optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X = check_array(X, accept_sparse='csr', dtype=np.float64) _, n_features = X.shape if _check_partial_fit_first_call(self, classes): # This is the first call to partial_fit: # initialize various cumulative counters n_effective_classes = len(classes) if len(classes) > 1 else 2 self.class_count_ = np.zeros(n_effective_classes, dtype=np.float64) self.feature_count_ = np.zeros((n_effective_classes, n_features), dtype=np.float64) elif n_features != self.coef_.shape[1]: msg = "Number of features %d does not match previous data %d." raise ValueError(msg % (n_features, self.coef_.shape[-1])) Y = label_binarize(y, classes=self.classes_) if Y.shape[1] == 1: Y = np.concatenate((1 - Y, Y), axis=1) n_samples, n_classes = Y.shape if X.shape[0] != Y.shape[0]: msg = "X.shape[0]=%d and y.shape[0]=%d are incompatible." raise ValueError(msg % (X.shape[0], y.shape[0])) # label_binarize() returns arrays with dtype=np.int64. # We convert it to np.float64 to support sample_weight consistently Y = Y.astype(np.float64) if sample_weight is not None: sample_weight = np.atleast_2d(sample_weight) Y *= check_array(sample_weight).T class_prior = self.class_prior # Count raw events from data before updating the class log prior # and feature log probas self._count(X, Y) # XXX: OPTIM: we could introduce a public finalization method to # be called by the user explicitly just once after several consecutive # calls to partial_fit and prior any call to predict[_[log_]proba] # to avoid computing the smooth log probas at each call to partial fit self._update_feature_log_prob() self._update_class_log_prior(class_prior=class_prior) return self def fit(self, X, y, sample_weight=None): """Fit Naive Bayes classifier according to X, y Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. sample_weight : array-like, shape = [n_samples], optional Weights applied to individual samples (1. for unweighted). Returns ------- self : object Returns self. """ X, y = check_X_y(X, y, 'csr') _, n_features = X.shape labelbin = LabelBinarizer() Y = labelbin.fit_transform(y) self.classes_ = labelbin.classes_ if Y.shape[1] == 1: Y = np.concatenate((1 - Y, Y), axis=1) # LabelBinarizer().fit_transform() returns arrays with dtype=np.int64. # We convert it to np.float64 to support sample_weight consistently; # this means we also don't have to cast X to floating point Y = Y.astype(np.float64) if sample_weight is not None: sample_weight = np.atleast_2d(sample_weight) Y *= check_array(sample_weight).T class_prior = self.class_prior # Count raw events from data before updating the class log prior # and feature log probas n_effective_classes = Y.shape[1] self.class_count_ = np.zeros(n_effective_classes, dtype=np.float64) self.feature_count_ = np.zeros((n_effective_classes, n_features), dtype=np.float64) self._count(X, Y) self._update_feature_log_prob() self._update_class_log_prior(class_prior=class_prior) return self # XXX The following is a stopgap measure; we need to set the dimensions # of class_log_prior_ and feature_log_prob_ correctly. def _get_coef(self): return (self.feature_log_prob_[1:] if len(self.classes_) == 2 else self.feature_log_prob_) def _get_intercept(self): return (self.class_log_prior_[1:] if len(self.classes_) == 2 else self.class_log_prior_) coef_ = property(_get_coef) intercept_ = property(_get_intercept) class MultinomialNB(BaseDiscreteNB): """ Naive Bayes classifier for multinomial models The multinomial Naive Bayes classifier is suitable for classification with discrete features (e.g., word counts for text classification). The multinomial distribution normally requires integer feature counts. However, in practice, fractional counts such as tf-idf may also work. Read more in the :ref:`User Guide <multinomial_naive_bayes>`. Parameters ---------- alpha : float, optional (default=1.0) Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). fit_prior : boolean Whether to learn class prior probabilities or not. If false, a uniform prior will be used. class_prior : array-like, size (n_classes,) Prior probabilities of the classes. If specified the priors are not adjusted according to the data. Attributes ---------- class_log_prior_ : array, shape (n_classes, ) Smoothed empirical log probability for each class. intercept_ : property Mirrors ``class_log_prior_`` for interpreting MultinomialNB as a linear model. feature_log_prob_ : array, shape (n_classes, n_features) Empirical log probability of features given a class, ``P(x_i|y)``. coef_ : property Mirrors ``feature_log_prob_`` for interpreting MultinomialNB as a linear model. class_count_ : array, shape (n_classes,) Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided. feature_count_ : array, shape (n_classes, n_features) Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided. Examples -------- >>> import numpy as np >>> X = np.random.randint(5, size=(6, 100)) >>> y = np.array([1, 2, 3, 4, 5, 6]) >>> from sklearn.naive_bayes import MultinomialNB >>> clf = MultinomialNB() >>> clf.fit(X, y) MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True) >>> print(clf.predict(X[2:3])) [3] Notes ----- For the rationale behind the names `coef_` and `intercept_`, i.e. naive Bayes as a linear classifier, see J. Rennie et al. (2003), Tackling the poor assumptions of naive Bayes text classifiers, ICML. References ---------- C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265. http://nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html """ def __init__(self, alpha=1.0, fit_prior=True, class_prior=None): self.alpha = alpha self.fit_prior = fit_prior self.class_prior = class_prior def _count(self, X, Y): """Count and smooth feature occurrences.""" if np.any((X.data if issparse(X) else X) < 0): raise ValueError("Input X must be non-negative") self.feature_count_ += safe_sparse_dot(Y.T, X) self.class_count_ += Y.sum(axis=0) def _update_feature_log_prob(self): """Apply smoothing to raw counts and recompute log probabilities""" smoothed_fc = self.feature_count_ + self.alpha smoothed_cc = smoothed_fc.sum(axis=1) self.feature_log_prob_ = (np.log(smoothed_fc) - np.log(smoothed_cc.reshape(-1, 1))) def _joint_log_likelihood(self, X): """Calculate the posterior log probability of the samples X""" check_is_fitted(self, "classes_") X = check_array(X, accept_sparse='csr') return (safe_sparse_dot(X, self.feature_log_prob_.T) + self.class_log_prior_) class BernoulliNB(BaseDiscreteNB): """Naive Bayes classifier for multivariate Bernoulli models. Like MultinomialNB, this classifier is suitable for discrete data. The difference is that while MultinomialNB works with occurrence counts, BernoulliNB is designed for binary/boolean features. Read more in the :ref:`User Guide <bernoulli_naive_bayes>`. Parameters ---------- alpha : float, optional (default=1.0) Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). binarize : float or None, optional Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors. fit_prior : boolean Whether to learn class prior probabilities or not. If false, a uniform prior will be used. class_prior : array-like, size=[n_classes,] Prior probabilities of the classes. If specified the priors are not adjusted according to the data. Attributes ---------- class_log_prior_ : array, shape = [n_classes] Log probability of each class (smoothed). feature_log_prob_ : array, shape = [n_classes, n_features] Empirical log probability of features given a class, P(x_i|y). class_count_ : array, shape = [n_classes] Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided. feature_count_ : array, shape = [n_classes, n_features] Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided. Examples -------- >>> import numpy as np >>> X = np.random.randint(2, size=(6, 100)) >>> Y = np.array([1, 2, 3, 4, 4, 5]) >>> from sklearn.naive_bayes import BernoulliNB >>> clf = BernoulliNB() >>> clf.fit(X, Y) BernoulliNB(alpha=1.0, binarize=0.0, class_prior=None, fit_prior=True) >>> print(clf.predict(X[2:3])) [3] References ---------- C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265. http://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html A. McCallum and K. Nigam (1998). A comparison of event models for naive Bayes text classification. Proc. AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41-48. V. Metsis, I. Androutsopoulos and G. Paliouras (2006). Spam filtering with naive Bayes -- Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS). """ def __init__(self, alpha=1.0, binarize=.0, fit_prior=True, class_prior=None): self.alpha = alpha self.binarize = binarize self.fit_prior = fit_prior self.class_prior = class_prior def _count(self, X, Y): """Count and smooth feature occurrences.""" if self.binarize is not None: X = binarize(X, threshold=self.binarize) self.feature_count_ += safe_sparse_dot(Y.T, X) self.class_count_ += Y.sum(axis=0) def _update_feature_log_prob(self): """Apply smoothing to raw counts and recompute log probabilities""" smoothed_fc = self.feature_count_ + self.alpha smoothed_cc = self.class_count_ + self.alpha * 2 self.feature_log_prob_ = (np.log(smoothed_fc) - np.log(smoothed_cc.reshape(-1, 1))) def _joint_log_likelihood(self, X): """Calculate the posterior log probability of the samples X""" check_is_fitted(self, "classes_") X = check_array(X, accept_sparse='csr') if self.binarize is not None: X = binarize(X, threshold=self.binarize) n_classes, n_features = self.feature_log_prob_.shape n_samples, n_features_X = X.shape if n_features_X != n_features: raise ValueError("Expected input with %d features, got %d instead" % (n_features, n_features_X)) neg_prob = np.log(1 - np.exp(self.feature_log_prob_)) # Compute neg_prob · (1 - X).T as ∑neg_prob - X · neg_prob jll = safe_sparse_dot(X, (self.feature_log_prob_ - neg_prob).T) jll += self.class_log_prior_ + neg_prob.sum(axis=1) return jll
bsd-3-clause
jamesp/Isca
src/extra/python/scripts/vert_coord_options.py
4
3719
import numpy as np import matplotlib.pyplot as plt def even_sigma_calc(num_levels): "The even sigma calculation just divides the atmosphere up into equal sigma increments between 1 and 0. So the height of the model is really set by your number of levels, as the higher the number of levels you have, the smaller your increment will be between 0hPa at the top and whatever your next level down is." num_levels_calc = num_levels -1 b=np.zeros(num_levels) for k in np.arange(1,num_levels): b[k-1] = (k-1.)/num_levels_calc b[num_levels-1]=1. p_half = b flipped_p_half = p_half[::-1] #Note that this is the opposite convention to the model, but done for visual clarity in plots. return flipped_p_half def uneven_sigma_calc(num_levels, surf_res, exponent, scale_heights): "The uneven sigma calculation first splits up the atmosphere into equal increments between 0 and 1, and then does different vertical spacings depending on the parameters. For example, if surf_res = 1 then you get even spacing in height. If surf_res = 0 then you get a height depending on zeta**exponent. For surf_res in between, you get a mix of the two. scale_heights sets the model top height, and exponent determines how heavily biased your level spacings are towards the troposphere. Larger exponent values are more tropospherically biased. " num_levels_calc = num_levels -1 b=np.zeros(num_levels) for k in np.arange(1,num_levels): zeta = 1. - (k-1.)/num_levels_calc Z = surf_res*zeta + (1. - surf_res)*np.power(zeta,exponent) b[k-1] = np.exp(-Z*scale_heights) b[num_levels-1]=1. # b[0]=0. p_half = b flipped_p_half = p_half[::-1] #Note that this is the opposite convention to the model, but done for visual clarity in plots. return flipped_p_half def p_half_to_p_full(p_half, num_levels): p_full = np.zeros(num_levels-1) # 0 to num_levels-1 is so that we go through all p_half[k], but we can't have p_half[k+1] with a top number of num_levels-1, so must be num_levels-2. BUT because np.arange doesn't include the end point, we use num_levels-1. for k in np.arange(0,num_levels-1): alpha = 1.0 - p_half[k]*( np.log(p_half[k+1]) - np.log(p_half[k]) )/ (p_half[k+1] - p_half[k]) p_full[k] = p_half[k+1] * np.exp(-alpha) return p_full if __name__ == "__main__": num_levels = 41 #number of half levels vert_coord_option = "uneven_sigma" surf_res = 0.5 scale_heights = 11.0 exponent = 7.0 if vert_coord_option == "uneven_sigma": plt.figure(1) for surf_res in [0., 0.2, 0.4, 0.5, 0.6, 0.8, 1.0]: p_half = uneven_sigma_calc(num_levels, surf_res, exponent, scale_heights) p_full = p_half_to_p_full(p_half, num_levels) plt.plot(-np.log(p_half), label=surf_res) plt.legend(loc='upper left') plt.title('Uneven sigma, varying surf_res') plt.figure(2) for scale_heights in [6., 8., 10., 12.]: surf_res = 1.0 p_half = uneven_sigma_calc(num_levels, surf_res, exponent, scale_heights) p_full = p_half_to_p_full(p_half, num_levels) plt.plot(-np.log(p_half), label=scale_heights) plt.legend(loc='upper left') plt.title('Uneven sigma, varying scale_heights') plt.figure(3) for exponent in [2., 4., 6., 7., 8., 10., 12.]: surf_res = 0. scale_heights = 11. p_half = uneven_sigma_calc(num_levels, surf_res, exponent, scale_heights) p_full = p_half_to_p_full(p_half, num_levels) plt.plot(-np.log(p_half), label=exponent) plt.legend(loc='upper left') plt.title('Uneven sigma, varying exponent') plt.show() if vert_coord_option == "even_sigma": p_half = even_sigma_calc(num_levels) p_full = p_half_to_p_full(p_half, num_levels) plt.plot((p_half)) plt.show()
gpl-3.0
aayushidwivedi01/spark-tk
regression-tests/sparktkregtests/testcases/scoretests/logistic_regression_test.py
12
2456
# vim: set encoding=utf-8 # Copyright (c) 2016 Intel Corporation  # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #       http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ Tests Logistic Regression scoring engine """ import unittest import os from sparktkregtests.lib import sparktk_test from sparktkregtests.lib import scoring_utils class LogisticRegression(sparktk_test.SparkTKTestCase): def setUp(self): """Build test frame""" super(LogisticRegression, self).setUp() binomial_dataset = self.get_file("small_logit_binary.csv") schema = [("vec0", float), ("vec1", float), ("vec2", float), ("vec3", float), ("vec4", float), ("res", int), ("count", int), ("actual", int)] self.frame = self.context.frame.import_csv( binomial_dataset, schema=schema, header=True) def test_model_scoring(self): """Test publishing a logistic regression model""" model = self.context.models.classification.logistic_regression.train( self.frame, ["vec0", "vec1", "vec2", "vec3", "vec4"], 'res') predict = model.predict( self.frame, ["vec0", "vec1", "vec2", "vec3", "vec4"]) test_rows = predict.to_pandas(100) file_name = self.get_name("logistic_regression") model_path = model.export_to_mar(self.get_export_file(file_name)) with scoring_utils.scorer( model_path, self.id()) as scorer: for i, row in test_rows.iterrows(): res = scorer.score( [dict(zip(["vec0", "vec1", "vec2", "vec3", "vec4"], list(row[0:5])))]) self.assertEqual( row["predicted_label"], res.json()["data"][0]['PredictedLabel']) if __name__ == '__main__': unittest.main()
apache-2.0
bowenliu16/deepchem
deepchem/dock/tests/test_pose_scoring.py
1
1917
""" Tests for Pose Scoring """ from __future__ import print_function from __future__ import division from __future__ import unicode_literals __author__ = "Bharath Ramsundar" __copyright__ = "Copyright 2016, Stanford University" __license__ = "GPL" import sys import unittest import tempfile import os import shutil import numpy as np import deepchem as dc from sklearn.ensemble import RandomForestRegressor from subprocess import call class TestPoseScoring(unittest.TestCase): """ Does sanity checks on pose generation. """ def setUp(self): """Downloads dataset.""" call("wget -c http://deepchem.io.s3-website-us-west-1.amazonaws.com/featurized_datasets/core_grid.tar.gz".split()) call("tar -zxvf core_grid.tar.gz".split()) self.core_dataset = dc.data.DiskDataset("core_grid/") def tearDown(self): """Removes dataset""" call("rm -rf core_grid/".split()) def test_pose_scorer_init(self): """Tests that pose-score works.""" if sys.version_info >= (3,0): return sklearn_model = RandomForestRegressor(n_estimators=10) model = dc.models.SklearnModel(sklearn_model) print("About to fit model on core set") model.fit(self.core_dataset) pose_scorer = dc.dock.GridPoseScorer(model, feat="grid") def test_pose_scorer_score(self): """Tests that scores are generated""" if sys.version_info >= (3,0): return current_dir = os.path.dirname(os.path.realpath(__file__)) protein_file = os.path.join(current_dir, "1jld_protein.pdb") ligand_file = os.path.join(current_dir, "1jld_ligand.sdf") sklearn_model = RandomForestRegressor(n_estimators=10) model = dc.models.SklearnModel(sklearn_model) print("About to fit model on core set") model.fit(self.core_dataset) pose_scorer = dc.dock.GridPoseScorer(model, feat="grid") score = pose_scorer.score(protein_file, ligand_file) assert score.shape == (1,)
gpl-3.0
huobaowangxi/scikit-learn
sklearn/utils/__init__.py
132
14185
""" The :mod:`sklearn.utils` module includes various utilities. """ from collections import Sequence import numpy as np from scipy.sparse import issparse import warnings from .murmurhash import murmurhash3_32 from .validation import (as_float_array, assert_all_finite, check_random_state, column_or_1d, check_array, check_consistent_length, check_X_y, indexable, check_symmetric, DataConversionWarning) from .class_weight import compute_class_weight, compute_sample_weight from ..externals.joblib import cpu_count __all__ = ["murmurhash3_32", "as_float_array", "assert_all_finite", "check_array", "check_random_state", "compute_class_weight", "compute_sample_weight", "column_or_1d", "safe_indexing", "check_consistent_length", "check_X_y", 'indexable', "check_symmetric"] class deprecated(object): """Decorator to mark a function or class as deprecated. Issue a warning when the function is called/the class is instantiated and adds a warning to the docstring. The optional extra argument will be appended to the deprecation message and the docstring. Note: to use this with the default value for extra, put in an empty of parentheses: >>> from sklearn.utils import deprecated >>> deprecated() # doctest: +ELLIPSIS <sklearn.utils.deprecated object at ...> >>> @deprecated() ... def some_function(): pass """ # Adapted from http://wiki.python.org/moin/PythonDecoratorLibrary, # but with many changes. def __init__(self, extra=''): """ Parameters ---------- extra: string to be added to the deprecation messages """ self.extra = extra def __call__(self, obj): if isinstance(obj, type): return self._decorate_class(obj) else: return self._decorate_fun(obj) def _decorate_class(self, cls): msg = "Class %s is deprecated" % cls.__name__ if self.extra: msg += "; %s" % self.extra # FIXME: we should probably reset __new__ for full generality init = cls.__init__ def wrapped(*args, **kwargs): warnings.warn(msg, category=DeprecationWarning) return init(*args, **kwargs) cls.__init__ = wrapped wrapped.__name__ = '__init__' wrapped.__doc__ = self._update_doc(init.__doc__) wrapped.deprecated_original = init return cls def _decorate_fun(self, fun): """Decorate function fun""" msg = "Function %s is deprecated" % fun.__name__ if self.extra: msg += "; %s" % self.extra def wrapped(*args, **kwargs): warnings.warn(msg, category=DeprecationWarning) return fun(*args, **kwargs) wrapped.__name__ = fun.__name__ wrapped.__dict__ = fun.__dict__ wrapped.__doc__ = self._update_doc(fun.__doc__) return wrapped def _update_doc(self, olddoc): newdoc = "DEPRECATED" if self.extra: newdoc = "%s: %s" % (newdoc, self.extra) if olddoc: newdoc = "%s\n\n%s" % (newdoc, olddoc) return newdoc def safe_mask(X, mask): """Return a mask which is safe to use on X. Parameters ---------- X : {array-like, sparse matrix} Data on which to apply mask. mask: array Mask to be used on X. Returns ------- mask """ mask = np.asarray(mask) if np.issubdtype(mask.dtype, np.int): return mask if hasattr(X, "toarray"): ind = np.arange(mask.shape[0]) mask = ind[mask] return mask def safe_indexing(X, indices): """Return items or rows from X using indices. Allows simple indexing of lists or arrays. Parameters ---------- X : array-like, sparse-matrix, list. Data from which to sample rows or items. indices : array-like, list Indices according to which X will be subsampled. """ if hasattr(X, "iloc"): # Pandas Dataframes and Series try: return X.iloc[indices] except ValueError: # Cython typed memoryviews internally used in pandas do not support # readonly buffers. warnings.warn("Copying input dataframe for slicing.", DataConversionWarning) return X.copy().iloc[indices] elif hasattr(X, "shape"): if hasattr(X, 'take') and (hasattr(indices, 'dtype') and indices.dtype.kind == 'i'): # This is often substantially faster than X[indices] return X.take(indices, axis=0) else: return X[indices] else: return [X[idx] for idx in indices] def resample(*arrays, **options): """Resample arrays or sparse matrices in a consistent way The default strategy implements one step of the bootstrapping procedure. Parameters ---------- *arrays : sequence of indexable data-structures Indexable data-structures can be arrays, lists, dataframes or scipy sparse matrices with consistent first dimension. replace : boolean, True by default Implements resampling with replacement. If False, this will implement (sliced) random permutations. n_samples : int, None by default Number of samples to generate. If left to None this is automatically set to the first dimension of the arrays. random_state : int or RandomState instance Control the shuffling for reproducible behavior. Returns ------- resampled_arrays : sequence of indexable data-structures Sequence of resampled views of the collections. The original arrays are not impacted. Examples -------- It is possible to mix sparse and dense arrays in the same run:: >>> X = np.array([[1., 0.], [2., 1.], [0., 0.]]) >>> y = np.array([0, 1, 2]) >>> from scipy.sparse import coo_matrix >>> X_sparse = coo_matrix(X) >>> from sklearn.utils import resample >>> X, X_sparse, y = resample(X, X_sparse, y, random_state=0) >>> X array([[ 1., 0.], [ 2., 1.], [ 1., 0.]]) >>> X_sparse # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE <3x2 sparse matrix of type '<... 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format> >>> X_sparse.toarray() array([[ 1., 0.], [ 2., 1.], [ 1., 0.]]) >>> y array([0, 1, 0]) >>> resample(y, n_samples=2, random_state=0) array([0, 1]) See also -------- :func:`sklearn.utils.shuffle` """ random_state = check_random_state(options.pop('random_state', None)) replace = options.pop('replace', True) max_n_samples = options.pop('n_samples', None) if options: raise ValueError("Unexpected kw arguments: %r" % options.keys()) if len(arrays) == 0: return None first = arrays[0] n_samples = first.shape[0] if hasattr(first, 'shape') else len(first) if max_n_samples is None: max_n_samples = n_samples if max_n_samples > n_samples: raise ValueError("Cannot sample %d out of arrays with dim %d" % ( max_n_samples, n_samples)) check_consistent_length(*arrays) if replace: indices = random_state.randint(0, n_samples, size=(max_n_samples,)) else: indices = np.arange(n_samples) random_state.shuffle(indices) indices = indices[:max_n_samples] # convert sparse matrices to CSR for row-based indexing arrays = [a.tocsr() if issparse(a) else a for a in arrays] resampled_arrays = [safe_indexing(a, indices) for a in arrays] if len(resampled_arrays) == 1: # syntactic sugar for the unit argument case return resampled_arrays[0] else: return resampled_arrays def shuffle(*arrays, **options): """Shuffle arrays or sparse matrices in a consistent way This is a convenience alias to ``resample(*arrays, replace=False)`` to do random permutations of the collections. Parameters ---------- *arrays : sequence of indexable data-structures Indexable data-structures can be arrays, lists, dataframes or scipy sparse matrices with consistent first dimension. random_state : int or RandomState instance Control the shuffling for reproducible behavior. n_samples : int, None by default Number of samples to generate. If left to None this is automatically set to the first dimension of the arrays. Returns ------- shuffled_arrays : sequence of indexable data-structures Sequence of shuffled views of the collections. The original arrays are not impacted. Examples -------- It is possible to mix sparse and dense arrays in the same run:: >>> X = np.array([[1., 0.], [2., 1.], [0., 0.]]) >>> y = np.array([0, 1, 2]) >>> from scipy.sparse import coo_matrix >>> X_sparse = coo_matrix(X) >>> from sklearn.utils import shuffle >>> X, X_sparse, y = shuffle(X, X_sparse, y, random_state=0) >>> X array([[ 0., 0.], [ 2., 1.], [ 1., 0.]]) >>> X_sparse # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE <3x2 sparse matrix of type '<... 'numpy.float64'>' with 3 stored elements in Compressed Sparse Row format> >>> X_sparse.toarray() array([[ 0., 0.], [ 2., 1.], [ 1., 0.]]) >>> y array([2, 1, 0]) >>> shuffle(y, n_samples=2, random_state=0) array([0, 1]) See also -------- :func:`sklearn.utils.resample` """ options['replace'] = False return resample(*arrays, **options) def safe_sqr(X, copy=True): """Element wise squaring of array-likes and sparse matrices. Parameters ---------- X : array like, matrix, sparse matrix copy : boolean, optional, default True Whether to create a copy of X and operate on it or to perform inplace computation (default behaviour). Returns ------- X ** 2 : element wise square """ X = check_array(X, accept_sparse=['csr', 'csc', 'coo']) if issparse(X): if copy: X = X.copy() X.data **= 2 else: if copy: X = X ** 2 else: X **= 2 return X def gen_batches(n, batch_size): """Generator to create slices containing batch_size elements, from 0 to n. The last slice may contain less than batch_size elements, when batch_size does not divide n. Examples -------- >>> from sklearn.utils import gen_batches >>> list(gen_batches(7, 3)) [slice(0, 3, None), slice(3, 6, None), slice(6, 7, None)] >>> list(gen_batches(6, 3)) [slice(0, 3, None), slice(3, 6, None)] >>> list(gen_batches(2, 3)) [slice(0, 2, None)] """ start = 0 for _ in range(int(n // batch_size)): end = start + batch_size yield slice(start, end) start = end if start < n: yield slice(start, n) def gen_even_slices(n, n_packs, n_samples=None): """Generator to create n_packs slices going up to n. Pass n_samples when the slices are to be used for sparse matrix indexing; slicing off-the-end raises an exception, while it works for NumPy arrays. Examples -------- >>> from sklearn.utils import gen_even_slices >>> list(gen_even_slices(10, 1)) [slice(0, 10, None)] >>> list(gen_even_slices(10, 10)) #doctest: +ELLIPSIS [slice(0, 1, None), slice(1, 2, None), ..., slice(9, 10, None)] >>> list(gen_even_slices(10, 5)) #doctest: +ELLIPSIS [slice(0, 2, None), slice(2, 4, None), ..., slice(8, 10, None)] >>> list(gen_even_slices(10, 3)) [slice(0, 4, None), slice(4, 7, None), slice(7, 10, None)] """ start = 0 if n_packs < 1: raise ValueError("gen_even_slices got n_packs=%s, must be >=1" % n_packs) for pack_num in range(n_packs): this_n = n // n_packs if pack_num < n % n_packs: this_n += 1 if this_n > 0: end = start + this_n if n_samples is not None: end = min(n_samples, end) yield slice(start, end, None) start = end def _get_n_jobs(n_jobs): """Get number of jobs for the computation. This function reimplements the logic of joblib to determine the actual number of jobs depending on the cpu count. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used. Parameters ---------- n_jobs : int Number of jobs stated in joblib convention. Returns ------- n_jobs : int The actual number of jobs as positive integer. Examples -------- >>> from sklearn.utils import _get_n_jobs >>> _get_n_jobs(4) 4 >>> jobs = _get_n_jobs(-2) >>> assert jobs == max(cpu_count() - 1, 1) >>> _get_n_jobs(0) Traceback (most recent call last): ... ValueError: Parameter n_jobs == 0 has no meaning. """ if n_jobs < 0: return max(cpu_count() + 1 + n_jobs, 1) elif n_jobs == 0: raise ValueError('Parameter n_jobs == 0 has no meaning.') else: return n_jobs def tosequence(x): """Cast iterable x to a Sequence, avoiding a copy if possible.""" if isinstance(x, np.ndarray): return np.asarray(x) elif isinstance(x, Sequence): return x else: return list(x) class ConvergenceWarning(UserWarning): """Custom warning to capture convergence problems""" class DataDimensionalityWarning(UserWarning): """Custom warning to notify potential issues with data dimensionality"""
bsd-3-clause