Skip to content

Commit

Permalink
Merge pull request #101 from sjsrey/3.8
Browse files Browse the repository at this point in the history
Fixes for pandas deprecation and 3.8 verbosity
  • Loading branch information
sjsrey authored Feb 1, 2020
2 parents 850f353 + 43f375e commit 4db2a7d
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 111 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ branches:
python:
- 3.6
- 3.7
- 3.8

env:
- PYSAL_PYPI=true MPLBACKEND="pdf"
Expand Down
2 changes: 1 addition & 1 deletion esda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "2.2.0"
__version__ = "2.2.1"
"""
:mod:`esda` --- Exploratory Spatial Data Analysis
=================================================
Expand Down
18 changes: 9 additions & 9 deletions esda/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __init__(self, y, w, transformation="r", permutations=PERMUTATIONS,
# provide .z attribute that is znormalized
sy = y.std()
self.z /= sy

def __moments(self):
self.n = len(self.y)
y = self.y
Expand All @@ -193,7 +193,7 @@ def __moments(self):
s0 = self.w.s0
s2 = self.w.s2
s02 = s0 * s0
v_num = n2 * s1 - n * s2 + 3 * s02
v_num = n2 * s1 - n * s2 + 3 * s02
v_den = (n - 1) * (n + 1) * s02
self.VI_norm = v_num / v_den - (1.0 / (n - 1)) ** 2
self.seI_norm = self.VI_norm ** (1 / 2.)
Expand All @@ -208,7 +208,7 @@ def __moments(self):
A = n * ((n2 - 3 * n + 3) * s1 - n * s2 + 3 * s02)
B = k * ((n2 - n) * s1 - 2 * n * s2 + 6 * s02 )
VIR = (A - B) / ((n - 1) * (n - 2) * (n - 3 ) * s02) - EI*EI
self.VI_rand = VIR
self.VI_rand = VIR
self.seI_rand = VIR ** (1 / 2.)

def __calc(self, z):
Expand Down Expand Up @@ -483,7 +483,7 @@ def Moran_BV_matrix(variables, w, permutations=0, varnames=None):
Examples
--------
open dbf
>>> import libpysal
Expand Down Expand Up @@ -524,7 +524,7 @@ def Moran_BV_matrix(variables, w, permutations=0, varnames=None):
variables_n = variables
except ImportError:
variables_n = variables

results = _Moran_BV_Matrix_array(variables=variables_n, w=w,
permutations=permutations,
varnames=varnames)
Expand Down Expand Up @@ -731,15 +731,15 @@ def by_col(cls, df, events, populations, w=None, inplace=False,

if isinstance(adjusted, bool):
adjusted = [adjusted] * len(events)
if swapname is '':
if swapname == '':
swapname = cls.__name__.lower()

rates = [assuncao_rate(df[e], df[pop]) if adj
else df[e].astype(float) / df[pop]
for e,pop,adj in zip(events, populations, adjusted)]
names = ['-'.join((e,p)) for e,p in zip(events, populations)]
out_df = df.copy()
rate_df = out_df.from_items(list(zip(names, rates))) #trick to avoid importing pandas
rate_df = out_df.from_dict(dict(zip(names, rates))) #trick to avoid importing pandas
stat_df = _univariate_handler(rate_df, names, w=w, inplace=False,
pvalue = pvalue, outvals = outvals,
swapname=swapname,
Expand Down Expand Up @@ -1389,15 +1389,15 @@ def by_col(cls, df, events, populations, w=None, inplace=False,

if isinstance(adjusted, bool):
adjusted = [adjusted] * len(events)
if swapname is '':
if swapname == '':
swapname = cls.__name__.lower()

rates = [assuncao_rate(df[e], df[pop]) if adj
else df[e].astype(float) / df[pop]
for e,pop,adj in zip(events, populations, adjusted)]
names = ['-'.join((e,p)) for e,p in zip(events, populations)]
out_df = df.copy()
rate_df = out_df.from_items(list(zip(names, rates))) #trick to avoid importing pandas
rate_df = out_df.from_dict(dict(zip(names, rates))) #trick to avoid importing pandas
_univariate_handler(rate_df, names, w=w, inplace=True,
pvalue = pvalue, outvals = outvals,
swapname=swapname,
Expand Down
6 changes: 3 additions & 3 deletions esda/smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,8 @@ def by_col(cls, df, e,b, w=None, s=None, **kwargs):
max_len = 0 if len(this_r) > max_len else max_len
rdf.append((outcol, this_r.tolist()))
padded = (r[1] + [None] * max_len for r in rdf)
rdf = list(zip((r[0] for r in rdf), padded))
rdf = pd.DataFrame.from_items(rdf)
rdf = dict(zip((r[0] for r in rdf), padded))
rdf = pd.DataFrame.from_dict(rdf)
return rdf


Expand Down Expand Up @@ -1586,7 +1586,7 @@ def by_col(cls, df, e, b, x_grid, y_grid, geom_col='geometry', **kwargs):
items = [(name, col) for name,col in zip(colnames, [grid[:,0],
grid[:,1],
r.r])]
res.append(pd.DataFrame.from_items(items))
res.append(pd.DataFrame.from_dict(dict(items)))
outdf = pd.concat(res)
return outdf

Expand Down
7 changes: 4 additions & 3 deletions esda/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _univariate_handler(df, cols, stat=None, w=None, inplace=True,
# objects, determine which pvalue types are available, and then grab them
# all if needed.

if pvalue is not '':
if pvalue != '':
outvals.append('p_'+pvalue.lower())
if isinstance(cols, str):
cols = [cols]
Expand All @@ -93,7 +93,7 @@ def column_stat(column):
outcols = ['_'.join((col, val)) for val in outvals]
for colname, attname in zip(outcols, outvals):
df[colname] = stat_obj.__getattribute__(attname)
if swapname is not '':
if swapname != '':
df.columns = [_swap_ending(col, swapname) if col.endswith('_statistic') else col
for col in df.columns]

Expand Down Expand Up @@ -148,7 +148,8 @@ def _bivariate_handler(df, x, y=None, w=None, inplace=True, pvalue='sim',
continue
_univariate_handler(df, cols=xi, w=w, y=df[yi], inplace=True,
pvalue=pvalue, outvals=outvals, swapname='', **kwargs)
if real_swapname is not '':
if real_swapname != '':

df.columns = [_swap_ending(col, real_swapname)
if col.endswith('_statistic')
else col for col in df.columns]
Expand Down
Empty file removed esda/tests/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion esda/tests/test_gamma.py → tests/test_gamma.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import numpy as np
from libpysal.weights.util import lat2W
from ..gamma import Gamma
from esda.gamma import Gamma
from libpysal.common import pandas

PANDAS_EXTINCT = pandas is None
Expand Down
2 changes: 1 addition & 1 deletion esda/tests/test_geary.py → tests/test_geary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from libpysal import examples
from libpysal.common import pandas

from .. import geary
from esda import geary
import numpy as np

PANDAS_EXTINCT = pandas is None
Expand Down
2 changes: 1 addition & 1 deletion esda/tests/test_getisord.py → tests/test_getisord.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import numpy as np

from .. import getisord
from esda import getisord
from libpysal.weights.distance import DistanceBand
from libpysal.common import pandas

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import numpy as np

from ..join_counts import Join_Counts
from esda.join_counts import Join_Counts
from libpysal.weights.util import lat2W
from libpysal.common import pandas

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import numpy as np
import libpysal
from .. import mixture_smoothing as m_s
from esda import mixture_smoothing as m_s


class MS_Tester(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions esda/tests/test_moran.py → tests/test_moran.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import libpysal
from libpysal.common import pandas, RTOL, ATOL
from .. import moran
from esda import moran
import numpy as np


Expand Down Expand Up @@ -32,14 +32,14 @@ def test_variance(self):
mi = moran.Moran(y, w, transformation='B')
np.testing.assert_allclose(mi.VI_rand, 0.059687500000000004, atol=ATOL, rtol=RTOL)
np.testing.assert_allclose(mi.VI_norm, 0.053125000000000006, atol=ATOL, rtol=RTOL)

def test_z_consistency(self):
m1 = moran.Moran(self.y, self.w)
# m2 = moran.Moran_BV(self.x, self.y, self.w) TODO testing for other.z values
m3 = moran.Moran_Local(self.y, self.w)
# m4 = moran.Moran_Local_BV(self.x, self.y, self.w)
np.testing.assert_allclose(m1.z, m3.z, atol=ATOL, rtol=RTOL)


@unittest.skipIf(PANDAS_EXTINCT, 'missing pandas')
def test_by_col(self):
Expand Down
9 changes: 4 additions & 5 deletions esda/tests/test_silhouette.py → tests/test_silhouette.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .. import silhouettes

from esda import silhouettes
import geopandas
import libpysal
import numpy
Expand Down Expand Up @@ -57,7 +56,7 @@ def test_boundary(self):
metric=self.precomputed)
numpy.testing.assert_allclose(known, test, rtol=RTOL, atol=ATOL)
with self.assertRaises(AssertionError):
silhouettes.boundary_silhouette(self.X, self.groups, self.w,
silhouettes.boundary_silhouette(self.X, self.groups, self.w,
metric=self.precomputed - self.precomputed.mean())

def test_path(self):
Expand All @@ -81,11 +80,11 @@ def test_path(self):
-0.03874118,
0.28623703,
0.40062121])
test = silhouettes.path_silhouette(self.X, self.groups, self.w,
test = silhouettes.path_silhouette(self.X, self.groups, self.w,
metric=self.altmetric)
numpy.testing.assert_allclose(known, test, rtol=RTOL, atol=ATOL)
with self.assertRaises(TypeError):
silhouettes.path_silhouette(self.X, self.groups, self.w,
silhouettes.path_silhouette(self.X, self.groups, self.w,
metric=self.precomputed)
with self.assertRaises(AssertionError):
silhouettes.path_silhouette(self.X, self.groups, self.w,
Expand Down
12 changes: 6 additions & 6 deletions esda/tests/test_smaup.py → tests/test_smaup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest
import libpysal
from libpysal.common import pandas, RTOL, ATOL
from ..smaup import Smaup
from ..moran import Moran
from esda.smaup import Smaup
from esda.moran import Moran
import numpy as np


Expand All @@ -17,7 +17,7 @@ def setup(self):
self.rho = Moran(self.y, self.w).I
self.n = len(self.y)
self.k = int(self.n/2)

def test_smaup(self):
self.setup()
sm = Smaup(self.n, self.k, self.rho)
Expand All @@ -29,22 +29,22 @@ def test_smaup(self):
self.assertAlmostEqual(sm.critical_05, 0.3557221333333333)
self.assertAlmostEqual(sm.critical_1, 0.3157950666666666)
self.assertEqual(sm.summary, 'Pseudo p-value > 0.10 (H0 is not rejected)')

def test_sids(self):
from esda import moran
w = libpysal.io.open(libpysal.examples.get_path("sids2.gal")).read()
f = libpysal.io.open(libpysal.examples.get_path("sids2.dbf"))
SIDR = np.array(f.by_col("SIDR74"))
rho = moran.Moran(SIDR, w, two_tailed=False).I
n = len(SIDR)
k = int(n/2)
k = int(n/2)
sm = Smaup(n, k, rho)
np.testing.assert_allclose(sm.smaup, 0.15176796553181948, rtol=RTOL, atol=ATOL)
self.assertAlmostEqual(sm.critical_01, 0.23404000000000003)
self.assertAlmostEqual(sm.critical_05, 0.21088)
self.assertAlmostEqual(sm.critical_1, 0.18239)
self.assertEqual(sm.summary, 'Pseudo p-value > 0.10 (H0 is not rejected)')

@unittest.skipIf(PANDAS_EXTINCT, 'missing pandas')
def test_by_col(self):
from libpysal.io import geotable as pdio
Expand Down
4 changes: 2 additions & 2 deletions esda/tests/test_smoothing.py → tests/test_smoothing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import libpysal
from libpysal.weights.distance import KNN, Kernel
from .. import smoothing as sm
from esda import smoothing as sm
import numpy as np
from libpysal.common import RTOL, ATOL, pandas

Expand Down Expand Up @@ -293,7 +293,7 @@ def test_Headbanging_Triples(self):
self.assertEqual(len(htr.r), len(self.e))
for i in htr.r:
self.assertTrue(i is not None)

@unittest.skip('Deprecated')
def test_Headbanging_Median_Rate(self):
s_ht = sm.Headbanging_Triples(self.d, self.w, k=5)
Expand Down
4 changes: 2 additions & 2 deletions esda/tests/test_util.py → tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import libpysal
from .. import util
from .. import moran
from esda import util
from esda import moran
import numpy as np

class Fdr_Tester(unittest.TestCase):
Expand Down
Loading

0 comments on commit 4db2a7d

Please sign in to comment.