Skip to content

Commit f4fb6a2

Browse files
committed
MAINT: Fix automatically documented issues
Fix a number of small issues found with auto code analysis tool
1 parent 321e160 commit f4fb6a2

39 files changed

+96
-86
lines changed

archive/docs/fix_longtable.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
read_file_path = os.path.join(BUILDDIR,'latex','statsmodels.tex')
88
write_file_path = os.path.join(BUILDDIR, 'latex','statsmodels_tmp.tex')
99

10-
read_file = open(read_file_path,'r')
11-
write_file = open(write_file_path, 'w')
10+
read_file = open(read_file_path, 'r', encoding="utf-8")
11+
write_file = open(write_file_path, 'w', encoding="utf-8")
1212

1313
for line in read_file:
1414
if 'longtable}{LL' in line:

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@
405405
# ghissue config
406406
github_project_url = 'https://github.com/statsmodels/statsmodels'
407407

408-
example_context = yaml.safe_load(open('examples/landing.yml'))
408+
example_context = yaml.safe_load(open('examples/landing.yml', encoding="utf-8"))
409409
html_context.update({'examples': example_context})
410410

411411
# --------------- DOCTEST -------------------

examples/python/generic_mle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def nloglikeobs(self, params):
130130
def fit(self, start_params=None, maxiter=10000, maxfun=5000, **kwds):
131131
# we have one additional parameter and we need to add it for summary
132132
self.exog_names.append('alpha')
133-
if start_params == None:
133+
if start_params is None:
134134
# Reasonable starting values
135135
start_params = np.append(np.zeros(self.exog.shape[1]), .5)
136136
# intercept

examples/run_all.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def no_show(*args):
2828
EXAMPLE_FILES = glob.glob('python/*.py')
2929
for example in EXAMPLE_FILES:
3030
KNOWN_BAD_FILE = any([bf in example for bf in BAD_FILES])
31-
with open(example, 'r') as pyfile:
31+
with open(example, 'r', encoding="utf-8") as pyfile:
3232
code = pyfile.read()
3333
try:
3434
sys.stdout = REDIRECT_STDOUT

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# These are strictly installation requirements. Builds requirements are
5151
# managed in pyproject.toml
5252
INSTALL_REQUIRES = []
53-
with open("requirements.txt") as req:
53+
with open("requirements.txt", encoding="utf-8") as req:
5454
for line in req.readlines():
5555
INSTALL_REQUIRES.append(line.split("#")[0].strip())
5656

@@ -266,11 +266,11 @@ def check_source(source_name):
266266
def process_tempita(source_name):
267267
"""Runs pyx.in files through tempita is needed"""
268268
if source_name.endswith("pyx.in"):
269-
with open(source_name, "r") as templated:
269+
with open(source_name, "r", encoding="utf-8") as templated:
270270
pyx_template = templated.read()
271271
pyx = Tempita.sub(pyx_template)
272272
pyx_filename = source_name[:-3]
273-
with open(pyx_filename, "w") as pyx_file:
273+
with open(pyx_filename, "w", encoding="utf-8") as pyx_file:
274274
pyx_file.write(pyx)
275275
file_stats = os.stat(source_name)
276276
try:

statsmodels/base/tests/test_penalized.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def setup_class(cls):
6767
cls._initialize()
6868

6969
@classmethod
70-
def _generate_endog(self, linpred):
70+
def _generate_endog(cls, linpred):
7171
mu = np.exp(linpred)
7272
np.random.seed(999)
7373
y = np.random.poisson(mu)
@@ -404,7 +404,7 @@ def test_cov_type(self):
404404
class CheckPenalizedLogit(CheckPenalizedPoisson):
405405

406406
@classmethod
407-
def _generate_endog(self, linpred):
407+
def _generate_endog(cls, linpred):
408408
mu = 1 / (1 + np.exp(-linpred + linpred.mean() - 0.5))
409409
np.random.seed(999)
410410
y = np.random.rand(len(mu)) < mu
@@ -517,7 +517,7 @@ def test_zeros(self):
517517
class CheckPenalizedBinomCount(CheckPenalizedPoisson):
518518

519519
@classmethod
520-
def _generate_endog(self, linpred):
520+
def _generate_endog(cls, linpred):
521521
mu = 1 / (1 + np.exp(-linpred + linpred.mean() - 0.5))
522522
np.random.seed(999)
523523
n_trials = 5 * np.ones(len(mu), int)
@@ -614,7 +614,7 @@ def _initialize(cls):
614614
class CheckPenalizedGaussian(CheckPenalizedPoisson):
615615

616616
@classmethod
617-
def _generate_endog(self, linpred):
617+
def _generate_endog(cls, linpred):
618618
sig_e = np.sqrt(0.1)
619619
np.random.seed(999)
620620
y = linpred + sig_e * np.random.rand(len(linpred))

statsmodels/datasets/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ def _get_cache(cache):
119119

120120
def _cache_it(data, cache_path):
121121
import zlib
122-
open(cache_path, "wb").write(zlib.compress(data))
122+
with open(cache_path, "wb") as zf:
123+
zf.write(zlib.compress(data))
123124

124125

125126
def _open_cache(cache_path):

statsmodels/discrete/tests/test_discrete.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ def test_issue_339():
16171617
smry = "\n".join(res1.summary().as_text().split('\n')[9:])
16181618
cur_dir = os.path.dirname(os.path.abspath(__file__))
16191619
test_case_file = os.path.join(cur_dir, 'results', 'mn_logit_summary.txt')
1620-
with open(test_case_file, 'r') as fd:
1620+
with open(test_case_file, 'r', encoding="utf-8") as fd:
16211621
test_case = fd.read()
16221622
np.testing.assert_equal(smry, test_case[:-1])
16231623
# smoke test for summary2

statsmodels/examples/ex_lowess.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
rpath = os.path.split(statsmodels.nonparametric.tests.results.__file__)[0]
6666
rfile = os.path.join(rpath, 'test_lowess_frac.csv')
6767
test_data = np.genfromtxt(open(rfile, 'rb'),
68-
delimiter = ',', names = True)
68+
delimiter=',', names=True)
6969
expected_lowess_23 = np.array([test_data['x'], test_data['out_2_3']]).T
7070
expected_lowess_15 = np.array([test_data['x'], test_data['out_1_5']]).T
7171

statsmodels/examples/run_all.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def noop(*args):
4949
try:
5050
print("\n\nExecuting example file", run_all_f)
5151
print("-----------------------" + "-"*len(run_all_f))
52-
exec(open(run_all_f).read())
52+
exec(open(run_all_f, encoding="utf-8").read())
5353
except:
5454
#f might be overwritten in the executed file
5555
print("**********************" + "*"*len(run_all_f))

statsmodels/genmod/generalized_estimating_equations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ class make sense when the model has been fit with regularization.
15151515
update, hm = self._update_regularized(
15161516
mean_params, pen_wt, scad_param, eps)
15171517
if update is None:
1518-
msg = "Singular matrix encountered in regularized GEE update",
1518+
msg = "Singular matrix encountered in regularized GEE update"
15191519
warnings.warn(msg, ConvergenceWarning)
15201520
break
15211521
if itr > miniter and np.sqrt(np.sum(update**2)) < ctol:

statsmodels/genmod/tests/gee_categorical_simulation_check.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def gendat_nominal():
187187

188188
nrep = 100
189189

190-
OUT = open("gee_categorical_simulation_check.txt", "w")
190+
OUT = open("gee_categorical_simulation_check.txt", "w", encoding="utf-8")
191191

192192
np.set_printoptions(formatter={'all': lambda x: "%8.3f" % x},
193193
suppress=True)

statsmodels/genmod/tests/gee_gaussian_simulation_check.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def gendat_nested1():
245245
# older numpy versions do not have formatter option
246246
pass
247247

248-
OUT = open("gee_gaussian_simulation_check.txt", "w")
248+
OUT = open("gee_gaussian_simulation_check.txt", "w", encoding="utf-8")
249249

250250
nrep = 100
251251

statsmodels/genmod/tests/gee_poisson_simulation_check.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def gendat_overdispersed():
178178
np.set_printoptions(formatter={'all': lambda x: "%8.3f" % x},
179179
suppress=True)
180180

181-
OUT = open("gee_poisson_simulation_check.txt", "w")
181+
OUT = open("gee_poisson_simulation_check.txt", "w", encoding="utf-8")
182182

183183
nrep = 100
184184

statsmodels/genmod/tests/gee_simulation_check.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
suppress=True)
1818

1919

20-
OUT = open("gee_simulation_check.txt", "w")
20+
OUT = open("gee_simulation_check.txt", "w", encoding="utf-8")
2121

2222
class GEE_simulator(object):
2323

statsmodels/genmod/tests/results/gee_generate_tests.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def generate_logistic():
2121

2222
p = len(beta)
2323

24-
OUT = open("gee_logistic_1.csv", "w")
24+
OUT = open("gee_logistic_1.csv", "w", encoding="utf-8")
2525

2626
for i in range(nclust):
2727

@@ -62,7 +62,7 @@ def generate_linear():
6262

6363
p = len(beta)
6464

65-
OUT = open("gee_linear_1.csv", "w")
65+
OUT = open("gee_linear_1.csv", "w", encoding="utf-8")
6666

6767
for i in range(nclust):
6868

@@ -102,7 +102,7 @@ def generate_nested_linear():
102102

103103
p = len(beta)
104104

105-
OUT = open("gee_nested_linear_1.csv", "w")
105+
OUT = open("gee_nested_linear_1.csv", "w", encoding="utf-8")
106106

107107
for i in range(nclust):
108108

@@ -130,7 +130,7 @@ def generate_ordinal():
130130

131131
rz = 0.5
132132

133-
OUT = open("gee_ordinal_1.csv", "w")
133+
OUT = open("gee_ordinal_1.csv", "w", encoding="utf-8")
134134

135135
for i in range(200):
136136

@@ -165,7 +165,7 @@ def generate_nominal():
165165

166166
rz = 0.5
167167

168-
OUT = open("gee_nominal_1.csv", "w")
168+
OUT = open("gee_nominal_1.csv", "w", encoding="utf-8")
169169

170170
for i in range(200):
171171

@@ -203,7 +203,7 @@ def generate_poisson():
203203

204204
nclust = 100
205205

206-
OUT = open("gee_poisson_1.csv", "w")
206+
OUT = open("gee_poisson_1.csv", "w", encoding="utf-8")
207207

208208
for i in range(nclust):
209209

statsmodels/genmod/tests/results/results_glm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ def __init__(self):
11931193
# set up data #
11941194
filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
11951195
"inv_gaussian.csv")
1196-
with open(filename, 'r') as fd:
1196+
with open(filename, 'r', encoding="utf-8") as fd:
11971197
data = np.genfromtxt(fd, delimiter=",", dtype=float)[1:]
11981198
self.endog = data[:5000, 0]
11991199
self.exog = data[:5000, 1:]

statsmodels/iolib/table.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def csv2st(csvfile, headers=False, stubs=False, title=None):
9797
Can also supply headers and stubs as tuples of strings.
9898
"""
9999
rows = list()
100-
with open(csvfile, 'r') as fh:
100+
with open(csvfile, 'r', encoding="utf-8") as fh:
101101
reader = csv.reader(fh)
102102
if headers is True:
103103
headers = next(reader)

statsmodels/nonparametric/tests/test_kernel_regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def write2file(self, file_name, data): # pragma: no cover
6464
"""Write some data to a csv file. Only use for debugging!"""
6565
import csv
6666

67-
data_file = csv.writer(open(file_name, "w"))
67+
data_file = csv.writer(open(file_name, "w", encoding="utf-8"))
6868
data = np.column_stack(data)
6969
nobs = max(np.shape(data))
7070
K = min(np.shape(data))

statsmodels/othermod/tests/test_beta.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ def assert_close(a, b, eps):
6767
class TestBetaModel(object):
6868

6969
@classmethod
70-
def setup_class(self):
70+
def setup_class(cls):
7171
model = "I(food/income) ~ income + persons"
72-
self.income_fit = BetaModel.from_formula(model, income).fit()
72+
cls.income_fit = BetaModel.from_formula(model, income).fit()
7373

74-
model = self.model = "methylation ~ gender + CpG"
75-
Z = self.Z = patsy.dmatrix("~ age", methylation)
74+
model = cls.model = "methylation ~ gender + CpG"
75+
Z = cls.Z = patsy.dmatrix("~ age", methylation)
7676
mod = BetaModel.from_formula(model, methylation, exog_precision=Z,
7777
link_precision=links.identity())
78-
self.meth_fit = mod.fit()
78+
cls.meth_fit = mod.fit()
7979
mod = BetaModel.from_formula(model, methylation, exog_precision=Z,
8080
link_precision=links.Log())
81-
self.meth_log_fit = mod.fit()
81+
cls.meth_log_fit = mod.fit()
8282

8383
def test_income_coefficients(self):
8484
rslt = self.income_fit

statsmodels/regression/tests/test_lme.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, meth, irfs, ds_ix):
5858
cur_dir = os.path.dirname(os.path.abspath(__file__))
5959
rdir = os.path.join(cur_dir, 'results')
6060
fname = os.path.join(rdir, "lme%02d.csv" % ds_ix)
61-
with open(fname) as fid:
61+
with open(fname, encoding="utf-8") as fid:
6262
rdr = csv.reader(fid)
6363
header = next(rdr)
6464
data = [[float(x) for x in line] for line in rdr]

statsmodels/sandbox/distributions/tests/test_norm_expan.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,29 @@ class TestExpandNormMom(CheckExpandNorm):
6060
# compare with normal, skew=0, excess_kurtosis=0
6161

6262
@classmethod
63-
def setup_class(kls):
64-
kls.scale = 2
65-
kls.dist1 = stats.norm(1, 2)
66-
kls.mvsk = [1., 2**2, 0, 0]
67-
kls.dist2 = NormExpan_gen(kls.mvsk, mode='mvsk')
63+
def setup_class(cls):
64+
cls.scale = 2
65+
cls.dist1 = stats.norm(1, 2)
66+
cls.mvsk = [1., 2**2, 0, 0]
67+
cls.dist2 = NormExpan_gen(cls.mvsk, mode='mvsk')
6868

6969

7070
class TestExpandNormSample(object):
7171
# do not subclass CheckExpandNorm,
7272
# precision not high enough because of mvsk from data
7373

7474
@classmethod
75-
def setup_class(kls):
76-
kls.dist1 = dist1 = stats.norm(1, 2)
75+
def setup_class(cls):
76+
cls.dist1 = dist1 = stats.norm(1, 2)
7777
np.random.seed(5999)
78-
kls.rvs = dist1.rvs(size=200)
78+
cls.rvs = dist1.rvs(size=200)
7979
#rvs = np.concatenate([rvs, -rvs])
8080
# fix mean and std of sample
8181
#rvs = (rvs - rvs.mean())/rvs.std(ddof=1) * np.sqrt(2) + 1
82-
kls.dist2 = NormExpan_gen(kls.rvs, mode='sample')
82+
cls.dist2 = NormExpan_gen(cls.rvs, mode='sample')
8383

84-
kls.scale = 2
85-
kls.atol_pdf = 1e-3
84+
cls.scale = 2
85+
cls.atol_pdf = 1e-3
8686

8787
def test_ks(self):
8888
# cdf is slow

statsmodels/sandbox/examples/run_all.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
try:
2424
print("Executing example file", run_all_f)
2525
print("-----------------------" + "-"*len(run_all_f))
26-
exec(open(run_all_f).read())
26+
exec(open(run_all_f, encoding="utf-8").read())
2727
except:
2828
#f might be overwritten in the executed file
2929
print("*********************")

statsmodels/sandbox/regression/anova_nistcertified.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
def getnist(filename):
2525
here = os.path.dirname(__file__)
2626
fname = os.path.abspath(os.path.join(here, 'data', filename))
27-
with open(fname, 'r') as fd:
27+
with open(fname, 'r', encoding="utf-8") as fd:
2828
content = fd.read().split('\n')
2929

3030
data = [line.split() for line in content[60:]]

statsmodels/sandbox/tests/maketests_mlabwrap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def save(self, what=None, filename=None, header=True, useinstant=True,
139139
txt.append('%s%s = %s' % (prefix, x, repr(getattr(self,x))))
140140
txt.extend(['','']) #add empty lines at end
141141
if filename is not None:
142-
with open(filename, 'a+') as fd:
142+
with open(filename, 'a+', encoding="utf-8") as fd:
143143
fd.write('\n'.join(txt))
144144
return txt
145145

statsmodels/stats/libqsturng/tests/test_qsturng.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def read_ch(fname):
22-
with open(fname) as f:
22+
with open(fname, encoding="utf-8") as f:
2323
lines = f.readlines()
2424
ps,rs,vs,qs = lzip(*[L.split(',') for L in lines])
2525
return lmap(float, ps), lmap(float, rs),lmap(float, vs), lmap(float, qs)

statsmodels/stats/tests/results/lilliefors_critical_value_simulation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def simulations(sim_type, save=False):
139139
140140
"""
141141
cv_filename = '../../_lilliefors_critical_values.py'
142-
with io.open(cv_filename, 'w', newline='\n') as cv:
142+
with io.open(cv_filename, 'w', newline='\n', encoding="utf-8") as cv:
143143
cv.write(FormatCode(header)[0])
144144
cv.write(FormatCode(normal)[0])
145145
cv.write('\n\n')

statsmodels/stats/tests/test_deltacov.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
class TestDeltacovOLS(object):
1717

18-
def setup_class(self):
18+
@classmethod
19+
def setup_class(cls):
1920
nobs, k_vars = 100, 4
2021
x = np.random.randn(nobs, k_vars)
2122
x[:, 0] = 1
2223
y = x[:, :-1].sum(1) + np.random.randn(nobs)
23-
self.res = OLS(y, x).fit()
24+
cls.res = OLS(y, x).fit()
2425

2526
def test_method(self):
2627
# test Results.method is same as calling function/class

0 commit comments

Comments
 (0)