def test_moran(self):
        """Test Moran's I global"""
        data = [{"id": d["id"], "attr1": d["value"], "neighbors": d["neighbors"]} for d in self.neighbors_data]
        random_seeds.set_random_seeds(1235)
        moran = Moran(FakeDataProvider(data))
        result = moran.global_stat("table", "value", "knn", 5, 99, "the_geom", "cartodb_id")

        result_moran = result[0][0]
        expected_moran = np.array([row[0] for row in self.moran_data]).mean()
        self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)
    def test_moran(self):
        """Test Moran's I global"""
        data = [{
            'id': d['id'],
            'attr1': d['value'],
            'neighbors': d['neighbors']
        } for d in self.neighbors_data]
        random_seeds.set_random_seeds(1235)
        moran = Moran(FakeDataProvider(data))
        result = moran.global_stat('table', 'value', 'knn', 5, 99, 'the_geom',
                                   'cartodb_id')

        result_moran = result[0][0]
        expected_moran = np.array([row[0] for row in self.moran_data]).mean()
        self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)
    def test_moran_local_rate(self):
        """Test Moran's I rate"""
        data = [
            {"id": d["id"], "attr1": d["value"], "attr2": 1, "neighbors": d["neighbors"]} for d in self.neighbors_data
        ]

        random_seeds.set_random_seeds(1234)
        moran = Moran(FakeDataProvider(data))
        result = moran.local_rate_stat("subquery", "numerator", "denominator", "knn", 5, 99, "the_geom", "cartodb_id")
        result = [(row[0], row[1]) for row in result]

        zipped_values = zip(result, self.moran_data)

        for ([res_val, res_quad], [exp_val, exp_quad]) in zipped_values:
            self.assertAlmostEqual(res_val, exp_val)
    def test_local_stat(self):
        """Test Moran's I local"""
        data = [
            OrderedDict([("id", d["id"]), ("attr1", d["value"]), ("neighbors", d["neighbors"])])
            for d in self.neighbors_data
        ]

        moran = Moran(FakeDataProvider(data))
        random_seeds.set_random_seeds(1234)
        result = moran.local_stat("subquery", "value", "knn", 5, 99, "the_geom", "cartodb_id")
        result = [(row[0], row[1]) for row in result]
        zipped_values = zip(result, self.moran_data)

        for ([res_val, res_quad], [exp_val, exp_quad]) in zipped_values:
            self.assertAlmostEqual(res_val, exp_val)
            self.assertEqual(res_quad, exp_quad)
    def test_local_stat(self):
        """Test Moran's I local"""
        data = [
            OrderedDict([('id', d['id']), ('attr1', d['value']),
                         ('neighbors', d['neighbors'])])
            for d in self.neighbors_data
        ]

        moran = Moran(FakeDataProvider(data))
        random_seeds.set_random_seeds(1234)
        result = moran.local_stat('subquery', 'value', 'knn', 5, 99,
                                  'the_geom', 'cartodb_id')
        result = [(row[0], row[6]) for row in result]
        zipped_values = zip(result, self.moran_data)

        for ([res_quad, res_val], [exp_val, exp_quad]) in zipped_values:
            self.assertAlmostEqual(res_val, exp_val)
            self.assertEqual(res_quad, exp_quad)
    def test_moran_local_rate(self):
        """Test Moran's I rate"""
        data = [{
            'id': d['id'],
            'attr1': d['value'],
            'attr2': 1,
            'neighbors': d['neighbors']
        } for d in self.neighbors_data]

        random_seeds.set_random_seeds(1234)
        moran = Moran(FakeDataProvider(data))
        result = moran.local_rate_stat('subquery', 'numerator', 'denominator',
                                       'knn', 5, 99, 'the_geom', 'cartodb_id')
        result = [(row[0], row[6]) for row in result]

        zipped_values = zip(result, self.moran_data)

        for ([res_quad, res_val], [exp_val, exp_quad]) in zipped_values:
            self.assertAlmostEqual(res_val, exp_val)