Skip to content

Commit 22f5008

Browse files
committed
better print output alignment when printing model coefficient labels and values
1 parent 6ca0842 commit 22f5008

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

causalpy/skl_experiments.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ def print_coefficients(self, round_to=None) -> None:
6262
Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
6363
"""
6464
print("Model coefficients:")
65+
# Determine the width of the longest label
66+
max_label_length = max(len(name) for name in self.labels)
67+
# Print each coefficient with formatted alignment
6568
for name, val in zip(self.labels, self.model.coef_[0]):
66-
print(f"\t{name}\t\t{round_num(val, round_to)}")
69+
# Left-align the name
70+
formatted_name = f"{name:<{max_label_length}}"
71+
# Right-align the value with width 10
72+
formatted_val = f"{round_num(val, round_to):>10}"
73+
print(f" {formatted_name}\t{formatted_val}")
6774

6875

6976
class PrePostFit(ExperimentalDesign, PrePostFitDataValidator):
@@ -729,7 +736,7 @@ def plot(self, round_to=None):
729736
ax.legend(fontsize=LEGEND_FONT_SIZE)
730737
return (fig, ax)
731738

732-
def summary(self, round_to=None):
739+
def summary(self, round_to=None) -> None:
733740
"""
734741
Print text output summarising the results
735742
@@ -742,4 +749,5 @@ def summary(self, round_to=None):
742749
print(f"Threshold on running variable: {self.treatment_threshold}")
743750
print("\nResults:")
744751
print(f"Discontinuity at threshold = {self.discontinuity_at_threshold:.2f}")
752+
print("\n")
745753
self.print_coefficients(round_to)

docs/source/notebooks/did_skl.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979
"Results:\n",
8080
"Causal impact = 0.504\n",
8181
"Model coefficients:\n",
82-
"\tIntercept\t\t0\n",
83-
"\tpost_treatment[T.True]\t\t0.986\n",
84-
"\tgroup\t\t0.162\n",
85-
"\tgroup:post_treatment[T.True]\t\t0.504\n"
82+
" Intercept \t 0\n",
83+
" post_treatment[T.True] \t 0.986\n",
84+
" group \t 0.162\n",
85+
" group:post_treatment[T.True]\t 0.504\n"
8686
]
8787
}
8888
],

docs/source/notebooks/its_skl.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,19 @@
211211
"==================================Pre-Post Fit==================================\n",
212212
"Formula: y ~ 1 + t + C(month)\n",
213213
"Model coefficients:\n",
214-
"\tIntercept\t\t0\n",
215-
"\tC(month)[T.2]\t\t2.85\n",
216-
"\tC(month)[T.3]\t\t1.16\n",
217-
"\tC(month)[T.4]\t\t7.15\n",
218-
"\tC(month)[T.5]\t\t15\n",
219-
"\tC(month)[T.6]\t\t24.8\n",
220-
"\tC(month)[T.7]\t\t18.2\n",
221-
"\tC(month)[T.8]\t\t33.5\n",
222-
"\tC(month)[T.9]\t\t16.2\n",
223-
"\tC(month)[T.10]\t\t9.19\n",
224-
"\tC(month)[T.11]\t\t6.28\n",
225-
"\tC(month)[T.12]\t\t0.578\n",
226-
"\tt\t\t0.21\n"
214+
" Intercept \t 0\n",
215+
" C(month)[T.2] \t 2.85\n",
216+
" C(month)[T.3] \t 1.16\n",
217+
" C(month)[T.4] \t 7.15\n",
218+
" C(month)[T.5] \t 15\n",
219+
" C(month)[T.6] \t 24.8\n",
220+
" C(month)[T.7] \t 18.2\n",
221+
" C(month)[T.8] \t 33.5\n",
222+
" C(month)[T.9] \t 16.2\n",
223+
" C(month)[T.10]\t 9.19\n",
224+
" C(month)[T.11]\t 6.28\n",
225+
" C(month)[T.12]\t 0.578\n",
226+
" t \t 0.21\n"
227227
]
228228
}
229229
],

docs/source/notebooks/rd_skl.ipynb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@
184184
"Results:\n",
185185
"Discontinuity at threshold = 0.19\n",
186186
"Model coefficients:\n",
187-
"\tIntercept\t\t0\n",
188-
"\ttreated[T.True]\t\t0.19\n",
189-
"\tx\t\t1.23\n"
187+
" Intercept \t 0\n",
188+
" treated[T.True]\t 0.19\n",
189+
" x \t 1.23\n"
190190
]
191191
}
192192
],
@@ -264,10 +264,10 @@
264264
"Results:\n",
265265
"Discontinuity at threshold = 0.92\n",
266266
"Model coefficients:\n",
267-
"\tIntercept\t\t0\n",
268-
"\ttreated[T.True]\t\t2.47\n",
269-
"\tx\t\t1.32\n",
270-
"\tx:treated[T.True]\t\t-3.11\n"
267+
" Intercept \t 0\n",
268+
" treated[T.True] \t 2.47\n",
269+
" x \t 1.32\n",
270+
" x:treated[T.True]\t -3.11\n"
271271
]
272272
}
273273
],
@@ -467,14 +467,14 @@
467467
"Results:\n",
468468
"Discontinuity at threshold = 0.41\n",
469469
"Model coefficients:\n",
470-
"\tIntercept\t\t0\n",
471-
"\ttreated[T.True]\t\t0.407\n",
472-
"\tbs(x, df=6)[0]\t\t-0.594\n",
473-
"\tbs(x, df=6)[1]\t\t-1.07\n",
474-
"\tbs(x, df=6)[2]\t\t0.278\n",
475-
"\tbs(x, df=6)[3]\t\t1.65\n",
476-
"\tbs(x, df=6)[4]\t\t1.03\n",
477-
"\tbs(x, df=6)[5]\t\t0.567\n"
470+
" Intercept \t 0\n",
471+
" treated[T.True]\t 0.407\n",
472+
" bs(x, df=6)[0] \t -0.594\n",
473+
" bs(x, df=6)[1] \t -1.07\n",
474+
" bs(x, df=6)[2] \t 0.278\n",
475+
" bs(x, df=6)[3] \t 1.65\n",
476+
" bs(x, df=6)[4] \t 1.03\n",
477+
" bs(x, df=6)[5] \t 0.567\n"
478478
]
479479
}
480480
],

docs/source/notebooks/rd_skl_drinking.ipynb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@
108108
"\n",
109109
"Results:\n",
110110
"Discontinuity at threshold = 7.66\n",
111+
"\n",
112+
"\n",
111113
"Model coefficients:\n",
112-
"\tIntercept\t\t0\n",
113-
"\ttreated[T.True]\t\t7.7\n",
114-
"\tage\t\t-0.97\n"
114+
" Intercept \t 0\n",
115+
" treated[T.True]\t 7.7\n",
116+
" age \t -0.97\n"
115117
]
116118
}
117119
],

0 commit comments

Comments
 (0)