# bubble-economy / test_nearness.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123``` ```#!/usr/bin/env python # encoding: utf-8 """ scipy_distance.py Created by dan mackinlay on 2010-10-26. Copyright (c) 2010 __MyCompanyName__. All rights reserved. """ import sys sys.path.insert(0,'.') #why do i need this? import unittest import nearness import numpy as np import scipy as sp from scipy.spatial import distance from numpy.testing import TestCase class EuclidianSanityCheck(TestCase): def setUp(self): """set up some colinear vectors for ease of testing """ self.points = np.array([[0,0,0],[1,0,0],[3,0,0],[6,0,0]]) def testDistance(self): m_t = nearness.distances(self.points) self.assertEquals(nearness.dist(m_t, 0, 1), 1.0) self.assertEquals(nearness.dist(m_t, 1, 0), 1.0) self.assertEquals(nearness.dist(m_t, 0, 2), 3.0) self.assertEquals(nearness.dist(m_t, 2, 0), 3.0) self.assertEquals(nearness.dist(m_t, 0, 3), 6.0) self.assertEquals(nearness.dist(m_t, 3, 0), 6.0) self.assertEquals(nearness.dist(m_t, 1, 2), 2.0) self.assertEquals(nearness.dist(m_t, 2, 1), 2.0) self.assertEquals(nearness.dist(m_t, 1, 3), 5.0) self.assertEquals(nearness.dist(m_t, 3, 1), 5.0) self.assertEquals(nearness.dist(m_t, 2, 3), 3.0) self.assertEquals(nearness.dist(m_t, 3, 2), 3.0) class OtherNorms(TestCase): def setUp(self): """set up some colinear vectors for ease of testing """ self.points = np.array([[0,0,0],[1,0,0],[1,1,0],[-1,0,0]]) def testDistance(self): m_t = nearness.distances(self.points, 'cityblock') self.assertEquals(nearness.dist(m_t, 0, 1), 1.0) self.assertEquals(nearness.dist(m_t, 1, 0), 1.0) self.assertEquals(nearness.dist(m_t, 0, 2), 2.0) self.assertEquals(nearness.dist(m_t, 2, 0), 2.0) self.assertEquals(nearness.dist(m_t, 0, 3), 1.0) self.assertEquals(nearness.dist(m_t, 3, 0), 1.0) self.assertEquals(nearness.dist(m_t, 1, 2), 1.0) self.assertEquals(nearness.dist(m_t, 2, 1), 1.0) self.assertEquals(nearness.dist(m_t, 1, 3), 2.0) self.assertEquals(nearness.dist(m_t, 3, 1), 2.0) self.assertEquals(nearness.dist(m_t, 2, 3), 3.0) self.assertEquals(nearness.dist(m_t, 3, 2), 3.0) class WrappedNorms(TestCase): def setUp(self): """set up vectors that are similar around boundary conds """ self.zero = np.array([0,0,0,0.0]) self.one = np.array([0,0,0,1]) def testP2Distance(self): """some basic sanity in the P2 norm world """ self.assertAlmostEqual( nearness.wrapped_p_norm( self.zero, 0.125*self.one), 0.125 ) self.assertAlmostEqual( nearness.wrapped_p_norm( self.zero, -0.125*self.one), 0.125 ) self.assertAlmostEqual( nearness.wrapped_p_norm( self.zero, self.one*0.875), 0.125 ) self.assertAlmostEqual( nearness.wrapped_p_norm( self.zero, self.zero), 0.0 ) self.assertAlmostEqual( nearness.wrapped_p_norm( self.zero, self.one*2), 0.0 ) self.assertAlmostEqual( nearness.wrapped_p_norm( self.one*0.125, self.one*2), 0.125 ) self.assertAlmostEqual( nearness.wrapped_p_norm( self.one*8, self.one*2), 0.0 ) self.assertAlmostEqual( nearness.wrapped_p_norm( -3*self.one, -8*self.one), 0.0 ) self.assertAlmostEqual( nearness.wrapped_p_norm( -3*self.one, 8*self.one), 0.0 ) if __name__ == '__main__': unittest.main() ```