Skip to content

Use numpy rng in moran module#448

Open
martibosch wants to merge 2 commits intopysal:mainfrom
martibosch:feat-moran-rng
Open

Use numpy rng in moran module#448
martibosch wants to merge 2 commits intopysal:mainfrom
martibosch:feat-moran-rng

Conversation

@martibosch
Copy link
Copy Markdown

Summary

Added seed parameter to Moran_BV and Moran_Rate to enable reproducible permutation-based significance testing, and update Moran_BV to use the NumPy Generator API.

Changes

esda/moran.py:

  • Moran_BV: replaced legacy np.random.permutation with np.random.default_rng(seed).permutation(), added seed parameter
  • Moran_Rate: added seed parameter, forwarded to Moran.init

esda/tests/test_moran.py:

  • Added TestMoranBV class with test_seed_reproducibility and test_seed_different_values
  • Added same two seed tests to TestMoranRate

Comment thread esda/tests/test_moran.py
mi1 = moran.Moran_Rate(self.e, self.b, w, permutations=99, seed=SEED)
mi2 = moran.Moran_Rate(self.e, self.b, w, permutations=99, seed=SEED + 1)
np.testing.assert_allclose(mi1.I, mi2.I)
assert mi1.p_sim != mi2.p_sim
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The p_sim is likely to be the same even with different seeds due to the same level of spatial autocorrelation in the data. Maybe better to omit the test on this attribute here, since you do check if the sim values differ with different seeds.

Comment thread esda/moran.py

def __init__(self, x, y, w, transformation="r", permutations=PERMUTATIONS):
def __init__(
self, x, y, w, transformation="r", permutations=PERMUTATIONS, seed=None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring should be updated to reflect the new parameter.

Copy link
Copy Markdown
Member

@sjsrey sjsrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

Some comments to help with test failures, and alignment with SPEC 07

Comment thread esda/moran.py

def __init__(self, x, y, w, transformation="r", permutations=PERMUTATIONS):
def __init__(
self, x, y, w, transformation="r", permutations=PERMUTATIONS, seed=None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are adopting spec 07 across pysal, and this recommends deprecating the seed argument infavor of an rng argument.

Comment thread esda/moran.py
transformation="r",
permutations=PERMUTATIONS,
two_tailed=True,
seed=None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/seed/rng/ for spec07

Comment thread esda/moran.py
transformation=transformation,
permutations=permutations,
two_tailed=two_tailed,
seed=seed,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/seed/rng/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants