import pysal as ps
from pysal import weights as w
import numpy as np
import scipy.sparse as sp

def OD(Wo, Wd):
    Wo = Wo.sparse
    Wd = Wd.sparse
    Ww = sp.kron(Wo, Wd)
    return w.WSP2W(w.WSP(Ww))
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)

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.  ]]

origins.weights
{0: [1.0, 1.0], 1: [1.0, 1.0], 2: [1.0, 1.0], 3: [1.0, 1.0]}
import os
os.chdir('/Users/toshan/dev/pysal/pysal/weights')
from spintW import ODW
origins = ps.weights.lat2W(2,2)
dests = ps.weights.lat2W(2,2)
Ww = ODW(origins, dests)
Ww.full()[0]
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.  ]])