def testSqureFormExecution(self):
        from scipy.spatial.distance import pdist as sp_pdist, \
            squareform as sp_squareform

        raw_a = np.random.rand(80, 10)
        raw_pdsit = sp_pdist(raw_a)
        raw_square = sp_squareform(raw_pdsit)

        # tomatrix, test 1 chunk
        vec = tensor(raw_pdsit, chunk_size=raw_pdsit.shape[0])
        mat = distance.squareform(vec, chunk_size=100)
        result = self._executor.execute_tensor(mat, concat=True)[0]
        np.testing.assert_array_equal(result, raw_square)

        # tomatrix, test more than 1 chunk
        vec = tensor(raw_pdsit, chunk_size=33)
        self.assertGreater(len(vec.tiles().chunks), 1)
        mat = distance.squareform(vec, chunk_size=34)
        result = self._executor.execute_tensor(mat, concat=True)[0]
        np.testing.assert_array_equal(result, raw_square)

        # tovec, test 1 chunk
        mat = tensor(raw_square)
        vec = distance.squareform(mat, chunk_size=raw_pdsit.shape[0])
        self.assertEqual(len(mat.tiles().chunks), 1)
        self.assertEqual(len(vec.tiles().chunks), 1)
        result = self._executor.execute_tensor(vec, concat=True)[0]
        np.testing.assert_array_equal(result, raw_pdsit)

        # tovec, test more than 1 chunk
        mat = tensor(raw_square, chunk_size=31)
        vec = distance.squareform(mat, chunk_size=40)
        self.assertGreater(len(vec.tiles().chunks), 1)
        result = self._executor.execute_tensor(vec, concat=True)[0]
        np.testing.assert_array_equal(result, raw_pdsit)

        # test checks
        # generate non-symmetric matrix
        non_sym_arr = np.random.RandomState(0).rand(10, 10)

        # 1 chunk
        mat = tensor(non_sym_arr)
        vec = distance.squareform(mat, checks=True, chunk_size=100)
        with self.assertRaises(ValueError):
            _ = self._executor.execute_tensor(vec, concat=True)[0]
        # force checks=False
        vec = distance.squareform(mat, checks=False, chunk_size=100)
        _ = self._executor.execute_tensor(vec, concat=True)[0]

        # more than 1 chunk
        mat = tensor(non_sym_arr, chunk_size=6)
        vec = distance.squareform(mat, checks=True, chunk_size=8)
        self.assertGreater(len(vec.tiles().chunks), 1)
        with self.assertRaises(ValueError):
            _ = self._executor.execute_tensor(vec, concat=True)[0]
        # force checks=False
        vec = distance.squareform(mat, checks=False, chunk_size=100)
        _ = self._executor.execute_tensor(vec, concat=True)[0]
def test_squareform_execution(setup):
    from scipy.spatial.distance import pdist as sp_pdist, \
        squareform as sp_squareform

    raw_a = np.random.rand(80, 10)
    raw_pdsit = sp_pdist(raw_a)
    raw_square = sp_squareform(raw_pdsit)

    # tomatrix, test 1 chunk
    vec = tensor(raw_pdsit, chunk_size=raw_pdsit.shape[0])
    mat = distance.squareform(vec, chunk_size=100)
    result = mat.execute().fetch()
    np.testing.assert_array_equal(result, raw_square)

    # tomatrix, test more than 1 chunk
    vec = tensor(raw_pdsit, chunk_size=33)
    assert len(tile(vec).chunks) > 1
    mat = distance.squareform(vec, chunk_size=34)
    result = mat.execute().fetch()
    np.testing.assert_array_equal(result, raw_square)

    # tovec, test 1 chunk
    mat = tensor(raw_square)
    vec = distance.squareform(mat, chunk_size=raw_pdsit.shape[0])
    assert len(tile(mat).chunks) == 1
    assert len(tile(vec).chunks) == 1
    result = vec.execute().fetch()
    np.testing.assert_array_equal(result, raw_pdsit)

    # tovec, test more than 1 chunk
    mat = tensor(raw_square, chunk_size=31)
    vec = distance.squareform(mat, chunk_size=40)
    assert len(tile(vec).chunks) > 1
    result = vec.execute().fetch()
    np.testing.assert_array_equal(result, raw_pdsit)

    # test checks
    # generate non-symmetric matrix
    non_sym_arr = np.random.RandomState(0).rand(10, 10)

    # 1 chunk
    mat = tensor(non_sym_arr)
    vec = distance.squareform(mat, checks=True, chunk_size=100)
    with pytest.raises(ValueError):
        _ = vec.execute().fetch()
    # force checks=False
    vec = distance.squareform(mat, checks=False, chunk_size=100)
    _ = vec.execute().fetch()

    # more than 1 chunk
    mat = tensor(non_sym_arr, chunk_size=6)
    vec = distance.squareform(mat, checks=True, chunk_size=8)
    assert len(tile(vec).chunks) > 1
    with pytest.raises(ValueError):
        _ = vec.execute().fetch()
    # force checks=False
    vec = distance.squareform(mat, checks=False, chunk_size=100)
    _ = vec.execute().fetch()
 def clashes(self):
     """Checks if there are any internal clashes.
     Atoms with occupancy of 0 are not taken into account.
     """
     dist_matrix = sp_squareform(sp_pdist(self.coor))
     mask = np.logical_not(self.connectivity)
     occupancy_matrix = (self.q.reshape(1, -1) * self.q.reshape(-1, 1)) > 0
     mask &= occupancy_matrix
     np.fill_diagonal(mask, False)
     clash_matrix = dist_matrix < self._cutoff_matrix
     if np.any(np.logical_and(clash_matrix, mask)):
         return True
     return False
    def _get_connectivity(self):
        """Determine connectivity matrix of ligand and associated distance
        cutoff matrix for later clash detection.
        """

        dist_matrix = sp_squareform(sp_pdist(self.coor))
        covrad = self.covalent_radius
        natoms = self.natoms
        cutoff_matrix = np.repeat(covrad, natoms).reshape(natoms, natoms)
        # Add 0.5 A to give covalently bound atoms more room
        cutoff_matrix = cutoff_matrix + cutoff_matrix.T + 0.5
        connectivity_matrix = (dist_matrix < cutoff_matrix)
        # Atoms are not connected to themselves
        np.fill_diagonal(connectivity_matrix, False)
        self.connectivity = connectivity_matrix
        self._cutoff_matrix = cutoff_matrix
def get_gp_covar(xgt, tgt, betai, sigma):
    n = xgt.shape[0]
    pairwise_dists = sp_squareform(sp_pdist(xgt, 'euclidean'))
    K = np.exp(-pairwise_dists**2 / sigma**2) + 1e-6 * np.eye(n)
    return K
def get_gp_covar(xgt, tgt, betai, gamma, sigma):
	n = xgt.shape[0]
	pairwise_dists = sp_squareform(sp_pdist(xgt, 'euclidean'))
	K = (gamma**2) * np.exp(-pairwise_dists ** 2 / sigma ** 2) + 1e-6*np.eye(n)
	return K