Skip to content

Commit 9385228

Browse files
committed
Testing improved
1 parent 8eec519 commit 9385228

File tree

5 files changed

+85
-34
lines changed

5 files changed

+85
-34
lines changed

.github/workflows/github-pytest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ jobs:
3939
run: python setup.py install
4040

4141
- name: Test with pytest
42-
run: pytest
42+
run: pytest navicat_spock/*py

navicat_spock/helpers.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@ def call_imputer(a, b, imputer_strat="iterative"):
4848
return a
4949

5050

51-
def slope_check(beta_hats, verb=0):
51+
def slope_check(alphas, verb=0):
5252
if verb > 4:
53-
print(f"Slopes are: {beta_hats}")
54-
if beta_hats is None:
53+
print(f"Slopes are: {alphas}")
54+
if alphas is None:
5555
return False
56-
if len(beta_hats) == 1:
56+
if len(alphas) == 1:
5757
return True
58-
if len(beta_hats) == 2:
59-
if beta_hats[0] > 0:
60-
return beta_hats[1] < 0
61-
if beta_hats[0] < 0:
62-
return beta_hats[1] > 0
63-
if len(beta_hats) == 3:
64-
if beta_hats[0] > 0:
65-
if beta_hats[1] > 0:
66-
return beta_hats[2] < 0
67-
if beta_hats[1] < 0:
68-
return beta_hats[2] < 0
69-
if beta_hats[0] < 0:
70-
if beta_hats[1] > 0:
71-
return beta_hats[2] > 0
72-
if beta_hats[1] < 0:
73-
return beta_hats[2] > 0
58+
if len(alphas) == 2:
59+
if alphas[0] > 0:
60+
return alphas[1] < 0
61+
if alphas[0] < 0:
62+
return alphas[1] > 0
63+
if len(alphas) == 3:
64+
if alphas[0] > 0:
65+
if alphas[1] > 0:
66+
return alphas[2] < 0
67+
if alphas[1] < 0:
68+
return alphas[2] < 0
69+
if alphas[0] < 0:
70+
if alphas[1] > 0:
71+
return alphas[2] > 0
72+
if alphas[1] < 0:
73+
return alphas[2] > 0
7474
return False
7575

7676

@@ -489,7 +489,7 @@ def check_input(filenames, wp, imputer_strat, verb):
489489
return dfs
490490

491491

492-
def test_vif_1():
492+
def test_vif():
493493
sol = np.array([22.95, 3.0, 12.95, 3.0])
494494
a = [1, 1, 2, 3, 4]
495495
b = [2, 2, 3, 2, 1]
@@ -503,5 +503,13 @@ def test_vif_1():
503503
assert np.allclose(vif, sol)
504504

505505

506+
def test_slope_check():
507+
assert not slope_check([10, 10, 10])
508+
assert not slope_check([10, -10, 10])
509+
assert not slope_check([-10, 10, -10])
510+
assert slope_check([10, 10, -10])
511+
assert slope_check([10, -10, -10])
512+
513+
506514
if __name__ == "__main__":
507-
test_vif_1()
515+
test_vif()

navicat_spock/piecewise_regression.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -647,18 +647,30 @@ def get_results(self):
647647
results["rss"] = self.best_muggeo.best_fit.residual_sum_squares
648648
results["converged"] = True
649649
breakpoints = self.best_muggeo.best_fit.next_breakpoints
650-
slopes = [self.best_muggeo.best_fit.raw_params[1]]
651-
for b in self.best_muggeo.best_fit.raw_params[2 : 2 + len(breakpoints)]:
652-
slopes.append(b + self.best_muggeo.best_fit.raw_params[1])
653-
results["betas"] = np.array(slopes)
650+
# slopes = [self.best_muggeo.best_fit.raw_params[1]]
651+
slopes = []
652+
653+
# Lets get the slopes right
654+
alpha_names = [
655+
"alpha{}".format(alpha_i)
656+
for alpha_i in range(1, self.n_breakpoints + 1)
657+
]
658+
for est_name in alpha_names:
659+
slopes.append(self.estimates[est_name]["estimate"])
660+
661+
# for i, b in enumerate(
662+
# self.best_muggeo.best_fit.raw_params[2 : 2 + len(breakpoints)]
663+
# ):
664+
# slopes.append(b + slopes[-1])
665+
results["slopes"] = np.array(slopes)
654666
else:
655667
results["converged"] = False
656668
results["estimates"] = None
657669
results["bic"] = None
658670
results["bic_w"] = None
659671
results["aic_w"] = None
660672
results["rss"] = None
661-
results["betas"] = None
673+
results["slopes"] = None
662674
return results
663675

664676
def bootstrap_restarting(self):
@@ -1052,8 +1064,8 @@ def summary(self):
10521064
table_contents += single_line
10531065

10541066
table_contents += (
1055-
"These alphas(gradients of segments) are estimated"
1056-
"from betas(change in gradient)\n"
1067+
"These alphas (gradients of segments) are estimated "
1068+
"from betas (change in gradient)\n"
10571069
)
10581070

10591071
alpha_names = [

navicat_spock/plotting2d.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,18 @@ def plot_2d(
136136

137137

138138
def plot_and_save(
139-
pw_fit, tags, idx, tidx, cb, ms, plotmode, fig, ax, return_value=True, save_fig=True, save_csv=True
139+
pw_fit,
140+
tags,
141+
idx,
142+
tidx,
143+
cb,
144+
ms,
145+
plotmode,
146+
fig,
147+
ax,
148+
return_value=True,
149+
save_fig=True,
150+
save_csv=True,
140151
):
141152
# Try to figure out good dimensions for the axes and ticks
142153
x = pw_fit.xx

navicat_spock/spock.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@
2424

2525

2626
def run_spock():
27-
(df, wp, verb, imputer_strat, plotmode, seed, prefit, save_fig, save_csv) = processargs(sys.argv[1:])
27+
(
28+
df,
29+
wp,
30+
verb,
31+
imputer_strat,
32+
plotmode,
33+
seed,
34+
prefit,
35+
save_fig,
36+
save_csv,
37+
) = processargs(sys.argv[1:])
2838
_ = run_spock_from_args(
2939
df=df,
3040
wp=wp,
@@ -131,7 +141,7 @@ def run_spock_from_args(
131141
)
132142
all_sc[i, :] = np.array(
133143
[
134-
slope_check(summary["betas"], verb)
144+
slope_check(summary["slopes"], verb)
135145
for summary in msel.model_summaries
136146
],
137147
dtype=bool,
@@ -298,7 +308,17 @@ def run_spock_from_args(
298308
pw_fit.summary()
299309
# Plot the data, fit, breakpoints and confidence intervals
300310
fig, ax = plot_and_save(
301-
pw_fit, tags, idx, tidx, cb, ms, fig=fig, ax=ax, plotmode=plotmode, save_fig=save_fig, save_csv=save_csv
311+
pw_fit,
312+
tags,
313+
idx,
314+
tidx,
315+
cb,
316+
ms,
317+
fig=fig,
318+
ax=ax,
319+
plotmode=plotmode,
320+
save_fig=save_fig,
321+
save_csv=save_csv,
302322
)
303323
return fig, ax
304324
else:

0 commit comments

Comments
 (0)