Beispiel #1
0
def test_point_point():
    p1 = Point(0,0)
    p2 = Point(1,1)

    ok_(p1.geom_type=='Point', p1.geom_type)
    assert_ae(p1.coords[:][0], (0,0))

    d = p1.distance(p2)
    assert_eq(d, sqrt(2))
Beispiel #2
0
    def test_2(self):
        """test write FrameArray"""
        farray = FrameArray("data/md1_prod.Tc5b.x",
                            "./data/Tc5b.top",
                            indices=range(10))
        writetraj("test_write_output.x", farray, farray.top, overwrite=True)
        writetraj("test_pdb_1.dummyext",
                  farray[0],
                  farray.top,
                  overwrite=True,
                  fmt='pdb')

        # test 'save'
        print farray
        farray.save("test_write_output_save_method.x", overwrite=True)

        # reproduce result?
        f0 = mdio.load("test_write_output.x", "./data/Tc5b.top")
        f1 = mdio.load("test_write_output_save_method.x", "./data/Tc5b.top")
        from numpy.testing import assert_almost_equal as assert_ae
        assert_ae(f0[:, :, :], f1[:, :, :])
read_c = f.variables['vc'][:]
read_f = f.variables['vf'][:]
print 'read_c.shape', read_c.shape
print 'read_f.shape', read_f.shape

read_f2 = f.variables['vf'][:].reshape((dim_nx, dim_ny), order='F')
read_f3 = numpy.zeros((dim_nx, dim_ny), order='F')
read_f3[:] = f.variables['vf'][:]

print "read_c.ravel('A')", read_c.ravel('A')
print "read_c.ravel('C')", read_c.ravel('C')
print "read_c.ravel('F')", read_c.ravel('F')
print '-'*80
print "read_f.ravel('A')", read_f.ravel('A')
print "read_f.ravel('C')", read_f.ravel('C')
print "read_f.ravel('F')", read_f.ravel('F')
print '-'*80
print "read_f2.ravel('A')", read_f2.ravel('A')
print "read_f2.ravel('C')", read_f2.ravel('C')
print "read_f2.ravel('F')", read_f2.ravel('F')
print '-'*80
print "read_f3.ravel('A')", read_f3.ravel('A')
print "read_f3.ravel('C')", read_f3.ravel('C')
print "read_f3.ravel('F')", read_f3.ravel('F')
print '-'*80

assert_ae(data_c.ravel('A'), read_c.ravel('A'))
assert_ae(data_f.ravel('A'), read_f3.ravel('A'))

f.close()
Beispiel #4
0
def test_combine_misfits(dump=False, reference=None):
    source, targets = scenario('wellposed', 'noisefree')

    p = ToyProblem(name='toy_problem',
                   ranges={
                       'north': gf.Range(start=-10., stop=10.),
                       'east': gf.Range(start=-10., stop=10.),
                       'depth': gf.Range(start=0., stop=10.)
                   },
                   base_source=source,
                   targets=targets)

    ngx, ngy, ngz = 11, 11, 11
    xg = num.zeros((ngz * ngy * ngx, 3))

    xbounds = p.get_parameter_bounds()
    cx = num.linspace(xbounds[0][0], xbounds[0][1], ngx)
    cy = num.linspace(xbounds[1][0], xbounds[1][1], ngy)
    cz = num.linspace(xbounds[2][0], xbounds[2][1], ngz)

    xg[:, 0] = num.tile(cx, ngy * ngz)
    xg[:, 1] = num.tile(num.repeat(cy, ngx), ngz)
    xg[:, 2] = num.repeat(cz, ngx * ngy)

    misfitss = p.misfits_many(xg)
    # misfitss[imodel, itarget, 0], misfitss[imodel, itarget, 1]
    gms = p.combine_misfits(misfitss)
    gms_contrib = p.combine_misfits(misfitss, get_contributions=True)

    # gms[imodel]
    # gms_contrib[imodel, itarget]

    bweights = num.ones((2, p.ntargets))
    gms_2 = p.combine_misfits(misfitss, extra_weights=bweights)
    gms_2_contrib = p.combine_misfits(misfitss,
                                      extra_weights=bweights,
                                      get_contributions=True)

    if dump:
        num.savez(dump, gms, gms_contrib, gms_2)

    # gms_2[imodel, ibootstrap]
    # gms_2_contrib[imodel, ibootstrap, itarget]

    for ix, x in enumerate(xg):
        misfits = p.misfits(x)
        # misfits[itarget, 0], misfits[itarget, 1]
        gm = p.combine_misfits(misfits)
        # gm is scalar
        t.assert_equal(gm, gms[ix])

        gm_contrib = p.combine_misfits(misfits, get_contributions=True)

        assert_ae(gms_contrib[ix, :], gm_contrib)

        gm_2 = p.combine_misfits(misfits, extra_weights=bweights)

        assert gm_2[0] == gm
        assert gm_2[1] == gm
        assert gms_2[ix, 0] == gm
        assert gms_2[ix, 1] == gm

        gm_2_contrib = p.combine_misfits(misfits,
                                         extra_weights=bweights,
                                         get_contributions=True)

        assert_ae(gm_2_contrib[0, :], gm_contrib)
        assert_ae(gm_2_contrib[1, :], gm_contrib)
        assert_ae(gms_2_contrib[ix, 0, :], gm_contrib)
        assert_ae(gms_2_contrib[ix, 1, :], gm_contrib)

    if reference:
        ref_data = num.load(reference)

        assert_ae(ref_data['arr_0'], gms)
        assert_ae(ref_data['arr_1'], gms_contrib)
        assert_ae(ref_data['arr_2'], gms_2)
Beispiel #5
0
# dataset
h5f.create_dataset('arr_a', data=arr_a, compression='gzip')
h5f.create_dataset('arr_b', data=arr_b, compression='gzip')

h5f.close()



#------------------------------------------------------------------------------
# read a nc file
#------------------------------------------------------------------------------
h5f = h5.File('write_read.h5', 'r')

print '='*80
print 'description', h5f.attrs['description']
print 'size', h5f.attrs['size']

read_a = h5f['arr_a'].value
read_b = h5f['arr_b'].value

print 'read arrays'
pprint(read_a)
pprint(read_b)


# verification
assert_ae(read_a, arr_a)
assert_ae(read_b, arr_b)

h5f.close()
block = (256, 1, 1)
grid = (nx // 256 + 1, 1)


# ------------------------------------------------------------------------------
# Compare
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Multiply
print "Multiply"
c[:] = a * b

# assert_ae(c, c_f90)
multiply_f90(a_f90, b_f90, c_f90)
assert_ae(c, c_f90)

# assert_ae(c, c_from_gpu)
multiply_gpu(numpy.int32(nx), a_gpu, b_gpu, c_gpu, block=block, grid=grid)
cuda.memcpy_dtoh(c_from_gpu, c_gpu)
assert_ae(c, c_from_gpu)


# ------------------------------------------------------------------------------
# Add
print "Add"
c[:] = a + b

# assert_ae(c, c_f90)
add_f90(a_f90, b_f90, c_f90)
assert_ae(c, c_f90)
Beispiel #7
0
import numpy
from numpy.testing import assert_array_equal as assert_ae
from numpy.testing import assert_array_almost_equal as assert_aae


TOL = numpy.finfo(numpy.float64).eps



#------------------------------------------------------------------------------
# Setup 
# 4x4 matrix
A = numpy.array([[10,-1,2,0], [-1,11,-1,3], [2,-1,10,-1], [0,3,-1,8]])
b = numpy.array([6,25,-11,15])
x_exact = (1,2,-1,1)
assert_ae(numpy.dot(A, x_exact), b)



#------------------------------------------------------------------------------
# numpy solve
x = numpy.linalg.solve(A, b)
assert_ae(x, x_exact)



#==============================================================================
# Gauss-Seidel method
#==============================================================================
# naive implementation
x = numpy.zeros(4)
Beispiel #8
0
    # setup
    nx = 1000000
    a = numpy.random.rand(nx)
    b = numpy.random.rand(nx)
    c = numpy.zeros(nx)


    #----------------------------------------------------------------------
    # Fortran code
    #----------------------------------------------------------------------
    src_f90 = '''
    SUBROUTINE add(nx, a, b, c)
      IMPLICIT NONE
      INTEGER, INTENT(IN) :: nx
      DOUBLE PRECISION, DIMENSION(nx), INTENT(IN) :: a, b
      DOUBLE PRECISION, DIMENSION(nx), INTENT(INOUT) :: c

      INTEGER :: ii

      DO ii=1,nx
        c(ii) = a(ii) + b(ii)
      END DO
    END SUBROUTINE
    '''

    mod = get_module_f90(src_f90)
    add_f90 = mod.add

    add_f90(a, b, c)
    assert_ae(a+b, c)
Beispiel #9
0
from __future__ import division
import numpy
from numpy.testing import assert_array_equal as assert_ae



# 2x2
A = numpy.array([[3,1], [1,2]])
b = numpy.array([9,8])
x = numpy.linalg.solve(A, b)

print x
assert_ae(x, (2,3))
assert_ae(numpy.dot(A, x), b)



# 4x4
A = numpy.array([[10,-1,2,0], [-1,11,-1,3], [2,-1,10,-1], [0,3,-1,8]])
b = numpy.array([6,25,-11,15])
x = numpy.linalg.solve(A, b)

print x
assert_ae(x, (1,2,-1,1))
assert_ae(numpy.dot(A, x), b)