def random_affine_transformation(ds, scale_fac=100., shift_fac=10.): """Distort a dataset by random scale, shift, and rotation. The original data samples are transformed by applying a random rotation, shifting by a random vector (randomly selected, scaled input sample), and scaled by a random factor (randomly selected input feature values, scaled by an additional factor). The effective transformation values are stored in the output dataset's attribute collection as 'random_rotation', 'random_shift', and 'random_scale' respectively. Parameters ---------- ds : Dataset Input dataset. Its sample and features attributes will be assigned to the output dataset. scale_fac : float Factor by which the randomly selected value for data scaling is scaled itself. shift_fac : float Factor by which the randomly selected shift vector is scaled. """ rndidx = np.random.randint R = get_random_rotation(ds.nfeatures) samples = ds.samples # reusing random data from dataset itself random_scale = samples[rndidx(len(ds)), rndidx(ds.nfeatures)] * scale_fac random_shift = samples[rndidx(len(ds))] * shift_fac samples = np.dot(samples, R) * random_scale \ + random_shift return Dataset(samples, sa=ds.sa, fa=ds.fa, a={'random_rotation': R, 'random_scale': random_scale, 'random_shift': random_shift})
def get_fake_data(nsubjects=20, noise_level=0.2, nbogus_classes=0): orig_ds = mean_group_sample(['targets'])(testing_datasets['uni3large']) # and creating an additional target which is a composition of the other two, so # it should be closer to them than to the left out L2 classes_data = [ orig_ds.samples, orig_ds[0].samples + orig_ds[1].samples, orig_ds[1].samples + 4 * orig_ds[2].samples ] classes_targets = list(orig_ds.T) + ['L0+1', 'L1+4*2'] if nbogus_classes: classes_data.append( np.zeros((nbogus_classes, classes_data[0].shape[1]), dtype=float)) classes_targets += ['B%02d' % i for i in xrange(nbogus_classes)] proto_ds = dataset_wizard(np.vstack(classes_data), targets=classes_targets) ntargets = len(proto_ds.UT) dss = [] for i in xrange(nsubjects): R = get_random_rotation(proto_ds.nfeatures) ds = dataset_wizard(np.dot(proto_ds.samples, R), targets=proto_ds.T) #ds = dataset_wizard(proto_ds.samples, targets=proto_ds.T) ds.sa['subjects'] = [i] # And select a varying number of features ds = ds[:, :np.random.randint(10, ds.nfeatures)] # Add some noise ds.samples += np.random.normal(size=ds.shape) * noise_level dss.append(ds) return dss
def rotate_surface(xyz): """ Function to rotate surface using a random rigid rotation Arguments --------- xyz : np.ndarray (n_points, 3) an array containing the euclidean coordinates of the vertices of the surface Returns ------- xyz_rnd : np.ndarray (n_points, 3) the rotated array with the euclidean coordinates of the rotated vertices of the surface dist : np.ndarray (n_points, 3) for each point of the rotated surface, the distance from the three nearest vertices in the original surface i : np.ndarray (n_points, 3) for each point of the rotated surface, the indices of the three nearest vertices in the original surface """ assert xyz.shape[1] == 3, 'I work only with surfaces in 3D spaces' # rotate xyz randomly rnd_rot = get_random_rotation(xyz.shape[1]) xyz_rnd = np.dot(xyz, rnd_rot) # find three closest neighbors making up the triangle nbrs = NearestNeighbors(n_neighbors=3, algorithm='auto').fit(xyz) dist, i = nbrs.kneighbors(xyz_rnd) return xyz_rnd, dist, i
def rotate_surface(xyz): """ Function to rotate surface using a random rigid rotation Arguments --------- xyz : np.ndarray (n_points, 3) an array containing the euclidean coordinates of the vertices of the surface Returns ------- xyz_rnd : np.ndarray (n_points, 3) the rotated array with the euclidean coordinates of the rotated vertices of the surface dist : np.ndarray (n_points, 3) for each point of the rotated surface, the distance from the three nearest vertices in the original surface i : np.ndarray (n_points, 3) for each point of the rotated surface, the indices of the three nearest vertices in the original surface """ assert xyz.shape[1] == 3, "I work only with surfaces in 3D spaces" # rotate xyz randomly rnd_rot = get_random_rotation(xyz.shape[1]) xyz_rnd = np.dot(xyz, rnd_rot) # find three closest neighbors making up the triangle nbrs = NearestNeighbors(n_neighbors=3, algorithm="auto").fit(xyz) dist, i = nbrs.kneighbors(xyz_rnd) return xyz_rnd, dist, i
def get_testdata(self): # rs = np.random.RandomState(0) rs = np.random.RandomState() nt = 200 n_triangles = 4 ns = 10 nv = n_triangles * 3 vertices = np.zeros((nv, 3)) # 4 separated triangles faces = [] for i in range(n_triangles): vertices[i * 3] = [i * 2, 0, 0] vertices[i * 3 + 1] = [i * 2 + 1, 1 / np.sqrt(3), 0] vertices[i * 3 + 2] = [i * 2 + 1, -1 / np.sqrt(3), 0] faces.append([i * 3, i * 3 + 1, i * 3 + 2]) faces = np.array(faces) surface = Surface(vertices, faces) ds_orig = np.zeros((nt, nv)) # add coarse-scale information for i in range(n_triangles): ds_orig[:, i * 3:(i + 1) * 3] += rs.normal(size=(nt, 1)) # add fine-scale information ds_orig += rs.normal(size=(nt, nv)) dss_train, dss_test = [], [] for i in range(ns): ds = np.zeros_like(ds_orig) for j in range(n_triangles): ds[:, j * 3:(j + 1) * 3] = np.dot(ds_orig[:, j * 3:(j + 1) * 3], get_random_rotation(3)) # special_ortho_group.rvs(3, random_state=rs)) ds = Dataset(ds) ds.fa['node_indices'] = np.arange(nv) ds_train, ds_test = ds[:nt // 2, :], ds[nt // 2:, :] zscore(ds_train, chunks_attr=None) zscore(ds_test, chunks_attr=None) dss_train.append(ds_train) dss_test.append(ds_test) return dss_train, dss_test, surface
def get_testdata(self): # rs = np.random.RandomState(0) rs = np.random.RandomState() nt = 200 n_triangles = 4 ns = 10 nv = n_triangles * 3 vertices = np.zeros((nv, 3)) # 4 separated triangles faces = [] for i in range(n_triangles): vertices[i*3] = [i*2, 0, 0] vertices[i*3+1] = [i*2+1, 1/np.sqrt(3), 0] vertices[i*3+2] = [i*2+1, -1/np.sqrt(3), 0] faces.append([i*3, i*3+1, i*3+2]) faces = np.array(faces) surface = Surface(vertices, faces) ds_orig = np.zeros((nt, nv)) # add coarse-scale information for i in range(n_triangles): ds_orig[:, i*3:(i+1)*3] += rs.normal(size=(nt, 1)) # add fine-scale information ds_orig += rs.normal(size=(nt, nv)) dss_train, dss_test = [], [] for i in range(ns): ds = np.zeros_like(ds_orig) for j in range(n_triangles): ds[:, j*3:(j+1)*3] = np.dot(ds_orig[:, j*3:(j+1)*3], get_random_rotation(3)) # special_ortho_group.rvs(3, random_state=rs)) ds = Dataset(ds) ds.fa['node_indices'] = np.arange(nv) ds_train, ds_test = ds[:nt//2, :], ds[nt//2:, :] zscore(ds_train, chunks_attr=None) zscore(ds_test, chunks_attr=None) dss_train.append(ds_train) dss_test.append(ds_test) return dss_train, dss_test, surface