def test_gradient():

    vc = VolumeConvolution(num_features=1)
    vc.W.data.fill_(1.0)
    v_size = 30
    inp1 = torch.zeros(1,
                       1,
                       v_size,
                       v_size,
                       v_size,
                       dtype=torch.float,
                       device='cuda').requires_grad_()
    inp1tmp = torch.zeros(1,
                          1,
                          v_size,
                          v_size,
                          v_size,
                          dtype=torch.float,
                          device='cuda')
    inp2 = torch.zeros(1,
                       1,
                       v_size,
                       v_size,
                       v_size,
                       dtype=torch.float,
                       device='cuda').requires_grad_()
    fill_delta(np.array([1, 6, 5]), inp1)
    fill_delta(np.array([5, 5, 5]), inp2)

    out = vc(inp1, inp2)
    E = out.sum()
    E.backward()

    back_grad_x0 = torch.zeros(inp1.grad.size(),
                               dtype=torch.float,
                               device='cpu').copy_(inp1.grad.data)
    inp1tmp.data.copy_(inp1.data)
    grads = []
    real_grads = []
    Np = 10

    for j in range(0, Np):
        x = random.randint(0, v_size - 1)
        y = random.randint(0, v_size - 1)
        z = random.randint(0, v_size - 1)
        dx = 0.0001
        inp1tmp.data.copy_(inp1.data)
        inp1tmp.data[0, 0, x, y, z] += dx
        E1 = vc(inp1tmp, inp2).sum()

        dy_dx = (E1.data[0] - E.data[0]) / (dx)
        grads.append(dy_dx)
        real_grads.append(back_grad_x0[0, 0, x, y, z])
        fig = plt.figure()
        plt.plot(grads, 'r.-', label='num grad')
        plt.plot(real_grads, 'bo', label='an grad')
        plt.legend()
        plt.savefig('TestFig/test_backward.png')
    def __init__(self,
                 representation,
                 filter,
                 threshold_clash=300,
                 normalize=False,
                 rotate_ligand=False,
                 exclude_clashes=True):
        super(GlobalDockingModel, self).__init__()

        self.threshold_clash = threshold_clash
        self.representation = representation
        self.filter = filter

        self.mult = MultiplyVolumes()
        self.convolve = VolumeConvolution(clip=5.0)
        self.vol_rotate = VolumeRotation()

        self.rotate_ligand = rotate_ligand
        self.normalize = normalize
        self.exclude = exclude_clashes
Exemple #3
0
    def __init__(self,
                 docking_model,
                 angle_inc=15.0,
                 box_size=80,
                 resolution=1.25,
                 max_conf=1000,
                 randomize_rot=False):
        self.docking_model = docking_model
        self.log = None

        self.box_size = box_size
        self.resolution = resolution
        self.box_length = box_size * resolution

        self.max_conf = max_conf
        self.rot = Rotations(angle_inc=angle_inc)

        self.rotate = CoordsTransform.CoordsRotate()
        self.translate = CoordsTransform.CoordsTranslate()
        self.project = TypedCoords2Volume(self.box_size, self.resolution)
        self.convolve = VolumeConvolution()

        self.box_center = torch.zeros(1, 3, dtype=torch.double, device='cpu')
        self.box_center.fill_(self.box_length / 2.0)

        self.pdb2coords = PDB2CoordsUnordered()
        self.assignTypes = Coords2TypedCoords()
        self.translation = CoordsTranslate()
        self.vol_rotate = VolumeRotation()

        self.randomize_rot = randomize_rot
        if self.randomize_rot:
            self.randR = getRandomRotation(1)
            print("Adding random rotation to the receptor:", self.randR)

        atexit.register(self.cleanup)
					arg = (x,y,z)
					max_val = val

	return max_val, arg

def conv_numpy(inp1, inp2):
	npv1 = inp1.squeeze().cpu().numpy()
	npv2 = inp2.squeeze().cpu().numpy()
	cv1 = np.fft.fftn(npv1, [30,30,30])
	cv2 = np.fft.fftn(npv2, [30,30,30])
	cv = cv1 * np.conj(cv2)
	v = np.real(np.fft.ifftn(cv))
	return v

if __name__=='__main__':
	vc = VolumeConvolution()
	
	v_size = 30
	r0 = [15,15,15]
	r1 = [23,15,15]
	l0 = [15,20,15]

	inp1 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda')
	inp1_border = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda')
	inp2 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda')
	out2 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda')
	fill_V1(np.array(r0), np.array(r1), 8, 5, inp1)
	get_boundary(inp1, inp1_border)
	
	fill_V2(np.array(l0), 5, inp2)
	
def test_gradient():

    vc = VolumeConvolution()
    v_size = 30
    inp1 = torch.zeros(1,
                       1,
                       v_size,
                       v_size,
                       v_size,
                       dtype=torch.float,
                       device='cuda',
                       requires_grad=True)
    inp1tmp_p = torch.zeros(1,
                            1,
                            v_size,
                            v_size,
                            v_size,
                            dtype=torch.float,
                            device='cuda')
    inp1tmp_m = torch.zeros(1,
                            1,
                            v_size,
                            v_size,
                            v_size,
                            dtype=torch.float,
                            device='cuda')
    inp2 = torch.zeros(1,
                       1,
                       v_size,
                       v_size,
                       v_size,
                       dtype=torch.float,
                       device='cuda',
                       requires_grad=True)

    target = torch.zeros(1,
                         1,
                         2 * v_size,
                         2 * v_size,
                         2 * v_size,
                         dtype=torch.float,
                         device='cuda')
    fill_V2(np.array([12, 30, 50]), 5, target)

    r0 = [20, 15, 15]
    r1 = [23, 15, 7]
    l0 = [7, 24, 7]
    fill_V1(np.array(r0), np.array(r1), 8, 5, inp1)
    fill_V2(np.array(l0), 5, inp2)

    _Volume.Volume2Xplor(inp1.squeeze().cpu(), "receptor.xplor", 1.0)
    _Volume.Volume2Xplor(inp2.squeeze().cpu(), "ligand.xplor", 1.0)

    out = vc(inp1, inp2)
    E = torch.sum((out - target) * (out - target))
    # E = torch.sum(torch.abs(out-target))
    # E = out[:,:,8,40,0] + out[:,:,10,30,25]
    E.backward()

    back_grad_x0 = torch.zeros(inp2.grad.size(),
                               dtype=torch.float,
                               device='cpu').copy_(inp2.grad)

    print("Grad max min", torch.max(back_grad_x0), torch.min(back_grad_x0))
    _Volume.Volume2Xplor(back_grad_x0[0, 0, :, :, :], "grad_conv.xplor", 1.00)

    num_grads = []
    an_grads = []
    for j in range(0, v_size):
        x = j
        y = 7
        z = 7
        dx = 0.1
        inp1tmp_p.copy_(inp2)
        inp1tmp_m.copy_(inp2)
        inp1tmp_p[0, 0, x, y, z] += dx
        inp1tmp_m[0, 0, x, y, z] -= dx

        # out1_p = vc(inp1tmp_p, inp2)
        # out1_m = vc(inp1tmp_m, inp2)
        out1_p = vc(inp1, inp1tmp_p)
        out1_m = vc(inp1, inp1tmp_m)
        E1_p = torch.sum((out1_p - target) * (out1_p - target))
        E1_m = torch.sum((out1_m - target) * (out1_m - target))
        # E1_p = torch.sum(torch.abs(out1_p-target))
        # E1_m = torch.sum(torch.abs(out1_m-target))
        # E1 = (out1[:,:,8,40,0] + out1[:,:,10,30,25])
        dy_dx = (E1_p.item() - E1_m.item()) / (2 * dx)

        num_grads.append(dy_dx)
        an_grads.append(back_grad_x0[0, 0, x, y, z].item())

    fig = plt.figure()
    plt.plot(num_grads, 'r.-', label='num grad')
    plt.plot(an_grads, 'bo', label='an grad')
    plt.legend()
    plt.savefig('TestFig/test_backward.png')
    arg = (0, 0, 0)
    max_val = volume.data[0, 0, 0]

    for x in range(volume_size):
        for y in range(volume_size):
            for z in range(volume_size):
                val = float(volume.data[x, y, z])
                if val > max_val:
                    arg = (x, y, z)
                    max_val = val

    return max_val, arg


if __name__ == '__main__':
    vc = VolumeConvolution(num_features=1)
    vc.W.data.fill_(1.0)
    v_size = 30
    inp1 = torch.zeros(1,
                       1,
                       v_size,
                       v_size,
                       v_size,
                       dtype=torch.float,
                       device='cuda')
    inp2 = torch.zeros(1,
                       1,
                       v_size,
                       v_size,
                       v_size,
                       dtype=torch.float,
Exemple #7
0
	def setUp(self):
		print(self.msg, self.device, self.dtype)
		self.vc = VolumeConvolution()
		self.box_size = 30
		self.resolution = 1.0