def convert_to_phases(input_path, output_path, brain_areas, t_phases, subject): """ Converts raw data into phases by Hilbert Transform :param input_path: path to input file :type input_path: str :param output_path: path to output directory :type output_path: str :param brain_areas: number of brain areas :type brain_areas: int :param t_phases: number of time phases :type t_phases: int :param subject: subject number :type subject: int :return: phases matrix :rtype: np.ndarray """ phases = np.full((brain_areas, t_phases), fill_value=0).astype(np.float64) array = np.genfromtxt(input_path, delimiter=',') for area in tqdm(range(0, brain_areas)): # select by columns, transform to phase time_series = pylab.demean(signal.detrend(array[:, area])) phases[area, :] = np.angle(signal.hilbert(time_series)) np.savez(os.path.join(output_path, 'phases_{}'.format(subject)), phases) return phases
def data2data(data, clfs, kind='svm'): """ Applies all classifiers to the data and returns their output as vectors """ W = array([clfs[i][0][kind] for i in range(0, len(clfs))]) X = dot(data, W.T) X = demean(X, axis=1) return X
def normalize_signals(signals, normalization=None, axis=None, percent=None): # Following pylab demean: def matrix_subtract_along_axis(x, y, axis=0): "Return x minus y, where y corresponds to some statistic of x along the specified axis" if axis == 0 or axis is None or x.ndim <= 1: return x - y ind = [slice(None)] * x.ndim ind[axis] = np.newaxis return x - y[ind] def matrix_divide_along_axis(x, y, axis=0): "Return x divided by y, where y corresponds to some statistic of x along the specified axis" if axis == 0 or axis is None or x.ndim <= 1: return x / y ind = [slice(None)] * x.ndim ind[axis] = np.newaxis return x / y[ind] for norm, ax, prcnd in zip(ensure_list(normalization), cycle(ensure_list(axis)), cycle(ensure_list(percent))): if isinstance(norm, string_types): if isequal_string(norm, "zscore"): signals = zscore(signals, axis=ax) # / 3.0 elif isequal_string(norm, "baseline-std"): signals = normalize_signals(["baseline", "std"], axis=axis) elif norm.find("baseline") == 0 and norm.find("amplitude") >= 0: signals = normalize_signals(signals, ["baseline", norm.split("-")[1]], axis=axis, percent=percent) elif isequal_string(norm, "minmax"): signals = normalize_signals(signals, ["min", "max"], axis=axis) elif isequal_string(norm, "mean"): signals = demean(signals, axis=ax) elif isequal_string(norm, "baseline"): if prcnd is None: prcnd = 1 signals = matrix_subtract_along_axis(signals, np.percentile(signals, prcnd, axis=ax), axis=ax) elif isequal_string(norm, "min"): signals = matrix_subtract_along_axis(signals, np.min(signals, axis=ax), axis=ax) elif isequal_string(norm, "max"): signals = matrix_divide_along_axis(signals, np.max(signals, axis=ax), axis=ax) elif isequal_string(norm, "std"): signals = matrix_divide_along_axis(signals, signals.std(axis=ax), axis=ax) elif norm.find("amplitude") >= 0: if prcnd is None: prcnd = [1, 99] amplitude = np.percentile(signals, prcnd[1], axis=ax) - np.percentile(signals, prcnd[0], axis=ax) this_ax = ax if isequal_string(norm.split("amplitude")[0], "max"): amplitude = amplitude.max() this_ax = None elif isequal_string(norm.split("amplitude")[0], "mean"): amplitude = amplitude.mean() this_ax = None signals = matrix_divide_along_axis(signals, amplitude, axis=this_ax) else: raise_value_error("Ignoring signals' normalization " + normalization + ",\nwhich is not one of the currently available " + str(NORMALIZATION_METHODS) + "!") return signals
def CreateNewPC(path2TrainingData, path2GoodData, dimensions, path2SavePCAData): trainingImg = ReadInPics(path2TrainingData, dimensions) # get mean and demean imagematrix mean = np.mean(trainingImg, axis=0) # demean by substract mean of all rows in matrix matrixDemeaned = pylab.demean(trainingImg, axis=0) # covariance cov = 1/(N-1) XX.T - matrix must be # transposed for it so samples are columns and # features are in rows cova = np.cov(matrixDemeaned.T) # compute eigenvalues and eigenvectors eigVa, eigVe = np.linalg.eig(cova) # sort by eigenvalues idx = eigVa.argsort()[::-1] eigVa = eigVa[idx] eigVe = eigVe[idx] assert np.all(eigVa[:-1] >= eigVa[1:]) # sorted # save in numpy file np.save('eigVe', eigVe) np.save(str(Path(str(path2SavePCAData), 'eigVe.npy')), eigVe) np.save(str(Path(str(path2SavePCAData), 'mean.npy')), mean) np.save(str(Path(str(path2SavePCAData), 'numberOTrainingsetImg.npy')), trainingImg.shape[0]) # create new matrix of other good images to get # boundarys of good projected images goodImg = ReadInPics(path2GoodData, dimensions) # project on PC projectedGoodImg = ProjectOnPC(eigVe, goodImg, mean, dimensions) maxGood = np.apply_along_axis(lambda x: (max(x)), 0, projectedGoodImg) minGood = np.apply_along_axis(lambda x: (min(x)), 0, projectedGoodImg) # save max and min for good projections np.save(str(Path(str(path2SavePCAData), 'maxBoundary.npy')), maxGood) np.save(str(Path(str(path2SavePCAData), 'minBoundary.npy')), minGood)
def center_table(self): self.df[self.features] = pylab.demean(self.df[self.features], axis=1)
def center_table(self): return pylab.demean(self.table, axis=1)