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
matthiasplappert/motion_classification
src/toolkit/hmm/impl_hmmlearn.py
1
30715
import itertools import logging import numpy as np from sklearn.mixture.gmm import log_multivariate_normal_density, sample_gaussian from sklearn.utils.extmath import logsumexp import hmmlearn.hmm as impl import hmmlearn.fhmmc as fhmmc from hmmlearn.utils import normalize from .base import (BaseHMM, transition_matrix, start_probabilities, estimate_normal_distribution_params) def _new_model(n_states, transition_init, means, covars, covar_type, topology, n_iter, thresh, verbose): # Generate transition matrix if transition_init == 'uniform': transitions = transition_matrix(n_states, topology, randomize=False) pi = start_probabilities(n_states, topology, randomize=False) elif transition_init == 'random': transitions = transition_matrix(n_states, topology, randomize=True) pi = start_probabilities(n_states, topology, randomize=True) else: raise ValueError('unknown initialization strategy %s' % transition_init) # Create a model that trains mean (m), covariance (c), transition probabilities (t). # Note: the probabilities for transmat and startprob will currently be replaced with a # very small number for all entries that are exactly zero. logging.info('creating HMM with n_states=%d, transition_init=%s, topology=%s, n_iter=%d, thresh=%f, covar_type=%s' % (n_states, transition_init, topology, n_iter, thresh, covar_type)) logging.info('transmat:\n' + str(transitions)) logging.info('pi:\n' + str(pi)) logging.info('means:\n' + str(means)) logging.info('covars:\n' + str(covars)) model = impl.GaussianHMM(n_states, transmat=transitions, startprob=pi, covariance_type=covar_type, params='mct', init_params='', verbose=verbose, n_iter=n_iter, thresh=thresh) if covar_type == 'diag': model.covars_ = [covar.diagonal() for covar in covars] else: model.covars_ = covars model.means_ = means model.verbose = True return model def greedy_sample(chains, means, covars, n_samples=1, random_state=None, max_cycle_duration=0): assert means.shape[0] == covars.shape[0] # Greedy sample algorithm as described by Takano et al. n_chains = len(chains) n_features = means.shape[-1] states = np.zeros((n_samples, n_chains), dtype=int) for chain_idx, chain in enumerate(chains): states[0, chain_idx] = np.argmax(chain._log_startprob) t = 1 while t < n_samples: prev_state = states[t - 1, chain_idx] # Stay in state until duration is over or until we reach the n_samples limit trans_prob_cycle = np.exp(chain._log_transmat[prev_state, prev_state]) if trans_prob_cycle == 1.0: trans_prob_cycle -= np.finfo(float).eps assert 0.0 <= trans_prob_cycle < 1.0 duration = int(np.floor(min(min(1.0 / (1.0 - trans_prob_cycle), n_samples-t), max_cycle_duration))) for d in xrange(duration): states[t + d, chain_idx] = prev_state t += duration if t >= n_samples: continue # Get argmax of transition probability of previous state but ignore transition from prev_state -> prev_state state = None for idx, val in enumerate(chain._log_transmat[prev_state]): if idx != prev_state and (state is None or val > chain._log_transmat[prev_state, state]): state = idx assert state is not None assert state != prev_state states[t, chain_idx] = state t += 1 obs = np.zeros((n_samples, n_features)) for t in xrange(n_samples): state_combination = tuple(states[t]) mean = means[state_combination] covar = covars[state_combination] obs[t] = sample_gaussian(mean, covar, 'diag', random_state=random_state) return obs class GaussianHMM(BaseHMM): def __init__(self, n_states=10, n_training_iterations=10, training_threshold=1e-2, topology='left-to-right', verbose=False, transition_init='uniform', emission_init='k-means', covar_type='diag'): super(GaussianHMM, self).__init__(n_states, n_training_iterations, training_threshold, topology, verbose, transition_init, emission_init, covar_type) self.model_ = None def _init(self, obs): randomize = True if self.emission_init == 'random' else False means, covars = estimate_normal_distribution_params(obs, n_states=self.n_states, covar_type=self.covar_type, randomize=randomize) self.model_ = _new_model(self.n_states, self.transition_init, means, covars, self.covar_type, self.topology, self.n_training_iterations, self.training_threshold, self.verbose) def _fit(self, obs): assert self.model_ self.model_.fit(obs) def _loglikelihood(self, ob, method): if method != 'exact': raise ValueError('unknown method "%s"' % method) assert self.model_ return self.model_.score(ob) def _sample(self, n_samples, max_cycle_duration): assert self.model_ return greedy_sample([self.model_], self.model_._means_, self.model_._covars_, n_samples=n_samples, max_cycle_duration=max_cycle_duration) # TODO: normalize seems to also do some sort of maximum thingy # TODO: implement masking! def _normalize_transmat(transmat): return normalize(np.maximum(transmat, 1e-20), axis=1) def _normalize_startprob(startprob): return normalize(np.maximum(startprob, 1e-20)) class ExactGaussianFHMM(BaseHMM): def __init__(self, n_states=10, n_chains=2, n_training_iterations=10, training_threshold=1e-2, topology='left-to-right', verbose=False, transition_init='uniform', emission_init='k-means', covar_type='diag'): super(ExactGaussianFHMM, self).__init__(n_states, n_training_iterations, training_threshold, topology, verbose, transition_init, emission_init, covar_type) self.n_chains = n_chains def _init(self, obs): self.log_transmat = np.zeros((self.n_chains, self.n_states, self.n_states)) self.log_startprob = np.zeros((self.n_chains, self.n_states)) for chain_idx in xrange(self.n_chains): self.log_transmat[chain_idx] = np.log(_normalize_transmat(transition_matrix(self.n_states, self.topology))) self.log_startprob[chain_idx] = np.log(_normalize_startprob(start_probabilities(self.n_states, self.topology))) # Estimate covar _, covars = estimate_normal_distribution_params(obs, n_states=1, covar_type='full') self.covar = covars[0] # Estimate means. We estimate different means for each chain and each state. Each mean is divided by the number # of chains since means from each chain are summed to form the actual mean. means, _ = estimate_normal_distribution_params(obs, n_states=self.n_states * self.n_chains) self.means = (means.reshape((self.n_chains, self.n_states, self.n_features_)) / float(self.n_chains)) def _do_forward_pass(self, framelogprob): n_observations = framelogprob.shape[0] state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))] fwdlattice = np.zeros((n_observations, self.n_states ** self.n_chains)) fhmmc._forward(n_observations, self.n_chains, self.n_states, state_combinations, self.log_startprob, self.log_transmat, framelogprob, fwdlattice) return logsumexp(fwdlattice[-1]), fwdlattice def _do_backward_pass(self, framelogprob): n_observations = framelogprob.shape[0] state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))] bwdlattice = np.zeros((n_observations, self.n_states ** self.n_chains)) fhmmc._backward(n_observations, self.n_chains, self.n_states, state_combinations, self.log_startprob, self.log_transmat, framelogprob, bwdlattice) return bwdlattice def _compute_logeta(self, framelogprob, fwdlattice, bwdlattice): n_observations = framelogprob.shape[0] state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))] logeta = np.zeros((n_observations - 1, self.n_chains, self.n_states, self.n_states)) fhmmc._compute_logeta(n_observations, self.n_chains, self.n_states, state_combinations, self.log_transmat, framelogprob, fwdlattice, bwdlattice, logeta) # TODO: remove this validation eventually for t in xrange(n_observations - 1): for chain_idx in xrange(self.n_chains): assert np.allclose(np.sum(np.exp(logeta[t, chain_idx])), 1.0) return logeta def _do_mstep(self, stats): # Startprob and transmat for chain_idx in xrange(self.n_chains): self.log_startprob[chain_idx] = np.log(_normalize_startprob(stats['start'][chain_idx])) self.log_transmat[chain_idx] = np.log(_normalize_transmat(stats['trans'][chain_idx])) # Means print 'means equal per chain before', np.allclose(self.means[0], self.means[1]) means = np.dot(stats['means_sum1'], np.linalg.pinv(stats['means_sum2'])).T means = means.reshape(self.means.shape) self.means = means print 'means equal per chain after', np.allclose(means[0], means[1]) # Covariance covar1 = 1.0 / float(stats['T']) * (stats['covar_sum1'] - stats['covar_sum2']) # Alternative way of calculating covar covar2 = (-2.0 * stats['covar_sum']) / float(stats['T']) print 'covar1 == covar2', np.allclose(covar1, covar2) #self.covar = covar1 assert np.allclose(self.covar.T, self.covar) # ensure that covar is symmetric def _accumulate_sufficient_statistics(self, stats, seq, framelogprob, in_posteriors, fwdlattice, bwdlattice): n_observations, n_features = seq.shape partial_state_combinations = [list(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains - 1))] posteriors = in_posteriors.view() state_combination_shape = tuple([self.n_states for _ in xrange(self.n_chains)]) posteriors.shape = (n_observations,) + state_combination_shape # Calculate posteriors for each time step and each chain (<S_t^(m)>) chain_posteriors = np.zeros((n_observations, self.n_chains, self.n_states)) for t in xrange(n_observations): for chain_idx in xrange(self.n_chains): for state in xrange(self.n_states): for partial_combination in partial_state_combinations: state_combination = tuple(partial_combination[:chain_idx] + [state] + partial_combination[chain_idx:]) chain_posteriors[t, chain_idx, state] += posteriors[t][state_combination] # Ensure that posteriors for each chain sum to 1 assert np.allclose(np.sum(chain_posteriors[t], axis=1), 1.0) chain_chain_posteriors = np.zeros((n_observations, self.n_chains, self.n_chains, self.n_states, self.n_states)) assert self.n_chains == 2, 'This code currently only works for 2 chains' for t in xrange(n_observations): for chain0_idx in xrange(self.n_chains): for chain1_idx in xrange(self.n_chains): for state0 in xrange(self.n_states): for state1 in xrange(self.n_states): # Now keep state0 and state1 fixed and vary the rest-in the case of only two chains, this # however means that we cannot vary anything chain_chain_posteriors[t, chain0_idx, chain1_idx, state0, state1] = posteriors[t, state0, state1] assert np.allclose(np.sum(chain_chain_posteriors[t, chain0_idx, chain1_idx]), 1.0) combined_chain_chain_posteriors = np.zeros((n_observations, self.n_chains * self.n_states, self.n_chains * self.n_states)) for t in xrange(n_observations): for chain0_idx in xrange(self.n_chains): for chain1_idx in xrange(self.n_chains): post = chain_chain_posteriors[t, chain0_idx, chain1_idx] assert post.shape == (self.n_states, self.n_states) idx0 = chain0_idx * self.n_states idx1 = chain1_idx * self.n_states assert combined_chain_chain_posteriors[t, idx0:idx0+self.n_states, idx1:idx1+self.n_states].shape == (self.n_states, self.n_states) assert np.allclose(combined_chain_chain_posteriors[t, idx0:idx0+self.n_states, idx1:idx1+self.n_states], 0) combined_chain_chain_posteriors[t, idx0:idx0+self.n_states, idx1:idx1+self.n_states] = post #combined_chain_chain_posteriors = chain_chain_posteriors.reshape(n_observations, self.n_states * self.n_chains, self.n_states * self.n_chains) # Calculate posteriors for each time step and each chain combination (<S_t^(m),S_t^(n)'>) # TODO: this is super sketchy code right here # start = timeit.default_timer() # chain_chain_posteriors = np.zeros((n_observations, self.n_chains, self.n_chains, self.n_states, self.n_states)) # steps = 0 # skipped = 0 # for t in xrange(n_observations): # for chain0_idx in xrange(self.n_chains): # for chain1_idx in xrange(self.n_chains): # processed_state_combinations = set() # TODO: is this correct? # for i in xrange(self.n_states): # for j in xrange(self.n_states): # for state_combination in state_combinations: # actual_state_combination = list(state_combination) # actual_state_combination[chain0_idx] = i # actual_state_combination[chain1_idx] = j # actual_state_combination = tuple(actual_state_combination) # if actual_state_combination in processed_state_combinations: # # Skip states that we have already processed. # # TODO: make this efficient, right now we skip most states # skipped += 1 # continue # steps += 1 # processed_state_combinations.add(actual_state_combination) # chain_chain_posteriors[t, chain0_idx, chain1_idx, i, j] += posteriors[t][actual_state_combination] # if posteriors[t][actual_state_combination] == 0.0: # print '0.0!' # print('took %fs, %d steps (%d)' % ((timeit.default_timer() - start), steps, skipped)) # print np.size(chain_chain_posteriors) # Update stats for start and trans stats['start'] += chain_posteriors[0] logeta = self._compute_logeta(framelogprob, fwdlattice, bwdlattice) for chain_idx in xrange(self.n_chains): # No need to normalize here since we'll do that later anyway stats['trans'][chain_idx] += np.exp(logsumexp(logeta[:, chain_idx], axis=0)) # Update stats for means for t in xrange(n_observations): ob = seq[t].reshape(n_features, 1) post = chain_posteriors[t].flatten().reshape(1, self.n_chains * self.n_states) val1 = np.dot(ob, post) assert val1.shape == stats['means_sum1'].shape stats['means_sum1'] += val1 val2 = combined_chain_chain_posteriors[t] assert val2.shape == stats['means_sum2'].shape stats['means_sum2'] += val2 new_means = np.dot(stats['means_sum1'], np.linalg.pinv(stats['means_sum2'])).T.reshape(self.means.shape) # Alternative way of calculcating the covariance stats (seems to yield same (bad) results) for t in xrange(n_observations): ob = seq[t].reshape(n_features, 1) sum1 = np.zeros((self.n_features_, self.n_features_)) for chain_idx in xrange(self.n_chains): post = chain_posteriors[t, chain_idx].reshape(self.n_states, 1) val1 = np.dot(np.dot(ob, post.T), self.means[chain_idx]) assert val1.shape == sum1.shape sum1 += val1 sum2 = np.zeros((self.n_features_, self.n_features_)) for chain0_idx in xrange(self.n_chains): for chain1_idx in xrange(self.n_chains): post = chain_chain_posteriors[t, chain1_idx, chain0_idx] val2 = np.dot(np.dot(self.means[chain1_idx].T, post), self.means[chain0_idx]) assert val2.shape == sum2.shape sum2 += val2 covar_sum = sum1 - 0.5 * np.dot(ob, ob.T) - 0.5 * sum2 assert covar_sum.shape == stats['covar_sum'].shape stats['covar_sum'] += covar_sum # Update covariance stats for t in xrange(n_observations): ob = seq[t].reshape(n_features, 1) val1 = np.dot(ob, ob.T) assert val1.shape == stats['covar_sum1'].shape stats['covar_sum1'] += val1 tmp = np.zeros((n_features, 1)) for chain_idx in xrange(self.n_chains): tmp += np.dot(new_means[chain_idx].T, chain_posteriors[t, chain_idx]).reshape((n_features, 1)) val2 = np.dot(tmp, ob.T) assert val2.shape == stats['covar_sum2'].shape stats['covar_sum2'] += val2 # Update bookkeeping stats stats['nobs'] += 1 stats['T'] += n_observations def _initialize_sufficient_statistics(self): stats = {'nobs': 0, 'T': 0, 'start': np.zeros((self.n_chains, self.n_states)), 'trans': np.zeros((self.n_chains, self.n_states, self.n_states)), 'covar_sum1': np.zeros((self.n_features_, self.n_features_)), 'covar_sum2': np.zeros((self.n_features_, self.n_features_)), 'covar_sum': np.zeros((self.n_features_, self.n_features_)), # TODO: remove eventually 'means_sum1': np.zeros((self.n_features_, self.n_states * self.n_chains)), 'means_sum2': np.zeros((self.n_states * self.n_chains, self.n_states * self.n_chains))} return stats def _compute_log_likelihood(self, seq): state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))] n_state_combinations = self.n_states ** self.n_chains n_observations, n_features = seq.shape covars = np.array([self.covar for _ in xrange(n_state_combinations)]) # TODO: correct?! means = np.zeros((n_state_combinations, n_features)) for idx, state_combination in enumerate(state_combinations): for chain_idx, state in enumerate(state_combination): means[idx] += self.means[chain_idx, state] framelogprob = log_multivariate_normal_density(seq, means, covars, covariance_type='full') return framelogprob def _fit(self, obs): prev_loglikelihood = None for iteration in xrange(self.n_training_iterations): stats = self._initialize_sufficient_statistics() curr_loglikelihood = 0 for seq in obs: # Forward-backward pass and accumulate stats framelogprob = self._compute_log_likelihood(seq) lpr, fwdlattice = self._do_forward_pass(framelogprob) bwdlattice = self._do_backward_pass(framelogprob) gamma = fwdlattice + bwdlattice posteriors = np.exp(gamma.T - logsumexp(gamma, axis=1)).T assert np.allclose(np.sum(posteriors, axis=1), 1.0) # posteriors must sum to 1 for each t curr_loglikelihood += lpr self._accumulate_sufficient_statistics(stats, seq, framelogprob, posteriors, fwdlattice, bwdlattice) # Test for convergence if prev_loglikelihood is not None: delta = curr_loglikelihood - prev_loglikelihood print ('%f (%f)' % (curr_loglikelihood, delta)) assert delta >= -0.01 # Likelihood when training with Baum-Welch should grow monotonically if delta <= self.training_threshold: break self._do_mstep(stats) prev_loglikelihood = curr_loglikelihood def _loglikelihood(self, ob, method): framelogprob = self._compute_log_likelihood(ob) loglikelihood, _ = self._do_forward_pass(framelogprob) return loglikelihood def _sample(self, n_samples, max_cycle_duration): raise NotImplementedError('not yet implemented') pass class SequentialGaussianFHMM(BaseHMM): def __init__(self, n_states=10, n_chains=2, n_training_iterations=10, training_threshold=1e-2, topology='left-to-right', verbose=False, transition_init='uniform', emission_init='k-means', covar_type='diag'): super(SequentialGaussianFHMM, self).__init__(n_states, n_training_iterations, training_threshold, topology, verbose, transition_init, emission_init, covar_type) self.n_chains = n_chains self.chains_ = None def _init(self, obs): # Initialize first chain randomize = True if self.emission_init == 'random' else False means, covars = estimate_normal_distribution_params(obs, n_states=self.n_states, randomize=randomize) chain = _new_model(self.n_states, self.transition_init, np.copy(means), np.copy(covars), self.covar_type, self.topology, self.n_training_iterations, self.training_threshold, self.verbose) self.chains_ = [chain] def _fit(self, obs): assert self.chains_ assert len(self.chains_) > 0 # Re-use generated observations for later use when iterating over already trained chains. We do not need an # entry for the last chain since it never generates an observation. generated_obs_cache = [[] for _ in xrange(self.n_chains - 1)] # Train first chain self.chains_[0].fit(obs) # Train subsequent chains on the error for curr_chain_idx in xrange(1, self.n_chains): # Calculate residual error for each observation weight = 1.0 / float(self.n_chains) # TODO: it's a bit unclear if this is correct err = [] for ob_idx, ob in enumerate(obs): # Calculate residual error combined_generated_ob = self._generate_observation(ob, ob_idx, curr_chain_idx, weight, generated_obs_cache) curr_err = (1.0 / weight) * (ob - combined_generated_ob) assert curr_err.shape == ob.shape err.append(curr_err) assert len(err) == len(obs) # Create new chain randomize = True if self.emission_init == 'random' else False means, covars = estimate_normal_distribution_params(err, n_states=self.n_states, randomize=randomize) curr_chain = _new_model(self.n_states, self.transition_init, means, covars, self.covar_type, self.topology, self.n_training_iterations, self.training_threshold, self.verbose) self.chains_.append(curr_chain) # Fit chain on residual error curr_chain.fit(err) # Ensure that cache was filled properly and works as expected assert len(generated_obs_cache) == self.n_chains - 1 for chain_cache in generated_obs_cache: assert len(chain_cache) == len(obs) def _loglikelihood_residual_approx(self, ob): scores = np.zeros(self.n_chains) # Re-use generated observations for later use when iterating over already trained chains. We do not need an # entry for the last chain since it never generates an observation. generated_obs_cache = [[] for _ in xrange(self.n_chains - 1)] for curr_chain_idx, curr_chain in enumerate(self.chains_): if curr_chain_idx == 0: scores[curr_chain_idx] = curr_chain.score(ob) continue # Calculate residual error for observation and calculate score on it weight = 1.0 / float(self.n_chains) # TODO: it's a bit unclear if this is correct combined_generated_ob = self._generate_observation(ob, 0, curr_chain_idx, weight, generated_obs_cache) curr_err = (1.0 / weight) * (ob - combined_generated_ob) scores[curr_chain_idx] = curr_chain.score(curr_err) return logsumexp(scores) def _exact_loglikelihood(self, ob): log_transmat = np.zeros((self.n_chains, self.n_states, self.n_states)) log_startprob = np.zeros((self.n_chains, self.n_states)) for idx, chain in enumerate(self.chains_): log_transmat[idx] = chain._log_transmat log_startprob[idx] = chain._log_startprob n_state_combinations = self.n_states ** self.n_chains state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))] n_observations = ob.shape[0] n_features = ob.shape[1] fwdlattice = np.zeros((n_observations, n_state_combinations)) # Calculate means and covariances for all state combinations and calculate emission probabilities weight = (1.0 / float(self.n_chains)) weight_squared = weight * weight covars = np.zeros((n_state_combinations, n_features)) # TODO: add support for all covariance types means = np.zeros((n_state_combinations, n_features)) for idx, state_combination in enumerate(state_combinations): for chain_idx, state in enumerate(state_combination): chain = self.chains_[chain_idx] covars[idx] += chain._covars_[state] means[idx] += chain._means_[state] covars[idx] *= weight_squared means[idx] *= weight framelogprob = log_multivariate_normal_density(ob, means, covars, covariance_type='diag') # TODO: add support for all covariance types # Run the forward algorithm fhmmc._forward(n_observations, self.n_chains, self.n_states, state_combinations, log_startprob, log_transmat, framelogprob, fwdlattice) last_column = fwdlattice[-1] assert np.size(last_column) == n_state_combinations score = logsumexp(last_column) return score def _loglikelihood(self, ob, method): if method == 'exact': return self._exact_loglikelihood(ob) elif method == 'approx': return self._loglikelihood_residual_approx(ob) else: raise ValueError('unknown method "%s"' % method) def _sample(self, n_samples, max_cycle_duration): # Calculate means and covariances for all state combinations and calculate emission probabilities state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))] state_combinations_shape = tuple([self.n_states for _ in xrange(self.n_chains)]) weight = (1.0 / float(self.n_chains)) weight_squared = weight * weight covars = np.zeros(state_combinations_shape + (self.n_features_,)) # TODO: add support for all covariance types means = np.zeros(state_combinations_shape + (self.n_features_,)) for state_combination in state_combinations: for chain_idx, state in enumerate(state_combination): chain = self.chains_[chain_idx] covars[state_combination] += chain._covars_[state] means[state_combination] += chain._means_[state] covars[state_combination] *= weight_squared means[state_combination] *= weight obs = greedy_sample(self.chains_, means, covars, n_samples=n_samples, max_cycle_duration=max_cycle_duration) return obs def _generate_observation(self, ob, ob_idx, curr_chain_idx, weight, generated_obs_cache): combined_generated_ob = np.zeros(ob.shape) # Iterate over all chains up until (but excluding) the current chain and combine their generated # observations for prev_chain_idx in xrange(curr_chain_idx): prev_chain = self.chains_[prev_chain_idx] generated_ob = np.zeros(ob.shape) if ob_idx < len(generated_obs_cache[prev_chain_idx]): # Use cached value assert generated_obs_cache[prev_chain_idx][ob_idx].shape == ob.shape generated_ob = generated_obs_cache[prev_chain_idx][ob_idx] else: # Option a: gamma framelogprob = prev_chain._compute_log_likelihood(ob) logprob, fwdlattice = prev_chain._do_forward_pass(framelogprob) bwdlattice = prev_chain._do_backward_pass(framelogprob) fwdbwdlattice = fwdlattice + bwdlattice gamma = np.exp(fwdbwdlattice.T - logsumexp(fwdbwdlattice, axis=1)).T # TODO: this can probably be vectorized for t in xrange(ob.shape[0]): for state in xrange(self.n_states): generated_ob[t] += prev_chain.means_[state] * gamma[t][state] # Option b: Viterbi # _, state_seq = prev_chain.decode(ob) # assert np.size(state_seq) == generated_ob.shape[0] # for idx, state in enumerate(state_seq): # generated_ob[idx] = prev_chain.means_[state] # Option c: generated sequence # generated_ob = prev_chain.sample(ob.shape[0])[0] # Cache for future iterations generated_obs_cache[prev_chain_idx].append(generated_ob) combined_generated_ob += weight * generated_ob return combined_generated_ob
mit
uglyboxer/linear_neuron
net-p3/lib/python3.5/site-packages/matplotlib/backends/backend_webagg.py
10
12184
""" Displays Agg images in the browser, with interactivity """ from __future__ import (absolute_import, division, print_function, unicode_literals) # The WebAgg backend is divided into two modules: # # - `backend_webagg_core.py` contains code necessary to embed a WebAgg # plot inside of a web application, and communicate in an abstract # way over a web socket. # # - `backend_webagg.py` contains a concrete implementation of a basic # application, implemented with tornado. import six import datetime import errno import json import os import random import sys import socket import threading try: import tornado except ImportError: raise RuntimeError("The WebAgg backend requires Tornado.") import tornado.web import tornado.ioloop import tornado.websocket import matplotlib from matplotlib import rcParams from matplotlib import backend_bases from matplotlib.figure import Figure from matplotlib._pylab_helpers import Gcf from . import backend_webagg_core as core from .backend_nbagg import TimerTornado def new_figure_manager(num, *args, **kwargs): """ Create a new figure manager instance """ FigureClass = kwargs.pop('FigureClass', Figure) thisFig = FigureClass(*args, **kwargs) return new_figure_manager_given_figure(num, thisFig) def new_figure_manager_given_figure(num, figure): """ Create a new figure manager instance for the given figure. """ canvas = FigureCanvasWebAgg(figure) manager = core.FigureManagerWebAgg(canvas, num) return manager def draw_if_interactive(): """ Is called after every pylab drawing command """ if matplotlib.is_interactive(): figManager = Gcf.get_active() if figManager is not None: figManager.canvas.draw_idle() class Show(backend_bases.ShowBase): def mainloop(self): WebAggApplication.initialize() url = "http://127.0.0.1:{port}{prefix}".format( port=WebAggApplication.port, prefix=WebAggApplication.url_prefix) if rcParams['webagg.open_in_browser']: import webbrowser webbrowser.open(url) else: print("To view figure, visit {0}".format(url)) WebAggApplication.start() show = Show().mainloop class ServerThread(threading.Thread): def run(self): tornado.ioloop.IOLoop.instance().start() webagg_server_thread = ServerThread() class FigureCanvasWebAgg(core.FigureCanvasWebAggCore): def show(self): # show the figure window show() def new_timer(self, *args, **kwargs): return TimerTornado(*args, **kwargs) def start_event_loop(self, timeout): backend_bases.FigureCanvasBase.start_event_loop_default( self, timeout) start_event_loop.__doc__ = \ backend_bases.FigureCanvasBase.start_event_loop_default.__doc__ def stop_event_loop(self): backend_bases.FigureCanvasBase.stop_event_loop_default(self) stop_event_loop.__doc__ = \ backend_bases.FigureCanvasBase.stop_event_loop_default.__doc__ class WebAggApplication(tornado.web.Application): initialized = False started = False class FavIcon(tornado.web.RequestHandler): def get(self): image_path = os.path.join( os.path.dirname(os.path.dirname(__file__)), 'mpl-data', 'images') self.set_header('Content-Type', 'image/png') with open(os.path.join(image_path, 'matplotlib.png'), 'rb') as fd: self.write(fd.read()) class SingleFigurePage(tornado.web.RequestHandler): def __init__(self, application, request, **kwargs): self.url_prefix = kwargs.pop('url_prefix', '') return tornado.web.RequestHandler.__init__(self, application, request, **kwargs) def get(self, fignum): fignum = int(fignum) manager = Gcf.get_fig_manager(fignum) ws_uri = 'ws://{req.host}{prefix}/'.format(req=self.request, prefix=self.url_prefix) self.render( "single_figure.html", prefix=self.url_prefix, ws_uri=ws_uri, fig_id=fignum, toolitems=core.NavigationToolbar2WebAgg.toolitems, canvas=manager.canvas) class AllFiguresPage(tornado.web.RequestHandler): def __init__(self, application, request, **kwargs): self.url_prefix = kwargs.pop('url_prefix', '') return tornado.web.RequestHandler.__init__(self, application, request, **kwargs) def get(self): ws_uri = 'ws://{req.host}{prefix}/'.format(req=self.request, prefix=self.url_prefix) self.render( "all_figures.html", prefix=self.url_prefix, ws_uri=ws_uri, figures=sorted( list(Gcf.figs.items()), key=lambda item: item[0]), toolitems=core.NavigationToolbar2WebAgg.toolitems) class MplJs(tornado.web.RequestHandler): def get(self): self.set_header('Content-Type', 'application/javascript') js_content = core.FigureManagerWebAgg.get_javascript() self.write(js_content) class Download(tornado.web.RequestHandler): def get(self, fignum, fmt): fignum = int(fignum) manager = Gcf.get_fig_manager(fignum) # TODO: Move this to a central location mimetypes = { 'ps': 'application/postscript', 'eps': 'application/postscript', 'pdf': 'application/pdf', 'svg': 'image/svg+xml', 'png': 'image/png', 'jpeg': 'image/jpeg', 'tif': 'image/tiff', 'emf': 'application/emf' } self.set_header('Content-Type', mimetypes.get(fmt, 'binary')) buff = six.BytesIO() manager.canvas.print_figure(buff, format=fmt) self.write(buff.getvalue()) class WebSocket(tornado.websocket.WebSocketHandler): supports_binary = True def open(self, fignum): self.fignum = int(fignum) self.manager = Gcf.get_fig_manager(self.fignum) self.manager.add_web_socket(self) if hasattr(self, 'set_nodelay'): self.set_nodelay(True) def on_close(self): self.manager.remove_web_socket(self) def on_message(self, message): message = json.loads(message) # The 'supports_binary' message is on a client-by-client # basis. The others affect the (shared) canvas as a # whole. if message['type'] == 'supports_binary': self.supports_binary = message['value'] else: manager = Gcf.get_fig_manager(self.fignum) # It is possible for a figure to be closed, # but a stale figure UI is still sending messages # from the browser. if manager is not None: manager.handle_json(message) def send_json(self, content): self.write_message(json.dumps(content)) def send_binary(self, blob): if self.supports_binary: self.write_message(blob, binary=True) else: data_uri = "data:image/png;base64,{0}".format( blob.encode('base64').replace('\n', '')) self.write_message(data_uri) def __init__(self, url_prefix=''): if url_prefix: assert url_prefix[0] == '/' and url_prefix[-1] != '/', \ 'url_prefix must start with a "/" and not end with one.' super(WebAggApplication, self).__init__( [ # Static files for the CSS and JS (url_prefix + r'/_static/(.*)', tornado.web.StaticFileHandler, {'path': core.FigureManagerWebAgg.get_static_file_path()}), # An MPL favicon (url_prefix + r'/favicon.ico', self.FavIcon), # The page that contains all of the pieces (url_prefix + r'/([0-9]+)', self.SingleFigurePage, {'url_prefix': url_prefix}), # The page that contains all of the figures (url_prefix + r'/?', self.AllFiguresPage, {'url_prefix': url_prefix}), (url_prefix + r'/mpl.js', self.MplJs), # Sends images and events to the browser, and receives # events from the browser (url_prefix + r'/([0-9]+)/ws', self.WebSocket), # Handles the downloading (i.e., saving) of static images (url_prefix + r'/([0-9]+)/download.([a-z0-9.]+)', self.Download), ], template_path=core.FigureManagerWebAgg.get_static_file_path()) @classmethod def initialize(cls, url_prefix='', port=None): if cls.initialized: return # Create the class instance app = cls(url_prefix=url_prefix) cls.url_prefix = url_prefix # This port selection algorithm is borrowed, more or less # verbatim, from IPython. def random_ports(port, n): """ Generate a list of n random ports near the given port. The first 5 ports will be sequential, and the remaining n-5 will be randomly selected in the range [port-2*n, port+2*n]. """ for i in range(min(5, n)): yield port + i for i in range(n - 5): yield port + random.randint(-2 * n, 2 * n) success = None cls.port = rcParams['webagg.port'] for port in random_ports(cls.port, rcParams['webagg.port_retries']): try: app.listen(port) except socket.error as e: if e.errno != errno.EADDRINUSE: raise else: cls.port = port success = True break if not success: raise SystemExit( "The webagg server could not be started because an available " "port could not be found") cls.initialized = True @classmethod def start(cls): if cls.started: return # Set the flag to True *before* blocking on IOLoop.instance().start() cls.started = True """ IOLoop.running() was removed as of Tornado 2.4; see for example https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY Thus there is no correct way to check if the loop has already been launched. We may end up with two concurrently running loops in that unlucky case with all the expected consequences. """ print("Press Ctrl+C to stop WebAgg server") sys.stdout.flush() try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print("Server is stopped") sys.stdout.flush() finally: cls.started = False def ipython_inline_display(figure): import tornado.template WebAggApplication.initialize() if not webagg_server_thread.is_alive(): webagg_server_thread.start() with open(os.path.join( core.FigureManagerWebAgg.get_static_file_path(), 'ipython_inline_figure.html')) as fd: tpl = fd.read() fignum = figure.number t = tornado.template.Template(tpl) return t.generate( prefix=WebAggApplication.url_prefix, fig_id=fignum, toolitems=core.NavigationToolbar2WebAgg.toolitems, canvas=figure.canvas, port=WebAggApplication.port).decode('utf-8') FigureCanvas = FigureCanvasWebAgg
mit
jschuecker/nest-simulator
pynest/examples/intrinsic_currents_subthreshold.py
13
7182
# -*- coding: utf-8 -*- # # intrinsic_currents_subthreshold.py # # This file is part of NEST. # # Copyright (C) 2004 The NEST Initiative # # NEST is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # NEST is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with NEST. If not, see <http://www.gnu.org/licenses/>. ''' Intrinsic currents subthreshold ------------------------------- This example illustrates how to record from a model with multiple intrinsic currents and visualize the results. This is illustrated using the `ht_neuron` which has four intrinsic currents: I_NaP, I_KNa, I_T, and I_h. It is a slightly simplified implementation of neuron model proposed in Hill and Tononi (2005) **Modeling Sleep and Wakefulness in the Thalamocortical System** *J Neurophysiol* 93:1671 http://dx.doi.org/10.1152/jn.00915.2004 . The neuron is driven by DC current, which is alternated between depolarizing and hyperpolarizing. Hyperpolarization intervals become increasingly longer. See also: intrinsic_currents_spiking.py ''' ''' We imported all necessary modules for simulation, analysis and plotting. ''' import nest import numpy as np import matplotlib.pyplot as plt ''' Additionally, we set the verbosity using `set_verbosity` to suppress info messages. We also reset the kernel to be sure to start with a clean NEST. ''' nest.set_verbosity("M_WARNING") nest.ResetKernel() ''' We define simulation parameters: - The length of depolarization intervals - The length of hyperpolarization intervals - The amplitude for de- and hyperpolarizing currents - The end of the time window to plot ''' n_blocks = 5 t_block = 20. t_dep = [t_block] * n_blocks t_hyp = [t_block * 2 ** n for n in range(n_blocks)] I_dep = 10. I_hyp = -5. t_end = 500. ''' We create the one neuron instance and the DC current generator and store the returned handles. ''' nrn = nest.Create('ht_neuron') dc = nest.Create('dc_generator') ''' We create a multimeter to record - membrane potential `V_m` - threshold value `theta` - intrinsic currents `I_NaP`, `I_KNa`, `I_T`, `I_h` by passing these names in the `record_from` list. To find out which quantities can be recorded from a given neuron, run:: nest.GetDefaults('ht_neuron')['recordables'] The result will contain an entry like:: <SLILiteral: V_m> for each recordable quantity. You need to pass the value of the `SLILiteral`, in this case `V_m` in the `record_from` list. We want to record values with 0.1 ms resolution, so we set the recording interval as well; the default recording resolution is 1 ms. ''' # create multimeter and configure it to record all information # we want at 0.1ms resolution mm = nest.Create('multimeter', params={'interval': 0.1, 'record_from': ['V_m', 'theta', 'I_NaP', 'I_KNa', 'I_T', 'I_h']} ) ''' We connect the DC generator and the multimeter to the neuron. Note that the multimeter, just like the voltmeter is connected to the neuron, not the neuron to the multimeter. ''' nest.Connect(dc, nrn) nest.Connect(mm, nrn) ''' We are ready to simulate. We alternate between driving the neuron with depolarizing and hyperpolarizing currents. Before each simulation interval, we set the amplitude of the DC generator to the correct value. ''' for t_sim_dep, t_sim_hyp in zip(t_dep, t_hyp): nest.SetStatus(dc, {'amplitude': I_dep}) nest.Simulate(t_sim_dep) nest.SetStatus(dc, {'amplitude': I_hyp}) nest.Simulate(t_sim_hyp) ''' We now fetch the data recorded by the multimeter. The data are returned as a dictionary with entry ``'times'`` containing timestamps for all recorded data, plus one entry per recorded quantity. All data is contained in the ``'events'`` entry of the status dictionary returned by the multimeter. Because all NEST function return arrays, we need to pick out element ``0`` from the result of `GetStatus`. ''' data = nest.GetStatus(mm)[0]['events'] t = data['times'] ''' The next step is to plot the results. We create a new figure, add a single subplot and plot at first membrane potential and threshold. ''' fig = plt.figure() Vax = fig.add_subplot(111) Vax.plot(t, data['V_m'], 'b-', lw=2, label=r'$V_m$') Vax.plot(t, data['theta'], 'g-', lw=2, label=r'$\Theta$') Vax.set_ylim(-80., 0.) Vax.set_ylabel('Voltageinf [mV]') Vax.set_xlabel('Time [ms]') ''' To plot the input current, we need to create an input current trace. We construct it from the durations of the de- and hyperpolarizing inputs and add the delay in the connection between DC generator and neuron: 1. We find the delay by checking the status of the dc->nrn connection. 1. We find the resolution of the simulation from the kernel status. 1. Each current interval begins one time step after the previous interval, is delayed by the delay and effective for the given duration. 1. We build the time axis incrementally. We only add the delay when adding the first time point after t=0. All subsequent points are then automatically shifted by the delay. ''' delay = nest.GetStatus(nest.GetConnections(dc, nrn))[0]['delay'] dt = nest.GetKernelStatus('resolution') t_dc, I_dc = [0], [0] for td, th in zip(t_dep, t_hyp): t_prev = t_dc[-1] t_start_dep = t_prev + dt if t_prev > 0 else t_prev + dt + delay t_end_dep = t_start_dep + td t_start_hyp = t_end_dep + dt t_end_hyp = t_start_hyp + th t_dc.extend([t_start_dep, t_end_dep, t_start_hyp, t_end_hyp]) I_dc.extend([I_dep, I_dep, I_hyp, I_hyp]) ''' The following function turns a name such as I_NaP into proper TeX code $I_{\mathrm{NaP}}$ for a pretty label. ''' def texify_name(name): return r'${}_{{\mathrm{{{}}}}}$'.format(*name.split('_')) ''' Next, we add a right vertical axis and plot the currents with respect to that axis. ''' Iax = Vax.twinx() Iax.plot(t_dc, I_dc, 'k-', lw=2, label=texify_name('I_DC')) for iname, color in (('I_h', 'maroon'), ('I_T', 'orange'), ('I_NaP', 'crimson'), ('I_KNa', 'aqua')): Iax.plot(t, data[iname], color=color, lw=2, label=texify_name(iname)) Iax.set_xlim(0, t_end) Iax.set_ylim(-10., 15.) Iax.set_ylabel('Current [pA]') Iax.set_title('ht_neuron driven by DC current') ''' We need to make a little extra effort to combine lines from the two axis into one legend. ''' lines_V, labels_V = Vax.get_legend_handles_labels() lines_I, labels_I = Iax.get_legend_handles_labels() try: Iax.legend(lines_V + lines_I, labels_V + labels_I, fontsize='small') except TypeError: # work-around for older Matplotlib versions Iax.legend(lines_V + lines_I, labels_V + labels_I) ''' Note that I_KNa is not activated in this example because the neuron does not spike. I_T has only a very small amplitude. '''
gpl-2.0
michigraber/scikit-learn
sklearn/ensemble/__init__.py
217
1307
""" The :mod:`sklearn.ensemble` module includes ensemble-based methods for classification and regression. """ from .base import BaseEnsemble from .forest import RandomForestClassifier from .forest import RandomForestRegressor from .forest import RandomTreesEmbedding from .forest import ExtraTreesClassifier from .forest import ExtraTreesRegressor from .bagging import BaggingClassifier from .bagging import BaggingRegressor from .weight_boosting import AdaBoostClassifier from .weight_boosting import AdaBoostRegressor from .gradient_boosting import GradientBoostingClassifier from .gradient_boosting import GradientBoostingRegressor from .voting_classifier import VotingClassifier from . import bagging from . import forest from . import weight_boosting from . import gradient_boosting from . import partial_dependence __all__ = ["BaseEnsemble", "RandomForestClassifier", "RandomForestRegressor", "RandomTreesEmbedding", "ExtraTreesClassifier", "ExtraTreesRegressor", "BaggingClassifier", "BaggingRegressor", "GradientBoostingClassifier", "GradientBoostingRegressor", "AdaBoostClassifier", "AdaBoostRegressor", "VotingClassifier", "bagging", "forest", "gradient_boosting", "partial_dependence", "weight_boosting"]
bsd-3-clause
Brotcrunsher/BrotboxEngine
Third-Party/portaudio/test/patest_suggested_vs_streaminfo_latency.py
74
5354
#!/usr/bin/env python """ Run and graph the results of patest_suggested_vs_streaminfo_latency.c Requires matplotlib for plotting: http://matplotlib.sourceforge.net/ """ import os from pylab import * import numpy from matplotlib.backends.backend_pdf import PdfPages testExeName = "PATest.exe" # rename to whatever the compiled patest_suggested_vs_streaminfo_latency.c binary is dataFileName = "patest_suggested_vs_streaminfo_latency.csv" # code below calls the exe to generate this file inputDeviceIndex = -1 # -1 means default outputDeviceIndex = -1 # -1 means default sampleRate = 44100 pdfFilenameSuffix = "_wmme" pdfFile = PdfPages("patest_suggested_vs_streaminfo_latency_" + str(sampleRate) + pdfFilenameSuffix +".pdf") #output this pdf file def loadCsvData( dataFileName ): params= "" inputDevice = "" outputDevice = "" startLines = file(dataFileName).readlines(1024) for line in startLines: if "output device" in line: outputDevice = line.strip(" \t\n\r#") if "input device" in line: inputDevice = line.strip(" \t\n\r#") params = startLines[0].strip(" \t\n\r#") data = numpy.loadtxt(dataFileName, delimiter=",", skiprows=4).transpose() class R(object): pass result = R() result.params = params for s in params.split(','): if "sample rate" in s: result.sampleRate = s result.inputDevice = inputDevice result.outputDevice = outputDevice result.suggestedLatency = data[0] result.halfDuplexOutputLatency = data[1] result.halfDuplexInputLatency = data[2] result.fullDuplexOutputLatency = data[3] result.fullDuplexInputLatency = data[4] return result; def setFigureTitleAndAxisLabels( framesPerBufferString ): title("PortAudio suggested (requested) vs. resulting (reported) stream latency\n" + framesPerBufferString) ylabel("PaStreamInfo::{input,output}Latency (s)") xlabel("Pa_OpenStream suggestedLatency (s)") grid(True) legend(loc="upper left") def setDisplayRangeSeconds( maxSeconds ): xlim(0, maxSeconds) ylim(0, maxSeconds) # run the test with different frames per buffer values: compositeTestFramesPerBufferValues = [0] # powers of two for i in range (1,11): compositeTestFramesPerBufferValues.append( pow(2,i) ) # multiples of 50 for i in range (1,20): compositeTestFramesPerBufferValues.append( i * 50 ) # 10ms buffer sizes compositeTestFramesPerBufferValues.append( 441 ) compositeTestFramesPerBufferValues.append( 882 ) # large primes #compositeTestFramesPerBufferValues.append( 39209 ) #compositeTestFramesPerBufferValues.append( 37537 ) #compositeTestFramesPerBufferValues.append( 26437 ) individualPlotFramesPerBufferValues = [0,64,128,256,512] #output separate plots for these isFirst = True for framesPerBuffer in compositeTestFramesPerBufferValues: commandString = testExeName + " " + str(inputDeviceIndex) + " " + str(outputDeviceIndex) + " " + str(sampleRate) + " " + str(framesPerBuffer) + ' > ' + dataFileName print commandString os.system(commandString) d = loadCsvData(dataFileName) if isFirst: figure(1) # title sheet gcf().text(0.1, 0.0, "patest_suggested_vs_streaminfo_latency\n%s\n%s\n%s\n"%(d.inputDevice,d.outputDevice,d.sampleRate)) pdfFile.savefig() figure(2) # composite plot, includes all compositeTestFramesPerBufferValues if isFirst: plot( d.suggestedLatency, d.suggestedLatency, label="Suggested latency" ) plot( d.suggestedLatency, d.halfDuplexOutputLatency ) plot( d.suggestedLatency, d.halfDuplexInputLatency ) plot( d.suggestedLatency, d.fullDuplexOutputLatency ) plot( d.suggestedLatency, d.fullDuplexInputLatency ) if framesPerBuffer in individualPlotFramesPerBufferValues: # individual plots figure( 3 + individualPlotFramesPerBufferValues.index(framesPerBuffer) ) plot( d.suggestedLatency, d.suggestedLatency, label="Suggested latency" ) plot( d.suggestedLatency, d.halfDuplexOutputLatency, label="Half-duplex output latency" ) plot( d.suggestedLatency, d.halfDuplexInputLatency, label="Half-duplex input latency" ) plot( d.suggestedLatency, d.fullDuplexOutputLatency, label="Full-duplex output latency" ) plot( d.suggestedLatency, d.fullDuplexInputLatency, label="Full-duplex input latency" ) if framesPerBuffer == 0: framesPerBufferText = "paFramesPerBufferUnspecified" else: framesPerBufferText = str(framesPerBuffer) setFigureTitleAndAxisLabels( "user frames per buffer: "+str(framesPerBufferText) ) setDisplayRangeSeconds(2.2) pdfFile.savefig() setDisplayRangeSeconds(0.1) setFigureTitleAndAxisLabels( "user frames per buffer: "+str(framesPerBufferText)+" (detail)" ) pdfFile.savefig() isFirst = False figure(2) setFigureTitleAndAxisLabels( "composite of frames per buffer values:\n"+str(compositeTestFramesPerBufferValues) ) setDisplayRangeSeconds(2.2) pdfFile.savefig() setDisplayRangeSeconds(0.1) setFigureTitleAndAxisLabels( "composite of frames per buffer values:\n"+str(compositeTestFramesPerBufferValues)+" (detail)" ) pdfFile.savefig() pdfFile.close() #uncomment this to display interactively, otherwise we just output a pdf #show()
mit
yyjiang/scikit-learn
sklearn/learning_curve.py
110
13467
"""Utilities to evaluate models with respect to a variable """ # Author: Alexander Fabisch <afabisch@informatik.uni-bremen.de> # # License: BSD 3 clause import warnings import numpy as np from .base import is_classifier, clone from .cross_validation import check_cv from .externals.joblib import Parallel, delayed from .cross_validation import _safe_split, _score, _fit_and_score from .metrics.scorer import check_scoring from .utils import indexable from .utils.fixes import astype __all__ = ['learning_curve', 'validation_curve'] def learning_curve(estimator, X, y, train_sizes=np.linspace(0.1, 1.0, 5), cv=None, scoring=None, exploit_incremental_learning=False, n_jobs=1, pre_dispatch="all", verbose=0): """Learning curve. Determines cross-validated training and test scores for different training set sizes. A cross-validation generator splits the whole dataset k times in training and test data. Subsets of the training set with varying sizes will be used to train the estimator and a score for each training subset size and the test set will be computed. Afterwards, the scores will be averaged over all k runs for each training subset size. Read more in the :ref:`User Guide <learning_curves>`. Parameters ---------- estimator : object type that implements the "fit" and "predict" methods An object of that type which is cloned for each validation. X : array-like, shape (n_samples, n_features) Training vector, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples) or (n_samples, n_features), optional Target relative to X for classification or regression; None for unsupervised learning. train_sizes : array-like, shape (n_ticks,), dtype float or int Relative or absolute numbers of training examples that will be used to generate the learning curve. If the dtype is float, it is regarded as a fraction of the maximum size of the training set (that is determined by the selected validation method), i.e. it has to be within (0, 1]. Otherwise it is interpreted as absolute sizes of the training sets. Note that for classification the number of samples usually have to be big enough to contain at least one sample from each class. (default: np.linspace(0.1, 1.0, 5)) cv : integer, cross-validation generator, optional If an integer is passed, it is the number of folds (defaults to 3). Specific cross-validation objects can be passed, see sklearn.cross_validation module for the list of possible objects scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. exploit_incremental_learning : boolean, optional, default: False If the estimator supports incremental learning, this will be used to speed up fitting for different training set sizes. n_jobs : integer, optional Number of jobs to run in parallel (default 1). pre_dispatch : integer or string, optional Number of predispatched jobs for parallel execution (default is all). The option can reduce the allocated memory. The string can be an expression like '2*n_jobs'. verbose : integer, optional Controls the verbosity: the higher, the more messages. Returns ------- train_sizes_abs : array, shape = (n_unique_ticks,), dtype int Numbers of training examples that has been used to generate the learning curve. Note that the number of ticks might be less than n_ticks because duplicate entries will be removed. train_scores : array, shape (n_ticks, n_cv_folds) Scores on training sets. test_scores : array, shape (n_ticks, n_cv_folds) Scores on test set. Notes ----- See :ref:`examples/model_selection/plot_learning_curve.py <example_model_selection_plot_learning_curve.py>` """ if exploit_incremental_learning and not hasattr(estimator, "partial_fit"): raise ValueError("An estimator must support the partial_fit interface " "to exploit incremental learning") X, y = indexable(X, y) # Make a list since we will be iterating multiple times over the folds cv = list(check_cv(cv, X, y, classifier=is_classifier(estimator))) scorer = check_scoring(estimator, scoring=scoring) # HACK as long as boolean indices are allowed in cv generators if cv[0][0].dtype == bool: new_cv = [] for i in range(len(cv)): new_cv.append((np.nonzero(cv[i][0])[0], np.nonzero(cv[i][1])[0])) cv = new_cv n_max_training_samples = len(cv[0][0]) # Because the lengths of folds can be significantly different, it is # not guaranteed that we use all of the available training data when we # use the first 'n_max_training_samples' samples. train_sizes_abs = _translate_train_sizes(train_sizes, n_max_training_samples) n_unique_ticks = train_sizes_abs.shape[0] if verbose > 0: print("[learning_curve] Training set sizes: " + str(train_sizes_abs)) parallel = Parallel(n_jobs=n_jobs, pre_dispatch=pre_dispatch, verbose=verbose) if exploit_incremental_learning: classes = np.unique(y) if is_classifier(estimator) else None out = parallel(delayed(_incremental_fit_estimator)( clone(estimator), X, y, classes, train, test, train_sizes_abs, scorer, verbose) for train, test in cv) else: out = parallel(delayed(_fit_and_score)( clone(estimator), X, y, scorer, train[:n_train_samples], test, verbose, parameters=None, fit_params=None, return_train_score=True) for train, test in cv for n_train_samples in train_sizes_abs) out = np.array(out)[:, :2] n_cv_folds = out.shape[0] // n_unique_ticks out = out.reshape(n_cv_folds, n_unique_ticks, 2) out = np.asarray(out).transpose((2, 1, 0)) return train_sizes_abs, out[0], out[1] def _translate_train_sizes(train_sizes, n_max_training_samples): """Determine absolute sizes of training subsets and validate 'train_sizes'. Examples: _translate_train_sizes([0.5, 1.0], 10) -> [5, 10] _translate_train_sizes([5, 10], 10) -> [5, 10] Parameters ---------- train_sizes : array-like, shape (n_ticks,), dtype float or int Numbers of training examples that will be used to generate the learning curve. If the dtype is float, it is regarded as a fraction of 'n_max_training_samples', i.e. it has to be within (0, 1]. n_max_training_samples : int Maximum number of training samples (upper bound of 'train_sizes'). Returns ------- train_sizes_abs : array, shape (n_unique_ticks,), dtype int Numbers of training examples that will be used to generate the learning curve. Note that the number of ticks might be less than n_ticks because duplicate entries will be removed. """ train_sizes_abs = np.asarray(train_sizes) n_ticks = train_sizes_abs.shape[0] n_min_required_samples = np.min(train_sizes_abs) n_max_required_samples = np.max(train_sizes_abs) if np.issubdtype(train_sizes_abs.dtype, np.float): if n_min_required_samples <= 0.0 or n_max_required_samples > 1.0: raise ValueError("train_sizes has been interpreted as fractions " "of the maximum number of training samples and " "must be within (0, 1], but is within [%f, %f]." % (n_min_required_samples, n_max_required_samples)) train_sizes_abs = astype(train_sizes_abs * n_max_training_samples, dtype=np.int, copy=False) train_sizes_abs = np.clip(train_sizes_abs, 1, n_max_training_samples) else: if (n_min_required_samples <= 0 or n_max_required_samples > n_max_training_samples): raise ValueError("train_sizes has been interpreted as absolute " "numbers of training samples and must be within " "(0, %d], but is within [%d, %d]." % (n_max_training_samples, n_min_required_samples, n_max_required_samples)) train_sizes_abs = np.unique(train_sizes_abs) if n_ticks > train_sizes_abs.shape[0]: warnings.warn("Removed duplicate entries from 'train_sizes'. Number " "of ticks will be less than than the size of " "'train_sizes' %d instead of %d)." % (train_sizes_abs.shape[0], n_ticks), RuntimeWarning) return train_sizes_abs def _incremental_fit_estimator(estimator, X, y, classes, train, test, train_sizes, scorer, verbose): """Train estimator on training subsets incrementally and compute scores.""" train_scores, test_scores = [], [] partitions = zip(train_sizes, np.split(train, train_sizes)[:-1]) for n_train_samples, partial_train in partitions: train_subset = train[:n_train_samples] X_train, y_train = _safe_split(estimator, X, y, train_subset) X_partial_train, y_partial_train = _safe_split(estimator, X, y, partial_train) X_test, y_test = _safe_split(estimator, X, y, test, train_subset) if y_partial_train is None: estimator.partial_fit(X_partial_train, classes=classes) else: estimator.partial_fit(X_partial_train, y_partial_train, classes=classes) train_scores.append(_score(estimator, X_train, y_train, scorer)) test_scores.append(_score(estimator, X_test, y_test, scorer)) return np.array((train_scores, test_scores)).T def validation_curve(estimator, X, y, param_name, param_range, cv=None, scoring=None, n_jobs=1, pre_dispatch="all", verbose=0): """Validation curve. Determine training and test scores for varying parameter values. Compute scores for an estimator with different values of a specified parameter. This is similar to grid search with one parameter. However, this will also compute training scores and is merely a utility for plotting the results. Read more in the :ref:`User Guide <validation_curve>`. Parameters ---------- estimator : object type that implements the "fit" and "predict" methods An object of that type which is cloned for each validation. X : array-like, shape (n_samples, n_features) Training vector, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape (n_samples) or (n_samples, n_features), optional Target relative to X for classification or regression; None for unsupervised learning. param_name : string Name of the parameter that will be varied. param_range : array-like, shape (n_values,) The values of the parameter that will be evaluated. cv : integer, cross-validation generator, optional If an integer is passed, it is the number of folds (defaults to 3). Specific cross-validation objects can be passed, see sklearn.cross_validation module for the list of possible objects scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. n_jobs : integer, optional Number of jobs to run in parallel (default 1). pre_dispatch : integer or string, optional Number of predispatched jobs for parallel execution (default is all). The option can reduce the allocated memory. The string can be an expression like '2*n_jobs'. verbose : integer, optional Controls the verbosity: the higher, the more messages. Returns ------- train_scores : array, shape (n_ticks, n_cv_folds) Scores on training sets. test_scores : array, shape (n_ticks, n_cv_folds) Scores on test set. Notes ----- See :ref:`examples/model_selection/plot_validation_curve.py <example_model_selection_plot_validation_curve.py>` """ X, y = indexable(X, y) cv = check_cv(cv, X, y, classifier=is_classifier(estimator)) scorer = check_scoring(estimator, scoring=scoring) parallel = Parallel(n_jobs=n_jobs, pre_dispatch=pre_dispatch, verbose=verbose) out = parallel(delayed(_fit_and_score)( estimator, X, y, scorer, train, test, verbose, parameters={param_name: v}, fit_params=None, return_train_score=True) for train, test in cv for v in param_range) out = np.asarray(out)[:, :2] n_params = len(param_range) n_cv_folds = out.shape[0] // n_params out = out.reshape(n_cv_folds, n_params, 2).transpose((2, 1, 0)) return out[0], out[1]
bsd-3-clause
kmacinnis/sympy
sympy/physics/quantum/circuitplot.py
25
12934
"""Matplotlib based plotting of quantum circuits. Todo: * Optimize printing of large circuits. * Get this to work with single gates. * Do a better job checking the form of circuits to make sure it is a Mul of Gates. * Get multi-target gates plotting. * Get initial and final states to plot. * Get measurements to plot. Might need to rethink measurement as a gate issue. * Get scale and figsize to be handled in a better way. * Write some tests/examples! """ from __future__ import print_function, division from sympy import Mul from sympy.core.compatibility import u from sympy.external import import_module from sympy.physics.quantum.gate import Gate, OneQubitGate, CGate, CGateS from sympy.core.core import BasicMeta from sympy.core.assumptions import ManagedProperties __all__ = [ 'CircuitPlot', 'circuit_plot', 'labeller', 'Mz', 'Mx', 'CreateOneQubitGate', 'CreateCGate', ] np = import_module('numpy') matplotlib = import_module( 'matplotlib', __import__kwargs={'fromlist': ['pyplot']}, catch=(RuntimeError,)) # This is raised in environments that have no display. if not np or not matplotlib: class CircuitPlot(object): def __init__(*args, **kwargs): raise ImportError('numpy or matplotlib not available.') def circuit_plot(*args, **kwargs): raise ImportError('numpy or matplotlib not available.') else: pyplot = matplotlib.pyplot Line2D = matplotlib.lines.Line2D Circle = matplotlib.patches.Circle #from matplotlib import rc #rc('text',usetex=True) class CircuitPlot(object): """A class for managing a circuit plot.""" scale = 1.0 fontsize = 20.0 linewidth = 1.0 control_radius = 0.05 not_radius = 0.15 swap_delta = 0.05 labels = [] inits = {} label_buffer = 0.5 def __init__(self, c, nqubits, **kwargs): self.circuit = c self.ngates = len(self.circuit.args) self.nqubits = nqubits self.update(kwargs) self._create_grid() self._create_figure() self._plot_wires() self._plot_gates() self._finish() def update(self, kwargs): """Load the kwargs into the instance dict.""" self.__dict__.update(kwargs) def _create_grid(self): """Create the grid of wires.""" scale = self.scale wire_grid = np.arange(0.0, self.nqubits*scale, scale, dtype=float) gate_grid = np.arange(0.0, self.ngates*scale, scale, dtype=float) self._wire_grid = wire_grid self._gate_grid = gate_grid def _create_figure(self): """Create the main matplotlib figure.""" self._figure = pyplot.figure( figsize=(self.ngates*self.scale, self.nqubits*self.scale), facecolor='w', edgecolor='w' ) ax = self._figure.add_subplot( 1, 1, 1, frameon=True ) ax.set_axis_off() offset = 0.5*self.scale ax.set_xlim(self._gate_grid[0] - offset, self._gate_grid[-1] + offset) ax.set_ylim(self._wire_grid[0] - offset, self._wire_grid[-1] + offset) ax.set_aspect('equal') self._axes = ax def _plot_wires(self): """Plot the wires of the circuit diagram.""" xstart = self._gate_grid[0] xstop = self._gate_grid[-1] xdata = (xstart - self.scale, xstop + self.scale) for i in range(self.nqubits): ydata = (self._wire_grid[i], self._wire_grid[i]) line = Line2D( xdata, ydata, color='k', lw=self.linewidth ) self._axes.add_line(line) if self.labels: init_label_buffer = 0 if self.inits.get(self.labels[i]): init_label_buffer = 0.25 self._axes.text( xdata[0]-self.label_buffer-init_label_buffer,ydata[0], render_label(self.labels[i],self.inits), size=self.fontsize, color='k',ha='center',va='center') self._plot_measured_wires() def _plot_measured_wires(self): ismeasured = self._measurements() xstop = self._gate_grid[-1] dy = 0.04 # amount to shift wires when doubled # Plot doubled wires after they are measured for im in ismeasured: xdata = (self._gate_grid[ismeasured[im]],xstop+self.scale) ydata = (self._wire_grid[im]+dy,self._wire_grid[im]+dy) line = Line2D( xdata, ydata, color='k', lw=self.linewidth ) self._axes.add_line(line) # Also double any controlled lines off these wires for i,g in enumerate(self._gates()): if isinstance(g, CGate) or isinstance(g, CGateS): wires = g.controls + g.targets for wire in wires: if wire in ismeasured and \ self._gate_grid[i] > self._gate_grid[ismeasured[wire]]: ydata = min(wires), max(wires) xdata = self._gate_grid[i]-dy, self._gate_grid[i]-dy line = Line2D( xdata, ydata, color='k', lw=self.linewidth ) self._axes.add_line(line) def _gates(self): """Create a list of all gates in the circuit plot.""" gates = [] if isinstance(self.circuit, Mul): for g in reversed(self.circuit.args): if isinstance(g, Gate): gates.append(g) elif isinstance(self.circuit, Gate): gates.append(self.circuit) return gates def _plot_gates(self): """Iterate through the gates and plot each of them.""" for i, gate in enumerate(self._gates()): gate.plot_gate(self, i) def _measurements(self): """Return a dict {i:j} where i is the index of the wire that has been measured, and j is the gate where the wire is measured. """ ismeasured = {} for i,g in enumerate(self._gates()): if getattr(g,'measurement',False): for target in g.targets: if target in ismeasured: if ismeasured[target] > i: ismeasured[target] = i else: ismeasured[target] = i return ismeasured def _finish(self): # Disable clipping to make panning work well for large circuits. for o in self._figure.findobj(): o.set_clip_on(False) def one_qubit_box(self, t, gate_idx, wire_idx): """Draw a box for a single qubit gate.""" x = self._gate_grid[gate_idx] y = self._wire_grid[wire_idx] self._axes.text( x, y, t, color='k', ha='center', va='center', bbox=dict(ec='k', fc='w', fill=True, lw=self.linewidth), size=self.fontsize ) def two_qubit_box(self, t, gate_idx, wire_idx): """Draw a box for a two qubit gate. Doesn't work yet. """ x = self._gate_grid[gate_idx] y = self._wire_grid[wire_idx]+0.5 print(self._gate_grid) print(self._wire_grid) obj = self._axes.text( x, y, t, color='k', ha='center', va='center', bbox=dict(ec='k', fc='w', fill=True, lw=self.linewidth), size=self.fontsize ) def control_line(self, gate_idx, min_wire, max_wire): """Draw a vertical control line.""" xdata = (self._gate_grid[gate_idx], self._gate_grid[gate_idx]) ydata = (self._wire_grid[min_wire], self._wire_grid[max_wire]) line = Line2D( xdata, ydata, color='k', lw=self.linewidth ) self._axes.add_line(line) def control_point(self, gate_idx, wire_idx): """Draw a control point.""" x = self._gate_grid[gate_idx] y = self._wire_grid[wire_idx] radius = self.control_radius c = Circle( (x, y), radius*self.scale, ec='k', fc='k', fill=True, lw=self.linewidth ) self._axes.add_patch(c) def not_point(self, gate_idx, wire_idx): """Draw a NOT gates as the circle with plus in the middle.""" x = self._gate_grid[gate_idx] y = self._wire_grid[wire_idx] radius = self.not_radius c = Circle( (x, y), radius, ec='k', fc='w', fill=False, lw=self.linewidth ) self._axes.add_patch(c) l = Line2D( (x, x), (y - radius, y + radius), color='k', lw=self.linewidth ) self._axes.add_line(l) def swap_point(self, gate_idx, wire_idx): """Draw a swap point as a cross.""" x = self._gate_grid[gate_idx] y = self._wire_grid[wire_idx] d = self.swap_delta l1 = Line2D( (x - d, x + d), (y - d, y + d), color='k', lw=self.linewidth ) l2 = Line2D( (x - d, x + d), (y + d, y - d), color='k', lw=self.linewidth ) self._axes.add_line(l1) self._axes.add_line(l2) def circuit_plot(c, nqubits, **kwargs): """Draw the circuit diagram for the circuit with nqubits. Parameters ========== c : circuit The circuit to plot. Should be a product of Gate instances. nqubits : int The number of qubits to include in the circuit. Must be at least as big as the largest `min_qubits`` of the gates. """ return CircuitPlot(c, nqubits, **kwargs) def render_label(label, inits={}): """Slightly more flexible way to render labels. >>> from sympy.physics.quantum.circuitplot import render_label >>> render_label('q0') '$|q0\\\\rangle$' >>> render_label('q0', {'q0':'0'}) '$|q0\\\\rangle=|0\\\\rangle$' """ init = inits.get(label) if init: return r'$|%s\rangle=|%s\rangle$' % (label, init) return r'$|%s\rangle$' % label def labeller(n, symbol='q'): """Autogenerate labels for wires of quantum circuits. Parameters ========== n : int number of qubits in the circuit symbol : string A character string to precede all gate labels. E.g. 'q_0', 'q_1', etc. >>> from sympy.physics.quantum.circuitplot import labeller >>> labeller(2) ['q_1', 'q_0'] >>> labeller(3,'j') ['j_2', 'j_1', 'j_0'] """ return ['%s_%d' % (symbol,n-i-1) for i in range(n)] class Mz(OneQubitGate): """Mock-up of a z measurement gate. This is in circuitplot rather than gate.py because it's not a real gate, it just draws one. """ measurement = True gate_name='Mz' gate_name_latex=u('M_z') class Mx(OneQubitGate): """Mock-up of an x measurement gate. This is in circuitplot rather than gate.py because it's not a real gate, it just draws one. """ measurement = True gate_name='Mx' gate_name_latex=u('M_x') class CreateOneQubitGate(ManagedProperties): def __new__(mcl, name, latexname=None): if not latexname: latexname = name return BasicMeta.__new__(mcl, name + "Gate", (OneQubitGate,), {'gate_name': name, 'gate_name_latex': latexname}) def CreateCGate(name, latexname=None): """Use a lexical closure to make a controlled gate. """ if not latexname: latexname = name onequbitgate = CreateOneQubitGate(name, latexname) def ControlledGate(ctrls,target): return CGate(tuple(ctrls),onequbitgate(target)) return ControlledGate
bsd-3-clause
camallen/aggregation
experimental/mapreduce/reducer/DBSCAN.py
2
1655
#!/usr/bin/env python import sys from sklearn.cluster import DBSCAN import numpy as np #read in the type for each of the parameters - continuous vs. discrete - we will ignore discrete values additional_param_type = [] configFile = sys.argv[1] with open(configFile, 'r') as conf: configuration = conf.read() exec(configuration) param_type = ["continuous","continuous"] param_type.extend(additional_param_type) curr_subject = None pts = [] # input comes from STDIN (standard input) for line in sys.stdin: subject_id,v = line.split("\t") v = v[:-1] if curr_subject != subject_id: if curr_subject is not None: X = np.array(pts) db = DBSCAN(eps=100, min_samples=1).fit(X) core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True cluster_id = db.labels_ unique_ids = set(cluster_id) sys.stdout.write(subject_id+","+str(len(pts[0]))) for id in unique_ids: if id == -1: continue in_cluster = [p for i,p in enumerate(pts) if cluster_id[i] == id] #print in_cluster center = [np.mean(c) for c in zip(*in_cluster)] for c in center: sys.stdout.write(","+ str(c)) print curr_subject = subject_id param = v.split(",") gold_standard = (param[0] == "True") user_id = param[1] #treat the rest of the parameters as a multi-dimensional point pts.append([float(p) for i,p in enumerate(param[2:]) if param_type[i] == "continuous"])
apache-2.0
dwweiss/pmLib
src/flow/initialTurbulenceEnergyDissipation.py
1
3650
""" Copyright (c) 2016-17 by Dietmar W Weiss This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org. Version: 2017-12-06 DWW """ def initialTurbulenceEnergyAndDissipation(v, D, nu): """ Turbulence kinetic energy and dissipation rate Args: v (float): axial component of velocity [m/s] D (float): inner pipe diameter [m] nu (float): kinematic viscosity [m^2/s] Returns: k_turb (float): turbulent kinetic energy [m^2/s^2] eps_turb (float): turbulence dissipation rate [m/s^2] """ C_u = 0.09 # turbulence model constant [m/s^2] Re = v * D / nu # Reynolds number [/] I = 0.16 * Re**-0.125 # turbulence intensity [/] l = 0.07 * D # turbulence length scale [m] k_turb = 1.5 * (v * I)**2 # turb. kinetic energy [m^2/s^2] eps_turb = C_u**0.75 * k_turb**1.5 / l # turb. dissipation rate [m/s^2] return k_turb, eps_turb # Example ##################################################################### if __name__ == '__main__': import numpy as np import matplotlib.pyplot as plt D = 25e-3 nu = 1e-6 v_seq = [0.1, 0.2, 0.5, 1, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 7, 8, 9, 10] nu_seq = [1E-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2] for nu in nu_seq: kk, ee = [], [] for v in v_seq: k, eps = initialTurbulenceEnergyAndDissipation(v, D, nu) kk.append(k) ee.append(eps) plt.plot(v_seq, kk, ls='--', label=r'$k(\nu:'+str(nu*1e6)+')$') plt.plot(v_seq, ee, label=r'$\varepsilon(\nu:'+str(nu*1e6)+')$') fontsize = 12 plt.title(r'Axial velocity $v_z(r) = f(n, \nu)$') plt.rcParams.update({'font.size': fontsize}) plt.rcParams['legend.fontsize'] = fontsize plt.xlabel('$v$ [m/s]') plt.ylabel(r'$k, \varepsilon$ [/]') plt.yscale('log', nonposy='clip') plt.grid() plt.legend(bbox_to_anchor=(1.1, 1.03), loc='upper left') plt.show() # same as above, y-axis is linear for nu in nu_seq: kk, ee = [], [] for v in v_seq: k, eps = initialTurbulenceEnergyAndDissipation(v, D, nu) kk.append(k) ee.append(eps) plt.plot(v_seq, kk, ls='--', label=r'$k(\nu:'+str(nu*1e6)+')$') # plt.plot(v_seq, ee, label=r'$\varepsilon(\nu:'+str(nu*1e6)+')$') fontsize = 12 plt.title(r'Axial velocity $v_z(r) = f(n, \nu)$') plt.rcParams.update({'font.size': fontsize}) plt.rcParams['legend.fontsize'] = fontsize plt.xlabel('$v$ [m/s]') plt.ylabel(r'$k, \varepsilon$ [/]') plt.grid() plt.legend(bbox_to_anchor=(1.1, 1.03), loc='upper left') plt.show()
lgpl-3.0
SANDAG/orca
orca/server/tests/test_server.py
2
10586
import json import orca import pandas as pd import pandas.util.testing as pdt import pytest from .. import server @pytest.fixture def tapp(): server.app.config['TESTING'] = True return server.app.test_client() @pytest.fixture(scope='module') def dfa(): return pd.DataFrame( {'a': [100, 200, 300, 200, 100]}, index=['v', 'w', 'x', 'y', 'z']) @pytest.fixture(scope='module') def dfb(): return pd.DataFrame( {'b': [70, 80, 90], 'a_id': ['w', 'v', 'z']}, index=['a', 'b', 'b']) @pytest.fixture(scope='module') def dfa_col(dfa): return pd.Series([2, 4, 6, 8, 10], index=dfa.index) @pytest.fixture(scope='module') def dfb_col(dfb): return pd.Series([10, 20, 30], index=dfb.index) @pytest.fixture(scope='module') def dfa_factor(): return 0.5 @pytest.fixture(scope='module') def dfb_factor(): return 2 @pytest.fixture(scope='module', autouse=True) def setup_orca(dfa, dfb, dfa_col, dfb_col, dfa_factor, dfb_factor): orca.add_injectable('a_factor', dfa_factor) @orca.injectable() def b_factor(): return dfb_factor orca.add_table('dfa', dfa) @orca.table('dfb') def dfb_table(): return dfb orca.add_column('dfa', 'acol', dfa_col) orca.add_column('dfb', 'bcol', dfb_col) @orca.column('dfa') def extra_acol(a_factor): return dfa_col * a_factor @orca.column('dfb') def extra_bcol(b_factor): return dfb_col * b_factor orca.broadcast('dfb', 'dfa', cast_on='a_id', onto_index=True) @orca.step() def test_step(dfa, dfb): pass def test_schema(tapp): rv = tapp.get('/schema') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert set(data['tables']) == {'dfa', 'dfb'} assert set(data['columns']['dfa']) == {'extra_acol', 'acol', 'a'} assert set(data['columns']['dfb']) == {'bcol', 'extra_bcol', 'a_id', 'b'} assert data['steps'] == ['test_step'] assert set(data['injectables']) == {'a_factor', 'b_factor'} assert data['broadcasts'] == [['dfb', 'dfa']] def test_list_tables(tapp): rv = tapp.get('/tables') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert set(data['tables']) == {'dfa', 'dfb'} def test_table_info(tapp): rv = tapp.get('/tables/dfa/info') assert rv.status_code == 200 data = rv.data.decode('utf-8') assert 'extra_acol' in data def test_table_preview(tapp): rv = tapp.get('/tables/dfa/preview') assert rv.status_code == 200 data = rv.data.decode('utf-8') assert data == orca.get_table('dfa').to_frame().to_json(orient='split') def test_table_preview_404(tapp): rv = tapp.get('/tables/not_a_table/preview') assert rv.status_code == 404 def test_table_describe(tapp): rv = tapp.get('/tables/dfa/describe') assert rv.status_code == 200 data = rv.data.decode('utf-8') assert data == \ orca.get_table('dfa').to_frame().describe().to_json(orient='split') def test_table_definition_frame(tapp): rv = tapp.get('/tables/dfa/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'type': 'dataframe'} def test_table_definition_func(tapp): rv = tapp.get('/tables/dfb/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['type'] == 'function' assert data['filename'].endswith('test_server.py') assert isinstance(data['lineno'], int) assert data['text'] == ( " @orca.table('dfb')\n" " def dfb_table():\n" " return dfb\n") assert 'dfb_table' in data['html'] def test_table_csv(tapp): rv = tapp.get('/tables/dfb/csv') assert rv.status_code == 200 data = rv.data.decode('utf-8') assert rv.mimetype == 'text/csv' assert data == orca.get_table('dfb').to_frame().to_csv() def test_list_table_columns(tapp): rv = tapp.get('/tables/dfb/columns') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert set(data['columns']) == {'a_id', 'b', 'bcol', 'extra_bcol'} def test_column_definition_local(tapp): rv = tapp.get('/tables/dfa/columns/a/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'type': 'local'} def test_column_definition_series(tapp): rv = tapp.get('/tables/dfa/columns/acol/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'type': 'series'} def test_column_definition_func(tapp): rv = tapp.get('/tables/dfa/columns/extra_acol/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['type'] == 'function' assert data['filename'].endswith('test_server.py') assert isinstance(data['lineno'], int) assert data['text'] == ( " @orca.column('dfa')\n" " def extra_acol(a_factor):\n" " return dfa_col * a_factor\n") assert 'extra_acol' in data['html'] def test_column_describe(tapp): rv = tapp.get('/tables/dfa/columns/extra_acol/describe') assert rv.status_code == 200 data = rv.data.decode('utf-8') assert data == \ orca.get_table('dfa').extra_acol.describe().to_json(orient='split') def test_column_csv(tapp, dfa): rv = tapp.get('/tables/dfa/columns/a/csv') assert rv.status_code == 200 data = rv.data.decode('utf-8') assert data == dfa.a.to_csv(path=None) def test_no_column_404(tapp): rv = tapp.get('/tables/dfa/columns/not-a-column/csv') assert rv.status_code == 404 def test_list_injectables(tapp): rv = tapp.get('/injectables') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert set(data['injectables']) == {'a_factor', 'b_factor'} def test_injectable_repr(tapp, dfb_factor): rv = tapp.get('/injectables/b_factor/repr') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'type': str(type(42)), 'repr': '2'} def test_no_injectable_404(tapp): rv = tapp.get('/injectables/nope/repr') assert rv.status_code == 404 def test_injectable_definition_var(tapp): rv = tapp.get('/injectables/a_factor/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'type': 'variable'} def test_injectable_definition_func(tapp): rv = tapp.get('/injectables/b_factor/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['type'] == 'function' assert data['filename'].endswith('test_server.py') assert isinstance(data['lineno'], int) assert data['text'] == ( " @orca.injectable()\n" " def b_factor():\n" " return dfb_factor\n") assert 'b_factor' in data['html'] def test_list_broadcasts(tapp): rv = tapp.get('/broadcasts') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'broadcasts': [{'cast': 'dfb', 'onto': 'dfa'}]} def test_broadcast_definition(tapp): rv = tapp.get('/broadcasts/dfb/dfa/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == { 'cast': 'dfb', 'onto': 'dfa', 'cast_on': 'a_id', 'onto_on': None, 'cast_index': False, 'onto_index': True} def test_no_broadcast_404(tapp): rv = tapp.get('/broadcasts/table1/table2/definition') assert rv.status_code == 404 def test_list_steps(tapp): rv = tapp.get('/steps') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == {'steps': ['test_step']} def test_no_step_404(tapp): rv = tapp.get('/steps/not_a_step/definition') assert rv.status_code == 404 def test_step_definition(tapp): rv = tapp.get('/steps/test_step/definition') assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['filename'].endswith('test_server.py') assert isinstance(data['lineno'], int) assert data['text'] == ( " @orca.step()\n" " def test_step(dfa, dfb):\n" " pass\n") assert 'test_step' in data['html'] def test_table_groupbyagg_errors(tapp): # non-existant column rv = tapp.get('/tables/dfa/groupbyagg?column=notacolumn') assert rv.status_code == 400 # both by and level missing rv = tapp.get('/tables/dfa/groupbyagg?column=a') assert rv.status_code == 400 # bad or missing agg type rv = tapp.get('/tables/dfa/groupbyagg?column=a&level=0&agg=notanagg') assert rv.status_code == 400 def test_table_groupbyagg_by_size(tapp): rv = tapp.get('/tables/dfa/groupbyagg?by=a&column=a&agg=size') assert rv.status_code == 200 data = rv.data.decode('utf-8') test = pd.read_json(data, orient='split', typ='series') pdt.assert_series_equal( test, pd.Series([2, 2, 1], index=[100, 200, 300])) def test_table_groupbyagg_level_mean(tapp): rv = tapp.get('/tables/dfb/groupbyagg?level=0&column=b&agg=mean') assert rv.status_code == 200 data = rv.data.decode('utf-8') test = pd.read_json(data, orient='split', typ='series') pdt.assert_series_equal( test, pd.Series([70, 85], index=['a', 'b'], name='b')) def test_table_groupbyagg_level_median(tapp): rv = tapp.get('/tables/dfb/groupbyagg?level=0&column=b&agg=median') assert rv.status_code == 200 data = rv.data.decode('utf-8') test = pd.read_json(data, orient='split', typ='series') pdt.assert_series_equal( test, pd.Series([70, 85], index=['a', 'b'], name='b')) def test_table_groupbyagg_level_sum(tapp): rv = tapp.get('/tables/dfb/groupbyagg?level=0&column=b&agg=sum') assert rv.status_code == 200 data = rv.data.decode('utf-8') test = pd.read_json(data, orient='split', typ='series') pdt.assert_series_equal( test, pd.Series([70, 170], index=['a', 'b'], name='b')) def test_table_groupbyagg_level_std(tapp): rv = tapp.get('/tables/dfb/groupbyagg?level=0&column=b&agg=std') assert rv.status_code == 200 data = rv.data.decode('utf-8') test = pd.read_json(data, orient='split', typ='series') pdt.assert_series_equal( test, pd.Series( [pd.np.nan, pd.Series([80, 90]).std()], index=['a', 'b'], name='b'))
bsd-3-clause
hansomesong/TracesAnalyzer
20160218Tasks/dissimilarity_Benoit.py
1
131080
# -*- coding: utf-8 -*- __author__ = 'yueli' import matplotlib.pyplot as plt from config.config import * import numpy as np from collections import Counter import math plt.rc('xtick', labelsize='45') plt.rc('ytick', labelsize='45') import matplotlib as mpl mpl.rcParams['text.usetex'] = True mpl.rcParams.update({'figure.autolayout': True}) # From the obtained original path file in column B of statistic_all.csv to compute the dissimilarity of its # corresponding <EID,MR,VP>, and then return the result # input = original file path # e.g.: "/Users/yueli/Documents/Codes/Luigi_Codes/PlanetLab_20140716/ucl/mappings/onelab1-EID-0.0.0.0-MR-149.20.48.61.log" # output = dissimilarity value for this file def dissimilarity_calculator(origin_file_path): # build a .csv file by using the original file path probe = origin_file_path.split("/")[7] file_name = "{0}.csv".format(origin_file_path.split("/")[9]) file = os.path.join(PLANET_CSV_DIR, probe, file_name) rloc_list_temp = [] rloc_list_current =[] dis_list = [] with open(file) as f_handler: next(f_handler) for line in f_handler: lines = line.split(";") if lines[0] == 'RoundNoReply': continue else: for rloc in lines[14:]: if not rloc: continue else: rloc_list_current.append(rloc.split(",")[1]) if not rloc_list_temp: rloc_list_temp = rloc_list_current rloc_list_current = [] continue # rloc_list_temp has been filled, begin to compare if rloc_list_current == rloc_list_temp: rloc_list_temp = rloc_list_current rloc_list_current = [] else: # Begin to compute the dissimilarity print "temp & current =", set(rloc_list_temp) & set(rloc_list_current) print "temp | current =", set(rloc_list_temp) | set(rloc_list_current) dis_list.append(1 - float(len(set(rloc_list_temp) & set(rloc_list_current)))/float(len(set(rloc_list_temp) | set(rloc_list_current)))) print "dis_list =", dis_list if not dis_list: dissimilarity_value = 0 else: dissimilarity_value = np.mean(dis_list) return dissimilarity_value # pdf list ==> cdf list # input = pdf_lidt[pdf1, pdf2, ...] # output = cdf_list[cdf1, cdf2, ...] def cdf_from_pdf_list(pdf_list): cdf_list = [] for value in pdf_list: if not cdf_list: cdf_list.append(value) else: cdf_list.append(value + cdf_list[-1]) return cdf_list if __name__ == '__main__': # with open(os.path.join(CSV_FILE_DESTDIR, 'statistic_all.csv')) as statistic_file: # dissimilarity_list = [] # # next(statistic_file) # for line in statistic_file: # lines = line.split(";") # if lines[5] == 'True': # dissimilarity_list.append(0) # elif 'NegativeReply' in lines[9].split(",") and 'RoundNormal' in lines[9].split(","): # dissimilarity_list.append(1) # else: # dissimilarity_list.append(dissimilarity_calculator(lines[1])) # # print "dissimilarity_list =", dissimilarity_list # To save time, once the above codes hava been executed once, we can record its result in dissimilarity_list, # so that the figure can be directly plotted from this list dissimilarity_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333337, 0.33333333333333343, 0.33333333333333337, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333348, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0.33333333333333343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.66716196136701356, 0.67013888888888906, 0.66765432098765454, 0.66716196136701356, 0.66864784546805367, 0.6671626984126986, 0.67013372956909378, 0.66765578635014855, 0.66716196136701356, 0.6716196136701339, 0.6671626984126986, 0.67013372956909378, 0.66765578635014855, 0.66716196136701356, 0.66716196136701356, 0.6671626984126986, 0.67013372956909378, 0.66765578635014855, 0.667163437655241, 0.66865671641791058, 0.66716196136701356, 0.67013372956909378, 0.66765432098765454, 0.66716196136701356, 0.6716196136701339, 0.66716196136701356, 0.667163437655241, 0.66765578635014855, 0.66766020864381537, 0.66914314016840037, 0.66716196136701356, 0.67013372956909378, 0.66765432098765454, 1, 0.6716196136701339, 0.66716492277030404, 0.66716566866267479, 0.66765873015873034, 0.66716641679160438, 0.66867167919799519, 0.66716196136701356, 0.67013372956909378, 0.66765432098765454, 0.66716196136701356, 0.6716196136701339, 0.66716196136701356, 0.67013372956909378, 0.66765578635014855, 0.66716196136701356, 0.6716196136701339, 0.66716196136701356, 0.67013372956909378, 0.66765432098765454, 0.66716196136701356, 0.6716196136701339, 0.66716196136701356, 0.67013372956909378, 0.66765578635014855, 0.66716196136701356, 0.66716196136701356, 0.66716196136701356, 0.67013372956909378, 0.66765578635014855, 0.66716196136701356, 0.66864784546805367, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.66759517177344496, 0.66851851851851873, 0.66851338873499566, 0.66898470097357465, 0.66805942432683407, 1, 0.67037037037037051, 1, 0.67037552155771929, 1, 0.66759906759906784, 0.66851851851851873, 0.66805362921867795, 0.66759388038943002, 0.66713027352804843, 0.6675964667596469, 0.66852367688022307, 0.66851851851851873, 0.66760037348272672, 0.66805942432683407, 1, 0.67037037037037051, 1, 0.67037552155771929, 0.66805942432683407, 0.66759517177344496, 0.66851851851851873, 0.66851595006934827, 0.67501159017153478, 0.66713027352804843, 1, 0.67037037037037051, 1, 1, 1, 0.66759776536312865, 0.66852886405959056, 0.66852109411219307, 0.66900093370681635, 0.6680691912108464, 0.66805555555555574, 1, 1, 0.67037552155771929, 1, 0.66805555555555574, 0.67037037037037051, 0.66851338873499566, 0.67037552155771929, 0.66805748725081149, 0.66805555555555574, 0.67037037037037051, 0.66851595006934827, 0.67037552155771929, 1, 0.66759517177344496, 0.67037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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.66708807416772042, 0.66718106995884796, 0.66714697406340073, 0.66821705426356626, 0.66727053140096637, 0.66708807416772042, 1, 1, 0.66718106995884796, 0.66759517177344496, 0.66834592779177171, 1, 0.33585222502099088, 0.33417296389588586, 0.66833960685905502, 0.66750524109014686, 0.33416770963704645, 0.66708648194794296, 0.33834586466165423, 0.33667502088554729, 0.66727828746177387, 0, 0, 0, 0.66726726726726748, 0.66708860759493682, 0.66718186501803212, 0.66714697406340073, 1, 0.66727272727272746, 0.66708807416772042, 0.66748366013071903, 1, 0.66773847802786712, 0.66727053140096637, 0.33588110403397037, 0.33502109704641359, 0.66708754208754228, 0.66751376535366391, 0.33417508417508429, 0.66708807416772042, 0.66732935719019226, 0.66714628297362133, 0.66718106995884796, 0.6682242990654208, 1, 0.66951566951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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.50187969924812026, 0.50253164556962027, 0.50125628140703515, 0.50062656641604009, 1, 0.50187969924812026, 0.50316856780735109, 0.50125628140703515, 0.50062656641604009, 1, 0.50188679245283019, 0.50125944584382875, 0.50062735257214552, 0.50125786163522013, 0.50062578222778475, 1, 1, 0.50062972292191432, 1, 0.50062656641604009, 0.50187969924812026, 0.50191326530612246, 0.50125313283208017, 0.50062656641604009, 0.50062499999999999, 0.50187969924812026, 0.50254777070063694, 0.50125470514429105, 0.50062814070351758, 0.50062578222778475, 0.50187969924812026, 0.5018987341772152, 0.50125470514429105, 0.50062656641604009, 0.50062499999999999, 0.50189633375474085, 0.50254129606099107, 0.50063291139240507, 0.50063371356147024, 0.50063131313131315, 0.50187969924812026, 0.50190114068441061, 0.50125470514429105, 1, 1, 1, 0.50252525252525249, 0.50125628140703515, 0.50062656641604009, 0.50062499999999999, 0.50187969924812026, 0.50127226463104324, 0.50125470514429105, 0.50062656641604009, 0.50062499999999999, 0.50187969924812026, 0.50125786163522013, 0.50062578222778475, 0.50187969924812026, 0.50062578222778475, 0.51005025125628145, 0.50126903553299496, 0.50062735257214552, 0.50126422250316061, 0.50062578222778475, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.64133333333333364, 0.5, 0.64133333333333364, 1, 0.5, 0.64133333333333364, 0.5, 0.64133333333333364, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.64112903225806472, 0.5, 0.64133333333333364, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.64112903225806472, 0.5, 0.64133333333333364, 1, 0.5, 0.64112903225806472, 0.5, 0.64133333333333364, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # After getting the dissimilarity of all <EID, MR, VP>, we compute the PDF & CDF of dissimilarity_list dissimilarity_int_list = [math.floor(i*100) for i in dissimilarity_list] dissimilarity_dict = dict(Counter(dissimilarity_int_list)) dissimilarity_dict = sorted(dissimilarity_dict.items(), key=lambda e:e[0]) print "dissimilarity_dict =", dissimilarity_dict x_values = [ i[0] for i in dissimilarity_dict[1:]] # Damien doesn't let plot the stable value, i.e., the first value y_values = [ i[1] for i in dissimilarity_dict[1:]] # Damien doesn't let plot the stable value, i.e., the first value y_pdf = [float(i)/float(sum(y_values))*100 for i in y_values] y_cdf = cdf_from_pdf_list(y_pdf) print "X:", x_values print "Y:", y_values print "Y pdf =", y_pdf print "Y cdf =", y_cdf # # ############### Plot Part # # Modify the size and dpi of picture, default size is (8,6), default dpi is 80 # or this command to set the figure size: # plt.figure(figsize=(15, 12)) # plt.gcf().set_size_inches(11,9) fig = plt.figure() ax = fig.add_axes([0.23, 0.23, 0.85, 0.83]) # plt.tight_layout() plt.plot(x_values, y_cdf, c='black', linewidth=5) plt.axis([x_values[0], 100, 0, 100], font) plt.xlabel(r'\mathrm{dissimilarity}', font) plt.ylabel(r'\mathrm{cdf (\%)}', font) locs = [ 40, 50, 60, 70, 80, 90, 100] n_labels = ['$0.4$', '$0.5$', '$0.6$', '$0.7$', '$0.8$', '$0.9$', '$1.0$'] plt.xticks(locs, n_labels) plt.grid(True, color='gray', linestyle='dashed') plt.gca().xaxis.grid(True, color='gray', linestyle='dashed', which='minor') plt.savefig(os.path.join(PLOT_DIR, 'Plot_newSize', 'toto.png'))
gpl-2.0
astocko/statsmodels
statsmodels/genmod/cov_struct.py
6
40352
from statsmodels.compat.python import iterkeys, itervalues, zip, range from statsmodels.stats.correlation_tools import cov_nearest import numpy as np import pandas as pd from scipy import linalg as spl from collections import defaultdict from statsmodels.tools.sm_exceptions import (ConvergenceWarning, IterationLimitWarning) import warnings """ Some details for the covariance calculations can be found in the Stata docs: http://www.stata.com/manuals13/xtxtgee.pdf """ class CovStruct(object): """ A base class for correlation and covariance structures of grouped data. Each implementation of this class takes the residuals from a regression model that has been fitted to grouped data, and uses them to estimate the within-group dependence structure of the random errors in the model. The state of the covariance structure is represented through the value of the class variable `dep_params`. The default state of a newly-created instance should correspond to the identity correlation matrix. """ def __init__(self, cov_nearest_method="clipped"): # Parameters describing the dependency structure self.dep_params = None # Keep track of the number of times that the covariance was # adjusted. self.cov_adjust = [] # Method for projecting the covariance matrix if it not SPD. self.cov_nearest_method = cov_nearest_method def initialize(self, model): """ Called by GEE, used by implementations that need additional setup prior to running `fit`. Parameters ---------- model : GEE class A reference to the parent GEE class instance. """ self.model = model def update(self, params): """ Updates the association parameter values based on the current regression coefficients. Parameters ---------- params : array-like Working values for the regression parameters. """ raise NotImplementedError def covariance_matrix(self, endog_expval, index): """ Returns the working covariance or correlation matrix for a given cluster of data. Parameters ---------- endog_expval: array-like The expected values of endog for the cluster for which the covariance or correlation matrix will be returned index: integer The index of the cluster for which the covariane or correlation matrix will be returned Returns ------- M: matrix The covariance or correlation matrix of endog is_cor: bool True if M is a correlation matrix, False if M is a covariance matrix """ raise NotImplementedError def covariance_matrix_solve(self, expval, index, stdev, rhs): """ Solves matrix equations of the form `covmat * soln = rhs` and returns the values of `soln`, where `covmat` is the covariance matrix represented by this class. Parameters ---------- expval: array-like The expected value of endog for each observed value in the group. index: integer The group index. stdev : array-like The standard deviation of endog for each observation in the group. rhs : list/tuple of array-like A set of right-hand sides; each defines a matrix equation to be solved. Returns ------- soln : list/tuple of array-like The solutions to the matrix equations. Notes ----- Returns None if the solver fails. Some dependence structures do not use `expval` and/or `index` to determine the correlation matrix. Some families (e.g. binomial) do not use the `stdev` parameter when forming the covariance matrix. If the covariance matrix is singular or not SPD, it is projected to the nearest such matrix. These projection events are recorded in the fit_history member of the GEE model. Systems of linear equations with the covariance matrix as the left hand side (LHS) are solved for different right hand sides (RHS); the LHS is only factorized once to save time. This is a default implementation, it can be reimplemented in subclasses to optimize the linear algebra according to the struture of the covariance matrix. """ vmat, is_cor = self.covariance_matrix(expval, index) if is_cor: vmat *= np.outer(stdev, stdev) # Factor the covariance matrix. If the factorization fails, # attempt to condition it into a factorizable matrix. threshold = 1e-2 success = False cov_adjust = 0 for itr in range(20): try: vco = spl.cho_factor(vmat) success = True break except np.linalg.LinAlgError: vmat = cov_nearest(vmat, method=self.cov_nearest_method, threshold=threshold) threshold *= 2 cov_adjust += 1 self.cov_adjust.append(cov_adjust) # Last resort if we still can't factor the covariance matrix. if success == False: warnings.warn("Unable to condition covariance matrix to an SPD matrix using cov_nearest", ConvergenceWarning) vmat = np.diag(np.diag(vmat)) vco = spl.cho_factor(vmat) soln = [spl.cho_solve(vco, x) for x in rhs] return soln def summary(self): """ Returns a text summary of the current estimate of the dependence structure. """ raise NotImplementedError class Independence(CovStruct): """ An independence working dependence structure. """ # Nothing to update def update(self, params): return def covariance_matrix(self, expval, index): dim = len(expval) return np.eye(dim, dtype=np.float64), True def covariance_matrix_solve(self, expval, index, stdev, rhs): v = stdev**2 rslt = [] for x in rhs: if x.ndim == 1: rslt.append(x / v) else: rslt.append(x / v[:, None]) return rslt update.__doc__ = CovStruct.update.__doc__ covariance_matrix.__doc__ = CovStruct.covariance_matrix.__doc__ covariance_matrix_solve.__doc__ = CovStruct.covariance_matrix_solve.__doc__ def summary(self): return "Observations within a cluster are modeled as being independent." class Exchangeable(CovStruct): """ An exchangeable working dependence structure. """ def __init__(self): super(Exchangeable, self).__init__() # The correlation between any two values in the same cluster self.dep_params = 0. def update(self, params): endog = self.model.endog_li nobs = self.model.nobs varfunc = self.model.family.variance cached_means = self.model.cached_means has_weights = self.model.weights is not None weights_li = self.model.weights residsq_sum, scale = 0, 0 fsum1, fsum2, n_pairs = 0., 0., 0. for i in range(self.model.num_group): expval, _ = cached_means[i] stdev = np.sqrt(varfunc(expval)) resid = (endog[i] - expval) / stdev f = weights_li[i] if has_weights else 1. ngrp = len(resid) residsq = np.outer(resid, resid) scale += f * np.trace(residsq) fsum1 += f * len(endog[i]) residsq = np.tril(residsq, -1) residsq_sum += f * residsq.sum() npr = 0.5 * ngrp * (ngrp - 1) fsum2 += f * npr n_pairs += npr ddof = self.model.ddof_scale scale /= (fsum1 * (nobs - ddof) / float(nobs)) residsq_sum /= scale self.dep_params = residsq_sum / (fsum2 * (n_pairs - ddof) / float(n_pairs)) def covariance_matrix(self, expval, index): dim = len(expval) dp = self.dep_params * np.ones((dim, dim), dtype=np.float64) np.fill_diagonal(dp, 1) return dp, True def covariance_matrix_solve(self, expval, index, stdev, rhs): k = len(expval) c = self.dep_params / (1. - self.dep_params) c /= 1. + self.dep_params * (k - 1) rslt = [] for x in rhs: if x.ndim == 1: x1 = x / stdev y = x1 / (1. - self.dep_params) y -= c * sum(x1) y /= stdev else: x1 = x / stdev[:, None] y = x1 / (1. - self.dep_params) y -= c * x1.sum(0) y /= stdev[:, None] rslt.append(y) return rslt update.__doc__ = CovStruct.update.__doc__ covariance_matrix.__doc__ = CovStruct.covariance_matrix.__doc__ covariance_matrix_solve.__doc__ = CovStruct.covariance_matrix_solve.__doc__ def summary(self): return ("The correlation between two observations in the " + "same cluster is %.3f" % self.dep_params) class Nested(CovStruct): """ A nested working dependence structure. A working dependence structure that captures a nested hierarchy of groups, each level of which contributes to the random error term of the model. When using this working covariance structure, `dep_data` of the GEE instance should contain a n_obs x k matrix of 0/1 indicators, corresponding to the k subgroups nested under the top-level `groups` of the GEE instance. These subgroups should be nested from left to right, so that two observations with the same value for column j of `dep_data` should also have the same value for all columns j' < j (this only applies to observations in the same top-level cluster given by the `groups` argument to GEE). Examples -------- Suppose our data are student test scores, and the students are in classrooms, nested in schools, nested in school districts. The school district is the highest level of grouping, so the school district id would be provided to GEE as `groups`, and the school and classroom id's would be provided to the Nested class as the `dep_data` argument, e.g. 0 0 # School 0, classroom 0, student 0 0 0 # School 0, classroom 0, student 1 0 1 # School 0, classroom 1, student 0 0 1 # School 0, classroom 1, student 1 1 0 # School 1, classroom 0, student 0 1 0 # School 1, classroom 0, student 1 1 1 # School 1, classroom 1, student 0 1 1 # School 1, classroom 1, student 1 Labels lower in the hierarchy are recycled, so that student 0 in classroom 0 is different fro student 0 in classroom 1, etc. Notes ----- The calculations for this dependence structure involve all pairs of observations within a group (that is, within the top level `group` structure passed to GEE). Large group sizes will result in slow iterations. The variance components are estimated using least squares regression of the products r*r', for standardized residuals r and r' in the same group, on a vector of indicators defining which variance components are shared by r and r'. """ def initialize(self, model): """ Called on the first call to update `ilabels` is a list of n_i x n_i matrices containing integer labels that correspond to specific correlation parameters. Two elements of ilabels[i] with the same label share identical variance components. `designx` is a matrix, with each row containing dummy variables indicating which variance components are associated with the corresponding element of QY. """ super(Nested, self).initialize(model) if self.model.weights is not None: warnings.warn("weights not implemented for nested cov_struct, using unweighted covariance estimate") # A bit of processing of the nest data id_matrix = np.asarray(self.model.dep_data) if id_matrix.ndim == 1: id_matrix = id_matrix[:,None] self.id_matrix = id_matrix endog = self.model.endog_li designx, ilabels = [], [] # The number of layers of nesting n_nest = self.id_matrix.shape[1] for i in range(self.model.num_group): ngrp = len(endog[i]) glab = self.model.group_labels[i] rix = self.model.group_indices[glab] # Determine the number of common variance components # shared by each pair of observations. ix1, ix2 = np.tril_indices(ngrp, -1) ncm = (self.id_matrix[rix[ix1], :] == self.id_matrix[rix[ix2], :]).sum(1) # This is used to construct the working correlation # matrix. ilabel = np.zeros((ngrp, ngrp), dtype=np.int32) ilabel[[ix1, ix2]] = ncm + 1 ilabel[[ix2, ix1]] = ncm + 1 ilabels.append(ilabel) # This is used to estimate the variance components. dsx = np.zeros((len(ix1), n_nest+1), dtype=np.float64) dsx[:,0] = 1 for k in np.unique(ncm): ii = np.flatnonzero(ncm == k) dsx[ii, 1:k+1] = 1 designx.append(dsx) self.designx = np.concatenate(designx, axis=0) self.ilabels = ilabels svd = np.linalg.svd(self.designx, 0) self.designx_u = svd[0] self.designx_s = svd[1] self.designx_v = svd[2].T def update(self, params): endog = self.model.endog_li nobs = self.model.nobs dim = len(params) if self.designx is None: self._compute_design(self.model) cached_means = self.model.cached_means varfunc = self.model.family.variance dvmat = [] scale = 0. for i in range(self.model.num_group): expval, _ = cached_means[i] stdev = np.sqrt(varfunc(expval)) resid = (endog[i] - expval) / stdev ix1, ix2 = np.tril_indices(len(resid), -1) dvmat.append(resid[ix1] * resid[ix2]) scale += np.sum(resid**2) dvmat = np.concatenate(dvmat) scale /= (nobs - dim) # Use least squares regression to estimate the variance # components vcomp_coeff = np.dot(self.designx_v, np.dot(self.designx_u.T, dvmat) / self.designx_s) self.vcomp_coeff = np.clip(vcomp_coeff, 0, np.inf) self.scale = scale self.dep_params = self.vcomp_coeff.copy() def covariance_matrix(self, expval, index): dim = len(expval) # First iteration if self.dep_params is None: return np.eye(dim, dtype=np.float64), True ilabel = self.ilabels[index] c = np.r_[self.scale, np.cumsum(self.vcomp_coeff)] vmat = c[ilabel] vmat /= self.scale return vmat, True update.__doc__ = CovStruct.update.__doc__ covariance_matrix.__doc__ = CovStruct.covariance_matrix.__doc__ def summary(self): """ Returns a summary string describing the state of the dependence structure. """ msg = "Variance estimates\n------------------\n" for k in range(len(self.vcomp_coeff)): msg += "Component %d: %.3f\n" % (k+1, self.vcomp_coeff[k]) msg += "Residual: %.3f\n" % (self.scale - np.sum(self.vcomp_coeff)) return msg class Autoregressive(CovStruct): """ An autoregressive working dependence structure. The dependence is defined in terms of the `time` component of the parent GEE class. Time represents a potentially multidimensional index from which distances between pairs of observations can be determined. The correlation between two observations in the same cluster is dep_params^distance, where `dep_params` is the autocorrelation parameter to be estimated, and `distance` is the distance between the two observations, calculated from their corresponding time values. `time` is stored as an n_obs x k matrix, where `k` represents the number of dimensions in the time index. The autocorrelation parameter is estimated using weighted nonlinear least squares, regressing each value within a cluster on each preceeding value in the same cluster. Parameters ---------- dist_func: function from R^k x R^k to R^+, optional A function that computes the distance between the two observations based on their `time` values. References ---------- B Rosner, A Munoz. Autoregressive modeling for the analysis of longitudinal data with unequally spaced examinations. Statistics in medicine. Vol 7, 59-71, 1988. """ def __init__(self, dist_func=None): super(Autoregressive, self).__init__() # The function for determining distances based on time if dist_func is None: self.dist_func = lambda x, y: np.abs(x - y).sum() else: self.dist_func = dist_func self.designx = None # The autocorrelation parameter self.dep_params = 0. def update(self, params): if self.model.weights is not None: warnings.warn("weights not implemented for autoregressive cov_struct, using unweighted covariance estimate") endog = self.model.endog_li time = self.model.time_li # Only need to compute this once if self.designx is not None: designx = self.designx else: designx = [] for i in range(self.model.num_group): ngrp = len(endog[i]) if ngrp == 0: continue # Loop over pairs of observations within a cluster for j1 in range(ngrp): for j2 in range(j1): designx.append(self.dist_func(time[i][j1, :], time[i][j2, :])) designx = np.array(designx) self.designx = designx scale = self.model.estimate_scale() varfunc = self.model.family.variance cached_means = self.model.cached_means # Weights var = 1. - self.dep_params**(2*designx) var /= 1. - self.dep_params**2 wts = 1. / var wts /= wts.sum() residmat = [] for i in range(self.model.num_group): expval, _ = cached_means[i] stdev = np.sqrt(scale * varfunc(expval)) resid = (endog[i] - expval) / stdev ngrp = len(resid) for j1 in range(ngrp): for j2 in range(j1): residmat.append([resid[j1], resid[j2]]) residmat = np.array(residmat) # Need to minimize this def fitfunc(a): dif = residmat[:, 0] - (a**designx)*residmat[:, 1] return np.dot(dif**2, wts) # Left bracket point b_lft, f_lft = 0., fitfunc(0.) # Center bracket point b_ctr, f_ctr = 0.5, fitfunc(0.5) while f_ctr > f_lft: b_ctr /= 2 f_ctr = fitfunc(b_ctr) if b_ctr < 1e-8: self.dep_params = 0 return # Right bracket point b_rgt, f_rgt = 0.75, fitfunc(0.75) while f_rgt < f_ctr: b_rgt = b_rgt + (1. - b_rgt) / 2 f_rgt = fitfunc(b_rgt) if b_rgt > 1. - 1e-6: raise ValueError( "Autoregressive: unable to find right bracket") from scipy.optimize import brent self.dep_params = brent(fitfunc, brack=[b_lft, b_ctr, b_rgt]) def covariance_matrix(self, endog_expval, index): ngrp = len(endog_expval) if self.dep_params == 0: return np.eye(ngrp, dtype=np.float64), True idx = np.arange(ngrp) cmat = self.dep_params**np.abs(idx[:, None] - idx[None, :]) return cmat, True def covariance_matrix_solve(self, expval, index, stdev, rhs): # The inverse of an AR(1) covariance matrix is tri-diagonal. k = len(expval) soln = [] # LHS has 1 column if k == 1: return [x / stdev**2 for x in rhs] # LHS has 2 columns if k == 2: mat = np.array([[1, -self.dep_params], [-self.dep_params, 1]]) mat /= (1. - self.dep_params**2) for x in rhs: if x.ndim == 1: x1 = x / stdev else: x1 = x / stdev[:, None] x1 = np.dot(mat, x1) if x.ndim == 1: x1 /= stdev else: x1 /= stdev[:, None] soln.append(x1) return soln # LHS has >= 3 columns: values c0, c1, c2 defined below give # the inverse. c0 is on the diagonal, except for the first # and last position. c1 is on the first and last position of # the diagonal. c2 is on the sub/super diagonal. c0 = (1. + self.dep_params**2) / (1. - self.dep_params**2) c1 = 1. / (1. - self.dep_params**2) c2 = -self.dep_params / (1. - self.dep_params**2) soln = [] for x in rhs: flatten = False if x.ndim == 1: x = x[:, None] flatten = True x1 = x / stdev[:, None] z0 = np.zeros((1, x.shape[1])) rhs1 = np.concatenate((x[1:,:], z0), axis=0) rhs2 = np.concatenate((z0, x[0:-1,:]), axis=0) y = c0*x + c2*rhs1 + c2*rhs2 y[0, :] = c1*x[0, :] + c2*x[1, :] y[-1, :] = c1*x[-1, :] + c2*x[-2, :] y /= stdev[:, None] if flatten: y = np.squeeze(y) soln.append(y) return soln update.__doc__ = CovStruct.update.__doc__ covariance_matrix.__doc__ = CovStruct.covariance_matrix.__doc__ covariance_matrix_solve.__doc__ = CovStruct.covariance_matrix_solve.__doc__ def summary(self): return ("Autoregressive(1) dependence parameter: %.3f\n" % self.dep_params) class GlobalOddsRatio(CovStruct): """ Estimate the global odds ratio for a GEE with ordinal or nominal data. References ---------- PJ Heagerty and S Zeger. "Marginal Regression Models for Clustered Ordinal Measurements". Journal of the American Statistical Association Vol. 91, Issue 435 (1996). Thomas Lumley. Generalized Estimating Equations for Ordinal Data: A Note on Working Correlation Structures. Biometrics Vol. 52, No. 1 (Mar., 1996), pp. 354-361 http://www.jstor.org/stable/2533173 Notes ----- The following data structures are calculated in the class: 'ibd' is a list whose i^th element ibd[i] is a sequence of integer pairs (a,b), where endog_li[i][a:b] is the subvector of binary indicators derived from the same ordinal value. `cpp` is a dictionary where cpp[group] is a map from cut-point pairs (c,c') to the indices of all between-subject pairs derived from the given cut points. """ def __init__(self, endog_type): super(GlobalOddsRatio, self).__init__() self.endog_type = endog_type self.dep_params = 0. def initialize(self, model): super(GlobalOddsRatio, self).initialize(model) if self.model.weights is not None: warnings.warn("weights not implemented for GlobalOddsRatio cov_struct, using unweighted covariance estimate") self.nlevel = len(model.endog_values) self.ncut = self.nlevel - 1 ibd = [] for v in model.endog_li: jj = np.arange(0, len(v) + 1, self.ncut) ibd1 = np.hstack((jj[0:-1][:, None], jj[1:][:, None])) ibd1 = [(jj[k], jj[k + 1]) for k in range(len(jj) - 1)] ibd.append(ibd1) self.ibd = ibd # Need to restrict to between-subject pairs cpp = [] for v in model.endog_li: # Number of subjects in this group m = int(len(v) / self.ncut) cpp1 = {} # Loop over distinct subject pairs for i1 in range(m): for i2 in range(i1): # Loop over cut point pairs for k1 in range(self.ncut): for k2 in range(k1+1): if (k2, k1) not in cpp1: cpp1[(k2, k1)] = [] j1 = i1*self.ncut + k1 j2 = i2*self.ncut + k2 cpp1[(k2, k1)].append([j2, j1]) for k in cpp1.keys(): cpp1[k] = np.asarray(cpp1[k]) cpp.append(cpp1) self.cpp = cpp # Initialize the dependence parameters self.crude_or = self.observed_crude_oddsratio() self.dep_params = self.crude_or def pooled_odds_ratio(self, tables): """ Returns the pooled odds ratio for a list of 2x2 tables. The pooled odds ratio is the inverse variance weighted average of the sample odds ratios of the tables. """ if len(tables) == 0: return 1. # Get the sampled odds ratios and variances log_oddsratio, var = [], [] for table in tables: lor = np.log(table[1, 1]) + np.log(table[0, 0]) -\ np.log(table[0, 1]) - np.log(table[1, 0]) log_oddsratio.append(lor) var.append((1 / table.astype(np.float64)).sum()) # Calculate the inverse variance weighted average wts = [1 / v for v in var] wtsum = sum(wts) wts = [w / wtsum for w in wts] log_pooled_or = sum([w*e for w, e in zip(wts, log_oddsratio)]) return np.exp(log_pooled_or) def covariance_matrix(self, expected_value, index): vmat = self.get_eyy(expected_value, index) vmat -= np.outer(expected_value, expected_value) return vmat, False def observed_crude_oddsratio(self): """ To obtain the crude (global) odds ratio, first pool all binary indicators corresponding to a given pair of cut points (c,c'), then calculate the odds ratio for this 2x2 table. The crude odds ratio is the inverse variance weighted average of these odds ratios. Since the covariate effects are ignored, this OR will generally be greater than the stratified OR. """ cpp = self.cpp endog = self.model.endog_li # Storage for the contingency tables for each (c,c') tables = {} for ii in iterkeys(cpp[0]): tables[ii] = np.zeros((2, 2), dtype=np.float64) # Get the observed crude OR for i in range(len(endog)): # The observed joint values for the current cluster yvec = endog[i] endog_11 = np.outer(yvec, yvec) endog_10 = np.outer(yvec, 1. - yvec) endog_01 = np.outer(1. - yvec, yvec) endog_00 = np.outer(1. - yvec, 1. - yvec) cpp1 = cpp[i] for ky in iterkeys(cpp1): ix = cpp1[ky] tables[ky][1, 1] += endog_11[ix[:, 0], ix[:, 1]].sum() tables[ky][1, 0] += endog_10[ix[:, 0], ix[:, 1]].sum() tables[ky][0, 1] += endog_01[ix[:, 0], ix[:, 1]].sum() tables[ky][0, 0] += endog_00[ix[:, 0], ix[:, 1]].sum() return self.pooled_odds_ratio(list(itervalues(tables))) def get_eyy(self, endog_expval, index): """ Returns a matrix V such that V[i,j] is the joint probability that endog[i] = 1 and endog[j] = 1, based on the marginal probabilities of endog and the global odds ratio `current_or`. """ current_or = self.dep_params ibd = self.ibd[index] # The between-observation joint probabilities if current_or == 1.0: vmat = np.outer(endog_expval, endog_expval) else: psum = endog_expval[:, None] + endog_expval[None, :] pprod = endog_expval[:, None] * endog_expval[None, :] pfac = np.sqrt((1. + psum * (current_or - 1.))**2 + 4 * current_or * (1. - current_or) * pprod) vmat = 1. + psum * (current_or - 1.) - pfac vmat /= 2. * (current_or - 1) # Fix E[YY'] for elements that belong to same observation for bdl in ibd: evy = endog_expval[bdl[0]:bdl[1]] if self.endog_type == "ordinal": eyr = np.outer(evy, np.ones(len(evy))) eyc = np.outer(np.ones(len(evy)), evy) vmat[bdl[0]:bdl[1], bdl[0]:bdl[1]] = \ np.where(eyr < eyc, eyr, eyc) else: vmat[bdl[0]:bdl[1], bdl[0]:bdl[1]] = np.diag(evy) return vmat def update(self, params): """ Update the global odds ratio based on the current value of params. """ endog = self.model.endog_li cpp = self.cpp cached_means = self.model.cached_means # This will happen if all the clusters have only # one observation if len(cpp[0]) == 0: return tables = {} for ii in cpp[0]: tables[ii] = np.zeros((2, 2), dtype=np.float64) for i in range(self.model.num_group): endog_expval, _ = cached_means[i] emat_11 = self.get_eyy(endog_expval, i) emat_10 = endog_expval[:, None] - emat_11 emat_01 = -emat_11 + endog_expval emat_00 = 1. - (emat_11 + emat_10 + emat_01) cpp1 = cpp[i] for ky in iterkeys(cpp1): ix = cpp1[ky] tables[ky][1, 1] += emat_11[ix[:, 0], ix[:, 1]].sum() tables[ky][1, 0] += emat_10[ix[:, 0], ix[:, 1]].sum() tables[ky][0, 1] += emat_01[ix[:, 0], ix[:, 1]].sum() tables[ky][0, 0] += emat_00[ix[:, 0], ix[:, 1]].sum() cor_expval = self.pooled_odds_ratio(list(itervalues(tables))) self.dep_params *= self.crude_or / cor_expval if not np.isfinite(self.dep_params): self.dep_params = 1. warnings.warn("dep_params became inf, resetting to 1", ConvergenceWarning) update.__doc__ = CovStruct.update.__doc__ covariance_matrix.__doc__ = CovStruct.covariance_matrix.__doc__ def summary(self): return "Global odds ratio: %.3f\n" % self.dep_params class Equivalence(CovStruct): """ A covariance structure defined in terms of equivalence classes. An 'equivalence class' is a set of pairs of observations such that the covariance of every pair within the equivalence class has a common value. Parameters ---------- pairs : dict-like A dictionary of dictionaries, where `pairs[group][label]` provides the indices of all pairs of observations in the group that have the same covariance value. Specifically, `pairs[group][label]` is a tuple `(j1, j2)`, where `j1` and `j2` are integer arrays of the same length. `j1[i], j2[i]` is one index pair that belongs to the `label` equivalence class. Only one triangle of each covariance matrix should be included. Positions where j1 and j2 have the same value are variance parameters. labels : array-like An array of labels such that every distinct pair of labels defines an equivalence class. Either `labels` or `pairs` must be provided. When the two labels in a pair are equal two equivalence classes are defined: one for the diagonal elements (corresponding to variances) and one for the off-diagonal elements (corresponding to covariances). return_cov : boolean If True, `covariance_matrix` returns an estimate of the covariance matrix, otherwise returns an estimate of the correlation matrix. Notes ----- Using `labels` to define the class is much easier than using `pairs`, but is less general. Any pair of values not contained in `pairs` will be assigned zero covariance. The index values in `pairs` are row indices into the `exog` matrix. They are not updated if missing data are present. When using this covariance structure, missing data should be removed before constructing the model. If using `labels`, after a model is defined using the covariance structure it is possible to remove a label pair from the second level of the `pairs` dictionary to force the corresponding covariance to be zero. Examples -------- The following sets up the `pairs` dictionary for a model with two groups, equal variance for all observations, and constant covariance for all pairs of observations within each group. >> pairs = {0: {}, 1: {}} >> pairs[0][0] = (np.r_[0, 1, 2], np.r_[0, 1, 2]) >> pairs[0][1] = np.tril_indices(3, -1) >> pairs[1][0] = (np.r_[3, 4, 5], np.r_[3, 4, 5]) >> pairs[1][2] = 3 + np.tril_indices(3, -1) """ def __init__(self, pairs=None, labels=None, return_cov=False): super(Equivalence, self).__init__() if (pairs is None) and (labels is None): raise ValueError("Equivalence cov_struct requires either `pairs` or `labels`") if (pairs is not None) and (labels is not None): raise ValueError("Equivalence cov_struct accepts only one of `pairs` and `labels`") if pairs is not None: import copy self.pairs = copy.deepcopy(pairs) if labels is not None: self.labels = np.asarray(labels) self.return_cov = return_cov def _make_pairs(self, i, j): """ Create arrays `i_`, `j_` containing all unique ordered pairs of elements in `i` and `j`. The arrays `i` and `j` must be one-dimensional containing non-negative integers. """ mat = np.zeros((len(i)*len(j), 2), dtype=np.int32) # Create the pairs and order them f = np.ones(len(j)) mat[:, 0] = np.kron(f, i).astype(np.int32) f = np.ones(len(i)) mat[:, 1] = np.kron(j, f).astype(np.int32) mat.sort(1) # Remove repeated rows try: dtype = np.dtype((np.void, mat.dtype.itemsize * mat.shape[1])) bmat = np.ascontiguousarray(mat).view(dtype) _, idx = np.unique(bmat, return_index=True) except TypeError: # workaround for old numpy that can't call unique with complex # dtypes np.random.seed(4234) bmat = np.dot(mat, np.random.uniform(size=mat.shape[1])) _, idx = np.unique(bmat, return_index=True) mat = mat[idx, :] return mat[:, 0], mat[:, 1] def _pairs_from_labels(self): from collections import defaultdict pairs = defaultdict(lambda : defaultdict(lambda : None)) model = self.model df = pd.DataFrame({"labels": self.labels, "groups": model.groups}) gb = df.groupby(["groups", "labels"]) ulabels = np.unique(self.labels) for g_ix, g_lb in enumerate(model.group_labels): # Loop over label pairs for lx1 in range(len(ulabels)): for lx2 in range(lx1+1): lb1 = ulabels[lx1] lb2 = ulabels[lx2] try: i1 = gb.groups[(g_lb, lb1)] i2 = gb.groups[(g_lb, lb2)] except KeyError: continue i1, i2 = self._make_pairs(i1, i2) clabel = str(lb1) + "/" + str(lb2) # Variance parameters belong in their own equiv class. jj = np.flatnonzero(i1 == i2) if len(jj) > 0: clabelv = clabel + "/v" pairs[g_lb][clabelv] = (i1[jj], i2[jj]) # Covariance parameters jj = np.flatnonzero(i1 != i2) if len(jj) > 0: i1 = i1[jj] i2 = i2[jj] pairs[g_lb][clabel] = (i1, i2) self.pairs = pairs def initialize(self, model): super(Equivalence, self).initialize(model) if self.model.weights is not None: warnings.warn("weights not implemented for equalence cov_struct, using unweighted covariance estimate") if not hasattr(self, 'pairs'): self._pairs_from_labels() # Initialize so that any equivalence class containing a # variance parameter has value 1. self.dep_params = defaultdict(lambda : 0.) self._var_classes = set([]) for gp in self.model.group_labels: for lb in self.pairs[gp]: j1, j2 = self.pairs[gp][lb] if np.any(j1 == j2): if not np.all(j1 == j2): warnings.warn("equivalence class contains both variance and covariance parameters") self._var_classes.add(lb) self.dep_params[lb] = 1 # Need to start indexing at 0 within each group. # rx maps olds indices to new indices rx = -1 * np.ones(len(self.model.endog), dtype=np.int32) for g_ix, g_lb in enumerate(self.model.group_labels): ii = self.model.group_indices[g_lb] rx[ii] = np.arange(len(ii), dtype=np.int32) # Reindex for gp in self.model.group_labels: for lb in self.pairs[gp].keys(): a, b = self.pairs[gp][lb] self.pairs[gp][lb] = (rx[a], rx[b]) def update(self, params): endog = self.model.endog_li varfunc = self.model.family.variance cached_means = self.model.cached_means dep_params = defaultdict(lambda : [0., 0., 0.]) n_pairs = defaultdict(lambda : 0) dim = len(params) for k, gp in enumerate(self.model.group_labels): expval, _ = cached_means[k] stdev = np.sqrt(varfunc(expval)) resid = (endog[k] - expval) / stdev for lb in self.pairs[gp].keys(): if (not self.return_cov) and lb in self._var_classes: continue jj = self.pairs[gp][lb] dep_params[lb][0] += np.sum(resid[jj[0]] * resid[jj[1]]) if not self.return_cov: dep_params[lb][1] += np.sum(resid[jj[0]]**2) dep_params[lb][2] += np.sum(resid[jj[1]]**2) n_pairs[lb] += len(jj[0]) if self.return_cov: for lb in dep_params.keys(): dep_params[lb] = dep_params[lb][0] / (n_pairs[lb] - dim) else: for lb in dep_params.keys(): den = np.sqrt(dep_params[lb][1] * dep_params[lb][2]) dep_params[lb] = dep_params[lb][0] / den for lb in self._var_classes: dep_params[lb] = 1. self.dep_params = dep_params self.n_pairs = n_pairs def covariance_matrix(self, expval, index): dim = len(expval) cmat = np.zeros((dim, dim)) g_lb = self.model.group_labels[index] for lb in self.pairs[g_lb].keys(): j1, j2 = self.pairs[g_lb][lb] cmat[j1, j2] = self.dep_params[lb] cmat = cmat + cmat.T np.fill_diagonal(cmat, cmat.diagonal() / 2) return cmat, not self.return_cov update.__doc__ = CovStruct.update.__doc__ covariance_matrix.__doc__ = CovStruct.covariance_matrix.__doc__
bsd-3-clause
fabianp/scikit-learn
sklearn/datasets/mldata.py
309
7838
"""Automatically download MLdata datasets.""" # Copyright (c) 2011 Pietro Berkes # License: BSD 3 clause import os from os.path import join, exists import re import numbers try: # Python 2 from urllib2 import HTTPError from urllib2 import quote from urllib2 import urlopen except ImportError: # Python 3+ from urllib.error import HTTPError from urllib.parse import quote from urllib.request import urlopen import numpy as np import scipy as sp from scipy import io from shutil import copyfileobj from .base import get_data_home, Bunch MLDATA_BASE_URL = "http://mldata.org/repository/data/download/matlab/%s" def mldata_filename(dataname): """Convert a raw name for a data set in a mldata.org filename.""" dataname = dataname.lower().replace(' ', '-') return re.sub(r'[().]', '', dataname) def fetch_mldata(dataname, target_name='label', data_name='data', transpose_data=True, data_home=None): """Fetch an mldata.org data set If the file does not exist yet, it is downloaded from mldata.org . mldata.org does not have an enforced convention for storing data or naming the columns in a data set. The default behavior of this function works well with the most common cases: 1) data values are stored in the column 'data', and target values in the column 'label' 2) alternatively, the first column stores target values, and the second data values 3) the data array is stored as `n_features x n_samples` , and thus needs to be transposed to match the `sklearn` standard Keyword arguments allow to adapt these defaults to specific data sets (see parameters `target_name`, `data_name`, `transpose_data`, and the examples below). mldata.org data sets may have multiple columns, which are stored in the Bunch object with their original name. Parameters ---------- dataname: Name of the data set on mldata.org, e.g.: "leukemia", "Whistler Daily Snowfall", etc. The raw name is automatically converted to a mldata.org URL . target_name: optional, default: 'label' Name or index of the column containing the target values. data_name: optional, default: 'data' Name or index of the column containing the data. transpose_data: optional, default: True If True, transpose the downloaded data array. data_home: optional, default: None Specify another download and cache folder for the data sets. By default all scikit learn data is stored in '~/scikit_learn_data' subfolders. Returns ------- data : Bunch Dictionary-like object, the interesting attributes are: 'data', the data to learn, 'target', the classification labels, 'DESCR', the full description of the dataset, and 'COL_NAMES', the original names of the dataset columns. Examples -------- Load the 'iris' dataset from mldata.org: >>> from sklearn.datasets.mldata import fetch_mldata >>> import tempfile >>> test_data_home = tempfile.mkdtemp() >>> iris = fetch_mldata('iris', data_home=test_data_home) >>> iris.target.shape (150,) >>> iris.data.shape (150, 4) Load the 'leukemia' dataset from mldata.org, which needs to be transposed to respects the sklearn axes convention: >>> leuk = fetch_mldata('leukemia', transpose_data=True, ... data_home=test_data_home) >>> leuk.data.shape (72, 7129) Load an alternative 'iris' dataset, which has different names for the columns: >>> iris2 = fetch_mldata('datasets-UCI iris', target_name=1, ... data_name=0, data_home=test_data_home) >>> iris3 = fetch_mldata('datasets-UCI iris', ... target_name='class', data_name='double0', ... data_home=test_data_home) >>> import shutil >>> shutil.rmtree(test_data_home) """ # normalize dataset name dataname = mldata_filename(dataname) # check if this data set has been already downloaded data_home = get_data_home(data_home=data_home) data_home = join(data_home, 'mldata') if not exists(data_home): os.makedirs(data_home) matlab_name = dataname + '.mat' filename = join(data_home, matlab_name) # if the file does not exist, download it if not exists(filename): urlname = MLDATA_BASE_URL % quote(dataname) try: mldata_url = urlopen(urlname) except HTTPError as e: if e.code == 404: e.msg = "Dataset '%s' not found on mldata.org." % dataname raise # store Matlab file try: with open(filename, 'w+b') as matlab_file: copyfileobj(mldata_url, matlab_file) except: os.remove(filename) raise mldata_url.close() # load dataset matlab file with open(filename, 'rb') as matlab_file: matlab_dict = io.loadmat(matlab_file, struct_as_record=True) # -- extract data from matlab_dict # flatten column names col_names = [str(descr[0]) for descr in matlab_dict['mldata_descr_ordering'][0]] # if target or data names are indices, transform then into names if isinstance(target_name, numbers.Integral): target_name = col_names[target_name] if isinstance(data_name, numbers.Integral): data_name = col_names[data_name] # rules for making sense of the mldata.org data format # (earlier ones have priority): # 1) there is only one array => it is "data" # 2) there are multiple arrays # a) copy all columns in the bunch, using their column name # b) if there is a column called `target_name`, set "target" to it, # otherwise set "target" to first column # c) if there is a column called `data_name`, set "data" to it, # otherwise set "data" to second column dataset = {'DESCR': 'mldata.org dataset: %s' % dataname, 'COL_NAMES': col_names} # 1) there is only one array => it is considered data if len(col_names) == 1: data_name = col_names[0] dataset['data'] = matlab_dict[data_name] # 2) there are multiple arrays else: for name in col_names: dataset[name] = matlab_dict[name] if target_name in col_names: del dataset[target_name] dataset['target'] = matlab_dict[target_name] else: del dataset[col_names[0]] dataset['target'] = matlab_dict[col_names[0]] if data_name in col_names: del dataset[data_name] dataset['data'] = matlab_dict[data_name] else: del dataset[col_names[1]] dataset['data'] = matlab_dict[col_names[1]] # set axes to sklearn conventions if transpose_data: dataset['data'] = dataset['data'].T if 'target' in dataset: if not sp.sparse.issparse(dataset['target']): dataset['target'] = dataset['target'].squeeze() return Bunch(**dataset) # The following is used by nosetests to setup the docstring tests fixture def setup_module(module): # setup mock urllib2 module to avoid downloading from mldata.org from sklearn.utils.testing import install_mldata_mock install_mldata_mock({ 'iris': { 'data': np.empty((150, 4)), 'label': np.empty(150), }, 'datasets-uci-iris': { 'double0': np.empty((150, 4)), 'class': np.empty((150,)), }, 'leukemia': { 'data': np.empty((72, 7129)), }, }) def teardown_module(module): from sklearn.utils.testing import uninstall_mldata_mock uninstall_mldata_mock()
bsd-3-clause
zihua/scikit-learn
sklearn/datasets/tests/test_lfw.py
55
7877
"""This test for the LFW require medium-size data downloading and processing If the data has not been already downloaded by running the examples, the tests won't run (skipped). If the test are run, the first execution will be long (typically a bit more than a couple of minutes) but as the dataset loader is leveraging joblib, successive runs will be fast (less than 200ms). """ import random import os import shutil import tempfile import numpy as np from sklearn.externals import six try: try: from scipy.misc import imsave except ImportError: from scipy.misc.pilutil import imsave except ImportError: imsave = None from sklearn.datasets import load_lfw_pairs from sklearn.datasets import load_lfw_people from sklearn.datasets import fetch_lfw_pairs from sklearn.datasets import fetch_lfw_people from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_warns_message from sklearn.utils.testing import SkipTest from sklearn.utils.testing import raises SCIKIT_LEARN_DATA = tempfile.mkdtemp(prefix="scikit_learn_lfw_test_") SCIKIT_LEARN_EMPTY_DATA = tempfile.mkdtemp(prefix="scikit_learn_empty_test_") LFW_HOME = os.path.join(SCIKIT_LEARN_DATA, 'lfw_home') FAKE_NAMES = [ 'Abdelatif_Smith', 'Abhati_Kepler', 'Camara_Alvaro', 'Chen_Dupont', 'John_Lee', 'Lin_Bauman', 'Onur_Lopez', ] def setup_module(): """Test fixture run once and common to all tests of this module""" if imsave is None: raise SkipTest("PIL not installed.") if not os.path.exists(LFW_HOME): os.makedirs(LFW_HOME) random_state = random.Random(42) np_rng = np.random.RandomState(42) # generate some random jpeg files for each person counts = {} for name in FAKE_NAMES: folder_name = os.path.join(LFW_HOME, 'lfw_funneled', name) if not os.path.exists(folder_name): os.makedirs(folder_name) n_faces = np_rng.randint(1, 5) counts[name] = n_faces for i in range(n_faces): file_path = os.path.join(folder_name, name + '_%04d.jpg' % i) uniface = np_rng.randint(0, 255, size=(250, 250, 3)) try: imsave(file_path, uniface) except ImportError: raise SkipTest("PIL not installed") # add some random file pollution to test robustness with open(os.path.join(LFW_HOME, 'lfw_funneled', '.test.swp'), 'wb') as f: f.write(six.b('Text file to be ignored by the dataset loader.')) # generate some pairing metadata files using the same format as LFW with open(os.path.join(LFW_HOME, 'pairsDevTrain.txt'), 'wb') as f: f.write(six.b("10\n")) more_than_two = [name for name, count in six.iteritems(counts) if count >= 2] for i in range(5): name = random_state.choice(more_than_two) first, second = random_state.sample(range(counts[name]), 2) f.write(six.b('%s\t%d\t%d\n' % (name, first, second))) for i in range(5): first_name, second_name = random_state.sample(FAKE_NAMES, 2) first_index = random_state.choice(np.arange(counts[first_name])) second_index = random_state.choice(np.arange(counts[second_name])) f.write(six.b('%s\t%d\t%s\t%d\n' % (first_name, first_index, second_name, second_index))) with open(os.path.join(LFW_HOME, 'pairsDevTest.txt'), 'wb') as f: f.write(six.b("Fake place holder that won't be tested")) with open(os.path.join(LFW_HOME, 'pairs.txt'), 'wb') as f: f.write(six.b("Fake place holder that won't be tested")) def teardown_module(): """Test fixture (clean up) run once after all tests of this module""" if os.path.isdir(SCIKIT_LEARN_DATA): shutil.rmtree(SCIKIT_LEARN_DATA) if os.path.isdir(SCIKIT_LEARN_EMPTY_DATA): shutil.rmtree(SCIKIT_LEARN_EMPTY_DATA) @raises(IOError) def test_load_empty_lfw_people(): fetch_lfw_people(data_home=SCIKIT_LEARN_EMPTY_DATA, download_if_missing=False) def test_load_lfw_people_deprecation(): msg = ("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.") assert_warns_message(DeprecationWarning, msg, load_lfw_people, data_home=SCIKIT_LEARN_DATA) def test_load_fake_lfw_people(): lfw_people = fetch_lfw_people(data_home=SCIKIT_LEARN_DATA, min_faces_per_person=3, download_if_missing=False) # The data is croped around the center as a rectangular bounding box # around the face. Colors are converted to gray levels: assert_equal(lfw_people.images.shape, (10, 62, 47)) assert_equal(lfw_people.data.shape, (10, 2914)) # the target is array of person integer ids assert_array_equal(lfw_people.target, [2, 0, 1, 0, 2, 0, 2, 1, 1, 2]) # names of the persons can be found using the target_names array expected_classes = ['Abdelatif Smith', 'Abhati Kepler', 'Onur Lopez'] assert_array_equal(lfw_people.target_names, expected_classes) # It is possible to ask for the original data without any croping or color # conversion and not limit on the number of picture per person lfw_people = fetch_lfw_people(data_home=SCIKIT_LEARN_DATA, resize=None, slice_=None, color=True, download_if_missing=False) assert_equal(lfw_people.images.shape, (17, 250, 250, 3)) # the ids and class names are the same as previously assert_array_equal(lfw_people.target, [0, 0, 1, 6, 5, 6, 3, 6, 0, 3, 6, 1, 2, 4, 5, 1, 2]) assert_array_equal(lfw_people.target_names, ['Abdelatif Smith', 'Abhati Kepler', 'Camara Alvaro', 'Chen Dupont', 'John Lee', 'Lin Bauman', 'Onur Lopez']) @raises(ValueError) def test_load_fake_lfw_people_too_restrictive(): fetch_lfw_people(data_home=SCIKIT_LEARN_DATA, min_faces_per_person=100, download_if_missing=False) @raises(IOError) def test_load_empty_lfw_pairs(): fetch_lfw_pairs(data_home=SCIKIT_LEARN_EMPTY_DATA, download_if_missing=False) def test_load_lfw_pairs_deprecation(): msg = ("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.") assert_warns_message(DeprecationWarning, msg, load_lfw_pairs, data_home=SCIKIT_LEARN_DATA) def test_load_fake_lfw_pairs(): lfw_pairs_train = fetch_lfw_pairs(data_home=SCIKIT_LEARN_DATA, download_if_missing=False) # The data is croped around the center as a rectangular bounding box # around the face. Colors are converted to gray levels: assert_equal(lfw_pairs_train.pairs.shape, (10, 2, 62, 47)) # the target is whether the person is the same or not assert_array_equal(lfw_pairs_train.target, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) # names of the persons can be found using the target_names array expected_classes = ['Different persons', 'Same person'] assert_array_equal(lfw_pairs_train.target_names, expected_classes) # It is possible to ask for the original data without any croping or color # conversion lfw_pairs_train = fetch_lfw_pairs(data_home=SCIKIT_LEARN_DATA, resize=None, slice_=None, color=True, download_if_missing=False) assert_equal(lfw_pairs_train.pairs.shape, (10, 2, 250, 250, 3)) # the ids and class names are the same as previously assert_array_equal(lfw_pairs_train.target, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) assert_array_equal(lfw_pairs_train.target_names, expected_classes)
bsd-3-clause
dotsdl/PyTables
doc/sphinxext/inheritance_diagram.py
98
13648
""" Defines a docutils directive for inserting inheritance diagrams. Provide the directive with one or more classes or modules (separated by whitespace). For modules, all of the classes in that module will be used. Example:: Given the following classes: class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E(B): pass .. inheritance-diagram: D E Produces a graph like the following: A / \ B C / \ / E D The graph is inserted as a PNG+image map into HTML and a PDF in LaTeX. """ import inspect import os import re import subprocess try: from hashlib import md5 except ImportError: from md5 import md5 from docutils.nodes import Body, Element from docutils.parsers.rst import directives from sphinx.roles import xfileref_role def my_import(name): """Module importer - taken from the python documentation. This function allows importing names with dots in them.""" mod = __import__(name) components = name.split('.') for comp in components[1:]: mod = getattr(mod, comp) return mod class DotException(Exception): pass class InheritanceGraph(object): """ Given a list of classes, determines the set of classes that they inherit from all the way to the root "object", and then is able to generate a graphviz dot graph from them. """ def __init__(self, class_names, show_builtins=False): """ *class_names* is a list of child classes to show bases from. If *show_builtins* is True, then Python builtins will be shown in the graph. """ self.class_names = class_names self.classes = self._import_classes(class_names) self.all_classes = self._all_classes(self.classes) if len(self.all_classes) == 0: raise ValueError("No classes found for inheritance diagram") self.show_builtins = show_builtins py_sig_re = re.compile(r'''^([\w.]*\.)? # class names (\w+) \s* $ # optionally arguments ''', re.VERBOSE) def _import_class_or_module(self, name): """ Import a class using its fully-qualified *name*. """ try: path, base = self.py_sig_re.match(name).groups() except: raise ValueError( "Invalid class or module '%s' specified for inheritance diagram" % name) fullname = (path or '') + base path = (path and path.rstrip('.')) if not path: path = base try: module = __import__(path, None, None, []) # We must do an import of the fully qualified name. Otherwise if a # subpackage 'a.b' is requested where 'import a' does NOT provide # 'a.b' automatically, then 'a.b' will not be found below. This # second call will force the equivalent of 'import a.b' to happen # after the top-level import above. my_import(fullname) except ImportError: raise ValueError( "Could not import class or module '%s' specified for inheritance diagram" % name) try: todoc = module for comp in fullname.split('.')[1:]: todoc = getattr(todoc, comp) except AttributeError: raise ValueError( "Could not find class or module '%s' specified for inheritance diagram" % name) # If a class, just return it if inspect.isclass(todoc): return [todoc] elif inspect.ismodule(todoc): classes = [] for cls in todoc.__dict__.values(): if inspect.isclass(cls) and cls.__module__ == todoc.__name__: classes.append(cls) return classes raise ValueError( "'%s' does not resolve to a class or module" % name) def _import_classes(self, class_names): """ Import a list of classes. """ classes = [] for name in class_names: classes.extend(self._import_class_or_module(name)) return classes def _all_classes(self, classes): """ Return a list of all classes that are ancestors of *classes*. """ all_classes = {} def recurse(cls): all_classes[cls] = None for c in cls.__bases__: if c not in all_classes: recurse(c) for cls in classes: recurse(cls) return all_classes.keys() def class_name(self, cls, parts=0): """ Given a class object, return a fully-qualified name. This works for things I've tested in matplotlib so far, but may not be completely general. """ module = cls.__module__ if module == '__builtin__': fullname = cls.__name__ else: fullname = "%s.%s" % (module, cls.__name__) if parts == 0: return fullname name_parts = fullname.split('.') return '.'.join(name_parts[-parts:]) def get_all_class_names(self): """ Get all of the class names involved in the graph. """ return [self.class_name(x) for x in self.all_classes] # These are the default options for graphviz default_graph_options = { "rankdir": "LR", "size": '"8.0, 12.0"' } default_node_options = { "shape": "box", "fontsize": 10, "height": 0.25, "fontname": "Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans", "style": '"setlinewidth(0.5)"' } default_edge_options = { "arrowsize": 0.5, "style": '"setlinewidth(0.5)"' } def _format_node_options(self, options): return ','.join(["%s=%s" % x for x in options.items()]) def _format_graph_options(self, options): return ''.join(["%s=%s;\n" % x for x in options.items()]) def generate_dot(self, fd, name, parts=0, urls={}, graph_options={}, node_options={}, edge_options={}): """ Generate a graphviz dot graph from the classes that were passed in to __init__. *fd* is a Python file-like object to write to. *name* is the name of the graph *urls* is a dictionary mapping class names to http urls *graph_options*, *node_options*, *edge_options* are dictionaries containing key/value pairs to pass on as graphviz properties. """ g_options = self.default_graph_options.copy() g_options.update(graph_options) n_options = self.default_node_options.copy() n_options.update(node_options) e_options = self.default_edge_options.copy() e_options.update(edge_options) fd.write('digraph %s {\n' % name) fd.write(self._format_graph_options(g_options)) for cls in self.all_classes: if not self.show_builtins and cls in __builtins__.values(): continue name = self.class_name(cls, parts) # Write the node this_node_options = n_options.copy() url = urls.get(self.class_name(cls)) if url is not None: this_node_options['URL'] = '"%s"' % url fd.write(' "%s" [%s];\n' % (name, self._format_node_options(this_node_options))) # Write the edges for base in cls.__bases__: if not self.show_builtins and base in __builtins__.values(): continue base_name = self.class_name(base, parts) fd.write(' "%s" -> "%s" [%s];\n' % (base_name, name, self._format_node_options(e_options))) fd.write('}\n') def run_dot(self, args, name, parts=0, urls={}, graph_options={}, node_options={}, edge_options={}): """ Run graphviz 'dot' over this graph, returning whatever 'dot' writes to stdout. *args* will be passed along as commandline arguments. *name* is the name of the graph *urls* is a dictionary mapping class names to http urls Raises DotException for any of the many os and installation-related errors that may occur. """ try: dot = subprocess.Popen(['dot'] + list(args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) except OSError: raise DotException("Could not execute 'dot'. Are you sure you have 'graphviz' installed?") except ValueError: raise DotException("'dot' called with invalid arguments") except: raise DotException("Unexpected error calling 'dot'") self.generate_dot(dot.stdin, name, parts, urls, graph_options, node_options, edge_options) dot.stdin.close() result = dot.stdout.read() returncode = dot.wait() if returncode != 0: raise DotException("'dot' returned the errorcode %d" % returncode) return result class inheritance_diagram(Body, Element): """ A docutils node to use as a placeholder for the inheritance diagram. """ pass def inheritance_diagram_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """ Run when the inheritance_diagram directive is first encountered. """ node = inheritance_diagram() class_names = arguments # Create a graph starting with the list of classes graph = InheritanceGraph(class_names) # Create xref nodes for each target of the graph's image map and # add them to the doc tree so that Sphinx can resolve the # references to real URLs later. These nodes will eventually be # removed from the doctree after we're done with them. for name in graph.get_all_class_names(): refnodes, x = xfileref_role( 'class', ':class:`%s`' % name, name, 0, state) node.extend(refnodes) # Store the graph object so we can use it to generate the # dot file later node['graph'] = graph # Store the original content for use as a hash node['parts'] = options.get('parts', 0) node['content'] = " ".join(class_names) return [node] def get_graph_hash(node): return md5(node['content'] + str(node['parts'])).hexdigest()[-10:] def html_output_graph(self, node): """ Output the graph for HTML. This will insert a PNG with clickable image map. """ graph = node['graph'] parts = node['parts'] graph_hash = get_graph_hash(node) name = "inheritance%s" % graph_hash path = '_images' dest_path = os.path.join(setup.app.builder.outdir, path) if not os.path.exists(dest_path): os.makedirs(dest_path) png_path = os.path.join(dest_path, name + ".png") path = setup.app.builder.imgpath # Create a mapping from fully-qualified class names to URLs. urls = {} for child in node: if child.get('refuri') is not None: urls[child['reftitle']] = child.get('refuri') elif child.get('refid') is not None: urls[child['reftitle']] = '#' + child.get('refid') # These arguments to dot will save a PNG file to disk and write # an HTML image map to stdout. image_map = graph.run_dot(['-Tpng', '-o%s' % png_path, '-Tcmapx'], name, parts, urls) return ('<img src="%s/%s.png" usemap="#%s" class="inheritance"/>%s' % (path, name, name, image_map)) def latex_output_graph(self, node): """ Output the graph for LaTeX. This will insert a PDF. """ graph = node['graph'] parts = node['parts'] graph_hash = get_graph_hash(node) name = "inheritance%s" % graph_hash dest_path = os.path.abspath(os.path.join(setup.app.builder.outdir, '_images')) if not os.path.exists(dest_path): os.makedirs(dest_path) pdf_path = os.path.abspath(os.path.join(dest_path, name + ".pdf")) graph.run_dot(['-Tpdf', '-o%s' % pdf_path], name, parts, graph_options={'size': '"6.0,6.0"'}) return '\n\\includegraphics{%s}\n\n' % pdf_path def visit_inheritance_diagram(inner_func): """ This is just a wrapper around html/latex_output_graph to make it easier to handle errors and insert warnings. """ def visitor(self, node): try: content = inner_func(self, node) except DotException, e: # Insert the exception as a warning in the document warning = self.document.reporter.warning(str(e), line=node.line) warning.parent = node node.children = [warning] else: source = self.document.attributes['source'] self.body.append(content) node.children = [] return visitor def do_nothing(self, node): pass def setup(app): setup.app = app setup.confdir = app.confdir app.add_node( inheritance_diagram, latex=(visit_inheritance_diagram(latex_output_graph), do_nothing), html=(visit_inheritance_diagram(html_output_graph), do_nothing)) app.add_directive( 'inheritance-diagram', inheritance_diagram_directive, False, (1, 100, 0), parts = directives.nonnegative_int)
bsd-3-clause
gfyoung/pandas
pandas/tests/indexes/timedeltas/methods/test_factorize.py
2
1275
import numpy as np from pandas import TimedeltaIndex, factorize, timedelta_range import pandas._testing as tm class TestTimedeltaIndexFactorize: def test_factorize(self): idx1 = TimedeltaIndex(["1 day", "1 day", "2 day", "2 day", "3 day", "3 day"]) exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp) exp_idx = TimedeltaIndex(["1 day", "2 day", "3 day"]) arr, idx = idx1.factorize() tm.assert_numpy_array_equal(arr, exp_arr) tm.assert_index_equal(idx, exp_idx) assert idx.freq == exp_idx.freq arr, idx = idx1.factorize(sort=True) tm.assert_numpy_array_equal(arr, exp_arr) tm.assert_index_equal(idx, exp_idx) assert idx.freq == exp_idx.freq def test_factorize_preserves_freq(self): # GH#38120 freq should be preserved idx3 = timedelta_range("1 day", periods=4, freq="s") exp_arr = np.array([0, 1, 2, 3], dtype=np.intp) arr, idx = idx3.factorize() tm.assert_numpy_array_equal(arr, exp_arr) tm.assert_index_equal(idx, idx3) assert idx.freq == idx3.freq arr, idx = factorize(idx3) tm.assert_numpy_array_equal(arr, exp_arr) tm.assert_index_equal(idx, idx3) assert idx.freq == idx3.freq
bsd-3-clause
eramirem/astroML
astroML/density_estimation/gauss_mixture.py
3
1283
import numpy as np from sklearn.mixture import GMM class GaussianMixture1D(object): """ Simple class to work with 1D mixtures of Gaussians Parameters ---------- means : array_like means of component distributions (default = 0) sigmas : array_like standard deviations of component distributions (default = 1) weights : array_like weight of component distributions (default = 1) """ def __init__(self, means=0, sigmas=1, weights=1): data = np.array([t for t in np.broadcast(means, sigmas, weights)]) self._gmm = GMM(data.shape[0]) self._gmm.fit = None # disable fit method for safety self._gmm.means_ = data[:, :1] self._gmm.covars_ = data[:, 1:2] ** 2 self._gmm.weights = data[:, 2] / data[:, 2].sum() def sample(self, size): """Random sample""" return self._gmm.sample(size) def pdf(self, x): """Compute probability distribution""" logprob, responsibilities = self._gmm.eval(x) return np.exp(logprob) def pdf_individual(self, x): """Compute probability distribution of each component""" logprob, responsibilities = self._gmm.eval(x) return responsibilities * np.exp(logprob[:, np.newaxis])
bsd-2-clause
raghavrv/scikit-learn
examples/covariance/plot_outlier_detection.py
15
5121
""" ========================================== Outlier detection with several methods. ========================================== When the amount of contamination is known, this example illustrates three different ways of performing :ref:`outlier_detection`: - based on a robust estimator of covariance, which is assuming that the data are Gaussian distributed and performs better than the One-Class SVM in that case. - using the One-Class SVM and its ability to capture the shape of the data set, hence performing better when the data is strongly non-Gaussian, i.e. with two well-separated clusters; - using the Isolation Forest algorithm, which is based on random forests and hence more adapted to large-dimensional settings, even if it performs quite well in the examples below. - using the Local Outlier Factor to measure the local deviation of a given data point with respect to its neighbors by comparing their local density. The ground truth about inliers and outliers is given by the points colors while the orange-filled area indicates which points are reported as inliers by each method. Here, we assume that we know the fraction of outliers in the datasets. Thus rather than using the 'predict' method of the objects, we set the threshold on the decision_function to separate out the corresponding fraction. """ import numpy as np from scipy import stats import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn import svm from sklearn.covariance import EllipticEnvelope from sklearn.ensemble import IsolationForest from sklearn.neighbors import LocalOutlierFactor print(__doc__) rng = np.random.RandomState(42) # Example settings n_samples = 200 outliers_fraction = 0.25 clusters_separation = [0, 1, 2] # define two outlier detection tools to be compared classifiers = { "One-Class SVM": svm.OneClassSVM(nu=0.95 * outliers_fraction + 0.05, kernel="rbf", gamma=0.1), "Robust covariance": EllipticEnvelope(contamination=outliers_fraction), "Isolation Forest": IsolationForest(max_samples=n_samples, contamination=outliers_fraction, random_state=rng), "Local Outlier Factor": LocalOutlierFactor( n_neighbors=35, contamination=outliers_fraction)} # Compare given classifiers under given settings xx, yy = np.meshgrid(np.linspace(-7, 7, 100), np.linspace(-7, 7, 100)) n_inliers = int((1. - outliers_fraction) * n_samples) n_outliers = int(outliers_fraction * n_samples) ground_truth = np.ones(n_samples, dtype=int) ground_truth[-n_outliers:] = -1 # Fit the problem with varying cluster separation for i, offset in enumerate(clusters_separation): np.random.seed(42) # Data generation X1 = 0.3 * np.random.randn(n_inliers // 2, 2) - offset X2 = 0.3 * np.random.randn(n_inliers // 2, 2) + offset X = np.r_[X1, X2] # Add outliers X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))] # Fit the model plt.figure(figsize=(9, 7)) for i, (clf_name, clf) in enumerate(classifiers.items()): # fit the data and tag outliers if clf_name == "Local Outlier Factor": y_pred = clf.fit_predict(X) scores_pred = clf.negative_outlier_factor_ else: clf.fit(X) scores_pred = clf.decision_function(X) y_pred = clf.predict(X) threshold = stats.scoreatpercentile(scores_pred, 100 * outliers_fraction) n_errors = (y_pred != ground_truth).sum() # plot the levels lines and the points if clf_name == "Local Outlier Factor": # decision_function is private for LOF Z = clf._decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) subplot = plt.subplot(2, 2, i + 1) subplot.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7), cmap=plt.cm.Blues_r) a = subplot.contour(xx, yy, Z, levels=[threshold], linewidths=2, colors='red') subplot.contourf(xx, yy, Z, levels=[threshold, Z.max()], colors='orange') b = subplot.scatter(X[:-n_outliers, 0], X[:-n_outliers, 1], c='white', s=20, edgecolor='k') c = subplot.scatter(X[-n_outliers:, 0], X[-n_outliers:, 1], c='black', s=20, edgecolor='k') subplot.axis('tight') subplot.legend( [a.collections[0], b, c], ['learned decision function', 'true inliers', 'true outliers'], prop=matplotlib.font_manager.FontProperties(size=10), loc='lower right') subplot.set_xlabel("%d. %s (errors: %d)" % (i + 1, clf_name, n_errors)) subplot.set_xlim((-7, 7)) subplot.set_ylim((-7, 7)) plt.subplots_adjust(0.04, 0.1, 0.96, 0.94, 0.1, 0.26) plt.suptitle("Outlier detection") plt.show()
bsd-3-clause
JPFrancoia/scikit-learn
examples/feature_selection/plot_feature_selection.py
95
2847
""" =============================== Univariate Feature Selection =============================== An example showing univariate feature selection. Noisy (non informative) features are added to the iris data and univariate feature selection is applied. For each feature, we plot the p-values for the univariate feature selection and the corresponding weights of an SVM. We can see that univariate feature selection selects the informative features and that these have larger SVM weights. In the total set of features, only the 4 first ones are significant. We can see that they have the highest score with univariate feature selection. The SVM assigns a large weight to one of these features, but also Selects many of the non-informative features. Applying univariate feature selection before the SVM increases the SVM weight attributed to the significant features, and will thus improve classification. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, svm from sklearn.feature_selection import SelectPercentile, f_classif ############################################################################### # import some data to play with # The iris dataset iris = datasets.load_iris() # Some noisy data not correlated E = np.random.uniform(0, 0.1, size=(len(iris.data), 20)) # Add the noisy data to the informative features X = np.hstack((iris.data, E)) y = iris.target ############################################################################### plt.figure(1) plt.clf() X_indices = np.arange(X.shape[-1]) ############################################################################### # Univariate feature selection with F-test for feature scoring # We use the default selection function: the 10% most significant features selector = SelectPercentile(f_classif, percentile=10) selector.fit(X, y) scores = -np.log10(selector.pvalues_) scores /= scores.max() plt.bar(X_indices - .45, scores, width=.2, label=r'Univariate score ($-Log(p_{value})$)', color='darkorange') ############################################################################### # Compare to the weights of an SVM clf = svm.SVC(kernel='linear') clf.fit(X, y) svm_weights = (clf.coef_ ** 2).sum(axis=0) svm_weights /= svm_weights.max() plt.bar(X_indices - .25, svm_weights, width=.2, label='SVM weight', color='navy') clf_selected = svm.SVC(kernel='linear') clf_selected.fit(selector.transform(X), y) svm_weights_selected = (clf_selected.coef_ ** 2).sum(axis=0) svm_weights_selected /= svm_weights_selected.max() plt.bar(X_indices[selector.get_support()] - .05, svm_weights_selected, width=.2, label='SVM weights after selection', color='c') plt.title("Comparing feature selection") plt.xlabel('Feature number') plt.yticks(()) plt.axis('tight') plt.legend(loc='upper right') plt.show()
bsd-3-clause
flightgong/scikit-learn
sklearn/datasets/tests/test_lfw.py
50
6849
"""This test for the LFW require medium-size data dowloading and processing If the data has not been already downloaded by running the examples, the tests won't run (skipped). If the test are run, the first execution will be long (typically a bit more than a couple of minutes) but as the dataset loader is leveraging joblib, successive runs will be fast (less than 200ms). """ import random import os import shutil import tempfile import numpy as np from sklearn.externals import six try: try: from scipy.misc import imsave except ImportError: from scipy.misc.pilutil import imsave except ImportError: imsave = None from sklearn.datasets import load_lfw_pairs from sklearn.datasets import load_lfw_people from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import SkipTest from sklearn.utils.testing import raises SCIKIT_LEARN_DATA = tempfile.mkdtemp(prefix="scikit_learn_lfw_test_") SCIKIT_LEARN_EMPTY_DATA = tempfile.mkdtemp(prefix="scikit_learn_empty_test_") LFW_HOME = os.path.join(SCIKIT_LEARN_DATA, 'lfw_home') FAKE_NAMES = [ 'Abdelatif_Smith', 'Abhati_Kepler', 'Camara_Alvaro', 'Chen_Dupont', 'John_Lee', 'Lin_Bauman', 'Onur_Lopez', ] def setup_module(): """Test fixture run once and common to all tests of this module""" if imsave is None: raise SkipTest("PIL not installed.") if not os.path.exists(LFW_HOME): os.makedirs(LFW_HOME) random_state = random.Random(42) np_rng = np.random.RandomState(42) # generate some random jpeg files for each person counts = {} for name in FAKE_NAMES: folder_name = os.path.join(LFW_HOME, 'lfw_funneled', name) if not os.path.exists(folder_name): os.makedirs(folder_name) n_faces = np_rng.randint(1, 5) counts[name] = n_faces for i in range(n_faces): file_path = os.path.join(folder_name, name + '_%04d.jpg' % i) uniface = np_rng.randint(0, 255, size=(250, 250, 3)) try: imsave(file_path, uniface) except ImportError: raise SkipTest("PIL not installed") # add some random file pollution to test robustness with open(os.path.join(LFW_HOME, 'lfw_funneled', '.test.swp'), 'wb') as f: f.write(six.b('Text file to be ignored by the dataset loader.')) # generate some pairing metadata files using the same format as LFW with open(os.path.join(LFW_HOME, 'pairsDevTrain.txt'), 'wb') as f: f.write(six.b("10\n")) more_than_two = [name for name, count in six.iteritems(counts) if count >= 2] for i in range(5): name = random_state.choice(more_than_two) first, second = random_state.sample(range(counts[name]), 2) f.write(six.b('%s\t%d\t%d\n' % (name, first, second))) for i in range(5): first_name, second_name = random_state.sample(FAKE_NAMES, 2) first_index = random_state.choice(np.arange(counts[first_name])) second_index = random_state.choice(np.arange(counts[second_name])) f.write(six.b('%s\t%d\t%s\t%d\n' % (first_name, first_index, second_name, second_index))) with open(os.path.join(LFW_HOME, 'pairsDevTest.txt'), 'wb') as f: f.write(six.b("Fake place holder that won't be tested")) with open(os.path.join(LFW_HOME, 'pairs.txt'), 'wb') as f: f.write(six.b("Fake place holder that won't be tested")) def teardown_module(): """Test fixture (clean up) run once after all tests of this module""" if os.path.isdir(SCIKIT_LEARN_DATA): shutil.rmtree(SCIKIT_LEARN_DATA) if os.path.isdir(SCIKIT_LEARN_EMPTY_DATA): shutil.rmtree(SCIKIT_LEARN_EMPTY_DATA) @raises(IOError) def test_load_empty_lfw_people(): load_lfw_people(data_home=SCIKIT_LEARN_EMPTY_DATA) def test_load_fake_lfw_people(): lfw_people = load_lfw_people(data_home=SCIKIT_LEARN_DATA, min_faces_per_person=3) # The data is croped around the center as a rectangular bounding box # arounthe the face. Colors are converted to gray levels: assert_equal(lfw_people.images.shape, (10, 62, 47)) assert_equal(lfw_people.data.shape, (10, 2914)) # the target is array of person integer ids assert_array_equal(lfw_people.target, [2, 0, 1, 0, 2, 0, 2, 1, 1, 2]) # names of the persons can be found using the target_names array expected_classes = ['Abdelatif Smith', 'Abhati Kepler', 'Onur Lopez'] assert_array_equal(lfw_people.target_names, expected_classes) # It is possible to ask for the original data without any croping or color # conversion and not limit on the number of picture per person lfw_people = load_lfw_people(data_home=SCIKIT_LEARN_DATA, resize=None, slice_=None, color=True) assert_equal(lfw_people.images.shape, (17, 250, 250, 3)) # the ids and class names are the same as previously assert_array_equal(lfw_people.target, [0, 0, 1, 6, 5, 6, 3, 6, 0, 3, 6, 1, 2, 4, 5, 1, 2]) assert_array_equal(lfw_people.target_names, ['Abdelatif Smith', 'Abhati Kepler', 'Camara Alvaro', 'Chen Dupont', 'John Lee', 'Lin Bauman', 'Onur Lopez']) @raises(ValueError) def test_load_fake_lfw_people_too_restrictive(): load_lfw_people(data_home=SCIKIT_LEARN_DATA, min_faces_per_person=100) @raises(IOError) def test_load_empty_lfw_pairs(): load_lfw_pairs(data_home=SCIKIT_LEARN_EMPTY_DATA) def test_load_fake_lfw_pairs(): lfw_pairs_train = load_lfw_pairs(data_home=SCIKIT_LEARN_DATA) # The data is croped around the center as a rectangular bounding box # arounthe the face. Colors are converted to gray levels: assert_equal(lfw_pairs_train.pairs.shape, (10, 2, 62, 47)) # the target is whether the person is the same or not assert_array_equal(lfw_pairs_train.target, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) # names of the persons can be found using the target_names array expected_classes = ['Different persons', 'Same person'] assert_array_equal(lfw_pairs_train.target_names, expected_classes) # It is possible to ask for the original data without any croping or color # conversion lfw_pairs_train = load_lfw_pairs(data_home=SCIKIT_LEARN_DATA, resize=None, slice_=None, color=True) assert_equal(lfw_pairs_train.pairs.shape, (10, 2, 250, 250, 3)) # the ids and class names are the same as previously assert_array_equal(lfw_pairs_train.target, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) assert_array_equal(lfw_pairs_train.target_names, expected_classes)
bsd-3-clause
ashhher3/scikit-learn
sklearn/decomposition/tests/test_incremental_pca.py
23
8317
"""Tests for Incremental PCA.""" import numpy as np from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_raises from sklearn import datasets from sklearn.decomposition import PCA, IncrementalPCA iris = datasets.load_iris() def test_incremental_pca(): """Incremental PCA on dense arrays.""" X = iris.data batch_size = X.shape[0] // 3 ipca = IncrementalPCA(n_components=2, batch_size=batch_size) pca = PCA(n_components=2) pca.fit_transform(X) X_transformed = ipca.fit_transform(X) np.testing.assert_equal(X_transformed.shape, (X.shape[0], 2)) assert_almost_equal(ipca.explained_variance_ratio_.sum(), pca.explained_variance_ratio_.sum(), 1) for n_components in [1, 2, X.shape[1]]: ipca = IncrementalPCA(n_components, batch_size=batch_size) ipca.fit(X) cov = ipca.get_covariance() precision = ipca.get_precision() assert_array_almost_equal(np.dot(cov, precision), np.eye(X.shape[1])) def test_incremental_pca_check_projection(): """Test that the projection of data is correct.""" rng = np.random.RandomState(1999) n, p = 100, 3 X = rng.randn(n, p) * .1 X[:10] += np.array([3, 4, 5]) Xt = 0.1 * rng.randn(1, p) + np.array([3, 4, 5]) # Get the reconstruction of the generated data X # Note that Xt has the same "components" as X, just separated # This is what we want to ensure is recreated correctly Yt = IncrementalPCA(n_components=2).fit(X).transform(Xt) # Normalize Yt /= np.sqrt((Yt ** 2).sum()) # Make sure that the first element of Yt is ~1, this means # the reconstruction worked as expected assert_almost_equal(np.abs(Yt[0][0]), 1., 1) def test_incremental_pca_inverse(): """Test that the projection of data can be inverted.""" rng = np.random.RandomState(1999) n, p = 50, 3 X = rng.randn(n, p) # spherical data X[:, 1] *= .00001 # make middle component relatively small X += [5, 4, 3] # make a large mean # same check that we can find the original data from the transformed # signal (since the data is almost of rank n_components) ipca = IncrementalPCA(n_components=2, batch_size=10).fit(X) Y = ipca.transform(X) Y_inverse = ipca.inverse_transform(Y) assert_almost_equal(X, Y_inverse, decimal=3) def test_incremental_pca_validation(): """Test that n_components is >=1 and <= n_features.""" X = [[0, 1], [1, 0]] for n_components in [-1, 0, .99, 3]: assert_raises(ValueError, IncrementalPCA(n_components, batch_size=10).fit, X) def test_incremental_pca_set_params(): """Test that components_ sign is stable over batch sizes.""" rng = np.random.RandomState(1999) n_samples = 100 n_features = 20 X = rng.randn(n_samples, n_features) X2 = rng.randn(n_samples, n_features) X3 = rng.randn(n_samples, n_features) ipca = IncrementalPCA(n_components=20) ipca.fit(X) # Decreasing number of components ipca.set_params(n_components=10) assert_raises(ValueError, ipca.partial_fit, X2) # Increasing number of components ipca.set_params(n_components=15) assert_raises(ValueError, ipca.partial_fit, X3) # Returning to original setting ipca.set_params(n_components=20) ipca.partial_fit(X) def test_incremental_pca_num_features_change(): """Test that changing n_components will raise an error.""" rng = np.random.RandomState(1999) n_samples = 100 X = rng.randn(n_samples, 20) X2 = rng.randn(n_samples, 50) ipca = IncrementalPCA(n_components=None) ipca.fit(X) assert_raises(ValueError, ipca.partial_fit, X2) def test_incremental_pca_batch_signs(): """Test that components_ sign is stable over batch sizes.""" rng = np.random.RandomState(1999) n_samples = 100 n_features = 3 X = rng.randn(n_samples, n_features) all_components = [] batch_sizes = np.arange(10, 20) for batch_size in batch_sizes: ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X) all_components.append(ipca.components_) for i, j in zip(all_components[:-1], all_components[1:]): assert_almost_equal(np.sign(i), np.sign(j), decimal=6) def test_incremental_pca_batch_values(): """Test that components_ values are stable over batch sizes.""" rng = np.random.RandomState(1999) n_samples = 100 n_features = 3 X = rng.randn(n_samples, n_features) all_components = [] batch_sizes = np.arange(20, 40, 3) for batch_size in batch_sizes: ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X) all_components.append(ipca.components_) for i, j in zip(all_components[:-1], all_components[1:]): assert_almost_equal(i, j, decimal=1) def test_incremental_pca_partial_fit(): """Test that fit and partial_fit get equivalent results.""" rng = np.random.RandomState(1999) n, p = 50, 3 X = rng.randn(n, p) # spherical data X[:, 1] *= .00001 # make middle component relatively small X += [5, 4, 3] # make a large mean # same check that we can find the original data from the transformed # signal (since the data is almost of rank n_components) batch_size = 10 ipca = IncrementalPCA(n_components=2, batch_size=batch_size).fit(X) pipca = IncrementalPCA(n_components=2, batch_size=batch_size) # Add one to make sure endpoint is included batch_itr = np.arange(0, n + 1, batch_size) for i, j in zip(batch_itr[:-1], batch_itr[1:]): pipca.partial_fit(X[i:j, :]) assert_almost_equal(ipca.components_, pipca.components_, decimal=3) def test_incremental_pca_against_pca_iris(): """Test that IncrementalPCA and PCA are approximate (to a sign flip).""" X = iris.data Y_pca = PCA(n_components=2).fit_transform(X) Y_ipca = IncrementalPCA(n_components=2, batch_size=25).fit_transform(X) assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1) def test_incremental_pca_against_pca_random_data(): """Test that IncrementalPCA and PCA are approximate (to a sign flip).""" rng = np.random.RandomState(1999) n_samples = 100 n_features = 3 X = rng.randn(n_samples, n_features) + 5 * rng.rand(1, n_features) Y_pca = PCA(n_components=3).fit_transform(X) Y_ipca = IncrementalPCA(n_components=3, batch_size=25).fit_transform(X) assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1) def test_explained_variances(): """Test that PCA and IncrementalPCA calculations match""" X = datasets.make_low_rank_matrix(1000, 100, tail_strength=0., effective_rank=10, random_state=1999) prec = 3 n_samples, n_features = X.shape for nc in [None, 99]: pca = PCA(n_components=nc).fit(X) ipca = IncrementalPCA(n_components=nc, batch_size=100).fit(X) assert_almost_equal(pca.explained_variance_, ipca.explained_variance_, decimal=prec) assert_almost_equal(pca.explained_variance_ratio_, ipca.explained_variance_ratio_, decimal=prec) assert_almost_equal(pca.noise_variance_, ipca.noise_variance_, decimal=prec) def test_whitening(): """Test that PCA and IncrementalPCA transforms match to sign flip.""" X = datasets.make_low_rank_matrix(1000, 10, tail_strength=0., effective_rank=2, random_state=1999) prec = 3 n_samples, n_features = X.shape for nc in [None, 9]: pca = PCA(whiten=True, n_components=nc).fit(X) ipca = IncrementalPCA(whiten=True, n_components=nc, batch_size=250).fit(X) Xt_pca = pca.transform(X) Xt_ipca = ipca.transform(X) assert_almost_equal(np.abs(Xt_pca), np.abs(Xt_ipca), decimal=prec) Xinv_ipca = ipca.inverse_transform(Xt_ipca) Xinv_pca = pca.inverse_transform(Xt_pca) assert_almost_equal(X, Xinv_ipca, decimal=prec) assert_almost_equal(X, Xinv_pca, decimal=prec) assert_almost_equal(Xinv_pca, Xinv_ipca, decimal=prec)
bsd-3-clause
chicm/carvana
car-segment/common.py
1
1940
import os os.environ['HOME'] = '/root' #os.environ['PYTHONUNBUFFERED'] = '1' #numerical libs import math import numpy as np import random import PIL import cv2 import matplotlib matplotlib.use('TkAgg') #matplotlib.use('Qt4Agg') #matplotlib.use('Qt5Agg') # torch libs import torch import torchvision.transforms as transforms from torch.utils.data.dataset import Dataset from torch.utils.data import DataLoader from torch.utils.data.sampler import * import torch.nn as nn import torch.nn.functional as F from torch.autograd import Variable import torch.optim as optim import torch.backends.cudnn as cudnn # std libs import collections import types import numbers import inspect import shutil #import pickle import dill from timeit import default_timer as timer #ubuntu: default_timer = time.time, seconds from datetime import datetime import csv import pandas as pd import pickle import glob import sys #from time import sleep from distutils.dir_util import copy_tree import zipfile import zlib import matplotlib.pyplot as plt import sklearn import sklearn.metrics from skimage import io from sklearn.metrics import fbeta_score ''' updating pytorch https://discuss.pytorch.org/t/updating-pytorch/309 ./conda config --add channels soumith conda update pytorch torchvision conda install pytorch torchvision cuda80 -c soumith ''' #--------------------------------------------------------------------------------- print('@%s: ' % os.path.basename(__file__)) if 0: SEED=235202 random.seed(SEED) np.random.seed(SEED) torch.manual_seed(SEED) torch.cuda.manual_seed(SEED) print ('\tset random seed') print ('\t\tSEED=%d'%SEED) if 1: cudnn.benchmark = True ##uses the inbuilt cudnn auto-tuner to find the fastest convolution algorithms. - print ('\tset cuda environment') print('') #---------------------------------------------------------------------------------
apache-2.0
vahndi/scitwi
scitwi/trends/trends.py
1
1635
from pandas import DataFrame from twitter.api import TwitterListResponse from scitwi.places.location import Location from scitwi.utils.attrs import datetime_attr, list_obj_attr from scitwi.utils.strs import obj_string, list_obj_string from .trend import Trend class Trends(object): def __init__(self, response: TwitterListResponse): self._response = response self.as_of = datetime_attr(response[0], 'as_of') self.created_at = datetime_attr(response[0], 'created_at') self.locations = list_obj_attr(response[0], 'locations', Location) self.trends_list = [ Trend(trend_dict=t, as_of=self.as_of, created_at=self.created_at, locations=self.locations) for t in self._response_dict['trends'] ] @property def _response_dict(self): return self._response[0] @property def trend_names(self): return [t.name for t in self.trends_list] @property def trends_dataframe(self): return DataFrame(self.trends_list) @staticmethod def common_trend_names(trends_1, trends_2): set_1 = set([trend.name for trend in trends_1.trends_list]) set_2 = set([trend.name for trend in trends_2.trends_list]) return sorted(set_1.intersection(set_2)) def __str__(self): str_out = '' str_out += obj_string('As Of', self.as_of) str_out += obj_string('Created At', self.created_at) str_out += list_obj_string('Locations', self.locations) str_out += list_obj_string('Trends', self.trend_names) return str_out
mit
bennlich/scikit-image
doc/examples/plot_hog.py
17
4358
""" =============================== Histogram of Oriented Gradients =============================== The `Histogram of Oriented Gradient <http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients>`__ (HOG) feature descriptor [1]_ is popular for object detection. In the following example, we compute the HOG descriptor and display a visualisation. Algorithm overview ------------------ Compute a Histogram of Oriented Gradients (HOG) by 1. (optional) global image normalisation 2. computing the gradient image in x and y 3. computing gradient histograms 4. normalising across blocks 5. flattening into a feature vector The first stage applies an optional global image normalisation equalisation that is designed to reduce the influence of illumination effects. In practice we use gamma (power law) compression, either computing the square root or the log of each colour channel. Image texture strength is typically proportional to the local surface illumination so this compression helps to reduce the effects of local shadowing and illumination variations. The second stage computes first order image gradients. These capture contour, silhouette and some texture information, while providing further resistance to illumination variations. The locally dominant colour channel is used, which provides colour invariance to a large extent. Variant methods may also include second order image derivatives, which act as primitive bar detectors - a useful feature for capturing, e.g. bar like structures in bicycles and limbs in humans. The third stage aims to produce an encoding that is sensitive to local image content while remaining resistant to small changes in pose or appearance. The adopted method pools gradient orientation information locally in the same way as the SIFT [2]_ feature. The image window is divided into small spatial regions, called "cells". For each cell we accumulate a local 1-D histogram of gradient or edge orientations over all the pixels in the cell. This combined cell-level 1-D histogram forms the basic "orientation histogram" representation. Each orientation histogram divides the gradient angle range into a fixed number of predetermined bins. The gradient magnitudes of the pixels in the cell are used to vote into the orientation histogram. The fourth stage computes normalisation, which takes local groups of cells and contrast normalises their overall responses before passing to next stage. Normalisation introduces better invariance to illumination, shadowing, and edge contrast. It is performed by accumulating a measure of local histogram "energy" over local groups of cells that we call "blocks". The result is used to normalise each cell in the block. Typically each individual cell is shared between several blocks, but its normalisations are block dependent and thus different. The cell thus appears several times in the final output vector with different normalisations. This may seem redundant but it improves the performance. We refer to the normalised block descriptors as Histogram of Oriented Gradient (HOG) descriptors. The final step collects the HOG descriptors from all blocks of a dense overlapping grid of blocks covering the detection window into a combined feature vector for use in the window classifier. References ---------- .. [1] Dalal, N. and Triggs, B., "Histograms of Oriented Gradients for Human Detection," IEEE Computer Society Conference on Computer Vision and Pattern Recognition, 2005, San Diego, CA, USA. .. [2] David G. Lowe, "Distinctive image features from scale-invariant keypoints," International Journal of Computer Vision, 60, 2 (2004), pp. 91-110. """ import matplotlib.pyplot as plt from skimage.feature import hog from skimage import data, color, exposure image = color.rgb2gray(data.astronaut()) fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=True) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4)) ax1.axis('off') ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Input image') # Rescale histogram for better display hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) ax2.axis('off') ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients') plt.show()
bsd-3-clause
amozie/amozie
testzie/opencv_helloworld.py
1
15032
import numpy as np from matplotlib import pyplot as plt import cv2 # show img = cv2.imread('f:/index.jpg', 1) cv2.namedWindow('img', cv2.WINDOW_NORMAL) cv2.imshow('img', img) k = cv2.waitKey(0) if k == ord('s'): print(chr(k)) cv2.destroyAllWindows() # draw def draw_circle(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: param[0] = True param[1] = (x, y) return if event == cv2.EVENT_LBUTTONUP: param[0] = False param[1] = None if param[0] and param[1] and event == cv2.EVENT_MOUSEMOVE: cv2.line(img, param[1], (x, y), (255, 0, 0), 10) param[1] = (x, y) img = np.zeros((512, 512, 3), np.uint8) + 255 cv2.namedWindow('image') cv2.setMouseCallback('image', draw_circle, [False, None]) while True: cv2.imshow('image', img) if cv2.waitKey(20) & 0xFF == 27: break cv2.destroyAllWindows() # basic img1 = cv2.imread('f:/index.jpg', cv2.IMREAD_COLOR) img2 = cv2.imread('f:/123.jpg', cv2.IMREAD_COLOR) # threshold def nothing(x): pass gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) ret, img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) cv2.namedWindow('img') cv2.createTrackbar('num', 'img', 0, 255, nothing) while True: cv2.imshow('img',img) if cv2.waitKey(1) == ord('q'): break num = cv2.getTrackbarPos('num', 'img') ret, img = cv2.threshold(gray, num, 255, cv2.THRESH_BINARY) cv2.destroyAllWindows() # bit calculation rows, cols, _ = img2.shape roi = img1[0:rows, 0:cols] ret, mask = cv2.threshold(gray, 160, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) img2_bg = cv2.bitwise_and(img2, img2, mask=mask) dst = cv2.add(img1_bg, img2_bg) cv2.imshow('dst', dst) img1[0:rows, 0:cols] = dst cv2.imshow('img', img1) # inRange cv2.imshow('img', img1) hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, np.array([0, 50, 0]), np.array([255, 255, 255])) cv2.imshow('mask', mask) res = cv2.bitwise_and(hsv, hsv, mask=mask) res = cv2.cvtColor(res, cv2.COLOR_HSV2BGR) cv2.imshow('res', res) # resize res = cv2.resize(img1, None, fx=2, fy=2) cv2.imshow('img', res) # Perspective Transform def mouse_event(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONUP: cv2.circle(img1, (x, y), 10, (0, 255, 0), 2) param.append([y, x]) img1 = cv2.imread('f:/index.jpg', cv2.IMREAD_COLOR) l = [] cv2.namedWindow('img') cv2.setMouseCallback('img', mouse_event, l) while True: cv2.imshow('img', img1) if cv2.waitKey(1) == ord('q'): break if len(l) == 4: w1 = l[2][1] - l[0][1] w2 = l[3][1] - l[1][1] h1 = l[1][0] - l[0][0] h2 = l[3][0] - l[2][0] w = int((w1 + w2) / 2) h = int((h1 + h2) / 2) p1 = np.float32(l) p2 = np.float32([[0, 0], [h, 0], [0, w], [h, w]]) M = cv2.getPerspectiveTransform(p1, p2) dst = cv2.warpPerspective(img1, M, (w, h)) cv2.imshow('dst', dst) cv2.destroyAllWindows() # pyramid img = cv2.imread('f:/index.jpg', cv2.IMREAD_COLOR) img = cv2.resize(img, (688, 400)) gp = [img] for i in range(4): img = cv2.pyrDown(img) gp.append(img) for i, v in enumerate(gp): cv2.imshow(str(i), v) lp = [gp[4]] for i in range(4, 0, -1): pu = cv2.pyrUp(gp[i]) sub = cv2.subtract(gp[i-1], pu) lp.append(sub) for i, v in enumerate(lp): cv2.imshow(str(i), v) ls = lp[0] for i, v in enumerate(lp): if i == 0: continue ls = cv2.pyrUp(ls) ls = cv2.add(ls, v) # contour img = cv2.imread('f:/index.jpg', 0) shape = img.shape img = cv2.bitwise_not(img) ret, th = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU) th = cv2.morphologyEx(img, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))) ret, th = cv2.threshold(th, 0, 255, cv2.THRESH_OTSU) cv2.imshow('th', th) image, contours, hierarchy = cv2.findContours(th, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) img = cv2.imread('f:/index.jpg') img = cv2.drawContours(img, contours, 7, (0, 0, 255), -1) cv2.imshow('img', img) # moment cnt = contours[7] m = cv2.moments(cnt) cx = int(m['m10']/m['m00']) cy = int(m['m01']/m['m00']) cv2.circle(img, (cx, cy), 2, (0, 0, 255), 2) cv2.imshow('img', img) cv2.contourArea(cnt) cv2.arcLength(cnt, True) epsilon = 0.01 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) img = cv2.polylines(img, approx.swapaxes(0, 1), True, (0, 255, 0), 2) cv2.imshow('img', img) hull = cv2.convexHull(cnt) img = cv2.polylines(img, hull.swapaxes(0, 1), True, (0, 255, 0), 2) cv2.imshow('img', img) mask = np.zeros(shape, np.uint8) cv2.drawContours(mask, [cnt], 0, 255, -1) cv2.minMaxLoc(img, mask) cv2.mean(img, mask) cv2.circle(img, tuple(cnt[cnt[:, :, 0].argmin()][0]), 2, (0, 0, 255), 2) cv2.circle(img, tuple(cnt[cnt[:, :, 0].argmax()][0]), 2, (0, 0, 255), 2) cv2.circle(img, tuple(cnt[cnt[:, :, 1].argmin()][0]), 2, (0, 0, 255), 2) cv2.circle(img, tuple(cnt[cnt[:, :, 1].argmax()][0]), 2, (0, 0, 255), 2) cv2.imshow('img', img) # region hull = cv2.convexHull(cnt, returnPoints=False) defects = cv2.convexityDefects(cnt, hull) for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] # cv2.circle(img, tuple(cnt[s][0]), 2, (0, 0, 255), 2) # cv2.circle(img, tuple(cnt[e][0]), 2, (0, 255, 0), 2) cv2.line(img, tuple(cnt[s][0]), tuple(cnt[e][0]), (0, 0, 255), 1) cv2.circle(img, tuple(cnt[f][0]), 2, (0, 255, 0), 2) cv2.imshow('img', img) dist = cv2.pointPolygonTest(cnt, (50, 50), True) inner = cv2.pointPolygonTest(cnt, (343, 218), False) img = cv2.imread('f:/index.jpg') img = cv2.drawContours(img, contours, 7, (0, 0, 255), 2) img = cv2.drawContours(img, contours, 9, (0, 0, 255), 2) cv2.imshow('img', img) cnt1 = contours[7] cnt2 = contours[9] cv2.matchShapes(cnt1, cnt2, cv2.CONTOURS_MATCH_I1, 0.0) # retrieval image, contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # histogram img = cv2.imread('f:/a.jpg', 0) hist = cv2.calcHist([img], [0], None, [256], [0, 256]) cdf = hist.cumsum() cdf_norm = cdf * hist.max() / cdf.max() plt.hist(img.ravel(), 256, [0, 256]) plt.plot(cdf_norm, 'r') cdf_m = np.ma.masked_equal(cdf, 0) cdf_m = (cdf_m - cdf_m.min()) * 255 / (cdf_m.max() - cdf_m.min()) cdf = np.ma.filled(cdf_m, 0).astype(np.uint8) img2 = cdf[img] cv2.imshow('img', img) cv2.imshow('img2', img2) equ = cv2.equalizeHist(img) cv2.imshow('equ', equ) clahe = cv2.createCLAHE(2, (8, 8)) cl = clahe.apply(img) cv2.imshow('cl', cl) # histogram 2D img = cv2.imread('f:/a.jpg') cv2.imshow('img', img) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256]) plt.imshow(hist) # back project img = cv2.imread('f:/se.jpg') p1 = 100 p2 = 200 roi = img[p1:p2, p1:p2].copy() # cv2.imshow('roi', roi) imgc = img.copy() imgc = cv2.rectangle(imgc, (p1, p1), (p2, p2), (255, 255, 255), 2) cv2.imshow('imgc', imgc) hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) hsvt = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) M = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256]) I = cv2.calcHist([hsvt], [0, 1], None, [180, 256], [0, 180, 0, 256]) Im = np.ma.masked_equal(I, 0.0) Rm = M/Im R = np.ma.filled(Rm, 1.0) h, s, v = cv2.split(hsvt) B = R[h.ravel(), s.ravel()] B = np.minimum(B, 1) B = B.reshape(hsvt.shape[:2]) cv2.imshow('B', B) disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) dst = cv2.filter2D(B, -1, disc) dst = np.uint8(dst) cv2.normalize(dst, dst, 0, 255, cv2.NORM_MINMAX) cv2.imshow('dst', dst) ret, th = cv2.threshold(dst, 5, 255, cv2.THRESH_BINARY) cv2.imshow('th', th) imgt = cv2.bitwise_and(img, img, mask=th) imgt = cv2.rectangle(imgt, (p1, p1), (p2, p2), (255, 255, 255), 2) cv2.imshow('imgt', imgt) B = cv2.calcBackProject([hsvt], [0, 1], M, [0, 180, 0, 256], 1) # FFT img = cv2.imread('f:/a.jpg', 0) f = np.fft.fft2(img) fs = np.fft.fftshift(f) mag = np.log(np.abs(fs)) mag = cv2.normalize(mag, mag, 0, 255, cv2.NORM_MINMAX) mag = mag.astype(int) plt.imshow(mag, 'gray') laplacian = np.array([[0, 1, 0],[1, -4, 1], [0, 1, 0]]) f = np.fft.fft2(laplacian) fs = np.fft.fftshift(f) mag = np.log(np.abs(fs)+1) plt.imshow(mag, 'gray') # match img = cv2.imread('f:/1.jpg', 0) ret, th = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU) th = cv2.bitwise_not(th) image, contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) x, y, w, h = cv2.boundingRect(contours[11]) img = cv2.imread('f:/1.jpg') cv2.rectangle(img, (x-3, y-3), (x+w+3, y+h+3), (0, 255, 0), 2) cv2.imshow('img', img) img = cv2.imread('f:/1.jpg', 0) roi = img[y-3:y+h+4,x-3:x+w+4].copy() hs, ws = roi.shape res = cv2.matchTemplate(img, roi, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) img = cv2.imread('f:/1.jpg') cv2.rectangle(img, max_loc, (max_loc[0]+ws, max_loc[1]+hs), (255, 0, 255), 2) cv2.namedWindow('img', cv2.WINDOW_NORMAL) cv2.imshow('img', img) loc = np.where(res >= 0.95) img = cv2.imread('f:/1.jpg') pts = [] for i, pt in enumerate(zip(*loc[::-1])): cv2.rectangle(img, pt, (pt[0]+ws, pt[1]+hs), (255, 0, 255), 2) cv2.putText(img, str(i+1), pt, cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 255), 2) cv2.namedWindow('img', cv2.WINDOW_NORMAL) cv2.imshow('img', img) # hough img = cv2.imread('f:/pat1.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edge = cv2.Canny(gray, 50, 150, apertureSize=3) cv2.imshow('edge',edge) lines = cv2.HoughLines(edge, 1, np.pi/360, 100) lines = lines.reshape(-1, 2) for r, theta in lines: a = np.cos(theta) b = np.sin(theta) x0 = r*a y0 = r*b x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.imshow('img', img) img = cv2.imread('f:/pat1.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edge = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLinesP(edge, 1, np.pi/360, 50, minLineLength=50, maxLineGap=10) lines = lines.reshape(-1, 4) for x1, y1, x2, y2 in lines: cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.imshow('img', img) # watershed img = cv2.imread('f:/bi.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV) cv2.imshow('th', th) mp = cv2.morphologyEx(th, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)), iterations=2) cv2.imshow('mp', mp) bg = cv2.dilate(mp, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)), iterations=3) cv2.imshow('bg', bg) dist = cv2.distanceTransform(mp, cv2.DIST_L1, 5) cv2.imshow('dist', dist) ret, fg = cv2.threshold(dist, 0.7*dist.max(), 255, cv2.THRESH_BINARY) fg = np.uint8(fg) cv2.imshow('fg', fg) unknown = cv2.subtract(bg, fg) cv2.imshow('unknown', unknown) ret, markers = cv2.connectedComponents(fg) # markers = (markers - markers.min())*255/(markers.max() - markers.min()) # markers = np.uint8(markers) # cv2.imshow('markers', markers) markers = markers + 1 markers[unknown == 255] = 0 markers3 = cv2.watershed(img, markers) img[markers3 == -1] = [255, 0, 0] cv2.imshow('img', img) # grabcut def grabcut(event, x, y, flags, param): global imga, point, flag, mask if event == cv2.EVENT_LBUTTONDOWN: cv2.circle(imga, (x, y), 5, (255, 0, 0, 64), -1) cv2.circle(mask, (x, y), 5, cv2.GC_PR_FGD, -1) point = (x, y) if event == cv2.EVENT_LBUTTONUP: point = None flag = True if point is not None and event == cv2.EVENT_MOUSEMOVE: cv2.line(imga, point, (x, y), (255, 0, 0, 64), 10) cv2.line(mask, point, (x, y), cv2.GC_PR_FGD, 10) point = (x, y) img = cv2.imread('f:/photo.jpg') imga = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # cv2.imshow('img', img) bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) mask = np.zeros(img.shape[:2], np.uint8) + cv2.GC_PR_BGD point = None flag = True cv2.namedWindow('image') cv2.setMouseCallback('image', grabcut, [False, None]) while True: cv2.imshow('image', imga) cv2.imshow('mask', cv2.multiply(mask2, 255)) if flag: cv2.grabCut(img, mask, None, bgdModel, fgdModel, 5) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype(np.uint8) mask2_not = cv2.subtract(1, mask2) img_m = cv2.bitwise_and(img, img, mask=mask2) img_m = cv2.cvtColor(img_m, cv2.COLOR_BGR2BGRA) img_n = cv2.bitwise_and(gray, gray, mask=mask2_not) img_n = cv2.merge([img_n, img_n, img_n, cv2.add(np.zeros(gray.shape, np.uint8), 255)]) imga = cv2.add(img_m, img_n) flag = False if cv2.waitKey(20) == ord('q'): break cv2.destroyAllWindows() # harris img = cv2.imread('f:/1.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = np.float32(gray) dst = cv2.cornerHarris(gray, 2, 3, 0.05) dst = cv2.dilate(dst, None) img[dst > 0.01*dst.max()] = [0, 0, 255] cv2.imshow('img', img) dst = np.uint8(dst) ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001) corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria) res = np.hstack((centroids, corners)) res = np.int0(res) img[res[:, 1], res[:, 0]] = [0, 0, 255] img[res[:, 3], res[:, 2]] = [0, 255, 0] for x1, y1, x2, y2 in res: cv2.circle(img, (x1, y1), 2, (0, 0, 255), -1) cv2.circle(img, (x2, y2), 2, (0, 255, 0), -1) cv2.imshow('img', img) # shi-tomasi img = cv2.imread('f:/1.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10) corners = np.int0(corners) for i in corners: x, y = i.ravel() cv2.circle(img, (x, y), 2, (0, 0, 255), -1) cv2.imshow('img', img) # SIFT img = cv2.imread('f:/index.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # SURF # FAST img = cv2.imread('f:/1.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) fast = cv2.FastFeatureDetector_create() kp = fast.detect(img, None) cv2.drawKeypoints(img, kp, img, color=(0, 0, 255)) cv2.imshow('img', img) fast.setNonmaxSuppression(False) kp = fast.detect(img, None) cv2.drawKeypoints(img, kp, img, color=(0, 0, 255)) cv2.imshow('img', img) # BRIEF & CenSurE(Star) img = cv2.imread('f:/a.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ORB img = cv2.imread('f:/a.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) orb = cv2.ORB_create() kp = orb.detect(gray, None) cv2.drawKeypoints(img, kp, img, color=(0, 0, 255)) cv2.imshow('img', img) # match img1 = cv2.imread('f:/m0.jpg', 0) img2 = cv2.imread('f:/m2.jpg', 0) orb = cv2.ORB_create() kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) bf = cv2.BFMatcher(cv2.NORM_HAMMING, True) matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=0) cv2.namedWindow('img', cv2.WINDOW_NORMAL) cv2.imshow('img', img3) # meanshift # camshift # optical flow
apache-2.0
HyukjinKwon/spark
python/pyspark/pandas/tests/test_sql.py
15
1979
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from pyspark import pandas as ps from pyspark.sql.utils import ParseException from pyspark.testing.pandasutils import PandasOnSparkTestCase from pyspark.testing.sqlutils import SQLTestUtils class SQLTest(PandasOnSparkTestCase, SQLTestUtils): def test_error_variable_not_exist(self): msg = "The key variable_foo in the SQL statement was not found.*" with self.assertRaisesRegex(ValueError, msg): ps.sql("select * from {variable_foo}") def test_error_unsupported_type(self): msg = "Unsupported variable type dict: {'a': 1}" with self.assertRaisesRegex(ValueError, msg): some_dict = {"a": 1} ps.sql("select * from {some_dict}") def test_error_bad_sql(self): with self.assertRaises(ParseException): ps.sql("this is not valid sql") if __name__ == "__main__": import unittest from pyspark.pandas.tests.test_sql import * # noqa: F401 try: import xmlrunner # type: ignore[import] testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2) except ImportError: testRunner = None unittest.main(testRunner=testRunner, verbosity=2)
apache-2.0
pinkavaj/gnuradio
gr-fec/python/fec/polar/decoder.py
24
10396
#!/usr/bin/env python # # Copyright 2015 Free Software Foundation, Inc. # # GNU Radio is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # GNU Radio 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 GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # import numpy as np from common import PolarCommon # for dev from encoder import PolarEncoder from matplotlib import pyplot as plt class PolarDecoder(PolarCommon): def __init__(self, n, k, frozen_bit_position, frozenbits=None): PolarCommon.__init__(self, n, k, frozen_bit_position, frozenbits) self.error_probability = 0.1 # this is kind of a dummy value. usually chosen individually. self.lrs = ((1 - self.error_probability) / self.error_probability, self.error_probability / (1 - self.error_probability)) self.llrs = np.log(self.lrs) def _llr_bit(self, bit): return self.llrs[bit] def _llr_odd(self, la, lb): # this functions uses the min-sum approximation # exact formula: np.log((np.exp(la + lb) + 1) / (np.exp(la) + np.exp(lb))) return np.sign(la) * np.sign(lb) * np.minimum(np.abs(la), np.abs(lb)) _f_vals = np.array((1.0, -1.0), dtype=float) def _llr_even(self, la, lb, f): return (la * self._f_vals[f]) + lb def _llr_bit_decision(self, llr): if llr < 0.0: ui = int(1) else: ui = int(0) return ui def _retrieve_bit_from_llr(self, lr, pos): f_index = np.where(self.frozen_bit_position == pos)[0] if not f_index.size == 0: ui = self.frozenbits[f_index][0] else: ui = self._llr_bit_decision(lr) return ui def _lr_bit(self, bit): return self.lrs[bit] def _lr_odd(self, la, lb): # la is upper branch and lb is lower branch return (la * lb + 1) / (la + lb) def _lr_even(self, la, lb, f): # la is upper branch and lb is lower branch, f is last decoded bit. return (la ** (1 - (2 * f))) * lb def _lr_bit_decision(self, lr): if lr < 1: return int(1) return int(0) def _get_even_indices_values(self, u_hat): # looks like overkill for some indexing, but zero and one based indexing mix-up gives you haedaches. return u_hat[1::2] def _get_odd_indices_values(self, u_hat): return u_hat[0::2] def _calculate_lrs(self, y, u): ue = self._get_even_indices_values(u) uo = self._get_odd_indices_values(u) ya = y[0:y.size//2] yb = y[(y.size//2):] la = self._lr_decision_element(ya, (ue + uo) % 2) lb = self._lr_decision_element(yb, ue) return la, lb def _lr_decision_element(self, y, u): if y.size == 1: return self._llr_bit(y[0]) if u.size % 2 == 0: # use odd branch formula la, lb = self._calculate_lrs(y, u) return self._llr_odd(la, lb) else: ui = u[-1] la, lb = self._calculate_lrs(y, u[0:-1]) return self._llr_even(la, lb, ui) def _retrieve_bit_from_lr(self, lr, pos): f_index = np.where(self.frozen_bit_position == pos)[0] if not f_index.size == 0: ui = self.frozenbits[f_index][0] else: ui = self._lr_bit_decision(lr) return ui def _lr_sc_decoder(self, y): # this is the standard SC decoder as derived from the formulas. It sticks to natural bit order. u = np.array([], dtype=int) for i in range(y.size): lr = self._lr_decision_element(y, u) ui = self._retrieve_bit_from_llr(lr, i) u = np.append(u, ui) return u def _llr_retrieve_bit(self, llr, pos): f_index = np.where(self.frozen_bit_position == pos)[0] if not f_index.size == 0: ui = self.frozenbits[f_index][0] else: ui = self._llr_bit_decision(llr) return ui def _butterfly_decode_bits(self, pos, graph, u): bit_num = u.size llr = graph[pos][0] ui = self._llr_retrieve_bit(llr, bit_num) # ui = self._llr_bit_decision(llr) u = np.append(u, ui) lower_right = pos + (self.N // 2) la = graph[pos][1] lb = graph[lower_right][1] graph[lower_right][0] = self._llr_even(la, lb, ui) llr = graph[lower_right][0] # ui = self._llr_bit_decision(llr) ui = self._llr_retrieve_bit(llr, u.size) u = np.append(u, ui) return graph, u def _lr_sc_decoder_efficient(self, y): graph = np.full((self.N, self.power + 1), np.NaN, dtype=float) for i in range(self.N): graph[i][self.power] = self._llr_bit(y[i]) decode_order = self._vector_bit_reversed(np.arange(self.N), self.power) decode_order = np.delete(decode_order, np.where(decode_order >= self.N // 2)) u = np.array([], dtype=int) for pos in decode_order: graph = self._butterfly(pos, 0, graph, u) graph, u = self._butterfly_decode_bits(pos, graph, u) return u def _stop_propagation(self, bf_entry_row, stage): # calculate break condition modulus = 2 ** (self.power - stage) # stage_size = self.N // (2 ** stage) # half_stage_size = stage_size // 2 half_stage_size = self.N // (2 ** (stage + 1)) stage_pos = bf_entry_row % modulus return stage_pos >= half_stage_size def _butterfly(self, bf_entry_row, stage, graph, u): if not self.power > stage: return graph if self._stop_propagation(bf_entry_row, stage): upper_right = bf_entry_row - self.N // (2 ** (stage + 1)) la = graph[upper_right][stage + 1] lb = graph[bf_entry_row][stage + 1] ui = u[-1] graph[bf_entry_row][stage] = self._llr_even(la, lb, ui) return graph # activate right side butterflies u_even = self._get_even_indices_values(u) u_odd = self._get_odd_indices_values(u) graph = self._butterfly(bf_entry_row, stage + 1, graph, (u_even + u_odd) % 2) lower_right = bf_entry_row + self.N // (2 ** (stage + 1)) graph = self._butterfly(lower_right, stage + 1, graph, u_even) la = graph[bf_entry_row][stage + 1] lb = graph[lower_right][stage + 1] graph[bf_entry_row][stage] = self._llr_odd(la, lb) return graph def decode(self, data, is_packed=False): if not len(data) == self.N: raise ValueError("len(data)={0} is not equal to n={1}!".format(len(data), self.N)) if is_packed: data = np.unpackbits(data) data = self._lr_sc_decoder_efficient(data) data = self._extract_info_bits(data) if is_packed: data = np.packbits(data) return data def _extract_info_bits_reversed(self, y): info_bit_positions_reversed = self._vector_bit_reversed(self.info_bit_position, self.power) return y[info_bit_positions_reversed] def decode_systematic(self, data): if not len(data) == self.N: raise ValueError("len(data)={0} is not equal to n={1}!".format(len(data), self.N)) # data = self._reverse_bits(data) data = self._lr_sc_decoder_efficient(data) data = self._encode_natural_order(data) data = self._extract_info_bits_reversed(data) return data def test_systematic_decoder(): ntests = 1000 n = 16 k = 8 frozenbitposition = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) encoder = PolarEncoder(n, k, frozenbitposition) decoder = PolarDecoder(n, k, frozenbitposition) for i in range(ntests): bits = np.random.randint(2, size=k) y = encoder.encode_systematic(bits) u_hat = decoder.decode_systematic(y) assert (bits == u_hat).all() def test_reverse_enc_dec(): n = 16 k = 8 frozenbits = np.zeros(n - k) frozenbitposition = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) bits = np.random.randint(2, size=k) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) print 'encoded:', encoded rx = decoder.decode(encoded) print 'bits:', bits print 'rx :', rx print (bits == rx).all() def compare_decoder_impls(): print '\nthis is decoder test' n = 8 k = 4 frozenbits = np.zeros(n - k) # frozenbitposition16 = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) frozenbitposition = np.array((0, 1, 2, 4), dtype=int) bits = np.random.randint(2, size=k) print 'bits:', bits encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) print 'encoded:', encoded rx_st = decoder._lr_sc_decoder(encoded) rx_eff = decoder._lr_sc_decoder_efficient(encoded) print 'standard :', rx_st print 'efficient:', rx_eff print (rx_st == rx_eff).all() def main(): # power = 3 # n = 2 ** power # k = 4 # frozenbits = np.zeros(n - k, dtype=int) # frozenbitposition = np.array((0, 1, 2, 4), dtype=int) # frozenbitposition4 = np.array((0, 1), dtype=int) # # # encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) # decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) # # bits = np.ones(k, dtype=int) # print "bits: ", bits # evec = encoder.encode(bits) # print "froz: ", encoder._insert_frozen_bits(bits) # print "evec: ", evec # # evec[1] = 0 # deced = decoder._lr_sc_decoder(evec) # print 'SC decoded:', deced # # test_reverse_enc_dec() # compare_decoder_impls() test_systematic_decoder() if __name__ == '__main__': main()
gpl-3.0
kevin-intel/scikit-learn
sklearn/utils/tests/test_pprint.py
11
27137
import re from pprint import PrettyPrinter import numpy as np from sklearn.utils._pprint import _EstimatorPrettyPrinter from sklearn.linear_model import LogisticRegressionCV from sklearn.pipeline import make_pipeline from sklearn.base import BaseEstimator, TransformerMixin from sklearn.feature_selection import SelectKBest, chi2 from sklearn import set_config, config_context # Ignore flake8 (lots of line too long issues) # flake8: noqa # Constructors excerpted to test pprinting class LogisticRegression(BaseEstimator): 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='warn', max_iter=100, multi_class='warn', verbose=0, warm_start=False, n_jobs=None, l1_ratio=None): 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 self.l1_ratio = l1_ratio def fit(self, X, y): return self class StandardScaler(TransformerMixin, BaseEstimator): def __init__(self, copy=True, with_mean=True, with_std=True): self.with_mean = with_mean self.with_std = with_std self.copy = copy def transform(self, X, copy=None): return self class RFE(BaseEstimator): def __init__(self, estimator, n_features_to_select=None, step=1, verbose=0): self.estimator = estimator self.n_features_to_select = n_features_to_select self.step = step self.verbose = verbose class GridSearchCV(BaseEstimator): def __init__(self, estimator, param_grid, scoring=None, n_jobs=None, iid='warn', refit=True, cv='warn', verbose=0, pre_dispatch='2*n_jobs', error_score='raise-deprecating', return_train_score=False): self.estimator = estimator self.param_grid = param_grid self.scoring = scoring self.n_jobs = n_jobs self.iid = iid self.refit = refit self.cv = cv self.verbose = verbose self.pre_dispatch = pre_dispatch self.error_score = error_score self.return_train_score = return_train_score class CountVectorizer(BaseEstimator): def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.max_df = max_df self.min_df = min_df self.max_features = max_features self.ngram_range = ngram_range self.vocabulary = vocabulary self.binary = binary self.dtype = dtype class Pipeline(BaseEstimator): def __init__(self, steps, memory=None): self.steps = steps self.memory = memory class SVC(BaseEstimator): def __init__(self, C=1.0, kernel='rbf', degree=3, gamma='auto_deprecated', coef0=0.0, shrinking=True, probability=False, tol=1e-3, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape='ovr', random_state=None): self.kernel = kernel self.degree = degree self.gamma = gamma self.coef0 = coef0 self.tol = tol self.C = C self.shrinking = shrinking self.probability = probability self.cache_size = cache_size self.class_weight = class_weight self.verbose = verbose self.max_iter = max_iter self.decision_function_shape = decision_function_shape self.random_state = random_state class PCA(BaseEstimator): def __init__(self, n_components=None, copy=True, whiten=False, svd_solver='auto', tol=0.0, iterated_power='auto', random_state=None): self.n_components = n_components self.copy = copy self.whiten = whiten self.svd_solver = svd_solver self.tol = tol self.iterated_power = iterated_power self.random_state = random_state class NMF(BaseEstimator): def __init__(self, n_components=None, init=None, solver='cd', beta_loss='frobenius', tol=1e-4, max_iter=200, random_state=None, alpha=0., l1_ratio=0., verbose=0, shuffle=False): self.n_components = n_components self.init = init self.solver = solver self.beta_loss = beta_loss self.tol = tol self.max_iter = max_iter self.random_state = random_state self.alpha = alpha self.l1_ratio = l1_ratio self.verbose = verbose self.shuffle = shuffle class SimpleImputer(BaseEstimator): def __init__(self, missing_values=np.nan, strategy="mean", fill_value=None, verbose=0, copy=True): self.missing_values = missing_values self.strategy = strategy self.fill_value = fill_value self.verbose = verbose self.copy = copy def test_basic(print_changed_only_false): # Basic pprint test lr = LogisticRegression() expected = """ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)""" expected = expected[1:] # remove first \n assert lr.__repr__() == expected def test_changed_only(): # Make sure the changed_only param is correctly used when True (default) lr = LogisticRegression(C=99) expected = """LogisticRegression(C=99)""" assert lr.__repr__() == expected # Check with a repr that doesn't fit on a single line lr = LogisticRegression(C=99, class_weight=.4, fit_intercept=False, tol=1234, verbose=True) expected = """ LogisticRegression(C=99, class_weight=0.4, fit_intercept=False, tol=1234, verbose=True)""" expected = expected[1:] # remove first \n assert lr.__repr__() == expected imputer = SimpleImputer(missing_values=0) expected = """SimpleImputer(missing_values=0)""" assert imputer.__repr__() == expected # Defaults to np.NaN, trying with float('NaN') imputer = SimpleImputer(missing_values=float('NaN')) expected = """SimpleImputer()""" assert imputer.__repr__() == expected # make sure array parameters don't throw error (see #13583) repr(LogisticRegressionCV(Cs=np.array([0.1, 1]))) def test_pipeline(print_changed_only_false): # Render a pipeline object pipeline = make_pipeline(StandardScaler(), LogisticRegression(C=999)) expected = """ Pipeline(memory=None, steps=[('standardscaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('logisticregression', LogisticRegression(C=999, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False))], verbose=False)""" expected = expected[1:] # remove first \n assert pipeline.__repr__() == expected def test_deeply_nested(print_changed_only_false): # Render a deeply nested estimator rfe = RFE(RFE(RFE(RFE(RFE(RFE(RFE(LogisticRegression()))))))) expected = """ RFE(estimator=RFE(estimator=RFE(estimator=RFE(estimator=RFE(estimator=RFE(estimator=RFE(estimator=LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False), n_features_to_select=None, step=1, verbose=0), n_features_to_select=None, step=1, verbose=0), n_features_to_select=None, step=1, verbose=0), n_features_to_select=None, step=1, verbose=0), n_features_to_select=None, step=1, verbose=0), n_features_to_select=None, step=1, verbose=0), n_features_to_select=None, step=1, verbose=0)""" expected = expected[1:] # remove first \n assert rfe.__repr__() == expected def test_gridsearch(print_changed_only_false): # render a gridsearch param_grid = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4], 'C': [1, 10, 100, 1000]}, {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}] gs = GridSearchCV(SVC(), param_grid, cv=5) expected = """ GridSearchCV(cv=5, error_score='raise-deprecating', estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto_deprecated', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False), iid='warn', n_jobs=None, param_grid=[{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']}, {'C': [1, 10, 100, 1000], 'kernel': ['linear']}], pre_dispatch='2*n_jobs', refit=True, return_train_score=False, scoring=None, verbose=0)""" expected = expected[1:] # remove first \n assert gs.__repr__() == expected def test_gridsearch_pipeline(print_changed_only_false): # render a pipeline inside a gridsearch pp = _EstimatorPrettyPrinter(compact=True, indent=1, indent_at_name=True) pipeline = Pipeline([ ('reduce_dim', PCA()), ('classify', SVC()) ]) N_FEATURES_OPTIONS = [2, 4, 8] C_OPTIONS = [1, 10, 100, 1000] param_grid = [ { 'reduce_dim': [PCA(iterated_power=7), NMF()], 'reduce_dim__n_components': N_FEATURES_OPTIONS, 'classify__C': C_OPTIONS }, { 'reduce_dim': [SelectKBest(chi2)], 'reduce_dim__k': N_FEATURES_OPTIONS, 'classify__C': C_OPTIONS } ] gspipline = GridSearchCV(pipeline, cv=3, n_jobs=1, param_grid=param_grid) expected = """ GridSearchCV(cv=3, error_score='raise-deprecating', estimator=Pipeline(memory=None, steps=[('reduce_dim', PCA(copy=True, iterated_power='auto', n_components=None, random_state=None, svd_solver='auto', tol=0.0, whiten=False)), ('classify', SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto_deprecated', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False))]), iid='warn', n_jobs=1, param_grid=[{'classify__C': [1, 10, 100, 1000], 'reduce_dim': [PCA(copy=True, iterated_power=7, n_components=None, random_state=None, svd_solver='auto', tol=0.0, whiten=False), NMF(alpha=0.0, beta_loss='frobenius', init=None, l1_ratio=0.0, max_iter=200, n_components=None, random_state=None, shuffle=False, solver='cd', tol=0.0001, verbose=0)], 'reduce_dim__n_components': [2, 4, 8]}, {'classify__C': [1, 10, 100, 1000], 'reduce_dim': [SelectKBest(k=10, score_func=<function chi2 at some_address>)], 'reduce_dim__k': [2, 4, 8]}], pre_dispatch='2*n_jobs', refit=True, return_train_score=False, scoring=None, verbose=0)""" expected = expected[1:] # remove first \n repr_ = pp.pformat(gspipline) # Remove address of '<function chi2 at 0x.....>' for reproducibility repr_ = re.sub('function chi2 at 0x.*>', 'function chi2 at some_address>', repr_) assert repr_ == expected def test_n_max_elements_to_show(print_changed_only_false): n_max_elements_to_show = 30 pp = _EstimatorPrettyPrinter( compact=True, indent=1, indent_at_name=True, n_max_elements_to_show=n_max_elements_to_show ) # No ellipsis vocabulary = {i: i for i in range(n_max_elements_to_show)} vectorizer = CountVectorizer(vocabulary=vocabulary) expected = r""" CountVectorizer(analyzer='word', binary=False, decode_error='strict', dtype=<class 'numpy.int64'>, encoding='utf-8', input='content', lowercase=True, max_df=1.0, max_features=None, min_df=1, ngram_range=(1, 1), preprocessor=None, stop_words=None, strip_accents=None, token_pattern='(?u)\\b\\w\\w+\\b', tokenizer=None, vocabulary={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29})""" expected = expected[1:] # remove first \n assert pp.pformat(vectorizer) == expected # Now with ellipsis vocabulary = {i: i for i in range(n_max_elements_to_show + 1)} vectorizer = CountVectorizer(vocabulary=vocabulary) expected = r""" CountVectorizer(analyzer='word', binary=False, decode_error='strict', dtype=<class 'numpy.int64'>, encoding='utf-8', input='content', lowercase=True, max_df=1.0, max_features=None, min_df=1, ngram_range=(1, 1), preprocessor=None, stop_words=None, strip_accents=None, token_pattern='(?u)\\b\\w\\w+\\b', tokenizer=None, vocabulary={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, ...})""" expected = expected[1:] # remove first \n assert pp.pformat(vectorizer) == expected # Also test with lists param_grid = {'C': list(range(n_max_elements_to_show))} gs = GridSearchCV(SVC(), param_grid) expected = """ GridSearchCV(cv='warn', error_score='raise-deprecating', estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto_deprecated', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False), iid='warn', n_jobs=None, param_grid={'C': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]}, pre_dispatch='2*n_jobs', refit=True, return_train_score=False, scoring=None, verbose=0)""" expected = expected[1:] # remove first \n assert pp.pformat(gs) == expected # Now with ellipsis param_grid = {'C': list(range(n_max_elements_to_show + 1))} gs = GridSearchCV(SVC(), param_grid) expected = """ GridSearchCV(cv='warn', error_score='raise-deprecating', estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto_deprecated', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False), iid='warn', n_jobs=None, param_grid={'C': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ...]}, pre_dispatch='2*n_jobs', refit=True, return_train_score=False, scoring=None, verbose=0)""" expected = expected[1:] # remove first \n assert pp.pformat(gs) == expected def test_bruteforce_ellipsis(print_changed_only_false): # Check that the bruteforce ellipsis (used when the number of non-blank # characters exceeds N_CHAR_MAX) renders correctly. lr = LogisticRegression() # test when the left and right side of the ellipsis aren't on the same # line. expected = """ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, in... multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)""" expected = expected[1:] # remove first \n assert expected == lr.__repr__(N_CHAR_MAX=150) # test with very small N_CHAR_MAX # Note that N_CHAR_MAX is not strictly enforced, but it's normal: to avoid # weird reprs we still keep the whole line of the right part (after the # ellipsis). expected = """ Lo... warm_start=False)""" expected = expected[1:] # remove first \n assert expected == lr.__repr__(N_CHAR_MAX=4) # test with N_CHAR_MAX == number of non-blank characters: In this case we # don't want ellipsis full_repr = lr.__repr__(N_CHAR_MAX=float('inf')) n_nonblank = len(''.join(full_repr.split())) assert lr.__repr__(N_CHAR_MAX=n_nonblank) == full_repr assert '...' not in full_repr # test with N_CHAR_MAX == number of non-blank characters - 10: the left and # right side of the ellispsis are on different lines. In this case we # want to expend the whole line of the right side expected = """ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_i... multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)""" expected = expected[1:] # remove first \n assert expected == lr.__repr__(N_CHAR_MAX=n_nonblank - 10) # test with N_CHAR_MAX == number of non-blank characters - 10: the left and # right side of the ellispsis are on the same line. In this case we don't # want to expend the whole line of the right side, just add the ellispsis # between the 2 sides. expected = """ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter..., multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)""" expected = expected[1:] # remove first \n assert expected == lr.__repr__(N_CHAR_MAX=n_nonblank - 4) # test with N_CHAR_MAX == number of non-blank characters - 2: the left and # right side of the ellispsis are on the same line, but adding the ellipsis # would actually make the repr longer. So we don't add the ellipsis. expected = """ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)""" expected = expected[1:] # remove first \n assert expected == lr.__repr__(N_CHAR_MAX=n_nonblank - 2) def test_builtin_prettyprinter(): # non regression test than ensures we can still use the builtin # PrettyPrinter class for estimators (as done e.g. by joblib). # Used to be a bug PrettyPrinter().pprint(LogisticRegression()) def test_kwargs_in_init(): # Make sure the changed_only=True mode is OK when an argument is passed as # kwargs. # Non-regression test for # https://github.com/scikit-learn/scikit-learn/issues/17206 class WithKWargs(BaseEstimator): # Estimator with a kwargs argument. These need to hack around # set_params and get_params. Here we mimic what LightGBM does. def __init__(self, a='willchange', b='unchanged', **kwargs): self.a = a self.b = b self._other_params = {} self.set_params(**kwargs) def get_params(self, deep=True): params = super().get_params(deep=deep) params.update(self._other_params) return params def set_params(self, **params): for key, value in params.items(): setattr(self, key, value) self._other_params[key] = value return self est = WithKWargs(a='something', c='abcd', d=None) expected = "WithKWargs(a='something', c='abcd', d=None)" assert expected == est.__repr__() with config_context(print_changed_only=False): expected = "WithKWargs(a='something', b='unchanged', c='abcd', d=None)" assert expected == est.__repr__() def test_complexity_print_changed_only(): # Make sure `__repr__` is called the same amount of times # whether `print_changed_only` is True or False # Non-regression test for # https://github.com/scikit-learn/scikit-learn/issues/18490 class DummyEstimator(TransformerMixin, BaseEstimator): nb_times_repr_called = 0 def __init__(self, estimator=None): self.estimator = estimator def __repr__(self): DummyEstimator.nb_times_repr_called += 1 return super().__repr__() def transform(self, X, copy=None): # pragma: no cover return X estimator = DummyEstimator(make_pipeline(DummyEstimator(DummyEstimator()), DummyEstimator(), 'passthrough')) with config_context(print_changed_only=False): repr(estimator) nb_repr_print_changed_only_false = DummyEstimator.nb_times_repr_called DummyEstimator.nb_times_repr_called = 0 with config_context(print_changed_only=True): repr(estimator) nb_repr_print_changed_only_true = DummyEstimator.nb_times_repr_called assert nb_repr_print_changed_only_false == nb_repr_print_changed_only_true
bsd-3-clause
Eric89GXL/mne-python
mne/decoding/mixin.py
14
2851
class TransformerMixin(object): """Mixin class for all transformers in scikit-learn.""" def fit_transform(self, X, y=None, **fit_params): """Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X. Parameters ---------- X : array, shape (n_samples, n_features) Training set. y : array, shape (n_samples,) Target values. **fit_params : dict Additional fitting parameters passed to ``self.fit``. Returns ------- X_new : array, shape (n_samples, n_features_new) Transformed array. """ # non-optimized default implementation; override when a better # method is possible for a given clustering algorithm if y is None: # fit method of arity 1 (unsupervised transformation) return self.fit(X, **fit_params).transform(X) else: # fit method of arity 2 (supervised transformation) return self.fit(X, y, **fit_params).transform(X) class EstimatorMixin(object): """Mixin class for estimators.""" def get_params(self, deep=True): """Get the estimator params. Parameters ---------- deep : bool Deep. """ return def set_params(self, **params): """Set parameters (mimics sklearn API). Parameters ---------- **params : dict Extra parameters. Returns ------- inst : object The instance. """ if not params: return self valid_params = self.get_params(deep=True) for key, value in params.items(): split = key.split('__', 1) if len(split) > 1: # nested objects case name, sub_name = split if name not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (name, self)) sub_object = valid_params[name] sub_object.set_params(**{sub_name: value}) else: # simple objects case if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self.__class__.__name__)) setattr(self, key, value) return self
bsd-3-clause
JoeJimFlood/RugbyPredictifier
2017SuperRugby/Validation/matchup_w_distance.py
1
14552
import os import sys import pandas as pd import numpy as np from numpy.random import poisson, uniform from numpy import mean import time import math po = True team_homes = pd.read_csv(os.path.join(os.path.split(__file__)[0], 'TeamHomes.csv'), header = None, index_col = 0) stadium_locs = pd.read_csv(os.path.join(os.path.split(__file__)[0], 'StadiumLocs.csv'), index_col = 0) teamsheetpath = os.path.join(os.path.split(__file__)[0], 'teamcsvs') compstat = {'TF': 'TA', 'TA': 'TF', #Dictionary to use to compare team stats with opponent stats 'CF': 'CA', 'CA': 'CF', 'CON%F': 'CON%A', 'CON%A': 'CON%F', 'PF': 'PA', 'PA': 'PF', 'DGF': 'DGA', 'DGA': 'DGF'} def get_opponent_stats(opponent, venue): #Gets summaries of statistics for opponent each week opponent_stats = {} global teamsheetpath, stadium_locs, team_homes opp_stats = pd.DataFrame.from_csv(os.path.join(teamsheetpath, opponent + '.csv')) opponent_home = team_homes[1][opponent] (venue_lat, venue_lng) = stadium_locs.loc[venue, ['Lat', 'Long']] (opponent_home_lat, opponent_home_lng) = stadium_locs.loc[opponent_home, ['Lat', 'Long']] opponent_reference_distance = geodesic_distance(opponent_home_lat, opponent_home_lng, venue_lat, venue_lng) def get_opponent_weight(location): return get_travel_weight(location, opponent_home_lat, opponent_home_lng, opponent_reference_distance) opp_stats['Weight'] = opp_stats['VENUE'].apply(get_opponent_weight) for stat in opp_stats.columns: if stat != 'VENUE': if stat != 'OPP': opponent_stats.update({stat: np.average(opp_stats[stat], weights = opp_stats['Weight'])}) opponent_stats.update({'CON%F': float((opp_stats['CF']*opp_stats['Weight']).sum())/(opp_stats['TF']*opp_stats['Weight']).sum()}) opponent_stats.update({'CON%A': float((opp_stats['CA']*opp_stats['Weight']).sum())/(opp_stats['TA']*opp_stats['Weight']).sum()}) return opponent_stats def get_residual_performance(score_df): #Get how each team has done compared to the average performance of their opponents global teamsheetpath, team_homes, stadium_locs #score_df = pd.DataFrame.from_csv(os.path.join(teamsheetpath, team + '.csv')) residual_stats = {} score_df['CON%F'] = np.nan score_df['CON%A'] = np.nan for week in score_df.index: opponent_stats = get_opponent_stats(score_df['OPP'][week], score_df['VENUE'][week]) for stat in opponent_stats: if week == score_df.index.tolist()[0]: score_df['OPP_' + stat] = np.nan score_df['OPP_' + stat][week] = opponent_stats[stat] score_df['CON%F'][week] = float(score_df['CF'][week]) / score_df['TF'][week] score_df['CON%A'][week] = float(score_df['CA'][week]) / score_df['TA'][week] #print opponent_stats for stat in opponent_stats: if stat == 'Weight': continue score_df['R_' + stat] = score_df[stat] - score_df['OPP_' + compstat[stat]] if stat in ['TF', 'PF', 'DGF', 'TA', 'PA', 'DGA']: residual_stats.update({stat: np.average(score_df['R_' + stat], weights = score_df['Weight'])}) elif stat == 'CON%F': residual_stats.update({stat: (score_df['R_CON%F'].multiply(score_df['TF'])*score_df['Weight']).sum() / (score_df['TF']*score_df['Weight']).sum()}) elif stat == 'CON%A': residual_stats.update({stat: (score_df['R_CON%A'].multiply(score_df['TA'])*score_df['Weight']).sum() / (score_df['TA']*score_df['Weight']).sum()}) return residual_stats def get_score(expected_scores): #Get the score for a team based on expected scores score = 0 if expected_scores['T'] > 0: tries = poisson(expected_scores['T']) else: tries = poisson(0.01) score = score + 6 * tries if expected_scores['P'] > 0: fgs = poisson(expected_scores['P']) else: fgs = poisson(0.01) score = score + 3 * fgs if expected_scores['DG'] > 0: sfs = poisson(expected_scores['DG']) else: sfs = poisson(0.01) score = score + 2 * sfs for t in range(tries): successful_con_determinant = uniform(0, 1) if successful_con_determinant <= expected_scores['CONPROB']: score += 2 else: continue #if tries >= 4: # bp = True #else: # bp = False return (score, tries) def game(team_1, team_2, expected_scores_1, expected_scores_2, playoff = False): #Get two scores and determine a winner (score_1, tries_1) = get_score(expected_scores_1) (score_2, tries_2) = get_score(expected_scores_2) if tries_1 - tries_2 >= 3: bp1 = True bp2 = False elif tries_2 - tries_1 >= 3: bp1 = False bp2 = True else: bp1 = False bp2 = False if score_1 > score_2: win_1 = 1 win_2 = 0 draw_1 = 0 draw_2 = 0 if bp1: bpw1 = 1 else: bpw1 = 0 if bp2: bpl2 = 1 else: bpl2 = 0 bpl1 = 0 bpw2 = 0 bpd1 = 0 bpd2 = 0 lbp1 = 0 if score_1 - score_2 <= 7: lbp2 = 1 else: lbp2 = 0 elif score_2 > score_1: win_1 = 0 win_2 = 1 draw_1 = 0 draw_2 = 0 if bp1: bpl1 = 1 else: bpl1 = 0 if bp2: bpw2 = 1 else: bpw2 = 0 bpw1 = 0 bpl2 = 0 bpd1 = 0 bpd2 = 0 lbp2 = 0 if score_2 - score_1 <= 7: lbp1 = 1 else: lbp1 = 0 else: if playoff: win_1 = 0.5 win_2 = 0.5 draw_1 = 0 draw_2 = 0 bpw1 = 0 bpw2 = 0 bpd1 = 0 bpd2 = 0 bpl1 = 0 bpl2 = 0 lbp1 = 0 lbp2 = 0 else: win_1 = 0 win_2 = 0 draw_1 = 1 draw_2 = 1 bpw1 = 0 bpw2 = 0 bpl1 = 0 bpl2 = 0 lbp1 = 0 lbp2 = 0 if bp1: bpd1 = 1 else: bpd1 = 0 if bp2: bpd2 = 1 else: bpd2 = 0 summary = {team_1: [win_1, draw_1, score_1, bpw1, bpd1, bpl1, lbp1]} summary.update({team_2: [win_2, draw_2, score_2, bpw2, bpd2, bpl2, lbp2]}) return summary def get_expected_scores(team_1_stats, team_2_stats, team_1_df, team_2_df): #Get the expected scores for a matchup based on the previous teams' performances expected_scores = {} for stat in team_1_stats: expected_scores.update({'T': mean([team_1_stats['TF'] + np.average(team_2_df['TA'], weights = team_2_df['Weight']), team_2_stats['TA'] + np.average(team_1_df['TF'], weights = team_1_df['Weight'])])}) expected_scores.update({'P': mean([team_1_stats['PF'] + np.average(team_2_df['PA'], weights = team_2_df['Weight']), team_2_stats['PA'] + np.average(team_1_df['PF'], weights = team_1_df['Weight'])])}) expected_scores.update({'DG': mean([team_1_stats['DGF'] + np.average(team_2_df['DGA'], weights = team_2_df['Weight']), team_2_stats['DGA'] + np.average(team_1_df['DGF'], weights = team_1_df['Weight'])])}) #print mean([team_1_stats['PAT1%F'] + team_2_df['PAT1AS'].astype('float').sum() / team_2_df['PAT1AA'].sum(), # team_2_stats['PAT1%A'] + team_1_df['PAT1FS'].astype('float').sum() / team_1_df['PAT1FA'].sum()]) conprob = mean([team_1_stats['CON%F'] + (team_2_df['CA']*team_2_df['Weight']).sum() / (team_2_df['TA']*team_2_df['Weight']).sum(), team_2_stats['CON%A'] + (team_1_df['CF']*team_1_df['Weight']).sum() / (team_1_df['TF']*team_1_df['Weight']).sum()]) if not math.isnan(conprob): expected_scores.update({'CONPROB': conprob}) else: expected_scores.update({'CONPROB': 0.75}) #print(expected_scores['PAT1PROB']) #print(expected_scores) return expected_scores def geodesic_distance(olat, olng, dlat, dlng): ''' Returns geodesic distance in percentage of half the earth's circumference between two points on the earth's surface ''' scale = math.tau/360 olat *= scale olng *= scale dlat *= scale dlng *= scale delta_lat = (dlat - olat) delta_lng = (dlng - olng) a = math.sin(delta_lat/2)**2 + math.cos(olat)*math.cos(dlat)*math.sin(delta_lng/2)**2 return 4*math.atan2(math.sqrt(a), math.sqrt(1-a))/math.tau def get_travel_weight(venue, home_lat, home_lng, reference_distance): ''' Gets the travel weight based on a venue, a team's home lat/long coordinates, and a reference distance ''' global stadium_locs (venue_lat, venue_lng) = stadium_locs.loc[venue, ['Lat', 'Long']] travel_distance = geodesic_distance(home_lat, home_lng, venue_lat, venue_lng) return 1 - abs(travel_distance - reference_distance) def matchup(team_1, team_2, venue = None): ts = time.time() global team_homes, stadium_locs team_1_home = team_homes[1][team_1] team_2_home = team_homes[1][team_2] if venue is None: venue = team_homes[1][team_1] (venue_lat, venue_lng) = stadium_locs.loc[venue, ['Lat', 'Long']] (team_1_home_lat, team_1_home_lng) = stadium_locs.loc[team_1_home, ['Lat', 'Long']] (team_2_home_lat, team_2_home_lng) = stadium_locs.loc[team_2_home, ['Lat', 'Long']] team_1_reference_distance = geodesic_distance(team_1_home_lat, team_1_home_lng, venue_lat, venue_lng) team_2_reference_distance = geodesic_distance(team_2_home_lat, team_2_home_lng, venue_lat, venue_lng) def get_team_1_weight(location): return get_travel_weight(location, team_1_home_lat, team_1_home_lng, team_1_reference_distance) def get_team_2_weight(location): return get_travel_weight(location, team_2_home_lat, team_2_home_lng, team_2_reference_distance) team_1_season = pd.DataFrame.from_csv(os.path.join(teamsheetpath, team_1 + '.csv')) team_2_season = pd.DataFrame.from_csv(os.path.join(teamsheetpath, team_2 + '.csv')) team_1_season['Weight'] = team_1_season['VENUE'].apply(get_team_1_weight) team_2_season['Weight'] = team_2_season['VENUE'].apply(get_team_2_weight) stats_1 = get_residual_performance(team_1_season) stats_2 = get_residual_performance(team_2_season) expected_scores_1 = get_expected_scores(stats_1, stats_2, team_1_season, team_2_season) expected_scores_2 = get_expected_scores(stats_2, stats_1, team_2_season, team_1_season) team_1_wins = 0 team_2_wins = 0 team_1_draws = 0 team_2_draws = 0 team_1_bpw = 0 team_2_bpw = 0 team_1_bpd = 0 team_2_bpd = 0 team_1_bpl = 0 team_2_bpl = 0 team_1_lbp = 0 team_2_lbp = 0 team_1_scores = [] team_2_scores = [] i = 0 error = 1 while error > 0.000001 or i < 5000000: #Run until convergence after 5 million iterations summary = game(team_1, team_2, expected_scores_1, expected_scores_2, playoff = po) team_1_prev_wins = team_1_wins team_1_wins += summary[team_1][0] team_2_wins += summary[team_2][0] team_1_draws += summary[team_1][1] team_2_draws += summary[team_2][1] team_1_scores.append(summary[team_1][2]) team_2_scores.append(summary[team_2][2]) team_1_bpw += summary[team_1][3] team_2_bpw += summary[team_2][3] team_1_bpd += summary[team_1][4] team_2_bpd += summary[team_2][4] team_1_bpl += summary[team_1][5] team_2_bpl += summary[team_2][5] team_1_lbp += summary[team_1][6] team_2_lbp += summary[team_2][6] team_1_prob = float(team_1_wins) / len(team_1_scores) team_2_prob = float(team_2_wins) / len(team_2_scores) team_1_bpw_prob = float(team_1_bpw) / len(team_1_scores) team_2_bpw_prob = float(team_2_bpw) / len(team_2_scores) team_1_bpd_prob = float(team_1_bpd) / len(team_1_scores) team_2_bpd_prob = float(team_2_bpd) / len(team_2_scores) team_1_bpl_prob = float(team_1_bpl) / len(team_1_scores) team_2_bpl_prob = float(team_2_bpl) / len(team_2_scores) team_1_lbp_prob = float(team_1_lbp) / len(team_1_scores) team_2_lbp_prob = float(team_2_lbp) / len(team_2_scores) if i > 0: team_1_prev_prob = float(team_1_prev_wins) / i error = team_1_prob - team_1_prev_prob i = i + 1 if i == 5000000: print('Probability converged within 5 million iterations') else: print('Probability converged after ' + str(i) + ' iterations') games = pd.DataFrame.from_items([(team_1, team_1_scores), (team_2, team_2_scores)]) pre_summaries = games.describe(percentiles = list(np.linspace(0.05, 0.95, 19))) summaries = pd.DataFrame(columns = pre_summaries.columns) summaries.loc['mean'] = pre_summaries.loc['mean'] for i in pre_summaries.index: try: percentile = int(round(float(i[:-1]))) summaries.loc['{}%'.format(percentile)] = pre_summaries.loc[i] except ValueError: continue summaries = summaries.reset_index() for item in summaries.index: try: summaries['index'][item] = str(int(float(summaries['index'][item][:-1]))) + '%' except ValueError: continue bonus_points = pd.DataFrame(index = ['4-Try Bonus Point with Win', '4-Try Bonus Point with Draw', '4-Try Bonus Point with Loss', 'Losing Bonus Point']) bonus_points[team_1] = [team_1_bpw_prob, team_1_bpd_prob, team_1_bpl_prob, team_1_lbp_prob] bonus_points[team_2] = [team_2_bpw_prob, team_2_bpd_prob, team_2_bpl_prob, team_2_lbp_prob] summaries = summaries.set_index('index') summaries = summaries.groupby(level = 0).last() output = {'ProbWin': {team_1: team_1_prob, team_2: team_2_prob}, 'Scores': summaries, 'Bonus Points': bonus_points} print(team_1 + '/' + team_2 + ' score distributions computed in ' + str(round(time.time() - ts, 1)) + ' seconds') return output
mit
wlamond/scikit-learn
examples/svm/plot_svm_nonlinear.py
62
1119
""" ============== Non-linear SVM ============== Perform binary classification using non-linear SVC with RBF kernel. The target to predict is a XOR of the inputs. The color map illustrates the decision function learned by the SVC. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500)) np.random.seed(0) X = np.random.randn(300, 2) Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0) # fit the model clf = svm.NuSVC() clf.fit(X, Y) # plot the decision function for each datapoint on the grid Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.imshow(Z, interpolation='nearest', extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto', origin='lower', cmap=plt.cm.PuOr_r) contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2, linetypes='--') plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors='k') plt.xticks(()) plt.yticks(()) plt.axis([-3, 3, -3, 3]) plt.show()
bsd-3-clause
JPFrancoia/scikit-learn
sklearn/metrics/pairwise.py
5
46491
# -*- coding: utf-8 -*- # Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Mathieu Blondel <mathieu@mblondel.org> # Robert Layton <robertlayton@gmail.com> # Andreas Mueller <amueller@ais.uni-bonn.de> # Philippe Gervais <philippe.gervais@inria.fr> # Lars Buitinck # Joel Nothman <joel.nothman@gmail.com> # License: BSD 3 clause import itertools import numpy as np from scipy.spatial import distance from scipy.sparse import csr_matrix from scipy.sparse import issparse from ..utils import check_array from ..utils import gen_even_slices from ..utils import gen_batches from ..utils.fixes import partial from ..utils.extmath import row_norms, safe_sparse_dot from ..preprocessing import normalize from ..externals.joblib import Parallel from ..externals.joblib import delayed from ..externals.joblib.parallel import cpu_count from .pairwise_fast import _chi2_kernel_fast, _sparse_manhattan # Utility Functions def _return_float_dtype(X, Y): """ 1. If dtype of X and Y is float32, then dtype float32 is returned. 2. Else dtype float is returned. """ if not issparse(X) and not isinstance(X, np.ndarray): X = np.asarray(X) if Y is None: Y_dtype = X.dtype elif not issparse(Y) and not isinstance(Y, np.ndarray): Y = np.asarray(Y) Y_dtype = Y.dtype else: Y_dtype = Y.dtype if X.dtype == Y_dtype == np.float32: dtype = np.float32 else: dtype = np.float return X, Y, dtype def check_pairwise_arrays(X, Y, precomputed=False, dtype=None): """ Set X and Y appropriately and checks inputs If Y is None, it is set as a pointer to X (i.e. not a copy). If Y is given, this does not happen. All distance metrics should use this function first to assert that the given parameters are correct and safe to use. Specifically, this function first ensures that both X and Y are arrays, then checks that they are at least two dimensional while ensuring that their elements are floats (or dtype if provided). Finally, the function checks that the size of the second dimension of the two arrays is equal, or the equivalent check for a precomputed distance matrix. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples_a, n_features) Y : {array-like, sparse matrix}, shape (n_samples_b, n_features) precomputed : bool True if X is to be treated as precomputed distances to the samples in Y. dtype : string, type, list of types or None (default=None) Data type required for X and Y. If None, the dtype will be an appropriate float type selected by _return_float_dtype. .. versionadded:: 0.18 Returns ------- safe_X : {array-like, sparse matrix}, shape (n_samples_a, n_features) An array equal to X, guaranteed to be a numpy array. safe_Y : {array-like, sparse matrix}, shape (n_samples_b, n_features) An array equal to Y if Y was not None, guaranteed to be a numpy array. If Y was None, safe_Y will be a pointer to X. """ X, Y, dtype_float = _return_float_dtype(X, Y) warn_on_dtype = dtype is not None estimator = 'check_pairwise_arrays' if dtype is None: dtype = dtype_float if Y is X or Y is None: X = Y = check_array(X, accept_sparse='csr', dtype=dtype, warn_on_dtype=warn_on_dtype, estimator=estimator) else: X = check_array(X, accept_sparse='csr', dtype=dtype, warn_on_dtype=warn_on_dtype, estimator=estimator) Y = check_array(Y, accept_sparse='csr', dtype=dtype, warn_on_dtype=warn_on_dtype, estimator=estimator) if precomputed: if X.shape[1] != Y.shape[0]: raise ValueError("Precomputed metric requires shape " "(n_queries, n_indexed). Got (%d, %d) " "for %d indexed." % (X.shape[0], X.shape[1], Y.shape[0])) elif X.shape[1] != Y.shape[1]: raise ValueError("Incompatible dimension for X and Y matrices: " "X.shape[1] == %d while Y.shape[1] == %d" % ( X.shape[1], Y.shape[1])) return X, Y def check_paired_arrays(X, Y): """ Set X and Y appropriately and checks inputs for paired distances All paired distance metrics should use this function first to assert that the given parameters are correct and safe to use. Specifically, this function first ensures that both X and Y are arrays, then checks that they are at least two dimensional while ensuring that their elements are floats. Finally, the function checks that the size of the dimensions of the two arrays are equal. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples_a, n_features) Y : {array-like, sparse matrix}, shape (n_samples_b, n_features) Returns ------- safe_X : {array-like, sparse matrix}, shape (n_samples_a, n_features) An array equal to X, guaranteed to be a numpy array. safe_Y : {array-like, sparse matrix}, shape (n_samples_b, n_features) An array equal to Y if Y was not None, guaranteed to be a numpy array. If Y was None, safe_Y will be a pointer to X. """ X, Y = check_pairwise_arrays(X, Y) if X.shape != Y.shape: raise ValueError("X and Y should be of same shape. They were " "respectively %r and %r long." % (X.shape, Y.shape)) return X, Y # Pairwise distances def euclidean_distances(X, Y=None, Y_norm_squared=None, squared=False, X_norm_squared=None): """ Considering the rows of X (and Y=X) as vectors, compute the distance matrix between each pair of vectors. For efficiency reasons, the euclidean distance between a pair of row vector x and y is computed as:: dist(x, y) = sqrt(dot(x, x) - 2 * dot(x, y) + dot(y, y)) This formulation has two advantages over other ways of computing distances. First, it is computationally efficient when dealing with sparse data. Second, if one argument varies but the other remains unchanged, then `dot(x, x)` and/or `dot(y, y)` can be pre-computed. However, this is not the most precise way of doing this computation, and the distance matrix returned by this function may not be exactly symmetric as required by, e.g., ``scipy.spatial.distance`` functions. Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples_1, n_features) Y : {array-like, sparse matrix}, shape (n_samples_2, n_features) Y_norm_squared : array-like, shape (n_samples_2, ), optional Pre-computed dot-products of vectors in Y (e.g., ``(Y**2).sum(axis=1)``) squared : boolean, optional Return squared Euclidean distances. X_norm_squared : array-like, shape = [n_samples_1], optional Pre-computed dot-products of vectors in X (e.g., ``(X**2).sum(axis=1)``) Returns ------- distances : {array, sparse matrix}, shape (n_samples_1, n_samples_2) Examples -------- >>> from sklearn.metrics.pairwise import euclidean_distances >>> X = [[0, 1], [1, 1]] >>> # distance between rows of X >>> euclidean_distances(X, X) array([[ 0., 1.], [ 1., 0.]]) >>> # get distance to origin >>> euclidean_distances(X, [[0, 0]]) array([[ 1. ], [ 1.41421356]]) See also -------- paired_distances : distances betweens pairs of elements of X and Y. """ X, Y = check_pairwise_arrays(X, Y) if X_norm_squared is not None: XX = check_array(X_norm_squared) if XX.shape == (1, X.shape[0]): XX = XX.T elif XX.shape != (X.shape[0], 1): raise ValueError( "Incompatible dimensions for X and X_norm_squared") else: XX = row_norms(X, squared=True)[:, np.newaxis] if X is Y: # shortcut in the common case euclidean_distances(X, X) YY = XX.T elif Y_norm_squared is not None: YY = np.atleast_2d(Y_norm_squared) if YY.shape != (1, Y.shape[0]): raise ValueError( "Incompatible dimensions for Y and Y_norm_squared") else: YY = row_norms(Y, squared=True)[np.newaxis, :] distances = safe_sparse_dot(X, Y.T, dense_output=True) distances *= -2 distances += XX distances += YY np.maximum(distances, 0, out=distances) if X is Y: # Ensure that distances between vectors and themselves are set to 0.0. # This may not be the case due to floating point rounding errors. distances.flat[::distances.shape[0] + 1] = 0.0 return distances if squared else np.sqrt(distances, out=distances) def pairwise_distances_argmin_min(X, Y, axis=1, metric="euclidean", batch_size=500, metric_kwargs=None): """Compute minimum distances between one point and a set of points. This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). The minimal distances are also returned. This is mostly equivalent to calling: (pairwise_distances(X, Y=Y, metric=metric).argmin(axis=axis), pairwise_distances(X, Y=Y, metric=metric).min(axis=axis)) but uses much less memory, and is faster for large arrays. Parameters ---------- X, Y : {array-like, sparse matrix} Arrays containing points. Respective shapes (n_samples1, n_features) and (n_samples2, n_features) batch_size : integer To reduce memory consumption over the naive solution, data are processed in batches, comprising batch_size rows of X and batch_size rows of Y. The default value is quite conservative, but can be changed for fine-tuning. The larger the number, the larger the memory usage. metric : string or callable, default 'euclidean' metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used. If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy's metrics, but is less efficient than passing the metric name as a string. Distance matrices are not supported. Valid values for metric are: - from scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2', 'manhattan'] - from scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev', 'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule'] See the documentation for scipy.spatial.distance for details on these metrics. metric_kwargs : dict, optional Keyword arguments to pass to specified metric function. axis : int, optional, default 1 Axis along which the argmin and distances are to be computed. Returns ------- argmin : numpy.ndarray Y[argmin[i], :] is the row in Y that is closest to X[i, :]. distances : numpy.ndarray distances[i] is the distance between the i-th row in X and the argmin[i]-th row in Y. See also -------- sklearn.metrics.pairwise_distances sklearn.metrics.pairwise_distances_argmin """ dist_func = None if metric in PAIRWISE_DISTANCE_FUNCTIONS: dist_func = PAIRWISE_DISTANCE_FUNCTIONS[metric] elif not callable(metric) and not isinstance(metric, str): raise ValueError("'metric' must be a string or a callable") X, Y = check_pairwise_arrays(X, Y) if metric_kwargs is None: metric_kwargs = {} if axis == 0: X, Y = Y, X # Allocate output arrays indices = np.empty(X.shape[0], dtype=np.intp) values = np.empty(X.shape[0]) values.fill(np.infty) for chunk_x in gen_batches(X.shape[0], batch_size): X_chunk = X[chunk_x, :] for chunk_y in gen_batches(Y.shape[0], batch_size): Y_chunk = Y[chunk_y, :] if dist_func is not None: if metric == 'euclidean': # special case, for speed d_chunk = safe_sparse_dot(X_chunk, Y_chunk.T, dense_output=True) d_chunk *= -2 d_chunk += row_norms(X_chunk, squared=True)[:, np.newaxis] d_chunk += row_norms(Y_chunk, squared=True)[np.newaxis, :] np.maximum(d_chunk, 0, d_chunk) else: d_chunk = dist_func(X_chunk, Y_chunk, **metric_kwargs) else: d_chunk = pairwise_distances(X_chunk, Y_chunk, metric=metric, **metric_kwargs) # Update indices and minimum values using chunk min_indices = d_chunk.argmin(axis=1) min_values = d_chunk[np.arange(chunk_x.stop - chunk_x.start), min_indices] flags = values[chunk_x] > min_values indices[chunk_x][flags] = min_indices[flags] + chunk_y.start values[chunk_x][flags] = min_values[flags] if metric == "euclidean" and not metric_kwargs.get("squared", False): np.sqrt(values, values) return indices, values def pairwise_distances_argmin(X, Y, axis=1, metric="euclidean", batch_size=500, metric_kwargs=None): """Compute minimum distances between one point and a set of points. This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). This is mostly equivalent to calling: pairwise_distances(X, Y=Y, metric=metric).argmin(axis=axis) but uses much less memory, and is faster for large arrays. This function works with dense 2D arrays only. Parameters ---------- X : array-like Arrays containing points. Respective shapes (n_samples1, n_features) and (n_samples2, n_features) Y : array-like Arrays containing points. Respective shapes (n_samples1, n_features) and (n_samples2, n_features) batch_size : integer To reduce memory consumption over the naive solution, data are processed in batches, comprising batch_size rows of X and batch_size rows of Y. The default value is quite conservative, but can be changed for fine-tuning. The larger the number, the larger the memory usage. metric : string or callable metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used. If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy's metrics, but is less efficient than passing the metric name as a string. Distance matrices are not supported. Valid values for metric are: - from scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2', 'manhattan'] - from scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev', 'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule'] See the documentation for scipy.spatial.distance for details on these metrics. metric_kwargs : dict keyword arguments to pass to specified metric function. axis : int, optional, default 1 Axis along which the argmin and distances are to be computed. Returns ------- argmin : numpy.ndarray Y[argmin[i], :] is the row in Y that is closest to X[i, :]. See also -------- sklearn.metrics.pairwise_distances sklearn.metrics.pairwise_distances_argmin_min """ if metric_kwargs is None: metric_kwargs = {} return pairwise_distances_argmin_min(X, Y, axis, metric, batch_size, metric_kwargs)[0] def manhattan_distances(X, Y=None, sum_over_features=True, size_threshold=5e8): """ Compute the L1 distances between the vectors in X and Y. With sum_over_features equal to False it returns the componentwise distances. Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array_like An array with shape (n_samples_X, n_features). Y : array_like, optional An array with shape (n_samples_Y, n_features). sum_over_features : bool, default=True If True the function returns the pairwise distance matrix else it returns the componentwise L1 pairwise-distances. Not supported for sparse matrix inputs. size_threshold : int, default=5e8 Unused parameter. Returns ------- D : array If sum_over_features is False shape is (n_samples_X * n_samples_Y, n_features) and D contains the componentwise L1 pairwise-distances (ie. absolute difference), else shape is (n_samples_X, n_samples_Y) and D contains the pairwise L1 distances. Examples -------- >>> from sklearn.metrics.pairwise import manhattan_distances >>> manhattan_distances([[3]], [[3]])#doctest:+ELLIPSIS array([[ 0.]]) >>> manhattan_distances([[3]], [[2]])#doctest:+ELLIPSIS array([[ 1.]]) >>> manhattan_distances([[2]], [[3]])#doctest:+ELLIPSIS array([[ 1.]]) >>> manhattan_distances([[1, 2], [3, 4]],\ [[1, 2], [0, 3]])#doctest:+ELLIPSIS array([[ 0., 2.], [ 4., 4.]]) >>> import numpy as np >>> X = np.ones((1, 2)) >>> y = 2 * np.ones((2, 2)) >>> manhattan_distances(X, y, sum_over_features=False)#doctest:+ELLIPSIS array([[ 1., 1.], [ 1., 1.]]...) """ X, Y = check_pairwise_arrays(X, Y) if issparse(X) or issparse(Y): if not sum_over_features: raise TypeError("sum_over_features=%r not supported" " for sparse matrices" % sum_over_features) X = csr_matrix(X, copy=False) Y = csr_matrix(Y, copy=False) D = np.zeros((X.shape[0], Y.shape[0])) _sparse_manhattan(X.data, X.indices, X.indptr, Y.data, Y.indices, Y.indptr, X.shape[1], D) return D if sum_over_features: return distance.cdist(X, Y, 'cityblock') D = X[:, np.newaxis, :] - Y[np.newaxis, :, :] D = np.abs(D, D) return D.reshape((-1, X.shape[1])) def cosine_distances(X, Y=None): """Compute cosine distance between samples in X and Y. Cosine distance is defined as 1.0 minus the cosine similarity. Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array_like, sparse matrix with shape (n_samples_X, n_features). Y : array_like, sparse matrix (optional) with shape (n_samples_Y, n_features). Returns ------- distance matrix : array An array with shape (n_samples_X, n_samples_Y). See also -------- sklearn.metrics.pairwise.cosine_similarity scipy.spatial.distance.cosine (dense matrices only) """ # 1.0 - cosine_similarity(X, Y) without copy S = cosine_similarity(X, Y) S *= -1 S += 1 return S # Paired distances def paired_euclidean_distances(X, Y): """ Computes the paired euclidean distances between X and Y Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array-like, shape (n_samples, n_features) Y : array-like, shape (n_samples, n_features) Returns ------- distances : ndarray (n_samples, ) """ X, Y = check_paired_arrays(X, Y) return row_norms(X - Y) def paired_manhattan_distances(X, Y): """Compute the L1 distances between the vectors in X and Y. Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array-like, shape (n_samples, n_features) Y : array-like, shape (n_samples, n_features) Returns ------- distances : ndarray (n_samples, ) """ X, Y = check_paired_arrays(X, Y) diff = X - Y if issparse(diff): diff.data = np.abs(diff.data) return np.squeeze(np.array(diff.sum(axis=1))) else: return np.abs(diff).sum(axis=-1) def paired_cosine_distances(X, Y): """ Computes the paired cosine distances between X and Y Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array-like, shape (n_samples, n_features) Y : array-like, shape (n_samples, n_features) Returns ------- distances : ndarray, shape (n_samples, ) Notes ------ The cosine distance is equivalent to the half the squared euclidean distance if each sample is normalized to unit norm """ X, Y = check_paired_arrays(X, Y) return .5 * row_norms(normalize(X) - normalize(Y), squared=True) PAIRED_DISTANCES = { 'cosine': paired_cosine_distances, 'euclidean': paired_euclidean_distances, 'l2': paired_euclidean_distances, 'l1': paired_manhattan_distances, 'manhattan': paired_manhattan_distances, 'cityblock': paired_manhattan_distances} def paired_distances(X, Y, metric="euclidean", **kwds): """ Computes the paired distances between X and Y. Computes the distances between (X[0], Y[0]), (X[1], Y[1]), etc... Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : ndarray (n_samples, n_features) Array 1 for distance computation. Y : ndarray (n_samples, n_features) Array 2 for distance computation. metric : string or callable The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options specified in PAIRED_DISTANCES, including "euclidean", "manhattan", or "cosine". Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. Returns ------- distances : ndarray (n_samples, ) Examples -------- >>> from sklearn.metrics.pairwise import paired_distances >>> X = [[0, 1], [1, 1]] >>> Y = [[0, 1], [2, 1]] >>> paired_distances(X, Y) array([ 0., 1.]) See also -------- pairwise_distances : pairwise distances. """ if metric in PAIRED_DISTANCES: func = PAIRED_DISTANCES[metric] return func(X, Y) elif callable(metric): # Check the matrix first (it is usually done by the metric) X, Y = check_paired_arrays(X, Y) distances = np.zeros(len(X)) for i in range(len(X)): distances[i] = metric(X[i], Y[i]) return distances else: raise ValueError('Unknown distance %s' % metric) # Kernels def linear_kernel(X, Y=None): """ Compute the linear kernel between X and Y. Read more in the :ref:`User Guide <linear_kernel>`. Parameters ---------- X : array of shape (n_samples_1, n_features) Y : array of shape (n_samples_2, n_features) Returns ------- Gram matrix : array of shape (n_samples_1, n_samples_2) """ X, Y = check_pairwise_arrays(X, Y) return safe_sparse_dot(X, Y.T, dense_output=True) def polynomial_kernel(X, Y=None, degree=3, gamma=None, coef0=1): """ Compute the polynomial kernel between X and Y:: K(X, Y) = (gamma <X, Y> + coef0)^degree Read more in the :ref:`User Guide <polynomial_kernel>`. Parameters ---------- X : ndarray of shape (n_samples_1, n_features) Y : ndarray of shape (n_samples_2, n_features) degree : int, default 3 gamma : float, default None if None, defaults to 1.0 / n_samples_1 coef0 : int, default 1 Returns ------- Gram matrix : array of shape (n_samples_1, n_samples_2) """ X, Y = check_pairwise_arrays(X, Y) if gamma is None: gamma = 1.0 / X.shape[1] K = safe_sparse_dot(X, Y.T, dense_output=True) K *= gamma K += coef0 K **= degree return K def sigmoid_kernel(X, Y=None, gamma=None, coef0=1): """ Compute the sigmoid kernel between X and Y:: K(X, Y) = tanh(gamma <X, Y> + coef0) Read more in the :ref:`User Guide <sigmoid_kernel>`. Parameters ---------- X : ndarray of shape (n_samples_1, n_features) Y : ndarray of shape (n_samples_2, n_features) gamma : float, default None If None, defaults to 1.0 / n_samples_1 coef0 : int, default 1 Returns ------- Gram matrix : array of shape (n_samples_1, n_samples_2) """ X, Y = check_pairwise_arrays(X, Y) if gamma is None: gamma = 1.0 / X.shape[1] K = safe_sparse_dot(X, Y.T, dense_output=True) K *= gamma K += coef0 np.tanh(K, K) # compute tanh in-place return K def rbf_kernel(X, Y=None, gamma=None): """ Compute the rbf (gaussian) kernel between X and Y:: K(x, y) = exp(-gamma ||x-y||^2) for each pair of rows x in X and y in Y. Read more in the :ref:`User Guide <rbf_kernel>`. Parameters ---------- X : array of shape (n_samples_X, n_features) Y : array of shape (n_samples_Y, n_features) gamma : float, default None If None, defaults to 1.0 / n_samples_X Returns ------- kernel_matrix : array of shape (n_samples_X, n_samples_Y) """ X, Y = check_pairwise_arrays(X, Y) if gamma is None: gamma = 1.0 / X.shape[1] K = euclidean_distances(X, Y, squared=True) K *= -gamma np.exp(K, K) # exponentiate K in-place return K def laplacian_kernel(X, Y=None, gamma=None): """Compute the laplacian kernel between X and Y. The laplacian kernel is defined as:: K(x, y) = exp(-gamma ||x-y||_1) for each pair of rows x in X and y in Y. Read more in the :ref:`User Guide <laplacian_kernel>`. .. versionadded:: 0.17 Parameters ---------- X : array of shape (n_samples_X, n_features) Y : array of shape (n_samples_Y, n_features) gamma : float, default None If None, defaults to 1.0 / n_samples_X Returns ------- kernel_matrix : array of shape (n_samples_X, n_samples_Y) """ X, Y = check_pairwise_arrays(X, Y) if gamma is None: gamma = 1.0 / X.shape[1] K = -gamma * manhattan_distances(X, Y) np.exp(K, K) # exponentiate K in-place return K def cosine_similarity(X, Y=None, dense_output=True): """Compute cosine similarity between samples in X and Y. Cosine similarity, or the cosine kernel, computes similarity as the normalized dot product of X and Y: K(X, Y) = <X, Y> / (||X||*||Y||) On L2-normalized data, this function is equivalent to linear_kernel. Read more in the :ref:`User Guide <cosine_similarity>`. Parameters ---------- X : ndarray or sparse array, shape: (n_samples_X, n_features) Input data. Y : ndarray or sparse array, shape: (n_samples_Y, n_features) Input data. If ``None``, the output will be the pairwise similarities between all samples in ``X``. dense_output : boolean (optional), default True Whether to return dense output even when the input is sparse. If ``False``, the output is sparse if both input arrays are sparse. .. versionadded:: 0.17 parameter ``dense_output`` for dense output. Returns ------- kernel matrix : array An array with shape (n_samples_X, n_samples_Y). """ # to avoid recursive import X, Y = check_pairwise_arrays(X, Y) X_normalized = normalize(X, copy=True) if X is Y: Y_normalized = X_normalized else: Y_normalized = normalize(Y, copy=True) K = safe_sparse_dot(X_normalized, Y_normalized.T, dense_output=dense_output) return K def additive_chi2_kernel(X, Y=None): """Computes the additive chi-squared kernel between observations in X and Y The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms. The chi-squared kernel is given by:: k(x, y) = -Sum [(x - y)^2 / (x + y)] It can be interpreted as a weighted difference per entry. Read more in the :ref:`User Guide <chi2_kernel>`. Notes ----- As the negative of a distance, this kernel is only conditionally positive definite. Parameters ---------- X : array-like of shape (n_samples_X, n_features) Y : array of shape (n_samples_Y, n_features) Returns ------- kernel_matrix : array of shape (n_samples_X, n_samples_Y) References ---------- * Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 http://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf See also -------- chi2_kernel : The exponentiated version of the kernel, which is usually preferable. sklearn.kernel_approximation.AdditiveChi2Sampler : A Fourier approximation to this kernel. """ if issparse(X) or issparse(Y): raise ValueError("additive_chi2 does not support sparse matrices.") X, Y = check_pairwise_arrays(X, Y) if (X < 0).any(): raise ValueError("X contains negative values.") if Y is not X and (Y < 0).any(): raise ValueError("Y contains negative values.") result = np.zeros((X.shape[0], Y.shape[0]), dtype=X.dtype) _chi2_kernel_fast(X, Y, result) return result def chi2_kernel(X, Y=None, gamma=1.): """Computes the exponential chi-squared kernel X and Y. The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms. The chi-squared kernel is given by:: k(x, y) = exp(-gamma Sum [(x - y)^2 / (x + y)]) It can be interpreted as a weighted difference per entry. Read more in the :ref:`User Guide <chi2_kernel>`. Parameters ---------- X : array-like of shape (n_samples_X, n_features) Y : array of shape (n_samples_Y, n_features) gamma : float, default=1. Scaling parameter of the chi2 kernel. Returns ------- kernel_matrix : array of shape (n_samples_X, n_samples_Y) References ---------- * Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 http://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf See also -------- additive_chi2_kernel : The additive version of this kernel sklearn.kernel_approximation.AdditiveChi2Sampler : A Fourier approximation to the additive version of this kernel. """ K = additive_chi2_kernel(X, Y) K *= gamma return np.exp(K, K) # Helper functions - distance PAIRWISE_DISTANCE_FUNCTIONS = { # If updating this dictionary, update the doc in both distance_metrics() # and also in pairwise_distances()! 'cityblock': manhattan_distances, 'cosine': cosine_distances, 'euclidean': euclidean_distances, 'l2': euclidean_distances, 'l1': manhattan_distances, 'manhattan': manhattan_distances, 'precomputed': None, # HACK: precomputed is always allowed, never called } def distance_metrics(): """Valid metrics for pairwise_distances. This function simply returns the valid pairwise distance metrics. It exists to allow for a description of the mapping for each of the valid strings. The valid distance metrics, and the function they map to, are: ============ ==================================== metric Function ============ ==================================== 'cityblock' metrics.pairwise.manhattan_distances 'cosine' metrics.pairwise.cosine_distances 'euclidean' metrics.pairwise.euclidean_distances 'l1' metrics.pairwise.manhattan_distances 'l2' metrics.pairwise.euclidean_distances 'manhattan' metrics.pairwise.manhattan_distances ============ ==================================== Read more in the :ref:`User Guide <metrics>`. """ return PAIRWISE_DISTANCE_FUNCTIONS def _parallel_pairwise(X, Y, func, n_jobs, **kwds): """Break the pairwise matrix in n_jobs even slices and compute them in parallel""" if n_jobs < 0: n_jobs = max(cpu_count() + 1 + n_jobs, 1) if Y is None: Y = X if n_jobs == 1: # Special case to avoid picklability checks in delayed return func(X, Y, **kwds) # TODO: in some cases, backend='threading' may be appropriate fd = delayed(func) ret = Parallel(n_jobs=n_jobs, verbose=0)( fd(X, Y[s], **kwds) for s in gen_even_slices(Y.shape[0], n_jobs)) return np.hstack(ret) def _pairwise_callable(X, Y, metric, **kwds): """Handle the callable case for pairwise_{distances,kernels} """ X, Y = check_pairwise_arrays(X, Y) if X is Y: # Only calculate metric for upper triangle out = np.zeros((X.shape[0], Y.shape[0]), dtype='float') iterator = itertools.combinations(range(X.shape[0]), 2) for i, j in iterator: out[i, j] = metric(X[i], Y[j], **kwds) # Make symmetric # NB: out += out.T will produce incorrect results out = out + out.T # Calculate diagonal # NB: nonzero diagonals are allowed for both metrics and kernels for i in range(X.shape[0]): x = X[i] out[i, i] = metric(x, x, **kwds) else: # Calculate all cells out = np.empty((X.shape[0], Y.shape[0]), dtype='float') iterator = itertools.product(range(X.shape[0]), range(Y.shape[0])) for i, j in iterator: out[i, j] = metric(X[i], Y[j], **kwds) return out _VALID_METRICS = ['euclidean', 'l2', 'l1', 'manhattan', 'cityblock', 'braycurtis', 'canberra', 'chebyshev', 'correlation', 'cosine', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule', "wminkowski"] def pairwise_distances(X, Y=None, metric="euclidean", n_jobs=1, **kwds): """ Compute the distance matrix from a vector array X and optional Y. This method takes either a vector array or a distance matrix, and returns a distance matrix. If the input is a vector array, the distances are computed. If the input is a distances matrix, it is returned instead. This method provides a safe way to take a distance matrix as input, while preserving compatibility with many other algorithms that take a vector array. If Y is given (default is None), then the returned matrix is the pairwise distance between the arrays from both X and Y. Valid values for metric are: - From scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2', 'manhattan']. These metrics support sparse matrix inputs. - From scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev', 'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule'] See the documentation for scipy.spatial.distance for details on these metrics. These metrics do not support sparse matrix inputs. Note that in the case of 'cityblock', 'cosine' and 'euclidean' (which are valid scipy.spatial.distance metrics), the scikit-learn implementation will be used, which is faster and has support for sparse matrices (except for 'cityblock'). For a verbose description of the metrics from scikit-learn, see the __doc__ of the sklearn.pairwise.distance_metrics function. Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array [n_samples_a, n_samples_a] if metric == "precomputed", or, \ [n_samples_a, n_features] otherwise Array of pairwise distances between samples, or a feature array. Y : array [n_samples_b, n_features], optional An optional second feature array. Only allowed if metric != "precomputed". metric : string, or callable The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by scipy.spatial.distance.pdist for its metric parameter, or a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. If metric is "precomputed", X is assumed to be a distance matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. n_jobs : int The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. 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. `**kwds` : optional keyword parameters Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples. Returns ------- D : array [n_samples_a, n_samples_a] or [n_samples_a, n_samples_b] A distance matrix D such that D_{i, j} is the distance between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then D_{i, j} is the distance between the ith array from X and the jth array from Y. """ if (metric not in _VALID_METRICS and not callable(metric) and metric != "precomputed"): raise ValueError("Unknown metric %s. " "Valid metrics are %s, or 'precomputed', or a " "callable" % (metric, _VALID_METRICS)) if metric == "precomputed": X, _ = check_pairwise_arrays(X, Y, precomputed=True) return X elif metric in PAIRWISE_DISTANCE_FUNCTIONS: func = PAIRWISE_DISTANCE_FUNCTIONS[metric] elif callable(metric): func = partial(_pairwise_callable, metric=metric, **kwds) else: if issparse(X) or issparse(Y): raise TypeError("scipy distance metrics do not" " support sparse matrices.") dtype = bool if metric in PAIRWISE_BOOLEAN_FUNCTIONS else None X, Y = check_pairwise_arrays(X, Y, dtype=dtype) if n_jobs == 1 and X is Y: return distance.squareform(distance.pdist(X, metric=metric, **kwds)) func = partial(distance.cdist, metric=metric, **kwds) return _parallel_pairwise(X, Y, func, n_jobs, **kwds) # These distances recquire boolean arrays, when using scipy.spatial.distance PAIRWISE_BOOLEAN_FUNCTIONS = [ 'dice', 'jaccard', 'kulsinski', 'matching', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', 'yule', ] # Helper functions - distance PAIRWISE_KERNEL_FUNCTIONS = { # If updating this dictionary, update the doc in both distance_metrics() # and also in pairwise_distances()! 'additive_chi2': additive_chi2_kernel, 'chi2': chi2_kernel, 'linear': linear_kernel, 'polynomial': polynomial_kernel, 'poly': polynomial_kernel, 'rbf': rbf_kernel, 'laplacian': laplacian_kernel, 'sigmoid': sigmoid_kernel, 'cosine': cosine_similarity, } def kernel_metrics(): """ Valid metrics for pairwise_kernels This function simply returns the valid pairwise distance metrics. It exists, however, to allow for a verbose description of the mapping for each of the valid strings. The valid distance metrics, and the function they map to, are: =============== ======================================== metric Function =============== ======================================== 'additive_chi2' sklearn.pairwise.additive_chi2_kernel 'chi2' sklearn.pairwise.chi2_kernel 'linear' sklearn.pairwise.linear_kernel 'poly' sklearn.pairwise.polynomial_kernel 'polynomial' sklearn.pairwise.polynomial_kernel 'rbf' sklearn.pairwise.rbf_kernel 'laplacian' sklearn.pairwise.laplacian_kernel 'sigmoid' sklearn.pairwise.sigmoid_kernel 'cosine' sklearn.pairwise.cosine_similarity =============== ======================================== Read more in the :ref:`User Guide <metrics>`. """ return PAIRWISE_KERNEL_FUNCTIONS KERNEL_PARAMS = { "additive_chi2": (), "chi2": (), "cosine": (), "exp_chi2": frozenset(["gamma"]), "linear": (), "poly": frozenset(["gamma", "degree", "coef0"]), "polynomial": frozenset(["gamma", "degree", "coef0"]), "rbf": frozenset(["gamma"]), "laplacian": frozenset(["gamma"]), "sigmoid": frozenset(["gamma", "coef0"]), } def pairwise_kernels(X, Y=None, metric="linear", filter_params=False, n_jobs=1, **kwds): """Compute the kernel between arrays X and optional array Y. This method takes either a vector array or a kernel matrix, and returns a kernel matrix. If the input is a vector array, the kernels are computed. If the input is a kernel matrix, it is returned instead. This method provides a safe way to take a kernel matrix as input, while preserving compatibility with many other algorithms that take a vector array. If Y is given (default is None), then the returned matrix is the pairwise kernel between the arrays from both X and Y. Valid values for metric are:: ['rbf', 'sigmoid', 'polynomial', 'poly', 'linear', 'cosine'] Read more in the :ref:`User Guide <metrics>`. Parameters ---------- X : array [n_samples_a, n_samples_a] if metric == "precomputed", or, \ [n_samples_a, n_features] otherwise Array of pairwise kernels between samples, or a feature array. Y : array [n_samples_b, n_features] A second feature array only if X has shape [n_samples_a, n_features]. metric : string, or callable The metric to use when calculating kernel between instances in a feature array. If metric is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If metric is "precomputed", X is assumed to be a kernel matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. n_jobs : int The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. 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. filter_params : boolean Whether to filter invalid parameters or not. `**kwds` : optional keyword parameters Any further parameters are passed directly to the kernel function. Returns ------- K : array [n_samples_a, n_samples_a] or [n_samples_a, n_samples_b] A kernel matrix K such that K_{i, j} is the kernel between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then K_{i, j} is the kernel between the ith array from X and the jth array from Y. Notes ----- If metric is 'precomputed', Y is ignored and X is returned. """ # import GPKernel locally to prevent circular imports from ..gaussian_process.kernels import Kernel as GPKernel if metric == "precomputed": X, _ = check_pairwise_arrays(X, Y, precomputed=True) return X elif isinstance(metric, GPKernel): func = metric.__call__ elif metric in PAIRWISE_KERNEL_FUNCTIONS: if filter_params: kwds = dict((k, kwds[k]) for k in kwds if k in KERNEL_PARAMS[metric]) func = PAIRWISE_KERNEL_FUNCTIONS[metric] elif callable(metric): func = partial(_pairwise_callable, metric=metric, **kwds) else: raise ValueError("Unknown kernel %r" % metric) return _parallel_pairwise(X, Y, func, n_jobs, **kwds)
bsd-3-clause
alxio/gmmreg
Python/_plotting.py
14
2435
#!/usr/bin/env python #coding=utf-8 ##==================================================== ## $Author$ ## $Date$ ## $Revision$ ##==================================================== from pylab import * from configobj import ConfigObj import matplotlib.pyplot as plt def display2Dpointset(A): fig = plt.figure() ax = fig.add_subplot(111) #ax.grid(True) ax.plot(A[:,0],A[:,1],'yo',markersize=8,mew=1) labels = plt.getp(plt.gca(), 'xticklabels') plt.setp(labels, color='k', fontweight='bold') labels = plt.getp(plt.gca(), 'yticklabels') plt.setp(labels, color='k', fontweight='bold') for i,x in enumerate(A): ax.annotate('%d'%(i+1), xy = x, xytext = x + 0) ax.set_axis_off() #fig.show() def display2Dpointsets(A, B, ax = None): """ display a pair of 2D point sets """ if not ax: fig = plt.figure() ax = fig.add_subplot(111) ax.plot(A[:,0],A[:,1],'yo',markersize=8,mew=1) ax.plot(B[:,0],B[:,1],'b+',markersize=8,mew=1) #pylab.setp(pylab.gca(), 'xlim', [-0.15,0.6]) labels = plt.getp(plt.gca(), 'xticklabels') plt.setp(labels, color='k', fontweight='bold') labels = plt.getp(plt.gca(), 'yticklabels') plt.setp(labels, color='k', fontweight='bold') def display3Dpointsets(A,B,ax): #ax.plot3d(A[:,0],A[:,1],A[:,2],'yo',markersize=10,mew=1) #ax.plot3d(B[:,0],B[:,1],B[:,2],'b+',markersize=10,mew=1) ax.scatter(A[:,0],A[:,1],A[:,2], c = 'y', marker = 'o') ax.scatter(B[:,0],B[:,1],B[:,2], c = 'b', marker = '+') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') from mpl_toolkits.mplot3d import Axes3D def displayABC(A,B,C): fig = plt.figure() dim = A.shape[1] if dim==2: ax = plt.subplot(121) display2Dpointsets(A, B, ax) ax = plt.subplot(122) display2Dpointsets(C, B, ax) if dim==3: plot1 = plt.subplot(1,2,1) ax = Axes3D(fig, rect = plot1.get_position()) display3Dpointsets(A,B,ax) plot2 = plt.subplot(1,2,2) ax = Axes3D(fig, rect = plot2.get_position()) display3Dpointsets(C,B,ax) plt.show() def display_pts(f_config): config = ConfigObj(f_config) file_section = config['FILES'] mf = file_section['model'] sf = file_section['scene'] tf = file_section['transformed_model'] m = np.loadtxt(mf) s = np.loadtxt(sf) t = np.loadtxt(tf) displayABC(m,s,t)
gpl-3.0
Haunter17/MIR_SU17
exp8/exp8f_none.py
1
7466
import numpy as np import tensorflow as tf import h5py from sklearn.preprocessing import OneHotEncoder import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import time import scipy.io # Functions for initializing neural nets parameters def weight_variable(shape, var_name): initial = tf.truncated_normal(shape, stddev=0.1, dtype=tf.float64) return tf.Variable(initial, name=var_name) def bias_variable(shape, var_name): initial = tf.constant(0.1, shape=shape, dtype=tf.float64) return tf.Variable(initial, name=var_name) def conv2d(x, W): return tf.nn.conv2d(x, W, [1, 1, 1, 1], 'VALID') def batch_nm(x, eps=1e-5): # batch normalization to have zero mean and unit variance mu, var = tf.nn.moments(x, [0]) return tf.nn.batch_normalization(x, mu, var, None, None, eps) # Download data from .mat file into numpy array print('==> Experiment 8f') filepath = '/scratch/ttanpras/exp8a_d7_1s.mat' print('==> Loading data from {}'.format(filepath)) f = h5py.File(filepath) data_train = np.array(f.get('trainingFeatures')) data_val = np.array(f.get('validationFeatures')) del f print('==> Data sizes:',data_train.shape, data_val.shape) # Transform labels into on-hot encoding form enc = OneHotEncoder(n_values = 71) ''' NN config parameters ''' sub_window_size = 32 num_features = 169*sub_window_size num_frames = 32 hidden_layer_size = 64 num_bits = 64 num_classes = 71 print("Number of features:", num_features) print("Number of songs:",num_classes) # Reshape input features X_train = np.reshape(data_train,(-1, num_features)) X_val = np.reshape(data_val,(-1, num_features)) print("Input sizes:", X_train.shape, X_val.shape) y_train = [] y_val = [] # Add Labels for label in range(num_classes): for sampleCount in range(X_train.shape[0]//num_classes): y_train.append([label]) for sampleCount in range(X_val.shape[0]//num_classes): y_val.append([label]) X_train = np.concatenate((X_train, y_train), axis=1) X_val = np.concatenate((X_val, y_val), axis=1) # Shuffle np.random.shuffle(X_train) np.random.shuffle(X_val) # Separate coefficients and labels y_train = X_train[:, -1].reshape(-1, 1) X_train = X_train[:, :-1] y_val = X_val[:, -1].reshape(-1, 1) X_val = X_val[:, :-1] print('==> Data sizes:',X_train.shape, y_train.shape,X_val.shape, y_val.shape) y_train = enc.fit_transform(y_train.copy()).astype(int).toarray() y_val = enc.fit_transform(y_val.copy()).astype(int).toarray() plotx = [] ploty_train = [] ploty_val = [] # Set-up NN layers x = tf.placeholder(tf.float64, [None, num_features]) W1 = weight_variable([num_features, hidden_layer_size], "W1") b1 = bias_variable([hidden_layer_size], "b1") OpW1 = tf.placeholder(tf.float64, [num_features, hidden_layer_size]) Opb1 = tf.placeholder(tf.float64, [hidden_layer_size]) # Hidden layer activation function: ReLU h1 = tf.nn.relu(tf.matmul(x, W1) + b1) W2 = weight_variable([hidden_layer_size, num_bits], "W2") b2 = bias_variable([num_bits], "b2") OpW2 = tf.placeholder(tf.float64, [hidden_layer_size, num_bits]) Opb2 = tf.placeholder(tf.float64, [num_bits]) # Pre-activation value for bit representation h = tf.matmul(h1, W2) + b2 h2 = tf.nn.relu(tf.matmul(h1, W2) + b2) W3 = weight_variable([num_bits, num_classes], "W3") b3 = bias_variable([num_classes], "b3") OpW3 = tf.placeholder(tf.float64, [num_bits, num_classes]) Opb3 = tf.placeholder(tf.float64, [num_classes]) # Softmax layer (Output), dtype = float64 y = tf.matmul(h2, W3) + b3 # NN desired value (labels) y_ = tf.placeholder(tf.float64, [None, num_classes]) # Loss function cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) sess = tf.InteractiveSession() correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float64)) sess.run(tf.initialize_all_variables()) # Training numTrainingVec = len(X_train) batchSize = 500 numEpochs = 1000 bestValErr = 10000 bestValEpoch = 0 startTime = time.time() for epoch in range(numEpochs): for i in range(0,numTrainingVec,batchSize): # Batch Data batchEndPoint = min(i+batchSize, numTrainingVec) trainBatchData = X_train[i:batchEndPoint] trainBatchLabel = y_train[i:batchEndPoint] train_step.run(feed_dict={x: trainBatchData, y_: trainBatchLabel}) # Print accuracy if epoch % 5 == 0 or epoch == numEpochs-1: plotx.append(epoch) train_error = cross_entropy.eval(feed_dict={x:trainBatchData, y_: trainBatchLabel}) train_acc = accuracy.eval(feed_dict={x:trainBatchData, y_: trainBatchLabel}) val_error = cross_entropy.eval(feed_dict={x:X_val, y_: y_val}) val_acc = accuracy.eval(feed_dict={x:X_val, y_: y_val}) ploty_train.append(train_error) ploty_val.append(val_error) print("epoch: %d, val error %g, train error %g"%(epoch, val_error, train_error)) if val_error < bestValErr: bestValErr = val_error bestValEpoch = epoch OpW1 = W1 Opb1 = b1 OpW2 = W2 Opb2 = b2 OpW3 = W3 Opb3 = b3 endTime = time.time() print("Elapse Time:", endTime - startTime) print("Best validation error: %g at epoch %d"%(bestValErr, bestValEpoch)) # Restore best model for early stopping W1 = OpW1 b1 = Opb1 W2 = OpW2 b2 = Opb2 W3 = OpW3 b3 = Opb3 print('==> Generating error plot...') errfig = plt.figure() trainErrPlot = errfig.add_subplot(111) trainErrPlot.set_xlabel('Number of Epochs') trainErrPlot.set_ylabel('Cross-Entropy Error') trainErrPlot.set_title('Error vs Number of Epochs') trainErrPlot.scatter(plotx, ploty_train) valErrPlot = errfig.add_subplot(111) valErrPlot.scatter(plotx, ploty_val) errfig.savefig('exp8f_none.png') ''' GENERATING REPRESENTATION OF NOISY FILES ''' namelist = ['orig','comp5','comp10','str5','str10','ampSat_(-15)','ampSat_(-10)','ampSat_(-5)', \ 'ampSat_(5)','ampSat_(10)','ampSat_(15)','pitchShift_(-1)','pitchShift_(-0.5)', \ 'pitchShift_(0.5)','pitchShift_(1)','rev_dkw','rev_gal','rev_shan0','rev_shan1', \ 'rev_gen','crowd-15','crowd-10','crowd-5','crowd0','crowd5','crowd10','crowd15', \ 'crowd100','rest-15','rest-10','rest-5','rest0','rest5','rest10','rest15', \ 'rest100','AWGN-15','AWGN-10','AWGN-5','AWGN0','AWGN5','AWGN10','AWGN15', 'AWGN100'] outdir = '/scratch/ttanpras/taylorswift_noisy_processed/' repDict = {} # Loop over each CQT files, not shuffled for count in range(len(namelist)): name = namelist[count] filename = outdir + name + '.mat' cqt = scipy.io.loadmat(filename)['Q'] cqt = np.transpose(np.array(cqt)) # Group into windows of 32 without overlapping # Discard any leftover frames num_windows = cqt.shape[0] // 32 cqt = cqt[:32*num_windows] X = np.reshape(cqt,(num_windows, num_features)) # Feed window through model (Only 1 layer of weight w/o non-linearity) rep = h.eval(feed_dict={x:X}) # Put the output representation into a dictionary repDict['n'+str(count)] = rep scipy.io.savemat('exp8f_none_repNon.mat',repDict)
mit
johnarban/arban
myhelpers.py
1
1202
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.colors as colors def clean_color(color, reverse=False): if isinstance(color, str): if color[-2:] == '_r': return color[:-2], True elif reverse is True: return color, True else: return color, False else: return color, reverse def color_cmap(c, alpha=1, to_white=True, reverse=False): if to_white: end = (1, 1, 1, alpha) else: end = (0, 0, 0, alpha) color, reverse = clean_color(c, reverse=reverse) cmap = mpl.colors.LinearSegmentedColormap.from_list( "density_cmap", [color, end]) if reverse: return cmap.reversed() else: return cmap def contour_level_colors(cmap, levels, vmin=None, vmax=None): vmin = vmin or 0 vmax = vmax or max(levels) norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) #offset = np.diff(levels)[0] * .5 #colors = mpl.cm.get_cmap(cmap)(norm(levels-offset)) levels = np.r_[0, levels] center_levels = 0.5 * (levels[1:] + levels[:-1]) return mpl.cm.get_cmap(cmap)(norm(center_levels))
mit
ptonner/GPy
GPy/models/gplvm.py
8
3049
# Copyright (c) 2012-2014, GPy authors (see AUTHORS.txt). # Licensed under the BSD 3-clause license (see LICENSE.txt) import numpy as np from .. import kern from ..core import GP, Param from ..likelihoods import Gaussian from .. import util class GPLVM(GP): """ Gaussian Process Latent Variable Model """ def __init__(self, Y, input_dim, init='PCA', X=None, kernel=None, name="gplvm"): """ :param Y: observed data :type Y: np.ndarray :param input_dim: latent dimensionality :type input_dim: int :param init: initialisation method for the latent space :type init: 'PCA'|'random' """ if X is None: from ..util.initialization import initialize_latent X, fracs = initialize_latent(init, input_dim, Y) else: fracs = np.ones(input_dim) if kernel is None: kernel = kern.RBF(input_dim, lengthscale=fracs, ARD=input_dim > 1) + kern.Bias(input_dim, np.exp(-2)) likelihood = Gaussian() super(GPLVM, self).__init__(X, Y, kernel, likelihood, name='GPLVM') self.X = Param('latent_mean', X) self.link_parameter(self.X, index=0) def parameters_changed(self): super(GPLVM, self).parameters_changed() self.X.gradient = self.kern.gradients_X(self.grad_dict['dL_dK'], self.X, None) #def jacobian(self,X): # J = np.zeros((X.shape[0],X.shape[1],self.output_dim)) # for i in range(self.output_dim): # J[:,:,i] = self.kern.gradients_X(self.posterior.woodbury_vector[:,i:i+1], X, self.X) # return J #def magnification(self,X): # target=np.zeros(X.shape[0]) # #J = np.zeros((X.shape[0],X.shape[1],self.output_dim)) ## J = self.jacobian(X) # for i in range(X.shape[0]): # target[i]=np.sqrt(np.linalg.det(np.dot(J[i,:,:],np.transpose(J[i,:,:])))) # return target def plot(self): assert self.Y.shape[1] == 2, "too high dimensional to plot. Try plot_latent" from matplotlib import pyplot as plt plt.scatter(self.Y[:, 0], self.Y[:, 1], 40, self.X[:, 0].copy(), linewidth=0, cmap=plt.cm.jet) Xnew = np.linspace(self.X.min(), self.X.max(), 200)[:, None] mu, _ = self.predict(Xnew) plt.plot(mu[:, 0], mu[:, 1], 'k', linewidth=1.5) def plot_latent(self, labels=None, which_indices=None, resolution=50, ax=None, marker='o', s=40, fignum=None, legend=True, plot_limits=None, aspect='auto', updates=False, **kwargs): import sys assert "matplotlib" in sys.modules, "matplotlib package has not been imported." from ..plotting.matplot_dep import dim_reduction_plots return dim_reduction_plots.plot_latent(self, labels, which_indices, resolution, ax, marker, s, fignum, False, legend, plot_limits, aspect, updates, **kwargs)
bsd-3-clause
victor-prado/broker-manager
environment/lib/python3.5/site-packages/pandas/core/generic.py
7
209040
# pylint: disable=W0231,E1101 import collections import warnings import operator import weakref import gc import numpy as np import pandas.lib as lib import pandas as pd from pandas.types.common import (_coerce_to_dtype, _ensure_int64, needs_i8_conversion, is_scalar, is_integer, is_bool, is_bool_dtype, is_numeric_dtype, is_datetime64_dtype, is_timedelta64_dtype, is_datetime64tz_dtype, is_list_like, is_dict_like, is_re_compilable) from pandas.types.cast import _maybe_promote, _maybe_upcast_putmask from pandas.types.missing import isnull, notnull from pandas.types.generic import ABCSeries, ABCPanel from pandas.core.common import (_values_from_object, _maybe_box_datetimelike, SettingWithCopyError, SettingWithCopyWarning, AbstractMethodError) from pandas.core.base import PandasObject from pandas.core.index import (Index, MultiIndex, _ensure_index, InvalidIndexError) import pandas.core.indexing as indexing from pandas.tseries.index import DatetimeIndex from pandas.tseries.period import PeriodIndex, Period from pandas.core.internals import BlockManager import pandas.core.algorithms as algos import pandas.core.common as com import pandas.core.missing as missing from pandas.formats.printing import pprint_thing from pandas.formats.format import format_percentiles from pandas.tseries.frequencies import to_offset from pandas import compat from pandas.compat.numpy import function as nv from pandas.compat import (map, zip, lrange, string_types, isidentifier, set_function_name) import pandas.core.nanops as nanops from pandas.util.decorators import Appender, Substitution, deprecate_kwarg from pandas.core import config # goal is to be able to define the docs close to function, while still being # able to share _shared_docs = dict() _shared_doc_kwargs = dict( axes='keywords for axes', klass='NDFrame', axes_single_arg='int or labels for object', args_transpose='axes to permute (int or label for object)', optional_by=""" by : str or list of str Name or list of names which refer to the axis items.""") def _single_replace(self, to_replace, method, inplace, limit): if self.ndim != 1: raise TypeError('cannot replace {0} with method {1} on a {2}' .format(to_replace, method, type(self).__name__)) orig_dtype = self.dtype result = self if inplace else self.copy() fill_f = missing.get_fill_func(method) mask = missing.mask_missing(result.values, to_replace) values = fill_f(result.values, limit=limit, mask=mask) if values.dtype == orig_dtype and inplace: return result = pd.Series(values, index=self.index, dtype=self.dtype).__finalize__(self) if inplace: self._update_inplace(result._data) return return result class NDFrame(PandasObject): """ N-dimensional analogue of DataFrame. Store multi-dimensional in a size-mutable, labeled data structure Parameters ---------- data : BlockManager axes : list copy : boolean, default False """ _internal_names = ['_data', '_cacher', '_item_cache', '_cache', 'is_copy', '_subtyp', '_name', '_index', '_default_kind', '_default_fill_value', '_metadata', '__array_struct__', '__array_interface__'] _internal_names_set = set(_internal_names) _accessors = frozenset([]) _metadata = [] is_copy = None def __init__(self, data, axes=None, copy=False, dtype=None, fastpath=False): if not fastpath: if dtype is not None: data = data.astype(dtype) elif copy: data = data.copy() if axes is not None: for i, ax in enumerate(axes): data = data.reindex_axis(ax, axis=i) object.__setattr__(self, 'is_copy', None) object.__setattr__(self, '_data', data) object.__setattr__(self, '_item_cache', {}) def _validate_dtype(self, dtype): """ validate the passed dtype """ if dtype is not None: dtype = _coerce_to_dtype(dtype) # a compound dtype if dtype.kind == 'V': raise NotImplementedError("compound dtypes are not implemented" "in the {0} constructor" .format(self.__class__.__name__)) return dtype def _init_mgr(self, mgr, axes=None, dtype=None, copy=False): """ passed a manager and a axes dict """ for a, axe in axes.items(): if axe is not None: mgr = mgr.reindex_axis(axe, axis=self._get_block_manager_axis(a), copy=False) # make a copy if explicitly requested if copy: mgr = mgr.copy() if dtype is not None: # avoid further copies if we can if len(mgr.blocks) > 1 or mgr.blocks[0].values.dtype != dtype: mgr = mgr.astype(dtype=dtype) return mgr # ---------------------------------------------------------------------- # Construction @property def _constructor(self): """Used when a manipulation result has the same dimensions as the original. """ raise AbstractMethodError(self) def __unicode__(self): # unicode representation based upon iterating over self # (since, by definition, `PandasContainers` are iterable) prepr = '[%s]' % ','.join(map(pprint_thing, self)) return '%s(%s)' % (self.__class__.__name__, prepr) def _dir_additions(self): """ add the string-like attributes from the info_axis """ return set([c for c in self._info_axis if isinstance(c, string_types) and isidentifier(c)]) @property def _constructor_sliced(self): """Used when a manipulation result has one lower dimension(s) as the original, such as DataFrame single columns slicing. """ raise AbstractMethodError(self) @property def _constructor_expanddim(self): """Used when a manipulation result has one higher dimension as the original, such as Series.to_frame() and DataFrame.to_panel() """ raise NotImplementedError # ---------------------------------------------------------------------- # Axis @classmethod def _setup_axes(cls, axes, info_axis=None, stat_axis=None, aliases=None, slicers=None, axes_are_reversed=False, build_axes=True, ns=None): """Provide axes setup for the major PandasObjects. Parameters ---------- axes : the names of the axes in order (lowest to highest) info_axis_num : the axis of the selector dimension (int) stat_axis_num : the number of axis for the default stats (int) aliases : other names for a single axis (dict) slicers : how axes slice to others (dict) axes_are_reversed : boolean whether to treat passed axes as reversed (DataFrame) build_axes : setup the axis properties (default True) """ cls._AXIS_ORDERS = axes cls._AXIS_NUMBERS = dict((a, i) for i, a in enumerate(axes)) cls._AXIS_LEN = len(axes) cls._AXIS_ALIASES = aliases or dict() cls._AXIS_IALIASES = dict((v, k) for k, v in cls._AXIS_ALIASES.items()) cls._AXIS_NAMES = dict(enumerate(axes)) cls._AXIS_SLICEMAP = slicers or None cls._AXIS_REVERSED = axes_are_reversed # typ setattr(cls, '_typ', cls.__name__.lower()) # indexing support cls._ix = None if info_axis is not None: cls._info_axis_number = info_axis cls._info_axis_name = axes[info_axis] if stat_axis is not None: cls._stat_axis_number = stat_axis cls._stat_axis_name = axes[stat_axis] # setup the actual axis if build_axes: def set_axis(a, i): setattr(cls, a, lib.AxisProperty(i)) cls._internal_names_set.add(a) if axes_are_reversed: m = cls._AXIS_LEN - 1 for i, a in cls._AXIS_NAMES.items(): set_axis(a, m - i) else: for i, a in cls._AXIS_NAMES.items(): set_axis(a, i) # addtl parms if isinstance(ns, dict): for k, v in ns.items(): setattr(cls, k, v) def _construct_axes_dict(self, axes=None, **kwargs): """Return an axes dictionary for myself.""" d = dict([(a, self._get_axis(a)) for a in (axes or self._AXIS_ORDERS)]) d.update(kwargs) return d @staticmethod def _construct_axes_dict_from(self, axes, **kwargs): """Return an axes dictionary for the passed axes.""" d = dict([(a, ax) for a, ax in zip(self._AXIS_ORDERS, axes)]) d.update(kwargs) return d def _construct_axes_dict_for_slice(self, axes=None, **kwargs): """Return an axes dictionary for myself.""" d = dict([(self._AXIS_SLICEMAP[a], self._get_axis(a)) for a in (axes or self._AXIS_ORDERS)]) d.update(kwargs) return d def _construct_axes_from_arguments(self, args, kwargs, require_all=False): """Construct and returns axes if supplied in args/kwargs. If require_all, raise if all axis arguments are not supplied return a tuple of (axes, kwargs). """ # construct the args args = list(args) for a in self._AXIS_ORDERS: # if we have an alias for this axis alias = self._AXIS_IALIASES.get(a) if alias is not None: if a in kwargs: if alias in kwargs: raise TypeError("arguments are mutually exclusive " "for [%s,%s]" % (a, alias)) continue if alias in kwargs: kwargs[a] = kwargs.pop(alias) continue # look for a argument by position if a not in kwargs: try: kwargs[a] = args.pop(0) except IndexError: if require_all: raise TypeError("not enough/duplicate arguments " "specified!") axes = dict([(a, kwargs.pop(a, None)) for a in self._AXIS_ORDERS]) return axes, kwargs @classmethod def _from_axes(cls, data, axes, **kwargs): # for construction from BlockManager if isinstance(data, BlockManager): return cls(data, **kwargs) else: if cls._AXIS_REVERSED: axes = axes[::-1] d = cls._construct_axes_dict_from(cls, axes, copy=False) d.update(kwargs) return cls(data, **d) def _get_axis_number(self, axis): axis = self._AXIS_ALIASES.get(axis, axis) if is_integer(axis): if axis in self._AXIS_NAMES: return axis else: try: return self._AXIS_NUMBERS[axis] except: pass raise ValueError('No axis named {0} for object type {1}' .format(axis, type(self))) def _get_axis_name(self, axis): axis = self._AXIS_ALIASES.get(axis, axis) if isinstance(axis, string_types): if axis in self._AXIS_NUMBERS: return axis else: try: return self._AXIS_NAMES[axis] except: pass raise ValueError('No axis named {0} for object type {1}' .format(axis, type(self))) def _get_axis(self, axis): name = self._get_axis_name(axis) return getattr(self, name) def _get_block_manager_axis(self, axis): """Map the axis to the block_manager axis.""" axis = self._get_axis_number(axis) if self._AXIS_REVERSED: m = self._AXIS_LEN - 1 return m - axis return axis def _get_axis_resolvers(self, axis): # index or columns axis_index = getattr(self, axis) d = dict() prefix = axis[0] for i, name in enumerate(axis_index.names): if name is not None: key = level = name else: # prefix with 'i' or 'c' depending on the input axis # e.g., you must do ilevel_0 for the 0th level of an unnamed # multiiindex key = '{prefix}level_{i}'.format(prefix=prefix, i=i) level = i level_values = axis_index.get_level_values(level) s = level_values.to_series() s.index = axis_index d[key] = s # put the index/columns itself in the dict if isinstance(axis_index, MultiIndex): dindex = axis_index else: dindex = axis_index.to_series() d[axis] = dindex return d def _get_index_resolvers(self): d = {} for axis_name in self._AXIS_ORDERS: d.update(self._get_axis_resolvers(axis_name)) return d @property def _info_axis(self): return getattr(self, self._info_axis_name) @property def _stat_axis(self): return getattr(self, self._stat_axis_name) @property def shape(self): """Return a tuple of axis dimensions""" return tuple(len(self._get_axis(a)) for a in self._AXIS_ORDERS) @property def axes(self): """Return index label(s) of the internal NDFrame""" # we do it this way because if we have reversed axes, then # the block manager shows then reversed return [self._get_axis(a) for a in self._AXIS_ORDERS] @property def ndim(self): """Number of axes / array dimensions""" return self._data.ndim @property def size(self): """number of elements in the NDFrame""" return np.prod(self.shape) def _expand_axes(self, key): new_axes = [] for k, ax in zip(key, self.axes): if k not in ax: if type(k) != ax.dtype.type: ax = ax.astype('O') new_axes.append(ax.insert(len(ax), k)) else: new_axes.append(ax) return new_axes def set_axis(self, axis, labels): """ public verson of axis assignment """ setattr(self, self._get_axis_name(axis), labels) def _set_axis(self, axis, labels): self._data.set_axis(axis, labels) self._clear_item_cache() _shared_docs['transpose'] = """ Permute the dimensions of the %(klass)s Parameters ---------- args : %(args_transpose)s copy : boolean, default False Make a copy of the underlying data. Mixed-dtype data will always result in a copy Examples -------- >>> p.transpose(2, 0, 1) >>> p.transpose(2, 0, 1, copy=True) Returns ------- y : same as input """ @Appender(_shared_docs['transpose'] % _shared_doc_kwargs) def transpose(self, *args, **kwargs): # construct the args axes, kwargs = self._construct_axes_from_arguments(args, kwargs, require_all=True) axes_names = tuple([self._get_axis_name(axes[a]) for a in self._AXIS_ORDERS]) axes_numbers = tuple([self._get_axis_number(axes[a]) for a in self._AXIS_ORDERS]) # we must have unique axes if len(axes) != len(set(axes)): raise ValueError('Must specify %s unique axes' % self._AXIS_LEN) new_axes = self._construct_axes_dict_from(self, [self._get_axis(x) for x in axes_names]) new_values = self.values.transpose(axes_numbers) if kwargs.pop('copy', None) or (len(args) and args[-1]): new_values = new_values.copy() nv.validate_transpose_for_generic(self, kwargs) return self._constructor(new_values, **new_axes).__finalize__(self) def swapaxes(self, axis1, axis2, copy=True): """ Interchange axes and swap values axes appropriately Returns ------- y : same as input """ i = self._get_axis_number(axis1) j = self._get_axis_number(axis2) if i == j: if copy: return self.copy() return self mapping = {i: j, j: i} new_axes = (self._get_axis(mapping.get(k, k)) for k in range(self._AXIS_LEN)) new_values = self.values.swapaxes(i, j) if copy: new_values = new_values.copy() return self._constructor(new_values, *new_axes).__finalize__(self) def pop(self, item): """ Return item and drop from frame. Raise KeyError if not found. """ result = self[item] del self[item] try: result._reset_cacher() except AttributeError: pass return result def squeeze(self, **kwargs): """Squeeze length 1 dimensions.""" nv.validate_squeeze(tuple(), kwargs) try: return self.iloc[tuple([0 if len(a) == 1 else slice(None) for a in self.axes])] except: return self def swaplevel(self, i=-2, j=-1, axis=0): """ Swap levels i and j in a MultiIndex on a particular axis Parameters ---------- i, j : int, string (can be mixed) Level of index to be swapped. Can pass level name as string. Returns ------- swapped : type of caller (new object) .. versionchanged:: 0.18.1 The indexes ``i`` and ``j`` are now optional, and default to the two innermost levels of the index. """ axis = self._get_axis_number(axis) result = self.copy() labels = result._data.axes[axis] result._data.set_axis(axis, labels.swaplevel(i, j)) return result # ---------------------------------------------------------------------- # Rename # TODO: define separate funcs for DataFrame, Series and Panel so you can # get completion on keyword arguments. _shared_docs['rename'] = """ Alter axes input function or functions. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don't throw an error. Alternatively, change ``Series.name`` with a scalar value (Series only). Parameters ---------- %(axes)s : scalar, list-like, dict-like or function, optional Scalar or list-like will alter the ``Series.name`` attribute, and raise on DataFrame or Panel. dict-like or functions are transformations to apply to that axis' values copy : boolean, default True Also copy underlying data inplace : boolean, default False Whether to return a new %(klass)s. If True then value of copy is ignored. Returns ------- renamed : %(klass)s (new object) See Also -------- pandas.NDFrame.rename_axis Examples -------- >>> s = pd.Series([1, 2, 3]) >>> s 0 1 1 2 2 3 dtype: int64 >>> s.rename("my_name") # scalar, changes Series.name 0 1 1 2 2 3 Name: my_name, dtype: int64 >>> s.rename(lambda x: x ** 2) # function, changes labels 0 1 1 2 4 3 dtype: int64 >>> s.rename({1: 3, 2: 5}) # mapping, changes labels 0 1 3 2 5 3 dtype: int64 >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.rename(2) ... TypeError: 'int' object is not callable >>> df.rename(index=str, columns={"A": "a", "B": "c"}) a c 0 1 4 1 2 5 2 3 6 >>> df.rename(index=str, columns={"A": "a", "C": "c"}) a B 0 1 4 1 2 5 2 3 6 """ @Appender(_shared_docs['rename'] % dict(axes='axes keywords for this' ' object', klass='NDFrame')) def rename(self, *args, **kwargs): axes, kwargs = self._construct_axes_from_arguments(args, kwargs) copy = kwargs.pop('copy', True) inplace = kwargs.pop('inplace', False) if kwargs: raise TypeError('rename() got an unexpected keyword ' 'argument "{0}"'.format(list(kwargs.keys())[0])) if com._count_not_none(*axes.values()) == 0: raise TypeError('must pass an index to rename') # renamer function if passed a dict def _get_rename_function(mapper): if isinstance(mapper, (dict, ABCSeries)): def f(x): if x in mapper: return mapper[x] else: return x else: f = mapper return f self._consolidate_inplace() result = self if inplace else self.copy(deep=copy) # start in the axis order to eliminate too many copies for axis in lrange(self._AXIS_LEN): v = axes.get(self._AXIS_NAMES[axis]) if v is None: continue f = _get_rename_function(v) baxis = self._get_block_manager_axis(axis) result._data = result._data.rename_axis(f, axis=baxis, copy=copy) result._clear_item_cache() if inplace: self._update_inplace(result._data) else: return result.__finalize__(self) rename.__doc__ = _shared_docs['rename'] def rename_axis(self, mapper, axis=0, copy=True, inplace=False): """ Alter index and / or columns using input function or functions. A scaler or list-like for ``mapper`` will alter the ``Index.name`` or ``MultiIndex.names`` attribute. A function or dict for ``mapper`` will alter the labels. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Parameters ---------- mapper : scalar, list-like, dict-like or function, optional axis : int or string, default 0 copy : boolean, default True Also copy underlying data inplace : boolean, default False Returns ------- renamed : type of caller See Also -------- pandas.NDFrame.rename pandas.Index.rename Examples -------- >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.rename_axis("foo") # scalar, alters df.index.name A B foo 0 1 4 1 2 5 2 3 6 >>> df.rename_axis(lambda x: 2 * x) # function: alters labels A B 0 1 4 2 2 5 4 3 6 >>> df.rename_axis({"A": "ehh", "C": "see"}, axis="columns") # mapping ehh B 0 1 4 1 2 5 2 3 6 """ non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not is_dict_like(mapper)) if non_mapper: return self._set_axis_name(mapper, axis=axis) else: axis = self._get_axis_name(axis) d = {'copy': copy, 'inplace': inplace} d[axis] = mapper return self.rename(**d) def _set_axis_name(self, name, axis=0): """ Alter the name or names of the axis, returning self. Parameters ---------- name : str or list of str Name for the Index, or list of names for the MultiIndex axis : int or str 0 or 'index' for the index; 1 or 'columns' for the columns Returns ------- renamed : type of caller See Also -------- pandas.DataFrame.rename pandas.Series.rename pandas.Index.rename Examples -------- >>> df._set_axis_name("foo") A foo 0 1 1 2 2 3 >>> df.index = pd.MultiIndex.from_product([['A'], ['a', 'b', 'c']]) >>> df._set_axis_name(["bar", "baz"]) A bar baz A a 1 b 2 c 3 """ axis = self._get_axis_number(axis) idx = self._get_axis(axis).set_names(name) renamed = self.copy(deep=True) renamed.set_axis(axis, idx) return renamed # ---------------------------------------------------------------------- # Comparisons def _indexed_same(self, other): return all([self._get_axis(a).equals(other._get_axis(a)) for a in self._AXIS_ORDERS]) def __neg__(self): values = _values_from_object(self) if values.dtype == np.bool_: arr = operator.inv(values) else: arr = operator.neg(values) return self.__array_wrap__(arr) def __invert__(self): try: arr = operator.inv(_values_from_object(self)) return self.__array_wrap__(arr) except: # inv fails with 0 len if not np.prod(self.shape): return self raise def equals(self, other): """ Determines if two NDFrame objects contain the same elements. NaNs in the same location are considered equal. """ if not isinstance(other, self._constructor): return False return self._data.equals(other._data) # ---------------------------------------------------------------------- # Iteration def __hash__(self): raise TypeError('{0!r} objects are mutable, thus they cannot be' ' hashed'.format(self.__class__.__name__)) def __iter__(self): """Iterate over infor axis""" return iter(self._info_axis) # can we get a better explanation of this? def keys(self): """Get the 'info axis' (see Indexing for more) This is index for Series, columns for DataFrame and major_axis for Panel. """ return self._info_axis def iteritems(self): """Iterate over (label, values) on info axis This is index for Series, columns for DataFrame, major_axis for Panel, and so on. """ for h in self._info_axis: yield h, self[h] # originally used to get around 2to3's changes to iteritems. # Now unnecessary. Sidenote: don't want to deprecate this for a while, # otherwise libraries that use 2to3 will have issues. def iterkv(self, *args, **kwargs): "iteritems alias used to get around 2to3. Deprecated" warnings.warn("iterkv is deprecated and will be removed in a future " "release, use ``iteritems`` instead.", FutureWarning, stacklevel=2) return self.iteritems(*args, **kwargs) def __len__(self): """Returns length of info axis""" return len(self._info_axis) def __contains__(self, key): """True if the key is in the info axis""" return key in self._info_axis @property def empty(self): """True if NDFrame is entirely empty [no items], meaning any of the axes are of length 0. Notes ----- If NDFrame contains only NaNs, it is still not considered empty. See the example below. Examples -------- An example of an actual empty DataFrame. Notice the index is empty: >>> df_empty = pd.DataFrame({'A' : []}) >>> df_empty Empty DataFrame Columns: [A] Index: [] >>> df_empty.empty True If we only have NaNs in our DataFrame, it is not considered empty! We will need to drop the NaNs to make the DataFrame empty: >>> df = pd.DataFrame({'A' : [np.nan]}) >>> df A 0 NaN >>> df.empty False >>> df.dropna().empty True See also -------- pandas.Series.dropna pandas.DataFrame.dropna """ return not all(len(self._get_axis(a)) > 0 for a in self._AXIS_ORDERS) def __nonzero__(self): raise ValueError("The truth value of a {0} is ambiguous. " "Use a.empty, a.bool(), a.item(), a.any() or a.all()." .format(self.__class__.__name__)) __bool__ = __nonzero__ def bool(self): """Return the bool of a single element PandasObject. This must be a boolean scalar value, either True or False. Raise a ValueError if the PandasObject does not have exactly 1 element, or that element is not boolean """ v = self.squeeze() if isinstance(v, (bool, np.bool_)): return bool(v) elif is_scalar(v): raise ValueError("bool cannot act on a non-boolean single element " "{0}".format(self.__class__.__name__)) self.__nonzero__() def __abs__(self): return self.abs() def __round__(self, decimals=0): return self.round(decimals) # ---------------------------------------------------------------------- # Array Interface def __array__(self, dtype=None): return _values_from_object(self) def __array_wrap__(self, result, context=None): d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False) return self._constructor(result, **d).__finalize__(self) # ideally we would define this to avoid the getattr checks, but # is slower # @property # def __array_interface__(self): # """ provide numpy array interface method """ # values = self.values # return dict(typestr=values.dtype.str,shape=values.shape,data=values) def to_dense(self): """Return dense representation of NDFrame (as opposed to sparse)""" # compat return self # ---------------------------------------------------------------------- # Picklability def __getstate__(self): meta = dict((k, getattr(self, k, None)) for k in self._metadata) return dict(_data=self._data, _typ=self._typ, _metadata=self._metadata, **meta) def __setstate__(self, state): if isinstance(state, BlockManager): self._data = state elif isinstance(state, dict): typ = state.get('_typ') if typ is not None: # set in the order of internal names # to avoid definitional recursion # e.g. say fill_value needing _data to be # defined meta = set(self._internal_names + self._metadata) for k in list(meta): if k in state: v = state[k] object.__setattr__(self, k, v) for k, v in state.items(): if k not in meta: object.__setattr__(self, k, v) else: self._unpickle_series_compat(state) elif isinstance(state[0], dict): if len(state) == 5: self._unpickle_sparse_frame_compat(state) else: self._unpickle_frame_compat(state) elif len(state) == 4: self._unpickle_panel_compat(state) elif len(state) == 2: self._unpickle_series_compat(state) else: # pragma: no cover # old pickling format, for compatibility self._unpickle_matrix_compat(state) self._item_cache = {} # ---------------------------------------------------------------------- # IO # ---------------------------------------------------------------------- # I/O Methods def to_json(self, path_or_buf=None, orient=None, date_format='epoch', double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False): """ Convert the object to a JSON string. Note NaN's and None will be converted to null and datetime objects will be converted to UNIX timestamps. Parameters ---------- path_or_buf : the path or buffer to write the result string if this is None, return a StringIO of the converted string orient : string * Series - default is 'index' - allowed values are: {'split','records','index'} * DataFrame - default is 'columns' - allowed values are: {'split','records','index','columns','values'} * The format of the JSON string - split : dict like {index -> [index], columns -> [columns], data -> [values]} - records : list like [{column -> value}, ... , {column -> value}] - index : dict like {index -> {column -> value}} - columns : dict like {column -> {index -> value}} - values : just the values array date_format : {'epoch', 'iso'} Type of date conversion. `epoch` = epoch milliseconds, `iso`` = ISO8601, default is epoch. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : string, default 'ms' (milliseconds) The time unit to encode to, governs timestamp and ISO8601 precision. One of 's', 'ms', 'us', 'ns' for second, millisecond, microsecond, and nanosecond respectively. default_handler : callable, default None Handler to call if object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serialisable object. lines : boolean, defalut False If 'orient' is 'records' write out line delimited json format. Will throw ValueError if incorrect 'orient' since others are not list like. .. versionadded:: 0.19.0 Returns ------- same type as input object with filtered info axis """ from pandas.io import json return json.to_json(path_or_buf=path_or_buf, obj=self, orient=orient, date_format=date_format, double_precision=double_precision, force_ascii=force_ascii, date_unit=date_unit, default_handler=default_handler, lines=lines) def to_hdf(self, path_or_buf, key, **kwargs): """Write the contained data to an HDF5 file using HDFStore. Parameters ---------- path_or_buf : the path (string) or HDFStore object key : string indentifier for the group in the store mode : optional, {'a', 'w', 'r+'}, default 'a' ``'w'`` Write; a new file is created (an existing file with the same name would be deleted). ``'a'`` Append; an existing file is opened for reading and writing, and if the file does not exist it is created. ``'r+'`` It is similar to ``'a'``, but the file must already exist. format : 'fixed(f)|table(t)', default is 'fixed' fixed(f) : Fixed format Fast writing/reading. Not-appendable, nor searchable table(t) : Table format Write as a PyTables Table structure which may perform worse but allow more flexible operations like searching / selecting subsets of the data append : boolean, default False For Table formats, append the input data to the existing data_columns : list of columns, or True, default None List of columns to create as indexed data columns for on-disk queries, or True to use all columns. By default only the axes of the object are indexed. See `here <http://pandas.pydata.org/pandas-docs/stable/io.html#query-via-data-columns>`__. Applicable only to format='table'. complevel : int, 1-9, default 0 If a complib is specified compression will be applied where possible complib : {'zlib', 'bzip2', 'lzo', 'blosc', None}, default None If complevel is > 0 apply compression to objects written in the store wherever possible fletcher32 : bool, default False If applying compression use the fletcher32 checksum dropna : boolean, default False. If true, ALL nan rows will not be written to store. """ from pandas.io import pytables return pytables.to_hdf(path_or_buf, key, self, **kwargs) def to_msgpack(self, path_or_buf=None, encoding='utf-8', **kwargs): """ msgpack (serialize) object to input file path THIS IS AN EXPERIMENTAL LIBRARY and the storage format may not be stable until a future release. Parameters ---------- path : string File path, buffer-like, or None if None, return generated string append : boolean whether to append to an existing msgpack (default is False) compress : type of compressor (zlib or blosc), default to None (no compression) """ from pandas.io import packers return packers.to_msgpack(path_or_buf, self, encoding=encoding, **kwargs) def to_sql(self, name, con, flavor=None, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None): """ Write records stored in a DataFrame to a SQL database. Parameters ---------- name : string Name of SQL table con : SQLAlchemy engine or DBAPI2 connection (legacy mode) Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported. flavor : 'sqlite', default None DEPRECATED: this parameter will be removed in a future version, as 'sqlite' is the only supported option if SQLAlchemy is not installed. schema : string, default None Specify the schema (if database flavor supports this). If None, use default schema. if_exists : {'fail', 'replace', 'append'}, default 'fail' - fail: If table exists, do nothing. - replace: If table exists, drop it, recreate it, and insert data. - append: If table exists, insert data. Create if does not exist. index : boolean, default True Write DataFrame index as a column. index_label : string or sequence, default None Column label for index column(s). If None is given (default) and `index` is True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex. chunksize : int, default None If not None, then rows will be written in batches of this size at a time. If None, all rows will be written at once. dtype : dict of column name to SQL type, default None Optional specifying the datatype for columns. The SQL type should be a SQLAlchemy type, or a string for sqlite3 fallback connection. """ from pandas.io import sql sql.to_sql(self, name, con, flavor=flavor, schema=schema, if_exists=if_exists, index=index, index_label=index_label, chunksize=chunksize, dtype=dtype) def to_pickle(self, path): """ Pickle (serialize) object to input file path. Parameters ---------- path : string File path """ from pandas.io.pickle import to_pickle return to_pickle(self, path) def to_clipboard(self, excel=None, sep=None, **kwargs): """ Attempt to write text representation of object to the system clipboard This can be pasted into Excel, for example. Parameters ---------- excel : boolean, defaults to True if True, use the provided separator, writing in a csv format for allowing easy pasting into excel. if False, write a string representation of the object to the clipboard sep : optional, defaults to tab other keywords are passed to to_csv Notes ----- Requirements for your platform - Linux: xclip, or xsel (with gtk or PyQt4 modules) - Windows: none - OS X: none """ from pandas.io import clipboard clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs) def to_xarray(self): """ Return an xarray object from the pandas object. Returns ------- a DataArray for a Series a Dataset for a DataFrame a DataArray for higher dims Examples -------- >>> df = pd.DataFrame({'A' : [1, 1, 2], 'B' : ['foo', 'bar', 'foo'], 'C' : np.arange(4.,7)}) >>> df A B C 0 1 foo 4.0 1 1 bar 5.0 2 2 foo 6.0 >>> df.to_xarray() <xarray.Dataset> Dimensions: (index: 3) Coordinates: * index (index) int64 0 1 2 Data variables: A (index) int64 1 1 2 B (index) object 'foo' 'bar' 'foo' C (index) float64 4.0 5.0 6.0 >>> df = pd.DataFrame({'A' : [1, 1, 2], 'B' : ['foo', 'bar', 'foo'], 'C' : np.arange(4.,7)} ).set_index(['B','A']) >>> df C B A foo 1 4.0 bar 1 5.0 foo 2 6.0 >>> df.to_xarray() <xarray.Dataset> Dimensions: (A: 2, B: 2) Coordinates: * B (B) object 'bar' 'foo' * A (A) int64 1 2 Data variables: C (B, A) float64 5.0 nan 4.0 6.0 >>> p = pd.Panel(np.arange(24).reshape(4,3,2), items=list('ABCD'), major_axis=pd.date_range('20130101', periods=3), minor_axis=['first', 'second']) >>> p <class 'pandas.core.panel.Panel'> Dimensions: 4 (items) x 3 (major_axis) x 2 (minor_axis) Items axis: A to D Major_axis axis: 2013-01-01 00:00:00 to 2013-01-03 00:00:00 Minor_axis axis: first to second >>> p.to_xarray() <xarray.DataArray (items: 4, major_axis: 3, minor_axis: 2)> array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]], [[12, 13], [14, 15], [16, 17]], [[18, 19], [20, 21], [22, 23]]]) Coordinates: * items (items) object 'A' 'B' 'C' 'D' * major_axis (major_axis) datetime64[ns] 2013-01-01 2013-01-02 2013-01-03 # noqa * minor_axis (minor_axis) object 'first' 'second' Notes ----- See the `xarray docs <http://xarray.pydata.org/en/stable/>`__ """ import xarray if self.ndim == 1: return xarray.DataArray.from_series(self) elif self.ndim == 2: return xarray.Dataset.from_dataframe(self) # > 2 dims coords = [(a, self._get_axis(a)) for a in self._AXIS_ORDERS] return xarray.DataArray(self, coords=coords, ) # ---------------------------------------------------------------------- # Fancy Indexing @classmethod def _create_indexer(cls, name, indexer): """Create an indexer like _name in the class.""" if getattr(cls, name, None) is None: iname = '_%s' % name setattr(cls, iname, None) def _indexer(self): i = getattr(self, iname) if i is None: i = indexer(self, name) setattr(self, iname, i) return i setattr(cls, name, property(_indexer, doc=indexer.__doc__)) # add to our internal names set cls._internal_names_set.add(iname) def get(self, key, default=None): """ Get item from object for given key (DataFrame column, Panel slice, etc.). Returns default value if not found. Parameters ---------- key : object Returns ------- value : type of items contained in object """ try: return self[key] except (KeyError, ValueError, IndexError): return default def __getitem__(self, item): return self._get_item_cache(item) def _get_item_cache(self, item): """Return the cached item, item represents a label indexer.""" cache = self._item_cache res = cache.get(item) if res is None: values = self._data.get(item) res = self._box_item_values(item, values) cache[item] = res res._set_as_cached(item, self) # for a chain res.is_copy = self.is_copy return res def _set_as_cached(self, item, cacher): """Set the _cacher attribute on the calling object with a weakref to cacher. """ self._cacher = (item, weakref.ref(cacher)) def _reset_cacher(self): """Reset the cacher.""" if hasattr(self, '_cacher'): del self._cacher def _iget_item_cache(self, item): """Return the cached item, item represents a positional indexer.""" ax = self._info_axis if ax.is_unique: lower = self._get_item_cache(ax[item]) else: lower = self.take(item, axis=self._info_axis_number, convert=True) return lower def _box_item_values(self, key, values): raise AbstractMethodError(self) def _maybe_cache_changed(self, item, value): """The object has called back to us saying maybe it has changed. numpy < 1.8 has an issue with object arrays and aliasing GH6026 """ self._data.set(item, value, check=pd._np_version_under1p8) @property def _is_cached(self): """Return boolean indicating if self is cached or not.""" return getattr(self, '_cacher', None) is not None def _get_cacher(self): """return my cacher or None""" cacher = getattr(self, '_cacher', None) if cacher is not None: cacher = cacher[1]() return cacher @property def _is_view(self): """Return boolean indicating if self is view of another array """ return self._data.is_view def _maybe_update_cacher(self, clear=False, verify_is_copy=True): """ See if we need to update our parent cacher if clear, then clear our cache. Parameters ---------- clear : boolean, default False clear the item cache verify_is_copy : boolean, default True provide is_copy checks """ cacher = getattr(self, '_cacher', None) if cacher is not None: ref = cacher[1]() # we are trying to reference a dead referant, hence # a copy if ref is None: del self._cacher else: try: ref._maybe_cache_changed(cacher[0], self) except: pass if verify_is_copy: self._check_setitem_copy(stacklevel=5, t='referant') if clear: self._clear_item_cache() def _clear_item_cache(self, i=None): if i is not None: self._item_cache.pop(i, None) else: self._item_cache.clear() def _slice(self, slobj, axis=0, kind=None): """ Construct a slice of this container. kind parameter is maintained for compatibility with Series slicing. """ axis = self._get_block_manager_axis(axis) result = self._constructor(self._data.get_slice(slobj, axis=axis)) result = result.__finalize__(self) # this could be a view # but only in a single-dtyped view slicable case is_copy = axis != 0 or result._is_view result._set_is_copy(self, copy=is_copy) return result def _set_item(self, key, value): self._data.set(key, value) self._clear_item_cache() def _set_is_copy(self, ref=None, copy=True): if not copy: self.is_copy = None else: if ref is not None: self.is_copy = weakref.ref(ref) else: self.is_copy = None def _check_is_chained_assignment_possible(self): """ Check if we are a view, have a cacher, and are of mixed type. If so, then force a setitem_copy check. Should be called just near setting a value Will return a boolean if it we are a view and are cached, but a single-dtype meaning that the cacher should be updated following setting. """ if self._is_view and self._is_cached: ref = self._get_cacher() if ref is not None and ref._is_mixed_type: self._check_setitem_copy(stacklevel=4, t='referant', force=True) return True elif self.is_copy: self._check_setitem_copy(stacklevel=4, t='referant') return False def _check_setitem_copy(self, stacklevel=4, t='setting', force=False): """ Parameters ---------- stacklevel : integer, default 4 the level to show of the stack when the error is output t : string, the type of setting error force : boolean, default False if True, then force showing an error validate if we are doing a settitem on a chained copy. If you call this function, be sure to set the stacklevel such that the user will see the error *at the level of setting* It is technically possible to figure out that we are setting on a copy even WITH a multi-dtyped pandas object. In other words, some blocks may be views while other are not. Currently _is_view will ALWAYS return False for multi-blocks to avoid having to handle this case. df = DataFrame(np.arange(0,9), columns=['count']) df['group'] = 'b' # This technically need not raise SettingWithCopy if both are view # (which is not # generally guaranteed but is usually True. However, # this is in general not a good practice and we recommend using .loc. df.iloc[0:5]['group'] = 'a' """ if force or self.is_copy: value = config.get_option('mode.chained_assignment') if value is None: return # see if the copy is not actually refererd; if so, then disolve # the copy weakref try: gc.collect(2) if not gc.get_referents(self.is_copy()): self.is_copy = None return except: pass # we might be a false positive try: if self.is_copy().shape == self.shape: self.is_copy = None return except: pass # a custom message if isinstance(self.is_copy, string_types): t = self.is_copy elif t == 'referant': t = ("\n" "A value is trying to be set on a copy of a slice from a " "DataFrame\n\n" "See the caveats in the documentation: " "http://pandas.pydata.org/pandas-docs/stable/" "indexing.html#indexing-view-versus-copy" ) else: t = ("\n" "A value is trying to be set on a copy of a slice from a " "DataFrame.\n" "Try using .loc[row_indexer,col_indexer] = value " "instead\n\nSee the caveats in the documentation: " "http://pandas.pydata.org/pandas-docs/stable/" "indexing.html#indexing-view-versus-copy" ) if value == 'raise': raise SettingWithCopyError(t) elif value == 'warn': warnings.warn(t, SettingWithCopyWarning, stacklevel=stacklevel) def __delitem__(self, key): """ Delete item """ deleted = False maybe_shortcut = False if hasattr(self, 'columns') and isinstance(self.columns, MultiIndex): try: maybe_shortcut = key not in self.columns._engine except TypeError: pass if maybe_shortcut: # Allow shorthand to delete all columns whose first len(key) # elements match key: if not isinstance(key, tuple): key = (key, ) for col in self.columns: if isinstance(col, tuple) and col[:len(key)] == key: del self[col] deleted = True if not deleted: # If the above loop ran and didn't delete anything because # there was no match, this call should raise the appropriate # exception: self._data.delete(key) # delete from the caches try: del self._item_cache[key] except KeyError: pass def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs): """ Analogous to ndarray.take Parameters ---------- indices : list / array of ints axis : int, default 0 convert : translate neg to pos indices (default) is_copy : mark the returned frame as a copy Returns ------- taken : type of caller """ nv.validate_take(tuple(), kwargs) self._consolidate_inplace() new_data = self._data.take(indices, axis=self._get_block_manager_axis(axis), convert=True, verify=True) result = self._constructor(new_data).__finalize__(self) # maybe set copy if we didn't actually change the index if is_copy: if not result._get_axis(axis).equals(self._get_axis(axis)): result._set_is_copy(self) return result def xs(self, key, axis=0, level=None, drop_level=True): """ Returns a cross-section (row(s) or column(s)) from the Series/DataFrame. Defaults to cross-section on the rows (axis=0). Parameters ---------- key : object Some label contained in the index, or partially in a MultiIndex axis : int, default 0 Axis to retrieve cross-section on level : object, defaults to first n levels (n=1 or len(key)) In case of a key partially contained in a MultiIndex, indicate which levels are used. Levels can be referred by label or position. drop_level : boolean, default True If False, returns object with same levels as self. Examples -------- >>> df A B C a 4 5 2 b 4 0 9 c 9 7 3 >>> df.xs('a') A 4 B 5 C 2 Name: a >>> df.xs('C', axis=1) a 2 b 9 c 3 Name: C >>> df A B C D first second third bar one 1 4 1 8 9 two 1 7 5 5 0 baz one 1 6 6 8 0 three 2 5 3 5 3 >>> df.xs(('baz', 'three')) A B C D third 2 5 3 5 3 >>> df.xs('one', level=1) A B C D first third bar 1 4 1 8 9 baz 1 6 6 8 0 >>> df.xs(('baz', 2), level=[0, 'third']) A B C D second three 5 3 5 3 Returns ------- xs : Series or DataFrame Notes ----- xs is only for getting, not setting values. MultiIndex Slicers is a generic way to get/set values on any level or levels. It is a superset of xs functionality, see :ref:`MultiIndex Slicers <advanced.mi_slicers>` """ axis = self._get_axis_number(axis) labels = self._get_axis(axis) if level is not None: loc, new_ax = labels.get_loc_level(key, level=level, drop_level=drop_level) # convert to a label indexer if needed if isinstance(loc, slice): lev_num = labels._get_level_number(level) if labels.levels[lev_num].inferred_type == 'integer': loc = labels[loc] # create the tuple of the indexer indexer = [slice(None)] * self.ndim indexer[axis] = loc indexer = tuple(indexer) result = self.ix[indexer] setattr(result, result._get_axis_name(axis), new_ax) return result if axis == 1: return self[key] self._consolidate_inplace() index = self.index if isinstance(index, MultiIndex): loc, new_index = self.index.get_loc_level(key, drop_level=drop_level) else: loc = self.index.get_loc(key) if isinstance(loc, np.ndarray): if loc.dtype == np.bool_: inds, = loc.nonzero() return self.take(inds, axis=axis, convert=False) else: return self.take(loc, axis=axis, convert=True) if not is_scalar(loc): new_index = self.index[loc] if is_scalar(loc): new_values = self._data.fast_xs(loc) # may need to box a datelike-scalar # # if we encounter an array-like and we only have 1 dim # that means that their are list/ndarrays inside the Series! # so just return them (GH 6394) if not is_list_like(new_values) or self.ndim == 1: return _maybe_box_datetimelike(new_values) result = self._constructor_sliced( new_values, index=self.columns, name=self.index[loc], dtype=new_values.dtype) else: result = self.iloc[loc] result.index = new_index # this could be a view # but only in a single-dtyped view slicable case result._set_is_copy(self, copy=not result._is_view) return result _xs = xs # TODO: Check if this was clearer in 0.12 def select(self, crit, axis=0): """ Return data corresponding to axis labels matching criteria Parameters ---------- crit : function To be called on each index (label). Should return True or False axis : int Returns ------- selection : type of caller """ axis = self._get_axis_number(axis) axis_name = self._get_axis_name(axis) axis_values = self._get_axis(axis) if len(axis_values) > 0: new_axis = axis_values[ np.asarray([bool(crit(label)) for label in axis_values])] else: new_axis = axis_values return self.reindex(**{axis_name: new_axis}) def reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None): """Return an object with matching indices to myself. Parameters ---------- other : Object method : string or None copy : boolean, default True limit : int, default None Maximum number of consecutive labels to fill for inexact matches. tolerance : optional Maximum distance between labels of the other object and this object for inexact matches. .. versionadded:: 0.17.0 Notes ----- Like calling s.reindex(index=other.index, columns=other.columns, method=...) Returns ------- reindexed : same as input """ d = other._construct_axes_dict(axes=self._AXIS_ORDERS, method=method, copy=copy, limit=limit, tolerance=tolerance) return self.reindex(**d) def drop(self, labels, axis=0, level=None, inplace=False, errors='raise'): """ Return new object with labels in requested axis removed. Parameters ---------- labels : single label or list-like axis : int or axis name level : int or level name, default None For MultiIndex inplace : bool, default False If True, do operation inplace and return None. errors : {'ignore', 'raise'}, default 'raise' If 'ignore', suppress error and existing labels are dropped. .. versionadded:: 0.16.1 Returns ------- dropped : type of caller """ axis = self._get_axis_number(axis) axis_name = self._get_axis_name(axis) axis, axis_ = self._get_axis(axis), axis if axis.is_unique: if level is not None: if not isinstance(axis, MultiIndex): raise AssertionError('axis must be a MultiIndex') new_axis = axis.drop(labels, level=level, errors=errors) else: new_axis = axis.drop(labels, errors=errors) dropped = self.reindex(**{axis_name: new_axis}) try: dropped.axes[axis_].set_names(axis.names, inplace=True) except AttributeError: pass result = dropped else: labels = com._index_labels_to_array(labels) if level is not None: if not isinstance(axis, MultiIndex): raise AssertionError('axis must be a MultiIndex') indexer = ~axis.get_level_values(level).isin(labels) else: indexer = ~axis.isin(labels) slicer = [slice(None)] * self.ndim slicer[self._get_axis_number(axis_name)] = indexer result = self.ix[tuple(slicer)] if inplace: self._update_inplace(result) else: return result def _update_inplace(self, result, verify_is_copy=True): """ Replace self internals with result. Parameters ---------- verify_is_copy : boolean, default True provide is_copy checks """ # NOTE: This does *not* call __finalize__ and that's an explicit # decision that we may revisit in the future. self._reset_cache() self._clear_item_cache() self._data = getattr(result, '_data', result) self._maybe_update_cacher(verify_is_copy=verify_is_copy) def add_prefix(self, prefix): """ Concatenate prefix string with panel items names. Parameters ---------- prefix : string Returns ------- with_prefix : type of caller """ new_data = self._data.add_prefix(prefix) return self._constructor(new_data).__finalize__(self) def add_suffix(self, suffix): """ Concatenate suffix string with panel items names. Parameters ---------- suffix : string Returns ------- with_suffix : type of caller """ new_data = self._data.add_suffix(suffix) return self._constructor(new_data).__finalize__(self) _shared_docs['sort_values'] = """ Sort by the values along either axis .. versionadded:: 0.17.0 Parameters ----------%(optional_by)s axis : %(axes_single_arg)s, default 0 Axis to direct sorting ascending : bool or list of bool, default True Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by. inplace : bool, default False if True, perform operation in-place kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort' Choice of sorting algorithm. See also ndarray.np.sort for more information. `mergesort` is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label. na_position : {'first', 'last'}, default 'last' `first` puts NaNs at the beginning, `last` puts NaNs at the end Returns ------- sorted_obj : %(klass)s """ def sort_values(self, by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last'): raise AbstractMethodError(self) _shared_docs['sort_index'] = """ Sort object by labels (along an axis) Parameters ---------- axis : %(axes)s to direct sorting level : int or level name or list of ints or list of level names if not None, sort on values in specified index level(s) ascending : boolean, default True Sort ascending vs. descending inplace : bool, default False if True, perform operation in-place kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort' Choice of sorting algorithm. See also ndarray.np.sort for more information. `mergesort` is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label. na_position : {'first', 'last'}, default 'last' `first` puts NaNs at the beginning, `last` puts NaNs at the end sort_remaining : bool, default True if true and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level Returns ------- sorted_obj : %(klass)s """ @Appender(_shared_docs['sort_index'] % dict(axes="axes", klass="NDFrame")) def sort_index(self, axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True): axis = self._get_axis_number(axis) axis_name = self._get_axis_name(axis) labels = self._get_axis(axis) if level is not None: raise NotImplementedError("level is not implemented") if inplace: raise NotImplementedError("inplace is not implemented") sort_index = labels.argsort() if not ascending: sort_index = sort_index[::-1] new_axis = labels.take(sort_index) return self.reindex(**{axis_name: new_axis}) _shared_docs['reindex'] = """ Conform %(klass)s to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False Parameters ---------- %(axes)s : array-like, optional (can be specified in order, or as keywords) New labels / index to conform to. Preferably an Index object to avoid duplicating data method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index. * default: don't fill gaps * pad / ffill: propagate last valid observation forward to next valid * backfill / bfill: use next valid observation to fill gap * nearest: use nearest valid observations to fill gap copy : boolean, default True Return a new object, even if the passed indexes are the same level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level fill_value : scalar, default np.NaN Value to use for missing values. Defaults to NaN, but can be any "compatible" value limit : int, default None Maximum number of consecutive elements to forward or backward fill tolerance : optional Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations most satisfy the equation ``abs(index[indexer] - target) <= tolerance``. .. versionadded:: 0.17.0 Examples -------- Create a dataframe with some fictional data. >>> index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror'] >>> df = pd.DataFrame({ ... 'http_status': [200,200,404,404,301], ... 'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]}, ... index=index) >>> df http_status response_time Firefox 200 0.04 Chrome 200 0.02 Safari 404 0.07 IE10 404 0.08 Konqueror 301 1.00 Create a new index and reindex the dataframe. By default values in the new index that do not have corresponding records in the dataframe are assigned ``NaN``. >>> new_index= ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10', ... 'Chrome'] >>> df.reindex(new_index) http_status response_time Safari 404 0.07 Iceweasel NaN NaN Comodo Dragon NaN NaN IE10 404 0.08 Chrome 200 0.02 We can fill in the missing values by passing a value to the keyword ``fill_value``. Because the index is not monotonically increasing or decreasing, we cannot use arguments to the keyword ``method`` to fill the ``NaN`` values. >>> df.reindex(new_index, fill_value=0) http_status response_time Safari 404 0.07 Iceweasel 0 0.00 Comodo Dragon 0 0.00 IE10 404 0.08 Chrome 200 0.02 >>> df.reindex(new_index, fill_value='missing') http_status response_time Safari 404 0.07 Iceweasel missing missing Comodo Dragon missing missing IE10 404 0.08 Chrome 200 0.02 To further illustrate the filling functionality in ``reindex``, we will create a dataframe with a monotonically increasing index (for example, a sequence of dates). >>> date_index = pd.date_range('1/1/2010', periods=6, freq='D') >>> df2 = pd.DataFrame({"prices": [100, 101, np.nan, 100, 89, 88]}, ... index=date_index) >>> df2 prices 2010-01-01 100 2010-01-02 101 2010-01-03 NaN 2010-01-04 100 2010-01-05 89 2010-01-06 88 Suppose we decide to expand the dataframe to cover a wider date range. >>> date_index2 = pd.date_range('12/29/2009', periods=10, freq='D') >>> df2.reindex(date_index2) prices 2009-12-29 NaN 2009-12-30 NaN 2009-12-31 NaN 2010-01-01 100 2010-01-02 101 2010-01-03 NaN 2010-01-04 100 2010-01-05 89 2010-01-06 88 2010-01-07 NaN The index entries that did not have a value in the original data frame (for example, '2009-12-29') are by default filled with ``NaN``. If desired, we can fill in the missing values using one of several options. For example, to backpropagate the last valid value to fill the ``NaN`` values, pass ``bfill`` as an argument to the ``method`` keyword. >>> df2.reindex(date_index2, method='bfill') prices 2009-12-29 100 2009-12-30 100 2009-12-31 100 2010-01-01 100 2010-01-02 101 2010-01-03 NaN 2010-01-04 100 2010-01-05 89 2010-01-06 88 2010-01-07 NaN Please note that the ``NaN`` value present in the original dataframe (at index value 2010-01-03) will not be filled by any of the value propagation schemes. This is because filling while reindexing does not look at dataframe values, but only compares the original and desired indexes. If you do want to fill in the ``NaN`` values present in the original dataframe, use the ``fillna()`` method. Returns ------- reindexed : %(klass)s """ # TODO: Decide if we care about having different examples for different # kinds @Appender(_shared_docs['reindex'] % dict(axes="axes", klass="NDFrame")) def reindex(self, *args, **kwargs): # construct the args axes, kwargs = self._construct_axes_from_arguments(args, kwargs) method = missing.clean_reindex_fill_method(kwargs.pop('method', None)) level = kwargs.pop('level', None) copy = kwargs.pop('copy', True) limit = kwargs.pop('limit', None) tolerance = kwargs.pop('tolerance', None) fill_value = kwargs.pop('fill_value', np.nan) if kwargs: raise TypeError('reindex() got an unexpected keyword ' 'argument "{0}"'.format(list(kwargs.keys())[0])) self._consolidate_inplace() # if all axes that are requested to reindex are equal, then only copy # if indicated must have index names equal here as well as values if all([self._get_axis(axis).identical(ax) for axis, ax in axes.items() if ax is not None]): if copy: return self.copy() return self # check if we are a multi reindex if self._needs_reindex_multi(axes, method, level): try: return self._reindex_multi(axes, copy, fill_value) except: pass # perform the reindex on the axes return self._reindex_axes(axes, level, limit, tolerance, method, fill_value, copy).__finalize__(self) def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy): """Perform the reindex for all the axes.""" obj = self for a in self._AXIS_ORDERS: labels = axes[a] if labels is None: continue ax = self._get_axis(a) new_index, indexer = ax.reindex(labels, level=level, limit=limit, tolerance=tolerance, method=method) axis = self._get_axis_number(a) obj = obj._reindex_with_indexers({axis: [new_index, indexer]}, fill_value=fill_value, copy=copy, allow_dups=False) return obj def _needs_reindex_multi(self, axes, method, level): """Check if we do need a multi reindex.""" return ((com._count_not_none(*axes.values()) == self._AXIS_LEN) and method is None and level is None and not self._is_mixed_type) def _reindex_multi(self, axes, copy, fill_value): return NotImplemented _shared_docs[ 'reindex_axis'] = ("""Conform input object to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False Parameters ---------- labels : array-like New labels / index to conform to. Preferably an Index object to avoid duplicating data axis : %(axes_single_arg)s method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional Method to use for filling holes in reindexed DataFrame: * default: don't fill gaps * pad / ffill: propagate last valid observation forward to next valid * backfill / bfill: use next valid observation to fill gap * nearest: use nearest valid observations to fill gap copy : boolean, default True Return a new object, even if the passed indexes are the same level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level limit : int, default None Maximum number of consecutive elements to forward or backward fill tolerance : optional Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations most satisfy the equation ``abs(index[indexer] - target) <= tolerance``. .. versionadded:: 0.17.0 Examples -------- >>> df.reindex_axis(['A', 'B', 'C'], axis=1) See Also -------- reindex, reindex_like Returns ------- reindexed : %(klass)s """) @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs) def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, limit=None, fill_value=np.nan): self._consolidate_inplace() axis_name = self._get_axis_name(axis) axis_values = self._get_axis(axis_name) method = missing.clean_reindex_fill_method(method) new_index, indexer = axis_values.reindex(labels, method, level, limit=limit) return self._reindex_with_indexers({axis: [new_index, indexer]}, fill_value=fill_value, copy=copy) def _reindex_with_indexers(self, reindexers, fill_value=np.nan, copy=False, allow_dups=False): """allow_dups indicates an internal call here """ # reindex doing multiple operations on different axes if indicated new_data = self._data for axis in sorted(reindexers.keys()): index, indexer = reindexers[axis] baxis = self._get_block_manager_axis(axis) if index is None: continue index = _ensure_index(index) if indexer is not None: indexer = _ensure_int64(indexer) # TODO: speed up on homogeneous DataFrame objects new_data = new_data.reindex_indexer(index, indexer, axis=baxis, fill_value=fill_value, allow_dups=allow_dups, copy=copy) if copy and new_data is self._data: new_data = new_data.copy() return self._constructor(new_data).__finalize__(self) def _reindex_axis(self, new_index, fill_method, axis, copy): new_data = self._data.reindex_axis(new_index, axis=axis, method=fill_method, copy=copy) if new_data is self._data and not copy: return self else: return self._constructor(new_data).__finalize__(self) def filter(self, items=None, like=None, regex=None, axis=None): """ Subset rows or columns of dataframe according to labels in the specified index. Note that this routine does not filter a dataframe on its contents. The filter is applied to the labels of the index. Parameters ---------- items : list-like List of info axis to restrict to (must not all be present) like : string Keep info axis where "arg in col == True" regex : string (regular expression) Keep info axis with re.search(regex, col) == True axis : int or string axis name The axis to filter on. By default this is the info axis, 'index' for Series, 'columns' for DataFrame Returns ------- same type as input object Examples -------- >>> df one two three mouse 1 2 3 rabbit 4 5 6 >>> # select columns by name >>> df.filter(items=['one', 'three']) one three mouse 1 3 rabbit 4 6 >>> # select columns by regular expression >>> df.filter(regex='e$', axis=1) one three mouse 1 3 rabbit 4 6 >>> # select rows containing 'bbi' >>> df.filter(like='bbi', axis=0) one two three rabbit 4 5 6 See Also -------- pandas.DataFrame.select Notes ----- The ``items``, ``like``, and ``regex`` parameters are enforced to be mutually exclusive. ``axis`` defaults to the info axis that is used when indexing with ``[]``. """ import re nkw = sum([x is not None for x in [items, like, regex]]) if nkw > 1: raise TypeError('Keyword arguments `items`, `like`, or `regex` ' 'are mutually exclusive') if axis is None: axis = self._info_axis_name axis_name = self._get_axis_name(axis) axis_values = self._get_axis(axis_name) if items is not None: return self.reindex(**{axis_name: [r for r in items if r in axis_values]}) elif like: matchf = lambda x: (like in x if isinstance(x, string_types) else like in str(x)) return self.select(matchf, axis=axis_name) elif regex: matcher = re.compile(regex) return self.select(lambda x: matcher.search(str(x)) is not None, axis=axis_name) else: raise TypeError('Must pass either `items`, `like`, or `regex`') def head(self, n=5): """ Returns first n rows """ return self.iloc[:n] def tail(self, n=5): """ Returns last n rows """ if n == 0: return self.iloc[0:0] return self.iloc[-n:] def sample(self, n=None, frac=None, replace=False, weights=None, random_state=None, axis=None): """ Returns a random sample of items from an axis of object. .. versionadded:: 0.16.1 Parameters ---------- n : int, optional Number of items from axis to return. Cannot be used with `frac`. Default = 1 if `frac` = None. frac : float, optional Fraction of axis items to return. Cannot be used with `n`. replace : boolean, optional Sample with or without replacement. Default = False. weights : str or ndarray-like, optional Default 'None' results in equal probability weighting. If passed a Series, will align with target object on index. Index values in weights not found in sampled object will be ignored and index values in sampled object not in weights will be assigned weights of zero. If called on a DataFrame, will accept the name of a column when axis = 0. Unless weights are a Series, weights must be same length as axis being sampled. If weights do not sum to 1, they will be normalized to sum to 1. Missing values in the weights column will be treated as zero. inf and -inf values not allowed. random_state : int or numpy.random.RandomState, optional Seed for the random number generator (if int), or numpy RandomState object. axis : int or string, optional Axis to sample. Accepts axis number or name. Default is stat axis for given data type (0 for Series and DataFrames, 1 for Panels). Returns ------- A new object of same type as caller. Examples -------- Generate an example ``Series`` and ``DataFrame``: >>> s = pd.Series(np.random.randn(50)) >>> s.head() 0 -0.038497 1 1.820773 2 -0.972766 3 -1.598270 4 -1.095526 dtype: float64 >>> df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD')) >>> df.head() A B C D 0 0.016443 -2.318952 -0.566372 -1.028078 1 -1.051921 0.438836 0.658280 -0.175797 2 -1.243569 -0.364626 -0.215065 0.057736 3 1.768216 0.404512 -0.385604 -1.457834 4 1.072446 -1.137172 0.314194 -0.046661 Next extract a random sample from both of these objects... 3 random elements from the ``Series``: >>> s.sample(n=3) 27 -0.994689 55 -1.049016 67 -0.224565 dtype: float64 And a random 10% of the ``DataFrame`` with replacement: >>> df.sample(frac=0.1, replace=True) A B C D 35 1.981780 0.142106 1.817165 -0.290805 49 -1.336199 -0.448634 -0.789640 0.217116 40 0.823173 -0.078816 1.009536 1.015108 15 1.421154 -0.055301 -1.922594 -0.019696 6 -0.148339 0.832938 1.787600 -1.383767 """ if axis is None: axis = self._stat_axis_number axis = self._get_axis_number(axis) axis_length = self.shape[axis] # Process random_state argument rs = com._random_state(random_state) # Check weights for compliance if weights is not None: # If a series, align with frame if isinstance(weights, pd.Series): weights = weights.reindex(self.axes[axis]) # Strings acceptable if a dataframe and axis = 0 if isinstance(weights, string_types): if isinstance(self, pd.DataFrame): if axis == 0: try: weights = self[weights] except KeyError: raise KeyError("String passed to weights not a " "valid column") else: raise ValueError("Strings can only be passed to " "weights when sampling from rows on " "a DataFrame") else: raise ValueError("Strings cannot be passed as weights " "when sampling from a Series or Panel.") weights = pd.Series(weights, dtype='float64') if len(weights) != axis_length: raise ValueError("Weights and axis to be sampled must be of " "same length") if (weights == np.inf).any() or (weights == -np.inf).any(): raise ValueError("weight vector may not include `inf` values") if (weights < 0).any(): raise ValueError("weight vector many not include negative " "values") # If has nan, set to zero. weights = weights.fillna(0) # Renormalize if don't sum to 1 if weights.sum() != 1: if weights.sum() != 0: weights = weights / weights.sum() else: raise ValueError("Invalid weights: weights sum to zero") weights = weights.values # If no frac or n, default to n=1. if n is None and frac is None: n = 1 elif n is not None and frac is None and n % 1 != 0: raise ValueError("Only integers accepted as `n` values") elif n is None and frac is not None: n = int(round(frac * axis_length)) elif n is not None and frac is not None: raise ValueError('Please enter a value for `frac` OR `n`, not ' 'both') # Check for negative sizes if n < 0: raise ValueError("A negative number of rows requested. Please " "provide positive value.") locs = rs.choice(axis_length, size=n, replace=replace, p=weights) return self.take(locs, axis=axis, is_copy=False) _shared_docs['pipe'] = (""" Apply func(self, \*args, \*\*kwargs) .. versionadded:: 0.16.2 Parameters ---------- func : function function to apply to the %(klass)s. ``args``, and ``kwargs`` are passed into ``func``. Alternatively a ``(callable, data_keyword)`` tuple where ``data_keyword`` is a string indicating the keyword of ``callable`` that expects the %(klass)s. args : positional arguments passed into ``func``. kwargs : a dictionary of keyword arguments passed into ``func``. Returns ------- object : the return type of ``func``. Notes ----- Use ``.pipe`` when chaining together functions that expect on Series or DataFrames. Instead of writing >>> f(g(h(df), arg1=a), arg2=b, arg3=c) You can write >>> (df.pipe(h) ... .pipe(g, arg1=a) ... .pipe(f, arg2=b, arg3=c) ... ) If you have a function that takes the data as (say) the second argument, pass a tuple indicating which keyword expects the data. For example, suppose ``f`` takes its data as ``arg2``: >>> (df.pipe(h) ... .pipe(g, arg1=a) ... .pipe((f, 'arg2'), arg1=a, arg3=c) ... ) See Also -------- pandas.DataFrame.apply pandas.DataFrame.applymap pandas.Series.map """) @Appender(_shared_docs['pipe'] % _shared_doc_kwargs) def pipe(self, func, *args, **kwargs): if isinstance(func, tuple): func, target = func if target in kwargs: raise ValueError('%s is both the pipe target and a keyword ' 'argument' % target) kwargs[target] = self return func(*args, **kwargs) else: return func(self, *args, **kwargs) # ---------------------------------------------------------------------- # Attribute access def __finalize__(self, other, method=None, **kwargs): """ Propagate metadata from other to self. Parameters ---------- other : the object from which to get the attributes that we are going to propagate method : optional, a passed method name ; possibly to take different types of propagation actions based on this """ if isinstance(other, NDFrame): for name in self._metadata: object.__setattr__(self, name, getattr(other, name, None)) return self def __getattr__(self, name): """After regular attribute access, try looking up the name This allows simpler access to columns for interactive use. """ # Note: obj.x will always call obj.__getattribute__('x') prior to # calling obj.__getattr__('x'). if (name in self._internal_names_set or name in self._metadata or name in self._accessors): return object.__getattribute__(self, name) else: if name in self._info_axis: return self[name] return object.__getattribute__(self, name) def __setattr__(self, name, value): """After regular attribute access, try setting the name This allows simpler access to columns for interactive use. """ # first try regular attribute access via __getattribute__, so that # e.g. ``obj.x`` and ``obj.x = 4`` will always reference/modify # the same attribute. try: object.__getattribute__(self, name) return object.__setattr__(self, name, value) except AttributeError: pass # if this fails, go on to more involved attribute setting # (note that this matches __getattr__, above). if name in self._internal_names_set: object.__setattr__(self, name, value) elif name in self._metadata: object.__setattr__(self, name, value) else: try: existing = getattr(self, name) if isinstance(existing, Index): object.__setattr__(self, name, value) elif name in self._info_axis: self[name] = value else: object.__setattr__(self, name, value) except (AttributeError, TypeError): object.__setattr__(self, name, value) # ---------------------------------------------------------------------- # Getting and setting elements # ---------------------------------------------------------------------- # Consolidation of internals def _protect_consolidate(self, f): """Consolidate _data -- if the blocks have changed, then clear the cache """ blocks_before = len(self._data.blocks) result = f() if len(self._data.blocks) != blocks_before: self._clear_item_cache() return result def _consolidate_inplace(self): """Consolidate data in place and return None""" def f(): self._data = self._data.consolidate() self._protect_consolidate(f) def consolidate(self, inplace=False): """ Compute NDFrame with "consolidated" internals (data of each dtype grouped together in a single ndarray). Mainly an internal API function, but available here to the savvy user Parameters ---------- inplace : boolean, default False If False return new object, otherwise modify existing object Returns ------- consolidated : type of caller """ if inplace: self._consolidate_inplace() else: f = lambda: self._data.consolidate() cons_data = self._protect_consolidate(f) return self._constructor(cons_data).__finalize__(self) @property def _is_mixed_type(self): f = lambda: self._data.is_mixed_type return self._protect_consolidate(f) @property def _is_numeric_mixed_type(self): f = lambda: self._data.is_numeric_mixed_type return self._protect_consolidate(f) @property def _is_datelike_mixed_type(self): f = lambda: self._data.is_datelike_mixed_type return self._protect_consolidate(f) def _check_inplace_setting(self, value): """ check whether we allow in-place setting with this type of value """ if self._is_mixed_type: if not self._is_numeric_mixed_type: # allow an actual np.nan thru try: if np.isnan(value): return True except: pass raise TypeError('Cannot do inplace boolean setting on ' 'mixed-types with a non np.nan value') return True def _get_numeric_data(self): return self._constructor( self._data.get_numeric_data()).__finalize__(self) def _get_bool_data(self): return self._constructor(self._data.get_bool_data()).__finalize__(self) # ---------------------------------------------------------------------- # Internal Interface Methods def as_matrix(self, columns=None): """ Convert the frame to its Numpy-array representation. Parameters ---------- columns: list, optional, default:None If None, return all columns, otherwise, returns specified columns. Returns ------- values : ndarray If the caller is heterogeneous and contains booleans or objects, the result will be of dtype=object. See Notes. Notes ----- Return is NOT a Numpy-matrix, rather, a Numpy-array. The dtype will be a lower-common-denominator dtype (implicit upcasting); that is to say if the dtypes (even of numeric types) are mixed, the one that accommodates all will be chosen. Use this with care if you are not dealing with the blocks. e.g. If the dtypes are float16 and float32, dtype will be upcast to float32. If dtypes are int32 and uint8, dtype will be upcase to int32. By numpy.find_common_type convention, mixing int64 and uint64 will result in a flot64 dtype. This method is provided for backwards compatibility. Generally, it is recommended to use '.values'. See Also -------- pandas.DataFrame.values """ self._consolidate_inplace() if self._AXIS_REVERSED: return self._data.as_matrix(columns).T return self._data.as_matrix(columns) @property def values(self): """Numpy representation of NDFrame Notes ----- The dtype will be a lower-common-denominator dtype (implicit upcasting); that is to say if the dtypes (even of numeric types) are mixed, the one that accommodates all will be chosen. Use this with care if you are not dealing with the blocks. e.g. If the dtypes are float16 and float32, dtype will be upcast to float32. If dtypes are int32 and uint8, dtype will be upcast to int32. By numpy.find_common_type convention, mixing int64 and uint64 will result in a flot64 dtype. """ return self.as_matrix() @property def _values(self): """internal implementation""" return self.values @property def _get_values(self): # compat return self.as_matrix() def get_values(self): """same as values (but handles sparseness conversions)""" return self.as_matrix() def get_dtype_counts(self): """Return the counts of dtypes in this object.""" from pandas import Series return Series(self._data.get_dtype_counts()) def get_ftype_counts(self): """Return the counts of ftypes in this object.""" from pandas import Series return Series(self._data.get_ftype_counts()) @property def dtypes(self): """Return the dtypes in this object.""" from pandas import Series return Series(self._data.get_dtypes(), index=self._info_axis, dtype=np.object_) @property def ftypes(self): """ Return the ftypes (indication of sparse/dense and dtype) in this object. """ from pandas import Series return Series(self._data.get_ftypes(), index=self._info_axis, dtype=np.object_) def as_blocks(self, copy=True): """ Convert the frame to a dict of dtype -> Constructor Types that each has a homogeneous dtype. NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in as_matrix) Parameters ---------- copy : boolean, default True .. versionadded: 0.16.1 Returns ------- values : a dict of dtype -> Constructor Types """ self._consolidate_inplace() bd = {} for b in self._data.blocks: bd.setdefault(str(b.dtype), []).append(b) result = {} for dtype, blocks in bd.items(): # Must combine even after consolidation, because there may be # sparse items which are never consolidated into one block. combined = self._data.combine(blocks, copy=copy) result[dtype] = self._constructor(combined).__finalize__(self) return result @property def blocks(self): """Internal property, property synonym for as_blocks()""" return self.as_blocks() def astype(self, dtype, copy=True, raise_on_error=True, **kwargs): """ Cast object to input numpy.dtype Return a copy when copy = True (be really careful with this!) Parameters ---------- dtype : data type, or dict of column name -> data type Use a numpy.dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, ...}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame's columns to column-specific types. raise_on_error : raise on invalid input kwargs : keyword arguments to pass on to the constructor Returns ------- casted : type of caller """ if isinstance(dtype, collections.Mapping): if self.ndim == 1: # i.e. Series if len(dtype) > 1 or list(dtype.keys())[0] != self.name: raise KeyError('Only the Series name can be used for ' 'the key in Series dtype mappings.') new_type = list(dtype.values())[0] return self.astype(new_type, copy, raise_on_error, **kwargs) elif self.ndim > 2: raise NotImplementedError( 'astype() only accepts a dtype arg of type dict when ' 'invoked on Series and DataFrames. A single dtype must be ' 'specified when invoked on a Panel.' ) for col_name in dtype.keys(): if col_name not in self: raise KeyError('Only a column name can be used for the ' 'key in a dtype mappings argument.') from pandas import concat results = [] for col_name, col in self.iteritems(): if col_name in dtype: results.append(col.astype(dtype[col_name], copy=copy)) else: results.append(results.append(col.copy() if copy else col)) return concat(results, axis=1, copy=False) # else, only a single dtype is given new_data = self._data.astype(dtype=dtype, copy=copy, raise_on_error=raise_on_error, **kwargs) return self._constructor(new_data).__finalize__(self) def copy(self, deep=True): """ Make a copy of this objects data. Parameters ---------- deep : boolean or string, default True Make a deep copy, including a copy of the data and the indices. With ``deep=False`` neither the indices or the data are copied. Note that when ``deep=True`` data is copied, actual python objects will not be copied recursively, only the reference to the object. This is in contrast to ``copy.deepcopy`` in the Standard Library, which recursively copies object data. Returns ------- copy : type of caller """ data = self._data.copy(deep=deep) return self._constructor(data).__finalize__(self) def _convert(self, datetime=False, numeric=False, timedelta=False, coerce=False, copy=True): """ Attempt to infer better dtype for object columns Parameters ---------- datetime : boolean, default False If True, convert to date where possible. numeric : boolean, default False If True, attempt to convert to numbers (including strings), with unconvertible values becoming NaN. timedelta : boolean, default False If True, convert to timedelta where possible. coerce : boolean, default False If True, force conversion with unconvertible values converted to nulls (NaN or NaT) copy : boolean, default True If True, return a copy even if no copy is necessary (e.g. no conversion was done). Note: This is meant for internal use, and should not be confused with inplace. Returns ------- converted : same as input object """ return self._constructor( self._data.convert(datetime=datetime, numeric=numeric, timedelta=timedelta, coerce=coerce, copy=copy)).__finalize__(self) # TODO: Remove in 0.18 or 2017, which ever is sooner def convert_objects(self, convert_dates=True, convert_numeric=False, convert_timedeltas=True, copy=True): """ Deprecated. Attempt to infer better dtype for object columns Parameters ---------- convert_dates : boolean, default True If True, convert to date where possible. If 'coerce', force conversion, with unconvertible values becoming NaT. convert_numeric : boolean, default False If True, attempt to coerce to numbers (including strings), with unconvertible values becoming NaN. convert_timedeltas : boolean, default True If True, convert to timedelta where possible. If 'coerce', force conversion, with unconvertible values becoming NaT. copy : boolean, default True If True, return a copy even if no copy is necessary (e.g. no conversion was done). Note: This is meant for internal use, and should not be confused with inplace. See Also -------- pandas.to_datetime : Convert argument to datetime. pandas.to_timedelta : Convert argument to timedelta. pandas.to_numeric : Return a fixed frequency timedelta index, with day as the default. Returns ------- converted : same as input object """ from warnings import warn warn("convert_objects is deprecated. Use the data-type specific " "converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.", FutureWarning, stacklevel=2) return self._constructor( self._data.convert(convert_dates=convert_dates, convert_numeric=convert_numeric, convert_timedeltas=convert_timedeltas, copy=copy)).__finalize__(self) # ---------------------------------------------------------------------- # Filling NA's _shared_docs['fillna'] = (""" Fill NA/NaN values using the specified method Parameters ---------- value : scalar, dict, Series, or DataFrame Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). (values not in the dict/Series/DataFrame will not be filled). This value cannot be a list. method : {'backfill', 'bfill', 'pad', 'ffill', None}, default None Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use NEXT valid observation to fill gap axis : %(axes_single_arg)s inplace : boolean, default False If True, fill in place. Note: this will modify any other views on this object, (e.g. a no-copy slice for a column in a DataFrame). limit : int, default None If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. downcast : dict, default is None a dict of item->dtype of what to downcast if possible, or the string 'infer' which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible) See Also -------- reindex, asfreq Returns ------- filled : %(klass)s """) @Appender(_shared_docs['fillna'] % _shared_doc_kwargs) def fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None): if isinstance(value, (list, tuple)): raise TypeError('"value" parameter must be a scalar or dict, but ' 'you passed a "{0}"'.format(type(value).__name__)) self._consolidate_inplace() # set the default here, so functions examining the signaure # can detect if something was set (e.g. in groupby) (GH9221) if axis is None: axis = 0 axis = self._get_axis_number(axis) method = missing.clean_fill_method(method) from pandas import DataFrame if value is None: if method is None: raise ValueError('must specify a fill method or value') if self._is_mixed_type and axis == 1: if inplace: raise NotImplementedError() result = self.T.fillna(method=method, limit=limit).T # need to downcast here because of all of the transposes result._data = result._data.downcast() return result # > 3d if self.ndim > 3: raise NotImplementedError('Cannot fillna with a method for > ' '3dims') # 3d elif self.ndim == 3: # fill in 2d chunks result = dict([(col, s.fillna(method=method, value=value)) for col, s in self.iteritems()]) new_obj = self._constructor.\ from_dict(result).__finalize__(self) new_data = new_obj._data else: # 2d or less method = missing.clean_fill_method(method) new_data = self._data.interpolate(method=method, axis=axis, limit=limit, inplace=inplace, coerce=True, downcast=downcast) else: if method is not None: raise ValueError('cannot specify both a fill method and value') if len(self._get_axis(axis)) == 0: return self if self.ndim == 1: if isinstance(value, (dict, ABCSeries)): from pandas import Series value = Series(value) elif not is_list_like(value): pass else: raise ValueError("invalid fill value with a %s" % type(value)) new_data = self._data.fillna(value=value, limit=limit, inplace=inplace, downcast=downcast) elif isinstance(value, (dict, ABCSeries)): if axis == 1: raise NotImplementedError('Currently only can fill ' 'with dict/Series column ' 'by column') result = self if inplace else self.copy() for k, v in compat.iteritems(value): if k not in result: continue obj = result[k] obj.fillna(v, limit=limit, inplace=True) return result elif not is_list_like(value): new_data = self._data.fillna(value=value, limit=limit, inplace=inplace, downcast=downcast) elif isinstance(value, DataFrame) and self.ndim == 2: new_data = self.where(self.notnull(), value) else: raise ValueError("invalid fill value with a %s" % type(value)) if inplace: self._update_inplace(new_data) else: return self._constructor(new_data).__finalize__(self) def ffill(self, axis=None, inplace=False, limit=None, downcast=None): """Synonym for NDFrame.fillna(method='ffill')""" return self.fillna(method='ffill', axis=axis, inplace=inplace, limit=limit, downcast=downcast) def bfill(self, axis=None, inplace=False, limit=None, downcast=None): """Synonym for NDFrame.fillna(method='bfill')""" return self.fillna(method='bfill', axis=axis, inplace=inplace, limit=limit, downcast=downcast) def replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None): """ Replace values given in 'to_replace' with 'value'. Parameters ---------- to_replace : str, regex, list, dict, Series, numeric, or None * str or regex: - str: string exactly matching `to_replace` will be replaced with `value` - regex: regexs matching `to_replace` will be replaced with `value` * list of str, regex, or numeric: - First, if `to_replace` and `value` are both lists, they **must** be the same length. - Second, if ``regex=True`` then all of the strings in **both** lists will be interpreted as regexs otherwise they will match directly. This doesn't matter much for `value` since there are only a few possible substitution regexes you can use. - str and regex rules apply as above. * dict: - Nested dictionaries, e.g., {'a': {'b': nan}}, are read as follows: look in column 'a' for the value 'b' and replace it with nan. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) **cannot** be regular expressions. - Keys map to column names and values map to substitution values. You can treat this as a special case of passing two lists except that you are specifying the column to search in. * None: - This means that the ``regex`` argument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. If `value` is also ``None`` then this **must** be a nested dictionary or ``Series``. See the examples section for examples of each of these. value : scalar, dict, list, str, regex, default None Value to use to fill holes (e.g. 0), alternately a dict of values specifying which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. inplace : boolean, default False If True, in place. Note: this will modify any other views on this object (e.g. a column form a DataFrame). Returns the caller if this is True. limit : int, default None Maximum size gap to forward or backward fill regex : bool or same types as `to_replace`, default False Whether to interpret `to_replace` and/or `value` as regular expressions. If this is ``True`` then `to_replace` *must* be a string. Otherwise, `to_replace` must be ``None`` because this parameter will be interpreted as a regular expression or a list, dict, or array of regular expressions. method : string, optional, {'pad', 'ffill', 'bfill'} The method to use when for replacement, when ``to_replace`` is a ``list``. See Also -------- NDFrame.reindex NDFrame.asfreq NDFrame.fillna Returns ------- filled : NDFrame Raises ------ AssertionError * If `regex` is not a ``bool`` and `to_replace` is not ``None``. TypeError * If `to_replace` is a ``dict`` and `value` is not a ``list``, ``dict``, ``ndarray``, or ``Series`` * If `to_replace` is ``None`` and `regex` is not compilable into a regular expression or is a list, dict, ndarray, or Series. ValueError * If `to_replace` and `value` are ``list`` s or ``ndarray`` s, but they are not the same length. Notes ----- * Regex substitution is performed under the hood with ``re.sub``. The rules for substitution for ``re.sub`` are the same. * Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched. However, if those floating point numbers *are* strings, then you can do this. * This method has *a lot* of options. You are encouraged to experiment and play with this method to gain intuition about how it works. """ if not is_bool(regex) and to_replace is not None: raise AssertionError("'to_replace' must be 'None' if 'regex' is " "not a bool") if axis is not None: from warnings import warn warn('the "axis" argument is deprecated and will be removed in' 'v0.13; this argument has no effect') self._consolidate_inplace() if value is None: # passing a single value that is scalar like # when value is None (GH5319), for compat if not is_dict_like(to_replace) and not is_dict_like(regex): to_replace = [to_replace] if isinstance(to_replace, (tuple, list)): return _single_replace(self, to_replace, method, inplace, limit) if not is_dict_like(to_replace): if not is_dict_like(regex): raise TypeError('If "to_replace" and "value" are both None' ' and "to_replace" is not a list, then ' 'regex must be a mapping') to_replace = regex regex = True items = list(compat.iteritems(to_replace)) keys, values = zip(*items) are_mappings = [is_dict_like(v) for v in values] if any(are_mappings): if not all(are_mappings): raise TypeError("If a nested mapping is passed, all values" " of the top level mapping must be " "mappings") # passed a nested dict/Series to_rep_dict = {} value_dict = {} for k, v in items: keys, values = zip(*v.items()) if set(keys) & set(values): raise ValueError("Replacement not allowed with " "overlapping keys and values") to_rep_dict[k] = list(keys) value_dict[k] = list(values) to_replace, value = to_rep_dict, value_dict else: to_replace, value = keys, values return self.replace(to_replace, value, inplace=inplace, limit=limit, regex=regex) else: # need a non-zero len on all axes for a in self._AXIS_ORDERS: if not len(self._get_axis(a)): return self new_data = self._data if is_dict_like(to_replace): if is_dict_like(value): # {'A' : NA} -> {'A' : 0} res = self if inplace else self.copy() for c, src in compat.iteritems(to_replace): if c in value and c in self: # object conversion is handled in # series.replace which is called recursivelly res[c] = res[c].replace(to_replace=src, value=value[c], inplace=False, regex=regex) return None if inplace else res # {'A': NA} -> 0 elif not is_list_like(value): keys = [(k, src) for k, src in compat.iteritems(to_replace) if k in self] keys_len = len(keys) - 1 for i, (k, src) in enumerate(keys): convert = i == keys_len new_data = new_data.replace(to_replace=src, value=value, filter=[k], inplace=inplace, regex=regex, convert=convert) else: raise TypeError('value argument must be scalar, dict, or ' 'Series') elif is_list_like(to_replace): # [NA, ''] -> [0, 'missing'] if is_list_like(value): if len(to_replace) != len(value): raise ValueError('Replacement lists must match ' 'in length. Expecting %d got %d ' % (len(to_replace), len(value))) new_data = self._data.replace_list(src_list=to_replace, dest_list=value, inplace=inplace, regex=regex) else: # [NA, ''] -> 0 new_data = self._data.replace(to_replace=to_replace, value=value, inplace=inplace, regex=regex) elif to_replace is None: if not (is_re_compilable(regex) or is_list_like(regex) or is_dict_like(regex)): raise TypeError("'regex' must be a string or a compiled " "regular expression or a list or dict of " "strings or regular expressions, you " "passed a" " {0!r}".format(type(regex).__name__)) return self.replace(regex, value, inplace=inplace, limit=limit, regex=True) else: # dest iterable dict-like if is_dict_like(value): # NA -> {'A' : 0, 'B' : -1} new_data = self._data for k, v in compat.iteritems(value): if k in self: new_data = new_data.replace(to_replace=to_replace, value=v, filter=[k], inplace=inplace, regex=regex) elif not is_list_like(value): # NA -> 0 new_data = self._data.replace(to_replace=to_replace, value=value, inplace=inplace, regex=regex) else: msg = ('Invalid "to_replace" type: ' '{0!r}').format(type(to_replace).__name__) raise TypeError(msg) # pragma: no cover if inplace: self._update_inplace(new_data) else: return self._constructor(new_data).__finalize__(self) _shared_docs['interpolate'] = """ Please note that only ``method='linear'`` is supported for DataFrames/Series with a MultiIndex. Parameters ---------- method : {'linear', 'time', 'index', 'values', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic', 'barycentric', 'krogh', 'polynomial', 'spline', 'piecewise_polynomial', 'from_derivatives', 'pchip', 'akima'} * 'linear': ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes. default * 'time': interpolation works on daily and higher resolution data to interpolate given length of interval * 'index', 'values': use the actual numerical values of the index * 'nearest', 'zero', 'slinear', 'quadratic', 'cubic', 'barycentric', 'polynomial' is passed to ``scipy.interpolate.interp1d``. Both 'polynomial' and 'spline' require that you also specify an `order` (int), e.g. df.interpolate(method='polynomial', order=4). These use the actual numerical values of the index. * 'krogh', 'piecewise_polynomial', 'spline', 'pchip' and 'akima' are all wrappers around the scipy interpolation methods of similar names. These use the actual numerical values of the index. See the scipy documentation for more on their behavior `here <http://docs.scipy.org/doc/scipy/reference/interpolate.html#univariate-interpolation>`__ # noqa `and here <http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html>`__ # noqa * 'from_derivatives' refers to BPoly.from_derivatives which replaces 'piecewise_polynomial' interpolation method in scipy 0.18 .. versionadded:: 0.18.1 Added support for the 'akima' method Added interpolate method 'from_derivatives' which replaces 'piecewise_polynomial' in scipy 0.18; backwards-compatible with scipy < 0.18 axis : {0, 1}, default 0 * 0: fill column-by-column * 1: fill row-by-row limit : int, default None. Maximum number of consecutive NaNs to fill. limit_direction : {'forward', 'backward', 'both'}, defaults to 'forward' If limit is specified, consecutive NaNs will be filled in this direction. .. versionadded:: 0.17.0 inplace : bool, default False Update the NDFrame in place if possible. downcast : optional, 'infer' or None, defaults to None Downcast dtypes if possible. kwargs : keyword arguments to pass on to the interpolating function. Returns ------- Series or DataFrame of same shape interpolated at the NaNs See Also -------- reindex, replace, fillna Examples -------- Filling in NaNs >>> s = pd.Series([0, 1, np.nan, 3]) >>> s.interpolate() 0 0 1 1 2 2 3 3 dtype: float64 """ @Appender(_shared_docs['interpolate'] % _shared_doc_kwargs) def interpolate(self, method='linear', axis=0, limit=None, inplace=False, limit_direction='forward', downcast=None, **kwargs): """ Interpolate values according to different methods. """ if self.ndim > 2: raise NotImplementedError("Interpolate has not been implemented " "on Panel and Panel 4D objects.") if axis == 0: ax = self._info_axis_name _maybe_transposed_self = self elif axis == 1: _maybe_transposed_self = self.T ax = 1 else: _maybe_transposed_self = self ax = _maybe_transposed_self._get_axis_number(ax) if _maybe_transposed_self.ndim == 2: alt_ax = 1 - ax else: alt_ax = ax if (isinstance(_maybe_transposed_self.index, MultiIndex) and method != 'linear'): raise ValueError("Only `method=linear` interpolation is supported " "on MultiIndexes.") if _maybe_transposed_self._data.get_dtype_counts().get( 'object') == len(_maybe_transposed_self.T): raise TypeError("Cannot interpolate with all NaNs.") # create/use the index if method == 'linear': # prior default index = np.arange(len(_maybe_transposed_self._get_axis(alt_ax))) else: index = _maybe_transposed_self._get_axis(alt_ax) if pd.isnull(index).any(): raise NotImplementedError("Interpolation with NaNs in the index " "has not been implemented. Try filling " "those NaNs before interpolating.") data = _maybe_transposed_self._data new_data = data.interpolate(method=method, axis=ax, index=index, values=_maybe_transposed_self, limit=limit, limit_direction=limit_direction, inplace=inplace, downcast=downcast, **kwargs) if inplace: if axis == 1: new_data = self._constructor(new_data).T._data self._update_inplace(new_data) else: res = self._constructor(new_data).__finalize__(self) if axis == 1: res = res.T return res # ---------------------------------------------------------------------- # Timeseries methods Methods def asof(self, where, subset=None): """ The last row without any NaN is taken (or the last row without NaN considering only the subset of columns in the case of a DataFrame) .. versionadded:: 0.19.0 For DataFrame If there is no good value, NaN is returned. Parameters ---------- where : date or array of dates subset : string or list of strings, default None if not None use these columns for NaN propagation Notes ----- Dates are assumed to be sorted Raises if this is not the case Returns ------- where is scalar - value or NaN if input is Series - Series if input is DataFrame where is Index: same shape object as input See Also -------- merge_asof """ if isinstance(where, compat.string_types): from pandas import to_datetime where = to_datetime(where) if not self.index.is_monotonic: raise ValueError("asof requires a sorted index") is_series = isinstance(self, ABCSeries) if is_series: if subset is not None: raise ValueError("subset is not valid for Series") elif self.ndim > 2: raise NotImplementedError("asof is not implemented " "for {type}".format(type(self))) else: if subset is None: subset = self.columns if not is_list_like(subset): subset = [subset] is_list = is_list_like(where) if not is_list: start = self.index[0] if isinstance(self.index, PeriodIndex): where = Period(where, freq=self.index.freq).ordinal start = start.ordinal if where < start: return np.nan # It's always much faster to use a *while* loop here for # Series than pre-computing all the NAs. However a # *while* loop is extremely expensive for DataFrame # so we later pre-compute all the NAs and use the same # code path whether *where* is a scalar or list. # See PR: https://github.com/pandas-dev/pandas/pull/14476 if is_series: loc = self.index.searchsorted(where, side='right') if loc > 0: loc -= 1 values = self._values while loc > 0 and isnull(values[loc]): loc -= 1 return values[loc] if not isinstance(where, Index): where = Index(where) if is_list else Index([where]) nulls = self.isnull() if is_series else self[subset].isnull().any(1) locs = self.index.asof_locs(where, ~(nulls.values)) # mask the missing missing = locs == -1 data = self.take(locs, is_copy=False) data.index = where data.loc[missing] = np.nan return data if is_list else data.iloc[-1] # ---------------------------------------------------------------------- # Action Methods _shared_docs['isnull'] = """ Return a boolean same-sized object indicating if the values are null. See Also -------- notnull : boolean inverse of isnull """ @Appender(_shared_docs['isnull']) def isnull(self): return isnull(self).__finalize__(self) _shared_docs['isnotnull'] = """ Return a boolean same-sized object indicating if the values are not null. See Also -------- isnull : boolean inverse of notnull """ @Appender(_shared_docs['isnotnull']) def notnull(self): return notnull(self).__finalize__(self) def clip(self, lower=None, upper=None, axis=None, *args, **kwargs): """ Trim values at input threshold(s). Parameters ---------- lower : float or array_like, default None upper : float or array_like, default None axis : int or string axis name, optional Align object with lower and upper along the given axis. Returns ------- clipped : Series Examples -------- >>> df 0 1 0 0.335232 -1.256177 1 -1.367855 0.746646 2 0.027753 -1.176076 3 0.230930 -0.679613 4 1.261967 0.570967 >>> df.clip(-1.0, 0.5) 0 1 0 0.335232 -1.000000 1 -1.000000 0.500000 2 0.027753 -1.000000 3 0.230930 -0.679613 4 0.500000 0.500000 >>> t 0 -0.3 1 -0.2 2 -0.1 3 0.0 4 0.1 dtype: float64 >>> df.clip(t, t + 1, axis=0) 0 1 0 0.335232 -0.300000 1 -0.200000 0.746646 2 0.027753 -0.100000 3 0.230930 0.000000 4 1.100000 0.570967 """ if isinstance(self, ABCPanel): raise NotImplementedError("clip is not supported yet for panels") axis = nv.validate_clip_with_axis(axis, args, kwargs) # GH 2747 (arguments were reversed) if lower is not None and upper is not None: if is_scalar(lower) and is_scalar(upper): lower, upper = min(lower, upper), max(lower, upper) result = self if lower is not None: result = result.clip_lower(lower, axis) if upper is not None: result = result.clip_upper(upper, axis) return result def clip_upper(self, threshold, axis=None): """ Return copy of input with values above given value(s) truncated. Parameters ---------- threshold : float or array_like axis : int or string axis name, optional Align object with threshold along the given axis. See Also -------- clip Returns ------- clipped : same type as input """ if np.any(isnull(threshold)): raise ValueError("Cannot use an NA value as a clip threshold") subset = self.le(threshold, axis=axis) | isnull(self) return self.where(subset, threshold, axis=axis) def clip_lower(self, threshold, axis=None): """ Return copy of the input with values below given value(s) truncated. Parameters ---------- threshold : float or array_like axis : int or string axis name, optional Align object with threshold along the given axis. See Also -------- clip Returns ------- clipped : same type as input """ if np.any(isnull(threshold)): raise ValueError("Cannot use an NA value as a clip threshold") subset = self.ge(threshold, axis=axis) | isnull(self) return self.where(subset, threshold, axis=axis) def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs): """ Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns. Parameters ---------- by : mapping function / list of functions, dict, Series, or tuple / list of column names. Called on each element of the object index to determine the groups. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups axis : int, default 0 level : int, level name, or sequence of such, default None If the axis is a MultiIndex (hierarchical), group by a particular level or levels as_index : boolean, default True For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively "SQL-style" grouped output sort : boolean, default True Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. groupby preserves the order of rows within each group. group_keys : boolean, default True When calling apply, add group keys to index to identify pieces squeeze : boolean, default False reduce the dimensionality of the return type if possible, otherwise return a consistent type Examples -------- DataFrame results >>> data.groupby(func, axis=0).mean() >>> data.groupby(['col1', 'col2'])['col3'].mean() DataFrame with hierarchical index >>> data.groupby(['col1', 'col2']).mean() Returns ------- GroupBy object """ from pandas.core.groupby import groupby if level is None and by is None: raise TypeError("You have to supply one of 'by' and 'level'") axis = self._get_axis_number(axis) return groupby(self, by=by, axis=axis, level=level, as_index=as_index, sort=sort, group_keys=group_keys, squeeze=squeeze, **kwargs) def asfreq(self, freq, method=None, how=None, normalize=False): """ Convert TimeSeries to specified frequency. Optionally provide filling method to pad/backfill missing values. Parameters ---------- freq : DateOffset object, or string method : {'backfill'/'bfill', 'pad'/'ffill'}, default None Method to use for filling holes in reindexed Series (note this does not fill NaNs that already were present): * 'pad' / 'ffill': propagate last valid observation forward to next valid * 'backfill' / 'bfill': use NEXT valid observation to fill how : {'start', 'end'}, default end For PeriodIndex only, see PeriodIndex.asfreq normalize : bool, default False Whether to reset output index to midnight Returns ------- converted : type of caller Notes ----- To learn more about the frequency strings, please see `this link <http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__. """ from pandas.tseries.resample import asfreq return asfreq(self, freq, method=method, how=how, normalize=normalize) def at_time(self, time, asof=False): """ Select values at particular time of day (e.g. 9:30AM). Parameters ---------- time : datetime.time or string Returns ------- values_at_time : type of caller """ try: indexer = self.index.indexer_at_time(time, asof=asof) return self.take(indexer, convert=False) except AttributeError: raise TypeError('Index must be DatetimeIndex') def between_time(self, start_time, end_time, include_start=True, include_end=True): """ Select values between particular times of the day (e.g., 9:00-9:30 AM). Parameters ---------- start_time : datetime.time or string end_time : datetime.time or string include_start : boolean, default True include_end : boolean, default True Returns ------- values_between_time : type of caller """ try: indexer = self.index.indexer_between_time( start_time, end_time, include_start=include_start, include_end=include_end) return self.take(indexer, convert=False) except AttributeError: raise TypeError('Index must be DatetimeIndex') def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0, on=None, level=None): """ Convenience method for frequency conversion and resampling of time series. Object must have a datetime-like index (DatetimeIndex, PeriodIndex, or TimedeltaIndex), or pass datetime-like values to the on or level keyword. Parameters ---------- rule : string the offset string or object representing target conversion axis : int, optional, default 0 closed : {'right', 'left'} Which side of bin interval is closed label : {'right', 'left'} Which bin edge label to label bucket with convention : {'start', 'end', 's', 'e'} loffset : timedelta Adjust the resampled time labels base : int, default 0 For frequencies that evenly subdivide 1 day, the "origin" of the aggregated intervals. For example, for '5min' frequency, base could range from 0 through 4. Defaults to 0 on : string, optional For a DataFrame, column to use instead of index for resampling. Column must be datetime-like. .. versionadded:: 0.19.0 level : string or int, optional For a MultiIndex, level (name or number) to use for resampling. Level must be datetime-like. .. versionadded:: 0.19.0 To learn more about the offset strings, please see `this link <http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__. Examples -------- Start by creating a series with 9 one minute timestamps. >>> index = pd.date_range('1/1/2000', periods=9, freq='T') >>> series = pd.Series(range(9), index=index) >>> series 2000-01-01 00:00:00 0 2000-01-01 00:01:00 1 2000-01-01 00:02:00 2 2000-01-01 00:03:00 3 2000-01-01 00:04:00 4 2000-01-01 00:05:00 5 2000-01-01 00:06:00 6 2000-01-01 00:07:00 7 2000-01-01 00:08:00 8 Freq: T, dtype: int64 Downsample the series into 3 minute bins and sum the values of the timestamps falling into a bin. >>> series.resample('3T').sum() 2000-01-01 00:00:00 3 2000-01-01 00:03:00 12 2000-01-01 00:06:00 21 Freq: 3T, dtype: int64 Downsample the series into 3 minute bins as above, but label each bin using the right edge instead of the left. Please note that the value in the bucket used as the label is not included in the bucket, which it labels. For example, in the original series the bucket ``2000-01-01 00:03:00`` contains the value 3, but the summed value in the resampled bucket with the label``2000-01-01 00:03:00`` does not include 3 (if it did, the summed value would be 6, not 3). To include this value close the right side of the bin interval as illustrated in the example below this one. >>> series.resample('3T', label='right').sum() 2000-01-01 00:03:00 3 2000-01-01 00:06:00 12 2000-01-01 00:09:00 21 Freq: 3T, dtype: int64 Downsample the series into 3 minute bins as above, but close the right side of the bin interval. >>> series.resample('3T', label='right', closed='right').sum() 2000-01-01 00:00:00 0 2000-01-01 00:03:00 6 2000-01-01 00:06:00 15 2000-01-01 00:09:00 15 Freq: 3T, dtype: int64 Upsample the series into 30 second bins. >>> series.resample('30S').asfreq()[0:5] #select first 5 rows 2000-01-01 00:00:00 0 2000-01-01 00:00:30 NaN 2000-01-01 00:01:00 1 2000-01-01 00:01:30 NaN 2000-01-01 00:02:00 2 Freq: 30S, dtype: float64 Upsample the series into 30 second bins and fill the ``NaN`` values using the ``pad`` method. >>> series.resample('30S').pad()[0:5] 2000-01-01 00:00:00 0 2000-01-01 00:00:30 0 2000-01-01 00:01:00 1 2000-01-01 00:01:30 1 2000-01-01 00:02:00 2 Freq: 30S, dtype: int64 Upsample the series into 30 second bins and fill the ``NaN`` values using the ``bfill`` method. >>> series.resample('30S').bfill()[0:5] 2000-01-01 00:00:00 0 2000-01-01 00:00:30 1 2000-01-01 00:01:00 1 2000-01-01 00:01:30 2 2000-01-01 00:02:00 2 Freq: 30S, dtype: int64 Pass a custom function via ``apply`` >>> def custom_resampler(array_like): ... return np.sum(array_like)+5 >>> series.resample('3T').apply(custom_resampler) 2000-01-01 00:00:00 8 2000-01-01 00:03:00 17 2000-01-01 00:06:00 26 Freq: 3T, dtype: int64 """ from pandas.tseries.resample import (resample, _maybe_process_deprecations) axis = self._get_axis_number(axis) r = resample(self, freq=rule, label=label, closed=closed, axis=axis, kind=kind, loffset=loffset, convention=convention, base=base, key=on, level=level) return _maybe_process_deprecations(r, how=how, fill_method=fill_method, limit=limit) def first(self, offset): """ Convenience method for subsetting initial periods of time series data based on a date offset. Parameters ---------- offset : string, DateOffset, dateutil.relativedelta Examples -------- ts.first('10D') -> First 10 days Returns ------- subset : type of caller """ from pandas.tseries.frequencies import to_offset if not isinstance(self.index, DatetimeIndex): raise NotImplementedError("'first' only supports a DatetimeIndex " "index") if len(self.index) == 0: return self offset = to_offset(offset) end_date = end = self.index[0] + offset # Tick-like, e.g. 3 weeks if not offset.isAnchored() and hasattr(offset, '_inc'): if end_date in self.index: end = self.index.searchsorted(end_date, side='left') return self.ix[:end] def last(self, offset): """ Convenience method for subsetting final periods of time series data based on a date offset. Parameters ---------- offset : string, DateOffset, dateutil.relativedelta Examples -------- ts.last('5M') -> Last 5 months Returns ------- subset : type of caller """ from pandas.tseries.frequencies import to_offset if not isinstance(self.index, DatetimeIndex): raise NotImplementedError("'last' only supports a DatetimeIndex " "index") if len(self.index) == 0: return self offset = to_offset(offset) start_date = start = self.index[-1] - offset start = self.index.searchsorted(start_date, side='right') return self.ix[start:] def rank(self, axis=0, method='average', numeric_only=None, na_option='keep', ascending=True, pct=False): """ Compute numerical data ranks (1 through n) along axis. Equal values are assigned a rank that is the average of the ranks of those values Parameters ---------- axis: {0 or 'index', 1 or 'columns'}, default 0 index to direct ranking method : {'average', 'min', 'max', 'first', 'dense'} * average: average rank of group * min: lowest rank in group * max: highest rank in group * first: ranks assigned in order they appear in the array * dense: like 'min', but rank always increases by 1 between groups numeric_only : boolean, default None Include only float, int, boolean data. Valid only for DataFrame or Panel objects na_option : {'keep', 'top', 'bottom'} * keep: leave NA values where they are * top: smallest rank if ascending * bottom: smallest rank if descending ascending : boolean, default True False for ranks by high (1) to low (N) pct : boolean, default False Computes percentage rank of data Returns ------- ranks : same type as caller """ axis = self._get_axis_number(axis) if self.ndim > 2: msg = "rank does not make sense when ndim > 2" raise NotImplementedError(msg) def ranker(data): ranks = algos.rank(data.values, axis=axis, method=method, ascending=ascending, na_option=na_option, pct=pct) ranks = self._constructor(ranks, **data._construct_axes_dict()) return ranks.__finalize__(self) # if numeric_only is None, and we can't get anything, we try with # numeric_only=True if numeric_only is None: try: return ranker(self) except TypeError: numeric_only = True if numeric_only: data = self._get_numeric_data() else: data = self return ranker(data) _shared_docs['align'] = (""" Align two object on their axes with the specified join method for each axis Index Parameters ---------- other : DataFrame or Series join : {'outer', 'inner', 'left', 'right'}, default 'outer' axis : allowed axis of the other object, default None Align on index (0), columns (1), or both (None) level : int or level name, default None Broadcast across a level, matching Index values on the passed MultiIndex level copy : boolean, default True Always returns new objects. If copy=False and no reindexing is required then original objects are returned. fill_value : scalar, default np.NaN Value to use for missing values. Defaults to NaN, but can be any "compatible" value method : str, default None limit : int, default None fill_axis : %(axes_single_arg)s, default 0 Filling axis, method and limit broadcast_axis : %(axes_single_arg)s, default None Broadcast values along this axis, if aligning two objects of different dimensions .. versionadded:: 0.17.0 Returns ------- (left, right) : (%(klass)s, type of other) Aligned objects """) @Appender(_shared_docs['align'] % _shared_doc_kwargs) def align(self, other, join='outer', axis=None, level=None, copy=True, fill_value=None, method=None, limit=None, fill_axis=0, broadcast_axis=None): from pandas import DataFrame, Series method = missing.clean_fill_method(method) if broadcast_axis == 1 and self.ndim != other.ndim: if isinstance(self, Series): # this means other is a DataFrame, and we need to broadcast # self cons = self._constructor_expanddim df = cons(dict((c, self) for c in other.columns), **other._construct_axes_dict()) return df._align_frame(other, join=join, axis=axis, level=level, copy=copy, fill_value=fill_value, method=method, limit=limit, fill_axis=fill_axis) elif isinstance(other, Series): # this means self is a DataFrame, and we need to broadcast # other cons = other._constructor_expanddim df = cons(dict((c, other) for c in self.columns), **self._construct_axes_dict()) return self._align_frame(df, join=join, axis=axis, level=level, copy=copy, fill_value=fill_value, method=method, limit=limit, fill_axis=fill_axis) if axis is not None: axis = self._get_axis_number(axis) if isinstance(other, DataFrame): return self._align_frame(other, join=join, axis=axis, level=level, copy=copy, fill_value=fill_value, method=method, limit=limit, fill_axis=fill_axis) elif isinstance(other, Series): return self._align_series(other, join=join, axis=axis, level=level, copy=copy, fill_value=fill_value, method=method, limit=limit, fill_axis=fill_axis) else: # pragma: no cover raise TypeError('unsupported type: %s' % type(other)) def _align_frame(self, other, join='outer', axis=None, level=None, copy=True, fill_value=np.nan, method=None, limit=None, fill_axis=0): # defaults join_index, join_columns = None, None ilidx, iridx = None, None clidx, cridx = None, None is_series = isinstance(self, ABCSeries) if axis is None or axis == 0: if not self.index.equals(other.index): join_index, ilidx, iridx = self.index.join( other.index, how=join, level=level, return_indexers=True) if axis is None or axis == 1: if not is_series and not self.columns.equals(other.columns): join_columns, clidx, cridx = self.columns.join( other.columns, how=join, level=level, return_indexers=True) if is_series: reindexers = {0: [join_index, ilidx]} else: reindexers = {0: [join_index, ilidx], 1: [join_columns, clidx]} left = self._reindex_with_indexers(reindexers, copy=copy, fill_value=fill_value, allow_dups=True) # other must be always DataFrame right = other._reindex_with_indexers({0: [join_index, iridx], 1: [join_columns, cridx]}, copy=copy, fill_value=fill_value, allow_dups=True) if method is not None: left = left.fillna(axis=fill_axis, method=method, limit=limit) right = right.fillna(axis=fill_axis, method=method, limit=limit) # if DatetimeIndex have different tz, convert to UTC if is_datetime64tz_dtype(left.index): if left.index.tz != right.index.tz: if join_index is not None: left.index = join_index right.index = join_index return left.__finalize__(self), right.__finalize__(other) def _align_series(self, other, join='outer', axis=None, level=None, copy=True, fill_value=None, method=None, limit=None, fill_axis=0): is_series = isinstance(self, ABCSeries) # series/series compat, other must always be a Series if is_series: if axis: raise ValueError('cannot align series to a series other than ' 'axis 0') # equal if self.index.equals(other.index): join_index, lidx, ridx = None, None, None else: join_index, lidx, ridx = self.index.join(other.index, how=join, level=level, return_indexers=True) left = self._reindex_indexer(join_index, lidx, copy) right = other._reindex_indexer(join_index, ridx, copy) else: # one has > 1 ndim fdata = self._data if axis == 0: join_index = self.index lidx, ridx = None, None if not self.index.equals(other.index): join_index, lidx, ridx = self.index.join( other.index, how=join, level=level, return_indexers=True) if lidx is not None: fdata = fdata.reindex_indexer(join_index, lidx, axis=1) elif axis == 1: join_index = self.columns lidx, ridx = None, None if not self.columns.equals(other.index): join_index, lidx, ridx = self.columns.join( other.index, how=join, level=level, return_indexers=True) if lidx is not None: fdata = fdata.reindex_indexer(join_index, lidx, axis=0) else: raise ValueError('Must specify axis=0 or 1') if copy and fdata is self._data: fdata = fdata.copy() left = self._constructor(fdata) if ridx is None: right = other else: right = other.reindex(join_index, level=level) # fill fill_na = notnull(fill_value) or (method is not None) if fill_na: left = left.fillna(fill_value, method=method, limit=limit, axis=fill_axis) right = right.fillna(fill_value, method=method, limit=limit) # if DatetimeIndex have different tz, convert to UTC if is_series or (not is_series and axis == 0): if is_datetime64tz_dtype(left.index): if left.index.tz != right.index.tz: if join_index is not None: left.index = join_index right.index = join_index return left.__finalize__(self), right.__finalize__(other) def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, try_cast=False, raise_on_error=True): """ Equivalent to public method `where`, except that `other` is not applied as a function even if callable. Used in __setitem__. """ cond = com._apply_if_callable(cond, self) if isinstance(cond, NDFrame): cond, _ = cond.align(self, join='right', broadcast_axis=1) else: if not hasattr(cond, 'shape'): raise ValueError('where requires an ndarray like object for ' 'its condition') if cond.shape != self.shape: raise ValueError('Array conditional must be same shape as ' 'self') cond = self._constructor(cond, **self._construct_axes_dict()) if inplace: cond = -(cond.fillna(True).astype(bool)) else: cond = cond.fillna(False).astype(bool) # try to align try_quick = True if hasattr(other, 'align'): # align with me if other.ndim <= self.ndim: _, other = self.align(other, join='left', axis=axis, level=level, fill_value=np.nan) # if we are NOT aligned, raise as we cannot where index if (axis is None and not all([other._get_axis(i).equals(ax) for i, ax in enumerate(self.axes)])): raise InvalidIndexError # slice me out of the other else: raise NotImplemented("cannot align with a higher dimensional " "NDFrame") elif is_list_like(other): if self.ndim == 1: # try to set the same dtype as ourselves try: new_other = np.array(other, dtype=self.dtype) except ValueError: new_other = np.array(other) except TypeError: new_other = other # we can end up comparing integers and m8[ns] # which is a numpy no no is_i8 = needs_i8_conversion(self.dtype) if is_i8: matches = False else: matches = (new_other == np.array(other)) if matches is False or not matches.all(): # coerce other to a common dtype if we can if needs_i8_conversion(self.dtype): try: other = np.array(other, dtype=self.dtype) except: other = np.array(other) else: other = np.asarray(other) other = np.asarray(other, dtype=np.common_type(other, new_other)) # we need to use the new dtype try_quick = False else: other = new_other else: other = np.array(other) if isinstance(other, np.ndarray): if other.shape != self.shape: if self.ndim == 1: icond = cond.values # GH 2745 / GH 4192 # treat like a scalar if len(other) == 1: other = np.array(other[0]) # GH 3235 # match True cond to other elif len(cond[icond]) == len(other): # try to not change dtype at first (if try_quick) if try_quick: try: new_other = _values_from_object(self).copy() new_other[icond] = other other = new_other except: try_quick = False # let's create a new (if we failed at the above # or not try_quick if not try_quick: dtype, fill_value = _maybe_promote(other.dtype) new_other = np.empty(len(icond), dtype=dtype) new_other.fill(fill_value) _maybe_upcast_putmask(new_other, icond, other) other = new_other else: raise ValueError('Length of replacements must equal ' 'series length') else: raise ValueError('other must be the same shape as self ' 'when an ndarray') # we are the same shape, so create an actual object for alignment else: other = self._constructor(other, **self._construct_axes_dict()) if axis is None: axis = 0 if self.ndim == getattr(other, 'ndim', 0): align = True else: align = (self._get_axis_number(axis) == 1) block_axis = self._get_block_manager_axis(axis) if inplace: # we may have different type blocks come out of putmask, so # reconstruct the block manager self._check_inplace_setting(other) new_data = self._data.putmask(mask=cond, new=other, align=align, inplace=True, axis=block_axis, transpose=self._AXIS_REVERSED) self._update_inplace(new_data) else: new_data = self._data.where(other=other, cond=cond, align=align, raise_on_error=raise_on_error, try_cast=try_cast, axis=block_axis, transpose=self._AXIS_REVERSED) return self._constructor(new_data).__finalize__(self) _shared_docs['where'] = (""" Return an object of same shape as self and whose corresponding entries are from self where cond is %(cond)s and otherwise are from other. Parameters ---------- cond : boolean %(klass)s, array or callable If cond is callable, it is computed on the %(klass)s and should return boolean %(klass)s or array. The callable must not change input %(klass)s (though pandas doesn't check it). .. versionadded:: 0.18.1 A callable can be used as cond. other : scalar, %(klass)s, or callable If other is callable, it is computed on the %(klass)s and should return scalar or %(klass)s. The callable must not change input %(klass)s (though pandas doesn't check it). .. versionadded:: 0.18.1 A callable can be used as other. inplace : boolean, default False Whether to perform the operation in place on the data axis : alignment axis if needed, default None level : alignment level if needed, default None try_cast : boolean, default False try to cast the result back to the input type (if possible), raise_on_error : boolean, default True Whether to raise on invalid data types (e.g. trying to where on strings) Returns ------- wh : same type as caller Notes ----- The %(name)s method is an application of the if-then idiom. For each element in the calling DataFrame, if ``cond`` is ``%(cond)s`` the element is used; otherwise the corresponding element from the DataFrame ``other`` is used. The signature for :func:`DataFrame.where` differs from :func:`numpy.where`. Roughly ``df1.where(m, df2)`` is equivalent to ``np.where(m, df1, df2)``. For further details and examples see the ``%(name)s`` documentation in :ref:`indexing <indexing.where_mask>`. Examples -------- >>> s = pd.Series(range(5)) >>> s.where(s > 0) 0 NaN 1 1.0 2 2.0 3 3.0 4 4.0 >>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B']) >>> m = df %% 3 == 0 >>> df.where(m, -df) A B 0 0 -1 1 -2 3 2 -4 -5 3 6 -7 4 -8 9 >>> df.where(m, -df) == np.where(m, df, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True >>> df.where(m, -df) == df.mask(~m, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True See Also -------- :func:`DataFrame.%(name_other)s` """) @Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True", name='where', name_other='mask')) def where(self, cond, other=np.nan, inplace=False, axis=None, level=None, try_cast=False, raise_on_error=True): other = com._apply_if_callable(other, self) return self._where(cond, other, inplace, axis, level, try_cast, raise_on_error) @Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False", name='mask', name_other='where')) def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, try_cast=False, raise_on_error=True): cond = com._apply_if_callable(cond, self) return self.where(~cond, other=other, inplace=inplace, axis=axis, level=level, try_cast=try_cast, raise_on_error=raise_on_error) _shared_docs['shift'] = (""" Shift index by desired number of periods with an optional time freq Parameters ---------- periods : int Number of periods to move, can be positive or negative freq : DateOffset, timedelta, or time rule string, optional Increment to use from the tseries module or time rule (e.g. 'EOM'). See Notes. axis : %(axes_single_arg)s Notes ----- If freq is specified then the index values are shifted but the data is not realigned. That is, use freq if you would like to extend the index when shifting and preserve the original data. Returns ------- shifted : %(klass)s """) @Appender(_shared_docs['shift'] % _shared_doc_kwargs) def shift(self, periods=1, freq=None, axis=0): if periods == 0: return self block_axis = self._get_block_manager_axis(axis) if freq is None: new_data = self._data.shift(periods=periods, axis=block_axis) else: return self.tshift(periods, freq) return self._constructor(new_data).__finalize__(self) def slice_shift(self, periods=1, axis=0): """ Equivalent to `shift` without copying data. The shifted data will not include the dropped periods and the shifted axis will be smaller than the original. Parameters ---------- periods : int Number of periods to move, can be positive or negative Notes ----- While the `slice_shift` is faster than `shift`, you may pay for it later during alignment. Returns ------- shifted : same type as caller """ if periods == 0: return self if periods > 0: vslicer = slice(None, -periods) islicer = slice(periods, None) else: vslicer = slice(-periods, None) islicer = slice(None, periods) new_obj = self._slice(vslicer, axis=axis) shifted_axis = self._get_axis(axis)[islicer] new_obj.set_axis(axis, shifted_axis) return new_obj.__finalize__(self) def tshift(self, periods=1, freq=None, axis=0): """ Shift the time index, using the index's frequency if available. Parameters ---------- periods : int Number of periods to move, can be positive or negative freq : DateOffset, timedelta, or time rule string, default None Increment to use from the tseries module or time rule (e.g. 'EOM') axis : int or basestring Corresponds to the axis that contains the Index Notes ----- If freq is not specified then tries to use the freq or inferred_freq attributes of the index. If neither of those attributes exist, a ValueError is thrown Returns ------- shifted : NDFrame """ index = self._get_axis(axis) if freq is None: freq = getattr(index, 'freq', None) if freq is None: freq = getattr(index, 'inferred_freq', None) if freq is None: msg = 'Freq was not given and was not set in the index' raise ValueError(msg) if periods == 0: return self if isinstance(freq, string_types): freq = to_offset(freq) block_axis = self._get_block_manager_axis(axis) if isinstance(index, PeriodIndex): orig_freq = to_offset(index.freq) if freq == orig_freq: new_data = self._data.copy() new_data.axes[block_axis] = index.shift(periods) else: msg = ('Given freq %s does not match PeriodIndex freq %s' % (freq.rule_code, orig_freq.rule_code)) raise ValueError(msg) else: new_data = self._data.copy() new_data.axes[block_axis] = index.shift(periods, freq) return self._constructor(new_data).__finalize__(self) def truncate(self, before=None, after=None, axis=None, copy=True): """Truncates a sorted NDFrame before and/or after some particular index value. If the axis contains only datetime values, before/after parameters are converted to datetime values. Parameters ---------- before : date Truncate before index value after : date Truncate after index value axis : the truncation axis, defaults to the stat axis copy : boolean, default is True, return a copy of the truncated section Returns ------- truncated : type of caller """ if axis is None: axis = self._stat_axis_number axis = self._get_axis_number(axis) ax = self._get_axis(axis) # if we have a date index, convert to dates, otherwise # treat like a slice if ax.is_all_dates: from pandas.tseries.tools import to_datetime before = to_datetime(before) after = to_datetime(after) if before is not None and after is not None: if before > after: raise ValueError('Truncate: %s must be after %s' % (after, before)) slicer = [slice(None, None)] * self._AXIS_LEN slicer[axis] = slice(before, after) result = self.ix[tuple(slicer)] if isinstance(ax, MultiIndex): setattr(result, self._get_axis_name(axis), ax.truncate(before, after)) if copy: result = result.copy() return result def tz_convert(self, tz, axis=0, level=None, copy=True): """ Convert tz-aware axis to target time zone. Parameters ---------- tz : string or pytz.timezone object axis : the axis to convert level : int, str, default None If axis ia a MultiIndex, convert a specific level. Otherwise must be None copy : boolean, default True Also make a copy of the underlying data Returns ------- Raises ------ TypeError If the axis is tz-naive. """ axis = self._get_axis_number(axis) ax = self._get_axis(axis) def _tz_convert(ax, tz): if not hasattr(ax, 'tz_convert'): if len(ax) > 0: ax_name = self._get_axis_name(axis) raise TypeError('%s is not a valid DatetimeIndex or ' 'PeriodIndex' % ax_name) else: ax = DatetimeIndex([], tz=tz) else: ax = ax.tz_convert(tz) return ax # if a level is given it must be a MultiIndex level or # equivalent to the axis name if isinstance(ax, MultiIndex): level = ax._get_level_number(level) new_level = _tz_convert(ax.levels[level], tz) ax = ax.set_levels(new_level, level=level) else: if level not in (None, 0, ax.name): raise ValueError("The level {0} is not valid".format(level)) ax = _tz_convert(ax, tz) result = self._constructor(self._data, copy=copy) result.set_axis(axis, ax) return result.__finalize__(self) @deprecate_kwarg(old_arg_name='infer_dst', new_arg_name='ambiguous', mapping={True: 'infer', False: 'raise'}) def tz_localize(self, tz, axis=0, level=None, copy=True, ambiguous='raise'): """ Localize tz-naive TimeSeries to target time zone. Parameters ---------- tz : string or pytz.timezone object axis : the axis to localize level : int, str, default None If axis ia a MultiIndex, localize a specific level. Otherwise must be None copy : boolean, default True Also make a copy of the underlying data ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' - 'infer' will attempt to infer fall dst-transition hours based on order - bool-ndarray where True signifies a DST time, False designates a non-DST time (note that this flag is only applicable for ambiguous times) - 'NaT' will return NaT where there are ambiguous times - 'raise' will raise an AmbiguousTimeError if there are ambiguous times infer_dst : boolean, default False (DEPRECATED) Attempt to infer fall dst-transition hours based on order Returns ------- Raises ------ TypeError If the TimeSeries is tz-aware and tz is not None. """ axis = self._get_axis_number(axis) ax = self._get_axis(axis) def _tz_localize(ax, tz, ambiguous): if not hasattr(ax, 'tz_localize'): if len(ax) > 0: ax_name = self._get_axis_name(axis) raise TypeError('%s is not a valid DatetimeIndex or ' 'PeriodIndex' % ax_name) else: ax = DatetimeIndex([], tz=tz) else: ax = ax.tz_localize(tz, ambiguous=ambiguous) return ax # if a level is given it must be a MultiIndex level or # equivalent to the axis name if isinstance(ax, MultiIndex): level = ax._get_level_number(level) new_level = _tz_localize(ax.levels[level], tz, ambiguous) ax = ax.set_levels(new_level, level=level) else: if level not in (None, 0, ax.name): raise ValueError("The level {0} is not valid".format(level)) ax = _tz_localize(ax, tz, ambiguous) result = self._constructor(self._data, copy=copy) result.set_axis(axis, ax) return result.__finalize__(self) # ---------------------------------------------------------------------- # Numeric Methods def abs(self): """ Return an object with absolute value taken--only applicable to objects that are all numeric. Returns ------- abs: type of caller """ return np.abs(self) _shared_docs['describe'] = """ Generate various summary statistics, excluding NaN values. Parameters ---------- percentiles : array-like, optional The percentiles to include in the output. Should all be in the interval [0, 1]. By default `percentiles` is [.25, .5, .75], returning the 25th, 50th, and 75th percentiles. include, exclude : list-like, 'all', or None (default) Specify the form of the returned result. Either: - None to both (default). The result will include only numeric-typed columns or, if none are, only categorical columns. - A list of dtypes or strings to be included/excluded. To select all numeric types use numpy numpy.number. To select categorical objects use type object. See also the select_dtypes documentation. eg. df.describe(include=['O']) - If include is the string 'all', the output column-set will match the input one. Returns ------- summary: %(klass)s of summary statistics Notes ----- The output DataFrame index depends on the requested dtypes: For numeric dtypes, it will include: count, mean, std, min, max, and lower, 50, and upper percentiles. For object dtypes (e.g. timestamps or strings), the index will include the count, unique, most common, and frequency of the most common. Timestamps also include the first and last items. For mixed dtypes, the index will be the union of the corresponding output types. Non-applicable entries will be filled with NaN. Note that mixed-dtype outputs can only be returned from mixed-dtype inputs and appropriate use of the include/exclude arguments. If multiple values have the highest count, then the `count` and `most common` pair will be arbitrarily chosen from among those with the highest count. The include, exclude arguments are ignored for Series. See Also -------- DataFrame.select_dtypes """ @Appender(_shared_docs['describe'] % _shared_doc_kwargs) def describe(self, percentiles=None, include=None, exclude=None): if self.ndim >= 3: msg = "describe is not implemented on Panel or PanelND objects." raise NotImplementedError(msg) elif self.ndim == 2 and self.columns.size == 0: raise ValueError("Cannot describe a DataFrame without columns") if percentiles is not None: # get them all to be in [0, 1] self._check_percentile(percentiles) # median should always be included if 0.5 not in percentiles: percentiles.append(0.5) percentiles = np.asarray(percentiles) else: percentiles = np.array([0.25, 0.5, 0.75]) # sort and check for duplicates unique_pcts = np.unique(percentiles) if len(unique_pcts) < len(percentiles): raise ValueError("percentiles cannot contain duplicates") percentiles = unique_pcts formatted_percentiles = format_percentiles(percentiles) def describe_numeric_1d(series): stat_index = (['count', 'mean', 'std', 'min'] + formatted_percentiles + ['max']) d = ([series.count(), series.mean(), series.std(), series.min()] + [series.quantile(x) for x in percentiles] + [series.max()]) return pd.Series(d, index=stat_index, name=series.name) def describe_categorical_1d(data): names = ['count', 'unique'] objcounts = data.value_counts() count_unique = len(objcounts[objcounts != 0]) result = [data.count(), count_unique] if result[1] > 0: top, freq = objcounts.index[0], objcounts.iloc[0] if is_datetime64_dtype(data): asint = data.dropna().values.view('i8') names += ['top', 'freq', 'first', 'last'] result += [lib.Timestamp(top), freq, lib.Timestamp(asint.min()), lib.Timestamp(asint.max())] else: names += ['top', 'freq'] result += [top, freq] return pd.Series(result, index=names, name=data.name) def describe_1d(data): if is_bool_dtype(data): return describe_categorical_1d(data) elif is_numeric_dtype(data): return describe_numeric_1d(data) elif is_timedelta64_dtype(data): return describe_numeric_1d(data) else: return describe_categorical_1d(data) if self.ndim == 1: return describe_1d(self) elif (include is None) and (exclude is None): # when some numerics are found, keep only numerics data = self.select_dtypes(include=[np.number]) if len(data.columns) == 0: data = self elif include == 'all': if exclude is not None: msg = "exclude must be None when include is 'all'" raise ValueError(msg) data = self else: data = self.select_dtypes(include=include, exclude=exclude) ldesc = [describe_1d(s) for _, s in data.iteritems()] # set a convenient order for rows names = [] ldesc_indexes = sorted([x.index for x in ldesc], key=len) for idxnames in ldesc_indexes: for name in idxnames: if name not in names: names.append(name) d = pd.concat(ldesc, join_axes=pd.Index([names]), axis=1) d.columns = data.columns.copy() return d def _check_percentile(self, q): """Validate percentiles (used by describe and quantile).""" msg = ("percentiles should all be in the interval [0, 1]. " "Try {0} instead.") q = np.asarray(q) if q.ndim == 0: if not 0 <= q <= 1: raise ValueError(msg.format(q / 100.0)) else: if not all(0 <= qs <= 1 for qs in q): raise ValueError(msg.format(q / 100.0)) return q _shared_docs['pct_change'] = """ Percent change over given number of periods. Parameters ---------- periods : int, default 1 Periods to shift for forming percent change fill_method : str, default 'pad' How to handle NAs before computing percent changes limit : int, default None The number of consecutive NAs to fill before stopping freq : DateOffset, timedelta, or offset alias string, optional Increment to use from time series API (e.g. 'M' or BDay()) Returns ------- chg : %(klass)s Notes ----- By default, the percentage change is calculated along the stat axis: 0, or ``Index``, for ``DataFrame`` and 1, or ``minor`` for ``Panel``. You can change this with the ``axis`` keyword argument. """ @Appender(_shared_docs['pct_change'] % _shared_doc_kwargs) def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None, **kwargs): # TODO: Not sure if above is correct - need someone to confirm. axis = self._get_axis_number(kwargs.pop('axis', self._stat_axis_name)) if fill_method is None: data = self else: data = self.fillna(method=fill_method, limit=limit, axis=axis) rs = (data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1) if freq is None: mask = isnull(_values_from_object(self)) np.putmask(rs.values, mask, np.nan) return rs def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs): grouped = self.groupby(level=level, axis=axis) if hasattr(grouped, name) and skipna: return getattr(grouped, name)(**kwargs) axis = self._get_axis_number(axis) method = getattr(type(self), name) applyf = lambda x: method(x, axis=axis, skipna=skipna, **kwargs) return grouped.aggregate(applyf) @classmethod def _add_numeric_operations(cls): """Add the operations to the cls; evaluate the doc strings again""" axis_descr, name, name2 = _doc_parms(cls) cls.any = _make_logical_function( cls, 'any', name, name2, axis_descr, 'Return whether any element is True over requested axis', nanops.nanany) cls.all = _make_logical_function( cls, 'all', name, name2, axis_descr, 'Return whether all elements are True over requested axis', nanops.nanall) @Substitution(outname='mad', desc="Return the mean absolute deviation of the values " "for the requested axis", name1=name, name2=name2, axis_descr=axis_descr) @Appender(_num_doc) def mad(self, axis=None, skipna=None, level=None): if skipna is None: skipna = True if axis is None: axis = self._stat_axis_number if level is not None: return self._agg_by_level('mad', axis=axis, level=level, skipna=skipna) data = self._get_numeric_data() if axis == 0: demeaned = data - data.mean(axis=0) else: demeaned = data.sub(data.mean(axis=1), axis=0) return np.abs(demeaned).mean(axis=axis, skipna=skipna) cls.mad = mad cls.sem = _make_stat_function_ddof( cls, 'sem', name, name2, axis_descr, "Return unbiased standard error of the mean over requested " "axis.\n\nNormalized by N-1 by default. This can be changed " "using the ddof argument", nanops.nansem) cls.var = _make_stat_function_ddof( cls, 'var', name, name2, axis_descr, "Return unbiased variance over requested axis.\n\nNormalized by " "N-1 by default. This can be changed using the ddof argument", nanops.nanvar) cls.std = _make_stat_function_ddof( cls, 'std', name, name2, axis_descr, "Return sample standard deviation over requested axis." "\n\nNormalized by N-1 by default. This can be changed using the " "ddof argument", nanops.nanstd) @Substitution(outname='compounded', desc="Return the compound percentage of the values for " "the requested axis", name1=name, name2=name2, axis_descr=axis_descr) @Appender(_num_doc) def compound(self, axis=None, skipna=None, level=None): if skipna is None: skipna = True return (1 + self).prod(axis=axis, skipna=skipna, level=level) - 1 cls.compound = compound cls.cummin = _make_cum_function( cls, 'cummin', name, name2, axis_descr, "cumulative minimum", lambda y, axis: np.minimum.accumulate(y, axis), np.inf, np.nan) cls.cumsum = _make_cum_function( cls, 'cumsum', name, name2, axis_descr, "cumulative sum", lambda y, axis: y.cumsum(axis), 0., np.nan) cls.cumprod = _make_cum_function( cls, 'cumprod', name, name2, axis_descr, "cumulative product", lambda y, axis: y.cumprod(axis), 1., np.nan) cls.cummax = _make_cum_function( cls, 'cummax', name, name2, axis_descr, "cumulative max", lambda y, axis: np.maximum.accumulate(y, axis), -np.inf, np.nan) cls.sum = _make_stat_function( cls, 'sum', name, name2, axis_descr, 'Return the sum of the values for the requested axis', nanops.nansum) cls.mean = _make_stat_function( cls, 'mean', name, name2, axis_descr, 'Return the mean of the values for the requested axis', nanops.nanmean) cls.skew = _make_stat_function( cls, 'skew', name, name2, axis_descr, 'Return unbiased skew over requested axis\nNormalized by N-1', nanops.nanskew) cls.kurt = _make_stat_function( cls, 'kurt', name, name2, axis_descr, "Return unbiased kurtosis over requested axis using Fisher's " "definition of\nkurtosis (kurtosis of normal == 0.0). Normalized " "by N-1\n", nanops.nankurt) cls.kurtosis = cls.kurt cls.prod = _make_stat_function( cls, 'prod', name, name2, axis_descr, 'Return the product of the values for the requested axis', nanops.nanprod) cls.product = cls.prod cls.median = _make_stat_function( cls, 'median', name, name2, axis_descr, 'Return the median of the values for the requested axis', nanops.nanmedian) cls.max = _make_stat_function( cls, 'max', name, name2, axis_descr, """This method returns the maximum of the values in the object. If you want the *index* of the maximum, use ``idxmax``. This is the equivalent of the ``numpy.ndarray`` method ``argmax``.""", nanops.nanmax) cls.min = _make_stat_function( cls, 'min', name, name2, axis_descr, """This method returns the minimum of the values in the object. If you want the *index* of the minimum, use ``idxmin``. This is the equivalent of the ``numpy.ndarray`` method ``argmin``.""", nanops.nanmin) @classmethod def _add_series_only_operations(cls): """Add the series only operations to the cls; evaluate the doc strings again. """ axis_descr, name, name2 = _doc_parms(cls) def nanptp(values, axis=0, skipna=True): nmax = nanops.nanmax(values, axis, skipna) nmin = nanops.nanmin(values, axis, skipna) return nmax - nmin cls.ptp = _make_stat_function( cls, 'ptp', name, name2, axis_descr, """Returns the difference between the maximum value and the minimum value in the object. This is the equivalent of the ``numpy.ndarray`` method ``ptp``.""", nanptp) @classmethod def _add_series_or_dataframe_operations(cls): """Add the series or dataframe only operations to the cls; evaluate the doc strings again. """ from pandas.core import window as rwindow @Appender(rwindow.rolling.__doc__) def rolling(self, window, min_periods=None, freq=None, center=False, win_type=None, on=None, axis=0): axis = self._get_axis_number(axis) return rwindow.rolling(self, window=window, min_periods=min_periods, freq=freq, center=center, win_type=win_type, on=on, axis=axis) cls.rolling = rolling @Appender(rwindow.expanding.__doc__) def expanding(self, min_periods=1, freq=None, center=False, axis=0): axis = self._get_axis_number(axis) return rwindow.expanding(self, min_periods=min_periods, freq=freq, center=center, axis=axis) cls.expanding = expanding @Appender(rwindow.ewm.__doc__) def ewm(self, com=None, span=None, halflife=None, alpha=None, min_periods=0, freq=None, adjust=True, ignore_na=False, axis=0): axis = self._get_axis_number(axis) return rwindow.ewm(self, com=com, span=span, halflife=halflife, alpha=alpha, min_periods=min_periods, freq=freq, adjust=adjust, ignore_na=ignore_na, axis=axis) cls.ewm = ewm def _doc_parms(cls): """Return a tuple of the doc parms.""" axis_descr = "{%s}" % ', '.join(["{0} ({1})".format(a, i) for i, a in enumerate(cls._AXIS_ORDERS)]) name = (cls._constructor_sliced.__name__ if cls._AXIS_LEN > 1 else 'scalar') name2 = cls.__name__ return axis_descr, name, name2 _num_doc = """ %(desc)s Parameters ---------- axis : %(axis_descr)s skipna : boolean, default True Exclude NA/null values. If an entire row/column is NA, the result will be NA level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a %(name1)s numeric_only : boolean, default None Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. Returns ------- %(outname)s : %(name1)s or %(name2)s (if level specified)\n""" _num_ddof_doc = """ %(desc)s Parameters ---------- axis : %(axis_descr)s skipna : boolean, default True Exclude NA/null values. If an entire row/column is NA, the result will be NA level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a %(name1)s ddof : int, default 1 degrees of freedom numeric_only : boolean, default None Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. Returns ------- %(outname)s : %(name1)s or %(name2)s (if level specified)\n""" _bool_doc = """ %(desc)s Parameters ---------- axis : %(axis_descr)s skipna : boolean, default True Exclude NA/null values. If an entire row/column is NA, the result will be NA level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a %(name1)s bool_only : boolean, default None Include only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series. Returns ------- %(outname)s : %(name1)s or %(name2)s (if level specified)\n""" _cnum_doc = """ Parameters ---------- axis : %(axis_descr)s skipna : boolean, default True Exclude NA/null values. If an entire row/column is NA, the result will be NA Returns ------- %(outname)s : %(name1)s\n""" def _make_stat_function(cls, name, name1, name2, axis_descr, desc, f): @Substitution(outname=name, desc=desc, name1=name1, name2=name2, axis_descr=axis_descr) @Appender(_num_doc) def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs): nv.validate_stat_func(tuple(), kwargs, fname=name) if skipna is None: skipna = True if axis is None: axis = self._stat_axis_number if level is not None: return self._agg_by_level(name, axis=axis, level=level, skipna=skipna) return self._reduce(f, name, axis=axis, skipna=skipna, numeric_only=numeric_only) return set_function_name(stat_func, name, cls) def _make_stat_function_ddof(cls, name, name1, name2, axis_descr, desc, f): @Substitution(outname=name, desc=desc, name1=name1, name2=name2, axis_descr=axis_descr) @Appender(_num_ddof_doc) def stat_func(self, axis=None, skipna=None, level=None, ddof=1, numeric_only=None, **kwargs): nv.validate_stat_ddof_func(tuple(), kwargs, fname=name) if skipna is None: skipna = True if axis is None: axis = self._stat_axis_number if level is not None: return self._agg_by_level(name, axis=axis, level=level, skipna=skipna, ddof=ddof) return self._reduce(f, name, axis=axis, numeric_only=numeric_only, skipna=skipna, ddof=ddof) return set_function_name(stat_func, name, cls) def _make_cum_function(cls, name, name1, name2, axis_descr, desc, accum_func, mask_a, mask_b): @Substitution(outname=name, desc=desc, name1=name1, name2=name2, axis_descr=axis_descr) @Appender("Return {0} over requested axis.".format(desc) + _cnum_doc) def cum_func(self, axis=None, skipna=True, *args, **kwargs): skipna = nv.validate_cum_func_with_skipna(skipna, args, kwargs, name) if axis is None: axis = self._stat_axis_number else: axis = self._get_axis_number(axis) y = _values_from_object(self).copy() if (skipna and issubclass(y.dtype.type, (np.datetime64, np.timedelta64))): result = accum_func(y, axis) mask = isnull(self) np.putmask(result, mask, pd.tslib.iNaT) elif skipna and not issubclass(y.dtype.type, (np.integer, np.bool_)): mask = isnull(self) np.putmask(y, mask, mask_a) result = accum_func(y, axis) np.putmask(result, mask, mask_b) else: result = accum_func(y, axis) d = self._construct_axes_dict() d['copy'] = False return self._constructor(result, **d).__finalize__(self) return set_function_name(cum_func, name, cls) def _make_logical_function(cls, name, name1, name2, axis_descr, desc, f): @Substitution(outname=name, desc=desc, name1=name1, name2=name2, axis_descr=axis_descr) @Appender(_bool_doc) def logical_func(self, axis=None, bool_only=None, skipna=None, level=None, **kwargs): nv.validate_logical_func(tuple(), kwargs, fname=name) if skipna is None: skipna = True if axis is None: axis = self._stat_axis_number if level is not None: if bool_only is not None: raise NotImplementedError("Option bool_only is not " "implemented with option level.") return self._agg_by_level(name, axis=axis, level=level, skipna=skipna) return self._reduce(f, axis=axis, skipna=skipna, numeric_only=bool_only, filter_type='bool', name=name) return set_function_name(logical_func, name, cls) # install the indexes for _name, _indexer in indexing.get_indexers_list(): NDFrame._create_indexer(_name, _indexer)
mit
lenovor/scikit-learn
examples/applications/topics_extraction_with_nmf_lda.py
133
3517
""" ======================================================================================== Topics extraction with Non-Negative Matrix Factorization And Latent Dirichlet Allocation ======================================================================================== This is an example of applying Non Negative Matrix Factorization and Latent Dirichlet Allocation on a corpus of documents and extract additive models of the topic structure of the corpus. The output is a list of topics, each represented as a list of terms (weights are not shown). The default parameters (n_samples / n_features / n_topics) should make the example runnable in a couple of tens of seconds. You can try to increase the dimensions of the problem, but be aware that the time complexity is polynomial in NMF. In LDA, the time complexity is proportional to (n_samples * iterations). """ # Author: Olivier Grisel <olivier.grisel@ensta.org> # Lars Buitinck <L.J.Buitinck@uva.nl> # Chyi-Kwei Yau <chyikwei.yau@gmail.com> # License: BSD 3 clause from __future__ import print_function from time import time from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer from sklearn.decomposition import NMF, LatentDirichletAllocation from sklearn.datasets import fetch_20newsgroups n_samples = 2000 n_features = 1000 n_topics = 10 n_top_words = 20 def print_top_words(model, feature_names, n_top_words): for topic_idx, topic in enumerate(model.components_): print("Topic #%d:" % topic_idx) print(" ".join([feature_names[i] for i in topic.argsort()[:-n_top_words - 1:-1]])) print() # Load the 20 newsgroups dataset and vectorize it. We use a few heuristics # to filter out useless terms early on: the posts are stripped of headers, # footers and quoted replies, and common English words, words occurring in # only one document or in at least 95% of the documents are removed. t0 = time() print("Loading dataset and extracting features...") dataset = fetch_20newsgroups(shuffle=True, random_state=1, remove=('headers', 'footers', 'quotes')) data_samples = dataset.data[:n_samples] # use tf-idf feature for NMF model tfidf_vectorizer = TfidfVectorizer(max_df=0.95, min_df=2, max_features=n_features, stop_words='english') tfidf = tfidf_vectorizer.fit_transform(data_samples) # use tf feature for LDA model tf_vectorizer = CountVectorizer(max_df=0.95, min_df=2, max_features=n_features, stop_words='english') tf = tf_vectorizer.fit_transform(data_samples) print("done in %0.3fs." % (time() - t0)) # Fit the NMF model print("Fitting the NMF model with tf-idf feature, n_samples=%d and n_features=%d..." % (n_samples, n_features)) nmf = NMF(n_components=n_topics, random_state=1).fit(tfidf) print("done in %0.3fs." % (time() - t0)) print("\nTopics in NMF model:") tfidf_feature_names = tfidf_vectorizer.get_feature_names() print_top_words(nmf, tfidf_feature_names, n_top_words) print("\nFitting LDA models with tf feature, n_samples=%d and n_features=%d..." % (n_samples, n_features)) lda = LatentDirichletAllocation(n_topics=n_topics, max_iter=5, learning_method='online', learning_offset=50., random_state=0) lda.fit(tf) print("done in %0.3fs." % (time() - t0)) print("\nTopics in LDA model:") tf_feature_names = tf_vectorizer.get_feature_names() print_top_words(lda, tf_feature_names, n_top_words)
bsd-3-clause
shnizzedy/SM_openSMILE
openSMILE_analysis/pca.py
1
3971
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ pca.py Run PCA analyses on [n_participants × n_files × n_features × n_Dxs] *.csv file from openSMILE_csv.py Authors: – Jon Clucas, 2017 (jon.clucas@childmind.org) @author: jon.clucas """ from sklearn.decomposition import FactorAnalysis, PCA from sklearn.preprocessing import LabelEncoder import ast, csv, numpy as np, os, sys if os.path.abspath('../../') not in sys.path: if os.path.isdir(os.path.join(os.path.abspath('../..'), 'SM_openSMILE')): sys.path.append(os.path.abspath('../..')) elif os.path.isdir(os.path.join(os.path.abspath('..'), 'SM_openSMILE')): sys.path.append(os.path.abspath('..')) elif os.path.isdir('SM_openSMILE'): sys.path.append(os.path.abspath('.')) from itertools import zip_longest from SM_openSMILE.cfg import conditions, configs, oSdir, replacements def main(): for cfg in configs: labels = [] with open(os.path.abspath(os.path.join(os.path.dirname(os.path.dirname( oSdir)), 'configs', cfg, ''.join([cfg, '_features.csv']))), 'r') as lp: lreader = csv.reader(lp) for row in lreader: labels.append(row[1]) np_labels = np.array(labels) for replacement in replacements: for condition in conditions: print(cfg, end=" : \n\t") print(replacement, end=" : ") print(condition.strip('_')) pca = PCA(n_components=12) fa = FactorAnalysis(n_components=12) features = [] dx = [] n_samples = 0 n_features = 0 rpath = os.path.join(oSdir, cfg, ''.join([replacement, condition, 'feature_data.csv'])) opath = os.path.join(oSdir, cfg, ''.join(['original', condition, 'feature_data.csv'])) if(os.path.exists(rpath)): # fill in unaltered rows when no altered row exists with open(rpath, 'r') as rf, open(opath, 'r') as of: rreader = csv.reader(rf) oreader = csv.reader(of) for rrow, orow in zip_longest(rreader, oreader): if rrow: if len(rrow) > 0: row = rrow else: row = orow for column in row: column = ast.literal_eval(column) if (column[0] != "['unknown']"): enc = LabelEncoder() enc.fit(np.array(column[0])) for feature in list(column[0]): try: feature = float(feature) except: feature = enc.transform(np.array([ feature]))[0] features.append(feature) if n_samples == 0: n_features = n_features + 1 dx.append(column[1]) n_samples = n_samples + 1 np_features = np.array(features).reshape(n_samples, n_features) np_dx = np.array(dx).reshape(n_samples) pca.fit(np_features, np_dx) fa.fit(np_features, np_dx) print(pca.components_) print(pca.explained_variance_ratio_) print(fa.components_) # ============================================================================ if __name__ == '__main__': main()
apache-2.0
boomsbloom/dtm-fmri
DTM/for_gensim/lib/python2.7/site-packages/pandas/tests/types/test_inference.py
7
31375
# -*- coding: utf-8 -*- """ These the test the public routines exposed in types/common.py related to inference and not otherwise tested in types/test_common.py """ import nose import collections import re from datetime import datetime, date, timedelta, time import numpy as np import pandas as pd from pandas import lib, tslib from pandas import (Series, Index, DataFrame, Timedelta, DatetimeIndex, TimedeltaIndex, Timestamp, Panel, Period, Categorical) from pandas.compat import u, PY2, lrange from pandas.types import inference from pandas.types.common import (is_timedelta64_dtype, is_timedelta64_ns_dtype, is_number, is_integer, is_float, is_bool, is_scalar, _ensure_int32, _ensure_categorical) from pandas.types.missing import isnull from pandas.util import testing as tm _multiprocess_can_split_ = True def test_is_sequence(): is_seq = inference.is_sequence assert (is_seq((1, 2))) assert (is_seq([1, 2])) assert (not is_seq("abcd")) assert (not is_seq(u("abcd"))) assert (not is_seq(np.int64)) class A(object): def __getitem__(self): return 1 assert (not is_seq(A())) def test_is_list_like(): passes = ([], [1], (1, ), (1, 2), {'a': 1}, set([1, 'a']), Series([1]), Series([]), Series(['a']).str) fails = (1, '2', object()) for p in passes: assert inference.is_list_like(p) for f in fails: assert not inference.is_list_like(f) def test_is_dict_like(): passes = [{}, {'A': 1}, Series([1])] fails = ['1', 1, [1, 2], (1, 2), range(2), Index([1])] for p in passes: assert inference.is_dict_like(p) for f in fails: assert not inference.is_dict_like(f) def test_is_named_tuple(): passes = (collections.namedtuple('Test', list('abc'))(1, 2, 3), ) fails = ((1, 2, 3), 'a', Series({'pi': 3.14})) for p in passes: assert inference.is_named_tuple(p) for f in fails: assert not inference.is_named_tuple(f) def test_is_hashable(): # all new-style classes are hashable by default class HashableClass(object): pass class UnhashableClass1(object): __hash__ = None class UnhashableClass2(object): def __hash__(self): raise TypeError("Not hashable") hashable = (1, 3.14, np.float64(3.14), 'a', tuple(), (1, ), HashableClass(), ) not_hashable = ([], UnhashableClass1(), ) abc_hashable_not_really_hashable = (([], ), UnhashableClass2(), ) for i in hashable: assert inference.is_hashable(i) for i in not_hashable: assert not inference.is_hashable(i) for i in abc_hashable_not_really_hashable: assert not inference.is_hashable(i) # numpy.array is no longer collections.Hashable as of # https://github.com/numpy/numpy/pull/5326, just test # is_hashable() assert not inference.is_hashable(np.array([])) # old-style classes in Python 2 don't appear hashable to # collections.Hashable but also seem to support hash() by default if PY2: class OldStyleClass(): pass c = OldStyleClass() assert not isinstance(c, collections.Hashable) assert inference.is_hashable(c) hash(c) # this will not raise def test_is_re(): passes = re.compile('ad'), fails = 'x', 2, 3, object() for p in passes: assert inference.is_re(p) for f in fails: assert not inference.is_re(f) def test_is_recompilable(): passes = (r'a', u('x'), r'asdf', re.compile('adsf'), u(r'\u2233\s*'), re.compile(r'')) fails = 1, [], object() for p in passes: assert inference.is_re_compilable(p) for f in fails: assert not inference.is_re_compilable(f) class TestInference(tm.TestCase): def test_infer_dtype_bytes(self): compare = 'string' if PY2 else 'bytes' # string array of bytes arr = np.array(list('abc'), dtype='S1') self.assertEqual(lib.infer_dtype(arr), compare) # object array of bytes arr = arr.astype(object) self.assertEqual(lib.infer_dtype(arr), compare) def test_isinf_scalar(self): # GH 11352 self.assertTrue(lib.isposinf_scalar(float('inf'))) self.assertTrue(lib.isposinf_scalar(np.inf)) self.assertFalse(lib.isposinf_scalar(-np.inf)) self.assertFalse(lib.isposinf_scalar(1)) self.assertFalse(lib.isposinf_scalar('a')) self.assertTrue(lib.isneginf_scalar(float('-inf'))) self.assertTrue(lib.isneginf_scalar(-np.inf)) self.assertFalse(lib.isneginf_scalar(np.inf)) self.assertFalse(lib.isneginf_scalar(1)) self.assertFalse(lib.isneginf_scalar('a')) def test_maybe_convert_numeric_infinities(self): # see gh-13274 infinities = ['inf', 'inF', 'iNf', 'Inf', 'iNF', 'InF', 'INf', 'INF'] na_values = set(['', 'NULL', 'nan']) pos = np.array(['inf'], dtype=np.float64) neg = np.array(['-inf'], dtype=np.float64) msg = "Unable to parse string" for infinity in infinities: for maybe_int in (True, False): out = lib.maybe_convert_numeric( np.array([infinity], dtype=object), na_values, maybe_int) tm.assert_numpy_array_equal(out, pos) out = lib.maybe_convert_numeric( np.array(['-' + infinity], dtype=object), na_values, maybe_int) tm.assert_numpy_array_equal(out, neg) out = lib.maybe_convert_numeric( np.array([u(infinity)], dtype=object), na_values, maybe_int) tm.assert_numpy_array_equal(out, pos) out = lib.maybe_convert_numeric( np.array(['+' + infinity], dtype=object), na_values, maybe_int) tm.assert_numpy_array_equal(out, pos) # too many characters with tm.assertRaisesRegexp(ValueError, msg): lib.maybe_convert_numeric( np.array(['foo_' + infinity], dtype=object), na_values, maybe_int) def test_maybe_convert_numeric_post_floatify_nan(self): # see gh-13314 data = np.array(['1.200', '-999.000', '4.500'], dtype=object) expected = np.array([1.2, np.nan, 4.5], dtype=np.float64) nan_values = set([-999, -999.0]) for coerce_type in (True, False): out = lib.maybe_convert_numeric(data, nan_values, coerce_type) tm.assert_numpy_array_equal(out, expected) def test_convert_infs(self): arr = np.array(['inf', 'inf', 'inf'], dtype='O') result = lib.maybe_convert_numeric(arr, set(), False) self.assertTrue(result.dtype == np.float64) arr = np.array(['-inf', '-inf', '-inf'], dtype='O') result = lib.maybe_convert_numeric(arr, set(), False) self.assertTrue(result.dtype == np.float64) def test_scientific_no_exponent(self): # See PR 12215 arr = np.array(['42E', '2E', '99e', '6e'], dtype='O') result = lib.maybe_convert_numeric(arr, set(), False, True) self.assertTrue(np.all(np.isnan(result))) def test_convert_non_hashable(self): # GH13324 # make sure that we are handing non-hashables arr = np.array([[10.0, 2], 1.0, 'apple']) result = lib.maybe_convert_numeric(arr, set(), False, True) tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan])) class TestTypeInference(tm.TestCase): _multiprocess_can_split_ = True def test_length_zero(self): result = lib.infer_dtype(np.array([], dtype='i4')) self.assertEqual(result, 'integer') result = lib.infer_dtype([]) self.assertEqual(result, 'empty') def test_integers(self): arr = np.array([1, 2, 3, np.int64(4), np.int32(5)], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'integer') arr = np.array([1, 2, 3, np.int64(4), np.int32(5), 'foo'], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'mixed-integer') arr = np.array([1, 2, 3, 4, 5], dtype='i4') result = lib.infer_dtype(arr) self.assertEqual(result, 'integer') def test_bools(self): arr = np.array([True, False, True, True, True], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'boolean') arr = np.array([np.bool_(True), np.bool_(False)], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'boolean') arr = np.array([True, False, True, 'foo'], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'mixed') arr = np.array([True, False, True], dtype=bool) result = lib.infer_dtype(arr) self.assertEqual(result, 'boolean') def test_floats(self): arr = np.array([1., 2., 3., np.float64(4), np.float32(5)], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'floating') arr = np.array([1, 2, 3, np.float64(4), np.float32(5), 'foo'], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'mixed-integer') arr = np.array([1, 2, 3, 4, 5], dtype='f4') result = lib.infer_dtype(arr) self.assertEqual(result, 'floating') arr = np.array([1, 2, 3, 4, 5], dtype='f8') result = lib.infer_dtype(arr) self.assertEqual(result, 'floating') def test_string(self): pass def test_unicode(self): pass def test_datetime(self): dates = [datetime(2012, 1, x) for x in range(1, 20)] index = Index(dates) self.assertEqual(index.inferred_type, 'datetime64') def test_infer_dtype_datetime(self): arr = np.array([Timestamp('2011-01-01'), Timestamp('2011-01-02')]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([np.datetime64('2011-01-01'), np.datetime64('2011-01-01')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'datetime64') arr = np.array([datetime(2011, 1, 1), datetime(2012, 2, 1)]) self.assertEqual(lib.infer_dtype(arr), 'datetime') # starts with nan for n in [pd.NaT, np.nan]: arr = np.array([n, pd.Timestamp('2011-01-02')]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([n, np.datetime64('2011-01-02')]) self.assertEqual(lib.infer_dtype(arr), 'datetime64') arr = np.array([n, datetime(2011, 1, 1)]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([n, pd.Timestamp('2011-01-02'), n]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([n, np.datetime64('2011-01-02'), n]) self.assertEqual(lib.infer_dtype(arr), 'datetime64') arr = np.array([n, datetime(2011, 1, 1), n]) self.assertEqual(lib.infer_dtype(arr), 'datetime') # different type of nat arr = np.array([np.timedelta64('nat'), np.datetime64('2011-01-02')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([np.datetime64('2011-01-02'), np.timedelta64('nat')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'mixed') # mixed datetime arr = np.array([datetime(2011, 1, 1), pd.Timestamp('2011-01-02')]) self.assertEqual(lib.infer_dtype(arr), 'datetime') # should be datetime? arr = np.array([np.datetime64('2011-01-01'), pd.Timestamp('2011-01-02')]) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([pd.Timestamp('2011-01-02'), np.datetime64('2011-01-01')]) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([np.nan, pd.Timestamp('2011-01-02'), 1]) self.assertEqual(lib.infer_dtype(arr), 'mixed-integer') arr = np.array([np.nan, pd.Timestamp('2011-01-02'), 1.1]) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([np.nan, '2011-01-01', pd.Timestamp('2011-01-02')]) self.assertEqual(lib.infer_dtype(arr), 'mixed') def test_infer_dtype_timedelta(self): arr = np.array([pd.Timedelta('1 days'), pd.Timedelta('2 days')]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([np.timedelta64(1, 'D'), np.timedelta64(2, 'D')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([timedelta(1), timedelta(2)]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') # starts with nan for n in [pd.NaT, np.nan]: arr = np.array([n, Timedelta('1 days')]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([n, np.timedelta64(1, 'D')]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([n, timedelta(1)]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([n, pd.Timedelta('1 days'), n]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([n, np.timedelta64(1, 'D'), n]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([n, timedelta(1), n]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') # different type of nat arr = np.array([np.datetime64('nat'), np.timedelta64(1, 'D')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([np.timedelta64(1, 'D'), np.datetime64('nat')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'mixed') def test_infer_dtype_period(self): # GH 13664 arr = np.array([pd.Period('2011-01', freq='D'), pd.Period('2011-02', freq='D')]) self.assertEqual(pd.lib.infer_dtype(arr), 'period') arr = np.array([pd.Period('2011-01', freq='D'), pd.Period('2011-02', freq='M')]) self.assertEqual(pd.lib.infer_dtype(arr), 'period') # starts with nan for n in [pd.NaT, np.nan]: arr = np.array([n, pd.Period('2011-01', freq='D')]) self.assertEqual(pd.lib.infer_dtype(arr), 'period') arr = np.array([n, pd.Period('2011-01', freq='D'), n]) self.assertEqual(pd.lib.infer_dtype(arr), 'period') # different type of nat arr = np.array([np.datetime64('nat'), pd.Period('2011-01', freq='M')], dtype=object) self.assertEqual(pd.lib.infer_dtype(arr), 'mixed') arr = np.array([pd.Period('2011-01', freq='M'), np.datetime64('nat')], dtype=object) self.assertEqual(pd.lib.infer_dtype(arr), 'mixed') def test_infer_dtype_all_nan_nat_like(self): arr = np.array([np.nan, np.nan]) self.assertEqual(lib.infer_dtype(arr), 'floating') # nan and None mix are result in mixed arr = np.array([np.nan, np.nan, None]) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([None, np.nan, np.nan]) self.assertEqual(lib.infer_dtype(arr), 'mixed') # pd.NaT arr = np.array([pd.NaT]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([pd.NaT, np.nan]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([np.nan, pd.NaT]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([np.nan, pd.NaT, np.nan]) self.assertEqual(lib.infer_dtype(arr), 'datetime') arr = np.array([None, pd.NaT, None]) self.assertEqual(lib.infer_dtype(arr), 'datetime') # np.datetime64(nat) arr = np.array([np.datetime64('nat')]) self.assertEqual(lib.infer_dtype(arr), 'datetime64') for n in [np.nan, pd.NaT, None]: arr = np.array([n, np.datetime64('nat'), n]) self.assertEqual(lib.infer_dtype(arr), 'datetime64') arr = np.array([pd.NaT, n, np.datetime64('nat'), n]) self.assertEqual(lib.infer_dtype(arr), 'datetime64') arr = np.array([np.timedelta64('nat')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'timedelta') for n in [np.nan, pd.NaT, None]: arr = np.array([n, np.timedelta64('nat'), n]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') arr = np.array([pd.NaT, n, np.timedelta64('nat'), n]) self.assertEqual(lib.infer_dtype(arr), 'timedelta') # datetime / timedelta mixed arr = np.array([pd.NaT, np.datetime64('nat'), np.timedelta64('nat'), np.nan]) self.assertEqual(lib.infer_dtype(arr), 'mixed') arr = np.array([np.timedelta64('nat'), np.datetime64('nat')], dtype=object) self.assertEqual(lib.infer_dtype(arr), 'mixed') def test_is_datetimelike_array_all_nan_nat_like(self): arr = np.array([np.nan, pd.NaT, np.datetime64('nat')]) self.assertTrue(lib.is_datetime_array(arr)) self.assertTrue(lib.is_datetime64_array(arr)) self.assertFalse(lib.is_timedelta_array(arr)) self.assertFalse(lib.is_timedelta64_array(arr)) self.assertFalse(lib.is_timedelta_or_timedelta64_array(arr)) arr = np.array([np.nan, pd.NaT, np.timedelta64('nat')]) self.assertFalse(lib.is_datetime_array(arr)) self.assertFalse(lib.is_datetime64_array(arr)) self.assertTrue(lib.is_timedelta_array(arr)) self.assertTrue(lib.is_timedelta64_array(arr)) self.assertTrue(lib.is_timedelta_or_timedelta64_array(arr)) arr = np.array([np.nan, pd.NaT, np.datetime64('nat'), np.timedelta64('nat')]) self.assertFalse(lib.is_datetime_array(arr)) self.assertFalse(lib.is_datetime64_array(arr)) self.assertFalse(lib.is_timedelta_array(arr)) self.assertFalse(lib.is_timedelta64_array(arr)) self.assertFalse(lib.is_timedelta_or_timedelta64_array(arr)) arr = np.array([np.nan, pd.NaT]) self.assertTrue(lib.is_datetime_array(arr)) self.assertTrue(lib.is_datetime64_array(arr)) self.assertTrue(lib.is_timedelta_array(arr)) self.assertTrue(lib.is_timedelta64_array(arr)) self.assertTrue(lib.is_timedelta_or_timedelta64_array(arr)) arr = np.array([np.nan, np.nan], dtype=object) self.assertFalse(lib.is_datetime_array(arr)) self.assertFalse(lib.is_datetime64_array(arr)) self.assertFalse(lib.is_timedelta_array(arr)) self.assertFalse(lib.is_timedelta64_array(arr)) self.assertFalse(lib.is_timedelta_or_timedelta64_array(arr)) def test_date(self): dates = [date(2012, 1, x) for x in range(1, 20)] index = Index(dates) self.assertEqual(index.inferred_type, 'date') def test_to_object_array_tuples(self): r = (5, 6) values = [r] result = lib.to_object_array_tuples(values) try: # make sure record array works from collections import namedtuple record = namedtuple('record', 'x y') r = record(5, 6) values = [r] result = lib.to_object_array_tuples(values) # noqa except ImportError: pass def test_object(self): # GH 7431 # cannot infer more than this as only a single element arr = np.array([None], dtype='O') result = lib.infer_dtype(arr) self.assertEqual(result, 'mixed') def test_to_object_array_width(self): # see gh-13320 rows = [[1, 2, 3], [4, 5, 6]] expected = np.array(rows, dtype=object) out = lib.to_object_array(rows) tm.assert_numpy_array_equal(out, expected) expected = np.array(rows, dtype=object) out = lib.to_object_array(rows, min_width=1) tm.assert_numpy_array_equal(out, expected) expected = np.array([[1, 2, 3, None, None], [4, 5, 6, None, None]], dtype=object) out = lib.to_object_array(rows, min_width=5) tm.assert_numpy_array_equal(out, expected) def test_is_period(self): self.assertTrue(lib.is_period(pd.Period('2011-01', freq='M'))) self.assertFalse(lib.is_period(pd.PeriodIndex(['2011-01'], freq='M'))) self.assertFalse(lib.is_period(pd.Timestamp('2011-01'))) self.assertFalse(lib.is_period(1)) self.assertFalse(lib.is_period(np.nan)) def test_categorical(self): # GH 8974 from pandas import Categorical, Series arr = Categorical(list('abc')) result = lib.infer_dtype(arr) self.assertEqual(result, 'categorical') result = lib.infer_dtype(Series(arr)) self.assertEqual(result, 'categorical') arr = Categorical(list('abc'), categories=['cegfab'], ordered=True) result = lib.infer_dtype(arr) self.assertEqual(result, 'categorical') result = lib.infer_dtype(Series(arr)) self.assertEqual(result, 'categorical') class TestNumberScalar(tm.TestCase): def test_is_number(self): self.assertTrue(is_number(True)) self.assertTrue(is_number(1)) self.assertTrue(is_number(1.1)) self.assertTrue(is_number(1 + 3j)) self.assertTrue(is_number(np.bool(False))) self.assertTrue(is_number(np.int64(1))) self.assertTrue(is_number(np.float64(1.1))) self.assertTrue(is_number(np.complex128(1 + 3j))) self.assertTrue(is_number(np.nan)) self.assertFalse(is_number(None)) self.assertFalse(is_number('x')) self.assertFalse(is_number(datetime(2011, 1, 1))) self.assertFalse(is_number(np.datetime64('2011-01-01'))) self.assertFalse(is_number(Timestamp('2011-01-01'))) self.assertFalse(is_number(Timestamp('2011-01-01', tz='US/Eastern'))) self.assertFalse(is_number(timedelta(1000))) self.assertFalse(is_number(Timedelta('1 days'))) # questionable self.assertFalse(is_number(np.bool_(False))) self.assertTrue(is_number(np.timedelta64(1, 'D'))) def test_is_bool(self): self.assertTrue(is_bool(True)) self.assertTrue(is_bool(np.bool(False))) self.assertTrue(is_bool(np.bool_(False))) self.assertFalse(is_bool(1)) self.assertFalse(is_bool(1.1)) self.assertFalse(is_bool(1 + 3j)) self.assertFalse(is_bool(np.int64(1))) self.assertFalse(is_bool(np.float64(1.1))) self.assertFalse(is_bool(np.complex128(1 + 3j))) self.assertFalse(is_bool(np.nan)) self.assertFalse(is_bool(None)) self.assertFalse(is_bool('x')) self.assertFalse(is_bool(datetime(2011, 1, 1))) self.assertFalse(is_bool(np.datetime64('2011-01-01'))) self.assertFalse(is_bool(Timestamp('2011-01-01'))) self.assertFalse(is_bool(Timestamp('2011-01-01', tz='US/Eastern'))) self.assertFalse(is_bool(timedelta(1000))) self.assertFalse(is_bool(np.timedelta64(1, 'D'))) self.assertFalse(is_bool(Timedelta('1 days'))) def test_is_integer(self): self.assertTrue(is_integer(1)) self.assertTrue(is_integer(np.int64(1))) self.assertFalse(is_integer(True)) self.assertFalse(is_integer(1.1)) self.assertFalse(is_integer(1 + 3j)) self.assertFalse(is_integer(np.bool(False))) self.assertFalse(is_integer(np.bool_(False))) self.assertFalse(is_integer(np.float64(1.1))) self.assertFalse(is_integer(np.complex128(1 + 3j))) self.assertFalse(is_integer(np.nan)) self.assertFalse(is_integer(None)) self.assertFalse(is_integer('x')) self.assertFalse(is_integer(datetime(2011, 1, 1))) self.assertFalse(is_integer(np.datetime64('2011-01-01'))) self.assertFalse(is_integer(Timestamp('2011-01-01'))) self.assertFalse(is_integer(Timestamp('2011-01-01', tz='US/Eastern'))) self.assertFalse(is_integer(timedelta(1000))) self.assertFalse(is_integer(Timedelta('1 days'))) # questionable self.assertTrue(is_integer(np.timedelta64(1, 'D'))) def test_is_float(self): self.assertTrue(is_float(1.1)) self.assertTrue(is_float(np.float64(1.1))) self.assertTrue(is_float(np.nan)) self.assertFalse(is_float(True)) self.assertFalse(is_float(1)) self.assertFalse(is_float(1 + 3j)) self.assertFalse(is_float(np.bool(False))) self.assertFalse(is_float(np.bool_(False))) self.assertFalse(is_float(np.int64(1))) self.assertFalse(is_float(np.complex128(1 + 3j))) self.assertFalse(is_float(None)) self.assertFalse(is_float('x')) self.assertFalse(is_float(datetime(2011, 1, 1))) self.assertFalse(is_float(np.datetime64('2011-01-01'))) self.assertFalse(is_float(Timestamp('2011-01-01'))) self.assertFalse(is_float(Timestamp('2011-01-01', tz='US/Eastern'))) self.assertFalse(is_float(timedelta(1000))) self.assertFalse(is_float(np.timedelta64(1, 'D'))) self.assertFalse(is_float(Timedelta('1 days'))) def test_is_timedelta(self): self.assertTrue(is_timedelta64_dtype('timedelta64')) self.assertTrue(is_timedelta64_dtype('timedelta64[ns]')) self.assertFalse(is_timedelta64_ns_dtype('timedelta64')) self.assertTrue(is_timedelta64_ns_dtype('timedelta64[ns]')) tdi = TimedeltaIndex([1e14, 2e14], dtype='timedelta64') self.assertTrue(is_timedelta64_dtype(tdi)) self.assertTrue(is_timedelta64_ns_dtype(tdi)) self.assertTrue(is_timedelta64_ns_dtype(tdi.astype('timedelta64[ns]'))) # Conversion to Int64Index: self.assertFalse(is_timedelta64_ns_dtype(tdi.astype('timedelta64'))) self.assertFalse(is_timedelta64_ns_dtype(tdi.astype('timedelta64[h]'))) class Testisscalar(tm.TestCase): def test_isscalar_builtin_scalars(self): self.assertTrue(is_scalar(None)) self.assertTrue(is_scalar(True)) self.assertTrue(is_scalar(False)) self.assertTrue(is_scalar(0.)) self.assertTrue(is_scalar(np.nan)) self.assertTrue(is_scalar('foobar')) self.assertTrue(is_scalar(b'foobar')) self.assertTrue(is_scalar(u('efoobar'))) self.assertTrue(is_scalar(datetime(2014, 1, 1))) self.assertTrue(is_scalar(date(2014, 1, 1))) self.assertTrue(is_scalar(time(12, 0))) self.assertTrue(is_scalar(timedelta(hours=1))) self.assertTrue(is_scalar(pd.NaT)) def test_isscalar_builtin_nonscalars(self): self.assertFalse(is_scalar({})) self.assertFalse(is_scalar([])) self.assertFalse(is_scalar([1])) self.assertFalse(is_scalar(())) self.assertFalse(is_scalar((1, ))) self.assertFalse(is_scalar(slice(None))) self.assertFalse(is_scalar(Ellipsis)) def test_isscalar_numpy_array_scalars(self): self.assertTrue(is_scalar(np.int64(1))) self.assertTrue(is_scalar(np.float64(1.))) self.assertTrue(is_scalar(np.int32(1))) self.assertTrue(is_scalar(np.object_('foobar'))) self.assertTrue(is_scalar(np.str_('foobar'))) self.assertTrue(is_scalar(np.unicode_(u('foobar')))) self.assertTrue(is_scalar(np.bytes_(b'foobar'))) self.assertTrue(is_scalar(np.datetime64('2014-01-01'))) self.assertTrue(is_scalar(np.timedelta64(1, 'h'))) def test_isscalar_numpy_zerodim_arrays(self): for zerodim in [np.array(1), np.array('foobar'), np.array(np.datetime64('2014-01-01')), np.array(np.timedelta64(1, 'h')), np.array(np.datetime64('NaT'))]: self.assertFalse(is_scalar(zerodim)) self.assertTrue(is_scalar(lib.item_from_zerodim(zerodim))) def test_isscalar_numpy_arrays(self): self.assertFalse(is_scalar(np.array([]))) self.assertFalse(is_scalar(np.array([[]]))) self.assertFalse(is_scalar(np.matrix('1; 2'))) def test_isscalar_pandas_scalars(self): self.assertTrue(is_scalar(Timestamp('2014-01-01'))) self.assertTrue(is_scalar(Timedelta(hours=1))) self.assertTrue(is_scalar(Period('2014-01-01'))) def test_lisscalar_pandas_containers(self): self.assertFalse(is_scalar(Series())) self.assertFalse(is_scalar(Series([1]))) self.assertFalse(is_scalar(DataFrame())) self.assertFalse(is_scalar(DataFrame([[1]]))) self.assertFalse(is_scalar(Panel())) self.assertFalse(is_scalar(Panel([[[1]]]))) self.assertFalse(is_scalar(Index([]))) self.assertFalse(is_scalar(Index([1]))) def test_datetimeindex_from_empty_datetime64_array(): for unit in ['ms', 'us', 'ns']: idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit)) assert (len(idx) == 0) def test_nan_to_nat_conversions(): df = DataFrame(dict({ 'A': np.asarray( lrange(10), dtype='float64'), 'B': Timestamp('20010101') })) df.iloc[3:6, :] = np.nan result = df.loc[4, 'B'].value assert (result == tslib.iNaT) s = df['B'].copy() s._data = s._data.setitem(indexer=tuple([slice(8, 9)]), value=np.nan) assert (isnull(s[8])) # numpy < 1.7.0 is wrong from distutils.version import LooseVersion if LooseVersion(np.__version__) >= '1.7.0': assert (s[8].value == np.datetime64('NaT').astype(np.int64)) def test_ensure_int32(): values = np.arange(10, dtype=np.int32) result = _ensure_int32(values) assert (result.dtype == np.int32) values = np.arange(10, dtype=np.int64) result = _ensure_int32(values) assert (result.dtype == np.int32) def test_ensure_categorical(): values = np.arange(10, dtype=np.int32) result = _ensure_categorical(values) assert (result.dtype == 'category') values = Categorical(values) result = _ensure_categorical(values) tm.assert_categorical_equal(result, values) if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)
mit
fdft/ml
ch01/webstats.py
1
1029
import scipy as sp import matplotlib.pyplot as plt data = sp.genfromtxt("data/web_traffic.tsv", delimiter="\t") print(data[:10]) x = data[:,0] y = data[:,1] x = x[~sp.isnan(y)]; y = y[~sp.isnan(y)]; plt.scatter(x,y) plt.title("Web traffic over the last month") plt.xlabel("Time") plt.ylabel("Hits/hour") plt.xticks([w*7*24 for w in range(10)],['week %i'%w for w in range(10)]) plt.autoscale(tight=True) plt.grid() #plt.show() fp1, residuals, rank, sv, rcond = sp.polyfit(x, y, 8, full=True) f1 = sp.poly1d(fp1) fx = sp.linspace(0,x[-1], 1000) # generate X-values for plotting plt.plot(fx, f1(fx), linewidth=4) plt.legend(["d=%i" % f1.order], loc="upper left") inflection = 3.5*7*24 # calculate the inflection point in hours xa = x[:inflection] # data before the inflection point ya = y[:inflection] xb = x[inflection:] # data after yb = y[inflection:] fa = sp.poly1d(sp.polyfit(xa, ya, 1)) fb = sp.poly1d(sp.polyfit(xb, yb, 1)) fa_error = error(fa, xa, ya) fb_error = error(fb, xb, yb) plt.plot(fx, f1(fx), linewidth=4)
mit
ahuang11/ahh
sketches/my_sleep/my_sleep.py
1
8823
from ahh import vis, ext, sci import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import datetime from scipy.stats import pearsonr sleep_df = pd.read_pickle('sleep_data_fmt.pkl') sleep_hours = sleep_df['minutes'] / 60 sleep_quality = sleep_df['quality'] * 100 sleep_df_interp = sleep_df sleep_df_interp['minutes'][sleep_df_interp.index[0]] = 9 sleep_hours_interp = sleep_df_interp['minutes'].interpolate() / 60 wx_df = pd.read_csv('kcmi_wx.csv') # https://www.wunderground.com/history/airport/KCMI/2014/1/1/ tmp = wx_df['Mean TemperatureF'] x = mdates.date2num(sleep_df_interp.index) xx = np.linspace(x.min(), x.max(), len(sleep_df_interp.index)) z4 = np.polyfit(x, sleep_hours_interp, 4) p4 = np.poly1d(z4) sleep_fit = p4(xx) fig, ax = vis.plot(sleep_df.index, sleep_hours, y2=sleep_fit, bar=True, bar_dates=True, save='andrew_sleep', sharex=True, figsize=(70, 20), major='months', interval=3, width=0.65, title="Andrew's Daily Sleep (2014 - 2016)", ylabel='Hours', titlescale=4, fontscale=3.5, labelscale=3.5, linewidth2=5, minor='years') years = range(2014,2017) yearly_sleep_avg_list = [] yearly_sleep_std_list = [] months = range(1, 13) monthly_sleep_avg_list = [] monthly_sleep_std_list = [] sleep_quality_avg_list = [] yr_monthly_sleep_avg_list = [] yr_monthly_quality_avg_list = [] sleep_masked = np.ma.masked_array(sleep_hours, np.isnan(sleep_hours)) quality_masked = np.ma.masked_array(sleep_quality, np.isnan(sleep_quality)) for year in years: year_idc = np.where(pd.DatetimeIndex(sleep_df.index).year == year)[0] yearly_sleep_avg_list.append(np.ma.average(sleep_masked[year_idc])) yearly_sleep_std_list.append(np.std(sleep_hours[year_idc])) for month in months: month_idc = np.where(pd.DatetimeIndex(sleep_df.index).month == month)[0] monthly_sleep_avg_list.append(np.ma.average(sleep_masked[month_idc])) monthly_sleep_std_list.append(np.std(sleep_hours[month_idc])) sleep_quality_avg_list.append(np.ma.average(quality_masked[month_idc])) months_avg = np.ones(len(months)) * np.average(monthly_sleep_avg_list) quality_months_avg = np.ones(len(months)) * np.average(sleep_quality_avg_list) caption = """ Yearly Avg: 2014:{avg2014:02.2f}, 2015:{avg2015:02.2f}, 2016:{avg2016:02.2f} Yearly Std: 2014:{std2014:02.2f}, 2015:{std2015:02.2f}, 2016:{std2016:02.2f} Monthly Avg: Jan:{jan:02.2f}, Feb:{feb:02.2f}, Mar:{mar:02.2f}, Apr:{apr:02.2f}, May:{may:02.2f}, Jun:{jun:02.2f}, Jul:{jul:02.2f}, Aug:{aug:02.2f}, Sep:{sep:02.2f}, Oct:{oct:02.2f}, Nov:{nov:02.2f}, Dec:{dec:02.2f} Monthly Std: Jan:{jan_std:02.2f}, Feb:{feb_std:02.2f}, Mar:{mar_std:02.2f}, Apr:{apr_std:02.2f}, May:{may_std:02.2f}, Jun:{jun_std:02.2f}, Jul:{jul_std:02.2f}, Aug:{aug_std:02.2f}, Sep:{sep_std:02.2f}, Oct:{oct_std:02.2f}, Nov:{nov_std:02.2f}, Dec:{dec_std:02.2f} """ plt.figtext(0.5, 0.005, caption.format( avg2014=yearly_sleep_avg_list[0], std2014=yearly_sleep_std_list[0], avg2015=yearly_sleep_avg_list[1], std2015=yearly_sleep_std_list[1], avg2016=yearly_sleep_avg_list[2], std2016=yearly_sleep_std_list[2], jan=monthly_sleep_avg_list[0], feb=monthly_sleep_avg_list[1], mar=monthly_sleep_avg_list[2], apr=monthly_sleep_avg_list[3], may=monthly_sleep_avg_list[4], jun=monthly_sleep_avg_list[5], jul=monthly_sleep_avg_list[6], aug=monthly_sleep_avg_list[7], sep=monthly_sleep_avg_list[8], oct=monthly_sleep_avg_list[9], nov=monthly_sleep_avg_list[10], dec=monthly_sleep_avg_list[11], jan_std=monthly_sleep_std_list[0], feb_std=monthly_sleep_std_list[1], mar_std=monthly_sleep_std_list[2], apr_std=monthly_sleep_std_list[3], may_std=monthly_sleep_std_list[4], jun_std=monthly_sleep_std_list[5], jul_std=monthly_sleep_std_list[6], aug_std=monthly_sleep_std_list[7], sep_std=monthly_sleep_std_list[8], oct_std=monthly_sleep_std_list[9], nov_std=monthly_sleep_std_list[10], dec_std=monthly_sleep_std_list[11], ), ha='center', size=40, color='.5', ) plt.savefig("andrew_sleep") for year in years: for month in months: yr_month_idc = np.where((pd.DatetimeIndex(sleep_df.index).month == month) & (pd.DatetimeIndex(sleep_df.index).year == year))[0] yr_monthly_sleep_avg_list.append(np.ma.average(sleep_masked[yr_month_idc])) yr_monthly_quality_avg_list.append(np.ma.average(quality_masked[yr_month_idc])) start = datetime.datetime(2013, 12, 31) dates = pd.date_range(start, periods=len(yr_monthly_sleep_avg_list), freq='m') x = mdates.date2num(dates[:-2]) xx = np.linspace(x.min(), x.max(), len(dates)) z4 = np.polyfit(x, np.array(yr_monthly_sleep_avg_list[:-2]), 4) p4 = np.poly1d(z4) yearly_monthly_sleep_fit = p4(xx) monthly_qual_norm = sci.get_norm_anom(np.array(yr_monthly_sleep_avg_list[:-2])) monthly_hour_norm = sci.get_norm_anom(np.array(yr_monthly_quality_avg_list[:-2])) coeff, pval = pearsonr(monthly_qual_norm, monthly_hour_norm) plt.figure() title_fmt = 'Monthly Average Hours of Sleep' fig, ax = vis.plot(dates, yr_monthly_sleep_avg_list, y2=yearly_monthly_sleep_fit, ylabel='Hours', sharex=True, extra=True, xlabel='Month', bar_dates=True, linewidth2=2, title=title_fmt.format(coeff), ylabel2='Quality', bar=True, ylim=(7, 9.5), width=15, figsize=(20,15), major='months', interval=3, fontscale=1.5, labelscale=1.5, minor='years') plt.savefig('yr_monthly_andrew_quality_hour.png') plt.figure() vis.plot(months, monthly_sleep_avg_list, y2=months_avg, ylabel='Hours', sharex=True, extra=True, title='Monthly Average Hours of Sleep (2014 - 2016)', xlabel='Month', save='monthly_andrew_sleep', figsize=(20,15), xlim=(1, 12)) plt.figure() vis.plot(months, sleep_quality_avg_list, y2=quality_months_avg, ylabel='%', sharex=True, extra=True, xlabel='Month', title='Monthly Average Quality of Sleep (2014 - 2016)', save='monthly_andrew_quality', figsize=(20,15), xlim=(1, 12)) plt.figure() hist, bins = np.histogram(sleep_hours, bins=20, range=(6, 11)) width = 0.7 * (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 vis.plot(center, hist, width=width, ylabel='Count', title='Hours of Sleep Histogram (2014 - 2016)', xlabel='Hours', save='histogram_andrew_sleep', figsize=(20,15), bar=True, xlim=(6, 11)) monthly_qual_norm = sci.get_norm_anom(np.array(monthly_sleep_avg_list)) monthly_hour_norm = sci.get_norm_anom(np.array(sleep_quality_avg_list)) coeff, pval = pearsonr(monthly_qual_norm, monthly_hour_norm) plt.figure() title_fmt = 'Monthly Quality of Sleep vs Hours of Sleep Correlation = {:.2f}' vis.plot(months, monthly_sleep_avg_list, y2=sleep_quality_avg_list, ylabel='Hours', sharex=True, extra=True, xlabel='Month', extray=True, title=title_fmt.format(coeff), ylabel2='Quality', save='monthly_andrew_quality_hour', figsize=(20,15), xlim=(1, 12)) qual_norm = sci.get_norm_anom(quality_masked) tmp_norm = sci.get_norm_anom(tmp) qual_norm_cut = qual_norm[~qual_norm.mask] tmp_norm_cut = tmp_norm[~qual_norm.mask] coeff, pval = pearsonr(qual_norm_cut, tmp_norm_cut) fig, ax = vis.plot(sleep_df.index, quality_masked, y2=tmp, dates=True, save='qual_vs_tmp', sharex=True, figsize=(70, 20), major='months', interval=3, extray=True, title="Sleep Quality vs Temperature Correlation: {}".format(coeff), ylabel='Sleep Quality', titlescale=4, fontscale=3.5, labelscale=3.5, linewidth2=5, minor='years')
mit
chengguo-clemson/chengguo-clemson.github.io
markdown_generator/talks.py
199
4000
# coding: utf-8 # # Talks markdown generator for academicpages # # Takes a TSV of talks with metadata and converts them for use with [academicpages.github.io](academicpages.github.io). This is an interactive Jupyter notebook ([see more info here](http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/what_is_jupyter.html)). The core python code is also in `talks.py`. Run either from the `markdown_generator` folder after replacing `talks.tsv` with one containing your data. # # TODO: Make this work with BibTex and other databases, rather than Stuart's non-standard TSV format and citation style. # In[1]: import pandas as pd import os # ## Data format # # The TSV needs to have the following columns: title, type, url_slug, venue, date, location, talk_url, description, with a header at the top. Many of these fields can be blank, but the columns must be in the TSV. # # - Fields that cannot be blank: `title`, `url_slug`, `date`. All else can be blank. `type` defaults to "Talk" # - `date` must be formatted as YYYY-MM-DD. # - `url_slug` will be the descriptive part of the .md file and the permalink URL for the page about the paper. # - The .md file will be `YYYY-MM-DD-[url_slug].md` and the permalink will be `https://[yourdomain]/talks/YYYY-MM-DD-[url_slug]` # - The combination of `url_slug` and `date` must be unique, as it will be the basis for your filenames # # ## Import TSV # # Pandas makes this easy with the read_csv function. We are using a TSV, so we specify the separator as a tab, or `\t`. # # I found it important to put this data in a tab-separated values format, because there are a lot of commas in this kind of data and comma-separated values can get messed up. However, you can modify the import statement, as pandas also has read_excel(), read_json(), and others. # In[3]: talks = pd.read_csv("talks.tsv", sep="\t", header=0) talks # ## Escape special characters # # YAML is very picky about how it takes a valid string, so we are replacing single and double quotes (and ampersands) with their HTML encoded equivilents. This makes them look not so readable in raw format, but they are parsed and rendered nicely. # In[4]: html_escape_table = { "&": "&amp;", '"': "&quot;", "'": "&apos;" } def html_escape(text): if type(text) is str: return "".join(html_escape_table.get(c,c) for c in text) else: return "False" # ## Creating the markdown files # # This is where the heavy lifting is done. This loops through all the rows in the TSV dataframe, then starts to concatentate a big string (```md```) that contains the markdown for each type. It does the YAML metadata first, then does the description for the individual page. # In[5]: loc_dict = {} for row, item in talks.iterrows(): md_filename = str(item.date) + "-" + item.url_slug + ".md" html_filename = str(item.date) + "-" + item.url_slug year = item.date[:4] md = "---\ntitle: \"" + item.title + '"\n' md += "collection: talks" + "\n" if len(str(item.type)) > 3: md += 'type: "' + item.type + '"\n' else: md += 'type: "Talk"\n' md += "permalink: /talks/" + html_filename + "\n" if len(str(item.venue)) > 3: md += 'venue: "' + item.venue + '"\n' if len(str(item.location)) > 3: md += "date: " + str(item.date) + "\n" if len(str(item.location)) > 3: md += 'location: "' + str(item.location) + '"\n' md += "---\n" if len(str(item.talk_url)) > 3: md += "\n[More information here](" + item.talk_url + ")\n" if len(str(item.description)) > 3: md += "\n" + html_escape(item.description) + "\n" md_filename = os.path.basename(md_filename) #print(md) with open("../_talks/" + md_filename, 'w') as f: f.write(md) # These files are in the talks directory, one directory below where we're working from.
mit
nicaogr/Style-Transfer
Filter_Rep.py
1
24343
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 02 2017 The goal of this script is to vizualised the reponse of the filter of the different convolution of the network @author: nicolas """ import scipy import numpy as np import tensorflow as tf import Style_Transfer as st from Arg_Parser import get_parser_args import seaborn as sns import matplotlib.pyplot as plt import os import pandas as pd import time import pickle from matplotlib.backends.backend_pdf import PdfPages import scipy.stats as stats from tensorflow.python.framework import dtypes import matplotlib.gridspec as gridspec import math from skimage import exposure from PIL import Image # Name of the 19 first layers of the VGG19 VGG19_LAYERS = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) VGG19_LAYERS_INDICES = {'conv1_1' : 0,'conv1_2' : 2,'conv2_1' : 5,'conv2_2' : 7, 'conv3_1' : 10,'conv3_2' : 12,'conv3_3' : 14,'conv3_4' : 16,'conv4_1' : 19, 'conv4_2' : 21,'conv4_3' : 23,'conv4_4' : 25,'conv5_1' : 28,'conv5_2' : 30, 'conv5_3' : 32,'conv5_4' : 34} VGG19_LAYERS_INTEREST = ( 'conv1_1','conv2_1', 'conv3_1' ) #VGG19_LAYERS_INTEREST = ('conv1_1' ,'conv1_2','conv2_1' ,'conv2_2' , #'conv3_1','conv3_2','conv3_3' ,'conv3_4','conv4_1' ,'conv4_2') VGG19_LAYERS_INTEREST = {'conv1_1'} def hist(values,value_range,nbins=100,dtype=dtypes.float32): nbins_float = float(nbins) # Map tensor values that fall within value_range to [0, 1]. # scaled_values = math_ops.truediv(values - value_range[0], # value_range[1] - value_range[0], # name='scaled_values') # values - value_range[0] / value_range[1] - value_range[0] scaled_values = tf.truediv(values - value_range[0],value_range[1] - value_range[0]) scaled_values =tf.multiply(nbins_float,scaled_values) # map tensor values within the open interval value_range to {0,.., nbins-1}, # values outside the open interval will be zero or less, or nbins or more. # indices = math_ops.floor(nbins_float * scaled_values, name='indices') indices = tf.floor(scaled_values) print(indices) print(type(indices)) histo = indices # Clip edge cases (e.g. value = value_range[1]) or "outliers." #indices = math_ops.cast( # clip_ops.clip_by_value(indices, 0, nbins_float- 1), dtypes.int32) # TODO(langmore) This creates an array of ones to add up and place in the # bins. This is inefficient, so replace when a better Op is available. #histo= math_ops.unsorted_segment_sum(array_ops.ones_like(indices, dtype=dtype),indices,nbins) return(histo) def is_square(apositiveint): x = apositiveint // 2 seen = set([x]) while x * x != apositiveint: x = (x + (apositiveint // x)) // 2 if x in seen: return False seen.add(x) return True def plot_and_save(Matrix,path,name=''): Matrix = Matrix[0] # Remove first dim h,w,channels = Matrix.shape df_Matrix = pd.DataFrame(np.reshape(Matrix,(h*w,channels))) len_columns = len(df_Matrix.columns) if(len_columns<6): fig, axes = plt.subplots(1,len_columns) else: if(len_columns%4==0): fig, axes = plt.subplots(len_columns//4, 4) elif(len_columns%3==0): fig, axes = plt.subplots(len_columns//3, 3) elif(len_columns%5==0): fig, axes = plt.subplots(len_columns//5, 5) elif(len_columns%2==0): fig, axes = plt.subplots(len_columns//2, 2) else: j=6 while(not(len_columns%j==0)): j += 1 fig, axes = plt.subplots(len_columns//j, j) i = 0 axes = axes.flatten() for axis in zip(axes): df_Matrix.hist(column = i, bins = 64, ax=axis) i += 1 pltname = path+name+'.png' # TODO avoid to Plot some ligne on the screen fig.savefig(pltname, dpi = 1000) def plot_and_save_pdf(Matrix,path,name=''): pltname = path+name+'_hist.pdf' pltname_rep = path+name+'_img.pdf' pp = PdfPages(pltname) Matrix = Matrix[0] # Remove first dim h,w,channels = Matrix.shape df_Matrix = pd.DataFrame(np.reshape(Matrix,(h*w,channels))) len_columns = len(df_Matrix.columns) for i in range(len_columns): df_Matrix.hist(column = i, bins = 128) plt.savefig(pp, format='pdf') plt.close() pp.close() plt.clf() # Result of the convolution pp_img = PdfPages(pltname_rep) for i in range(len_columns): plt.imshow(Matrix[:,:,i], cmap='gray') plt.savefig(pp_img, format='pdf') plt.close() pp_img.close() def plot_Rep(args): """ Plot the reponse to the filters/kernels and the histogram """ directory_path = 'Results/Filter_Rep/'+args.style_img_name+'/' if not os.path.exists(directory_path): os.makedirs(directory_path) sns.set() image_style_path = args.img_folder + args.style_img_name + args.img_ext image_style = st.preprocess(scipy.misc.imread(image_style_path).astype('float32')) _,image_h_art, image_w_art, _ = image_style.shape plot_and_save_pdf(image_style,directory_path,'ProcessIm') print("Plot initial image") vgg_layers = st.get_vgg_layers() net = st.net_preloaded(vgg_layers, image_style) # net for the style image sess = tf.Session() sess.run(net['input'].assign(image_style)) for layer in VGG19_LAYERS_INTEREST: a = net[layer].eval(session=sess) print(layer,a.shape) plot_and_save_pdf(a,directory_path,layer) def estimate_gennorm(args): sns.set_style("white") image_style_path = args.img_folder + args.style_img_name + args.img_ext image_style = st.preprocess(scipy.misc.imread(image_style_path).astype('float32')) vgg_layers = st.get_vgg_layers() net = st.net_preloaded(vgg_layers, image_style) # net for the style image sess = tf.Session() sess.run(net['input'].assign(image_style)) Distrib_Estimation = {} dict_pvalue = {} alpha = 0.1 for layer in VGG19_LAYERS_INTEREST: print(layer) a = net[layer].eval(session=sess) a = a[0] h,w,number_of_features = a.shape a_reshaped = np.reshape(a,(h*w,number_of_features)) print(h*w) Distrib_Estimation[layer] = np.array([]) dict_pvalue[layer] = [] for i in range(number_of_features): print(i) samples = a_reshaped[:,i] # This fit is computed by maximizing a log-likelihood function, with # penalty applied for samples outside of range of the distribution. The # returned answer is not guaranteed to be the globally optimal MLE, it # may only be locally optimal, or the optimization may fail altogether. beta, loc, scale = stats.gennorm.fit(samples) if(len(Distrib_Estimation[layer])==0): print("Number of points",len(samples)) Distrib_Estimation[layer] = np.array([beta,loc,scale]) else: Distrib_Estimation[layer] = np.vstack((Distrib_Estimation[layer],np.array([beta,loc,scale]))) # The KS test is only valid for continuous distributions. and with a theoritical distribution D,pvalue = stats.kstest(samples, 'gennorm',(beta, loc, scale )) dict_pvalue[layer] += [pvalue] if(pvalue > alpha ): #p-value> α print(layer,i,pvalue) pass #print(Distrib_Estimation[layer]) #print(dict_pvalue[layer]) return(Distrib_Estimation) def unpool(value, name='unpool'): """N-dimensional version of the unpooling operation from https://www.robots.ox.ac.uk/~vgg/rg/papers/Dosovitskiy_Learning_to_Generate_2015_CVPR_paper.pdf :param value: A Tensor of shape [b, d0, d1, ..., dn, ch] :return: A Tensor of shape [b, 2*d0, 2*d1, ..., 2*dn, ch] """ with tf.name_scope(name) as scope: sh = value.get_shape().as_list() dim = len(sh[1:-1]) out = (tf.reshape(value, [-1] + sh[-dim:])) for i in range(dim, 0, -1): out = tf.concat(i, [out, tf.zeros_like(out)]) out_size = [-1] + [s * 2 for s in sh[1:-1]] + [sh[-1]] out = tf.reshape(out, out_size, name=scope) return(out) def calculate_output_shape(in_layer, n_kernel, kernel_size, border_mode='same'): """ Always assumes stride=1 """ in_shape = in_layer.get_shape() # assumes in_shape[0] = None or batch_size out_shape = [s for s in in_shape] # copy out_shape[-1] = n_kernel # always true if border_mode=='same': out_shape[1] = in_shape[1] out_shape[2] = in_shape[2] elif border_mode == 'valid': out_shape[1] = tf.to_int32(in_shape[1]+kernel_size - 1) out_shape[2] = tf.to_int32(in_shape[2]+kernel_size - 1) return(out_shape) def genTexture(args): image_style_path = args.img_folder + args.style_img_name + args.img_ext image_style = st.preprocess(scipy.misc.imread(image_style_path).astype('float32')) output_image_path = args.img_folder + args.output_img_name + args.img_ext vgg_layers = st.get_vgg_layers() net = st.net_preloaded(vgg_layers, image_style) # net for the style image sess = tf.Session() sess.run(net['input'].assign(image_style)) Distrib_Estimation = {} dict_pvalue = {} data = 'data.pkl' try: Distrib_Estimation = pickle.load(open(data, 'rb')) except(FileNotFoundError): for layer in VGG19_LAYERS_INTEREST: print(layer) a = net[layer].eval(session=sess) a = a[0] h,w,number_of_features = a.shape a_reshaped = np.reshape(a,(h*w,number_of_features)) Distrib_Estimation[layer] = np.array([]) dict_pvalue[layer] = [] for i in range(number_of_features): samples = a_reshaped[:,i] # This fit is computed by maximizing a log-likelihood function, with # penalty applied for samples outside of range of the distribution. The # returned answer is not guaranteed to be the globally optimal MLE, it # may only be locally optimal, or the optimization may fail altogether. beta, loc, scale = stats.gennorm.fit(samples) if(len(Distrib_Estimation[layer])==0): print("Number of points",len(samples)) Distrib_Estimation[layer] = np.array([beta,loc,scale]) else: Distrib_Estimation[layer] = np.vstack((Distrib_Estimation[layer],np.array([beta,loc,scale]))) # chaque ligne est un channel # The KS test is only valid for continuous distributions. and with a theoritical distribution D,pvalue = stats.kstest(samples, 'gennorm',(beta, loc, scale )) dict_pvalue[layer] += [pvalue] with open(data, 'wb') as output: pickle.dump(Distrib_Estimation,output) print('End Computation of marginal distrib') generative_net = {} generative_net['conv3_1_input'] = tf.Variable(np.zeros(net['conv3_1'].shape, dtype=np.float32)) weights = tf.constant(np.transpose(vgg_layers[10][0][0][2][0][0], (1, 0, 2, 3))) bias = -tf.constant(vgg_layers[10][0][0][2][0][1].reshape(-1)) #print(weights.get_shape()[0],weights.get_shape()[1],weights.get_shape()[2],weights.get_shape()[3]) #print(calculate_output_shape(generative_net['conv3_1_input'],tf.to_int32(tf.shape(weights)[3]),tf.to_int32(tf.shape(weights)[1]))) generative_net['conv3_1'] = tf.nn.conv2d_transpose(value=tf.nn.bias_add(generative_net['conv3_1_input'],bias),filter=weights, output_shape=calculate_output_shape(generative_net['conv3_1_input'],256,3), strides=(1, 1, 1, 1), padding='SAME') generative_net['pool2'] = unpool(generative_net['conv3_1']) # RELU ??????? weights = tf.constant(np.transpose(vgg_layers[7][0][0][2][0][0], (1, 0, 2, 3))) bias = -tf.constant(vgg_layers[7][0][0][2][0][1].reshape(-1)) generative_net['conv2_2'] = tf.nn.conv2d_transpose(tf.nn.bias_add(generative_net['pool2'],bias), tf.shape(generative_net['pool2']), weights, strides=(1, 1, 1, 1), padding='SAME') weights = tf.constant(np.transpose(vgg_layers[5][0][0][2][0][0], (1, 0, 2, 3))) bias = -tf.constant(vgg_layers[5][0][0][2][0][1].reshape(-1)) generative_net['conv2_1'] = tf.nn.conv2d_transpose(tf.nn.bias_add(generative_net['conv2_2'],bias), tf.shape(generative_net['conv2_2']),weights, strides=(1, 1, 1, 1), padding='SAME') generative_net['pool1'] = unpool(generative_net['conv2_1']) weights = tf.constant(np.transpose(vgg_layers[2][0][0][2][0][0], (1, 0, 2, 3))) bias = -tf.constant(vgg_layers[2][0][0][2][0][1].reshape(-1)) generative_net['conv1_2'] = tf.nn.conv2d_transpose(tf.nn.bias_add(generative_net['pool2'],bias), tf.shape(generative_net['pool1']),weights, strides=(1, 1, 1, 1), padding='SAME') weights = tf.constant(np.transpose(vgg_layers[0][0][0][2][0][0], (1, 0, 2, 3))) bias = -tf.constant(vgg_layers[0][0][0][2][0][1].reshape(-1)) generative_net['conv1_1'] = tf.nn.conv2d_transpose(tf.nn.bias_add(generative_net['conv2_2'],bias), tf.shape(generative_net['conv1_2']).shape,weights, strides=(1, 1, 1, 1), padding='SAME') generative_net['output'] = tf.Variable(np.zeros(image_style.shape).astype('float32')) # Random draw marginal distribution for layer in VGG19_LAYERS_INTEREST: print(layer) a = net[layer].eval(session=sess) a = a[0] h,w,number_of_features = a.shape #number_samples = h*w #a_reshaped = np.reshape(a,(h*w,number_of_features)) distribs = Distrib_Estimation[layer] generative_filters_response = np.zeros(net[layer].shape, dtype=np.float32) for i in range(number_of_features): print(i) beta, loc, scale = distribs[i,:] r = stats.gennorm.rvs(beta,loc=loc,scale=scale, size=(h,w)) generative_filters_response[1,:,:,i] = r sess.run(net['conv3_1_input'].assign(generative_filters_response)) print('End generative initialisation') result_img = sess.run(net['input']) result_img_postproc = st.postprocess(result_img) scipy.misc.toimage(result_img_postproc).save(output_image_path) def generateArt(args): if args.verbose: print("verbosity turned on") output_image_path = args.img_folder + args.output_img_name +args.img_ext image_style_path = args.img_folder + args.style_img_name + args.img_ext image_style = st.preprocess(scipy.misc.imread(image_style_path).astype('float32')) _,image_h_art, image_w_art, _ = image_style.shape t1 = time.time() vgg_layers = st.get_vgg_layers() net = st.net_preloaded(vgg_layers, image_style) # The output image as the same size as the content one t2 = time.time() if(args.verbose): print("net loaded and gram computation after ",t2-t1," s") try: sess = tf.Session() init_img = st.get_init_noise_img(image_style,1) loss_total = hist_style_loss(sess,net,image_style) if(args.verbose): print("init loss total") print(tf.trainable_variables()) #optimizer = tf.train.AdamOptimizer(args.learning_rate) # Gradient Descent #train = optimizer.minimize(loss_total) bnds = st.get_lbfgs_bnds(init_img) optimizer_kwargs = {'maxiter': args.max_iter,'iprint': args.print_iter} optimizer = tf.contrib.opt.ScipyOptimizerInterface(loss_total,bounds=bnds, method='L-BFGS-B',options=optimizer_kwargs) sess.run(tf.global_variables_initializer()) sess.run(net['input'].assign(init_img)) # This line must be after variables initialization ! optimizer.minimize(sess) t3 = time.time() if(args.verbose): print("sess Adam initialized after ",t3-t2," s") if(args.verbose): print("loss before optimization") if(args.verbose): print(sess.run(loss_total)) # for i in range(args.max_iter): # if(i%args.print_iter==0): # t3 = time.time() # sess.run(train) # t4 = time.time() # if(args.verbose): print("Iteration ",i, "after ",t4-t3," s") # if(args.verbose): print(sess.run(loss_total)) # result_img = sess.run(net['input']) # result_img_postproc = st.postprocess(result_img) # scipy.misc.toimage(result_img_postproc).save(output_image_path) # else: # sess.run(train) except: print("Error") result_img = sess.run(net['input']) result_img_postproc = st.postprocess(result_img) output_image_path_error = args.img_folder + args.output_img_name+'_error' +args.img_ext scipy.misc.toimage(result_img_postproc).save(output_image_path_error) raise finally: if(args.verbose): print("Close Sess") sess.close() def hist_style_loss(sess,net,style_img): #value_range = [-2000.0,2000.0] # TODO change according to the layer value_range = [-2000.0,2000.0] style_value_range = {'conv1_1' : [-200.0,200.0],'conv2_1': [-500.0,500.0],'conv3_1' : [-2000.0,2000.0] } nbins = 2048 style_layers = [('conv1_1',1.),('conv2_1',1.),('conv3_1',1.)] #style_layers = [('conv1_1',1.)] #style_layers_size = {'conv1' : 64,'conv2' : 128,'conv3' : 256,'conv4': 512,'conv5' : 512} length_style_layers = float(len(style_layers)) sess.run(net['input'].assign(style_img)) style_loss = 0.0 weight_help_convergence = 10**9 for layer, weight in style_layers: value_range = style_value_range[layer] style_loss_layer = 0.0 a = sess.run(net[layer]) _,h,w,N = a.shape M =h*w tf_M = tf.to_int32(M) tf_N = tf.to_int32(N) a_reshaped = tf.reshape(a,[tf_M,tf_N]) a_split = tf.unstack(a_reshaped,axis=1) x = net[layer] #print("x.op",x.op) x_reshaped = tf.reshape(x,[tf_M,tf_N]) x_split = tf.unstack(x_reshaped,axis=1) for a_slide,x_slide in zip(a_split,x_split): # N iteration # Descripteur des representations des histogrammes moment d'ordre 1 a N #hist_a = hist(a_slide,value_range, nbins=nbins,dtype=tf.float32) #hist_x = hist(x_slide,value_range, nbins=nbins,dtype=tf.float32) hist_a = tf.histogram_fixed_width(a_slide, value_range, nbins=nbins,dtype=tf.float32) hist_x = tf.histogram_fixed_width(x_slide, value_range, nbins=nbins,dtype=tf.float32) #hist_a = tf.floor(a_slide) #hist_x = tf.floor(x_slide) # TODO normalized les histogrammes avant le calcul plutot qu'apres #style_loss_layer += tf.to_float(tf.reduce_mean(tf.abs(hist_a- hist_x))) # norm L1 #style_loss_layer += tf.reduce_mean(tf.pow(hist_a- hist_x,2)) # Norm L2 style_loss_layer += tf.sqrt(1-tf.reduce_sum(tf.multiply(tf.sqrt(hist_a),tf.sqrt(hist_x)))) # TODO use bhattacharyya distance style_loss_layer *= weight * weight_help_convergence / (2.*tf.to_float(N**2)*tf.to_float(M**2)*length_style_layers) style_loss += style_loss_layer return(style_loss) def do_pdf_comparison(args): directory_path = 'Results/Rep/'+args.style_img_name+'/' if not os.path.exists(directory_path): os.makedirs(directory_path) sns.set_style("white") image_style_path = args.img_folder + args.style_img_name + args.img_ext image_style = st.preprocess(scipy.misc.imread(image_style_path).astype('float32')) _,image_h_art, image_w_art, _ = image_style.shape vgg_layers = st.get_vgg_layers() net = st.net_preloaded(vgg_layers, image_style) # net for the style image placeholder = tf.placeholder(tf.float32, shape=image_style.shape) assign_op = net['input'].assign(placeholder) sess = tf.Session() sess.run(tf.global_variables_initializer()) sess.run(assign_op, {placeholder: image_style}) sess.graph.finalize() for layer in VGG19_LAYERS_INTEREST: a = net[layer].eval(session=sess) print(layer,a.shape) plot_compare_pdf(vgg_layers,a,directory_path,layer) def plot_compare_pdf(vgg_layers,Matrix,path,name): number_img_large_tab = {'conv1_1' : 1,'conv1_2' : 4,'conv2_1' : 4,'conv2_2' : 8, 'conv3_1' : 8,'conv3_2' : 8,'conv3_3' : 16,'conv3_4' : 16,'conv4_1' : 16, 'conv4_2' : 16,'conv4_3' : 16,'conv4_4' : 16,'conv5_1' : 16,'conv5_2' : 16, 'conv5_3' : 16,'conv5_4' : 16} pltname = path+name+'_comp.pdf' pp = PdfPages(pltname) Matrix = Matrix[0] # Remove first dim h,w,channels = Matrix.shape Matrix_reshaped = np.reshape(Matrix,(h*w,channels)) df_Matrix = pd.DataFrame(Matrix_reshaped) len_columns = len(df_Matrix.columns) index_in_vgg = VGG19_LAYERS_INDICES[name] kernels = vgg_layers[index_in_vgg][0][0][2][0][0] # A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels] print(kernels.shape) bias = vgg_layers[index_in_vgg][0][0][2][0][1] print(bias.shape) #len_columns = 1 input_kernel = kernels.shape[2] alpha=0.75 #cmkernel = 'gray' cmImg = 'jet' cmkernel = plt.get_cmap('hot') for i in range(len_columns): #print("Features",i) # For each feature f = plt.figure() gs0 = gridspec.GridSpec(1,3, width_ratios=[0.05,4,4]) # 2 columns axcm = plt.subplot(gs0[0]) number_img_large = number_img_large_tab[name] if(not(name=='conv1_1')): gs00 = gridspec.GridSpecFromSubplotSpec(input_kernel//number_img_large, number_img_large, subplot_spec=gs0[1]) axes = [] for j in range(input_kernel): ax = plt.subplot(gs00[j]) axes += [ax] kernel = kernels[:,:,:,i] mean_kernel = np.mean(kernel) bias_i = bias[i,0] j = 0 vmin = np.min(kernel) vmax = np.max(kernel) for ax in axes: im = ax.matshow(kernel[:,:,j],cmap=cmkernel,alpha=alpha,vmin=vmin, vmax=vmax) ax.axis('off') j += 1 else: gs00 = gridspec.GridSpecFromSubplotSpec(4, 1, subplot_spec=gs0[1]) axes = [] for j in range(input_kernel): ax = plt.subplot(gs00[j]) axes += [ax] kernel = kernels[:,:,:,i] mean_kernel = np.mean(kernel) bias_i = bias[i,0] j = 0 vmin = np.min(kernel) vmax = np.max(kernel) for ax in axes: im = ax.matshow(kernel[:,:,j],cmap=cmkernel,alpha=alpha,vmin=vmin, vmax=vmax) ax.axis('off') j += 1 ax0 = plt.subplot(gs00[3]) # bgr to rgb img = kernel[...,::-1] #img = Image.fromarray(img, 'RGB') #img = exposure.rescale_intensity(img, in_range='uint8') img -= np.min(img) img /= (np.max(img)/255.) img = np.floor(img).astype('uint8') ax0.imshow(img) ax0.axis('off') ax0.set_title('Color Kernel') plt.colorbar(im, cax=axcm) #plt.colorbar(im, cax=axes[-1]) #f.subplots_adjust(right=0.8) #cbar_ax = f.add_axes([0.85, 0.15, 0.05, 0.7]) #f.colorbar(im, cax=cbar_ax) #plt.colorbar(im, cax=axes) gs01 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[2],height_ratios=[4,3]) ax1 = plt.subplot(gs01[1]) ax2 = plt.subplot(gs01[0]) samples = Matrix_reshaped[:,i] beta, loc, scale = stats.gennorm.fit(samples) D,pvalue = stats.kstest(samples, 'gennorm',(beta, loc, scale )) # 1-alph = 0.9 #D,pvalue = stats.stats.ks_2samp(samples,stats.gennorm.rvs(beta,loc=loc,scale=scale,size=len(samples) )) Dcritial = 1.224/math.sqrt(len(samples)) #print("pvalue",pvalue) df_Matrix.hist(column = i, bins = 128,ax=ax1,normed=True, histtype='stepfilled', alpha=1) x = np.linspace(stats.gennorm.ppf(0.005, beta, loc, scale), stats.gennorm.ppf(0.995, beta, loc, scale), 128) ax1.plot(x, stats.gennorm.pdf(x, beta, loc, scale ),'r-',alpha=0.4, label='gennorm pdf') #ax1.legend(loc='best', frameon=False) #textstr = '$\mu=%.2f$\n$\mathrm{scale}=%.2f$\n$beta=%.2f$ \n $\mathrm{D}=%.4f$ \n $\mathrm{Dcri}=%.4f$ '%(loc,scale,beta,D,Dcritial) textstr = '$\mu=%.2f$\n$\mathrm{scale}=%.2f$\n$beta=%.2f$\n$\mathrm{pvalue}=%.4f$'%(loc,scale,beta,pvalue) props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) ax1.text(0.05, 0.95, textstr, transform=ax1.transAxes, fontsize=6, verticalalignment='top', bbox=props) ax2.matshow(Matrix[:,:,i], cmap=cmImg) ax2.axis('off') titre = 'Kernel {} with mean = {:.2e} in range [{:.2e},{:.2e}] and bias = {:.2e}'.format(i,mean_kernel,vmin,vmax,bias_i) plt.suptitle(titre) #gs0.tight_layout(f) plt.savefig(pp, format='pdf') plt.close() pp.close() plt.clf() def main_plot(name=None): """ Plot the reponse to the filters/kernels and histogram in differents pdfs """ parser = get_parser_args() if(name==None): style_img_name = "StarryNight" else: style_img_name = name #style_img_name = "Louvre_Big" parser.set_defaults(style_img_name=style_img_name) args = parser.parse_args() plot_Rep(args) def main_distrib(name=None): """ Estimate the distribution of the distribution """ parser = get_parser_args() if(name==None): style_img_name = "StarryNight" else: style_img_name = name parser.set_defaults(style_img_name=style_img_name) args = parser.parse_args() estimate_gennorm(args) def main_plot_commun(name=None): """ Plot for each layer in VGG Interest the kernels, the response of the kernel but also the histogram fitted """ parser = get_parser_args() if(name==None): style_img_name = "StarryNight" else: style_img_name = name parser.set_defaults(style_img_name=style_img_name) args = parser.parse_args() do_pdf_comparison(args) if __name__ == '__main__': main_plot_commun('grad_Uniform')
gpl-3.0
htygithub/bokeh
examples/glyphs/trail.py
2
4262
# -*- coding: utf-8 -*- from __future__ import print_function from math import sin, cos, atan2, sqrt, radians import numpy as np import scipy.ndimage as im from bokeh.document import Document from bokeh.embed import file_html from bokeh.resources import INLINE from bokeh.browserlib import view from bokeh.models.glyphs import Line, Patches from bokeh.models.widgets import VBox from bokeh.models import ( Plot, GMapPlot, GMapOptions, DataRange1d, ColumnDataSource, LinearAxis, Grid, PanTool, WheelZoomTool, ResetTool) from bokeh.sampledata.mtb import obiszow_mtb_xcm def haversin(theta): return sin(0.5 * theta) ** 2 def distance(p1, p2): """Distance between (lat1, lon1) and (lat2, lon2). """ R = 6371 lat1, lon1 = p1 lat2, lon2 = p2 phi1 = radians(lat1) phi2 = radians(lat2) delta_lat = radians(lat2 - lat1) delta_lon = radians(lon2 - lon1) a = haversin(delta_lat) + cos(phi1) * cos(phi2) * haversin(delta_lon) return 2 * R * atan2(sqrt(a), sqrt(1 - a)) def prep_data(dataset): df = dataset.copy() latlon = list(zip(df.lat, df.lon)) dist = np.array([distance(latlon[i + 1], latlon[i]) for i in range(len((latlon[:-1])))]) df["dist"] = np.concatenate(([0], np.cumsum(dist))) slope = np.abs(100 * np.diff(df.alt) / (1000 * dist)) slope[np.where( slope < 4) ] = 0 # "green" slope[np.where((slope >= 4) & (slope < 6))] = 1 # "yellow" slope[np.where((slope >= 6) & (slope < 10))] = 2 # "pink" slope[np.where((slope >= 10) & (slope < 15))] = 3 # "orange" slope[np.where( slope >= 15 )] = 4 # "red" slope = im.median_filter(slope, 6) colors = np.empty_like(slope, dtype=object) colors[np.where(slope == 0)] = "green" colors[np.where(slope == 1)] = "yellow" colors[np.where(slope == 2)] = "pink" colors[np.where(slope == 3)] = "orange" colors[np.where(slope == 4)] = "red" df["colors"] = list(colors) + [None] # NOTE: add [None] just make pandas happy return df title = "Obiszów MTB XCM" def trail_map(data): lon = (min(data.lon) + max(data.lon)) / 2 lat = (min(data.lat) + max(data.lat)) / 2 map_options = GMapOptions(lng=lon, lat=lat, zoom=13) plot = GMapPlot(title="%s - Trail Map" % title, map_options=map_options, plot_width=800, plot_height=800) plot.x_range = DataRange1d() plot.y_range = DataRange1d() plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) line_source = ColumnDataSource(dict(x=data.lon, y=data.lat, dist=data.dist)) line = Line(x="x", y="y", line_color="blue", line_width=2) plot.add_glyph(line_source, line) return plot def altitude_profile(data): plot = Plot(title="%s - Altitude Profile" % title, plot_width=800, plot_height=400) plot.x_range = DataRange1d() plot.y_range = DataRange1d() xaxis = LinearAxis(axis_label="Distance (km)") plot.add_layout(xaxis, 'below') yaxis = LinearAxis(axis_label="Altitude (m)") plot.add_layout(yaxis, 'left') xgrid = Grid(plot=plot, dimension=0, ticker=xaxis.ticker) ygrid = Grid(plot=plot, dimension=1, ticker=yaxis.ticker) plot.renderers.extend([xgrid, ygrid]) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) X, Y = data.dist, data.alt y0 = min(Y) patches_source = ColumnDataSource(dict( xs=[[X[i], X[i+1], X[i+1], X[i]] for i in range(len(X[:-1])) ], ys=[[y0, y0, Y[i+1], Y[i]] for i in range(len(Y[:-1])) ], color=data.colors[:-1] )) patches = Patches(xs="xs", ys="ys", fill_color="color", line_color="color") plot.add_glyph(patches_source, patches) line_source = ColumnDataSource(dict(x=data.dist, y=data.alt)) line = Line(x='x', y='y', line_color="black", line_width=1) plot.add_glyph(line_source, line) return plot data = prep_data(obiszow_mtb_xcm) trail = trail_map(data) altitude = altitude_profile(data) layout = VBox(children=[altitude, trail]) doc = Document() doc.add_root(layout) if __name__ == "__main__": filename = "trail.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Trail map and altitude profile")) print("Wrote %s" % filename) view(filename)
bsd-3-clause
fspaolo/scikit-learn
sklearn/datasets/tests/test_base.py
8
5607
import os import shutil import tempfile import warnings import nose import numpy from sklearn.datasets import get_data_home from sklearn.datasets import clear_data_home from sklearn.datasets import load_files from sklearn.datasets import load_sample_images from sklearn.datasets import load_sample_image from sklearn.datasets import load_digits from sklearn.datasets import load_diabetes from sklearn.datasets import load_linnerud from sklearn.datasets import load_iris from sklearn.datasets import load_boston from sklearn.externals.six import b, u from sklearn.utils.testing import assert_false from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_raises DATA_HOME = tempfile.mkdtemp(prefix="scikit_learn_data_home_test_") LOAD_FILES_ROOT = tempfile.mkdtemp(prefix="scikit_learn_load_files_test_") TEST_CATEGORY_DIR1 = "" TEST_CATEGORY_DIR2 = "" def _remove_dir(path): if os.path.isdir(path): shutil.rmtree(path) def teardown_module(): """Test fixture (clean up) run once after all tests of this module""" for path in [DATA_HOME, LOAD_FILES_ROOT]: _remove_dir(path) def setup_load_files(): global TEST_CATEGORY_DIR1 global TEST_CATEGORY_DIR2 TEST_CATEGORY_DIR1 = tempfile.mkdtemp(dir=LOAD_FILES_ROOT) TEST_CATEGORY_DIR2 = tempfile.mkdtemp(dir=LOAD_FILES_ROOT) sample_file = tempfile.NamedTemporaryFile(dir=TEST_CATEGORY_DIR1, delete=False) sample_file.write(b("Hello World!\n")) sample_file.close() def teardown_load_files(): _remove_dir(TEST_CATEGORY_DIR1) _remove_dir(TEST_CATEGORY_DIR2) def test_data_home(): # get_data_home will point to a pre-existing folder data_home = get_data_home(data_home=DATA_HOME) assert_equal(data_home, DATA_HOME) assert_true(os.path.exists(data_home)) # clear_data_home will delete both the content and the folder it-self clear_data_home(data_home=data_home) assert_false(os.path.exists(data_home)) # if the folder is missing it will be created again data_home = get_data_home(data_home=DATA_HOME) assert_true(os.path.exists(data_home)) def test_default_empty_load_files(): res = load_files(LOAD_FILES_ROOT) assert_equal(len(res.filenames), 0) assert_equal(len(res.target_names), 0) assert_equal(res.DESCR, None) @nose.tools.with_setup(setup_load_files, teardown_load_files) def test_default_load_files(): res = load_files(LOAD_FILES_ROOT) assert_equal(len(res.filenames), 1) assert_equal(len(res.target_names), 2) assert_equal(res.DESCR, None) assert_equal(res.data, [b("Hello World!\n")]) @nose.tools.with_setup(setup_load_files, teardown_load_files) def test_load_files_w_categories_desc_and_encoding(): category = os.path.abspath(TEST_CATEGORY_DIR1).split('/').pop() res = load_files(LOAD_FILES_ROOT, description="test", categories=category, encoding="utf-8") assert_equal(len(res.filenames), 1) assert_equal(len(res.target_names), 1) assert_equal(res.DESCR, "test") assert_equal(res.data, [u("Hello World!\n")]) @nose.tools.with_setup(setup_load_files, teardown_load_files) def test_load_files_wo_load_content(): res = load_files(LOAD_FILES_ROOT, load_content=False) assert_equal(len(res.filenames), 1) assert_equal(len(res.target_names), 2) assert_equal(res.DESCR, None) assert_equal(res.get('data'), None) def test_load_sample_images(): try: res = load_sample_images() assert_equal(len(res.images), 2) assert_equal(len(res.filenames), 2) assert_true(res.DESCR) except ImportError: warnings.warn("Could not load sample images, PIL is not available.") def test_load_digits(): digits = load_digits() assert_equal(digits.data.shape, (1797, 64)) assert_equal(numpy.unique(digits.target).size, 10) def test_load_digits_n_class_lt_10(): digits = load_digits(9) assert_equal(digits.data.shape, (1617, 64)) assert_equal(numpy.unique(digits.target).size, 9) def test_load_sample_image(): try: china = load_sample_image('china.jpg') assert_equal(china.dtype, 'uint8') assert_equal(china.shape, (427, 640, 3)) except ImportError: warnings.warn("Could not load sample images, PIL is not available.") def test_load_missing_sample_image_error(): have_PIL = True try: try: from scipy.misc import imread except ImportError: from scipy.misc.pilutil import imread except ImportError: have_PIL = False if have_PIL: assert_raises(AttributeError, load_sample_image, 'blop.jpg') else: warnings.warn("Could not load sample images, PIL is not available.") def test_load_diabetes(): res = load_diabetes() assert_equal(res.data.shape, (442, 10)) assert_true(res.target.size, 442) def test_load_linnerud(): res = load_linnerud() assert_equal(res.data.shape, (20, 3)) assert_equal(res.target.shape, (20, 3)) assert_equal(len(res.target_names), 3) assert_true(res.DESCR) def test_load_iris(): res = load_iris() assert_equal(res.data.shape, (150, 4)) assert_equal(res.target.size, 150) assert_equal(res.target_names.size, 3) assert_true(res.DESCR) def test_load_boston(): res = load_boston() assert_equal(res.data.shape, (506, 13)) assert_equal(res.target.size, 506) assert_equal(res.feature_names.size, 14) assert_true(res.DESCR)
bsd-3-clause
glouppe/scikit-learn
sklearn/linear_model/tests/test_randomized_l1.py
35
4726
# Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # License: BSD 3 clause import numpy as np 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.linear_model.randomized_l1 import (lasso_stability_path, RandomizedLasso, RandomizedLogisticRegression) from sklearn.datasets import load_diabetes, load_iris from sklearn.feature_selection import f_regression, f_classif from sklearn.preprocessing import StandardScaler from sklearn.linear_model.base import center_data diabetes = load_diabetes() X = diabetes.data y = diabetes.target X = StandardScaler().fit_transform(X) X = X[:, [2, 3, 6, 7, 8]] # test that the feature score of the best features F, _ = f_regression(X, y) def test_lasso_stability_path(): # Check lasso stability path # Load diabetes data and add noisy features scaling = 0.3 coef_grid, scores_path = lasso_stability_path(X, y, scaling=scaling, random_state=42, n_resampling=30) assert_array_equal(np.argsort(F)[-3:], np.argsort(np.sum(scores_path, axis=1))[-3:]) def test_randomized_lasso(): # Check randomized lasso scaling = 0.3 selection_threshold = 0.5 # or with 1 alpha clf = RandomizedLasso(verbose=False, alpha=1, random_state=42, scaling=scaling, selection_threshold=selection_threshold) feature_scores = clf.fit(X, y).scores_ assert_array_equal(np.argsort(F)[-3:], np.argsort(feature_scores)[-3:]) # or with many alphas clf = RandomizedLasso(verbose=False, alpha=[1, 0.8], random_state=42, scaling=scaling, selection_threshold=selection_threshold) feature_scores = clf.fit(X, y).scores_ assert_equal(clf.all_scores_.shape, (X.shape[1], 2)) assert_array_equal(np.argsort(F)[-3:], np.argsort(feature_scores)[-3:]) X_r = clf.transform(X) X_full = clf.inverse_transform(X_r) assert_equal(X_r.shape[1], np.sum(feature_scores > selection_threshold)) assert_equal(X_full.shape, X.shape) clf = RandomizedLasso(verbose=False, alpha='aic', random_state=42, scaling=scaling) feature_scores = clf.fit(X, y).scores_ assert_array_equal(feature_scores, X.shape[1] * [1.]) clf = RandomizedLasso(verbose=False, scaling=-0.1) assert_raises(ValueError, clf.fit, X, y) clf = RandomizedLasso(verbose=False, scaling=1.1) assert_raises(ValueError, clf.fit, X, y) def test_randomized_logistic(): # Check randomized sparse logistic regression iris = load_iris() X = iris.data[:, [0, 2]] y = iris.target X = X[y != 2] y = y[y != 2] F, _ = f_classif(X, y) scaling = 0.3 clf = RandomizedLogisticRegression(verbose=False, C=1., random_state=42, scaling=scaling, n_resampling=50, tol=1e-3) X_orig = X.copy() feature_scores = clf.fit(X, y).scores_ assert_array_equal(X, X_orig) # fit does not modify X assert_array_equal(np.argsort(F), np.argsort(feature_scores)) clf = RandomizedLogisticRegression(verbose=False, C=[1., 0.5], random_state=42, scaling=scaling, n_resampling=50, tol=1e-3) feature_scores = clf.fit(X, y).scores_ assert_array_equal(np.argsort(F), np.argsort(feature_scores)) def test_randomized_logistic_sparse(): # Check randomized sparse logistic regression on sparse data iris = load_iris() X = iris.data[:, [0, 2]] y = iris.target X = X[y != 2] y = y[y != 2] # center here because sparse matrices are usually not centered # labels should not be centered X, _, _, _, _ = center_data(X, y, True, True) X_sp = sparse.csr_matrix(X) F, _ = f_classif(X, y) scaling = 0.3 clf = RandomizedLogisticRegression(verbose=False, C=1., random_state=42, scaling=scaling, n_resampling=50, tol=1e-3) feature_scores = clf.fit(X, y).scores_ clf = RandomizedLogisticRegression(verbose=False, C=1., random_state=42, scaling=scaling, n_resampling=50, tol=1e-3) feature_scores_sp = clf.fit(X_sp, y).scores_ assert_array_equal(feature_scores, feature_scores_sp)
bsd-3-clause
ClimbsRocks/scikit-learn
sklearn/cluster/bicluster.py
66
19850
"""Spectral biclustering algorithms. Authors : Kemal Eren License: BSD 3 clause """ from abc import ABCMeta, abstractmethod import numpy as np from scipy.sparse import dia_matrix from scipy.sparse import issparse from . import KMeans, MiniBatchKMeans from ..base import BaseEstimator, BiclusterMixin from ..externals import six from ..utils import check_random_state from ..utils.arpack import eigsh, svds from ..utils.extmath import (make_nonnegative, norm, randomized_svd, safe_sparse_dot) from ..utils.validation import assert_all_finite, check_array __all__ = ['SpectralCoclustering', 'SpectralBiclustering'] def _scale_normalize(X): """Normalize ``X`` by scaling rows and columns independently. Returns the normalized matrix and the row and column scaling factors. """ X = make_nonnegative(X) row_diag = np.asarray(1.0 / np.sqrt(X.sum(axis=1))).squeeze() col_diag = np.asarray(1.0 / np.sqrt(X.sum(axis=0))).squeeze() row_diag = np.where(np.isnan(row_diag), 0, row_diag) col_diag = np.where(np.isnan(col_diag), 0, col_diag) if issparse(X): n_rows, n_cols = X.shape r = dia_matrix((row_diag, [0]), shape=(n_rows, n_rows)) c = dia_matrix((col_diag, [0]), shape=(n_cols, n_cols)) an = r * X * c else: an = row_diag[:, np.newaxis] * X * col_diag return an, row_diag, col_diag def _bistochastic_normalize(X, max_iter=1000, tol=1e-5): """Normalize rows and columns of ``X`` simultaneously so that all rows sum to one constant and all columns sum to a different constant. """ # According to paper, this can also be done more efficiently with # deviation reduction and balancing algorithms. X = make_nonnegative(X) X_scaled = X dist = None for _ in range(max_iter): X_new, _, _ = _scale_normalize(X_scaled) if issparse(X): dist = norm(X_scaled.data - X.data) else: dist = norm(X_scaled - X_new) X_scaled = X_new if dist is not None and dist < tol: break return X_scaled def _log_normalize(X): """Normalize ``X`` according to Kluger's log-interactions scheme.""" X = make_nonnegative(X, min_value=1) if issparse(X): raise ValueError("Cannot compute log of a sparse matrix," " because log(x) diverges to -infinity as x" " goes to 0.") L = np.log(X) row_avg = L.mean(axis=1)[:, np.newaxis] col_avg = L.mean(axis=0) avg = L.mean() return L - row_avg - col_avg + avg class BaseSpectral(six.with_metaclass(ABCMeta, BaseEstimator, BiclusterMixin)): """Base class for spectral biclustering.""" @abstractmethod def __init__(self, n_clusters=3, svd_method="randomized", n_svd_vecs=None, mini_batch=False, init="k-means++", n_init=10, n_jobs=1, random_state=None): self.n_clusters = n_clusters self.svd_method = svd_method self.n_svd_vecs = n_svd_vecs self.mini_batch = mini_batch self.init = init self.n_init = n_init self.n_jobs = n_jobs self.random_state = random_state def _check_parameters(self): legal_svd_methods = ('randomized', 'arpack') if self.svd_method not in legal_svd_methods: raise ValueError("Unknown SVD method: '{0}'. svd_method must be" " one of {1}.".format(self.svd_method, legal_svd_methods)) def fit(self, X): """Creates a biclustering for X. Parameters ---------- X : array-like, shape (n_samples, n_features) """ X = check_array(X, accept_sparse='csr', dtype=np.float64) self._check_parameters() self._fit(X) def _svd(self, array, n_components, n_discard): """Returns first `n_components` left and right singular vectors u and v, discarding the first `n_discard`. """ if self.svd_method == 'randomized': kwargs = {} if self.n_svd_vecs is not None: kwargs['n_oversamples'] = self.n_svd_vecs u, _, vt = randomized_svd(array, n_components, random_state=self.random_state, **kwargs) elif self.svd_method == 'arpack': u, _, vt = svds(array, k=n_components, ncv=self.n_svd_vecs) if np.any(np.isnan(vt)): # some eigenvalues of A * A.T are negative, causing # sqrt() to be np.nan. This causes some vectors in vt # to be np.nan. A = safe_sparse_dot(array.T, array) random_state = check_random_state(self.random_state) # initialize with [-1,1] as in ARPACK v0 = random_state.uniform(-1, 1, A.shape[0]) _, v = eigsh(A, ncv=self.n_svd_vecs, v0=v0) vt = v.T if np.any(np.isnan(u)): A = safe_sparse_dot(array, array.T) random_state = check_random_state(self.random_state) # initialize with [-1,1] as in ARPACK v0 = random_state.uniform(-1, 1, A.shape[0]) _, u = eigsh(A, ncv=self.n_svd_vecs, v0=v0) assert_all_finite(u) assert_all_finite(vt) u = u[:, n_discard:] vt = vt[n_discard:] return u, vt.T def _k_means(self, data, n_clusters): if self.mini_batch: model = MiniBatchKMeans(n_clusters, init=self.init, n_init=self.n_init, random_state=self.random_state) else: model = KMeans(n_clusters, init=self.init, n_init=self.n_init, n_jobs=self.n_jobs, random_state=self.random_state) model.fit(data) centroid = model.cluster_centers_ labels = model.labels_ return centroid, labels class SpectralCoclustering(BaseSpectral): """Spectral Co-Clustering algorithm (Dhillon, 2001). Clusters rows and columns of an array `X` to solve the relaxed normalized cut of the bipartite graph created from `X` as follows: the edge between row vertex `i` and column vertex `j` has weight `X[i, j]`. The resulting bicluster structure is block-diagonal, since each row and each column belongs to exactly one bicluster. Supports sparse matrices, as long as they are nonnegative. Read more in the :ref:`User Guide <spectral_coclustering>`. Parameters ---------- n_clusters : integer, optional, default: 3 The number of biclusters to find. svd_method : string, optional, default: 'randomized' Selects the algorithm for finding singular vectors. May be 'randomized' or 'arpack'. If 'randomized', use :func:`sklearn.utils.extmath.randomized_svd`, which may be faster for large matrices. If 'arpack', use :func:`sklearn.utils.arpack.svds`, which is more accurate, but possibly slower in some cases. n_svd_vecs : int, optional, default: None Number of vectors to use in calculating the SVD. Corresponds to `ncv` when `svd_method=arpack` and `n_oversamples` when `svd_method` is 'randomized`. mini_batch : bool, optional, default: False Whether to use mini-batch k-means, which is faster but may get different results. init : {'k-means++', 'random' or an ndarray} Method for initialization of k-means algorithm; defaults to 'k-means++'. n_init : int, optional, default: 10 Number of random initializations that are tried with the k-means algorithm. If mini-batch k-means is used, the best initialization is chosen and the algorithm runs once. Otherwise, the algorithm is run for each initialization and the best solution chosen. n_jobs : int, optional, default: 1 The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. 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. random_state : int seed, RandomState instance, or None (default) A pseudo random number generator used by the K-Means initialization. Attributes ---------- rows_ : array-like, shape (n_row_clusters, n_rows) Results of the clustering. `rows[i, r]` is True if cluster `i` contains row `r`. Available only after calling ``fit``. columns_ : array-like, shape (n_column_clusters, n_columns) Results of the clustering, like `rows`. row_labels_ : array-like, shape (n_rows,) The bicluster label of each row. column_labels_ : array-like, shape (n_cols,) The bicluster label of each column. References ---------- * Dhillon, Inderjit S, 2001. `Co-clustering documents and words using bipartite spectral graph partitioning <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.140.3011>`__. """ def __init__(self, n_clusters=3, svd_method='randomized', n_svd_vecs=None, mini_batch=False, init='k-means++', n_init=10, n_jobs=1, random_state=None): super(SpectralCoclustering, self).__init__(n_clusters, svd_method, n_svd_vecs, mini_batch, init, n_init, n_jobs, random_state) def _fit(self, X): normalized_data, row_diag, col_diag = _scale_normalize(X) n_sv = 1 + int(np.ceil(np.log2(self.n_clusters))) u, v = self._svd(normalized_data, n_sv, n_discard=1) z = np.vstack((row_diag[:, np.newaxis] * u, col_diag[:, np.newaxis] * v)) _, labels = self._k_means(z, self.n_clusters) n_rows = X.shape[0] self.row_labels_ = labels[:n_rows] self.column_labels_ = labels[n_rows:] self.rows_ = np.vstack(self.row_labels_ == c for c in range(self.n_clusters)) self.columns_ = np.vstack(self.column_labels_ == c for c in range(self.n_clusters)) class SpectralBiclustering(BaseSpectral): """Spectral biclustering (Kluger, 2003). Partitions rows and columns under the assumption that the data has an underlying checkerboard structure. For instance, if there are two row partitions and three column partitions, each row will belong to three biclusters, and each column will belong to two biclusters. The outer product of the corresponding row and column label vectors gives this checkerboard structure. Read more in the :ref:`User Guide <spectral_biclustering>`. Parameters ---------- n_clusters : integer or tuple (n_row_clusters, n_column_clusters) The number of row and column clusters in the checkerboard structure. method : string, optional, default: 'bistochastic' Method of normalizing and converting singular vectors into biclusters. May be one of 'scale', 'bistochastic', or 'log'. The authors recommend using 'log'. If the data is sparse, however, log normalization will not work, which is why the default is 'bistochastic'. CAUTION: if `method='log'`, the data must not be sparse. n_components : integer, optional, default: 6 Number of singular vectors to check. n_best : integer, optional, default: 3 Number of best singular vectors to which to project the data for clustering. svd_method : string, optional, default: 'randomized' Selects the algorithm for finding singular vectors. May be 'randomized' or 'arpack'. If 'randomized', uses `sklearn.utils.extmath.randomized_svd`, which may be faster for large matrices. If 'arpack', uses `sklearn.utils.arpack.svds`, which is more accurate, but possibly slower in some cases. n_svd_vecs : int, optional, default: None Number of vectors to use in calculating the SVD. Corresponds to `ncv` when `svd_method=arpack` and `n_oversamples` when `svd_method` is 'randomized`. mini_batch : bool, optional, default: False Whether to use mini-batch k-means, which is faster but may get different results. init : {'k-means++', 'random' or an ndarray} Method for initialization of k-means algorithm; defaults to 'k-means++'. n_init : int, optional, default: 10 Number of random initializations that are tried with the k-means algorithm. If mini-batch k-means is used, the best initialization is chosen and the algorithm runs once. Otherwise, the algorithm is run for each initialization and the best solution chosen. n_jobs : int, optional, default: 1 The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. 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. random_state : int seed, RandomState instance, or None (default) A pseudo random number generator used by the K-Means initialization. Attributes ---------- rows_ : array-like, shape (n_row_clusters, n_rows) Results of the clustering. `rows[i, r]` is True if cluster `i` contains row `r`. Available only after calling ``fit``. columns_ : array-like, shape (n_column_clusters, n_columns) Results of the clustering, like `rows`. row_labels_ : array-like, shape (n_rows,) Row partition labels. column_labels_ : array-like, shape (n_cols,) Column partition labels. References ---------- * Kluger, Yuval, et. al., 2003. `Spectral biclustering of microarray data: coclustering genes and conditions <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.135.1608>`__. """ def __init__(self, n_clusters=3, method='bistochastic', n_components=6, n_best=3, svd_method='randomized', n_svd_vecs=None, mini_batch=False, init='k-means++', n_init=10, n_jobs=1, random_state=None): super(SpectralBiclustering, self).__init__(n_clusters, svd_method, n_svd_vecs, mini_batch, init, n_init, n_jobs, random_state) self.method = method self.n_components = n_components self.n_best = n_best def _check_parameters(self): super(SpectralBiclustering, self)._check_parameters() legal_methods = ('bistochastic', 'scale', 'log') if self.method not in legal_methods: raise ValueError("Unknown method: '{0}'. method must be" " one of {1}.".format(self.method, legal_methods)) try: int(self.n_clusters) except TypeError: try: r, c = self.n_clusters int(r) int(c) except (ValueError, TypeError): raise ValueError("Incorrect parameter n_clusters has value:" " {}. It should either be a single integer" " or an iterable with two integers:" " (n_row_clusters, n_column_clusters)") if self.n_components < 1: raise ValueError("Parameter n_components must be greater than 0," " but its value is {}".format(self.n_components)) if self.n_best < 1: raise ValueError("Parameter n_best must be greater than 0," " but its value is {}".format(self.n_best)) if self.n_best > self.n_components: raise ValueError("n_best cannot be larger than" " n_components, but {} > {}" "".format(self.n_best, self.n_components)) def _fit(self, X): n_sv = self.n_components if self.method == 'bistochastic': normalized_data = _bistochastic_normalize(X) n_sv += 1 elif self.method == 'scale': normalized_data, _, _ = _scale_normalize(X) n_sv += 1 elif self.method == 'log': normalized_data = _log_normalize(X) n_discard = 0 if self.method == 'log' else 1 u, v = self._svd(normalized_data, n_sv, n_discard) ut = u.T vt = v.T try: n_row_clusters, n_col_clusters = self.n_clusters except TypeError: n_row_clusters = n_col_clusters = self.n_clusters best_ut = self._fit_best_piecewise(ut, self.n_best, n_row_clusters) best_vt = self._fit_best_piecewise(vt, self.n_best, n_col_clusters) self.row_labels_ = self._project_and_cluster(X, best_vt.T, n_row_clusters) self.column_labels_ = self._project_and_cluster(X.T, best_ut.T, n_col_clusters) self.rows_ = np.vstack(self.row_labels_ == label for label in range(n_row_clusters) for _ in range(n_col_clusters)) self.columns_ = np.vstack(self.column_labels_ == label for _ in range(n_row_clusters) for label in range(n_col_clusters)) def _fit_best_piecewise(self, vectors, n_best, n_clusters): """Find the ``n_best`` vectors that are best approximated by piecewise constant vectors. The piecewise vectors are found by k-means; the best is chosen according to Euclidean distance. """ def make_piecewise(v): centroid, labels = self._k_means(v.reshape(-1, 1), n_clusters) return centroid[labels].ravel() piecewise_vectors = np.apply_along_axis(make_piecewise, axis=1, arr=vectors) dists = np.apply_along_axis(norm, axis=1, arr=(vectors - piecewise_vectors)) result = vectors[np.argsort(dists)[:n_best]] return result def _project_and_cluster(self, data, vectors, n_clusters): """Project ``data`` to ``vectors`` and cluster the result.""" projected = safe_sparse_dot(data, vectors) _, labels = self._k_means(projected, n_clusters) return labels
bsd-3-clause
JoeJimFlood/RugbyPredictifier
2018SuperRugby/round.py
1
9347
import os os.chdir(os.path.dirname(__file__)) import pandas as pd import matchup import ranking import xlsxwriter import xlrd import sys import time import collections import matplotlib.pyplot as plt from math import ceil, sqrt, log2 def rgb2hex(r, g, b): r_hex = hex(r)[-2:].replace('x', '0') g_hex = hex(g)[-2:].replace('x', '0') b_hex = hex(b)[-2:].replace('x', '0') return '#' + r_hex + g_hex + b_hex plot_shape = {1: (1, 1), 2: (1, 2), 3: (2, 2), 4: (2, 2), 5: (2, 3), 6: (2, 3), 7: (3, 3)} round_timer = time.time() round_number = 'Postseason' matchups = collections.OrderedDict() matchups['Saturday'] = [] location = os.getcwd().replace('\\', '/') stadium_file = location + '/StadiumLocs.csv' teamloc_file = location + '/TeamHomes.csv' output_file = location + '/Weekly Forecasts/Round_' + str(round_number) + '.xlsx' output_fig = location + '/Weekly Forecasts/Round_' + str(round_number) + '.png' rankings = ranking.rank(os.path.join(location, 'teamcsvs'), round_number) n_games = 0 for day in matchups: n_games += len(matchups[day]) colours = {} team_formats = {} colour_df = pd.DataFrame.from_csv(location + '/colours.csv') teams = list(colour_df.index) for team in teams: primary = rgb2hex(int(colour_df.loc[team, 'R1']), int(colour_df.loc[team, 'G1']), int(colour_df.loc[team, 'B1'])) secondary = rgb2hex(int(colour_df.loc[team, 'R2']), int(colour_df.loc[team, 'G2']), int(colour_df.loc[team, 'B2'])) colours[team] = (primary, secondary) plt.figure(figsize = (15, 15), dpi = 96) plt.title('Round ' + str(round_number)) counter = 0 stadiums = pd.read_csv(stadium_file, index_col = 0) teamlocs = pd.read_csv(teamloc_file, header = None, index_col = 0)[1] for read_data in range(1): week_book = xlsxwriter.Workbook(output_file) header_format = week_book.add_format({'align': 'center', 'bold': True, 'bottom': True}) index_format = week_book.add_format({'align': 'right', 'bold': True}) score_format = week_book.add_format({'num_format': '#0', 'align': 'right'}) percent_format = week_book.add_format({'num_format': '#0%', 'align': 'right'}) merged_format = week_book.add_format({'num_format': '#0.00', 'align': 'center'}) merged_format2 = week_book.add_format({'num_format': '0.000', 'align': 'center'}) for team in teams: team_formats[team] = week_book.add_format({'align': 'center', 'bold': True, 'border': True, 'bg_color': colours[team][0], 'font_color': colours[team][1]}) for game_time in matchups: if read_data: data_book = xlrd.open_workbook(output_file) data_sheet = data_book.sheet_by_name(game_time) sheet = week_book.add_worksheet(game_time) sheet.write_string(1, 0, 'City', index_format) sheet.write_string(2, 0, 'Quality', index_format) sheet.write_string(3, 0, 'Entropy', index_format) sheet.write_string(4, 0, 'Hype', index_format) sheet.write_string(5, 0, 'Chance of Winning', index_format) sheet.write_string(6, 0, 'Expected Score', index_format) for i in range(1, 20): sheet.write_string(6+i, 0, str(5*i) + 'th Percentile Score', index_format) sheet.write_string(26, 0, 'Chance of Bonus Point Win', index_format) #sheet.write_string(23, 0, 'Chance of 4-Try Bonus Point with Draw', index_format) #sheet.write_string(24, 0, 'Chance of 4-Try Bonus Point with Loss', index_format) sheet.write_string(27, 0, 'Chance of Losing Bonus Point', index_format) sheet.freeze_panes(0, 1) games = matchups[game_time] for i in range(len(games)): home = games[i][0] away = games[i][1] try: venue = games[i][2] except IndexError: venue = teamlocs.loc[home] stadium = stadiums.loc[venue, 'Venue'] city = stadiums.loc[venue, 'City'] country = stadiums.loc[venue, 'Country'] homecol = 3 * i + 1 awaycol = 3 * i + 2 sheet.write_string(0, homecol, home, team_formats[home]) sheet.write_string(0, awaycol, away, team_formats[away]) sheet.write_string(0, awaycol + 1, ' ') if read_data: #Get rid of this as I never use this option anymore sheet.write_number(5, homecol, data_sheet.cell(1, homecol).value, percent_format) sheet.write_number(5, awaycol, data_sheet.cell(1, awaycol).value, percent_format) for rownum in range(6, 26): sheet.write_number(rownum, homecol, data_sheet.cell(rownum, homecol).value, score_format) sheet.write_number(rownum, awaycol, data_sheet.cell(rownum, awaycol).value, score_format) for rownum in range(26, 30): sheet.write_number(rownum, homecol, data_sheet.cell(rownum, homecol).value, percent_format) sheet.write_number(rownum, awaycol, data_sheet.cell(rownum, awaycol).value, percent_format) else: results = matchup.matchup(home, away) probwin = results['ProbWin'] hwin = probwin[home] awin = probwin[away] draw = 1 - hwin - awin #Calculate hype home_ranking = rankings.loc[home, 'Quantile'] away_ranking = rankings.loc[away, 'Quantile'] ranking_factor = (home_ranking + away_ranking)/2 #uncertainty_factor = 1 - (hwin - awin)**2 hp = hwin/(1-draw) ap = awin/(1-draw) entropy = -hp*log2(hp) - ap*log2(ap) hype = 100*ranking_factor*entropy sheet.write_number(5, homecol, probwin[home], percent_format) sheet.write_number(5, awaycol, probwin[away], percent_format) home_dist = results['Scores'][home] away_dist = results['Scores'][away] home_bp = results['Bonus Points'][home] away_bp = results['Bonus Points'][away] sheet.write_number(6, homecol, home_dist['mean'], score_format) sheet.write_number(6, awaycol, away_dist['mean'], score_format) for i in range(1, 20): #print(type(home_dist)) #print(home_dist[str(5*i)+'%']) sheet.merge_range(1, homecol, 1, awaycol, city, merged_format) sheet.merge_range(2, homecol, 2, awaycol, ranking_factor, merged_format2) sheet.merge_range(3, homecol, 3, awaycol, entropy, merged_format2) sheet.merge_range(4, homecol, 4, awaycol, hype, merged_format) sheet.write_number(6+i, homecol, home_dist[str(5*i)+'%'], score_format) sheet.write_number(6+i, awaycol, away_dist[str(5*i)+'%'], score_format) sheet.write_number(26, homecol, home_bp['4-Try Bonus Point with Win'], percent_format) #sheet.write_number(23, homecol, home_bp['Try-Scoring Bonus Point with Draw'], percent_format) #sheet.write_number(24, homecol, home_bp['Try-Scoring Bonus Point with Loss'], percent_format) sheet.write_number(27, homecol, home_bp['Losing Bonus Point'], percent_format) sheet.write_number(26, awaycol, away_bp['4-Try Bonus Point with Win'], percent_format) #sheet.write_number(23, awaycol, away_bp['Try-Scoring Bonus Point with Draw'], percent_format) #sheet.write_number(24, awaycol, away_bp['Try-Scoring Bonus Point with Loss'], percent_format) sheet.write_number(27, awaycol, away_bp['Losing Bonus Point'], percent_format) if i != len(games) - 1: sheet.write_string(0, 3 * i + 3, ' ') counter += 1 if n_games == 5 and counter == 5: plot_pos = 6 elif n_games == 7 and counter == 7: plot_pos = 8 elif n_games == 8 and counter == 8: plot_pos = 9 else: plot_pos = counter plt.subplot(plot_shape[n_games][0], plot_shape[n_games][1], plot_pos) labels = [home[:3], away[:3]]#, 'DRAW'] values = [hp, ap]#, 1 - hwin - awin] colors = [colours[home][0], colours[away][0]]#, '#808080'] ex = 0.05 explode = [ex, ex]#, ex] plt.pie(values, colors = colors, labels = labels, explode = explode, autopct='%.0f%%', startangle = 90, labeldistance = 1, textprops = {'backgroundcolor': '#ffffff', 'ha': 'center', 'va': 'center', 'fontsize': 24}) plt.title(home + ' vs ' + away + '\n' + stadium + '\n' + city + ', ' + country + '\nHype: ' + str(round(hype, 2)), size = 24) plt.axis('equal') week_book.close() plt.savefig(output_fig) print('Round ' + str(round_number) + ' predictions calculated in ' + str(round((time.time() - round_timer) / 60, 2)) + ' minutes')
mit
zymsys/sms-tools
lectures/06-Harmonic-model/plots-code/sines-partials-harmonics.py
24
2020
import numpy as np import matplotlib.pyplot as plt from scipy.signal import hamming, triang, blackmanharris import sys, os, functools, time sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/models/')) import dftModel as DFT import utilFunctions as UF (fs, x) = UF.wavread('../../../sounds/sine-440-490.wav') w = np.hamming(3529) N = 32768 hN = N/2 t = -20 pin = 4850 x1 = x[pin:pin+w.size] mX1, pX1 = DFT.dftAnal(x1, w, N) ploc = UF.peakDetection(mX1, t) pmag = mX1[ploc] iploc, ipmag, ipphase = UF.peakInterp(mX1, pX1, ploc) plt.figure(1, figsize=(9, 6)) plt.subplot(311) plt.plot(fs*np.arange(mX1.size)/float(N), mX1-max(mX1), 'r', lw=1.5) plt.plot(fs * iploc/N, ipmag-max(mX1), marker='x', color='b', alpha=1, linestyle='', markeredgewidth=1.5) plt.axis([200, 1000, -80, 4]) plt.title('mX + peaks (sine-440-490.wav)') (fs, x) = UF.wavread('../../../sounds/vibraphone-C6.wav') w = np.blackman(401) N = 1024 hN = N/2 t = -80 pin = 200 x2 = x[pin:pin+w.size] mX2, pX2 = DFT.dftAnal(x2, w, N) ploc = UF.peakDetection(mX2, t) pmag = mX2[ploc] iploc, ipmag, ipphase = UF.peakInterp(mX2, pX2, ploc) plt.subplot(3,1,2) plt.plot(fs*np.arange(mX2.size)/float(N), mX2-max(mX2), 'r', lw=1.5) plt.plot(fs * iploc/N, ipmag-max(mX2), marker='x', color='b', alpha=1, linestyle='', markeredgewidth=1.5) plt.axis([500,10000,-100,4]) plt.title('mX + peaks (vibraphone-C6.wav)') (fs, x) = UF.wavread('../../../sounds/oboe-A4.wav') w = np.blackman(651) N = 2048 hN = N/2 t = -80 pin = 10000 x3 = x[pin:pin+w.size] mX3, pX3 = DFT.dftAnal(x3, w, N) ploc = UF.peakDetection(mX3, t) pmag = mX3[ploc] iploc, ipmag, ipphase = UF.peakInterp(mX3, pX3, ploc) plt.subplot(3,1,3) plt.plot(fs*np.arange(mX3.size)/float(N), mX3-max(mX3), 'r', lw=1.5) plt.plot(fs * iploc/N, ipmag-max(mX3), marker='x', color='b', alpha=1, linestyle='', markeredgewidth=1.5) plt.axis([0,6000,-70,2]) plt.title('mX + peaks (oboe-A4.wav)') plt.tight_layout() plt.savefig('sines-partials-harmonics.png') plt.show()
agpl-3.0
mattilyra/scikit-learn
sklearn/linear_model/tests/test_sgd.py
21
47783
import pickle import unittest import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_less from sklearn.utils.testing import raises from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_false, assert_true from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_raises_regexp from sklearn.utils.testing import ignore_warnings from sklearn import linear_model, datasets, metrics from sklearn.base import clone from sklearn.linear_model import SGDClassifier, SGDRegressor from sklearn.preprocessing import LabelEncoder, scale, MinMaxScaler from sklearn.linear_model import sgd_fast class SparseSGDClassifier(SGDClassifier): def fit(self, X, y, *args, **kw): X = sp.csr_matrix(X) return super(SparseSGDClassifier, self).fit(X, y, *args, **kw) def partial_fit(self, X, y, *args, **kw): X = sp.csr_matrix(X) return super(SparseSGDClassifier, self).partial_fit(X, y, *args, **kw) def decision_function(self, X): X = sp.csr_matrix(X) return super(SparseSGDClassifier, self).decision_function(X) def predict_proba(self, X): X = sp.csr_matrix(X) return super(SparseSGDClassifier, self).predict_proba(X) class SparseSGDRegressor(SGDRegressor): def fit(self, X, y, *args, **kw): X = sp.csr_matrix(X) return SGDRegressor.fit(self, X, y, *args, **kw) def partial_fit(self, X, y, *args, **kw): X = sp.csr_matrix(X) return SGDRegressor.partial_fit(self, X, y, *args, **kw) def decision_function(self, X, *args, **kw): X = sp.csr_matrix(X) return SGDRegressor.decision_function(self, X, *args, **kw) # Test Data # test sample 1 X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) Y = [1, 1, 1, 2, 2, 2] T = np.array([[-1, -1], [2, 2], [3, 2]]) true_result = [1, 2, 2] # test sample 2; string class labels X2 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5], [1, 1], [0.75, 0.5], [1.5, 1.5], [-1, -1], [0, -0.5], [1, -1]]) Y2 = ["one"] * 3 + ["two"] * 3 + ["three"] * 3 T2 = np.array([[-1.5, 0.5], [1, 2], [0, -2]]) true_result2 = ["one", "two", "three"] # test sample 3 X3 = np.array([[1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0]]) Y3 = np.array([1, 1, 1, 1, 2, 2, 2, 2]) # test sample 4 - two more or less redundant feature groups X4 = np.array([[1, 0.9, 0.8, 0, 0, 0], [1, .84, .98, 0, 0, 0], [1, .96, .88, 0, 0, 0], [1, .91, .99, 0, 0, 0], [0, 0, 0, .89, .91, 1], [0, 0, 0, .79, .84, 1], [0, 0, 0, .91, .95, 1], [0, 0, 0, .93, 1, 1]]) Y4 = np.array([1, 1, 1, 1, 2, 2, 2, 2]) iris = datasets.load_iris() # test sample 5 - test sample 1 as binary classification problem X5 = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) Y5 = [1, 1, 1, 2, 2, 2] true_result5 = [0, 1, 1] # Classification Test Case class CommonTest(object): def factory(self, **kwargs): if "random_state" not in kwargs: kwargs["random_state"] = 42 return self.factory_class(**kwargs) # a simple implementation of ASGD to use for testing # uses squared loss to find the gradient def asgd(self, X, y, eta, alpha, weight_init=None, intercept_init=0.0): if weight_init is None: weights = np.zeros(X.shape[1]) else: weights = weight_init average_weights = np.zeros(X.shape[1]) intercept = intercept_init average_intercept = 0.0 decay = 1.0 # sparse data has a fixed decay of .01 if (isinstance(self, SparseSGDClassifierTestCase) or isinstance(self, SparseSGDRegressorTestCase)): decay = .01 for i, entry in enumerate(X): p = np.dot(entry, weights) p += intercept gradient = p - y[i] weights *= 1.0 - (eta * alpha) weights += -(eta * gradient * entry) intercept += -(eta * gradient) * decay average_weights *= i average_weights += weights average_weights /= i + 1.0 average_intercept *= i average_intercept += intercept average_intercept /= i + 1.0 return average_weights, average_intercept def _test_warm_start(self, X, Y, lr): # Test that explicit warm restart... clf = self.factory(alpha=0.01, eta0=0.01, n_iter=5, shuffle=False, learning_rate=lr) clf.fit(X, Y) clf2 = self.factory(alpha=0.001, eta0=0.01, n_iter=5, shuffle=False, learning_rate=lr) clf2.fit(X, Y, coef_init=clf.coef_.copy(), intercept_init=clf.intercept_.copy()) # ... and implicit warm restart are equivalent. clf3 = self.factory(alpha=0.01, eta0=0.01, n_iter=5, shuffle=False, warm_start=True, learning_rate=lr) clf3.fit(X, Y) assert_equal(clf3.t_, clf.t_) assert_array_almost_equal(clf3.coef_, clf.coef_) clf3.set_params(alpha=0.001) clf3.fit(X, Y) assert_equal(clf3.t_, clf2.t_) assert_array_almost_equal(clf3.coef_, clf2.coef_) def test_warm_start_constant(self): self._test_warm_start(X, Y, "constant") def test_warm_start_invscaling(self): self._test_warm_start(X, Y, "invscaling") def test_warm_start_optimal(self): self._test_warm_start(X, Y, "optimal") def test_input_format(self): # Input format tests. clf = self.factory(alpha=0.01, n_iter=5, shuffle=False) clf.fit(X, Y) Y_ = np.array(Y)[:, np.newaxis] Y_ = np.c_[Y_, Y_] assert_raises(ValueError, clf.fit, X, Y_) def test_clone(self): # Test whether clone works ok. clf = self.factory(alpha=0.01, n_iter=5, penalty='l1') clf = clone(clf) clf.set_params(penalty='l2') clf.fit(X, Y) clf2 = self.factory(alpha=0.01, n_iter=5, penalty='l2') clf2.fit(X, Y) assert_array_equal(clf.coef_, clf2.coef_) def test_plain_has_no_average_attr(self): clf = self.factory(average=True, eta0=.01) clf.fit(X, Y) assert_true(hasattr(clf, 'average_coef_')) assert_true(hasattr(clf, 'average_intercept_')) assert_true(hasattr(clf, 'standard_intercept_')) assert_true(hasattr(clf, 'standard_coef_')) clf = self.factory() clf.fit(X, Y) assert_false(hasattr(clf, 'average_coef_')) assert_false(hasattr(clf, 'average_intercept_')) assert_false(hasattr(clf, 'standard_intercept_')) assert_false(hasattr(clf, 'standard_coef_')) def test_late_onset_averaging_not_reached(self): clf1 = self.factory(average=600) clf2 = self.factory() for _ in range(100): if isinstance(clf1, SGDClassifier): clf1.partial_fit(X, Y, classes=np.unique(Y)) clf2.partial_fit(X, Y, classes=np.unique(Y)) else: clf1.partial_fit(X, Y) clf2.partial_fit(X, Y) assert_array_almost_equal(clf1.coef_, clf2.coef_, decimal=16) assert_almost_equal(clf1.intercept_, clf2.intercept_, decimal=16) def test_late_onset_averaging_reached(self): eta0 = .001 alpha = .0001 Y_encode = np.array(Y) Y_encode[Y_encode == 1] = -1.0 Y_encode[Y_encode == 2] = 1.0 clf1 = self.factory(average=7, learning_rate="constant", loss='squared_loss', eta0=eta0, alpha=alpha, n_iter=2, shuffle=False) clf2 = self.factory(average=0, learning_rate="constant", loss='squared_loss', eta0=eta0, alpha=alpha, n_iter=1, shuffle=False) clf1.fit(X, Y_encode) clf2.fit(X, Y_encode) average_weights, average_intercept = \ self.asgd(X, Y_encode, eta0, alpha, weight_init=clf2.coef_.ravel(), intercept_init=clf2.intercept_) assert_array_almost_equal(clf1.coef_.ravel(), average_weights.ravel(), decimal=16) assert_almost_equal(clf1.intercept_, average_intercept, decimal=16) @raises(ValueError) def test_sgd_bad_alpha_for_optimal_learning_rate(self): # Check whether expected ValueError on bad alpha, i.e. 0 # since alpha is used to compute the optimal learning rate self.factory(alpha=0, learning_rate="optimal") class DenseSGDClassifierTestCase(unittest.TestCase, CommonTest): """Test suite for the dense representation variant of SGD""" factory_class = SGDClassifier def test_sgd(self): # Check that SGD gives any results :-) for loss in ("hinge", "squared_hinge", "log", "modified_huber"): clf = self.factory(penalty='l2', alpha=0.01, fit_intercept=True, loss=loss, n_iter=10, shuffle=True) clf.fit(X, Y) # assert_almost_equal(clf.coef_[0], clf.coef_[1], decimal=7) assert_array_equal(clf.predict(T), true_result) @raises(ValueError) def test_sgd_bad_l1_ratio(self): # Check whether expected ValueError on bad l1_ratio self.factory(l1_ratio=1.1) @raises(ValueError) def test_sgd_bad_learning_rate_schedule(self): # Check whether expected ValueError on bad learning_rate self.factory(learning_rate="<unknown>") @raises(ValueError) def test_sgd_bad_eta0(self): # Check whether expected ValueError on bad eta0 self.factory(eta0=0, learning_rate="constant") @raises(ValueError) def test_sgd_bad_alpha(self): # Check whether expected ValueError on bad alpha self.factory(alpha=-.1) @raises(ValueError) def test_sgd_bad_penalty(self): # Check whether expected ValueError on bad penalty self.factory(penalty='foobar', l1_ratio=0.85) @raises(ValueError) def test_sgd_bad_loss(self): # Check whether expected ValueError on bad loss self.factory(loss="foobar") @raises(ValueError) def test_sgd_n_iter_param(self): # Test parameter validity check self.factory(n_iter=-10000) @raises(ValueError) def test_sgd_shuffle_param(self): # Test parameter validity check self.factory(shuffle="false") @raises(TypeError) def test_argument_coef(self): # Checks coef_init not allowed as model argument (only fit) # Provided coef_ does not match dataset. self.factory(coef_init=np.zeros((3,))).fit(X, Y) @raises(ValueError) def test_provide_coef(self): # Checks coef_init shape for the warm starts # Provided coef_ does not match dataset. self.factory().fit(X, Y, coef_init=np.zeros((3,))) @raises(ValueError) def test_set_intercept(self): # Checks intercept_ shape for the warm starts # Provided intercept_ does not match dataset. self.factory().fit(X, Y, intercept_init=np.zeros((3,))) def test_set_intercept_binary(self): # Checks intercept_ shape for the warm starts in binary case self.factory().fit(X5, Y5, intercept_init=0) def test_average_binary_computed_correctly(self): # Checks the SGDClassifier correctly computes the average weights eta = .1 alpha = 2. n_samples = 20 n_features = 10 rng = np.random.RandomState(0) X = rng.normal(size=(n_samples, n_features)) w = rng.normal(size=n_features) clf = self.factory(loss='squared_loss', learning_rate='constant', eta0=eta, alpha=alpha, fit_intercept=True, n_iter=1, average=True, shuffle=False) # simple linear function without noise y = np.dot(X, w) y = np.sign(y) clf.fit(X, y) average_weights, average_intercept = self.asgd(X, y, eta, alpha) average_weights = average_weights.reshape(1, -1) assert_array_almost_equal(clf.coef_, average_weights, decimal=14) assert_almost_equal(clf.intercept_, average_intercept, decimal=14) def test_set_intercept_to_intercept(self): # Checks intercept_ shape consistency for the warm starts # Inconsistent intercept_ shape. clf = self.factory().fit(X5, Y5) self.factory().fit(X5, Y5, intercept_init=clf.intercept_) clf = self.factory().fit(X, Y) self.factory().fit(X, Y, intercept_init=clf.intercept_) @raises(ValueError) def test_sgd_at_least_two_labels(self): # Target must have at least two labels self.factory(alpha=0.01, n_iter=20).fit(X2, np.ones(9)) def test_partial_fit_weight_class_balanced(self): # partial_fit with class_weight='balanced' not supported""" assert_raises_regexp(ValueError, "class_weight 'balanced' is not supported for " "partial_fit. In order to use 'balanced' weights, " "use compute_class_weight\('balanced', classes, y\). " "In place of y you can us a large enough sample " "of the full training set target to properly " "estimate the class frequency distributions. " "Pass the resulting weights as the class_weight " "parameter.", self.factory(class_weight='balanced').partial_fit, X, Y, classes=np.unique(Y)) def test_sgd_multiclass(self): # Multi-class test case clf = self.factory(alpha=0.01, n_iter=20).fit(X2, Y2) assert_equal(clf.coef_.shape, (3, 2)) assert_equal(clf.intercept_.shape, (3,)) assert_equal(clf.decision_function([[0, 0]]).shape, (1, 3)) pred = clf.predict(T2) assert_array_equal(pred, true_result2) def test_sgd_multiclass_average(self): eta = .001 alpha = .01 # Multi-class average test case clf = self.factory(loss='squared_loss', learning_rate='constant', eta0=eta, alpha=alpha, fit_intercept=True, n_iter=1, average=True, shuffle=False) np_Y2 = np.array(Y2) clf.fit(X2, np_Y2) classes = np.unique(np_Y2) for i, cl in enumerate(classes): y_i = np.ones(np_Y2.shape[0]) y_i[np_Y2 != cl] = -1 average_coef, average_intercept = self.asgd(X2, y_i, eta, alpha) assert_array_almost_equal(average_coef, clf.coef_[i], decimal=16) assert_almost_equal(average_intercept, clf.intercept_[i], decimal=16) def test_sgd_multiclass_with_init_coef(self): # Multi-class test case clf = self.factory(alpha=0.01, n_iter=20) clf.fit(X2, Y2, coef_init=np.zeros((3, 2)), intercept_init=np.zeros(3)) assert_equal(clf.coef_.shape, (3, 2)) assert_true(clf.intercept_.shape, (3,)) pred = clf.predict(T2) assert_array_equal(pred, true_result2) def test_sgd_multiclass_njobs(self): # Multi-class test case with multi-core support clf = self.factory(alpha=0.01, n_iter=20, n_jobs=2).fit(X2, Y2) assert_equal(clf.coef_.shape, (3, 2)) assert_equal(clf.intercept_.shape, (3,)) assert_equal(clf.decision_function([[0, 0]]).shape, (1, 3)) pred = clf.predict(T2) assert_array_equal(pred, true_result2) def test_set_coef_multiclass(self): # Checks coef_init and intercept_init shape for multi-class # problems # Provided coef_ does not match dataset clf = self.factory() assert_raises(ValueError, clf.fit, X2, Y2, coef_init=np.zeros((2, 2))) # Provided coef_ does match dataset clf = self.factory().fit(X2, Y2, coef_init=np.zeros((3, 2))) # Provided intercept_ does not match dataset clf = self.factory() assert_raises(ValueError, clf.fit, X2, Y2, intercept_init=np.zeros((1,))) # Provided intercept_ does match dataset. clf = self.factory().fit(X2, Y2, intercept_init=np.zeros((3,))) def test_sgd_proba(self): # Check SGD.predict_proba # Hinge loss does not allow for conditional prob estimate. # We cannot use the factory here, because it defines predict_proba # anyway. clf = SGDClassifier(loss="hinge", alpha=0.01, n_iter=10).fit(X, Y) assert_false(hasattr(clf, "predict_proba")) assert_false(hasattr(clf, "predict_log_proba")) # log and modified_huber losses can output probability estimates # binary case for loss in ["log", "modified_huber"]: clf = self.factory(loss="modified_huber", alpha=0.01, n_iter=10) clf.fit(X, Y) p = clf.predict_proba([[3, 2]]) assert_true(p[0, 1] > 0.5) p = clf.predict_proba([[-1, -1]]) assert_true(p[0, 1] < 0.5) p = clf.predict_log_proba([[3, 2]]) assert_true(p[0, 1] > p[0, 0]) p = clf.predict_log_proba([[-1, -1]]) assert_true(p[0, 1] < p[0, 0]) # log loss multiclass probability estimates clf = self.factory(loss="log", alpha=0.01, n_iter=10).fit(X2, Y2) d = clf.decision_function([[.1, -.1], [.3, .2]]) p = clf.predict_proba([[.1, -.1], [.3, .2]]) assert_array_equal(np.argmax(p, axis=1), np.argmax(d, axis=1)) assert_almost_equal(p[0].sum(), 1) assert_true(np.all(p[0] >= 0)) p = clf.predict_proba([[-1, -1]]) d = clf.decision_function([[-1, -1]]) assert_array_equal(np.argsort(p[0]), np.argsort(d[0])) l = clf.predict_log_proba([[3, 2]]) p = clf.predict_proba([[3, 2]]) assert_array_almost_equal(np.log(p), l) l = clf.predict_log_proba([[-1, -1]]) p = clf.predict_proba([[-1, -1]]) assert_array_almost_equal(np.log(p), l) # Modified Huber multiclass probability estimates; requires a separate # test because the hard zero/one probabilities may destroy the # ordering present in decision_function output. clf = self.factory(loss="modified_huber", alpha=0.01, n_iter=10) clf.fit(X2, Y2) d = clf.decision_function([[3, 2]]) p = clf.predict_proba([[3, 2]]) if not isinstance(self, SparseSGDClassifierTestCase): assert_equal(np.argmax(d, axis=1), np.argmax(p, axis=1)) else: # XXX the sparse test gets a different X2 (?) assert_equal(np.argmin(d, axis=1), np.argmin(p, axis=1)) # the following sample produces decision_function values < -1, # which would cause naive normalization to fail (see comment # in SGDClassifier.predict_proba) x = X.mean(axis=0) d = clf.decision_function([x]) if np.all(d < -1): # XXX not true in sparse test case (why?) p = clf.predict_proba([x]) assert_array_almost_equal(p[0], [1 / 3.] * 3) def test_sgd_l1(self): # Test L1 regularization n = len(X4) rng = np.random.RandomState(13) idx = np.arange(n) rng.shuffle(idx) X = X4[idx, :] Y = Y4[idx] clf = self.factory(penalty='l1', alpha=.2, fit_intercept=False, n_iter=2000, shuffle=False) clf.fit(X, Y) assert_array_equal(clf.coef_[0, 1:-1], np.zeros((4,))) pred = clf.predict(X) assert_array_equal(pred, Y) # test sparsify with dense inputs clf.sparsify() assert_true(sp.issparse(clf.coef_)) pred = clf.predict(X) assert_array_equal(pred, Y) # pickle and unpickle with sparse coef_ clf = pickle.loads(pickle.dumps(clf)) assert_true(sp.issparse(clf.coef_)) pred = clf.predict(X) assert_array_equal(pred, Y) def test_class_weights(self): # Test class weights. X = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], [1.0, 1.0], [1.0, 0.0]]) y = [1, 1, 1, -1, -1] clf = self.factory(alpha=0.1, n_iter=1000, fit_intercept=False, class_weight=None) clf.fit(X, y) assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([1])) # we give a small weights to class 1 clf = self.factory(alpha=0.1, n_iter=1000, fit_intercept=False, class_weight={1: 0.001}) clf.fit(X, y) # now the hyperplane should rotate clock-wise and # the prediction on this point should shift assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([-1])) def test_equal_class_weight(self): # Test if equal class weights approx. equals no class weights. X = [[1, 0], [1, 0], [0, 1], [0, 1]] y = [0, 0, 1, 1] clf = self.factory(alpha=0.1, n_iter=1000, class_weight=None) clf.fit(X, y) X = [[1, 0], [0, 1]] y = [0, 1] clf_weighted = self.factory(alpha=0.1, n_iter=1000, class_weight={0: 0.5, 1: 0.5}) clf_weighted.fit(X, y) # should be similar up to some epsilon due to learning rate schedule assert_almost_equal(clf.coef_, clf_weighted.coef_, decimal=2) @raises(ValueError) def test_wrong_class_weight_label(self): # ValueError due to not existing class label. clf = self.factory(alpha=0.1, n_iter=1000, class_weight={0: 0.5}) clf.fit(X, Y) @raises(ValueError) def test_wrong_class_weight_format(self): # ValueError due to wrong class_weight argument type. clf = self.factory(alpha=0.1, n_iter=1000, class_weight=[0.5]) clf.fit(X, Y) def test_weights_multiplied(self): # Tests that class_weight and sample_weight are multiplicative class_weights = {1: .6, 2: .3} sample_weights = np.random.random(Y4.shape[0]) multiplied_together = np.copy(sample_weights) multiplied_together[Y4 == 1] *= class_weights[1] multiplied_together[Y4 == 2] *= class_weights[2] clf1 = self.factory(alpha=0.1, n_iter=20, class_weight=class_weights) clf2 = self.factory(alpha=0.1, n_iter=20) clf1.fit(X4, Y4, sample_weight=sample_weights) clf2.fit(X4, Y4, sample_weight=multiplied_together) assert_almost_equal(clf1.coef_, clf2.coef_) def test_balanced_weight(self): # Test class weights for imbalanced data""" # compute reference metrics on iris dataset that is quite balanced by # default X, y = iris.data, iris.target X = scale(X) idx = np.arange(X.shape[0]) rng = np.random.RandomState(6) rng.shuffle(idx) X = X[idx] y = y[idx] clf = self.factory(alpha=0.0001, n_iter=1000, class_weight=None, shuffle=False).fit(X, y) assert_almost_equal(metrics.f1_score(y, clf.predict(X), average='weighted'), 0.96, decimal=1) # make the same prediction using balanced class_weight clf_balanced = self.factory(alpha=0.0001, n_iter=1000, class_weight="balanced", shuffle=False).fit(X, y) assert_almost_equal(metrics.f1_score(y, clf_balanced.predict(X), average='weighted'), 0.96, decimal=1) # Make sure that in the balanced case it does not change anything # to use "balanced" assert_array_almost_equal(clf.coef_, clf_balanced.coef_, 6) # build an very very imbalanced dataset out of iris data X_0 = X[y == 0, :] y_0 = y[y == 0] X_imbalanced = np.vstack([X] + [X_0] * 10) y_imbalanced = np.concatenate([y] + [y_0] * 10) # fit a model on the imbalanced data without class weight info clf = self.factory(n_iter=1000, class_weight=None, shuffle=False) clf.fit(X_imbalanced, y_imbalanced) y_pred = clf.predict(X) assert_less(metrics.f1_score(y, y_pred, average='weighted'), 0.96) # fit a model with balanced class_weight enabled clf = self.factory(n_iter=1000, class_weight="balanced", shuffle=False) clf.fit(X_imbalanced, y_imbalanced) y_pred = clf.predict(X) assert_greater(metrics.f1_score(y, y_pred, average='weighted'), 0.96) # fit another using a fit parameter override clf = self.factory(n_iter=1000, class_weight="balanced", shuffle=False) clf.fit(X_imbalanced, y_imbalanced) y_pred = clf.predict(X) assert_greater(metrics.f1_score(y, y_pred, average='weighted'), 0.96) def test_sample_weights(self): # Test weights on individual samples X = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], [1.0, 1.0], [1.0, 0.0]]) y = [1, 1, 1, -1, -1] clf = self.factory(alpha=0.1, n_iter=1000, fit_intercept=False) clf.fit(X, y) assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([1])) # we give a small weights to class 1 clf.fit(X, y, sample_weight=[0.001] * 3 + [1] * 2) # now the hyperplane should rotate clock-wise and # the prediction on this point should shift assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([-1])) @raises(ValueError) def test_wrong_sample_weights(self): # Test if ValueError is raised if sample_weight has wrong shape clf = self.factory(alpha=0.1, n_iter=1000, fit_intercept=False) # provided sample_weight too long clf.fit(X, Y, sample_weight=np.arange(7)) @raises(ValueError) def test_partial_fit_exception(self): clf = self.factory(alpha=0.01) # classes was not specified clf.partial_fit(X3, Y3) def test_partial_fit_binary(self): third = X.shape[0] // 3 clf = self.factory(alpha=0.01) classes = np.unique(Y) clf.partial_fit(X[:third], Y[:third], classes=classes) assert_equal(clf.coef_.shape, (1, X.shape[1])) assert_equal(clf.intercept_.shape, (1,)) assert_equal(clf.decision_function([[0, 0]]).shape, (1, )) id1 = id(clf.coef_.data) clf.partial_fit(X[third:], Y[third:]) id2 = id(clf.coef_.data) # check that coef_ haven't been re-allocated assert_true(id1, id2) y_pred = clf.predict(T) assert_array_equal(y_pred, true_result) def test_partial_fit_multiclass(self): third = X2.shape[0] // 3 clf = self.factory(alpha=0.01) classes = np.unique(Y2) clf.partial_fit(X2[:third], Y2[:third], classes=classes) assert_equal(clf.coef_.shape, (3, X2.shape[1])) assert_equal(clf.intercept_.shape, (3,)) assert_equal(clf.decision_function([[0, 0]]).shape, (1, 3)) id1 = id(clf.coef_.data) clf.partial_fit(X2[third:], Y2[third:]) id2 = id(clf.coef_.data) # check that coef_ haven't been re-allocated assert_true(id1, id2) def test_partial_fit_multiclass_average(self): third = X2.shape[0] // 3 clf = self.factory(alpha=0.01, average=X2.shape[0]) classes = np.unique(Y2) clf.partial_fit(X2[:third], Y2[:third], classes=classes) assert_equal(clf.coef_.shape, (3, X2.shape[1])) assert_equal(clf.intercept_.shape, (3,)) clf.partial_fit(X2[third:], Y2[third:]) assert_equal(clf.coef_.shape, (3, X2.shape[1])) assert_equal(clf.intercept_.shape, (3,)) def test_fit_then_partial_fit(self): # Partial_fit should work after initial fit in the multiclass case. # Non-regression test for #2496; fit would previously produce a # Fortran-ordered coef_ that subsequent partial_fit couldn't handle. clf = self.factory() clf.fit(X2, Y2) clf.partial_fit(X2, Y2) # no exception here def _test_partial_fit_equal_fit(self, lr): for X_, Y_, T_ in ((X, Y, T), (X2, Y2, T2)): clf = self.factory(alpha=0.01, eta0=0.01, n_iter=2, learning_rate=lr, shuffle=False) clf.fit(X_, Y_) y_pred = clf.decision_function(T_) t = clf.t_ classes = np.unique(Y_) clf = self.factory(alpha=0.01, eta0=0.01, learning_rate=lr, shuffle=False) for i in range(2): clf.partial_fit(X_, Y_, classes=classes) y_pred2 = clf.decision_function(T_) assert_equal(clf.t_, t) assert_array_almost_equal(y_pred, y_pred2, decimal=2) def test_partial_fit_equal_fit_constant(self): self._test_partial_fit_equal_fit("constant") def test_partial_fit_equal_fit_optimal(self): self._test_partial_fit_equal_fit("optimal") def test_partial_fit_equal_fit_invscaling(self): self._test_partial_fit_equal_fit("invscaling") def test_regression_losses(self): clf = self.factory(alpha=0.01, learning_rate="constant", eta0=0.1, loss="epsilon_insensitive") clf.fit(X, Y) assert_equal(1.0, np.mean(clf.predict(X) == Y)) clf = self.factory(alpha=0.01, learning_rate="constant", eta0=0.1, loss="squared_epsilon_insensitive") clf.fit(X, Y) assert_equal(1.0, np.mean(clf.predict(X) == Y)) clf = self.factory(alpha=0.01, loss="huber") clf.fit(X, Y) assert_equal(1.0, np.mean(clf.predict(X) == Y)) clf = self.factory(alpha=0.01, learning_rate="constant", eta0=0.01, loss="squared_loss") clf.fit(X, Y) assert_equal(1.0, np.mean(clf.predict(X) == Y)) def test_warm_start_multiclass(self): self._test_warm_start(X2, Y2, "optimal") def test_multiple_fit(self): # Test multiple calls of fit w/ different shaped inputs. clf = self.factory(alpha=0.01, n_iter=5, shuffle=False) clf.fit(X, Y) assert_true(hasattr(clf, "coef_")) # Non-regression test: try fitting with a different label set. y = [["ham", "spam"][i] for i in LabelEncoder().fit_transform(Y)] clf.fit(X[:, :-1], y) class SparseSGDClassifierTestCase(DenseSGDClassifierTestCase): """Run exactly the same tests using the sparse representation variant""" factory_class = SparseSGDClassifier ############################################################################### # Regression Test Case class DenseSGDRegressorTestCase(unittest.TestCase, CommonTest): """Test suite for the dense representation variant of SGD""" factory_class = SGDRegressor def test_sgd(self): # Check that SGD gives any results. clf = self.factory(alpha=0.1, n_iter=2, fit_intercept=False) clf.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2]) assert_equal(clf.coef_[0], clf.coef_[1]) @raises(ValueError) def test_sgd_bad_penalty(self): # Check whether expected ValueError on bad penalty self.factory(penalty='foobar', l1_ratio=0.85) @raises(ValueError) def test_sgd_bad_loss(self): # Check whether expected ValueError on bad loss self.factory(loss="foobar") def test_sgd_averaged_computed_correctly(self): # Tests the average regressor matches the naive implementation eta = .001 alpha = .01 n_samples = 20 n_features = 10 rng = np.random.RandomState(0) X = rng.normal(size=(n_samples, n_features)) w = rng.normal(size=n_features) # simple linear function without noise y = np.dot(X, w) clf = self.factory(loss='squared_loss', learning_rate='constant', eta0=eta, alpha=alpha, fit_intercept=True, n_iter=1, average=True, shuffle=False) clf.fit(X, y) average_weights, average_intercept = self.asgd(X, y, eta, alpha) assert_array_almost_equal(clf.coef_, average_weights, decimal=16) assert_almost_equal(clf.intercept_, average_intercept, decimal=16) def test_sgd_averaged_partial_fit(self): # Tests whether the partial fit yields the same average as the fit eta = .001 alpha = .01 n_samples = 20 n_features = 10 rng = np.random.RandomState(0) X = rng.normal(size=(n_samples, n_features)) w = rng.normal(size=n_features) # simple linear function without noise y = np.dot(X, w) clf = self.factory(loss='squared_loss', learning_rate='constant', eta0=eta, alpha=alpha, fit_intercept=True, n_iter=1, average=True, shuffle=False) clf.partial_fit(X[:int(n_samples / 2)][:], y[:int(n_samples / 2)]) clf.partial_fit(X[int(n_samples / 2):][:], y[int(n_samples / 2):]) average_weights, average_intercept = self.asgd(X, y, eta, alpha) assert_array_almost_equal(clf.coef_, average_weights, decimal=16) assert_almost_equal(clf.intercept_[0], average_intercept, decimal=16) def test_average_sparse(self): # Checks the average weights on data with 0s eta = .001 alpha = .01 clf = self.factory(loss='squared_loss', learning_rate='constant', eta0=eta, alpha=alpha, fit_intercept=True, n_iter=1, average=True, shuffle=False) n_samples = Y3.shape[0] clf.partial_fit(X3[:int(n_samples / 2)][:], Y3[:int(n_samples / 2)]) clf.partial_fit(X3[int(n_samples / 2):][:], Y3[int(n_samples / 2):]) average_weights, average_intercept = self.asgd(X3, Y3, eta, alpha) assert_array_almost_equal(clf.coef_, average_weights, decimal=16) assert_almost_equal(clf.intercept_, average_intercept, decimal=16) def test_sgd_least_squares_fit(self): xmin, xmax = -5, 5 n_samples = 100 rng = np.random.RandomState(0) X = np.linspace(xmin, xmax, n_samples).reshape(n_samples, 1) # simple linear function without noise y = 0.5 * X.ravel() clf = self.factory(loss='squared_loss', alpha=0.1, n_iter=20, fit_intercept=False) clf.fit(X, y) score = clf.score(X, y) assert_greater(score, 0.99) # simple linear function with noise y = 0.5 * X.ravel() + rng.randn(n_samples, 1).ravel() clf = self.factory(loss='squared_loss', alpha=0.1, n_iter=20, fit_intercept=False) clf.fit(X, y) score = clf.score(X, y) assert_greater(score, 0.5) def test_sgd_epsilon_insensitive(self): xmin, xmax = -5, 5 n_samples = 100 X = np.linspace(xmin, xmax, n_samples).reshape(n_samples, 1) # simple linear function without noise y = 0.5 * X.ravel() clf = self.factory(loss='epsilon_insensitive', epsilon=0.01, alpha=0.1, n_iter=20, fit_intercept=False) clf.fit(X, y) score = clf.score(X, y) assert_true(score > 0.99) # simple linear function with noise y = 0.5 * X.ravel() \ + np.random.randn(n_samples, 1).ravel() clf = self.factory(loss='epsilon_insensitive', epsilon=0.01, alpha=0.1, n_iter=20, fit_intercept=False) clf.fit(X, y) score = clf.score(X, y) assert_true(score > 0.5) def test_sgd_huber_fit(self): xmin, xmax = -5, 5 n_samples = 100 rng = np.random.RandomState(0) X = np.linspace(xmin, xmax, n_samples).reshape(n_samples, 1) # simple linear function without noise y = 0.5 * X.ravel() clf = self.factory(loss="huber", epsilon=0.1, alpha=0.1, n_iter=20, fit_intercept=False) clf.fit(X, y) score = clf.score(X, y) assert_greater(score, 0.99) # simple linear function with noise y = 0.5 * X.ravel() + rng.randn(n_samples, 1).ravel() clf = self.factory(loss="huber", epsilon=0.1, alpha=0.1, n_iter=20, fit_intercept=False) clf.fit(X, y) score = clf.score(X, y) assert_greater(score, 0.5) def test_elasticnet_convergence(self): # Check that the SGD output is consistent with coordinate descent n_samples, n_features = 1000, 5 rng = np.random.RandomState(0) X = np.random.randn(n_samples, n_features) # ground_truth linear model that generate y from X and to which the # models should converge if the regularizer would be set to 0.0 ground_truth_coef = rng.randn(n_features) y = np.dot(X, ground_truth_coef) # XXX: alpha = 0.1 seems to cause convergence problems for alpha in [0.01, 0.001]: for l1_ratio in [0.5, 0.8, 1.0]: cd = linear_model.ElasticNet(alpha=alpha, l1_ratio=l1_ratio, fit_intercept=False) cd.fit(X, y) sgd = self.factory(penalty='elasticnet', n_iter=50, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=False) sgd.fit(X, y) err_msg = ("cd and sgd did not converge to comparable " "results for alpha=%f and l1_ratio=%f" % (alpha, l1_ratio)) assert_almost_equal(cd.coef_, sgd.coef_, decimal=2, err_msg=err_msg) @ignore_warnings def test_partial_fit(self): third = X.shape[0] // 3 clf = self.factory(alpha=0.01) clf.partial_fit(X[:third], Y[:third]) assert_equal(clf.coef_.shape, (X.shape[1], )) assert_equal(clf.intercept_.shape, (1,)) assert_equal(clf.predict([[0, 0]]).shape, (1, )) id1 = id(clf.coef_.data) clf.partial_fit(X[third:], Y[third:]) id2 = id(clf.coef_.data) # check that coef_ haven't been re-allocated assert_true(id1, id2) def _test_partial_fit_equal_fit(self, lr): clf = self.factory(alpha=0.01, n_iter=2, eta0=0.01, learning_rate=lr, shuffle=False) clf.fit(X, Y) y_pred = clf.predict(T) t = clf.t_ clf = self.factory(alpha=0.01, eta0=0.01, learning_rate=lr, shuffle=False) for i in range(2): clf.partial_fit(X, Y) y_pred2 = clf.predict(T) assert_equal(clf.t_, t) assert_array_almost_equal(y_pred, y_pred2, decimal=2) def test_partial_fit_equal_fit_constant(self): self._test_partial_fit_equal_fit("constant") def test_partial_fit_equal_fit_optimal(self): self._test_partial_fit_equal_fit("optimal") def test_partial_fit_equal_fit_invscaling(self): self._test_partial_fit_equal_fit("invscaling") def test_loss_function_epsilon(self): clf = self.factory(epsilon=0.9) clf.set_params(epsilon=0.1) assert clf.loss_functions['huber'][1] == 0.1 class SparseSGDRegressorTestCase(DenseSGDRegressorTestCase): # Run exactly the same tests using the sparse representation variant factory_class = SparseSGDRegressor def test_l1_ratio(): # Test if l1 ratio extremes match L1 and L2 penalty settings. X, y = datasets.make_classification(n_samples=1000, n_features=100, n_informative=20, random_state=1234) # test if elasticnet with l1_ratio near 1 gives same result as pure l1 est_en = SGDClassifier(alpha=0.001, penalty='elasticnet', l1_ratio=0.9999999999, random_state=42).fit(X, y) est_l1 = SGDClassifier(alpha=0.001, penalty='l1', random_state=42).fit(X, y) assert_array_almost_equal(est_en.coef_, est_l1.coef_) # test if elasticnet with l1_ratio near 0 gives same result as pure l2 est_en = SGDClassifier(alpha=0.001, penalty='elasticnet', l1_ratio=0.0000000001, random_state=42).fit(X, y) est_l2 = SGDClassifier(alpha=0.001, penalty='l2', random_state=42).fit(X, y) assert_array_almost_equal(est_en.coef_, est_l2.coef_) def test_underflow_or_overlow(): with np.errstate(all='raise'): # Generate some weird data with hugely unscaled features rng = np.random.RandomState(0) n_samples = 100 n_features = 10 X = rng.normal(size=(n_samples, n_features)) X[:, :2] *= 1e300 assert_true(np.isfinite(X).all()) # Use MinMaxScaler to scale the data without introducing a numerical # instability (computing the standard deviation naively is not possible # on this data) X_scaled = MinMaxScaler().fit_transform(X) assert_true(np.isfinite(X_scaled).all()) # Define a ground truth on the scaled data ground_truth = rng.normal(size=n_features) y = (np.dot(X_scaled, ground_truth) > 0.).astype(np.int32) assert_array_equal(np.unique(y), [0, 1]) model = SGDClassifier(alpha=0.1, loss='squared_hinge', n_iter=500) # smoke test: model is stable on scaled data model.fit(X_scaled, y) assert_true(np.isfinite(model.coef_).all()) # model is numerically unstable on unscaled data msg_regxp = (r"Floating-point under-/overflow occurred at epoch #.*" " Scaling input data with StandardScaler or MinMaxScaler" " might help.") assert_raises_regexp(ValueError, msg_regxp, model.fit, X, y) def test_numerical_stability_large_gradient(): # Non regression test case for numerical stability on scaled problems # where the gradient can still explode with some losses model = SGDClassifier(loss='squared_hinge', n_iter=10, shuffle=True, penalty='elasticnet', l1_ratio=0.3, alpha=0.01, eta0=0.001, random_state=0) with np.errstate(all='raise'): model.fit(iris.data, iris.target) assert_true(np.isfinite(model.coef_).all()) def test_large_regularization(): # Non regression tests for numerical stability issues caused by large # regularization parameters for penalty in ['l2', 'l1', 'elasticnet']: model = SGDClassifier(alpha=1e5, learning_rate='constant', eta0=0.1, n_iter=5, penalty=penalty, shuffle=False) with np.errstate(all='raise'): model.fit(iris.data, iris.target) assert_array_almost_equal(model.coef_, np.zeros_like(model.coef_)) def _test_gradient_common(loss_function, cases): # Test gradient of different loss functions # cases is a list of (p, y, expected) for p, y, expected in cases: assert_almost_equal(loss_function.dloss(p, y), expected) def test_gradient_hinge(): # Test Hinge (hinge / perceptron) # hinge loss = sgd_fast.Hinge(1.0) cases = [ # (p, y, expected) (1.1, 1.0, 0.0), (-2.0, -1.0, 0.0), (1.0, 1.0, -1.0), (-1.0, -1.0, 1.0), (0.5, 1.0, -1.0), (2.0, -1.0, 1.0), (-0.5, -1.0, 1.0), (0.0, 1.0, -1.0) ] _test_gradient_common(loss, cases) # perceptron loss = sgd_fast.Hinge(0.0) cases = [ # (p, y, expected) (1.0, 1.0, 0.0), (-0.1, -1.0, 0.0), (0.0, 1.0, -1.0), (0.0, -1.0, 1.0), (0.5, -1.0, 1.0), (2.0, -1.0, 1.0), (-0.5, 1.0, -1.0), (-1.0, 1.0, -1.0), ] _test_gradient_common(loss, cases) def test_gradient_squared_hinge(): # Test SquaredHinge loss = sgd_fast.SquaredHinge(1.0) cases = [ # (p, y, expected) (1.0, 1.0, 0.0), (-2.0, -1.0, 0.0), (1.0, -1.0, 4.0), (-1.0, 1.0, -4.0), (0.5, 1.0, -1.0), (0.5, -1.0, 3.0) ] _test_gradient_common(loss, cases) def test_gradient_log(): # Test Log (logistic loss) loss = sgd_fast.Log() cases = [ # (p, y, expected) (1.0, 1.0, -1.0 / (np.exp(1.0) + 1.0)), (1.0, -1.0, 1.0 / (np.exp(-1.0) + 1.0)), (-1.0, -1.0, 1.0 / (np.exp(1.0) + 1.0)), (-1.0, 1.0, -1.0 / (np.exp(-1.0) + 1.0)), (0.0, 1.0, -0.5), (0.0, -1.0, 0.5), (17.9, -1.0, 1.0), (-17.9, 1.0, -1.0), ] _test_gradient_common(loss, cases) assert_almost_equal(loss.dloss(18.1, 1.0), np.exp(-18.1) * -1.0, 16) assert_almost_equal(loss.dloss(-18.1, -1.0), np.exp(-18.1) * 1.0, 16) def test_gradient_squared_loss(): # Test SquaredLoss loss = sgd_fast.SquaredLoss() cases = [ # (p, y, expected) (0.0, 0.0, 0.0), (1.0, 1.0, 0.0), (1.0, 0.0, 1.0), (0.5, -1.0, 1.5), (-2.5, 2.0, -4.5) ] _test_gradient_common(loss, cases) def test_gradient_huber(): # Test Huber loss = sgd_fast.Huber(0.1) cases = [ # (p, y, expected) (0.0, 0.0, 0.0), (0.1, 0.0, 0.1), (0.0, 0.1, -0.1), (3.95, 4.0, -0.05), (5.0, 2.0, 0.1), (-1.0, 5.0, -0.1) ] _test_gradient_common(loss, cases) def test_gradient_modified_huber(): # Test ModifiedHuber loss = sgd_fast.ModifiedHuber() cases = [ # (p, y, expected) (1.0, 1.0, 0.0), (-1.0, -1.0, 0.0), (2.0, 1.0, 0.0), (0.0, 1.0, -2.0), (-1.0, 1.0, -4.0), (0.5, -1.0, 3.0), (0.5, -1.0, 3.0), (-2.0, 1.0, -4.0), (-3.0, 1.0, -4.0) ] _test_gradient_common(loss, cases) def test_gradient_epsilon_insensitive(): # Test EpsilonInsensitive loss = sgd_fast.EpsilonInsensitive(0.1) cases = [ (0.0, 0.0, 0.0), (0.1, 0.0, 0.0), (-2.05, -2.0, 0.0), (3.05, 3.0, 0.0), (2.2, 2.0, 1.0), (2.0, -1.0, 1.0), (2.0, 2.2, -1.0), (-2.0, 1.0, -1.0) ] _test_gradient_common(loss, cases) def test_gradient_squared_epsilon_insensitive(): # Test SquaredEpsilonInsensitive loss = sgd_fast.SquaredEpsilonInsensitive(0.1) cases = [ (0.0, 0.0, 0.0), (0.1, 0.0, 0.0), (-2.05, -2.0, 0.0), (3.05, 3.0, 0.0), (2.2, 2.0, 0.2), (2.0, -1.0, 5.8), (2.0, 2.2, -0.2), (-2.0, 1.0, -5.8) ] _test_gradient_common(loss, cases)
bsd-3-clause
jaidevd/scikit-learn
examples/covariance/plot_outlier_detection.py
36
5023
""" ========================================== Outlier detection with several methods. ========================================== When the amount of contamination is known, this example illustrates three different ways of performing :ref:`outlier_detection`: - based on a robust estimator of covariance, which is assuming that the data are Gaussian distributed and performs better than the One-Class SVM in that case. - using the One-Class SVM and its ability to capture the shape of the data set, hence performing better when the data is strongly non-Gaussian, i.e. with two well-separated clusters; - using the Isolation Forest algorithm, which is based on random forests and hence more adapted to large-dimensional settings, even if it performs quite well in the examples below. - using the Local Outlier Factor to measure the local deviation of a given data point with respect to its neighbors by comparing their local density. The ground truth about inliers and outliers is given by the points colors while the orange-filled area indicates which points are reported as inliers by each method. Here, we assume that we know the fraction of outliers in the datasets. Thus rather than using the 'predict' method of the objects, we set the threshold on the decision_function to separate out the corresponding fraction. """ import numpy as np from scipy import stats import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn import svm from sklearn.covariance import EllipticEnvelope from sklearn.ensemble import IsolationForest from sklearn.neighbors import LocalOutlierFactor print(__doc__) rng = np.random.RandomState(42) # Example settings n_samples = 200 outliers_fraction = 0.25 clusters_separation = [0, 1, 2] # define two outlier detection tools to be compared classifiers = { "One-Class SVM": svm.OneClassSVM(nu=0.95 * outliers_fraction + 0.05, kernel="rbf", gamma=0.1), "Robust covariance": EllipticEnvelope(contamination=outliers_fraction), "Isolation Forest": IsolationForest(max_samples=n_samples, contamination=outliers_fraction, random_state=rng), "Local Outlier Factor": LocalOutlierFactor( n_neighbors=35, contamination=outliers_fraction)} # Compare given classifiers under given settings xx, yy = np.meshgrid(np.linspace(-7, 7, 100), np.linspace(-7, 7, 100)) n_inliers = int((1. - outliers_fraction) * n_samples) n_outliers = int(outliers_fraction * n_samples) ground_truth = np.ones(n_samples, dtype=int) ground_truth[-n_outliers:] = -1 # Fit the problem with varying cluster separation for i, offset in enumerate(clusters_separation): np.random.seed(42) # Data generation X1 = 0.3 * np.random.randn(n_inliers // 2, 2) - offset X2 = 0.3 * np.random.randn(n_inliers // 2, 2) + offset X = np.r_[X1, X2] # Add outliers X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))] # Fit the model plt.figure(figsize=(9, 7)) for i, (clf_name, clf) in enumerate(classifiers.items()): # fit the data and tag outliers if clf_name == "Local Outlier Factor": y_pred = clf.fit_predict(X) scores_pred = clf.negative_outlier_factor_ else: clf.fit(X) scores_pred = clf.decision_function(X) y_pred = clf.predict(X) threshold = stats.scoreatpercentile(scores_pred, 100 * outliers_fraction) n_errors = (y_pred != ground_truth).sum() # plot the levels lines and the points if clf_name == "Local Outlier Factor": # decision_function is private for LOF Z = clf._decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) subplot = plt.subplot(2, 2, i + 1) subplot.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7), cmap=plt.cm.Blues_r) a = subplot.contour(xx, yy, Z, levels=[threshold], linewidths=2, colors='red') subplot.contourf(xx, yy, Z, levels=[threshold, Z.max()], colors='orange') b = subplot.scatter(X[:-n_outliers, 0], X[:-n_outliers, 1], c='white') c = subplot.scatter(X[-n_outliers:, 0], X[-n_outliers:, 1], c='black') subplot.axis('tight') subplot.legend( [a.collections[0], b, c], ['learned decision function', 'true inliers', 'true outliers'], prop=matplotlib.font_manager.FontProperties(size=10), loc='lower right') subplot.set_xlabel("%d. %s (errors: %d)" % (i + 1, clf_name, n_errors)) subplot.set_xlim((-7, 7)) subplot.set_ylim((-7, 7)) plt.subplots_adjust(0.04, 0.1, 0.96, 0.94, 0.1, 0.26) plt.suptitle("Outlier detection") plt.show()
bsd-3-clause
jbbskinny/sympy
sympy/utilities/runtests.py
34
81153
""" This is our testing framework. Goals: * it should be compatible with py.test and operate very similarly (or identically) * doesn't require any external dependencies * preferably all the functionality should be in this file only * no magic, just import the test file and execute the test functions, that's it * portable """ from __future__ import print_function, division import os import sys import platform import inspect import traceback import pdb import re import linecache import time from fnmatch import fnmatch from timeit import default_timer as clock import doctest as pdoctest # avoid clashing with our doctest() function from doctest import DocTestFinder, DocTestRunner import random import subprocess import signal import stat from inspect import isgeneratorfunction from sympy.core.cache import clear_cache from sympy.core.compatibility import exec_, PY3, string_types, range from sympy.utilities.misc import find_executable from sympy.external import import_module from sympy.utilities.exceptions import SymPyDeprecationWarning IS_WINDOWS = (os.name == 'nt') class Skipped(Exception): pass import __future__ # add more flags ?? future_flags = __future__.division.compiler_flag def _indent(s, indent=4): """ Add the given number of space characters to the beginning of every non-blank line in ``s``, and return the result. If the string ``s`` is Unicode, it is encoded using the stdout encoding and the ``backslashreplace`` error handler. """ # After a 2to3 run the below code is bogus, so wrap it with a version check if not PY3: if isinstance(s, unicode): s = s.encode(pdoctest._encoding, 'backslashreplace') # This regexp matches the start of non-blank lines: return re.sub('(?m)^(?!$)', indent*' ', s) pdoctest._indent = _indent # ovverride reporter to maintain windows and python3 def _report_failure(self, out, test, example, got): """ Report that the given example failed. """ s = self._checker.output_difference(example, got, self.optionflags) s = s.encode('raw_unicode_escape').decode('utf8', 'ignore') out(self._failure_header(test, example) + s) if PY3 and IS_WINDOWS: DocTestRunner.report_failure = _report_failure def convert_to_native_paths(lst): """ Converts a list of '/' separated paths into a list of native (os.sep separated) paths and converts to lowercase if the system is case insensitive. """ newlst = [] for i, rv in enumerate(lst): rv = os.path.join(*rv.split("/")) # on windows the slash after the colon is dropped if sys.platform == "win32": pos = rv.find(':') if pos != -1: if rv[pos + 1] != '\\': rv = rv[:pos + 1] + '\\' + rv[pos + 1:] newlst.append(sys_normcase(rv)) return newlst def get_sympy_dir(): """ Returns the root sympy directory and set the global value indicating whether the system is case sensitive or not. """ global sys_case_insensitive this_file = os.path.abspath(__file__) sympy_dir = os.path.join(os.path.dirname(this_file), "..", "..") sympy_dir = os.path.normpath(sympy_dir) sys_case_insensitive = (os.path.isdir(sympy_dir) and os.path.isdir(sympy_dir.lower()) and os.path.isdir(sympy_dir.upper())) return sys_normcase(sympy_dir) def sys_normcase(f): if sys_case_insensitive: # global defined after call to get_sympy_dir() return f.lower() return f def setup_pprint(): from sympy import pprint_use_unicode, init_printing # force pprint to be in ascii mode in doctests pprint_use_unicode(False) # hook our nice, hash-stable strprinter init_printing(pretty_print=False) def run_in_subprocess_with_hash_randomization(function, function_args=(), function_kwargs={}, command=sys.executable, module='sympy.utilities.runtests', force=False): """ Run a function in a Python subprocess with hash randomization enabled. If hash randomization is not supported by the version of Python given, it returns False. Otherwise, it returns the exit value of the command. The function is passed to sys.exit(), so the return value of the function will be the return value. The environment variable PYTHONHASHSEED is used to seed Python's hash randomization. If it is set, this function will return False, because starting a new subprocess is unnecessary in that case. If it is not set, one is set at random, and the tests are run. Note that if this environment variable is set when Python starts, hash randomization is automatically enabled. To force a subprocess to be created even if PYTHONHASHSEED is set, pass ``force=True``. This flag will not force a subprocess in Python versions that do not support hash randomization (see below), because those versions of Python do not support the ``-R`` flag. ``function`` should be a string name of a function that is importable from the module ``module``, like "_test". The default for ``module`` is "sympy.utilities.runtests". ``function_args`` and ``function_kwargs`` should be a repr-able tuple and dict, respectively. The default Python command is sys.executable, which is the currently running Python command. This function is necessary because the seed for hash randomization must be set by the environment variable before Python starts. Hence, in order to use a predetermined seed for tests, we must start Python in a separate subprocess. Hash randomization was added in the minor Python versions 2.6.8, 2.7.3, 3.1.5, and 3.2.3, and is enabled by default in all Python versions after and including 3.3.0. Examples ======== >>> from sympy.utilities.runtests import ( ... run_in_subprocess_with_hash_randomization) >>> # run the core tests in verbose mode >>> run_in_subprocess_with_hash_randomization("_test", ... function_args=("core",), ... function_kwargs={'verbose': True}) # doctest: +SKIP # Will return 0 if sys.executable supports hash randomization and tests # pass, 1 if they fail, and False if it does not support hash # randomization. """ # Note, we must return False everywhere, not None, as subprocess.call will # sometimes return None. # First check if the Python version supports hash randomization # If it doesn't have this support, it won't reconize the -R flag p = subprocess.Popen([command, "-RV"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p.communicate() if p.returncode != 0: return False hash_seed = os.getenv("PYTHONHASHSEED") if not hash_seed: os.environ["PYTHONHASHSEED"] = str(random.randrange(2**32)) else: if not force: return False # Now run the command commandstring = ("import sys; from %s import %s;sys.exit(%s(*%s, **%s))" % (module, function, function, repr(function_args), repr(function_kwargs))) try: p = subprocess.Popen([command, "-R", "-c", commandstring]) p.communicate() except KeyboardInterrupt: p.wait() finally: # Put the environment variable back, so that it reads correctly for # the current Python process. if hash_seed is None: del os.environ["PYTHONHASHSEED"] else: os.environ["PYTHONHASHSEED"] = hash_seed return p.returncode def run_all_tests(test_args=(), test_kwargs={}, doctest_args=(), doctest_kwargs={}, examples_args=(), examples_kwargs={'quiet': True}): """ Run all tests. Right now, this runs the regular tests (bin/test), the doctests (bin/doctest), the examples (examples/all.py), and the sage tests (see sympy/external/tests/test_sage.py). This is what ``setup.py test`` uses. You can pass arguments and keyword arguments to the test functions that support them (for now, test, doctest, and the examples). See the docstrings of those functions for a description of the available options. For example, to run the solvers tests with colors turned off: >>> from sympy.utilities.runtests import run_all_tests >>> run_all_tests(test_args=("solvers",), ... test_kwargs={"colors:False"}) # doctest: +SKIP """ tests_successful = True try: # Regular tests if not test(*test_args, **test_kwargs): # some regular test fails, so set the tests_successful # flag to false and continue running the doctests tests_successful = False # Doctests print() if not doctest(*doctest_args, **doctest_kwargs): tests_successful = False # Examples print() sys.path.append("examples") from all import run_examples # examples/all.py if not run_examples(*examples_args, **examples_kwargs): tests_successful = False # Sage tests if not (sys.platform == "win32" or PY3): # run Sage tests; Sage currently doesn't support Windows or Python 3 dev_null = open(os.devnull, 'w') if subprocess.call("sage -v", shell=True, stdout=dev_null, stderr=dev_null) == 0: if subprocess.call("sage -python bin/test " "sympy/external/tests/test_sage.py", shell=True) != 0: tests_successful = False if tests_successful: return else: # Return nonzero exit code sys.exit(1) except KeyboardInterrupt: print() print("DO *NOT* COMMIT!") sys.exit(1) def test(*paths, **kwargs): """ Run tests in the specified test_*.py files. Tests in a particular test_*.py file are run if any of the given strings in ``paths`` matches a part of the test file's path. If ``paths=[]``, tests in all test_*.py files are run. Notes: - If sort=False, tests are run in random order (not default). - Paths can be entered in native system format or in unix, forward-slash format. - Files that are on the blacklist can be tested by providing their path; they are only excluded if no paths are given. **Explanation of test results** ====== =============================================================== Output Meaning ====== =============================================================== . passed F failed X XPassed (expected to fail but passed) f XFAILed (expected to fail and indeed failed) s skipped w slow T timeout (e.g., when ``--timeout`` is used) K KeyboardInterrupt (when running the slow tests with ``--slow``, you can interrupt one of them without killing the test runner) ====== =============================================================== Colors have no additional meaning and are used just to facilitate interpreting the output. Examples ======== >>> import sympy Run all tests: >>> sympy.test() # doctest: +SKIP Run one file: >>> sympy.test("sympy/core/tests/test_basic.py") # doctest: +SKIP >>> sympy.test("_basic") # doctest: +SKIP Run all tests in sympy/functions/ and some particular file: >>> sympy.test("sympy/core/tests/test_basic.py", ... "sympy/functions") # doctest: +SKIP Run all tests in sympy/core and sympy/utilities: >>> sympy.test("/core", "/util") # doctest: +SKIP Run specific test from a file: >>> sympy.test("sympy/core/tests/test_basic.py", ... kw="test_equality") # doctest: +SKIP Run specific test from any file: >>> sympy.test(kw="subs") # doctest: +SKIP Run the tests with verbose mode on: >>> sympy.test(verbose=True) # doctest: +SKIP Don't sort the test output: >>> sympy.test(sort=False) # doctest: +SKIP Turn on post-mortem pdb: >>> sympy.test(pdb=True) # doctest: +SKIP Turn off colors: >>> sympy.test(colors=False) # doctest: +SKIP Force colors, even when the output is not to a terminal (this is useful, e.g., if you are piping to ``less -r`` and you still want colors) >>> sympy.test(force_colors=False) # doctest: +SKIP The traceback verboseness can be set to "short" or "no" (default is "short") >>> sympy.test(tb='no') # doctest: +SKIP The ``split`` option can be passed to split the test run into parts. The split currently only splits the test files, though this may change in the future. ``split`` should be a string of the form 'a/b', which will run part ``a`` of ``b``. For instance, to run the first half of the test suite: >>> sympy.test(split='1/2') # doctest: +SKIP You can disable running the tests in a separate subprocess using ``subprocess=False``. This is done to support seeding hash randomization, which is enabled by default in the Python versions where it is supported. If subprocess=False, hash randomization is enabled/disabled according to whether it has been enabled or not in the calling Python process. However, even if it is enabled, the seed cannot be printed unless it is called from a new Python process. Hash randomization was added in the minor Python versions 2.6.8, 2.7.3, 3.1.5, and 3.2.3, and is enabled by default in all Python versions after and including 3.3.0. If hash randomization is not supported ``subprocess=False`` is used automatically. >>> sympy.test(subprocess=False) # doctest: +SKIP To set the hash randomization seed, set the environment variable ``PYTHONHASHSEED`` before running the tests. This can be done from within Python using >>> import os >>> os.environ['PYTHONHASHSEED'] = '42' # doctest: +SKIP Or from the command line using $ PYTHONHASHSEED=42 ./bin/test If the seed is not set, a random seed will be chosen. Note that to reproduce the same hash values, you must use both the same seed as well as the same architecture (32-bit vs. 64-bit). """ subprocess = kwargs.pop("subprocess", True) rerun = kwargs.pop("rerun", 0) # count up from 0, do not print 0 print_counter = lambda i : (print("rerun %d" % (rerun-i)) if rerun-i else None) if subprocess: # loop backwards so last i is 0 for i in range(rerun, -1, -1): print_counter(i) ret = run_in_subprocess_with_hash_randomization("_test", function_args=paths, function_kwargs=kwargs) if ret is False: break val = not bool(ret) # exit on the first failure or if done if not val or i == 0: return val # rerun even if hash randomization is not supported for i in range(rerun, -1, -1): print_counter(i) val = not bool(_test(*paths, **kwargs)) if not val or i == 0: return val def _test(*paths, **kwargs): """ Internal function that actually runs the tests. All keyword arguments from ``test()`` are passed to this function except for ``subprocess``. Returns 0 if tests passed and 1 if they failed. See the docstring of ``test()`` for more information. """ verbose = kwargs.get("verbose", False) tb = kwargs.get("tb", "short") kw = kwargs.get("kw", None) or () # ensure that kw is a tuple if isinstance(kw, str): kw = (kw, ) post_mortem = kwargs.get("pdb", False) colors = kwargs.get("colors", True) force_colors = kwargs.get("force_colors", False) sort = kwargs.get("sort", True) seed = kwargs.get("seed", None) if seed is None: seed = random.randrange(100000000) timeout = kwargs.get("timeout", False) slow = kwargs.get("slow", False) enhance_asserts = kwargs.get("enhance_asserts", False) split = kwargs.get('split', None) blacklist = kwargs.get('blacklist', []) blacklist = convert_to_native_paths(blacklist) fast_threshold = kwargs.get('fast_threshold', None) slow_threshold = kwargs.get('slow_threshold', None) r = PyTestReporter(verbose=verbose, tb=tb, colors=colors, force_colors=force_colors, split=split) t = SymPyTests(r, kw, post_mortem, seed, fast_threshold=fast_threshold, slow_threshold=slow_threshold) # Disable warnings for external modules import sympy.external sympy.external.importtools.WARN_OLD_VERSION = False sympy.external.importtools.WARN_NOT_INSTALLED = False # Show deprecation warnings import warnings warnings.simplefilter("error", SymPyDeprecationWarning) test_files = t.get_test_files('sympy') not_blacklisted = [f for f in test_files if not any(b in f for b in blacklist)] if len(paths) == 0: matched = not_blacklisted else: paths = convert_to_native_paths(paths) matched = [] for f in not_blacklisted: basename = os.path.basename(f) for p in paths: if p in f or fnmatch(basename, p): matched.append(f) break if slow: # Seed to evenly shuffle slow tests among splits random.seed(41992450) random.shuffle(matched) if split: matched = split_list(matched, split) t._testfiles.extend(matched) return int(not t.test(sort=sort, timeout=timeout, slow=slow, enhance_asserts=enhance_asserts)) def doctest(*paths, **kwargs): """ Runs doctests in all \*.py files in the sympy directory which match any of the given strings in ``paths`` or all tests if paths=[]. Notes: - Paths can be entered in native system format or in unix, forward-slash format. - Files that are on the blacklist can be tested by providing their path; they are only excluded if no paths are given. Examples ======== >>> import sympy Run all tests: >>> sympy.doctest() # doctest: +SKIP Run one file: >>> sympy.doctest("sympy/core/basic.py") # doctest: +SKIP >>> sympy.doctest("polynomial.rst") # doctest: +SKIP Run all tests in sympy/functions/ and some particular file: >>> sympy.doctest("/functions", "basic.py") # doctest: +SKIP Run any file having polynomial in its name, doc/src/modules/polynomial.rst, sympy/functions/special/polynomials.py, and sympy/polys/polynomial.py: >>> sympy.doctest("polynomial") # doctest: +SKIP The ``split`` option can be passed to split the test run into parts. The split currently only splits the test files, though this may change in the future. ``split`` should be a string of the form 'a/b', which will run part ``a`` of ``b``. Note that the regular doctests and the Sphinx doctests are split independently. For instance, to run the first half of the test suite: >>> sympy.doctest(split='1/2') # doctest: +SKIP The ``subprocess`` and ``verbose`` options are the same as with the function ``test()``. See the docstring of that function for more information. """ subprocess = kwargs.pop("subprocess", True) rerun = kwargs.pop("rerun", 0) # count up from 0, do not print 0 print_counter = lambda i : (print("rerun %d" % (rerun-i)) if rerun-i else None) if subprocess: # loop backwards so last i is 0 for i in range(rerun, -1, -1): print_counter(i) ret = run_in_subprocess_with_hash_randomization("_doctest", function_args=paths, function_kwargs=kwargs) if ret is False: break val = not bool(ret) # exit on the first failure or if done if not val or i == 0: return val # rerun even if hash randomization is not supported for i in range(rerun, -1, -1): print_counter(i) val = not bool(_doctest(*paths, **kwargs)) if not val or i == 0: return val def _doctest(*paths, **kwargs): """ Internal function that actually runs the doctests. All keyword arguments from ``doctest()`` are passed to this function except for ``subprocess``. Returns 0 if tests passed and 1 if they failed. See the docstrings of ``doctest()`` and ``test()`` for more information. """ normal = kwargs.get("normal", False) verbose = kwargs.get("verbose", False) colors = kwargs.get("colors", True) force_colors = kwargs.get("force_colors", False) blacklist = kwargs.get("blacklist", []) split = kwargs.get('split', None) blacklist.extend([ "doc/src/modules/plotting.rst", # generates live plots "sympy/utilities/compilef.py", # needs tcc "sympy/physics/gaussopt.py", # raises deprecation warning ]) if import_module('numpy') is None: blacklist.extend([ "sympy/plotting/experimental_lambdify.py", "sympy/plotting/plot_implicit.py", "examples/advanced/autowrap_integrators.py", "examples/advanced/autowrap_ufuncify.py", "examples/intermediate/sample.py", "examples/intermediate/mplot2d.py", "examples/intermediate/mplot3d.py", "doc/src/modules/numeric-computation.rst" ]) else: if import_module('matplotlib') is None: blacklist.extend([ "examples/intermediate/mplot2d.py", "examples/intermediate/mplot3d.py" ]) else: # don't display matplotlib windows from sympy.plotting.plot import unset_show unset_show() if import_module('pyglet') is None: blacklist.extend(["sympy/plotting/pygletplot"]) if import_module('theano') is None: blacklist.extend(["doc/src/modules/numeric-computation.rst"]) # disabled because of doctest failures in asmeurer's bot blacklist.extend([ "sympy/utilities/autowrap.py", "examples/advanced/autowrap_integrators.py", "examples/advanced/autowrap_ufuncify.py" ]) # blacklist these modules until issue 4840 is resolved blacklist.extend([ "sympy/conftest.py", "sympy/utilities/benchmarking.py" ]) blacklist = convert_to_native_paths(blacklist) # Disable warnings for external modules import sympy.external sympy.external.importtools.WARN_OLD_VERSION = False sympy.external.importtools.WARN_NOT_INSTALLED = False # Show deprecation warnings import warnings warnings.simplefilter("error", SymPyDeprecationWarning) r = PyTestReporter(verbose, split=split, colors=colors,\ force_colors=force_colors) t = SymPyDocTests(r, normal) test_files = t.get_test_files('sympy') test_files.extend(t.get_test_files('examples', init_only=False)) not_blacklisted = [f for f in test_files if not any(b in f for b in blacklist)] if len(paths) == 0: matched = not_blacklisted else: # take only what was requested...but not blacklisted items # and allow for partial match anywhere or fnmatch of name paths = convert_to_native_paths(paths) matched = [] for f in not_blacklisted: basename = os.path.basename(f) for p in paths: if p in f or fnmatch(basename, p): matched.append(f) break if split: matched = split_list(matched, split) t._testfiles.extend(matched) # run the tests and record the result for this *py portion of the tests if t._testfiles: failed = not t.test() else: failed = False # N.B. # -------------------------------------------------------------------- # Here we test *.rst files at or below doc/src. Code from these must # be self supporting in terms of imports since there is no importing # of necessary modules by doctest.testfile. If you try to pass *.py # files through this they might fail because they will lack the needed # imports and smarter parsing that can be done with source code. # test_files = t.get_test_files('doc/src', '*.rst', init_only=False) test_files.sort() not_blacklisted = [f for f in test_files if not any(b in f for b in blacklist)] if len(paths) == 0: matched = not_blacklisted else: # Take only what was requested as long as it's not on the blacklist. # Paths were already made native in *py tests so don't repeat here. # There's no chance of having a *py file slip through since we # only have *rst files in test_files. matched = [] for f in not_blacklisted: basename = os.path.basename(f) for p in paths: if p in f or fnmatch(basename, p): matched.append(f) break if split: matched = split_list(matched, split) setup_pprint() first_report = True for rst_file in matched: if not os.path.isfile(rst_file): continue old_displayhook = sys.displayhook try: out = sympytestfile( rst_file, module_relative=False, encoding='utf-8', optionflags=pdoctest.ELLIPSIS | pdoctest.NORMALIZE_WHITESPACE | pdoctest.IGNORE_EXCEPTION_DETAIL) finally: # make sure we return to the original displayhook in case some # doctest has changed that sys.displayhook = old_displayhook rstfailed, tested = out if tested: failed = rstfailed or failed if first_report: first_report = False msg = 'rst doctests start' if not t._testfiles: r.start(msg=msg) else: r.write_center(msg) print() # use as the id, everything past the first 'sympy' file_id = rst_file[rst_file.find('sympy') + len('sympy') + 1:] print(file_id, end=" ") # get at least the name out so it is know who is being tested wid = r.terminal_width - len(file_id) - 1 # update width test_file = '[%s]' % (tested) report = '[%s]' % (rstfailed or 'OK') print(''.join( [test_file, ' '*(wid - len(test_file) - len(report)), report]) ) # the doctests for *py will have printed this message already if there was # a failure, so now only print it if there was intervening reporting by # testing the *rst as evidenced by first_report no longer being True. if not first_report and failed: print() print("DO *NOT* COMMIT!") return int(failed) sp = re.compile(r'([0-9]+)/([1-9][0-9]*)') def split_list(l, split): """ Splits a list into part a of b split should be a string of the form 'a/b'. For instance, '1/3' would give the split one of three. If the length of the list is not divisible by the number of splits, the last split will have more items. >>> from sympy.utilities.runtests import split_list >>> a = list(range(10)) >>> split_list(a, '1/3') [0, 1, 2] >>> split_list(a, '2/3') [3, 4, 5] >>> split_list(a, '3/3') [6, 7, 8, 9] """ m = sp.match(split) if not m: raise ValueError("split must be a string of the form a/b where a and b are ints") i, t = map(int, m.groups()) return l[(i - 1)*len(l)//t:i*len(l)//t] from collections import namedtuple SymPyTestResults = namedtuple('TestResults', 'failed attempted') def sympytestfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=pdoctest.DocTestParser(), encoding=None): """ Test examples in the given file. Return (#failures, #tests). Optional keyword arg ``module_relative`` specifies how filenames should be interpreted: - If ``module_relative`` is True (the default), then ``filename`` specifies a module-relative path. By default, this path is relative to the calling module's directory; but if the ``package`` argument is specified, then it is relative to that package. To ensure os-independence, ``filename`` should use "/" characters to separate path segments, and should not be an absolute path (i.e., it may not begin with "/"). - If ``module_relative`` is False, then ``filename`` specifies an os-specific path. The path may be absolute or relative (to the current working directory). Optional keyword arg ``name`` gives the name of the test; by default use the file's basename. Optional keyword argument ``package`` is a Python package or the name of a Python package whose directory should be used as the base directory for a module relative filename. If no package is specified, then the calling module's directory is used as the base directory for module relative filenames. It is an error to specify ``package`` if ``module_relative`` is False. Optional keyword arg ``globs`` gives a dict to be used as the globals when executing examples; by default, use {}. A copy of this dict is actually used for each docstring, so that each docstring's examples start with a clean slate. Optional keyword arg ``extraglobs`` gives a dictionary that should be merged into the globals that are used to execute examples. By default, no extra globals are used. Optional keyword arg ``verbose`` prints lots of stuff if true, prints only failures if false; by default, it's true iff "-v" is in sys.argv. Optional keyword arg ``report`` prints a summary at the end when true, else prints nothing at the end. In verbose mode, the summary is detailed, else very brief (in fact, empty if all tests passed). Optional keyword arg ``optionflags`` or's together module constants, and defaults to 0. Possible values (see the docs for details): - DONT_ACCEPT_TRUE_FOR_1 - DONT_ACCEPT_BLANKLINE - NORMALIZE_WHITESPACE - ELLIPSIS - SKIP - IGNORE_EXCEPTION_DETAIL - REPORT_UDIFF - REPORT_CDIFF - REPORT_NDIFF - REPORT_ONLY_FIRST_FAILURE Optional keyword arg ``raise_on_error`` raises an exception on the first unexpected exception or failure. This allows failures to be post-mortem debugged. Optional keyword arg ``parser`` specifies a DocTestParser (or subclass) that should be used to extract tests from the files. Optional keyword arg ``encoding`` specifies an encoding that should be used to convert the file to unicode. Advanced tomfoolery: testmod runs methods of a local instance of class doctest.Tester, then merges the results into (or creates) global Tester instance doctest.master. Methods of doctest.master can be called directly too, if you want to do something unusual. Passing report=0 to testmod is especially useful then, to delay displaying a summary. Invoke doctest.master.summarize(verbose) when you're done fiddling. """ if package and not module_relative: raise ValueError("Package may only be specified for module-" "relative paths.") # Relativize the path if not PY3: text, filename = pdoctest._load_testfile( filename, package, module_relative) if encoding is not None: text = text.decode(encoding) else: text, filename = pdoctest._load_testfile( filename, package, module_relative, encoding) # If no name was given, then use the file's name. if name is None: name = os.path.basename(filename) # Assemble the globals. if globs is None: globs = {} else: globs = globs.copy() if extraglobs is not None: globs.update(extraglobs) if '__name__' not in globs: globs['__name__'] = '__main__' if raise_on_error: runner = pdoctest.DebugRunner(verbose=verbose, optionflags=optionflags) else: runner = SymPyDocTestRunner(verbose=verbose, optionflags=optionflags) runner._checker = SymPyOutputChecker() # Read the file, convert it to a test, and run it. test = parser.get_doctest(text, globs, name, filename, 0) runner.run(test, compileflags=future_flags) if report: runner.summarize() if pdoctest.master is None: pdoctest.master = runner else: pdoctest.master.merge(runner) return SymPyTestResults(runner.failures, runner.tries) class SymPyTests(object): def __init__(self, reporter, kw="", post_mortem=False, seed=None, fast_threshold=None, slow_threshold=None): self._post_mortem = post_mortem self._kw = kw self._count = 0 self._root_dir = sympy_dir self._reporter = reporter self._reporter.root_dir(self._root_dir) self._testfiles = [] self._seed = seed if seed is not None else random.random() # Defaults in seconds, from human / UX design limits # http://www.nngroup.com/articles/response-times-3-important-limits/ # # These defaults are *NOT* set in stone as we are measuring different # things, so others feel free to come up with a better yardstick :) if fast_threshold: self._fast_threshold = float(fast_threshold) else: self._fast_threshold = 0.1 if slow_threshold: self._slow_threshold = float(slow_threshold) else: self._slow_threshold = 10 def test(self, sort=False, timeout=False, slow=False, enhance_asserts=False): """ Runs the tests returning True if all tests pass, otherwise False. If sort=False run tests in random order. """ if sort: self._testfiles.sort() elif slow: pass else: random.seed(self._seed) random.shuffle(self._testfiles) self._reporter.start(self._seed) for f in self._testfiles: try: self.test_file(f, sort, timeout, slow, enhance_asserts) except KeyboardInterrupt: print(" interrupted by user") self._reporter.finish() raise return self._reporter.finish() def _enhance_asserts(self, source): from ast import (NodeTransformer, Compare, Name, Store, Load, Tuple, Assign, BinOp, Str, Mod, Assert, parse, fix_missing_locations) ops = {"Eq": '==', "NotEq": '!=', "Lt": '<', "LtE": '<=', "Gt": '>', "GtE": '>=', "Is": 'is', "IsNot": 'is not', "In": 'in', "NotIn": 'not in'} class Transform(NodeTransformer): def visit_Assert(self, stmt): if isinstance(stmt.test, Compare): compare = stmt.test values = [compare.left] + compare.comparators names = [ "_%s" % i for i, _ in enumerate(values) ] names_store = [ Name(n, Store()) for n in names ] names_load = [ Name(n, Load()) for n in names ] target = Tuple(names_store, Store()) value = Tuple(values, Load()) assign = Assign([target], value) new_compare = Compare(names_load[0], compare.ops, names_load[1:]) msg_format = "\n%s " + "\n%s ".join([ ops[op.__class__.__name__] for op in compare.ops ]) + "\n%s" msg = BinOp(Str(msg_format), Mod(), Tuple(names_load, Load())) test = Assert(new_compare, msg, lineno=stmt.lineno, col_offset=stmt.col_offset) return [assign, test] else: return stmt tree = parse(source) new_tree = Transform().visit(tree) return fix_missing_locations(new_tree) def test_file(self, filename, sort=True, timeout=False, slow=False, enhance_asserts=False): reporter = self._reporter funcs = [] try: gl = {'__file__': filename} try: if PY3: open_file = lambda: open(filename, encoding="utf8") else: open_file = lambda: open(filename) with open_file() as f: source = f.read() if self._kw: for l in source.splitlines(): if l.lstrip().startswith('def '): if any(l.find(k) != -1 for k in self._kw): break else: return if enhance_asserts: try: source = self._enhance_asserts(source) except ImportError: pass code = compile(source, filename, "exec") exec_(code, gl) except (SystemExit, KeyboardInterrupt): raise except ImportError: reporter.import_error(filename, sys.exc_info()) return clear_cache() self._count += 1 random.seed(self._seed) pytestfile = "" if "XFAIL" in gl: pytestfile = inspect.getsourcefile(gl["XFAIL"]) pytestfile2 = "" if "slow" in gl: pytestfile2 = inspect.getsourcefile(gl["slow"]) disabled = gl.get("disabled", False) if not disabled: # we need to filter only those functions that begin with 'test_' # that are defined in the testing file or in the file where # is defined the XFAIL decorator funcs = [gl[f] for f in gl.keys() if f.startswith("test_") and (inspect.isfunction(gl[f]) or inspect.ismethod(gl[f])) and (inspect.getsourcefile(gl[f]) == filename or inspect.getsourcefile(gl[f]) == pytestfile or inspect.getsourcefile(gl[f]) == pytestfile2)] if slow: funcs = [f for f in funcs if getattr(f, '_slow', False)] # Sorting of XFAILed functions isn't fixed yet :-( funcs.sort(key=lambda x: inspect.getsourcelines(x)[1]) i = 0 while i < len(funcs): if isgeneratorfunction(funcs[i]): # some tests can be generators, that return the actual # test functions. We unpack it below: f = funcs.pop(i) for fg in f(): func = fg[0] args = fg[1:] fgw = lambda: func(*args) funcs.insert(i, fgw) i += 1 else: i += 1 # drop functions that are not selected with the keyword expression: funcs = [x for x in funcs if self.matches(x)] if not funcs: return except Exception: reporter.entering_filename(filename, len(funcs)) raise reporter.entering_filename(filename, len(funcs)) if not sort: random.shuffle(funcs) for f in funcs: start = time.time() reporter.entering_test(f) try: if getattr(f, '_slow', False) and not slow: raise Skipped("Slow") if timeout: self._timeout(f, timeout) else: random.seed(self._seed) f() except KeyboardInterrupt: if getattr(f, '_slow', False): reporter.test_skip("KeyboardInterrupt") else: raise except Exception: if timeout: signal.alarm(0) # Disable the alarm. It could not be handled before. t, v, tr = sys.exc_info() if t is AssertionError: reporter.test_fail((t, v, tr)) if self._post_mortem: pdb.post_mortem(tr) elif t.__name__ == "Skipped": reporter.test_skip(v) elif t.__name__ == "XFail": reporter.test_xfail() elif t.__name__ == "XPass": reporter.test_xpass(v) else: reporter.test_exception((t, v, tr)) if self._post_mortem: pdb.post_mortem(tr) else: reporter.test_pass() taken = time.time() - start if taken > self._slow_threshold: reporter.slow_test_functions.append((f.__name__, taken)) if getattr(f, '_slow', False) and slow: if taken < self._fast_threshold: reporter.fast_test_functions.append((f.__name__, taken)) reporter.leaving_filename() def _timeout(self, function, timeout): def callback(x, y): signal.alarm(0) raise Skipped("Timeout") signal.signal(signal.SIGALRM, callback) signal.alarm(timeout) # Set an alarm with a given timeout function() signal.alarm(0) # Disable the alarm def matches(self, x): """ Does the keyword expression self._kw match "x"? Returns True/False. Always returns True if self._kw is "". """ if not self._kw: return True for kw in self._kw: if x.__name__.find(kw) != -1: return True return False def get_test_files(self, dir, pat='test_*.py'): """ Returns the list of test_*.py (default) files at or below directory ``dir`` relative to the sympy home directory. """ dir = os.path.join(self._root_dir, convert_to_native_paths([dir])[0]) g = [] for path, folders, files in os.walk(dir): g.extend([os.path.join(path, f) for f in files if fnmatch(f, pat)]) return sorted([sys_normcase(gi) for gi in g]) class SymPyDocTests(object): def __init__(self, reporter, normal): self._count = 0 self._root_dir = sympy_dir self._reporter = reporter self._reporter.root_dir(self._root_dir) self._normal = normal self._testfiles = [] def test(self): """ Runs the tests and returns True if all tests pass, otherwise False. """ self._reporter.start() for f in self._testfiles: try: self.test_file(f) except KeyboardInterrupt: print(" interrupted by user") self._reporter.finish() raise return self._reporter.finish() def test_file(self, filename): clear_cache() from sympy.core.compatibility import StringIO rel_name = filename[len(self._root_dir) + 1:] dirname, file = os.path.split(filename) module = rel_name.replace(os.sep, '.')[:-3] if rel_name.startswith("examples"): # Examples files do not have __init__.py files, # So we have to temporarily extend sys.path to import them sys.path.insert(0, dirname) module = file[:-3] # remove ".py" setup_pprint() try: module = pdoctest._normalize_module(module) tests = SymPyDocTestFinder().find(module) except (SystemExit, KeyboardInterrupt): raise except ImportError: self._reporter.import_error(filename, sys.exc_info()) return finally: if rel_name.startswith("examples"): del sys.path[0] tests = [test for test in tests if len(test.examples) > 0] # By default tests are sorted by alphabetical order by function name. # We sort by line number so one can edit the file sequentially from # bottom to top. However, if there are decorated functions, their line # numbers will be too large and for now one must just search for these # by text and function name. tests.sort(key=lambda x: -x.lineno) if not tests: return self._reporter.entering_filename(filename, len(tests)) for test in tests: assert len(test.examples) != 0 # check if there are external dependencies which need to be met if '_doctest_depends_on' in test.globs: if not self._process_dependencies(test.globs['_doctest_depends_on']): self._reporter.test_skip() continue runner = SymPyDocTestRunner(optionflags=pdoctest.ELLIPSIS | pdoctest.NORMALIZE_WHITESPACE | pdoctest.IGNORE_EXCEPTION_DETAIL) runner._checker = SymPyOutputChecker() old = sys.stdout new = StringIO() sys.stdout = new # If the testing is normal, the doctests get importing magic to # provide the global namespace. If not normal (the default) then # then must run on their own; all imports must be explicit within # a function's docstring. Once imported that import will be # available to the rest of the tests in a given function's # docstring (unless clear_globs=True below). if not self._normal: test.globs = {} # if this is uncommented then all the test would get is what # comes by default with a "from sympy import *" #exec('from sympy import *') in test.globs test.globs['print_function'] = print_function try: f, t = runner.run(test, compileflags=future_flags, out=new.write, clear_globs=False) except KeyboardInterrupt: raise finally: sys.stdout = old if f > 0: self._reporter.doctest_fail(test.name, new.getvalue()) else: self._reporter.test_pass() self._reporter.leaving_filename() def get_test_files(self, dir, pat='*.py', init_only=True): """ Returns the list of \*.py files (default) from which docstrings will be tested which are at or below directory ``dir``. By default, only those that have an __init__.py in their parent directory and do not start with ``test_`` will be included. """ def importable(x): """ Checks if given pathname x is an importable module by checking for __init__.py file. Returns True/False. Currently we only test if the __init__.py file exists in the directory with the file "x" (in theory we should also test all the parent dirs). """ init_py = os.path.join(os.path.dirname(x), "__init__.py") return os.path.exists(init_py) dir = os.path.join(self._root_dir, convert_to_native_paths([dir])[0]) g = [] for path, folders, files in os.walk(dir): g.extend([os.path.join(path, f) for f in files if not f.startswith('test_') and fnmatch(f, pat)]) if init_only: # skip files that are not importable (i.e. missing __init__.py) g = [x for x in g if importable(x)] return [sys_normcase(gi) for gi in g] def _process_dependencies(self, deps): """ Returns ``False`` if some dependencies are not met and the test should be skipped otherwise returns ``True``. """ executables = deps.get('exe', None) moduledeps = deps.get('modules', None) viewers = deps.get('disable_viewers', None) pyglet = deps.get('pyglet', None) # print deps if executables is not None: for ex in executables: found = find_executable(ex) if found is None: return False if moduledeps is not None: for extmod in moduledeps: if extmod == 'matplotlib': matplotlib = import_module( 'matplotlib', __import__kwargs={'fromlist': ['pyplot', 'cm', 'collections']}, min_module_version='1.0.0', catch=(RuntimeError,)) if matplotlib is not None: pass else: return False else: # TODO min version support mod = import_module(extmod) if mod is not None: version = "unknown" if hasattr(mod, '__version__'): version = mod.__version__ else: return False if viewers is not None: import tempfile tempdir = tempfile.mkdtemp() os.environ['PATH'] = '%s:%s' % (tempdir, os.environ['PATH']) if PY3: vw = '#!/usr/bin/env python3\n' \ 'import sys\n' \ 'if len(sys.argv) <= 1:\n' \ ' exit("wrong number of args")\n' else: vw = '#!/usr/bin/env python\n' \ 'import sys\n' \ 'if len(sys.argv) <= 1:\n' \ ' exit("wrong number of args")\n' for viewer in viewers: with open(os.path.join(tempdir, viewer), 'w') as fh: fh.write(vw) # make the file executable os.chmod(os.path.join(tempdir, viewer), stat.S_IREAD | stat.S_IWRITE | stat.S_IXUSR) if pyglet: # monkey-patch pyglet s.t. it does not open a window during # doctesting import pyglet class DummyWindow(object): def __init__(self, *args, **kwargs): self.has_exit=True self.width = 600 self.height = 400 def set_vsync(self, x): pass def switch_to(self): pass def push_handlers(self, x): pass def close(self): pass pyglet.window.Window = DummyWindow return True class SymPyDocTestFinder(DocTestFinder): """ A class used to extract the DocTests that are relevant to a given object, from its docstring and the docstrings of its contained objects. Doctests can currently be extracted from the following object types: modules, functions, classes, methods, staticmethods, classmethods, and properties. Modified from doctest's version by looking harder for code in the case that it looks like the the code comes from a different module. In the case of decorated functions (e.g. @vectorize) they appear to come from a different module (e.g. multidemensional) even though their code is not there. """ def _find(self, tests, obj, name, module, source_lines, globs, seen): """ Find tests for the given object and any contained objects, and add them to ``tests``. """ if self._verbose: print('Finding tests in %s' % name) # If we've already processed this object, then ignore it. if id(obj) in seen: return seen[id(obj)] = 1 # Make sure we don't run doctests for classes outside of sympy, such # as in numpy or scipy. if inspect.isclass(obj): if obj.__module__.split('.')[0] != 'sympy': return # Find a test for this object, and add it to the list of tests. test = self._get_test(obj, name, module, globs, source_lines) if test is not None: tests.append(test) if not self._recurse: return # Look for tests in a module's contained objects. if inspect.ismodule(obj): for rawname, val in obj.__dict__.items(): # Recurse to functions & classes. if inspect.isfunction(val) or inspect.isclass(val): # Make sure we don't run doctests functions or classes # from different modules if val.__module__ != module.__name__: continue assert self._from_module(module, val), \ "%s is not in module %s (rawname %s)" % (val, module, rawname) try: valname = '%s.%s' % (name, rawname) self._find(tests, val, valname, module, source_lines, globs, seen) except KeyboardInterrupt: raise # Look for tests in a module's __test__ dictionary. for valname, val in getattr(obj, '__test__', {}).items(): if not isinstance(valname, string_types): raise ValueError("SymPyDocTestFinder.find: __test__ keys " "must be strings: %r" % (type(valname),)) if not (inspect.isfunction(val) or inspect.isclass(val) or inspect.ismethod(val) or inspect.ismodule(val) or isinstance(val, string_types)): raise ValueError("SymPyDocTestFinder.find: __test__ values " "must be strings, functions, methods, " "classes, or modules: %r" % (type(val),)) valname = '%s.__test__.%s' % (name, valname) self._find(tests, val, valname, module, source_lines, globs, seen) # Look for tests in a class's contained objects. if inspect.isclass(obj): for valname, val in obj.__dict__.items(): # Special handling for staticmethod/classmethod. if isinstance(val, staticmethod): val = getattr(obj, valname) if isinstance(val, classmethod): val = getattr(obj, valname).__func__ # Recurse to methods, properties, and nested classes. if (inspect.isfunction(val) or inspect.isclass(val) or isinstance(val, property)): # Make sure we don't run doctests functions or classes # from different modules if isinstance(val, property): if hasattr(val.fget, '__module__'): if val.fget.__module__ != module.__name__: continue else: if val.__module__ != module.__name__: continue assert self._from_module(module, val), \ "%s is not in module %s (valname %s)" % ( val, module, valname) valname = '%s.%s' % (name, valname) self._find(tests, val, valname, module, source_lines, globs, seen) def _get_test(self, obj, name, module, globs, source_lines): """ Return a DocTest for the given object, if it defines a docstring; otherwise, return None. """ lineno = None # Extract the object's docstring. If it doesn't have one, # then return None (no test for this object). if isinstance(obj, string_types): # obj is a string in the case for objects in the polys package. # Note that source_lines is a binary string (compiled polys # modules), which can't be handled by _find_lineno so determine # the line number here. docstring = obj matches = re.findall("line \d+", name) assert len(matches) == 1, \ "string '%s' does not contain lineno " % name # NOTE: this is not the exact linenumber but its better than no # lineno ;) lineno = int(matches[0][5:]) else: try: if obj.__doc__ is None: docstring = '' else: docstring = obj.__doc__ if not isinstance(docstring, string_types): docstring = str(docstring) except (TypeError, AttributeError): docstring = '' # Don't bother if the docstring is empty. if self._exclude_empty and not docstring: return None # check that properties have a docstring because _find_lineno # assumes it if isinstance(obj, property): if obj.fget.__doc__ is None: return None # Find the docstring's location in the file. if lineno is None: # handling of properties is not implemented in _find_lineno so do # it here if hasattr(obj, 'func_closure') and obj.func_closure is not None: tobj = obj.func_closure[0].cell_contents elif isinstance(obj, property): tobj = obj.fget else: tobj = obj lineno = self._find_lineno(tobj, source_lines) if lineno is None: return None # Return a DocTest for this object. if module is None: filename = None else: filename = getattr(module, '__file__', module.__name__) if filename[-4:] in (".pyc", ".pyo"): filename = filename[:-1] if hasattr(obj, '_doctest_depends_on'): globs['_doctest_depends_on'] = obj._doctest_depends_on else: globs['_doctest_depends_on'] = {} return self._parser.get_doctest(docstring, globs, name, filename, lineno) class SymPyDocTestRunner(DocTestRunner): """ A class used to run DocTest test cases, and accumulate statistics. The ``run`` method is used to process a single DocTest case. It returns a tuple ``(f, t)``, where ``t`` is the number of test cases tried, and ``f`` is the number of test cases that failed. Modified from the doctest version to not reset the sys.displayhook (see issue 5140). See the docstring of the original DocTestRunner for more information. """ def run(self, test, compileflags=None, out=None, clear_globs=True): """ Run the examples in ``test``, and display the results using the writer function ``out``. The examples are run in the namespace ``test.globs``. If ``clear_globs`` is true (the default), then this namespace will be cleared after the test runs, to help with garbage collection. If you would like to examine the namespace after the test completes, then use ``clear_globs=False``. ``compileflags`` gives the set of flags that should be used by the Python compiler when running the examples. If not specified, then it will default to the set of future-import flags that apply to ``globs``. The output of each example is checked using ``SymPyDocTestRunner.check_output``, and the results are formatted by the ``SymPyDocTestRunner.report_*`` methods. """ self.test = test if compileflags is None: compileflags = pdoctest._extract_future_flags(test.globs) save_stdout = sys.stdout if out is None: out = save_stdout.write sys.stdout = self._fakeout # Patch pdb.set_trace to restore sys.stdout during interactive # debugging (so it's not still redirected to self._fakeout). # Note that the interactive output will go to *our* # save_stdout, even if that's not the real sys.stdout; this # allows us to write test cases for the set_trace behavior. save_set_trace = pdb.set_trace self.debugger = pdoctest._OutputRedirectingPdb(save_stdout) self.debugger.reset() pdb.set_trace = self.debugger.set_trace # Patch linecache.getlines, so we can see the example's source # when we're inside the debugger. self.save_linecache_getlines = pdoctest.linecache.getlines linecache.getlines = self.__patched_linecache_getlines try: test.globs['print_function'] = print_function return self.__run(test, compileflags, out) finally: sys.stdout = save_stdout pdb.set_trace = save_set_trace linecache.getlines = self.save_linecache_getlines if clear_globs: test.globs.clear() # We have to override the name mangled methods. SymPyDocTestRunner._SymPyDocTestRunner__patched_linecache_getlines = \ DocTestRunner._DocTestRunner__patched_linecache_getlines SymPyDocTestRunner._SymPyDocTestRunner__run = DocTestRunner._DocTestRunner__run SymPyDocTestRunner._SymPyDocTestRunner__record_outcome = \ DocTestRunner._DocTestRunner__record_outcome class SymPyOutputChecker(pdoctest.OutputChecker): """ Compared to the OutputChecker from the stdlib our OutputChecker class supports numerical comparison of floats occuring in the output of the doctest examples """ def __init__(self): # NOTE OutputChecker is an old-style class with no __init__ method, # so we can't call the base class version of __init__ here got_floats = r'(\d+\.\d*|\.\d+)' # floats in the 'want' string may contain ellipses want_floats = got_floats + r'(\.{3})?' front_sep = r'\s|\+|\-|\*|,' back_sep = front_sep + r'|j|e' fbeg = r'^%s(?=%s|$)' % (got_floats, back_sep) fmidend = r'(?<=%s)%s(?=%s|$)' % (front_sep, got_floats, back_sep) self.num_got_rgx = re.compile(r'(%s|%s)' %(fbeg, fmidend)) fbeg = r'^%s(?=%s|$)' % (want_floats, back_sep) fmidend = r'(?<=%s)%s(?=%s|$)' % (front_sep, want_floats, back_sep) self.num_want_rgx = re.compile(r'(%s|%s)' %(fbeg, fmidend)) def check_output(self, want, got, optionflags): """ Return True iff the actual output from an example (`got`) matches the expected output (`want`). These strings are always considered to match if they are identical; but depending on what option flags the test runner is using, several non-exact match types are also possible. See the documentation for `TestRunner` for more information about option flags. """ # Handle the common case first, for efficiency: # if they're string-identical, always return true. if got == want: return True # TODO parse integers as well ? # Parse floats and compare them. If some of the parsed floats contain # ellipses, skip the comparison. matches = self.num_got_rgx.finditer(got) numbers_got = [match.group(1) for match in matches] # list of strs matches = self.num_want_rgx.finditer(want) numbers_want = [match.group(1) for match in matches] # list of strs if len(numbers_got) != len(numbers_want): return False if len(numbers_got) > 0: nw_ = [] for ng, nw in zip(numbers_got, numbers_want): if '...' in nw: nw_.append(ng) continue else: nw_.append(nw) if abs(float(ng)-float(nw)) > 1e-5: return False got = self.num_got_rgx.sub(r'%s', got) got = got % tuple(nw_) # <BLANKLINE> can be used as a special sequence to signify a # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used. if not (optionflags & pdoctest.DONT_ACCEPT_BLANKLINE): # Replace <BLANKLINE> in want with a blank line. want = re.sub('(?m)^%s\s*?$' % re.escape(pdoctest.BLANKLINE_MARKER), '', want) # If a line in got contains only spaces, then remove the # spaces. got = re.sub('(?m)^\s*?$', '', got) if got == want: return True # This flag causes doctest to ignore any differences in the # contents of whitespace strings. Note that this can be used # in conjunction with the ELLIPSIS flag. if optionflags & pdoctest.NORMALIZE_WHITESPACE: got = ' '.join(got.split()) want = ' '.join(want.split()) if got == want: return True # The ELLIPSIS flag says to let the sequence "..." in `want` # match any substring in `got`. if optionflags & pdoctest.ELLIPSIS: if pdoctest._ellipsis_match(want, got): return True # We didn't find any match; return false. return False class Reporter(object): """ Parent class for all reporters. """ pass class PyTestReporter(Reporter): """ Py.test like reporter. Should produce output identical to py.test. """ def __init__(self, verbose=False, tb="short", colors=True, force_colors=False, split=None): self._verbose = verbose self._tb_style = tb self._colors = colors self._force_colors = force_colors self._xfailed = 0 self._xpassed = [] self._failed = [] self._failed_doctest = [] self._passed = 0 self._skipped = 0 self._exceptions = [] self._terminal_width = None self._default_width = 80 self._split = split # TODO: Should these be protected? self.slow_test_functions = [] self.fast_test_functions = [] # this tracks the x-position of the cursor (useful for positioning # things on the screen), without the need for any readline library: self._write_pos = 0 self._line_wrap = False def root_dir(self, dir): self._root_dir = dir @property def terminal_width(self): if self._terminal_width is not None: return self._terminal_width def findout_terminal_width(): if sys.platform == "win32": # Windows support is based on: # # http://code.activestate.com/recipes/ # 440694-determine-size-of-console-window-on-windows/ from ctypes import windll, create_string_buffer h = windll.kernel32.GetStdHandle(-12) csbi = create_string_buffer(22) res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) if res: import struct (_, _, _, _, _, left, _, right, _, _, _) = \ struct.unpack("hhhhHhhhhhh", csbi.raw) return right - left else: return self._default_width if hasattr(sys.stdout, 'isatty') and not sys.stdout.isatty(): return self._default_width # leave PIPEs alone try: process = subprocess.Popen(['stty', '-a'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout = process.stdout.read() if PY3: stdout = stdout.decode("utf-8") except (OSError, IOError): pass else: # We support the following output formats from stty: # # 1) Linux -> columns 80 # 2) OS X -> 80 columns # 3) Solaris -> columns = 80 re_linux = r"columns\s+(?P<columns>\d+);" re_osx = r"(?P<columns>\d+)\s*columns;" re_solaris = r"columns\s+=\s+(?P<columns>\d+);" for regex in (re_linux, re_osx, re_solaris): match = re.search(regex, stdout) if match is not None: columns = match.group('columns') try: width = int(columns) except ValueError: pass if width != 0: return width return self._default_width width = findout_terminal_width() self._terminal_width = width return width def write(self, text, color="", align="left", width=None, force_colors=False): """ Prints a text on the screen. It uses sys.stdout.write(), so no readline library is necessary. Parameters ========== color : choose from the colors below, "" means default color align : "left"/"right", "left" is a normal print, "right" is aligned on the right-hand side of the screen, filled with spaces if necessary width : the screen width """ color_templates = ( ("Black", "0;30"), ("Red", "0;31"), ("Green", "0;32"), ("Brown", "0;33"), ("Blue", "0;34"), ("Purple", "0;35"), ("Cyan", "0;36"), ("LightGray", "0;37"), ("DarkGray", "1;30"), ("LightRed", "1;31"), ("LightGreen", "1;32"), ("Yellow", "1;33"), ("LightBlue", "1;34"), ("LightPurple", "1;35"), ("LightCyan", "1;36"), ("White", "1;37"), ) colors = {} for name, value in color_templates: colors[name] = value c_normal = '\033[0m' c_color = '\033[%sm' if width is None: width = self.terminal_width if align == "right": if self._write_pos + len(text) > width: # we don't fit on the current line, create a new line self.write("\n") self.write(" "*(width - self._write_pos - len(text))) if not self._force_colors and hasattr(sys.stdout, 'isatty') and not \ sys.stdout.isatty(): # the stdout is not a terminal, this for example happens if the # output is piped to less, e.g. "bin/test | less". In this case, # the terminal control sequences would be printed verbatim, so # don't use any colors. color = "" elif sys.platform == "win32": # Windows consoles don't support ANSI escape sequences color = "" elif not self._colors: color = "" if self._line_wrap: if text[0] != "\n": sys.stdout.write("\n") # Avoid UnicodeEncodeError when printing out test failures if PY3 and IS_WINDOWS: text = text.encode('raw_unicode_escape').decode('utf8', 'ignore') elif PY3 and not sys.stdout.encoding.lower().startswith('utf'): text = text.encode(sys.stdout.encoding, 'backslashreplace' ).decode(sys.stdout.encoding) if color == "": sys.stdout.write(text) else: sys.stdout.write("%s%s%s" % (c_color % colors[color], text, c_normal)) sys.stdout.flush() l = text.rfind("\n") if l == -1: self._write_pos += len(text) else: self._write_pos = len(text) - l - 1 self._line_wrap = self._write_pos >= width self._write_pos %= width def write_center(self, text, delim="="): width = self.terminal_width if text != "": text = " %s " % text idx = (width - len(text)) // 2 t = delim*idx + text + delim*(width - idx - len(text)) self.write(t + "\n") def write_exception(self, e, val, tb): t = traceback.extract_tb(tb) # remove the first item, as that is always runtests.py t = t[1:] t = traceback.format_list(t) self.write("".join(t)) t = traceback.format_exception_only(e, val) self.write("".join(t)) def start(self, seed=None, msg="test process starts"): self.write_center(msg) executable = sys.executable v = tuple(sys.version_info) python_version = "%s.%s.%s-%s-%s" % v implementation = platform.python_implementation() if implementation == 'PyPy': implementation += " %s.%s.%s-%s-%s" % sys.pypy_version_info self.write("executable: %s (%s) [%s]\n" % (executable, python_version, implementation)) from .misc import ARCH self.write("architecture: %s\n" % ARCH) from sympy.core.cache import USE_CACHE self.write("cache: %s\n" % USE_CACHE) from sympy.core.compatibility import GROUND_TYPES, HAS_GMPY version = '' if GROUND_TYPES =='gmpy': if HAS_GMPY == 1: import gmpy elif HAS_GMPY == 2: import gmpy2 as gmpy version = gmpy.version() self.write("ground types: %s %s\n" % (GROUND_TYPES, version)) if seed is not None: self.write("random seed: %d\n" % seed) from .misc import HASH_RANDOMIZATION self.write("hash randomization: ") hash_seed = os.getenv("PYTHONHASHSEED") or '0' if HASH_RANDOMIZATION and (hash_seed == "random" or int(hash_seed)): self.write("on (PYTHONHASHSEED=%s)\n" % hash_seed) else: self.write("off\n") if self._split: self.write("split: %s\n" % self._split) self.write('\n') self._t_start = clock() def finish(self): self._t_end = clock() self.write("\n") global text, linelen text = "tests finished: %d passed, " % self._passed linelen = len(text) def add_text(mytext): global text, linelen """Break new text if too long.""" if linelen + len(mytext) > self.terminal_width: text += '\n' linelen = 0 text += mytext linelen += len(mytext) if len(self._failed) > 0: add_text("%d failed, " % len(self._failed)) if len(self._failed_doctest) > 0: add_text("%d failed, " % len(self._failed_doctest)) if self._skipped > 0: add_text("%d skipped, " % self._skipped) if self._xfailed > 0: add_text("%d expected to fail, " % self._xfailed) if len(self._xpassed) > 0: add_text("%d expected to fail but passed, " % len(self._xpassed)) if len(self._exceptions) > 0: add_text("%d exceptions, " % len(self._exceptions)) add_text("in %.2f seconds" % (self._t_end - self._t_start)) if self.slow_test_functions: self.write_center('slowest tests', '_') sorted_slow = sorted(self.slow_test_functions, key=lambda r: r[1]) for slow_func_name, taken in sorted_slow: print('%s - Took %.3f seconds' % (slow_func_name, taken)) if self.fast_test_functions: self.write_center('unexpectedly fast tests', '_') sorted_fast = sorted(self.fast_test_functions, key=lambda r: r[1]) for fast_func_name, taken in sorted_fast: print('%s - Took %.3f seconds' % (fast_func_name, taken)) if len(self._xpassed) > 0: self.write_center("xpassed tests", "_") for e in self._xpassed: self.write("%s: %s\n" % (e[0], e[1])) self.write("\n") if self._tb_style != "no" and len(self._exceptions) > 0: for e in self._exceptions: filename, f, (t, val, tb) = e self.write_center("", "_") if f is None: s = "%s" % filename else: s = "%s:%s" % (filename, f.__name__) self.write_center(s, "_") self.write_exception(t, val, tb) self.write("\n") if self._tb_style != "no" and len(self._failed) > 0: for e in self._failed: filename, f, (t, val, tb) = e self.write_center("", "_") self.write_center("%s:%s" % (filename, f.__name__), "_") self.write_exception(t, val, tb) self.write("\n") if self._tb_style != "no" and len(self._failed_doctest) > 0: for e in self._failed_doctest: filename, msg = e self.write_center("", "_") self.write_center("%s" % filename, "_") self.write(msg) self.write("\n") self.write_center(text) ok = len(self._failed) == 0 and len(self._exceptions) == 0 and \ len(self._failed_doctest) == 0 if not ok: self.write("DO *NOT* COMMIT!\n") return ok def entering_filename(self, filename, n): rel_name = filename[len(self._root_dir) + 1:] self._active_file = rel_name self._active_file_error = False self.write(rel_name) self.write("[%d] " % n) def leaving_filename(self): self.write(" ") if self._active_file_error: self.write("[FAIL]", "Red", align="right") else: self.write("[OK]", "Green", align="right") self.write("\n") if self._verbose: self.write("\n") def entering_test(self, f): self._active_f = f if self._verbose: self.write("\n" + f.__name__ + " ") def test_xfail(self): self._xfailed += 1 self.write("f", "Green") def test_xpass(self, v): message = str(v) self._xpassed.append((self._active_file, message)) self.write("X", "Green") def test_fail(self, exc_info): self._failed.append((self._active_file, self._active_f, exc_info)) self.write("F", "Red") self._active_file_error = True def doctest_fail(self, name, error_msg): # the first line contains "******", remove it: error_msg = "\n".join(error_msg.split("\n")[1:]) self._failed_doctest.append((name, error_msg)) self.write("F", "Red") self._active_file_error = True def test_pass(self, char="."): self._passed += 1 if self._verbose: self.write("ok", "Green") else: self.write(char, "Green") def test_skip(self, v=None): char = "s" self._skipped += 1 if v is not None: message = str(v) if message == "KeyboardInterrupt": char = "K" elif message == "Timeout": char = "T" elif message == "Slow": char = "w" self.write(char, "Blue") if self._verbose: self.write(" - ", "Blue") if v is not None: self.write(message, "Blue") def test_exception(self, exc_info): self._exceptions.append((self._active_file, self._active_f, exc_info)) self.write("E", "Red") self._active_file_error = True def import_error(self, filename, exc_info): self._exceptions.append((filename, None, exc_info)) rel_name = filename[len(self._root_dir) + 1:] self.write(rel_name) self.write("[?] Failed to import", "Red") self.write(" ") self.write("[FAIL]", "Red", align="right") self.write("\n") sympy_dir = get_sympy_dir()
bsd-3-clause
heli522/scikit-learn
sklearn/metrics/__init__.py
214
3440
""" The :mod:`sklearn.metrics` module includes score functions, performance metrics and pairwise metrics and distance computations. """ from .ranking import auc from .ranking import average_precision_score from .ranking import coverage_error from .ranking import label_ranking_average_precision_score from .ranking import label_ranking_loss from .ranking import precision_recall_curve from .ranking import roc_auc_score from .ranking import roc_curve from .classification import accuracy_score from .classification import classification_report from .classification import cohen_kappa_score from .classification import confusion_matrix from .classification import f1_score from .classification import fbeta_score from .classification import hamming_loss from .classification import hinge_loss from .classification import jaccard_similarity_score from .classification import log_loss from .classification import matthews_corrcoef from .classification import precision_recall_fscore_support from .classification import precision_score from .classification import recall_score from .classification import zero_one_loss from .classification import brier_score_loss from . import cluster from .cluster import adjusted_mutual_info_score from .cluster import adjusted_rand_score from .cluster import completeness_score from .cluster import consensus_score from .cluster import homogeneity_completeness_v_measure from .cluster import homogeneity_score from .cluster import mutual_info_score from .cluster import normalized_mutual_info_score from .cluster import silhouette_samples from .cluster import silhouette_score from .cluster import v_measure_score from .pairwise import euclidean_distances from .pairwise import pairwise_distances from .pairwise import pairwise_distances_argmin from .pairwise import pairwise_distances_argmin_min from .pairwise import pairwise_kernels from .regression import explained_variance_score from .regression import mean_absolute_error from .regression import mean_squared_error from .regression import median_absolute_error from .regression import r2_score from .scorer import make_scorer from .scorer import SCORERS from .scorer import get_scorer __all__ = [ 'accuracy_score', 'adjusted_mutual_info_score', 'adjusted_rand_score', 'auc', 'average_precision_score', 'classification_report', 'cluster', 'completeness_score', 'confusion_matrix', 'consensus_score', 'coverage_error', 'euclidean_distances', 'explained_variance_score', 'f1_score', 'fbeta_score', 'get_scorer', 'hamming_loss', 'hinge_loss', 'homogeneity_completeness_v_measure', 'homogeneity_score', 'jaccard_similarity_score', 'label_ranking_average_precision_score', 'label_ranking_loss', 'log_loss', 'make_scorer', 'matthews_corrcoef', 'mean_absolute_error', 'mean_squared_error', 'median_absolute_error', 'mutual_info_score', 'normalized_mutual_info_score', 'pairwise_distances', 'pairwise_distances_argmin', 'pairwise_distances_argmin_min', 'pairwise_distances_argmin_min', 'pairwise_kernels', 'precision_recall_curve', 'precision_recall_fscore_support', 'precision_score', 'r2_score', 'recall_score', 'roc_auc_score', 'roc_curve', 'SCORERS', 'silhouette_samples', 'silhouette_score', 'v_measure_score', 'zero_one_loss', 'brier_score_loss', ]
bsd-3-clause
helgako/cms-dqm
notebooks/evaluation.py
1
2680
from sklearn.metrics import roc_curve, roc_auc_score, precision_recall_curve, average_precision_score import numpy as np import matplotlib.pyplot as plt #fixed recall values rec_values = [0.8, 0.9, 0.95, 0.99] # precision at 10 or P@10 measures classification performance, # being the fraction of the top 10 scored instances which are actually anomalous. def Pat10(y_test, y_pred, n=10): ind = np.argpartition(y_pred, -n)[-n:] return np.mean(y_test[ind]) def perfomance(y_test, y_pred, sample_weight=None, n=10): print ("P@"+str(n), Pat10(y_test, y_pred, n)) fig, axes = plt.subplots(nrows=3, figsize=(6, 15)) ax = axes[0] ax.grid(True) precision, recall, _ = precision_recall_curve(y_test,y_pred) print ("recalls_values",rec_values) prec_values = [] for v in rec_values: prec_values.append(max(precision[recall > v])) print ("precision_values", prec_values) print ("average_precision_score", average_precision_score(y_test, y_pred, sample_weight=sample_weight)) print ("roc_auc_score", roc_auc_score(y_test, y_pred)) ax.step(recall, precision, color='b', alpha=0.2, where='post') ax.fill_between(recall, precision, step='post', alpha=0.2, color='b') ax.set_xlabel('Recall', fontsize=10) ax.set_ylabel('Precision', fontsize=10) ax.set_ylim([0.0, 1.05]) ax.set_xlim([0.0, 1.0]) ax.tick_params(axis='x', labelsize=15) ax.tick_params(axis='y', labelsize=15) ax.set_title('2-class Precision-Recall curve: AP={0:0.3f}'.format( average_precision_score(y_test, y_pred, sample_weight=sample_weight)), fontsize=25) ax = axes[1] ax.grid(True) fpr, tpr, _ = roc_curve(y_test, y_pred, sample_weight=sample_weight) ax.plot(fpr, tpr) ax.set_title('ROC curve: roc_auc ={0:0.3f}'.format( roc_auc_score(y_test, y_pred)), fontsize=25) ax.tick_params(axis='x', labelsize=15) ax.tick_params(axis='y', labelsize=15) ax.set_xlabel('FPR', fontsize=10) ax.set_ylabel('TPR', fontsize=10) ax = axes[2] bad_test = np.sum(y_test) good_test = len(y_test)-np.sum(y_test) ax.plot(sorted(y_pred[np.where( y_test == 0.)[0]], reverse=True), np.arange(good_test)/good_test*100, label = "good") ax.plot(sorted(y_pred[np.where( y_test == 1.)[0]]), np.arange(bad_test)/bad_test*100, label = "bad") ax.set_title('Predicted proba', fontsize=25) ax.tick_params(axis='x', labelsize=15) ax.tick_params(axis='y', labelsize=15) fig.subplots_adjust(hspace=0.5) plt.legend() plt.grid(True) plt.show() return precision, recall
mit
go-bears/nupic
src/nupic/research/monitor_mixin/plot.py
19
5187
# ---------------------------------------------------------------------- # Numenta Platform for Intelligent Computing (NuPIC) # Copyright (C) 2014-2015, Numenta, Inc. Unless you have an agreement # with Numenta, Inc., for a separate license for this software code, the # following terms and conditions apply: # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero Public License version 3 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Affero Public License for more details. # # You should have received a copy of the GNU Affero Public License # along with this program. If not, see http://www.gnu.org/licenses. # # http://numenta.org/licenses/ # ---------------------------------------------------------------------- """ Plot class used in monitor mixin framework. """ import logging import os try: # We import in here to avoid creating a matplotlib dependency in nupic. import matplotlib.pyplot as plt import matplotlib.cm as cm except ImportError: # Suppress; we log it at debug level to avoid polluting the logs of apps # and services that don't care about plotting logging.debug("Cannot import matplotlib. Plot class will not work.", exc_info=True) class Plot(object): def __init__(self, monitor, title, show=True): """ @param monitor (MonitorMixinBase) Monitor Mixin instance that generated this plot @param title (string) Plot title """ self._monitor = monitor self._title = title self._fig = self._initFigure() self._show = show if self._show: plt.ion() plt.show() def _initFigure(self): fig = plt.figure() fig.suptitle(self._prettyPrintTitle()) return fig def _prettyPrintTitle(self): if self._monitor.mmName is not None: return "[{0}] {1}".format(self._monitor.mmName, self._title) return self._title def addGraph(self, data, position=111, xlabel=None, ylabel=None): """ Adds a graph to the plot's figure. @param data See matplotlib.Axes.plot documentation. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis """ ax = self._addBase(position, xlabel=xlabel, ylabel=ylabel) ax.plot(data) plt.draw() def addHistogram(self, data, position=111, xlabel=None, ylabel=None, bins=None): """ Adds a histogram to the plot's figure. @param data See matplotlib.Axes.hist documentation. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis """ ax = self._addBase(position, xlabel=xlabel, ylabel=ylabel) ax.hist(data, bins=bins, color="green", alpha=0.8) plt.draw() def add2DArray(self, data, position=111, xlabel=None, ylabel=None, cmap=None, aspect="auto", interpolation="nearest", name=None): """ Adds an image to the plot's figure. @param data a 2D array. See matplotlib.Axes.imshow documentation. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis @param cmap color map used in the rendering @param aspect how aspect ratio is handled during resize @param interpolation interpolation method """ if cmap is None: # The default colormodel is an ugly blue-red model. cmap = cm.Greys ax = self._addBase(position, xlabel=xlabel, ylabel=ylabel) ax.imshow(data, cmap=cmap, aspect=aspect, interpolation=interpolation) if self._show: plt.draw() if name is not None: if not os.path.exists("log"): os.mkdir("log") plt.savefig("log/{name}.png".format(name=name), bbox_inches="tight", figsize=(8, 6), dpi=400) def _addBase(self, position, xlabel=None, ylabel=None): """ Adds a subplot to the plot's figure at specified position. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis @returns (matplotlib.Axes) Axes instance """ ax = self._fig.add_subplot(position) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) return ax
agpl-3.0
pythonvietnam/scikit-learn
sklearn/metrics/cluster/tests/test_bicluster.py
394
1770
"""Testing for bicluster metrics module""" import numpy as np from sklearn.utils.testing import assert_equal, assert_almost_equal from sklearn.metrics.cluster.bicluster import _jaccard from sklearn.metrics import consensus_score def test_jaccard(): a1 = np.array([True, True, False, False]) a2 = np.array([True, True, True, True]) a3 = np.array([False, True, True, False]) a4 = np.array([False, False, True, True]) assert_equal(_jaccard(a1, a1, a1, a1), 1) assert_equal(_jaccard(a1, a1, a2, a2), 0.25) assert_equal(_jaccard(a1, a1, a3, a3), 1.0 / 7) assert_equal(_jaccard(a1, a1, a4, a4), 0) def test_consensus_score(): a = [[True, True, False, False], [False, False, True, True]] b = a[::-1] assert_equal(consensus_score((a, a), (a, a)), 1) assert_equal(consensus_score((a, a), (b, b)), 1) assert_equal(consensus_score((a, b), (a, b)), 1) assert_equal(consensus_score((a, b), (b, a)), 1) assert_equal(consensus_score((a, a), (b, a)), 0) assert_equal(consensus_score((a, a), (a, b)), 0) assert_equal(consensus_score((b, b), (a, b)), 0) assert_equal(consensus_score((b, b), (b, a)), 0) def test_consensus_score_issue2445(): ''' Different number of biclusters in A and B''' a_rows = np.array([[True, True, False, False], [False, False, True, True], [False, False, False, True]]) a_cols = np.array([[True, True, False, False], [False, False, True, True], [False, False, False, True]]) idx = [0, 2] s = consensus_score((a_rows, a_cols), (a_rows[idx], a_cols[idx])) # B contains 2 of the 3 biclusters in A, so score should be 2/3 assert_almost_equal(s, 2.0/3.0)
bsd-3-clause
DSSG2017/florence
src/output/cdr_network.py
1
1732
import pandas as pd import igraph as ig from ..utils.database import dbutils # TODO: Cleanup def hourly_graph(): connection = dbutils.connect() foreigners = pd.read_sql(""" SELECT prev_tower_id, tower_id, count(*) AS weight FROM optourism.foreigners_path_records_joined WHERE tower_id != prev_tower_id AND EXTRACT(HOUR FROM date_time_m) = 22 AND delta < (INTERVAL '30 minutes') GROUP BY tower_id, prev_tower_id """, con=connection) tower_vertices = pd.read_sql(""" SELECT DISTINCT tower_id, lat, lon FROM optourism.foreigners_path_records_joined """, con=connection) connection.close() foreigners['tower_id'] = foreigners['tower_id'].apply( lambda x: 'tower-%s' % x) foreigners['prev_tower_id'] = foreigners['prev_tower_id'].apply( lambda x: 'tower-%s' % x) tower_vertices['tower_id'] = tower_vertices['tower_id'].apply( lambda x: 'tower-%s' % x) graph = ig.Graph() graph.add_vertices(tower_vertices.shape[0]) graph.vs['name'] = tower_vertices['tower_id'] graph.vs['x'] = tower_vertices['lat'] graph.vs['y'] = tower_vertices['lon'] edges = zip(foreigners['prev_tower_id'], foreigners['tower_id']) graph.add_edges(edges) graph.es['weight'] = foreigners['weight'] graph.es.select(weight_lt=5).delete() visual_style = {'vertex_color': 'black', 'vertex_size': 3, 'edge_width': [.01 * i for i in graph.es["weight"]]} ig.plot(graph, bbox=(800, 800), **visual_style) def dwell_time_graph(): pass if __name__ == '__main__': pass
mit
asnorkin/sentiment_analysis
site/lib/python2.7/site-packages/scipy/stats/stats.py
7
186886
# Copyright 2002 Gary Strangman. All rights reserved # Copyright 2002-2016 The SciPy Developers # # The original code from Gary Strangman was heavily adapted for # use in SciPy by Travis Oliphant. The original code came with the # following disclaimer: # # This software is provided "as-is". There are no expressed or implied # warranties of any kind, including, but not limited to, the warranties # of merchantability and fitness for a given application. In no event # shall Gary Strangman be liable for any direct, indirect, incidental, # special, exemplary or consequential damages (including, but not limited # to, loss of use, data or profits, or business interruption) however # caused and on any theory of liability, whether in contract, strict # liability or tort (including negligence or otherwise) arising in any way # out of the use of this software, even if advised of the possibility of # such damage. """ A collection of basic statistical functions for python. The function names appear below. Some scalar functions defined here are also available in the scipy.special package where they work on arbitrary sized arrays. Disclaimers: The function list is obviously incomplete and, worse, the functions are not optimized. All functions have been tested (some more so than others), but they are far from bulletproof. Thus, as with any free software, no warranty or guarantee is expressed or implied. :-) A few extra functions that don't appear in the list below can be found by interested treasure-hunters. These functions don't necessarily have both list and array versions but were deemed useful. Central Tendency ---------------- .. autosummary:: :toctree: generated/ gmean hmean mode Moments ------- .. autosummary:: :toctree: generated/ moment variation skew kurtosis normaltest Altered Versions ---------------- .. autosummary:: :toctree: generated/ tmean tvar tstd tsem describe Frequency Stats --------------- .. autosummary:: :toctree: generated/ itemfreq scoreatpercentile percentileofscore histogram cumfreq relfreq Variability ----------- .. autosummary:: :toctree: generated/ obrientransform signaltonoise sem zmap zscore iqr Trimming Functions ------------------ .. autosummary:: :toctree: generated/ threshold trimboth trim1 Correlation Functions --------------------- .. autosummary:: :toctree: generated/ pearsonr fisher_exact spearmanr pointbiserialr kendalltau weightedtau linregress theilslopes Inferential Stats ----------------- .. autosummary:: :toctree: generated/ ttest_1samp ttest_ind ttest_ind_from_stats ttest_rel chisquare power_divergence ks_2samp mannwhitneyu ranksums wilcoxon kruskal friedmanchisquare combine_pvalues Probability Calculations ------------------------ .. autosummary:: :toctree: generated/ chisqprob betai ANOVA Functions --------------- .. autosummary:: :toctree: generated/ f_oneway f_value Support Functions ----------------- .. autosummary:: :toctree: generated/ ss square_of_sums rankdata References ---------- .. [CRCProbStat2000] Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000. """ from __future__ import division, print_function, absolute_import import warnings import math from collections import namedtuple import numpy as np from numpy import array, asarray, ma, zeros from scipy._lib.six import callable, string_types from scipy._lib._version import NumpyVersion import scipy.special as special import scipy.linalg as linalg from . import distributions from . import mstats_basic from ._distn_infrastructure import _lazywhere from ._stats_mstats_common import _find_repeats, linregress, theilslopes from ._stats import _kendall_dis, _toint64, _weightedrankedtau __all__ = ['find_repeats', 'gmean', 'hmean', 'mode', 'tmean', 'tvar', 'tmin', 'tmax', 'tstd', 'tsem', 'moment', 'variation', 'skew', 'kurtosis', 'describe', 'skewtest', 'kurtosistest', 'normaltest', 'jarque_bera', 'itemfreq', 'scoreatpercentile', 'percentileofscore', 'histogram', 'histogram2', 'cumfreq', 'relfreq', 'obrientransform', 'signaltonoise', 'sem', 'zmap', 'zscore', 'iqr', 'threshold', 'sigmaclip', 'trimboth', 'trim1', 'trim_mean', 'f_oneway', 'pearsonr', 'fisher_exact', 'spearmanr', 'pointbiserialr', 'kendalltau', 'weightedtau', 'linregress', 'theilslopes', 'ttest_1samp', 'ttest_ind', 'ttest_ind_from_stats', 'ttest_rel', 'kstest', 'chisquare', 'power_divergence', 'ks_2samp', 'mannwhitneyu', 'tiecorrect', 'ranksums', 'kruskal', 'friedmanchisquare', 'chisqprob', 'betai', 'f_value_wilks_lambda', 'f_value', 'f_value_multivariate', 'ss', 'square_of_sums', 'fastsort', 'rankdata', 'combine_pvalues', ] def _chk_asarray(a, axis): if axis is None: a = np.ravel(a) outaxis = 0 else: a = np.asarray(a) outaxis = axis if a.ndim == 0: a = np.atleast_1d(a) return a, outaxis def _chk2_asarray(a, b, axis): if axis is None: a = np.ravel(a) b = np.ravel(b) outaxis = 0 else: a = np.asarray(a) b = np.asarray(b) outaxis = axis if a.ndim == 0: a = np.atleast_1d(a) if b.ndim == 0: b = np.atleast_1d(b) return a, b, outaxis def _contains_nan(a, nan_policy='propagate'): policies = ['propagate', 'raise', 'omit'] if nan_policy not in policies: raise ValueError("nan_policy must be one of {%s}" % ', '.join("'%s'" % s for s in policies)) try: # Calling np.sum to avoid creating a huge array into memory # e.g. np.isnan(a).any() with np.errstate(invalid='ignore'): contains_nan = np.isnan(np.sum(a)) except TypeError: # If the check cannot be properly performed we fallback to omiting # nan values and raising a warning. This can happen when attempting to # sum things that are not numbers (e.g. as in the function `mode`). contains_nan = False nan_policy = 'omit' warnings.warn("The input array could not be properly checked for nan " "values. nan values will be ignored.", RuntimeWarning) if contains_nan and nan_policy == 'raise': raise ValueError("The input contains nan values") return (contains_nan, nan_policy) def gmean(a, axis=0, dtype=None): """ Compute the geometric mean along the specified axis. Returns the geometric average of the array elements. That is: n-th root of (x1 * x2 * ... * xn) Parameters ---------- a : array_like Input array or object that can be converted to an array. axis : int or None, optional Axis along which the geometric mean is computed. Default is 0. If None, compute over the whole array `a`. dtype : dtype, optional Type of the returned array and of the accumulator in which the elements are summed. If dtype is not specified, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used. Returns ------- gmean : ndarray see dtype parameter above See Also -------- numpy.mean : Arithmetic average numpy.average : Weighted average hmean : Harmonic mean Notes ----- The geometric average is computed over a single dimension of the input array, axis=0 by default, or all values in the array if axis=None. float64 intermediate and return values are used for integer inputs. Use masked arrays to ignore any non-finite values in the input or that arise in the calculations such as Not a Number and infinity because masked arrays automatically mask any non-finite values. """ if not isinstance(a, np.ndarray): # if not an ndarray object attempt to convert it log_a = np.log(np.array(a, dtype=dtype)) elif dtype: # Must change the default dtype allowing array type if isinstance(a, np.ma.MaskedArray): log_a = np.log(np.ma.asarray(a, dtype=dtype)) else: log_a = np.log(np.asarray(a, dtype=dtype)) else: log_a = np.log(a) return np.exp(log_a.mean(axis=axis)) def hmean(a, axis=0, dtype=None): """ Calculates the harmonic mean along the specified axis. That is: n / (1/x1 + 1/x2 + ... + 1/xn) Parameters ---------- a : array_like Input array, masked array or object that can be converted to an array. axis : int or None, optional Axis along which the harmonic mean is computed. Default is 0. If None, compute over the whole array `a`. dtype : dtype, optional Type of the returned array and of the accumulator in which the elements are summed. If `dtype` is not specified, it defaults to the dtype of `a`, unless `a` has an integer `dtype` with a precision less than that of the default platform integer. In that case, the default platform integer is used. Returns ------- hmean : ndarray see `dtype` parameter above See Also -------- numpy.mean : Arithmetic average numpy.average : Weighted average gmean : Geometric mean Notes ----- The harmonic mean is computed over a single dimension of the input array, axis=0 by default, or all values in the array if axis=None. float64 intermediate and return values are used for integer inputs. Use masked arrays to ignore any non-finite values in the input or that arise in the calculations such as Not a Number and infinity. """ if not isinstance(a, np.ndarray): a = np.array(a, dtype=dtype) if np.all(a > 0): # Harmonic mean only defined if greater than zero if isinstance(a, np.ma.MaskedArray): size = a.count(axis) else: if axis is None: a = a.ravel() size = a.shape[0] else: size = a.shape[axis] return size / np.sum(1.0/a, axis=axis, dtype=dtype) else: raise ValueError("Harmonic mean only defined if all elements greater than zero") ModeResult = namedtuple('ModeResult', ('mode', 'count')) def mode(a, axis=0, nan_policy='propagate'): """ Returns an array of the modal (most common) value in the passed array. If there is more than one such value, only the smallest is returned. The bin-count for the modal bins is also returned. Parameters ---------- a : array_like n-dimensional array of which to find mode(s). axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- mode : ndarray Array of modal values. count : ndarray Array of counts for each mode. Examples -------- >>> a = np.array([[6, 8, 3, 0], ... [3, 2, 1, 7], ... [8, 1, 8, 4], ... [5, 3, 0, 5], ... [4, 7, 5, 9]]) >>> from scipy import stats >>> stats.mode(a) (array([[3, 1, 0, 0]]), array([[1, 1, 1, 1]])) To get mode of whole array, specify ``axis=None``: >>> stats.mode(a, axis=None) (array([3]), array([3])) """ a, axis = _chk_asarray(a, axis) if a.size == 0: return ModeResult(np.array([]), np.array([])) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.mode(a, axis) scores = np.unique(np.ravel(a)) # get ALL unique values testshape = list(a.shape) testshape[axis] = 1 oldmostfreq = np.zeros(testshape, dtype=a.dtype) oldcounts = np.zeros(testshape, dtype=int) for score in scores: template = (a == score) counts = np.expand_dims(np.sum(template, axis), axis) mostfrequent = np.where(counts > oldcounts, score, oldmostfreq) oldcounts = np.maximum(counts, oldcounts) oldmostfreq = mostfrequent return ModeResult(mostfrequent, oldcounts) def _mask_to_limits(a, limits, inclusive): """Mask an array for values outside of given limits. This is primarily a utility function. Parameters ---------- a : array limits : (float or None, float or None) A tuple consisting of the (lower limit, upper limit). Values in the input array less than the lower limit or greater than the upper limit will be masked out. None implies no limit. inclusive : (bool, bool) A tuple consisting of the (lower flag, upper flag). These flags determine whether values exactly equal to lower or upper are allowed. Returns ------- A MaskedArray. Raises ------ A ValueError if there are no values within the given limits. """ lower_limit, upper_limit = limits lower_include, upper_include = inclusive am = ma.MaskedArray(a) if lower_limit is not None: if lower_include: am = ma.masked_less(am, lower_limit) else: am = ma.masked_less_equal(am, lower_limit) if upper_limit is not None: if upper_include: am = ma.masked_greater(am, upper_limit) else: am = ma.masked_greater_equal(am, upper_limit) if am.count() == 0: raise ValueError("No array values within given limits") return am def tmean(a, limits=None, inclusive=(True, True), axis=None): """ Compute the trimmed mean. This function finds the arithmetic mean of given values, ignoring values outside the given `limits`. Parameters ---------- a : array_like Array of values. limits : None or (lower limit, upper limit), optional Values in the input array less than the lower limit or greater than the upper limit will be ignored. When limits is None (default), then all values are used. Either of the limit values in the tuple can also be None representing a half-open interval. inclusive : (bool, bool), optional A tuple consisting of the (lower flag, upper flag). These flags determine whether values exactly equal to the lower or upper limits are included. The default value is (True, True). axis : int or None, optional Axis along which to compute test. Default is None. Returns ------- tmean : float See also -------- trim_mean : returns mean after trimming a proportion from both tails. Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.tmean(x) 9.5 >>> stats.tmean(x, (3,17)) 10.0 """ a = asarray(a) if limits is None: return np.mean(a, None) am = _mask_to_limits(a.ravel(), limits, inclusive) return am.mean(axis=axis) def tvar(a, limits=None, inclusive=(True, True), axis=0, ddof=1): """ Compute the trimmed variance This function computes the sample variance of an array of values, while ignoring values which are outside of given `limits`. Parameters ---------- a : array_like Array of values. limits : None or (lower limit, upper limit), optional Values in the input array less than the lower limit or greater than the upper limit will be ignored. When limits is None, then all values are used. Either of the limit values in the tuple can also be None representing a half-open interval. The default value is None. inclusive : (bool, bool), optional A tuple consisting of the (lower flag, upper flag). These flags determine whether values exactly equal to the lower or upper limits are included. The default value is (True, True). axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Delta degrees of freedom. Default is 1. Returns ------- tvar : float Trimmed variance. Notes ----- `tvar` computes the unbiased sample variance, i.e. it uses a correction factor ``n / (n - 1)``. Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.tvar(x) 35.0 >>> stats.tvar(x, (3,17)) 20.0 """ a = asarray(a) a = a.astype(float).ravel() if limits is None: n = len(a) return a.var() * n/(n-1.) am = _mask_to_limits(a, limits, inclusive) return np.ma.var(am, ddof=ddof, axis=axis) def tmin(a, lowerlimit=None, axis=0, inclusive=True, nan_policy='propagate'): """ Compute the trimmed minimum This function finds the miminum value of an array `a` along the specified axis, but only considering values greater than a specified lower limit. Parameters ---------- a : array_like array of values lowerlimit : None or float, optional Values in the input array less than the given limit will be ignored. When lowerlimit is None, then all values are used. The default value is None. axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. inclusive : {True, False}, optional This flag determines whether values exactly equal to the lower limit are included. The default value is True. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- tmin : float, int or ndarray Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.tmin(x) 0 >>> stats.tmin(x, 13) 13 >>> stats.tmin(x, 13, inclusive=False) 14 """ a, axis = _chk_asarray(a, axis) am = _mask_to_limits(a, (lowerlimit, None), (inclusive, False)) contains_nan, nan_policy = _contains_nan(am, nan_policy) if contains_nan and nan_policy == 'omit': am = ma.masked_invalid(am) res = ma.minimum.reduce(am, axis).data if res.ndim == 0: return res[()] return res def tmax(a, upperlimit=None, axis=0, inclusive=True, nan_policy='propagate'): """ Compute the trimmed maximum This function computes the maximum value of an array along a given axis, while ignoring values larger than a specified upper limit. Parameters ---------- a : array_like array of values upperlimit : None or float, optional Values in the input array greater than the given limit will be ignored. When upperlimit is None, then all values are used. The default value is None. axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. inclusive : {True, False}, optional This flag determines whether values exactly equal to the upper limit are included. The default value is True. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- tmax : float, int or ndarray Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.tmax(x) 19 >>> stats.tmax(x, 13) 13 >>> stats.tmax(x, 13, inclusive=False) 12 """ a, axis = _chk_asarray(a, axis) am = _mask_to_limits(a, (None, upperlimit), (False, inclusive)) contains_nan, nan_policy = _contains_nan(am, nan_policy) if contains_nan and nan_policy == 'omit': am = ma.masked_invalid(am) res = ma.maximum.reduce(am, axis).data if res.ndim == 0: return res[()] return res def tstd(a, limits=None, inclusive=(True, True), axis=0, ddof=1): """ Compute the trimmed sample standard deviation This function finds the sample standard deviation of given values, ignoring values outside the given `limits`. Parameters ---------- a : array_like array of values limits : None or (lower limit, upper limit), optional Values in the input array less than the lower limit or greater than the upper limit will be ignored. When limits is None, then all values are used. Either of the limit values in the tuple can also be None representing a half-open interval. The default value is None. inclusive : (bool, bool), optional A tuple consisting of the (lower flag, upper flag). These flags determine whether values exactly equal to the lower or upper limits are included. The default value is (True, True). axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Delta degrees of freedom. Default is 1. Returns ------- tstd : float Notes ----- `tstd` computes the unbiased sample standard deviation, i.e. it uses a correction factor ``n / (n - 1)``. Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.tstd(x) 5.9160797830996161 >>> stats.tstd(x, (3,17)) 4.4721359549995796 """ return np.sqrt(tvar(a, limits, inclusive, axis, ddof)) def tsem(a, limits=None, inclusive=(True, True), axis=0, ddof=1): """ Compute the trimmed standard error of the mean. This function finds the standard error of the mean for given values, ignoring values outside the given `limits`. Parameters ---------- a : array_like array of values limits : None or (lower limit, upper limit), optional Values in the input array less than the lower limit or greater than the upper limit will be ignored. When limits is None, then all values are used. Either of the limit values in the tuple can also be None representing a half-open interval. The default value is None. inclusive : (bool, bool), optional A tuple consisting of the (lower flag, upper flag). These flags determine whether values exactly equal to the lower or upper limits are included. The default value is (True, True). axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Delta degrees of freedom. Default is 1. Returns ------- tsem : float Notes ----- `tsem` uses unbiased sample standard deviation, i.e. it uses a correction factor ``n / (n - 1)``. Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.tsem(x) 1.3228756555322954 >>> stats.tsem(x, (3,17)) 1.1547005383792515 """ a = np.asarray(a).ravel() if limits is None: return a.std(ddof=ddof) / np.sqrt(a.size) am = _mask_to_limits(a, limits, inclusive) sd = np.sqrt(np.ma.var(am, ddof=ddof, axis=axis)) return sd / np.sqrt(am.count()) ##################################### # MOMENTS # ##################################### def moment(a, moment=1, axis=0, nan_policy='propagate'): r""" Calculates the nth moment about the mean for a sample. A moment is a specific quantitative measure of the shape of a set of points. It is often used to calculate coefficients of skewness and kurtosis due to its close relationship with them. Parameters ---------- a : array_like data moment : int or array_like of ints, optional order of central moment that is returned. Default is 1. axis : int or None, optional Axis along which the central moment is computed. Default is 0. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- n-th central moment : ndarray or float The appropriate moment along the given axis or over all values if axis is None. The denominator for the moment calculation is the number of observations, no degrees of freedom correction is done. See also -------- kurtosis, skew, describe Notes ----- The k-th central moment of a data sample is: .. math:: m_k = \frac{1}{n} \sum_{i = 1}^n (x_i - \bar{x})^k Where n is the number of samples and x-bar is the mean. This function uses exponentiation by squares [1]_ for efficiency. References ---------- .. [1] http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.moment(a, moment, axis) if a.size == 0: # empty array, return nan(s) with shape matching `moment` if np.isscalar(moment): return np.nan else: return np.ones(np.asarray(moment).shape, dtype=np.float64) * np.nan # for array_like moment input, return a value for each. if not np.isscalar(moment): mmnt = [_moment(a, i, axis) for i in moment] return np.array(mmnt) else: return _moment(a, moment, axis) def _moment(a, moment, axis): if np.abs(moment - np.round(moment)) > 0: raise ValueError("All moment parameters must be integers") if moment == 0: # When moment equals 0, the result is 1, by definition. shape = list(a.shape) del shape[axis] if shape: # return an actual array of the appropriate shape return np.ones(shape, dtype=float) else: # the input was 1D, so return a scalar instead of a rank-0 array return 1.0 elif moment == 1: # By definition the first moment about the mean is 0. shape = list(a.shape) del shape[axis] if shape: # return an actual array of the appropriate shape return np.zeros(shape, dtype=float) else: # the input was 1D, so return a scalar instead of a rank-0 array return np.float64(0.0) else: # Exponentiation by squares: form exponent sequence n_list = [moment] current_n = moment while current_n > 2: if current_n % 2: current_n = (current_n-1)/2 else: current_n /= 2 n_list.append(current_n) # Starting point for exponentiation by squares a_zero_mean = a - np.expand_dims(np.mean(a, axis), axis) if n_list[-1] == 1: s = a_zero_mean.copy() else: s = a_zero_mean**2 # Perform multiplications for n in n_list[-2::-1]: s = s**2 if n % 2: s *= a_zero_mean return np.mean(s, axis) def variation(a, axis=0, nan_policy='propagate'): """ Computes the coefficient of variation, the ratio of the biased standard deviation to the mean. Parameters ---------- a : array_like Input array. axis : int or None, optional Axis along which to calculate the coefficient of variation. Default is 0. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- variation : ndarray The calculated variation along the requested axis. References ---------- .. [1] Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000. """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.variation(a, axis) return a.std(axis) / a.mean(axis) def skew(a, axis=0, bias=True, nan_policy='propagate'): """ Computes the skewness of a data set. For normally distributed data, the skewness should be about 0. A skewness value > 0 means that there is more weight in the left tail of the distribution. The function `skewtest` can be used to determine if the skewness value is close enough to 0, statistically speaking. Parameters ---------- a : ndarray data axis : int or None, optional Axis along which skewness is calculated. Default is 0. If None, compute over the whole array `a`. bias : bool, optional If False, then the calculations are corrected for statistical bias. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- skewness : ndarray The skewness of values along an axis, returning 0 where all values are equal. References ---------- .. [1] Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000. Section 2.2.24.1 """ a, axis = _chk_asarray(a, axis) n = a.shape[axis] contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.skew(a, axis, bias) m2 = moment(a, 2, axis) m3 = moment(a, 3, axis) zero = (m2 == 0) vals = _lazywhere(~zero, (m2, m3), lambda m2, m3: m3 / m2**1.5, 0.) if not bias: can_correct = (n > 2) & (m2 > 0) if can_correct.any(): m2 = np.extract(can_correct, m2) m3 = np.extract(can_correct, m3) nval = np.sqrt((n-1.0)*n) / (n-2.0) * m3/m2**1.5 np.place(vals, can_correct, nval) if vals.ndim == 0: return vals.item() return vals def kurtosis(a, axis=0, fisher=True, bias=True, nan_policy='propagate'): """ Computes the kurtosis (Fisher or Pearson) of a dataset. Kurtosis is the fourth central moment divided by the square of the variance. If Fisher's definition is used, then 3.0 is subtracted from the result to give 0.0 for a normal distribution. If bias is False then the kurtosis is calculated using k statistics to eliminate bias coming from biased moment estimators Use `kurtosistest` to see if result is close enough to normal. Parameters ---------- a : array data for which the kurtosis is calculated axis : int or None, optional Axis along which the kurtosis is calculated. Default is 0. If None, compute over the whole array `a`. fisher : bool, optional If True, Fisher's definition is used (normal ==> 0.0). If False, Pearson's definition is used (normal ==> 3.0). bias : bool, optional If False, then the calculations are corrected for statistical bias. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- kurtosis : array The kurtosis of values along an axis. If all values are equal, return -3 for Fisher's definition and 0 for Pearson's definition. References ---------- .. [1] Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000. """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.kurtosis(a, axis, fisher, bias) n = a.shape[axis] m2 = moment(a, 2, axis) m4 = moment(a, 4, axis) zero = (m2 == 0) olderr = np.seterr(all='ignore') try: vals = np.where(zero, 0, m4 / m2**2.0) finally: np.seterr(**olderr) if not bias: can_correct = (n > 3) & (m2 > 0) if can_correct.any(): m2 = np.extract(can_correct, m2) m4 = np.extract(can_correct, m4) nval = 1.0/(n-2)/(n-3) * ((n**2-1.0)*m4/m2**2.0 - 3*(n-1)**2.0) np.place(vals, can_correct, nval + 3.0) if vals.ndim == 0: vals = vals.item() # array scalar if fisher: return vals - 3 else: return vals DescribeResult = namedtuple('DescribeResult', ('nobs', 'minmax', 'mean', 'variance', 'skewness', 'kurtosis')) def describe(a, axis=0, ddof=1, bias=True, nan_policy='propagate'): """ Computes several descriptive statistics of the passed array. Parameters ---------- a : array_like Input data. axis : int or None, optional Axis along which statistics are calculated. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Delta degrees of freedom (only for variance). Default is 1. bias : bool, optional If False, then the skewness and kurtosis calculations are corrected for statistical bias. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- nobs : int Number of observations (length of data along `axis`). minmax: tuple of ndarrays or floats Minimum and maximum value of data array. mean : ndarray or float Arithmetic mean of data along axis. variance : ndarray or float Unbiased variance of the data along axis, denominator is number of observations minus one. skewness : ndarray or float Skewness, based on moment calculations with denominator equal to the number of observations, i.e. no degrees of freedom correction. kurtosis : ndarray or float Kurtosis (Fisher). The kurtosis is normalized so that it is zero for the normal distribution. No degrees of freedom are used. See Also -------- skew, kurtosis Examples -------- >>> from scipy import stats >>> a = np.arange(10) >>> stats.describe(a) DescribeResult(nobs=10, minmax=(0, 9), mean=4.5, variance=9.1666666666666661, skewness=0.0, kurtosis=-1.2242424242424244) >>> b = [[1, 2], [3, 4]] >>> stats.describe(b) DescribeResult(nobs=2, minmax=(array([1, 2]), array([3, 4])), mean=array([ 2., 3.]), variance=array([ 2., 2.]), skewness=array([ 0., 0.]), kurtosis=array([-2., -2.])) """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.describe(a, axis, ddof, bias) if a.size == 0: raise ValueError("The input must not be empty.") n = a.shape[axis] mm = (np.min(a, axis=axis), np.max(a, axis=axis)) m = np.mean(a, axis=axis) v = np.var(a, axis=axis, ddof=ddof) sk = skew(a, axis, bias=bias) kurt = kurtosis(a, axis, bias=bias) return DescribeResult(n, mm, m, v, sk, kurt) ##################################### # NORMALITY TESTS # ##################################### SkewtestResult = namedtuple('SkewtestResult', ('statistic', 'pvalue')) def skewtest(a, axis=0, nan_policy='propagate'): """ Tests whether the skew is different from the normal distribution. This function tests the null hypothesis that the skewness of the population that the sample was drawn from is the same as that of a corresponding normal distribution. Parameters ---------- a : array The data to be tested axis : int or None, optional Axis along which statistics are calculated. Default is 0. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float The computed z-score for this test. pvalue : float a 2-sided p-value for the hypothesis test Notes ----- The sample size must be at least 8. References ---------- .. [1] R. B. D'Agostino, A. J. Belanger and R. B. D'Agostino Jr., "A suggestion for using powerful and informative tests of normality", American Statistician 44, pp. 316-321, 1990. """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.skewtest(a, axis) if axis is None: a = np.ravel(a) axis = 0 b2 = skew(a, axis) n = float(a.shape[axis]) if n < 8: raise ValueError( "skewtest is not valid with less than 8 samples; %i samples" " were given." % int(n)) y = b2 * math.sqrt(((n + 1) * (n + 3)) / (6.0 * (n - 2))) beta2 = (3.0 * (n**2 + 27*n - 70) * (n+1) * (n+3) / ((n-2.0) * (n+5) * (n+7) * (n+9))) W2 = -1 + math.sqrt(2 * (beta2 - 1)) delta = 1 / math.sqrt(0.5 * math.log(W2)) alpha = math.sqrt(2.0 / (W2 - 1)) y = np.where(y == 0, 1, y) Z = delta * np.log(y / alpha + np.sqrt((y / alpha)**2 + 1)) return SkewtestResult(Z, 2 * distributions.norm.sf(np.abs(Z))) KurtosistestResult = namedtuple('KurtosistestResult', ('statistic', 'pvalue')) def kurtosistest(a, axis=0, nan_policy='propagate'): """ Tests whether a dataset has normal kurtosis This function tests the null hypothesis that the kurtosis of the population from which the sample was drawn is that of the normal distribution: ``kurtosis = 3(n-1)/(n+1)``. Parameters ---------- a : array array of the sample data axis : int or None, optional Axis along which to compute test. Default is 0. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float The computed z-score for this test. pvalue : float The 2-sided p-value for the hypothesis test Notes ----- Valid only for n>20. The Z-score is set to 0 for bad entries. This function uses the method described in [1]_. References ---------- .. [1] see e.g. F. J. Anscombe, W. J. Glynn, "Distribution of the kurtosis statistic b2 for normal samples", Biometrika, vol. 70, pp. 227-234, 1983. """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.kurtosistest(a, axis) n = float(a.shape[axis]) if n < 5: raise ValueError( "kurtosistest requires at least 5 observations; %i observations" " were given." % int(n)) if n < 20: warnings.warn("kurtosistest only valid for n>=20 ... continuing " "anyway, n=%i" % int(n)) b2 = kurtosis(a, axis, fisher=False) E = 3.0*(n-1) / (n+1) varb2 = 24.0*n*(n-2)*(n-3) / ((n+1)*(n+1.)*(n+3)*(n+5)) # [1]_ Eq. 1 x = (b2-E) / np.sqrt(varb2) # [1]_ Eq. 4 # [1]_ Eq. 2: sqrtbeta1 = 6.0*(n*n-5*n+2)/((n+7)*(n+9)) * np.sqrt((6.0*(n+3)*(n+5)) / (n*(n-2)*(n-3))) # [1]_ Eq. 3: A = 6.0 + 8.0/sqrtbeta1 * (2.0/sqrtbeta1 + np.sqrt(1+4.0/(sqrtbeta1**2))) term1 = 1 - 2/(9.0*A) denom = 1 + x*np.sqrt(2/(A-4.0)) denom = np.where(denom < 0, 99, denom) term2 = np.where(denom < 0, term1, np.power((1-2.0/A)/denom, 1/3.0)) Z = (term1 - term2) / np.sqrt(2/(9.0*A)) # [1]_ Eq. 5 Z = np.where(denom == 99, 0, Z) if Z.ndim == 0: Z = Z[()] # zprob uses upper tail, so Z needs to be positive return KurtosistestResult(Z, 2 * distributions.norm.sf(np.abs(Z))) NormaltestResult = namedtuple('NormaltestResult', ('statistic', 'pvalue')) def normaltest(a, axis=0, nan_policy='propagate'): """ Tests whether a sample differs from a normal distribution. This function tests the null hypothesis that a sample comes from a normal distribution. It is based on D'Agostino and Pearson's [1]_, [2]_ test that combines skew and kurtosis to produce an omnibus test of normality. Parameters ---------- a : array_like The array containing the data to be tested. axis : int or None, optional Axis along which to compute test. Default is 0. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float or array ``s^2 + k^2``, where ``s`` is the z-score returned by `skewtest` and ``k`` is the z-score returned by `kurtosistest`. pvalue : float or array A 2-sided chi squared probability for the hypothesis test. References ---------- .. [1] D'Agostino, R. B. (1971), "An omnibus test of normality for moderate and large sample size", Biometrika, 58, 341-348 .. [2] D'Agostino, R. and Pearson, E. S. (1973), "Tests for departure from normality", Biometrika, 60, 613-622 """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.normaltest(a, axis) s, _ = skewtest(a, axis) k, _ = kurtosistest(a, axis) k2 = s*s + k*k return NormaltestResult(k2, distributions.chi2.sf(k2, 2)) def jarque_bera(x): """ Perform the Jarque-Bera goodness of fit test on sample data. The Jarque-Bera test tests whether the sample data has the skewness and kurtosis matching a normal distribution. Note that this test only works for a large enough number of data samples (>2000) as the test statistic asymptotically has a Chi-squared distribution with 2 degrees of freedom. Parameters ---------- x : array_like Observations of a random variable. Returns ------- jb_value : float The test statistic. p : float The p-value for the hypothesis test. References ---------- .. [1] Jarque, C. and Bera, A. (1980) "Efficient tests for normality, homoscedasticity and serial independence of regression residuals", 6 Econometric Letters 255-259. Examples -------- >>> from scipy import stats >>> np.random.seed(987654321) >>> x = np.random.normal(0, 1, 100000) >>> y = np.random.rayleigh(1, 100000) >>> stats.jarque_bera(x) (4.7165707989581342, 0.09458225503041906) >>> stats.jarque_bera(y) (6713.7098548143422, 0.0) """ x = np.asarray(x) n = float(x.size) if n == 0: raise ValueError('At least one observation is required.') mu = x.mean() diffx = x - mu skewness = (1 / n * np.sum(diffx**3)) / (1 / n * np.sum(diffx**2))**(3 / 2.) kurtosis = (1 / n * np.sum(diffx**4)) / (1 / n * np.sum(diffx**2))**2 jb_value = n / 6 * (skewness**2 + (kurtosis - 3)**2 / 4) p = 1 - distributions.chi2.cdf(jb_value, 2) return jb_value, p ##################################### # FREQUENCY FUNCTIONS # ##################################### def itemfreq(a): """ Returns a 2-D array of item frequencies. Parameters ---------- a : (N,) array_like Input array. Returns ------- itemfreq : (K, 2) ndarray A 2-D frequency table. Column 1 contains sorted, unique values from `a`, column 2 contains their respective counts. Examples -------- >>> from scipy import stats >>> a = np.array([1, 1, 5, 0, 1, 2, 2, 0, 1, 4]) >>> stats.itemfreq(a) array([[ 0., 2.], [ 1., 4.], [ 2., 2.], [ 4., 1.], [ 5., 1.]]) >>> np.bincount(a) array([2, 4, 2, 0, 1, 1]) >>> stats.itemfreq(a/10.) array([[ 0. , 2. ], [ 0.1, 4. ], [ 0.2, 2. ], [ 0.4, 1. ], [ 0.5, 1. ]]) """ items, inv = np.unique(a, return_inverse=True) freq = np.bincount(inv) return np.array([items, freq]).T def scoreatpercentile(a, per, limit=(), interpolation_method='fraction', axis=None): """ Calculate the score at a given percentile of the input sequence. For example, the score at `per=50` is the median. If the desired quantile lies between two data points, we interpolate between them, according to the value of `interpolation`. If the parameter `limit` is provided, it should be a tuple (lower, upper) of two values. Parameters ---------- a : array_like A 1-D array of values from which to extract score. per : array_like Percentile(s) at which to extract score. Values should be in range [0,100]. limit : tuple, optional Tuple of two scalars, the lower and upper limits within which to compute the percentile. Values of `a` outside this (closed) interval will be ignored. interpolation_method : {'fraction', 'lower', 'higher'}, optional This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points `i` and `j` - fraction: ``i + (j - i) * fraction`` where ``fraction`` is the fractional part of the index surrounded by ``i`` and ``j``. - lower: ``i``. - higher: ``j``. axis : int, optional Axis along which the percentiles are computed. Default is None. If None, compute over the whole array `a`. Returns ------- score : float or ndarray Score at percentile(s). See Also -------- percentileofscore, numpy.percentile Notes ----- This function will become obsolete in the future. For Numpy 1.9 and higher, `numpy.percentile` provides all the functionality that `scoreatpercentile` provides. And it's significantly faster. Therefore it's recommended to use `numpy.percentile` for users that have numpy >= 1.9. Examples -------- >>> from scipy import stats >>> a = np.arange(100) >>> stats.scoreatpercentile(a, 50) 49.5 """ # adapted from NumPy's percentile function. When we require numpy >= 1.8, # the implementation of this function can be replaced by np.percentile. a = np.asarray(a) if a.size == 0: # empty array, return nan(s) with shape matching `per` if np.isscalar(per): return np.nan else: return np.ones(np.asarray(per).shape, dtype=np.float64) * np.nan if limit: a = a[(limit[0] <= a) & (a <= limit[1])] sorted = np.sort(a, axis=axis) if axis is None: axis = 0 return _compute_qth_percentile(sorted, per, interpolation_method, axis) # handle sequence of per's without calling sort multiple times def _compute_qth_percentile(sorted, per, interpolation_method, axis): if not np.isscalar(per): score = [_compute_qth_percentile(sorted, i, interpolation_method, axis) for i in per] return np.array(score) if (per < 0) or (per > 100): raise ValueError("percentile must be in the range [0, 100]") indexer = [slice(None)] * sorted.ndim idx = per / 100. * (sorted.shape[axis] - 1) if int(idx) != idx: # round fractional indices according to interpolation method if interpolation_method == 'lower': idx = int(np.floor(idx)) elif interpolation_method == 'higher': idx = int(np.ceil(idx)) elif interpolation_method == 'fraction': pass # keep idx as fraction and interpolate else: raise ValueError("interpolation_method can only be 'fraction', " "'lower' or 'higher'") i = int(idx) if i == idx: indexer[axis] = slice(i, i + 1) weights = array(1) sumval = 1.0 else: indexer[axis] = slice(i, i + 2) j = i + 1 weights = array([(j - idx), (idx - i)], float) wshape = [1] * sorted.ndim wshape[axis] = 2 weights.shape = wshape sumval = weights.sum() # Use np.add.reduce (== np.sum but a little faster) to coerce data type return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval def percentileofscore(a, score, kind='rank'): """ The percentile rank of a score relative to a list of scores. A `percentileofscore` of, for example, 80% means that 80% of the scores in `a` are below the given score. In the case of gaps or ties, the exact definition depends on the optional keyword, `kind`. Parameters ---------- a : array_like Array of scores to which `score` is compared. score : int or float Score that is compared to the elements in `a`. kind : {'rank', 'weak', 'strict', 'mean'}, optional This optional parameter specifies the interpretation of the resulting score: - "rank": Average percentage ranking of score. In case of multiple matches, average the percentage rankings of all matching scores. - "weak": This kind corresponds to the definition of a cumulative distribution function. A percentileofscore of 80% means that 80% of values are less than or equal to the provided score. - "strict": Similar to "weak", except that only values that are strictly less than the given score are counted. - "mean": The average of the "weak" and "strict" scores, often used in testing. See http://en.wikipedia.org/wiki/Percentile_rank Returns ------- pcos : float Percentile-position of score (0-100) relative to `a`. See Also -------- numpy.percentile Examples -------- Three-quarters of the given values lie below a given score: >>> from scipy import stats >>> stats.percentileofscore([1, 2, 3, 4], 3) 75.0 With multiple matches, note how the scores of the two matches, 0.6 and 0.8 respectively, are averaged: >>> stats.percentileofscore([1, 2, 3, 3, 4], 3) 70.0 Only 2/5 values are strictly less than 3: >>> stats.percentileofscore([1, 2, 3, 3, 4], 3, kind='strict') 40.0 But 4/5 values are less than or equal to 3: >>> stats.percentileofscore([1, 2, 3, 3, 4], 3, kind='weak') 80.0 The average between the weak and the strict scores is >>> stats.percentileofscore([1, 2, 3, 3, 4], 3, kind='mean') 60.0 """ a = np.array(a) n = len(a) if kind == 'rank': if not np.any(a == score): a = np.append(a, score) a_len = np.array(list(range(len(a)))) else: a_len = np.array(list(range(len(a)))) + 1.0 a = np.sort(a) idx = [a == score] pct = (np.mean(a_len[idx]) / n) * 100.0 return pct elif kind == 'strict': return np.sum(a < score) / float(n) * 100 elif kind == 'weak': return np.sum(a <= score) / float(n) * 100 elif kind == 'mean': return (np.sum(a < score) + np.sum(a <= score)) * 50 / float(n) else: raise ValueError("kind can only be 'rank', 'strict', 'weak' or 'mean'") @np.deprecate(message=("scipy.stats.histogram2 is deprecated in scipy 0.16.0; " "use np.histogram2d instead")) def histogram2(a, bins): """ Compute histogram using divisions in bins. Count the number of times values from array `a` fall into numerical ranges defined by `bins`. Range x is given by bins[x] <= range_x < bins[x+1] where x =0,N and N is the length of the `bins` array. The last range is given by bins[N] <= range_N < infinity. Values less than bins[0] are not included in the histogram. Parameters ---------- a : array_like of rank 1 The array of values to be assigned into bins bins : array_like of rank 1 Defines the ranges of values to use during histogramming. Returns ------- histogram2 : ndarray of rank 1 Each value represents the occurrences for a given bin (range) of values. """ # comment: probably obsoleted by numpy.histogram() n = np.searchsorted(np.sort(a), bins) n = np.concatenate([n, [len(a)]]) return n[1:] - n[:-1] HistogramResult = namedtuple('HistogramResult', ('count', 'lowerlimit', 'binsize', 'extrapoints')) @np.deprecate(message=("scipy.stats.histogram is deprecated in scipy 0.17.0; " "use np.histogram instead")) def histogram(a, numbins=10, defaultlimits=None, weights=None, printextras=False): # _histogram is used in relfreq/cumfreq, so need to keep it res = _histogram(a, numbins=numbins, defaultlimits=defaultlimits, weights=weights, printextras=printextras) return res def _histogram(a, numbins=10, defaultlimits=None, weights=None, printextras=False): """ Separates the range into several bins and returns the number of instances in each bin. Parameters ---------- a : array_like Array of scores which will be put into bins. numbins : int, optional The number of bins to use for the histogram. Default is 10. defaultlimits : tuple (lower, upper), optional The lower and upper values for the range of the histogram. If no value is given, a range slightly larger than the range of the values in a is used. Specifically ``(a.min() - s, a.max() + s)``, where ``s = (1/2)(a.max() - a.min()) / (numbins - 1)``. weights : array_like, optional The weights for each value in `a`. Default is None, which gives each value a weight of 1.0 printextras : bool, optional If True, if there are extra points (i.e. the points that fall outside the bin limits) a warning is raised saying how many of those points there are. Default is False. Returns ------- count : ndarray Number of points (or sum of weights) in each bin. lowerlimit : float Lowest value of histogram, the lower limit of the first bin. binsize : float The size of the bins (all bins have the same size). extrapoints : int The number of points outside the range of the histogram. See Also -------- numpy.histogram Notes ----- This histogram is based on numpy's histogram but has a larger range by default if default limits is not set. """ a = np.ravel(a) if defaultlimits is None: if a.size == 0: # handle empty arrays. Undetermined range, so use 0-1. defaultlimits = (0, 1) else: # no range given, so use values in `a` data_min = a.min() data_max = a.max() # Have bins extend past min and max values slightly s = (data_max - data_min) / (2. * (numbins - 1.)) defaultlimits = (data_min - s, data_max + s) # use numpy's histogram method to compute bins hist, bin_edges = np.histogram(a, bins=numbins, range=defaultlimits, weights=weights) # hist are not always floats, convert to keep with old output hist = np.array(hist, dtype=float) # fixed width for bins is assumed, as numpy's histogram gives # fixed width bins for int values for 'bins' binsize = bin_edges[1] - bin_edges[0] # calculate number of extra points extrapoints = len([v for v in a if defaultlimits[0] > v or v > defaultlimits[1]]) if extrapoints > 0 and printextras: warnings.warn("Points outside given histogram range = %s" % extrapoints) return HistogramResult(hist, defaultlimits[0], binsize, extrapoints) CumfreqResult = namedtuple('CumfreqResult', ('cumcount', 'lowerlimit', 'binsize', 'extrapoints')) def cumfreq(a, numbins=10, defaultreallimits=None, weights=None): """ Returns a cumulative frequency histogram, using the histogram function. A cumulative histogram is a mapping that counts the cumulative number of observations in all of the bins up to the specified bin. Parameters ---------- a : array_like Input array. numbins : int, optional The number of bins to use for the histogram. Default is 10. defaultreallimits : tuple (lower, upper), optional The lower and upper values for the range of the histogram. If no value is given, a range slightly larger than the range of the values in `a` is used. Specifically ``(a.min() - s, a.max() + s)``, where ``s = (1/2)(a.max() - a.min()) / (numbins - 1)``. weights : array_like, optional The weights for each value in `a`. Default is None, which gives each value a weight of 1.0 Returns ------- cumcount : ndarray Binned values of cumulative frequency. lowerlimit : float Lower real limit binsize : float Width of each bin. extrapoints : int Extra points. Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy import stats >>> x = [1, 4, 2, 1, 3, 1] >>> res = stats.cumfreq(x, numbins=4, defaultreallimits=(1.5, 5)) >>> res.cumcount array([ 1., 2., 3., 3.]) >>> res.extrapoints 3 Create a normal distribution with 1000 random values >>> rng = np.random.RandomState(seed=12345) >>> samples = stats.norm.rvs(size=1000, random_state=rng) Calculate cumulative frequencies >>> res = stats.cumfreq(samples, numbins=25) Calculate space of values for x >>> x = res.lowerlimit + np.linspace(0, res.binsize*res.cumcount.size, ... res.cumcount.size) Plot histogram and cumulative histogram >>> fig = plt.figure(figsize=(10, 4)) >>> ax1 = fig.add_subplot(1, 2, 1) >>> ax2 = fig.add_subplot(1, 2, 2) >>> ax1.hist(samples, bins=25) >>> ax1.set_title('Histogram') >>> ax2.bar(x, res.cumcount, width=res.binsize) >>> ax2.set_title('Cumulative histogram') >>> ax2.set_xlim([x.min(), x.max()]) >>> plt.show() """ h, l, b, e = _histogram(a, numbins, defaultreallimits, weights=weights) cumhist = np.cumsum(h * 1, axis=0) return CumfreqResult(cumhist, l, b, e) RelfreqResult = namedtuple('RelfreqResult', ('frequency', 'lowerlimit', 'binsize', 'extrapoints')) def relfreq(a, numbins=10, defaultreallimits=None, weights=None): """ Returns a relative frequency histogram, using the histogram function. A relative frequency histogram is a mapping of the number of observations in each of the bins relative to the total of observations. Parameters ---------- a : array_like Input array. numbins : int, optional The number of bins to use for the histogram. Default is 10. defaultreallimits : tuple (lower, upper), optional The lower and upper values for the range of the histogram. If no value is given, a range slightly larger than the range of the values in a is used. Specifically ``(a.min() - s, a.max() + s)``, where ``s = (1/2)(a.max() - a.min()) / (numbins - 1)``. weights : array_like, optional The weights for each value in `a`. Default is None, which gives each value a weight of 1.0 Returns ------- frequency : ndarray Binned values of relative frequency. lowerlimit : float Lower real limit binsize : float Width of each bin. extrapoints : int Extra points. Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy import stats >>> a = np.array([2, 4, 1, 2, 3, 2]) >>> res = stats.relfreq(a, numbins=4) >>> res.frequency array([ 0.16666667, 0.5 , 0.16666667, 0.16666667]) >>> np.sum(res.frequency) # relative frequencies should add up to 1 1.0 Create a normal distribution with 1000 random values >>> rng = np.random.RandomState(seed=12345) >>> samples = stats.norm.rvs(size=1000, random_state=rng) Calculate relative frequencies >>> res = stats.relfreq(samples, numbins=25) Calculate space of values for x >>> x = res.lowerlimit + np.linspace(0, res.binsize*res.frequency.size, ... res.frequency.size) Plot relative frequency histogram >>> fig = plt.figure(figsize=(5, 4)) >>> ax = fig.add_subplot(1, 1, 1) >>> ax.bar(x, res.frequency, width=res.binsize) >>> ax.set_title('Relative frequency histogram') >>> ax.set_xlim([x.min(), x.max()]) >>> plt.show() """ a = np.asanyarray(a) h, l, b, e = _histogram(a, numbins, defaultreallimits, weights=weights) h = h / float(a.shape[0]) return RelfreqResult(h, l, b, e) ##################################### # VARIABILITY FUNCTIONS # ##################################### def obrientransform(*args): """ Computes the O'Brien transform on input data (any number of arrays). Used to test for homogeneity of variance prior to running one-way stats. Each array in ``*args`` is one level of a factor. If `f_oneway` is run on the transformed data and found significant, the variances are unequal. From Maxwell and Delaney [1]_, p.112. Parameters ---------- args : tuple of array_like Any number of arrays. Returns ------- obrientransform : ndarray Transformed data for use in an ANOVA. The first dimension of the result corresponds to the sequence of transformed arrays. If the arrays given are all 1-D of the same length, the return value is a 2-D array; otherwise it is a 1-D array of type object, with each element being an ndarray. References ---------- .. [1] S. E. Maxwell and H. D. Delaney, "Designing Experiments and Analyzing Data: A Model Comparison Perspective", Wadsworth, 1990. Examples -------- We'll test the following data sets for differences in their variance. >>> x = [10, 11, 13, 9, 7, 12, 12, 9, 10] >>> y = [13, 21, 5, 10, 8, 14, 10, 12, 7, 15] Apply the O'Brien transform to the data. >>> from scipy.stats import obrientransform >>> tx, ty = obrientransform(x, y) Use `scipy.stats.f_oneway` to apply a one-way ANOVA test to the transformed data. >>> from scipy.stats import f_oneway >>> F, p = f_oneway(tx, ty) >>> p 0.1314139477040335 If we require that ``p < 0.05`` for significance, we cannot conclude that the variances are different. """ TINY = np.sqrt(np.finfo(float).eps) # `arrays` will hold the transformed arguments. arrays = [] for arg in args: a = np.asarray(arg) n = len(a) mu = np.mean(a) sq = (a - mu)**2 sumsq = sq.sum() # The O'Brien transform. t = ((n - 1.5) * n * sq - 0.5 * sumsq) / ((n - 1) * (n - 2)) # Check that the mean of the transformed data is equal to the # original variance. var = sumsq / (n - 1) if abs(var - np.mean(t)) > TINY: raise ValueError('Lack of convergence in obrientransform.') arrays.append(t) return np.array(arrays) @np.deprecate(message="scipy.stats.signaltonoise is deprecated in scipy 0.16.0") def signaltonoise(a, axis=0, ddof=0): """ The signal-to-noise ratio of the input data. Returns the signal-to-noise ratio of `a`, here defined as the mean divided by the standard deviation. Parameters ---------- a : array_like An array_like object containing the sample data. axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Degrees of freedom correction for standard deviation. Default is 0. Returns ------- s2n : ndarray The mean to standard deviation ratio(s) along `axis`, or 0 where the standard deviation is 0. """ a = np.asanyarray(a) m = a.mean(axis) sd = a.std(axis=axis, ddof=ddof) return np.where(sd == 0, 0, m/sd) def sem(a, axis=0, ddof=1, nan_policy='propagate'): """ Calculates the standard error of the mean (or standard error of measurement) of the values in the input array. Parameters ---------- a : array_like An array containing the values for which the standard error is returned. axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Delta degrees-of-freedom. How many degrees of freedom to adjust for bias in limited samples relative to the population estimate of variance. Defaults to 1. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- s : ndarray or float The standard error of the mean in the sample(s), along the input axis. Notes ----- The default value for `ddof` is different to the default (0) used by other ddof containing routines, such as np.std and np.nanstd. Examples -------- Find standard error along the first axis: >>> from scipy import stats >>> a = np.arange(20).reshape(5,4) >>> stats.sem(a) array([ 2.8284, 2.8284, 2.8284, 2.8284]) Find standard error across the whole array, using n degrees of freedom: >>> stats.sem(a, axis=None, ddof=0) 1.2893796958227628 """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.sem(a, axis, ddof) n = a.shape[axis] s = np.std(a, axis=axis, ddof=ddof) / np.sqrt(n) return s def zscore(a, axis=0, ddof=0): """ Calculates the z score of each value in the sample, relative to the sample mean and standard deviation. Parameters ---------- a : array_like An array like object containing the sample data. axis : int or None, optional Axis along which to operate. Default is 0. If None, compute over the whole array `a`. ddof : int, optional Degrees of freedom correction in the calculation of the standard deviation. Default is 0. Returns ------- zscore : array_like The z-scores, standardized by mean and standard deviation of input array `a`. Notes ----- This function preserves ndarray subclasses, and works also with matrices and masked arrays (it uses `asanyarray` instead of `asarray` for parameters). Examples -------- >>> a = np.array([ 0.7972, 0.0767, 0.4383, 0.7866, 0.8091, ... 0.1954, 0.6307, 0.6599, 0.1065, 0.0508]) >>> from scipy import stats >>> stats.zscore(a) array([ 1.1273, -1.247 , -0.0552, 1.0923, 1.1664, -0.8559, 0.5786, 0.6748, -1.1488, -1.3324]) Computing along a specified axis, using n-1 degrees of freedom (``ddof=1``) to calculate the standard deviation: >>> b = np.array([[ 0.3148, 0.0478, 0.6243, 0.4608], ... [ 0.7149, 0.0775, 0.6072, 0.9656], ... [ 0.6341, 0.1403, 0.9759, 0.4064], ... [ 0.5918, 0.6948, 0.904 , 0.3721], ... [ 0.0921, 0.2481, 0.1188, 0.1366]]) >>> stats.zscore(b, axis=1, ddof=1) array([[-0.19264823, -1.28415119, 1.07259584, 0.40420358], [ 0.33048416, -1.37380874, 0.04251374, 1.00081084], [ 0.26796377, -1.12598418, 1.23283094, -0.37481053], [-0.22095197, 0.24468594, 1.19042819, -1.21416216], [-0.82780366, 1.4457416 , -0.43867764, -0.1792603 ]]) """ a = np.asanyarray(a) mns = a.mean(axis=axis) sstd = a.std(axis=axis, ddof=ddof) if axis and mns.ndim < a.ndim: return ((a - np.expand_dims(mns, axis=axis)) / np.expand_dims(sstd, axis=axis)) else: return (a - mns) / sstd def zmap(scores, compare, axis=0, ddof=0): """ Calculates the relative z-scores. Returns an array of z-scores, i.e., scores that are standardized to zero mean and unit variance, where mean and variance are calculated from the comparison array. Parameters ---------- scores : array_like The input for which z-scores are calculated. compare : array_like The input from which the mean and standard deviation of the normalization are taken; assumed to have the same dimension as `scores`. axis : int or None, optional Axis over which mean and variance of `compare` are calculated. Default is 0. If None, compute over the whole array `scores`. ddof : int, optional Degrees of freedom correction in the calculation of the standard deviation. Default is 0. Returns ------- zscore : array_like Z-scores, in the same shape as `scores`. Notes ----- This function preserves ndarray subclasses, and works also with matrices and masked arrays (it uses `asanyarray` instead of `asarray` for parameters). Examples -------- >>> from scipy.stats import zmap >>> a = [0.5, 2.0, 2.5, 3] >>> b = [0, 1, 2, 3, 4] >>> zmap(a, b) array([-1.06066017, 0. , 0.35355339, 0.70710678]) """ scores, compare = map(np.asanyarray, [scores, compare]) mns = compare.mean(axis=axis) sstd = compare.std(axis=axis, ddof=ddof) if axis and mns.ndim < compare.ndim: return ((scores - np.expand_dims(mns, axis=axis)) / np.expand_dims(sstd, axis=axis)) else: return (scores - mns) / sstd # Private dictionary initialized only once at module level # See https://en.wikipedia.org/wiki/Robust_measures_of_scale _scale_conversions = {'raw': 1.0, 'normal': special.erfinv(0.5) * 2.0 * math.sqrt(2.0)} def iqr(x, axis=None, rng=(25, 75), scale='raw', nan_policy='propagate', interpolation='linear', keepdims=False): """ Compute the interquartile range of the data along the specified axis. The interquartile range (IQR) is the difference between the 75th and 25th percentile of the data. It is a measure of the dispersion similar to standard deviation or variance, but is much more robust against outliers [2]_. The ``rng`` parameter allows this function to compute other percentile ranges than the actual IQR. For example, setting ``rng=(0, 100)`` is equivalent to `numpy.ptp`. The IQR of an empty array is `np.nan`. .. versionadded:: 0.18.0 Parameters ---------- x : array_like Input array or object that can be converted to an array. axis : int or sequence of int, optional Axis along which the range is computed. The default is to compute the IQR for the entire array. rng : Two-element sequence containing floats in range of [0,100] optional Percentiles over which to compute the range. Each must be between 0 and 100, inclusive. The default is the true IQR: `(25, 75)`. The order of the elements is not important. scale : scalar or str, optional The numerical value of scale will be divided out of the final result. The following string values are recognized: 'raw' : No scaling, just return the raw IQR. 'normal' : Scale by :math:`2 \\sqrt{2} erf^{-1}(\\frac{1}{2}) \\approx 1.349`. The default is 'raw'. Array-like scale is also allowed, as long as it broadcasts correctly to the output such that ``out / scale`` is a valid operation. The output dimensions depend on the input array, `x`, the `axis` argument, and the `keepdims` flag. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}, optional Specifies the interpolation method to use when the percentile boundaries lie between two data points `i` and `j`: * 'linear' : `i + (j - i) * fraction`, where `fraction` is the fractional part of the index surrounded by `i` and `j`. * 'lower' : `i`. * 'higher' : `j`. * 'nearest' : `i` or `j` whichever is nearest. * 'midpoint' : `(i + j) / 2`. Default is 'linear'. keepdims : bool, optional If this is set to `True`, the reduced axes are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array `x`. Returns ------- iqr : scalar or ndarray If ``axis=None``, a scalar is returned. If the input contains integers or floats of smaller precision than ``np.float64``, then the output data-type is ``np.float64``. Otherwise, the output data-type is the same as that of the input. See Also -------- numpy.std, numpy.var Examples -------- >>> from scipy.stats import iqr >>> x = np.array([[10, 7, 4], [3, 2, 1]]) >>> x array([[10, 7, 4], [ 3, 2, 1]]) >>> iqr(x) 4.0 >>> iqr(x, axis=0) array([ 3.5, 2.5, 1.5]) >>> iqr(x, axis=1) array([ 3., 1.]) >>> iqr(x, axis=1, keepdims=True) array([[ 3.], [ 1.]]) Notes ----- This function is heavily dependent on the version of `numpy` that is installed. Versions greater than 1.11.0b3 are highly recommended, as they include a number of enhancements and fixes to `numpy.percentile` and `numpy.nanpercentile` that affect the operation of this function. The following modifications apply: Below 1.10.0 : `nan_policy` is poorly defined. The default behavior of `numpy.percentile` is used for 'propagate'. This is a hybrid of 'omit' and 'propagate' that mostly yields a skewed version of 'omit' since NaNs are sorted to the end of the data. A warning is raised if there are NaNs in the data. Below 1.9.0: `numpy.nanpercentile` does not exist. This means that `numpy.percentile` is used regardless of `nan_policy` and a warning is issued. See previous item for a description of the behavior. Below 1.9.0: `keepdims` and `interpolation` are not supported. The keywords get ignored with a warning if supplied with non-default values. However, multiple axes are still supported. References ---------- .. [1] "Interquartile range" https://en.wikipedia.org/wiki/Interquartile_range .. [2] "Robust measures of scale" https://en.wikipedia.org/wiki/Robust_measures_of_scale .. [3] "Quantile" https://en.wikipedia.org/wiki/Quantile """ x = asarray(x) # This check prevents percentile from raising an error later. Also, it is # consistent with `np.var` and `np.std`. if not x.size: return np.nan # An error may be raised here, so fail-fast, before doing lengthy # computations, even though `scale` is not used until later if isinstance(scale, string_types): scale_key = scale.lower() if scale_key not in _scale_conversions: raise ValueError("{0} not a valid scale for `iqr`".format(scale)) scale = _scale_conversions[scale_key] # Select the percentile function to use based on nans and policy contains_nan, nan_policy = _contains_nan(x, nan_policy) if contains_nan and nan_policy == 'omit': percentile_func = _iqr_nanpercentile else: percentile_func = _iqr_percentile if len(rng) != 2: raise TypeError("quantile range must be two element sequence") rng = sorted(rng) pct = percentile_func(x, rng, axis=axis, interpolation=interpolation, keepdims=keepdims, contains_nan=contains_nan) out = np.subtract(pct[1], pct[0]) if scale != 1.0: out /= scale return out def _iqr_percentile(x, q, axis=None, interpolation='linear', keepdims=False, contains_nan=False): """ Private wrapper that works around older versions of `numpy`. While this function is pretty much necessary for the moment, it should be removed as soon as the minimum supported numpy version allows. """ if contains_nan and NumpyVersion(np.__version__) < '1.10.0a': # I see no way to avoid the version check to ensure that the corrected # NaN behavior has been implemented except to call `percentile` on a # small array. msg = "Keyword nan_policy='propagate' not correctly supported for " \ "numpy versions < 1.10.x. The default behavior of " \ "`numpy.percentile` will be used." warnings.warn(msg, RuntimeWarning) try: # For older versions of numpy, there are two things that can cause a # problem here: missing keywords and non-scalar axis. The former can be # partially handled with a warning, the latter can be handled fully by # hacking in an implementation similar to numpy's function for # providing multi-axis functionality # (`numpy.lib.function_base._ureduce` for the curious). result = np.percentile(x, q, axis=axis, keepdims=keepdims, interpolation=interpolation) except TypeError: if interpolation != 'linear' or keepdims: # At time or writing, this means np.__version__ < 1.9.0 warnings.warn("Keywords interpolation and keepdims not supported " "for your version of numpy", RuntimeWarning) try: # Special processing if axis is an iterable original_size = len(axis) except TypeError: # Axis is a scalar at this point pass else: axis = np.unique(np.asarray(axis) % x.ndim) if original_size > axis.size: # mimic numpy if axes are duplicated raise ValueError("duplicate value in axis") if axis.size == x.ndim: # axis includes all axes: revert to None axis = None elif axis.size == 1: # no rolling necessary axis = axis[0] else: # roll multiple axes to the end and flatten that part out for ax in axis[::-1]: x = np.rollaxis(x, ax, x.ndim) x = x.reshape(x.shape[:-axis.size] + (np.prod(x.shape[-axis.size:]),)) axis = -1 result = np.percentile(x, q, axis=axis) return result def _iqr_nanpercentile(x, q, axis=None, interpolation='linear', keepdims=False, contains_nan=False): """ Private wrapper that works around the following: 1. A bug in `np.nanpercentile` that was around until numpy version 1.11.0. 2. A bug in `np.percentile` NaN handling that was fixed in numpy version 1.10.0. 3. The non-existence of `np.nanpercentile` before numpy version 1.9.0. While this function is pretty much necessary for the moment, it should be removed as soon as the minimum supported numpy version allows. """ if hasattr(np, 'nanpercentile'): # At time or writing, this means np.__version__ < 1.9.0 result = np.nanpercentile(x, q, axis=axis, interpolation=interpolation, keepdims=keepdims) # If non-scalar result and nanpercentile does not do proper axis roll. # I see no way of avoiding the version test since dimensions may just # happen to match in the data. if result.ndim > 1 and NumpyVersion(np.__version__) < '1.11.0a': axis = np.asarray(axis) if axis.size == 1: # If only one axis specified, reduction happens along that dimension if axis.ndim == 0: axis = axis[None] result = np.rollaxis(result, axis[0]) else: # If multiple axes, reduced dimeision is last result = np.rollaxis(result, -1) else: msg = "Keyword nan_policy='omit' not correctly supported for numpy " \ "versions < 1.9.x. The default behavior of numpy.percentile " \ "will be used." warnings.warn(msg, RuntimeWarning) result = _iqr_percentile(x, q, axis=axis) return result ##################################### # TRIMMING FUNCTIONS # ##################################### @np.deprecate(message="stats.threshold is deprecated in scipy 0.17.0") def threshold(a, threshmin=None, threshmax=None, newval=0): """ Clip array to a given value. Similar to numpy.clip(), except that values less than `threshmin` or greater than `threshmax` are replaced by `newval`, instead of by `threshmin` and `threshmax` respectively. Parameters ---------- a : array_like Data to threshold. threshmin : float, int or None, optional Minimum threshold, defaults to None. threshmax : float, int or None, optional Maximum threshold, defaults to None. newval : float or int, optional Value to put in place of values in `a` outside of bounds. Defaults to 0. Returns ------- out : ndarray The clipped input array, with values less than `threshmin` or greater than `threshmax` replaced with `newval`. Examples -------- >>> a = np.array([9, 9, 6, 3, 1, 6, 1, 0, 0, 8]) >>> from scipy import stats >>> stats.threshold(a, threshmin=2, threshmax=8, newval=-1) array([-1, -1, 6, 3, -1, 6, -1, -1, -1, 8]) """ a = asarray(a).copy() mask = zeros(a.shape, dtype=bool) if threshmin is not None: mask |= (a < threshmin) if threshmax is not None: mask |= (a > threshmax) a[mask] = newval return a SigmaclipResult = namedtuple('SigmaclipResult', ('clipped', 'lower', 'upper')) def sigmaclip(a, low=4., high=4.): """ Iterative sigma-clipping of array elements. The output array contains only those elements of the input array `c` that satisfy the conditions :: mean(c) - std(c)*low < c < mean(c) + std(c)*high Starting from the full sample, all elements outside the critical range are removed. The iteration continues with a new critical range until no elements are outside the range. Parameters ---------- a : array_like Data array, will be raveled if not 1-D. low : float, optional Lower bound factor of sigma clipping. Default is 4. high : float, optional Upper bound factor of sigma clipping. Default is 4. Returns ------- clipped : ndarray Input array with clipped elements removed. lower : float Lower threshold value use for clipping. upper : float Upper threshold value use for clipping. Examples -------- >>> from scipy.stats import sigmaclip >>> a = np.concatenate((np.linspace(9.5, 10.5, 31), ... np.linspace(0, 20, 5))) >>> fact = 1.5 >>> c, low, upp = sigmaclip(a, fact, fact) >>> c array([ 9.96666667, 10. , 10.03333333, 10. ]) >>> c.var(), c.std() (0.00055555555555555165, 0.023570226039551501) >>> low, c.mean() - fact*c.std(), c.min() (9.9646446609406727, 9.9646446609406727, 9.9666666666666668) >>> upp, c.mean() + fact*c.std(), c.max() (10.035355339059327, 10.035355339059327, 10.033333333333333) >>> a = np.concatenate((np.linspace(9.5, 10.5, 11), ... np.linspace(-100, -50, 3))) >>> c, low, upp = sigmaclip(a, 1.8, 1.8) >>> (c == np.linspace(9.5, 10.5, 11)).all() True """ c = np.asarray(a).ravel() delta = 1 while delta: c_std = c.std() c_mean = c.mean() size = c.size critlower = c_mean - c_std*low critupper = c_mean + c_std*high c = c[(c > critlower) & (c < critupper)] delta = size - c.size return SigmaclipResult(c, critlower, critupper) def trimboth(a, proportiontocut, axis=0): """ Slices off a proportion of items from both ends of an array. Slices off the passed proportion of items from both ends of the passed array (i.e., with `proportiontocut` = 0.1, slices leftmost 10% **and** rightmost 10% of scores). The trimmed values are the lowest and highest ones. Slices off less if proportion results in a non-integer slice index (i.e., conservatively slices off`proportiontocut`). Parameters ---------- a : array_like Data to trim. proportiontocut : float Proportion (in range 0-1) of total data set to trim of each end. axis : int or None, optional Axis along which to trim data. Default is 0. If None, compute over the whole array `a`. Returns ------- out : ndarray Trimmed version of array `a`. The order of the trimmed content is undefined. See Also -------- trim_mean Examples -------- >>> from scipy import stats >>> a = np.arange(20) >>> b = stats.trimboth(a, 0.1) >>> b.shape (16,) """ a = np.asarray(a) if a.size == 0: return a if axis is None: a = a.ravel() axis = 0 nobs = a.shape[axis] lowercut = int(proportiontocut * nobs) uppercut = nobs - lowercut if (lowercut >= uppercut): raise ValueError("Proportion too big.") atmp = np.partition(a, (lowercut, uppercut - 1), axis) sl = [slice(None)] * atmp.ndim sl[axis] = slice(lowercut, uppercut) return atmp[sl] def trim1(a, proportiontocut, tail='right', axis=0): """ Slices off a proportion from ONE end of the passed array distribution. If `proportiontocut` = 0.1, slices off 'leftmost' or 'rightmost' 10% of scores. The lowest or highest values are trimmed (depending on the tail). Slices off less if proportion results in a non-integer slice index (i.e., conservatively slices off `proportiontocut` ). Parameters ---------- a : array_like Input array proportiontocut : float Fraction to cut off of 'left' or 'right' of distribution tail : {'left', 'right'}, optional Defaults to 'right'. axis : int or None, optional Axis along which to trim data. Default is 0. If None, compute over the whole array `a`. Returns ------- trim1 : ndarray Trimmed version of array `a`. The order of the trimmed content is undefined. """ a = np.asarray(a) if axis is None: a = a.ravel() axis = 0 nobs = a.shape[axis] # avoid possible corner case if proportiontocut >= 1: return [] if tail.lower() == 'right': lowercut = 0 uppercut = nobs - int(proportiontocut * nobs) elif tail.lower() == 'left': lowercut = int(proportiontocut * nobs) uppercut = nobs atmp = np.partition(a, (lowercut, uppercut - 1), axis) return atmp[lowercut:uppercut] def trim_mean(a, proportiontocut, axis=0): """ Return mean of array after trimming distribution from both tails. If `proportiontocut` = 0.1, slices off 'leftmost' and 'rightmost' 10% of scores. The input is sorted before slicing. Slices off less if proportion results in a non-integer slice index (i.e., conservatively slices off `proportiontocut` ). Parameters ---------- a : array_like Input array proportiontocut : float Fraction to cut off of both tails of the distribution axis : int or None, optional Axis along which the trimmed means are computed. Default is 0. If None, compute over the whole array `a`. Returns ------- trim_mean : ndarray Mean of trimmed array. See Also -------- trimboth tmean : compute the trimmed mean ignoring values outside given `limits`. Examples -------- >>> from scipy import stats >>> x = np.arange(20) >>> stats.trim_mean(x, 0.1) 9.5 >>> x2 = x.reshape(5, 4) >>> x2 array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]) >>> stats.trim_mean(x2, 0.25) array([ 8., 9., 10., 11.]) >>> stats.trim_mean(x2, 0.25, axis=1) array([ 1.5, 5.5, 9.5, 13.5, 17.5]) """ a = np.asarray(a) if a.size == 0: return np.nan if axis is None: a = a.ravel() axis = 0 nobs = a.shape[axis] lowercut = int(proportiontocut * nobs) uppercut = nobs - lowercut if (lowercut > uppercut): raise ValueError("Proportion too big.") atmp = np.partition(a, (lowercut, uppercut - 1), axis) sl = [slice(None)] * atmp.ndim sl[axis] = slice(lowercut, uppercut) return np.mean(atmp[sl], axis=axis) F_onewayResult = namedtuple('F_onewayResult', ('statistic', 'pvalue')) def f_oneway(*args): """ Performs a 1-way ANOVA. The one-way ANOVA tests the null hypothesis that two or more groups have the same population mean. The test is applied to samples from two or more groups, possibly with differing sizes. Parameters ---------- sample1, sample2, ... : array_like The sample measurements for each group. Returns ------- statistic : float The computed F-value of the test. pvalue : float The associated p-value from the F-distribution. Notes ----- The ANOVA test has important assumptions that must be satisfied in order for the associated p-value to be valid. 1. The samples are independent. 2. Each sample is from a normally distributed population. 3. The population standard deviations of the groups are all equal. This property is known as homoscedasticity. If these assumptions are not true for a given set of data, it may still be possible to use the Kruskal-Wallis H-test (`scipy.stats.kruskal`) although with some loss of power. The algorithm is from Heiman[2], pp.394-7. References ---------- .. [1] Lowry, Richard. "Concepts and Applications of Inferential Statistics". Chapter 14. http://faculty.vassar.edu/lowry/ch14pt1.html .. [2] Heiman, G.W. Research Methods in Statistics. 2002. .. [3] McDonald, G. H. "Handbook of Biological Statistics", One-way ANOVA. http://www.biostathandbook.com/onewayanova.html Examples -------- >>> import scipy.stats as stats [3]_ Here are some data on a shell measurement (the length of the anterior adductor muscle scar, standardized by dividing by length) in the mussel Mytilus trossulus from five locations: Tillamook, Oregon; Newport, Oregon; Petersburg, Alaska; Magadan, Russia; and Tvarminne, Finland, taken from a much larger data set used in McDonald et al. (1991). >>> tillamook = [0.0571, 0.0813, 0.0831, 0.0976, 0.0817, 0.0859, 0.0735, ... 0.0659, 0.0923, 0.0836] >>> newport = [0.0873, 0.0662, 0.0672, 0.0819, 0.0749, 0.0649, 0.0835, ... 0.0725] >>> petersburg = [0.0974, 0.1352, 0.0817, 0.1016, 0.0968, 0.1064, 0.105] >>> magadan = [0.1033, 0.0915, 0.0781, 0.0685, 0.0677, 0.0697, 0.0764, ... 0.0689] >>> tvarminne = [0.0703, 0.1026, 0.0956, 0.0973, 0.1039, 0.1045] >>> stats.f_oneway(tillamook, newport, petersburg, magadan, tvarminne) (7.1210194716424473, 0.00028122423145345439) """ args = [np.asarray(arg, dtype=float) for arg in args] # ANOVA on N groups, each in its own array num_groups = len(args) alldata = np.concatenate(args) bign = len(alldata) # Determine the mean of the data, and subtract that from all inputs to a # variance (via sum_of_sq / sq_of_sum) calculation. Variance is invariance # to a shift in location, and centering all data around zero vastly # improves numerical stability. offset = alldata.mean() alldata -= offset sstot = _sum_of_squares(alldata) - (_square_of_sums(alldata) / float(bign)) ssbn = 0 for a in args: ssbn += _square_of_sums(a - offset) / float(len(a)) # Naming: variables ending in bn/b are for "between treatments", wn/w are # for "within treatments" ssbn -= (_square_of_sums(alldata) / float(bign)) sswn = sstot - ssbn dfbn = num_groups - 1 dfwn = bign - num_groups msb = ssbn / float(dfbn) msw = sswn / float(dfwn) f = msb / msw prob = special.fdtrc(dfbn, dfwn, f) # equivalent to stats.f.sf return F_onewayResult(f, prob) def pearsonr(x, y): """ Calculates a Pearson correlation coefficient and the p-value for testing non-correlation. The Pearson correlation coefficient measures the linear relationship between two datasets. Strictly speaking, Pearson's correlation requires that each dataset be normally distributed, and not necessarily zero-mean. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. Parameters ---------- x : (N,) array_like Input y : (N,) array_like Input Returns ------- r : float Pearson's correlation coefficient p-value : float 2-tailed p-value References ---------- http://www.statsoft.com/textbook/glosp.html#Pearson%20Correlation """ # x and y should have same length. x = np.asarray(x) y = np.asarray(y) n = len(x) mx = x.mean() my = y.mean() xm, ym = x - mx, y - my r_num = np.add.reduce(xm * ym) r_den = np.sqrt(_sum_of_squares(xm) * _sum_of_squares(ym)) r = r_num / r_den # Presumably, if abs(r) > 1, then it is only some small artifact of floating # point arithmetic. r = max(min(r, 1.0), -1.0) df = n - 2 if abs(r) == 1.0: prob = 0.0 else: t_squared = r**2 * (df / ((1.0 - r) * (1.0 + r))) prob = _betai(0.5*df, 0.5, df/(df+t_squared)) return r, prob def fisher_exact(table, alternative='two-sided'): """Performs a Fisher exact test on a 2x2 contingency table. Parameters ---------- table : array_like of ints A 2x2 contingency table. Elements should be non-negative integers. alternative : {'two-sided', 'less', 'greater'}, optional Which alternative hypothesis to the null hypothesis the test uses. Default is 'two-sided'. Returns ------- oddsratio : float This is prior odds ratio and not a posterior estimate. p_value : float P-value, the probability of obtaining a distribution at least as extreme as the one that was actually observed, assuming that the null hypothesis is true. See Also -------- chi2_contingency : Chi-square test of independence of variables in a contingency table. Notes ----- The calculated odds ratio is different from the one R uses. This scipy implementation returns the (more common) "unconditional Maximum Likelihood Estimate", while R uses the "conditional Maximum Likelihood Estimate". For tables with large numbers, the (inexact) chi-square test implemented in the function `chi2_contingency` can also be used. Examples -------- Say we spend a few days counting whales and sharks in the Atlantic and Indian oceans. In the Atlantic ocean we find 8 whales and 1 shark, in the Indian ocean 2 whales and 5 sharks. Then our contingency table is:: Atlantic Indian whales 8 2 sharks 1 5 We use this table to find the p-value: >>> import scipy.stats as stats >>> oddsratio, pvalue = stats.fisher_exact([[8, 2], [1, 5]]) >>> pvalue 0.0349... The probability that we would observe this or an even more imbalanced ratio by chance is about 3.5%. A commonly used significance level is 5%--if we adopt that, we can therefore conclude that our observed imbalance is statistically significant; whales prefer the Atlantic while sharks prefer the Indian ocean. """ hypergeom = distributions.hypergeom c = np.asarray(table, dtype=np.int64) # int32 is not enough for the algorithm if not c.shape == (2, 2): raise ValueError("The input `table` must be of shape (2, 2).") if np.any(c < 0): raise ValueError("All values in `table` must be nonnegative.") if 0 in c.sum(axis=0) or 0 in c.sum(axis=1): # If both values in a row or column are zero, the p-value is 1 and # the odds ratio is NaN. return np.nan, 1.0 if c[1,0] > 0 and c[0,1] > 0: oddsratio = c[0,0] * c[1,1] / float(c[1,0] * c[0,1]) else: oddsratio = np.inf n1 = c[0,0] + c[0,1] n2 = c[1,0] + c[1,1] n = c[0,0] + c[1,0] def binary_search(n, n1, n2, side): """Binary search for where to begin lower/upper halves in two-sided test. """ if side == "upper": minval = mode maxval = n else: minval = 0 maxval = mode guess = -1 while maxval - minval > 1: if maxval == minval + 1 and guess == minval: guess = maxval else: guess = (maxval + minval) // 2 pguess = hypergeom.pmf(guess, n1 + n2, n1, n) if side == "upper": ng = guess - 1 else: ng = guess + 1 if pguess <= pexact < hypergeom.pmf(ng, n1 + n2, n1, n): break elif pguess < pexact: maxval = guess else: minval = guess if guess == -1: guess = minval if side == "upper": while guess > 0 and hypergeom.pmf(guess, n1 + n2, n1, n) < pexact * epsilon: guess -= 1 while hypergeom.pmf(guess, n1 + n2, n1, n) > pexact / epsilon: guess += 1 else: while hypergeom.pmf(guess, n1 + n2, n1, n) < pexact * epsilon: guess += 1 while guess > 0 and hypergeom.pmf(guess, n1 + n2, n1, n) > pexact / epsilon: guess -= 1 return guess if alternative == 'less': pvalue = hypergeom.cdf(c[0,0], n1 + n2, n1, n) elif alternative == 'greater': # Same formula as the 'less' case, but with the second column. pvalue = hypergeom.cdf(c[0,1], n1 + n2, n1, c[0,1] + c[1,1]) elif alternative == 'two-sided': mode = int(float((n + 1) * (n1 + 1)) / (n1 + n2 + 2)) pexact = hypergeom.pmf(c[0,0], n1 + n2, n1, n) pmode = hypergeom.pmf(mode, n1 + n2, n1, n) epsilon = 1 - 1e-4 if np.abs(pexact - pmode) / np.maximum(pexact, pmode) <= 1 - epsilon: return oddsratio, 1. elif c[0,0] < mode: plower = hypergeom.cdf(c[0,0], n1 + n2, n1, n) if hypergeom.pmf(n, n1 + n2, n1, n) > pexact / epsilon: return oddsratio, plower guess = binary_search(n, n1, n2, "upper") pvalue = plower + hypergeom.sf(guess - 1, n1 + n2, n1, n) else: pupper = hypergeom.sf(c[0,0] - 1, n1 + n2, n1, n) if hypergeom.pmf(0, n1 + n2, n1, n) > pexact / epsilon: return oddsratio, pupper guess = binary_search(n, n1, n2, "lower") pvalue = pupper + hypergeom.cdf(guess, n1 + n2, n1, n) else: msg = "`alternative` should be one of {'two-sided', 'less', 'greater'}" raise ValueError(msg) if pvalue > 1.0: pvalue = 1.0 return oddsratio, pvalue SpearmanrResult = namedtuple('SpearmanrResult', ('correlation', 'pvalue')) def spearmanr(a, b=None, axis=0, nan_policy='propagate'): """ Calculates a Spearman rank-order correlation coefficient and the p-value to test for non-correlation. The Spearman correlation is a nonparametric measure of the monotonicity of the relationship between two datasets. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact monotonic relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. Parameters ---------- a, b : 1D or 2D array_like, b is optional One or two 1-D or 2-D arrays containing multiple variables and observations. When these are 1-D, each represents a vector of observations of a single variable. For the behavior in the 2-D case, see under ``axis``, below. Both arrays need to have the same length in the ``axis`` dimension. axis : int or None, optional If axis=0 (default), then each column represents a variable, with observations in the rows. If axis=1, the relationship is transposed: each row represents a variable, while the columns contain observations. If axis=None, then both arrays will be raveled. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- correlation : float or ndarray (2-D square) Spearman correlation matrix or correlation coefficient (if only 2 variables are given as parameters. Correlation matrix is square with length equal to total number of variables (columns or rows) in a and b combined. pvalue : float The two-sided p-value for a hypothesis test whose null hypothesis is that two sets of data are uncorrelated, has same dimension as rho. Notes ----- Changes in scipy 0.8.0: rewrite to add tie-handling, and axis. References ---------- .. [1] Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000. Section 14.7 Examples -------- >>> from scipy import stats >>> stats.spearmanr([1,2,3,4,5], [5,6,7,8,7]) (0.82078268166812329, 0.088587005313543798) >>> np.random.seed(1234321) >>> x2n = np.random.randn(100, 2) >>> y2n = np.random.randn(100, 2) >>> stats.spearmanr(x2n) (0.059969996999699973, 0.55338590803773591) >>> stats.spearmanr(x2n[:,0], x2n[:,1]) (0.059969996999699973, 0.55338590803773591) >>> rho, pval = stats.spearmanr(x2n, y2n) >>> rho array([[ 1. , 0.05997 , 0.18569457, 0.06258626], [ 0.05997 , 1. , 0.110003 , 0.02534653], [ 0.18569457, 0.110003 , 1. , 0.03488749], [ 0.06258626, 0.02534653, 0.03488749, 1. ]]) >>> pval array([[ 0. , 0.55338591, 0.06435364, 0.53617935], [ 0.55338591, 0. , 0.27592895, 0.80234077], [ 0.06435364, 0.27592895, 0. , 0.73039992], [ 0.53617935, 0.80234077, 0.73039992, 0. ]]) >>> rho, pval = stats.spearmanr(x2n.T, y2n.T, axis=1) >>> rho array([[ 1. , 0.05997 , 0.18569457, 0.06258626], [ 0.05997 , 1. , 0.110003 , 0.02534653], [ 0.18569457, 0.110003 , 1. , 0.03488749], [ 0.06258626, 0.02534653, 0.03488749, 1. ]]) >>> stats.spearmanr(x2n, y2n, axis=None) (0.10816770419260482, 0.1273562188027364) >>> stats.spearmanr(x2n.ravel(), y2n.ravel()) (0.10816770419260482, 0.1273562188027364) >>> xint = np.random.randint(10, size=(100, 2)) >>> stats.spearmanr(xint) (0.052760927029710199, 0.60213045837062351) """ a, axisout = _chk_asarray(a, axis) a_contains_nan, nan_policy = _contains_nan(a, nan_policy) if a_contains_nan: a = ma.masked_invalid(a) if a.size <= 1: return SpearmanrResult(np.nan, np.nan) ar = np.apply_along_axis(rankdata, axisout, a) br = None if b is not None: b, axisout = _chk_asarray(b, axis) b_contains_nan, nan_policy = _contains_nan(b, nan_policy) if a_contains_nan or b_contains_nan: b = ma.masked_invalid(b) if nan_policy == 'propagate': rho, pval = mstats_basic.spearmanr(a, b, axis) return SpearmanrResult(rho * np.nan, pval * np.nan) if nan_policy == 'omit': return mstats_basic.spearmanr(a, b, axis) br = np.apply_along_axis(rankdata, axisout, b) n = a.shape[axisout] rs = np.corrcoef(ar, br, rowvar=axisout) olderr = np.seterr(divide='ignore') # rs can have elements equal to 1 try: # clip the small negative values possibly caused by rounding # errors before taking the square root t = rs * np.sqrt(((n-2)/((rs+1.0)*(1.0-rs))).clip(0)) finally: np.seterr(**olderr) prob = 2 * distributions.t.sf(np.abs(t), n-2) if rs.shape == (2, 2): return SpearmanrResult(rs[1, 0], prob[1, 0]) else: return SpearmanrResult(rs, prob) PointbiserialrResult = namedtuple('PointbiserialrResult', ('correlation', 'pvalue')) def pointbiserialr(x, y): r""" Calculates a point biserial correlation coefficient and its p-value. The point biserial correlation is used to measure the relationship between a binary variable, x, and a continuous variable, y. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply a determinative relationship. This function uses a shortcut formula but produces the same result as `pearsonr`. Parameters ---------- x : array_like of bools Input array. y : array_like Input array. Returns ------- correlation : float R value pvalue : float 2-tailed p-value Notes ----- `pointbiserialr` uses a t-test with ``n-1`` degrees of freedom. It is equivalent to `pearsonr.` The value of the point-biserial correlation can be calculated from: .. math:: r_{pb} = \frac{\overline{Y_{1}} - \overline{Y_{0}}}{s_{y}}\sqrt{\frac{N_{1} N_{2}}{N (N - 1))}} Where :math:`Y_{0}` and :math:`Y_{1}` are means of the metric observations coded 0 and 1 respectively; :math:`N_{0}` and :math:`N_{1}` are number of observations coded 0 and 1 respectively; :math:`N` is the total number of observations and :math:`s_{y}` is the standard deviation of all the metric observations. A value of :math:`r_{pb}` that is significantly different from zero is completely equivalent to a significant difference in means between the two groups. Thus, an independent groups t Test with :math:`N-2` degrees of freedom may be used to test whether :math:`r_{pb}` is nonzero. The relation between the t-statistic for comparing two independent groups and :math:`r_{pb}` is given by: .. math:: t = \sqrt{N - 2}\frac{r_{pb}}{\sqrt{1 - r^{2}_{pb}}} References ---------- .. [1] J. Lev, "The Point Biserial Coefficient of Correlation", Ann. Math. Statist., Vol. 20, no.1, pp. 125-126, 1949. .. [2] R.F. Tate, "Correlation Between a Discrete and a Continuous Variable. Point-Biserial Correlation.", Ann. Math. Statist., Vol. 25, np. 3, pp. 603-607, 1954. .. [3] http://onlinelibrary.wiley.com/doi/10.1002/9781118445112.stat06227/full Examples -------- >>> from scipy import stats >>> a = np.array([0, 0, 0, 1, 1, 1, 1]) >>> b = np.arange(7) >>> stats.pointbiserialr(a, b) (0.8660254037844386, 0.011724811003954652) >>> stats.pearsonr(a, b) (0.86602540378443871, 0.011724811003954626) >>> np.corrcoef(a, b) array([[ 1. , 0.8660254], [ 0.8660254, 1. ]]) """ rpb, prob = pearsonr(x, y) return PointbiserialrResult(rpb, prob) KendalltauResult = namedtuple('KendalltauResult', ('correlation', 'pvalue')) def kendalltau(x, y, initial_lexsort=None, nan_policy='propagate'): """ Calculates Kendall's tau, a correlation measure for ordinal data. Kendall's tau is a measure of the correspondence between two rankings. Values close to 1 indicate strong agreement, values close to -1 indicate strong disagreement. This is the 1945 "tau-b" version of Kendall's tau [2]_, which can account for ties and which reduces to the 1938 "tau-a" version [1]_ in absence of ties. Parameters ---------- x, y : array_like Arrays of rankings, of the same shape. If arrays are not 1-D, they will be flattened to 1-D. initial_lexsort : bool, optional Unused (deprecated). nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Note that if the input contains nan 'omit' delegates to mstats_basic.kendalltau(), which has a different implementation. Returns ------- correlation : float The tau statistic. pvalue : float The two-sided p-value for a hypothesis test whose null hypothesis is an absence of association, tau = 0. See also -------- spearmanr : Calculates a Spearman rank-order correlation coefficient. theilslopes : Computes the Theil-Sen estimator for a set of points (x, y). weightedtau : Computes a weighted version of Kendall's tau. Notes ----- The definition of Kendall's tau that is used is [2]_:: tau = (P - Q) / sqrt((P + Q + T) * (P + Q + U)) where P is the number of concordant pairs, Q the number of discordant pairs, T the number of ties only in `x`, and U the number of ties only in `y`. If a tie occurs for the same pair in both `x` and `y`, it is not added to either T or U. References ---------- .. [1] Maurice G. Kendall, "A New Measure of Rank Correlation", Biometrika Vol. 30, No. 1/2, pp. 81-93, 1938. .. [2] Maurice G. Kendall, "The treatment of ties in ranking problems", Biometrika Vol. 33, No. 3, pp. 239-251. 1945. .. [3] Gottfried E. Noether, "Elements of Nonparametric Statistics", John Wiley & Sons, 1967. .. [4] Peter M. Fenwick, "A new data structure for cumulative frequency tables", Software: Practice and Experience, Vol. 24, No. 3, pp. 327-336, 1994. Examples -------- >>> from scipy import stats >>> x1 = [12, 2, 1, 12, 2] >>> x2 = [1, 4, 7, 1, 0] >>> tau, p_value = stats.kendalltau(x1, x2) >>> tau -0.47140452079103173 >>> p_value 0.2827454599327748 """ x = np.asarray(x).ravel() y = np.asarray(y).ravel() if x.size != y.size: raise ValueError("All inputs to `kendalltau` must be of the same size, " "found x-size %s and y-size %s" % (x.size, y.size)) elif not x.size or not y.size: return KendalltauResult(np.nan, np.nan) # Return NaN if arrays are empty # check both x and y cnx, npx = _contains_nan(x, nan_policy) cny, npy = _contains_nan(y, nan_policy) contains_nan = cnx or cny if npx == 'omit' or npy == 'omit': nan_policy = 'omit' if contains_nan and nan_policy == 'propagate': return KendalltauResult(np.nan, np.nan) elif contains_nan and nan_policy == 'omit': x = ma.masked_invalid(x) y = ma.masked_invalid(y) return mstats_basic.kendalltau(x, y) if initial_lexsort is not None: # deprecate to drop! warnings.warn('"initial_lexsort" is gone!') def count_rank_tie(ranks): cnt = np.bincount(ranks).astype('int64', copy=False) cnt = cnt[cnt > 1] return ((cnt * (cnt - 1) // 2).sum(), (cnt * (cnt - 1.) * (cnt - 2)).sum(), (cnt * (cnt - 1.) * (2*cnt + 5)).sum()) size = x.size perm = np.argsort(y) # sort on y and convert y to dense ranks x, y = x[perm], y[perm] y = np.r_[True, y[1:] != y[:-1]].cumsum(dtype=np.intp) # stable sort on x and convert x to dense ranks perm = np.argsort(x, kind='mergesort') x, y = x[perm], y[perm] x = np.r_[True, x[1:] != x[:-1]].cumsum(dtype=np.intp) dis = _kendall_dis(x, y) # discordant pairs obs = np.r_[True, (x[1:] != x[:-1]) | (y[1:] != y[:-1]), True] cnt = np.diff(np.where(obs)[0]).astype('int64', copy=False) ntie = (cnt * (cnt - 1) // 2).sum() # joint ties xtie, x0, x1 = count_rank_tie(x) # ties in x, stats ytie, y0, y1 = count_rank_tie(y) # ties in y, stats tot = (size * (size - 1)) // 2 if xtie == tot or ytie == tot: return KendalltauResult(np.nan, np.nan) # Note that tot = con + dis + (xtie - ntie) + (ytie - ntie) + ntie # = con + dis + xtie + ytie - ntie con_minus_dis = tot - xtie - ytie + ntie - 2 * dis tau = con_minus_dis / np.sqrt(tot - xtie) / np.sqrt(tot - ytie) # Limit range to fix computational errors tau = min(1., max(-1., tau)) # con_minus_dis is approx normally distributed with this variance [3]_ var = (size * (size - 1) * (2.*size + 5) - x1 - y1) / 18. + ( 2. * xtie * ytie) / (size * (size - 1)) + x0 * y0 / (9. * size * (size - 1) * (size - 2)) pvalue = special.erfc(np.abs(con_minus_dis) / np.sqrt(var) / np.sqrt(2)) # Limit range to fix computational errors return KendalltauResult(min(1., max(-1., tau)), pvalue) WeightedTauResult = namedtuple('WeightedTauResult', ('correlation', 'pvalue')) def weightedtau(x, y, rank=True, weigher=None, additive=True): r""" Computes a weighted version of Kendall's :math:`\tau`. The weighted :math:`\tau` is a weighted version of Kendall's :math:`\tau` in which exchanges of high weight are more influential than exchanges of low weight. The default parameters compute the additive hyperbolic version of the index, :math:`\tau_\mathrm h`, which has been shown to provide the best balance between important and unimportant elements [1]_. The weighting is defined by means of a rank array, which assigns a nonnegative rank to each element, and a weigher function, which assigns a weight based from the rank to each element. The weight of an exchange is then the sum or the product of the weights of the ranks of the exchanged elements. The default parameters compute :math:`\tau_\mathrm h`: an exchange between elements with rank :math:`r` and :math:`s` (starting from zero) has weight :math:`1/(r+1) + 1/(s+1)`. Specifying a rank array is meaningful only if you have in mind an external criterion of importance. If, as it usually happens, you do not have in mind a specific rank, the weighted :math:`\tau` is defined by averaging the values obtained using the decreasing lexicographical rank by (`x`, `y`) and by (`y`, `x`). This is the behavior with default parameters. Note that if you are computing the weighted :math:`\tau` on arrays of ranks, rather than of scores (i.e., a larger value implies a lower rank) you must negate the ranks, so that elements of higher rank are associated with a larger value. Parameters ---------- x, y : array_like Arrays of scores, of the same shape. If arrays are not 1-D, they will be flattened to 1-D. rank: array_like of ints or bool, optional A nonnegative rank assigned to each element. If it is None, the decreasing lexicographical rank by (`x`, `y`) will be used: elements of higher rank will be those with larger `x`-values, using `y`-values to break ties (in particular, swapping `x` and `y` will give a different result). If it is False, the element indices will be used directly as ranks. The default is True, in which case this function returns the average of the values obtained using the decreasing lexicographical rank by (`x`, `y`) and by (`y`, `x`). weigher : callable, optional The weigher function. Must map nonnegative integers (zero representing the most important element) to a nonnegative weight. The default, None, provides hyperbolic weighing, that is, rank :math:`r` is mapped to weight :math:`1/(r+1)`. additive : bool, optional If True, the weight of an exchange is computed by adding the weights of the ranks of the exchanged elements; otherwise, the weights are multiplied. The default is True. Returns ------- correlation : float The weighted :math:`\tau` correlation index. pvalue : float Presently ``np.nan``, as the null statistics is unknown (even in the additive hyperbolic case). See also -------- kendalltau : Calculates Kendall's tau. spearmanr : Calculates a Spearman rank-order correlation coefficient. theilslopes : Computes the Theil-Sen estimator for a set of points (x, y). Notes ----- This function uses an :math:`O(n \log n)`, mergesort-based algorithm [1]_ that is a weighted extension of Knight's algorithm for Kendall's :math:`\tau` [2]_. It can compute Shieh's weighted :math:`\tau` [3]_ between rankings without ties (i.e., permutations) by setting `additive` and `rank` to False, as the definition given in [1]_ is a generalization of Shieh's. NaNs are considered the smallest possible score. .. versionadded:: 0.19.0 References ---------- .. [1] Sebastiano Vigna, "A weighted correlation index for rankings with ties", Proceedings of the 24th international conference on World Wide Web, pp. 1166-1176, ACM, 2015. .. [2] W.R. Knight, "A Computer Method for Calculating Kendall's Tau with Ungrouped Data", Journal of the American Statistical Association, Vol. 61, No. 314, Part 1, pp. 436-439, 1966. .. [3] Grace S. Shieh. "A weighted Kendall's tau statistic", Statistics & Probability Letters, Vol. 39, No. 1, pp. 17-24, 1998. Examples -------- >>> from scipy import stats >>> x = [12, 2, 1, 12, 2] >>> y = [1, 4, 7, 1, 0] >>> tau, p_value = stats.weightedtau(x, y) >>> tau -0.56694968153682723 >>> p_value nan >>> tau, p_value = stats.weightedtau(x, y, additive=False) >>> tau -0.62205716951801038 NaNs are considered the smallest possible score: >>> x = [12, 2, 1, 12, 2] >>> y = [1, 4, 7, 1, np.nan] >>> tau, _ = stats.weightedtau(x, y) >>> tau -0.56694968153682723 This is exactly Kendall's tau: >>> x = [12, 2, 1, 12, 2] >>> y = [1, 4, 7, 1, 0] >>> tau, _ = stats.weightedtau(x, y, weigher=lambda x: 1) >>> tau -0.47140452079103173 >>> x = [12, 2, 1, 12, 2] >>> y = [1, 4, 7, 1, 0] >>> stats.weightedtau(x, y, rank=None) WeightedTauResult(correlation=-0.4157652301037516, pvalue=nan) >>> stats.weightedtau(y, x, rank=None) WeightedTauResult(correlation=-0.71813413296990281, pvalue=nan) """ x = np.asarray(x).ravel() y = np.asarray(y).ravel() if x.size != y.size: raise ValueError("All inputs to `weightedtau` must be of the same size, " "found x-size %s and y-size %s" % (x.size, y.size)) if not x.size: return WeightedTauResult(np.nan, np.nan) # Return NaN if arrays are empty # If there are NaNs we apply _toint64() if np.isnan(np.min(x)): x = _toint64(x) if np.isnan(np.min(y)): y = _toint64(y) # Reduce to ranks unsupported types if x.dtype != y.dtype: if x.dtype != np.int64: x = _toint64(x) if y.dtype != np.int64: y = _toint64(y) else: if x.dtype not in (np.int32, np.int64, np.float32, np.float64): x = _toint64(x) y = _toint64(y) if rank is True: return WeightedTauResult(( _weightedrankedtau(x, y, None, weigher, additive) + _weightedrankedtau(y, x, None, weigher, additive) ) / 2, np.nan) if rank is False: rank = np.arange(x.size, dtype=np.intp) elif rank is not None: rank = np.asarray(rank).ravel() if rank.size != x.size: raise ValueError("All inputs to `weightedtau` must be of the same size, " "found x-size %s and rank-size %s" % (x.size, rank.size)) return WeightedTauResult(_weightedrankedtau(x, y, rank, weigher, additive), np.nan) ##################################### # INFERENTIAL STATISTICS # ##################################### Ttest_1sampResult = namedtuple('Ttest_1sampResult', ('statistic', 'pvalue')) def ttest_1samp(a, popmean, axis=0, nan_policy='propagate'): """ Calculates the T-test for the mean of ONE group of scores. This is a two-sided test for the null hypothesis that the expected value (mean) of a sample of independent observations `a` is equal to the given population mean, `popmean`. Parameters ---------- a : array_like sample observation popmean : float or array_like expected value in null hypothesis, if array_like than it must have the same shape as `a` excluding the axis dimension axis : int or None, optional Axis along which to compute test. If None, compute over the whole array `a`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float or array t-statistic pvalue : float or array two-tailed p-value Examples -------- >>> from scipy import stats >>> np.random.seed(7654567) # fix seed to get the same result >>> rvs = stats.norm.rvs(loc=5, scale=10, size=(50,2)) Test if mean of random sample is equal to true mean, and different mean. We reject the null hypothesis in the second case and don't reject it in the first case. >>> stats.ttest_1samp(rvs,5.0) (array([-0.68014479, -0.04323899]), array([ 0.49961383, 0.96568674])) >>> stats.ttest_1samp(rvs,0.0) (array([ 2.77025808, 4.11038784]), array([ 0.00789095, 0.00014999])) Examples using axis and non-scalar dimension for population mean. >>> stats.ttest_1samp(rvs,[5.0,0.0]) (array([-0.68014479, 4.11038784]), array([ 4.99613833e-01, 1.49986458e-04])) >>> stats.ttest_1samp(rvs.T,[5.0,0.0],axis=1) (array([-0.68014479, 4.11038784]), array([ 4.99613833e-01, 1.49986458e-04])) >>> stats.ttest_1samp(rvs,[[5.0],[0.0]]) (array([[-0.68014479, -0.04323899], [ 2.77025808, 4.11038784]]), array([[ 4.99613833e-01, 9.65686743e-01], [ 7.89094663e-03, 1.49986458e-04]])) """ a, axis = _chk_asarray(a, axis) contains_nan, nan_policy = _contains_nan(a, nan_policy) if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) return mstats_basic.ttest_1samp(a, popmean, axis) n = a.shape[axis] df = n - 1 d = np.mean(a, axis) - popmean v = np.var(a, axis, ddof=1) denom = np.sqrt(v / float(n)) with np.errstate(divide='ignore', invalid='ignore'): t = np.divide(d, denom) t, prob = _ttest_finish(df, t) return Ttest_1sampResult(t, prob) def _ttest_finish(df, t): """Common code between all 3 t-test functions.""" prob = distributions.t.sf(np.abs(t), df) * 2 # use np.abs to get upper tail if t.ndim == 0: t = t[()] return t, prob def _ttest_ind_from_stats(mean1, mean2, denom, df): d = mean1 - mean2 with np.errstate(divide='ignore', invalid='ignore'): t = np.divide(d, denom) t, prob = _ttest_finish(df, t) return (t, prob) def _unequal_var_ttest_denom(v1, n1, v2, n2): vn1 = v1 / n1 vn2 = v2 / n2 with np.errstate(divide='ignore', invalid='ignore'): df = (vn1 + vn2)**2 / (vn1**2 / (n1 - 1) + vn2**2 / (n2 - 1)) # If df is undefined, variances are zero (assumes n1 > 0 & n2 > 0). # Hence it doesn't matter what df is as long as it's not NaN. df = np.where(np.isnan(df), 1, df) denom = np.sqrt(vn1 + vn2) return df, denom def _equal_var_ttest_denom(v1, n1, v2, n2): df = n1 + n2 - 2.0 svar = ((n1 - 1) * v1 + (n2 - 1) * v2) / df denom = np.sqrt(svar * (1.0 / n1 + 1.0 / n2)) return df, denom Ttest_indResult = namedtuple('Ttest_indResult', ('statistic', 'pvalue')) def ttest_ind_from_stats(mean1, std1, nobs1, mean2, std2, nobs2, equal_var=True): """ T-test for means of two independent samples from descriptive statistics. This is a two-sided test for the null hypothesis that 2 independent samples have identical average (expected) values. Parameters ---------- mean1 : array_like The mean(s) of sample 1. std1 : array_like The standard deviation(s) of sample 1. nobs1 : array_like The number(s) of observations of sample 1. mean2 : array_like The mean(s) of sample 2 std2 : array_like The standard deviations(s) of sample 2. nobs2 : array_like The number(s) of observations of sample 2. equal_var : bool, optional If True (default), perform a standard independent 2 sample test that assumes equal population variances [1]_. If False, perform Welch's t-test, which does not assume equal population variance [2]_. Returns ------- statistic : float or array The calculated t-statistics pvalue : float or array The two-tailed p-value. See also -------- scipy.stats.ttest_ind Notes ----- .. versionadded:: 0.16.0 References ---------- .. [1] http://en.wikipedia.org/wiki/T-test#Independent_two-sample_t-test .. [2] http://en.wikipedia.org/wiki/Welch%27s_t_test """ if equal_var: df, denom = _equal_var_ttest_denom(std1**2, nobs1, std2**2, nobs2) else: df, denom = _unequal_var_ttest_denom(std1**2, nobs1, std2**2, nobs2) res = _ttest_ind_from_stats(mean1, mean2, denom, df) return Ttest_indResult(*res) def ttest_ind(a, b, axis=0, equal_var=True, nan_policy='propagate'): """ Calculates the T-test for the means of *two independent* samples of scores. This is a two-sided test for the null hypothesis that 2 independent samples have identical average (expected) values. This test assumes that the populations have identical variances by default. Parameters ---------- a, b : array_like The arrays must have the same shape, except in the dimension corresponding to `axis` (the first, by default). axis : int or None, optional Axis along which to compute test. If None, compute over the whole arrays, `a`, and `b`. equal_var : bool, optional If True (default), perform a standard independent 2 sample test that assumes equal population variances [1]_. If False, perform Welch's t-test, which does not assume equal population variance [2]_. .. versionadded:: 0.11.0 nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float or array The calculated t-statistic. pvalue : float or array The two-tailed p-value. Notes ----- We can use this test, if we observe two independent samples from the same or different population, e.g. exam scores of boys and girls or of two ethnic groups. The test measures whether the average (expected) value differs significantly across samples. If we observe a large p-value, for example larger than 0.05 or 0.1, then we cannot reject the null hypothesis of identical average scores. If the p-value is smaller than the threshold, e.g. 1%, 5% or 10%, then we reject the null hypothesis of equal averages. References ---------- .. [1] http://en.wikipedia.org/wiki/T-test#Independent_two-sample_t-test .. [2] http://en.wikipedia.org/wiki/Welch%27s_t_test Examples -------- >>> from scipy import stats >>> np.random.seed(12345678) Test with sample with identical means: >>> rvs1 = stats.norm.rvs(loc=5,scale=10,size=500) >>> rvs2 = stats.norm.rvs(loc=5,scale=10,size=500) >>> stats.ttest_ind(rvs1,rvs2) (0.26833823296239279, 0.78849443369564776) >>> stats.ttest_ind(rvs1,rvs2, equal_var = False) (0.26833823296239279, 0.78849452749500748) `ttest_ind` underestimates p for unequal variances: >>> rvs3 = stats.norm.rvs(loc=5, scale=20, size=500) >>> stats.ttest_ind(rvs1, rvs3) (-0.46580283298287162, 0.64145827413436174) >>> stats.ttest_ind(rvs1, rvs3, equal_var = False) (-0.46580283298287162, 0.64149646246569292) When n1 != n2, the equal variance t-statistic is no longer equal to the unequal variance t-statistic: >>> rvs4 = stats.norm.rvs(loc=5, scale=20, size=100) >>> stats.ttest_ind(rvs1, rvs4) (-0.99882539442782481, 0.3182832709103896) >>> stats.ttest_ind(rvs1, rvs4, equal_var = False) (-0.69712570584654099, 0.48716927725402048) T-test with different means, variance, and n: >>> rvs5 = stats.norm.rvs(loc=8, scale=20, size=100) >>> stats.ttest_ind(rvs1, rvs5) (-1.4679669854490653, 0.14263895620529152) >>> stats.ttest_ind(rvs1, rvs5, equal_var = False) (-0.94365973617132992, 0.34744170334794122) """ a, b, axis = _chk2_asarray(a, b, axis) # check both a and b cna, npa = _contains_nan(a, nan_policy) cnb, npb = _contains_nan(b, nan_policy) contains_nan = cna or cnb if npa == 'omit' or npb == 'omit': nan_policy = 'omit' if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) b = ma.masked_invalid(b) return mstats_basic.ttest_ind(a, b, axis, equal_var) if a.size == 0 or b.size == 0: return Ttest_indResult(np.nan, np.nan) v1 = np.var(a, axis, ddof=1) v2 = np.var(b, axis, ddof=1) n1 = a.shape[axis] n2 = b.shape[axis] if equal_var: df, denom = _equal_var_ttest_denom(v1, n1, v2, n2) else: df, denom = _unequal_var_ttest_denom(v1, n1, v2, n2) res = _ttest_ind_from_stats(np.mean(a, axis), np.mean(b, axis), denom, df) return Ttest_indResult(*res) Ttest_relResult = namedtuple('Ttest_relResult', ('statistic', 'pvalue')) def ttest_rel(a, b, axis=0, nan_policy='propagate'): """ Calculates the T-test on TWO RELATED samples of scores, a and b. This is a two-sided test for the null hypothesis that 2 related or repeated samples have identical average (expected) values. Parameters ---------- a, b : array_like The arrays must have the same shape. axis : int or None, optional Axis along which to compute test. If None, compute over the whole arrays, `a`, and `b`. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float or array t-statistic pvalue : float or array two-tailed p-value Notes ----- Examples for the use are scores of the same set of student in different exams, or repeated sampling from the same units. The test measures whether the average score differs significantly across samples (e.g. exams). If we observe a large p-value, for example greater than 0.05 or 0.1 then we cannot reject the null hypothesis of identical average scores. If the p-value is smaller than the threshold, e.g. 1%, 5% or 10%, then we reject the null hypothesis of equal averages. Small p-values are associated with large t-statistics. References ---------- http://en.wikipedia.org/wiki/T-test#Dependent_t-test Examples -------- >>> from scipy import stats >>> np.random.seed(12345678) # fix random seed to get same numbers >>> rvs1 = stats.norm.rvs(loc=5,scale=10,size=500) >>> rvs2 = (stats.norm.rvs(loc=5,scale=10,size=500) + ... stats.norm.rvs(scale=0.2,size=500)) >>> stats.ttest_rel(rvs1,rvs2) (0.24101764965300962, 0.80964043445811562) >>> rvs3 = (stats.norm.rvs(loc=8,scale=10,size=500) + ... stats.norm.rvs(scale=0.2,size=500)) >>> stats.ttest_rel(rvs1,rvs3) (-3.9995108708727933, 7.3082402191726459e-005) """ a, b, axis = _chk2_asarray(a, b, axis) cna, npa = _contains_nan(a, nan_policy) cnb, npb = _contains_nan(b, nan_policy) contains_nan = cna or cnb if npa == 'omit' or npb == 'omit': nan_policy = 'omit' if contains_nan and nan_policy == 'omit': a = ma.masked_invalid(a) b = ma.masked_invalid(b) m = ma.mask_or(ma.getmask(a), ma.getmask(b)) aa = ma.array(a, mask=m, copy=True) bb = ma.array(b, mask=m, copy=True) return mstats_basic.ttest_rel(aa, bb, axis) if a.shape[axis] != b.shape[axis]: raise ValueError('unequal length arrays') if a.size == 0 or b.size == 0: return np.nan, np.nan n = a.shape[axis] df = float(n - 1) d = (a - b).astype(np.float64) v = np.var(d, axis, ddof=1) dm = np.mean(d, axis) denom = np.sqrt(v / float(n)) with np.errstate(divide='ignore', invalid='ignore'): t = np.divide(dm, denom) t, prob = _ttest_finish(df, t) return Ttest_relResult(t, prob) KstestResult = namedtuple('KstestResult', ('statistic', 'pvalue')) def kstest(rvs, cdf, args=(), N=20, alternative='two-sided', mode='approx'): """ Perform the Kolmogorov-Smirnov test for goodness of fit. This performs a test of the distribution G(x) of an observed random variable against a given distribution F(x). Under the null hypothesis the two distributions are identical, G(x)=F(x). The alternative hypothesis can be either 'two-sided' (default), 'less' or 'greater'. The KS test is only valid for continuous distributions. Parameters ---------- rvs : str, array or callable If a string, it should be the name of a distribution in `scipy.stats`. If an array, it should be a 1-D array of observations of random variables. If a callable, it should be a function to generate random variables; it is required to have a keyword argument `size`. cdf : str or callable If a string, it should be the name of a distribution in `scipy.stats`. If `rvs` is a string then `cdf` can be False or the same as `rvs`. If a callable, that callable is used to calculate the cdf. args : tuple, sequence, optional Distribution parameters, used if `rvs` or `cdf` are strings. N : int, optional Sample size if `rvs` is string or callable. Default is 20. alternative : {'two-sided', 'less','greater'}, optional Defines the alternative hypothesis (see explanation above). Default is 'two-sided'. mode : 'approx' (default) or 'asymp', optional Defines the distribution used for calculating the p-value. - 'approx' : use approximation to exact distribution of test statistic - 'asymp' : use asymptotic distribution of test statistic Returns ------- statistic : float KS test statistic, either D, D+ or D-. pvalue : float One-tailed or two-tailed p-value. Notes ----- In the one-sided test, the alternative is that the empirical cumulative distribution function of the random variable is "less" or "greater" than the cumulative distribution function F(x) of the hypothesis, ``G(x)<=F(x)``, resp. ``G(x)>=F(x)``. Examples -------- >>> from scipy import stats >>> x = np.linspace(-15, 15, 9) >>> stats.kstest(x, 'norm') (0.44435602715924361, 0.038850142705171065) >>> np.random.seed(987654321) # set random seed to get the same result >>> stats.kstest('norm', False, N=100) (0.058352892479417884, 0.88531190944151261) The above lines are equivalent to: >>> np.random.seed(987654321) >>> stats.kstest(stats.norm.rvs(size=100), 'norm') (0.058352892479417884, 0.88531190944151261) *Test against one-sided alternative hypothesis* Shift distribution to larger values, so that ``cdf_dgp(x) < norm.cdf(x)``: >>> np.random.seed(987654321) >>> x = stats.norm.rvs(loc=0.2, size=100) >>> stats.kstest(x,'norm', alternative = 'less') (0.12464329735846891, 0.040989164077641749) Reject equal distribution against alternative hypothesis: less >>> stats.kstest(x,'norm', alternative = 'greater') (0.0072115233216311081, 0.98531158590396395) Don't reject equal distribution against alternative hypothesis: greater >>> stats.kstest(x,'norm', mode='asymp') (0.12464329735846891, 0.08944488871182088) *Testing t distributed random variables against normal distribution* With 100 degrees of freedom the t distribution looks close to the normal distribution, and the K-S test does not reject the hypothesis that the sample came from the normal distribution: >>> np.random.seed(987654321) >>> stats.kstest(stats.t.rvs(100,size=100),'norm') (0.072018929165471257, 0.67630062862479168) With 3 degrees of freedom the t distribution looks sufficiently different from the normal distribution, that we can reject the hypothesis that the sample came from the normal distribution at the 10% level: >>> np.random.seed(987654321) >>> stats.kstest(stats.t.rvs(3,size=100),'norm') (0.131016895759829, 0.058826222555312224) """ if isinstance(rvs, string_types): if (not cdf) or (cdf == rvs): cdf = getattr(distributions, rvs).cdf rvs = getattr(distributions, rvs).rvs else: raise AttributeError("if rvs is string, cdf has to be the " "same distribution") if isinstance(cdf, string_types): cdf = getattr(distributions, cdf).cdf if callable(rvs): kwds = {'size': N} vals = np.sort(rvs(*args, **kwds)) else: vals = np.sort(rvs) N = len(vals) cdfvals = cdf(vals, *args) # to not break compatibility with existing code if alternative == 'two_sided': alternative = 'two-sided' if alternative in ['two-sided', 'greater']: Dplus = (np.arange(1.0, N + 1)/N - cdfvals).max() if alternative == 'greater': return KstestResult(Dplus, distributions.ksone.sf(Dplus, N)) if alternative in ['two-sided', 'less']: Dmin = (cdfvals - np.arange(0.0, N)/N).max() if alternative == 'less': return KstestResult(Dmin, distributions.ksone.sf(Dmin, N)) if alternative == 'two-sided': D = np.max([Dplus, Dmin]) if mode == 'asymp': return KstestResult(D, distributions.kstwobign.sf(D * np.sqrt(N))) if mode == 'approx': pval_two = distributions.kstwobign.sf(D * np.sqrt(N)) if N > 2666 or pval_two > 0.80 - N*0.3/1000: return KstestResult(D, pval_two) else: return KstestResult(D, 2 * distributions.ksone.sf(D, N)) # Map from names to lambda_ values used in power_divergence(). _power_div_lambda_names = { "pearson": 1, "log-likelihood": 0, "freeman-tukey": -0.5, "mod-log-likelihood": -1, "neyman": -2, "cressie-read": 2/3, } def _count(a, axis=None): """ Count the number of non-masked elements of an array. This function behaves like np.ma.count(), but is much faster for ndarrays. """ if hasattr(a, 'count'): num = a.count(axis=axis) if isinstance(num, np.ndarray) and num.ndim == 0: # In some cases, the `count` method returns a scalar array (e.g. # np.array(3)), but we want a plain integer. num = int(num) else: if axis is None: num = a.size else: num = a.shape[axis] return num Power_divergenceResult = namedtuple('Power_divergenceResult', ('statistic', 'pvalue')) def power_divergence(f_obs, f_exp=None, ddof=0, axis=0, lambda_=None): """ Cressie-Read power divergence statistic and goodness of fit test. This function tests the null hypothesis that the categorical data has the given frequencies, using the Cressie-Read power divergence statistic. Parameters ---------- f_obs : array_like Observed frequencies in each category. f_exp : array_like, optional Expected frequencies in each category. By default the categories are assumed to be equally likely. ddof : int, optional "Delta degrees of freedom": adjustment to the degrees of freedom for the p-value. The p-value is computed using a chi-squared distribution with ``k - 1 - ddof`` degrees of freedom, where `k` is the number of observed frequencies. The default value of `ddof` is 0. axis : int or None, optional The axis of the broadcast result of `f_obs` and `f_exp` along which to apply the test. If axis is None, all values in `f_obs` are treated as a single data set. Default is 0. lambda_ : float or str, optional `lambda_` gives the power in the Cressie-Read power divergence statistic. The default is 1. For convenience, `lambda_` may be assigned one of the following strings, in which case the corresponding numerical value is used:: String Value Description "pearson" 1 Pearson's chi-squared statistic. In this case, the function is equivalent to `stats.chisquare`. "log-likelihood" 0 Log-likelihood ratio. Also known as the G-test [3]_. "freeman-tukey" -1/2 Freeman-Tukey statistic. "mod-log-likelihood" -1 Modified log-likelihood ratio. "neyman" -2 Neyman's statistic. "cressie-read" 2/3 The power recommended in [5]_. Returns ------- statistic : float or ndarray The Cressie-Read power divergence test statistic. The value is a float if `axis` is None or if` `f_obs` and `f_exp` are 1-D. pvalue : float or ndarray The p-value of the test. The value is a float if `ddof` and the return value `stat` are scalars. See Also -------- chisquare Notes ----- This test is invalid when the observed or expected frequencies in each category are too small. A typical rule is that all of the observed and expected frequencies should be at least 5. When `lambda_` is less than zero, the formula for the statistic involves dividing by `f_obs`, so a warning or error may be generated if any value in `f_obs` is 0. Similarly, a warning or error may be generated if any value in `f_exp` is zero when `lambda_` >= 0. The default degrees of freedom, k-1, are for the case when no parameters of the distribution are estimated. If p parameters are estimated by efficient maximum likelihood then the correct degrees of freedom are k-1-p. If the parameters are estimated in a different way, then the dof can be between k-1-p and k-1. However, it is also possible that the asymptotic distribution is not a chisquare, in which case this test is not appropriate. This function handles masked arrays. If an element of `f_obs` or `f_exp` is masked, then data at that position is ignored, and does not count towards the size of the data set. .. versionadded:: 0.13.0 References ---------- .. [1] Lowry, Richard. "Concepts and Applications of Inferential Statistics". Chapter 8. http://faculty.vassar.edu/lowry/ch8pt1.html .. [2] "Chi-squared test", http://en.wikipedia.org/wiki/Chi-squared_test .. [3] "G-test", http://en.wikipedia.org/wiki/G-test .. [4] Sokal, R. R. and Rohlf, F. J. "Biometry: the principles and practice of statistics in biological research", New York: Freeman (1981) .. [5] Cressie, N. and Read, T. R. C., "Multinomial Goodness-of-Fit Tests", J. Royal Stat. Soc. Series B, Vol. 46, No. 3 (1984), pp. 440-464. Examples -------- (See `chisquare` for more examples.) When just `f_obs` is given, it is assumed that the expected frequencies are uniform and given by the mean of the observed frequencies. Here we perform a G-test (i.e. use the log-likelihood ratio statistic): >>> from scipy.stats import power_divergence >>> power_divergence([16, 18, 16, 14, 12, 12], lambda_='log-likelihood') (2.006573162632538, 0.84823476779463769) The expected frequencies can be given with the `f_exp` argument: >>> power_divergence([16, 18, 16, 14, 12, 12], ... f_exp=[16, 16, 16, 16, 16, 8], ... lambda_='log-likelihood') (3.3281031458963746, 0.6495419288047497) When `f_obs` is 2-D, by default the test is applied to each column. >>> obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T >>> obs.shape (6, 2) >>> power_divergence(obs, lambda_="log-likelihood") (array([ 2.00657316, 6.77634498]), array([ 0.84823477, 0.23781225])) By setting ``axis=None``, the test is applied to all data in the array, which is equivalent to applying the test to the flattened array. >>> power_divergence(obs, axis=None) (23.31034482758621, 0.015975692534127565) >>> power_divergence(obs.ravel()) (23.31034482758621, 0.015975692534127565) `ddof` is the change to make to the default degrees of freedom. >>> power_divergence([16, 18, 16, 14, 12, 12], ddof=1) (2.0, 0.73575888234288467) The calculation of the p-values is done by broadcasting the test statistic with `ddof`. >>> power_divergence([16, 18, 16, 14, 12, 12], ddof=[0,1,2]) (2.0, array([ 0.84914504, 0.73575888, 0.5724067 ])) `f_obs` and `f_exp` are also broadcast. In the following, `f_obs` has shape (6,) and `f_exp` has shape (2, 6), so the result of broadcasting `f_obs` and `f_exp` has shape (2, 6). To compute the desired chi-squared statistics, we must use ``axis=1``: >>> power_divergence([16, 18, 16, 14, 12, 12], ... f_exp=[[16, 16, 16, 16, 16, 8], ... [8, 20, 20, 16, 12, 12]], ... axis=1) (array([ 3.5 , 9.25]), array([ 0.62338763, 0.09949846])) """ # Convert the input argument `lambda_` to a numerical value. if isinstance(lambda_, string_types): if lambda_ not in _power_div_lambda_names: names = repr(list(_power_div_lambda_names.keys()))[1:-1] raise ValueError("invalid string for lambda_: {0!r}. Valid strings " "are {1}".format(lambda_, names)) lambda_ = _power_div_lambda_names[lambda_] elif lambda_ is None: lambda_ = 1 f_obs = np.asanyarray(f_obs) if f_exp is not None: f_exp = np.atleast_1d(np.asanyarray(f_exp)) else: # Compute the equivalent of # f_exp = f_obs.mean(axis=axis, keepdims=True) # Older versions of numpy do not have the 'keepdims' argument, so # we have to do a little work to achieve the same result. # Ignore 'invalid' errors so the edge case of a data set with length 0 # is handled without spurious warnings. with np.errstate(invalid='ignore'): f_exp = np.atleast_1d(f_obs.mean(axis=axis)) if axis is not None: reduced_shape = list(f_obs.shape) reduced_shape[axis] = 1 f_exp.shape = reduced_shape # `terms` is the array of terms that are summed along `axis` to create # the test statistic. We use some specialized code for a few special # cases of lambda_. if lambda_ == 1: # Pearson's chi-squared statistic terms = (f_obs - f_exp)**2 / f_exp elif lambda_ == 0: # Log-likelihood ratio (i.e. G-test) terms = 2.0 * special.xlogy(f_obs, f_obs / f_exp) elif lambda_ == -1: # Modified log-likelihood ratio terms = 2.0 * special.xlogy(f_exp, f_exp / f_obs) else: # General Cressie-Read power divergence. terms = f_obs * ((f_obs / f_exp)**lambda_ - 1) terms /= 0.5 * lambda_ * (lambda_ + 1) stat = terms.sum(axis=axis) num_obs = _count(terms, axis=axis) ddof = asarray(ddof) p = distributions.chi2.sf(stat, num_obs - 1 - ddof) return Power_divergenceResult(stat, p) def chisquare(f_obs, f_exp=None, ddof=0, axis=0): """ Calculates a one-way chi square test. The chi square test tests the null hypothesis that the categorical data has the given frequencies. Parameters ---------- f_obs : array_like Observed frequencies in each category. f_exp : array_like, optional Expected frequencies in each category. By default the categories are assumed to be equally likely. ddof : int, optional "Delta degrees of freedom": adjustment to the degrees of freedom for the p-value. The p-value is computed using a chi-squared distribution with ``k - 1 - ddof`` degrees of freedom, where `k` is the number of observed frequencies. The default value of `ddof` is 0. axis : int or None, optional The axis of the broadcast result of `f_obs` and `f_exp` along which to apply the test. If axis is None, all values in `f_obs` are treated as a single data set. Default is 0. Returns ------- chisq : float or ndarray The chi-squared test statistic. The value is a float if `axis` is None or `f_obs` and `f_exp` are 1-D. p : float or ndarray The p-value of the test. The value is a float if `ddof` and the return value `chisq` are scalars. See Also -------- power_divergence mstats.chisquare Notes ----- This test is invalid when the observed or expected frequencies in each category are too small. A typical rule is that all of the observed and expected frequencies should be at least 5. The default degrees of freedom, k-1, are for the case when no parameters of the distribution are estimated. If p parameters are estimated by efficient maximum likelihood then the correct degrees of freedom are k-1-p. If the parameters are estimated in a different way, then the dof can be between k-1-p and k-1. However, it is also possible that the asymptotic distribution is not a chisquare, in which case this test is not appropriate. References ---------- .. [1] Lowry, Richard. "Concepts and Applications of Inferential Statistics". Chapter 8. http://faculty.vassar.edu/lowry/ch8pt1.html .. [2] "Chi-squared test", http://en.wikipedia.org/wiki/Chi-squared_test Examples -------- When just `f_obs` is given, it is assumed that the expected frequencies are uniform and given by the mean of the observed frequencies. >>> from scipy.stats import chisquare >>> chisquare([16, 18, 16, 14, 12, 12]) (2.0, 0.84914503608460956) With `f_exp` the expected frequencies can be given. >>> chisquare([16, 18, 16, 14, 12, 12], f_exp=[16, 16, 16, 16, 16, 8]) (3.5, 0.62338762774958223) When `f_obs` is 2-D, by default the test is applied to each column. >>> obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T >>> obs.shape (6, 2) >>> chisquare(obs) (array([ 2. , 6.66666667]), array([ 0.84914504, 0.24663415])) By setting ``axis=None``, the test is applied to all data in the array, which is equivalent to applying the test to the flattened array. >>> chisquare(obs, axis=None) (23.31034482758621, 0.015975692534127565) >>> chisquare(obs.ravel()) (23.31034482758621, 0.015975692534127565) `ddof` is the change to make to the default degrees of freedom. >>> chisquare([16, 18, 16, 14, 12, 12], ddof=1) (2.0, 0.73575888234288467) The calculation of the p-values is done by broadcasting the chi-squared statistic with `ddof`. >>> chisquare([16, 18, 16, 14, 12, 12], ddof=[0,1,2]) (2.0, array([ 0.84914504, 0.73575888, 0.5724067 ])) `f_obs` and `f_exp` are also broadcast. In the following, `f_obs` has shape (6,) and `f_exp` has shape (2, 6), so the result of broadcasting `f_obs` and `f_exp` has shape (2, 6). To compute the desired chi-squared statistics, we use ``axis=1``: >>> chisquare([16, 18, 16, 14, 12, 12], ... f_exp=[[16, 16, 16, 16, 16, 8], [8, 20, 20, 16, 12, 12]], ... axis=1) (array([ 3.5 , 9.25]), array([ 0.62338763, 0.09949846])) """ return power_divergence(f_obs, f_exp=f_exp, ddof=ddof, axis=axis, lambda_="pearson") Ks_2sampResult = namedtuple('Ks_2sampResult', ('statistic', 'pvalue')) def ks_2samp(data1, data2): """ Computes the Kolmogorov-Smirnov statistic on 2 samples. This is a two-sided test for the null hypothesis that 2 independent samples are drawn from the same continuous distribution. Parameters ---------- data1, data2 : sequence of 1-D ndarrays two arrays of sample observations assumed to be drawn from a continuous distribution, sample sizes can be different Returns ------- statistic : float KS statistic pvalue : float two-tailed p-value Notes ----- This tests whether 2 samples are drawn from the same distribution. Note that, like in the case of the one-sample K-S test, the distribution is assumed to be continuous. This is the two-sided test, one-sided tests are not implemented. The test uses the two-sided asymptotic Kolmogorov-Smirnov distribution. If the K-S statistic is small or the p-value is high, then we cannot reject the hypothesis that the distributions of the two samples are the same. Examples -------- >>> from scipy import stats >>> np.random.seed(12345678) #fix random seed to get the same result >>> n1 = 200 # size of first sample >>> n2 = 300 # size of second sample For a different distribution, we can reject the null hypothesis since the pvalue is below 1%: >>> rvs1 = stats.norm.rvs(size=n1, loc=0., scale=1) >>> rvs2 = stats.norm.rvs(size=n2, loc=0.5, scale=1.5) >>> stats.ks_2samp(rvs1, rvs2) (0.20833333333333337, 4.6674975515806989e-005) For a slightly different distribution, we cannot reject the null hypothesis at a 10% or lower alpha since the p-value at 0.144 is higher than 10% >>> rvs3 = stats.norm.rvs(size=n2, loc=0.01, scale=1.0) >>> stats.ks_2samp(rvs1, rvs3) (0.10333333333333333, 0.14498781825751686) For an identical distribution, we cannot reject the null hypothesis since the p-value is high, 41%: >>> rvs4 = stats.norm.rvs(size=n2, loc=0.0, scale=1.0) >>> stats.ks_2samp(rvs1, rvs4) (0.07999999999999996, 0.41126949729859719) """ data1 = np.sort(data1) data2 = np.sort(data2) n1 = data1.shape[0] n2 = data2.shape[0] data_all = np.concatenate([data1, data2]) cdf1 = np.searchsorted(data1, data_all, side='right') / (1.0*n1) cdf2 = np.searchsorted(data2, data_all, side='right') / (1.0*n2) d = np.max(np.absolute(cdf1 - cdf2)) # Note: d absolute not signed distance en = np.sqrt(n1 * n2 / float(n1 + n2)) try: prob = distributions.kstwobign.sf((en + 0.12 + 0.11 / en) * d) except: prob = 1.0 return Ks_2sampResult(d, prob) def tiecorrect(rankvals): """ Tie correction factor for ties in the Mann-Whitney U and Kruskal-Wallis H tests. Parameters ---------- rankvals : array_like A 1-D sequence of ranks. Typically this will be the array returned by `stats.rankdata`. Returns ------- factor : float Correction factor for U or H. See Also -------- rankdata : Assign ranks to the data mannwhitneyu : Mann-Whitney rank test kruskal : Kruskal-Wallis H test References ---------- .. [1] Siegel, S. (1956) Nonparametric Statistics for the Behavioral Sciences. New York: McGraw-Hill. Examples -------- >>> from scipy.stats import tiecorrect, rankdata >>> tiecorrect([1, 2.5, 2.5, 4]) 0.9 >>> ranks = rankdata([1, 3, 2, 4, 5, 7, 2, 8, 4]) >>> ranks array([ 1. , 4. , 2.5, 5.5, 7. , 8. , 2.5, 9. , 5.5]) >>> tiecorrect(ranks) 0.9833333333333333 """ arr = np.sort(rankvals) idx = np.nonzero(np.r_[True, arr[1:] != arr[:-1], True])[0] cnt = np.diff(idx).astype(np.float64) size = np.float64(arr.size) return 1.0 if size < 2 else 1.0 - (cnt**3 - cnt).sum() / (size**3 - size) MannwhitneyuResult = namedtuple('MannwhitneyuResult', ('statistic', 'pvalue')) def mannwhitneyu(x, y, use_continuity=True, alternative=None): """ Computes the Mann-Whitney rank test on samples x and y. Parameters ---------- x, y : array_like Array of samples, should be one-dimensional. use_continuity : bool, optional Whether a continuity correction (1/2.) should be taken into account. Default is True. alternative : None (deprecated), 'less', 'two-sided', or 'greater' Whether to get the p-value for the one-sided hypothesis ('less' or 'greater') or for the two-sided hypothesis ('two-sided'). Defaults to None, which results in a p-value half the size of the 'two-sided' p-value and a different U statistic. The default behavior is not the same as using 'less' or 'greater': it only exists for backward compatibility and is deprecated. Returns ------- statistic : float The Mann-Whitney U statistic, equal to min(U for x, U for y) if `alternative` is equal to None (deprecated; exists for backward compatibility), and U for y otherwise. pvalue : float p-value assuming an asymptotic normal distribution. One-sided or two-sided, depending on the choice of `alternative`. Notes ----- Use only when the number of observation in each sample is > 20 and you have 2 independent samples of ranks. Mann-Whitney U is significant if the u-obtained is LESS THAN or equal to the critical value of U. This test corrects for ties and by default uses a continuity correction. """ if alternative is None: warnings.warn("Calling `mannwhitneyu` without specifying " "`alternative` is deprecated.", DeprecationWarning) x = np.asarray(x) y = np.asarray(y) n1 = len(x) n2 = len(y) ranked = rankdata(np.concatenate((x, y))) rankx = ranked[0:n1] # get the x-ranks u1 = n1*n2 + (n1*(n1+1))/2.0 - np.sum(rankx, axis=0) # calc U for x u2 = n1*n2 - u1 # remainder is U for y T = tiecorrect(ranked) if T == 0: raise ValueError('All numbers are identical in mannwhitneyu') sd = np.sqrt(T * n1 * n2 * (n1+n2+1) / 12.0) meanrank = n1*n2/2.0 + 0.5 * use_continuity if alternative is None or alternative == 'two-sided': bigu = max(u1, u2) elif alternative == 'less': bigu = u1 elif alternative == 'greater': bigu = u2 else: raise ValueError("alternative should be None, 'less', 'greater' " "or 'two-sided'") z = (bigu - meanrank) / sd if alternative is None: # This behavior, equal to half the size of the two-sided # p-value, is deprecated. p = distributions.norm.sf(abs(z)) elif alternative == 'two-sided': p = 2 * distributions.norm.sf(abs(z)) else: p = distributions.norm.sf(z) u = u2 # This behavior is deprecated. if alternative is None: u = min(u1, u2) return MannwhitneyuResult(u, p) RanksumsResult = namedtuple('RanksumsResult', ('statistic', 'pvalue')) def ranksums(x, y): """ Compute the Wilcoxon rank-sum statistic for two samples. The Wilcoxon rank-sum test tests the null hypothesis that two sets of measurements are drawn from the same distribution. The alternative hypothesis is that values in one sample are more likely to be larger than the values in the other sample. This test should be used to compare two samples from continuous distributions. It does not handle ties between measurements in x and y. For tie-handling and an optional continuity correction see `scipy.stats.mannwhitneyu`. Parameters ---------- x,y : array_like The data from the two samples Returns ------- statistic : float The test statistic under the large-sample approximation that the rank sum statistic is normally distributed pvalue : float The two-sided p-value of the test References ---------- .. [1] http://en.wikipedia.org/wiki/Wilcoxon_rank-sum_test """ x, y = map(np.asarray, (x, y)) n1 = len(x) n2 = len(y) alldata = np.concatenate((x, y)) ranked = rankdata(alldata) x = ranked[:n1] s = np.sum(x, axis=0) expected = n1 * (n1+n2+1) / 2.0 z = (s - expected) / np.sqrt(n1*n2*(n1+n2+1)/12.0) prob = 2 * distributions.norm.sf(abs(z)) return RanksumsResult(z, prob) KruskalResult = namedtuple('KruskalResult', ('statistic', 'pvalue')) def kruskal(*args, **kwargs): """ Compute the Kruskal-Wallis H-test for independent samples The Kruskal-Wallis H-test tests the null hypothesis that the population median of all of the groups are equal. It is a non-parametric version of ANOVA. The test works on 2 or more independent samples, which may have different sizes. Note that rejecting the null hypothesis does not indicate which of the groups differs. Post-hoc comparisons between groups are required to determine which groups are different. Parameters ---------- sample1, sample2, ... : array_like Two or more arrays with the sample measurements can be given as arguments. nan_policy : {'propagate', 'raise', 'omit'}, optional Defines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Returns ------- statistic : float The Kruskal-Wallis H statistic, corrected for ties pvalue : float The p-value for the test using the assumption that H has a chi square distribution See Also -------- f_oneway : 1-way ANOVA mannwhitneyu : Mann-Whitney rank test on two samples. friedmanchisquare : Friedman test for repeated measurements Notes ----- Due to the assumption that H has a chi square distribution, the number of samples in each group must not be too small. A typical rule is that each sample must have at least 5 measurements. References ---------- .. [1] W. H. Kruskal & W. W. Wallis, "Use of Ranks in One-Criterion Variance Analysis", Journal of the American Statistical Association, Vol. 47, Issue 260, pp. 583-621, 1952. .. [2] http://en.wikipedia.org/wiki/Kruskal-Wallis_one-way_analysis_of_variance Examples -------- >>> from scipy import stats >>> x = [1, 3, 5, 7, 9] >>> y = [2, 4, 6, 8, 10] >>> stats.kruskal(x, y) KruskalResult(statistic=0.27272727272727337, pvalue=0.60150813444058948) >>> x = [1, 1, 1] >>> y = [2, 2, 2] >>> z = [2, 2] >>> stats.kruskal(x, y, z) KruskalResult(statistic=7.0, pvalue=0.030197383422318501) """ args = list(map(np.asarray, args)) num_groups = len(args) if num_groups < 2: raise ValueError("Need at least two groups in stats.kruskal()") for arg in args: if arg.size == 0: return KruskalResult(np.nan, np.nan) n = np.asarray(list(map(len, args))) if 'nan_policy' in kwargs.keys(): if kwargs['nan_policy'] not in ('propagate', 'raise', 'omit'): raise ValueError("nan_policy must be 'propagate', " "'raise' or'omit'") else: nan_policy = kwargs['nan_policy'] else: nan_policy = 'propagate' contains_nan = False for arg in args: cn = _contains_nan(arg, nan_policy) if cn[0]: contains_nan = True break if contains_nan and nan_policy == 'omit': for a in args: a = ma.masked_invalid(a) return mstats_basic.kruskal(*args) if contains_nan and nan_policy == 'propagate': return KruskalResult(np.nan, np.nan) alldata = np.concatenate(args) ranked = rankdata(alldata) ties = tiecorrect(ranked) if ties == 0: raise ValueError('All numbers are identical in kruskal') # Compute sum^2/n for each group and sum j = np.insert(np.cumsum(n), 0, 0) ssbn = 0 for i in range(num_groups): ssbn += _square_of_sums(ranked[j[i]:j[i+1]]) / float(n[i]) totaln = np.sum(n) h = 12.0 / (totaln * (totaln + 1)) * ssbn - 3 * (totaln + 1) df = num_groups - 1 h /= ties return KruskalResult(h, distributions.chi2.sf(h, df)) FriedmanchisquareResult = namedtuple('FriedmanchisquareResult', ('statistic', 'pvalue')) def friedmanchisquare(*args): """ Computes the Friedman test for repeated measurements The Friedman test tests the null hypothesis that repeated measurements of the same individuals have the same distribution. It is often used to test for consistency among measurements obtained in different ways. For example, if two measurement techniques are used on the same set of individuals, the Friedman test can be used to determine if the two measurement techniques are consistent. Parameters ---------- measurements1, measurements2, measurements3... : array_like Arrays of measurements. All of the arrays must have the same number of elements. At least 3 sets of measurements must be given. Returns ------- statistic : float the test statistic, correcting for ties pvalue : float the associated p-value assuming that the test statistic has a chi squared distribution Notes ----- Due to the assumption that the test statistic has a chi squared distribution, the p-value is only reliable for n > 10 and more than 6 repeated measurements. References ---------- .. [1] http://en.wikipedia.org/wiki/Friedman_test """ k = len(args) if k < 3: raise ValueError('Less than 3 levels. Friedman test not appropriate.') n = len(args[0]) for i in range(1, k): if len(args[i]) != n: raise ValueError('Unequal N in friedmanchisquare. Aborting.') # Rank data data = np.vstack(args).T data = data.astype(float) for i in range(len(data)): data[i] = rankdata(data[i]) # Handle ties ties = 0 for i in range(len(data)): replist, repnum = find_repeats(array(data[i])) for t in repnum: ties += t * (t*t - 1) c = 1 - ties / float(k*(k*k - 1)*n) ssbn = np.sum(data.sum(axis=0)**2) chisq = (12.0 / (k*n*(k+1)) * ssbn - 3*n*(k+1)) / c return FriedmanchisquareResult(chisq, distributions.chi2.sf(chisq, k - 1)) def combine_pvalues(pvalues, method='fisher', weights=None): """ Methods for combining the p-values of independent tests bearing upon the same hypothesis. Parameters ---------- pvalues : array_like, 1-D Array of p-values assumed to come from independent tests. method : {'fisher', 'stouffer'}, optional Name of method to use to combine p-values. The following methods are available: - "fisher": Fisher's method (Fisher's combined probability test), the default. - "stouffer": Stouffer's Z-score method. weights : array_like, 1-D, optional Optional array of weights used only for Stouffer's Z-score method. Returns ------- statistic: float The statistic calculated by the specified method: - "fisher": The chi-squared statistic - "stouffer": The Z-score pval: float The combined p-value. Notes ----- Fisher's method (also known as Fisher's combined probability test) [1]_ uses a chi-squared statistic to compute a combined p-value. The closely related Stouffer's Z-score method [2]_ uses Z-scores rather than p-values. The advantage of Stouffer's method is that it is straightforward to introduce weights, which can make Stouffer's method more powerful than Fisher's method when the p-values are from studies of different size [3]_ [4]_. Fisher's method may be extended to combine p-values from dependent tests [5]_. Extensions such as Brown's method and Kost's method are not currently implemented. .. versionadded:: 0.15.0 References ---------- .. [1] https://en.wikipedia.org/wiki/Fisher%27s_method .. [2] http://en.wikipedia.org/wiki/Fisher's_method#Relation_to_Stouffer.27s_Z-score_method .. [3] Whitlock, M. C. "Combining probability from independent tests: the weighted Z-method is superior to Fisher's approach." Journal of Evolutionary Biology 18, no. 5 (2005): 1368-1373. .. [4] Zaykin, Dmitri V. "Optimally weighted Z-test is a powerful method for combining probabilities in meta-analysis." Journal of Evolutionary Biology 24, no. 8 (2011): 1836-1841. .. [5] https://en.wikipedia.org/wiki/Extensions_of_Fisher%27s_method """ pvalues = np.asarray(pvalues) if pvalues.ndim != 1: raise ValueError("pvalues is not 1-D") if method == 'fisher': Xsq = -2 * np.sum(np.log(pvalues)) pval = distributions.chi2.sf(Xsq, 2 * len(pvalues)) return (Xsq, pval) elif method == 'stouffer': if weights is None: weights = np.ones_like(pvalues) elif len(weights) != len(pvalues): raise ValueError("pvalues and weights must be of the same size.") weights = np.asarray(weights) if weights.ndim != 1: raise ValueError("weights is not 1-D") Zi = distributions.norm.isf(pvalues) Z = np.dot(weights, Zi) / np.linalg.norm(weights) pval = distributions.norm.sf(Z) return (Z, pval) else: raise ValueError( "Invalid method '%s'. Options are 'fisher' or 'stouffer'", method) ##################################### # PROBABILITY CALCULATIONS # ##################################### @np.deprecate(message="stats.chisqprob is deprecated in scipy 0.17.0; " "use stats.distributions.chi2.sf instead.") def chisqprob(chisq, df): """ Probability value (1-tail) for the Chi^2 probability distribution. Broadcasting rules apply. Parameters ---------- chisq : array_like or float > 0 df : array_like or float, probably int >= 1 Returns ------- chisqprob : ndarray The area from `chisq` to infinity under the Chi^2 probability distribution with degrees of freedom `df`. """ return distributions.chi2.sf(chisq, df) @np.deprecate(message="stats.betai is deprecated in scipy 0.17.0; " "use special.betainc instead") def betai(a, b, x): """ Returns the incomplete beta function. I_x(a,b) = 1/B(a,b)*(Integral(0,x) of t^(a-1)(1-t)^(b-1) dt) where a,b>0 and B(a,b) = G(a)*G(b)/(G(a+b)) where G(a) is the gamma function of a. The standard broadcasting rules apply to a, b, and x. Parameters ---------- a : array_like or float > 0 b : array_like or float > 0 x : array_like or float x will be clipped to be no greater than 1.0 . Returns ------- betai : ndarray Incomplete beta function. """ return _betai(a, b, x) def _betai(a, b, x): x = np.asarray(x) x = np.where(x < 1.0, x, 1.0) # if x > 1 then return 1.0 return special.betainc(a, b, x) ##################################### # ANOVA CALCULATIONS # ##################################### @np.deprecate(message="stats.f_value_wilks_lambda deprecated in scipy 0.17.0") def f_value_wilks_lambda(ER, EF, dfnum, dfden, a, b): """Calculation of Wilks lambda F-statistic for multivarite data, per Maxwell & Delaney p.657. """ if isinstance(ER, (int, float)): ER = array([[ER]]) if isinstance(EF, (int, float)): EF = array([[EF]]) lmbda = linalg.det(EF) / linalg.det(ER) if (a-1)**2 + (b-1)**2 == 5: q = 1 else: q = np.sqrt(((a-1)**2*(b-1)**2 - 2) / ((a-1)**2 + (b-1)**2 - 5)) n_um = (1 - lmbda**(1.0/q))*(a-1)*(b-1) d_en = lmbda**(1.0/q) / (n_um*q - 0.5*(a-1)*(b-1) + 1) return n_um / d_en @np.deprecate(message="stats.f_value deprecated in scipy 0.17.0") def f_value(ER, EF, dfR, dfF): """ Returns an F-statistic for a restricted vs. unrestricted model. Parameters ---------- ER : float `ER` is the sum of squared residuals for the restricted model or null hypothesis EF : float `EF` is the sum of squared residuals for the unrestricted model or alternate hypothesis dfR : int `dfR` is the degrees of freedom in the restricted model dfF : int `dfF` is the degrees of freedom in the unrestricted model Returns ------- F-statistic : float """ return (ER - EF) / float(dfR - dfF) / (EF / float(dfF)) @np.deprecate(message="stats.f_value_multivariate deprecated in scipy 0.17.0") def f_value_multivariate(ER, EF, dfnum, dfden): """ Returns a multivariate F-statistic. Parameters ---------- ER : ndarray Error associated with the null hypothesis (the Restricted model). From a multivariate F calculation. EF : ndarray Error associated with the alternate hypothesis (the Full model) From a multivariate F calculation. dfnum : int Degrees of freedom the Restricted model. dfden : int Degrees of freedom associated with the Restricted model. Returns ------- fstat : float The computed F-statistic. """ if isinstance(ER, (int, float)): ER = array([[ER]]) if isinstance(EF, (int, float)): EF = array([[EF]]) n_um = (linalg.det(ER) - linalg.det(EF)) / float(dfnum) d_en = linalg.det(EF) / float(dfden) return n_um / d_en ##################################### # SUPPORT FUNCTIONS # ##################################### RepeatedResults = namedtuple('RepeatedResults', ('values', 'counts')) def find_repeats(arr): """ Find repeats and repeat counts. Parameters ---------- arr : array_like Input array. This is cast to float64. Returns ------- values : ndarray The unique values from the (flattened) input that are repeated. counts : ndarray Number of times the corresponding 'value' is repeated. Notes ----- In numpy >= 1.9 `numpy.unique` provides similar functionality. The main difference is that `find_repeats` only returns repeated values. Examples -------- >>> from scipy import stats >>> stats.find_repeats([2, 1, 2, 3, 2, 2, 5]) RepeatedResults(values=array([ 2.]), counts=array([4])) >>> stats.find_repeats([[10, 20, 1, 2], [5, 5, 4, 4]]) RepeatedResults(values=array([ 4., 5.]), counts=array([2, 2])) """ # Note: always copies. return RepeatedResults(*_find_repeats(np.array(arr, dtype=np.float64))) @np.deprecate(message="scipy.stats.ss is deprecated in scipy 0.17.0") def ss(a, axis=0): return _sum_of_squares(a, axis) def _sum_of_squares(a, axis=0): """ Squares each element of the input array, and returns the sum(s) of that. Parameters ---------- a : array_like Input array. axis : int or None, optional Axis along which to calculate. Default is 0. If None, compute over the whole array `a`. Returns ------- sum_of_squares : ndarray The sum along the given axis for (a**2). See also -------- _square_of_sums : The square(s) of the sum(s) (the opposite of `_sum_of_squares`). """ a, axis = _chk_asarray(a, axis) return np.sum(a*a, axis) @np.deprecate(message="scipy.stats.square_of_sums is deprecated " "in scipy 0.17.0") def square_of_sums(a, axis=0): return _square_of_sums(a, axis) def _square_of_sums(a, axis=0): """ Sums elements of the input array, and returns the square(s) of that sum. Parameters ---------- a : array_like Input array. axis : int or None, optional Axis along which to calculate. Default is 0. If None, compute over the whole array `a`. Returns ------- square_of_sums : float or ndarray The square of the sum over `axis`. See also -------- _sum_of_squares : The sum of squares (the opposite of `square_of_sums`). """ a, axis = _chk_asarray(a, axis) s = np.sum(a, axis) if not np.isscalar(s): return s.astype(float) * s else: return float(s) * s @np.deprecate(message="scipy.stats.fastsort is deprecated in scipy 0.16.0") def fastsort(a): """ Sort an array and provide the argsort. Parameters ---------- a : array_like Input array. Returns ------- fastsort : ndarray of type int sorted indices into the original array """ # TODO: the wording in the docstring is nonsense. it = np.argsort(a) as_ = a[it] return as_, it def rankdata(a, method='average'): """ rankdata(a, method='average') Assign ranks to data, dealing with ties appropriately. Ranks begin at 1. The `method` argument controls how ranks are assigned to equal values. See [1]_ for further discussion of ranking methods. Parameters ---------- a : array_like The array of values to be ranked. The array is first flattened. method : str, optional The method used to assign ranks to tied elements. The options are 'average', 'min', 'max', 'dense' and 'ordinal'. 'average': The average of the ranks that would have been assigned to all the tied values is assigned to each value. 'min': The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as "competition" ranking.) 'max': The maximum of the ranks that would have been assigned to all the tied values is assigned to each value. 'dense': Like 'min', but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements. 'ordinal': All values are given a distinct rank, corresponding to the order that the values occur in `a`. The default is 'average'. Returns ------- ranks : ndarray An array of length equal to the size of `a`, containing rank scores. References ---------- .. [1] "Ranking", http://en.wikipedia.org/wiki/Ranking Examples -------- >>> from scipy.stats import rankdata >>> rankdata([0, 2, 3, 2]) array([ 1. , 2.5, 4. , 2.5]) >>> rankdata([0, 2, 3, 2], method='min') array([ 1, 2, 4, 2]) >>> rankdata([0, 2, 3, 2], method='max') array([ 1, 3, 4, 3]) >>> rankdata([0, 2, 3, 2], method='dense') array([ 1, 2, 3, 2]) >>> rankdata([0, 2, 3, 2], method='ordinal') array([ 1, 2, 4, 3]) """ if method not in ('average', 'min', 'max', 'dense', 'ordinal'): raise ValueError('unknown method "{0}"'.format(method)) arr = np.ravel(np.asarray(a)) algo = 'mergesort' if method == 'ordinal' else 'quicksort' sorter = np.argsort(arr, kind=algo) inv = np.empty(sorter.size, dtype=np.intp) inv[sorter] = np.arange(sorter.size, dtype=np.intp) if method == 'ordinal': return inv + 1 arr = arr[sorter] obs = np.r_[True, arr[1:] != arr[:-1]] dense = obs.cumsum()[inv] if method == 'dense': return dense # cumulative counts of each unique value count = np.r_[np.nonzero(obs)[0], len(obs)] if method == 'max': return count[dense] if method == 'min': return count[dense - 1] + 1 # average method return .5 * (count[dense] + count[dense - 1] + 1)
mit
hainm/scikit-learn
sklearn/svm/setup.py
321
3157
import os from os.path import join import numpy from sklearn._build_utils import get_blas_info def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('svm', parent_package, top_path) config.add_subpackage('tests') # Section LibSVM # we compile both libsvm and libsvm_sparse config.add_library('libsvm-skl', sources=[join('src', 'libsvm', 'libsvm_template.cpp')], depends=[join('src', 'libsvm', 'svm.cpp'), join('src', 'libsvm', 'svm.h')], # Force C++ linking in case gcc is picked up instead # of g++ under windows with some versions of MinGW extra_link_args=['-lstdc++'], ) libsvm_sources = ['libsvm.c'] libsvm_depends = [join('src', 'libsvm', 'libsvm_helper.c'), join('src', 'libsvm', 'libsvm_template.cpp'), join('src', 'libsvm', 'svm.cpp'), join('src', 'libsvm', 'svm.h')] config.add_extension('libsvm', sources=libsvm_sources, include_dirs=[numpy.get_include(), join('src', 'libsvm')], libraries=['libsvm-skl'], depends=libsvm_depends, ) ### liblinear module cblas_libs, blas_info = get_blas_info() if os.name == 'posix': cblas_libs.append('m') liblinear_sources = ['liblinear.c', join('src', 'liblinear', '*.cpp')] liblinear_depends = [join('src', 'liblinear', '*.h'), join('src', 'liblinear', 'liblinear_helper.c')] config.add_extension('liblinear', sources=liblinear_sources, libraries=cblas_libs, include_dirs=[join('..', 'src', 'cblas'), numpy.get_include(), blas_info.pop('include_dirs', [])], extra_compile_args=blas_info.pop('extra_compile_args', []), depends=liblinear_depends, # extra_compile_args=['-O0 -fno-inline'], ** blas_info) ## end liblinear module # this should go *after* libsvm-skl libsvm_sparse_sources = ['libsvm_sparse.c'] config.add_extension('libsvm_sparse', libraries=['libsvm-skl'], sources=libsvm_sparse_sources, include_dirs=[numpy.get_include(), join("src", "libsvm")], depends=[join("src", "libsvm", "svm.h"), join("src", "libsvm", "libsvm_sparse_helper.c")]) return config if __name__ == '__main__': from numpy.distutils.core import setup setup(**configuration(top_path='').todict())
bsd-3-clause
rahul-c1/scikit-learn
sklearn/utils/extmath.py
14
20521
""" Extended math utilities. """ # Authors: Gael Varoquaux # Alexandre Gramfort # Alexandre T. Passos # Olivier Grisel # Lars Buitinck # Stefan van der Walt # Kyle Kastner # License: BSD 3 clause from __future__ import division from functools import partial import warnings import numpy as np from scipy import linalg from scipy.sparse import issparse from . import check_random_state, deprecated from .fixes import np_version from ._logistic_sigmoid import _log_logistic_sigmoid from ..externals.six.moves import xrange from .sparsefuncs_fast import csr_row_norms from .validation import check_array, NonBLASDotWarning def norm(x): """Compute the Euclidean or Frobenius norm of x. Returns the Euclidean norm when x is a vector, the Frobenius norm when x is a matrix (2-d array). More precise than sqrt(squared_norm(x)). """ x = np.asarray(x) nrm2, = linalg.get_blas_funcs(['nrm2'], [x]) return nrm2(x) # Newer NumPy has a ravel that needs less copying. if np_version < (1, 7, 1): _ravel = np.ravel else: _ravel = partial(np.ravel, order='K') def squared_norm(x): """Squared Euclidean or Frobenius norm of x. Returns the Euclidean norm when x is a vector, the Frobenius norm when x is a matrix (2-d array). Faster than norm(x) ** 2. """ x = _ravel(x) return np.dot(x, x) def row_norms(X, squared=False): """Row-wise (squared) Euclidean norm of X. Equivalent to np.sqrt((X * X).sum(axis=1)), but also supports CSR sparse matrices and does not create an X.shape-sized temporary. Performs no input validation. """ if issparse(X): norms = csr_row_norms(X) else: norms = np.einsum('ij,ij->i', X, X) if not squared: np.sqrt(norms, norms) return norms def fast_logdet(A): """Compute log(det(A)) for A symmetric Equivalent to : np.log(nl.det(A)) but more robust. It returns -Inf if det(A) is non positive or is not defined. """ sign, ld = np.linalg.slogdet(A) if not sign > 0: return -np.inf return ld def _impose_f_order(X): """Helper Function""" # important to access flags instead of calling np.isfortran, # this catches corner cases. if X.flags.c_contiguous: return check_array(X.T, copy=False, order='F'), True else: return check_array(X, copy=False, order='F'), False def _fast_dot(A, B): if B.shape[0] != A.shape[A.ndim - 1]: # check adopted from '_dotblas.c' raise ValueError if A.dtype != B.dtype or any(x.dtype not in (np.float32, np.float64) for x in [A, B]): warnings.warn('Data must be of same type. Supported types ' 'are 32 and 64 bit float. ' 'Falling back to np.dot.', NonBLASDotWarning) raise ValueError if min(A.shape) == 1 or min(B.shape) == 1 or A.ndim != 2 or B.ndim != 2: raise ValueError # scipy 0.9 compliant API dot = linalg.get_blas_funcs(['gemm'], (A, B))[0] A, trans_a = _impose_f_order(A) B, trans_b = _impose_f_order(B) return dot(alpha=1.0, a=A, b=B, trans_a=trans_a, trans_b=trans_b) def _have_blas_gemm(): try: linalg.get_blas_funcs(['gemm']) return True except (AttributeError, ValueError): warnings.warn('Could not import BLAS, falling back to np.dot') return False # Only use fast_dot for older NumPy; newer ones have tackled the speed issue. if np_version < (1, 7, 2) and _have_blas_gemm(): def fast_dot(A, B): """Compute fast dot products directly calling BLAS. This function calls BLAS directly while warranting Fortran contiguity. This helps avoiding extra copies `np.dot` would have created. For details see section `Linear Algebra on large Arrays`: http://wiki.scipy.org/PerformanceTips Parameters ---------- A, B: instance of np.ndarray Input arrays. Arrays are supposed to be of the same dtype and to have exactly 2 dimensions. Currently only floats are supported. In case these requirements aren't met np.dot(A, B) is returned instead. To activate the related warning issued in this case execute the following lines of code: >> import warnings >> from sklearn.utils.validation import NonBLASDotWarning >> warnings.simplefilter('always', NonBLASDotWarning) """ try: return _fast_dot(A, B) except ValueError: # Maltyped or malformed data. return np.dot(A, B) else: fast_dot = np.dot def density(w, **kwargs): """Compute density of a sparse vector Return a value between 0 and 1 """ if hasattr(w, "toarray"): d = float(w.nnz) / (w.shape[0] * w.shape[1]) else: d = 0 if w is None else float((w != 0).sum()) / w.size return d def safe_sparse_dot(a, b, dense_output=False): """Dot product that handle the sparse matrix case correctly Uses BLAS GEMM as replacement for numpy.dot where possible to avoid unnecessary copies. """ if issparse(a) or issparse(b): ret = a * b if dense_output and hasattr(ret, "toarray"): ret = ret.toarray() return ret else: return fast_dot(a, b) def randomized_range_finder(A, size, n_iter, random_state=None): """Computes an orthonormal matrix whose range approximates the range of A. Parameters ---------- A: 2D array The input data matrix size: integer Size of the return array n_iter: integer Number of power iterations used to stabilize the result random_state: RandomState or an int seed (0 by default) A random number generator instance Returns ------- Q: 2D array A (size x size) projection matrix, the range of which approximates well the range of the input matrix A. Notes ----- Follows Algorithm 4.3 of Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 (arXiv:909) http://arxiv.org/pdf/0909.4061 """ random_state = check_random_state(random_state) # generating random gaussian vectors r with shape: (A.shape[1], size) R = random_state.normal(size=(A.shape[1], size)) # sampling the range of A using by linear projection of r Y = safe_sparse_dot(A, R) del R # perform power iterations with Y to further 'imprint' the top # singular vectors of A in Y for i in xrange(n_iter): Y = safe_sparse_dot(A, safe_sparse_dot(A.T, Y)) # extracting an orthonormal basis of the A range samples Q, R = linalg.qr(Y, mode='economic') return Q def randomized_svd(M, n_components, n_oversamples=10, n_iter=0, transpose='auto', flip_sign=True, random_state=0): """Computes a truncated randomized SVD Parameters ---------- M: ndarray or sparse matrix Matrix to decompose n_components: int Number of singular values and vectors to extract. n_oversamples: int (default is 10) Additional number of random vectors to sample the range of M so as to ensure proper conditioning. The total number of random vectors used to find the range of M is n_components + n_oversamples. n_iter: int (default is 0) Number of power iterations (can be used to deal with very noisy problems). transpose: True, False or 'auto' (default) Whether the algorithm should be applied to M.T instead of M. The result should approximately be the same. The 'auto' mode will trigger the transposition if M.shape[1] > M.shape[0] since this implementation of randomized SVD tend to be a little faster in that case). flip_sign: boolean, (True by default) The output of a singular value decomposition is only unique up to a permutation of the signs of the singular vectors. If `flip_sign` is set to `True`, the sign ambiguity is resolved by making the largest loadings for each component in the left singular vectors positive. random_state: RandomState or an int seed (0 by default) A random number generator instance to make behavior Notes ----- This algorithm finds a (usually very good) approximate truncated singular value decomposition using randomization to speed up the computations. It is particularly fast on large matrices on which you wish to extract only a small number of components. References ---------- * Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 http://arxiv.org/abs/arXiv:0909.4061 * A randomized algorithm for the decomposition of matrices Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert """ random_state = check_random_state(random_state) n_random = n_components + n_oversamples n_samples, n_features = M.shape if transpose == 'auto' and n_samples > n_features: transpose = True if transpose: # this implementation is a bit faster with smaller shape[1] M = M.T Q = randomized_range_finder(M, n_random, n_iter, random_state) # project M to the (k + p) dimensional space using the basis vectors B = safe_sparse_dot(Q.T, M) # compute the SVD on the thin matrix: (k + p) wide Uhat, s, V = linalg.svd(B, full_matrices=False) del B U = np.dot(Q, Uhat) if flip_sign: U, V = svd_flip(U, V) if transpose: # transpose back the results according to the input convention return V[:n_components, :].T, s[:n_components], U[:, :n_components].T else: return U[:, :n_components], s[:n_components], V[:n_components, :] def logsumexp(arr, axis=0): """Computes the sum of arr assuming arr is in the log domain. Returns log(sum(exp(arr))) while minimizing the possibility of over/underflow. Examples -------- >>> import numpy as np >>> from sklearn.utils.extmath import logsumexp >>> a = np.arange(10) >>> np.log(np.sum(np.exp(a))) 9.4586297444267107 >>> logsumexp(a) 9.4586297444267107 """ arr = np.rollaxis(arr, axis) # Use the max to normalize, as with the log this is what accumulates # the less errors vmax = arr.max(axis=0) out = np.log(np.sum(np.exp(arr - vmax), axis=0)) out += vmax return out def weighted_mode(a, w, axis=0): """Returns an array of the weighted modal (most common) value in a If there is more than one such value, only the first is returned. The bin-count for the modal bins is also returned. This is an extension of the algorithm in scipy.stats.mode. Parameters ---------- a : array_like n-dimensional array of which to find mode(s). w : array_like n-dimensional array of weights for each value axis : int, optional Axis along which to operate. Default is 0, i.e. the first axis. Returns ------- vals : ndarray Array of modal values. score : ndarray Array of weighted counts for each mode. Examples -------- >>> from sklearn.utils.extmath import weighted_mode >>> x = [4, 1, 4, 2, 4, 2] >>> weights = [1, 1, 1, 1, 1, 1] >>> weighted_mode(x, weights) (array([ 4.]), array([ 3.])) The value 4 appears three times: with uniform weights, the result is simply the mode of the distribution. >>> weights = [1, 3, 0.5, 1.5, 1, 2] # deweight the 4's >>> weighted_mode(x, weights) (array([ 2.]), array([ 3.5])) The value 2 has the highest score: it appears twice with weights of 1.5 and 2: the sum of these is 3. See Also -------- scipy.stats.mode """ if axis is None: a = np.ravel(a) w = np.ravel(w) axis = 0 else: a = np.asarray(a) w = np.asarray(w) axis = axis if a.shape != w.shape: w = np.zeros(a.shape, dtype=w.dtype) + w scores = np.unique(np.ravel(a)) # get ALL unique values testshape = list(a.shape) testshape[axis] = 1 oldmostfreq = np.zeros(testshape) oldcounts = np.zeros(testshape) for score in scores: template = np.zeros(a.shape) ind = (a == score) template[ind] = w[ind] counts = np.expand_dims(np.sum(template, axis), axis) mostfrequent = np.where(counts > oldcounts, score, oldmostfreq) oldcounts = np.maximum(counts, oldcounts) oldmostfreq = mostfrequent return mostfrequent, oldcounts def pinvh(a, cond=None, rcond=None, lower=True): """Compute the (Moore-Penrose) pseudo-inverse of a hermetian matrix. Calculate a generalized inverse of a symmetric matrix using its eigenvalue decomposition and including all 'large' eigenvalues. Parameters ---------- a : array, shape (N, N) Real symmetric or complex hermetian matrix to be pseudo-inverted cond, rcond : float or None Cutoff for 'small' eigenvalues. Singular values smaller than rcond * largest_eigenvalue are considered zero. If None or -1, suitable machine precision is used. lower : boolean Whether the pertinent array data is taken from the lower or upper triangle of a. (Default: lower) Returns ------- B : array, shape (N, N) Raises ------ LinAlgError If eigenvalue does not converge Examples -------- >>> import numpy as np >>> a = np.random.randn(9, 6) >>> a = np.dot(a, a.T) >>> B = pinvh(a) >>> np.allclose(a, np.dot(a, np.dot(B, a))) True >>> np.allclose(B, np.dot(B, np.dot(a, B))) True """ a = np.asarray_chkfinite(a) s, u = linalg.eigh(a, lower=lower) if rcond is not None: cond = rcond if cond in [None, -1]: t = u.dtype.char.lower() factor = {'f': 1E3, 'd': 1E6} cond = factor[t] * np.finfo(t).eps # unlike svd case, eigh can lead to negative eigenvalues above_cutoff = (abs(s) > cond * np.max(abs(s))) psigma_diag = np.zeros_like(s) psigma_diag[above_cutoff] = 1.0 / s[above_cutoff] return np.dot(u * psigma_diag, np.conjugate(u).T) def cartesian(arrays, out=None): """Generate a cartesian product of input arrays. Parameters ---------- arrays : list of array-like 1-D arrays to form the cartesian product of. out : ndarray Array to place the cartesian product in. Returns ------- out : ndarray 2-D array of shape (M, len(arrays)) containing cartesian products formed of input arrays. Examples -------- >>> cartesian(([1, 2, 3], [4, 5], [6, 7])) array([[1, 4, 6], [1, 4, 7], [1, 5, 6], [1, 5, 7], [2, 4, 6], [2, 4, 7], [2, 5, 6], [2, 5, 7], [3, 4, 6], [3, 4, 7], [3, 5, 6], [3, 5, 7]]) """ arrays = [np.asarray(x) for x in arrays] shape = (len(x) for x in arrays) dtype = arrays[0].dtype ix = np.indices(shape) ix = ix.reshape(len(arrays), -1).T if out is None: out = np.empty_like(ix, dtype=dtype) for n, arr in enumerate(arrays): out[:, n] = arrays[n][ix[:, n]] return out def svd_flip(u, v, u_based_decision=True): """Sign correction to ensure deterministic output from SVD. Adjusts the columns of u and the rows of v such that the loadings in the columns in u that are largest in absolute value are always positive. Parameters ---------- u, v : arrays The output of `linalg.svd` or `sklearn.utils.extmath.randomized_svd`, with matching inner dimensions so one can compute `np.dot(u * s, v)`. u_based_decision : boolean, (default=True) If True, use the columns of u as the basis for sign flipping. Otherwise, use the rows of v. The choice of which variable to base the decision on is generally algorithm dependent. Returns ------- u_adjusted, v_adjusted : arrays with the same dimensions as the input. """ if u_based_decision: # columns of u, rows of v max_abs_cols = np.argmax(np.abs(u), axis=0) signs = np.sign(u[max_abs_cols, xrange(u.shape[1])]) u *= signs v *= signs[:, np.newaxis] else: # rows of v, columns of u max_abs_rows = np.argmax(np.abs(v), axis=1) signs = np.sign(v[xrange(v.shape[0]), max_abs_rows]) u *= signs v *= signs[:, np.newaxis] return u, v @deprecated('to be removed in 0.17; use scipy.special.expit or log_logistic') def logistic_sigmoid(X, log=False, out=None): """Logistic function, ``1 / (1 + e ** (-x))``, or its log.""" from .fixes import expit fn = log_logistic if log else expit return fn(X, out) def log_logistic(X, out=None): """Compute the log of the logistic function, ``log(1 / (1 + e ** -x))``. This implementation is numerically stable because it splits positive and negative values:: -log(1 + exp(-x_i)) if x_i > 0 x_i - log(1 + exp(x_i)) if x_i <= 0 For the ordinary logistic function, use ``sklearn.utils.fixes.expit``. Parameters ---------- X: array-like, shape (M, N) Argument to the logistic function out: array-like, shape: (M, N), optional: Preallocated output array. Returns ------- out: array, shape (M, N) Log of the logistic function evaluated at every point in x Notes ----- See the blog post describing this implementation: http://fa.bianp.net/blog/2013/numerical-optimizers-for-logistic-regression/ """ is_1d = X.ndim == 1 X = check_array(X, dtype=np.float) n_samples, n_features = X.shape if out is None: out = np.empty_like(X) _log_logistic_sigmoid(n_samples, n_features, X, out) if is_1d: return np.squeeze(out) return out def safe_min(X): """Returns the minimum value of a dense or a CSR/CSC matrix. Adapated from http://stackoverflow.com/q/13426580 """ if issparse(X): if len(X.data) == 0: return 0 m = X.data.min() return m if X.getnnz() == X.size else min(m, 0) else: return X.min() def make_nonnegative(X, min_value=0): """Ensure `X.min()` >= `min_value`.""" min_ = safe_min(X) if min_ < min_value: if issparse(X): raise ValueError("Cannot make the data matrix" " nonnegative because it is sparse." " Adding a value to every entry would" " make it no longer sparse.") X = X + (min_value - min_) return X def _batch_mean_variance_update(X, old_mean, old_variance, old_sample_count): """Calculate an average mean update and a Youngs and Cramer variance update. From the paper "Algorithms for computing the sample variance: analysis and recommendations", by Chan, Golub, and LeVeque. Parameters ---------- X : array-like, shape (n_samples, n_features) Data to use for variance update old_mean : array-like, shape: (n_features,) old_variance : array-like, shape: (n_features,) old_sample_count : int Returns ------- updated_mean : array, shape (n_features,) updated_variance : array, shape (n_features,) updated_sample_count : int References ---------- T. Chan, G. Golub, R. LeVeque. Algorithms for computing the sample variance: recommendations, The American Statistician, Vol. 37, No. 3, pp. 242-247 """ new_sum = X.sum(axis=0) new_variance = X.var(axis=0) * X.shape[0] old_sum = old_mean * old_sample_count n_samples = X.shape[0] updated_sample_count = old_sample_count + n_samples partial_variance = old_sample_count / (n_samples * updated_sample_count) * ( n_samples / old_sample_count * old_sum - new_sum) ** 2 unnormalized_variance = old_variance * old_sample_count + new_variance + \ partial_variance return ((old_sum + new_sum) / updated_sample_count, unnormalized_variance / updated_sample_count, updated_sample_count)
bsd-3-clause
PanDAWMS/panda-server
pandaserver/test/testG4sim.py
1
2574
import sys import time import uuid import pandaserver.userinterface.Client as Client from pandaserver.taskbuffer.JobSpec import JobSpec from pandaserver.taskbuffer.FileSpec import FileSpec if len(sys.argv)>1: site = sys.argv[1] else: site = None datasetName = 'panda.destDB.%s' % str(uuid.uuid4()) destName = 'BNL_ATLAS_2' #destName = 'BU_ATLAS_Tier2' files = { 'mc11.007204.singlepart_mu4.evgen.EVNT.v11000302._00037.pool.root.1':None, 'mc11.007204.singlepart_mu4.evgen.EVNT.v11000302._00038.pool.root.1':None, } jobList = [] for lfn in files: job = JobSpec() job.jobDefinitionID = int(time.time()) % 10000 job.jobName = str(uuid.uuid4()) job.AtlasRelease = 'Atlas-11.0.3' job.homepackage = 'JobTransforms-11-00-03-02' job.transformation = 'share/csc.simul.trf' job.destinationDBlock = datasetName job.destinationSE = destName job.computingSite = site job.prodDBlock = 'mc11.007204.singlepart_mu4.evgen.EVNT.v11000302' job.cmtConfig = 'i686-slc4-gcc34-opt' job.prodSourceLabel = 'test' job.currentPriority = 1000 fileI = FileSpec() fileI.dataset = job.prodDBlock fileI.prodDBlock = job.prodDBlock fileI.lfn = lfn fileI.type = 'input' job.addFile(fileI) fileOE = FileSpec() fileOE.lfn = "%s.HITS.pool.root" % str(uuid.uuid4()) fileOE.destinationDBlock = job.destinationDBlock fileOE.destinationSE = job.destinationSE fileOE.dataset = job.destinationDBlock fileOE.destinationDBlockToken = 'ATLASDATADISK' fileOE.type = 'output' job.addFile(fileOE) fileOA = FileSpec() fileOA.lfn = "%s.RDO.pool.root" % str(uuid.uuid4()) fileOA.destinationDBlock = job.destinationDBlock fileOA.destinationSE = job.destinationSE fileOA.dataset = job.destinationDBlock fileOA.destinationDBlockToken = 'ATLASDATADISK' fileOA.type = 'output' job.addFile(fileOA) fileOL = FileSpec() fileOL.lfn = "%s.job.log.tgz" % str(uuid.uuid4()) fileOL.destinationDBlock = job.destinationDBlock fileOL.destinationSE = job.destinationSE fileOL.dataset = job.destinationDBlock fileOL.destinationDBlockToken = 'ATLASDATADISK' fileOL.type = 'log' job.addFile(fileOL) job.jobParameters="%s %s %s 100 700 2158" % (fileI.lfn,fileOE.lfn,fileOA.lfn) jobList.append(job) s,o = Client.submitJobs(jobList) print("---------------------") print(s) for x in o: print("PandaID=%s" % x[0])
apache-2.0
RayMick/scikit-learn
examples/model_selection/plot_precision_recall.py
249
6150
""" ================ Precision-Recall ================ Example of Precision-Recall metric to evaluate classifier output quality. In information retrieval, precision is a measure of result relevancy, while recall is a measure of how many truly relevant results are returned. A high area under the curve represents both high recall and high precision, where high precision relates to a low false positive rate, and high recall relates to a low false negative rate. High scores for both show that the classifier is returning accurate results (high precision), as well as returning a majority of all positive results (high recall). A system with high recall but low precision returns many results, but most of its predicted labels are incorrect when compared to the training labels. A system with high precision but low recall is just the opposite, returning very few results, but most of its predicted labels are correct when compared to the training labels. An ideal system with high precision and high recall will return many results, with all results labeled correctly. Precision (:math:`P`) is defined as the number of true positives (:math:`T_p`) over the number of true positives plus the number of false positives (:math:`F_p`). :math:`P = \\frac{T_p}{T_p+F_p}` Recall (:math:`R`) is defined as the number of true positives (:math:`T_p`) over the number of true positives plus the number of false negatives (:math:`F_n`). :math:`R = \\frac{T_p}{T_p + F_n}` These quantities are also related to the (:math:`F_1`) score, which is defined as the harmonic mean of precision and recall. :math:`F1 = 2\\frac{P \\times R}{P+R}` It is important to note that the precision may not decrease with recall. The definition of precision (:math:`\\frac{T_p}{T_p + F_p}`) shows that lowering the threshold of a classifier may increase the denominator, by increasing the number of results returned. If the threshold was previously set too high, the new results may all be true positives, which will increase precision. If the previous threshold was about right or too low, further lowering the threshold will introduce false positives, decreasing precision. Recall is defined as :math:`\\frac{T_p}{T_p+F_n}`, where :math:`T_p+F_n` does not depend on the classifier threshold. This means that lowering the classifier threshold may increase recall, by increasing the number of true positive results. It is also possible that lowering the threshold may leave recall unchanged, while the precision fluctuates. The relationship between recall and precision can be observed in the stairstep area of the plot - at the edges of these steps a small change in the threshold considerably reduces precision, with only a minor gain in recall. See the corner at recall = .59, precision = .8 for an example of this phenomenon. Precision-recall curves are typically used in binary classification to study the output of a classifier. In order to extend Precision-recall curve and average precision to multi-class or multi-label classification, it is necessary to binarize the output. One curve can be drawn per label, but one can also draw a precision-recall curve by considering each element of the label indicator matrix as a binary prediction (micro-averaging). .. note:: See also :func:`sklearn.metrics.average_precision_score`, :func:`sklearn.metrics.recall_score`, :func:`sklearn.metrics.precision_score`, :func:`sklearn.metrics.f1_score` """ print(__doc__) import matplotlib.pyplot as plt import numpy as np from sklearn import svm, datasets from sklearn.metrics import precision_recall_curve from sklearn.metrics import average_precision_score from sklearn.cross_validation import train_test_split from sklearn.preprocessing import label_binarize from sklearn.multiclass import OneVsRestClassifier # import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target # Binarize the output y = label_binarize(y, classes=[0, 1, 2]) n_classes = y.shape[1] # Add noisy features random_state = np.random.RandomState(0) n_samples, n_features = X.shape X = np.c_[X, random_state.randn(n_samples, 200 * n_features)] # Split into training and test X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=random_state) # Run classifier classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, random_state=random_state)) y_score = classifier.fit(X_train, y_train).decision_function(X_test) # Compute Precision-Recall and plot curve precision = dict() recall = dict() average_precision = dict() for i in range(n_classes): precision[i], recall[i], _ = precision_recall_curve(y_test[:, i], y_score[:, i]) average_precision[i] = average_precision_score(y_test[:, i], y_score[:, i]) # Compute micro-average ROC curve and ROC area precision["micro"], recall["micro"], _ = precision_recall_curve(y_test.ravel(), y_score.ravel()) average_precision["micro"] = average_precision_score(y_test, y_score, average="micro") # Plot Precision-Recall curve plt.clf() plt.plot(recall[0], precision[0], label='Precision-Recall curve') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title('Precision-Recall example: AUC={0:0.2f}'.format(average_precision[0])) plt.legend(loc="lower left") plt.show() # Plot Precision-Recall curve for each class plt.clf() plt.plot(recall["micro"], precision["micro"], label='micro-average Precision-recall curve (area = {0:0.2f})' ''.format(average_precision["micro"])) for i in range(n_classes): plt.plot(recall[i], precision[i], label='Precision-recall curve of class {0} (area = {1:0.2f})' ''.format(i, average_precision[i])) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('Recall') plt.ylabel('Precision') plt.title('Extension of Precision-Recall curve to multi-class') plt.legend(loc="lower right") plt.show()
bsd-3-clause
zihangdai/xlnet
run_race.py
1
19028
from __future__ import absolute_import from __future__ import division from __future__ import print_function from os.path import join from absl import flags import os import csv import collections import numpy as np import time import math import json import random from copy import copy from collections import defaultdict as dd from scipy.stats import pearsonr, spearmanr from sklearn.metrics import matthews_corrcoef, f1_score import absl.logging as _logging # pylint: disable=unused-import import tensorflow as tf import sentencepiece as spm from data_utils import SEP_ID, VOCAB_SIZE, CLS_ID import model_utils import function_builder from classifier_utils import PaddingInputExample from classifier_utils import convert_single_example from prepro_utils import preprocess_text, encode_ids # Model flags.DEFINE_string("model_config_path", default=None, help="Model config path.") flags.DEFINE_float("dropout", default=0.1, help="Dropout rate.") flags.DEFINE_float("dropatt", default=0.1, help="Attention dropout rate.") flags.DEFINE_integer("clamp_len", default=-1, help="Clamp length") flags.DEFINE_string("summary_type", default="last", help="Method used to summarize a sequence into a compact vector.") flags.DEFINE_bool("use_summ_proj", default=True, help="Whether to use projection for summarizing sequences.") flags.DEFINE_bool("use_bfloat16", default=False, help="Whether to use bfloat16.") # Parameter initialization flags.DEFINE_enum("init", default="normal", enum_values=["normal", "uniform"], help="Initialization method.") flags.DEFINE_float("init_std", default=0.02, help="Initialization std when init is normal.") flags.DEFINE_float("init_range", default=0.1, help="Initialization std when init is uniform.") # I/O paths flags.DEFINE_bool("overwrite_data", default=False, help="If False, will use cached data if available.") flags.DEFINE_string("init_checkpoint", default=None, help="checkpoint path for initializing the model. " "Could be a pretrained model or a finetuned model.") flags.DEFINE_string("output_dir", default="", help="Output dir for TF records.") flags.DEFINE_string("spiece_model_file", default="", help="Sentence Piece model path.") flags.DEFINE_string("model_dir", default="", help="Directory for saving the finetuned model.") flags.DEFINE_string("data_dir", default="", help="Directory for input data.") # TPUs and machines flags.DEFINE_bool("use_tpu", default=False, help="whether to use TPU.") flags.DEFINE_integer("num_hosts", default=1, help="How many TPU hosts.") flags.DEFINE_integer("num_core_per_host", default=8, help="8 for TPU v2 and v3-8, 16 for larger TPU v3 pod. In the context " "of GPU training, it refers to the number of GPUs used.") flags.DEFINE_string("tpu_job_name", default=None, help="TPU worker job name.") flags.DEFINE_string("tpu", default=None, help="TPU name.") flags.DEFINE_string("tpu_zone", default=None, help="TPU zone.") flags.DEFINE_string("gcp_project", default=None, help="gcp project.") flags.DEFINE_string("master", default=None, help="master") flags.DEFINE_integer("iterations", default=1000, help="number of iterations per TPU training loop.") # Training flags.DEFINE_bool("do_train", default=False, help="whether to do training") flags.DEFINE_integer("train_steps", default=12000, help="Number of training steps") flags.DEFINE_integer("warmup_steps", default=0, help="number of warmup steps") flags.DEFINE_float("learning_rate", default=2e-5, help="initial learning rate") flags.DEFINE_float("lr_layer_decay_rate", 1.0, "Top layer: lr[L] = FLAGS.learning_rate." "Low layer: lr[l-1] = lr[l] * lr_layer_decay_rate.") flags.DEFINE_float("min_lr_ratio", default=0.0, help="min lr ratio for cos decay.") flags.DEFINE_float("clip", default=1.0, help="Gradient clipping") flags.DEFINE_integer("max_save", default=0, help="Max number of checkpoints to save. Use 0 to save all.") flags.DEFINE_integer("save_steps", default=None, help="Save the model for every save_steps. " "If None, not to save any model.") flags.DEFINE_integer("train_batch_size", default=8, help="Batch size for training. Note that batch size 1 corresponds to " "4 sequences: one paragraph + one quesetion + 4 candidate answers.") flags.DEFINE_float("weight_decay", default=0.00, help="weight decay rate") flags.DEFINE_float("adam_epsilon", default=1e-6, help="adam epsilon") flags.DEFINE_string("decay_method", default="poly", help="poly or cos") # Evaluation flags.DEFINE_bool("do_eval", default=False, help="whether to do eval") flags.DEFINE_string("eval_split", default="dev", help="could be dev or test") flags.DEFINE_integer("eval_batch_size", default=32, help="Batch size for evaluation.") # Data config flags.DEFINE_integer("max_seq_length", default=512, help="Max length for the paragraph.") flags.DEFINE_integer("max_qa_length", default=128, help="Max length for the concatenated question and answer.") flags.DEFINE_integer("shuffle_buffer", default=2048, help="Buffer size used for shuffle.") flags.DEFINE_bool("uncased", default=False, help="Use uncased.") flags.DEFINE_bool("high_only", default=False, help="Evaluate on high school only.") flags.DEFINE_bool("middle_only", default=False, help="Evaluate on middle school only.") FLAGS = flags.FLAGS SEG_ID_A = 0 SEG_ID_B = 1 SEG_ID_CLS = 2 SEG_ID_SEP = 3 SEG_ID_PAD = 4 class PaddingInputExample(object): """Fake example so the num input examples is a multiple of the batch size. When running eval/predict on the TPU, we need to pad the number of examples to be a multiple of the batch size, because the TPU requires a fixed batch size. The alternative is to drop the last batch, which is bad because it means the entire output data won't be generated. We use this class instead of `None` because treating `None` as padding battches could cause silent errors. """ class InputFeatures(object): """A single set of features of data.""" def __init__(self, input_ids, input_mask, segment_ids, label_id, is_real_example=True): self.input_ids = input_ids self.input_mask = input_mask self.segment_ids = segment_ids self.label_id = label_id self.is_real_example = is_real_example def convert_single_example(example, tokenize_fn): """Converts a single `InputExample` into a single `InputFeatures`.""" if isinstance(example, PaddingInputExample): return InputFeatures( input_ids=[0] * FLAGS.max_seq_length * 4, input_mask=[1] * FLAGS.max_seq_length * 4, segment_ids=[0] * FLAGS.max_seq_length * 4, label_id=0, is_real_example=False) input_ids, input_mask, all_seg_ids = [], [], [] tokens_context = tokenize_fn(example.context) for i in range(len(example.qa_list)): tokens_qa = tokenize_fn(example.qa_list[i]) if len(tokens_qa) > FLAGS.max_qa_length: tokens_qa = tokens_qa[- FLAGS.max_qa_length:] if len(tokens_context) + len(tokens_qa) > FLAGS.max_seq_length - 3: tokens = tokens_context[: FLAGS.max_seq_length - 3 - len(tokens_qa)] else: tokens = tokens_context segment_ids = [SEG_ID_A] * len(tokens) tokens.append(SEP_ID) segment_ids.append(SEG_ID_A) tokens.extend(tokens_qa) segment_ids.extend([SEG_ID_B] * len(tokens_qa)) tokens.append(SEP_ID) segment_ids.append(SEG_ID_B) tokens.append(CLS_ID) segment_ids.append(SEG_ID_CLS) cur_input_ids = tokens cur_input_mask = [0] * len(cur_input_ids) if len(cur_input_ids) < FLAGS.max_seq_length: delta_len = FLAGS.max_seq_length - len(cur_input_ids) cur_input_ids = [0] * delta_len + cur_input_ids cur_input_mask = [1] * delta_len + cur_input_mask segment_ids = [SEG_ID_PAD] * delta_len + segment_ids assert len(cur_input_ids) == FLAGS.max_seq_length assert len(cur_input_mask) == FLAGS.max_seq_length assert len(segment_ids) == FLAGS.max_seq_length input_ids.extend(cur_input_ids) input_mask.extend(cur_input_mask) all_seg_ids.extend(segment_ids) label_id = example.label feature = InputFeatures( input_ids=input_ids, input_mask=input_mask, segment_ids=all_seg_ids, label_id=label_id) return feature class InputExample(object): def __init__(self, context, qa_list, label, level): self.context = context self.qa_list = qa_list self.label = label self.level = level def get_examples(data_dir, set_type): examples = [] for level in ["middle", "high"]: if level == "middle" and FLAGS.high_only: continue if level == "high" and FLAGS.middle_only: continue cur_dir = os.path.join(data_dir, set_type, level) for filename in tf.gfile.ListDirectory(cur_dir): cur_path = os.path.join(cur_dir, filename) with tf.gfile.Open(cur_path) as f: cur_data = json.load(f) answers = cur_data["answers"] options = cur_data["options"] questions = cur_data["questions"] context = cur_data["article"] for i in range(len(answers)): label = ord(answers[i]) - ord("A") qa_list = [] question = questions[i] for j in range(4): option = options[i][j] if "_" in question: qa_cat = question.replace("_", option) else: qa_cat = " ".join([question, option]) qa_list.append(qa_cat) examples.append(InputExample(context, qa_list, label, level)) return examples def file_based_convert_examples_to_features(examples, tokenize_fn, output_file): if tf.gfile.Exists(output_file) and not FLAGS.overwrite_data: return tf.logging.info("Start writing tfrecord %s.", output_file) writer = tf.python_io.TFRecordWriter(output_file) for ex_index, example in enumerate(examples): if ex_index % 10000 == 0: tf.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(example, tokenize_fn) def create_int_feature(values): f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return f def create_float_feature(values): f = tf.train.Feature(float_list=tf.train.FloatList(value=list(values))) return f features = collections.OrderedDict() features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_float_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) features["label_ids"] = create_int_feature([feature.label_id]) features["is_real_example"] = create_int_feature( [int(feature.is_real_example)]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tf_example.SerializeToString()) writer.close() def file_based_input_fn_builder(input_file, seq_length, is_training, drop_remainder): """Creates an `input_fn` closure to be passed to TPUEstimator.""" name_to_features = { "input_ids": tf.FixedLenFeature([seq_length * 4], tf.int64), "input_mask": tf.FixedLenFeature([seq_length * 4], tf.float32), "segment_ids": tf.FixedLenFeature([seq_length * 4], tf.int64), "label_ids": tf.FixedLenFeature([], tf.int64), "is_real_example": tf.FixedLenFeature([], tf.int64), } tf.logging.info("Input tfrecord file {}".format(input_file)) def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.cast(t, tf.int32) example[name] = t return example def input_fn(params): """The actual input function.""" if FLAGS.use_tpu: batch_size = params["batch_size"] elif is_training: batch_size = FLAGS.train_batch_size elif FLAGS.do_eval: batch_size = FLAGS.eval_batch_size # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. d = tf.data.TFRecordDataset(input_file) if is_training: d = d.shuffle(buffer_size=FLAGS.shuffle_buffer) d = d.repeat() # d = d.shuffle(buffer_size=100) d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder)) return d return input_fn def get_model_fn(): def model_fn(features, labels, mode, params): #### Training or Evaluation is_training = (mode == tf.estimator.ModeKeys.TRAIN) total_loss, per_example_loss, logits = function_builder.get_race_loss( FLAGS, features, is_training) #### Check model parameters num_params = sum([np.prod(v.shape) for v in tf.trainable_variables()]) tf.logging.info('#params: {}'.format(num_params)) #### load pretrained models scaffold_fn = model_utils.init_from_checkpoint(FLAGS) #### Evaluation mode if mode == tf.estimator.ModeKeys.EVAL: assert FLAGS.num_hosts == 1 def metric_fn(per_example_loss, label_ids, logits, is_real_example): predictions = tf.argmax(logits, axis=-1, output_type=tf.int32) eval_input_dict = { 'labels': label_ids, 'predictions': predictions, 'weights': is_real_example } accuracy = tf.metrics.accuracy(**eval_input_dict) loss = tf.metrics.mean(values=per_example_loss, weights=is_real_example) return { 'eval_accuracy': accuracy, 'eval_loss': loss} is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32) #### Constucting evaluation TPUEstimatorSpec with new cache. label_ids = tf.reshape(features['label_ids'], [-1]) metric_args = [per_example_loss, label_ids, logits, is_real_example] if FLAGS.use_tpu: eval_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, loss=total_loss, eval_metrics=(metric_fn, metric_args), scaffold_fn=scaffold_fn) else: eval_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, eval_metric_ops=metric_fn(*metric_args)) return eval_spec #### Configuring the optimizer train_op, learning_rate, _ = model_utils.get_train_op(FLAGS, total_loss) monitor_dict = {} monitor_dict["lr"] = learning_rate #### Constucting training TPUEstimatorSpec with new cache. if FLAGS.use_tpu: #### Creating host calls host_call = None train_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, loss=total_loss, train_op=train_op, host_call=host_call, scaffold_fn=scaffold_fn) else: train_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) return train_spec return model_fn def main(_): tf.logging.set_verbosity(tf.logging.INFO) #### Validate flags if FLAGS.save_steps is not None: FLAGS.iterations = min(FLAGS.iterations, FLAGS.save_steps) if not FLAGS.do_train and not FLAGS.do_eval: raise ValueError( "At least one of `do_train` or `do_eval` must be True.") if not tf.gfile.Exists(FLAGS.output_dir): tf.gfile.MakeDirs(FLAGS.output_dir) sp = spm.SentencePieceProcessor() sp.Load(FLAGS.spiece_model_file) def tokenize_fn(text): text = preprocess_text(text, lower=FLAGS.uncased) return encode_ids(sp, text) # TPU Configuration run_config = model_utils.configure_tpu(FLAGS) model_fn = get_model_fn() spm_basename = os.path.basename(FLAGS.spiece_model_file) # If TPU is not available, this will fall back to normal Estimator on CPU # or GPU. if FLAGS.use_tpu: estimator = tf.contrib.tpu.TPUEstimator( use_tpu=FLAGS.use_tpu, model_fn=model_fn, config=run_config, train_batch_size=FLAGS.train_batch_size, eval_batch_size=FLAGS.eval_batch_size) else: estimator = tf.estimator.Estimator( model_fn=model_fn, config=run_config) if FLAGS.do_train: train_file_base = "{}.len-{}.train.tf_record".format( spm_basename, FLAGS.max_seq_length) train_file = os.path.join(FLAGS.output_dir, train_file_base) if not tf.gfile.Exists(train_file) or FLAGS.overwrite_data: train_examples = get_examples(FLAGS.data_dir, "train") random.shuffle(train_examples) file_based_convert_examples_to_features( train_examples, tokenize_fn, train_file) train_input_fn = file_based_input_fn_builder( input_file=train_file, seq_length=FLAGS.max_seq_length, is_training=True, drop_remainder=True) estimator.train(input_fn=train_input_fn, max_steps=FLAGS.train_steps) if FLAGS.do_eval: eval_examples = get_examples(FLAGS.data_dir, FLAGS.eval_split) tf.logging.info("Num of eval samples: {}".format(len(eval_examples))) # TPU requires a fixed batch size for all batches, therefore the number # of examples must be a multiple of the batch size, or else examples # will get dropped. So we pad with fake examples which are ignored # later on. These do NOT count towards the metric (all tf.metrics # support a per-instance weight, and these get a weight of 0.0). # # Modified in XL: We also adopt the same mechanism for GPUs. while len(eval_examples) % FLAGS.eval_batch_size != 0: eval_examples.append(PaddingInputExample()) eval_file_base = "{}.len-{}.{}.tf_record".format( spm_basename, FLAGS.max_seq_length, FLAGS.eval_split) if FLAGS.high_only: eval_file_base = "high." + eval_file_base elif FLAGS.middle_only: eval_file_base = "middle." + eval_file_base eval_file = os.path.join(FLAGS.output_dir, eval_file_base) file_based_convert_examples_to_features( eval_examples, tokenize_fn, eval_file) assert len(eval_examples) % FLAGS.eval_batch_size == 0 eval_steps = int(len(eval_examples) // FLAGS.eval_batch_size) eval_input_fn = file_based_input_fn_builder( input_file=eval_file, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=True) ret = estimator.evaluate( input_fn=eval_input_fn, steps=eval_steps) # Log current result tf.logging.info("=" * 80) log_str = "Eval | " for key, val in ret.items(): log_str += "{} {} | ".format(key, val) tf.logging.info(log_str) tf.logging.info("=" * 80) if __name__ == "__main__": tf.app.run()
apache-2.0
mugizico/scikit-learn
benchmarks/bench_tree.py
297
3617
""" To run this, you'll need to have installed. * scikit-learn Does two benchmarks First, we fix a training set, increase the number of samples to classify and plot number of classified samples as a function of time. In the second benchmark, we increase the number of dimensions of the training set, classify a sample and plot the time taken as a function of the number of dimensions. """ import numpy as np import pylab as pl import gc from datetime import datetime # to store the results scikit_classifier_results = [] scikit_regressor_results = [] mu_second = 0.0 + 10 ** 6 # number of microseconds in a second def bench_scikit_tree_classifier(X, Y): """Benchmark with scikit-learn decision tree classifier""" from sklearn.tree import DecisionTreeClassifier gc.collect() # start time tstart = datetime.now() clf = DecisionTreeClassifier() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_classifier_results.append( delta.seconds + delta.microseconds / mu_second) def bench_scikit_tree_regressor(X, Y): """Benchmark with scikit-learn decision tree regressor""" from sklearn.tree import DecisionTreeRegressor gc.collect() # start time tstart = datetime.now() clf = DecisionTreeRegressor() clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_regressor_results.append( delta.seconds + delta.microseconds / mu_second) if __name__ == '__main__': print('============================================') print('Warning: this is going to take a looong time') print('============================================') n = 10 step = 10000 n_samples = 10000 dim = 10 n_classes = 10 for i in range(n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') n_samples += step X = np.random.randn(n_samples, dim) Y = np.random.randint(0, n_classes, (n_samples,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(n_samples) bench_scikit_tree_regressor(X, Y) xx = range(0, n * step, step) pl.figure('scikit-learn tree benchmark results') pl.subplot(211) pl.title('Learning with varying number of samples') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of samples') pl.ylabel('Time (s)') scikit_classifier_results = [] scikit_regressor_results = [] n = 10 step = 500 start_dim = 500 n_classes = 10 dim = start_dim for i in range(0, n): print('============================================') print('Entering iteration %s of %s' % (i, n)) print('============================================') dim += step X = np.random.randn(100, dim) Y = np.random.randint(0, n_classes, (100,)) bench_scikit_tree_classifier(X, Y) Y = np.random.randn(100) bench_scikit_tree_regressor(X, Y) xx = np.arange(start_dim, start_dim + n * step, step) pl.subplot(212) pl.title('Learning in high dimensional spaces') pl.plot(xx, scikit_classifier_results, 'g-', label='classification') pl.plot(xx, scikit_regressor_results, 'r-', label='regression') pl.legend(loc='upper left') pl.xlabel('number of dimensions') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
jljones/berkeley
python_email/simulation_example/chirikov.py
10
1970
import matplotlib.pyplot as plt import numpy as np import math def generateChirikovMaps(ks, numInits, numParticles): '''Given four values for the "kick" intensity, this function plots a Chirikov-Taylor map for each KI value in the same window for easy comparison. Recommended Values: ks = (.5,.75,.95,1.0) numInits = 20 numParticles = 10000 For more information on the standard map, see: http://en.wikipedia.org/wiki/Standard_map ''' f, ax = plt.subplots(2, 2) kin = 0 # Check to make sure the input length is 4, as expected if len(ks) != 4: print "Must have 4 and only 4 k values. Exiting..." return 1 # The outer 2 loops (j,l) handle the plotting for each axis for j in range( 0,len(ax) ): for l in range( 0,len(ax) ): # Run the simulation for numInits # of initial points print 'Simulating k = %s with %s particles...' %(k[kin], numInits) for i in range(1,numInits): temp = np.zeros( (2, numParticles) ) x = temp[0,:] p = temp[1,:] # Pick a starting point at random in p-phase space x[0] = np.random.rand()*2*math.pi p[0] = np.random.rand()*2*math.pi # Follow the trajectory of the random point in phase space according # to the chirikov-taylor relation for n in range(1,numParticles): p[n] = ( p[n-1] + ks[kin]*math.sin( x[n-1] ) ) % (2*math.pi) x[n] = ( x[n-1] + p[n] ) % (2*math.pi) # Plot each of the numInits trajectories on the same plot ax[j][l].plot( x/(2*math.pi), p/(2*math.pi), 'b.', ms=1) ax[j][l].set_title('Chirikov Map, K = %s' %(ks[kin]) ) ax[j][l].set_xlabel(r'$\frac{x}{2\pi}$') ax[j][l].set_ylabel(r'$\frac{p}{2\pi}$') kin += 1 plt.savefig('chirikov_results.png') return ax if __name__ == '__main__': k = [0.5, 0.75, 0.9, 1.0] generateChirikovMaps(k, 20, 50000)
bsd-3-clause
kou/arrow
python/examples/flight/client.py
6
6791
# 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. """An example Flight CLI client.""" import argparse import sys import pyarrow import pyarrow.flight import pyarrow.csv as csv def list_flights(args, client, connection_args={}): print('Flights\n=======') for flight in client.list_flights(): descriptor = flight.descriptor if descriptor.descriptor_type == pyarrow.flight.DescriptorType.PATH: print("Path:", descriptor.path) elif descriptor.descriptor_type == pyarrow.flight.DescriptorType.CMD: print("Command:", descriptor.command) else: print("Unknown descriptor type") print("Total records:", end=" ") if flight.total_records >= 0: print(flight.total_records) else: print("Unknown") print("Total bytes:", end=" ") if flight.total_bytes >= 0: print(flight.total_bytes) else: print("Unknown") print("Number of endpoints:", len(flight.endpoints)) print("Schema:") print(flight.schema) print('---') print('\nActions\n=======') for action in client.list_actions(): print("Type:", action.type) print("Description:", action.description) print('---') def do_action(args, client, connection_args={}): try: buf = pyarrow.allocate_buffer(0) action = pyarrow.flight.Action(args.action_type, buf) print('Running action', args.action_type) for result in client.do_action(action): print("Got result", result.body.to_pybytes()) except pyarrow.lib.ArrowIOError as e: print("Error calling action:", e) def push_data(args, client, connection_args={}): print('File Name:', args.file) my_table = csv.read_csv(args.file) print('Table rows=', str(len(my_table))) df = my_table.to_pandas() print(df.head()) writer, _ = client.do_put( pyarrow.flight.FlightDescriptor.for_path(args.file), my_table.schema) writer.write_table(my_table) writer.close() def get_flight(args, client, connection_args={}): if args.path: descriptor = pyarrow.flight.FlightDescriptor.for_path(*args.path) else: descriptor = pyarrow.flight.FlightDescriptor.for_command(args.command) info = client.get_flight_info(descriptor) for endpoint in info.endpoints: print('Ticket:', endpoint.ticket) for location in endpoint.locations: print(location) get_client = pyarrow.flight.FlightClient(location, **connection_args) reader = get_client.do_get(endpoint.ticket) df = reader.read_pandas() print(df) def _add_common_arguments(parser): parser.add_argument('--tls', action='store_true', help='Enable transport-level security') parser.add_argument('--tls-roots', default=None, help='Path to trusted TLS certificate(s)') parser.add_argument("--mtls", nargs=2, default=None, metavar=('CERTFILE', 'KEYFILE'), help="Enable transport-level security") parser.add_argument('host', type=str, help="Address or hostname to connect to") def main(): parser = argparse.ArgumentParser() subcommands = parser.add_subparsers() cmd_list = subcommands.add_parser('list') cmd_list.set_defaults(action='list') _add_common_arguments(cmd_list) cmd_list.add_argument('-l', '--list', action='store_true', help="Print more details.") cmd_do = subcommands.add_parser('do') cmd_do.set_defaults(action='do') _add_common_arguments(cmd_do) cmd_do.add_argument('action_type', type=str, help="The action type to run.") cmd_put = subcommands.add_parser('put') cmd_put.set_defaults(action='put') _add_common_arguments(cmd_put) cmd_put.add_argument('file', type=str, help="CSV file to upload.") cmd_get = subcommands.add_parser('get') cmd_get.set_defaults(action='get') _add_common_arguments(cmd_get) cmd_get_descriptor = cmd_get.add_mutually_exclusive_group(required=True) cmd_get_descriptor.add_argument('-p', '--path', type=str, action='append', help="The path for the descriptor.") cmd_get_descriptor.add_argument('-c', '--command', type=str, help="The command for the descriptor.") args = parser.parse_args() if not hasattr(args, 'action'): parser.print_help() sys.exit(1) commands = { 'list': list_flights, 'do': do_action, 'get': get_flight, 'put': push_data, } host, port = args.host.split(':') port = int(port) scheme = "grpc+tcp" connection_args = {} if args.tls: scheme = "grpc+tls" if args.tls_roots: with open(args.tls_roots, "rb") as root_certs: connection_args["tls_root_certs"] = root_certs.read() if args.mtls: with open(args.mtls[0], "rb") as cert_file: tls_cert_chain = cert_file.read() with open(args.mtls[1], "rb") as key_file: tls_private_key = key_file.read() connection_args["cert_chain"] = tls_cert_chain connection_args["private_key"] = tls_private_key client = pyarrow.flight.FlightClient(f"{scheme}://{host}:{port}", **connection_args) while True: try: action = pyarrow.flight.Action("healthcheck", b"") options = pyarrow.flight.FlightCallOptions(timeout=1) list(client.do_action(action, options=options)) break except pyarrow.ArrowIOError as e: if "Deadline" in str(e): print("Server is not ready, waiting...") commands[args.action](args, client, connection_args) if __name__ == '__main__': main()
apache-2.0
zaxliu/scipy
scipy/integrate/quadrature.py
26
27908
from __future__ import division, print_function, absolute_import __all__ = ['fixed_quad','quadrature','romberg','trapz','simps','romb', 'cumtrapz','newton_cotes'] from scipy.special.orthogonal import p_roots from scipy.special import gammaln from numpy import sum, ones, add, diff, isinf, isscalar, \ asarray, real, trapz, arange, empty import numpy as np import math import warnings from scipy._lib.six import xrange class AccuracyWarning(Warning): pass def _cached_p_roots(n): """ Cache p_roots results for speeding up multiple calls of the fixed_quad function. """ if n in _cached_p_roots.cache: return _cached_p_roots.cache[n] _cached_p_roots.cache[n] = p_roots(n) return _cached_p_roots.cache[n] _cached_p_roots.cache = dict() def fixed_quad(func,a,b,args=(),n=5): """ Compute a definite integral using fixed-order Gaussian quadrature. Integrate `func` from `a` to `b` using Gaussian quadrature of order `n`. Parameters ---------- func : callable A Python function or method to integrate (must accept vector inputs). a : float Lower limit of integration. b : float Upper limit of integration. args : tuple, optional Extra arguments to pass to function, if any. n : int, optional Order of quadrature integration. Default is 5. Returns ------- val : float Gaussian quadrature approximation to the integral none : None Statically returned value of None See Also -------- quad : adaptive quadrature using QUADPACK dblquad : double integrals tplquad : triple integrals romberg : adaptive Romberg quadrature quadrature : adaptive Gaussian quadrature romb : integrators for sampled data simps : integrators for sampled data cumtrapz : cumulative integration for sampled data ode : ODE integrator odeint : ODE integrator """ [x,w] = _cached_p_roots(n) x = real(x) ainf, binf = map(isinf,(a,b)) if ainf or binf: raise ValueError("Gaussian quadrature is only available for " "finite limits.") y = (b-a)*(x+1)/2.0 + a return (b-a)/2.0*sum(w*func(y,*args),0), None def vectorize1(func, args=(), vec_func=False): """Vectorize the call to a function. This is an internal utility function used by `romberg` and `quadrature` to create a vectorized version of a function. If `vec_func` is True, the function `func` is assumed to take vector arguments. Parameters ---------- func : callable User defined function. args : tuple, optional Extra arguments for the function. vec_func : bool, optional True if the function func takes vector arguments. Returns ------- vfunc : callable A function that will take a vector argument and return the result. """ if vec_func: def vfunc(x): return func(x, *args) else: def vfunc(x): if isscalar(x): return func(x, *args) x = asarray(x) # call with first point to get output type y0 = func(x[0], *args) n = len(x) if hasattr(y0, 'dtype'): output = empty((n,), dtype=y0.dtype) else: output = empty((n,), dtype=type(y0)) output[0] = y0 for i in xrange(1, n): output[i] = func(x[i], *args) return output return vfunc def quadrature(func, a, b, args=(), tol=1.49e-8, rtol=1.49e-8, maxiter=50, vec_func=True, miniter=1): """ Compute a definite integral using fixed-tolerance Gaussian quadrature. Integrate `func` from `a` to `b` using Gaussian quadrature with absolute tolerance `tol`. Parameters ---------- func : function A Python function or method to integrate. a : float Lower limit of integration. b : float Upper limit of integration. args : tuple, optional Extra arguments to pass to function. tol, rtol : float, optional Iteration stops when error between last two iterates is less than `tol` OR the relative change is less than `rtol`. maxiter : int, optional Maximum order of Gaussian quadrature. vec_func : bool, optional True or False if func handles arrays as arguments (is a "vector" function). Default is True. miniter : int, optional Minimum order of Gaussian quadrature. Returns ------- val : float Gaussian quadrature approximation (within tolerance) to integral. err : float Difference between last two estimates of the integral. See also -------- romberg: adaptive Romberg quadrature fixed_quad: fixed-order Gaussian quadrature quad: adaptive quadrature using QUADPACK dblquad: double integrals tplquad: triple integrals romb: integrator for sampled data simps: integrator for sampled data cumtrapz: cumulative integration for sampled data ode: ODE integrator odeint: ODE integrator """ if not isinstance(args, tuple): args = (args,) vfunc = vectorize1(func, args, vec_func=vec_func) val = np.inf err = np.inf maxiter = max(miniter+1, maxiter) for n in xrange(miniter, maxiter+1): newval = fixed_quad(vfunc, a, b, (), n)[0] err = abs(newval-val) val = newval if err < tol or err < rtol*abs(val): break else: warnings.warn( "maxiter (%d) exceeded. Latest difference = %e" % (maxiter, err), AccuracyWarning) return val, err def tupleset(t, i, value): l = list(t) l[i] = value return tuple(l) def cumtrapz(y, x=None, dx=1.0, axis=-1, initial=None): """ Cumulatively integrate y(x) using the composite trapezoidal rule. Parameters ---------- y : array_like Values to integrate. x : array_like, optional The coordinate to integrate along. If None (default), use spacing `dx` between consecutive elements in `y`. dx : int, optional Spacing between elements of `y`. Only used if `x` is None. axis : int, optional Specifies the axis to cumulate. Default is -1 (last axis). initial : scalar, optional If given, uses this value as the first value in the returned result. Typically this value should be 0. Default is None, which means no value at ``x[0]`` is returned and `res` has one element less than `y` along the axis of integration. Returns ------- res : ndarray The result of cumulative integration of `y` along `axis`. If `initial` is None, the shape is such that the axis of integration has one less value than `y`. If `initial` is given, the shape is equal to that of `y`. See Also -------- numpy.cumsum, numpy.cumprod quad: adaptive quadrature using QUADPACK romberg: adaptive Romberg quadrature quadrature: adaptive Gaussian quadrature fixed_quad: fixed-order Gaussian quadrature dblquad: double integrals tplquad: triple integrals romb: integrators for sampled data ode: ODE integrators odeint: ODE integrators Examples -------- >>> from scipy import integrate >>> import matplotlib.pyplot as plt >>> x = np.linspace(-2, 2, num=20) >>> y = x >>> y_int = integrate.cumtrapz(y, x, initial=0) >>> plt.plot(x, y_int, 'ro', x, y[0] + 0.5 * x**2, 'b-') >>> plt.show() """ y = asarray(y) if x is None: d = dx else: x = asarray(x) if x.ndim == 1: d = diff(x) # reshape to correct shape shape = [1] * y.ndim shape[axis] = -1 d = d.reshape(shape) elif len(x.shape) != len(y.shape): raise ValueError("If given, shape of x must be 1-d or the " "same as y.") else: d = diff(x, axis=axis) if d.shape[axis] != y.shape[axis] - 1: raise ValueError("If given, length of x along axis must be the " "same as y.") nd = len(y.shape) slice1 = tupleset((slice(None),)*nd, axis, slice(1, None)) slice2 = tupleset((slice(None),)*nd, axis, slice(None, -1)) res = add.accumulate(d * (y[slice1] + y[slice2]) / 2.0, axis) if initial is not None: if not np.isscalar(initial): raise ValueError("`initial` parameter should be a scalar.") shape = list(res.shape) shape[axis] = 1 res = np.concatenate([np.ones(shape, dtype=res.dtype) * initial, res], axis=axis) return res def _basic_simps(y,start,stop,x,dx,axis): nd = len(y.shape) if start is None: start = 0 step = 2 all = (slice(None),)*nd slice0 = tupleset(all, axis, slice(start, stop, step)) slice1 = tupleset(all, axis, slice(start+1, stop+1, step)) slice2 = tupleset(all, axis, slice(start+2, stop+2, step)) if x is None: # Even spaced Simpson's rule. result = add.reduce(dx/3.0 * (y[slice0]+4*y[slice1]+y[slice2]), axis) else: # Account for possibly different spacings. # Simpson's rule changes a bit. h = diff(x,axis=axis) sl0 = tupleset(all, axis, slice(start, stop, step)) sl1 = tupleset(all, axis, slice(start+1, stop+1, step)) h0 = h[sl0] h1 = h[sl1] hsum = h0 + h1 hprod = h0 * h1 h0divh1 = h0 / h1 result = add.reduce(hsum/6.0*(y[slice0]*(2-1.0/h0divh1) + y[slice1]*hsum*hsum/hprod + y[slice2]*(2-h0divh1)),axis) return result def simps(y, x=None, dx=1, axis=-1, even='avg'): """ Integrate y(x) using samples along the given axis and the composite Simpson's rule. If x is None, spacing of dx is assumed. If there are an even number of samples, N, then there are an odd number of intervals (N-1), but Simpson's rule requires an even number of intervals. The parameter 'even' controls how this is handled. Parameters ---------- y : array_like Array to be integrated. x : array_like, optional If given, the points at which `y` is sampled. dx : int, optional Spacing of integration points along axis of `y`. Only used when `x` is None. Default is 1. axis : int, optional Axis along which to integrate. Default is the last axis. even : {'avg', 'first', 'str'}, optional 'avg' : Average two results:1) use the first N-2 intervals with a trapezoidal rule on the last interval and 2) use the last N-2 intervals with a trapezoidal rule on the first interval. 'first' : Use Simpson's rule for the first N-2 intervals with a trapezoidal rule on the last interval. 'last' : Use Simpson's rule for the last N-2 intervals with a trapezoidal rule on the first interval. See Also -------- quad: adaptive quadrature using QUADPACK romberg: adaptive Romberg quadrature quadrature: adaptive Gaussian quadrature fixed_quad: fixed-order Gaussian quadrature dblquad: double integrals tplquad: triple integrals romb: integrators for sampled data cumtrapz: cumulative integration for sampled data ode: ODE integrators odeint: ODE integrators Notes ----- For an odd number of samples that are equally spaced the result is exact if the function is a polynomial of order 3 or less. If the samples are not equally spaced, then the result is exact only if the function is a polynomial of order 2 or less. """ y = asarray(y) nd = len(y.shape) N = y.shape[axis] last_dx = dx first_dx = dx returnshape = 0 if x is not None: x = asarray(x) if len(x.shape) == 1: shapex = ones(nd) shapex[axis] = x.shape[0] saveshape = x.shape returnshape = 1 x = x.reshape(tuple(shapex)) elif len(x.shape) != len(y.shape): raise ValueError("If given, shape of x must be 1-d or the " "same as y.") if x.shape[axis] != N: raise ValueError("If given, length of x along axis must be the " "same as y.") if N % 2 == 0: val = 0.0 result = 0.0 slice1 = (slice(None),)*nd slice2 = (slice(None),)*nd if even not in ['avg', 'last', 'first']: raise ValueError("Parameter 'even' must be 'avg', 'last', or 'first'.") # Compute using Simpson's rule on first intervals if even in ['avg', 'first']: slice1 = tupleset(slice1, axis, -1) slice2 = tupleset(slice2, axis, -2) if x is not None: last_dx = x[slice1] - x[slice2] val += 0.5*last_dx*(y[slice1]+y[slice2]) result = _basic_simps(y,0,N-3,x,dx,axis) # Compute using Simpson's rule on last set of intervals if even in ['avg', 'last']: slice1 = tupleset(slice1, axis, 0) slice2 = tupleset(slice2, axis, 1) if x is not None: first_dx = x[tuple(slice2)] - x[tuple(slice1)] val += 0.5*first_dx*(y[slice2]+y[slice1]) result += _basic_simps(y,1,N-2,x,dx,axis) if even == 'avg': val /= 2.0 result /= 2.0 result = result + val else: result = _basic_simps(y,0,N-2,x,dx,axis) if returnshape: x = x.reshape(saveshape) return result def romb(y, dx=1.0, axis=-1, show=False): """ Romberg integration using samples of a function. Parameters ---------- y : array_like A vector of ``2**k + 1`` equally-spaced samples of a function. dx : float, optional The sample spacing. Default is 1. axis : int, optional The axis along which to integrate. Default is -1 (last axis). show : bool, optional When `y` is a single 1-D array, then if this argument is True print the table showing Richardson extrapolation from the samples. Default is False. Returns ------- romb : ndarray The integrated result for `axis`. See also -------- quad : adaptive quadrature using QUADPACK romberg : adaptive Romberg quadrature quadrature : adaptive Gaussian quadrature fixed_quad : fixed-order Gaussian quadrature dblquad : double integrals tplquad : triple integrals simps : integrators for sampled data cumtrapz : cumulative integration for sampled data ode : ODE integrators odeint : ODE integrators """ y = asarray(y) nd = len(y.shape) Nsamps = y.shape[axis] Ninterv = Nsamps-1 n = 1 k = 0 while n < Ninterv: n <<= 1 k += 1 if n != Ninterv: raise ValueError("Number of samples must be one plus a " "non-negative power of 2.") R = {} all = (slice(None),) * nd slice0 = tupleset(all, axis, 0) slicem1 = tupleset(all, axis, -1) h = Ninterv*asarray(dx)*1.0 R[(0,0)] = (y[slice0] + y[slicem1])/2.0*h slice_R = all start = stop = step = Ninterv for i in range(1,k+1): start >>= 1 slice_R = tupleset(slice_R, axis, slice(start,stop,step)) step >>= 1 R[(i,0)] = 0.5*(R[(i-1,0)] + h*add.reduce(y[slice_R],axis)) for j in range(1,i+1): R[(i,j)] = R[(i,j-1)] + \ (R[(i,j-1)]-R[(i-1,j-1)]) / ((1 << (2*j))-1) h = h / 2.0 if show: if not isscalar(R[(0,0)]): print("*** Printing table only supported for integrals" + " of a single data set.") else: try: precis = show[0] except (TypeError, IndexError): precis = 5 try: width = show[1] except (TypeError, IndexError): width = 8 formstr = "%" + str(width) + '.' + str(precis)+'f' print("\n Richardson Extrapolation Table for Romberg Integration ") print("====================================================================") for i in range(0,k+1): for j in range(0,i+1): print(formstr % R[(i,j)], end=' ') print() print("====================================================================\n") return R[(k,k)] # Romberg quadratures for numeric integration. # # Written by Scott M. Ransom <ransom@cfa.harvard.edu> # last revision: 14 Nov 98 # # Cosmetic changes by Konrad Hinsen <hinsen@cnrs-orleans.fr> # last revision: 1999-7-21 # # Adapted to scipy by Travis Oliphant <oliphant.travis@ieee.org> # last revision: Dec 2001 def _difftrap(function, interval, numtraps): """ Perform part of the trapezoidal rule to integrate a function. Assume that we had called difftrap with all lower powers-of-2 starting with 1. Calling difftrap only returns the summation of the new ordinates. It does _not_ multiply by the width of the trapezoids. This must be performed by the caller. 'function' is the function to evaluate (must accept vector arguments). 'interval' is a sequence with lower and upper limits of integration. 'numtraps' is the number of trapezoids to use (must be a power-of-2). """ if numtraps <= 0: raise ValueError("numtraps must be > 0 in difftrap().") elif numtraps == 1: return 0.5*(function(interval[0])+function(interval[1])) else: numtosum = numtraps/2 h = float(interval[1]-interval[0])/numtosum lox = interval[0] + 0.5 * h points = lox + h * arange(0, numtosum) s = sum(function(points),0) return s def _romberg_diff(b, c, k): """ Compute the differences for the Romberg quadrature corrections. See Forman Acton's "Real Computing Made Real," p 143. """ tmp = 4.0**k return (tmp * c - b)/(tmp - 1.0) def _printresmat(function, interval, resmat): # Print the Romberg result matrix. i = j = 0 print('Romberg integration of', repr(function), end=' ') print('from', interval) print('') print('%6s %9s %9s' % ('Steps', 'StepSize', 'Results')) for i in range(len(resmat)): print('%6d %9f' % (2**i, (interval[1]-interval[0])/(2.**i)), end=' ') for j in range(i+1): print('%9f' % (resmat[i][j]), end=' ') print('') print('') print('The final result is', resmat[i][j], end=' ') print('after', 2**(len(resmat)-1)+1, 'function evaluations.') def romberg(function, a, b, args=(), tol=1.48e-8, rtol=1.48e-8, show=False, divmax=10, vec_func=False): """ Romberg integration of a callable function or method. Returns the integral of `function` (a function of one variable) over the interval (`a`, `b`). If `show` is 1, the triangular array of the intermediate results will be printed. If `vec_func` is True (default is False), then `function` is assumed to support vector arguments. Parameters ---------- function : callable Function to be integrated. a : float Lower limit of integration. b : float Upper limit of integration. Returns ------- results : float Result of the integration. Other Parameters ---------------- args : tuple, optional Extra arguments to pass to function. Each element of `args` will be passed as a single argument to `func`. Default is to pass no extra arguments. tol, rtol : float, optional The desired absolute and relative tolerances. Defaults are 1.48e-8. show : bool, optional Whether to print the results. Default is False. divmax : int, optional Maximum order of extrapolation. Default is 10. vec_func : bool, optional Whether `func` handles arrays as arguments (i.e whether it is a "vector" function). Default is False. See Also -------- fixed_quad : Fixed-order Gaussian quadrature. quad : Adaptive quadrature using QUADPACK. dblquad : Double integrals. tplquad : Triple integrals. romb : Integrators for sampled data. simps : Integrators for sampled data. cumtrapz : Cumulative integration for sampled data. ode : ODE integrator. odeint : ODE integrator. References ---------- .. [1] 'Romberg's method' http://en.wikipedia.org/wiki/Romberg%27s_method Examples -------- Integrate a gaussian from 0 to 1 and compare to the error function. >>> from scipy import integrate >>> from scipy.special import erf >>> gaussian = lambda x: 1/np.sqrt(np.pi) * np.exp(-x**2) >>> result = integrate.romberg(gaussian, 0, 1, show=True) Romberg integration of <function vfunc at ...> from [0, 1] :: Steps StepSize Results 1 1.000000 0.385872 2 0.500000 0.412631 0.421551 4 0.250000 0.419184 0.421368 0.421356 8 0.125000 0.420810 0.421352 0.421350 0.421350 16 0.062500 0.421215 0.421350 0.421350 0.421350 0.421350 32 0.031250 0.421317 0.421350 0.421350 0.421350 0.421350 0.421350 The final result is 0.421350396475 after 33 function evaluations. >>> print("%g %g" % (2*result, erf(1))) 0.842701 0.842701 """ if isinf(a) or isinf(b): raise ValueError("Romberg integration only available for finite limits.") vfunc = vectorize1(function, args, vec_func=vec_func) n = 1 interval = [a,b] intrange = b-a ordsum = _difftrap(vfunc, interval, n) result = intrange * ordsum resmat = [[result]] err = np.inf for i in xrange(1, divmax+1): n = n * 2 ordsum = ordsum + _difftrap(vfunc, interval, n) resmat.append([]) resmat[i].append(intrange * ordsum / n) for k in range(i): resmat[i].append(_romberg_diff(resmat[i-1][k], resmat[i][k], k+1)) result = resmat[i][i] lastresult = resmat[i-1][i-1] err = abs(result - lastresult) if err < tol or err < rtol*abs(result): break else: warnings.warn( "divmax (%d) exceeded. Latest difference = %e" % (divmax, err), AccuracyWarning) if show: _printresmat(vfunc, interval, resmat) return result # Coefficients for Netwon-Cotes quadrature # # These are the points being used # to construct the local interpolating polynomial # a are the weights for Newton-Cotes integration # B is the error coefficient. # error in these coefficients grows as N gets larger. # or as samples are closer and closer together # You can use maxima to find these rational coefficients # for equally spaced data using the commands # a(i,N) := integrate(product(r-j,j,0,i-1) * product(r-j,j,i+1,N),r,0,N) / ((N-i)! * i!) * (-1)^(N-i); # Be(N) := N^(N+2)/(N+2)! * (N/(N+3) - sum((i/N)^(N+2)*a(i,N),i,0,N)); # Bo(N) := N^(N+1)/(N+1)! * (N/(N+2) - sum((i/N)^(N+1)*a(i,N),i,0,N)); # B(N) := (if (mod(N,2)=0) then Be(N) else Bo(N)); # # pre-computed for equally-spaced weights # # num_a, den_a, int_a, num_B, den_B = _builtincoeffs[N] # # a = num_a*array(int_a)/den_a # B = num_B*1.0 / den_B # # integrate(f(x),x,x_0,x_N) = dx*sum(a*f(x_i)) + B*(dx)^(2k+3) f^(2k+2)(x*) # where k = N // 2 # _builtincoeffs = { 1:(1,2,[1,1],-1,12), 2:(1,3,[1,4,1],-1,90), 3:(3,8,[1,3,3,1],-3,80), 4:(2,45,[7,32,12,32,7],-8,945), 5:(5,288,[19,75,50,50,75,19],-275,12096), 6:(1,140,[41,216,27,272,27,216,41],-9,1400), 7:(7,17280,[751,3577,1323,2989,2989,1323,3577,751],-8183,518400), 8:(4,14175,[989,5888,-928,10496,-4540,10496,-928,5888,989], -2368,467775), 9:(9,89600,[2857,15741,1080,19344,5778,5778,19344,1080, 15741,2857], -4671, 394240), 10:(5,299376,[16067,106300,-48525,272400,-260550,427368, -260550,272400,-48525,106300,16067], -673175, 163459296), 11:(11,87091200,[2171465,13486539,-3237113, 25226685,-9595542, 15493566,15493566,-9595542,25226685,-3237113, 13486539,2171465], -2224234463, 237758976000), 12:(1, 5255250, [1364651,9903168,-7587864,35725120,-51491295, 87516288,-87797136,87516288,-51491295,35725120, -7587864,9903168,1364651], -3012, 875875), 13:(13, 402361344000,[8181904909, 56280729661, -31268252574, 156074417954,-151659573325,206683437987, -43111992612,-43111992612,206683437987, -151659573325,156074417954,-31268252574, 56280729661,8181904909], -2639651053, 344881152000), 14:(7, 2501928000, [90241897,710986864,-770720657,3501442784, -6625093363,12630121616,-16802270373,19534438464, -16802270373,12630121616,-6625093363,3501442784, -770720657,710986864,90241897], -3740727473, 1275983280000) } def newton_cotes(rn, equal=0): """ Return weights and error coefficient for Newton-Cotes integration. Suppose we have (N+1) samples of f at the positions x_0, x_1, ..., x_N. Then an N-point Newton-Cotes formula for the integral between x_0 and x_N is: :math:`\\int_{x_0}^{x_N} f(x)dx = \\Delta x \\sum_{i=0}^{N} a_i f(x_i) + B_N (\\Delta x)^{N+2} f^{N+1} (\\xi)` where :math:`\\xi \\in [x_0,x_N]` and :math:`\\Delta x = \\frac{x_N-x_0}{N}` is the averages samples spacing. If the samples are equally-spaced and N is even, then the error term is :math:`B_N (\\Delta x)^{N+3} f^{N+2}(\\xi)`. Parameters ---------- rn : int The integer order for equally-spaced data or the relative positions of the samples with the first sample at 0 and the last at N, where N+1 is the length of `rn`. N is the order of the Newton-Cotes integration. equal : int, optional Set to 1 to enforce equally spaced data. Returns ------- an : ndarray 1-D array of weights to apply to the function at the provided sample positions. B : float Error coefficient. Notes ----- Normally, the Newton-Cotes rules are used on smaller integration regions and a composite rule is used to return the total integral. """ try: N = len(rn)-1 if equal: rn = np.arange(N+1) elif np.all(np.diff(rn) == 1): equal = 1 except: N = rn rn = np.arange(N+1) equal = 1 if equal and N in _builtincoeffs: na, da, vi, nb, db = _builtincoeffs[N] return na*np.array(vi,float)/da, float(nb)/db if (rn[0] != 0) or (rn[-1] != N): raise ValueError("The sample positions must start at 0" " and end at N") yi = rn / float(N) ti = 2.0*yi - 1 nvec = np.arange(0,N+1) C = ti**nvec[:,np.newaxis] Cinv = np.linalg.inv(C) # improve precision of result for i in range(2): Cinv = 2*Cinv - Cinv.dot(C).dot(Cinv) vec = 2.0 / (nvec[::2]+1) ai = np.dot(Cinv[:,::2],vec) * N/2 if (N % 2 == 0) and equal: BN = N/(N+3.) power = N+2 else: BN = N/(N+2.) power = N+1 BN = BN - np.dot(yi**power, ai) p1 = power+1 fac = power*math.log(N) - gammaln(p1) fac = math.exp(fac) return ai, BN*fac
bsd-3-clause
evanbiederstedt/RRBSfun
epiphen/total_chr11.py
2
32998
import glob import pandas as pd import numpy as np pd.set_option('display.max_columns', 50) # print all rows import os os.chdir("/gpfs/commons/home/biederstedte-934/evan_projects/correct_phylo_files") normalB = glob.glob("binary_position_RRBS_normal_B_cell*") mcell = glob.glob("binary_position_RRBS_NormalBCD19pCD27mcell*") pcell = glob.glob("binary_position_RRBS_NormalBCD19pCD27pcell*") cd19cell = glob.glob("binary_position_RRBS_NormalBCD19pcell*") cw154 = glob.glob("binary_position_RRBS_cw154*") trito = glob.glob("binary_position_RRBS_trito_pool*") print(len(normalB)) print(len(mcell)) print(len(pcell)) print(len(cd19cell)) print(len(cw154)) print(len(trito)) totalfiles = normalB + mcell + pcell + cd19cell + cw154 + trito print(len(totalfiles)) df_list = [] for file in totalfiles: df = pd.read_csv(file) df = df.drop("Unnamed: 0", axis=1) df["chromosome"] = df["position"].map(lambda x: str(x)[:5]) df = df[df["chromosome"] == "chr11"] df = df.drop("chromosome", axis=1) df_list.append(df) print(len(df_list)) total_matrix = pd.concat([df.set_index("position") for df in df_list], axis=1).reset_index().astype(object) total_matrix = total_matrix.drop("index", axis=1) len(total_matrix.columns) total_matrix.columns = ["RRBS_normal_B_cell_A1_24_TAAGGCGA.ACAACC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.ACCGCG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.ACGTGG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.AGGATG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.ATAGCG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.ATCGAC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.CAAGAG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.CATGAC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.CGGTAG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.CTATTG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.CTCAGC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.GACACG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.GCTGCC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.GGCATC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.GTGAGG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.GTTGAG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.TAGCGG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.TATCTC", "RRBS_normal_B_cell_A1_24_TAAGGCGA.TCTCTG", "RRBS_normal_B_cell_A1_24_TAAGGCGA.TGACAG", "RRBS_normal_B_cell_B1_24_CGTACTAG.ACAACC", "RRBS_normal_B_cell_B1_24_CGTACTAG.ACCGCG", "RRBS_normal_B_cell_B1_24_CGTACTAG.ACTCAC", "RRBS_normal_B_cell_B1_24_CGTACTAG.ATAGCG", "RRBS_normal_B_cell_B1_24_CGTACTAG.CAAGAG", "RRBS_normal_B_cell_B1_24_CGTACTAG.CATGAC", "RRBS_normal_B_cell_B1_24_CGTACTAG.CCTTCG", "RRBS_normal_B_cell_B1_24_CGTACTAG.CGGTAG", "RRBS_normal_B_cell_B1_24_CGTACTAG.CTATTG", "RRBS_normal_B_cell_B1_24_CGTACTAG.CTCAGC", "RRBS_normal_B_cell_B1_24_CGTACTAG.GACACG", "RRBS_normal_B_cell_B1_24_CGTACTAG.GCATTC", "RRBS_normal_B_cell_B1_24_CGTACTAG.GGCATC", "RRBS_normal_B_cell_B1_24_CGTACTAG.GTGAGG", "RRBS_normal_B_cell_B1_24_CGTACTAG.GTTGAG", "RRBS_normal_B_cell_B1_24_CGTACTAG.TAGCGG", "RRBS_normal_B_cell_B1_24_CGTACTAG.TATCTC", "RRBS_normal_B_cell_B1_24_CGTACTAG.TCTCTG", "RRBS_normal_B_cell_B1_24_CGTACTAG.TGACAG", "RRBS_normal_B_cell_B1_24_CGTACTAG.TGCTGC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.ACAACC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.ACCGCG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.ACGTGG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.ACTCAC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.AGGATG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.ATAGCG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.ATCGAC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.CAAGAG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.CATGAC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.CGGTAG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.CTATTG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.GACACG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.GCATTC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.GCTGCC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.GGCATC", "RRBS_normal_B_cell_C1_24_AGGCAGAA.GTGAGG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.GTTGAG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.TAGCGG", "RRBS_normal_B_cell_C1_24_AGGCAGAA.TATCTC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.ACAACC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.ACCGCG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.ACGTGG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.ACTCAC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.AGGATG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.ATCGAC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.CAAGAG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.CATGAC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.CCTTCG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.CGGTAG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.CTATTG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.CTCAGC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.GACACG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.GCATTC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.GCTGCC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.GGCATC", "RRBS_normal_B_cell_D1_24_TCCTGAGC.GTTGAG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.TAGCGG", "RRBS_normal_B_cell_D1_24_TCCTGAGC.TATCTC", "RRBS_normal_B_cell_G1_22_GGACTCCT.ACAACC", "RRBS_normal_B_cell_G1_22_GGACTCCT.ACCGCG", "RRBS_normal_B_cell_G1_22_GGACTCCT.ACGTGG", "RRBS_normal_B_cell_G1_22_GGACTCCT.ACTCAC", "RRBS_normal_B_cell_G1_22_GGACTCCT.AGGATG", "RRBS_normal_B_cell_G1_22_GGACTCCT.ATAGCG", "RRBS_normal_B_cell_G1_22_GGACTCCT.ATCGAC", "RRBS_normal_B_cell_G1_22_GGACTCCT.CAAGAG", "RRBS_normal_B_cell_G1_22_GGACTCCT.CATGAC", "RRBS_normal_B_cell_G1_22_GGACTCCT.CGGTAG", "RRBS_normal_B_cell_G1_22_GGACTCCT.CTATTG", "RRBS_normal_B_cell_G1_22_GGACTCCT.CTCAGC", "RRBS_normal_B_cell_G1_22_GGACTCCT.GACACG", "RRBS_normal_B_cell_G1_22_GGACTCCT.GCATTC", "RRBS_normal_B_cell_G1_22_GGACTCCT.GCTGCC", "RRBS_normal_B_cell_G1_22_GGACTCCT.GGCATC", "RRBS_normal_B_cell_G1_22_GGACTCCT.GTGAGG", "RRBS_normal_B_cell_G1_22_GGACTCCT.TAGCGG", "RRBS_normal_B_cell_G1_22_GGACTCCT.TATCTC", "RRBS_normal_B_cell_H1_22_TAGGCATG.ACCGCG", "RRBS_normal_B_cell_H1_22_TAGGCATG.ACGTGG", "RRBS_normal_B_cell_H1_22_TAGGCATG.ACTCAC", "RRBS_normal_B_cell_H1_22_TAGGCATG.AGGATG", "RRBS_normal_B_cell_H1_22_TAGGCATG.ATCGAC", "RRBS_normal_B_cell_H1_22_TAGGCATG.CAAGAG", "RRBS_normal_B_cell_H1_22_TAGGCATG.CATGAC", "RRBS_normal_B_cell_H1_22_TAGGCATG.CCTTCG", "RRBS_normal_B_cell_H1_22_TAGGCATG.CTATTG", "RRBS_normal_B_cell_H1_22_TAGGCATG.CTCAGC", "RRBS_normal_B_cell_H1_22_TAGGCATG.GCATTC", "RRBS_normal_B_cell_H1_22_TAGGCATG.GCTGCC", "RRBS_normal_B_cell_H1_22_TAGGCATG.GGCATC", "RRBS_normal_B_cell_H1_22_TAGGCATG.GTGAGG", "RRBS_normal_B_cell_H1_22_TAGGCATG.GTTGAG", "RRBS_normal_B_cell_H1_22_TAGGCATG.TCTCTG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.ACCGCG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.ACGTGG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.ACTCAC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.ATAGCG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.ATCGAC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.CAAGAG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.CATGAC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.CCTTCG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.CTATTG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.CTCAGC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.GACACG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.GCATTC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.GCTGCC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.GGCATC", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.GTGAGG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.GTTGAG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.TAGCGG", "RRBS_NormalBCD19pCD27mcell1_22_CGAGGCTG.TATCTC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.ACAACC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.ACCGCG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.ACGTGG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.ACTCAC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.AGGATG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.ATAGCG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.ATCGAC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.CAAGAG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.CATGAC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.CCTTCG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.CGGTAG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.CTATTG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.CTCAGC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.GACACG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.GCATTC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.GTGAGG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.GTTGAG", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.TATCTC", "RRBS_NormalBCD19pCD27mcell23_44_GTAGAGGA.TCTCTG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.ACAACC", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.ACGTGG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.ACTCAC", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.AGGATG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.ATAGCG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.ATCGAC", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.CAAGAG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.CATGAC", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.CCTTCG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.CGGTAG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.CTATTG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.CTCAGC", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.GACACG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.GTGAGG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.TAGCGG", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.TATCTC", "RRBS_NormalBCD19pCD27mcell45_66_TAAGGCGA.TCTCTG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.ACAACC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.ACCGCG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.ACGTGG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.ACTCAC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.AGGATG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.ATAGCG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.ATCGAC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.CAAGAG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.CATGAC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.CCTTCG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.CGGTAG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.CTATTG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.CTCAGC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.GACACG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.GCATTC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.GGCATC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.GTGAGG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.GTTGAG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.TAGCGG", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.TATCTC", "RRBS_NormalBCD19pCD27mcell67_88_CGTACTAG.TCTCTG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.ACAACC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.ACCGCG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.ACTCAC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.AGGATG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.ATAGCG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.ATCGAC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.CAAGAG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.CATGAC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.CCTTCG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.CGGTAG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.CTATTG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.CTCAGC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.GCATTC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.GCTGCC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.GGCATC", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.GTGAGG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.GTTGAG", "RRBS_NormalBCD19pCD27pcell1_22_TAGGCATG.TAGCGG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.ACAACC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.ACCGCG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.ACGTGG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.ACTCAC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.AGGATG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.ATAGCG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.ATCGAC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.CAAGAG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.CATGAC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.CCTTCG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.CGGTAG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.CTATTG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.CTCAGC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.GACACG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.GCATTC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.GCTGCC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.GGCATC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.GTGAGG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.GTTGAG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.TAGCGG", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.TATCTC", "RRBS_NormalBCD19pCD27pcell23_44_CTCTCTAC.TCTCTG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.ACCGCG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.ACTCAC", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.ATAGCG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.CAAGAG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.CCTTCG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.CTATTG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.GACACG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.GTGAGG", "RRBS_NormalBCD19pCD27pcell45_66_CAGAGAGG.TAGCGG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.ACAACC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.ACCGCG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.ACGTGG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.ACTCAC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.AGGATG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.ATAGCG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.ATCGAC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.CATGAC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.CCTTCG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.CGGTAG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.CTATTG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.CTCAGC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.GACACG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.GCATTC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.GCTGCC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.GGCATC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.GTGAGG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.GTTGAG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.TAGCGG", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.TATCTC", "RRBS_NormalBCD19pCD27pcell67_88_GCTACGCT.TCTCTG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.ACAACC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.ACCGCG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.ACGTGG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.ACTCAC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.AGGATG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.ATAGCG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.ATCGAC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.CAAGAG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.CATGAC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.CCTTCG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.CGGTAG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.CTATTG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.CTCAGC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.GACACG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.GCATTC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.GCTGCC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.GGCATC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.GTTGAG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.TAGCGG", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.TATCTC", "RRBS_NormalBCD19pcell1_22_TAAGGCGA.TCTCTG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.ACAACC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.ACCGCG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.ACGTGG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.ACTCAC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.AGGATG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.ATAGCG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.ATCGAC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.CATGAC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.CCTTCG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.CGGTAG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.CTATTG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.CTCAGC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.GACACG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.GCATTC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.GCTGCC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.GGCATC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.GTGAGG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.TAGCGG", "RRBS_NormalBCD19pcell23_44_CGTACTAG.TATCTC", "RRBS_NormalBCD19pcell23_44_CGTACTAG.TCTCTG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.ACAACC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.ACCGCG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.ACGTGG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.ACTCAC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.AGGATG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.ATAGCG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.ATCGAC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.CAAGAG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.CATGAC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.CCTTCG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.CGGTAG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.CTATTG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.CTCAGC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.GACACG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.GCATTC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.GCTGCC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.GGCATC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.GTGAGG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.GTTGAG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.TAGCGG", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.TATCTC", "RRBS_NormalBCD19pcell45_66_AGGCAGAA.TCTCTG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.ACAACC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.ACCGCG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.ACGTGG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.ACTCAC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.AGGATG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.ATAGCG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.ATCGAC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.CAAGAG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.CATGAC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.CCTTCG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.CGGTAG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.CTATTG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.CTCAGC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.GCATTC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.GCTGCC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.GGCATC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.GTGAGG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.GTTGAG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.TAGCGG", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.TATCTC", "RRBS_NormalBCD19pcell67_88_TCCTGAGC.TCTCTG", 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.ACAACC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.ACCGCG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.ACGTGG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.ACTCAC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.AGGATG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.ATAGCG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.ATCGAC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.CAAGAG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.CATGAC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.CCTTCG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.CGGTAG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.CTCAGC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.GACACG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.GCATTC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.GCTGCC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.GGCATC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.GTGAGG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.TAGCGG', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.TATCTC', 'RRBS_cw154_CutSmart_proteinase_K_TAGGCATG.TCTCTG', 'RRBS_cw154_Tris_protease_CTCTCTAC.ACAACC', 'RRBS_cw154_Tris_protease_CTCTCTAC.ACCGCG', 'RRBS_cw154_Tris_protease_CTCTCTAC.ACGTGG', 'RRBS_cw154_Tris_protease_CTCTCTAC.ACTCAC', 'RRBS_cw154_Tris_protease_CTCTCTAC.AGGATG', 'RRBS_cw154_Tris_protease_CTCTCTAC.ATAGCG', 'RRBS_cw154_Tris_protease_CTCTCTAC.ATCGAC', 'RRBS_cw154_Tris_protease_CTCTCTAC.CATGAC', 'RRBS_cw154_Tris_protease_CTCTCTAC.CCTTCG', 'RRBS_cw154_Tris_protease_CTCTCTAC.CGGTAG', 'RRBS_cw154_Tris_protease_CTCTCTAC.CTATTG', 'RRBS_cw154_Tris_protease_CTCTCTAC.CTCAGC', 'RRBS_cw154_Tris_protease_CTCTCTAC.GACACG', 'RRBS_cw154_Tris_protease_CTCTCTAC.GCATTC', 'RRBS_cw154_Tris_protease_CTCTCTAC.GCTGCC', 'RRBS_cw154_Tris_protease_CTCTCTAC.GGCATC', 'RRBS_cw154_Tris_protease_CTCTCTAC.GTGAGG', 'RRBS_cw154_Tris_protease_CTCTCTAC.GTTGAG', 'RRBS_cw154_Tris_protease_CTCTCTAC.TAGCGG', 'RRBS_cw154_Tris_protease_CTCTCTAC.TATCTC', 'RRBS_cw154_Tris_protease_CTCTCTAC.TCTCTG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.ACAACC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.ACCGCG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.ACGTGG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.ACTCAC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.AGGATG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.ATAGCG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.ATCGAC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.CATGAC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.CCTTCG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.CGGTAG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.CTATTG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.CTCAGC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.GACACG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.GCATTC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.GCTGCC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.GGCATC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.GTGAGG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.GTTGAG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.TAGCGG', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.TATCTC', 'RRBS_cw154_Tris_protease_GR_CAGAGAGG.TCTCTG', 'RRBS_trito_pool_1_TAAGGCGA.ACAACC', 'RRBS_trito_pool_1_TAAGGCGA.ACGTGG', 'RRBS_trito_pool_1_TAAGGCGA.ACTCAC', 'RRBS_trito_pool_1_TAAGGCGA.ATAGCG', 'RRBS_trito_pool_1_TAAGGCGA.ATCGAC', 'RRBS_trito_pool_1_TAAGGCGA.CAAGAG', 'RRBS_trito_pool_1_TAAGGCGA.CATGAC', 'RRBS_trito_pool_1_TAAGGCGA.CCTTCG', 'RRBS_trito_pool_1_TAAGGCGA.CGGTAG', 'RRBS_trito_pool_1_TAAGGCGA.CTATTG', 'RRBS_trito_pool_1_TAAGGCGA.GACACG', 'RRBS_trito_pool_1_TAAGGCGA.GCATTC', 'RRBS_trito_pool_1_TAAGGCGA.GCTGCC', 'RRBS_trito_pool_1_TAAGGCGA.GGCATC', 'RRBS_trito_pool_1_TAAGGCGA.GTGAGG', 'RRBS_trito_pool_1_TAAGGCGA.GTTGAG', 'RRBS_trito_pool_1_TAAGGCGA.TAGCGG', 'RRBS_trito_pool_1_TAAGGCGA.TATCTC', 'RRBS_trito_pool_1_TAAGGCGA.TCTCTG', 'RRBS_trito_pool_1_TAAGGCGA.TGACAG', 'RRBS_trito_pool_1_TAAGGCGA.TGCTGC', 'RRBS_trito_pool_2_CGTACTAG.ACAACC', 'RRBS_trito_pool_2_CGTACTAG.ACGTGG', 'RRBS_trito_pool_2_CGTACTAG.ACTCAC', 'RRBS_trito_pool_2_CGTACTAG.AGGATG', 'RRBS_trito_pool_2_CGTACTAG.ATAGCG', 'RRBS_trito_pool_2_CGTACTAG.ATCGAC', 'RRBS_trito_pool_2_CGTACTAG.CAAGAG', 'RRBS_trito_pool_2_CGTACTAG.CATGAC', 'RRBS_trito_pool_2_CGTACTAG.CCTTCG', 'RRBS_trito_pool_2_CGTACTAG.CGGTAG', 'RRBS_trito_pool_2_CGTACTAG.CTATTG', 'RRBS_trito_pool_2_CGTACTAG.GACACG', 'RRBS_trito_pool_2_CGTACTAG.GCATTC', 'RRBS_trito_pool_2_CGTACTAG.GCTGCC', 'RRBS_trito_pool_2_CGTACTAG.GGCATC', 'RRBS_trito_pool_2_CGTACTAG.GTGAGG', 'RRBS_trito_pool_2_CGTACTAG.GTTGAG', 'RRBS_trito_pool_2_CGTACTAG.TAGCGG', 'RRBS_trito_pool_2_CGTACTAG.TATCTC', 'RRBS_trito_pool_2_CGTACTAG.TCTCTG', 'RRBS_trito_pool_2_CGTACTAG.TGACAG'] print(total_matrix.shape) total_matrix = total_matrix.applymap(lambda x: int(x) if pd.notnull(x) else str("?")) total_matrix = total_matrix.astype(str).apply(''.join) tott = pd.Series(total_matrix.index.astype(str).str.cat(total_matrix.astype(str),' ')) tott.to_csv("total_chrom11.phy", header=None, index=None) print(tott.shape)
mit
annayqho/TheCannon
code/lamost/mass_age/paper_plots/plot_survey_coverage.py
1
5792
#!/usr/bin/env python import numpy as np import healpy as hp import astropy.table as Table import matplotlib.pyplot as plt from matplotlib import cm from matplotlib import rc from matplotlib import rcParams from matplotlib.colors import LogNorm plt.rc('text', usetex=True) plt.rc('font', family='serif') import pyfits print("Import data") # import the data hdulist = pyfits.open( "/Users/annaho/Data/LAMOST/Mass_And_Age/catalog_paper.fits") tbdata = hdulist[1].data # # cols = hdulist[1].columns # # cols.names in_martig_range = tbdata.field("in_martig_range") snr = tbdata.field("snr") #choose = np.logical_and(in_martig_range, snr > 80) choose = in_martig_range print(sum(choose)) chisq = tbdata.field("chisq") ra_lamost = tbdata.field('ra')[choose] dec_lamost = tbdata.field('dec')[choose] val_lamost = 10**(tbdata.field("cannon_age")[choose]) hdulist.close() print("Getting APOGEE data") hdulist = pyfits.open( "/Users/annaho/Data/APOGEE/Ness2016_Catalog_Full_DR12_Info.fits") tbdata = hdulist[1].data ra_apogee_all = tbdata['RA'] dec_apogee_all = tbdata['DEC'] val_apogee_all = np.exp(tbdata['lnAge']) good_coords = np.logical_and(ra_apogee_all > -90, dec_apogee_all > -90) good = np.logical_and(good_coords, val_apogee_all > -90) ra_apogee = ra_apogee_all[good] dec_apogee = dec_apogee_all[good] val_apogee = val_apogee_all[good] hdulist.close() ra_both = np.hstack((ra_apogee, ra_lamost)) dec_both = np.hstack((dec_apogee, dec_lamost)) val_all = np.hstack((val_apogee, val_lamost)) print("create grid") # create a RA and Dec grid ra_all = [] dec_all = [] for ra in np.arange(0, 360, 0.5): for dec in np.arange(-90, 90, 0.5): ra_all.append(ra) dec_all.append(dec) ra = np.array(ra_all) dec = np.array(dec_all) # convert RA and Dec to phi and theta coordinates def toPhiTheta(ra, dec): phi = ra * np.pi/180. theta = (90.0 - dec) * np.pi / 180. return phi, theta phi, theta = toPhiTheta(ra, dec) phi_lamost, theta_lamost = toPhiTheta(ra_lamost, dec_lamost) phi_apogee, theta_apogee = toPhiTheta(ra_apogee, dec_apogee) phi_all, theta_all = toPhiTheta(ra_both, dec_both) # to just plot all points, do #hp.visufunc.projplot(theta, phi, 'bo') #hp.visufunc.projplot(theta_lamost, phi_lamost, 'bo') #hp.visufunc.graticule() # just the bare background w/ lines # more examples are here # https://healpy.readthedocs.org/en/latest/generated/healpy.visufunc.projplot.html#healpy.visufunc.projplot ## to plot a 2D histogram in the Mollweide projection # define the HEALPIX level # NSIDE = 32 # defines the resolution of the map # NSIDE = 128 # from paper 1 NSIDE = 64 # find the pixel ID for each point # pix = hp.pixelfunc.ang2pix(NSIDE, theta, phi) pix_lamost = hp.pixelfunc.ang2pix(NSIDE, theta_lamost, phi_lamost) pix_apogee = hp.pixelfunc.ang2pix(NSIDE, theta_apogee, phi_apogee) pix_all = hp.pixelfunc.ang2pix(NSIDE, theta_all, phi_all) # pix is in the order of ra and dec # prepare the map array m_lamost = hp.ma(np.zeros(hp.nside2npix(NSIDE), dtype='float')) mask_lamost = np.zeros(hp.nside2npix(NSIDE), dtype='bool') for pix_val in np.unique(pix_lamost): choose = np.where(pix_lamost==pix_val)[0] if len(choose) == 1: # #m_lamost[pix_val] = rmag_lamost[choose[0]] m_lamost[pix_val] = val_lamost[choose[0]] else: #m_lamost[pix_val] = np.median(rmag_lamost[choose]) m_lamost[pix_val] = np.median(val_lamost[choose]) mask_lamost[np.setdiff1d(np.arange(len(m_lamost)), pix_lamost)] = 1 m_lamost.mask = mask_lamost m_apogee= hp.ma(np.zeros(hp.nside2npix(NSIDE), dtype='float')) mask_apogee= np.zeros(hp.nside2npix(NSIDE), dtype='bool') for pix_val in np.unique(pix_apogee): choose = np.where(pix_apogee==pix_val)[0] if len(choose) == 1: m_apogee[pix_val] = val_apogee[choose[0]] else: m_apogee[pix_val] = np.median(val_apogee[choose]) mask_apogee[np.setdiff1d(np.arange(len(m_apogee)), pix_apogee)] = 1 m_apogee.mask = mask_apogee m_all = hp.ma(np.zeros(hp.nside2npix(NSIDE), dtype='float')) mask_all= np.zeros(hp.nside2npix(NSIDE), dtype='bool') for pix_val in np.unique(pix_all): choose = np.where(pix_all==pix_val)[0] if len(choose) == 1: m_all[pix_val] = val_all[choose[0]] else: m_all[pix_val] = np.median(val_all[choose]) mask_all[np.setdiff1d(np.arange(len(m_all)), pix_all)] = 1 m_all.mask = mask_all # perceptually uniform: inferno, viridis, plasma, magma #cmap=cm.magma cmap = cm.RdYlBu_r cmap.set_under('w') # composite map # plot map ('C' means the input coordinates were in the equatorial system) # rcParams.update({'font.size':16}) hp.visufunc.mollview(m_apogee, coord=['C','G'], rot=(150, 0, 0), flip='astro', notext=False, title=r'Ages from Ness et al. 2016 (APOGEE)', cbar=True, norm=None, min=0, max=12, cmap=cmap, unit = 'Gyr') #hp.visufunc.mollview(m_lamost, coord=['C','G'], rot=(150, 0, 0), flip='astro', # notext=True, title=r'$\alpha$/M for 500,000 LAMOST giants', cbar=True, # norm=None, min=-0.07, max=0.3, cmap=cmap, unit = r'$\alpha$/M [dex]') #notext=True, title="r-band magnitude for 500,000 LAMOST giants", cbar=True, #norm=None, min=11, max=17, cmap=cmap, unit = r"r-band magnitude [mag]") # hp.visufunc.mollview(m_all, coord=['C','G'], rot=(150, 0, 0), flip='astro', # notext=True, title='Ages from Ness et al. 2016 + LAMOST giants', # cbar=True, norm=None, min=0.00, max=12, cmap=cmap, unit = 'Gyr') hp.visufunc.graticule() plt.show() #plt.savefig("full_age_map.png") #plt.savefig("apogee_age_map.png") #plt.savefig("lamost_am_map_magma.png") #plt.savefig("lamost_rmag_map.png")
mit
QUANTAXIS/QUANTAXIS
QUANTAXIS/QAApplication/OldBacktest.py
2
3257
# @Hakase import QUANTAXIS as QA import numpy as np import pandas as pd import datetime import sys import random class backtest(): """依据回测场景的建模 """ def __init__(self, start_time='2015-01-01', end_time='2018-09-24', init_cash=500000, code='RBL8', frequence=QA.FREQUENCE.FIFTEEN_MIN): self.start_time = start_time self.end_time = end_time self.frequence = frequence self.code = code self.init_cach = init_cash self.time_ = None self.market_data_ = None self.res = False @property def position(self): return self.account.sell_available.get(self.code, 0) @property def time(self): return self.time_ @property def market_data(self): return self.market_data_ # 自定义函数------------------------------------------------------------------- @property def hold_judge(self): """仓位判断器 Returns: [type] -- [description] """ if self.account.cash/self.account.init_cash < 0.3: return False else: return True def before_backtest(self): raise NotImplementedError def before(self, *args, **kwargs): self.before_backtest() self.data_min = QA.QA_fetch_future_min_adv( self.code, self.start_time, self.end_time, frequence=self.frequence) self.data_day = QA.QA_fetch_future_day_adv( self.code, self.start_time, self.end_time) self.Broker = QA.QA_BacktestBroker() def model(self, *arg, **kwargs): raise NotImplementedError def load_strategy(self, *arg, **kwargs): # self.load_model(func1) raise NotImplementedError def run(self, *arg, **kwargs): raise NotImplementedError def buy(self, pos, towards): self.account.receive_simpledeal(code=self.code, trade_price=self.market_data.open, trade_amount=pos, trade_towards=towards, trade_time=self.time, message=towards) def sell(self, pos, towards): self.account.receive_simpledeal(code=self.code, trade_price=self.market_data.open, trade_amount=pos, trade_towards=towards, trade_time=self.time, message=towards) def main(self, *arg, **kwargs): print(vars(self)) self.identity_code = '_'.join([str(x) for x in list(kwargs.values())]) self.backtest_cookie = 'future_{}_{}'.format( datetime.datetime.now().time().__str__()[:8], self.identity_code) self.account = QA.QA_Account(allow_sellopen=True, allow_t0=True, account_cookie=self.backtest_cookie, market_type=QA.MARKET_TYPE.FUTURE_CN, frequence=self.frequence, init_cash=self.init_cash) self.gen = self.data_min.reindex( self.res) if self.res else self.data_min for ind, item in self.gen.iterrows: self.time_ = ind[0] self.code = ind[1] self.market_data_ = item self.run()
mit
bartosh/zipline
tests/data/test_resample.py
4
33653
# 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 collections import OrderedDict from numbers import Real from nose_parameterized import parameterized from numpy.testing import assert_almost_equal from numpy import nan, array, full, isnan import pandas as pd from pandas import DataFrame from six import iteritems from zipline.data.resample import ( minute_frame_to_session_frame, DailyHistoryAggregator, MinuteResampleSessionBarReader, ReindexMinuteBarReader, ReindexSessionBarReader, ) from zipline.testing import parameter_space from zipline.testing.fixtures import ( WithEquityMinuteBarData, WithBcolzEquityMinuteBarReader, WithBcolzEquityDailyBarReader, WithBcolzFutureMinuteBarReader, ZiplineTestCase, ) OHLC = ['open', 'high', 'low', 'close'] OHLCV = OHLC + ['volume'] NYSE_MINUTES = OrderedDict(( ('day_0_front', pd.date_range('2016-03-15 9:31', '2016-03-15 9:33', freq='min', tz='US/Eastern').tz_convert('UTC')), ('day_0_back', pd.date_range('2016-03-15 15:58', '2016-03-15 16:00', freq='min', tz='US/Eastern').tz_convert('UTC')), ('day_1_front', pd.date_range('2016-03-16 9:31', '2016-03-16 9:33', freq='min', tz='US/Eastern').tz_convert('UTC')), ('day_1_back', pd.date_range('2016-03-16 15:58', '2016-03-16 16:00', freq='min', tz='US/Eastern').tz_convert('UTC')), )) FUT_MINUTES = OrderedDict(( ('day_0_front', pd.date_range('2016-03-15 18:01', '2016-03-15 18:03', freq='min', tz='US/Eastern').tz_convert('UTC')), ('day_0_back', pd.date_range('2016-03-16 17:58', '2016-03-16 18:00', freq='min', tz='US/Eastern').tz_convert('UTC')), ('day_1_front', pd.date_range('2016-03-16 18:01', '2016-03-16 18:03', freq='min', tz='US/Eastern').tz_convert('UTC')), ('day_1_back', pd.date_range('2016-03-17 17:58', '2016-03-17 18:00', freq='min', tz='US/Eastern').tz_convert('UTC')), )) SCENARIOS = OrderedDict(( ('none_missing', array([ [101.5, 101.9, 101.1, 101.3, 1001], [103.5, 103.9, 103.1, 103.3, 1003], [102.5, 102.9, 102.1, 102.3, 1002], ])), ('all_missing', array([ [nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], ])), ('missing_first', array([ [nan, nan, nan, nan, 0], [103.5, 103.9, 103.1, 103.3, 1003], [102.5, 102.9, 102.1, 102.3, 1002], ])), ('missing_last', array([ [107.5, 107.9, 107.1, 107.3, 1007], [108.5, 108.9, 108.1, 108.3, 1008], [nan, nan, nan, nan, 0], ])), ('missing_middle', array([ [103.5, 103.9, 103.1, 103.3, 1003], [nan, nan, nan, nan, 0], [102.5, 102.5, 102.1, 102.3, 1002], ])), )) OHLCV = ('open', 'high', 'low', 'close', 'volume') _EQUITY_CASES = ( (1, (('none_missing', 'day_0_front'), ('missing_last', 'day_0_back'))), (2, (('missing_first', 'day_0_front'), ('none_missing', 'day_0_back'))), (3, (('missing_last', 'day_0_back'), ('missing_first', 'day_1_front'))), # Asset 4 has a start date on day 1 (4, (('all_missing', 'day_0_back'), ('none_missing', 'day_1_front'))), # Asset 5 has a start date before day_0, but does not have data on that # day. (5, (('all_missing', 'day_0_back'), ('none_missing', 'day_1_front'))), ) EQUITY_CASES = OrderedDict() for sid, combos in _EQUITY_CASES: frames = [DataFrame(SCENARIOS[s], columns=OHLCV). set_index(NYSE_MINUTES[m]) for s, m in combos] EQUITY_CASES[sid] = pd.concat(frames) _FUTURE_CASES = ( (1001, (('none_missing', 'day_0_front'), ('none_missing', 'day_0_back'))), (1002, (('missing_first', 'day_0_front'), ('none_missing', 'day_0_back'))), (1003, (('missing_last', 'day_0_back'), ('missing_first', 'day_1_front'))), (1004, (('all_missing', 'day_0_back'), ('none_missing', 'day_1_front'))), ) FUTURE_CASES = OrderedDict() for sid, combos in _FUTURE_CASES: frames = [DataFrame(SCENARIOS[s], columns=OHLCV). set_index(FUT_MINUTES[m]) for s, m in combos] FUTURE_CASES[sid] = pd.concat(frames) EXPECTED_AGGREGATION = { 1: DataFrame({ 'open': [101.5, 101.5, 101.5, 101.5, 101.5, 101.5], 'high': [101.9, 103.9, 103.9, 107.9, 108.9, 108.9], 'low': [101.1, 101.1, 101.1, 101.1, 101.1, 101.1], 'close': [101.3, 103.3, 102.3, 107.3, 108.3, 108.3], 'volume': [1001, 2004, 3006, 4013, 5021, 5021], }, columns=OHLCV), 2: DataFrame({ 'open': [nan, 103.5, 103.5, 103.5, 103.5, 103.5], 'high': [nan, 103.9, 103.9, 103.9, 103.9, 103.9], 'low': [nan, 103.1, 102.1, 101.1, 101.1, 101.1], 'close': [nan, 103.3, 102.3, 101.3, 103.3, 102.3], 'volume': [0, 1003, 2005, 3006, 4009, 5011], }, columns=OHLCV), # Equity 3 straddles two days. 3: DataFrame({ 'open': [107.5, 107.5, 107.5, nan, 103.5, 103.5], 'high': [107.9, 108.9, 108.9, nan, 103.9, 103.9], 'low': [107.1, 107.1, 107.1, nan, 103.1, 102.1], 'close': [107.3, 108.3, 108.3, nan, 103.3, 102.3], 'volume': [1007, 2015, 2015, 0, 1003, 2005], }, columns=OHLCV), # Equity 4 straddles two days and is not active the first day. 4: DataFrame({ 'open': [nan, nan, nan, 101.5, 101.5, 101.5], 'high': [nan, nan, nan, 101.9, 103.9, 103.9], 'low': [nan, nan, nan, 101.1, 101.1, 101.1], 'close': [nan, nan, nan, 101.3, 103.3, 102.3], 'volume': [0, 0, 0, 1001, 2004, 3006], }, columns=OHLCV), # Equity 5 straddles two days and does not have data the first day. 5: DataFrame({ 'open': [nan, nan, nan, 101.5, 101.5, 101.5], 'high': [nan, nan, nan, 101.9, 103.9, 103.9], 'low': [nan, nan, nan, 101.1, 101.1, 101.1], 'close': [nan, nan, nan, 101.3, 103.3, 102.3], 'volume': [0, 0, 0, 1001, 2004, 3006], }, columns=OHLCV), 1001: DataFrame({ 'open': [101.5, 101.5, 101.5, 101.5, 101.5, 101.5], 'high': [101.9, 103.9, 103.9, 103.9, 103.9, 103.9], 'low': [101.1, 101.1, 101.1, 101.1, 101.1, 101.1], 'close': [101.3, 103.3, 102.3, 101.3, 103.3, 102.3], 'volume': [1001, 2004, 3006, 4007, 5010, 6012], }, columns=OHLCV), 1002: DataFrame({ 'open': [nan, 103.5, 103.5, 103.5, 103.5, 103.5], 'high': [nan, 103.9, 103.9, 103.9, 103.9, 103.9], 'low': [nan, 103.1, 102.1, 101.1, 101.1, 101.1], 'close': [nan, 103.3, 102.3, 101.3, 103.3, 102.3], 'volume': [0, 1003, 2005, 3006, 4009, 5011], }, columns=OHLCV), 1003: DataFrame({ 'open': [107.5, 107.5, 107.5, nan, 103.5, 103.5], 'high': [107.9, 108.9, 108.9, nan, 103.9, 103.9], 'low': [107.1, 107.1, 107.1, nan, 103.1, 102.1], 'close': [107.3, 108.3, 108.3, nan, 103.3, 102.3], 'volume': [1007, 2015, 2015, 0, 1003, 2005], }, columns=OHLCV), 1004: DataFrame({ 'open': [nan, nan, nan, 101.5, 101.5, 101.5], 'high': [nan, nan, nan, 101.9, 103.9, 103.9], 'low': [nan, nan, nan, 101.1, 101.1, 101.1], 'close': [nan, nan, nan, 101.3, 103.3, 102.3], 'volume': [0, 0, 0, 1001, 2004, 3006], }, columns=OHLCV), } EXPECTED_SESSIONS = { 1: DataFrame([EXPECTED_AGGREGATION[1].iloc[-1].values], columns=OHLCV, index=pd.to_datetime(['2016-03-15'], utc=True)), 2: DataFrame([EXPECTED_AGGREGATION[2].iloc[-1].values], columns=OHLCV, index=pd.to_datetime(['2016-03-15'], utc=True)), 3: DataFrame(EXPECTED_AGGREGATION[3].iloc[[2, 5]].values, columns=OHLCV, index=pd.to_datetime(['2016-03-15', '2016-03-16'], utc=True)), 1001: DataFrame([EXPECTED_AGGREGATION[1001].iloc[-1].values], columns=OHLCV, index=pd.to_datetime(['2016-03-16'], utc=True)), 1002: DataFrame([EXPECTED_AGGREGATION[1002].iloc[-1].values], columns=OHLCV, index=pd.to_datetime(['2016-03-16'], utc=True)), 1003: DataFrame(EXPECTED_AGGREGATION[1003].iloc[[2, 5]].values, columns=OHLCV, index=pd.to_datetime(['2016-03-16', '2016-03-17'], utc=True)), 1004: DataFrame(EXPECTED_AGGREGATION[1004].iloc[[2, 5]].values, columns=OHLCV, index=pd.to_datetime(['2016-03-16', '2016-03-17'], utc=True)), } class MinuteToDailyAggregationTestCase(WithBcolzEquityMinuteBarReader, WithBcolzFutureMinuteBarReader, ZiplineTestCase): # March 2016 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 # 6 7 8 9 10 11 12 # 13 14 15 16 17 18 19 # 20 21 22 23 24 25 26 # 27 28 29 30 31 TRADING_ENV_MIN_DATE = START_DATE = pd.Timestamp( '2016-03-01', tz='UTC', ) TRADING_ENV_MAX_DATE = END_DATE = pd.Timestamp( '2016-03-31', tz='UTC', ) TRADING_CALENDAR_STRS = ('NYSE', 'us_futures') ASSET_FINDER_EQUITY_SIDS = 1, 2, 3, 4, 5 ASSET_FINDER_FUTURE_SIDS = 1001, 1002, 1003, 1004 @classmethod def make_equity_info(cls): frame = super(MinuteToDailyAggregationTestCase, cls).make_equity_info() # Make equity 4 start a day behind the data start to exercise assets # which not alive for the session. frame.loc[[4], 'start_date'] = pd.Timestamp('2016-03-16', tz='UTC') return frame @classmethod def make_equity_minute_bar_data(cls): for sid in cls.ASSET_FINDER_EQUITY_SIDS: frame = EQUITY_CASES[sid] yield sid, frame @classmethod def make_futures_info(cls): future_dict = {} for future_sid in cls.ASSET_FINDER_FUTURE_SIDS: future_dict[future_sid] = { 'multiplier': 1000, 'exchange': 'CME', 'root_symbol': "ABC" } return pd.DataFrame.from_dict(future_dict, orient='index') @classmethod def make_future_minute_bar_data(cls): for sid in cls.ASSET_FINDER_FUTURE_SIDS: frame = FUTURE_CASES[sid] yield sid, frame def init_instance_fixtures(self): super(MinuteToDailyAggregationTestCase, self).init_instance_fixtures() # Set up a fresh data portal for each test, since order of calling # needs to be tested. self.equity_daily_aggregator = DailyHistoryAggregator( self.nyse_calendar.schedule.market_open, self.bcolz_equity_minute_bar_reader, self.nyse_calendar, ) self.future_daily_aggregator = DailyHistoryAggregator( self.us_futures_calendar.schedule.market_open, self.bcolz_future_minute_bar_reader, self.us_futures_calendar ) @parameter_space( field=OHLCV, sid=ASSET_FINDER_EQUITY_SIDS, __fail_fast=True, ) def test_equity_contiguous_minutes_individual(self, field, sid): asset = self.asset_finder.retrieve_asset(sid) minutes = EQUITY_CASES[asset].index self._test_contiguous_minutes_individual( field, asset, minutes, self.equity_daily_aggregator, ) @parameter_space( field=OHLCV, sid=ASSET_FINDER_FUTURE_SIDS, __fail_fast=True, ) def test_future_contiguous_minutes_individual(self, field, sid): asset = self.asset_finder.retrieve_asset(sid) minutes = FUTURE_CASES[asset].index self._test_contiguous_minutes_individual( field, asset, minutes, self.future_daily_aggregator, ) def _test_contiguous_minutes_individual( self, field, asset, minutes, aggregator, ): # First test each minute in order. method_name = field + 's' results = [] repeat_results = [] for minute in minutes: value = getattr(aggregator, method_name)( [asset], minute)[0] # Prevent regression on building an array when scalar is intended. self.assertIsInstance(value, Real) results.append(value) # Call a second time with the same dt, to prevent regression # against case where crossed start and end dts caused a crash # instead of the last value. value = getattr(aggregator, method_name)( [asset], minute)[0] # Prevent regression on building an array when scalar is intended. self.assertIsInstance(value, Real) repeat_results.append(value) assert_almost_equal(results, EXPECTED_AGGREGATION[asset][field], err_msg='sid={0} field={1}'.format(asset, field)) assert_almost_equal(repeat_results, EXPECTED_AGGREGATION[asset][field], err_msg='sid={0} field={1}'.format(asset, field)) @parameterized.expand([ ('open_sid_1', 'open', 1), ('high_1', 'high', 1), ('low_1', 'low', 1), ('close_1', 'close', 1), ('volume_1', 'volume', 1), ('open_2', 'open', 2), ('high_2', 'high', 2), ('low_2', 'low', 2), ('close_2', 'close', 2), ('volume_2', 'volume', 2), ('open_3', 'open', 3), ('high_3', 'high', 3), ('low_3', 'low', 3), ('close_3', 'close', 3), ('volume_3', 'volume', 3), ('open_4', 'open', 4), ('high_4', 'high', 4), ('low_4', 'low', 4), ('close_4', 'close', 4), ('volume_4', 'volume', 4), ('open_5', 'open', 5), ('high_5', 'high', 5), ('low_5', 'low', 5), ('close_5', 'close', 5), ('volume_5', 'volume', 5), ]) def test_skip_minutes_individual(self, name, field, sid): # Test skipping minutes, to exercise backfills. # Tests initial backfill and mid day backfill. method_name = field + 's' asset = self.asset_finder.retrieve_asset(sid) minutes = EQUITY_CASES[asset].index for i in [0, 2, 3, 5]: minute = minutes[i] value = getattr(self.equity_daily_aggregator, method_name)( [asset], minute)[0] # Prevent regression on building an array when scalar is intended. self.assertIsInstance(value, Real) assert_almost_equal(value, EXPECTED_AGGREGATION[sid][field][i], err_msg='sid={0} field={1} dt={2}'.format( sid, field, minute)) # Call a second time with the same dt, to prevent regression # against case where crossed start and end dts caused a crash # instead of the last value. value = getattr(self.equity_daily_aggregator, method_name)( [asset], minute)[0] # Prevent regression on building an array when scalar is intended. self.assertIsInstance(value, Real) assert_almost_equal(value, EXPECTED_AGGREGATION[sid][field][i], err_msg='sid={0} field={1} dt={2}'.format( sid, field, minute)) @parameterized.expand(OHLCV) def test_contiguous_minutes_multiple(self, field): # First test each minute in order. method_name = field + 's' assets = self.asset_finder.retrieve_all([1, 2]) results = {asset: [] for asset in assets} repeat_results = {asset: [] for asset in assets} minutes = EQUITY_CASES[1].index for minute in minutes: values = getattr(self.equity_daily_aggregator, method_name)( assets, minute) for j, asset in enumerate(assets): value = values[j] # Prevent regression on building an array when scalar is # intended. self.assertIsInstance(value, Real) results[asset].append(value) # Call a second time with the same dt, to prevent regression # against case where crossed start and end dts caused a crash # instead of the last value. values = getattr(self.equity_daily_aggregator, method_name)( assets, minute) for j, asset in enumerate(assets): value = values[j] # Prevent regression on building an array when scalar is # intended. self.assertIsInstance(value, Real) repeat_results[asset].append(value) for asset in assets: assert_almost_equal(results[asset], EXPECTED_AGGREGATION[asset][field], err_msg='sid={0} field={1}'.format( asset, field)) assert_almost_equal(repeat_results[asset], EXPECTED_AGGREGATION[asset][field], err_msg='sid={0} field={1}'.format( asset, field)) @parameterized.expand(OHLCV) def test_skip_minutes_multiple(self, field): # Test skipping minutes, to exercise backfills. # Tests initial backfill and mid day backfill. method_name = field + 's' assets = self.asset_finder.retrieve_all([1, 2]) minutes = EQUITY_CASES[1].index for i in [1, 5]: minute = minutes[i] values = getattr(self.equity_daily_aggregator, method_name)( assets, minute) for j, asset in enumerate(assets): value = values[j] # Prevent regression on building an array when scalar is # intended. self.assertIsInstance(value, Real) assert_almost_equal( value, EXPECTED_AGGREGATION[asset][field][i], err_msg='sid={0} field={1} dt={2}'.format( asset, field, minute)) # Call a second time with the same dt, to prevent regression # against case where crossed start and end dts caused a crash # instead of the last value. values = getattr(self.equity_daily_aggregator, method_name)( assets, minute) for j, asset in enumerate(assets): value = values[j] # Prevent regression on building an array when scalar is # intended. self.assertIsInstance(value, Real) assert_almost_equal( value, EXPECTED_AGGREGATION[asset][field][i], err_msg='sid={0} field={1} dt={2}'.format( asset, field, minute)) class TestMinuteToSession(WithEquityMinuteBarData, ZiplineTestCase): # March 2016 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 # 6 7 8 9 10 11 12 # 13 14 15 16 17 18 19 # 20 21 22 23 24 25 26 # 27 28 29 30 31 START_DATE = pd.Timestamp( '2016-03-15', tz='UTC', ) END_DATE = pd.Timestamp( '2016-03-15', tz='UTC', ) ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 @classmethod def make_equity_minute_bar_data(cls): for sid, frame in iteritems(EQUITY_CASES): yield sid, frame @classmethod def init_class_fixtures(cls): super(TestMinuteToSession, cls).init_class_fixtures() cls.equity_frames = { sid: frame for sid, frame in cls.make_equity_minute_bar_data()} def test_minute_to_session(self): for sid in self.ASSET_FINDER_EQUITY_SIDS: frame = self.equity_frames[sid] expected = EXPECTED_SESSIONS[sid] result = minute_frame_to_session_frame(frame, self.nyse_calendar) assert_almost_equal(expected.values, result.values, err_msg='sid={0}'.format(sid)) class TestResampleSessionBars(WithBcolzFutureMinuteBarReader, ZiplineTestCase): TRADING_CALENDAR_STRS = ('us_futures',) TRADING_CALENDAR_PRIMARY_CAL = 'us_futures' ASSET_FINDER_FUTURE_SIDS = 1001, 1002, 1003, 1004 START_DATE = pd.Timestamp('2016-03-16', tz='UTC') END_DATE = pd.Timestamp('2016-03-17', tz='UTC') NUM_SESSIONS = 2 @classmethod def make_futures_info(cls): future_dict = {} for future_sid in cls.ASSET_FINDER_FUTURE_SIDS: future_dict[future_sid] = { 'multiplier': 1000, 'exchange': 'CME', 'root_symbol': "ABC" } return pd.DataFrame.from_dict(future_dict, orient='index') @classmethod def make_future_minute_bar_data(cls): for sid in cls.ASSET_FINDER_FUTURE_SIDS: frame = FUTURE_CASES[sid] yield sid, frame def init_instance_fixtures(self): super(TestResampleSessionBars, self).init_instance_fixtures() self.session_bar_reader = MinuteResampleSessionBarReader( self.trading_calendar, self.bcolz_future_minute_bar_reader ) def test_resample(self): calendar = self.trading_calendar for sid in self.ASSET_FINDER_FUTURE_SIDS: case_frame = FUTURE_CASES[sid] first = calendar.minute_to_session_label( case_frame.index[0]) last = calendar.minute_to_session_label( case_frame.index[-1]) result = self.session_bar_reader.load_raw_arrays( OHLCV, first, last, [sid]) for i, field in enumerate(OHLCV): assert_almost_equal( EXPECTED_SESSIONS[sid][[field]], result[i], err_msg="sid={0} field={1}".format(sid, field)) def test_sessions(self): sessions = self.session_bar_reader.sessions self.assertEqual(self.NUM_SESSIONS, len(sessions)) self.assertEqual(self.START_DATE, sessions[0]) self.assertEqual(self.END_DATE, sessions[-1]) def test_last_available_dt(self): calendar = self.trading_calendar session_bar_reader = MinuteResampleSessionBarReader( calendar, self.bcolz_future_minute_bar_reader ) self.assertEqual(self.END_DATE, session_bar_reader.last_available_dt) def test_get_value(self): calendar = self.trading_calendar session_bar_reader = MinuteResampleSessionBarReader( calendar, self.bcolz_future_minute_bar_reader ) for sid in self.ASSET_FINDER_FUTURE_SIDS: expected = EXPECTED_SESSIONS[sid] for dt_str, values in expected.iterrows(): dt = pd.Timestamp(dt_str, tz='UTC') for col in OHLCV: result = session_bar_reader.get_value(sid, dt, col) assert_almost_equal(result, values[col], err_msg="sid={0} col={1} dt={2}". format(sid, col, dt)) def test_first_trading_day(self): self.assertEqual(self.START_DATE, self.session_bar_reader.first_trading_day) def test_get_last_traded_dt(self): future = self.asset_finder.retrieve_asset( self.ASSET_FINDER_FUTURE_SIDS[0] ) self.assertEqual( self.trading_calendar.previous_session_label(self.END_DATE), self.session_bar_reader.get_last_traded_dt(future, self.END_DATE) ) class TestReindexMinuteBars(WithBcolzEquityMinuteBarReader, ZiplineTestCase): TRADING_CALENDAR_STRS = ('us_futures', 'NYSE') TRADING_CALENDAR_PRIMARY_CAL = 'us_futures' ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 START_DATE = pd.Timestamp('2015-12-01', tz='UTC') END_DATE = pd.Timestamp('2015-12-31', tz='UTC') def test_load_raw_arrays(self): reindex_reader = ReindexMinuteBarReader( self.trading_calendar, self.bcolz_equity_minute_bar_reader, self.START_DATE, self.END_DATE, ) m_open, m_close = self.trading_calendar.open_and_close_for_session( self.START_DATE) outer_minutes = self.trading_calendar.minutes_in_range(m_open, m_close) result = reindex_reader.load_raw_arrays( OHLCV, m_open, m_close, [1, 2]) opens = DataFrame(data=result[0], index=outer_minutes, columns=[1, 2]) opens_with_price = opens.dropna() self.assertEqual( 1440, len(opens), "The result should have 1440 bars, the number of minutes in a " "trading session on the target calendar." ) self.assertEqual( 390, len(opens_with_price), "The result, after dropping nans, should have 390 bars, the " " number of bars in a trading session in the reader's calendar." ) slicer = outer_minutes.slice_indexer( end=pd.Timestamp('2015-12-01 14:30', tz='UTC')) assert_almost_equal( opens[1][slicer], full(slicer.stop, nan), err_msg="All values before the NYSE market open should be nan.") slicer = outer_minutes.slice_indexer( start=pd.Timestamp('2015-12-01 21:01', tz='UTC')) assert_almost_equal( opens[1][slicer], full(slicer.stop - slicer.start, nan), err_msg="All values after the NYSE market close should be nan.") first_minute_loc = outer_minutes.get_loc(pd.Timestamp( '2015-12-01 14:31', tz='UTC')) # Spot check a value. # The value is the autogenerated value from test fixtures. assert_almost_equal( 10.0, opens[1][first_minute_loc], err_msg="The value for Equity 1, should be 10.0, at NYSE open.") class TestReindexSessionBars(WithBcolzEquityDailyBarReader, ZiplineTestCase): TRADING_CALENDAR_STRS = ('us_futures', 'NYSE') TRADING_CALENDAR_PRIMARY_CAL = 'us_futures' ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 # Dates are chosen to span Thanksgiving, which is not a Holiday on # us_futures. START_DATE = pd.Timestamp('2015-11-02', tz='UTC') END_DATE = pd.Timestamp('2015-11-30', tz='UTC') # November 2015 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 6 7 # 8 9 10 11 12 13 14 # 15 16 17 18 19 20 21 # 22 23 24 25 26 27 28 # 29 30 def init_instance_fixtures(self): super(TestReindexSessionBars, self).init_instance_fixtures() self.reader = ReindexSessionBarReader( self.trading_calendar, self.bcolz_equity_daily_bar_reader, self.START_DATE, self.END_DATE, ) def test_load_raw_arrays(self): outer_sessions = self.trading_calendar.sessions_in_range( self.START_DATE, self.END_DATE) result = self.reader.load_raw_arrays( OHLCV, self.START_DATE, self.END_DATE, [1, 2]) opens = DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) opens_with_price = opens.dropna() self.assertEqual( 21, len(opens), "The reindexed result should have 21 days, which is the number of " "business days in 2015-11") self.assertEqual( 20, len(opens_with_price), "The reindexed result after dropping nans should have 20 days, " "because Thanksgiving is a NYSE holiday.") tday = pd.Timestamp('2015-11-26', tz='UTC') # Thanksgiving, 2015-11-26. # Is a holiday in NYSE, but not in us_futures. tday_loc = outer_sessions.get_loc(tday) assert_almost_equal( nan, opens[1][tday_loc], err_msg="2015-11-26 should be `nan`, since Thanksgiving is a " "holiday in the reader's calendar.") # Thanksgiving, 2015-11-26. # Is a holiday in NYSE, but not in us_futures. tday_loc = outer_sessions.get_loc(pd.Timestamp('2015-11-26', tz='UTC')) assert_almost_equal( nan, opens[1][tday_loc], err_msg="2015-11-26 should be `nan`, since Thanksgiving is a " "holiday in the reader's calendar.") def test_load_raw_arrays_holiday_start(self): tday = pd.Timestamp('2015-11-26', tz='UTC') outer_sessions = self.trading_calendar.sessions_in_range( tday, self.END_DATE) result = self.reader.load_raw_arrays( OHLCV, tday, self.END_DATE, [1, 2]) opens = DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) opens_with_price = opens.dropna() self.assertEqual( 3, len(opens), "The reindexed result should have 3 days, which is the number of " "business days in from Thanksgiving to end of 2015-11.") self.assertEqual( 2, len(opens_with_price), "The reindexed result after dropping nans should have 2 days, " "because Thanksgiving is a NYSE holiday.") def test_load_raw_arrays_holiday_end(self): tday = pd.Timestamp('2015-11-26', tz='UTC') outer_sessions = self.trading_calendar.sessions_in_range( self.START_DATE, tday) result = self.reader.load_raw_arrays( OHLCV, self.START_DATE, tday, [1, 2]) opens = DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) opens_with_price = opens.dropna() self.assertEqual( 19, len(opens), "The reindexed result should have 19 days, which is the number of " "business days in from start of 2015-11 up to Thanksgiving.") self.assertEqual( 18, len(opens_with_price), "The reindexed result after dropping nans should have 18 days, " "because Thanksgiving is a NYSE holiday.") def test_get_value(self): assert_almost_equal(self.reader.get_value(1, self.START_DATE, 'open'), 10.0, err_msg="The open of the fixture data on the " "first session should be 10.") tday = pd.Timestamp('2015-11-26', tz='UTC') self.assertTrue(isnan(self.reader.get_value(1, tday, 'close'))) self.assertEqual(self.reader.get_value(1, tday, 'volume'), 0) def test_last_availabe_dt(self): self.assertEqual(self.reader.last_available_dt, self.END_DATE) def test_get_last_traded_dt(self): asset = self.asset_finder.retrieve_asset(1) self.assertEqual(self.reader.get_last_traded_dt(asset, self.END_DATE), self.END_DATE) def test_sessions(self): sessions = self.reader.sessions self.assertEqual(21, len(sessions), "There should be 21 sessions in 2015-11.") self.assertEqual(pd.Timestamp('2015-11-02', tz='UTC'), sessions[0]) self.assertEqual(pd.Timestamp('2015-11-30', tz='UTC'), sessions[-1]) def test_first_trading_day(self): self.assertEqual(self.reader.first_trading_day, self.START_DATE) def test_trading_calendar(self): self.assertEqual('us_futures', self.reader.trading_calendar.name, "The calendar for the reindex reader should be the " "specified futures calendar.")
apache-2.0
shareactorIO/pipeline
source.ml/jupyterhub.ml/notebooks/zz_old/TensorFlow/SkFlow_DEPRECATED/text_classification_cnn.py
6
3587
# Copyright 2015-present Scikit Flow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np from sklearn import metrics import pandas import tensorflow as tf import skflow ### Training data # Download dbpedia_csv.tar.gz from # https://drive.google.com/folderview?id=0Bz8a_Dbh9Qhbfll6bVpmNUtUcFdjYmF2SEpmZUZUcVNiMUw1TWN6RDV3a0JHT3kxLVhVR2M # Unpack: tar -xvf dbpedia_csv.tar.gz train = pandas.read_csv('dbpedia_csv/train.csv', header=None) X_train, y_train = train[2], train[0] test = pandas.read_csv('dbpedia_csv/test.csv', header=None) X_test, y_test = test[2], test[0] ### Process vocabulary MAX_DOCUMENT_LENGTH = 100 vocab_processor = skflow.preprocessing.VocabularyProcessor(MAX_DOCUMENT_LENGTH) X_train = np.array(list(vocab_processor.fit_transform(X_train))) X_test = np.array(list(vocab_processor.transform(X_test))) n_words = len(vocab_processor.vocabulary_) print('Total words: %d' % n_words) ### Models EMBEDDING_SIZE = 20 N_FILTERS = 10 WINDOW_SIZE = 20 FILTER_SHAPE1 = [WINDOW_SIZE, EMBEDDING_SIZE] FILTER_SHAPE2 = [WINDOW_SIZE, N_FILTERS] POOLING_WINDOW = 4 POOLING_STRIDE = 2 def cnn_model(X, y): """2 layer Convolutional network to predict from sequence of words to a class.""" # Convert indexes of words into embeddings. # This creates embeddings matrix of [n_words, EMBEDDING_SIZE] and then # maps word indexes of the sequence into [batch_size, sequence_length, # EMBEDDING_SIZE]. word_vectors = skflow.ops.categorical_variable(X, n_classes=n_words, embedding_size=EMBEDDING_SIZE, name='words') word_vectors = tf.expand_dims(word_vectors, 3) with tf.variable_scope('CNN_Layer1'): # Apply Convolution filtering on input sequence. conv1 = skflow.ops.conv2d(word_vectors, N_FILTERS, FILTER_SHAPE1, padding='VALID') # Add a RELU for non linearity. conv1 = tf.nn.relu(conv1) # Max pooling across output of Convlution+Relu. pool1 = tf.nn.max_pool(conv1, ksize=[1, POOLING_WINDOW, 1, 1], strides=[1, POOLING_STRIDE, 1, 1], padding='SAME') # Transpose matrix so that n_filters from convolution becomes width. pool1 = tf.transpose(pool1, [0, 1, 3, 2]) with tf.variable_scope('CNN_Layer2'): # Second level of convolution filtering. conv2 = skflow.ops.conv2d(pool1, N_FILTERS, FILTER_SHAPE2, padding='VALID') # Max across each filter to get useful features for classification. pool2 = tf.squeeze(tf.reduce_max(conv2, 1), squeeze_dims=[1]) # Apply regular WX + B and classification. return skflow.models.logistic_regression(pool2, y) classifier = skflow.TensorFlowEstimator(model_fn=cnn_model, n_classes=15, steps=100, optimizer='Adam', learning_rate=0.01, continue_training=True) # Continuesly train for 1000 steps & predict on test set. while True: classifier.fit(X_train, y_train, logdir='/tmp/tf_examples/word_cnn') score = metrics.accuracy_score(y_test, classifier.predict(X_test)) print('Accuracy: {0:f}'.format(score))
apache-2.0
IntelLabs/hpat
examples/series/str/series_str_len.py
1
1782
# ***************************************************************************** # Copyright (c) 2020, Intel Corporation All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ***************************************************************************** import pandas as pd from numba import njit @njit def series_str_len(): series = pd.Series(['foo', 'bar', 'foobar']) # Series of 'foo', 'bar', 'foobar' out_series = series.str.len() return out_series # Expect series of 3, 3, 6 print(series_str_len())
bsd-2-clause
DougBurke/astropy
astropy/modeling/powerlaws.py
2
16066
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ Power law model variants """ from collections import OrderedDict import numpy as np from .core import Fittable1DModel from .parameters import Parameter, InputParameterError from ..units import Quantity __all__ = ['PowerLaw1D', 'BrokenPowerLaw1D', 'SmoothlyBrokenPowerLaw1D', 'ExponentialCutoffPowerLaw1D', 'LogParabola1D'] class PowerLaw1D(Fittable1DModel): """ One dimensional power law model. Parameters ---------- amplitude : float Model amplitude at the reference point x_0 : float Reference point alpha : float Power law index See Also -------- BrokenPowerLaw1D, ExponentialCutoffPowerLaw1D, LogParabola1D Notes ----- Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha` for ``alpha``): .. math:: f(x) = A (x / x_0) ^ {-\\alpha} """ amplitude = Parameter(default=1) x_0 = Parameter(default=1) alpha = Parameter(default=1) @staticmethod def evaluate(x, amplitude, x_0, alpha): """One dimensional power law model function""" xx = x / x_0 return amplitude * xx ** (-alpha) @staticmethod def fit_deriv(x, amplitude, x_0, alpha): """One dimensional power law derivative with respect to parameters""" xx = x / x_0 d_amplitude = xx ** (-alpha) d_x_0 = amplitude * alpha * d_amplitude / x_0 d_alpha = -amplitude * d_amplitude * np.log(xx) return [d_amplitude, d_x_0, d_alpha] @property def input_units(self): if self.x_0.unit is None: return None else: return {'x': self.x_0.unit} def _parameter_units_for_data_units(self, inputs_unit, outputs_unit): return OrderedDict([('x_0', inputs_unit['x']), ('amplitude', outputs_unit['y'])]) class BrokenPowerLaw1D(Fittable1DModel): """ One dimensional power law model with a break. Parameters ---------- amplitude : float Model amplitude at the break point. x_break : float Break point. alpha_1 : float Power law index for x < x_break. alpha_2 : float Power law index for x > x_break. See Also -------- PowerLaw1D, ExponentialCutoffPowerLaw1D, LogParabola1D Notes ----- Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha_1` for ``alpha_1`` and :math:`\\alpha_2` for ``alpha_2``): .. math:: f(x) = \\left \\{ \\begin{array}{ll} A (x / x_{break}) ^ {-\\alpha_1} & : x < x_{break} \\\\ A (x / x_{break}) ^ {-\\alpha_2} & : x > x_{break} \\\\ \\end{array} \\right. """ amplitude = Parameter(default=1) x_break = Parameter(default=1) alpha_1 = Parameter(default=1) alpha_2 = Parameter(default=1) @staticmethod def evaluate(x, amplitude, x_break, alpha_1, alpha_2): """One dimensional broken power law model function""" alpha = np.where(x < x_break, alpha_1, alpha_2) xx = x / x_break return amplitude * xx ** (-alpha) @staticmethod def fit_deriv(x, amplitude, x_break, alpha_1, alpha_2): """One dimensional broken power law derivative with respect to parameters""" alpha = np.where(x < x_break, alpha_1, alpha_2) xx = x / x_break d_amplitude = xx ** (-alpha) d_x_break = amplitude * alpha * d_amplitude / x_break d_alpha = -amplitude * d_amplitude * np.log(xx) d_alpha_1 = np.where(x < x_break, d_alpha, 0) d_alpha_2 = np.where(x >= x_break, d_alpha, 0) return [d_amplitude, d_x_break, d_alpha_1, d_alpha_2] @property def input_units(self): if self.x_break.unit is None: return None else: return {'x': self.x_break.unit} def _parameter_units_for_data_units(self, inputs_unit, outputs_unit): return OrderedDict([('x_break', inputs_unit['x']), ('amplitude', outputs_unit['y'])]) class SmoothlyBrokenPowerLaw1D(Fittable1DModel): """One dimensional smoothly broken power law model. Parameters ---------- amplitude : float Model amplitude at the break point. x_break : float Break point. alpha_1 : float Power law index for ``x << x_break``. alpha_2 : float Power law index for ``x >> x_break``. delta : float Smoothness parameter. See Also -------- BrokenPowerLaw1D Notes ----- Model formula (with :math:`A` for ``amplitude``, :math:`x_b` for ``x_break``, :math:`\\alpha_1` for ``alpha_1``, :math:`\\alpha_2` for ``alpha_2`` and :math:`\\Delta` for ``delta``): .. math:: f(x) = A \\left( \\frac{x}{x_b} \\right) ^ {-\\alpha_1} \\left\\{ \\frac{1}{2} \\left[ 1 + \\left( \\frac{x}{x_b}\\right)^{1 / \\Delta} \\right] \\right\\}^{(\\alpha_1 - \\alpha_2) \\Delta} The change of slope occurs between the values :math:`x_1` and :math:`x_2` such that: .. math:: \\log_{10} \\frac{x_2}{x_b} = \\log_{10} \\frac{x_b}{x_1} \\sim \\Delta At values :math:`x \\lesssim x_1` and :math:`x \\gtrsim x_2` the model is approximately a simple power law with index :math:`\\alpha_1` and :math:`\\alpha_2` respectively. The two power laws are smoothly joined at values :math:`x_1 < x < x_2`, hence the :math:`\\Delta` parameter sets the "smoothness" of the slope change. The ``delta`` parameter is bounded to values greater than 1e-3 (corresponding to :math:`x_2 / x_1 \\gtrsim 1.002`) to avoid overflow errors. The ``amplitude`` parameter is bounded to positive values since this model is typically used to represent positive quantities. Examples -------- .. plot:: :include-source: import numpy as np import matplotlib.pyplot as plt from astropy.modeling import models x = np.logspace(0.7, 2.3, 500) f = models.SmoothlyBrokenPowerLaw1D(amplitude=1, x_break=20, alpha_1=-2, alpha_2=2) plt.figure() plt.title("amplitude=1, x_break=20, alpha_1=-2, alpha_2=2") f.delta = 0.5 plt.loglog(x, f(x), '--', label='delta=0.5') f.delta = 0.3 plt.loglog(x, f(x), '-.', label='delta=0.3') f.delta = 0.1 plt.loglog(x, f(x), label='delta=0.1') plt.axis([x.min(), x.max(), 0.1, 1.1]) plt.legend(loc='lower center') plt.grid(True) plt.show() """ amplitude = Parameter(default=1, min=0) x_break = Parameter(default=1) alpha_1 = Parameter(default=-2) alpha_2 = Parameter(default=2) delta = Parameter(default=1, min=1.e-3) @amplitude.validator def amplitude(self, value): if np.any(value <= 0): raise InputParameterError( "amplitude parameter must be > 0") @delta.validator def delta(self, value): if np.any(value < 0.001): raise InputParameterError( "delta parameter must be >= 0.001") @staticmethod def evaluate(x, amplitude, x_break, alpha_1, alpha_2, delta): """One dimensional smoothly broken power law model function""" # Pre-calculate `x/x_b` xx = x / x_break # Initialize the return value f = np.zeros_like(xx, subok=False) if isinstance(amplitude, Quantity): return_unit = amplitude.unit amplitude = amplitude.value else: return_unit = None # The quantity `t = (x / x_b)^(1 / delta)` can become quite # large. To avoid overflow errors we will start by calculating # its natural logarithm: logt = np.log(xx) / delta # When `t >> 1` or `t << 1` we don't actually need to compute # the `t` value since the main formula (see docstring) can be # significantly simplified by neglecting `1` or `t` # respectively. In the following we will check whether `t` is # much greater, much smaller, or comparable to 1 by comparing # the `logt` value with an appropriate threshold. threshold = 30 # corresponding to exp(30) ~ 1e13 i = logt > threshold if (i.max()): # In this case the main formula reduces to a simple power # law with index `alpha_2`. f[i] = amplitude * xx[i] ** (-alpha_2) \ / (2. ** ((alpha_1 - alpha_2) * delta)) i = logt < -threshold if (i.max()): # In this case the main formula reduces to a simple power # law with index `alpha_1`. f[i] = amplitude * xx[i] ** (-alpha_1) \ / (2. ** ((alpha_1 - alpha_2) * delta)) i = np.abs(logt) <= threshold if (i.max()): # In this case the `t` value is "comparable" to 1, hence we # we will evaluate the whole formula. t = np.exp(logt[i]) r = (1. + t) / 2. f[i] = amplitude * xx[i] ** (-alpha_1) \ * r ** ((alpha_1 - alpha_2) * delta) if return_unit: return Quantity(f, unit=return_unit, copy=False) else: return f @staticmethod def fit_deriv(x, amplitude, x_break, alpha_1, alpha_2, delta): """One dimensional smoothly broken power law derivative with respect to parameters""" # Pre-calculate `x_b` and `x/x_b` and `logt` (see comments in # SmoothlyBrokenPowerLaw1D.evaluate) xx = x / x_break logt = np.log(xx) / delta # Initialize the return values f = np.zeros_like(xx) d_amplitude = np.zeros_like(xx) d_x_break = np.zeros_like(xx) d_alpha_1 = np.zeros_like(xx) d_alpha_2 = np.zeros_like(xx) d_delta = np.zeros_like(xx) threshold = 30 # (see comments in SmoothlyBrokenPowerLaw1D.evaluate) i = logt > threshold if (i.max()): f[i] = amplitude * xx[i] ** (-alpha_2) \ / (2. ** ((alpha_1 - alpha_2) * delta)) d_amplitude[i] = f[i] / amplitude d_x_break[i] = f[i] * alpha_2 / x_break d_alpha_1[i] = f[i] * (-delta * np.log(2)) d_alpha_2[i] = f[i] * (-np.log(xx[i]) + delta * np.log(2)) d_delta[i] = f[i] * (-(alpha_1 - alpha_2) * np.log(2)) i = logt < -threshold if (i.max()): f[i] = amplitude * xx[i] ** (-alpha_1) \ / (2. ** ((alpha_1 - alpha_2) * delta)) d_amplitude[i] = f[i] / amplitude d_x_break[i] = f[i] * alpha_1 / x_break d_alpha_1[i] = f[i] * (-np.log(xx[i]) - delta * np.log(2)) d_alpha_2[i] = f[i] * delta * np.log(2) d_delta[i] = f[i] * (-(alpha_1 - alpha_2) * np.log(2)) i = np.abs(logt) <= threshold if (i.max()): t = np.exp(logt[i]) r = (1. + t) / 2. f[i] = amplitude * xx[i] ** (-alpha_1) \ * r ** ((alpha_1 - alpha_2) * delta) d_amplitude[i] = f[i] / amplitude d_x_break[i] = f[i] * (alpha_1 - (alpha_1 - alpha_2) * t / 2. / r) / x_break d_alpha_1[i] = f[i] * (-np.log(xx[i]) + delta * np.log(r)) d_alpha_2[i] = f[i] * (-delta * np.log(r)) d_delta[i] = f[i] * (alpha_1 - alpha_2) \ * (np.log(r) - t / (1. + t) / delta * np.log(xx[i])) return [d_amplitude, d_x_break, d_alpha_1, d_alpha_2, d_delta] @property def input_units(self): if self.x_break.unit is None: return None else: return {'x': self.x_break.unit} def _parameter_units_for_data_units(self, inputs_unit, outputs_unit): return OrderedDict([('x_break', inputs_unit['x']), ('amplitude', outputs_unit['y'])]) class ExponentialCutoffPowerLaw1D(Fittable1DModel): """ One dimensional power law model with an exponential cutoff. Parameters ---------- amplitude : float Model amplitude x_0 : float Reference point alpha : float Power law index x_cutoff : float Cutoff point See Also -------- PowerLaw1D, BrokenPowerLaw1D, LogParabola1D Notes ----- Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha` for ``alpha``): .. math:: f(x) = A (x / x_0) ^ {-\\alpha} \\exp (-x / x_{cutoff}) """ amplitude = Parameter(default=1) x_0 = Parameter(default=1) alpha = Parameter(default=1) x_cutoff = Parameter(default=1) @staticmethod def evaluate(x, amplitude, x_0, alpha, x_cutoff): """One dimensional exponential cutoff power law model function""" xx = x / x_0 return amplitude * xx ** (-alpha) * np.exp(-x / x_cutoff) @staticmethod def fit_deriv(x, amplitude, x_0, alpha, x_cutoff): """One dimensional exponential cutoff power law derivative with respect to parameters""" xx = x / x_0 xc = x / x_cutoff d_amplitude = xx ** (-alpha) * np.exp(-xc) d_x_0 = alpha * amplitude * d_amplitude / x_0 d_alpha = -amplitude * d_amplitude * np.log(xx) d_x_cutoff = amplitude * x * d_amplitude / x_cutoff ** 2 return [d_amplitude, d_x_0, d_alpha, d_x_cutoff] @property def input_units(self): if self.x_0.unit is None: return None else: return {'x': self.x_0.unit} def _parameter_units_for_data_units(self, inputs_unit, outputs_unit): return OrderedDict([('x_0', inputs_unit['x']), ('x_cutoff', inputs_unit['x']), ('amplitude', outputs_unit['y'])]) class LogParabola1D(Fittable1DModel): """ One dimensional log parabola model (sometimes called curved power law). Parameters ---------- amplitude : float Model amplitude x_0 : float Reference point alpha : float Power law index beta : float Power law curvature See Also -------- PowerLaw1D, BrokenPowerLaw1D, ExponentialCutoffPowerLaw1D Notes ----- Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha` for ``alpha`` and :math:`\\beta` for ``beta``): .. math:: f(x) = A \\left(\\frac{x}{x_{0}}\\right)^{- \\alpha - \\beta \\log{\\left (\\frac{x}{x_{0}} \\right )}} """ amplitude = Parameter(default=1) x_0 = Parameter(default=1) alpha = Parameter(default=1) beta = Parameter(default=0) @staticmethod def evaluate(x, amplitude, x_0, alpha, beta): """One dimensional log parabola model function""" xx = x / x_0 exponent = -alpha - beta * np.log(xx) return amplitude * xx ** exponent @staticmethod def fit_deriv(x, amplitude, x_0, alpha, beta): """One dimensional log parabola derivative with respect to parameters""" xx = x / x_0 log_xx = np.log(xx) exponent = -alpha - beta * log_xx d_amplitude = xx ** exponent d_beta = -amplitude * d_amplitude * log_xx ** 2 d_x_0 = amplitude * d_amplitude * (beta * log_xx / x_0 - exponent / x_0) d_alpha = -amplitude * d_amplitude * log_xx return [d_amplitude, d_x_0, d_alpha, d_beta] @property def input_units(self): if self.x_0.unit is None: return None else: return {'x': self.x_0.unit} def _parameter_units_for_data_units(self, inputs_unit, outputs_unit): return OrderedDict([('x_0', inputs_unit['x']), ('amplitude', outputs_unit['y'])])
bsd-3-clause
tensorflow/lattice
tensorflow_lattice/python/premade_test.py
1
34444
# Copyright 2019 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. """Tests for Tensorflow Lattice premade.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import copy import json import tempfile from absl import logging from absl.testing import parameterized import numpy as np import pandas as pd import tensorflow as tf from tensorflow_lattice.python import configs from tensorflow_lattice.python import premade from tensorflow_lattice.python import premade_lib fake_data = { 'train_xs': [np.array([1]), np.array([3]), np.array([0])], 'train_ys': np.array([1]), 'eval_xs': [np.array([2]), np.array([30]), np.array([-3])] } unspecified_feature_configs = [ configs.FeatureConfig( name='numerical_1', lattice_size=2, pwl_calibration_input_keypoints=np.linspace(0.0, 1.0, num=10), ), configs.FeatureConfig( name='numerical_2', lattice_size=2, pwl_calibration_input_keypoints=np.linspace(0.0, 1.0, num=10), ), configs.FeatureConfig( name='categorical', lattice_size=2, num_buckets=2, monotonicity=[('0.0', '1.0')], vocabulary_list=['0.0', '1.0'], ), ] specified_feature_configs = [ configs.FeatureConfig( name='numerical_1', lattice_size=2, pwl_calibration_input_keypoints=np.linspace(0.0, 1.0, num=10), ), configs.FeatureConfig( name='numerical_2', lattice_size=2, pwl_calibration_input_keypoints=np.linspace(0.0, 1.0, num=10), ), configs.FeatureConfig( name='categorical', lattice_size=2, num_buckets=2, monotonicity=[(0, 1)], ), ] feature_configs = [ configs.FeatureConfig( name='numerical_1', lattice_size=2, pwl_calibration_input_keypoints=np.linspace(0.0, 1.0, num=10), ), configs.FeatureConfig( name='numerical_2', lattice_size=2, pwl_calibration_input_keypoints=np.linspace(0.0, 1.0, num=10), ), configs.FeatureConfig( name='categorical', lattice_size=2, num_buckets=2, monotonicity=[(0, 1)], ), ] class PremadeTest(parameterized.TestCase, tf.test.TestCase): """Tests for TFL premade.""" def setUp(self): super(PremadeTest, self).setUp() # UCI Statlog (Heart) dataset. heart_csv_file = tf.keras.utils.get_file( 'heart.csv', 'http://storage.googleapis.com/download.tensorflow.org/data/heart.csv') heart_df = pd.read_csv(heart_csv_file) heart_train_size = int(len(heart_df) * 0.8) heart_train_dataframe = heart_df[:heart_train_size] heart_test_dataframe = heart_df[heart_train_size:] # Features: # - age # - sex # - cp chest pain type (4 values) # - trestbps resting blood pressure # - chol serum cholestoral in mg/dl # - fbs fasting blood sugar > 120 mg/dl # - restecg resting electrocardiographic results (values 0,1,2) # - thalach maximum heart rate achieved # - exang exercise induced angina # - oldpeak ST depression induced by exercise relative to rest # - slope the slope of the peak exercise ST segment # - ca number of major vessels (0-3) colored by flourosopy # - thal 3 = normal; 6 = fixed defect; 7 = reversable defect # # This ordering of feature names will be the exact same order that we # construct our model to expect. self.heart_feature_names = [ 'age', 'sex', 'cp', 'chol', 'fbs', 'trestbps', 'thalach', 'restecg', 'exang', 'oldpeak', 'slope', 'ca', 'thal' ] feature_name_indices = { name: index for index, name in enumerate(self.heart_feature_names) } # This is the vocab list and mapping we will use for the 'thal' categorical # feature. thal_vocab_list = ['normal', 'fixed', 'reversible'] thal_map = {category: i for i, category in enumerate(thal_vocab_list)} # Custom function for converting thal categories to buckets def convert_thal_features(thal_features): # Note that two examples in the test set are already converted. return np.array([ thal_map[feature] if feature in thal_vocab_list else feature for feature in thal_features ]) # Custom function for extracting each feature. def extract_features(dataframe, label_name='target'): features = [] for feature_name in self.heart_feature_names: if feature_name == 'thal': features.append( convert_thal_features( dataframe[feature_name].values).astype(float)) else: features.append(dataframe[feature_name].values.astype(float)) labels = dataframe[label_name].values.astype(float) return features, labels self.heart_train_x, self.heart_train_y = extract_features( heart_train_dataframe) self.heart_test_x, self.heart_test_y = extract_features( heart_test_dataframe) # Let's define our label minimum and maximum. self.heart_min_label = float(np.min(self.heart_train_y)) self.heart_max_label = float(np.max(self.heart_train_y)) # Our lattice models may have predictions above 1.0 due to numerical errors. # We can subtract this small epsilon value from our output_max to make sure # we do not predict values outside of our label bound. self.numerical_error_epsilon = 1e-5 def compute_quantiles(features, num_keypoints=10, clip_min=None, clip_max=None, missing_value=None): # Clip min and max if desired. if clip_min is not None: features = np.maximum(features, clip_min) features = np.append(features, clip_min) if clip_max is not None: features = np.minimum(features, clip_max) features = np.append(features, clip_max) # Make features unique. unique_features = np.unique(features) # Remove missing values if specified. if missing_value is not None: unique_features = np.delete(unique_features, np.where(unique_features == missing_value)) # Compute and return quantiles over unique non-missing feature values. return np.quantile( unique_features, np.linspace(0., 1., num=num_keypoints), interpolation='nearest').astype(float) self.heart_feature_configs = [ configs.FeatureConfig( name='age', lattice_size=3, monotonicity='increasing', # We must set the keypoints manually. pwl_calibration_num_keypoints=5, pwl_calibration_input_keypoints=compute_quantiles( self.heart_train_x[feature_name_indices['age']], num_keypoints=5, clip_max=100), # Per feature regularization. regularizer_configs=[ configs.RegularizerConfig(name='calib_wrinkle', l2=0.1), ], ), configs.FeatureConfig( name='sex', num_buckets=2, ), configs.FeatureConfig( name='cp', monotonicity='increasing', # Keypoints that are uniformly spaced. pwl_calibration_num_keypoints=4, pwl_calibration_input_keypoints=np.linspace( np.min(self.heart_train_x[feature_name_indices['cp']]), np.max(self.heart_train_x[feature_name_indices['cp']]), num=4), ), configs.FeatureConfig( name='chol', monotonicity='increasing', # Explicit input keypoints initialization. pwl_calibration_input_keypoints=[126.0, 210.0, 247.0, 286.0, 564.0], # Calibration can be forced to span the full output range # by clamping. pwl_calibration_clamp_min=True, pwl_calibration_clamp_max=True, # Per feature regularization. regularizer_configs=[ configs.RegularizerConfig(name='calib_hessian', l2=1e-4), ], ), configs.FeatureConfig( name='fbs', # Partial monotonicity: output(0) <= output(1) monotonicity=[(0, 1)], num_buckets=2, ), configs.FeatureConfig( name='trestbps', monotonicity='decreasing', pwl_calibration_num_keypoints=5, pwl_calibration_input_keypoints=compute_quantiles( self.heart_train_x[feature_name_indices['trestbps']], num_keypoints=5), ), configs.FeatureConfig( name='thalach', monotonicity='decreasing', pwl_calibration_num_keypoints=5, pwl_calibration_input_keypoints=compute_quantiles( self.heart_train_x[feature_name_indices['thalach']], num_keypoints=5), ), configs.FeatureConfig( name='restecg', # Partial monotonicity: # output(0) <= output(1), output(0) <= output(2) monotonicity=[(0, 1), (0, 2)], num_buckets=3, ), configs.FeatureConfig( name='exang', # Partial monotonicity: output(0) <= output(1) monotonicity=[(0, 1)], num_buckets=2, ), configs.FeatureConfig( name='oldpeak', monotonicity='increasing', pwl_calibration_num_keypoints=5, pwl_calibration_input_keypoints=compute_quantiles( self.heart_train_x[feature_name_indices['oldpeak']], num_keypoints=5), ), configs.FeatureConfig( name='slope', # Partial monotonicity: # output(0) <= output(1), output(1) <= output(2) monotonicity=[(0, 1), (1, 2)], num_buckets=3, ), configs.FeatureConfig( name='ca', monotonicity='increasing', pwl_calibration_num_keypoints=4, pwl_calibration_input_keypoints=compute_quantiles( self.heart_train_x[feature_name_indices['ca']], num_keypoints=4), ), configs.FeatureConfig( name='thal', # Partial monotonicity: # output(normal) <= output(fixed) # output(normal) <= output(reversible) monotonicity=[('normal', 'fixed'), ('normal', 'reversible')], num_buckets=3, # We must specify the vocabulary list in order to later set the # monotonicities since we used names and not indices. vocabulary_list=thal_vocab_list, ), ] premade_lib.set_categorical_monotonicities(self.heart_feature_configs) def _ResetAllBackends(self): tf.keras.backend.clear_session() tf.compat.v1.reset_default_graph() class Encoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.int32): return int(obj) if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) def testSetRandomLattices(self): random_model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(unspecified_feature_configs), lattices='random', num_lattices=3, lattice_rank=2, separate_calibrators=True, output_initialization=[-1.0, 1.0]) premade_lib.set_random_lattice_ensemble(random_model_config) self.assertLen(random_model_config.lattices, 3) self.assertListEqual( [2, 2, 2], [len(lattice) for lattice in random_model_config.lattices]) specified_model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(specified_feature_configs), lattices=[['numerical_1', 'categorical'], ['numerical_2', 'categorical']], num_lattices=2, lattice_rank=2, separate_calibrators=True, output_initialization=[-1.0, 1.0]) with self.assertRaisesRegex( ValueError, 'model_config.lattices must be set to \'random\'.'): premade_lib.set_random_lattice_ensemble(specified_model_config) def testSetCategoricalMonotonicities(self): set_feature_configs = copy.deepcopy(unspecified_feature_configs) premade_lib.set_categorical_monotonicities(set_feature_configs) expectation = [(0, 1)] self.assertListEqual(expectation, set_feature_configs[2].monotonicity) def testVerifyConfig(self): unspecified_model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(unspecified_feature_configs), lattices='random', num_lattices=3, lattice_rank=2, separate_calibrators=True, output_initialization=[-1.0, 1.0]) with self.assertRaisesRegex( ValueError, 'Lattices are not fully specified for ensemble config.'): premade_lib.verify_config(unspecified_model_config) premade_lib.set_random_lattice_ensemble(unspecified_model_config) with self.assertRaisesRegex( ValueError, 'Element 0 for list/tuple 0 for feature categorical monotonicity is ' 'not an index: 0.0'): premade_lib.verify_config(unspecified_model_config) fixed_feature_configs = copy.deepcopy(unspecified_feature_configs) premade_lib.set_categorical_monotonicities(fixed_feature_configs) unspecified_model_config.feature_configs = fixed_feature_configs premade_lib.verify_config(unspecified_model_config) specified_model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(specified_feature_configs), lattices=[['numerical_1', 'categorical'], ['numerical_2', 'categorical']], num_lattices=2, lattice_rank=2, separate_calibrators=True, output_initialization=[-1.0, 1.0]) premade_lib.verify_config(specified_model_config) def testLatticeEnsembleFromConfig(self): model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(feature_configs), lattices=[['numerical_1', 'categorical'], ['numerical_2', 'categorical']], num_lattices=2, lattice_rank=2, separate_calibrators=True, regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-3), configs.RegularizerConfig('torsion', l2=1e-4), ], output_min=-1.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=5, output_initialization=[-1.0, 1.0]) model = premade.CalibratedLatticeEnsemble(model_config) loaded_model = premade.CalibratedLatticeEnsemble.from_config( model.get_config(), custom_objects=premade.get_custom_objects()) self.assertEqual( json.dumps(model.get_config(), sort_keys=True, cls=self.Encoder), json.dumps(loaded_model.get_config(), sort_keys=True, cls=self.Encoder)) def testLatticeFromConfig(self): model_config = configs.CalibratedLatticeConfig( feature_configs=copy.deepcopy(feature_configs), regularizer_configs=[ configs.RegularizerConfig('calib_wrinkle', l2=1e-3), configs.RegularizerConfig('torsion', l2=1e-3), ], output_min=0.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=6, output_initialization=[0.0, 1.0]) model = premade.CalibratedLattice(model_config) loaded_model = premade.CalibratedLattice.from_config( model.get_config(), custom_objects=premade.get_custom_objects()) self.assertEqual( json.dumps(model.get_config(), sort_keys=True, cls=self.Encoder), json.dumps(loaded_model.get_config(), sort_keys=True, cls=self.Encoder)) def testLatticeSimplexFromConfig(self): model_config = configs.CalibratedLatticeConfig( feature_configs=copy.deepcopy(feature_configs), regularizer_configs=[ configs.RegularizerConfig('calib_wrinkle', l2=1e-3), configs.RegularizerConfig('torsion', l2=1e-3), ], output_min=0.0, output_max=1.0, interpolation='simplex', output_calibration=True, output_calibration_num_keypoints=6, output_initialization=[0.0, 1.0]) model = premade.CalibratedLattice(model_config) loaded_model = premade.CalibratedLattice.from_config( model.get_config(), custom_objects=premade.get_custom_objects()) self.assertEqual( json.dumps(model.get_config(), sort_keys=True, cls=self.Encoder), json.dumps(loaded_model.get_config(), sort_keys=True, cls=self.Encoder)) def testLinearFromConfig(self): model_config = configs.CalibratedLinearConfig( feature_configs=copy.deepcopy(feature_configs), regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-4), configs.RegularizerConfig('torsion', l2=1e-3), ], use_bias=True, output_min=0.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=6, output_initialization=[0.0, 1.0]) model = premade.CalibratedLinear(model_config) loaded_model = premade.CalibratedLinear.from_config( model.get_config(), custom_objects=premade.get_custom_objects()) self.assertEqual( json.dumps(model.get_config(), sort_keys=True, cls=self.Encoder), json.dumps(loaded_model.get_config(), sort_keys=True, cls=self.Encoder)) def testAggregateFromConfig(self): model_config = configs.AggregateFunctionConfig( feature_configs=feature_configs, regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-4), configs.RegularizerConfig('torsion', l2=1e-3), ], middle_calibration=True, middle_monotonicity='increasing', output_min=0.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=8, output_initialization=[0.0, 1.0]) model = premade.AggregateFunction(model_config) loaded_model = premade.AggregateFunction.from_config( model.get_config(), custom_objects=premade.get_custom_objects()) self.assertEqual( json.dumps(model.get_config(), sort_keys=True, cls=self.Encoder), json.dumps(loaded_model.get_config(), sort_keys=True, cls=self.Encoder)) @parameterized.parameters( ('hypercube', 'all_vertices', 0, 0.85), ('simplex', 'all_vertices', 0, 0.89), ('hypercube', 'kronecker_factored', 2, 0.82), ('hypercube', 'kronecker_factored', 4, 0.82), ) def testCalibratedLatticeEnsembleCrystals(self, interpolation, parameterization, num_terms, expected_minimum_auc): # Construct model. self._ResetAllBackends() crystals_feature_configs = copy.deepcopy(self.heart_feature_configs) model_config = configs.CalibratedLatticeEnsembleConfig( regularizer_configs=[ configs.RegularizerConfig(name='torsion', l2=1e-4), configs.RegularizerConfig(name='output_calib_hessian', l2=1e-4), ], feature_configs=crystals_feature_configs, lattices='crystals', num_lattices=6, lattice_rank=5, interpolation=interpolation, parameterization=parameterization, num_terms=num_terms, separate_calibrators=True, output_calibration=False, output_min=self.heart_min_label, output_max=self.heart_max_label - self.numerical_error_epsilon, output_initialization=[self.heart_min_label, self.heart_max_label], ) if parameterization == 'kronecker_factored': model_config.regularizer_configs = None for feature_config in model_config.feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None # Perform prefitting steps. prefitting_model_config = premade_lib.construct_prefitting_model_config( model_config) prefitting_model = premade.CalibratedLatticeEnsemble( prefitting_model_config) prefitting_model.compile( loss=tf.keras.losses.BinaryCrossentropy(), optimizer=tf.keras.optimizers.Adam(0.01)) prefitting_model.fit( self.heart_train_x, self.heart_train_y, batch_size=100, epochs=50, verbose=False) premade_lib.set_crystals_lattice_ensemble(model_config, prefitting_model_config, prefitting_model) # Construct and train final model model = premade.CalibratedLatticeEnsemble(model_config) model.compile( loss=tf.keras.losses.BinaryCrossentropy(), metrics=tf.keras.metrics.AUC(), optimizer=tf.keras.optimizers.Adam(0.01)) model.fit( self.heart_train_x, self.heart_train_y, batch_size=100, epochs=200, verbose=False) results = model.evaluate( self.heart_test_x, self.heart_test_y, verbose=False) logging.info('Calibrated lattice ensemble crystals classifier results:') logging.info(results) self.assertGreater(results[1], expected_minimum_auc) @parameterized.parameters( ('hypercube', 'all_vertices', 0, 0.85), ('simplex', 'all_vertices', 0, 0.88), ('hypercube', 'kronecker_factored', 2, 0.86), ('hypercube', 'kronecker_factored', 4, 0.9), ) def testCalibratedLatticeEnsembleRTL(self, interpolation, parameterization, num_terms, expected_minimum_auc): # Construct model. self._ResetAllBackends() rtl_feature_configs = copy.deepcopy(self.heart_feature_configs) for feature_config in rtl_feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None model_config = configs.CalibratedLatticeEnsembleConfig( regularizer_configs=[ configs.RegularizerConfig(name='torsion', l2=1e-4), configs.RegularizerConfig(name='output_calib_hessian', l2=1e-4), ], feature_configs=rtl_feature_configs, lattices='rtl_layer', num_lattices=6, lattice_rank=5, interpolation=interpolation, parameterization=parameterization, num_terms=num_terms, separate_calibrators=True, output_calibration=False, output_min=self.heart_min_label, output_max=self.heart_max_label - self.numerical_error_epsilon, output_initialization=[self.heart_min_label, self.heart_max_label], ) # We must remove all regularization if using 'kronecker_factored'. if parameterization == 'kronecker_factored': model_config.regularizer_configs = None # Construct and train final model model = premade.CalibratedLatticeEnsemble(model_config) model.compile( loss=tf.keras.losses.BinaryCrossentropy(), metrics=tf.keras.metrics.AUC(), optimizer=tf.keras.optimizers.Adam(0.01)) model.fit( self.heart_train_x, self.heart_train_y, batch_size=100, epochs=200, verbose=False) results = model.evaluate( self.heart_test_x, self.heart_test_y, verbose=False) logging.info('Calibrated lattice ensemble rtl classifier results:') logging.info(results) self.assertGreater(results[1], expected_minimum_auc) @parameterized.parameters( ('hypercube', 'all_vertices', 0, 0.81), ('simplex', 'all_vertices', 0, 0.81), ('hypercube', 'kronecker_factored', 2, 0.79), ('hypercube', 'kronecker_factored', 4, 0.8), ) def testCalibratedLattice(self, interpolation, parameterization, num_terms, expected_minimum_auc): # Construct model configuration. self._ResetAllBackends() lattice_feature_configs = copy.deepcopy(self.heart_feature_configs[:5]) model_config = configs.CalibratedLatticeConfig( feature_configs=lattice_feature_configs, interpolation=interpolation, parameterization=parameterization, num_terms=num_terms, regularizer_configs=[ configs.RegularizerConfig(name='torsion', l2=1e-4), configs.RegularizerConfig(name='output_calib_hessian', l2=1e-4), ], output_min=self.heart_min_label, output_max=self.heart_max_label, output_calibration=False, output_initialization=[self.heart_min_label, self.heart_max_label], ) if parameterization == 'kronecker_factored': model_config.regularizer_configs = None for feature_config in model_config.feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None # Construct and train final model model = premade.CalibratedLattice(model_config) model.compile( loss=tf.keras.losses.BinaryCrossentropy(), metrics=tf.keras.metrics.AUC(), optimizer=tf.keras.optimizers.Adam(0.01)) model.fit( self.heart_train_x[:5], self.heart_train_y, batch_size=100, epochs=200, verbose=False) results = model.evaluate( self.heart_test_x[:5], self.heart_test_y, verbose=False) logging.info('Calibrated lattice classifier results:') logging.info(results) self.assertGreater(results[1], expected_minimum_auc) @parameterized.parameters( ('all_vertices', 0), ('kronecker_factored', 2), ) def testLatticeEnsembleH5FormatSaveLoad(self, parameterization, num_terms): model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(feature_configs), lattices=[['numerical_1', 'categorical'], ['numerical_2', 'categorical']], num_lattices=2, lattice_rank=2, parameterization=parameterization, num_terms=num_terms, separate_calibrators=True, regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-3), configs.RegularizerConfig('torsion', l2=1e-4), ], output_min=-1.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=5, output_initialization=[-1.0, 1.0]) if parameterization == 'kronecker_factored': model_config.regularizer_configs = None for feature_config in model_config.feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None model = premade.CalibratedLatticeEnsemble(model_config) # Compile and fit model. model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(0.1)) model.fit(fake_data['train_xs'], fake_data['train_ys']) # Save model using H5 format. with tempfile.NamedTemporaryFile(suffix='.h5') as f: tf.keras.models.save_model(model, f.name) loaded_model = tf.keras.models.load_model( f.name, custom_objects=premade.get_custom_objects()) self.assertAllClose( model.predict(fake_data['eval_xs']), loaded_model.predict(fake_data['eval_xs'])) @parameterized.parameters( ('all_vertices', 0), ('kronecker_factored', 2), ) def testLatticeEnsembleRTLH5FormatSaveLoad(self, parameterization, num_terms): rtl_feature_configs = copy.deepcopy(feature_configs) for feature_config in rtl_feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None model_config = configs.CalibratedLatticeEnsembleConfig( feature_configs=copy.deepcopy(rtl_feature_configs), lattices='rtl_layer', num_lattices=2, lattice_rank=2, parameterization=parameterization, num_terms=num_terms, separate_calibrators=True, regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-3), configs.RegularizerConfig('torsion', l2=1e-4), ], output_min=-1.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=5, output_initialization=[-1.0, 1.0]) if parameterization == 'kronecker_factored': model_config.regularizer_configs = None model = premade.CalibratedLatticeEnsemble(model_config) # Compile and fit model. model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(0.1)) model.fit(fake_data['train_xs'], fake_data['train_ys']) # Save model using H5 format. with tempfile.NamedTemporaryFile(suffix='.h5') as f: tf.keras.models.save_model(model, f.name) loaded_model = tf.keras.models.load_model( f.name, custom_objects=premade.get_custom_objects()) self.assertAllClose( model.predict(fake_data['eval_xs']), loaded_model.predict(fake_data['eval_xs'])) @parameterized.parameters( ('all_vertices', 0), ('kronecker_factored', 2), ) def testLatticeH5FormatSaveLoad(self, parameterization, num_terms): model_config = configs.CalibratedLatticeConfig( feature_configs=copy.deepcopy(feature_configs), parameterization=parameterization, num_terms=num_terms, regularizer_configs=[ configs.RegularizerConfig('calib_wrinkle', l2=1e-3), configs.RegularizerConfig('torsion', l2=1e-3), ], output_min=0.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=6, output_initialization=[0.0, 1.0]) if parameterization == 'kronecker_factored': model_config.regularizer_configs = None for feature_config in model_config.feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None model = premade.CalibratedLattice(model_config) # Compile and fit model. model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(0.1)) model.fit(fake_data['train_xs'], fake_data['train_ys']) # Save model using H5 format. with tempfile.NamedTemporaryFile(suffix='.h5') as f: tf.keras.models.save_model(model, f.name) loaded_model = tf.keras.models.load_model( f.name, custom_objects=premade.get_custom_objects()) self.assertAllClose( model.predict(fake_data['eval_xs']), loaded_model.predict(fake_data['eval_xs'])) def testLinearH5FormatSaveLoad(self): model_config = configs.CalibratedLinearConfig( feature_configs=copy.deepcopy(feature_configs), regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-4), configs.RegularizerConfig('torsion', l2=1e-3), ], use_bias=True, output_min=0.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=6, output_initialization=[0.0, 1.0]) model = premade.CalibratedLinear(model_config) # Compile and fit model. model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(0.1)) model.fit(fake_data['train_xs'], fake_data['train_ys']) # Save model using H5 format. with tempfile.NamedTemporaryFile(suffix='.h5') as f: tf.keras.models.save_model(model, f.name) loaded_model = tf.keras.models.load_model( f.name, custom_objects=premade.get_custom_objects()) self.assertAllClose( model.predict(fake_data['eval_xs']), loaded_model.predict(fake_data['eval_xs'])) def testAggregateH5FormatSaveLoad(self): model_config = configs.AggregateFunctionConfig( feature_configs=feature_configs, regularizer_configs=[ configs.RegularizerConfig('calib_hessian', l2=1e-4), configs.RegularizerConfig('torsion', l2=1e-3), ], middle_calibration=True, middle_monotonicity='increasing', output_min=0.0, output_max=1.0, output_calibration=True, output_calibration_num_keypoints=8, output_initialization=[0.0, 1.0]) model = premade.AggregateFunction(model_config) # Compile and fit model. model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(0.1)) model.fit(fake_data['train_xs'], fake_data['train_ys']) # Save model using H5 format. with tempfile.NamedTemporaryFile(suffix='.h5') as f: # Note: because of naming clashes in the optimizer, we cannot include it # when saving in HDF5. The keras team has informed us that we should not # push to support this since SavedModel format is the new default and no # new HDF5 functionality is desired. tf.keras.models.save_model(model, f.name, include_optimizer=False) loaded_model = tf.keras.models.load_model( f.name, custom_objects=premade.get_custom_objects()) self.assertAllClose( model.predict(fake_data['eval_xs']), loaded_model.predict(fake_data['eval_xs'])) if __name__ == '__main__': tf.test.main()
apache-2.0
heli522/scikit-learn
examples/cluster/plot_dict_face_patches.py
337
2747
""" Online learning of a dictionary of parts of faces ================================================== This example uses a large dataset of faces to learn a set of 20 x 20 images patches that constitute faces. From the programming standpoint, it is interesting because it shows how to use the online API of the scikit-learn to process a very large dataset by chunks. The way we proceed is that we load an image at a time and extract randomly 50 patches from this image. Once we have accumulated 500 of these patches (using 10 images), we run the `partial_fit` method of the online KMeans object, MiniBatchKMeans. The verbose setting on the MiniBatchKMeans enables us to see that some clusters are reassigned during the successive calls to partial-fit. This is because the number of patches that they represent has become too low, and it is better to choose a random new cluster. """ print(__doc__) import time import matplotlib.pyplot as plt import numpy as np from sklearn import datasets from sklearn.cluster import MiniBatchKMeans from sklearn.feature_extraction.image import extract_patches_2d faces = datasets.fetch_olivetti_faces() ############################################################################### # Learn the dictionary of images print('Learning the dictionary... ') rng = np.random.RandomState(0) kmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True) patch_size = (20, 20) buffer = [] index = 1 t0 = time.time() # The online learning part: cycle over the whole dataset 6 times index = 0 for _ in range(6): for img in faces.images: data = extract_patches_2d(img, patch_size, max_patches=50, random_state=rng) data = np.reshape(data, (len(data), -1)) buffer.append(data) index += 1 if index % 10 == 0: data = np.concatenate(buffer, axis=0) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) kmeans.partial_fit(data) buffer = [] if index % 100 == 0: print('Partial fit of %4i out of %i' % (index, 6 * len(faces.images))) dt = time.time() - t0 print('done in %.2fs.' % dt) ############################################################################### # Plot the results plt.figure(figsize=(4.2, 4)) for i, patch in enumerate(kmeans.cluster_centers_): plt.subplot(9, 9, i + 1) plt.imshow(patch.reshape(patch_size), cmap=plt.cm.gray, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.suptitle('Patches of faces\nTrain time %.1fs on %d patches' % (dt, 8 * len(faces.images)), fontsize=16) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) plt.show()
bsd-3-clause
rishikksh20/scikit-learn
sklearn/datasets/tests/test_kddcup99.py
42
1278
"""Test kddcup99 loader. Only 'percent10' mode is tested, as the full data is too big to use in unit-testing. The test is skipped if the data wasn't previously fetched and saved to scikit-learn data folder. """ from sklearn.datasets import fetch_kddcup99 from sklearn.utils.testing import assert_equal, SkipTest def test_percent10(): try: data = fetch_kddcup99(download_if_missing=False) except IOError: raise SkipTest("kddcup99 dataset can not be loaded.") assert_equal(data.data.shape, (494021, 41)) assert_equal(data.target.shape, (494021,)) data_shuffled = fetch_kddcup99(shuffle=True, random_state=0) assert_equal(data.data.shape, data_shuffled.data.shape) assert_equal(data.target.shape, data_shuffled.target.shape) data = fetch_kddcup99('SA') assert_equal(data.data.shape, (100655, 41)) assert_equal(data.target.shape, (100655,)) data = fetch_kddcup99('SF') assert_equal(data.data.shape, (73237, 4)) assert_equal(data.target.shape, (73237,)) data = fetch_kddcup99('http') assert_equal(data.data.shape, (58725, 3)) assert_equal(data.target.shape, (58725,)) data = fetch_kddcup99('smtp') assert_equal(data.data.shape, (9571, 3)) assert_equal(data.target.shape, (9571,))
bsd-3-clause
benglard/ConvNetPy
examples/faces.py
1
2118
# Requires scikit-learn from vol_util import augment from vol import Vol from net import Net from trainers import Trainer from sklearn.cross_validation import train_test_split from sklearn.datasets import fetch_lfw_people training_data = None testing_data = None network = None t = None def load_data(): global training_data, testing_data lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) xs = lfw_people.data ys = lfw_people.target inputs = [] labels = list(ys) for face in xs: V = Vol(50, 37, 1, 0.0) V.w = list(face) inputs.append(augment(V, 30)) x_tr, x_te, y_tr, y_te = train_test_split(inputs, labels, test_size=0.25) training_data = zip(x_tr, y_tr) testing_data = zip(x_te, y_te) print 'Dataset made...' def start(): global network, t layers = [] layers.append({'type': 'input', 'out_sx': 30, 'out_sy': 30, 'out_depth': 1}) layers.append({'type': 'fc', 'num_neurons': 100, 'activation': 'sigmoid'}) layers.append({'type': 'softmax', 'num_classes': 7}) print 'Layers made...' network = Net(layers) print 'Net made...' print network t = Trainer(network, {'method': 'adadelta', 'batch_size': 20, 'l2_decay': 0.001}) print 'Trainer made...' print t def train(): global training_data, network, t print 'In training...' print 'k', 'time\t\t ', 'loss\t ', 'training accuracy' print '----------------------------------------------------' try: for x, y in training_data: stats = t.train(x, y) print stats['k'], stats['time'], stats['loss'], stats['accuracy'] except: #hit control-c or other return def test(): global training_data, testing_data, network, t print 'In testing' right = 0 count = 0 try: for x, y in testing_data: network.forward(x) right += network.getPrediction() == y print count count += 1 except: pass finally: accuracy = float(right) / count * 100 print accuracy
mit
jmuhlich/rasmodel
kras_gtp_hydrolysis.py
6
1613
from rasmodel.scenarios.default import model import numpy as np from matplotlib import pyplot as plt from pysb.integrate import Solver from pysb import * from tbidbaxlipo.util import fitting KRAS = model.monomers['KRAS'] GTP = model.monomers['GTP'] total_pi = 50000 for mutant in KRAS.site_states['mutant']: Initial(KRAS(gtp=1, gap=None, gef=None, p_loop=None, s1s2='open', CAAX=None, mutant=mutant) % GTP(p=1, label='n'), Parameter('KRAS_%s_GTP_0' % mutant, 0)) plt.ion() plt.figure() t = np.linspace(0, 1000, 1000) # 1000 seconds for mutant in KRAS.site_states['mutant']: # Zero out all initial conditions for ic in model.initial_conditions: ic[1].value = 0 model.parameters['KRAS_%s_GTP_0' % mutant].value = total_pi sol = Solver(model, t) sol.run() plt.plot(t, sol.yobs['Pi_'] / total_pi, label=mutant) plt.ylabel('GTP hydrolyzed (%)') plt.ylim(top=1) plt.xlabel('Time (s)') plt.title('Intrinsic hydrolysis') plt.legend(loc='upper left', fontsize=11, frameon=False) plt.figure() for mutant in KRAS.site_states['mutant']: # Zero out all initial conditions for ic in model.initial_conditions: ic[1].value = 0 model.parameters['RASA1_0'].value = 50000 model.parameters['KRAS_%s_GTP_0' % mutant].value = total_pi sol = Solver(model, t) sol.run() plt.plot(t, sol.yobs['Pi_'] / total_pi, label=mutant) plt.ylabel('GTP hydrolyzed (%)') plt.ylim(top=1) plt.xlabel('Time (s)') plt.title('GAP-mediated hydrolysis') plt.legend(loc='upper right', fontsize=11, frameon=False)
mit
pombredanne/numba
examples/mandel/mandel_vectorize.py
3
1448
#! /usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function, division, absolute_import import sys from numba import vectorize import numpy as np from timeit import default_timer as timer from matplotlib.pylab import imshow, jet, show, ion sig = 'uint8(uint32, f4, f4, f4, f4, uint32, uint32, uint32)' @vectorize([sig], target='cuda') def mandel(tid, min_x, max_x, min_y, max_y, width, height, iters): pixel_size_x = (max_x - min_x) / width pixel_size_y = (max_y - min_y) / height x = tid % width y = tid / width real = min_x + x * pixel_size_x imag = min_y + y * pixel_size_y c = complex(real, imag) z = 0.0j for i in range(iters): z = z * z + c if (z.real * z.real + z.imag * z.imag) >= 4: return i return 255 def create_fractal(min_x, max_x, min_y, max_y, width, height, iters): tids = np.arange(width * height, dtype=np.uint32) return mandel(tids, np.float32(min_x), np.float32(max_x), np.float32(min_y), np.float32(max_y), np.uint32(height), np.uint32(width), np.uint32(iters)) def main(): width = 500 * 10 height = 750 * 10 ts = timer() pixels = create_fractal(-2.0, 1.0, -1.0, 1.0, width, height, 20) te = timer() print('time: %f' % (te - ts)) image = pixels.reshape(width, height) #print(image) imshow(image) show() if __name__ == '__main__': main()
bsd-2-clause
anhaidgroup/py_entitymatching
py_entitymatching/feature/attributeutils.py
1
8443
""" This module contains some utility functions for attributes in the DataFrame. """ import logging import pandas as pd import numpy as np import six from py_entitymatching.utils.validation_helper import validate_object_type logger = logging.getLogger(__name__) def get_attr_types(data_frame): """ This function gets the attribute types for a DataFrame. Specifically this function gets the attribute types based on the statistics of the attributes. These attribute types can be str_eq_1w, str_bt_1w_5w, str_bt_5w_10w, str_gt_10w, boolean or numeric. The types roughly capture whether the attribute is of type string, boolean or numeric. Further, with in the string type the subtypes are capture the average number of tokens in the column values. For example, str_bt_1w_5w means the average number of tokens in that column is greater than one word but less than 5 words. Args: data_frame (DataFrame): The input DataFrame for which types of attributes must be determined. Returns: A Python dictionary is returned containing the attribute types. Specifically, in the dictionary key is an attribute name, value is the type of that attribute. Further, the dictionary will have a key _table, and the value of that should be a pointer to the input DataFrame. Raises: AssertionError: If `data_frame` is not of type pandas DataFrame. Examples: >>> import py_entitymatching as em >>> A = em.read_csv_metadata('path_to_csv_dir/table_A.csv', key='ID') >>> B = em.read_csv_metadata('path_to_csv_dir/table_B.csv', key='ID') >>> atypes1 = em.get_attr_types(A) >>> atypes2 = em.get_attr_types(B) """ # Validate input paramaters # # We expect the input object (data_frame) to be of type pandas DataFrame. if not isinstance(data_frame, pd.DataFrame): logger.error('Input table is not of type pandas dataframe') raise AssertionError('Input table is not of type pandas dataframe') # Now get type for each column type_list = [_get_type(data_frame[col]) for col in data_frame.columns] # Create a dictionary containing attribute types attribute_type_dict = dict(zip(data_frame.columns, type_list)) # Update the dictionary with the _table key and value set to the input # DataFrame attribute_type_dict['_table'] = data_frame # Return the attribute type dictionary return attribute_type_dict def get_attr_corres(ltable, rtable): """ This function gets the attribute correspondences between the attributes of ltable and rtable. The user may need to get the correspondences so that he/she can generate features based those correspondences. Args: ltable,rtable (DataFrame): Input DataFrames for which the attribute correspondences must be obtained. Returns: A Python dictionary is returned containing the attribute correspondences. Specifically, this returns a dictionary with the following key-value pairs: corres: points to the list correspondences as tuples. Each correspondence is a tuple with two attributes: one from ltable and the other from rtable. ltable: points to ltable. rtable: points to rtable. Currently, 'corres' contains only pairs of attributes with exact names in ltable and rtable. Raises: AssertionError: If `ltable` is not of type pandas DataFrame. AssertionError: If `rtable` is not of type pandas DataFrame. Examples: >>> import py_entitymatching as em >>> A = em.read_csv_metadata('path_to_csv_dir/table_A.csv', key='ID') >>> B = em.read_csv_metadata('path_to_csv_dir/table_B.csv', key='ID') >>> match_c = em.get_attr_corres(A, B) """ # Validate input parameters # # We expect the input object (ltable) to be of type pandas # DataFrame validate_object_type(ltable, pd.DataFrame, error_prefix='Input ltable') # # We expect the input object (rtable) to be of type pandas # DataFrame validate_object_type(rtable, pd.DataFrame, error_prefix='Input rtable') # Initialize the correspondence list correspondence_list = [] # Check for each column in ltable, if column exists in rtable, # If so, add it to the correspondence list. # Note: This may not be the fastest way to implement this. We could # refactor this later. for column in ltable.columns: if column in rtable.columns: correspondence_list.append((column, column)) # Initialize a correspondence dictionary. correspondence_dict = dict() # Fill the corres, ltable and rtable. correspondence_dict['corres'] = correspondence_list correspondence_dict['ltable'] = ltable correspondence_dict['rtable'] = rtable # Finally, return the correspondence dictionary return correspondence_dict def _get_type(column): """ Given a pandas Series (i.e column in pandas DataFrame) obtain its type """ # Validate input parameters # # We expect the input column to be of type pandas Series if not isinstance(column, pd.Series): raise AssertionError('Input (column) is not of type pandas series') # To get the type first drop all NaNa column = column.dropna() # Get type for each element and convert it into a set (and for # convenience convert the resulting set into a list) type_list = list(set(column.map(type).tolist())) # If the list is empty, then we cannot decide anything about the column. # We will raise a warning and return the type to be numeric. # Note: The reason numeric is returned instead of a special type because, # we want to keep the types minimal. Further, explicitly recommend the # user to update the returned types later. if len(type_list) == 0: logger.warning("Column {0} does not seem to qualify as any atomic type. " "It may contain all NaNs. Please update the values of column {0}".format(column.name)) return 'un_determined' # If the column qualifies to be of more than one type (for instance, # in a numeric column, some values may be inferred as strings), then we # will raise an error for the user to fix this case. if len(type_list) > 1: logger.warning('Column %s qualifies to be more than one type. \n' 'Please explicitly set the column type like this:\n' 'A["address"] = A["address"].astype(str) \n' 'Similarly use int, float, boolean types.' % column.name) raise AssertionError('Column %s qualifies to be more than one type. \n' 'Please explicitly set the column type like this:\n' 'A["address"] = A["address"].astype(str) \n' 'Similarly use int, float, boolean types.' % column.name) else: # the number of types is 1. returned_type = type_list[0] # Check if the type is boolean, if so return boolean if returned_type == bool or returned_type == np.bool_: return 'boolean' # Check if the type is string, if so identify the subtype under it. # We use average token length to identify the subtypes # Consider string and unicode as same elif returned_type == str or returned_type == six.unichr or returned_type == six.text_type: # get average token length average_token_len = \ pd.Series.mean(column.str.split().apply(_len_handle_nan)) if average_token_len == 1: return "str_eq_1w" elif average_token_len <= 5: return "str_bt_1w_5w" elif average_token_len <= 10: return "str_bt_5w_10w" else: return "str_gt_10w" else: # Finally, return numeric if it does not qualify for any of the # types above. return "numeric" def _len_handle_nan(input_list): """ Get the length of list, handling NaN """ # Check if the input is of type list, if so return the len else return NaN if isinstance(input_list, list): return len(input_list) else: return np.NaN
bsd-3-clause
mne-tools/mne-tools.github.io
0.21/_downloads/823e809619804fe332c02f86cbc25654/plot_find_eog_artifacts.py
5
1216
""" ================== Find EOG artifacts ================== Locate peaks of EOG to spot blinks and general EOG artifacts. """ # Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # # License: BSD (3-clause) import numpy as np import matplotlib.pyplot as plt import mne from mne import io from mne.datasets import sample print(__doc__) data_path = sample.data_path() ############################################################################### # Set parameters raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' # Setup for reading the raw data raw = io.read_raw_fif(raw_fname) event_id = 998 eog_events = mne.preprocessing.find_eog_events(raw, event_id) # Read epochs picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, eog=True, exclude='bads') tmin, tmax = -0.2, 0.2 epochs = mne.Epochs(raw, eog_events, event_id, tmin, tmax, picks=picks) data = epochs.get_data() print("Number of detected EOG artifacts : %d" % len(data)) ############################################################################### # Plot EOG artifacts plt.plot(1e3 * epochs.times, np.squeeze(data).T) plt.xlabel('Times (ms)') plt.ylabel('EOG (µV)') plt.show()
bsd-3-clause
paulsbrookes/cqed_sims_qutip
spectroscopy/spec_multi_4.py
1
16074
import numpy as np import yaml from qutip import * from pylab import * from scipy.fftpack import fft import matplotlib.pyplot as plt import yaml from scipy.interpolate import interp1d from scipy.optimize import fsolve from qutip.ui.progressbar import TextProgressBar class Parameters: def __init__(self, wc, wq, eps, g, chi, kappa, gamma, t_levels, c_levels): self.wc = wc self.wq = wq self.eps = eps self.g = g self.chi = chi self.gamma = gamma self.kappa = kappa self.t_levels = t_levels self.c_levels = c_levels def copy(self): params = Parameters(self.wc, self.wq, self.eps, self.g, self.chi, self.kappa, self.gamma, self.t_levels, self.c_levels) return params class Results: def __init__(self, params=np.array([]), wd_points=np.array([]), transmissions=np.array([]), edge_occupations_c=np.array([]), edge_occupations_t=np.array([])): self.params = params self.wd_points = wd_points self.transmissions = transmissions self.edge_occupations_c = edge_occupations_c self.edge_occupations_t = edge_occupations_t self.abs_transmissions = np.absolute(self.transmissions) self.size = self.wd_points.size def concatenate(self, results): combined_params = np.concatenate([self.params, results.params]) combined_wd_points = np.concatenate([self.wd_points, results.wd_points]) combined_transmissions = np.concatenate([self.transmissions, results.transmissions]) combined_edge_occupations_c = np.concatenate([self.edge_occupations_c, results.edge_occupations_c]) combined_edge_occupations_t = np.concatenate([self.edge_occupations_t, results.edge_occupations_t]) sort_indices = np.argsort(combined_wd_points) combined_params = combined_params[sort_indices] combined_wd_points = combined_wd_points[sort_indices] combined_transmissions = combined_transmissions[sort_indices] combined_edge_occupations_c = combined_edge_occupations_c[sort_indices] combined_edge_occupations_t = combined_edge_occupations_t[sort_indices] combined_results = Results(combined_params, combined_wd_points, combined_transmissions, combined_edge_occupations_c, combined_edge_occupations_t) return combined_results def delete(self, indices): reduced_params = np.delete(self.params, indices) reduced_wd_points = np.delete(self.wd_points, indices) reduced_transmissions = np.delete(self.transmissions, indices) reduced_edge_occupations_c = np.delete(self.edge_occupations_c, indices) reduced_edge_occupations_t = np.delete(self.edge_occupations_t, indices) reduced_results = Results(reduced_params, reduced_wd_points, reduced_transmissions, reduced_edge_occupations_c, reduced_edge_occupations_t) params_change = (reduced_params == self.params) wd_points_change = (reduced_wd_points == self.wd_points) transmissions_change = (reduced_transmissions == self.transmissions) edge_occupations_c_change = (reduced_edge_occupations_c == self.edge_occupations_c) edge_occupations_t_change = (reduced_edge_occupations_t == self.edge_occupations_t) print np.all([params_change, wd_points_change, transmissions_change, edge_occupations_c_change, edge_occupations_t_change]) return reduced_results def queue(self): queue = Queue(self.params, self.wd_points) return queue class Queue: def __init__(self, params = np.array([]), wd_points = np.array([])): self.params = params self.wd_points = wd_points self.size = self.wd_points.size sort_indices = np.argsort(self.wd_points) self.wd_points = self.wd_points[sort_indices] self.params = self.params[sort_indices] def curvature_generate(self, results, threshold = 0.05): curvature_info = CurvatureInfo(results, threshold) self.wd_points = curvature_info.new_points() self.params = hilbert_interpolation(self.wd_points, results) self.size = self.wd_points.size sort_indices = np.argsort(self.wd_points) self.wd_points = self.wd_points[sort_indices] self.params = self.params[sort_indices] def hilbert_generate(self, results, threshold_c, threshold_t): suggested_c_levels = [] suggested_t_levels = [] overload_occurred = False for index, params_instance in enumerate(results.params): threshold_c_weighted = threshold_c / params_instance.c_levels threshold_t_weighted = threshold_t / params_instance.t_levels overload_c = (results.edge_occupations_c[index] > threshold_c_weighted) overload_t = (results.edge_occupations_t[index] > threshold_t_weighted) if overload_c: overload_occurred = True suggestion = size_correction( results.edge_occupations_c[index], params_instance.c_levels, threshold_c_weighted / 2) else: suggestion = params_instance.c_levels suggested_c_levels.append(suggestion) if overload_t: overload_occurred = True suggestion = size_correction( results.edge_occupations_t[index], params_instance.t_levels, threshold_t_weighted / 2) else: suggestion = params_instance.t_levels suggested_t_levels.append(suggestion) if overload_occurred: c_levels_new = np.max(suggested_c_levels) t_levels_new = np.max(suggested_t_levels) self.wd_points = results.wd_points for index, params_instance in enumerate(results.params): results.params[index].t_levels = t_levels_new results.params[index].c_levels = c_levels_new self.params = results.params self.size = results.size return Results() else: self.wd_points = np.array([]) self.params = np.array([]) self.size = 0 return results def hilbert_generate_alternate(self, results, threshold_c, threshold_t): old_c_levels = np.zeros(results.size) suggested_c_levels = np.zeros(results.size) old_t_levels = np.zeros(results.size) suggested_t_levels = np.zeros(results.size) for index, params_instance in enumerate(results.params): suggested_c_levels[index] = \ size_suggestion(results.edge_occupations_c[index], params_instance.c_levels, threshold_c) old_c_levels[index] = params_instance.c_levels suggested_t_levels[index] = \ size_suggestion(results.edge_occupations_t[index], params_instance.t_levels, threshold_t) old_t_levels[index] = params_instance.t_levels if np.any(suggested_c_levels > old_c_levels) or np.any(suggested_t_levels > old_t_levels): c_levels_new = np.max(suggested_c_levels) t_levels_new = np.max(suggested_t_levels) self.wd_points = results.wd_points for index, params_instance in enumerate(results.params): results.params[index].t_levels = t_levels_new results.params[index].c_levels = c_levels_new self.params = results.params self.size = results.size return Results() else: self.wd_points = np.array([]) self.params = np.array([]) self.size = 0 return results class CurvatureInfo: def __init__(self, results, threshold = 0.05): self.threshold = threshold self.wd_points = results.wd_points self.new_wd_points_unique = None self.abs_transmissions = results.abs_transmissions self.n_points = self.abs_transmissions.size def new_points(self): self.curvature_positions, self.curvatures = derivative(self.wd_points, self.abs_transmissions, 2) self.abs_curvatures = np.absolute(self.curvatures) self.mean_curvatures = moving_average(self.abs_curvatures, 2) self.midpoint_curvatures = \ np.concatenate((np.array([self.abs_curvatures[0]]), self.mean_curvatures)) self.midpoint_curvatures = \ np.concatenate((self.midpoint_curvatures, np.array([self.abs_curvatures[self.n_points - 3]]))) self.midpoint_transmissions = moving_average(self.abs_transmissions, 2) self.midpoint_curvatures_normed = self.midpoint_curvatures / self.midpoint_transmissions self.midpoints = moving_average(self.wd_points, 2) self.intervals = np.diff(self.wd_points) self.num_of_sections_required = \ np.ceil(self.intervals * np.sqrt(self.midpoint_curvatures_normed / threshold)) new_wd_points = np.array([]) for index in np.arange(self.n_points - 1): multi_section = \ np.linspace(self.wd_points[index], self.wd_points[index + 1], self.num_of_sections_required[index] + 1) new_wd_points = np.concatenate((new_wd_points, multi_section)) unique_set = set(new_wd_points) - set(self.wd_points) self.new_wd_points_unique = np.array(list(unique_set)) return self.new_wd_points_unique def size_suggestion(edge_occupation, size, threshold): beta = fsolve(zero_func, 1, args=(edge_occupation, size - 1, size)) new_size = - np.log(threshold) / beta new_size = int(np.ceil(new_size)) return new_size def size_correction(edge_occupation, size, threshold): beta_estimate = np.log(1 + 1 / edge_occupation) / size beta = fsolve(zero_func, beta_estimate, args=(edge_occupation, size - 1, size)) new_size = 1 + np.log((1 - np.exp(-beta)) / threshold) / beta new_size = int(np.ceil(new_size)) return new_size def exponential_occupation(n, beta, size): factor = np.exp(-beta) f = np.power(factor, n) * (1 - factor) / (1 - np.power(factor, size)) return f def zero_func(beta, p, level, size): f = exponential_occupation(level, beta, size) f = f - p return f def hilbert_interpolation(new_wd_points, results): c_levels_array = np.array([params.c_levels for params in results.params]) t_levels_array = np.array([params.t_levels for params in results.params]) wd_points = results.wd_points c_interp = interp1d(wd_points, c_levels_array) t_interp = interp1d(wd_points, t_levels_array) base_params = results.params[0] params_list = [] for wd in new_wd_points: new_params = base_params.copy() new_params.c_levels = int(round(c_interp(wd))) new_params.t_levels = int(round(t_interp(wd))) params_list.append(new_params) params_array = np.array(params_list) return params_array def moving_average(interval, window_size): window = np.ones(int(window_size)) / float(window_size) averages = np.convolve(interval, window, 'same') return averages[window_size - 1 : averages.size] def derivative(x, y, n_derivative = 1): derivatives = np.zeros(y.size - 1) positions = np.zeros(x.size - 1) for index in np.arange(y.size - 1): grad = (y[index + 1] - y[index]) / (x[index + 1] - x[index]) position = np.mean([x[index], x[index + 1]]) derivatives[index] = grad positions[index] = position if n_derivative > 1: positions, derivatives = derivative(positions, derivatives, n_derivative - 1) return positions, derivatives def hamiltonian(params, wd): a = tensor(destroy(params.c_levels), qeye(params.t_levels)) sm = tensor(qeye(params.c_levels), destroy(params.t_levels)) H = (params.wc - wd) * a.dag() * a + (params.wq - wd) * sm.dag() * sm \ + params.chi * sm.dag() * sm * (sm.dag() * sm - 1) + params.g * (a.dag() * sm + a * sm.dag()) \ + params.eps * (a + a.dag()) return H def transmission_calc_array(queue): args = [] for index, value in enumerate(queue.wd_points): args.append([value, queue.params[index]]) steady_states = parallel_map(transmission_calc, args, num_cpus=10, progress_bar=TextProgressBar()) transmissions = np.array([steady_state[0] for steady_state in steady_states]) edge_occupations_c = np.array([steady_state[1] for steady_state in steady_states]) edge_occupations_c = np.absolute(edge_occupations_c) edge_occupations_t = np.array([steady_state[2] for steady_state in steady_states]) edge_occupations_t = np.absolute(edge_occupations_t) results = Results(queue.params, queue.wd_points, transmissions, edge_occupations_c, edge_occupations_t) abs_transmissions = np.absolute(transmissions) return results def transmission_calc(args): wd = args[0] params = args[1] a = tensor(destroy(params.c_levels), qeye(params.t_levels)) sm = tensor(qeye(params.c_levels), destroy(params.t_levels)) c_ops = [] c_ops.append(np.sqrt(params.kappa) * a) c_ops.append(np.sqrt(params.gamma) * sm) H = hamiltonian(params, wd) rho_ss = steadystate(H, c_ops) rho_c_ss = rho_ss.ptrace(0) rho_t_ss = rho_ss.ptrace(1) c_occupations = rho_c_ss.diag() t_occupations = rho_t_ss.diag() edge_occupation_c = c_occupations[params.c_levels - 1] edge_occupation_t = t_occupations[params.t_levels - 1] transmission = expect(a, rho_ss) return np.array([transmission, edge_occupation_c, edge_occupation_t]) def sweep(eps, wd_lower, wd_upper, params, threshold): hilbert_adjustment = False threshold_c = 0.001 threshold_t = 0.001 params.eps = eps wd_points = np.linspace(wd_lower, wd_upper, 10) params_array = np.array([params.copy() for wd in wd_points]) queue = Queue(params_array, wd_points) curvature_iterations = 0 results = Results() while (queue.size > 0) and (curvature_iterations < 3): print curvature_iterations curvature_iterations = curvature_iterations + 1 new_results = transmission_calc_array(queue) results = results.concatenate(new_results) if hilbert_adjustment == True: results = queue.hilbert_generate(results, threshold_c, threshold_t) hilbert_iterations = 0 while (queue.size > 0) and (hilbert_iterations < 3) and hilbert_adjustment: hilbert_iterations = hilbert_iterations + 1 results = transmission_calc_array(queue) results = queue.hilbert_generate(results, threshold_c, threshold_t) queue.curvature_generate(results) c_levels = [params_instance.c_levels for params_instance in results.params] t_levels = [params_instance.t_levels for params_instance in results.params] return results def multi_sweep(eps_array, wd_lower, wd_upper, params, threshold): multi_results_dict = dict() for eps in eps_array: multi_results_dict[eps] = sweep(eps, wd_lower, wd_upper, params, threshold) params = multi_results_dict[eps].params[0] print params.c_levels print params.t_levels return multi_results_dict if __name__ == '__main__': #wc, wq, eps, g, chi, kappa, gamma, t_levels, c_levels t_levels = 2 c_levels = 10 params = Parameters(10.4267, 9.39128, 0.0002, 0.3096, -0.097, 0.00146, 0.000833, t_levels, c_levels) threshold = 0.01 wd_lower = 10.495 wd_upper = 10.520 eps = 0.008 eps_array = np.array([eps]) multi_results = multi_sweep(eps_array, wd_lower, wd_upper, params, threshold) #with open('data.yml', 'w') as outfile: # yaml.dump(multi_results, outfile, default_flow_style=False) #multi_results = [] #multi_results = yaml.load(open('data.yml')) results = multi_results[eps] print results.params[0].t_levels print results.params[0].c_levels plt.scatter(results.wd_points, results.abs_transmissions) plt.title('txc: ' + str(t_levels) + 'x' + str(c_levels)) plt.show()
apache-2.0
pratapvardhan/pandas
asv_bench/benchmarks/frame_methods.py
2
14286
import string import warnings import numpy as np import pandas.util.testing as tm from pandas import (DataFrame, Series, MultiIndex, date_range, period_range, isnull, NaT) from .pandas_vb_common import setup # noqa class GetNumericData(object): goal_time = 0.2 def setup(self): self.df = DataFrame(np.random.randn(10000, 25)) self.df['foo'] = 'bar' self.df['bar'] = 'baz' with warnings.catch_warnings(record=True): self.df = self.df.consolidate() def time_frame_get_numeric_data(self): self.df._get_numeric_data() class Lookup(object): goal_time = 0.2 def setup(self): self.df = DataFrame(np.random.randn(10000, 8), columns=list('abcdefgh')) self.df['foo'] = 'bar' self.row_labels = list(self.df.index[::10])[:900] self.col_labels = list(self.df.columns) * 100 self.row_labels_all = np.array( list(self.df.index) * len(self.df.columns), dtype='object') self.col_labels_all = np.array( list(self.df.columns) * len(self.df.index), dtype='object') def time_frame_fancy_lookup(self): self.df.lookup(self.row_labels, self.col_labels) def time_frame_fancy_lookup_all(self): self.df.lookup(self.row_labels_all, self.col_labels_all) class Reindex(object): goal_time = 0.2 def setup(self): N = 10**3 self.df = DataFrame(np.random.randn(N * 10, N)) self.idx = np.arange(4 * N, 7 * N) self.df2 = DataFrame( {c: {0: np.random.randint(0, 2, N).astype(np.bool_), 1: np.random.randint(0, N, N).astype(np.int16), 2: np.random.randint(0, N, N).astype(np.int32), 3: np.random.randint(0, N, N).astype(np.int64)} [np.random.randint(0, 4)] for c in range(N)}) def time_reindex_axis0(self): self.df.reindex(self.idx) def time_reindex_axis1(self): self.df.reindex(columns=self.idx) def time_reindex_both_axes(self): self.df.reindex(index=self.idx, columns=self.idx) def time_reindex_both_axes_ix(self): self.df.ix[self.idx, self.idx] def time_reindex_upcast(self): self.df2.reindex(np.random.permutation(range(1200))) class Iteration(object): goal_time = 0.2 def setup(self): N = 1000 self.df = DataFrame(np.random.randn(N * 10, N)) self.df2 = DataFrame(np.random.randn(N * 50, 10)) self.df3 = DataFrame(np.random.randn(N, 5 * N), columns=['C' + str(c) for c in range(N * 5)]) def time_iteritems(self): # (monitor no-copying behaviour) if hasattr(self.df, '_item_cache'): self.df._item_cache.clear() for name, col in self.df.iteritems(): pass def time_iteritems_cached(self): for name, col in self.df.iteritems(): pass def time_iteritems_indexing(self): for col in self.df3: self.df3[col] def time_itertuples(self): for row in self.df2.itertuples(): pass def time_iterrows(self): for row in self.df.iterrows(): pass class ToString(object): goal_time = 0.2 def setup(self): self.df = DataFrame(np.random.randn(100, 10)) def time_to_string_floats(self): self.df.to_string() class ToHTML(object): goal_time = 0.2 def setup(self): nrows = 500 self.df2 = DataFrame(np.random.randn(nrows, 10)) self.df2[0] = period_range('2000', periods=nrows) self.df2[1] = range(nrows) def time_to_html_mixed(self): self.df2.to_html() class Repr(object): goal_time = 0.2 def setup(self): nrows = 10000 data = np.random.randn(nrows, 10) arrays = np.tile(np.random.randn(3, int(nrows / 100)), 100) idx = MultiIndex.from_arrays(arrays) self.df3 = DataFrame(data, index=idx) self.df4 = DataFrame(data, index=np.random.randn(nrows)) self.df_tall = DataFrame(np.random.randn(nrows, 10)) self.df_wide = DataFrame(np.random.randn(10, nrows)) def time_html_repr_trunc_mi(self): self.df3._repr_html_() def time_html_repr_trunc_si(self): self.df4._repr_html_() def time_repr_tall(self): repr(self.df_tall) def time_frame_repr_wide(self): repr(self.df_wide) class MaskBool(object): goal_time = 0.2 def setup(self): data = np.random.randn(1000, 500) df = DataFrame(data) df = df.where(df > 0) self.bools = df > 0 self.mask = isnull(df) def time_frame_mask_bools(self): self.bools.mask(self.mask) def time_frame_mask_floats(self): self.bools.astype(float).mask(self.mask) class Isnull(object): goal_time = 0.2 def setup(self): N = 10**3 self.df_no_null = DataFrame(np.random.randn(N, N)) sample = np.array([np.nan, 1.0]) data = np.random.choice(sample, (N, N)) self.df = DataFrame(data) sample = np.array(list(string.ascii_letters + string.whitespace)) data = np.random.choice(sample, (N, N)) self.df_strings = DataFrame(data) sample = np.array([NaT, np.nan, None, np.datetime64('NaT'), np.timedelta64('NaT'), 0, 1, 2.0, '', 'abcd']) data = np.random.choice(sample, (N, N)) self.df_obj = DataFrame(data) def time_isnull_floats_no_null(self): isnull(self.df_no_null) def time_isnull(self): isnull(self.df) def time_isnull_strngs(self): isnull(self.df_strings) def time_isnull_obj(self): isnull(self.df_obj) class Fillna(object): goal_time = 0.2 params = ([True, False], ['pad', 'bfill']) param_names = ['inplace', 'method'] def setup(self, inplace, method): values = np.random.randn(10000, 100) values[::2] = np.nan self.df = DataFrame(values) def time_frame_fillna(self, inplace, method): self.df.fillna(inplace=inplace, method=method) class Dropna(object): goal_time = 0.2 params = (['all', 'any'], [0, 1]) param_names = ['how', 'axis'] def setup(self, how, axis): self.df = DataFrame(np.random.randn(10000, 1000)) self.df.ix[50:1000, 20:50] = np.nan self.df.ix[2000:3000] = np.nan self.df.ix[:, 60:70] = np.nan self.df_mixed = self.df.copy() self.df_mixed['foo'] = 'bar' def time_dropna(self, how, axis): self.df.dropna(how=how, axis=axis) def time_dropna_axis_mixed_dtypes(self, how, axis): self.df_mixed.dropna(how=how, axis=axis) class Count(object): goal_time = 0.2 params = [0, 1] param_names = ['axis'] def setup(self, axis): self.df = DataFrame(np.random.randn(10000, 1000)) self.df.ix[50:1000, 20:50] = np.nan self.df.ix[2000:3000] = np.nan self.df.ix[:, 60:70] = np.nan self.df_mixed = self.df.copy() self.df_mixed['foo'] = 'bar' self.df.index = MultiIndex.from_arrays([self.df.index, self.df.index]) self.df.columns = MultiIndex.from_arrays([self.df.columns, self.df.columns]) self.df_mixed.index = MultiIndex.from_arrays([self.df_mixed.index, self.df_mixed.index]) self.df_mixed.columns = MultiIndex.from_arrays([self.df_mixed.columns, self.df_mixed.columns]) def time_count_level_multi(self, axis): self.df.count(axis=axis, level=1) def time_count_level_mixed_dtypes_multi(self, axis): self.df_mixed.count(axis=axis, level=1) class Apply(object): goal_time = 0.2 def setup(self): self.df = DataFrame(np.random.randn(1000, 100)) self.s = Series(np.arange(1028.0)) self.df2 = DataFrame({i: self.s for i in range(1028)}) self.df3 = DataFrame(np.random.randn(1000, 3), columns=list('ABC')) def time_apply_user_func(self): self.df2.apply(lambda x: np.corrcoef(x, self.s)[(0, 1)]) def time_apply_axis_1(self): self.df.apply(lambda x: x + 1, axis=1) def time_apply_lambda_mean(self): self.df.apply(lambda x: x.mean()) def time_apply_np_mean(self): self.df.apply(np.mean) def time_apply_pass_thru(self): self.df.apply(lambda x: x) def time_apply_ref_by_name(self): self.df3.apply(lambda x: x['A'] + x['B'], axis=1) class Dtypes(object): goal_time = 0.2 def setup(self): self.df = DataFrame(np.random.randn(1000, 1000)) def time_frame_dtypes(self): self.df.dtypes class Equals(object): goal_time = 0.2 def setup(self): N = 10**3 self.float_df = DataFrame(np.random.randn(N, N)) self.float_df_nan = self.float_df.copy() self.float_df_nan.iloc[-1, -1] = np.nan self.object_df = DataFrame('foo', index=range(N), columns=range(N)) self.object_df_nan = self.object_df.copy() self.object_df_nan.iloc[-1, -1] = np.nan self.nonunique_cols = self.object_df.copy() self.nonunique_cols.columns = ['A'] * len(self.nonunique_cols.columns) self.nonunique_cols_nan = self.nonunique_cols.copy() self.nonunique_cols_nan.iloc[-1, -1] = np.nan def time_frame_float_equal(self): self.float_df.equals(self.float_df) def time_frame_float_unequal(self): self.float_df.equals(self.float_df_nan) def time_frame_nonunique_equal(self): self.nonunique_cols.equals(self.nonunique_cols) def time_frame_nonunique_unequal(self): self.nonunique_cols.equals(self.nonunique_cols_nan) def time_frame_object_equal(self): self.object_df.equals(self.object_df) def time_frame_object_unequal(self): self.object_df.equals(self.object_df_nan) class Interpolate(object): goal_time = 0.2 params = [None, 'infer'] param_names = ['downcast'] def setup(self, downcast): N = 10000 # this is the worst case, where every column has NaNs. self.df = DataFrame(np.random.randn(N, 100)) self.df.values[::2] = np.nan self.df2 = DataFrame({'A': np.arange(0, N), 'B': np.random.randint(0, 100, N), 'C': np.random.randn(N), 'D': np.random.randn(N)}) self.df2.loc[1::5, 'A'] = np.nan self.df2.loc[1::5, 'C'] = np.nan def time_interpolate(self, downcast): self.df.interpolate(downcast=downcast) def time_interpolate_some_good(self, downcast): self.df2.interpolate(downcast=downcast) class Shift(object): # frame shift speedup issue-5609 goal_time = 0.2 params = [0, 1] param_names = ['axis'] def setup(self, axis): self.df = DataFrame(np.random.rand(10000, 500)) def time_shift(self, axis): self.df.shift(1, axis=axis) class Nunique(object): def setup(self): self.df = DataFrame(np.random.randn(10000, 1000)) def time_frame_nunique(self): self.df.nunique() class Duplicated(object): goal_time = 0.2 def setup(self): n = (1 << 20) t = date_range('2015-01-01', freq='S', periods=(n // 64)) xs = np.random.randn(n // 64).round(2) self.df = DataFrame({'a': np.random.randint(-1 << 8, 1 << 8, n), 'b': np.random.choice(t, n), 'c': np.random.choice(xs, n)}) self.df2 = DataFrame(np.random.randn(1000, 100).astype(str)).T def time_frame_duplicated(self): self.df.duplicated() def time_frame_duplicated_wide(self): self.df2.duplicated() class XS(object): goal_time = 0.2 params = [0, 1] param_names = ['axis'] def setup(self, axis): self.N = 10**4 self.df = DataFrame(np.random.randn(self.N, self.N)) def time_frame_xs(self, axis): self.df.xs(self.N / 2, axis=axis) class SortValues(object): goal_time = 0.2 params = [True, False] param_names = ['ascending'] def setup(self, ascending): self.df = DataFrame(np.random.randn(1000000, 2), columns=list('AB')) def time_frame_sort_values(self, ascending): self.df.sort_values(by='A', ascending=ascending) class SortIndexByColumns(object): goal_time = 0.2 def setup(self): N = 10000 K = 10 self.df = DataFrame({'key1': tm.makeStringIndex(N).values.repeat(K), 'key2': tm.makeStringIndex(N).values.repeat(K), 'value': np.random.randn(N * K)}) def time_frame_sort_values_by_columns(self): self.df.sort_values(by=['key1', 'key2']) class Quantile(object): goal_time = 0.2 params = [0, 1] param_names = ['axis'] def setup(self, axis): self.df = DataFrame(np.random.randn(1000, 3), columns=list('ABC')) def time_frame_quantile(self, axis): self.df.quantile([0.1, 0.5], axis=axis) class GetDtypeCounts(object): # 2807 goal_time = 0.2 def setup(self): self.df = DataFrame(np.random.randn(10, 10000)) def time_frame_get_dtype_counts(self): self.df.get_dtype_counts() def time_info(self): self.df.info() class NSort(object): goal_time = 0.2 params = ['first', 'last', 'all'] param_names = ['keep'] def setup(self, keep): self.df = DataFrame(np.random.randn(1000, 3), columns=list('ABC')) def time_nlargest(self, keep): self.df.nlargest(100, 'A', keep=keep) def time_nsmallest(self, keep): self.df.nsmallest(100, 'A', keep=keep) class Describe(object): goal_time = 0.2 def setup(self): self.df = DataFrame({ 'a': np.random.randint(0, 100, int(1e6)), 'b': np.random.randint(0, 100, int(1e6)), 'c': np.random.randint(0, 100, int(1e6)) }) def time_series_describe(self): self.df['a'].describe() def time_dataframe_describe(self): self.df.describe()
bsd-3-clause
draperjames/bokeh
bokeh/charts/builders/chord_builder.py
7
12304
"""This is the Bokeh charts interface. It gives you a high level API to build complex plot is a simple way. This is the Chord class which lets you build your Chord charts just passing the arguments to the Chart class and calling the proper functions. """ # ----------------------------------------------------------------------------- # Copyright (c) 2012 - 2016, Continuum Analytics, Inc. All rights reserved. # # Powered by the Bokeh Development Team. # # The full license is in the file LICENSE.txt, distributed with this software. # ----------------------------------------------------------------------------- # ----------------------------------------------------------------------------- # Imports # ----------------------------------------------------------------------------- from __future__ import absolute_import, division import numpy as np import pandas as pd from math import cos, sin, pi from bokeh.charts.properties import Dimension from bokeh.charts.builder import create_and_build, Builder from bokeh.charts.attributes import MarkerAttr, ColorAttr, CatAttr from bokeh.charts.utils import color_in_equal_space, help from bokeh.models import Range1d from bokeh.models.glyphs import Arc, Bezier, Text from bokeh.models.renderers import GlyphRenderer from bokeh.models.sources import ColumnDataSource from bokeh.core.properties import Instance, Bool, String, Array, Float, Any, Seq, Either, Int # ----------------------------------------------------------------------------- # Classes and functions # ----------------------------------------------------------------------------- class Area: """ It represents an arc area. It will create a list of available points through the arc representing that area and then those points will be used as start and end for the beziers lines. """ def __init__(self, n_conn, start_point, end_point): # Number of connections in that arc area self.n_conn = n_conn # The start point of the arc representing the area self.start_point = start_point self.end_point = end_point # Equally spaced points between start point and end point free_points_angles = np.linspace(start_point, end_point, n_conn) # A list of available X,Y in the chart to consume by each bezier's start and end point self.free_points = [[cos(angle), sin(angle)] for angle in free_points_angles] assert self.n_conn == len(self.free_points) class ChordBuilder(Builder): """ This is the Chord builder and it is in charge of plotting Chord graphs in an easy and intuitive way. Essentially, we provide a way to ingest the data, make the proper calculations and push the references into a source object. We additionally make calculations for the ranges. And finally add the needed glyphs (markers) taking the references from the source. """ default_attributes = {'color': ColorAttr(), 'marker': MarkerAttr(), 'stack': CatAttr()} dimensions = ['values'] values = Dimension('values') arcs_data = Instance(ColumnDataSource) text_data = Instance(ColumnDataSource) connection_data = Instance(ColumnDataSource) origin = String() destination = String() value = Any() square_matrix = Bool() label = Seq(Any()) matrix = Array(Array(Either(Float(), Int()))) def set_ranges(self): rng = 1.1 if not self.label else 1.8 self.x_range = Range1d(-rng, rng) self.y_range = Range1d(-rng, rng) def setup(self): # Process only if not a square_matrix if not self.square_matrix: source = self.values._data[self.origin] target = self.values._data[self.destination] union = source.append(target).unique() N = union.shape[0] m = pd.DataFrame(np.zeros((N, N)), columns=union, index=union) if not self.label: self.label = list(union) if self.value is None: for _, row in self.values._data.iterrows(): m[row[self.origin]][row[self.destination]] += 1 self.matrix = m.get_values() if self.value is not None: if isinstance(self.value, int) or isinstance(self.value, float): for _, row in self.values._data.iterrows(): m[row[self.origin]][row[self.destination]] = self.value self.matrix = m.get_values() elif isinstance(self.value, str): for _, row in self.values._data.iterrows(): m[row[self.origin]][row[self.destination]] = row[self.value] self.matrix = m.get_values().T else: # It's already a square matrix self.matrix = self._data.df.get_values() if self.label: assert len(self.label) == self.matrix.shape[0] def process_data(self): weights_of_areas = (self.matrix.sum(axis=0) + self.matrix.sum(axis=1)) - self.matrix.diagonal() areas_in_radians = (weights_of_areas / weights_of_areas.sum()) * (2 * pi) # We add a zero in the begging for the cumulative sum points = np.zeros((areas_in_radians.shape[0] + 1)) points[1:] = areas_in_radians points = points.cumsum() colors = [color_in_equal_space(area / areas_in_radians.shape[0]) for area in range(areas_in_radians.shape[0])] arcs_data = pd.DataFrame({ 'start_angle': points[:-1], 'end_angle': points[1:], 'line_color': colors }) self.arcs_data = ColumnDataSource(arcs_data) # Text if self.label: text_radius = 1.1 angles = (points[:-1]+points[1:])/2.0 text_positions = pd.DataFrame({ 'angles': angles, 'text_x': np.cos(angles) * text_radius, 'text_y': np.sin(angles) * text_radius, 'text': list(self.label) }) self.text_data = ColumnDataSource(text_positions) # Lines all_areas = [] for i in range(areas_in_radians.shape[0]): all_areas.append(Area(weights_of_areas[i], points[:-1][i], points[1:][i])) all_connections = [] for j, region1 in enumerate(self.matrix): # Get the connections origin region source = all_areas[j] color = colors[j] weight = weights_of_areas[j] for k, region2 in enumerate(region1): # Get the connection destination region target = all_areas[k] for _ in range(int(region2)): p1 = source.free_points.pop() p2 = target.free_points.pop() # Get both regions free points and create a connection with the data all_connections.append(p1 + p2 + [color, weight]) connections_df = pd.DataFrame(all_connections, dtype=str) connections_df.columns = ["start_x", "start_y", "end_x", "end_y", "colors", "weight"] connections_df["cx0"] = connections_df.start_x.astype("float64")/2 connections_df["cy0"] = connections_df.start_y.astype("float64")/2 connections_df["cx1"] = connections_df.end_x.astype("float64")/2 connections_df["cy1"] = connections_df.end_y.astype("float64")/2 connections_df.weight = (connections_df.weight.astype("float64")/connections_df.weight.astype("float64").sum()) * 3000 self.connection_data = ColumnDataSource(connections_df) def yield_renderers(self): """Use the marker glyphs to display the arcs and beziers. Takes reference points from data loaded at the ColumnDataSource. """ beziers = Bezier(x0='start_x', y0='start_y', x1='end_x', y1='end_y', cx0='cx0', cy0='cy0', cx1='cx1', cy1='cy1', line_alpha='weight', line_color='colors') yield GlyphRenderer(data_source=self.connection_data, glyph=beziers) arcs = Arc(x=0, y=0, radius=1, line_width=10, start_angle='start_angle', end_angle='end_angle', line_color='line_color') yield GlyphRenderer(data_source=self.arcs_data, glyph=arcs) if self.label: text_props = { "text_color": "#000000", "text_font_size": "8pt", "text_align": "left", "text_baseline": "middle" } labels = Text(x='text_x', y='text_y', text='text', angle='angles', **text_props ) yield GlyphRenderer(data_source=self.text_data, glyph=labels) @help(ChordBuilder) def Chord(data, source=None, target=None, value=None, square_matrix=False, label=None, xgrid=False, ygrid=False, **kw): """ Create a chord chart using :class:`ChordBuilder <bokeh.charts.builders.chord_builder.ChordBuilder>` to render a chord graph from a variety of value forms. This chart displays the inter-relationships between data in a matrix. The data can be generated by the chart interface. Given a :class:`DataFrame <pandas.DataFrame>`, select two columns to be used as arcs with `source` and `target` attributes, passing by the name of those columns. The :class:`Chord <bokeh.charts.builders.chord_builder.Chord>` chart will then deduce the relationship between the arcs. The value of the connections can be inferred automatically by counting `source` and `target`. If you prefer you can assign a fixed value for all the connections with `value` simply passing by a number. A third option is to pass a reference to a third column in the :class:`DataFrame <pandas.DataFrame>` with the values for the connections. If you want to plot the relationships in a squared matrix, simply pass the matrix and set `square_matrix` attribute to `True`. Reference: `Chord diagram on Wikipedia <https://en.wikipedia.org/wiki/Chord_diagram>`_ Args: data (:ref:`userguide_charts_data_types`): the data source for the chart. source (list(str) or str, optional): Data source to use as origin of the connection to a destination. target (list(str) or str, optional): Data source to use as destination of a connection. value (list(num) or num, optional): The value the connection should have. square_matrix (bool, optional): If square matrix, avoid any calculations during the setup. label (list(str), optional): The labels to be put in the areas. Returns: :class:`Chart`: includes glyph renderers that generate the chord Examples: .. bokeh-plot:: :source-position: above import pandas as pd from bokeh.charts import Chord from bokeh.io import show, output_file from bokeh.sampledata.les_mis import data nodes = data['nodes'] links = data['links'] nodes_df = pd.DataFrame(nodes) links_df = pd.DataFrame(links) source_data = links_df.merge(nodes_df, how='left', left_on='source', right_index=True) source_data = source_data.merge(nodes_df, how='left', left_on='target', right_index=True) source_data = source_data[source_data["value"] > 5] # Select those with 5 or more connections chord_from_df = Chord(source_data, source="name_x", target="name_y", value="value") output_file('chord_from_df.html') show(chord_from_df) """ kw["origin"] = source kw["destination"] = target kw["value"] = value kw["square_matrix"] = square_matrix kw["label"] = label kw['xgrid'] = xgrid kw['ygrid'] = ygrid chart = create_and_build(ChordBuilder, data, **kw) chart.left[0].visible = False chart.below[0].visible = False chart.outline_line_color = None return chart
bsd-3-clause
matthieudumont/dipy
dipy/viz/tests/test_fvtk.py
4
3224
"""Testing visualization with fvtk.""" import os import numpy as np from dipy.viz import fvtk from dipy import data import numpy.testing as npt from dipy.testing.decorators import xvfb_it use_xvfb = os.environ.get('TEST_WITH_XVFB', False) if use_xvfb == 'skip': skip_it = True else: skip_it = False @npt.dec.skipif(not fvtk.have_vtk or not fvtk.have_vtk_colors or skip_it) @xvfb_it def test_fvtk_functions(): # This tests will fail if any of the given actors changed inputs or do # not exist # Create a renderer r = fvtk.ren() # Create 2 lines with 2 different colors lines = [np.random.rand(10, 3), np.random.rand(20, 3)] colors = np.random.rand(2, 3) c = fvtk.line(lines, colors) fvtk.add(r, c) # create streamtubes of the same lines and shift them a bit c2 = fvtk.streamtube(lines, colors) c2.SetPosition(2, 0, 0) fvtk.add(r, c2) # Create a volume and return a volumetric actor using volumetric rendering vol = 100 * np.random.rand(100, 100, 100) vol = vol.astype('uint8') r = fvtk.ren() v = fvtk.volume(vol) fvtk.add(r, v) # Remove all objects fvtk.rm_all(r) # Put some text l = fvtk.label(r, text='Yes Men') fvtk.add(r, l) # Slice the volume slicer = fvtk.slicer(vol) slicer.display(50, None, None) fvtk.add(r, slicer) # Change the position of the active camera fvtk.camera(r, pos=(0.6, 0, 0), verbose=False) fvtk.clear(r) # Peak directions p = fvtk.peaks(np.random.rand(3, 3, 3, 5, 3)) fvtk.add(r, p) p2 = fvtk.peaks(np.random.rand(3, 3, 3, 5, 3), np.random.rand(3, 3, 3, 5), colors=(0, 1, 0)) fvtk.add(r, p2) @npt.dec.skipif(not fvtk.have_vtk or not fvtk.have_vtk_colors or skip_it) @xvfb_it def test_fvtk_ellipsoid(): evals = np.array([1.4, .35, .35]) * 10 ** (-3) evecs = np.eye(3) mevals = np.zeros((3, 2, 4, 3)) mevecs = np.zeros((3, 2, 4, 3, 3)) mevals[..., :] = evals mevecs[..., :, :] = evecs from dipy.data import get_sphere sphere = get_sphere('symmetric724') ren = fvtk.ren() fvtk.add(ren, fvtk.tensor(mevals, mevecs, sphere=sphere)) fvtk.add(ren, fvtk.tensor(mevals, mevecs, np.ones(mevals.shape), sphere=sphere)) npt.assert_equal(ren.GetActors().GetNumberOfItems(), 2) def test_colormap(): v = np.linspace(0., .5) map1 = fvtk.create_colormap(v, 'bone', auto=True) map2 = fvtk.create_colormap(v, 'bone', auto=False) npt.assert_(not np.allclose(map1, map2)) npt.assert_raises(ValueError, fvtk.create_colormap, np.ones((2, 3))) npt.assert_raises(ValueError, fvtk.create_colormap, v, 'no such map') @npt.dec.skipif(not fvtk.have_matplotlib) def test_colormaps_matplotlib(): v = np.random.random(1000) for name in 'jet', 'Blues', 'Accent', 'bone': # Matplotlib version of get_cmap rgba1 = fvtk.get_cmap(name)(v) # Dipy version of get_cmap rgba2 = data.get_cmap(name)(v) # dipy's colormaps are close to matplotlibs colormaps, but not perfect npt.assert_array_almost_equal(rgba1, rgba2, 1) if __name__ == "__main__": npt.run_module_suite()
bsd-3-clause
fyffyt/scikit-learn
sklearn/mixture/tests/test_gmm.py
200
17427
import unittest import copy import sys from nose.tools import assert_true import numpy as np from numpy.testing import (assert_array_equal, assert_array_almost_equal, assert_raises) from scipy import stats from sklearn import mixture from sklearn.datasets.samples_generator import make_spd_matrix from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_raise_message from sklearn.metrics.cluster import adjusted_rand_score from sklearn.externals.six.moves import cStringIO as StringIO rng = np.random.RandomState(0) def test_sample_gaussian(): # Test sample generation from mixture.sample_gaussian where covariance # is diagonal, spherical and full n_features, n_samples = 2, 300 axis = 1 mu = rng.randint(10) * rng.rand(n_features) cv = (rng.rand(n_features) + 1.0) ** 2 samples = mixture.sample_gaussian( mu, cv, covariance_type='diag', n_samples=n_samples) assert_true(np.allclose(samples.mean(axis), mu, atol=1.3)) assert_true(np.allclose(samples.var(axis), cv, atol=1.5)) # the same for spherical covariances cv = (rng.rand() + 1.0) ** 2 samples = mixture.sample_gaussian( mu, cv, covariance_type='spherical', n_samples=n_samples) assert_true(np.allclose(samples.mean(axis), mu, atol=1.5)) assert_true(np.allclose( samples.var(axis), np.repeat(cv, n_features), atol=1.5)) # and for full covariances A = rng.randn(n_features, n_features) cv = np.dot(A.T, A) + np.eye(n_features) samples = mixture.sample_gaussian( mu, cv, covariance_type='full', n_samples=n_samples) assert_true(np.allclose(samples.mean(axis), mu, atol=1.3)) assert_true(np.allclose(np.cov(samples), cv, atol=2.5)) # Numerical stability check: in SciPy 0.12.0 at least, eigh may return # tiny negative values in its second return value. from sklearn.mixture import sample_gaussian x = sample_gaussian([0, 0], [[4, 3], [1, .1]], covariance_type='full', random_state=42) print(x) assert_true(np.isfinite(x).all()) def _naive_lmvnpdf_diag(X, mu, cv): # slow and naive implementation of lmvnpdf ref = np.empty((len(X), len(mu))) stds = np.sqrt(cv) for i, (m, std) in enumerate(zip(mu, stds)): ref[:, i] = np.log(stats.norm.pdf(X, m, std)).sum(axis=1) return ref def test_lmvnpdf_diag(): # test a slow and naive implementation of lmvnpdf and # compare it to the vectorized version (mixture.lmvnpdf) to test # for correctness n_features, n_components, n_samples = 2, 3, 10 mu = rng.randint(10) * rng.rand(n_components, n_features) cv = (rng.rand(n_components, n_features) + 1.0) ** 2 X = rng.randint(10) * rng.rand(n_samples, n_features) ref = _naive_lmvnpdf_diag(X, mu, cv) lpr = mixture.log_multivariate_normal_density(X, mu, cv, 'diag') assert_array_almost_equal(lpr, ref) def test_lmvnpdf_spherical(): n_features, n_components, n_samples = 2, 3, 10 mu = rng.randint(10) * rng.rand(n_components, n_features) spherecv = rng.rand(n_components, 1) ** 2 + 1 X = rng.randint(10) * rng.rand(n_samples, n_features) cv = np.tile(spherecv, (n_features, 1)) reference = _naive_lmvnpdf_diag(X, mu, cv) lpr = mixture.log_multivariate_normal_density(X, mu, spherecv, 'spherical') assert_array_almost_equal(lpr, reference) def test_lmvnpdf_full(): n_features, n_components, n_samples = 2, 3, 10 mu = rng.randint(10) * rng.rand(n_components, n_features) cv = (rng.rand(n_components, n_features) + 1.0) ** 2 X = rng.randint(10) * rng.rand(n_samples, n_features) fullcv = np.array([np.diag(x) for x in cv]) reference = _naive_lmvnpdf_diag(X, mu, cv) lpr = mixture.log_multivariate_normal_density(X, mu, fullcv, 'full') assert_array_almost_equal(lpr, reference) def test_lvmpdf_full_cv_non_positive_definite(): n_features, n_samples = 2, 10 rng = np.random.RandomState(0) X = rng.randint(10) * rng.rand(n_samples, n_features) mu = np.mean(X, 0) cv = np.array([[[-1, 0], [0, 1]]]) expected_message = "'covars' must be symmetric, positive-definite" assert_raise_message(ValueError, expected_message, mixture.log_multivariate_normal_density, X, mu, cv, 'full') def test_GMM_attributes(): n_components, n_features = 10, 4 covariance_type = 'diag' g = mixture.GMM(n_components, covariance_type, random_state=rng) weights = rng.rand(n_components) weights = weights / weights.sum() means = rng.randint(-20, 20, (n_components, n_features)) assert_true(g.n_components == n_components) assert_true(g.covariance_type == covariance_type) g.weights_ = weights assert_array_almost_equal(g.weights_, weights) g.means_ = means assert_array_almost_equal(g.means_, means) covars = (0.1 + 2 * rng.rand(n_components, n_features)) ** 2 g.covars_ = covars assert_array_almost_equal(g.covars_, covars) assert_raises(ValueError, g._set_covars, []) assert_raises(ValueError, g._set_covars, np.zeros((n_components - 2, n_features))) assert_raises(ValueError, mixture.GMM, n_components=20, covariance_type='badcovariance_type') class GMMTester(): do_test_eval = True def _setUp(self): self.n_components = 10 self.n_features = 4 self.weights = rng.rand(self.n_components) self.weights = self.weights / self.weights.sum() self.means = rng.randint(-20, 20, (self.n_components, self.n_features)) self.threshold = -0.5 self.I = np.eye(self.n_features) self.covars = { 'spherical': (0.1 + 2 * rng.rand(self.n_components, self.n_features)) ** 2, 'tied': (make_spd_matrix(self.n_features, random_state=0) + 5 * self.I), 'diag': (0.1 + 2 * rng.rand(self.n_components, self.n_features)) ** 2, 'full': np.array([make_spd_matrix(self.n_features, random_state=0) + 5 * self.I for x in range(self.n_components)])} def test_eval(self): if not self.do_test_eval: return # DPGMM does not support setting the means and # covariances before fitting There is no way of fixing this # due to the variational parameters being more expressive than # covariance matrices g = self.model(n_components=self.n_components, covariance_type=self.covariance_type, random_state=rng) # Make sure the means are far apart so responsibilities.argmax() # picks the actual component used to generate the observations. g.means_ = 20 * self.means g.covars_ = self.covars[self.covariance_type] g.weights_ = self.weights gaussidx = np.repeat(np.arange(self.n_components), 5) n_samples = len(gaussidx) X = rng.randn(n_samples, self.n_features) + g.means_[gaussidx] ll, responsibilities = g.score_samples(X) self.assertEqual(len(ll), n_samples) self.assertEqual(responsibilities.shape, (n_samples, self.n_components)) assert_array_almost_equal(responsibilities.sum(axis=1), np.ones(n_samples)) assert_array_equal(responsibilities.argmax(axis=1), gaussidx) def test_sample(self, n=100): g = self.model(n_components=self.n_components, covariance_type=self.covariance_type, random_state=rng) # Make sure the means are far apart so responsibilities.argmax() # picks the actual component used to generate the observations. g.means_ = 20 * self.means g.covars_ = np.maximum(self.covars[self.covariance_type], 0.1) g.weights_ = self.weights samples = g.sample(n) self.assertEqual(samples.shape, (n, self.n_features)) def test_train(self, params='wmc'): g = mixture.GMM(n_components=self.n_components, covariance_type=self.covariance_type) g.weights_ = self.weights g.means_ = self.means g.covars_ = 20 * self.covars[self.covariance_type] # Create a training set by sampling from the predefined distribution. X = g.sample(n_samples=100) g = self.model(n_components=self.n_components, covariance_type=self.covariance_type, random_state=rng, min_covar=1e-1, n_iter=1, init_params=params) g.fit(X) # Do one training iteration at a time so we can keep track of # the log likelihood to make sure that it increases after each # iteration. trainll = [] for _ in range(5): g.params = params g.init_params = '' g.fit(X) trainll.append(self.score(g, X)) g.n_iter = 10 g.init_params = '' g.params = params g.fit(X) # finish fitting # Note that the log likelihood will sometimes decrease by a # very small amount after it has more or less converged due to # the addition of min_covar to the covariance (to prevent # underflow). This is why the threshold is set to -0.5 # instead of 0. delta_min = np.diff(trainll).min() self.assertTrue( delta_min > self.threshold, "The min nll increase is %f which is lower than the admissible" " threshold of %f, for model %s. The likelihoods are %s." % (delta_min, self.threshold, self.covariance_type, trainll)) def test_train_degenerate(self, params='wmc'): # Train on degenerate data with 0 in some dimensions # Create a training set by sampling from the predefined distribution. X = rng.randn(100, self.n_features) X.T[1:] = 0 g = self.model(n_components=2, covariance_type=self.covariance_type, random_state=rng, min_covar=1e-3, n_iter=5, init_params=params) g.fit(X) trainll = g.score(X) self.assertTrue(np.sum(np.abs(trainll / 100 / X.shape[1])) < 5) def test_train_1d(self, params='wmc'): # Train on 1-D data # Create a training set by sampling from the predefined distribution. X = rng.randn(100, 1) # X.T[1:] = 0 g = self.model(n_components=2, covariance_type=self.covariance_type, random_state=rng, min_covar=1e-7, n_iter=5, init_params=params) g.fit(X) trainll = g.score(X) if isinstance(g, mixture.DPGMM): self.assertTrue(np.sum(np.abs(trainll / 100)) < 5) else: self.assertTrue(np.sum(np.abs(trainll / 100)) < 2) def score(self, g, X): return g.score(X).sum() class TestGMMWithSphericalCovars(unittest.TestCase, GMMTester): covariance_type = 'spherical' model = mixture.GMM setUp = GMMTester._setUp class TestGMMWithDiagonalCovars(unittest.TestCase, GMMTester): covariance_type = 'diag' model = mixture.GMM setUp = GMMTester._setUp class TestGMMWithTiedCovars(unittest.TestCase, GMMTester): covariance_type = 'tied' model = mixture.GMM setUp = GMMTester._setUp class TestGMMWithFullCovars(unittest.TestCase, GMMTester): covariance_type = 'full' model = mixture.GMM setUp = GMMTester._setUp def test_multiple_init(): # Test that multiple inits does not much worse than a single one X = rng.randn(30, 5) X[:10] += 2 g = mixture.GMM(n_components=2, covariance_type='spherical', random_state=rng, min_covar=1e-7, n_iter=5) train1 = g.fit(X).score(X).sum() g.n_init = 5 train2 = g.fit(X).score(X).sum() assert_true(train2 >= train1 - 1.e-2) def test_n_parameters(): # Test that the right number of parameters is estimated n_samples, n_dim, n_components = 7, 5, 2 X = rng.randn(n_samples, n_dim) n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41} for cv_type in ['full', 'tied', 'diag', 'spherical']: g = mixture.GMM(n_components=n_components, covariance_type=cv_type, random_state=rng, min_covar=1e-7, n_iter=1) g.fit(X) assert_true(g._n_parameters() == n_params[cv_type]) def test_1d_1component(): # Test all of the covariance_types return the same BIC score for # 1-dimensional, 1 component fits. n_samples, n_dim, n_components = 100, 1, 1 X = rng.randn(n_samples, n_dim) g_full = mixture.GMM(n_components=n_components, covariance_type='full', random_state=rng, min_covar=1e-7, n_iter=1) g_full.fit(X) g_full_bic = g_full.bic(X) for cv_type in ['tied', 'diag', 'spherical']: g = mixture.GMM(n_components=n_components, covariance_type=cv_type, random_state=rng, min_covar=1e-7, n_iter=1) g.fit(X) assert_array_almost_equal(g.bic(X), g_full_bic) def assert_fit_predict_correct(model, X): model2 = copy.deepcopy(model) predictions_1 = model.fit(X).predict(X) predictions_2 = model2.fit_predict(X) assert adjusted_rand_score(predictions_1, predictions_2) == 1.0 def test_fit_predict(): """ test that gmm.fit_predict is equivalent to gmm.fit + gmm.predict """ lrng = np.random.RandomState(101) n_samples, n_dim, n_comps = 100, 2, 2 mu = np.array([[8, 8]]) component_0 = lrng.randn(n_samples, n_dim) component_1 = lrng.randn(n_samples, n_dim) + mu X = np.vstack((component_0, component_1)) for m_constructor in (mixture.GMM, mixture.VBGMM, mixture.DPGMM): model = m_constructor(n_components=n_comps, covariance_type='full', min_covar=1e-7, n_iter=5, random_state=np.random.RandomState(0)) assert_fit_predict_correct(model, X) model = mixture.GMM(n_components=n_comps, n_iter=0) z = model.fit_predict(X) assert np.all(z == 0), "Quick Initialization Failed!" def test_aic(): # Test the aic and bic criteria n_samples, n_dim, n_components = 50, 3, 2 X = rng.randn(n_samples, n_dim) SGH = 0.5 * (X.var() + np.log(2 * np.pi)) # standard gaussian entropy for cv_type in ['full', 'tied', 'diag', 'spherical']: g = mixture.GMM(n_components=n_components, covariance_type=cv_type, random_state=rng, min_covar=1e-7) g.fit(X) aic = 2 * n_samples * SGH * n_dim + 2 * g._n_parameters() bic = (2 * n_samples * SGH * n_dim + np.log(n_samples) * g._n_parameters()) bound = n_dim * 3. / np.sqrt(n_samples) assert_true(np.abs(g.aic(X) - aic) / n_samples < bound) assert_true(np.abs(g.bic(X) - bic) / n_samples < bound) def check_positive_definite_covars(covariance_type): r"""Test that covariance matrices do not become non positive definite Due to the accumulation of round-off errors, the computation of the covariance matrices during the learning phase could lead to non-positive definite covariance matrices. Namely the use of the formula: .. math:: C = (\sum_i w_i x_i x_i^T) - \mu \mu^T instead of: .. math:: C = \sum_i w_i (x_i - \mu)(x_i - \mu)^T while mathematically equivalent, was observed a ``LinAlgError`` exception, when computing a ``GMM`` with full covariance matrices and fixed mean. This function ensures that some later optimization will not introduce the problem again. """ rng = np.random.RandomState(1) # we build a dataset with 2 2d component. The components are unbalanced # (respective weights 0.9 and 0.1) X = rng.randn(100, 2) X[-10:] += (3, 3) # Shift the 10 last points gmm = mixture.GMM(2, params="wc", covariance_type=covariance_type, min_covar=1e-3) # This is a non-regression test for issue #2640. The following call used # to trigger: # numpy.linalg.linalg.LinAlgError: 2-th leading minor not positive definite gmm.fit(X) if covariance_type == "diag" or covariance_type == "spherical": assert_greater(gmm.covars_.min(), 0) else: if covariance_type == "tied": covs = [gmm.covars_] else: covs = gmm.covars_ for c in covs: assert_greater(np.linalg.det(c), 0) def test_positive_definite_covars(): # Check positive definiteness for all covariance types for covariance_type in ["full", "tied", "diag", "spherical"]: yield check_positive_definite_covars, covariance_type def test_verbose_first_level(): # Create sample data X = rng.randn(30, 5) X[:10] += 2 g = mixture.GMM(n_components=2, n_init=2, verbose=1) old_stdout = sys.stdout sys.stdout = StringIO() try: g.fit(X) finally: sys.stdout = old_stdout def test_verbose_second_level(): # Create sample data X = rng.randn(30, 5) X[:10] += 2 g = mixture.GMM(n_components=2, n_init=2, verbose=2) old_stdout = sys.stdout sys.stdout = StringIO() try: g.fit(X) finally: sys.stdout = old_stdout
bsd-3-clause
Pold87/paparazzi_clean
sw/airborne/test/stabilization/compare_ref_quat.py
48
1123
#! /usr/bin/env python from __future__ import division, print_function, absolute_import import numpy as np import matplotlib.pyplot as plt from ref_quat_float import RefQuatFloat from ref_quat_int import RefQuatInt steps = 512 * 2 ref_float_res = np.zeros((steps, 3)) ref_int_res = np.zeros((steps, 3)) ref_float = RefQuatFloat() ref_int = RefQuatInt() q_sp = np.array([0.92387956, 0.38268346, 0., 0.]) ref_float.setpoint = q_sp ref_int.setpoint = q_sp #print(ref_int.setpoint) dt = 1/512 for i in range(0, steps): ref_float.update(dt) ref_float_res[i, :] = ref_float.eulers ref_int.update(dt) ref_int_res[i, :] = ref_int.eulers plt.figure(1) plt.subplot(311) plt.title("reference in euler angles") plt.plot(np.degrees(ref_float_res[:, 0]), 'g') plt.plot(np.degrees(ref_int_res[:, 0]), 'r') plt.ylabel("phi [deg]") plt.subplot(312) plt.plot(np.degrees(ref_float_res[:, 1]), 'g') plt.plot(np.degrees(ref_int_res[:, 1]), 'r') plt.ylabel("theta [deg]") plt.subplot(313) plt.plot(np.degrees(ref_float_res[:, 2]), 'g') plt.plot(np.degrees(ref_int_res[:, 2]), 'r') plt.ylabel("psi [deg]") plt.show()
gpl-2.0
balazssimon/ml-playground
udemy/Machine Learning A-Z/Part 3 - Classification/Section 20 - Random Forest Classification/random_forest_classification.py
6
2748
# Random Forest Classification # Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset dataset = pd.read_csv('Social_Network_Ads.csv') X = dataset.iloc[:, [2, 3]].values y = dataset.iloc[:, 4].values # Splitting the dataset into the Training set and Test set from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) # Feature Scaling from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) # Fitting Random Forest Classification to the Training set from sklearn.ensemble import RandomForestClassifier classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0) classifier.fit(X_train, y_train) # Predicting the Test set results y_pred = classifier.predict(X_test) # Making the Confusion Matrix from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred) # Visualising the Training set results from matplotlib.colors import ListedColormap X_set, y_set = X_train, y_train X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j) plt.title('Random Forest Classification (Training set)') plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend() plt.show() # Visualising the Test set results from matplotlib.colors import ListedColormap X_set, y_set = X_test, y_test X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j) plt.title('Random Forest Classification (Test set)') plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend() plt.show()
apache-2.0
anbasile/pmu
bot/bus.py
2
3488
import numpy as np import scipy.spatial as spatial import pandas as pd import time import datetime stops = pd.read_csv("data/stops.txt") stop_times = pd.read_csv("data/stop_times.txt") routes = pd.read_csv("data/routes.txt") trips = pd.read_csv("data/trips.txt") calendar = pd.read_csv("data/calendar.txt",names=['service_id','monday','tuesday','wednesday','thursday','friday','saturday','sunday','start_date','end_date'],header=0) def dow(date): days=["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] dayNumber=date.weekday() return days[dayNumber] def prettyprint(df): """Take a pandas dataframe in input and return a string containing one row per line""" temp = [] for i,row in df.iterrows(): temp.append(str(row.values)) response = '\n'.join(temp) if not temp: response = "No, mi spiace, ma non ci sono bus adesso..." return response def getstop(location): """Take a location in input and return the name of the nearest bus stop the ids of one or two (one per direction, if available) stop_id """ pt = location.latitude,location.longitude pt = np.asarray(pt) points = stops[['stop_lat','stop_lon']].values nn = points[spatial.KDTree(points).query(pt)[1]] #find nearest stop given a tree and a point (pt) stop_name = stops['stop_name'][(stops['stop_lat'] ==nn[0]) & (stops['stop_lon'] ==nn[1])].iloc[0] stop_id = stops['stop_id'][stops['stop_name'] == stop_name].iloc[0] stop_code = stops['stop_code'][stops['stop_id'] == stop_id].iloc[0] stop_id2 = None #check if there is the bus stop for opposite direction if stop_code[-1] == 'x': try: stop_code2 = stop_code temp = list(stop_code2) temp[-1] = 'z' stop_code2 = ''.join(temp) stop_id2 = stops['stop_id'][stops['stop_code'] == stop_code2].iloc[0] except (KeyError,IndexError): return stop_name,stop_id elif stop_code[-1] == 'z': try: stop_code2 = stop_code temp = list(stop_code2) temp[-1] = 'x' stop_code2 = ''.join(temp) stop_id2 = stops['stop_id'][stops['stop_code'] == stop_code2].iloc[0] except (KeyError,IndexError): return stop_name,stop_id return stop_name,stop_id,stop_id2 def gettrip(location): """Take in input a location and return a list containing info about the 6 next buses (3 per direction). Info means: time,route short name and route headsign""" stop_id = getstop(location)[1] time = datetime.datetime.now() now = time.strftime('%H:%M:%S') a = pd.merge(stop_times,trips,on=['trip_id']) #merge the dfs in order to simplify extraction process b = pd.merge(a,calendar,on=['service_id']) c = pd.merge(b,routes,on=['route_id']) c = c.sort_values('arrival_time') nextbuses = c[(c['stop_id'] == stop_id) & (c['arrival_time'] >= now) & (c[dow(time)] != 0)].iloc[0:3] try: getstop(location)[2] stop_id2 = getstop(location)[2] nextbuses2 = c[(c['stop_id'] == stop_id2) & (c['arrival_time'] >= now) & (c[dow(time)] != 0)].iloc[0:3] dfs = [nextbuses,nextbuses2] response = prettyprint(pd.concat(dfs)[['arrival_time','route_short_name','trip_headsign']]) return response except IndexError: response = prettyprint(nextbuses[['arrival_time','route_short_name','trip_headsign']]) return response
gpl-3.0
michigraber/scikit-learn
sklearn/mixture/tests/test_dpgmm.py
261
4490
import unittest import sys import numpy as np from sklearn.mixture import DPGMM, VBGMM from sklearn.mixture.dpgmm import log_normalize from sklearn.datasets import make_blobs from sklearn.utils.testing import assert_array_less, assert_equal from sklearn.mixture.tests.test_gmm import GMMTester from sklearn.externals.six.moves import cStringIO as StringIO np.seterr(all='warn') def test_class_weights(): # check that the class weights are updated # simple 3 cluster dataset X, y = make_blobs(random_state=1) for Model in [DPGMM, VBGMM]: dpgmm = Model(n_components=10, random_state=1, alpha=20, n_iter=50) dpgmm.fit(X) # get indices of components that are used: indices = np.unique(dpgmm.predict(X)) active = np.zeros(10, dtype=np.bool) active[indices] = True # used components are important assert_array_less(.1, dpgmm.weights_[active]) # others are not assert_array_less(dpgmm.weights_[~active], .05) def test_verbose_boolean(): # checks that the output for the verbose output is the same # for the flag values '1' and 'True' # simple 3 cluster dataset X, y = make_blobs(random_state=1) for Model in [DPGMM, VBGMM]: dpgmm_bool = Model(n_components=10, random_state=1, alpha=20, n_iter=50, verbose=True) dpgmm_int = Model(n_components=10, random_state=1, alpha=20, n_iter=50, verbose=1) old_stdout = sys.stdout sys.stdout = StringIO() try: # generate output with the boolean flag dpgmm_bool.fit(X) verbose_output = sys.stdout verbose_output.seek(0) bool_output = verbose_output.readline() # generate output with the int flag dpgmm_int.fit(X) verbose_output = sys.stdout verbose_output.seek(0) int_output = verbose_output.readline() assert_equal(bool_output, int_output) finally: sys.stdout = old_stdout def test_verbose_first_level(): # simple 3 cluster dataset X, y = make_blobs(random_state=1) for Model in [DPGMM, VBGMM]: dpgmm = Model(n_components=10, random_state=1, alpha=20, n_iter=50, verbose=1) old_stdout = sys.stdout sys.stdout = StringIO() try: dpgmm.fit(X) finally: sys.stdout = old_stdout def test_verbose_second_level(): # simple 3 cluster dataset X, y = make_blobs(random_state=1) for Model in [DPGMM, VBGMM]: dpgmm = Model(n_components=10, random_state=1, alpha=20, n_iter=50, verbose=2) old_stdout = sys.stdout sys.stdout = StringIO() try: dpgmm.fit(X) finally: sys.stdout = old_stdout def test_log_normalize(): v = np.array([0.1, 0.8, 0.01, 0.09]) a = np.log(2 * v) assert np.allclose(v, log_normalize(a), rtol=0.01) def do_model(self, **kwds): return VBGMM(verbose=False, **kwds) class DPGMMTester(GMMTester): model = DPGMM do_test_eval = False def score(self, g, train_obs): _, z = g.score_samples(train_obs) return g.lower_bound(train_obs, z) class TestDPGMMWithSphericalCovars(unittest.TestCase, DPGMMTester): covariance_type = 'spherical' setUp = GMMTester._setUp class TestDPGMMWithDiagCovars(unittest.TestCase, DPGMMTester): covariance_type = 'diag' setUp = GMMTester._setUp class TestDPGMMWithTiedCovars(unittest.TestCase, DPGMMTester): covariance_type = 'tied' setUp = GMMTester._setUp class TestDPGMMWithFullCovars(unittest.TestCase, DPGMMTester): covariance_type = 'full' setUp = GMMTester._setUp class VBGMMTester(GMMTester): model = do_model do_test_eval = False def score(self, g, train_obs): _, z = g.score_samples(train_obs) return g.lower_bound(train_obs, z) class TestVBGMMWithSphericalCovars(unittest.TestCase, VBGMMTester): covariance_type = 'spherical' setUp = GMMTester._setUp class TestVBGMMWithDiagCovars(unittest.TestCase, VBGMMTester): covariance_type = 'diag' setUp = GMMTester._setUp class TestVBGMMWithTiedCovars(unittest.TestCase, VBGMMTester): covariance_type = 'tied' setUp = GMMTester._setUp class TestVBGMMWithFullCovars(unittest.TestCase, VBGMMTester): covariance_type = 'full' setUp = GMMTester._setUp
bsd-3-clause
PYPIT/PYPIT
pypeit/wavemodel.py
1
32569
# Module to create models of arc lines. from __future__ import absolute_import, division, print_function import astropy import re import scipy import numpy as np import matplotlib.pyplot as plt from pkg_resources import resource_filename from astropy.io import fits from astropy.convolution import convolve, Gaussian1DKernel from astropy.table import Table from pypeit import msgs from pypeit.core import arc from pypeit import utils def blackbody(wavelength, T_BB=250., debug=False): """ Given wavelength [in microns] and Temperature in Kelvin it returns the black body emission. Parameters ---------- wavelength : np.array wavelength vector in microns T_BB : float black body temperature in Kelvin. Default is set to: T_BB = 250. Returns ------- blackbody : np.array spectral radiance of the black body in cgs units: B_lambda = 2.*h*c^2/lambda^5.*(1./(exp(h*c/(lambda*k_b*T_BB))-1.) blackbody_counts : np.array Same as above but in flux density """ # Define constants in cgs PLANCK = astropy.constants.h.cgs.value # erg*s C_LIGHT = astropy.constants.c.cgs.value # cm/s K_BOLTZ = astropy.constants.k_B.cgs.value # erg/K RADIAN_PER_ARCSEC = 1./3600.*np.pi/180. msgs.info("Creating BB spectrum at T={}K".format(T_BB)) lam = wavelength / 1e4 # convert wave in cm. blackbody_pol = 2.*PLANCK*np.power(C_LIGHT,2) / np.power(lam,5) blackbody_exp = np.exp(PLANCK*C_LIGHT/(lam*K_BOLTZ*T_BB)) - 1. blackbody = blackbody_pol / blackbody_exp blackbody_counts = blackbody / (PLANCK * C_LIGHT / lam) * 1e-4 \ * np.power(RADIAN_PER_ARCSEC, 2.) if debug: utils.pyplot_rcparams() msgs.info("Plot of the blackbody spectrum.") plt.figure() plt.plot(wavelength, blackbody, color='navy', linestyle='-', alpha=0.8, label=r'T_BB={}'.format(T_BB)) plt.legend() plt.xlabel(r"Wavelength [micron]") plt.ylabel(r"Spectral Radiance") plt.title(r"Planck's law") msgs.info("Close the Figure to continue.") plt.show(block=True) plt.close() utils.pyplot_rcparams_default() return blackbody, blackbody_counts def addlines2spec(wavelength, wl_line, fl_line, resolution, scale_spec=1., debug=False): """ Create a spectrum with a set of (gaussian) emission lines. Parameters ---------- wavelength : np.array wavelength vector of the input spectrum wl_line, fl_line : np.arrays wavelength and flux of each individual line resolution : np.float resolution of the spectrograph. In other words, the lines will have a FWHM equal to: fwhm_line = wl_line / resolution scale_spec : np.float rescale all the normalization of the final spectrum. Default scale_spec=1. debug : boolean If True will show debug plots Returns ------- line_spec : np.array Spectrum with lines """ line_spec = np.zeros_like(wavelength) wl_line_min, wl_line_max = np.min(wavelength), np.max(wavelength) good_lines = (wl_line>wl_line_min) & (wl_line<wl_line_max) wl_line_good = wl_line[good_lines] fl_line_good = fl_line[good_lines] # define sigma of the gaussians sigma = wl_line_good / resolution / 2.355 msgs.info("Creating line spectrum") for ii in np.arange(len(wl_line_good)): line_spec += scale_spec*fl_line_good[ii]*\ np.exp(-np.power((wl_line_good[ii]-wavelength),2.)/(2.*np.power(sigma[ii],2.))) if debug: utils.pyplot_rcparams() msgs.info("Plot of the line spectrum.") plt.figure() plt.plot(wavelength, line_spec, color='navy', linestyle='-', alpha=0.8, label=r'Spectrum with lines included') plt.legend() plt.xlabel(r'Wavelength') plt.ylabel(r'Flux') msgs.info("Close the Figure to continue.") plt.show(block=True) plt.close() utils.pyplot_rcparams_default() return line_spec def oh_lines(): """ Reads in the Rousselot (2000) OH line list" Returns ------- wavelength, amplitude : np.arrays Wavelength [in microns] and amplitude of the OH lines. """ msgs.info("Reading in the Rousselot (2000) OH line list") skisim_dir = resource_filename('pypeit', 'data/skisim/') oh = np.loadtxt(skisim_dir+"rousselot2000.dat", usecols=(0, 1)) return oh[:,0]/10000., oh[:,1] # wave converted to microns def transparency(wavelength, debug=False): """ Interpolate the atmospheric transmission model in the IR over a given wavelength (in microns) range. Parameters ---------- wavelength : np.array wavelength vector in microns debug : boolean If True will show debug plots Returns ------- transparency : np.array Transmission of the sky over the considered wavelength rage. 1. means fully transparent and 0. fully opaque """ msgs.info("Reading in the atmospheric transmission model") skisim_dir = resource_filename('pypeit', 'data/skisim/') transparency = np.loadtxt(skisim_dir+'atm_transmission_secz1.5_1.6mm.dat') wave_mod = transparency[:,0] tran_mod = transparency[:,1] # Limit model between 0.8 and np.max(wavelength) microns filt_wave_mod = (wave_mod>0.8) & (wave_mod<np.max(wavelength)) wave_mod = wave_mod[filt_wave_mod] tran_mod = tran_mod[filt_wave_mod] # Interpolate over input wavelengths interp_tran = scipy.interpolate.interp1d(wave_mod, tran_mod, kind='cubic', fill_value='extrapolate') transmission = interp_tran(wavelength) transmission[wavelength<0.9] = 1. # Clean for spourious values due to interpolation transmission[transmission<0.] = 0. transmission[transmission>1.] = 1. if debug: utils.pyplot_rcparams() msgs.info("Plot of the sky transmission template") plt.figure() plt.plot(wave_mod, tran_mod, color='navy', linestyle='-', alpha=0.8, label=r'Original') plt.plot(wavelength, transmission, color='crimson', linestyle='-', alpha=0.8, label=r'Resampled') plt.legend() plt.xlabel(r'Wavelength [microns]') plt.ylabel(r'Transmission') plt.title(r' IR Transmission Spectra ') msgs.info("Close the Figure to continue.") plt.show(block=True) plt.close() utils.pyplot_rcparams_default() # Returns return transmission def h2o_lines(): """ Reads in the H2O atmospheric spectrum" Returns ------- wavelength, flux : np.arrays Wavelength [in microns] and flux of the H2O atmospheric spectrum. """ msgs.info("Reading in the water atmsopheric spectrum") skisim_dir = resource_filename('pypeit', 'data/skisim/') h2o = np.loadtxt(skisim_dir+"HITRAN.txt", usecols=(0, 1)) h2o_wv = 1./ h2o[:,0] * 1e4 # microns h2o_rad = h2o[:,1] * 5e11 # added to match XIDL return h2o_wv, h2o_rad def thar_lines(): """ Reads in the H2O atmospheric spectrum" Detailed information are here: http://astronomy.swin.edu.au/~mmurphy/thar/index.html Returns ------- wavelength, flux : np.arrays Wavelength [in angstrom] and flux of the ThAr lamp spectrum. """ msgs.info("Reading in the ThAr spectrum") arclines_dir = resource_filename('pypeit', 'data/arc_lines/') thar = fits.open(arclines_dir+'thar_spec_MM201006.fits') # create pixel array thar_pix = np.arange(thar[0].header['CRPIX1'],len(thar[0].data[0,:])+1) # convert pixels to wavelength in Angstrom thar_wv = thar[0].header['UP_WLSRT']*10**((thar_pix-thar[0].header['CRPIX1'])*thar[0].header['CD1_1']) # read in spectrum thar_spec = thar[0].data[0,:] return thar_wv, thar_spec def nearIR_modelsky(resolution, waveminmax=(0.8,2.6), dlam=40.0, flgd=True, nirsky_outfile=None, T_BB=250., SCL_BB=1., SCL_OH=1., SCL_H2O=10., WAVE_WATER=2.3, debug=False): """ Generate a model sky in the near-IR. This includes a continuum model to match to gemini broadband level, a black body at T_BB, OH lines, and H2O lines (but only at lambda>WAVE_WATER). Everythins is smoothed at the given resolution. Parameters ---------- resolution : np.float resolution of the spectrograph. The OH and H2O lines will have a FWHM equal to: fwhm_line = wl_line / resolution waveminmax : tuple wavelength range in microns to be covered by the model. Default is: (0.8, 2.6) dlam : bin to be used to create the wavelength grid of the model. If flgd='True' it is a bin in velocity (km/s). If flgd='False' it is a bin in linear space (microns). Default is: 40.0 (with flgd='True') flgd : boolean if flgd='True' (default) wavelengths are created with equal steps in log space. If 'False', wavelengths will be created wit equal steps in linear space. nirsky_outfile : str name of the fits file where the model sky spectrum will be stored. default is 'None' (i.e., no file will be written). T_BB : float black body temperature in Kelvin. Default is set to: T_BB = 250. SCL_BB : float scale factor for modelling the sky black body emssion. Default: SCL_BB=1. SCL_OH : float scale factor for modelling the OH emssion. Default: SCL_OH=1. SCL_H2O : float scale factor for modelling the H2O emssion. Default: SCL_H2O=10. WAVE_WATER : float wavelength (in microns) at which the H2O are inclued. Default: WAVE_WATER = 2.3 debug : boolean If True will show debug plots Returns ------- wave, sky_model : np.arrays wavelength (in Ang.) and flux of the final model of the sky. """ # Create the wavelength array: wv_min = waveminmax[0] wv_max = waveminmax[1] if flgd : msgs.info("Creating wavelength vector in velocity space.") velpix = dlam # km/s loglam = np.log10(1.0 + velpix/299792.458) wave = np.power(10.,np.arange(np.log10(wv_min), np.log10(wv_max), loglam)) else : msgs.info("Creating wavelength vector in linear space.") wave = np.arange(wv_min, wv_max, dlam) # Calculate transparency # trans = transparency(wave, debug=False) # Empirical match to gemini broadband continuum level logy = - 0.55 - 0.55 * (wave-1.0) y = np.power(10.,logy) msgs.info("Add in a blackbody for the atmosphere.") bb, bb_counts = blackbody(wave, T_BB=T_BB, debug=debug) bb_counts = bb_counts msgs.info("Add in OH lines") oh_wv, oh_fx = oh_lines() # produces better wavelength solutions with 1.0 threshold msgs.info("Selecting stronger OH lines") filt_oh = oh_fx > 1. oh_wv, oh_fx = oh_wv[filt_oh], oh_fx[filt_oh] # scale_spec was added to match the XIDL code ohspec = addlines2spec(wave, oh_wv, oh_fx, resolution=resolution, scale_spec=((resolution/1000.)/40.), debug=debug) if wv_max > WAVE_WATER : msgs.info("Add in H2O lines") h2o_wv, h2o_rad = h2o_lines() filt_h2o = (h2o_wv>wv_min-0.1) & (h2o_wv<wv_max+0.1) h2o_wv = h2o_wv[filt_h2o] h2o_rad = h2o_rad[filt_h2o] # calculate sigma at the mean wavelenght of the H2O spectrum filt_h2o_med = h2o_wv>WAVE_WATER mn_wv = np.mean(h2o_wv[filt_h2o_med]) # Convolve to the instrument resolution. This is only # approximate. smooth_fx, dwv, h2o_dwv = conv2res(h2o_wv, h2o_rad, resolution, central_wl = mn_wv, debug=debug) # Interpolate over input wavelengths interp_h2o = scipy.interpolate.interp1d(h2o_wv, smooth_fx, kind='cubic', fill_value='extrapolate') h2ospec = interp_h2o(wave) # Zero out below WAVE_WATER microns (reconsider) h2ospec[wave<WAVE_WATER] = 0. h2ospec[wave>np.max(h2o_wv)] = 0. else: h2ospec = np.zeros(len(wave),dtype='float') sky_model = y+bb_counts*SCL_BB+ohspec*SCL_OH+h2ospec*SCL_H2O if nirsky_outfile is not None: msgs.info("Saving the sky model in: {}".format(nirsky_outfile)) hdu = fits.PrimaryHDU(np.array(sky_model)) header = hdu.header if flgd : header['CRVAL1'] = np.log10(wv_min) header['CDELT1'] = loglam header['DC-FLAG'] = 1 else : header['CRVAL1'] = wv_min header['CDELT1'] = dlam header['DC-FLAG'] = 0 hdu.writeto(nirsky_outfile, overwrite = True) if debug: utils.pyplot_rcparams() msgs.info("Plot of the sky emission at R={}".format(resolution)) plt.figure() plt.plot(wave, sky_model, color='black', linestyle='-', alpha=0.8, label=r'Sky Model') plt.plot(wave, y, color='darkorange', linestyle='-', alpha=0.6, label=r'Continuum') plt.plot(wave, bb_counts*SCL_BB, color='green', linestyle='-', alpha=0.6, label=r'Black Body at T={}K'.format(T_BB)) plt.plot(wave, ohspec*SCL_OH, color='darkviolet', linestyle='-', alpha=0.6, label=r'OH') plt.plot(wave, h2ospec*SCL_H2O, color='dodgerblue', linestyle='-', alpha=0.6, label=r'H2O') plt.legend() plt.xlabel(r'Wavelength [microns]') plt.ylabel(r'Emission') plt.title(r'Sky Emission Spectrum at R={}'.format(resolution)) msgs.info("Close the Figure to continue.") plt.show(block=True) plt.close() utils.pyplot_rcparams_default() return np.array(wave*10000.), np.array(sky_model) def optical_modelThAr(resolution, waveminmax=(3000.,10500.), dlam=40.0, flgd=True, thar_outfile=None, debug=False): """ Generate a model of a ThAr lamp in the uvb/optical. This is based on the Murphy et al. ThAr spectrum. Detailed information are here: http://astronomy.swin.edu.au/~mmurphy/thar/index.html Everythins is smoothed at the given resolution. Parameters ---------- resolution : np.float resolution of the spectrograph. The ThAr lines will have a FWHM equal to: fwhm_line = wl_line / resolution waveminmax : tuple wavelength range in angstrom to be covered by the model. Default is: (3000.,10500.) dlam : bin to be used to create the wavelength grid of the model. If flgd='True' it is a bin in velocity (km/s). If flgd='False' it is a bin in linear space (microns). Default is: 40.0 (with flgd='True') flgd : boolean if flgd='True' (default) wavelengths are created with equal steps in log space. If 'False', wavelengths will be created wit equal steps in linear space. thar_outfile : str name of the fits file where the model sky spectrum will be stored. default is 'None' (i.e., no file will be written). debug : boolean If True will show debug plots Returns ------- wave, thar_model : np.arrays wavelength (in Ang.) and flux of the final model of the ThAr lamp emission. """ # Create the wavelength array: wv_min = waveminmax[0] wv_max = waveminmax[1] if flgd : msgs.info("Creating wavelength vector in velocity space.") velpix = dlam # km/s loglam = np.log10(1.0 + velpix/299792.458) wave = np.power(10.,np.arange(np.log10(wv_min), np.log10(wv_max), loglam)) else : msgs.info("Creating wavelength vector in linear space.") wave = np.arange(wv_min, wv_max, dlam) msgs.info("Add in ThAr lines") th_wv, th_fx = thar_lines() # select spectral region filt_wl = (th_wv>=wv_min) & (th_wv<=wv_max) # calculate sigma at the mean wavelenght of the ThAr spectrum mn_wv = np.mean(th_wv[filt_wl]) # Convolve to the instrument resolution. This is only # approximate. smooth_fx, dwv, thar_dwv = conv2res(th_wv, th_fx, resolution, central_wl = mn_wv, debug=debug) # Interpolate over input wavelengths interp_thar = scipy.interpolate.interp1d(th_wv, smooth_fx, kind='cubic', fill_value='extrapolate') thar_spec = interp_thar(wave) # remove negative artifacts thar_spec[thar_spec<0.] = 0. # Remove regions of the spectrum outside the wavelength covered by the ThAr model if wv_min<np.min(th_wv): msgs.warn("Model of the ThAr spectrum outside the template coverage.") thar_spec[wave<np.min(th_wv)] = 0. if wv_max<np.max(th_wv): msgs.warn("Model of the ThAr spectrum outside the template coverage.") thar_spec[wave>np.max(th_wv)] = 0. if thar_outfile is not None: msgs.info("Saving the ThAr model in: {}".format(thar_outfile)) hdu = fits.PrimaryHDU(np.array(thar_spec)) header = hdu.header if flgd : header['CRVAL1'] = np.log10(wv_min) header['CDELT1'] = loglam header['DC-FLAG'] = 1 else : header['CRVAL1'] = wv_min header['CDELT1'] = dlam header['DC-FLAG'] = 0 hdu.writeto(thar_outfile, overwrite = True) if debug: utils.pyplot_rcparams() msgs.info("Plot of the Murphy et al. template at R={}".format(resolution)) plt.figure() plt.plot(th_wv, th_fx, color='navy', linestyle='-', alpha=0.3, label=r'Original') plt.plot(th_wv, smooth_fx, color='crimson', linestyle='-', alpha=0.6, label=r'Convolved at R={}'.format(resolution)) plt.plot(wave, thar_spec, color='maroon', linestyle='-', alpha=1.0, label=r'Convolved at R={} and resampled'.format(resolution)) plt.legend() plt.xlabel(r'Wavelength [Ang.]') plt.ylabel(r'Emission') plt.title(r'Murphy et al. ThAr spectrum at R={}'.format(resolution)) msgs.info("Close the Figure to continue.") plt.show(block=True) plt.close() utils.pyplot_rcparams_default() return np.array(wave), np.array(thar_spec) def conv2res(wavelength, flux, resolution, central_wl='midpt', debug=False): """Convolve an imput spectrum to a specific resolution. This is only approximate. It takes a fix FWHM for the entire spectrum given by: fwhm = wl_cent / resolution Parameters ---------- wavelength : np.array wavelength flux : np.array flux resolution : np.float resolution of the spectrograph central_wl if 'midpt' the central pixel of wavelength is used, otherwise the central_wl will be used. debug : boolean If True will show debug plots Returns ------- flux_convolved :np.array Resulting flux after convolution px_sigma : float Size of the sigma in pixels at central_wl px_bin : float Size of one pixel at central_wl """ if central_wl is 'midpt': wl_cent = np.median(wavelength) else: wl_cent = np.float(central_wl) wl_sigma = wl_cent / resolution / 2.355 wl_bin = np.abs((wavelength - np.roll(wavelength,1))[np.where( np.abs(wavelength-wl_cent) == np.min(np.abs(wavelength-wl_cent)) )]) msgs.info("The binning of the wavelength array at {} is: {}".format(wl_cent, wl_bin[0])) px_bin = wl_bin[0] px_sigma = wl_sigma / px_bin msgs.info("Covolving with a Gaussian kernel with sigma = {} pixels".format(px_sigma)) gauss_kernel = Gaussian1DKernel(px_sigma) flux_convolved = convolve(flux, gauss_kernel) if debug: utils.pyplot_rcparams() msgs.info("Spectrum Convolved at R = {}".format(resolution)) plt.figure() plt.plot(wavelength, flux, color='navy', linestyle='-', alpha=0.8, label=r'Original') plt.plot(wavelength, flux_convolved, color='crimson', linestyle='-', alpha=0.8, label=r'Convolved') plt.legend() plt.xlabel(r'Wavelength') plt.ylabel(r'Flux') plt.title(r'Spectrum Convolved at R = {}'.format(resolution)) msgs.info("Close the Figure to continue.") plt.show(block=True) plt.close() utils.pyplot_rcparams_default() return flux_convolved, px_sigma, px_bin def iraf_datareader(database_dir, id_file): """Reads in a line identification database created with IRAF identify. These are usually locate in a directory called 'database'. This read pixel location and wavelength of the lines that have been id with IRAF. Note that the first pixel in IRAF is '1', while is '0' in python. The pixel location is thus shifted of one pixel while reading the database. Parameters ---------- database_dir : string directory where the id files are located. id_file : string filename that is going to be read. Returns ------- pixel, line_id : np.arrays Position of the line in pixel and ID of the line. For IRAF output, these are usually in Ang. """ lines_database = [] # Open file for reading of text data. with open (database_dir+id_file, 'rt') as id_file_iraf: for line in id_file_iraf: lines_database.append(line.split()) feat_line = re.search(r'features\t(\d+)', line) if feat_line is not None: N_lines = np.int(feat_line.group(1)) msgs.info("The number of IDs in the IRAF database {} is {}".format(id_file, N_lines)) pixel = np.zeros(N_lines) line_id = np.zeros(N_lines) for iii in range(0,N_lines): pixel[iii] = lines_database[10:N_lines+10][iii][0] line_id[iii] = lines_database[10:N_lines+10][iii][2] # Moving from IRAF 1-based to Python 0-based convention. pixel = pixel - 1. return pixel, line_id def create_linelist(wavelength, spec, fwhm, sigdetec=2., cont_samp=10., line_name=None, file_root_name=None, iraf_frmt=False, debug=False): """ Create list of lines detected in a spectrum in a PypeIt compatible format. The name of the output file is file_root_name+'_lines.dat'. Parameters ---------- wavelength : np.array wavelength spec : np.array spectrum fwhm : float fwhm in pixels used for filtering out arc lines that are too wide and not considered in fits. Parameter of arc.detect_lines(). sigdetec : float sigma threshold above fluctuations for line detection. Parameter of arc.detect_lines(). Default = 2. cont_samp : float the number of samples across the spectrum used for continuum subtraction. Parameter of arc.detect_lines(). Default = 10. line_name : str name of the lines to listed in the file. file_root_name : str name of the file where the identified lines will be stored. The code automatically add '_lines.dat' at the end of the root name. iraf_frmt : bool if True, the file is written in the IRAF format (i.e. wavelength, ion name, amplitude). """ msgs.info("Searching for peaks {} sigma above background".format(sigdetec)) tampl_true, tampl, tcent, twid, centerr, ww, arcnorm, nsig = arc.detect_lines(spec, sigdetect=sigdetec, fwhm=fwhm, cont_samp=cont_samp, debug=debug) peaks_good = tcent[ww] ampl_good = tampl[ww] # convert from pixel location to wavelength pixvec = np.arange(spec.size) wave_peak = scipy.interpolate.interp1d(pixvec, wavelength, bounds_error=False, fill_value='extrapolate')(peaks_good) npeak = len(wave_peak) ion = npeak*[str(line_name)] NIST = npeak*[1] Instr = npeak*[32] Source = npeak*['wavemodel.py'] if iraf_frmt: msgs.info("Printing file in IRAF format: {}".format(file_root_name+'_iraf_lines.dat')) ion = np.array(ion) id_lines_iraf = np.vstack( (np.round(wave_peak,5), ion, np.round(ampl_good,5)) ).T np.savetxt(file_root_name+'_iraf_lines.dat', id_lines_iraf, fmt="%15s %6s %15s", delimiter=" ") else: msgs.info("Printing file: {}".format(file_root_name+'_lines.dat')) dat = Table([wave_peak, ion, NIST, Instr, ampl_good, Source], names=('wave', 'ion','NIST','Instr','amplitude','Source')) dat.write(file_root_name+'_lines.dat',format='ascii.fixed_width') def create_OHlinelist(resolution, waveminmax=(0.8,2.6), dlam=40.0, flgd=True, nirsky_outfile=None, fwhm=None, sigdetec=3., line_name='OH', file_root_name=None, iraf_frmt=False, debug=False): """Create a syntetic sky spectrum at a given resolution, extract significant lines, and store them in a PypeIt compatibile file. The skymodel is built from nearIR_modelsky and includes black body at 250K, OH lines, and H2O lines (but only at lambda>2.3microns). Parameters ---------- resolution : np.float resolution of the spectrograph waveminmax : tuple wavelength range in microns to be covered by the model. Default is: (0.8, 2.6) dlam : bin to be used to create the wavelength grid of the model. If flgd='True' it is a bin in velocity (km/s). If flgd='False' it is a bin in linear space (microns). Default is: 40.0 (with flgd='True') flgd : boolean if flgd='True' (default) wavelengths are created with equal steps in log space. If 'False', wavelengths will be created wit equal steps in linear space. nirsky_outfile : str name of the fits file where the model sky spectrum will be stored. default is 'None' (i.e., no file will be written). fwhm : float fwhm in pixels used for filtering out arc lines that are too wide and not considered in fits. Parameter of arc.detect_lines(). If set to 'None' the fwhm will be derived from the resolution as: 2. * central_wavelength / resolution sigdetec : float sigma threshold above fluctuations for line detection. Parameter of arc.detect_lines(). Default = 2. line_name : str name of the lines to listed in the file. Default is 'OH'. file_root_name : str name of the file where the identified lines will be stored. The code automatically add '_lines.dat' at the end of the root name. iraf_frmt : bool if True, the file is written in the IRAF format (i.e. wavelength, ion name, amplitude). debug : boolean If True will show debug plots """ wavelength, spec = nearIR_modelsky(resolution, waveminmax=waveminmax, dlam=dlam, flgd=flgd, nirsky_outfile=nirsky_outfile, debug=debug) if fwhm is None: msgs.warn("No min FWHM for the line detection set. Derived from the resolution at the center of the spectrum") wl_cent = np.average(wavelength) wl_fwhm = wl_cent / resolution wl_bin = np.abs((wavelength-np.roll(wavelength,1))[np.where(np.abs(wavelength-wl_cent)==np.min(np.abs(wavelength-wl_cent)))]) # In order not to exclude all the lines, fwhm is set to 5 times # the minimum fwhm of the spectrum fwhm = 1.1 * wl_fwhm / wl_bin[0] if fwhm < 1.: msgs.warn("Lines are unresolved. Setting FWHM=2.pixels") fwhm = 2. if line_name is None: msgs.warn("No line_name as been set. The file will contain XXX as ion") line_name = 'XXX' if file_root_name is None: msgs.warn("No file_root_name as been set. The file will called OH_SKY_lines.dat") file_root_name = 'OH_SKY' create_linelist(wavelength, spec, fwhm=fwhm, sigdetec=sigdetec, line_name=line_name, file_root_name=file_root_name, iraf_frmt=iraf_frmt, debug=debug) def create_ThArlinelist(resolution, waveminmax=(3000.,10500.), dlam=40.0, flgd=True, thar_outfile=None, fwhm=None, sigdetec=3., line_name='ThAr', file_root_name=None, iraf_frmt=False, debug=False): """Create a syntetic ThAr spectrum at a given resolution, extract significant lines, and store them in a PypeIt compatibile file. This is based on the Murphy et al. ThAr spectrum. Detailed information are here: http://astronomy.swin.edu.au/~mmurphy/thar/index.html Parameters ---------- resolution : np.float resolution of the spectrograph waveminmax : tuple wavelength range in ang. to be covered by the model. Default is: (3000., 10500.) dlam : bin to be used to create the wavelength grid of the model. If flgd='True' it is a bin in velocity (km/s). If flgd='False' it is a bin in linear space (angstrom). Default is: 40.0 (with flgd='True') flgd : boolean if flgd='True' (default) wavelengths are created with equal steps in log space. If 'False', wavelengths will be created wit equal steps in linear space. thar_outfile : str name of the fits file where the model sky spectrum will be stored. default is 'None' (i.e., no file will be written). fwhm : float fwhm in pixels used for filtering out arc lines that are too wide and not considered in fits. Parameter of arc.detect_lines(). If set to 'None' the fwhm will be derived from the resolution as: 2. * central_wavelength / resolution sigdetec : float sigma threshold above fluctuations for line detection. Parameter of arc.detect_lines(). Default = 2. line_name : str name of the lines to listed in the file. file_root_name : str name of the file where the identified lines will be stored. The code automatically add '_lines.dat' at the end of the root name. iraf_frmt : bool if True, the file is written in the IRAF format (i.e. wavelength, ion name, amplitude). debug : boolean If True will show debug plots """ wavelength, spec = optical_modelThAr(resolution, waveminmax=waveminmax, dlam=dlam, flgd=flgd, thar_outfile=thar_outfile, debug=debug) if fwhm is None: msgs.warn("No min FWHM for the line detection set. Derived from the resolution at the center of the spectrum") wl_cent = np.average(wavelength) wl_fwhm = wl_cent / resolution wl_bin = np.abs((wavelength-np.roll(wavelength,1))[np.where(np.abs(wavelength-wl_cent)==np.min(np.abs(wavelength-wl_cent)))]) # In order not to exclude all the lines, fwhm is set to 5 times # the minimum fwhm of the spectrum fwhm = 1.1 * wl_fwhm / wl_bin[0] if fwhm < 1.: msgs.warn("Lines are unresolved. Setting FWHM=2.*pixels") fwhm = 2. if line_name is None: msgs.warn("No line_name as been set. The file will contain XXX as ion") line_name = 'XXX' if file_root_name is None: msgs.warn("No file_root_name as been set. The file will called ThAr_lines.dat") file_root_name = 'ThAr' create_linelist(wavelength, spec, fwhm=fwhm, sigdetec=sigdetec, line_name=line_name, file_root_name=file_root_name, iraf_frmt=iraf_frmt, debug=debug)
gpl-3.0
tmhm/scikit-learn
sklearn/gaussian_process/gaussian_process.py
83
34544
# -*- coding: utf-8 -*- # Author: Vincent Dubourg <vincent.dubourg@gmail.com> # (mostly translation, see implementation details) # Licence: BSD 3 clause from __future__ import print_function import numpy as np from scipy import linalg, optimize from ..base import BaseEstimator, RegressorMixin from ..metrics.pairwise import manhattan_distances from ..utils import check_random_state, check_array, check_X_y from ..utils.validation import check_is_fitted from . import regression_models as regression from . import correlation_models as correlation MACHINE_EPSILON = np.finfo(np.double).eps def l1_cross_distances(X): """ Computes the nonzero componentwise L1 cross-distances between the vectors in X. Parameters ---------- X: array_like An array with shape (n_samples, n_features) Returns ------- D: array with shape (n_samples * (n_samples - 1) / 2, n_features) The array of componentwise L1 cross-distances. ij: arrays with shape (n_samples * (n_samples - 1) / 2, 2) The indices i and j of the vectors in X associated to the cross- distances in D: D[k] = np.abs(X[ij[k, 0]] - Y[ij[k, 1]]). """ X = check_array(X) n_samples, n_features = X.shape n_nonzero_cross_dist = n_samples * (n_samples - 1) // 2 ij = np.zeros((n_nonzero_cross_dist, 2), dtype=np.int) D = np.zeros((n_nonzero_cross_dist, n_features)) ll_1 = 0 for k in range(n_samples - 1): ll_0 = ll_1 ll_1 = ll_0 + n_samples - k - 1 ij[ll_0:ll_1, 0] = k ij[ll_0:ll_1, 1] = np.arange(k + 1, n_samples) D[ll_0:ll_1] = np.abs(X[k] - X[(k + 1):n_samples]) return D, ij class GaussianProcess(BaseEstimator, RegressorMixin): """The Gaussian Process model class. Read more in the :ref:`User Guide <gaussian_process>`. Parameters ---------- regr : string or callable, optional A regression function returning an array of outputs of the linear regression functional basis. The number of observations n_samples should be greater than the size p of this basis. Default assumes a simple constant regression trend. Available built-in regression models are:: 'constant', 'linear', 'quadratic' corr : string or callable, optional A stationary autocorrelation function returning the autocorrelation between two points x and x'. Default assumes a squared-exponential autocorrelation model. Built-in correlation models are:: 'absolute_exponential', 'squared_exponential', 'generalized_exponential', 'cubic', 'linear' beta0 : double array_like, optional The regression weight vector to perform Ordinary Kriging (OK). Default assumes Universal Kriging (UK) so that the vector beta of regression weights is estimated using the maximum likelihood principle. storage_mode : string, optional A string specifying whether the Cholesky decomposition of the correlation matrix should be stored in the class (storage_mode = 'full') or not (storage_mode = 'light'). Default assumes storage_mode = 'full', so that the Cholesky decomposition of the correlation matrix is stored. This might be a useful parameter when one is not interested in the MSE and only plan to estimate the BLUP, for which the correlation matrix is not required. verbose : boolean, optional A boolean specifying the verbose level. Default is verbose = False. theta0 : double array_like, optional An array with shape (n_features, ) or (1, ). The parameters in the autocorrelation model. If thetaL and thetaU are also specified, theta0 is considered as the starting point for the maximum likelihood estimation of the best set of parameters. Default assumes isotropic autocorrelation model with theta0 = 1e-1. thetaL : double array_like, optional An array with shape matching theta0's. Lower bound on the autocorrelation parameters for maximum likelihood estimation. Default is None, so that it skips maximum likelihood estimation and it uses theta0. thetaU : double array_like, optional An array with shape matching theta0's. Upper bound on the autocorrelation parameters for maximum likelihood estimation. Default is None, so that it skips maximum likelihood estimation and it uses theta0. normalize : boolean, optional Input X and observations y are centered and reduced wrt means and standard deviations estimated from the n_samples observations provided. Default is normalize = True so that data is normalized to ease maximum likelihood estimation. nugget : double or ndarray, optional Introduce a nugget effect to allow smooth predictions from noisy data. If nugget is an ndarray, it must be the same length as the number of data points used for the fit. The nugget is added to the diagonal of the assumed training covariance; in this way it acts as a Tikhonov regularization in the problem. In the special case of the squared exponential correlation function, the nugget mathematically represents the variance of the input values. Default assumes a nugget close to machine precision for the sake of robustness (nugget = 10. * MACHINE_EPSILON). optimizer : string, optional A string specifying the optimization algorithm to be used. Default uses 'fmin_cobyla' algorithm from scipy.optimize. Available optimizers are:: 'fmin_cobyla', 'Welch' 'Welch' optimizer is dued to Welch et al., see reference [WBSWM1992]_. It consists in iterating over several one-dimensional optimizations instead of running one single multi-dimensional optimization. random_start : int, optional The number of times the Maximum Likelihood Estimation should be performed from a random starting point. The first MLE always uses the specified starting point (theta0), the next starting points are picked at random according to an exponential distribution (log-uniform on [thetaL, thetaU]). Default does not use random starting point (random_start = 1). random_state: integer or numpy.RandomState, optional The generator used to shuffle the sequence of coordinates of theta in the Welch optimizer. If an integer is given, it fixes the seed. Defaults to the global numpy random number generator. Attributes ---------- theta_ : array Specified theta OR the best set of autocorrelation parameters (the \ sought maximizer of the reduced likelihood function). reduced_likelihood_function_value_ : array The optimal reduced likelihood function value. Examples -------- >>> import numpy as np >>> from sklearn.gaussian_process import GaussianProcess >>> X = np.array([[1., 3., 5., 6., 7., 8.]]).T >>> y = (X * np.sin(X)).ravel() >>> gp = GaussianProcess(theta0=0.1, thetaL=.001, thetaU=1.) >>> gp.fit(X, y) # doctest: +ELLIPSIS GaussianProcess(beta0=None... ... Notes ----- The presentation implementation is based on a translation of the DACE Matlab toolbox, see reference [NLNS2002]_. References ---------- .. [NLNS2002] `H.B. Nielsen, S.N. Lophaven, H. B. Nielsen and J. Sondergaard. DACE - A MATLAB Kriging Toolbox.` (2002) http://www2.imm.dtu.dk/~hbn/dace/dace.pdf .. [WBSWM1992] `W.J. Welch, R.J. Buck, J. Sacks, H.P. Wynn, T.J. Mitchell, and M.D. Morris (1992). Screening, predicting, and computer experiments. Technometrics, 34(1) 15--25.` http://www.jstor.org/pss/1269548 """ _regression_types = { 'constant': regression.constant, 'linear': regression.linear, 'quadratic': regression.quadratic} _correlation_types = { 'absolute_exponential': correlation.absolute_exponential, 'squared_exponential': correlation.squared_exponential, 'generalized_exponential': correlation.generalized_exponential, 'cubic': correlation.cubic, 'linear': correlation.linear} _optimizer_types = [ 'fmin_cobyla', 'Welch'] def __init__(self, regr='constant', corr='squared_exponential', beta0=None, storage_mode='full', verbose=False, theta0=1e-1, thetaL=None, thetaU=None, optimizer='fmin_cobyla', random_start=1, normalize=True, nugget=10. * MACHINE_EPSILON, random_state=None): self.regr = regr self.corr = corr self.beta0 = beta0 self.storage_mode = storage_mode self.verbose = verbose self.theta0 = theta0 self.thetaL = thetaL self.thetaU = thetaU self.normalize = normalize self.nugget = nugget self.optimizer = optimizer self.random_start = random_start self.random_state = random_state def fit(self, X, y): """ The Gaussian Process model fitting method. Parameters ---------- X : double array_like An array with shape (n_samples, n_features) with the input at which observations were made. y : double array_like An array with shape (n_samples, ) or shape (n_samples, n_targets) with the observations of the output to be predicted. Returns ------- gp : self A fitted Gaussian Process model object awaiting data to perform predictions. """ # Run input checks self._check_params() self.random_state = check_random_state(self.random_state) # Force data to 2D numpy.array X, y = check_X_y(X, y, multi_output=True, y_numeric=True) self.y_ndim_ = y.ndim if y.ndim == 1: y = y[:, np.newaxis] # Check shapes of DOE & observations n_samples, n_features = X.shape _, n_targets = y.shape # Run input checks self._check_params(n_samples) # Normalize data or don't if self.normalize: X_mean = np.mean(X, axis=0) X_std = np.std(X, axis=0) y_mean = np.mean(y, axis=0) y_std = np.std(y, axis=0) X_std[X_std == 0.] = 1. y_std[y_std == 0.] = 1. # center and scale X if necessary X = (X - X_mean) / X_std y = (y - y_mean) / y_std else: X_mean = np.zeros(1) X_std = np.ones(1) y_mean = np.zeros(1) y_std = np.ones(1) # Calculate matrix of distances D between samples D, ij = l1_cross_distances(X) if (np.min(np.sum(D, axis=1)) == 0. and self.corr != correlation.pure_nugget): raise Exception("Multiple input features cannot have the same" " target value.") # Regression matrix and parameters F = self.regr(X) n_samples_F = F.shape[0] if F.ndim > 1: p = F.shape[1] else: p = 1 if n_samples_F != n_samples: raise Exception("Number of rows in F and X do not match. Most " "likely something is going wrong with the " "regression model.") if p > n_samples_F: raise Exception(("Ordinary least squares problem is undetermined " "n_samples=%d must be greater than the " "regression model size p=%d.") % (n_samples, p)) if self.beta0 is not None: if self.beta0.shape[0] != p: raise Exception("Shapes of beta0 and F do not match.") # Set attributes self.X = X self.y = y self.D = D self.ij = ij self.F = F self.X_mean, self.X_std = X_mean, X_std self.y_mean, self.y_std = y_mean, y_std # Determine Gaussian Process model parameters if self.thetaL is not None and self.thetaU is not None: # Maximum Likelihood Estimation of the parameters if self.verbose: print("Performing Maximum Likelihood Estimation of the " "autocorrelation parameters...") self.theta_, self.reduced_likelihood_function_value_, par = \ self._arg_max_reduced_likelihood_function() if np.isinf(self.reduced_likelihood_function_value_): raise Exception("Bad parameter region. " "Try increasing upper bound") else: # Given parameters if self.verbose: print("Given autocorrelation parameters. " "Computing Gaussian Process model parameters...") self.theta_ = self.theta0 self.reduced_likelihood_function_value_, par = \ self.reduced_likelihood_function() if np.isinf(self.reduced_likelihood_function_value_): raise Exception("Bad point. Try increasing theta0.") self.beta = par['beta'] self.gamma = par['gamma'] self.sigma2 = par['sigma2'] self.C = par['C'] self.Ft = par['Ft'] self.G = par['G'] if self.storage_mode == 'light': # Delete heavy data (it will be computed again if required) # (it is required only when MSE is wanted in self.predict) if self.verbose: print("Light storage mode specified. " "Flushing autocorrelation matrix...") self.D = None self.ij = None self.F = None self.C = None self.Ft = None self.G = None return self def predict(self, X, eval_MSE=False, batch_size=None): """ This function evaluates the Gaussian Process model at x. Parameters ---------- X : array_like An array with shape (n_eval, n_features) giving the point(s) at which the prediction(s) should be made. eval_MSE : boolean, optional A boolean specifying whether the Mean Squared Error should be evaluated or not. Default assumes evalMSE = False and evaluates only the BLUP (mean prediction). batch_size : integer, optional An integer giving the maximum number of points that can be evaluated simultaneously (depending on the available memory). Default is None so that all given points are evaluated at the same time. Returns ------- y : array_like, shape (n_samples, ) or (n_samples, n_targets) An array with shape (n_eval, ) if the Gaussian Process was trained on an array of shape (n_samples, ) or an array with shape (n_eval, n_targets) if the Gaussian Process was trained on an array of shape (n_samples, n_targets) with the Best Linear Unbiased Prediction at x. MSE : array_like, optional (if eval_MSE == True) An array with shape (n_eval, ) or (n_eval, n_targets) as with y, with the Mean Squared Error at x. """ check_is_fitted(self, "X") # Check input shapes X = check_array(X) n_eval, _ = X.shape n_samples, n_features = self.X.shape n_samples_y, n_targets = self.y.shape # Run input checks self._check_params(n_samples) if X.shape[1] != n_features: raise ValueError(("The number of features in X (X.shape[1] = %d) " "should match the number of features used " "for fit() " "which is %d.") % (X.shape[1], n_features)) if batch_size is None: # No memory management # (evaluates all given points in a single batch run) # Normalize input X = (X - self.X_mean) / self.X_std # Initialize output y = np.zeros(n_eval) if eval_MSE: MSE = np.zeros(n_eval) # Get pairwise componentwise L1-distances to the input training set dx = manhattan_distances(X, Y=self.X, sum_over_features=False) # Get regression function and correlation f = self.regr(X) r = self.corr(self.theta_, dx).reshape(n_eval, n_samples) # Scaled predictor y_ = np.dot(f, self.beta) + np.dot(r, self.gamma) # Predictor y = (self.y_mean + self.y_std * y_).reshape(n_eval, n_targets) if self.y_ndim_ == 1: y = y.ravel() # Mean Squared Error if eval_MSE: C = self.C if C is None: # Light storage mode (need to recompute C, F, Ft and G) if self.verbose: print("This GaussianProcess used 'light' storage mode " "at instantiation. Need to recompute " "autocorrelation matrix...") reduced_likelihood_function_value, par = \ self.reduced_likelihood_function() self.C = par['C'] self.Ft = par['Ft'] self.G = par['G'] rt = linalg.solve_triangular(self.C, r.T, lower=True) if self.beta0 is None: # Universal Kriging u = linalg.solve_triangular(self.G.T, np.dot(self.Ft.T, rt) - f.T, lower=True) else: # Ordinary Kriging u = np.zeros((n_targets, n_eval)) MSE = np.dot(self.sigma2.reshape(n_targets, 1), (1. - (rt ** 2.).sum(axis=0) + (u ** 2.).sum(axis=0))[np.newaxis, :]) MSE = np.sqrt((MSE ** 2.).sum(axis=0) / n_targets) # Mean Squared Error might be slightly negative depending on # machine precision: force to zero! MSE[MSE < 0.] = 0. if self.y_ndim_ == 1: MSE = MSE.ravel() return y, MSE else: return y else: # Memory management if type(batch_size) is not int or batch_size <= 0: raise Exception("batch_size must be a positive integer") if eval_MSE: y, MSE = np.zeros(n_eval), np.zeros(n_eval) for k in range(max(1, n_eval / batch_size)): batch_from = k * batch_size batch_to = min([(k + 1) * batch_size + 1, n_eval + 1]) y[batch_from:batch_to], MSE[batch_from:batch_to] = \ self.predict(X[batch_from:batch_to], eval_MSE=eval_MSE, batch_size=None) return y, MSE else: y = np.zeros(n_eval) for k in range(max(1, n_eval / batch_size)): batch_from = k * batch_size batch_to = min([(k + 1) * batch_size + 1, n_eval + 1]) y[batch_from:batch_to] = \ self.predict(X[batch_from:batch_to], eval_MSE=eval_MSE, batch_size=None) return y def reduced_likelihood_function(self, theta=None): """ This function determines the BLUP parameters and evaluates the reduced likelihood function for the given autocorrelation parameters theta. Maximizing this function wrt the autocorrelation parameters theta is equivalent to maximizing the likelihood of the assumed joint Gaussian distribution of the observations y evaluated onto the design of experiments X. Parameters ---------- theta : array_like, optional An array containing the autocorrelation parameters at which the Gaussian Process model parameters should be determined. Default uses the built-in autocorrelation parameters (ie ``theta = self.theta_``). Returns ------- reduced_likelihood_function_value : double The value of the reduced likelihood function associated to the given autocorrelation parameters theta. par : dict A dictionary containing the requested Gaussian Process model parameters: sigma2 Gaussian Process variance. beta Generalized least-squares regression weights for Universal Kriging or given beta0 for Ordinary Kriging. gamma Gaussian Process weights. C Cholesky decomposition of the correlation matrix [R]. Ft Solution of the linear equation system : [R] x Ft = F G QR decomposition of the matrix Ft. """ check_is_fitted(self, "X") if theta is None: # Use built-in autocorrelation parameters theta = self.theta_ # Initialize output reduced_likelihood_function_value = - np.inf par = {} # Retrieve data n_samples = self.X.shape[0] D = self.D ij = self.ij F = self.F if D is None: # Light storage mode (need to recompute D, ij and F) D, ij = l1_cross_distances(self.X) if (np.min(np.sum(D, axis=1)) == 0. and self.corr != correlation.pure_nugget): raise Exception("Multiple X are not allowed") F = self.regr(self.X) # Set up R r = self.corr(theta, D) R = np.eye(n_samples) * (1. + self.nugget) R[ij[:, 0], ij[:, 1]] = r R[ij[:, 1], ij[:, 0]] = r # Cholesky decomposition of R try: C = linalg.cholesky(R, lower=True) except linalg.LinAlgError: return reduced_likelihood_function_value, par # Get generalized least squares solution Ft = linalg.solve_triangular(C, F, lower=True) try: Q, G = linalg.qr(Ft, econ=True) except: #/usr/lib/python2.6/dist-packages/scipy/linalg/decomp.py:1177: # DeprecationWarning: qr econ argument will be removed after scipy # 0.7. The economy transform will then be available through the # mode='economic' argument. Q, G = linalg.qr(Ft, mode='economic') pass sv = linalg.svd(G, compute_uv=False) rcondG = sv[-1] / sv[0] if rcondG < 1e-10: # Check F sv = linalg.svd(F, compute_uv=False) condF = sv[0] / sv[-1] if condF > 1e15: raise Exception("F is too ill conditioned. Poor combination " "of regression model and observations.") else: # Ft is too ill conditioned, get out (try different theta) return reduced_likelihood_function_value, par Yt = linalg.solve_triangular(C, self.y, lower=True) if self.beta0 is None: # Universal Kriging beta = linalg.solve_triangular(G, np.dot(Q.T, Yt)) else: # Ordinary Kriging beta = np.array(self.beta0) rho = Yt - np.dot(Ft, beta) sigma2 = (rho ** 2.).sum(axis=0) / n_samples # The determinant of R is equal to the squared product of the diagonal # elements of its Cholesky decomposition C detR = (np.diag(C) ** (2. / n_samples)).prod() # Compute/Organize output reduced_likelihood_function_value = - sigma2.sum() * detR par['sigma2'] = sigma2 * self.y_std ** 2. par['beta'] = beta par['gamma'] = linalg.solve_triangular(C.T, rho) par['C'] = C par['Ft'] = Ft par['G'] = G return reduced_likelihood_function_value, par def _arg_max_reduced_likelihood_function(self): """ This function estimates the autocorrelation parameters theta as the maximizer of the reduced likelihood function. (Minimization of the opposite reduced likelihood function is used for convenience) Parameters ---------- self : All parameters are stored in the Gaussian Process model object. Returns ------- optimal_theta : array_like The best set of autocorrelation parameters (the sought maximizer of the reduced likelihood function). optimal_reduced_likelihood_function_value : double The optimal reduced likelihood function value. optimal_par : dict The BLUP parameters associated to thetaOpt. """ # Initialize output best_optimal_theta = [] best_optimal_rlf_value = [] best_optimal_par = [] if self.verbose: print("The chosen optimizer is: " + str(self.optimizer)) if self.random_start > 1: print(str(self.random_start) + " random starts are required.") percent_completed = 0. # Force optimizer to fmin_cobyla if the model is meant to be isotropic if self.optimizer == 'Welch' and self.theta0.size == 1: self.optimizer = 'fmin_cobyla' if self.optimizer == 'fmin_cobyla': def minus_reduced_likelihood_function(log10t): return - self.reduced_likelihood_function( theta=10. ** log10t)[0] constraints = [] for i in range(self.theta0.size): constraints.append(lambda log10t, i=i: log10t[i] - np.log10(self.thetaL[0, i])) constraints.append(lambda log10t, i=i: np.log10(self.thetaU[0, i]) - log10t[i]) for k in range(self.random_start): if k == 0: # Use specified starting point as first guess theta0 = self.theta0 else: # Generate a random starting point log10-uniformly # distributed between bounds log10theta0 = (np.log10(self.thetaL) + self.random_state.rand(*self.theta0.shape) * np.log10(self.thetaU / self.thetaL)) theta0 = 10. ** log10theta0 # Run Cobyla try: log10_optimal_theta = \ optimize.fmin_cobyla(minus_reduced_likelihood_function, np.log10(theta0).ravel(), constraints, iprint=0) except ValueError as ve: print("Optimization failed. Try increasing the ``nugget``") raise ve optimal_theta = 10. ** log10_optimal_theta optimal_rlf_value, optimal_par = \ self.reduced_likelihood_function(theta=optimal_theta) # Compare the new optimizer to the best previous one if k > 0: if optimal_rlf_value > best_optimal_rlf_value: best_optimal_rlf_value = optimal_rlf_value best_optimal_par = optimal_par best_optimal_theta = optimal_theta else: best_optimal_rlf_value = optimal_rlf_value best_optimal_par = optimal_par best_optimal_theta = optimal_theta if self.verbose and self.random_start > 1: if (20 * k) / self.random_start > percent_completed: percent_completed = (20 * k) / self.random_start print("%s completed" % (5 * percent_completed)) optimal_rlf_value = best_optimal_rlf_value optimal_par = best_optimal_par optimal_theta = best_optimal_theta elif self.optimizer == 'Welch': # Backup of the given atrributes theta0, thetaL, thetaU = self.theta0, self.thetaL, self.thetaU corr = self.corr verbose = self.verbose # This will iterate over fmin_cobyla optimizer self.optimizer = 'fmin_cobyla' self.verbose = False # Initialize under isotropy assumption if verbose: print("Initialize under isotropy assumption...") self.theta0 = check_array(self.theta0.min()) self.thetaL = check_array(self.thetaL.min()) self.thetaU = check_array(self.thetaU.max()) theta_iso, optimal_rlf_value_iso, par_iso = \ self._arg_max_reduced_likelihood_function() optimal_theta = theta_iso + np.zeros(theta0.shape) # Iterate over all dimensions of theta allowing for anisotropy if verbose: print("Now improving allowing for anisotropy...") for i in self.random_state.permutation(theta0.size): if verbose: print("Proceeding along dimension %d..." % (i + 1)) self.theta0 = check_array(theta_iso) self.thetaL = check_array(thetaL[0, i]) self.thetaU = check_array(thetaU[0, i]) def corr_cut(t, d): return corr(check_array(np.hstack([optimal_theta[0][0:i], t[0], optimal_theta[0][(i + 1)::]])), d) self.corr = corr_cut optimal_theta[0, i], optimal_rlf_value, optimal_par = \ self._arg_max_reduced_likelihood_function() # Restore the given atrributes self.theta0, self.thetaL, self.thetaU = theta0, thetaL, thetaU self.corr = corr self.optimizer = 'Welch' self.verbose = verbose else: raise NotImplementedError("This optimizer ('%s') is not " "implemented yet. Please contribute!" % self.optimizer) return optimal_theta, optimal_rlf_value, optimal_par def _check_params(self, n_samples=None): # Check regression model if not callable(self.regr): if self.regr in self._regression_types: self.regr = self._regression_types[self.regr] else: raise ValueError("regr should be one of %s or callable, " "%s was given." % (self._regression_types.keys(), self.regr)) # Check regression weights if given (Ordinary Kriging) if self.beta0 is not None: self.beta0 = check_array(self.beta0) if self.beta0.shape[1] != 1: # Force to column vector self.beta0 = self.beta0.T # Check correlation model if not callable(self.corr): if self.corr in self._correlation_types: self.corr = self._correlation_types[self.corr] else: raise ValueError("corr should be one of %s or callable, " "%s was given." % (self._correlation_types.keys(), self.corr)) # Check storage mode if self.storage_mode != 'full' and self.storage_mode != 'light': raise ValueError("Storage mode should either be 'full' or " "'light', %s was given." % self.storage_mode) # Check correlation parameters self.theta0 = check_array(self.theta0) lth = self.theta0.size if self.thetaL is not None and self.thetaU is not None: self.thetaL = check_array(self.thetaL) self.thetaU = check_array(self.thetaU) if self.thetaL.size != lth or self.thetaU.size != lth: raise ValueError("theta0, thetaL and thetaU must have the " "same length.") if np.any(self.thetaL <= 0) or np.any(self.thetaU < self.thetaL): raise ValueError("The bounds must satisfy O < thetaL <= " "thetaU.") elif self.thetaL is None and self.thetaU is None: if np.any(self.theta0 <= 0): raise ValueError("theta0 must be strictly positive.") elif self.thetaL is None or self.thetaU is None: raise ValueError("thetaL and thetaU should either be both or " "neither specified.") # Force verbose type to bool self.verbose = bool(self.verbose) # Force normalize type to bool self.normalize = bool(self.normalize) # Check nugget value self.nugget = np.asarray(self.nugget) if np.any(self.nugget) < 0.: raise ValueError("nugget must be positive or zero.") if (n_samples is not None and self.nugget.shape not in [(), (n_samples,)]): raise ValueError("nugget must be either a scalar " "or array of length n_samples.") # Check optimizer if self.optimizer not in self._optimizer_types: raise ValueError("optimizer should be one of %s" % self._optimizer_types) # Force random_start type to int self.random_start = int(self.random_start)
bsd-3-clause
bajibabu/merlin
src/frontend/label_normalisation.py
1
40966
import os import numpy, re, sys from multiprocessing import Pool from io_funcs.binary_io import BinaryIOCollection from .linguistic_base import LinguisticBase import matplotlib.mlab as mlab import math import logging # from logplot.logging_plotting import LoggerPlotter #, MultipleTimeSeriesPlot, SingleWeightMatrixPlot class LabelNormalisation(LinguisticBase): # this class only knows how to deal with a single style of labels (XML or HTS) # (to deal with composite labels, use LabelComposer instead) def __init__(self, question_file_name=None,xpath_file_name=None): pass def extract_linguistic_features(self, in_file_name, out_file_name=None, label_type="state_align", dur_file_name=None): if label_type=="phone_align": A = self.load_labels_with_phone_alignment(in_file_name, dur_file_name) elif label_type=="state_align": A = self.load_labels_with_state_alignment(in_file_name) else: logger.critical("we don't support %s labels as of now!!" % (label_type)) if out_file_name: io_funcs = BinaryIOCollection() io_funcs.array_to_binary_file(A, out_file_name) else: return A # ----------------------------- class HTSLabelNormalisation(LabelNormalisation): """This class is to convert HTS format labels into continous or binary values, and store as binary format with float32 precision. The class supports two kinds of questions: QS and CQS. **QS**: is the same as that used in HTS **CQS**: is the new defined question in the system. Here is an example of the question: CQS C-Syl-Tone {_(\d+)+}. regular expression is used for continous values. Time alignments are expected in the HTS labels. Here is an example of the HTS labels: 3050000 3100000 xx~#-p+l=i:1_4/A/0_0_0/B/1-1-4:1-1&1-4#1-3$1-4>0-1<0-1|i/C/1+1+3/D/0_0/E/content+1:1+3&1+2#0+1/F/content_1/G/0_0/H/4=3:1=1&L-L%/I/0_0/J/4+3-1[2] 3100000 3150000 xx~#-p+l=i:1_4/A/0_0_0/B/1-1-4:1-1&1-4#1-3$1-4>0-1<0-1|i/C/1+1+3/D/0_0/E/content+1:1+3&1+2#0+1/F/content_1/G/0_0/H/4=3:1=1&L-L%/I/0_0/J/4+3-1[3] 3150000 3250000 xx~#-p+l=i:1_4/A/0_0_0/B/1-1-4:1-1&1-4#1-3$1-4>0-1<0-1|i/C/1+1+3/D/0_0/E/content+1:1+3&1+2#0+1/F/content_1/G/0_0/H/4=3:1=1&L-L%/I/0_0/J/4+3-1[4] 3250000 3350000 xx~#-p+l=i:1_4/A/0_0_0/B/1-1-4:1-1&1-4#1-3$1-4>0-1<0-1|i/C/1+1+3/D/0_0/E/content+1:1+3&1+2#0+1/F/content_1/G/0_0/H/4=3:1=1&L-L%/I/0_0/J/4+3-1[5] 3350000 3900000 xx~#-p+l=i:1_4/A/0_0_0/B/1-1-4:1-1&1-4#1-3$1-4>0-1<0-1|i/C/1+1+3/D/0_0/E/content+1:1+3&1+2#0+1/F/content_1/G/0_0/H/4=3:1=1&L-L%/I/0_0/J/4+3-1[6] 305000 310000 are the starting and ending time. [2], [3], [4], [5], [6] mean the HMM state index. """ # this subclass support HTS labels, which include time alignments def __init__(self, question_file_name=None, add_frame_features=True, subphone_feats='full', continuous_flag=True): logger = logging.getLogger("labels") self.question_dict = {} self.ori_question_dict = {} self.dict_size = 0 self.continuous_flag = continuous_flag try: # self.question_dict, self.ori_question_dict = self.load_question_set(question_file_name) self.discrete_dict, self.continuous_dict = self.load_question_set_continous(question_file_name) except: logger.critical('error whilst loading HTS question set') raise ###self.dict_size = len(self.question_dict) self.dict_size = len(self.discrete_dict) + len(self.continuous_dict) self.add_frame_features = add_frame_features self.subphone_feats = subphone_feats if self.subphone_feats == 'full': self.frame_feature_size = 9 ## zhizheng's original 5 state features + 4 phoneme features elif self.subphone_feats == 'minimal_frame': self.frame_feature_size = 2 ## the minimal features necessary to go from a state-level to frame-level model elif self.subphone_feats == 'state_only': self.frame_feature_size = 1 ## this is equivalent to a state-based system elif self.subphone_feats == 'none': self.frame_feature_size = 0 ## the phoneme level features only elif self.subphone_feats == 'frame_only': self.frame_feature_size = 1 ## this is equivalent to a frame-based system without relying on state-features elif self.subphone_feats == 'uniform_state': self.frame_feature_size = 2 ## this is equivalent to a frame-based system with uniform state-features elif self.subphone_feats == 'minimal_phoneme': self.frame_feature_size = 3 ## this is equivalent to a frame-based system with minimal features elif self.subphone_feats == 'coarse_coding': self.frame_feature_size = 4 ## this is equivalent to a frame-based positioning system reported in Heiga Zen's work self.cc_features = self.compute_coarse_coding_features(3) else: sys.exit('Unknown value for subphone_feats: %s'%(subphone_feats)) self.dimension = self.dict_size + self.frame_feature_size ### if user wants to define their own input, simply set the question set to empty. if self.dict_size == 0: self.dimension = 0 logger.debug('HTS-derived input feature dimension is %d + %d = %d' % (self.dict_size, self.frame_feature_size, self.dimension) ) def prepare_dur_data(self, ori_file_list, output_file_list, label_type="state_align", feature_type=None, unit_size=None, feat_size=None): ''' extracting duration binary features or numerical features. ''' logger = logging.getLogger("dur") utt_number = len(ori_file_list) if utt_number != len(output_file_list): print("the number of input and output files should be the same!\n"); sys.exit(1) ### set default feature type to numerical, if not assigned ### if not feature_type: feature_type = "numerical" ### set default unit size to state, if not assigned ### if not unit_size: unit_size = "state" if label_type=="phone_align": unit_size = "phoneme" ### set default feat size to frame or phoneme, if not assigned ### if feature_type=="binary": if not feat_size: feat_size = "frame" elif feature_type=="numerical": if not feat_size: feat_size = "phoneme" else: logger.critical("Unknown feature type: %s \n Please use one of the following: binary, numerical\n" %(feature_type)) sys.exit(1) for i in range(utt_number): self.extract_dur_features(ori_file_list[i], output_file_list[i], label_type, feature_type, unit_size, feat_size) def extract_dur_features(self, in_file_name, out_file_name=None, label_type="state_align", feature_type=None, unit_size=None, feat_size=None): logger = logging.getLogger("dur") if label_type=="phone_align": A = self.extract_dur_from_phone_alignment_labels(in_file_name, feature_type, unit_size, feat_size) elif label_type=="state_align": A = self.extract_dur_from_state_alignment_labels(in_file_name, feature_type, unit_size, feat_size) else: logger.critical("we don't support %s labels as of now!!" % (label_type)) sys.exit(1) if out_file_name: io_funcs = BinaryIOCollection() io_funcs.array_to_binary_file(A, out_file_name) else: return A def extract_dur_from_state_alignment_labels(self, file_name, feature_type, unit_size, feat_size): logger = logging.getLogger("dur") state_number = 5 dur_dim = state_number if feature_type=="binary": dur_feature_matrix = numpy.empty((100000, 1)) elif feature_type=="numerical": if unit_size=="state": dur_feature_matrix = numpy.empty((100000, dur_dim)) current_dur_array = numpy.zeros((dur_dim, 1)) elif unit_size=="phoneme": dur_feature_matrix = numpy.empty((100000, 1)) fid = open(file_name) utt_labels = fid.readlines() fid.close() label_number = len(utt_labels) logger.info('loaded %s, %3d labels' % (file_name, label_number) ) current_index = 0 dur_feature_index = 0 for line in utt_labels: line = line.strip() if len(line) < 1: continue temp_list = re.split('\s+', line) start_time = int(temp_list[0]) end_time = int(temp_list[1]) full_label = temp_list[2] full_label_length = len(full_label) - 3 # remove state information [k] state_index = full_label[full_label_length + 1] state_index = int(state_index) - 1 frame_number = int((end_time - start_time)/50000) if state_index == 1: phone_duration = frame_number for i in range(state_number - 1): line = utt_labels[current_index + i + 1].strip() temp_list = re.split('\s+', line) phone_duration += int((int(temp_list[1]) - int(temp_list[0]))/50000) if feature_type == "binary": current_block_array = numpy.zeros((frame_number, 1)) if unit_size == "state": current_block_array[-1] = 1 elif unit_size == "phoneme": if state_index == state_number: current_block_array[-1] = 1 else: logger.critical("Unknown unit size: %s \n Please use one of the following: state, phoneme\n" %(unit_size)) sys.exit(1) elif feature_type == "numerical": if unit_size == "state": current_dur_array[current_index%5] = frame_number if feat_size == "phoneme" and state_index == state_number: current_block_array = current_dur_array.transpose() if feat_size == "frame": current_block_array = numpy.tile(current_dur_array.transpose(), (frame_number, 1)) elif unit_size == "phoneme": current_block_array = numpy.array([phone_duration]) ### writing into dur_feature_matrix ### if feat_size == "frame": dur_feature_matrix[dur_feature_index:dur_feature_index+frame_number,] = current_block_array dur_feature_index = dur_feature_index + frame_number elif feat_size == "phoneme" and state_index == state_number: dur_feature_matrix[dur_feature_index:dur_feature_index+1,] = current_block_array dur_feature_index = dur_feature_index + 1 current_index += 1 dur_feature_matrix = dur_feature_matrix[0:dur_feature_index,] logger.debug('made duration matrix of %d frames x %d features' % dur_feature_matrix.shape ) return dur_feature_matrix def extract_dur_from_phone_alignment_labels(self, file_name, feature_type, unit_size, feat_size): logger = logging.getLogger("dur") dur_dim = 1 if feature_type=="binary": dur_feature_matrix = numpy.empty((100000, 1)) elif feature_type=="numerical": if unit_size=="phoneme": dur_feature_matrix = numpy.empty((100000, 1)) fid = open(file_name) utt_labels = fid.readlines() fid.close() label_number = len(utt_labels) logger.info('loaded %s, %3d labels' % (file_name, label_number) ) current_index = 0 dur_feature_index = 0 for line in utt_labels: line = line.strip() if len(line) < 1: continue temp_list = re.split('\s+', line) start_time = int(temp_list[0]) end_time = int(temp_list[1]) full_label = temp_list[2] frame_number = int((end_time - start_time)/50000) phone_duration = frame_number if feature_type == "binary": current_block_array = numpy.zeros((frame_number, 1)) if unit_size == "phoneme": current_block_array[-1] = 1 else: logger.critical("Unknown unit size: %s \n Please use one of the following: phoneme\n" %(unit_size)) sys.exit(1) elif feature_type == "numerical": if unit_size == "phoneme": current_block_array = numpy.array([phone_duration]) ### writing into dur_feature_matrix ### if feat_size == "frame": dur_feature_matrix[dur_feature_index:dur_feature_index+frame_number,] = current_block_array dur_feature_index = dur_feature_index + frame_number elif feat_size == "phoneme": dur_feature_matrix[dur_feature_index:dur_feature_index+1,] = current_block_array dur_feature_index = dur_feature_index + 1 current_index += 1 dur_feature_matrix = dur_feature_matrix[0:dur_feature_index,] logger.debug('made duration matrix of %d frames x %d features' % dur_feature_matrix.shape ) return dur_feature_matrix def load_labels_with_phone_alignment(self, file_name, dur_file_name): # this is not currently used ??? -- it works now :D logger = logging.getLogger("labels") #logger.critical('unused function ???') #raise Exception if dur_file_name: io_funcs = BinaryIOCollection() dur_dim = 1 ## hard coded for now manual_dur_data = io_funcs.load_binary_file(dur_file_name, dur_dim) if self.add_frame_features: assert self.dimension == self.dict_size+self.frame_feature_size elif self.subphone_feats != 'none': assert self.dimension == self.dict_size+self.frame_feature_size else: assert self.dimension == self.dict_size label_feature_matrix = numpy.empty((100000, self.dimension)) ph_count=0 label_feature_index = 0 fid = open(file_name) for line in fid.readlines(): line = line.strip() if len(line) < 1: continue temp_list = re.split('\s+', line) if len(temp_list)==1: frame_number = 0 full_label = temp_list[0] else: start_time = int(temp_list[0]) end_time = int(temp_list[1]) full_label = temp_list[2] # to do - support different frame shift - currently hardwired to 5msec # currently under beta testing: support different frame shift if dur_file_name: frame_number = manual_dur_data[ph_count] else: frame_number = int((end_time - start_time)/50000) if self.subphone_feats == "coarse_coding": cc_feat_matrix = self.extract_coarse_coding_features_relative(frame_number) ph_count = ph_count+1 #label_binary_vector = self.pattern_matching(full_label) label_binary_vector = self.pattern_matching_binary(full_label) # if there is no CQS question, the label_continuous_vector will become to empty label_continuous_vector = self.pattern_matching_continous_position(full_label) label_vector = numpy.concatenate([label_binary_vector, label_continuous_vector], axis = 1) if self.add_frame_features: current_block_binary_array = numpy.zeros((frame_number, self.dict_size+self.frame_feature_size)) for i in range(frame_number): current_block_binary_array[i, 0:self.dict_size] = label_vector if self.subphone_feats == 'minimal_phoneme': ## features which distinguish frame position in phoneme current_block_binary_array[i, self.dict_size] = float(i+1)/float(frame_number) # fraction through phone forwards current_block_binary_array[i, self.dict_size+1] = float(frame_number - i)/float(frame_number) # fraction through phone backwards current_block_binary_array[i, self.dict_size+2] = float(frame_number) # phone duration elif self.subphone_feats == 'coarse_coding': ## features which distinguish frame position in phoneme using three continous numerical features current_block_binary_array[i, self.dict_size+0] = cc_feat_matrix[i, 0] current_block_binary_array[i, self.dict_size+1] = cc_feat_matrix[i, 1] current_block_binary_array[i, self.dict_size+2] = cc_feat_matrix[i, 2] current_block_binary_array[i, self.dict_size+3] = float(frame_number) elif self.subphone_feats == 'none': pass else: sys.exit('unknown subphone_feats type') label_feature_matrix[label_feature_index:label_feature_index+frame_number,] = current_block_binary_array label_feature_index = label_feature_index + frame_number elif self.subphone_feats == 'none': current_block_binary_array = label_vector label_feature_matrix[label_feature_index:label_feature_index+1,] = current_block_binary_array label_feature_index = label_feature_index + 1 fid.close() label_feature_matrix = label_feature_matrix[0:label_feature_index,] logger.info('loaded %s, %3d labels' % (file_name, ph_count) ) logger.debug('made label matrix of %d frames x %d labels' % label_feature_matrix.shape ) return label_feature_matrix def load_labels_with_state_alignment(self, file_name): ## setting add_frame_features to False performs either state/phoneme level normalisation logger = logging.getLogger("labels") if self.add_frame_features: assert self.dimension == self.dict_size+self.frame_feature_size elif self.subphone_feats != 'none': assert self.dimension == self.dict_size+self.frame_feature_size else: assert self.dimension == self.dict_size # label_feature_matrix = numpy.empty((100000, self.dict_size+self.frame_feature_size)) label_feature_matrix = numpy.empty((100000, self.dimension)) label_feature_index = 0 state_number = 5 lab_binary_vector = numpy.zeros((1, self.dict_size)) fid = open(file_name) utt_labels = fid.readlines() fid.close() current_index = 0 label_number = len(utt_labels) logger.info('loaded %s, %3d labels' % (file_name, label_number) ) phone_duration = 0 state_duration_base = 0 for line in utt_labels: line = line.strip() if len(line) < 1: continue temp_list = re.split('\s+', line) if len(temp_list)==1: frame_number = 0 state_index = 1 full_label = temp_list[0] else: start_time = int(temp_list[0]) end_time = int(temp_list[1]) frame_number = int((end_time - start_time)/50000) full_label = temp_list[2] full_label_length = len(full_label) - 3 # remove state information [k] state_index = full_label[full_label_length + 1] state_index = int(state_index) - 1 state_index_backward = 6 - state_index full_label = full_label[0:full_label_length] if state_index == 1: current_frame_number = 0 phone_duration = frame_number state_duration_base = 0 # label_binary_vector = self.pattern_matching(full_label) label_binary_vector = self.pattern_matching_binary(full_label) # if there is no CQS question, the label_continuous_vector will become to empty label_continuous_vector = self.pattern_matching_continous_position(full_label) label_vector = numpy.concatenate([label_binary_vector, label_continuous_vector], axis = 1) if len(temp_list)==1: state_index = state_number else: for i in range(state_number - 1): line = utt_labels[current_index + i + 1].strip() temp_list = re.split('\s+', line) phone_duration += int((int(temp_list[1]) - int(temp_list[0]))/50000) if self.subphone_feats == "coarse_coding": cc_feat_matrix = self.extract_coarse_coding_features_relative(phone_duration) if self.add_frame_features: current_block_binary_array = numpy.zeros((frame_number, self.dict_size+self.frame_feature_size)) for i in range(frame_number): current_block_binary_array[i, 0:self.dict_size] = label_vector if self.subphone_feats == 'full': ## Zhizheng's original 9 subphone features: current_block_binary_array[i, self.dict_size] = float(i+1) / float(frame_number) ## fraction through state (forwards) current_block_binary_array[i, self.dict_size+1] = float(frame_number - i) / float(frame_number) ## fraction through state (backwards) current_block_binary_array[i, self.dict_size+2] = float(frame_number) ## length of state in frames current_block_binary_array[i, self.dict_size+3] = float(state_index) ## state index (counting forwards) current_block_binary_array[i, self.dict_size+4] = float(state_index_backward) ## state index (counting backwards) current_block_binary_array[i, self.dict_size+5] = float(phone_duration) ## length of phone in frames current_block_binary_array[i, self.dict_size+6] = float(frame_number) / float(phone_duration) ## fraction of the phone made up by current state current_block_binary_array[i, self.dict_size+7] = float(phone_duration - i - state_duration_base) / float(phone_duration) ## fraction through phone (backwards) current_block_binary_array[i, self.dict_size+8] = float(state_duration_base + i + 1) / float(phone_duration) ## fraction through phone (forwards) elif self.subphone_feats == 'state_only': ## features which only distinguish state: current_block_binary_array[i, self.dict_size] = float(state_index) ## state index (counting forwards) elif self.subphone_feats == 'frame_only': ## features which distinguish frame position in phoneme: current_frame_number += 1 current_block_binary_array[i, self.dict_size] = float(current_frame_number) / float(phone_duration) ## fraction through phone (counting forwards) elif self.subphone_feats == 'uniform_state': ## features which distinguish frame position in phoneme: current_frame_number += 1 current_block_binary_array[i, self.dict_size] = float(current_frame_number) / float(phone_duration) ## fraction through phone (counting forwards) new_state_index = max(1, round(float(current_frame_number)/float(phone_duration)*5)) current_block_binary_array[i, self.dict_size+1] = float(new_state_index) ## state index (counting forwards) elif self.subphone_feats == "coarse_coding": ## features which distinguish frame position in phoneme using three continous numerical features current_block_binary_array[i, self.dict_size+0] = cc_feat_matrix[current_frame_number, 0] current_block_binary_array[i, self.dict_size+1] = cc_feat_matrix[current_frame_number, 1] current_block_binary_array[i, self.dict_size+2] = cc_feat_matrix[current_frame_number, 2] current_block_binary_array[i, self.dict_size+3] = float(phone_duration) current_frame_number += 1 elif self.subphone_feats == 'minimal_frame': ## features which distinguish state and minimally frame position in state: current_block_binary_array[i, self.dict_size] = float(i+1) / float(frame_number) ## fraction through state (forwards) current_block_binary_array[i, self.dict_size+1] = float(state_index) ## state index (counting forwards) elif self.subphone_feats == 'none': pass else: sys.exit('unknown subphone_feats type') label_feature_matrix[label_feature_index:label_feature_index+frame_number,] = current_block_binary_array label_feature_index = label_feature_index + frame_number elif self.subphone_feats == 'state_only' and state_index == state_number: current_block_binary_array = numpy.zeros((state_number, self.dict_size+self.frame_feature_size)) for i in range(state_number): current_block_binary_array[i, 0:self.dict_size] = label_vector current_block_binary_array[i, self.dict_size] = float(i+1) ## state index (counting forwards) label_feature_matrix[label_feature_index:label_feature_index+state_number,] = current_block_binary_array label_feature_index = label_feature_index + state_number elif self.subphone_feats == 'none' and state_index == state_number: current_block_binary_array = label_vector label_feature_matrix[label_feature_index:label_feature_index+1,] = current_block_binary_array label_feature_index = label_feature_index + 1 state_duration_base += frame_number current_index += 1 label_feature_matrix = label_feature_matrix[0:label_feature_index,] logger.debug('made label matrix of %d frames x %d labels' % label_feature_matrix.shape ) return label_feature_matrix def extract_durational_features(self, dur_file_name=None, dur_data=None): if dur_file_name: io_funcs = BinaryIOCollection() dur_dim = 1 ## hard coded for now dur_data = io_funcs.load_binary_file(dur_file_name, dur_dim) ph_count = len(dur_data) total_num_of_frames = int(sum(dur_data)) duration_feature_array = numpy.zeros((total_num_of_frames, self.frame_feature_size)) frame_index=0 for i in range(ph_count): frame_number = int(dur_data[i]) if self.subphone_feats == "coarse_coding": cc_feat_matrix = self.extract_coarse_coding_features_relative(frame_number) for j in range(frame_number): duration_feature_array[frame_index, 0] = cc_feat_matrix[j, 0] duration_feature_array[frame_index, 1] = cc_feat_matrix[j, 1] duration_feature_array[frame_index, 2] = cc_feat_matrix[j, 2] duration_feature_array[frame_index, 3] = float(frame_number) frame_index+=1 return duration_feature_array def compute_coarse_coding_features(self, num_states): assert num_states == 3 npoints = 600 cc_features = numpy.zeros((num_states, npoints)) x1 = numpy.linspace(-1.5, 1.5, npoints) x2 = numpy.linspace(-1.0, 2.0, npoints) x3 = numpy.linspace(-0.5, 2.5, npoints) mu1 = 0.0 mu2 = 0.5 mu3 = 1.0 sigma = 0.4 cc_features[0, :] = mlab.normpdf(x1, mu1, sigma) cc_features[1, :] = mlab.normpdf(x2, mu2, sigma) cc_features[2, :] = mlab.normpdf(x3, mu3, sigma) return cc_features def extract_coarse_coding_features_relative(self, phone_duration): dur = int(phone_duration) cc_feat_matrix = numpy.zeros((dur, 3)) for i in range(dur): rel_indx = int((200/float(dur))*i) cc_feat_matrix[i,0] = self.cc_features[0, 300+rel_indx] cc_feat_matrix[i,1] = self.cc_features[1, 200+rel_indx] cc_feat_matrix[i,2] = self.cc_features[2, 100+rel_indx] return cc_feat_matrix ### this function is not used now def extract_coarse_coding_features_absolute(self, phone_duration): dur = int(phone_duration) cc_feat_matrix = numpy.zeros((dur, 3)) npoints1 = (dur*2)*10+1 npoints2 = (dur-1)*10+1 npoints3 = (2*dur-1)*10+1 x1 = numpy.linspace(-dur, dur, npoints1) x2 = numpy.linspace(1, dur, npoints2) x3 = numpy.linspace(1, 2*dur-1, npoints3) mu1 = 0 mu2 = (1+dur)/2 mu3 = dur variance = 1 sigma = variance*((dur/10)+2) sigma1 = sigma sigma2 = sigma-1 sigma3 = sigma y1 = mlab.normpdf(x1, mu1, sigma1) y2 = mlab.normpdf(x2, mu2, sigma2) y3 = mlab.normpdf(x3, mu3, sigma3) for i in range(dur): cc_feat_matrix[i,0] = y1[(dur+1+i)*10] cc_feat_matrix[i,1] = y2[i*10] cc_feat_matrix[i,2] = y3[i*10] for i in range(3): cc_feat_matrix[:,i] = cc_feat_matrix[:,i]/max(cc_feat_matrix[:,i]) return cc_feat_matrix ### this function is not used now def pattern_matching(self, label): # this function is where most time is spent during label preparation # # it might be possible to speed it up by using pre-compiled regular expressions? # (not trying this now, since we may change to to XML tree format for input instead of HTS labels) # label_size = len(label) lab_binary_vector = numpy.zeros((1, self.dict_size)) for i in range(self.dict_size): current_question_list = self.question_dict[str(i)] binary_flag = 0 for iq in range(len(current_question_list)): current_question = current_question_list[iq] current_size = len(current_question) if current_question[0] == '*' and current_question[current_size-1] == '*': temp_question = current_question[1:current_size-1] for il in range(1, label_size-current_size+2): if temp_question == label[il:il+current_size-2]: binary_flag = 1 elif current_question[current_size-1] != '*': temp_question = current_question[1:current_size] if temp_question == label[label_size-current_size+1:label_size]: binary_flag = 1 elif current_question[0] != '*': temp_question = current_question[0:current_size-1] if temp_question == label[0:current_size-1]: binary_flag = 1 if binary_flag == 1: break lab_binary_vector[0, i] = binary_flag return lab_binary_vector def pattern_matching_binary(self, label): dict_size = len(self.discrete_dict) lab_binary_vector = numpy.zeros((1, dict_size)) for i in range(dict_size): current_question_list = self.discrete_dict[str(i)] binary_flag = 0 for iq in range(len(current_question_list)): current_compiled = current_question_list[iq] ms = current_compiled.search(label) if ms is not None: binary_flag = 1 break lab_binary_vector[0, i] = binary_flag return lab_binary_vector def pattern_matching_continous_position(self, label): dict_size = len(self.continuous_dict) lab_continuous_vector = numpy.zeros((1, dict_size)) for i in range(dict_size): continuous_value = -1.0 current_compiled = self.continuous_dict[str(i)] ms = current_compiled.search(label) if ms is not None: # assert len(ms.group()) == 1 continuous_value = ms.group(1) lab_continuous_vector[0, i] = continuous_value return lab_continuous_vector def load_question_set(self, qs_file_name): fid = open(qs_file_name) question_index = 0 question_dict = {} ori_question_dict = {} for line in fid.readlines(): line = line.replace('\n', '') if len(line) > 5: temp_list = line.split('{') temp_line = temp_list[1] temp_list = temp_line.split('}') temp_line = temp_list[0] question_list = temp_line.split(',') question_dict[str(question_index)] = question_list ori_question_dict[str(question_index)] = line question_index += 1 fid.close() logger = logging.getLogger("labels") logger.debug('loaded question set with %d questions' % len(question_dict)) return question_dict, ori_question_dict def load_question_set_continous(self, qs_file_name): logger = logging.getLogger("labels") fid = open(qs_file_name) binary_qs_index = 0 continuous_qs_index = 0 binary_dict = {} continuous_dict = {} LL=re.compile(re.escape('LL-')) for line in fid.readlines(): line = line.replace('\n', '') if len(line) > 5: temp_list = line.split('{') temp_line = temp_list[1] temp_list = temp_line.split('}') temp_line = temp_list[0] temp_line = temp_line.strip() question_list = temp_line.split(',') temp_list = line.split(' ') question_key = temp_list[1] # print line if temp_list[0] == 'CQS': assert len(question_list) == 1 processed_question = self.wildcards2regex(question_list[0], convert_number_pattern=True) continuous_dict[str(continuous_qs_index)] = re.compile(processed_question) #save pre-compiled regular expression continuous_qs_index = continuous_qs_index + 1 elif temp_list[0] == 'QS': re_list = [] for temp_question in question_list: processed_question = self.wildcards2regex(temp_question) if LL.search(question_key): processed_question = '^'+processed_question re_list.append(re.compile(processed_question)) binary_dict[str(binary_qs_index)] = re_list binary_qs_index = binary_qs_index + 1 else: logger.critical('The question set is not defined correctly: %s' %(line)) raise Exception # question_index = question_index + 1 return binary_dict, continuous_dict def wildcards2regex(self, question, convert_number_pattern=False): """ Convert HTK-style question into regular expression for searching labels. If convert_number_pattern, keep the following sequences unescaped for extracting continuous values): (\d+) -- handles digit without decimal point ([\d\.]+) -- handles digits with and without decimal point """ ## handle HTK wildcards (and lack of them) at ends of label: prefix = "" postfix = "" if '*' in question: if not question.startswith('*'): prefix = "\A" if not question.endswith('*'): postfix = "\Z" question = question.strip('*') question = re.escape(question) ## convert remaining HTK wildcards * and ? to equivalent regex: question = question.replace('\\*', '.*') question = prefix + question + postfix if convert_number_pattern: question = question.replace('\\(\\\\d\\+\\)', '(\d+)') question = question.replace('\\(\\[\\\\d\\\\\\.\\]\\+\\)', '([\d\.]+)') return question class HTSDurationLabelNormalisation(HTSLabelNormalisation): """ Unlike HTSLabelNormalisation, HTSDurationLabelNormalisation does not accept timings. One line of labels is converted into 1 datapoint, that is, the label is not 'unpacked' into frames. HTK state index [\d] is not handled in any special way. """ def __init__(self, question_file_name=None, subphone_feats='full', continuous_flag=True): super(HTSDurationLabelNormalisation, self).__init__(question_file_name=question_file_name, \ subphone_feats=subphone_feats, continuous_flag=continuous_flag) ## don't use extra features beyond those in questions for duration labels: self.dimension = self.dict_size def load_labels_with_state_alignment(self, file_name, add_frame_features=False): ## add_frame_features not used in HTSLabelNormalisation -- only in XML version logger = logging.getLogger("labels") assert self.dimension == self.dict_size label_feature_matrix = numpy.empty((100000, self.dimension)) label_feature_index = 0 lab_binary_vector = numpy.zeros((1, self.dict_size)) fid = open(file_name) utt_labels = fid.readlines() fid.close() current_index = 0 label_number = len(utt_labels) logger.info('loaded %s, %3d labels' % (file_name, label_number) ) ## remove empty lines utt_labels = [line for line in utt_labels if line != ''] for (line_number, line) in enumerate(utt_labels): temp_list = re.split('\s+', line.strip()) full_label = temp_list[-1] ## take last entry -- ignore timings if present label_binary_vector = self.pattern_matching_binary(full_label) # if there is no CQS question, the label_continuous_vector will become to empty label_continuous_vector = self.pattern_matching_continous_position(full_label) label_vector = numpy.concatenate([label_binary_vector, label_continuous_vector], axis = 1) label_feature_matrix[line_number, :] = label_vector[:] label_feature_matrix = label_feature_matrix[:line_number+1,:] logger.debug('made label matrix of %d frames x %d labels' % label_feature_matrix.shape ) return label_feature_matrix # ----------------------------- if __name__ == '__main__': qs_file_name = '/afs/inf.ed.ac.uk/group/cstr/projects/blizzard_entries/blizzard2016/straight_voice/Hybrid_duration_experiments/dnn_tts_release/lstm_rnn/data/questions.hed' print(qs_file_name) ori_file_list = ['/afs/inf.ed.ac.uk/group/cstr/projects/blizzard_entries/blizzard2016/straight_voice/Hybrid_duration_experiments/dnn_tts_release/lstm_rnn/data/label_state_align/AMidsummerNightsDream_000_000.lab'] output_file_list = ['/afs/inf.ed.ac.uk/group/cstr/projects/blizzard_entries/blizzard2016/straight_voice/Hybrid_duration_experiments/dnn_tts_release/lstm_rnn/data/binary_label_601/AMidsummerNightsDream_000_000.lab'] #output_file_list = ['/afs/inf.ed.ac.uk/group/cstr/projects/blizzard_entries/blizzard2016/straight_voice/Hybrid_duration_experiments/dnn_tts_release/lstm_rnn/data/dur/AMidsummerNightsDream_000_000.dur'] label_operater = HTSLabelNormalisation(qs_file_name) label_operater.perform_normalisation(ori_file_list, output_file_list) #feature_type="binary" #unit_size = "phoneme" #feat_size = "phoneme" #label_operater.prepare_dur_data(ori_file_list, output_file_list, feature_type, unit_size, feat_size) #label_operater.prepare_dur_data(ori_file_list, output_file_list, feature_type) print(label_operater.dimension)
apache-2.0
biocore/qiita
qiita_db/test/test_processing_job.py
3
50090
# ----------------------------------------------------------------------------- # Copyright (c) 2014--, The Qiita Development Team. # # Distributed under the terms of the BSD 3-clause License. # # The full license is in the file LICENSE, distributed with this software. # ----------------------------------------------------------------------------- from unittest import TestCase, main from datetime import datetime from os import close from tempfile import mkstemp from json import dumps, loads from time import sleep import networkx as nx import pandas as pd import qiita_db as qdb from qiita_core.util import qiita_test_checker def _create_job(force=True): job = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), qdb.software.Parameters.load( qdb.software.Command(2), values_dict={"min_seq_len": 100, "max_seq_len": 1000, "trim_seq_length": False, "min_qual_score": 25, "max_ambig": 6, "max_homopolymer": 6, "max_primer_mismatch": 0, "barcode_type": "golay_12", "max_barcode_errors": 1.5, "disable_bc_correction": False, "qual_score_window": 0, "disable_primers": False, "reverse_primers": "disable", "reverse_primer_mismatches": 0, "truncate_ambi_bases": False, "input_data": 1}), force) return job @qiita_test_checker() class ProcessingJobUtilTest(TestCase): def test_system_call(self): obs_out, obs_err, obs_status = qdb.processing_job._system_call( 'echo "Test system call stdout"') self.assertEqual(obs_out, "Test system call stdout\n") self.assertEqual(obs_err, "") self.assertEqual(obs_status, 0) def test_system_call_error(self): obs_out, obs_err, obs_status = qdb.processing_job._system_call( '>&2 echo "Test system call stderr"; exit 1') self.assertEqual(obs_out, "") self.assertEqual(obs_err, "Test system call stderr\n") self.assertEqual(obs_status, 1) @qiita_test_checker() class ProcessingJobTest(TestCase): def setUp(self): self.tester1 = qdb.processing_job.ProcessingJob( "063e553b-327c-4818-ab4a-adfe58e49860") self.tester2 = qdb.processing_job.ProcessingJob( "bcc7ebcd-39c1-43e4-af2d-822e3589f14d") self.tester3 = qdb.processing_job.ProcessingJob( "b72369f9-a886-4193-8d3d-f7b504168e75") self.tester4 = qdb.processing_job.ProcessingJob( "d19f76ee-274e-4c1b-b3a2-a12d73507c55") self._clean_up_files = [] def _get_all_job_ids(self): sql = "SELECT processing_job_id FROM qiita.processing_job" with qdb.sql_connection.TRN: qdb.sql_connection.TRN.add(sql) return qdb.sql_connection.TRN.execute_fetchflatten() def _wait_for_job(self, job): while job.status not in ('error', 'success'): sleep(0.5) def test_exists(self): self.assertTrue(qdb.processing_job.ProcessingJob.exists( "063e553b-327c-4818-ab4a-adfe58e49860")) self.assertTrue(qdb.processing_job.ProcessingJob.exists( "bcc7ebcd-39c1-43e4-af2d-822e3589f14d")) self.assertTrue(qdb.processing_job.ProcessingJob.exists( "b72369f9-a886-4193-8d3d-f7b504168e75")) self.assertTrue(qdb.processing_job.ProcessingJob.exists( "d19f76ee-274e-4c1b-b3a2-a12d73507c55")) self.assertFalse(qdb.processing_job.ProcessingJob.exists( "d19f76ee-274e-4c1b-b3a2-b12d73507c55")) self.assertFalse(qdb.processing_job.ProcessingJob.exists( "some-other-string")) def test_user(self): exp_user = qdb.user.User('test@foo.bar') self.assertEqual(self.tester1.user, exp_user) self.assertEqual(self.tester2.user, exp_user) exp_user = qdb.user.User('shared@foo.bar') self.assertEqual(self.tester3.user, exp_user) self.assertEqual(self.tester4.user, exp_user) def test_command(self): cmd1 = qdb.software.Command(1) cmd2 = qdb.software.Command(2) cmd3 = qdb.software.Command(3) self.assertEqual(self.tester1.command, cmd1) self.assertEqual(self.tester2.command, cmd2) self.assertEqual(self.tester3.command, cmd1) self.assertEqual(self.tester4.command, cmd3) def test_parameters(self): json_str = ( '{"max_bad_run_length":3,"min_per_read_length_fraction":0.75,' '"sequence_max_n":0,"rev_comp_barcode":false,' '"rev_comp_mapping_barcodes":false,"rev_comp":false,' '"phred_quality_threshold":3,"barcode_type":"golay_12",' '"max_barcode_errors":1.5,"input_data":1,"phred_offset":"auto"}') exp_params = qdb.software.Parameters.load(qdb.software.Command(1), json_str=json_str) self.assertEqual(self.tester1.parameters, exp_params) json_str = ( '{"min_seq_len":100,"max_seq_len":1000,"trim_seq_length":false,' '"min_qual_score":25,"max_ambig":6,"max_homopolymer":6,' '"max_primer_mismatch":0,"barcode_type":"golay_12",' '"max_barcode_errors":1.5,"disable_bc_correction":false,' '"qual_score_window":0,"disable_primers":false,' '"reverse_primers":"disable","reverse_primer_mismatches":0,' '"truncate_ambi_bases":false,"input_data":1}') exp_params = qdb.software.Parameters.load(qdb.software.Command(2), json_str=json_str) self.assertEqual(self.tester2.parameters, exp_params) json_str = ( '{"max_bad_run_length":3,"min_per_read_length_fraction":0.75,' '"sequence_max_n":0,"rev_comp_barcode":false,' '"rev_comp_mapping_barcodes":true,"rev_comp":false,' '"phred_quality_threshold":3,"barcode_type":"golay_12",' '"max_barcode_errors":1.5,"input_data":1,"phred_offset":"auto"}') exp_params = qdb.software.Parameters.load(qdb.software.Command(1), json_str=json_str) self.assertEqual(self.tester3.parameters, exp_params) json_str = ( '{"reference":1,"sortmerna_e_value":1,"sortmerna_max_pos":10000,' '"similarity":0.97,"sortmerna_coverage":0.97,"threads":1,' '"input_data":2}') exp_params = qdb.software.Parameters.load(qdb.software.Command(3), json_str=json_str) self.assertEqual(self.tester4.parameters, exp_params) def test_input_artifacts(self): exp = [qdb.artifact.Artifact(1)] self.assertEqual(self.tester1.input_artifacts, exp) self.assertEqual(self.tester2.input_artifacts, exp) self.assertEqual(self.tester3.input_artifacts, exp) exp = [qdb.artifact.Artifact(2)] self.assertEqual(self.tester4.input_artifacts, exp) def test_status(self): self.assertEqual(self.tester1.status, 'queued') self.assertEqual(self.tester2.status, 'running') self.assertEqual(self.tester3.status, 'success') self.assertEqual(self.tester4.status, 'error') def test_submit(self): # In order to test a success, we need to actually run the job, which # will mean to run split libraries, for example. # TODO: rewrite this test pass def test_log(self): self.assertIsNone(self.tester1.log) self.assertIsNone(self.tester2.log) self.assertIsNone(self.tester3.log) self.assertEqual(self.tester4.log, qdb.logger.LogEntry(1)) def test_heartbeat(self): self.assertIsNone(self.tester1.heartbeat) self.assertEqual(self.tester2.heartbeat, datetime(2015, 11, 22, 21, 00, 00)) self.assertEqual(self.tester3.heartbeat, datetime(2015, 11, 22, 21, 15, 00)) self.assertEqual(self.tester4.heartbeat, datetime(2015, 11, 22, 21, 30, 00)) def test_step(self): self.assertIsNone(self.tester1.step) self.assertEqual(self.tester2.step, 'demultiplexing') self.assertIsNone(self.tester3.step) self.assertEqual(self.tester4.step, 'generating demux file') def test_children(self): self.assertEqual(list(self.tester1.children), []) self.assertEqual(list(self.tester3.children), [self.tester4]) def test_update_and_launch_children(self): # In order to test a success, we need to actually run the children # jobs, which will mean to run split libraries, for example. pass def test_create(self): exp_command = qdb.software.Command(1) json_str = ( '{"input_data": 1, "max_barcode_errors": 1.5, ' '"barcode_type": "golay_12", "max_bad_run_length": 3, ' '"rev_comp": false, "phred_quality_threshold": 3, ' '"rev_comp_barcode": false, "rev_comp_mapping_barcodes": false, ' '"min_per_read_length_fraction": 0.75, "sequence_max_n": 0, ' '"phred_offset": "auto"}') exp_params = qdb.software.Parameters.load(exp_command, json_str=json_str) exp_user = qdb.user.User('test@foo.bar') obs = qdb.processing_job.ProcessingJob.create( exp_user, exp_params, True) self.assertEqual(obs.user, exp_user) self.assertEqual(obs.command, exp_command) self.assertEqual(obs.parameters, exp_params) self.assertEqual(obs.status, 'in_construction') self.assertEqual(obs.log, None) self.assertEqual(obs.heartbeat, None) self.assertEqual(obs.step, None) self.assertTrue(obs in qdb.artifact.Artifact(1).jobs()) # test with paramters with ' exp_command = qdb.software.Command(1) exp_params.values["a tests with '"] = 'this is a tests with "' exp_params.values['a tests with "'] = "this is a tests with '" obs = qdb.processing_job.ProcessingJob.create( exp_user, exp_params) self.assertEqual(obs.user, exp_user) self.assertEqual(obs.command, exp_command) self.assertEqual(obs.status, 'in_construction') self.assertEqual(obs.log, None) self.assertEqual(obs.heartbeat, None) self.assertEqual(obs.step, None) self.assertTrue(obs in qdb.artifact.Artifact(1).jobs()) def test_set_status(self): job = _create_job() self.assertEqual(job.status, 'in_construction') job._set_status('queued') self.assertEqual(job.status, 'queued') job._set_status('running') self.assertEqual(job.status, 'running') with self.assertRaises(qdb.exceptions.QiitaDBStatusError): job._set_status('queued') job._set_status('error') self.assertEqual(job.status, 'error') job._set_status('running') self.assertEqual(job.status, 'running') job._set_status('success') self.assertEqual(job.status, 'success') with self.assertRaises(qdb.exceptions.QiitaDBStatusError): job._set_status('running') def test_submit_error(self): job = _create_job() job._set_status('queued') with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): job.submit() def test_submit_environment(self): job = _create_job() software = job.command.software current = software.environment_script # temporal update and then rollback to not commit change with qdb.sql_connection.TRN: sql = """UPDATE qiita.software SET environment_script = %s WHERE software_id = %s""" qdb.sql_connection.TRN.add(sql, [ f'{current} ENVIRONMENT', software.id]) job.submit() self.assertEqual(job.status, 'error') qdb.sql_connection.TRN.rollback() def test_complete_multiple_outputs(self): # This test performs the test of multiple functions at the same # time. "release", "release_validators" and # "_set_validator_jobs" are tested here for correct execution. # Those functions are designed to work together, so it becomes # really hard to test each of the functions individually for # successfull execution. # We need to create a new command with multiple outputs, since # in the test DB there is no command with such characteristics cmd = qdb.software.Command.create( qdb.software.Software(1), "TestCommand", "Test command", {'input': ['artifact:["Demultiplexed"]', None]}, {'out1': 'BIOM', 'out2': 'BIOM'}) job = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), qdb.software.Parameters.load( cmd, values_dict={"input": 1})) job._set_status("running") fd, fp1 = mkstemp(suffix="_table.biom") self._clean_up_files.append(fp1) close(fd) with open(fp1, 'w') as f: f.write('\n') fd, fp2 = mkstemp(suffix="_table.biom") self._clean_up_files.append(fp2) close(fd) with open(fp2, 'w') as f: f.write('\n') # `job` has 2 output artifacts. Each of these artifacts needs to be # validated by 2 different validation jobs. We are creating those jobs # here, and add in the 'procenance' parameter that links the original # jobs with the validator jobs. params = qdb.software.Parameters.load( qdb.software.Command(4), values_dict={'template': 1, 'files': fp1, 'artifact_type': 'BIOM', 'provenance': dumps( {'job': job.id, 'cmd_out_id': qdb.util.convert_to_id( 'out1', "command_output", "name"), 'name': 'out1'})}) user = qdb.user.User('test@foo.bar') obs1 = qdb.processing_job.ProcessingJob.create(user, params, True) obs1._set_status('running') params = qdb.software.Parameters.load( qdb.software.Command(4), values_dict={'template': 1, 'files': fp2, 'artifact_type': 'BIOM', 'provenance': dumps( {'job': job.id, 'cmd_out_id': qdb.util.convert_to_id( 'out1', "command_output", "name"), 'name': 'out1'})}) obs2 = qdb.processing_job.ProcessingJob.create(user, params, True) obs2._set_status('running') # Make sure that we link the original job with its validator jobs job._set_validator_jobs([obs1, obs2]) artifact_data_1 = {'filepaths': [(fp1, 'biom')], 'artifact_type': 'BIOM'} # Complete one of the validator jobs. This jobs should store all the # information about the new artifact, but it does not create it. The # job then goes to a "waiting" state, where it waits until all the # validator jobs are completed. obs1._complete_artifact_definition(artifact_data_1) self.assertEqual(obs1.status, 'waiting') self.assertEqual(job.status, 'running') # When we complete the second validation job, the previous validation # job is realeaed from its waiting state. All jobs then create the # artifacts in a single transaction, so either all of them successfully # complete, or all of them fail. artifact_data_2 = {'filepaths': [(fp2, 'biom')], 'artifact_type': 'BIOM'} obs2._complete_artifact_definition(artifact_data_2) self.assertEqual(obs1.status, 'waiting') self.assertEqual(obs2.status, 'waiting') self.assertEqual(job.status, 'running') job.release_validators() self.assertEqual(obs1.status, 'success') self.assertEqual(obs2.status, 'success') self.assertEqual(job.status, 'success') def test_complete_artifact_definition(self): job = _create_job() job._set_status('running') fd, fp = mkstemp(suffix="_table.biom") self._clean_up_files.append(fp) close(fd) with open(fp, 'w') as f: f.write('\n') artifact_data = {'filepaths': [(fp, 'biom')], 'artifact_type': 'BIOM'} params = qdb.software.Parameters.load( qdb.software.Command(4), values_dict={'template': 1, 'files': fp, 'artifact_type': 'BIOM', 'provenance': dumps( {'job': job.id, 'cmd_out_id': 3})} ) obs = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), params) job._set_validator_jobs([obs]) obs._complete_artifact_definition(artifact_data) self.assertEqual(obs.status, 'waiting') self.assertEqual(job.status, 'running') # Upload case implicitly tested by "test_complete_type" def test_complete_artifact_transformation(self): # Implicitly tested by "test_complete" pass def test_complete_no_artifact_data(self): job = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), qdb.software.Parameters.load( qdb.software.Command(5), values_dict={"input_data": 1})) job._set_status('running') job.complete(True) self.assertEqual(job.status, 'success') job = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), qdb.software.Parameters.load( qdb.software.Command(5), values_dict={"input_data": 1}), True) job._set_status('running') job.complete(False, error='Some Error') self.assertEqual(job.status, 'error') def test_complete_type(self): fd, fp = mkstemp(suffix="_table.biom") self._clean_up_files.append(fp) close(fd) with open(fp, 'w') as f: f.write('\n') exp_artifact_count = qdb.util.get_count('qiita.artifact') + 1 artifacts_data = {'ignored': {'filepaths': [(fp, 'biom')], 'artifact_type': 'BIOM'}} metadata_dict = { 'SKB8.640193': {'center_name': 'ANL', 'primer': 'GTGCCAGCMGCCGCGGTAA', 'barcode': 'GTCCGCAAGTTA', 'run_prefix': "s_G1_L001_sequences", 'platform': 'Illumina', 'instrument_model': 'Illumina MiSeq', 'library_construction_protocol': 'AAAA', 'experiment_design_description': 'BBBB'}} metadata = pd.DataFrame.from_dict(metadata_dict, orient='index', dtype=str) pt = qdb.metadata_template.prep_template.PrepTemplate.create( metadata, qdb.study.Study(1), "16S") self._clean_up_files.extend([ptfp for _, ptfp in pt.get_filepaths()]) params = qdb.software.Parameters.load( qdb.software.Command(4), values_dict={'template': pt.id, 'files': fp, 'artifact_type': 'BIOM'}) obs = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), params, True) obs._set_status('running') obs.complete(True, artifacts_data=artifacts_data) self.assertEqual(obs.status, 'success') self.assertEqual(qdb.util.get_count('qiita.artifact'), exp_artifact_count) self._clean_up_files.extend( [x['fp'] for x in qdb.artifact.Artifact(exp_artifact_count).filepaths]) def test_complete_success(self): # Note that here we are submitting and creating other multiple jobs; # thus here is the best place to test any intermediary steps/functions # of the job creation, submission, exectution, and completion. # # This first part of the test is just to test that by default the # naming of the output artifact will be the name of the output fd, fp = mkstemp(suffix='_table.biom') self._clean_up_files.append(fp) close(fd) with open(fp, 'w') as f: f.write('\n') artifacts_data = {'demultiplexed': {'filepaths': [(fp, 'biom')], 'artifact_type': 'BIOM'}} job = _create_job() job._set_status('running') # here we can test that job.release_validator_job hasn't been created # yet so it has to be None self.assertIsNone(job.release_validator_job) job.complete(True, artifacts_data=artifacts_data) self._wait_for_job(job) # let's check for the job that released the validators self.assertIsNotNone(job.release_validator_job) self.assertEqual(job.release_validator_job.parameters.values['job'], job.id) # Retrieve the job that is performing the validation: validators = list(job.validator_jobs) self.assertEqual(len(validators), 1) # the validator actually runs on the system so it gets an external_id # assigned, let's test that is not None self.assertFalse(validators[0].external_id == 'Not Available') # Test the output artifact is going to be named based on the # input parameters self.assertEqual( loads(validators[0].parameters.values['provenance'])['name'], "demultiplexed") # To test that the naming of the output artifact is based on the # parameters that the command is indicating, we need to update the # parameter information of the command - since the ones existing # in the database currently do not require using any input parameter # to name the output artifact with qdb.sql_connection.TRN: sql = """UPDATE qiita.command_parameter SET name_order = %s WHERE command_parameter_id = %s""" # Hard-coded values; 19 -> barcode_type, 20 -> max_barcode_errors qdb.sql_connection.TRN.add(sql, [[1, 19], [2, 20]], many=True) qdb.sql_connection.TRN.execute() fd, fp = mkstemp(suffix='_table.biom') self._clean_up_files.append(fp) close(fd) with open(fp, 'w') as f: f.write('\n') artifacts_data = {'demultiplexed': {'filepaths': [(fp, 'biom')], 'artifact_type': 'BIOM'}} job = _create_job() job._set_status('running') alljobs = set(self._get_all_job_ids()) job.complete(True, artifacts_data=artifacts_data) # When completing the previous job, it creates a new job that needs # to validate the BIOM table that is being added as new artifact. # Hence, this job is still in running state until the validation job # is completed. Note that this is tested by making sure that the status # of this job is running, and that we have one more job than before # (see assertEqual with len of all jobs) self.assertEqual(job.status, 'running') self.assertTrue(job.step.startswith( 'Validating outputs (1 remaining) via job(s)')) obsjobs = set(self._get_all_job_ids()) # The complete call above submits 2 new jobs: the validator job and # the release validators job. Hence the +2 self.assertEqual(len(obsjobs), len(alljobs) + 2) self._wait_for_job(job) # Retrieve the job that is performing the validation: validators = list(job.validator_jobs) self.assertEqual(len(validators), 1) # here we can test that the validator shape and allocation is correct validator = validators[0] self.assertEqual(validator.parameters.values['artifact_type'], 'BIOM') self.assertEqual(validator.get_resource_allocation_info(), '-q qiita ' '-l nodes=1:ppn=1 -l mem=90gb -l walltime=150:00:00') self.assertEqual(validator.shape, (27, 31, None)) # Test the output artifact is going to be named based on the # input parameters self.assertEqual( loads(validator.parameters.values['provenance'])['name'], "demultiplexed golay_12 1.5") def test_complete_failure(self): job = _create_job() job.complete(False, error="Job failure") self.assertEqual(job.status, 'error') self.assertEqual(job.log, qdb.logger.LogEntry.newest_records(numrecords=1)[0]) self.assertEqual(job.log.msg, 'Job failure') # Test the artifact definition case job = _create_job() job._set_status('running') params = qdb.software.Parameters.load( qdb.software.Command(4), values_dict={'template': 1, 'files': 'ignored', 'artifact_type': 'BIOM', 'provenance': dumps( {'job': job.id, 'cmd_out_id': 3})} ) obs = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), params, True) job._set_validator_jobs([obs]) obs.complete(False, error="Validation failure") self.assertEqual(obs.status, 'error') self.assertEqual(obs.log.msg, 'Validation failure') self.assertEqual(job.status, 'running') job.release_validators() self.assertEqual(job.status, 'error') self.assertEqual( job.log.msg, '1 validator jobs failed: Validator %s ' 'error message: Validation failure' % obs.id) def test_complete_error(self): with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): self.tester1.complete(True, artifacts_data={}) def test_set_error(self): job1 = _create_job() job1._set_status('queued') job2 = _create_job() job2._set_status('running') for t in [job1, job2]: t._set_error('Job failure') self.assertEqual(t.status, 'error') self.assertEqual( t.log, qdb.logger.LogEntry.newest_records(numrecords=1)[0]) with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): self.tester3._set_error("Job failure") def test_update_heartbeat_state(self): job = _create_job() job._set_status('running') before = datetime.now() job.update_heartbeat_state() self.assertTrue(before < job.heartbeat < datetime.now()) job = _create_job() job._set_status('queued') before = datetime.now() job.update_heartbeat_state() self.assertTrue(before < job.heartbeat < datetime.now()) self.assertEqual(job.status, 'running') with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): self.tester3.update_heartbeat_state() def test_step_setter(self): job = _create_job() job._set_status('running') job.step = 'demultiplexing' self.assertEqual(job.step, 'demultiplexing') job.step = 'generating demux file' self.assertEqual(job.step, 'generating demux file') with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): self.tester1.step = 'demultiplexing' with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): self.tester3.step = 'demultiplexing' with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): self.tester4.step = 'demultiplexing' def test_update_children(self): # Create a workflow so we can test this functionality exp_command = qdb.software.Command(1) json_str = ( '{"input_data": 1, "max_barcode_errors": 1.5, ' '"barcode_type": "golay_12", "max_bad_run_length": 3, ' '"rev_comp": false, "phred_quality_threshold": 3, ' '"rev_comp_barcode": false, "rev_comp_mapping_barcodes": false, ' '"min_per_read_length_fraction": 0.75, "sequence_max_n": 0, ' '"phred_offset": "auto"}') exp_params = qdb.software.Parameters.load(exp_command, json_str=json_str) exp_user = qdb.user.User('test@foo.bar') name = "Test processing workflow" tester = qdb.processing_job.ProcessingWorkflow.from_scratch( exp_user, exp_params, name=name, force=True) parent = list(tester.graph.nodes())[0] connections = {parent: {'demultiplexed': 'input_data'}} dflt_params = qdb.software.DefaultParameters(10) tester.add(dflt_params, connections=connections) # we could get the child using tester.graph.nodes()[1] but networkx # doesn't assure order so using the actual graph to get the child child = list(nx.topological_sort(tester.graph))[1] mapping = {1: 3} obs = parent._update_children(mapping) exp = [child] self.assertTrue(obs, exp) self.assertEqual(child.input_artifacts, [qdb.artifact.Artifact(3)]) def test_outputs(self): job = _create_job() job._set_status('running') QE = qdb.exceptions with self.assertRaises(QE.QiitaDBOperationNotPermittedError): job.outputs fd, fp = mkstemp(suffix="_table.biom") self._clean_up_files.append(fp) close(fd) with open(fp, 'w') as f: f.write('\n') artifact_data = {'filepaths': [(fp, 'biom')], 'artifact_type': 'BIOM'} params = qdb.software.Parameters.load( qdb.software.Command(4), values_dict={'template': 1, 'files': fp, 'artifact_type': 'BIOM', 'provenance': dumps( {'job': job.id, 'cmd_out_id': 3, 'name': 'outArtifact'})} ) obs = qdb.processing_job.ProcessingJob.create( qdb.user.User('test@foo.bar'), params, True) job._set_validator_jobs([obs]) exp_artifact_count = qdb.util.get_count('qiita.artifact') + 1 obs._complete_artifact_definition(artifact_data) job.release_validators() self.assertEqual(job.status, 'success') artifact = qdb.artifact.Artifact(exp_artifact_count) obs = job.outputs self.assertEqual(obs, {'OTU table': artifact}) self._clean_up_files.extend([x['fp'] for x in artifact.filepaths]) self.assertEqual(artifact.name, 'outArtifact') def test_processing_job_workflow(self): # testing None job = qdb.processing_job.ProcessingJob( "063e553b-327c-4818-ab4a-adfe58e49860") self.assertIsNone(job.processing_job_workflow) # testing actual workflow job = qdb.processing_job.ProcessingJob( "b72369f9-a886-4193-8d3d-f7b504168e75") self.assertEqual(job.processing_job_workflow, qdb.processing_job.ProcessingWorkflow(1)) # testing child job from workflow job = qdb.processing_job.ProcessingJob( 'd19f76ee-274e-4c1b-b3a2-a12d73507c55') self.assertEqual(job.processing_job_workflow, qdb.processing_job.ProcessingWorkflow(1)) def test_hidden(self): self.assertTrue(self.tester1.hidden) self.assertTrue(self.tester2.hidden) self.assertFalse(self.tester3.hidden) self.assertTrue(self.tester4.hidden) def test_hide(self): QE = qdb.exceptions # It's in a queued state with self.assertRaises(QE.QiitaDBOperationNotPermittedError): self.tester1.hide() # It's in a running state with self.assertRaises(QE.QiitaDBOperationNotPermittedError): self.tester2.hide() # It's in a success state with self.assertRaises(QE.QiitaDBOperationNotPermittedError): self.tester3.hide() job = _create_job() job._set_error('Setting to error for testing') self.assertFalse(job.hidden) job.hide() self.assertTrue(job.hidden) def test_shape(self): jids = { # Split libraries FASTQ '6d368e16-2242-4cf8-87b4-a5dc40bb890b': (27, 31, 116), # Pick closed-reference OTUs '80bf25f3-5f1d-4e10-9369-315e4244f6d5': (27, 31, 0), # Single Rarefaction / Analysis '8a7a8461-e8a1-4b4e-a428-1bc2f4d3ebd0': (5, 56, 3770436), # Split libraries 'bcc7ebcd-39c1-43e4-af2d-822e3589f14d': (27, 31, 116)} for jid, shape in jids.items(): job = qdb.processing_job.ProcessingJob(jid) self.assertEqual(job.shape, shape) def test_get_resource_allocation_info(self): jids = { # Split libraries FASTQ '6d368e16-2242-4cf8-87b4-a5dc40bb890b': '-q qiita -l nodes=1:ppn=1 -l mem=120gb -l walltime=80:00:00', # Pick closed-reference OTUs '80bf25f3-5f1d-4e10-9369-315e4244f6d5': '-q qiita -l nodes=1:ppn=5 -l mem=120gb -l walltime=130:00:00', # Single Rarefaction / Analysis '8a7a8461-e8a1-4b4e-a428-1bc2f4d3ebd0': '-q qiita -l nodes=1:ppn=5 -l pmem=8gb -l walltime=168:00:00', # Split libraries 'bcc7ebcd-39c1-43e4-af2d-822e3589f14d': '-q qiita -l nodes=1:ppn=1 -l mem=60gb -l walltime=25:00:00'} for jid, allocation in jids.items(): job = qdb.processing_job.ProcessingJob(jid) self.assertEqual(job.get_resource_allocation_info(), allocation) # now let's test get_resource_allocation_info formulas, fun!! job_changed = qdb.processing_job.ProcessingJob( '6d368e16-2242-4cf8-87b4-a5dc40bb890b') job_not_changed = qdb.processing_job.ProcessingJob( '80bf25f3-5f1d-4e10-9369-315e4244f6d5') # helper to set memory allocations easier def _set_allocation(memory): sql = """UPDATE qiita.processing_job_resource_allocation SET allocation = '{0}' WHERE name = 'Split libraries FASTQ'""".format( '-q qiita -l mem=%s' % memory) qdb.sql_connection.perform_as_transaction(sql) # let's start with something simple, samples*1000 # 27*1000 ~ 27000 _set_allocation('{samples}*1000') self.assertEqual( job_not_changed.get_resource_allocation_info(), '-q qiita -l nodes=1:ppn=5 -l mem=120gb -l walltime=130:00:00') self.assertEqual(job_changed.get_resource_allocation_info(), '-q qiita -l mem=26K') # a little more complex ((samples+columns)*1000000)+4000000 # (( 27 + 31 )*1000000)+4000000 ~ 62000000 _set_allocation('(({samples}+{columns})*1000000)+4000000') self.assertEqual( job_not_changed.get_resource_allocation_info(), '-q qiita -l nodes=1:ppn=5 -l mem=120gb -l walltime=130:00:00') self.assertEqual(job_changed.get_resource_allocation_info(), '-q qiita -l mem=59M') # now something real input_size+(2*1e+9) # 116 +(2*1e+9) ~ 2000000116 _set_allocation('{input_size}+(2*1e+9)') self.assertEqual( job_not_changed.get_resource_allocation_info(), '-q qiita -l nodes=1:ppn=5 -l mem=120gb -l walltime=130:00:00') self.assertEqual(job_changed.get_resource_allocation_info(), '-q qiita -l mem=2G') @qiita_test_checker() class ProcessingWorkflowTests(TestCase): def test_name(self): self.assertEqual(qdb.processing_job.ProcessingWorkflow(1).name, 'Testing processing workflow') def test_user(self): self.assertEqual(qdb.processing_job.ProcessingWorkflow(1).user, qdb.user.User('shared@foo.bar')) def test_graph(self): obs = qdb.processing_job.ProcessingWorkflow(1).graph self.assertTrue(isinstance(obs, nx.DiGraph)) exp_nodes = [ qdb.processing_job.ProcessingJob( 'b72369f9-a886-4193-8d3d-f7b504168e75'), qdb.processing_job.ProcessingJob( 'd19f76ee-274e-4c1b-b3a2-a12d73507c55')] self.assertCountEqual(obs.nodes(), exp_nodes) self.assertEqual(list(obs.edges()), [(exp_nodes[0], exp_nodes[1])]) def test_graph_only_root(self): obs = qdb.processing_job.ProcessingWorkflow(2).graph self.assertTrue(isinstance(obs, nx.DiGraph)) exp_nodes = [ qdb.processing_job.ProcessingJob( 'ac653cb5-76a6-4a45-929e-eb9b2dee6b63')] self.assertCountEqual(obs.nodes(), exp_nodes) self.assertEqual(list(obs.edges()), []) def test_raise_if_not_in_construction(self): # We just need to test that the execution continues (i.e. no raise) tester = qdb.processing_job.ProcessingWorkflow(2) tester._raise_if_not_in_construction() def test_raise_if_not_in_construction_error(self): tester = qdb.processing_job.ProcessingWorkflow(1) with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): tester._raise_if_not_in_construction() def test_submit(self): # The submit method is being tested in test_complete_success via # a job, its release validators and validators submissions. # Leaving this note here in case it's helpful for future development pass def test_from_default_workflow(self): exp_user = qdb.user.User('test@foo.bar') dflt_wf = qdb.software.DefaultWorkflow(1) req_params = {qdb.software.Command(1): {'input_data': 1}} name = "Test processing workflow" obs = qdb.processing_job.ProcessingWorkflow.from_default_workflow( exp_user, dflt_wf, req_params, name=name, force=True) self.assertEqual(obs.name, name) self.assertEqual(obs.user, exp_user) obs_graph = obs.graph self.assertTrue(isinstance(obs_graph, nx.DiGraph)) self.assertEqual(len(obs_graph.nodes()), 2) obs_edges = obs_graph.edges() self.assertEqual(len(obs_edges), 1) obs_edges = list(obs_edges)[0] obs_src, obs_dst = list(obs_edges) self.assertTrue(isinstance(obs_src, qdb.processing_job.ProcessingJob)) self.assertTrue(isinstance(obs_dst, qdb.processing_job.ProcessingJob)) self.assertTrue(obs_src.command, qdb.software.Command(1)) self.assertTrue(obs_dst.command, qdb.software.Command(1)) obs_params = obs_dst.parameters.values exp_params = { 'input_data': [obs_src.id, u'demultiplexed'], 'reference': 1, 'similarity': 0.97, 'sortmerna_coverage': 0.97, 'sortmerna_e_value': 1, 'sortmerna_max_pos': 10000, 'threads': 1} self.assertEqual(obs_params, exp_params) exp_pending = {obs_src.id: {'input_data': 'demultiplexed'}} self.assertEqual(obs_dst.pending, exp_pending) def test_from_default_workflow_error(self): with self.assertRaises(qdb.exceptions.QiitaDBError) as err: qdb.processing_job.ProcessingWorkflow.from_default_workflow( qdb.user.User('test@foo.bar'), qdb.software.DefaultWorkflow(1), {}, name="Test name") exp = ('Provided required parameters do not match the initial set of ' 'commands for the workflow. Command(s) "Split libraries FASTQ"' ' are missing the required parameter set.') self.assertEqual(str(err.exception), exp) req_params = {qdb.software.Command(1): {'input_data': 1}, qdb.software.Command(2): {'input_data': 2}} with self.assertRaises(qdb.exceptions.QiitaDBError) as err: qdb.processing_job.ProcessingWorkflow.from_default_workflow( qdb.user.User('test@foo.bar'), qdb.software.DefaultWorkflow(1), req_params, name="Test name") exp = ('Provided required parameters do not match the initial set of ' 'commands for the workflow. Paramters for command(s) ' '"Split libraries" have been provided, but they are not the ' 'initial commands for the workflow.') self.assertEqual(str(err.exception), exp) def test_from_scratch(self): exp_command = qdb.software.Command(1) json_str = ( '{"input_data": 1, "max_barcode_errors": 1.5, ' '"barcode_type": "golay_12", "max_bad_run_length": 3, ' '"rev_comp": false, "phred_quality_threshold": 3, ' '"rev_comp_barcode": false, "rev_comp_mapping_barcodes": false, ' '"min_per_read_length_fraction": 0.75, "sequence_max_n": 0, ' '"phred_offset": "auto"}') exp_params = qdb.software.Parameters.load(exp_command, json_str=json_str) exp_user = qdb.user.User('test@foo.bar') name = "Test processing workflow" obs = qdb.processing_job.ProcessingWorkflow.from_scratch( exp_user, exp_params, name=name, force=True) self.assertEqual(obs.name, name) self.assertEqual(obs.user, exp_user) obs_graph = obs.graph self.assertTrue(isinstance(obs_graph, nx.DiGraph)) nodes = obs_graph.nodes() self.assertEqual(len(nodes), 1) self.assertEqual(list(nodes)[0].parameters, exp_params) self.assertEqual(list(obs_graph.edges()), []) def test_add(self): exp_command = qdb.software.Command(1) json_str = ( '{"input_data": 1, "max_barcode_errors": 1.5, ' '"barcode_type": "golay_12", "max_bad_run_length": 3, ' '"rev_comp": false, "phred_quality_threshold": 3, ' '"rev_comp_barcode": false, "rev_comp_mapping_barcodes": false, ' '"min_per_read_length_fraction": 0.75, "sequence_max_n": 0, ' '"phred_offset": "auto"}') exp_params = qdb.software.Parameters.load(exp_command, json_str=json_str) exp_user = qdb.user.User('test@foo.bar') name = "Test processing workflow" obs = qdb.processing_job.ProcessingWorkflow.from_scratch( exp_user, exp_params, name=name, force=True) parent = list(obs.graph.nodes())[0] connections = {parent: {'demultiplexed': 'input_data'}} dflt_params = qdb.software.DefaultParameters(10) obs.add(dflt_params, connections=connections, force=True) obs_graph = obs.graph self.assertTrue(isinstance(obs_graph, nx.DiGraph)) obs_nodes = obs_graph.nodes() self.assertEqual(len(obs_nodes), 2) obs_edges = obs_graph.edges() self.assertEqual(len(obs_edges), 1) obs_edges = list(obs_edges)[0] obs_src, obs_dst = list(obs_edges) self.assertEqual(obs_src, parent) self.assertTrue(isinstance(obs_dst, qdb.processing_job.ProcessingJob)) obs_params = obs_dst.parameters.values exp_params = { 'input_data': [parent.id, u'demultiplexed'], 'reference': 1, 'similarity': 0.97, 'sortmerna_coverage': 0.97, 'sortmerna_e_value': 1, 'sortmerna_max_pos': 10000, 'threads': 1} self.assertEqual(obs_params, exp_params) # Adding a new root job # This also tests that the `graph` property returns the graph correctly # when there are root nodes that don't have any children dflt_params = qdb.software.DefaultParameters(1) obs.add(dflt_params, req_params={'input_data': 1}, force=True) obs_graph = obs.graph self.assertTrue(isinstance(obs_graph, nx.DiGraph)) root_obs_nodes = obs_graph.nodes() self.assertEqual(len(root_obs_nodes), 3) obs_edges = obs_graph.edges() self.assertEqual(len(obs_edges), 1) obs_new_jobs = set(root_obs_nodes) - set(obs_nodes) self.assertEqual(len(obs_new_jobs), 1) obs_job = obs_new_jobs.pop() exp_params = {'barcode_type': u'golay_12', 'input_data': 1, 'max_bad_run_length': 3, 'max_barcode_errors': 1.5, 'min_per_read_length_fraction': 0.75, 'phred_quality_threshold': 3, 'rev_comp': False, 'rev_comp_barcode': False, 'rev_comp_mapping_barcodes': False, 'sequence_max_n': 0, 'phred_offset': 'auto'} self.assertEqual(obs_job.parameters.values, exp_params) def test_add_error(self): with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): qdb.processing_job.ProcessingWorkflow(1).add({}, None) def test_remove(self): exp_command = qdb.software.Command(1) json_str = ( '{"input_data": 1, "max_barcode_errors": 1.5, ' '"barcode_type": "golay_12", "max_bad_run_length": 3, ' '"rev_comp": false, "phred_quality_threshold": 3, ' '"rev_comp_barcode": false, "rev_comp_mapping_barcodes": false, ' '"min_per_read_length_fraction": 0.75, "sequence_max_n": 0,' '"phred_offset": "auto"}') exp_params = qdb.software.Parameters.load(exp_command, json_str=json_str) exp_user = qdb.user.User('test@foo.bar') name = "Test processing workflow" tester = qdb.processing_job.ProcessingWorkflow.from_scratch( exp_user, exp_params, name=name, force=True) parent = list(tester.graph.nodes())[0] connections = {parent: {'demultiplexed': 'input_data'}} dflt_params = qdb.software.DefaultParameters(10) tester.add(dflt_params, connections=connections) self.assertEqual(len(tester.graph.nodes()), 2) element = list(tester.graph.edges())[0] tester.remove(element[1]) g = tester.graph obs_nodes = g.nodes() self.assertEqual(len(obs_nodes), 1) self.assertEqual(list(obs_nodes)[0], parent) self.assertEqual(list(g.edges()), []) # Test with cascade = true exp_user = qdb.user.User('test@foo.bar') dflt_wf = qdb.software.DefaultWorkflow(1) req_params = {qdb.software.Command(1): {'input_data': 1}} name = "Test processing workflow" tester = qdb.processing_job.ProcessingWorkflow.from_default_workflow( exp_user, dflt_wf, req_params, name=name, force=True) element = list(tester.graph.edges())[0] tester.remove(element[0], cascade=True) self.assertEqual(list(tester.graph.nodes()), []) def test_remove_error(self): with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): qdb.processing_job.ProcessingWorkflow(1).remove( qdb.processing_job.ProcessingJob( 'b72369f9-a886-4193-8d3d-f7b504168e75')) exp_user = qdb.user.User('test@foo.bar') dflt_wf = qdb.software.DefaultWorkflow(1) req_params = {qdb.software.Command(1): {'input_data': 1}} name = "Test processing workflow" tester = qdb.processing_job.ProcessingWorkflow.from_default_workflow( exp_user, dflt_wf, req_params, name=name, force=True) with self.assertRaises( qdb.exceptions.QiitaDBOperationNotPermittedError): element = list(tester.graph.edges())[0] tester.remove(element[0]) @qiita_test_checker() class ProcessingJobDuplicated(TestCase): def test_create_duplicated(self): job = _create_job() job._set_status('success') with self.assertRaisesRegex(ValueError, 'Cannot create job because ' 'the parameters are the same as jobs ' 'that are queued, running or already ' 'have succeeded:') as context: _create_job(False) # If it failed it's because we have jobs in non finished status so # setting them as error. This is basically testing that the duplicated # job creation allows to create if all jobs are error and if success # that the job doesn't have children for jobs in str(context.exception).split('\n')[1:]: jid, status = jobs.split(': ') if status != 'success': qdb.processing_job.ProcessingJob(jid)._set_status('error') _create_job(False) if __name__ == '__main__': main()
bsd-3-clause
joshbohde/scikit-learn
examples/plot_lda_vs_qda.py
2
2680
""" ==================================================================== Linear and Quadratic Discriminant Analysis with confidence ellipsoid ==================================================================== Plot the confidence ellipsoids of each class and decision boundary """ print __doc__ from scipy import linalg import numpy as np import pylab as pl import matplotlib as mpl from sklearn.lda import LDA from sklearn.qda import QDA ################################################################################ # load sample dataset from sklearn.datasets import load_iris iris = load_iris() X = iris.data[:,:2] # Take only 2 dimensions y = iris.target X = X[y > 0] y = y[y > 0] y -= 1 target_names = iris.target_names[1:] ################################################################################ # LDA lda = LDA() y_pred = lda.fit(X, y, store_covariance=True).predict(X) # QDA qda = QDA() y_pred = qda.fit(X, y, store_covariances=True).predict(X) ############################################################################### # Plot results def plot_ellipse(splot, mean, cov, color): v, w = linalg.eigh(cov) u = w[0] / linalg.norm(w[0]) angle = np.arctan(u[1]/u[0]) angle = 180 * angle / np.pi # convert to degrees # filled gaussian at 2 standard deviation ell = mpl.patches.Ellipse(mean, 2 * v[0] ** 0.5, 2 * v[1] ** 0.5, 180 + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(0.5) splot.add_artist(ell) xx, yy = np.meshgrid(np.linspace(4, 8.5, 200), np.linspace(1.5, 4.5, 200)) X_grid = np.c_[xx.ravel(), yy.ravel()] zz_lda = lda.predict_proba(X_grid)[:,1].reshape(xx.shape) zz_qda = qda.predict_proba(X_grid)[:,1].reshape(xx.shape) pl.figure() splot = pl.subplot(1, 2, 1) pl.contourf(xx, yy, zz_lda > 0.5, alpha=0.5) pl.scatter(X[y==0,0], X[y==0,1], c='b', label=target_names[0]) pl.scatter(X[y==1,0], X[y==1,1], c='r', label=target_names[1]) pl.contour(xx, yy, zz_lda, [0.5], linewidths=2., colors='k') plot_ellipse(splot, lda.means_[0], lda.covariance_, 'b') plot_ellipse(splot, lda.means_[1], lda.covariance_, 'r') pl.legend() pl.axis('tight') pl.title('Linear Discriminant Analysis') splot = pl.subplot(1, 2, 2) pl.contourf(xx, yy, zz_qda > 0.5, alpha=0.5) pl.scatter(X[y==0,0], X[y==0,1], c='b', label=target_names[0]) pl.scatter(X[y==1,0], X[y==1,1], c='r', label=target_names[1]) pl.contour(xx, yy, zz_qda, [0.5], linewidths=2., colors='k') plot_ellipse(splot, qda.means_[0], qda.covariances_[0], 'b') plot_ellipse(splot, qda.means_[1], qda.covariances_[1], 'r') pl.legend() pl.axis('tight') pl.title('Quadratic Discriminant Analysis') pl.show()
bsd-3-clause
aflaxman/scikit-learn
examples/hetero_feature_union.py
81
6241
""" ============================================= Feature Union with Heterogeneous Data Sources ============================================= Datasets can often contain components of that require different feature extraction and processing pipelines. This scenario might occur when: 1. Your dataset consists of heterogeneous data types (e.g. raster images and text captions) 2. Your dataset is stored in a Pandas DataFrame and different columns require different processing pipelines. This example demonstrates how to use :class:`sklearn.feature_extraction.FeatureUnion` on a dataset containing different types of features. We use the 20-newsgroups dataset and compute standard bag-of-words features for the subject line and body in separate pipelines as well as ad hoc features on the body. We combine them (with weights) using a FeatureUnion and finally train a classifier on the combined set of features. The choice of features is not particularly helpful, but serves to illustrate the technique. """ # Author: Matt Terry <matt.terry@gmail.com> # # License: BSD 3 clause from __future__ import print_function import numpy as np from sklearn.base import BaseEstimator, TransformerMixin from sklearn.datasets import fetch_20newsgroups from sklearn.datasets.twenty_newsgroups import strip_newsgroup_footer from sklearn.datasets.twenty_newsgroups import strip_newsgroup_quoting from sklearn.decomposition import TruncatedSVD from sklearn.feature_extraction import DictVectorizer from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics import classification_report from sklearn.pipeline import FeatureUnion from sklearn.pipeline import Pipeline from sklearn.svm import SVC class ItemSelector(BaseEstimator, TransformerMixin): """For data grouped by feature, select subset of data at a provided key. The data is expected to be stored in a 2D data structure, where the first index is over features and the second is over samples. i.e. >> len(data[key]) == n_samples Please note that this is the opposite convention to scikit-learn feature matrixes (where the first index corresponds to sample). ItemSelector only requires that the collection implement getitem (data[key]). Examples include: a dict of lists, 2D numpy array, Pandas DataFrame, numpy record array, etc. >> data = {'a': [1, 5, 2, 5, 2, 8], 'b': [9, 4, 1, 4, 1, 3]} >> ds = ItemSelector(key='a') >> data['a'] == ds.transform(data) ItemSelector is not designed to handle data grouped by sample. (e.g. a list of dicts). If your data is structured this way, consider a transformer along the lines of `sklearn.feature_extraction.DictVectorizer`. Parameters ---------- key : hashable, required The key corresponding to the desired value in a mappable. """ def __init__(self, key): self.key = key def fit(self, x, y=None): return self def transform(self, data_dict): return data_dict[self.key] class TextStats(BaseEstimator, TransformerMixin): """Extract features from each document for DictVectorizer""" def fit(self, x, y=None): return self def transform(self, posts): return [{'length': len(text), 'num_sentences': text.count('.')} for text in posts] class SubjectBodyExtractor(BaseEstimator, TransformerMixin): """Extract the subject & body from a usenet post in a single pass. Takes a sequence of strings and produces a dict of sequences. Keys are `subject` and `body`. """ def fit(self, x, y=None): return self def transform(self, posts): features = np.recarray(shape=(len(posts),), dtype=[('subject', object), ('body', object)]) for i, text in enumerate(posts): headers, _, bod = text.partition('\n\n') bod = strip_newsgroup_footer(bod) bod = strip_newsgroup_quoting(bod) features['body'][i] = bod prefix = 'Subject:' sub = '' for line in headers.split('\n'): if line.startswith(prefix): sub = line[len(prefix):] break features['subject'][i] = sub return features pipeline = Pipeline([ # Extract the subject & body ('subjectbody', SubjectBodyExtractor()), # Use FeatureUnion to combine the features from subject and body ('union', FeatureUnion( transformer_list=[ # Pipeline for pulling features from the post's subject line ('subject', Pipeline([ ('selector', ItemSelector(key='subject')), ('tfidf', TfidfVectorizer(min_df=50)), ])), # Pipeline for standard bag-of-words model for body ('body_bow', Pipeline([ ('selector', ItemSelector(key='body')), ('tfidf', TfidfVectorizer()), ('best', TruncatedSVD(n_components=50)), ])), # Pipeline for pulling ad hoc features from post's body ('body_stats', Pipeline([ ('selector', ItemSelector(key='body')), ('stats', TextStats()), # returns a list of dicts ('vect', DictVectorizer()), # list of dicts -> feature matrix ])), ], # weight components in FeatureUnion transformer_weights={ 'subject': 0.8, 'body_bow': 0.5, 'body_stats': 1.0, }, )), # Use a SVC classifier on the combined features ('svc', SVC(kernel='linear')), ]) # limit the list of categories to make running this example faster. categories = ['alt.atheism', 'talk.religion.misc'] train = fetch_20newsgroups(random_state=1, subset='train', categories=categories, ) test = fetch_20newsgroups(random_state=1, subset='test', categories=categories, ) pipeline.fit(train.data, train.target) y = pipeline.predict(test.data) print(classification_report(y, test.target))
bsd-3-clause
theoryno3/scikit-learn
sklearn/manifold/tests/test_isomap.py
28
4007
from itertools import product import numpy as np from numpy.testing import assert_almost_equal, assert_array_almost_equal from sklearn import datasets from sklearn import manifold from sklearn import neighbors from sklearn import pipeline from sklearn import preprocessing from sklearn.utils.testing import assert_less eigen_solvers = ['auto', 'dense', 'arpack'] path_methods = ['auto', 'FW', 'D'] def test_isomap_simple_grid(): # Isomap should preserve distances when all neighbors are used N_per_side = 5 Npts = N_per_side ** 2 n_neighbors = Npts - 1 # grid of equidistant points in 2D, n_components = n_dim X = np.array(list(product(range(N_per_side), repeat=2))) # distances from each point to all others G = neighbors.kneighbors_graph(X, n_neighbors, mode='distance').toarray() for eigen_solver in eigen_solvers: for path_method in path_methods: clf = manifold.Isomap(n_neighbors=n_neighbors, n_components=2, eigen_solver=eigen_solver, path_method=path_method) clf.fit(X) G_iso = neighbors.kneighbors_graph(clf.embedding_, n_neighbors, mode='distance').toarray() assert_array_almost_equal(G, G_iso) def test_isomap_reconstruction_error(): # Same setup as in test_isomap_simple_grid, with an added dimension N_per_side = 5 Npts = N_per_side ** 2 n_neighbors = Npts - 1 # grid of equidistant points in 2D, n_components = n_dim X = np.array(list(product(range(N_per_side), repeat=2))) # add noise in a third dimension rng = np.random.RandomState(0) noise = 0.1 * rng.randn(Npts, 1) X = np.concatenate((X, noise), 1) # compute input kernel G = neighbors.kneighbors_graph(X, n_neighbors, mode='distance').toarray() centerer = preprocessing.KernelCenterer() K = centerer.fit_transform(-0.5 * G ** 2) for eigen_solver in eigen_solvers: for path_method in path_methods: clf = manifold.Isomap(n_neighbors=n_neighbors, n_components=2, eigen_solver=eigen_solver, path_method=path_method) clf.fit(X) # compute output kernel G_iso = neighbors.kneighbors_graph(clf.embedding_, n_neighbors, mode='distance').toarray() K_iso = centerer.fit_transform(-0.5 * G_iso ** 2) # make sure error agrees reconstruction_error = np.linalg.norm(K - K_iso) / Npts assert_almost_equal(reconstruction_error, clf.reconstruction_error()) def test_transform(): n_samples = 200 n_components = 10 noise_scale = 0.01 # Create S-curve dataset X, y = datasets.samples_generator.make_s_curve(n_samples, random_state=0) # Compute isomap embedding iso = manifold.Isomap(n_components, 2) X_iso = iso.fit_transform(X) # Re-embed a noisy version of the points rng = np.random.RandomState(0) noise = noise_scale * rng.randn(*X.shape) X_iso2 = iso.transform(X + noise) # Make sure the rms error on re-embedding is comparable to noise_scale assert_less(np.sqrt(np.mean((X_iso - X_iso2) ** 2)), 2 * noise_scale) def test_pipeline(): # check that Isomap works fine as a transformer in a Pipeline # only checks that no error is raised. # TODO check that it actually does something useful X, y = datasets.make_blobs(random_state=0) clf = pipeline.Pipeline( [('isomap', manifold.Isomap()), ('clf', neighbors.KNeighborsClassifier())]) clf.fit(X, y) assert_less(.9, clf.score(X, y)) if __name__ == '__main__': import nose nose.runmodule()
bsd-3-clause
Zubair-Marediya/project-zeta
code/linear_model_scripts_sub4.py
3
25730
# Goal for this scripts: # # Perform linear regression and analyze the similarity in terms of the activated brain area when recognizing different # objects in odd and even runs of subject 1 # Load required function and modules: from __future__ import print_function, division import numpy as np import numpy.linalg as npl import matplotlib import matplotlib.pyplot as plt from matplotlib import colors from matplotlib import gridspec import os import re import json import nibabel as nib from utils import subject_class as sc from utils import outlier from utils import diagnostics as diagnos from utils import get_object_neural as neural from utils import stimuli from utils import convolution as convol from utils import smooth as sm from utils import linear_model as lm from utils import maskfunc as msk from utils import affine import copy # important path: base_path = os.path.abspath(os.path.dirname(__file__)) base_path = os.path.join(base_path, "..") # where to store figures figure_path = os.path.join(base_path, "code", "images", "") # where to store txt files file_path = os.path.join(base_path, "code", "txt", "") # help to make directory to save figures and txt files # if figure folder doesn't exist -> make it if not os.path.exists(figure_path): os.makedirs(figure_path) # if txt folder doesn't exist -> make it if not os.path.exists(file_path): os.makedirs(file_path) # color display: # list of all objects in this study in alphabetical order object_list = ["bottle", "cat", "chair", "face", "house", "scissors", "scrambledpix", "shoe"] # assign color for task time course for each object color_list_s = ["b", "g", "r", "c", "m", "y", "k", "sienna"] match_color_s = dict(zip(object_list, color_list_s)) # assign color for convolved result for each object color_list_c = ["royalblue", "darksage", "tomato", "cadetblue", "orchid", "goldenrod", "dimgrey", "sandybrown"] match_color_c = dict(zip(object_list, color_list_c)) # color for showing beta values nice_cmap_values = np.loadtxt(file_path + 'actc.txt') nice_cmap = colors.ListedColormap(nice_cmap_values, 'actc') # assign object parameter number: each object has a iterable number match_para = dict(zip(object_list, range(8))) # check slice number: # this is the specific slice we use to run 2D correlation slice_number = 39 # separator for better report display sec_separator = '#' * 80 separator = "-" * 80 # which subject to work on? subid = "sub004" # work on results from this subject: ################################### START ##################################### print (sec_separator) print ("Project-Zeta: use linear regression to study ds105 dataset") print (separator) print ("Focus on %s for the analysis" % subid) print (sec_separator) print ("Progress: Clean up data") print (separator) # load important data for this subject by using subject_class sub = sc.subject(subid) # get image files of this subject: sub_img = sub.run_img_result # get run numbers of this subject: run_num = len(sub.run_keys) # report keys of all images: print ("Import %s images" % subid) print (separator) print ("These images are imported:") img_key = sub_img.keys() img_key = sorted(img_key) for i in img_key: print (i) # report how many runs in this subject print ("There are %d runs for %s" % (run_num, subid)) print (separator) # get data for those figures print ("Get data from images...") sub_data = {} for key, img in sub_img.items(): sub_data[key] = img.get_data() print ("Complete!") print (separator) # use rms_diff to check outlier for all runs of this subject print ("Analyze outliers in these runs:") for key, data in sub_data.items(): rms_diff = diagnos.vol_rms_diff(data) # get outlier indices and the threshold for the outlier rms_outlier_indices, rms_thresh = diagnos.iqr_outliers(rms_diff) y_value2 = [rms_diff[i] for i in rms_outlier_indices] # create figures to show the outlier fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.55, 0.75]) ax.plot(rms_diff, label='rms') ax.plot([0, len(rms_diff)], [rms_thresh[1], rms_thresh[1]], "k--",\ label='high threshold', color='m') ax.plot([0, len(rms_diff)], [rms_thresh[0], rms_thresh[0]], "k--",\ label='low threshold', color='c') ax.plot(rms_outlier_indices, y_value2, 'o', color='g', label='outlier') # label the figure ax.set_xlabel('Scan time course') ax.set_ylabel('Volumne RMS difference') ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., numpoints=1) fig.text(0.05, 0.9, 'Volume RMS Difference with Outliers for %s' % key, weight='bold') # save figure fig.savefig(figure_path + 'Volume_RMS_Difference_Outliers_%s.png' % key) # clear figure fig.clf() # close pyplot window plt.close() # report print ("Outlier analysis results are saved as figures!") print (separator) # remove outlier from images sub_clean_img, outlier_index = outlier.remove_data_outlier(sub_img) print ("Remove outlier:") print ("outliers are removed from each run!") print (sec_separator) # run generate predicted bold signals: print ("Progress: create predicted BOLD signals based on condition files") # get general all tr times == 121*2.5 = about 300 s # this is the x-axis to plot hemodynamic prediction all_tr_times = np.arange(sub.BOLD_shape[-1]) * sub.TR # the y-axis to plot hemodynamic prediction is the neural value from condition (on-off) sub_neural = neural.get_object_neural(sub.sub_id, sub.conditions, sub.TR, sub.BOLD_shape[-1]) # report info for all run details print ("The detailed run info for %s:" % subid) neural_key = sub_neural.keys() neural_key = sorted(neural_key) for i in neural_key: print (i) print (separator) # get task time course for all runs -> save as images print ("generate task time course images") print (separator) for run in range(1, run_num): # make plots to display task time course fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.55, 0.75]) for item in object_list: check_key = "run0%02d-%s" % (run, item) ax.plot(all_tr_times, sub_neural[check_key][0], label="%s" % item, c=match_color_s[item]) # make labels: ax.set_title("Task time course for %s-run0%02d" % (subid, run), weight='bold') ax.set_xlabel("Time course (second)") ax.set_ylabel("Task (Off = 0, On = 1)") ax.set_yticks([0, 1]) ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., numpoints=1) # save figure fig.savefig(figure_path + "Task_time_course_%s_run0%02d" % (subid, run)) # clear figure fig.clf() # close pyplot window plt.close() # report print ("task time course images are saved!") print (separator) # assume true HRF starts at zero, and gets to zero sometime before 35 seconds. tr_times = np.arange(0, 30, sub.TR) hrf_at_trs = convol.hrf(tr_times) # get convolution data for each objects in this run -> show figure for run001 print ("Work on convolution based on condition files:") print (separator) sub_convolved = convol.get_all_convolved(sub_neural, hrf_at_trs, file_path) print ("convolution analysis for all runs is complete") print (separator) # save convolved data for key, data in sub_convolved.items(): np.savetxt(file_path + "convolved_%s.txt" % key, data) print ("convolved results are saved as txt files") # show relationship between task time course and bold signals print ("Show relationship between task time course and predicted BOLD signals") # get keys for each neural conditions sub_neural_key = sub_neural.keys() # sort key for better display sub_neural_key = sorted(sub_neural_key) # create figures fig = plt.figure() for run in range(1, run_num): ax = plt.subplot(111) ax2 = ax.twinx() figures = {} count = 0 for item in object_list: # focus on one at a time: check_key = "run0%02d-%s" % (run, item) # perform convolution to generate estimated BOLD signals convolved = convol.convolution(sub_neural[check_key][0], hrf_at_trs) # plot the task time course and the estimated BOLD signals in same plot # plot the estimated BOLD signals # plot the task time course figures["fig" + "%s" % str(count)] = ax.plot(all_tr_times, sub_neural[check_key][0], c=match_color_s[item], label="%s-task" % item) count += 1 # plot estimated BOLD signal figures["fig" + "%s" % str(count)] = ax2.plot(all_tr_times, convolved, c=match_color_c[item], label="%s-BOLD" % item) count += 1 # label this plot plt.subplots_adjust(left=0.1, right=0.6, bottom=0.1, top=0.85) plt.text(0.25, 1.05, "Hemodynamic prediction of %s-run0%02d" % (subid, run), weight='bold') ax.set_xlabel("Time course (second)") ax.set_ylabel("Task (Off = 0, On = 1)") ax.set_yticks([-0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0]) ax2.set_ylabel("Estimated BOLD signal") ax2.set_yticks([-0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0]) # label legend total_figures = figures["fig0"] for i in range(1, len(figures)): total_figures += figures["fig" + "%s" % str(i)] labs = [fig.get_label() for fig in total_figures] ax.legend(total_figures, labs, bbox_to_anchor=(1.2, 1.0), loc=0, borderaxespad=0., fontsize=11) # save plot plt.savefig(figure_path + "%s_run0%02d_bold_prediction.png" % (subid, run)) # clear plot plt.clf() # close pyplot window plt.close() print (sec_separator) # remove outlier from convolved results print("Progress: clean up convolved results") sub_convolved = convol.remove_outlier(sub.sub_id, sub_convolved, outlier_index) print ("Outliers are removed from convolved results") print (sec_separator) # smooth the images: print ("Progress: Smooth images") # subject clean and smooth img == sub_cs_img sub_cs_img = sm.smooth(sub_clean_img) print ("Smooth images: Complete!") print (sec_separator) # get shape info of the images print ("Progress: record shape information") shape = {} for key, img in sub_cs_img.items(): shape[key] = img.shape print ("shape of %s = %s" % (key, shape[key])) with open(file_path+'new_shape.json', 'a') as fp: json.dump(shape, fp) print ("New shape info of images is recorded and saved as file") print (sec_separator) ############################## Linear regression ############################## print ("Let's run Linear regression") print (separator) # generate design matrix print ("Progress: generate design matrix") # generate design matrix for each runs design_matrix = lm.batch_make_design(sub_cs_img, sub_convolved) # check parameter numbers parameters = design_matrix["%s_run001" % subid].shape[-1] print ("parameter number: %d" % parameters) print ("Design matrix generated") print (separator) # rescale design matrix print ("Progress: rescale design matrix") design_matrix = lm.batch_scale_matrix(design_matrix) print ("Rescale design matrix: complete!") # save scaled design matrix as figure # plot scaled design matrix fig = plt.figure(figsize=(8.0, 8.0)) for key, matrix in design_matrix.items(): ax = plt.subplot(111) ax.imshow(matrix, aspect=0.1, interpolation="nearest", cmap="gray") # label this plot fig.text(0.15, 0.95, "scaled design matrix for %s" % key, weight='bold', fontsize=18) ax.set_xlabel("Parameters", fontsize=16) ax.set_xticklabels([]) ax.set_ylabel("Scan time course", fontsize=16) # save plot plt.savefig(figure_path + "design_matrix_%s" % key) # clean plot plt.clf() # close pyplot window plt.close() print ("Design matrices are saved as figures") print (separator) # use maskfunc to generate mask print ("Progress: generate mask for brain images") mask, mean_data = msk.generateMaskedBrain(sub_clean_img) print ("Generate mask for brain images: complete!") # save mask as figure for key, each in mask.items(): for i in range(1, 90): plt.subplot(9, 10, i) plt.imshow(each[:, :, i], interpolation="nearest", cmap="gray", alpha=0.5) # label plot ax = plt.gca() ax.set_xticklabels([]) ax.set_yticklabels([]) # save plot plt.savefig(figure_path + "all_masks_for_%s.png" % key) # clear plot plt.clf() # close pyplot window plt.close() print (separator) # run linear regression to generate betas # first step: use mask to get data and reshape to 2D print ("Progress: Use mask to subset brain images") sub_cs_mask_img = lm.apply_mask(sub_cs_img, mask) sub_cs_mask_img_2d = lm.batch_convert_2d_based(sub_cs_mask_img, shape) # sub1_cs_mask_img_2d = lm.batch_convert_2d(sub1_cs_mask_img) print ("Use mask to subset brain images: complete!") print (separator) # second step: run linear regression to get betas: print ("Progress: Run linear regression to get beta hat") all_betas = {} for key, img in sub_cs_mask_img_2d.items(): #img_2d = np.reshape(img, (-1, img.shape[-1])) Y = img.T all_betas[key] = npl.pinv(design_matrix[key]).dot(Y) print ("Getting betas from linear regression: complete!") print (separator) # third step: put betas back into it's original place: print ("Save beta figures:") beta_vols = {} raw_beta_vols = {} for key, betas in all_betas.items(): # create 3D zeros to hold betas beta_vols[key] = np.zeros(shape[key][:-1] + (parameters,)) # get the mask info check_mask = (mask[key] == 1) # fit betas back to 3D beta_vols[key][check_mask] = betas.T print ("betas of %s is fitted back to 3D!" % key) # save 3D betas in dictionary raw_beta_vols[key] = beta_vols[key] # get min and max of figure vmin = beta_vols[key][:, :, 21:70].min() vmax = beta_vols[key][:, :, 21:70].max() # clear the background beta_vols[key][~check_mask] = np.nan mean_data[key][~check_mask] = np.nan # plot betas fig = plt.figure(figsize=(8.0, 8.0)) for item in object_list: # plot 50 pictures fig, axes = plt.subplots(nrows=5, ncols=10) lookat = 20 for ax in axes.flat: # show plot from z= 21~70 ax.imshow(mean_data[key][:, :, lookat], interpolation="nearest", cmap="gray", alpha=0.5) im = ax.imshow(beta_vols[key][:, :, lookat, match_para[item]], cmap=nice_cmap, alpha=0.5) # label the plot ax.set_xticks([]) ax.set_yticks([]) ax.set_xticklabels([]) ax.set_yticklabels([]) lookat += 1 # label the plot fig.subplots_adjust(bottom=0.2, hspace=0) fig.text(0.28, 0.9, "Brain area responding to %s in %s" % (item, subid), weight='bold') # color bar cbar_ax = fig.add_axes([0.15, 0.08, 0.7, 0.04]) fig.colorbar(im, cax=cbar_ax, ticks=[], orientation='horizontal') fig.text(0.35, 0.15, "Relative responding intensity") fig.text(0.095, 0.09, "Low") fig.text(0.87, 0.09, "High") # save plot plt.savefig(figure_path + "betas_for_%s_%s.png" % (key, item)) # clear plot plt.clf() # close pyplot window plt.close() # report print ("beta figures are generated!!") print (sec_separator) # analyze based on odd runs even runs using affine print ("Progress: Use affine matrix to check brain position") print ("print affine matrix for each images:") affine_matrix = {} for key, img in sub_img.items(): affine_matrix[key] = img.affine print("%s :\n %s" % (key, img.affine)) # check if they all have same affine matrix same_affine = True check_matrix = affine_matrix["%s_run001" % subid] for key, aff in affine_matrix.items(): if aff.all() != check_matrix.all(): same_affine = False if same_affine: print ("They have the same affine matrix!") else: print ("They don't have same affine matrix -> be careful about the brain position") print (sec_separator) ############################## 2D correlation ################################# # Focus on 2D slice to run the analysis: print ("Progress: Try 2D correlation") print ("Focus on one slice: k = %d" % slice_number) print (separator) print ("Run correlation between run1_house, run2_house and run2_face") # get 2D slice for run1 house run1_house = raw_beta_vols["%s_run001" % subid][:, 25:50, slice_number, 5] # save as plot plt.imshow(run1_house, interpolation="nearest", cmap=nice_cmap, alpha=0.5) plt.title("%s_Run1_House" % subid) plt.savefig(figure_path + "%s_run1_house.png" % subid) plt.clf() # get 2D slice of run2 house run2_house = raw_beta_vols["%s_run002" % subid][:, 25:50, slice_number, 5] # save as plot plt.imshow(run2_house, interpolation="nearest", cmap=nice_cmap, alpha=0.5) plt.title("%s_Run2_House" % subid) plt.savefig(figure_path + "%s_run2_house.png" % subid) plt.clf() # get 2D slice for run2 face run2_face = raw_beta_vols["%s_run002" % subid][:, 25:50, slice_number, 4] # save as plot plt.imshow(run2_face, interpolation="nearest", cmap=nice_cmap, alpha=0.5) plt.title("%s_Run2_Face" % subid) plt.savefig(figure_path + "%s_run2_face.png" % subid) plt.close() # put those 2D plots together fig = plt.figure() plt.subplot(1, 3, 1, xticks=[], yticks=[], xticklabels=[], yticklabels=[]) plt.imshow(run1_house, interpolation="nearest", cmap=nice_cmap, alpha=0.5) plt.title("Sub%s_Run1_House" % subid[-1], weight='bold', fontsize=10) plt.subplot(1, 3, 2, xticks=[], yticks=[]) plt.imshow(run2_house, interpolation="nearest", cmap=nice_cmap, alpha=0.5) plt.title("Sub%s_Run2_House" % subid[-1], weight='bold', fontsize=10) plt.subplot(1, 3, 3, xticks=[], yticks=[]) plt.imshow(run2_face, interpolation="nearest", cmap=nice_cmap, alpha=0.5) plt.title("Sub%s_Run2_Face" % subid[-1], weight='bold', fontsize=10) # label plot fig.subplots_adjust(bottom=0.2, hspace=0) cbar_ax = fig.add_axes([0.15, 0.08, 0.7, 0.04]) plt.colorbar(cax=cbar_ax, ticks=[], orientation='horizontal') fig.text(0.35, 0.15, "Relative responding intensity") fig.text(0.095, 0.09, "Low") fig.text(0.87, 0.09, "High") # save plot plt.savefig(figure_path + "%s_run_figure_compile.png" % subid) # close pyplot window plt.close() print ("plots for analysis are saved as figures") print ("Progress: Run correlation coefficient") # create a deepcopy of raw_beta_vols for correlation analysis: raw_beta_vols_corr = copy.deepcopy(raw_beta_vols) # flatten the 2D matrix house1 = np.ravel(raw_beta_vols_corr["%s_run001" % subid][:, 25:50, slice_number, match_para["house"]]) house2 = np.ravel(raw_beta_vols_corr["%s_run001" % subid][:, 25:50, slice_number, match_para["house"]]) face2 = np.ravel(raw_beta_vols_corr["%s_run001" % subid][:, 25:50, slice_number, match_para["face"]]) # save flatten results for further analysis np.savetxt(file_path + "%s_house1.txt" % subid, house1) np.savetxt(file_path + "%s_house2.txt" % subid, house2) np.savetxt(file_path + "%s_face2.txt" % subid, face2) # change nan to 0 in the array house1[np.isnan(house1)] = 0 house2[np.isnan(house2)] = 0 face2[np.isnan(face2)] = 0 # correlation coefficient study: house1_house2 = np.corrcoef(house1, house2) house1_face2 = np.corrcoef(house1, face2) print ("%s run1 house vs run2 house: %s" % (subid, house1_house2)) print ("%s run1 house vs run2 face : %s" % (subid, house1_face2)) print (sec_separator) # save individual 2D slice as txt for further analysis print ("save 2D result for each object and each run individually as txt file") for i in range(1, run_num+1): for item in object_list: temp = raw_beta_vols_corr["%s_run0%02d" % (subid, i)][:, 25:50, slice_number, match_para[item]] np.savetxt(file_path + "%s_run0%02d_%s.txt" % (subid, i, item), np.ravel(temp)) print ("Complete!!") print (sec_separator) # analyze based on odd runs even runs print ("Progress: prepare data to run correlation based on odd runs and even runs:") print ("Take average of odd run / even run results to deal with impacts of variations between runs") even_run = {} odd_run = {} even_count = 0 odd_count = 0 # add up even run results / odd run results and take mean for each groups for item in object_list: even_run[item] = np.zeros_like(raw_beta_vols_corr["%s_run001" % subid][:, 25:50, slice_number, 5]) odd_run[item] = np.zeros_like(raw_beta_vols_corr["%s_run001" % subid][:, 25:50, slice_number, 5]) print ("make average of odd run results:") # add up odd run results for i in range(1, run_num+1, 2): temp = raw_beta_vols_corr["%s_run0%02d" % (subid, i)][:, 25:50, slice_number, match_para[item]] temp[np.isnan(temp)] = 0 odd_run[item] += temp odd_count += 1 print("odd runs: %d-%s" % (i, item)) print ("make average od even run results:") # take mean odd_run[item] = odd_run[item]/odd_count # add up even run results for i in range(2, run_num+1, 2): temp = raw_beta_vols_corr["%s_run0%02d" % (subid, i)][:, 25:50, slice_number, match_para[item]] temp[np.isnan(temp)] = 0 even_run[item] += temp even_count += 1 print("even: %d, %s" % (i, item)) # take mean even_run[item] = even_run[item]/even_count print (separator) # save odd run and even run results as txt file print ("Progress: save flatten mean odd / even run results as txt files") for key, fig in even_run.items(): np.savetxt(file_path + "%s_even_%s.txt" % (subid, key), np.ravel(fig)) for key, fig in odd_run.items(): np.savetxt(file_path + "%s_odd_%s.txt" % (subid, key), np.ravel(fig)) print ("odd run and even run results are saved as txt files!!!!!") print (separator) ############################ 3D correlation ################################### # check 3D: print ("Focus on one 3D analysis, shape = [:, 25:50, 31:36]") # put 3D slice of run1 house, run2 face, run2 house together fig = plt.figure() i = 1 run1_house = raw_beta_vols["%s_run001" % subid][:, 25:50, 37:42, match_para["house"]] for z in range(5): plt.subplot(3, 5, i, xticks=[], yticks=[]) plt.imshow(run1_house[:, :, z], interpolation="nearest", cmap=nice_cmap, alpha=0.5) i += 1 if z == 2: plt.title("%s_run1_house" % subid) run2_house = raw_beta_vols["%s_run002" % subid][:, 25:50, 37:42, match_para["house"]] for z in range(5): plt.subplot(3, 5, i, xticks=[], yticks=[]) plt.imshow(run2_house[:, :, z], interpolation="nearest", cmap=nice_cmap, alpha=0.5) i += 1 if z == 2: plt.title("%s_run2_house" % subid) run2_face = raw_beta_vols["%s_run002" % subid][:, 25:50, 37:42, match_para["face"]] for z in range(5): plt.subplot(3, 5, i, xticks=[], yticks=[]) plt.imshow(run2_face[:, :, z], interpolation="nearest", cmap=nice_cmap, alpha=0.5) i += 1 if z == 2: plt.title("%s_run2_face" % subid) # label plot fig.subplots_adjust(bottom=0.2, hspace=0.5) cbar_ax = fig.add_axes([0.15, 0.06, 0.7, 0.02]) plt.colorbar(cax=cbar_ax, ticks=[], orientation='horizontal') fig.text(0.35, 0.1, "Relative responding intensity") fig.text(0.095, 0.07, "Low") fig.text(0.87, 0.07, "High") plt.savefig(figure_path + "Try_3D_correlation_%s.png" % subid) plt.close() # try to run 3D correlation study: print ("Progress: Run correlation coefficient with 3D data") # make a deepcopy of the raw_beta_vols for correlation study: raw_beta_vols_3d_corr = copy.deepcopy(raw_beta_vols) # get flatten 3D slice: house1_3d = np.ravel(raw_beta_vols_3d_corr["%s_run001" % subid][:, 25:50, 37:42, match_para["house"]]) house2_3d = np.ravel(raw_beta_vols_3d_corr["%s_run002" % subid][:, 25:50, 37:42, match_para["house"]]) face2_3d = np.ravel(raw_beta_vols_3d_corr["%s_run002" % subid][:, 25:50, 37:42, match_para["face"]]) # change nan to 0 in the array house1_3d[np.isnan(house1_3d)] = 0 house2_3d[np.isnan(house2_3d)] = 0 face2_3d[np.isnan(face2_3d)] = 0 # correlation coefficient study: threeD_house1_house2 = np.corrcoef(house1_3d, house2_3d) threeD_house1_face2 = np.corrcoef(house1_3d, face2_3d) print ("%s run1 house vs run2 house in 3D: %s" % (subid, threeD_house1_house2)) print ("%s run1 house vs run2 face in 3D: %s" % (subid, threeD_house1_face2)) print (separator) # prepare data to analyze 3D brain based on odd runs even runs print ("Prepare data to analyze \"3D\" brain based on odd runs and even runs:") print ("Take average of \"3D\" odd runs / even runs to deal with impacts of variations between runs") even_run_3d = {} odd_run_3d = {} # add up even run results / odd run results and take mean for each groups for item in object_list: even_run_3d[item] = np.zeros_like(raw_beta_vols_3d_corr["%s_run001" % subid][:, 25:50, 37:42, match_para[item]]) odd_run_3d[item] = np.zeros_like(raw_beta_vols_3d_corr["%s_run001" % subid][:, 25:50, 37:42, match_para[item]]) print ("make average of \"3D\" odd run results:") # add up odd runs results for i in range(1, run_num+1, 2): temp = raw_beta_vols_3d_corr["%s_run0%02d" % (subid, i)][:, 25:50, 37:42, match_para[item]] temp[np.isnan(temp)] = 0 odd_run_3d[item] += temp print("odd runs 3D: %d-%s" % (i, item)) # take mean odd_run_3d[item] = odd_run_3d[item]/odd_count print ("make average of \"3D\" even run results:") # add up even runs results for i in range(2, run_num+1, 2): temp = raw_beta_vols_3d_corr["%s_run0%02d" % (subid, i)][:, 25:50, 37:42, match_para[item]] temp[np.isnan(temp)] = 0 even_run_3d[item] += temp print("even runs 3D: %d-%s" % (i, item)) # take mean even_run_3d[item] = even_run_3d[item]/even_count # save odd run and even run results as txt file for key, fig in even_run_3d.items(): np.savetxt(file_path + "%s_even_%s_3d.txt" % (subid, key), np.ravel(fig)) for key, fig in odd_run_3d.items(): np.savetxt(file_path + "%s_odd_%s_3d.txt" % (subid, key), np.ravel(fig)) print ("\"3D\" odd run and even run results are saved as txt files!!!!!") print (separator) print ("Analysis and Data Pre-processing for %s : Complete!!!" % subid)
bsd-3-clause