Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint repo with ruff #202

Merged
merged 13 commits into from
Oct 17, 2023
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
files: "giddy\/"
repos:
- repo: https://github.com/psf/black
rev: "23.9.1"
hooks:
- id: black
language_version: python3
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.0.292"
hooks:
- id: ruff

ci:
autofix_prs: false
autoupdate_schedule: quarterly
14 changes: 7 additions & 7 deletions giddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

"""

from . import directional
from . import ergodic
from . import markov
from . import mobility
from . import rank
from . import util
from . import sequence
from . import directional # noqa F401, E402
Copy link
Member

Choose a reason for hiding this comment

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

what does # noqa F401, E402 mean?

Copy link
Member Author

Choose a reason for hiding this comment

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

These are codes telling ruff that those lines are not problems:

Code Name Message
F401 unused-import {name} imported but unused; consider using importlib.util.find_spec to test for availability
E402 module-import-not-at-top-of-file Module level import not at top of file

from . import ergodic # noqa F401, E402
from . import markov # noqa F401, E402
from . import mobility # noqa F401, E402
from . import rank # noqa F401, E402
from . import util # noqa F401, E402
from . import sequence # noqa F401, E402
9 changes: 5 additions & 4 deletions giddy/directional.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__all__ = ["Rose"]

import warnings

import numpy as np
from libpysal import weights
from libpysal.common import requires as _requires
Expand All @@ -27,7 +27,8 @@ class Rose(object):
Parameters
----------
Y : array (n,2)
Columns correspond to end-point time periods to calculate LISA vectors for n object.
Columns correspond to end-point time periods to
calculate LISA vectors for n object.
w : PySAL W
Spatial weights object.
k : int
Expand Down Expand Up @@ -349,10 +350,10 @@ def plot_origin(self): # TODO add attribute option to color vectors
Plot vectors of positional transition of LISA values starting
from the same origin.
"""
import matplotlib.cm as cm
# import matplotlib.cm as cm
Copy link
Member Author

Choose a reason for hiding this comment

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

For all commented out lines that I added, they were raising linting errors in ruff.

Copy link
Member Author

Choose a reason for hiding this comment

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

They can actually be entirely removed, but I decided to simply comment out for now so we can discuss.

Copy link
Member

Choose a reason for hiding this comment

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

I see. I think these lines are redundant and we should simply remove them.

Copy link
Member Author

@jGaboardi jGaboardi Oct 10, 2023

Choose a reason for hiding this comment

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

So these lines aren't actually redundant, it's just that import matplotlib.cm as cm isn't used. That goes for all other commented out lines in this PR (except for L284-289 in markov.py).

import matplotlib.pyplot as plt

ax = plt.subplot(111)
# ax = plt.subplot(111)
xlim = [self._dx.min(), self._dx.max()]
ylim = [self._dy.min(), self._dy.max()]
for x, y in zip(self._dx, self._dy):
Expand Down
13 changes: 8 additions & 5 deletions giddy/ergodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def steady_state(P, fill_empty_classes=False):
...
ValueError: Input transition probability matrix has 1 rows full of 0s. Please set fill_empty_classes=True to set diagonal elements for these rows to be 1 to make sure the matrix is stochastic.

"""
""" # noqa E501

P = np.asarray(P)
rows0 = (P.sum(axis=1) == 0).sum()
Expand Down Expand Up @@ -204,7 +204,7 @@ def _mfpt_ergodic(P):
for i in range(k):
A[:, i] = ss
A = A.transpose()
I = np.identity(k)
I = np.identity(k) # noqa E741
Z = la.inv(I - P + A)
E = np.ones_like(Z)
A_diag = np.diag(A)
Expand Down Expand Up @@ -293,7 +293,7 @@ def mfpt(P, fill_empty_classes=False):
Traceback (most recent call last):
...
ValueError: Input transition probability matrix has 1 rows full of 0s. Please set fill_empty_classes=True to set diagonal elements for these rows to be 1 to make sure the matrix is stochastic.
"""
""" # noqa E501

P = np.asarray(P)
rows0 = (P.sum(axis=1) == 0).sum()
Expand Down Expand Up @@ -353,7 +353,10 @@ def mfpt(P, fill_empty_classes=False):

def var_fmpt_ergodic(p):
warn(
"var_fmpt_ergodic is deprecated. It will be replaced in giddy 2.5 with var_fmpt_ergodic",
(
"var_fmpt_ergodic is deprecated. It will be "
"replaced in giddy 2.5 with var_fmpt_ergodic"
),
DeprecationWarning,
stacklevel=2,
)
Expand Down Expand Up @@ -398,7 +401,7 @@ def var_mfpt_ergodic(p):
k = P.shape[0]
A = _steady_state_ergodic(P)
A = np.tile(A, (k, 1))
I = np.identity(k)
I = np.identity(k) # noqa E741
Copy link
Member

Choose a reason for hiding this comment

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

what does # noqa E741 mean?

Copy link
Member Author

Choose a reason for hiding this comment

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

Code Name Message
E741 ambiguous-variable-name Ambiguous variable name: {name}

Z = la.inv(I - P + A)
E = np.ones_like(Z)
D = np.diag(1.0 / np.diag(A))
Expand Down
28 changes: 15 additions & 13 deletions giddy/markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from .ergodic import steady_state, mfpt
from .util import fill_empty_diagonals
from .components import Graph
from warnings import warn
from scipy import stats
from scipy.stats import rankdata
from operator import gt
Expand Down Expand Up @@ -282,12 +281,12 @@ def __init__(self, class_ids, classes=None, fill_empty_classes=False, summary=Tr
)
print(*self.astates_indices, sep=", ")

@property
def mfpt(self):
warn("self._mfpt is deprecated. Please use self._mfpt")
if not hasattr(self, "_mfpt"):
self._mfpt = mfpt(self.p, fill_empty_classes=True)
return self._mfpt
# @property
Copy link
Member

Choose a reason for hiding this comment

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

why is this commented out?

Copy link
Member Author

Choose a reason for hiding this comment

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

mfpt() is defined twice there, which triggers a ruff error:

giddy/markov.py:292:9: F811 Redefinition of unused `mfpt` from line 285

Also, I am a bit confused on the need for two identical methods. Should one be removed?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, I will make the change and add to the PR. Thank you!

# def mfpt(self):
# warn("self._mfpt is deprecated. Please use self._mfpt")
# if not hasattr(self, "_mfpt"):
# self._mfpt = mfpt(self.p, fill_empty_classes=True)
# return self._mfpt

@property
def mfpt(self):
Expand Down Expand Up @@ -708,7 +707,9 @@ class Spatial_Markov(object):
We can easily adjust this assigning `fill_empty_classes = True` when initializing
`Spatial_Markov`.

>>> sm = Spatial_Markov(rpci, w, cutoffs=cc, lag_cutoffs=cc, fill_empty_classes=True)
>>> sm = Spatial_Markov(
... rpci, w, cutoffs=cc, lag_cutoffs=cc, fill_empty_classes=True
... )
>>> for p in sm.P:
... print(p)
[[0.96703297 0.03296703 0. 0. 0. ]
Expand Down Expand Up @@ -1075,7 +1076,7 @@ def _maybe_classify(self, y, k, cutoffs):


def chi2(T1, T2):
"""
r"""
chi-squared test of difference between two transition matrices.

Parameters
Expand Down Expand Up @@ -1849,7 +1850,7 @@ def summary(self, file_name=None, title="Markov Homogeneity Test"):
lead = "-" * width
head = title.center(width)
contents = [lead, head, lead]
l = "Number of regimes: %d" % int(self.m)
l = "Number of regimes: %d" % int(self.m) # noqa E741
k = "Number of classes: %d" % int(self.k)
r = "Regime names: "
r += ", ".join(regime_names)
Expand Down Expand Up @@ -2085,13 +2086,13 @@ def sojourn_time(p, summary=True):
>>> sojourn_time(p)
Sojourn times are infinite for absorbing states! In this Markov Chain, states [2] are absorbing states.
array([ 2., 1., inf])
"""
""" # noqa E501

p = np.asarray(p)
if (p.sum(axis=1) == 0).sum() > 0:
p = fill_empty_diagonals(p)

markovchain = qe.MarkovChain(p)
# markovchain = qe.MarkovChain(p)
pii = p.diagonal()

if not (1 - pii).all():
Expand Down Expand Up @@ -2213,7 +2214,8 @@ class GeoRank_Markov(Markov):

def __init__(self, y, fill_empty_classes=False, summary=True):
y = np.asarray(y)
n = y.shape[0]
# n = y.shape[0]

# resolve ties: All values are given a distinct rank, corresponding
# to the order that the values occur in each cross section.
ranks = np.array([rankdata(col, method="ordinal") for col in y.T]).T
Expand Down
8 changes: 4 additions & 4 deletions giddy/rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from scipy.stats.mstats import rankdata
from scipy.special import erfc
import numpy as np
import scipy as sp
from libpysal import weights


Expand Down Expand Up @@ -184,7 +183,7 @@ def _calc(self, x, y):
n = len(y)
perm = list(range(n))
perm.sort(key=lambda a: (x[a], y[a]))
vals = y[perm]
# vals = y[perm]
ExtraY = 0
ExtraX = 0
ACount = 0
Expand Down Expand Up @@ -315,8 +314,9 @@ class SpatialTau(object):
taus : float
spatial tau values for permuted samples (if permutations>0).
tau_spatial_psim : float
one-sided pseudo p-value for observed tau_spatial under the null
of spatial randomness of rank exchanges (if permutations>0).
one-sided pseudo p-value for observed tau_spatial
under the null of spatial randomness of rank exchanges
(if permutations>0).

Notes
-----
Expand Down
29 changes: 25 additions & 4 deletions giddy/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ class Sequence(object):

1.2 User-defined substitution cost matrix and indel cost

>>> subs_mat = np.array([[0, 0.76, 0.29, 0.05],[0.30, 0, 0.40, 0.60],[0.16, 0.61, 0, 0.26],[0.38, 0.20, 0.12, 0]])
>>> subs_mat = np.array(
... [
... [0, 0.76, 0.29, 0.05],
... [0.30, 0, 0.40, 0.60],
... [0.16, 0.61, 0, 0.26],
... [0.38, 0.20, 0.12, 0]
... ]
... )
>>> indel = subs_mat.max()
>>> seqAna = Sequence([seq1,seq2,seq3], subs_mat=subs_mat, indel=indel)
>>> seqAna.seq_dis_mat
Expand Down Expand Up @@ -117,7 +124,14 @@ class Sequence(object):
between different types is always 1 and indel cost is 2) - give the same
sequence distance matrix as "hamming" distance

>>> subs_mat = np.array([[0., 1., 1., 1.],[1., 0., 1., 1.],[1., 1., 0., 1.],[1., 1., 1., 0.]])
>>> subs_mat = np.array(
... [
... [0., 1., 1., 1.],
... [1., 0., 1., 1.],
... [1., 1., 0., 1.],
... [1., 1., 1., 0.]
... ]
... )
>>> indel = 2
>>> seqAna = Sequence([seq1,seq2,seq3], subs_mat=subs_mat, indel=indel)
>>> seqAna.seq_dis_mat
Expand All @@ -130,7 +144,14 @@ class Sequence(object):
slightly different sequence distance matrix from "hamming" distance since
insertion and deletion is happening

>>> subs_mat = np.array([[0., 1., 1., 1.],[1., 0., 1., 1.],[1., 1., 0.,1.],[1., 1., 1., 0.]])
>>> subs_mat = np.array(
... [
... [0., 1., 1., 1.],
... [1., 0., 1., 1.],
... [1., 1., 0., 1.],
... [1., 1., 1., 0.]
... ]
... )
>>> indel = 1
>>> seqAna = Sequence([seq1,seq2,seq3], subs_mat=subs_mat, indel=indel)
>>> seqAna.seq_dis_mat
Expand All @@ -151,7 +172,7 @@ class Sequence(object):
>>> seqAna = Sequence([seq1,seq2,seq3], indel=indel)
Traceback (most recent call last):
ValueError: Please specify a proper `dist_type` or `subs_mat` and `indel` to proceed!
"""
""" # noqa E501

def __init__(self, y, subs_mat=None, dist_type=None, indel=None, cluster_type=None):
y = np.asarray(y)
Expand Down
1 change: 0 additions & 1 deletion giddy/tests/test_sequence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
import numpy as np
import pytest
from ..sequence import Sequence
Expand Down