In [1]:
import pysal as ps
from pysal import weights as w
import numpy as np
import scipy.sparse as sp

In [2]:
def OD(Wo, Wd):
    Wo = Wo.sparse
    Wd = Wd.sparse
    Ww = sp.kron(Wo, Wd)
    return w.WSP2W(w.WSP(Ww))

In [7]:
origins = ps.weights.lat2W(4,4)
dests = ps.weights.lat2W(4,4)
Ww = OD(origins, dests)
Ww.transform = 'r'
print Ww.full()[0].shape


(256, 256)

In [4]:
flows = np.random.randint(0,100, (4,4))
np.fill_diagonal(flows, 0)
flows = flows.reshape((16,1))
print flows
slag = ps.lag_spatial(Ww, flows)
print slag


[[ 0]
 [38]
 [36]
 [86]
 [30]
 [ 0]
 [69]
 [19]
 [84]
 [43]
 [ 0]
 [80]
 [58]
 [ 3]
 [35]
 [ 0]]
[[ 28.  ]
 [ 53.25]
 [ 53.25]
 [ 28.  ]
 [ 28.  ]
 [ 36.  ]
 [ 36.  ]
 [ 28.  ]
 [ 28.  ]
 [ 36.  ]
 [ 36.  ]
 [ 28.  ]
 [ 28.  ]
 [ 53.25]
 [ 53.25]
 [ 28.  ]]

In [5]:
origins.weights


Out[5]:
{0: [1.0, 1.0], 1: [1.0, 1.0], 2: [1.0, 1.0], 3: [1.0, 1.0]}

In [6]:
import os
os.chdir('/Users/toshan/dev/pysal/pysal/weights')
from pysal.model.spintW import ODW

In [16]:
origins = ps.weights.lat2W(2,2)
dests = ps.weights.lat2W(2,2)
Ww = ODW(origins, dests)

In [17]:
Ww.full()[0]


Out[17]:
array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ,  0.  ,
         0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25,  0.25,
         0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25,  0.25,
         0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ,  0.  ,
         0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ],
       [ 0.25,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25],
       [ 0.25,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25],
       [ 0.  ,  0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ],
       [ 0.  ,  0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ],
       [ 0.25,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25],
       [ 0.25,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25],
       [ 0.  ,  0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,
         0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ,  0.  ,
         0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25,  0.25,
         0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.25,  0.25,
         0.  ,  0.  ,  0.25,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.25,  0.25,  0.  ,  0.  ,
         0.25,  0.25,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ]])

In [ ]:


In [ ]: