def ctest1DistsListArray(self): exp = numpy.array([1., 1.414213, 1.0], 'd') desc = [ numpy.array([0.0, 0.0], 'd'), numpy.array([1.0, 0.0], 'd'), numpy.array([1.0, 1.0], 'd') ] dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i]) # repeat the test with a list of numpy.arrays of floats desc = [ numpy.array([0.0, 0.0], 'f'), numpy.array([1.0, 0.0], 'f'), numpy.array([1.0, 1.0], 'f') ] dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i]) # repeat the test with a list of numpy.arrays of ints desc = [ numpy.array([0, 0], 'i'), numpy.array([1, 0], 'i'), numpy.array([1, 1], 'i') ] dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i])
def test0DistsArray(self): exp = numpy.array([1., 1.414213, 1.0], 'd') # initialize a double array and check if get back the expected distances desc = numpy.zeros((3, 2), 'd') desc[1, 0] = 1.0 desc[2, 0] = 1.0 desc[2, 1] = 1.0 dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i]) # repeat with an flaot array desc = numpy.zeros((3, 2), 'f') desc[1, 0] = 1.0 desc[2, 0] = 1.0 desc[2, 1] = 1.0 dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i]) # finally with an interger array desc = numpy.zeros((3, 2), 'i') desc[1, 0] = 1 desc[2, 0] = 1 desc[2, 1] = 1 dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i])
def test2DistListList(self): exp = numpy.array([1., 1.414213, 1.0], 'd') desc = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]] dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i]) # test with ints desc = [[0, 0], [1, 0], [1, 1]] dmat = rdmmc.GetEuclideanDistMat(desc) for i in range(numpy.shape(dmat)[0]): assert feq(dmat[i], exp[i])
def test1HierarchPick(self): fname = os.path.join(RDConfig.RDBaseDir, 'Code', 'SimDivPickers', 'Wrap', 'test_data', 'points.csv') with open(fname) as infil: lines = infil.readlines() self.dataPts = numpy.zeros((len(lines), 2), 'd') labels = [] i = 0 for line in lines: tlst = line.strip().split(',') self.dataPts[i, 0] = float(tlst[1]) self.dataPts[i, 1] = float(tlst[2]) labels.append(int(tlst[3])) i += 1 self.dMat = rdmmc.GetEuclideanDistMat(self.dataPts) pkr = rdSimDivPickers.HierarchicalClusterPicker( rdSimDivPickers.ClusterMethod.WARD) clusters = pkr.Cluster(self.dMat, i, 2) # check that each of the clusters have the same label for cl in clusters: clbl = labels[cl[0]] for id in cl: assert clbl == labels[id] hierarch = pkr.Pick(self.dMat, i, 2) self.assertEqual(tuple(hierarch), (1, 30))
def setUp(self): self.n = 1000 self.m = 80 self.d = 2 self.dataPts = numpy.zeros((self.n, self.d), 'd') for i in range(self.n): for j in range(self.d): self.dataPts[i, j] = random.random() self.dMat = rdmmc.GetEuclideanDistMat(self.dataPts)
def test3Compare(self): n = 30 m = 5 dscArr = numpy.zeros((n, m), 'd') for i in range(n): for j in range(m): dscArr[i, j] = random.random() dmatArr = rdmmc.GetEuclideanDistMat(dscArr) dscLL = [] for i in range(n): row = [] for j in range(m): row.append(dscArr[i, j]) dscLL.append(row) dmatLL = rdmmc.GetEuclideanDistMat(dscLL) assert numpy.shape(dmatArr) == numpy.shape(dmatLL) for i in range(n * (n - 1) // 2): assert feq(dmatArr[i], dmatLL[i])
def test1HierarchPick(self): infil = open("test_data/points.csv", 'r') lines = infil.readlines() infil.close() self.dataPts = numpy.zeros((len(lines), 2), 'd') labels = [] i = 0 for line in lines: tlst = line.strip().split(',') self.dataPts[i, 0] = float(tlst[1]) self.dataPts[i, 1] = float(tlst[2]) labels.append(int(tlst[3])) i += 1 self.dMat = rdmmc.GetEuclideanDistMat(self.dataPts) pkr = rdSimDivPickers.HierarchicalClusterPicker( rdSimDivPickers.ClusterMethod.WARD) clusters = pkr.Cluster(self.dMat, i, 2) # check that each of the clusters have the same label for cl in clusters: clbl = labels[cl[0]] for id in cl: assert clbl == labels[id] hierarch = pkr.Pick(self.dMat, i, 2) assert tuple(hierarch) == (1, 30)