Skip to content

Commit d893204

Browse files
Add guarded temporary ruff repair script
1 parent 77a6d7e commit d893204

1 file changed

Lines changed: 207 additions & 0 deletions

File tree

scripts/apply_ruff_repairs.py

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
5+
6+
def replace_once(path: str, old: str, new: str) -> None:
7+
file = Path(path)
8+
text = file.read_text()
9+
count = text.count(old)
10+
if count != 1:
11+
raise RuntimeError(
12+
f"{path}: expected exactly one match, found {count}\nOLD:\n{old}"
13+
)
14+
file.write_text(text.replace(old, new, 1))
15+
16+
17+
replace_once(
18+
"src/nns/_reg_engine.py",
19+
"import math\nimport re\n",
20+
"import math\nimport re\nfrom itertools import pairwise\n",
21+
)
22+
replace_once(
23+
"src/nns/_reg_engine.py",
24+
"ord_value = max(1, int(math.floor(dep * 10 + 0.5)))",
25+
"ord_value = max(1, math.floor(dep * 10 + 0.5))",
26+
)
27+
replace_once(
28+
"src/nns/_reg_engine.py",
29+
"ord_value = max(1, int(math.floor(ord_value / 2)))",
30+
"ord_value = max(1, math.floor(ord_value / 2))",
31+
)
32+
replace_once(
33+
"src/nns/_reg_engine.py",
34+
"for a, b in zip(unique_knots[:-1], unique_knots[1:]):",
35+
"for a, b in pairwise(unique_knots):",
36+
)
37+
replace_once(
38+
"src/nns/_reg_engine.py",
39+
'"Variable": names + ["DENOMINATOR"],',
40+
'"Variable": [*names, "DENOMINATOR"],',
41+
)
42+
replace_once(
43+
"src/nns/_reg_engine.py",
44+
"k = max(1, int(math.floor((1.0 - dep) * math.sqrt(x.shape[0]))))",
45+
"k = max(1, math.floor((1.0 - dep) * math.sqrt(x.shape[0])))",
46+
)
47+
48+
replace_once(
49+
"src/nns/_rrng.py",
50+
"v1 = int(math.floor(self.unif_rand() * 65536))",
51+
"v1 = math.floor(self.unif_rand() * 65536)",
52+
)
53+
replace_once(
54+
"src/nns/_rrng.py",
55+
"bits = int(math.ceil(math.log2(dn)))",
56+
"bits = math.ceil(math.log2(dn))",
57+
)
58+
replace_once(
59+
"src/nns/_rrng.py",
60+
" def sample_int(self, n: int, size: int | None = None, replace: bool = False) -> NDArray[np.int64]:\n",
61+
" def sample_int(\n"
62+
" self, n: int, size: int | None = None, replace: bool = False\n"
63+
" ) -> NDArray[np.int64]:\n",
64+
)
65+
66+
replace_once(
67+
"src/nns/boost.py",
68+
"size = max(1, min(n_obs - 1, int(round(cv_fraction * n_obs))))",
69+
"size = max(1, min(n_obs - 1, round(cv_fraction * n_obs)))",
70+
)
71+
replace_once(
72+
"src/nns/boost.py",
73+
"k_small = max(1, int(math.floor(math.sqrt(minimum_train_size))))",
74+
"k_small = max(1, math.floor(math.sqrt(minimum_train_size)))",
75+
)
76+
replace_once(
77+
"src/nns/boost.py",
78+
"k_candidates = list(dict.fromkeys(list(range(1, k_small + 1)) + [minimum_train_size]))",
79+
"k_candidates = list(dict.fromkeys([*range(1, k_small + 1), minimum_train_size]))",
80+
)
81+
82+
replace_once(
83+
"src/nns/stack.py",
84+
"def _scalar_integer(value: Any, name: str, minimum: int = 0, allow_null: bool = False) -> int | None:\n",
85+
"def _scalar_integer(\n"
86+
" value: Any, name: str, minimum: int = 0, allow_null: bool = False\n"
87+
") -> int | None:\n",
88+
)
89+
replace_once(
90+
"src/nns/stack.py",
91+
" [class_values.index(v) + 1 for v in (dv.tolist() if response_categorical else dv.astype(np.float64).tolist())],\n",
92+
" [\n"
93+
" class_values.index(v) + 1\n"
94+
" for v in (\n"
95+
" dv.tolist()\n"
96+
" if response_categorical\n"
97+
" else dv.astype(np.float64).tolist()\n"
98+
" )\n"
99+
" ],\n",
100+
)
101+
replace_once(
102+
"src/nns/stack.py",
103+
' "Method 2 was removed because dimension reduction requires more than one original predictor.",\n',
104+
' "Method 2 was removed because dimension reduction requires more than "\n'
105+
' "one original predictor.",\n',
106+
)
107+
replace_once(
108+
"src/nns/stack.py",
109+
" return np.clip(np.asarray(code, dtype=np.float64), 1, n_classes).astype(np.int64).astype(np.float64)\n",
110+
" return (\n"
111+
" np.clip(np.asarray(code, dtype=np.float64), 1, n_classes)\n"
112+
" .astype(np.int64)\n"
113+
" .astype(np.float64)\n"
114+
" )\n",
115+
)
116+
replace_once(
117+
"src/nns/stack.py",
118+
"size = min(g.size - 1, max(1, int(round(holdout_size * g.size))))",
119+
"size = min(g.size - 1, max(1, round(holdout_size * g.size)))",
120+
)
121+
replace_once(
122+
"src/nns/stack.py",
123+
"size = max(1, min(n_obs - 1, int(round(holdout_size * n_obs))))",
124+
"size = max(1, min(n_obs - 1, round(holdout_size * n_obs)))",
125+
)
126+
replace_once(
127+
"src/nns/stack.py",
128+
" def coefficient_vector(design: NDArray[np.float64], response: NDArray[np.float64]) -> NDArray[np.float64]:\n",
129+
" def coefficient_vector(\n"
130+
" design: NDArray[np.float64], response: NDArray[np.float64]\n"
131+
" ) -> NDArray[np.float64]:\n",
132+
)
133+
replace_once(
134+
"src/nns/stack.py",
135+
"l_small = max(1, int(math.floor(math.sqrt(n_obs))))",
136+
"l_small = max(1, math.floor(math.sqrt(n_obs)))",
137+
)
138+
139+
replace_once(
140+
"src/nns/var.py",
141+
" _METHOD1_GUARD_MESSAGE = (\n",
142+
" method1_guard_message = (\n",
143+
)
144+
replace_once(
145+
"src/nns/var.py",
146+
''' def run_var_stack(stack_obj_fn: Any, stack_objective: str) -> dict[str, Any]:
147+
return nns_stack(
148+
lagged_iv,
149+
lagged_dv,
150+
ivs_test=ivs_test,
151+
obj_fn=cast(Any, stack_obj_fn),
152+
objective=cast(Any, stack_objective),
153+
folds=1,
154+
method=(1, 2),
155+
order=None,
156+
stack=True,
157+
dim_red_method=cast(Any, dim_red_threshold_method),
158+
ts_test=ts_test,
159+
)
160+
161+
try:
162+
result = run_var_stack(var_obj_fn, objective_value)
163+
except ValueError as error:
164+
if not (use_default_objective and str(error) == _METHOD1_GUARD_MESSAGE):
165+
raise
166+
result = run_var_stack(mse_obj_fn, "min")
167+
''',
168+
''' def run_var_stack(
169+
stack_obj_fn: Any,
170+
stack_objective: str,
171+
stack_iv: np.ndarray,
172+
stack_dv: np.ndarray,
173+
stack_test: np.ndarray,
174+
stack_ts_test: int,
175+
) -> dict[str, Any]:
176+
return nns_stack(
177+
stack_iv,
178+
stack_dv,
179+
ivs_test=stack_test,
180+
obj_fn=cast(Any, stack_obj_fn),
181+
objective=cast(Any, stack_objective),
182+
folds=1,
183+
method=(1, 2),
184+
order=None,
185+
stack=True,
186+
dim_red_method=cast(Any, dim_red_threshold_method),
187+
ts_test=stack_ts_test,
188+
)
189+
190+
try:
191+
result = run_var_stack(
192+
var_obj_fn, objective_value, lagged_iv, lagged_dv, ivs_test, ts_test
193+
)
194+
except ValueError as error:
195+
if not (use_default_objective and str(error) == method1_guard_message):
196+
raise
197+
result = run_var_stack(
198+
mse_obj_fn, "min", lagged_iv, lagged_dv, ivs_test, ts_test
199+
)
200+
''',
201+
)
202+
203+
replace_once(
204+
"tests/parity/test_regression.py",
205+
'with pytest.raises(ValueError, match="noise.reduction"):',
206+
'with pytest.raises(ValueError, match=r"noise\\.reduction"):',
207+
)

0 commit comments

Comments
 (0)