示例#1
0
    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]
示例#2
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
示例#5
0
    def testPdistExecution(self):
        from scipy.spatial.distance import pdist as sp_pdist

        raw = np.random.rand(100, 10)

        # test 1 chunk
        x = tensor(raw, chunk_size=100)

        dist = distance.pdist(x)
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw)
        np.testing.assert_array_equal(result, expected)

        dist = distance.pdist(x, metric='hamming')
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw, metric='hamming')
        np.testing.assert_array_equal(result, expected)

        f = lambda u, v: np.sqrt(((u - v)**2).sum())
        dist = distance.pdist(x, metric=f)
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw, metric=f)
        np.testing.assert_array_equal(result, expected)

        # test more than 1 chunk
        x = tensor(raw, chunk_size=12)

        dist = distance.pdist(x)
        tdist = dist.tiles()
        self.assertEqual(len(tdist.chunks), 1)
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw)
        np.testing.assert_array_equal(result, expected)

        dist = distance.pdist(x, aggregate_size=3)
        tdist = dist.tiles()
        self.assertEqual(len(tdist.chunks), 3)
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw)
        np.testing.assert_array_equal(result, expected)

        dist = distance.pdist(x, metric='hamming', aggregate_size=2)
        tdist = dist.tiles()
        self.assertEqual(len(tdist.chunks), 2)
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw, metric='hamming')
        np.testing.assert_array_equal(result, expected)

        f = lambda u, v: np.sqrt(((u - v)**2).sum())
        dist = distance.pdist(x, metric=f, aggregate_size=2)
        result = self._executor.execute_tensor(dist, concat=True)[0]
        expected = sp_pdist(raw, metric=f)
        np.testing.assert_array_equal(result, expected)

        for x in [tensor(raw), tensor(raw, chunk_size=12)]:
            # test w
            weight = np.random.rand(10)
            w = tensor(weight, chunk_size=7)
            dist = distance.pdist(x, metric='wminkowski', p=3, w=w)
            result = self._executor.execute_tensor(dist, concat=True)[0]
            expected = sp_pdist(raw, metric='wminkowski', p=3, w=weight)
            np.testing.assert_array_equal(result, expected)

            # test V
            v = np.random.rand(10)
            V = tensor(v, chunk_size=7)
            dist = distance.pdist(x, metric='seuclidean', V=V)
            result = self._executor.execute_tensor(dist, concat=True)[0]
            expected = sp_pdist(raw, metric='seuclidean', V=v)
            np.testing.assert_array_equal(result, expected)

            # test VI
            vi = np.random.rand(10, 10)
            VI = tensor(vi, chunk_size=8)
            dist = distance.pdist(x, metric='mahalanobis', VI=VI)
            result = self._executor.execute_tensor(dist, concat=True)[0]
            expected = sp_pdist(raw, metric='mahalanobis', VI=vi)
            np.testing.assert_array_equal(result, expected)
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
示例#7
0
def test_pdist_execution(setup):
    from scipy.spatial.distance import pdist as sp_pdist

    raw = np.random.rand(100, 10)

    # test 1 chunk
    x = tensor(raw, chunk_size=100)

    dist = distance.pdist(x)
    result = dist.execute().fetch()
    expected = sp_pdist(raw)
    np.testing.assert_array_equal(result, expected)

    dist = distance.pdist(x, metric="hamming")
    result = dist.execute().fetch()
    expected = sp_pdist(raw, metric="hamming")
    np.testing.assert_array_equal(result, expected)

    f = lambda u, v: np.sqrt(((u - v)**2).sum())
    dist = distance.pdist(x, metric=f)
    result = dist.execute().fetch()
    expected = sp_pdist(raw, metric=f)
    np.testing.assert_array_equal(result, expected)

    # test more than 1 chunk
    x = tensor(raw, chunk_size=12)

    dist = distance.pdist(x)
    tdist = tile(dist)
    assert len(tdist.chunks) == 1
    result = dist.execute().fetch()
    expected = sp_pdist(raw)
    np.testing.assert_array_equal(result, expected)

    dist = distance.pdist(x, aggregate_size=3)
    tdist = tile(dist)
    assert len(tdist.chunks) == 3
    result = dist.execute().fetch()
    expected = sp_pdist(raw)
    np.testing.assert_array_equal(result, expected)

    dist = distance.pdist(x, metric="hamming", aggregate_size=2)
    tdist = tile(dist)
    assert len(tdist.chunks) == 2
    result = dist.execute().fetch()
    expected = sp_pdist(raw, metric="hamming")
    np.testing.assert_array_equal(result, expected)

    f = lambda u, v: np.sqrt(((u - v)**2).sum())
    dist = distance.pdist(x, metric=f, aggregate_size=2)
    result = dist.execute().fetch()
    expected = sp_pdist(raw, metric=f)
    np.testing.assert_array_equal(result, expected)

    for x in [tensor(raw), tensor(raw, chunk_size=12)]:
        # test w
        weight = np.random.rand(10)
        w = tensor(weight, chunk_size=7)
        dist = distance.pdist(x, metric="wminkowski", p=3, w=w)
        result = dist.execute().fetch()
        expected = sp_pdist(raw, metric="minkowski", p=3, w=weight)
        np.testing.assert_array_equal(result, expected)

        # test V
        v = np.random.rand(10)
        V = tensor(v, chunk_size=7)
        dist = distance.pdist(x, metric="seuclidean", V=V)
        result = dist.execute().fetch()
        expected = sp_pdist(raw, metric="seuclidean", V=v)
        np.testing.assert_array_equal(result, expected)

        # test VI
        vi = np.random.rand(10, 10)
        VI = tensor(vi, chunk_size=8)
        dist = distance.pdist(x, metric="mahalanobis", VI=VI)
        result = dist.execute().fetch()
        expected = sp_pdist(raw, metric="mahalanobis", VI=vi)
        np.testing.assert_array_equal(result, expected)
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