<commit_before>"""Unit test for util.py"""
import pysal
from pysal.common import *
import pysal.weights
import numpy as np
from scipy import sparse,float32
from scipy.spatial import KDTree
import os, gc

class _Testutil(unittest.TestCase):
    def setUp(self):
        self.neighbors={'c': ['b'], 'b': ['c', 'a'], 'a': ['b']}
        self.weights ={'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
        self.id_order=['a','b','c']
        self.weights ={'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
        self.w=pysal.W(self.neighbors,self.weights,self.id_order)
        self.y = np.array([0,1,2])

    def test_lat2W(self):
        w9=pysal.lat2W(3,3)
        self.assertEquals(w9.pct_nonzero, 0.29629629629629628)
        self.assertEquals(w9[0], {1: 1.0, 3: 1.0})
        self.assertEquals(w9[3], {0: 1.0, 4: 1.0, 6: 1.0})

    def test_regime_weights(self):
        regimes=np.ones(25)
        regimes[range(10,20)]=2
        regimes[range(21,25)]=3
        regimes = np.array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            2.,  2.,  2., 2.,  2.,  2.,  2.,  2.,  2.,  2.,  1.,  3.,  3.,
            3.,  3.])
        w = pysal.regime_weights(regimes)
        ww0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
        self.assertEquals(w.weights[0], ww0)
        wn0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
        self.assertEquals(w.neighbors[0], wn0)
        regimes=['n','n','s','s','e','e','w','w','e']
        n = len(regimes)
        w = pysal.regime_weights(regimes)
        wn = {0: [1], 1: [0], 2: [3], 3: [2], 4: [5, 8], 5: [4, 8], 6: [7], 7: [6], 8: [4, 5]}
        self.assertEquals(w.neighbors, wn)
       
    def test_comb(self):
        x = range(4)
        c = comb(x, 2)

        for c in comb(x,2):
            print c
        ...     
        [0, 1]
        [0, 2]
        [0, 3]
        [1, 2]
        [1, 3]
        [2, 3]
        
suite = unittest.TestLoader().loadTestsFromTestCase(_Testutil)

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite)<commit_msg>Unit tests for weights.util finished (pending lat2SW)<commit_after>"""Unit test for util.py"""
import pysal
from pysal.common import *
import pysal.weights
import numpy as np
from scipy import sparse,float32
from scipy.spatial import KDTree
import os, gc

class _Testutil(unittest.TestCase):
    def setUp(self):
        self.w = pysal.rook_from_shapefile('../../examples/10740.shp')

    def test_lat2W(self):
        w9=pysal.lat2W(3,3)
        self.assertEquals(w9.pct_nonzero, 0.29629629629629628)
        self.assertEquals(w9[0], {1: 1.0, 3: 1.0})
        self.assertEquals(w9[3], {0: 1.0, 4: 1.0, 6: 1.0})

    def test_regime_weights(self):
        regimes=np.ones(25)
        regimes[range(10,20)]=2
        regimes[range(21,25)]=3
        regimes = np.array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            2.,  2.,  2., 2.,  2.,  2.,  2.,  2.,  2.,  2.,  1.,  3.,  3.,
            3.,  3.])
        w = pysal.regime_weights(regimes)
        ww0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
        self.assertEquals(w.weights[0], ww0)
        wn0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
        self.assertEquals(w.neighbors[0], wn0)
        regimes=['n','n','s','s','e','e','w','w','e']
        n = len(regimes)
        w = pysal.regime_weights(regimes)
        wn = {0: [1], 1: [0], 2: [3], 3: [2], 4: [5, 8], 5: [4, 8], 6: [7], 7: [6], 8: [4, 5]}
        self.assertEquals(w.neighbors, wn)
       
    def test_comb(self):
        x = range(4)
        l = []
        for i in pysal.comb(x,2):
            l.append(i)
        lo = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]
        self.assertEquals(l, lo)

    def test_order(self):
        w3 = pysal.order(self.w, kmax=3)
        w3105 = [1, -1, 1, 2, 1]
        self.assertEquals(w3105, w3[1][0:5])
 
    def test_higher_order(self):
        w10 = pysal.lat2W(10,10)
        w10_2 = pysal.higher_order(w10,2)
        w10_20 = {2: 1.0, 11: 1.0, 20: 1.0}
        self.assertEquals(w10_20, w10_2[0])
        w5 = pysal.lat2W()
        w50 = {1: 1.0, 5: 1.0}
        self.assertEquals(w50, w5[0])
        w51 = {0: 1.0, 2: 1.0, 6: 1.0}
        self.assertEquals(w51, w5[1])
        w5_2 = pysal.higher_order(w5,2)
        w5_20 = {2: 1.0, 10: 1.0, 6: 1.0}
        self.assertEquals(w5_20, w5_2[0])

    def test_shimbel(self):
        w5 = pysal.lat2W()
        w5_shimbel = pysal.shimbel(w5)
        w5_shimbel024 = 8
        self.assertEquals(w5_shimbel024, w5_shimbel[0][24])
        w5_shimbel004 = [-1, 1, 2, 3]
        self.assertEquals(w5_shimbel004, w5_shimbel[0][0:4])

    def test_full(self):
        neighbors={'first':['second'],'second':['first','third'],'third':['second']}
        weights={'first':[1],'second':[1,1],'third':[1]}
        w = pysal.W(neighbors,weights)
        wf,ids = pysal.full(w)
        wfo = np.array([[ 0.,  1.,  0.], [ 1.,  0.,  1.], [ 0.,  1.,  0.]])
        np.testing.assert_array_almost_equal(wfo, wf, decimal=8)
        idso = ['first', 'second', 'third']
        self.assertEquals(idso, ids)

    def test_remap_ids(self):
        w = pysal.lat2W(3,2)
        wid_order = [0, 1, 2, 3, 4, 5]
        self.assertEquals(wid_order, w.id_order)
        wneighbors0 = [2, 1]
        self.assertEquals(wneighbors0, w.neighbors[0])
        old_to_new = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
        w_new = pysal.remap_ids(w, old_to_new)
        w_newid_order = ['a', 'b', 'c', 'd', 'e', 'f']
        self.assertEquals(w_newid_order, w_new.id_order)
        w_newdneighborsa = ['c', 'b']
        self.assertEquals(w_newdneighborsa, w_new.neighbors['a'])

    def test_get_ids(self):
        polyids = pysal.weights.util.get_ids("../../examples/columbus.shp", "POLYID")      
        polyids5 = [1, 2, 3, 4, 5]
        self.assertEquals(polyids5, polyids[:5])

    def test_get_points_array_from_shapefile(self):
        xy = pysal.weights.util.get_points_array_from_shapefile('../../examples/juvenile.shp')
        xy3 = np.array([[ 94.,  93.], [ 80.,  95.], [ 79.,  90.]])
        np.testing.assert_array_almost_equal(xy3, xy[:3], decimal=8)
        xy = pysal.weights.util.get_points_array_from_shapefile('../../examples/columbus.shp')
        xy3 = np.array([[  8.82721847,  14.36907602], [  8.33265837,  14.03162401], [  9.01226541,  13.81971908]])
        np.testing.assert_array_almost_equal(xy3, xy[:3], decimal=8)

    def test_min_threshold_distance(self):
        x,y=np.indices((5,5))
        x.shape=(25,1)
        y.shape=(25,1)
        data=np.hstack([x,y])
        mint = 1.0
        self.assertEquals(mint, pysal.weights.util.min_threshold_distance(data))

suite = unittest.TestLoader().loadTestsFromTestCase(_Testutil)

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite)
{'fraction_non_alphanumeric': 0.19817163078737834, 'fraction_numerical': 0.08891182542023002, 'mean_word_length': 2.254798464491363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6938047', 'n_tokens_mistral': 3172, 'n_tokens_neox': 2860, 'n_words': 567}