Skip to content

Commit

Permalink
Add a pre-commit configuration to conform to new pydata conventions.
Browse files Browse the repository at this point in the history
This patch also reformats code using the `ruff` code formatter.
  • Loading branch information
matthewwardrop committed Nov 11, 2024
1 parent 9fba33e commit 5b40a64
Show file tree
Hide file tree
Showing 43 changed files with 3,863 additions and 2,496 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ doc/cdoc/build
ehthumbs.db
Icon?
Thumbs.db

# Test generated files #
########################
.python-version
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: fix-byte-order-marker
- id: destroyed-symlinks
- id: fix-encoding-pragma
args: ["--remove"]
- id: mixed-line-ending
- id: name-tests-test
args: ["--pytest-test-first"]
- id: pretty-format-json
args: ["--autofix", "--no-ensure-ascii"]
exclude: ".ipynb"

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
hooks:
- id: ruff-format
types_or: [ python, pyi, jupyter ]
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ the cheap trick way of doing it is:
def arima(n, m):
return ArimaModelType(n, m)
and then in the factor type sniffing code detect these things and
separate them out from "real" factors.
separate them out from "real" factors.

* make sure that pickling works
- And make sure that if we allow it at all, then it's sustainable!
Expand Down
4 changes: 2 additions & 2 deletions doc/R-comparison.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Differences from R:
# R:
> qr(model.matrix(~ 1 + a:b))$rank
[1] 4
However, the matrix produced for this formula has 5 columns, meaning
that it contains redundant overspecification:

Expand Down Expand Up @@ -149,7 +149,7 @@ Differences from R:
use a full-rank encoding for ``b``. Therefore, we *should* use a
full-rank encoding for ``b``, and produce a model matrix with 6
columns. But in fact, R gives us only 4:

.. code-block:: rconsole
# R:
Expand Down
24 changes: 13 additions & 11 deletions doc/_examples/example_lm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import numpy as np
from patsy import dmatrices, build_design_matrices


class LM(object):
"""An example ordinary least squares linear model class, analogous to R's
lm() function. Don't use this in real life, it isn't properly tested."""

def __init__(self, formula_like, data={}):
y, x = dmatrices(formula_like, data, 1)
self.nobs = x.shape[0]
Expand All @@ -12,27 +14,27 @@ def __init__(self, formula_like, data={}):
self._x_design_info = x.design_info

def __repr__(self):
summary = ("Ordinary least-squares regression\n"
" Model: %s ~ %s\n"
" Regression (beta) coefficients:\n"
% (self._y_design_info.describe(),
self._x_design_info.describe()))
summary = (
"Ordinary least-squares regression\n"
" Model: %s ~ %s\n"
" Regression (beta) coefficients:\n"
% (self._y_design_info.describe(), self._x_design_info.describe())
)
for name, value in zip(self._x_design_info.column_names, self.betas):
summary += " %s: %0.3g\n" % (name, value[0])
return summary

def predict(self, new_data):
(new_x,) = build_design_matrices([self._x_design_info],
new_data)
(new_x,) = build_design_matrices([self._x_design_info], new_data)
return np.dot(new_x, self.betas)

def loglik(self, new_data):
(new_y, new_x) = build_design_matrices([self._y_design_info,
self._x_design_info],
new_data)
(new_y, new_x) = build_design_matrices(
[self._y_design_info, self._x_design_info], new_data
)
new_pred = np.dot(new_x, self.betas)
sigma2 = self.rss / self.nobs
# It'd be more elegant to use scipy.stats.norm.logpdf here, but adding
# a dependency on scipy makes the docs build more complicated:
Z = -0.5 * np.log(2 * np.pi * sigma2)
return Z + -0.5 * (new_y - new_x) ** 2/sigma2
return Z + -0.5 * (new_y - new_x) ** 2 / sigma2
22 changes: 15 additions & 7 deletions doc/_examples/example_treatment.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import numpy as np


class MyTreat(object):
def __init__(self, reference=0):
self.reference = reference

def code_with_intercept(self, levels):
return ContrastMatrix(np.eye(len(levels)),
["[My.%s]" % (level,) for level in levels])
return ContrastMatrix(
np.eye(len(levels)), ["[My.%s]" % (level,) for level in levels]
)

def code_without_intercept(self, levels):
eye = np.eye(len(levels) - 1)
contrasts = np.vstack((eye[:self.reference, :],
np.zeros((1, len(levels) - 1)),
eye[self.reference:, :]))
suffixes = ["[MyT.%s]" % (level,) for level in
levels[:self.reference] + levels[self.reference + 1:]]
contrasts = np.vstack(
(
eye[: self.reference, :],
np.zeros((1, len(levels) - 1)),
eye[self.reference :, :],
)
)
suffixes = [
"[MyT.%s]" % (level,)
for level in levels[: self.reference] + levels[self.reference + 1 :]
]
return ContrastMatrix(contrasts, suffixes)
2 changes: 1 addition & 1 deletion doc/_static/facebox.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@
.facebox_overlayBG {
background-color: #000;
z-index: 99;
}
}
4 changes: 2 additions & 2 deletions doc/_static/show-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ function scrapeText(codebox){
return newlines.join('\\n');
}

$(document).ready(
$(document).ready(
function() {
// grab all code boxes
var ipythoncode = $(".highlight-ipython");
$.each(ipythoncode, function() {
var code = scrapeText($(this).text());
// give them a facebox pop-up with plain text code
// give them a facebox pop-up with plain text code
$(this).append('<span style="text-align:right; display:block; margin-top:-10px; margin-left:10px; font-size:60%"><a href="javascript: jQuery.facebox(\'<textarea cols=80 rows=10 readonly style=margin:5px onmouseover=javascript:this.select();>'+htmlescape(htmlescape(code))+'</textarea>\');">View Code</a></span>');
$(this,"textarea").select();
});
Expand Down
2 changes: 1 addition & 1 deletion doc/categorical-coding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ As an example, here's a simplified version of the built-in
:class:`Treatment` coding object:

.. literalinclude:: _examples/example_treatment.py

.. ipython:: python
:suppress:
Expand Down
Loading

0 comments on commit 5b40a64

Please sign in to comment.