Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ python:
- 3.6

env:
- PYSAL_PYPI=true
- PYSAL_PYPI=true #testing on dependencies libpysal, esda, mapcalssify on pypi
- PYSAL_PYPI=false

matrix:
Expand All @@ -33,8 +33,12 @@ install:
- pip install -r requirements_tests.txt
- pip install https://github.com/pysal/splot/archive/master.zip
- if "$PYSAL_PYPI"; then
echo 'testing pypi libpysal' && pip install libpysal;
else echo 'testing git libpysal'; git clone https://github.com/pysal/libpysal.git; cd libpysal; pip install .; cd ../;
echo 'testing pypi libpysal esda mapclassify' && pip install libpysal esda mapclassify;
else
echo 'testing git libpysal esda mapclassify';
git clone https://github.com/pysal/libpysal.git; cd libpysal; pip install .; cd ../;
git clone https://github.com/pysal/mapclassify.git; cd mapclassify; pip install .; cd ../;
git clone https://github.com/pysal/esda.git; cd esda; pip install .; cd ../;
fi;
# We need the Agg backend to use Matplotlib on TravisCI, otherwise
# we get an error "DISPLAY variable not set"
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2017, pysal-giddy developers
Copyright 2018, pysal-giddy developers

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Expand Down
3 changes: 1 addition & 2 deletions giddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
from . import markov
from . import mobility
from . import rank
from . import util
from . import api
from . import util
8 changes: 0 additions & 8 deletions giddy/api.py

This file was deleted.

4 changes: 2 additions & 2 deletions giddy/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def check_contiguity(w, neighbors, leaver):

Setup imports and a 25x25 spatial weights matrix on a 5x5 square region.

>>> import libpysal.api as lps
>>> w = lps.lat2W(5, 5)
>>> import libpysal as lps
>>> w = lps.weights.lat2W(5, 5)

Test removing various areas from a subset of the region's areas. In the
first case the subset is defined as observations 0, 1, 2, 3 and 4. The
Expand Down
14 changes: 7 additions & 7 deletions giddy/directional.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import warnings

import numpy as np
from libpysal.api import lag_spatial
from libpysal import weights

_POS8 = np.array([1, 1, 0, 0, 1, 1, 0, 0])
_POS4 = np.array([1, 0, 1, 0])
Expand Down Expand Up @@ -93,9 +93,10 @@ def __init__(self, Y, w, k=8):
Load comma delimited data file in and convert to a numpy array

>>> import libpysal
>>> from giddy.api import Rose
>>> from giddy.directional import Rose
>>> import matplotlib.pyplot as plt
>>> f=open(libpysal.examples.get_path("spi_download.csv"),'r')
>>> file_path = libpysal.examples.get_path("spi_download.csv")
>>> f=open(file_path,'r')
>>> lines=f.readlines()
>>> f.close()
>>> lines=[line.strip().split(",") for line in lines]
Expand Down Expand Up @@ -132,7 +133,7 @@ def __init__(self, Y, w, k=8):
Create our contiguity matrix from an external GAL file and row
standardize the resulting weights

>>> gal=libpysal.open(libpysal.examples.get_path('states48.gal'))
>>> gal=libpysal.io.open(libpysal.examples.get_path('states48.gal'))
>>> w=gal.read()
>>> w.transform='r'

Expand Down Expand Up @@ -299,7 +300,7 @@ def permute(self, permutations=99, alternative='two.sided'):
print(('Bad option for alternative: %s.' % alternative))

def _calc(self, Y, w, k):
wY = lag_spatial(w, Y)
wY = weights.lag_spatial(w, Y)
dx = Y[:, -1] - Y[:,0]
dy = wY[:, -1] - wY[:, 0]
self.wY = wY
Expand Down Expand Up @@ -343,7 +344,6 @@ def plot(self, attribute=None, ax=None, **kwargs):

"""

use_splot = False
try:
import splot.giddy
use_splot = True
Expand Down Expand Up @@ -412,7 +412,7 @@ def plot_vectors(self, arrows=True):
Axes in which the figure is plotted

"""
use_splot = False

try:
import splot.giddy
use_splot = True
Expand Down
6 changes: 3 additions & 3 deletions giddy/ergodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def steady_state(P):
is melting).

>>> import numpy as np
>>> from giddy.api import steady_state
>>> from giddy.ergodic import steady_state
>>> p=np.array([[.5, .25, .25],[.5,0,.5],[.25,.25,.5]])
>>> steady_state(p)
array([0.4, 0.2, 0.4])
Expand Down Expand Up @@ -79,7 +79,7 @@ def fmpt(P):
Examples
--------
>>> import numpy as np
>>> from giddy.api import fmpt
>>> from giddy.ergodic import fmpt
>>> p=np.array([[.5, .25, .25],[.5,0,.5],[.25,.25,.5]])
>>> fm=fmpt(p)
>>> fm
Expand Down Expand Up @@ -137,7 +137,7 @@ def var_fmpt(P):
Examples
--------
>>> import numpy as np
>>> from giddy.api import var_fmpt
>>> from giddy.ergodic import var_fmpt
>>> p=np.array([[.5, .25, .25],[.5,0,.5],[.25,.25,.5]])
>>> vfm=var_fmpt(p)
>>> vfm
Expand Down
49 changes: 24 additions & 25 deletions giddy/markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from .components import Graph
from scipy import stats
from operator import gt
import libpysal.api as ps
from libpysal import weights
from esda.moran import Moran_Local
import mapclassify.api as mc
import mapclassify as mc
import itertools

# TT predefine LISA transitions
Expand Down Expand Up @@ -75,7 +75,7 @@ class Markov(object):
Examples
--------
>>> import numpy as np
>>> from giddy.api import Markov
>>> from giddy.markov import Markov
>>> c = [['b','a','c'],['c','c','a'],['c','b','c']]
>>> c.extend([['a','a','b'], ['a','b','c']])
>>> c = np.array(c)
Expand All @@ -92,8 +92,8 @@ class Markov(object):
US nominal per capita income 48 states 81 years 1929-2009

>>> import libpysal
>>> import mapclassify.api as mc
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> import mapclassify as mc
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> pci = np.array([f.by_col[str(y)] for y in range(1929,2010)])

set classes to quintiles for each year
Expand Down Expand Up @@ -303,13 +303,13 @@ class Spatial_Markov(object):
Examples
--------
>>> import libpysal
>>> from giddy.api import Spatial_Markov
>>> from giddy.markov import Spatial_Markov
>>> import numpy as np
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> pci = np.array([f.by_col[str(y)] for y in range(1929,2010)])
>>> pci = pci.transpose()
>>> rpci = pci/(pci.mean(axis=0))
>>> w = libpysal.open(libpysal.examples.get_path("states48.gal")).read()
>>> w = libpysal.io.open(libpysal.examples.get_path("states48.gal")).read()
>>> w.transform = 'r'

Now we create a `Spatial_Markov` instance for the continuous relative per
Expand Down Expand Up @@ -550,7 +550,7 @@ class Spatial_Markov(object):
Let's still use the US state income time series to demonstrate. We first
discretize them into categories and then pass them to Spatial_Markov.

>>> import mapclassify.api as mc
>>> import mapclassify as mc
>>> y = mc.Quantiles(rpci.flatten(), k=5).yb.reshape(rpci.shape)
>>> np.random.seed(5)
>>> sm = Spatial_Markov(y, w, discrete=True, variable_name='discretized rpci')
Expand Down Expand Up @@ -738,10 +738,10 @@ def _calc(self, y, w):
'''
if self.discrete:
#np.random.seed(24788)
self.lclass_ids = ps.lag_categorical(w, self.class_ids,
self.lclass_ids = weights.lag_categorical(w, self.class_ids,
ties="tryself")
else:
ly = ps.lag_spatial(w, y)
ly = weights.lag_spatial(w, y)
self.lclass_ids, self.lag_cutoffs,self.m = self._maybe_classify(
ly, self.m, self.lag_cutoffs)
self.lclasses = np.arange(self.m)
Expand Down Expand Up @@ -881,13 +881,12 @@ def chi2(T1, T2):
Examples
--------
>>> import libpysal
>>> from giddy.api import Spatial_Markov
>>> from giddy.markov import chi2
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> from giddy.markov import Spatial_Markov, chi2
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> years = list(range(1929, 2010))
>>> pci = np.array([f.by_col[str(y)] for y in years]).transpose()
>>> rpci = pci/(pci.mean(axis=0))
>>> w = libpysal.open(libpysal.examples.get_path("states48.gal")).read()
>>> w = libpysal.io.open(libpysal.examples.get_path("states48.gal")).read()
>>> w.transform='r'
>>> sm = Spatial_Markov(rpci, w, fixed=True)
>>> T1 = sm.T[0]
Expand Down Expand Up @@ -1078,11 +1077,11 @@ class LISA_Markov(Markov):
--------
>>> import libpysal
>>> import numpy as np
>>> from giddy.api import LISA_Markov
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> from giddy.markov import LISA_Markov
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> years = list(range(1929, 2010))
>>> pci = np.array([f.by_col[str(y)] for y in years]).transpose()
>>> w = libpysal.open(libpysal.examples.get_path("states48.gal")).read()
>>> w = libpysal.io.open(libpysal.examples.get_path("states48.gal")).read()
>>> lm = LISA_Markov(pci,w)
>>> lm.classes
array([1, 2, 3, 4])
Expand Down Expand Up @@ -1194,7 +1193,7 @@ def __init__(self, y, w, permutations=0,

ybar = y.mean(axis=0)
r = y / ybar
ylag = np.array([ps.lag_spatial(w, yt) for yt in y])
ylag = np.array([weights.lag_spatial(w, yt) for yt in y])
rlag = ylag / ybar
rc = r < 1.
rlagc = rlag < 1.
Expand Down Expand Up @@ -1244,11 +1243,11 @@ def spillover(self, quadrant=1, neighbors_on=False):
Examples
--------
>>> import libpysal
>>> from giddy.api import LISA_Markov
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> from giddy.markov import LISA_Markov
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> years = list(range(1929, 2010))
>>> pci = np.array([f.by_col[str(y)] for y in years]).transpose()
>>> w = libpysal.open(libpysal.examples.get_path("states48.gal")).read()
>>> w = libpysal.io.open(libpysal.examples.get_path("states48.gal")).read()
>>> np.random.seed(10)
>>> lm_random = LISA_Markov(pci, w, permutations=99)
>>> r = lm_random.spillover()
Expand Down Expand Up @@ -1383,7 +1382,7 @@ def kullback(F):
Examples
--------
>>> import numpy as np
>>> from giddy.api import kullback
>>> from giddy.markov import kullback
>>> s1 = np.array([
... [ 22, 11, 24, 2, 2, 7],
... [ 5, 23, 15, 3, 42, 6],
Expand Down Expand Up @@ -1469,8 +1468,8 @@ def prais(pmat):
--------
>>> import numpy as np
>>> import libpysal
>>> from giddy.api import prais
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> from giddy.markov import Markov,prais
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> pci = np.array([f.by_col[str(y)] for y in range(1929,2010)])
>>> q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
>>> m = Markov(q5)
Expand Down
32 changes: 19 additions & 13 deletions giddy/mobility.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ def markov_mobility(p, measure="P",ini=None):
--------
>>> import numpy as np
>>> import libpysal
>>> import mapclassify.api as mc
>>> from giddy.api import Markov, markov_mobility
>>> f = libpysal.open(libpysal.examples.get_path("usjoin.csv"))
>>> import mapclassify as mc
>>> from giddy.markov import Markov
>>> from giddy.mobility import markov_mobility
>>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
>>> pci = np.array([f.by_col[str(y)] for y in range(1929,2010)])
>>> q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
>>> m = Markov(q5)
Expand All @@ -68,32 +69,37 @@ def markov_mobility(p, measure="P",ini=None):

(1) Estimate Shorrock1 mobility index:

>>> markov_mobility(m.p, measure="P")
0.19758992000997844
>>> mobi_1 = markov_mobility(m.p, measure="P")
>>> print("{:.5f}".format(mobi_1))
0.19759

(2) Estimate Shorrock2 mobility index:

>>> markov_mobility(m.p, measure="D")
0.6068485462369559
>>> mobi_2 = markov_mobility(m.p, measure="D")
>>> print("{:.5f}".format(mobi_2))
0.60685

(3) Estimate Sommers and Conlisk mobility index:

>>> markov_mobility(m.p, measure="L2")
0.03978200230815965
>>> mobi_3 = markov_mobility(m.p, measure="L2")
>>> print("{:.5f}".format(mobi_3))
0.03978

(4) Estimate Bartholomew1 mobility index (note that the initial
distribution should be given):

>>> ini = np.array([0.1,0.2,0.2,0.4,0.1])
>>> markov_mobility(m.p, measure = "B1", ini=ini)
0.2277675878319787
>>> mobi_4 = markov_mobility(m.p, measure = "B1", ini=ini)
>>> print("{:.5f}".format(mobi_4))
0.22777

(5) Estimate Bartholomew2 mobility index (note that the initial
distribution should be given):

>>> ini = np.array([0.1,0.2,0.2,0.4,0.1])
>>> markov_mobility(m.p, measure = "B2", ini=ini)
0.04636660119478926
>>> mobi_5 = markov_mobility(m.p, measure = "B2", ini=ini)
>>> print("{:.5f}".format(mobi_5))
0.04637

"""

Expand Down
Loading