import pysal.lib 
import numpy as np
from pysal.explore.giddy.directional import Rose
%matplotlib inline

f = open(pysal.lib.examples.get_path('spi_download.csv'), 'r')
lines = f.readlines()
f.close()

lines = [line.strip().split(",") for line in lines]
names = [line[2] for line in lines[1:-5]]
data = np.array([list(map(int, line[3:])) for line in lines[1:-5]])

sids  = list(range(60))
out = ['"United States 3/"',
      '"Alaska 3/"',
      '"District of Columbia"',
      '"Hawaii 3/"',
      '"New England"','"Mideast"',
       '"Great Lakes"',
       '"Plains"',
       '"Southeast"',
       '"Southwest"',
       '"Rocky Mountain"',
       '"Far West 3/"']

snames = [name for name in names if name not in out]

sids = [names.index(name) for name in snames]

states = data[sids,:]
us = data[0]
years = np.arange(1969, 2009)

rel = states/(us*1.)

gal = pysal.lib.io.open(pysal.lib.examples.get_path('states48.gal'))
w = gal.read()
w.transform = 'r'

Y = rel[:, [0, -1]]

Y.shape

Y

np.random.seed(100)
r4 = Rose(Y, w, k=4)

"""
Explanation: Directional Analysis of Dynamic LISAs
This notebook demonstrates how to use Rose diagram based inference for directional LISAs.
End of explanation
"""


r4.plot()

r4.plot(Y[:,0]) # condition on starting relative income

r4.plot(attribute=r4.lag[:,0]) # condition on the spatial lag of starting relative income

r4.plot_vectors() # lisa vectors

r4.plot_vectors(arrows=False)

r4.plot_origin() # origin standardized

"""
Explanation: Visualization
End of explanation
"""


r4.cuts

r4.counts

np.random.seed(1234)

r4.permute(permutations=999)

r4.p

"""
Explanation: Inference
The Rose class contains methods to carry out inference on the circular distribution of the LISA vectors. The first approach is based on a two-sided alternative where the null is that the distribution of the vectors across the segments reflects independence in the movements of the focal unit and its spatial lag. Inference is based on random spatial permutations under the null.
End of explanation
"""


r4.permute(alternative='positive', permutations=999)
r4.p

r4.expected_perm

"""
Explanation: Here all the four sector counts are signficantly different from their expectation under the null.
A directional test can also be implemented. Here the direction of the departure from the null due to positive co-movement of a focal unit and its spatial lag over the time period results in two  two general cases. For sectors in the positive quadrants (I and III), the observed counts are considered extreme if they are larger than expectation, while for the negative quadrants (II, IV) the observed counts are considered extreme if they are small than the expected counts under the null.
End of explanation
"""


r4.permute(alternative='negative', permutations=999)
r4.p

"""
Explanation: Finally, a directional alternative reflecting negative association between the movement of the focal unit and its lag has the complimentary interpretation to the positive alternative: lower counts in I and III, and higher counts in II and IV relative to the null.
End of explanation
"""


