def _assert_files_equal(expected_path, actual_path): def o(f): if f.endswith('.gz'): return gzip.open(f, 'rb') elif f.endswith('.hdf5'): return h5py.File(f, 'r') elif f.endswith('.ps'): return open(f, 'r') elif f.endswith('.pickle'): return open(f, 'rb') else: return open(f, 'rb') with o(expected_path) as e: with o(actual_path) as a: if expected_path.endswith('.hdf5'): _compare_hdf5(e, a) elif expected_path.endswith('.ps'): _compare_ps(e, a) elif expected_path.endswith('.pickle'): ta = pickle.load(a, encoding='latin1') te = pickle.load(e, encoding='latin1') if len(ta) == 0 or isinstance(ta[0], sp.int64): assert sp.all(ta == te) else: assert sp.all([_compare_gene(_[0], _[1]) for _ in zip(ta, te)]) elif os.path.basename(expected_path).startswith('test_results'): _assert_files_equal_testing(e, a) else: assert e.read() == a.read(), 'actual and expected content differ!\nactual path: %s\nexpected path: %s\n' % (expected_path, actual_path)
def test_hessian(): """ Test the the numerical hessian for the rosenbrock function with and without explicit gradient and a function of a higher order """ def F(x): return x[0]**3+x[1]**3+x[2]**3+x[0]**2 *x[1]**2 *x[2]**2 def F_hessian(x): return array([ [6.*x[0] + 2.*x[1]**2*x[2]**2, 4.*x[0]*x[1]*x[2]**2, 4.*x[0]*x[1]**2 *x[2]] , [4.*x[0]*x[1]*x[2]**2, 6.*x[1] + 2.*x[0]**2*x[2]**2, 4.*x[0]**2 *x[1]*x[2]] , [4.*x[0]*x[1]**2 *x[2], 4.*x[0]**2 *x[1]*x[2],6.*x[2] + 2.*x[0]**2*x[1]**2 ]]) opt1 = p.OptimizationProblem(rosen) opt2 = p.OptimizationProblem(rosen, rosen_der) opt3 = p.OptimizationProblem(F,3) for i in range(-3,3): for j in range(-3,3): x = array([i, j, 3], dtype=double) k = opt1.hessian(x) - rosen_hess(x) kk = opt2.hessian(x) - rosen_hess(x) kkk = opt3.hessian(x) - F_hessian(x) print k, abs(k) <1e-2 print kk, abs(kk) <1e-2 print kkk, abs(kkk) <1e-2 assert all( abs(k) <1e-2 ) assert all( abs(kk) <1e-2 ) assert all( abs(kkk) <1e-2 )
def test_map_pores_reverse(self): Ps = self.net2.Ps[:5] b = self.geo21.map_pores(pores=Ps, origin=self.net2) assert sp.all(b == [0, 1, 2, 3, 4]) Ps = self.net2.Ps[-5:] b = self.geo22.map_pores(pores=Ps, origin=self.net2) assert sp.all(b == [4, 5, 6, 7, 8])
def plot_saved(): import matplotlib import matplotlib.pyplot as plt data = sp.memmap(get_filename(), dtype=sp.float64, mode='r', shape=(N_samp + 2, N_steps / res_every + 1, N)) exa = data[-1] data = data[:-2] fins = sp.array([d[plot_res] for d in data if not sp.all(d[plot_res] == 0)]) nsam = len(fins) print("Samples:", nsam) av = fins.sum(axis=0) / nsam av_var = 1./(nsam - 1) / nsam * sp.sum((fins/nsam - av)**2, axis=0) av_e1 = av + sp.sqrt(av_var) av_e2 = av - sp.sqrt(av_var) plt.figure() pav = plt.plot(av, 'k-')[0] plt.plot(av_e1, 'k--') plt.plot(av_e2, 'k--') if not sp.all(exa[-1] == 0): pexa = plt.plot(exa[-1], 'r-')[0] plt.legend([pexa, pav], ["Density matrix", "Sample average"]) plt.ylim((-1, 1)) plt.xlabel(r"$n$") plt.ylabel(r"$\langle \sigma^z_n \rangle$") plt.show()
def est_condprob2(data, val, given): """Calculate the probability of P(X|Y,Z) est_condprob2(data, 'A', ['M', 'LC'])""" if not isinstance(given, list): raise IndexError("Given must be a list or tuple of givens") elif len(given) != 2: raise IndexError("I need multiple givens! Give me more...give me more!") gcols = [] for g in given: if g in ['M', 'F']: gcols.append(1) elif g in ['LC', 'SC', 'T']: gcols.append(2) elif g in ['A', 'B', 'C']: gcols.append(0) if val in ['M', 'F']: vcol = 1 elif val in ['LC', 'SC', 'T']: vcol = 2 elif val in ['A', 'B', 'C']: vcol = 0 datsize = data.shape[0] needed = [val, given[0], given[1]] t = sp.where([sp.all(data[i]==needed) for i in range(datsize)])[0] t2 = sp.where([sp.all(data[i,1:]==given) for i in range(datsize)])[0] return float(t.size)/t2.size
def ismember(element, array, rows=False): """Check if element is member of array""" if rows: return sp.any([sp.all(array[x, :] == element) for x in range(array.shape[0])]) else: return sp.all([element[i] in array for i in element.shape[0]])
def test_block_mesh_dict(self, data_field_class): field = data_field_class() offsets = data_field_class() # params = { 'convertToMeters': '0.000010000', 'numbersOfCells': '(5 10 15)', 'cellExpansionRatios': 'simpleGrading (1 2 3)', # 'boundary.left.type': 'empty', 'boundary.right.type': 'empty', 'boundary.top.type': 'wall', 'boundary.bottom.type': 'wall', 'boundary.front.type': 'wall', 'boundary.back.type': 'wall' } mesh = OpenFoam.BlockMeshDict(field, avg_fact=10.0, mesh_params=params, offset_field=offsets) mesh._edges = ['placeholder'] mesh._mergePatchPairs = ['placeholder'] mesh.write_foam_file(TEMP_DIR, overwrite=True) # # attempting to overwrite existing mesh file with pytest.raises(FileExistsError): mesh.write_mesh_file(TEMP_DIR, overwrite=False) # # writing out a symmetry plane mesh.write_symmetry_plane(TEMP_DIR, overwrite=True) # # testing generation of a thresholded mesh mesh.generate_threshold_mesh(min_value=9, max_value=90) assert sp.all(mesh.data_map[0, :] == 0) assert sp.all(mesh.data_map[9, :] == 0) assert len(mesh._blocks) == 80
def _calc_pcs(weight_dict, sids, nts, snps): num_nt_issues = 0 num_snps_used = 0 num_indivs = snps.shape[1] pcs = sp.zeros((num_indivs, 2)) for snp_i, sid in enumerate(sids): try: d = weight_dict[sid] except: continue nt = nts[snp_i] snp = snps[snp_i] pc_weights = sp.array([d['pc1w'], d['pc2w']]) pc_weights.shape = (1, 2) af = d['mean_g'] / 2.0 if sp.all([nt[1], nt[0]] == d['nts']): # print 'Flip sign' pc_weights = -pc_weights af = 1 - af elif not sp.all(nt == d['nts']): num_nt_issues += 1 continue mean_g = 2 * af sd_g = sp.sqrt(af * (1 - af)) # "Normalizing" the SNPs with the given allele frequencies norm_snp = (snp - mean_g) / sd_g norm_snp.shape = (num_indivs, 1) # Project on the PCs pcs += sp.dot(norm_snp, pc_weights) num_snps_used += 1 return {'num_snps_used': num_snps_used, 'num_nt_issues': num_nt_issues, 'pcs': pcs}
def test_assignment4(): ##Example without noise x_train = sp.array([[ 0, 0, 1 , 1],[ 0, 1, 0, 1]]) y_train = sp.array([[0, 1, 1, 2]]) w_est = train_ols(x_train, y_train) w_est_ridge = train_ols(x_train, y_train, llambda = 1) assert(sp.all(w_est.T == [[1, 1]])) assert(sp.all(w_est_ridge.T == [[.75, .75]])) y_est = apply_ols(w_est,x_train) assert(sp.all(y_train == y_est)) print 'No-noise-case tests passed' ##Example with noise #Data generation w_true = 4 X_train = sp.arange(10) X_train = X_train[None,:] Y_train = w_true * X_train + sp.random.normal(0,2,X_train.shape) #Regression w_est = train_ols(X_train, Y_train) Y_est = apply_ols(w_est,X_train) #Plot result pl.figure() pl.plot(X_train.T, Y_train.T, '+', label = 'Train Data') pl.plot(X_train.T, Y_est.T, label = 'Estimated regression') pl.xlabel('x') pl.ylabel('y') pl.legend(loc = 'lower right')
def testMomentOfInertiaRotatedEllipsoid(self): img = mango.zeros(shape=self.imgShape*2, mtype="tomo", origin=(0,0,0)) img.md.setVoxelSize((1,1,1)) img.md.setVoxelSizeUnit("mm") c = (sp.array(img.origin) + img.origin + img.shape-1)*0.5 r = sp.array(img.shape-1)*0.25 mango.data.fill_ellipsoid(img, centre=c, radius=r, fill=512) rMatrix = \ ( mango.image.rotation_matrix(-25, 2).dot( mango.image.rotation_matrix( 10, 1).dot( mango.image.rotation_matrix( 45, 0) )) ) img = mango.image.affine_transform(img, rMatrix, offset=c-img.origin, interptype=mango.image.InterpolationType.CATMULL_ROM_CUBIC_SPLINE) #mango.io.writeDds("tomoMoiRotEllipsoid.nc", img) pmoi, pmoi_axes, com = mango.image.moment_of_inertia(img) rootLogger.info("rmtx = \n%s" % (rMatrix,)) rootLogger.info("pmoi = \n%s" % (pmoi,)) rootLogger.info("pmoi_axes = \n%s" % (pmoi_axes,)) rootLogger.info("c = %s, com = %s" % (c, com)) self.assertTrue(sp.all(sp.absolute(c - com) <= 1.0e-10)) self.assertLess(pmoi[0], pmoi[1]) self.assertLess(pmoi[1], pmoi[2]) self.assertTrue(sp.all(sp.absolute(pmoi_axes[:,0]-rMatrix[:,2]) <= 1.0e-3)) self.assertTrue(sp.all(sp.absolute(pmoi_axes[:,1]-rMatrix[:,1]) <= 1.0e-3)) self.assertTrue(sp.all(sp.absolute(pmoi_axes[:,2]-rMatrix[:,0]) <= 1.0e-3))
def test_shape_3D(self): net = op.network.Cubic(shape=[5, 5, 5]) assert sp.all(net.shape == [5, 5, 5]) net = op.network.Cubic(shape=[3, 4, 5]) assert sp.all(net.shape == [3, 4, 5]) net = op.network.Cubic(shape=[1, 5, 1]) assert sp.all(net.shape == [1, 5, 1])
def check( x ): y = sl.canonicalise( x ) yr = y[0,:] yc = y[:,0] assert all( yr == sc.sort( yr ) ) assert all( yc == sc.sort( yc ) )
def test_multiphase_init(self): m = op.phases.MultiPhase(network=self.net, phases=[self.air, self.water]) assert sp.all(m['pore.occupancy.all'] == 0.0) assert sp.all(m['throat.occupancy.all'] == 0.0) assert self.air.name in m.settings['phases'] assert self.water.name in m.settings['phases']
def effectiveHeight(surf1, surf2, plasma, t, lim1=.88, lim2=.92): """ calculate the effective height of a view through the scrape-off-layer""" segments = viewPoints(surf1,surf2,plasma,t,lim1=lim1,lim2=lim2,fillorder=False) output = scipy.zeros((len(segments),)) for i in xrange(len(segments)): area = 0 if not scipy.all(segments[i] == []): inlen = geometry.pts2Vec(segments[i][0][0][0],segments[i][0][0][1]) outlen = geometry.pts2Vec(segments[i][1][0][0],segments[i][1][0][1]) for j in xrange(len(segments[i])): # loop over in vs out temp = [] for k in segments[i][j]: # loop over number of 'rays' for l in k: temp += [l.x()[[0,2]]] temp = scipy.array(temp) #delaunay for k in xrange((len(temp)-2)/2): y = temp[[0,1,2*(k+1),2*(k+1)+1]] if not(scipy.all([y[0] == y[1],y[2] == y[3]])): tri = scipy.spatial.Delaunay(y) #divide into a lower and upper section #plt.triplot(y[:,0],y[:,1],tri.vertices.copy()) for l in tri.points[tri.vertices]: area += calcArea(l) output[i] = area/(inlen.s + outlen.s) #plt.show() return output
def test_late_pore_filling(self): self.phase['pore.pc_star'] = 1000 self.phys.add_model(propname='pore.nwp_saturation', model=pm.multiphase.late_filling, Pc_star='pore.pc_star', pressure='pore.pressure') assert sp.all(self.phase['pore.nwp_saturation'] < 1.0) assert sp.all(self.phase['pore.nwp_saturation'] > 0.0)
def check( x, perm ): n = len( x ) perm_ = sl.invert_permutation( perm ) y = sl.apply_permutation( perm_, sl.apply_permutation( perm, x ) ) assert all( x == y ) z = sl.apply_permutation( perm, sl.apply_permutation( perm_, x ) ) assert all( x == z )
def isequal(A, B): if A is None or B is None: return False if not sp.all(A.shape == B.shape): return False else: return sp.all(A == B)
def isDuplicateImage(self, md, mdOther): return \ ( sp.all((md.voxelSize - mdOther.voxelSize) < 1.0e-5) and sp.all(md.voxelUnit == mdOther.voxelUnit) and sp.all(md.volumeSize == mdOther.volumeSize) )
def check( x, perm ): m, n = x.shape perm_ = sl.invert_matrix_permutation( perm ) x = sc.randn( m, n ) y = sl.apply_matrix_permutation( perm_, sl.apply_matrix_permutation( perm, x ) ) assert all( x == y ) z = sl.apply_matrix_permutation( perm, sl.apply_matrix_permutation( perm_, x ) ) assert all( x == z )
def test_channels(self): self.Data.data[...] = 10 self.DataSub.data[...] = 10 + rand.rand(*self.Data.data.shape) self.DataSub.data[:,:,:,123] = 10 + 6*rand.rand( *self.Data.data.shape[:-1]) self.DataSub.data[:,:,:,54] += 10 reflag.flag(self.Data, self.DataSub, thres=2.0, max_noise_factor=3) self.assertTrue(sp.all(self.Data.data.mask[:,:,:,123])) self.assertFalse(sp.all(self.Data.data.mask[:,:,:,54]))
def test_trim_extend(): pn = OpenPNM.Network.Cubic(shape=[5, 5, 5]) assert sp.all(sp.in1d(pn.find_neighbor_pores(pores=0), [1, 5, 25])) assert [pn.Np, pn.Nt] == [125, 300] pn.trim(pores=[0]) assert sp.all(sp.in1d(pn.find_neighbor_pores(pores=0), [1, 5, 25])) assert [pn.Np, pn.Nt] == [124, 297] pn.extend(pore_coords=[0, 0, 0], throat_conns=[[124, 0]]) assert [pn.Np, pn.Nt] == [125, 298] assert sp.all(sp.in1d(pn.find_neighbor_pores(pores=0), [1, 5, 25, 124]))
def test_times_limited_by_t_start_and_t_stop(self): t_start = 10 * pq.s t_stop = 20 * pq.s st = self.invoke_gen_func( self.defaultRate, t_start=t_start, t_stop=t_stop) self.assertTrue(sp.all(t_start < st)) self.assertTrue(sp.all(st <= t_stop)) self.assertEqual(t_start, st.t_start) self.assertEqual(t_stop, st.t_stop)
def _compare_hdf5(expected, actual): for k in expected: if isinstance(expected[k], h5py._hl.group.Group): for l in expected[k]: assert sp.all(expected[k][l][:] == actual[k][l][:]) else: if k in ['psi']: assert sp.all(expected[k][:].astype('str') == actual[k][:].astype('str')) else: assert sp.all(expected[k][:] == actual[k][:])
def test_map_pores(self): a = self.geo21['pore._id'] b = self.geo22['pore._id'] assert a.size == self.geo21.Np assert b.size == self.geo22.Np assert ~sp.any(sp.in1d(a, b)) Pgeo21 = self.net2.map_pores(pores=self.geo21.Ps, origin=self.geo21) assert sp.all(Pgeo21 == self.net2.pores(self.geo21.name)) Pgeo22 = self.net2.map_pores(pores=self.geo22.Ps, origin=self.geo22) assert sp.all(Pgeo22 == self.net2.pores(self.geo22.name))
def testCircularCylinderFill(self): dir = self.createTmpDir("testCircularCylinderFill") #dir = "." dds = mango.zeros(shape=self.shape, dtype="uint16") c = dds.shape*0.5 r = np.min(dds.shape-3)*0.5 axislen = dds.shape[0]*2 mango.data.fill_circular_cylinder(dds, c, r, axislen, fill=1) mango.io.writeDds(os.path.join(dir, "tomoCyl0.nc"), dds) dds1 = mango.zeros(shape=self.shape, dtype="uint16", origin=(100, 55, 13)) c = dds.shape*0.5 r = np.min(dds.shape-3)*0.5 axislen = dds.shape[0]*2 mango.data.fill_circular_cylinder(dds1, c, r, axislen, fill=1) mango.io.writeDds(os.path.join(dir, "tomoCyl1.nc"), dds1) self.assertTrue(sp.all(dds.subd.asarray() == dds1.subd.asarray())) del dds1 dds2 = mango.zeros(shape=self.shape, mtype="tomo", origin=(100, 55, 13)) dds2.md.setVoxelSize((0.1,0.1,0.1), "cm") c = dds.shape*0.5 r = np.min(dds.shape-3)*0.5 axislen = dds.shape[0]*2 mango.data.fill_circular_cylinder(dds2, c, r, axislen, fill=1, unit="mm") mango.io.writeDds(os.path.join(dir, "tomoCyl2.nc"), dds2) self.assertTrue(sp.all(dds.subd.asarray() == dds2.subd.asarray())) del dds2 dds3 = mango.zeros(shape=self.shape, mtype="tomo", origin=(100, 55, 13)) dds3.md.setVoxelSize((2.5,2.5,2.5), "um") c = dds.shape*0.5*dds3.md.getVoxelSize("mm") r = np.min(dds.shape-3)*0.5*dds3.md.getVoxelSize("mm")[0] axislen = dds.shape[0]*2*dds3.md.getVoxelSize("mm")[0] mango.data.fill_circular_cylinder(dds3, c, r, axislen, fill=1, unit="mm") mango.io.writeDds(os.path.join(dir, "tomoCyl3.nc"), dds3) self.assertTrue(sp.all(dds.subd.asarray() == dds3.subd.asarray())) del dds3 dds4 = mango.zeros(shape=self.shape, mtype="tomo", origin=(100, 55, 13)) dds4.md.setVoxelSize((2.5,2.5,2.5), "um") c = (dds4.origin + dds.shape*0.5)*dds4.md.getVoxelSize("mm") r = np.min(dds.shape-3)*0.5*dds4.md.getVoxelSize("mm")[0] axislen = dds.shape[0]*2*dds4.md.getVoxelSize("mm")[0] mango.data.fill_circular_cylinder(dds4, c, r, axislen, fill=1, unit="mm", coordsys="abs") mango.io.writeDds(os.path.join(dir, "tomoCyl4.nc"), dds4) self.assertTrue(sp.all(dds.subd.asarray() == dds4.subd.asarray())) del dds4
def test_multiphase_occupancy_set_two_phase(self): m = op.phases.MultiPhase(network=self.net) self.water['pore.temperature'] = 300 self.air['pore.temperature'] = 200 assert sp.all(self.water['pore.temperature'] == 300) assert sp.all(self.air['pore.temperature'] == 200) Ps = self.net['pore.coords'][:, 0] < 3 Ts = self.net.tomask(throats=self.net.find_neighbor_throats(Ps)) m.set_occupancy(phase=self.water, Pvals=Ps, Tvals=Ts) m.set_occupancy(phase=self.air, Pvals=~Ps, Tvals=~Ts) assert sp.all(m['pore.temperature'] >= 200) assert sp.all(m['pore.temperature'] <= 300)
def test_data_field(self): r""" Builds a data field and tests its properties """ # # covering basic methods map_file = 'parallel-plate-01vox.txt' fname = os.path.join(FIXTURE_DIR, 'TEST-FRACTURES', map_file) field = amt.DataField(fname) field.create_point_data() field.copy_data(field) # assert field.nx == 100 assert field.nz == 100 assert field.data_map.size == 10000 assert field.point_data.size == 40000 # # testing clone method returns proper data and class reference cloned_field = field.clone() assert isinstance(cloned_field, field.__class__) assert sp.all(cloned_field.data_map == field.data_map) assert sp.all(cloned_field._raw_data == field._raw_data) # # testing adjacency matrix matrix = field.create_adjacency_matrix() assert matrix is not None # # testing thresholding field.data_map = sp.arange(field.nz*field.nx, dtype=float).reshape(field.nz, field.nx) field.data_vector = sp.ravel(field.data_map) low_inds = sp.where(field.data_vector <= 100) high_inds = sp.where(field.data_vector >= 900) field.threshold_data(min_value=100, repl=-1) field.threshold_data(max_value=900) assert sp.all(field.data_vector[low_inds] == -1) assert sp.all(sp.isnan(field.data_vector[high_inds])) # # testing VTK file generation field.infile = os.path.join(TEMP_DIR, 'test-export.csv') field.export_vtk() fname = os.path.join(TEMP_DIR, 'test-export.vtk') assert os.path.isfile(fname) # with open(fname, 'r') as file: content = file.read() assert re.search('DATASET STRUCTURED_GRID', content) assert re.search('DIMENSIONS 101 2 101', content) assert re.search('POINTS 20402 float', content) assert re.search('CELL_DATA 10000', content) assert re.search('SCALARS data float', content) # with pytest.raises(FileExistsError): field.export_vtk()
def testDdsWriteAndRead(self): outDir = self.createTmpDir("testDdsWriteAndRead") dds = mango.data.gaussian_noise(shape=self.shape, mtype="float64", mean=0.0, stdd=20000.) outFileName = mango.io.writeDds(os.path.join(outDir, "float64Noise.nc"), dds) readDds = mango.io.readDds(outFileName, mpidims=dds.mpi.shape) self.assertTrue(sp.all(dds.asarray() == readDds.asarray())) if (mango.haveFloat16): dds = mango.data.gaussian_noise(shape=self.shape, mtype="float16", mean=0.0, stdd=2000.) outFileName = mango.io.writeDds(os.path.join(outDir, "float16Noise.nc"), dds, writehistogram=True) readDds = mango.io.readDds(outFileName, mpidims=dds.mpi.shape) self.assertTrue(sp.all(dds.asarray() == readDds.asarray()))
def test_find_connected_pores(): pn = OpenPNM.Network.Cubic(shape=(10, 10, 10)) a = pn.find_connected_pores(throats=[0, 1]) assert sp.all(a.flatten() == [0, 1, 1, 2]) a = pn.find_connected_pores(throats=[0, 1], flatten=True) assert sp.all(a == [0, 1, 2]) Tind = sp.zeros((pn.Nt,), dtype=bool) Tind[[0, 1]] = True a = pn.find_connected_pores(throats=Tind, flatten=True) assert sp.all(a == [0, 1, 2]) a = pn.find_connected_pores(throats=[], flatten=True) assert sp.shape(a) == (0, )
def test_neighbor(self): self.geo.models.add(propname='throat.seed_max', model=OpenPNM.Geometry.models.throat_seed.neighbor, mode='max') self.geo.models.add(propname='throat.seed_min', model=OpenPNM.Geometry.models.throat_seed.neighbor, mode='min') self.geo.models.add(propname='throat.seed_mean', model=OpenPNM.Geometry.models.throat_seed.neighbor, mode='mean') assert sp.all(self.geo['throat.seed_min'] <= self.geo['throat.seed_max']) assert sp.all(self.geo['throat.seed_min'] <= self.geo['throat.seed_mean']) assert sp.all(self.geo['throat.seed_mean'] <= self.geo['throat.seed_max'])
def test_two_value_conditions(self): alg = op.algorithms.GenericTransport(network=self.net, phase=self.phase) alg.settings['conductance'] = 'throat.diffusive_conductance' alg.settings['quantity'] = 'pore.mole_fraction' alg.set_value_BC(pores=self.net.pores('top'), values=1) alg.set_value_BC(pores=self.net.pores('bottom'), values=0) alg.run() x = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0] y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3)) assert sp.all(x == y)
def run_testing(cov, dmatrix0, dmatrix1, sf, options, event_type, test_idx, r_idx=None): ### estimate dispersion (disp_raw, disp_raw_conv) = estimate_dispersion(cov, dmatrix1, sf, options, test_idx, event_type) ### fit dispersion (disp_fitted, Lambda, disp_idx) = fit_dispersion(cov, disp_raw, (disp_raw_conv[:, 0] & test_idx)[:, sp.newaxis], sf, options, dmatrix1, event_type) ### adjust dispersion estimates (disp_adj, disp_adj_conv) = adjust_dispersion(cov, dmatrix1, disp_raw, disp_fitted, disp_idx, sf, options, event_type) ### do test pvals = test_count(cov, disp_adj, sf, dmatrix0, dmatrix1, options, test_idx) ### revert from unique if r_idx is not None: pvals = pvals[r_idx] ### reshape and adjust p-values pvals = pvals.reshape((2, int(pvals.shape[0] / 2))).T m_idx = sp.zeros(shape=(pvals.shape[0],), dtype='int') #for i in range(pvals.shape[0]): # if sp.all(sp.isnan(pvals[i, :])): # continue # elif sp.isnan(pvals[i, 0]): # m_idx[i] = 1 # elif sp.isnan(pvals[i, 1]): # m_idx[i] = 0 # else: # m_idx[i] = sp.argmin(pvals[i, :]) #pvals = 2 * sp.array([pvals[i, m_idx[i]] for i in range(pvals.shape[0])], dtype='float') for i in range(pvals.shape[0]): if sp.all(sp.isnan(pvals[i, :])): continue elif sp.isnan(pvals[i, 0]): #pvals[i, 1] = sp.nan m_idx[i] = 1 elif sp.isnan(pvals[i, 1]): #pvals[i, 0] = sp.nan m_idx[i] = 0 else: m_idx[i] = sp.argmax(pvals[i, :]) #m_idx[i] = sp.argmin(pvals[i, :]) #pvals[i, m_idx[i]] = min(1, 2*pvals[i, m_idx[i]]) pvals = sp.array([pvals[i, m_idx[i]] for i in range(pvals.shape[0])], dtype='float') offset = int(cov.shape[0] / 2) cov_used = sp.array([cov[i, :] if m_idx[i] == 0 else cov[i + offset, :] for i in range(pvals.shape[0])], dtype=cov.dtype) disp_raw_used = sp.array([disp_raw[i] if m_idx[i] == 0 else disp_raw[i + offset] for i in range(pvals.shape[0])], dtype=disp_raw.dtype) disp_adj_used = sp.array([disp_adj[i] if m_idx[i] == 0 else disp_adj[i + offset] for i in range(pvals.shape[0])], dtype=disp_adj.dtype) pvals[pvals > 1] = 1 return (pvals, cov_used, disp_raw_used, disp_adj_used)
def auto_truncate(self, update=True, zero_tol=None, return_update_data=False): """Automatically reduces the bond-dimension in case of rank-deficiency. Canonical form is required. Always perform self.restore_CF() first! Parameters ---------- update : bool (True) Whether to call self.update() after truncation. zero_tol : float Tolerance for interpretation of values as zero. return_update_data : bool Whether to return additional data needed to perform a minimal update. Returns ------- truncated : bool Whether truncation was performed (if return_update_data == False). data : stuff Additional data needed by self._update_after_truncate() (if return_update_data == True). """ if zero_tol is None: zero_tol = self.zero_tol new_D = self.D.copy() if self.canonical_form == 'right': for n in range(1, self.N + 1): try: ldiag = self.l[n].diag except AttributeError: ldiag = self.l[n].diagonal() new_D[n] = sp.count_nonzero(abs(ldiag) > zero_tol) else: for n in range(1, self.N + 1): try: rdiag = self.r[n].diag except AttributeError: rdiag = self.r[n].diagonal() new_D[n] = sp.count_nonzero(abs(rdiag) > zero_tol) if not sp.all(new_D == self.D): data = self.truncate(new_D, update=update, return_update_data=return_update_data) if update: self.update() if return_update_data: return data else: return True else: return False
def test_reset(self): alg = op.algorithms.GenericTransport(network=self.net, phase=self.phase) alg.settings['conductance'] = 'throat.diffusive_conductance' alg.settings['quantity'] = 'pore.mole_fraction' alg.set_rate_BC(pores=self.net.pores('bottom'), values=1) alg.set_value_BC(pores=self.net.pores('top'), values=0) alg.run() assert ~sp.all(sp.isnan(alg['pore.bc_value'])) assert ~sp.all(sp.isnan(alg['pore.bc_rate'])) assert 'pore.mole_fraction' in alg.keys() alg.reset(bcs=True, results=False) assert sp.all(sp.isnan(alg['pore.bc_value'])) assert sp.all(sp.isnan(alg['pore.bc_rate'])) assert 'pore.mole_fraction' in alg.keys() alg.reset(bcs=True, results=True) assert 'pore.mole_fraction' not in alg.keys() alg.set_rate_BC(pores=self.net.pores('bottom'), values=1) alg.set_value_BC(pores=self.net.pores('top'), values=0) alg.run()
def valid_after_filter(filtermap, filtertype, positions): """Description""" if filtertype == 'all': return not sp.all(filtermap[:, positions]) elif filtertype == 'start': return not filtermap[:, positions[0]] elif filtertype == 'any': return not sp.any(filtermap[:, positions]) else: return False
def removeDimensions(self, axis, idx): # Method to remove undesired dimensions # - axis (int): axis from where to remove the elements # - idx (numpy array): indices of the elements to remove assert axis <= len(self.dim) assert s.all(idx < self.dim[axis]) self.S.removeDimensions(axis, idx) self.W_S0.removeDimensions(axis, idx) self.W_S1.removeDimensions(axis, idx) self.updateParameters() self.updateExpectations()
def test_find_interface_throats(self): self.net['pore.domain1'] = False self.net['pore.domain2'] = False self.net['pore.domain3'] = False self.net['pore.domain1'][[0, 1, 2]] = True self.net['pore.domain2'][[100, 101, 102]] = True self.net['pore.domain3'][900:] = True a = self.net.find_interface_throats(labels=['domain1', 'domain2']) assert sp.all(a == [1800, 1801, 1802]) a = self.net.find_interface_throats(labels=['domain1', 'domain3']) assert sp.size(a) == 0
def test_one_value_one_rate(self): alg = op.algorithms.GenericTransport(network=self.net, phase=self.phase) alg.settings['conductance'] = 'throat.diffusive_conductance' alg.settings['quantity'] = 'pore.mole_fraction' alg.set_rate_BC(pores=self.net.pores('bottom'), values=1) alg.set_value_BC(pores=self.net.pores('top'), values=0) alg.run() x = [0., 1., 2., 3., 4., 5., 6., 7., 8.] y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3)) assert sp.all(x == y)
def test_static_pressure(self): self.water['pore.density'] = 1000 self.water['pore.occupancy'] = True f = OpenPNM.Physics.models.capillary_pressure.static_pressure self.phys.models.add(propname='pore.static_pressure', model=f, gravity=[0, 0, 9.81], pore_density='pore.density', pore_occupancy='pore.occupancy') a = [0., 9810., 19620.] assert sp.all(sp.unique(self.water['pore.static_pressure']) == a)
def effectiveHeight(surf1, surf2, plasma, t, lim1=.88, lim2=.92): """ calculate the effective height of a view through the scrape-off-layer""" segments = viewPoints(surf1, surf2, plasma, t, lim1=lim1, lim2=lim2, fillorder=False) output = scipy.zeros((len(segments), )) for i in range(len(segments)): area = 0 if not scipy.all(segments[i] == []): inlen = geometry.pts2Vec(segments[i][0][0][0], segments[i][0][0][1]) outlen = geometry.pts2Vec(segments[i][1][0][0], segments[i][1][0][1]) for j in range(len(segments[i])): # loop over in vs out temp = [] for k in segments[i][j]: # loop over number of 'rays' for l in k: temp += [l.x()[[0, 2]]] temp = scipy.array(temp) #delaunay for k in range(old_div((len(temp) - 2), 2)): y = temp[[0, 1, 2 * (k + 1), 2 * (k + 1) + 1]] if not (scipy.all([y[0] == y[1], y[2] == y[3]])): tri = scipy.spatial.Delaunay( y) #divide into a lower and upper section #plt.triplot(y[:,0],y[:,1],tri.vertices.copy()) for l in tri.points[tri.vertices]: area += calcArea(l) output[i] = old_div(area, (inlen.s + outlen.s)) #plt.show() return output
def testDdsMetaData(self): dds = mango.zeros(shape=self.shape, dtype="uint16") self.assertNotEqual(None, dds.md) rootLogger.info( "dds.md.getVoxelSize()=%s, dds.md.getVoxelSizeUnit()=%s" % (dds.md.getVoxelSize(), dds.md.getVoxelSizeUnit())) vSz = (0.5, 0.125, 0.25) dds.md.setVoxelSize(vSz) dds.md.setVoxelSizeUnit("um") rootLogger.info( "dds.md.getVoxelSize()=%s, dds.md.getVoxelSizeUnit()=%s" % (dds.md.getVoxelSize(), dds.md.getVoxelSizeUnit())) self.assertEqual("micrometre", dds.md.getVoxelSizeUnit()) self.assertTrue(sp.all(dds.md.getVoxelSize() - vSz < 1.0e-6)) dds.md.setVoxelSize(1) dds.md.setVoxelSizeUnit("millimetre") rootLogger.info( "dds.md.getVoxelSize()=%s, dds.md.getVoxelSizeUnit()=%s" % (dds.md.getVoxelSize(), dds.md.getVoxelSizeUnit())) self.assertEqual("millimetre", dds.md.getVoxelSizeUnit()) self.assertTrue(sp.all(dds.md.getVoxelSize() == 1))
def test_subdict_getitem_on_two_geometries(self): pn = op.network.Cubic(shape=[5, 5, 5]) geo1 = op.geometry.GenericGeometry(network=pn, pores=pn.Ps[:75], throats=pn.Ts[:75]) geo2 = op.geometry.GenericGeometry(network=pn, pores=pn.Ps[75:], throats=pn.Ts[75:]) geo1['pore.foo.bar'] = 1 geo1['pore.foo.baz'] = 2 d = pn['pore.foo'] assert len(d) == 2 assert 'pore.foo.bar' in d.keys() assert 'pore.foo.baz' in d.keys() assert sp.any(sp.isnan(d['pore.foo.bar'])) assert sp.any(sp.isnan(d['pore.foo.baz'])) geo2['pore.foo.bar'] = 1 geo2['pore.foo.baz'] = 2 d = pn['pore.foo'] assert sp.all(~sp.isnan(d['pore.foo.bar'])) assert sp.all(~sp.isnan(d['pore.foo.baz']))
def import_meas_data(self): """ Import measured data from file """ Tt_meas_data = load_meas_file(self.meas_file) self.meas_data = Tt_meas_data[:, 1:] assert (sp.all(Tt_meas_data[:, 0] == self.Tt)), "Tt vector in "\ "measured data file must be same as Tt" assert self.meas_data.shape[1] == len(self.L_idxs), "Dimension of "\ "imported measurement vectors must be same as length of L_idxs"
def test_load_Berea(self): path = Path(os.path.realpath(__file__), '../../../fixtures/ICL-Sandstone(Berea)') project = op.io.Statoil.load(path=path, prefix='Berea') assert len(project) == 1 net = project.network assert net.Np == 6298 assert net.Nt == 12098 assert sp.shape(net['pore.coords']) == (6298, 3) assert sp.shape(net['throat.conns']) == (12098, 2) assert 'pore.radius' in net.keys() assert sp.all(net.find_neighbor_pores(pores=1000) == [221, 1214])
def test_product(self): self.geo.add_model(model=mods.constant, propname='pore.value1', value=2) self.geo.add_model(model=mods.constant, propname='pore.value2', value=2) self.geo.add_model(model=mods.product, propname='pore.result1', prop1='pore.value1', prop2='pore.value2') assert sp.all(sp.unique(self.geo['pore.result1']) == 4) self.geo.add_model(model=mods.constant, propname='pore.value3', value=2) self.geo.add_model(model=mods.product, propname='pore.result2', prop1='pore.value1', prop2='pore.value2', prop3='pore.value3') assert sp.all(sp.unique(self.geo['pore.result2']) == 8)
def test_transient_steady_mode_reactive_transport(self): alg = op.algorithms.TransientReactiveTransport(network=self.net, phase=self.phase, settings=self.settings) alg.settings.update({'t_scheme': 'steady', 'rxn_tolerance': 1e-06}) alg.set_IC(0) alg.set_value_BC(pores=self.net.pores('left'), values=2) alg.set_source(propname='pore.reaction', pores=self.net.pores('right')) alg.run() x = [2, 1.00158, 0.00316, 2, 1.00158, 0.00316, 2, 1.00158, 0.00316] y = sp.around(alg[alg.settings['quantity']], decimals=5) assert sp.all(x == y)
def testKernelCoeffs(self): for scale in [0.35, 0.5, 0.75, 1]: for dim in [0, 1, 2, 3, 4]: dgK = mango.image.discrete_gaussian_kernel(sigma=scale, dim=dim, errtol=0.001) self.assertAlmostEqual(1.0, sp.sum(dgK), 8) mxElem = sp.argmax(dgK) self.assertTrue( sp.all( sp.array(dgK.shape) // 2 == sp.unravel_index(mxElem, dgK.shape)))
def twinsurr(X, m=1, t=1, e=0.1, nSurr=100, RP=None): """ Returns Twin Surrogates (TS) based on RP with FAN metric. X := time series m := dimension of embedding (default = 1) t := time delay of embedding (default = 1) e := recurrence threshold (default = 0.1) nSurr := number of Surrogates (default = 100) RP := recurrence Plot of X Output: S := matrix where each column is a TS nTwins := number of twins found twinMat := matrix of twin pairs twinMat[i, j] = 1 => twins """ if RP is None: RP = rpfan(X, m, t, e) nX = len(RP) print 'Searching for twins...' twinMat = sparse.lil_matrix((nX, nX), dtype=sp.int8) pbar = ProgressBar(maxval=nX).start() for j in range(nX): i = sp.tile(RP[:, j], (nX, 1)).T i = sp.all(RP == i, axis=0) * any(RP[:, j]) twinMat[i, j] = 1 pbar.update(j + 1) pbar.finish() nTwins = sum(sp.any((twinMat - sparse.eye(nX, nX)).toarray(), axis=0)) if nTwins == 0: print 'Warning: No twins detected!' print 'Surrogates are same as original time series!' S = sp.empty((nX, nSurr)) print 'Creating surrogates...' pbar = ProgressBar(maxval=nSurr).start() for i in range(nSurr): k, l = 0, sp.ceil(nX * sp.rand()) - 1 k, l = int(k), int(l) S[k, i] = X[l] while k < nX - 1: twins = sp.where(twinMat[:, l].toarray().squeeze())[0] if len(twins) > 1: idx = int(sp.ceil(len(twins) * sp.rand()) - 1) l = twins[idx] l += 1 if l > nX - 1: while l > nX - 1: l = sp.ceil(nX * sp.rand()) - 1 l = int(l) S[k + 1, i] = X[l] k += 1 pbar.update(i + 1) pbar.finish() return S, nTwins, twinMat.toarray()
def add_intron(self, idx1, flag1, idx2, flag2): """adds new introns into splicegraph between idx1 and idx2""" ### if flag1, all end terminal exons in idx1 are preserved ### if flag2, all start terminal exons in idx2 are preserved if idx2.shape[0] > 0: adj_mat = sp.triu(self.edges) if flag1: for i1 in idx1: ### if exon is end-terminal if sp.all(adj_mat[i1, :] == 0): self.vertices = sp.c_[self.vertices, self.vertices[:, i1]] self.new_edge() self.edges[:, -1] = self.edges[:, i1] self.edges[-1, :] = self.edges[i1, :] self.terminals = sp.c_[self.terminals, self.terminals[:, i1]] if flag2: for i2 in idx2: ### if exon is start-terminal if sp.all(adj_mat[:, i2] == 0): self.vertices = sp.c_[self.vertices, self.vertices[:, i2]] self.new_edge() self.edges[:, -1] = self.edges[:, i2] self.edges[-1, :] = self.edges[i2, :] self.terminals = sp.c_[self.terminals, self.terminals[:, i2]] for i1 in idx1: for i2 in idx2: self.edges[i1, i2] = 1 self.edges[i2, i1] = 1 self.uniquify()
def test_process_data(self, data_field_class): r""" Testing the results of getting data vectors in multiple directions """ fmt = '{:4.2f}' prof = Profile(data_field_class()) prof.args = {'axis': 'x', 'locations': [0, 50, 100]} prof._process_data() assert sp.all( prof.processed_data[fmt.format(0)] == prof.data_map[0, :]) assert sp.all( prof.processed_data[fmt.format(50)] == prof.data_map[5, :]) assert sp.all( prof.processed_data[fmt.format(100)] == prof.data_map[9, :]) # prof.args = {'axis': 'z', 'locations': [0, 50, 100]} prof._process_data() assert sp.all(prof.processed_data[fmt.format(0)] == prof.data_map[:, 0]) assert sp.all(prof.processed_data[fmt.format(50)] == prof.data_map[:, 5]) assert sp.all(prof.processed_data[fmt.format(100)] == prof.data_map[:, 9]) # prof.args = {'axis': 'y', 'locations': [0, 50, 100]} prof._process_data() assert prof.processed_data is None
def test_residual_and_lpf(): phys.models.add(propname='pore.fractional_filling', model=OpenPNM.Physics.models.multiphase.late_pore_filling, Pc=0, Swp_star=0.2, eta=1) phys.models.add(propname='throat.fractional_filling', model=OpenPNM.Physics.models.multiphase.late_throat_filling, Pc=0, Swp_star=0.2, eta=1) phys.regenerate() drainage.setup(invading_phase=water, defending_phase=air, pore_filling='pore.fractional_filling', throat_filling='throat.fractional_filling') drainage.set_inlets(pores=pn.pores('boundary_top')) resPs = pn.pores('internal')[sp.random.random(len(pn.pores('internal')))<0.1] resTs = pn.throats('internal')[sp.random.random(len(pn.throats('internal')))<0.1] drainage.set_residual(pores=resPs, throats=resTs) drainage.run() drainage.return_results(Pc=5000) data = drainage.get_drainage_data() assert sp.all(water["pore.partial_occupancy"][resPs] == 1.0) assert sp.all(water["throat.partial_occupancy"][resTs] == 1.0) assert sp.amin(data['invading_phase_saturation']) > 0.0 assert sp.amax(data['invading_phase_saturation']) < 1.0 assert sp.all(water["pore.occupancy"]+air["pore.occupancy"] == 1.0) total_pp = water["pore.partial_occupancy"]+air["pore.partial_occupancy"] assert sp.all(total_pp == 1.0) assert sp.all(water["throat.occupancy"]+air["throat.occupancy"] == 1.0) total_pt = water["throat.partial_occupancy"]+air["throat.partial_occupancy"] assert sp.all(total_pt == 1.0)
def testSphericalHistogram(self): #tmpDir = "." tmpDir = self.createTmpDir("testSphericalHistogram") img, c, r = createPhantom(self.imgShape, voidFill=mango.mtype("tomo").maskValue()) mango.io.writeDds(os.path.join(tmpDir, "tomoPhantomHead.nc"), img) sphhist = mango.image.spherical_histogram(sphere_c=c - img.origin, sphere_r=1) mango.image.intensity_spherical_histogram(img, sphhist) sphhist.reduce(img.mpi.comm) sphhistcpy = copy.deepcopy(sphhist) self.assertTrue( sp.all(sphhist.getBinCounts() == sphhistcpy.getBinCounts())) self.assertTrue( sp.all(sphhist.getBinCentres() == sphhistcpy.getBinCentres())) tmp_c = sphhistcpy.sphere_c sphhistcpy.sphere_c = tmp_c + sp.array( (0.25, 0.5, 0.125)) * 0.33333333333333 self.assertEqual(sphhist.getBinCentres().size, sphhistcpy.getBinCentres().size) self.assertEqual(sphhist.getBinAreas().size, sphhistcpy.getBinAreas().size) self.assertTrue( sp.all( sp.sqrt((sphhistcpy.getBinCentres() - sphhistcpy.sphere_c)**2) <= sphhistcpy.sphere_r)) self.assertTrue( sp.all( sp.absolute( (sphhistcpy.getBinCentres() - sphhistcpy.sphere_c) - (sphhist.getBinCentres() - sphhist.sphere_c)) <= 1.0e-12))
def load_snp_data(snpreader, pheno_fn, cov_fn=None, offset=True, mpheno=0, standardizer=Unit()): """Load plink files ---------- snpreader : snpreader object object to read in binary SNP file pheno_fn : str File name of phenotype file cov_fn : str File name of covariates file offset : bool, default=True Adds offset to the covariates specified in cov_fn, if neccesssary Returns ------- G : array, shape = [n_samples, n_features] SNP matrix X : array, shape = [n_samples, n_covariates] Matrix of covariates (e.g. age, gender) y : array, shape = [n_samples] Phenotype (target) vector """ #TODO: completely remove this pheno = pstpheno.loadOnePhen(pheno_fn,mpheno, vectorize=True) geno = snpreader.read(order='C').standardize(standardizer) # sanity check #assert np.testing.assert_array_equal(ind_iid, pheno['iid'][indarr[:,0]]) # load covariates or generate vector of ones (for bias) if cov_fn == None: cov = {'vals': np.ones((len(pheno['iid']), 1)), 'iid':pheno['iid']} else: cov = pstpheno.loadPhen(cov_fn) (y, yiid), G, (X, xiid) = pstutil.intersect_apply([(pheno['vals'],pheno['iid']), geno, (cov['vals'],cov['iid'])], sort_by_dataset=False) G = G.read(order='C', view_ok=True) # add bias column if not present if offset and sp.all(X.std(0)!=0): offset = sp.ones((len(indarr),1)) X = sp.hstack((X,offset)) return G, X, y
def run(self, n_steps=None, **kwargs): r""" Perform the algorithm Parameters ---------- n_steps : int The number of throats to invaded during this step """ if 'throat.entry_pressure' not in self.keys(): self.setup(**kwargs) if sp.all(self['pore.invaded'] == -1): self.set_inlets(**kwargs) if n_steps is None: n_steps = sp.inf queue = self.queue if len(queue) == 0: logger.warn('queue is empty, this network is fully invaded') return t_sorted = self['throat.sorted'] t_order = self['throat.order'] t_inv = self['throat.invaded'] p_inv = self['pore.invaded'] count = 0 while (len(queue) > 0) and (count < n_steps): # Find throat at the top of the queue t = hq.heappop(queue) # Extract actual throat number t_next = t_sorted[t] t_inv[t_next] = self._tcount # If throat is duplicated while len(queue) > 0 and queue[0] == t: # Note: Preventing duplicate entries below might save some time here t = hq.heappop(queue) # Find pores connected to newly invaded throat Ps = self._net['throat.conns'][t_next] # Remove already invaded pores from Ps Ps = Ps[p_inv[Ps] < 0] if len(Ps) > 0: p_inv[Ps] = self._tcount Ts = self._net.find_neighbor_throats(pores=Ps) Ts = Ts[t_inv[Ts] < 0] # Remove already invaded throats from Ts [hq.heappush(queue, T) for T in t_order[Ts]] count += 1 self._tcount += 1 self['throat.invasion_sequence'] = t_inv self['pore.invasion_sequence'] = p_inv
def updateDim(self, axis, new_dim, m=None): """Method to update the dimensionality of the node PARAMETERS ---------- axis: int new_dim: int m: iterable views to update """ assert s.all(m in self.activeM), "Trying to update the dimensionality of a node that doesnt exist in a view" M = self.activeM if m is None else m for m in M: self.nodes[m].updateDim(axis,new_dim)
def add_intron_retention(self, idx1, idx2): adj_mat = sp.triu(self.edges) self.vertices = sp.c_[self.vertices, sp.array([self.vertices[0, idx1], self.vertices[1, idx2]], dtype='int')] self.new_edge() adj_mat = sp.r_[adj_mat, sp.zeros((1, adj_mat.shape[1]), dtype='int')] adj_mat = sp.c_[adj_mat, sp.zeros((adj_mat.shape[0], 1), dtype='int')] ### check if adjacency matrix is symmetric ### otherwise or is not justyfied assert(sp.all(sp.all(adj_mat - (self.edges - adj_mat).T == 0))) ### AK: under the assumption that our splice graph representation is symmetric ### I preserve symmetry by using OR over the adj_mat column and row self.edges[:, -1] = adj_mat[:, idx1] | adj_mat[idx2, :].T self.edges[-1, :] = adj_mat[:, idx1].T | adj_mat[idx2, :] self.terminals = sp.c_[self.terminals, sp.array([self.terminals[0, idx1], self.terminals[1, idx2]], dtype='int')]
def testDdsWriteAndRead(self): outDir = self.createTmpDir("testDdsWriteAndRead") dds = mango.data.gaussian_noise(shape=self.shape, mtype="float64", mean=0.0, stdd=20000.) outFileName = mango.io.writeDds( os.path.join(outDir, "float64Noise.nc"), dds) readDds = mango.io.readDds(outFileName, mpidims=dds.mpi.shape) self.assertTrue(sp.all(dds.asarray() == readDds.asarray())) if (mango.haveFloat16): dds = mango.data.gaussian_noise(shape=self.shape, mtype="float16", mean=0.0, stdd=2000.) outFileName = mango.io.writeDds(os.path.join( outDir, "float16Noise.nc"), dds, writehistogram=True) readDds = mango.io.readDds(outFileName, mpidims=dds.mpi.shape) self.assertTrue(sp.all(dds.asarray() == readDds.asarray()))
def test_from_neighbor_throats_max(self): self.geo.pop('pore.seed', None) self.geo.models.pop('pore.seed', None) self.geo.models.pop('throat.seed', None) self.geo['throat.seed'] = sp.rand(self.net.Nt, ) self.geo.add_model(model=mods.from_neighbor_throats, propname='pore.seed', throat_prop='throat.seed', mode='max') assert sp.all(sp.in1d(self.geo['pore.seed'], self.geo['throat.seed'])) pmin = sp.amin(self.geo['pore.seed']) tmin = sp.amin(self.geo['throat.seed']) assert pmin >= tmin
def load_covariates(self, pheno): if self.cov_fn == None: cov_iid = pheno['iid'] X = np.ones((len(cov_iid), 1)) else: cov = pstpheno.loadPhen(self.cov_fn) X = cov['vals'] cov_iid = cov['iid'] # add bias column if not present - #!! LATER -- Bug? should this test be done after intersection in case removing an iid makes it constant? if self.offset and sp.all(X.std(0) != 0): offset = sp.ones((len(X), 1)) self.X = sp.hstack((X, offset)) return X, cov_iid
def getSoln(self, wsize=10, winterval=1, abstol=1e-6, reltol=1e-6): """Keep performing SGD steps until: afunc.feval(x*_prev) and afunc.feval(x*) are within the specified tolerances. x* -- is a solution obtained from averaging the last wsize points. x*_prev -- is a solution obtained by averaging the wsize points that were wsize*(winterval_1) back in history. Intuitively, this function keeps performing steps until "the objective value" doesn't change much. Be careful because it involves calls to afunc.feval that may be slow.""" nreq = wsize * (winterval + 2) fold = abstol fnew = -abstol abstest = 0 reltestmore = 0 reltestless = 0 def step(): self.nsteps(nreq) xold = self.getAvgSoln(wsize) xnew = scipy.average(self.x_hist[-nreq:(-nreq + wsize)], axis=0) fold = self.afunc.feval(xold) fnew = self.afunc.feval(xnew) return (fold, fnew) while abstest != 1: fold, fnew = step() if scipy.all(scipy.absolute(fold - fnew) < abstol): abstest += 1 while reltestmore != 1: fold, fnew = step() if scipy.all(float(fold + 1e-11) / (fnew - 1e-11) < 1 + reltol): reltestmore += 1 while reltestless != 1: fold, fnew = step() if scipy.all(float(fold + 1e-11) / (fnew - 1e-11) < 1 - reltol): reltestless += 1 return self.stepcount