-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
profile_report.py
571 lines (473 loc) · 19.9 KB
/
profile_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import copy
import json
import warnings
from pathlib import Path
from typing import Any, Optional, Union
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import pkg_resources
try:
from pyspark.sql import DataFrame as sDataFrame
except: # noqa: E722
from typing import TypeVar
sDataFrame = TypeVar("sDataFrame") # type: ignore
from dataclasses import asdict, is_dataclass
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
from typeguard import typechecked
from visions import VisionsTypeset
from ydata_profiling.config import Config, Settings, SparkSettings
from ydata_profiling.expectations_report import ExpectationsReport
from ydata_profiling.model import BaseDescription
from ydata_profiling.model.alerts import AlertType
from ydata_profiling.model.describe import describe as describe_df
from ydata_profiling.model.sample import Sample
from ydata_profiling.model.summarizer import (
BaseSummarizer,
PandasProfilingSummarizer,
format_summary,
redact_summary,
)
from ydata_profiling.model.typeset import ProfilingTypeSet
from ydata_profiling.report import get_report_structure
from ydata_profiling.report.presentation.core import Root
from ydata_profiling.report.presentation.flavours.html.templates import (
create_html_assets,
)
from ydata_profiling.serialize_report import SerializeReport
from ydata_profiling.utils.dataframe import hash_dataframe
from ydata_profiling.utils.logger import ProfilingLogger
from ydata_profiling.utils.paths import get_config
logger = ProfilingLogger(name="ReportLogger")
@typechecked
class ProfileReport(SerializeReport, ExpectationsReport):
"""Generate a profile report from a Dataset stored as a pandas `DataFrame`.
Used as is, it will output its content as an HTML report in a Jupyter notebook.
"""
_description_set = None
_report = None
_html = None
_widgets = None
_json = None
config: Settings
def __init__(
self,
df: Optional[Union[pd.DataFrame, sDataFrame]] = None,
minimal: bool = False,
tsmode: bool = False,
sortby: Optional[str] = None,
sensitive: bool = False,
explorative: bool = False,
dark_mode: bool = False,
orange_mode: bool = False,
sample: Optional[dict] = None,
config_file: Optional[Union[Path, str]] = None,
lazy: bool = True,
typeset: Optional[VisionsTypeset] = None,
summarizer: Optional[BaseSummarizer] = None,
config: Optional[Settings] = None,
type_schema: Optional[dict] = None,
**kwargs,
):
"""Generate a ProfileReport based on a pandas or spark.sql DataFrame
Config processing order (in case of duplicate entries, entries later in the order are retained):
- config presets (e.g. `config_file`, `minimal` arguments)
- config groups (e.g. `explorative` and `sensitive` arguments)
- custom settings (e.g. `config` argument)
- custom settings **kwargs (e.g. `title`)
Args:
df: a pandas or spark.sql DataFrame
minimal: minimal mode is a default configuration with minimal computation
ts_mode: activates time-series analysis for all the numerical variables from the dataset.
Only available for pd.DataFrame
sort_by: ignored if ts_mode=False. Order the dataset by a provided column.
sensitive: hides the values for categorical and text variables for report privacy
config_file: a config file (.yml), mutually exclusive with `minimal`
lazy: compute when needed
sample: optional dict(name="Sample title", caption="Caption", data=pd.DataFrame())
typeset: optional user typeset to use for type inference
summarizer: optional user summarizer to generate custom summary output
type_schema: optional dict containing pairs of `column name`: `type`
**kwargs: other arguments, for valid arguments, check the default configuration file.
"""
self.__validate_inputs(df, minimal, tsmode, config_file, lazy)
if config_file or minimal:
if not config_file:
config_file = get_config("config_minimal.yaml")
report_config = Settings().from_file(config_file)
elif config is not None:
report_config = config
else:
if isinstance(df, pd.DataFrame):
report_config = Settings()
else:
report_config = SparkSettings()
groups = [
(explorative, "explorative"),
(sensitive, "sensitive"),
(dark_mode, "dark_mode"),
(orange_mode, "orange_mode"),
]
if any(condition for condition, _ in groups):
cfg = Settings()
for condition, key in groups:
if condition:
cfg = cfg.update(Config.get_arg_groups(key))
report_config = cfg.update(report_config.dict(exclude_defaults=True))
if len(kwargs) > 0:
shorthands, kwargs = Config.shorthands(kwargs)
report_config = (
Settings()
.update(shorthands)
.update(report_config.dict(exclude_defaults=True))
)
if kwargs:
report_config = report_config.update(kwargs)
report_config.vars.timeseries.active = tsmode
if tsmode and sortby:
report_config.vars.timeseries.sortby = sortby
self.df = self.__initialize_dataframe(df, report_config)
self.config = report_config
self._df_hash = None
self._sample = sample
self._type_schema = type_schema
self._typeset = typeset
self._summarizer = summarizer
if not lazy:
# Trigger building the report structure
_ = self.report
@staticmethod
def __validate_inputs(
df: Optional[Union[pd.DataFrame, sDataFrame]],
minimal: bool,
tsmode: bool,
config_file: Optional[Union[Path, str]],
lazy: bool,
) -> None:
# Lazy profile cannot be set if no DataFrame is provided
if df is None and not lazy:
raise ValueError("Can init a not-lazy ProfileReport with no DataFrame")
if config_file is not None and minimal:
raise ValueError(
"Arguments `config_file` and `minimal` are mutually exclusive."
)
# Spark Dataframe validations
if isinstance(df, pd.DataFrame):
if df is not None and df.empty:
raise ValueError(
"DataFrame is empty. Please" "provide a non-empty DataFrame."
)
else:
if tsmode:
raise NotImplementedError(
"Time-Series dataset analysis is not yet supported for Spark DataFrames"
)
if (
df is not None and df.rdd.isEmpty()
): # df.isEmpty is only support by 3.3.0 pyspark version
raise ValueError(
"DataFrame is empty. Please" "provide a non-empty DataFrame."
)
@staticmethod
def __initialize_dataframe(
df: Optional[Union[pd.DataFrame, sDataFrame]], report_config: Settings
) -> Optional[Union[pd.DataFrame, sDataFrame]]:
logger.info_def_report(
dataframe=type(df), timeseries=report_config.vars.timeseries.active
)
if (
df is not None
and isinstance(df, pd.DataFrame)
and report_config.vars.timeseries.active
):
if report_config.vars.timeseries.sortby:
df = df.sort_values(by=report_config.vars.timeseries.sortby)
df = df.set_index(report_config.vars.timeseries.sortby, drop=False)
df.index.name = None
else:
df = df.sort_index()
return df
def invalidate_cache(self, subset: Optional[str] = None) -> None:
"""Invalidate report cache. Useful after changing setting.
Args:
subset:
- "rendering" to invalidate the html, json and widget report rendering
- "report" to remove the caching of the report structure
- None (default) to invalidate all caches
Returns:
None
"""
if subset is not None and subset not in ["rendering", "report"]:
raise ValueError(
"'subset' parameter should be None, 'rendering' or 'report'"
)
if subset is None or subset in ["rendering", "report"]:
self._widgets = None
self._json = None
self._html = None
if subset is None or subset == "report":
self._report = None
if subset is None:
self._description_set = None
@property
def typeset(self) -> Optional[VisionsTypeset]:
if self._typeset is None:
self._typeset = ProfilingTypeSet(self.config, self._type_schema)
return self._typeset
@property
def summarizer(self) -> BaseSummarizer:
if self._summarizer is None:
self._summarizer = PandasProfilingSummarizer(self.typeset)
return self._summarizer
@property
def description_set(self) -> BaseDescription:
if self._description_set is None:
self._description_set = describe_df(
self.config,
self.df,
self.summarizer,
self.typeset,
self._sample,
)
return self._description_set
@property
def df_hash(self) -> Optional[str]:
if self._df_hash is None and self.df is not None:
self._df_hash = hash_dataframe(self.df)
return self._df_hash
@property
def report(self) -> Root:
if self._report is None:
self._report = get_report_structure(self.config, self.description_set)
return self._report
@property
def html(self) -> str:
if self._html is None:
self._html = self._render_html()
return self._html
@property
def json(self) -> str:
if self._json is None:
self._json = self._render_json()
return self._json
@property
def widgets(self) -> Any:
if (
isinstance(self.description_set.table["n"], list)
and len(self.description_set.table["n"]) > 1
):
raise RuntimeError(
"Widgets interface not (yet) supported for comparing reports, please use the HTML rendering."
)
if self._widgets is None:
self._widgets = self._render_widgets()
return self._widgets
def get_duplicates(self) -> Optional[pd.DataFrame]:
"""Get duplicate rows and counts based on the configuration
Returns:
A DataFrame with the duplicate rows and their counts.
"""
return self.description_set.duplicates
def get_sample(self) -> dict:
"""Get head/tail samples based on the configuration
Returns:
A dict with the head and tail samples.
"""
return self.description_set.sample
def get_description(self) -> BaseDescription:
"""Return the description (a raw statistical summary) of the dataset.
Returns:
Dict containing a description for each variable in the DataFrame.
"""
return self.description_set
def get_rejected_variables(self) -> set:
"""Get variables that are rejected for analysis (e.g. constant, mixed data types)
Returns:
a set of column names that are unsupported
"""
return {
alert.column_name
for alert in self.description_set.alerts
if alert.alert_type == AlertType.REJECTED
}
def to_file(self, output_file: Union[str, Path], silent: bool = True) -> None:
"""Write the report to a file.
Args:
output_file: The name or the path of the file to generate including the extension (.html, .json).
silent: if False, opens the file in the default browser or download it in a Google Colab environment
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
pillow_version = pkg_resources.get_distribution("Pillow").version
version_tuple = tuple(map(int, pillow_version.split(".")))
if version_tuple < (9, 5, 0):
warnings.warn(
"Try running command: 'pip install --upgrade Pillow' to avoid ValueError"
)
if not isinstance(output_file, Path):
output_file = Path(str(output_file))
if output_file.suffix == ".json":
data = self.to_json()
else:
if not self.config.html.inline:
self.config.html.assets_path = str(output_file.parent)
if self.config.html.assets_prefix is None:
self.config.html.assets_prefix = str(output_file.stem) + "_assets"
create_html_assets(self.config, output_file)
data = self.to_html()
if output_file.suffix != ".html":
suffix = output_file.suffix
output_file = output_file.with_suffix(".html")
warnings.warn(
f"Extension {suffix} not supported. For now we assume .html was intended. "
f"To remove this warning, please use .html or .json."
)
disable_progress_bar = not self.config.progress_bar
with tqdm(
total=1, desc="Export report to file", disable=disable_progress_bar
) as pbar:
output_file.write_text(data, encoding="utf-8")
pbar.update()
if not silent:
try:
from google.colab import files # noqa: F401
files.download(output_file.absolute().as_uri())
except ModuleNotFoundError:
import webbrowser
webbrowser.open_new_tab(output_file.absolute().as_uri())
def _render_html(self) -> str:
from ydata_profiling.report.presentation.flavours import HTMLReport
report = self.report
with tqdm(
total=1, desc="Render HTML", disable=not self.config.progress_bar
) as pbar:
html = HTMLReport(copy.deepcopy(report)).render(
nav=self.config.html.navbar_show,
offline=self.config.html.use_local_assets,
inline=self.config.html.inline,
assets_prefix=self.config.html.assets_prefix,
primary_color=self.config.html.style.primary_colors[0],
logo=self.config.html.style.logo,
theme=self.config.html.style.theme,
title=self.description_set.analysis.title,
date=self.description_set.analysis.date_start,
version=self.description_set.package["ydata_profiling_version"],
)
if self.config.html.minify_html:
from htmlmin.main import minify
html = minify(html, remove_all_empty_space=True, remove_comments=True)
pbar.update()
return html
def _render_widgets(self) -> Any:
from ydata_profiling.report.presentation.flavours import WidgetReport
report = self.report
with tqdm(
total=1,
desc="Render widgets",
disable=not self.config.progress_bar,
leave=False,
) as pbar:
widgets = WidgetReport(copy.deepcopy(report)).render()
pbar.update()
return widgets
def _render_json(self) -> str:
def encode_it(o: Any) -> Any:
if is_dataclass(o):
o = asdict(o)
if isinstance(o, dict):
return {encode_it(k): encode_it(v) for k, v in o.items()}
else:
if isinstance(o, (bool, int, float, str)):
return o
elif isinstance(o, list):
return [encode_it(v) for v in o]
elif isinstance(o, set):
return {encode_it(v) for v in o}
elif isinstance(o, pd.Series):
return encode_it(o.to_list())
elif isinstance(o, pd.DataFrame):
return encode_it(o.to_dict(orient="records"))
elif isinstance(o, np.ndarray):
return encode_it(o.tolist())
elif isinstance(o, Sample):
return encode_it(o.dict())
elif isinstance(o, np.generic):
return o.item()
else:
return str(o)
description = self.description_set
with tqdm(
total=1, desc="Render JSON", disable=not self.config.progress_bar
) as pbar:
description_dict = format_summary(description)
description_dict = encode_it(description_dict)
description_dict = redact_summary(description_dict, self.config)
data = json.dumps(description_dict, indent=4)
pbar.update()
return data
def to_html(self) -> str:
"""Generate and return complete template as lengthy string
for using with frameworks.
Returns:
Profiling report html including wrapper.
"""
return self.html
def to_json(self) -> str:
"""Represent the ProfileReport as a JSON string
Returns:
JSON string
"""
return self.json
def to_notebook_iframe(self) -> None:
"""Used to output the HTML representation to a Jupyter notebook.
When config.notebook.iframe.attribute is "src", this function creates a temporary HTML file
in `./tmp/profile_[hash].html` and returns an Iframe pointing to that contents.
When config.notebook.iframe.attribute is "srcdoc", the same HTML is injected in the "srcdoc" attribute of
the Iframe.
Notes:
This constructions solves problems with conflicting stylesheets and navigation links.
"""
from IPython.core.display import display
from ydata_profiling.report.presentation.flavours.widget.notebook import (
get_notebook_iframe,
)
# Ignore warning: https://github.com/ipython/ipython/pull/11350/files
with warnings.catch_warnings():
warnings.simplefilter("ignore")
display(get_notebook_iframe(self.config, self))
def to_widgets(self) -> None:
"""The ipython notebook widgets user interface."""
try:
from google.colab import files # noqa: F401
warnings.warn(
"Ipywidgets is not yet fully supported on Google Colab (https://github.com/googlecolab/colabtools/issues/60)."
"As an alternative, you can use the HTML report. See the documentation for more information."
)
except ModuleNotFoundError:
pass
from IPython.core.display import display
display(self.widgets)
def _repr_html_(self) -> None:
"""The ipython notebook widgets user interface gets called by the jupyter notebook."""
self.to_notebook_iframe()
def __repr__(self) -> str:
"""Override so that Jupyter Notebook does not print the object."""
return ""
def compare(
self, other: "ProfileReport", config: Optional[Settings] = None
) -> "ProfileReport":
"""Compare this report with another ProfileReport
Alias for:
```
ydata_profiling.compare([report1, report2], config=config)
```
See `ydata_profiling.compare` for details.
Args:
other: the ProfileReport to compare to
config: the settings object for the merged ProfileReport. If `None`, uses the caller's config
Returns:
Comparison ProfileReport
"""
from ydata_profiling.compare_reports import compare
return compare([self, other], config if config is not None else self.config)