def __init__(self, modelfile, var=('X1', 'X2')): if type(modelfile) is str: x1 = loadmatvar(modelfile[0], var[0]) x2 = loadmatvar(modelfile[1], var[1]) else: x1 = modelfile[0] x2 = modelfile[1] x1_hat = pinv(x1) self.x1 = x1 self.x2 = x2 self.x1_hat = x1_hat
def crossCorr(self, signal, lag=0, var=None): """ Cross correlate time series data against another signal Parameters ---------- signal : array, or str Signal to correlate against, can be a numpy array or a MAT file containing the signal as a variable var : str Variable name if loading from a MAT file lag : int Range of lags to consider, will cover (-lag, +lag) """ from scipy.linalg import norm if type(signal) is str: s = loadmatvar(signal, var) else: s = signal # standardize signal s = s - mean(s) s = s / norm(s) if size(s) != size(self.index): raise Exception('Size of signal to cross correlate with, %g, does not match size of series' % size(s)) # created a matrix with lagged signals if lag is not 0: shifts = range(-lag, lag+1) d = len(s) m = len(shifts) s_shifted = zeros((m, d)) for i in range(0, len(shifts)): tmp = roll(s, shifts[i]) if shifts[i] < 0: # zero padding tmp[(d+shifts[i]):] = 0 if shifts[i] > 0: tmp[:shifts[i]] = 0 s_shifted[i, :] = tmp s = s_shifted else: shifts = 0 def get(y, s): y = y - mean(y) n = norm(y) if n == 0: b = zeros((s.shape[0],)) else: y /= norm(y) b = dot(s, y) return b rdd = self.rdd.mapValues(lambda x: get(x, s)) return self._constructor(rdd, index=shifts).__finalize__(self)
def query(self, inds, var='inds', order='F', onebased=True): """ Extract records with indices matching those provided Keys will be automatically linearized before matching to provided indices. This will not affect Parameters ---------- inds : str, or array-like (2D) Array of indices, each an array-like of integer indices, or filename of a MAT file containing a set of indices as a cell array var : str, optional, default = 'inds' Variable name if loading from a MAT file order : str, optional, default = 'F' Specify ordering for linearizing indices (see subtoind) onebased : boolean, optional, default = True Specify zero or one based indexing for linearizing (see subtoind) Returns ------- keys : array, shape (n, k) where k is the length of each value Averaged values values : array, shape (n, d) where d is the number of keys Averaged keys """ if isinstance(inds, str): inds = loadmatvar(inds, var)[0] else: inds = asarray(inds) n = len(inds) from thunder.rdds.keys import _indtosub_converter converter = _indtosub_converter(dims=self.dims.max, order=order, onebased=onebased) keys = zeros((n, len(self.dims.count))) values = zeros((n, len(self.first()[1]))) data = self.subtoind(order=order, onebased=onebased) for idx, indlist in enumerate(inds): if len(indlist) > 0: inds_set = set(asarray(indlist).flat) inds_bc = self.rdd.context.broadcast(inds_set) values[idx, :] = data.filterOnKeys( lambda k: k in inds_bc.value).values().mean() keys[idx, :] = mean(map(lambda k: converter(k), indlist), axis=0) return keys, values
def __init__(self, modelfile, var='X'): if type(modelfile) is str: x = loadmatvar(modelfile, var) else: x = modelfile x = x.astype(float) x_hat = (x.T / sum(x, axis=1)).T self.x = x self.x_hat = x_hat
def __init__(self, modelfile, var='X'): if type(modelfile) is str: x = loadmatvar(modelfile, var) else: x = modelfile x = concatenate((ones((1, shape(x)[1])), x)) x_hat = pinv(x) self.x = x self.x_hat = x_hat
def query(self, inds, var='inds', order='F', onebased=True): """ Extract records with indices matching those provided Keys will be automatically linearized before matching to provided indices. This will not affect Parameters ---------- inds : str, or array-like (2D) Array of indices, each an array-like of integer indices, or filename of a MAT file containing a set of indices as a cell array var : str, optional, default = 'inds' Variable name if loading from a MAT file order : str, optional, default = 'F' Specify ordering for linearizing indices (see subtoind) onebased : boolean, optional, default = True Specify zero or one based indexing for linearizing (see subtoind) Returns ------- keys : array, shape (n, k) where k is the length of each value Averaged values values : array, shape (n, d) where d is the number of keys Averaged keys """ if isinstance(inds, str): inds = loadmatvar(inds, var)[0] else: inds = asarray(inds) n = len(inds) from thunder.rdds.keys import _indtosub_converter converter = _indtosub_converter(dims=self.dims.max, order=order, onebased=onebased) keys = zeros((n, len(self.dims.count))) values = zeros((n, len(self.first()[1]))) data = self.subtoind(order=order, onebased=onebased) for idx, indlist in enumerate(inds): if len(indlist) > 0: inds_set = set(asarray(indlist).flat) inds_bc = self.rdd.context.broadcast(inds_set) values[idx, :] = data.filterOnKeys(lambda k: k in inds_bc.value).values().mean() keys[idx, :] = mean(map(lambda k: converter(k), indlist), axis=0) return keys, values
def convolve(self, signal, mode='full', var=None): """ Conolve time series data against another signal Parameters ---------- signal : array, or str Signal to convolve with, can be a numpy array or a MAT file containing the signal as a variable var : str Variable name if loading from a MAT file mode : str, optional, default='full' Mode of convolution, options are 'full', 'same', and 'same' """ from numpy import convolve if type(signal) is str: s = loadmatvar(signal, var) else: s = asarray(signal) n = size(self.index) m = size(s) newrdd = self.rdd.mapValues(lambda x: convolve(x, signal, mode)) # use expected lengths to make a new index if mode == 'same': newmax = max(n, m) elif mode == 'valid': newmax = max(m, n) - min(m, n) + 1 else: newmax = n+m-1 newindex = arange(0, newmax) return self._constructor(newrdd, index=newindex).__finalize__(self)
def __init__(self, modelfile, var='s'): if type(modelfile) is str: self.s = loadmatvar(modelfile, var) else: self.s = modelfile