Source

bubble-economy / test_nearness.py

Full commit
#!/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()