Skip to content

Commit

Permalink
fix RUFF rules
Browse files Browse the repository at this point in the history
  • Loading branch information
juanitorduz committed Nov 5, 2024
1 parent c14781e commit 5e24787
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 128 deletions.
190 changes: 95 additions & 95 deletions pixi.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyfixest/did/did2s.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _did2s_estimate(
_first_u = data[f"{yname}"].to_numpy().flatten() - Y_hat
data[f"{yname}_hat"] = _first_u

# intercept needs to be dropped by hand due to the presence of fixed effects in the first stage # noqa: W505
# intercept needs to be dropped by hand due to the presence of fixed effects in the first stage
fit2 = cast(
Feols,
feols(
Expand Down Expand Up @@ -302,7 +302,7 @@ def _did2s_vcov(
else:
weights_array = np.sqrt(data[weights].to_numpy())

# some formula parsing to get the correct formula for the first and second stage model matrix # noqa: W505
# some formula parsing to get the correct formula for the first and second stage model matrix
first_stage_x, first_stage_fe = first_stage.split("|")
first_stage_fe_list = [f"C({i})" for i in first_stage_fe.split("+")]
first_stage_fe_fml = "+".join(first_stage_fe_list)
Expand Down
2 changes: 1 addition & 1 deletion pyfixest/estimation/FixestMulti_.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def _estimate_all_models(
FIT.wls_transform()

FIT.get_fit()
# if X is empty: no inference (empty X only as shorthand for demeaning) # noqa: W505
# if X is empty: no inference (empty X only as shorthand for demeaning)
if not FIT._X_is_empty:
# inference
vcov_type = _get_vcov_type(vcov, fval)
Expand Down
2 changes: 1 addition & 1 deletion pyfixest/estimation/FormulaParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ def _dict_to_list_of_formulas(unpacked: dict[str, list[str]]) -> list[str]:
else:
variable_fml = [res["variable"][i] for i in range(len(res["variable"]))]
if variable_type in ["sw0", "csw0"]:
variable_fml = ["0"] + variable_fml
variable_fml = ["0", *variable_fml]

fml_list = []
if variable_fml:
Expand Down
12 changes: 6 additions & 6 deletions pyfixest/estimation/feols_.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def _vcov_crv3_fast(self, clustid, cluster_col):
tXX = np.transpose(_X) @ _X
tXy = np.transpose(_X) @ _Y

# compute leave-one-out regression coefficients (aka clusterjacks') # noqa: W505
# compute leave-one-out regression coefficients (aka clusterjacks')
for ixg, g in enumerate(clustid):
Xg = _X[np.equal(g, cluster_col)]
Yg = _Y[np.equal(g, cluster_col)]
Expand Down Expand Up @@ -2420,14 +2420,14 @@ def _check_vcov_input(vcov: Union[str, dict[str, str]], data: pd.DataFrame):
"""
assert isinstance(vcov, (dict, str, list)), "vcov must be a dict, string or list"
if isinstance(vcov, dict):
assert list(vcov.keys())[0] in [
assert next(iter(vcov.keys())) in [
"CRV1",
"CRV3",
], "vcov dict key must be CRV1 or CRV3"
assert isinstance(
list(vcov.values())[0], str
next(iter(vcov.values())), str
), "vcov dict value must be a string"
deparse_vcov = list(vcov.values())[0].split("+")
deparse_vcov = next(iter(vcov.values())).split("+")
assert len(deparse_vcov) <= 2, "not more than twoway clustering is supported"

if isinstance(vcov, list):
Expand Down Expand Up @@ -2471,8 +2471,8 @@ def _deparse_vcov_input(vcov: Union[str, dict[str, str]], has_fixef: bool, is_iv
The name of the cluster variable.
"""
if isinstance(vcov, dict):
vcov_type_detail = list(vcov.keys())[0]
deparse_vcov = list(vcov.values())[0].split("+")
vcov_type_detail = next(iter(vcov.keys()))
deparse_vcov = next(iter(vcov.values())).split("+")
if isinstance(deparse_vcov, str):
deparse_vcov = [deparse_vcov]
deparse_vcov = [x.replace(" ", "") for x in deparse_vcov]
Expand Down
2 changes: 1 addition & 1 deletion pyfixest/estimation/feols_compressed_.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def prepare_model_matrix(self):
+ [col for col in data_long_mundlak.columns if col != "Intercept"]
)

self._coefnames = ["Intercept"] + covars_updated
self._coefnames = ["Intercept", *covars_updated]
self._fe = None

else:
Expand Down
2 changes: 1 addition & 1 deletion pyfixest/estimation/fepois_.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def _check_for_separation(

if separation_na:
warnings.warn(
f"{str(len(separation_na))} observations removed because of separation."
f"{len(separation_na)!s} observations removed because of separation."
)

return list(separation_na)
Expand Down
9 changes: 5 additions & 4 deletions pyfixest/report/summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def etable(
pandas.DataFrame
A styled DataFrame with the coefficients and standard errors of the models.
When output is "tex", the LaTeX code is returned as a string.
""" # noqa: D301
"""
if signif_code is None:
signif_code = [0.001, 0.01, 0.05]
assert (
Expand Down Expand Up @@ -689,7 +689,8 @@ def _parse_coef_fmt(coef_fmt: str, custom_stats: dict):
r"\[",
r"\]",
",",
] + custom_elements
*custom_elements,
]
allowed_elements.sort(key=len, reverse=True)

coef_fmt_elements = re.findall("|".join(allowed_elements), coef_fmt)
Expand Down Expand Up @@ -1249,7 +1250,7 @@ def mean_newline_std(x):

# Calculate the desired statistics
if (byrow is not None) and (bycol is not None):
bylist = [byrow] + bycol
bylist = [byrow, *bycol]

Check warning on line 1253 in pyfixest/report/summarize.py

View check run for this annotation

Codecov / codecov/patch

pyfixest/report/summarize.py#L1253

Added line #L1253 was not covered by tests
res = df.groupby(bylist).agg(agg_funcs)
if (byrow is None) and (bycol is None):
res = df.agg(agg_funcs)
Expand Down Expand Up @@ -1342,7 +1343,7 @@ def mean_newline_std(x):
# Finally we want to have the objects first and then the statistics
if not isinstance(res.columns, pd.MultiIndex):
res.columns = pd.MultiIndex.from_tuples(res.columns)
res.columns = res.columns.reorder_levels(bycol + ["Statistics"])
res.columns = res.columns.reorder_levels([*bycol, "Statistics"])
# And sort it properly by the variables
# (we want to preserve the order of the lowest level for the stats)
levels_to_sort = list(range(res.columns.nlevels - 1))
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ select = [
"D", # flake8-docstrings
"UP", # pyupgrade
"SIM", # flake8-simplify
"RUF", # ruff,
"TRY", # tryceratops
]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_did.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_errors(data):
rng = np.random.default_rng(12)
data["treat2"] = rng.choice([0, 1, 2], size=len(data))
with pytest.raises(ValueError):
fit = did2s_pyfixest( # noqa: F841
fit = did2s_pyfixest(
data,
yname="dep_var",
first_stage="~ X | state + year",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_separation():
with pytest.warns(
UserWarning, match="2 observations removed because of separation."
):
fepois("Y ~ X | fe1", data=example1, vcov="hetero", separation_check=["fe"]) # noqa: F841
fepois("Y ~ X | fe1", data=example1, vcov="hetero", separation_check=["fe"])

if False:
# this example is taken from ppmlhdfe's primer on separation https://github.com/sergiocorreia/ppmlhdfe/blob/master/guides/separation_primer.md
Expand All @@ -52,7 +52,7 @@ def test_separation():
with pytest.warns(
UserWarning, match="2 observations removed because of separation."
):
fepois("Y ~ X1 + X2", data=example2, vcov="hetero", separation_check=["ir"]) # noqa: F841
fepois("Y ~ X1 + X2", data=example2, vcov="hetero", separation_check=["ir"])

# ppmlhdfe test data sets (check readme in data/ppmlhdfe_separation_examples)
path = os.path.dirname(os.path.abspath(__file__))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_predict_resid_fixef.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_vs_fixest(data, fml):
raise ValueError("Predictions for OLS are not of the same length.")

# test predict for Poisson
# if not np.allclose(fepois_mod.predict(data = data2), stats.predict(r_fixest_pois, newdata = data2)): # noqa: W505
# if not np.allclose(fepois_mod.predict(data = data2), stats.predict(r_fixest_pois, newdata = data2)):
# raise ValueError("Predictions for Poisson are not equal")

# test resid for OLS
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_CRV3_fixef(N, seed, beta_type, error_type):
@pytest.mark.slow
def run_crv3_poisson():
data = get_data(N=1000, seed=1234, beta_type="1", error_type="1", model="Fepois")
fit = fepois( # noqa: F841
fit = fepois(
fml="Y~X1 + C(f2)",
data=data,
vcov={"CRV3": "f1"},
Expand Down
24 changes: 12 additions & 12 deletions tests/test_vs_fixest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
("Y ~ X1 + i(f1, ref = 1) | f2 + f3"),
("Y ~ X1 + i(f1) + i(f2)"),
("Y ~ X1 + i(f1, ref = 1) + i(f2, ref = 2)"),
# ("Y ~ X1 + C(f1):C(fe2)"), # currently does not work as C():C() translation not implemented # noqa: W505
# ("Y ~ X1 + C(f1):C(fe2) | f3"), # currently does not work as C():C() translation not implemented # noqa: W505
# ("Y ~ X1 + C(f1):C(fe2)"), # currently does not work as C():C() translation not implemented
# ("Y ~ X1 + C(f1):C(fe2) | f3"), # currently does not work as C():C() translation not implemented
("Y ~ X1 + X2:f1"),
("Y ~ X1 + X2:f1 | f3"),
("Y ~ X1 + X2:f1 | f3 + f1"),
# ("log(Y) ~ X1:X2 | f3 + f1"), # currently, causes big problems for Fepois (takes a long time) # noqa: W505
# ("log(Y) ~ log(X1):X2 | f3 + f1"), # currently, causes big problems for Fepois (takes a long time) # noqa: W505
# ("Y ~ X2 + exp(X1) | f3 + f1"), # currently, causes big problems for Fepois (takes a long time) # noqa: W505
# ("log(Y) ~ X1:X2 | f3 + f1"), # currently, causes big problems for Fepois (takes a long time)
# ("log(Y) ~ log(X1):X2 | f3 + f1"), # currently, causes big problems for Fepois (takes a long time)
# ("Y ~ X2 + exp(X1) | f3 + f1"), # currently, causes big problems for Fepois (takes a long time)
("Y ~ X1 + i(f1,X2)"),
("Y ~ X1 + i(f1,X2) + i(f2, X2)"),
("Y ~ X1 + i(f1,X2, ref =1) + i(f2)"),
Expand All @@ -69,14 +69,14 @@
("Y ~ X1 + i(f2,X2, ref=2.0)"),
("Y ~ X1 + i(f1,X2, ref=3.0) | f2"),
("Y ~ X1 + i(f1,X2, ref=4.0) | f2 + f3"),
# ("Y ~ C(f1):X2"), # currently does not work as C():X translation not implemented # noqa: W505
# ("Y ~ C(f1):X2"), # currently does not work as C():X translation not implemented
# ("Y ~ C(f1):C(f2)"), # currently does not work
("Y ~ X1 + I(X2 ** 2)"),
("Y ~ X1 + I(X1 ** 2) + I(X2**4)"),
("Y ~ X1*X2"),
("Y ~ X1*X2 | f1+f2"),
# ("Y ~ X1/X2"), # currently does not work as X1/X2 translation not implemented # noqa: W505
# ("Y ~ X1/X2 | f1+f2"), # currently does not work as X1/X2 translation not implemented # noqa: W505
# ("Y ~ X1/X2"), # currently does not work as X1/X2 translation not implemented
# ("Y ~ X1/X2 | f1+f2"), # currently does not work as X1/X2 translation not implemented
("Y ~ X1 + poly(X2, 2) | f1"),
]

Expand Down Expand Up @@ -470,10 +470,10 @@ def test_single_fit_fepois(
check_absolute_diff(py_deviance, r_deviance, 1e-08, "py_deviance != r_deviance")

if not mod._has_fixef:
py_predict_response = mod.predict(type="response") # noqa: F841
py_predict_link = mod.predict(type="link") # noqa: F841
r_predict_response = stats.predict(r_fixest, type="response") # noqa: F841
r_predict_link = stats.predict(r_fixest, type="link") # noqa: F841
py_predict_response = mod.predict(type="response")
py_predict_link = mod.predict(type="link")
r_predict_response = stats.predict(r_fixest, type="response")
r_predict_link = stats.predict(r_fixest, type="link")
check_absolute_diff(
py_predict_response[0:5],
r_predict_response[0:5],
Expand Down

0 comments on commit 5e24787

Please sign in to comment.