コード例 #1
0
    def test_divide_array_function(self):
        s = [[1, 1], [2, 2], [3, 2], [4, 3]]

        x_points, y_points = KdTree.get_points_vector(s)

        s1, s2 = KdTree.divide_array(s, MathFunc.median(x_points), 'x')

        self.assertEqual([[1, 1], [2, 2]], s1)
        self.assertEqual([[3, 2], [4, 3]], s2)

        s1, s2 = KdTree.divide_array(s, MathFunc.median(x_points), 'y')

        self.assertEqual([[1, 1], [2, 2], [3, 2]], s1)
        self.assertEqual([[4, 3]], s2)
コード例 #2
0
def testReadKdTree():
    dataway = '../samples/3D_languesIsa_dil0.txt'
    metadataway = '../samples/3D_languesIsa_dil0_metadata.txt'
    h5way = '3D_languesIsa_dil0.h5'
    hm = HDF5Manager([KdTree])
    metadata = {}
    myre = re.compile('^#(.*):(.*)$')
    with open(metadataway) as f:
        for line in f:
            if line.startswith('#'):
                k, v = myre.match(line).groups()
                metadata[k.strip()] = v.strip()
    metadata[METADATA.statedimension] = int(metadata[METADATA.statedimension])
    metadata['results.submissiondate'] = time.strftime('%Y-%m-%d %H:%M',
                                                       time.localtime())
    #    return metadata

    explorationdomain = metadata[
        METADATA.results_softwareparametervalues].split("/")[0].split(",")
    origin = explorationdomain[:len(explorationdomain) / 2]
    opposite = explorationdomain[len(explorationdomain) / 2:]
    k = KdTree.readViabilitree(dataway, metadata, origin, opposite)
    print(k.cells[0])
    print("Kdtree loaded with %d cells" % len(k.cells))
    hm.writeKernel(k, h5way)
    k2 = hm.readKernel(h5way)
    return k2
コード例 #3
0
ファイル: buildSamples.py プロジェクト: NicolasDumoulin/vino
def testReadKdTree(): 
    dataway = '../samples/3D_languesIsa_dil0.txt'
    metadataway = '../samples/3D_languesIsa_dil0_metadata.txt'
    h5way = '3D_languesIsa_dil0.h5'
    hm = HDF5Manager([KdTree])
    metadata = {}
    myre = re.compile('^#(.*):(.*)$')
    with open(metadataway) as f:
        for line in f:
            if line.startswith('#'):
                k, v = myre.match(line).groups()
                metadata[k.strip()] = v.strip()
    metadata[METADATA.statedimension] = int(metadata[METADATA.statedimension])
    metadata['results.submissiondate'] = time.strftime('%Y-%m-%d %H:%M',time.localtime()) 
#    return metadata
    
    explorationdomain = metadata[METADATA.results_softwareparametervalues].split("/")[0].split(",")
    origin = explorationdomain[:len(explorationdomain)/2]
    opposite = explorationdomain[len(explorationdomain)/2:]    
    k = KdTree.readViabilitree(dataway, metadata,origin,opposite)
    print(k.cells[0])
    print("Kdtree loaded with %d cells" % len(k.cells))
    hm.writeKernel(k, h5way)
    k2 = hm.readKernel(h5way)
    return k2
コード例 #4
0
    def test_get_point_vector_function(self):
        s = [[1, 1], [2, 2], [3, 2], [4, 3]]

        x_points, y_points = KdTree.get_points_vector(s)

        self.assertEqual([1, 2, 3, 4], x_points)
        self.assertEqual([1, 2, 2, 3], y_points)
コード例 #5
0
 def test_case_replicate(self):
     import numpy as np
     X2T = [np.linspace(1, 1, 20), np.linspace(2, 2, 20), np.linspace(3, 3, 20)]
     X = np.array(X2T).transpose()
     Tree2 = KdTree(X)
     self.assertEqual(Tree2.tree.num_points, 20)
     self.assertTrue(np.any(Tree2.tree.bounds[0] - np.array([1, 2, 3]) == 0))
     self.assertTrue(np.any(Tree2.tree.bounds[1] - np.array([1, 2, 3]) == 0))
コード例 #6
0
 def fit(self, train_x, train_y):
     """
     :param train_x:
         the features of train data
     :param train_y:
         the targets of train data
     :return:
         return None
     """
     self.train_x = train_x
     self.train_y = train_y
     self.tree = KdTree(train_x)
コード例 #7
0
    def read(self, filename):
        metadata = {}
#        myre = re.compile('^#(.*):(.*)$')
        myre = re.compile('^#([^:]*):(.*)$')
#        myre = re.compile('^([^:]*):(.*)$')
        with open(os.path.splitext(filename)[0]+'.txt') as f:
            for line in f:
                if line.startswith('#'):
                    match = myre.match(line)
                    if match:
                        k, v = match.groups()
                        metadata[k.strip().lower()] = v.strip()
        metadata[METADATA.statedimension] = int(metadata[METADATA.statedimension])
        k = KdTree.readViabilitree(filename, metadata)
        return k
コード例 #8
0
 def test_case_simple(self):
     import numpy as np
     X = [[1, 2, 3], [4, 11, 1], [8, 5, 2], [7, 7, 3], [11, 0, 6], [6, 8, 8]]
     # first split: dim 1.
     # Left part: [1,2,3],[8,5,2],[11,0,6]
     # Right part: [4,11,1],[7,7,3],[6,8,8]
     # second split: 0--left, 2--right
     # Left-left  part: [1,2,3](leaf)
     # Left-right part: [8,5,2],[11,0,6](leaf)
     # Right-left part: [4,11,1],[7,7,3](leaf)
     # Right-rigt part: [6,8,8](leaf)
     Tree1 = KdTree(X, leafsize=2)
     self.assertEqual(Tree1.tree.left.split_axis, 0)
     self.assertEqual(Tree1.tree.right.split, 3)
     self.assertTrue(isinstance(Tree1.tree.left.right, KdTree.LeafNode))
     self.assertEqual(Tree1.tree.left.left.num_points, 1)
     self.assertTrue(np.any(Tree1.tree.left.right.bounds[0] - np.array([8, 0, 6]) == 0))
     self.assertTrue(np.any(Tree1.tree.left.right.bounds[1] - np.array([11, 5, 6]) == 0))
コード例 #9
0
 def test_case_empty(self):
     with self.assertRaises(ValueError):
         KdTree([], leafsize=1)
コード例 #10
0
 def rand_tree(self):
     n = np.random.randint(5, high=200)
     p = np.random.randint(3, high=10)
     X = np.random.rand(n, p)
     return (KdTree(X))
コード例 #11
0
 def rand_tree(self, p=None):
     n = np.random.randint(5, high=200)
     if not p:
         p = np.random.randint(3, high=10)
     X = np.random.rand(n, p)
     return (KdTree(X))
コード例 #12
0
parser = argparse.ArgumentParser()
parser.add_argument(
    '-D',
    '--data',
    help='n*p data matrix to be loaded where n is sample size.',
    type=str)
parser.add_argument('-L',
                    '--leafsize',
                    help='maximal leaf size, default 10.',
                    default=10,
                    type=int)
parser.add_argument('-T',
                    '--test',
                    help='run three unit tests for kdTree construction.',
                    action='store_true')
parser.add_argument('-dis',
                    '--distance',
                    help='return all data points within distance r of a'
                    'point d. Please enter (r,a)')
parser.add_argument('-N',
                    '--neighbors',
                    help='KNN seaech. Returns the (set of) k-nearest neighbors'
                    'in the data X to the point p. Please enter(p,k [,r_max])')

args = parser.parse_args()
if args.test:
    testKdTree()
else:
    Tree = KdTree(eval(args.data), leafsize=args.leafsize, method=args.method)
コード例 #13
0
    def test_kd_tree_s_par_is_one_point(self):
        s = [[1, 1]]

        self.assertEqual(s, KdTree.kd_build(s))
コード例 #14
0
    def test_run_alg1(self):
        s = [[1, 1], [2, 2], [3, 6], [4, 3], [5, 7]]

        node = KdTree.kd_build(s)

        KdTree.display_tree(node)
コード例 #15
0
 def rand_tree():
     n = 100
     p = 6
     X = np.random.rand(n, p)
     return (KdTree(X))
コード例 #16
0
ファイル: DualTree_run.py プロジェクト: yuelicmu/DualTree
                    '--output',
                    help='file to store results',
                    defult='results.txt')
parser.add_argument('--display',
                    help='whether to print results on screen, default not.')
parser.add_argument('-T',
                    '--test',
                    help='running tests suites with randomly'
                    ' generated data.',
                    action='store_true')
args = parser.parse_args()
if args.test:
    testKdTree()
    testDualTree()
else:
    query = KdTree(args.query)
    data = KdTree(args.data)
    kde_est, kde_lower, kde_upper = DualTree(query,
                                             data,
                                             args.tolerance,
                                             kernel=args.kernel,
                                             h=args.h)
    if args.display:
        print(kde_est)
        print(kde_lower)
        print(kde_upper)

    file = open(args.output, 'w')
    file.write('Estimates of kernel density:')
    file.write(kde_est)
    file.write('Estimates of kernel density lower bound:')
コード例 #17
0
import timeit
from DualTree import DualTree, KdeBase, ExactKDE, Kernel
from KdTree import KdTree
import numpy as np

time_matrix = np.full((4, 3), 0)
N = [500, 1000, 10000, 50000]
P = [3, 5, 10]
for i in range(4):
    for j in range(3):
        start = timeit.default_timer()
        X = np.random.rand(N[i], P[j])
        Q = np.random.rand(20, P[j])
        kde_est, kde_lower, kde_upper = DualTree(KdTree(Q), KdTree(X), 1)
        print(i, j)
        print(max(kde_upper - kde_lower))
        time_matrix[i, j] = (timeit.default_timer() - start) * 10**4
print(time_matrix)


def exactKDE(X, Q, h=0.1, kernel='gaussian'):
    M, p = np.shape(X)
    N = np.shape(Q)[0]
    density = np.zeros(N)
    for query_idx in range(N):
        for data_idx in range(M):
            density[query_idx] += 1 / M * Kernel(
                Q[query_idx, :] - X[data_idx, :], h, kernel)
    return (density)