<commit_before>from .. import Contiguity as c
from .. import util
from ...common import pandas
from ...io.FileIO import FileIO as ps_open
from ...io import geotable as pdio

import pysal_examples
import unittest as ut
from warnings import warn as Warn

PANDAS_EXTINCT = pandas is None
class Mock(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

class Contiguity_Mixin(object):
    polygon_path = pysal_examples.get_path('columbus.shp')
    point_path = pysal_examples.get_path('baltim.shp')
    f = ps_open(polygon_path) # our file handler
    polygons = f.read() # our iterable
    f.seek(0) #go back to head of file
    # Without requiring users to have GDAL to run tests, 
    # we can mock a shapely object by an object with a geo_interface
    mocks = [Mock(__geo_interface__=p.__geo_interface__) 
                  for p in polygons] 
    cls = object # class constructor
    known_wi = None #index of known w entry to compare
    known_w = dict() #actual w entry
    known_name = known_wi
    known_namedw = known_w
    idVariable = None # id variable from file or column

    def setUp(self):
        self.__dict__.update({k:v for k,v in Contiguity_Mixin.__dict__.items()
            if not k.startswith('_')})
    
    def runTest(self):
        pass 

    def test_init(self):
        # basic
        w = self.cls(self.polygons)
        self.assertEqual(w[self.known_wi], self.known_w)

        # sparse
        #w = self.cls(self.polygons, sparse=True)
        #srowvec = ws.sparse[self.known_wi].todense().tolist()[0]
        #this_w = {i:k for i,k in enumerate(srowvec) if k>0}
        #self.assertEqual(this_w, self.known_w)
        #ids = ps.weights2.utils.get_ids(self.polygon_path, self.idVariable)

        # named
        ids = util.get_ids(self.polygon_path, self.idVariable)
        w = self.cls(self.polygons, ids = ids)
        self.assertEqual(w[self.known_name], self.known_namedw)

    def test_from_iterable(self):
        w = self.cls.from_iterable(self.f)
        self.f.seek(0)
        self.assertEqual(w[self.known_wi], self.known_w)
        
        w = self.cls.from_iterable(self.mocks)
        self.assertEqual(w[self.known_wi], self.known_w)

    def test_from_shapefile(self):
        # basic
        w = self.cls.from_shapefile(self.polygon_path)
        self.assertEqual(w[self.known_wi], self.known_w)

        # sparse
        ws = self.cls.from_shapefile(self.polygon_path, sparse=True)
        srowvec = ws.sparse[self.known_wi].todense().tolist()[0]
        this_w = {i:k for i,k in enumerate(srowvec) if k>0}
        self.assertEqual(this_w, self.known_w)

        # named
        w = self.cls.from_shapefile(self.polygon_path, idVariable=self.idVariable)
        self.assertEqual(w[self.known_name], self.known_namedw)

    def test_from_array(self):
        # test named, sparse from point array
        pass 

    @ut.skipIf(PANDAS_EXTINCT, 'Missing pandas')
    def test_from_dataframe(self):
        # basic
        df = pdio.read_files(self.polygon_path)
        w = self.cls.from_dataframe(df)
        self.assertEqual(w[self.known_wi], self.known_w)
<commit_msg>remove mocks since we test the real thing now<commit_after>from .. import Contiguity as c
from .. import util
from ...common import pandas
from ...io.FileIO import FileIO as ps_open
from ...io import geotable as pdio

import pysal_examples
import unittest as ut
from warnings import warn as Warn

PANDAS_EXTINCT = pandas is None

class Contiguity_Mixin(object):
    polygon_path = pysal_examples.get_path('columbus.shp')
    point_path = pysal_examples.get_path('baltim.shp')
    f = ps_open(polygon_path) # our file handler
    polygons = f.read() # our iterable
    f.seek(0) #go back to head of file
    cls = object # class constructor
    known_wi = None #index of known w entry to compare
    known_w = dict() #actual w entry
    known_name = known_wi
    known_namedw = known_w
    idVariable = None # id variable from file or column

    def setUp(self):
        self.__dict__.update({k:v for k,v in Contiguity_Mixin.__dict__.items()
            if not k.startswith('_')})
    
    def runTest(self):
        pass 

    def test_init(self):
        # basic
        w = self.cls(self.polygons)
        self.assertEqual(w[self.known_wi], self.known_w)

        # sparse
        #w = self.cls(self.polygons, sparse=True)
        #srowvec = ws.sparse[self.known_wi].todense().tolist()[0]
        #this_w = {i:k for i,k in enumerate(srowvec) if k>0}
        #self.assertEqual(this_w, self.known_w)
        #ids = ps.weights2.utils.get_ids(self.polygon_path, self.idVariable)

        # named
        ids = util.get_ids(self.polygon_path, self.idVariable)
        w = self.cls(self.polygons, ids = ids)
        self.assertEqual(w[self.known_name], self.known_namedw)

    def test_from_iterable(self):
        w = self.cls.from_iterable(self.f)
        self.f.seek(0)
        self.assertEqual(w[self.known_wi], self.known_w)
        
    def test_from_shapefile(self):
        # basic
        w = self.cls.from_shapefile(self.polygon_path)
        self.assertEqual(w[self.known_wi], self.known_w)

        # sparse
        ws = self.cls.from_shapefile(self.polygon_path, sparse=True)
        srowvec = ws.sparse[self.known_wi].todense().tolist()[0]
        this_w = {i:k for i,k in enumerate(srowvec) if k>0}
        self.assertEqual(this_w, self.known_w)

        # named
        w = self.cls.from_shapefile(self.polygon_path, idVariable=self.idVariable)
        self.assertEqual(w[self.known_name], self.known_namedw)

    def test_from_array(self):
        # test named, sparse from point array
        pass 

    @ut.skipIf(PANDAS_EXTINCT, 'Missing pandas')
    def test_from_dataframe(self):
        # basic
        df = pdio.read_files(self.polygon_path)
        w = self.cls.from_dataframe(df)
        self.assertEqual(w[self.known_wi], self.known_w)

{'fraction_non_alphanumeric': 0.10682241408498391, 'fraction_numerical': 0.0023700694091755544, 'mean_word_length': 2.7654557042702357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5206442', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2002, 'n_words': 441}