From d2dd334196d0b8b43cfc47a39686c03bc71ca95b Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Sat, 7 Oct 2023 09:44:33 -0400 Subject: [PATCH 1/4] ruff lint repo --- spglm/__init__.py | 8 ++++---- spglm/base.py | 5 ++++- spglm/family.py | 4 ++-- spglm/glm.py | 42 +++++++++++++++++++++--------------------- spglm/iwls.py | 1 - spglm/utils.py | 9 ++++++--- spglm/varfuncs.py | 11 ++++++++--- 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/spglm/__init__.py b/spglm/__init__.py index f312d2a..478c65b 100644 --- a/spglm/__init__.py +++ b/spglm/__init__.py @@ -1,6 +1,6 @@ __version__ = "1.0.8" -from . import glm -from . import family -from . import utils -from . import iwls +from . import glm # noqa F401 +from . import family # noqa F401 +from . import utils # noqa F401 +from . import iwls # noqa F401 diff --git a/spglm/base.py b/spglm/base.py index 190f353..691a594 100644 --- a/spglm/base.py +++ b/spglm/base.py @@ -306,7 +306,10 @@ def cov_params( "l1", "l1_cvxopt_cp", ]: - dot_fun = nan_dot + ###################################################################### + # TODO - remove GH#38 + dot_fun = nan_dot # noqa F821 - `nan_dot` not defined - should remove + ###################################################################### else: dot_fun = np.dot diff --git a/spglm/family.py b/spglm/family.py index c1b6e07..687d200 100644 --- a/spglm/family.py +++ b/spglm/family.py @@ -797,7 +797,7 @@ def __init__(self, link=L.logit): # , n=1.): self.link = link() def starting_mu(self, y): - """ + r""" The starting values for the IRLS algorithm for the Binomial family. A good choice for the binomial family is :math:`\mu_0 = (Y_i + 0.5)/2` """ @@ -980,7 +980,7 @@ def resid_anscombe(self, endog, mu): Journal of the Royal Statistical Society B. 30, 248-75. """ - cox_snell = lambda x: ( + cox_snell = lambda x: ( # noqa E731 - skip "don't use lambda" special.betainc(2 / 3.0, 2 / 3.0, x) * special.beta(2 / 3.0, 2 / 3.0) ) return np.sqrt(self.n) * ( diff --git a/spglm/glm.py b/spglm/glm.py index c1871d0..a2a003a 100644 --- a/spglm/glm.py +++ b/spglm/glm.py @@ -234,26 +234,26 @@ class GLMResults(LikelihoodModelResults): tr_S : trace of the hat matrix S resid_response : array response residuals; defined as y-mu - resid_pearson : array - Pearson residuals; defined as (y-mu)/sqrt(VAR(mu)) - where VAR is the distribution specific variance - function; see family.py and varfuncs.py for more information. - resid_working : array - Working residuals; the working residuals are defined as - resid_response/link'(mu); see links.py for the - derivatives of the link functions. - - resid_anscombe : array - Anscombe residuals; see family.py for - distribution-specific Anscombe residuals. - - resid_deviance : array - deviance residuals; see family.py for - distribution-specific deviance residuals. - - pearson_chi2 : float - chi-Squared statistic is defined as the sum - of the squares of the Pearson residuals + resid_pearson : array + Pearson residuals; defined as (y-mu)/sqrt(VAR(mu)) + where VAR is the distribution specific variance + function; see family.py and varfuncs.py for more information. + resid_working : array + Working residuals; the working residuals are defined as + resid_response/link'(mu); see links.py for the + derivatives of the link functions. + + resid_anscombe : array + Anscombe residuals; see family.py for + distribution-specific Anscombe residuals. + + resid_deviance : array + deviance residuals; see family.py for + distribution-specific deviance residuals. + + pearson_chi2 : float + chi-Squared statistic is defined as the sum + of the squares of the Pearson residuals normalized_cov_params : array k*k, approximates [X.T*X]-1 @@ -339,7 +339,7 @@ def pearson_chi2(self): @cache_readonly def null(self): y = np.reshape(self.y, (-1, 1)) - model = self.model + model = self.model # noqa F841 - `model` never used X = np.ones((len(y), 1)) null_mod = GLM(y, X, family=self.family, offset=self.offset, constant=False) return null_mod.fit().mu diff --git a/spglm/iwls.py b/spglm/iwls.py index a3573a9..1e13288 100644 --- a/spglm/iwls.py +++ b/spglm/iwls.py @@ -2,7 +2,6 @@ from scipy import linalg import numpy.linalg as la from scipy import sparse as sp -from scipy.sparse import linalg as spla from spreg.utils import spdot, spmultiply from .family import Binomial, Poisson diff --git a/spglm/utils.py b/spglm/utils.py index 6713cd3..1893916 100644 --- a/spglm/utils.py +++ b/spglm/utils.py @@ -16,8 +16,11 @@ def _bit_length_26(x): from numpy.lib._version import NumpyVersion except ImportError: import re - - string_types = basestring + + ###################################################################### + # TODO - remove GH#39 + string_types = basestring # noqa F821 - `nan_dot` not defined - should remove + ###################################################################### class NumpyVersion: """Parse and compare numpy version strings. @@ -365,7 +368,7 @@ def __get__(self, obj, type=None): setattr(_cache, name, _cachedval) # Update the reset list if needed (and possible) resetlist = self.resetlist - if resetlist is not (): + if resetlist != (): try: _cache._resetdict[name] = self.resetlist except AttributeError: diff --git a/spglm/varfuncs.py b/spglm/varfuncs.py index 1e78c83..6fd73fd 100644 --- a/spglm/varfuncs.py +++ b/spglm/varfuncs.py @@ -114,9 +114,14 @@ def deriv(self, mu): """ Derivative of the variance function v'(mu) """ - from statsmodels.tools.numdiff import approx_fprime_cs, approx_fprime - # return approx_fprime_cs(mu, self) # TODO fix breaks in `fabs + ######################################################################## + # `approx_fprime_cs` is imported by unused + from statsmodels.tools.numdiff import approx_fprime_cs # noqa F401 + #return approx_fprime_cs(mu, self) # TODO fix breaks in `fabs + ######################################################################## + + from statsmodels.tools.numdiff import approx_fprime # TODO: diag is workaround problem with numdiff for 1d return np.diag(approx_fprime(mu, self)) @@ -205,7 +210,7 @@ def deriv(self, mu): """ Derivative of the variance function v'(mu) """ - from statsmodels.tools.numdiff import approx_fprime_cs, approx_fprime + from statsmodels.tools.numdiff import approx_fprime_cs # TODO: diag workaround proplem with numdiff for 1d return np.diag(approx_fprime_cs(mu, self)) From a2120544907b899b8935e333a2329a6bddc4e956 Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Wed, 11 Oct 2023 09:16:57 -0400 Subject: [PATCH 2/4] base commit --- spglm/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spglm/base.py b/spglm/base.py index 691a594..7cfdda7 100644 --- a/spglm/base.py +++ b/spglm/base.py @@ -400,9 +400,9 @@ def conf_int(self, alpha=0.05, cols=None, method="default"): >>> model = GLM(y, X) >>> results = model.fit() >>> results.conf_int() - array([[ 20.57281401, 72.28355135], - [ -0.42138121, 1.67934915], - [ -0.84292086, -0.12685622]]) + array([[20.57281401, 72.28355135], + [-0.42138121, 1.67934915], + [-0.84292086, -0.12685622]]) Notes ----- From f4dfea64dcc38ec50d98c3057b25838557da1fab Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Wed, 11 Oct 2023 11:03:17 -0400 Subject: [PATCH 3/4] add codecov to README --- README.md | 1 + spglm/utils.py | 2 +- spglm/varfuncs.py | 6 ++++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index de306c8..7d00af8 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![Continuous Integration](https://github.com/pysal/spglm/actions/workflows/testing.yml/badge.svg)](https://github.com/pysal/spglm/actions/workflows/testing.yml) [![Documentation Status](https://readthedocs.org/projects/spglm/badge/?version=latest)](https://spglm.readthedocs.io/en/latest/?badge=latest) [![PyPI version](https://badge.fury.io/py/spglm.svg)](https://badge.fury.io/py/spglm) +[![codecov](https://codecov.io/gh/pysal/spglm/branch/main/graph/badge.svg)](https://codecov.io/gh/pysal/spglm) This module is an adaptation of a portion of [GLM functionality from the diff --git a/spglm/utils.py b/spglm/utils.py index 1893916..4881a41 100644 --- a/spglm/utils.py +++ b/spglm/utils.py @@ -16,7 +16,7 @@ def _bit_length_26(x): from numpy.lib._version import NumpyVersion except ImportError: import re - + ###################################################################### # TODO - remove GH#39 string_types = basestring # noqa F821 - `nan_dot` not defined - should remove diff --git a/spglm/varfuncs.py b/spglm/varfuncs.py index 6fd73fd..8e3bebc 100644 --- a/spglm/varfuncs.py +++ b/spglm/varfuncs.py @@ -118,10 +118,12 @@ def deriv(self, mu): ######################################################################## # `approx_fprime_cs` is imported by unused from statsmodels.tools.numdiff import approx_fprime_cs # noqa F401 - #return approx_fprime_cs(mu, self) # TODO fix breaks in `fabs + + # return approx_fprime_cs(mu, self) # TODO fix breaks in `fabs ######################################################################## - + from statsmodels.tools.numdiff import approx_fprime + # TODO: diag is workaround problem with numdiff for 1d return np.diag(approx_fprime(mu, self)) From b8ce11133b94a3dda081f4ed110f9ebf529b71f7 Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Wed, 11 Oct 2023 11:39:08 -0400 Subject: [PATCH 4/4] martin comment --- spglm/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spglm/utils.py b/spglm/utils.py index 4881a41..f988cad 100644 --- a/spglm/utils.py +++ b/spglm/utils.py @@ -19,7 +19,7 @@ def _bit_length_26(x): ###################################################################### # TODO - remove GH#39 - string_types = basestring # noqa F821 - `nan_dot` not defined - should remove + string_types = basestring # noqa F821 - `basestring` not defined - should remove ###################################################################### class NumpyVersion: