How to use the molgrid.Grid2f function in molgrid

To help you get started, we’ve selected a few molgrid examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gnina / libmolgrid / test / test_numpy.py View on Github external
def test_numpy():
  mg3d = molgrid.MGrid3d(3,3,3)
  mg2f = molgrid.MGrid2f(2,4)
  
  a2f = np.arange(27).astype(np.float32).reshape(3,-1)
  a3d = np.arange(27).astype(np.float).reshape(3,3,-1)

  g2f = molgrid.Grid2f(a2f)
  g3d = molgrid.Grid3d(a3d)
  
  g2f[0,0] = 100
  g3d[0,0,0] = 101
        
  assert a2f[0,0] == 100
  assert a3d[0,0,0] == 101
  
  mg2f.copyFrom(g2f)
  mg3d.copyFrom(g3d)
  
  assert mg2f[1,3] == 7
  
  mg2f[1,3] = 200
  assert g2f[1,3] == 12
  assert a2f[1,3] == 12
github gnina / libmolgrid / test / test_gridmaker.py View on Github external
np.testing.assert_allclose(g[1,:],v2g[1,:]*2.0,atol=1e-5)
    
    vgridgpu = molgrid.MGrid4f(*shape)
    v2gridgpu = molgrid.MGrid4f(*shape)
    g1.forward((0,0,0),vcoords, vgridgpu.gpu())
    g1.forward((0,0,0),v2coords, v2gridgpu.gpu())
    
    np.testing.assert_allclose(reference.tonumpy(),vgridgpu.tonumpy(),atol=1e-5)
    v2gpu = v2gridgpu.tonumpy()
    
    np.testing.assert_allclose(g[0,:],v2gpu[0,:]*2.0,atol=1e-5)
    np.testing.assert_allclose(g[1,:],v2gpu[1,:]*2.0,atol=1e-5)    
    
    #create target grid with equal type density at 1,0,0
    tc = molgrid.Grid2f(np.array([[1,0,0]],np.float32))
    tv = molgrid.Grid2f(np.array([[0.5,0.5]],np.float32))
    tr = molgrid.Grid1f(np.array([1.0],np.float32))
    targetc = molgrid.CoordinateSet(tc,tv,tr)
    tgrid = molgrid.MGrid4f(*shape)
    g1.forward((0,0,0),targetc,tgrid.cpu())
    
    gradc = molgrid.MGrid2f(2,3)
    gradt = molgrid.MGrid2f(2,2)
    g1.backward((0,0,0),vcoords,tgrid.cpu(),gradc.cpu(),gradt.cpu())
    assert gradc[0,0] == approx(-gradc[1,0],abs=1e-4)
    assert gradc[0,0] > 0
    
    gradc.fill_zero()
    gradt.fill_zero()
    g1.backward((0,0,0),vcoords,tgrid.gpu(),gradc.gpu(),gradt.gpu())

    assert gradc[0,0] == approx(-gradc[1,0],abs=1e-4)
github gnina / libmolgrid / test / test_gridmaker.py View on Github external
def test_vector_types():
    g1 = molgrid.GridMaker(resolution=.25,dimension=6.0)
    c = np.array([[0,0,0],[2,0,0]],np.float32)
    t = np.array([0,1],np.float32)
    vt = np.array([[1.0,0],[0,1.0]],np.float32)
    vt2 = np.array([[0.5,0.0],[0.0,0.5]],np.float32)
    r = np.array([1.0,1.0],np.float32)
    coords = molgrid.CoordinateSet(molgrid.Grid2f(c),molgrid.Grid1f(t),molgrid.Grid1f(r),2)
    vcoords = molgrid.CoordinateSet(molgrid.Grid2f(c),molgrid.Grid2f(vt),molgrid.Grid1f(r))
    v2coords = molgrid.CoordinateSet(molgrid.Grid2f(c),molgrid.Grid2f(vt2),molgrid.Grid1f(r))

    shape = g1.grid_dimensions(2)
    reference = molgrid.MGrid4f(*shape)
    vgrid = molgrid.MGrid4f(*shape)
    v2grid = molgrid.MGrid4f(*shape)
    v3grid = molgrid.MGrid4f(*shape)
    
    g1.forward((0,0,0),coords, reference.cpu())
    g1.forward((0,0,0),vcoords, vgrid.cpu())
    g1.forward((0,0,0),v2coords, v2grid.cpu())
    g1.forward((0,0,0),c,vt,r, v3grid.cpu())        
    np.testing.assert_allclose(reference.tonumpy(),vgrid.tonumpy(),atol=1e-5)
    np.testing.assert_allclose(vgrid.tonumpy(),v3grid.tonumpy(),atol=1e-6)
    
    v2g = v2grid.tonumpy()
    g = reference.tonumpy()
github gnina / libmolgrid / test / test_grid.py View on Github external
def test_grid():
    g = molgrid.MGrid2f(10,2)
    assert g.size() == 20
    g2 = molgrid.Grid2f(g.cpu())
    g2[5][1] = 3.0
    assert g[5,1] == g2[5,1]
    g1 = g2[5]
    g1[1] = 4.0
    assert g[5,1] == 4.0
    
    gclone = g.clone()
    gclone[5,1] = 5.5
    assert g[5,1] == 4.0
    assert gclone[5][1] == 5.5