-
Notifications
You must be signed in to change notification settings - Fork 0
Fix errors in make risk report function #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
151d643
bd72da6
175ad19
e16579c
f4fd5b4
9dc245b
7c83669
1621eb2
8267a97
14b5b67
0b13022
a2fdde6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||
| import textwrap | ||||||||||||||||||||||||||||||||||||||||||
| import warnings | ||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||
| from typing import Dict, Optional, Sequence, Tuple, Union | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -13,6 +15,27 @@ | |||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _latex_escape(line: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||
| """Escape LaTeX special characters in a string.""" | ||||||||||||||||||||||||||||||||||||||||||
| # LaTeX special characters that need escaping: | ||||||||||||||||||||||||||||||||||||||||||
| # \ & % $ # _ { } ~ ^ | ||||||||||||||||||||||||||||||||||||||||||
| # We replace each occurrence with its escaped form. | ||||||||||||||||||||||||||||||||||||||||||
| specials = { | ||||||||||||||||||||||||||||||||||||||||||
| "\\": r"\textbackslash{}", | ||||||||||||||||||||||||||||||||||||||||||
| "&": r"\&", | ||||||||||||||||||||||||||||||||||||||||||
| "%": r"\%", | ||||||||||||||||||||||||||||||||||||||||||
| "$": r"\$", | ||||||||||||||||||||||||||||||||||||||||||
| "#": r"\#", | ||||||||||||||||||||||||||||||||||||||||||
| "_": r"\_", | ||||||||||||||||||||||||||||||||||||||||||
| "{": r"\{", | ||||||||||||||||||||||||||||||||||||||||||
| "}": r"\}", | ||||||||||||||||||||||||||||||||||||||||||
| "~": r"\textasciitilde{}", | ||||||||||||||||||||||||||||||||||||||||||
| "^": r"\textasciicircum{}", | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| pattern = re.compile(r"([\\&%$#_{}~^])") | ||||||||||||||||||||||||||||||||||||||||||
| return pattern.sub(lambda m: specials[m.group(1)], line) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def make_risk_report( | ||||||||||||||||||||||||||||||||||||||||||
| data_source: Union[pd.DataFrame, Tuple[pd.DataFrame, pd.DataFrame]], | ||||||||||||||||||||||||||||||||||||||||||
| target: str, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -220,7 +243,7 @@ def make_risk_report( | |||||||||||||||||||||||||||||||||||||||||
| n_positive[ref_item], | ||||||||||||||||||||||||||||||||||||||||||
| n_affected[ref_item], | ||||||||||||||||||||||||||||||||||||||||||
| conf_level, | ||||||||||||||||||||||||||||||||||||||||||
| method, | ||||||||||||||||||||||||||||||||||||||||||
| diff_method, | ||||||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||||
| ).astuple(), | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -283,17 +306,37 @@ def make_risk_report( | |||||||||||||||||||||||||||||||||||||||||
| if return_type.lower() == "pd": | ||||||||||||||||||||||||||||||||||||||||||
| return df_risk_table | ||||||||||||||||||||||||||||||||||||||||||
| elif return_type.lower() == "latex": | ||||||||||||||||||||||||||||||||||||||||||
| rows = [line.replace("%", r"\%") for line in df_risk_table.to_latex(header=False, index=False).splitlines()] | ||||||||||||||||||||||||||||||||||||||||||
| rows[0] = r"\begin{tabular}{@{\extracolsep{6pt}}lllllll@{}}" | ||||||||||||||||||||||||||||||||||||||||||
| rows[2] = ( | ||||||||||||||||||||||||||||||||||||||||||
| r"\multicolumn{2}{l}{Feature} & \multicolumn{affected_cols}{l}{Affected} & \multicolumn{2}{l}{risk_name Risk ($95\%$ CI)} & risk_name Risk Difference ($95\%$ CI) \\ \cline{1-2}\cline{3-4}\cline{5-6}\cline{7-7}" | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| rows[2].replace("risk_name", risk_name).replace("95", str(int(conf_level * 100))) | ||||||||||||||||||||||||||||||||||||||||||
| latex_body = df_risk_table.to_latex(header=False, index=False) | ||||||||||||||||||||||||||||||||||||||||||
| body_lines = latex_body.splitlines() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if is_split: | ||||||||||||||||||||||||||||||||||||||||||
| rows[2].replace("affected_cols", "3") | ||||||||||||||||||||||||||||||||||||||||||
| header = rf""" | ||||||||||||||||||||||||||||||||||||||||||
| \begin{{tabular}}{{@{{\extracolsep{{6pt}}}}llllllll@{{}}}} | ||||||||||||||||||||||||||||||||||||||||||
| \toprule | ||||||||||||||||||||||||||||||||||||||||||
| \multicolumn{{2}}{{l}}{{Feature}} & | ||||||||||||||||||||||||||||||||||||||||||
| \multicolumn{{3}}{{l}}{{Affected}} & | ||||||||||||||||||||||||||||||||||||||||||
| \multicolumn{{2}}{{l}}{{{risk_name} Risk (${int(conf_level*100)}\%$ CI)}} & | ||||||||||||||||||||||||||||||||||||||||||
| {risk_name} Risk Difference (${int(conf_level*100)}\%$ CI) \\ | ||||||||||||||||||||||||||||||||||||||||||
| \cmidrule(lr){{1-2}}\cmidrule(lr){{3-5}}\cmidrule(lr){{6-7}}\cmidrule(lr){{8-8}} | ||||||||||||||||||||||||||||||||||||||||||
| & & n & \% & t/v & n & \% & \\ \midrule | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+314
to
+321
|
||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||
| rows[2].replace("affected_cols", "2") | ||||||||||||||||||||||||||||||||||||||||||
| ret_lines = "\n".join(rows) | ||||||||||||||||||||||||||||||||||||||||||
| header = rf""" | ||||||||||||||||||||||||||||||||||||||||||
| \begin{{tabular}}{{@{{\extracolsep{{6pt}}}}lllllll@{{}}}} | ||||||||||||||||||||||||||||||||||||||||||
| \toprule | ||||||||||||||||||||||||||||||||||||||||||
| \multicolumn{{2}}{{l}}{{Feature}} & | ||||||||||||||||||||||||||||||||||||||||||
| \multicolumn{{2}}{{l}}{{Affected}} & | ||||||||||||||||||||||||||||||||||||||||||
| \multicolumn{{2}}{{l}}{{{risk_name} Risk (${int(conf_level*100)}\%$ CI)}} & | ||||||||||||||||||||||||||||||||||||||||||
| {risk_name} Risk Difference (${int(conf_level*100)}\%$ CI) \\ | ||||||||||||||||||||||||||||||||||||||||||
| \cmidrule(lr){{1-2}}\cmidrule(lr){{3-4}}\cmidrule(lr){{5-6}}\cmidrule(lr){{7-7}} | ||||||||||||||||||||||||||||||||||||||||||
| & & n & \% & n & \% & \\ \midrule | ||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||
| # remove extra leading spaces | ||||||||||||||||||||||||||||||||||||||||||
| header = textwrap.dedent(header).strip() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| body = "\n".join(_latex_escape(line) for line in body_lines[5:-1]) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+337
to
+338
|
||||||||||||||||||||||||||||||||||||||||||
| body = "\n".join(_latex_escape(line) for line in body_lines[5:-1]) | |
| # Keep only data rows from the pandas-generated LaTeX, dropping wrapper/rule lines | |
| filtered_body_lines = [] | |
| for line in body_lines: | |
| stripped = line.strip() | |
| if not stripped: | |
| continue | |
| if stripped.startswith(r"\begin{tabular}"): | |
| continue | |
| if stripped.startswith(r"\end{tabular}"): | |
| continue | |
| if stripped.startswith(r"\toprule"): | |
| continue | |
| if stripped.startswith(r"\midrule"): | |
| continue | |
| if stripped.startswith(r"\bottomrule"): | |
| continue | |
| filtered_body_lines.append(_latex_escape(line)) | |
| body = "\n".join(filtered_body_lines) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The flake8 config change globally ignores E251/E202, which reduces lint coverage repo-wide and can hide real formatting issues. If this was only needed for a small number of lines, prefer fixing the formatting or using targeted
# noqa: E251/# noqa: E202on the specific lines triggering it instead of disabling the checks globally.