def nsigtf_real(*args): nargin = len(args) if nargin < 4: raiseValueError('Not enough input arguments') # iscell if iscell(c) == 0: if np.ndims(c) == 2: N, chan_len = c.shape CH = 1 # c = mat2cell(c.',chan_len,ones(1,N)).') else: N, chan_len, CH = c.shape ctemp = mat2cell(np.permute(c,[2,1,3]),chan_len,np.ones((1,N)),np.ones((1,CH))) c = np.permute(ctemp,[2,3,1]) else: CH, N = c.shape posit = np.cumsum(shift) NN = posit[-1] posit = posit - shift[1] fr = np.zeros((NN,CH)) for ii in range(N): Lg = len(g[ii]) win_range = (posit[ii]+(-np.floor(Lg/2):math.ceil(Lg/2)-1)) % NN + 1 temp = np.fft(c[ii], [], 1) * len(c[ii]) if phasemode == 'global': fsNewBins = c[ii].shape[0] fkBins = posit[ii] displace = fkBins - np.floor(fkBins/fsNewBins) * fsNewBins temp = np.roll(temp, -displace) # temp = temp[] fr[win_range,:] = fr[win_Range,:] + temp * g[ii][Lg-np.floor(Lg/2)+1:Lg, 1:np.ceil(Lg/2)] nyqBin = np.floor(Ls/2) + 1 # fr[nyqBin+1:end] = conj( fr(nyqBin - (~logical(mod(Ls,2))) : -1 : 2) ) fr = np.real(np.fft.ifft(fr)) return fr
def generate_files_sony(old_subj_data, savepath): num_subj = len(old_subj_data) num_cond = len(old_subj_data[0]) subj_data = [[dcpy(empty_map) for i in range(num_cond)] for j in range(num_subj)] clip_size = old_subj_data[0][0]['trials'].shape[2] / 2 num_electrodes = old_subj_data[0][0]['trials'].shape[1] for subj_ind in range(num_subj): for cond in range(num_cond): conds = conds_map[cond] old_entry = old_subj_data[subj_ind][cond] for i in range(2): clip = range(clip_size * i, clip_size * (i + 1)) entry = subj_data[subj_ind][conds[i]] entry['trials'] = concat(entry['trials'], old_entry['trials'][:, :, clip]) if not os.path.isdir(savepath): os.makedirs(savepath) dats = [] dats_det = [] for cond in range(num_cond): print 'Generating condition', cond + 1, 'files...' dats.append(np.zeros((0, clip_size, num_electrodes))) dats_det.append(np.zeros((0, clip_size, num_electrodes))) for subj in range(num_subj): trials = subj_data[subj][cond]['trials'] dats[-1] = concat(dats[-1], permute(trials, axes=(0, 2, 1))) savename = os.path.join(savepath, cond_names_map[cond]) savemat(savename, {'dat': dats[-1]}) dats_det[-1] = detrend(dats[-1], axis=1) savename = os.path.join(savepath, cond_names_map[cond] + '_det') savemat(savename, {'dat': dats_det[-1]})
def invariant_perturbation(equation_system, constants, variables, operators): permuted_system = np.permute(equation_system) equation = equation_system.join(',') all_vars = [chr(i) for i in range(97, 123)] for var in variables(): replacement = np.random.choice(all_vars) all_vars.remove(replacement) equation = equation.replace(var, replacement) for i, char in enumerate(equation): if char in ['+', '-']: left_begin = EquivalenceDataset.get_expression_subtree_suffix(equation, i - 1, operators) right_end = EquivalenceDataset.get
def decode_segmap(mask, unk_label=255): """Decode segmentation label prediction as RGB images Args: mask (torch.tensor): class map with dimensions (B, M,N), where the value at a given location is the integer denoting the class index. Returns: (np.ndarray): colored image of shape (BM, BN, 3) """ mask[mask == unk_label] == 0 mask = mask.numpy() cmap = get_pascal_labels() cmap_exp = cmap[..., None] colored = cmap[mask].squeeze() grid = make_grid(torch.tensor(colored).permute(0, 3, 1, 2)) return np.permute(grid, (1, 2, 0))
def generate_data(path, savepath): subjs = [item for item in os.listdir(path) if '.' not in item] num_cond = get_num_conds(os.path.join(path, subjs[0])) subj_data = [[dcpy(empty_map) for i in range(num_cond)] for j in range(len(subjs))] for subj_ind in xrange(len(subjs)): subj = subjs[subj_ind] print 'Adding subject', subj subj_path = os.path.join(path, subj) files = [item for item in os.listdir(subj_path) if 'Raw' in item] for data_ind in xrange(len(files)): data_file = files[data_ind] cond = int(data_file[data_file.rfind('c00') + 3]) - 1 data = loadmat(os.path.join(subj_path, data_file)) raw_data = data['RawTrial'][:, electrodes] num_TP = raw_data.shape[0] / data['NmbEpochs'] raw_data = permute(raw_data.reshape(num_TP, data['NmbEpochs'], len(electrodes)), axes=(1, 2, 0)) valid_epochs = [i for i in range(data['NmbEpochs']) if \ np.sum(data['IsEpochOK'][i]) > num_channels/2] raw_data = raw_data[valid_epochs, :, :] num_epochs = raw_data.shape[0] if num_epochs: entry = subj_data[subj_ind][cond] entry['groups'].append(num_epochs) entry['trials'] = concat(entry['trials'], raw_data) raw_data_mean = np.mean(raw_data, axis=0)[None, :] entry['mean_trials'] = concat(entry['mean_trials'], raw_data_mean) for cond in xrange(num_cond): entry = subj_data[subj_ind][cond] entry['groups'] = np.squeeze(entry['groups']) entry['trials'] = np.squeeze(entry['trials']) entry['mean_trials'] = np.squeeze(entry['mean_trials']) if not os.path.isdir(savepath): os.makedirs(savepath) savemat(os.path.join(savepath, 'subj_data'), {'subj_data': subj_data}) return subj_data
def perturbImage(img, pmesh): mr = 89 mc = 69 tr = 100 tc = 100 mg = 5 [mh, mw,c] = img.shape mx = np.linspace(mh, 1, mr) my = np.linspace(1, mw, mc) [my,mx] = np.meshgrid(mx,my) smesh = np.concatenate((mx,my),axis=1) px = pmesh[:, 0] py = pmesh[:, 1] minx = np.min(px) maxx = np.max(px) miny = np.min(py) maxy = np.max(py) px = (px - minx) / (maxx - minx) py = (py - miny) / (maxy - miny) px = px * (tc - 2 * mg - 1) + 1 + mg py = py * (tr - 2 * mg - 1) + 1 + mg pmesh = np.concatenate((px, py),axis=0); print(pmesh.shape) fm = localWarp(pmesh, smesh, tr, tc, mr, mc); fm = np.reshape(fm, 2, tr, tc); fm = np.permute(fm, [2, 3, 1]); fm = np.concatenate((fm[2 : end, :, :], fm[1, :, :]),axis=0) fm = np.concatenate((fm[:, 2 : end, :], fm[:, 1, :]),axis = 1) pimg = imgMeshWarp(img, fm) pv = pmesh pv = np.reshape(pv, mc, mr, 2) pv = pv[:, end : -1 : 1, :] pv = np.reshape(pv, (-1, 2)) return pimg
def save_np_img(fname, x): if x.shape[0] == 1: x = np.tile(x, (3, 1, 1)) img = Image.fromarray((np.permute(x, (1, 2, 0)) * 255.0).astype(np.uint8), 'RGB') img.save(fname)
def randomize_position(pos): # Find non indel species I = np.where(pos != '-') Iperm = np.permute(I) return pos[Iperm]
def shuffle_random(x, ind): return np.permute(x)