Skip to content
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

Feat/target feat analysis #889

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions dataprep/eda/create_diff_report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

def create_diff_report(
df_list: Union[List[pd.DataFrame], Dict[str, pd.DataFrame]],
target: Optional[str] = None,
config: Optional[Dict[str, Any]] = None,
display: Optional[List[str]] = None,
title: Optional[str] = "DataPrep Report",
Expand All @@ -35,6 +36,8 @@ def create_diff_report(
----------
df_list
The DataFrames for which data are calculated.
target
Target feature to be compared against all other columns.
config
A dictionary for configuring the visualizations
E.g. config={"hist.bins": 20}
Expand Down Expand Up @@ -63,7 +66,7 @@ def create_diff_report(
_suppress_warnings()
cfg = Config.from_dict(display, config)

components = format_diff_report(df_list, cfg, mode, progress)
components = format_diff_report(df_list, cfg, mode, progress, target)

dict_stats = defaultdict(list)

Expand All @@ -82,16 +85,6 @@ def create_diff_report(
"legend_labels": components["legend_lables"],
}

# {% for div in value.plots[1] %}
# <div class="vp-plot">
# {{ div }}
# {% if key in context.components.dfs[1].variables %}
# {{ context.components.dfs[1].variables[key].plots[1][loop.index0] }}
# {% endif %}
# </div>

# return context

template_base = ENV_LOADER.get_template("base.html")
report = template_base.render(context=context, zip=zip)
return Report(report)
Expand Down
54 changes: 44 additions & 10 deletions dataprep/eda/create_diff_report/diff_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def format_diff_report(
cfg: Config,
mode: Optional[str],
progress: bool = True,
target: Optional[str] = None,
) -> Dict[str, Any]:
"""
Format the data and figures needed by create_diff_report
Expand All @@ -83,6 +84,8 @@ def format_diff_report(
Currently only the 'basic' is fully implemented.
progress
Whether to show the progress bar.
target
Target feature to be compared against all other columns.

Returns
-------
Expand Down Expand Up @@ -110,20 +113,44 @@ def format_diff_report(
if mode == "basic":
# note: we need the type ignore comment for mypy otherwise it complains because
# it doesn't realize that we converted df_list to a list if it's a dictionary
report = format_basic(df_list, cfg) # type: ignore
if target:
validate_target(target, df_list)
report = format_basic(df_list, target, cfg) # type: ignore
else:
raise ValueError(f"Unknown mode: {mode}")
return report


def format_basic(df_list: List[pd.DataFrame], cfg: Config) -> Dict[str, Any]:
def validate_target(target: str, df_list: List[pd.DataFrame]):
"""
Helper function, verify that target column exists

Parameters
----------
target
Target feature to be compared against all other columns.
df_list
The Dataframe for which data are calculated.
"""
exists = False
for df in df_list:
if target in df.columns:
exists = True
break
if not exists:
raise ValueError(f"Sorry, {target} is not a valid column")


def format_basic(df_list: List[pd.DataFrame], target: Optional[str], cfg: Config) -> Dict[str, Any]:
"""
Format basic version.

Parameters
----------
df_list
The DataFrames for which data are calculated.
target
Target feature to be compared against all other columns.
cfg
The config dict user passed in. E.g. config = {"hist.bins": 20}
Without user's specifications, the default is "auto"
Expand Down Expand Up @@ -158,7 +185,7 @@ def format_basic(df_list: List[pd.DataFrame], cfg: Config) -> Dict[str, Any]:
# data = dask.compute(data)
delayed_results.append(data)

res_plots = dask.delayed(_format_plots)(cfg=cfg, df_list=df_list)
res_plots = dask.delayed(_format_plots)(cfg=cfg, df_list=df_list, target=target)

dask_results["df_computations"] = delayed_results
dask_results["plots"] = res_plots
Expand Down Expand Up @@ -211,7 +238,7 @@ def basic_computations(df: EDAFrame, cfg: Config) -> Dict[str, Any]:


def compute_plot_data(
df_list: List[dd.DataFrame], cfg: Config, dtype: Optional[DTypeDef]
pd_list: List[pd.DataFrame], cfg: Config, dtype: Optional[DTypeDef], target: Optional[str]
) -> Intermediate:
"""
Compute function for create_diff_report's plots
Expand All @@ -226,9 +253,15 @@ def compute_plot_data(
E.g. dtype = {"a": Continuous, "b": "Nominal"} or
dtype = {"a": Continuous(), "b": "nominal"}
or dtype = Continuous() or dtype = "Continuous" or dtype = Continuous()
target
Target feature to be compared against all other columns.
"""
# pylint: disable=too-many-branches, too-many-locals

df_list = list(map(to_dask, pd_list))
for i, _ in enumerate(df_list):
df_list[i].columns = df_list[i].columns.astype(str)

dfs = Dfs(df_list)
dfs_cols = dfs.columns.apply("to_list").data

Expand All @@ -249,6 +282,8 @@ def compute_plot_data(
col_dtype = col_dtype[0]

orig = [src for src, seq in labeled_cols.items() if col in seq]
if col == target and not is_dtype(col_dtype, Continuous_v1()):
raise ValueError("Sorry, target must be a numerical feature.")

if is_dtype(col_dtype, Continuous_v1()):
data.append((col, Continuous_v1(), diff_cont_calcs(srs.apply("dropna"), cfg), orig))
Expand Down Expand Up @@ -277,7 +312,9 @@ def compute_plot_data(
elif is_dtype(dtp, DateTime_v1()):
plot_data.append((col, dtp, dask.compute(*datum), orig)) # workaround

return Intermediate(data=plot_data, stats=stats, visual_type="comparison_grid")
return Intermediate(
data=plot_data, stats=stats, visual_type="comparison_grid", target=target, df_list=pd_list
)


def _compute_variables(df: EDAFrame, cfg: Config) -> Dict[str, Any]:
Expand Down Expand Up @@ -407,14 +444,11 @@ def _format_variables(df: EDAFrame, cfg: Config, data: Dict[str, Any]) -> Dict[s


def _format_plots(
df_list: Union[List[pd.DataFrame], Dict[str, pd.DataFrame]], cfg: Config
df_list: Union[List[pd.DataFrame], Dict[str, pd.DataFrame]], cfg: Config, target: Optional[str]
) -> Dict[str, Any]:
"""Formatting of plots section"""
df_list = list(map(to_dask, df_list))
for i, _ in enumerate(df_list):
df_list[i].columns = df_list[i].columns.astype(str)

itmdt = compute_plot_data(df_list=df_list, cfg=cfg, dtype=None)
itmdt = compute_plot_data(pd_list=df_list, cfg=cfg, dtype=None, target=target)
return render_diff(itmdt, cfg=cfg)


Expand Down
Loading