pysal / esda

statistics and classes for exploratory spatial data analysis
https://pysal.org/esda
BSD 3-Clause "New" or "Revised" License
214 stars 55 forks source link

Need to use our own permutations of indices in Local Moran's Index #344

Open martirenom opened 1 month ago

martirenom commented 1 month ago

Hi there,

We are in need to use our own permutations that maintain certain linearity of the data when calculating LMI. This is done at the function Moran_Local(), which in turn calls crand() (esda/crand.py). Could you please implement that the function takes an external array of permuted indices (within crand() called permuted_ids), that are user provided? That would be perfect for our needs and could be tailored to other users that may need different permutation strategies.

For example, vec_permutations(10, 11, 10, 66666) within crand() would result in:

array([[5, 8, 7, 0, 1, 6, 4, 3, 9, 2],
       [0, 3, 8, 5, 4, 2, 6, 1, 9, 7],
       [6, 7, 4, 8, 2, 9, 0, 3, 5, 1],
       [8, 3, 1, 0, 7, 2, 4, 6, 9, 5],
       [3, 9, 7, 2, 8, 6, 0, 5, 1, 4],
       [8, 6, 1, 0, 7, 2, 5, 3, 4, 9],
       [7, 9, 2, 0, 5, 6, 3, 4, 1, 8],
       [8, 3, 7, 4, 0, 5, 2, 1, 9, 6],
       [2, 1, 0, 3, 9, 7, 6, 8, 5, 4],
       [6, 0, 5, 8, 9, 1, 2, 7, 4, 3]])

However, if we want to keep certain linearly restrained permutations we would use an array similar to this:

array([[6, 7, 8, 9, 5, 3, 4, 0, 1, 2],
       [6, 7, 8, 9, 5, 1, 2, 3, 4, 0],
       [5, 6, 7, 8, 9, 1, 2, 3, 4, 0],
       [4, 0, 1, 2, 3, 8, 9, 5, 6, 7],
       [1, 2, 3, 4, 0, 8, 9, 5, 6, 7],
       [2, 3, 4, 0, 1, 9, 5, 6, 7, 8],
       [2, 3, 4, 0, 1, 5, 6, 7, 8, 9],
       [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
       [1, 2, 3, 4, 0, 7, 8, 9, 5, 6],
       [1, 2, 3, 4, 0, 8, 9, 5, 6, 7]])

In summary, in our application, we do not want to use a totally random permutation of the data and such, we propose to add this feature to the function.

Thanks in advance, Marc

martinfleis commented 1 month ago

Hi, I don't think that there's anything blocking the implementation of this. Essentially, if someone passes an array of the right shape to permutations, instead of an integer, we can just use that directly.

Given you have a use case for that, could you try putting together a pull request implementing this?