Skip to content

Commit

Permalink
refactor: use Enum for verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
lukany committed Aug 9, 2023
1 parent 14fab92 commit d85be60
Show file tree
Hide file tree
Showing 28 changed files with 553 additions and 633 deletions.
3 changes: 2 additions & 1 deletion api-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jupyter:
%autoreload 2

import edvart
from edvart import Verbosity

import plotly.offline as py
py.init_notebook_mode()
Expand All @@ -37,7 +38,7 @@ dataset.head()
```python
report = edvart.DefaultReport(
dataset,
verbosity=0,
verbosity=Verbosity.LOW,
columns_overview=['Name', 'Survived'],
columns_univariate_analysis=['Name', 'Age', 'Pclass'],
groupby='Survived',
Expand Down
15 changes: 8 additions & 7 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ The verbosity helps us to generate a code with a specific level of detail.

edvart supports three levels of verbosity:

- verbosity 0
- LOW
- High level functions for whole sections are generated. User can modify the markdown description.
- verbosity 1
- MEDIUM
- edvart functions are generated. User can modify parameters of these functions.
- verbosity 2
- HIGH
- Raw code is generated. User can do very advanced modification such as changing visualisations style.

The verbosity can be set to whole report or to each section separately.
Expand All @@ -176,17 +176,18 @@ Examples:

.. code-block:: python
# Set default verbosity for all sections to 1
# Set default verbosity for all sections to Verbosity.MEDIUM
import edvart
from edvart import Verbosity
df = edvart.example_datasets.dataset_titanic()
edvart.DefaultReport(df, verbosity=1).export_notebook("test-export.ipynb")
edvart.DefaultReport(df, verbosity=Verbosity.MEDIUM).export_notebook("test-export.ipynb")
.. code-block:: python
# Set default verbosity to 1 but use verbosity 2 for univariate analysis
# Set default verbosity to Verbosity.MEDIUM but use verbosity Verbosity.HIGH for univariate analysis
import edvart
df = edvart.example_datasets.dataset_titanic()
edvart.DefaultReport(df, verbosity=1, verbosity_univariate_analysis=2).export_notebook("test-export.ipynb")
edvart.DefaultReport(df, verbosity=Verbosity.MEDIUM, verbosity_univariate_analysis=Verbosity.HIGH).export_notebook("test-export.ipynb")
1 change: 1 addition & 0 deletions edvart/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from edvart import example_datasets
from edvart.report import DefaultReport, DefaultTimeseriesReport, Report, TimeseriesReport
from edvart.report_sections.dataset_overview import Overview
from edvart.report_sections.section_base import Verbosity

logging.basicConfig(level=logging.INFO)

Expand Down
271 changes: 106 additions & 165 deletions edvart/report.py

Large diffs are not rendered by default.

62 changes: 27 additions & 35 deletions edvart/report_sections/bivariate_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from edvart import utils
from edvart.data_types import is_boolean, is_categorical, is_numeric
from edvart.report_sections.code_string_formatting import get_code, total_dedent
from edvart.report_sections.section_base import ReportSection, Section
from edvart.report_sections.section_base import ReportSection, Section, Verbosity


class BivariateAnalysis(ReportSection):
Expand All @@ -26,16 +26,8 @@ class BivariateAnalysis(ReportSection):
subsections : List[BivariateAnalysisSubsection], optional
List of subsections to include.
All subsection in BivariateAnalysisSubsection are included by default.
verbosity : int (default = 0)
Generated code verbosity global to the Bivariate analysis sections, must be on of [0, 1, 2].
0
A single cell which generates the bivariate analysis section is exported.
1
Parameterizable function calls for each subsection are exported.
2
Similar to 1, but in addition function definitions are also exported.
verbosity : Verbosity (default = Verbosity.LOW)
Generated code verbosity global to the Bivariate analysis sections.
If subsection verbosities are None, then they will be overridden by this parameter.
columns : List[str], optional
Columns on which to do bivariate analysis.
Expand All @@ -60,11 +52,11 @@ class BivariateAnalysis(ReportSection):
`columns`, `columns_x`, `columns_y` is specified. In that case, the first elements of each
pair are treated as `columns_x` and the second elements as `columns_y` in pairplots and
correlations.
verbosity_correlations : int, optional
verbosity_correlations : Verbosity, optional
Correlation plots subsection code verbosity.
verbosity_pairplot: int, optional
verbosity_pairplot: Verbosity, optional
Pairplot subsection code verbosity.
verbosity_contingency_table: int, optional
verbosity_contingency_table: Verbosity, optional
Contingency table subsection code verbosity.
color_col : str, optional
Name of column according to use for coloring of the bivariate analysis subsections.
Expand All @@ -90,14 +82,14 @@ def __str__(self):
def __init__(
self,
subsections: Optional[List[BivariateAnalysisSubsection]] = None,
verbosity: int = 0,
verbosity: Verbosity = Verbosity.LOW,
columns: Optional[List[str]] = None,
columns_x: Optional[List[str]] = None,
columns_y: Optional[List[str]] = None,
columns_pairs: Optional[List[Tuple[str, str]]] = None,
verbosity_correlations: Optional[int] = None,
verbosity_pairplot: Optional[int] = None,
verbosity_contingency_table: Optional[int] = None,
verbosity_correlations: Optional[Verbosity] = None,
verbosity_pairplot: Optional[Verbosity] = None,
verbosity_contingency_table: Optional[Verbosity] = None,
color_col: Optional[str] = None,
):
verbosity_correlations = (
Expand Down Expand Up @@ -214,7 +206,7 @@ def bivariate_analysis(
"""
bivariate_analysis = BivariateAnalysis(
subsections=subsections,
verbosity=0,
verbosity=Verbosity.LOW,
columns=columns,
columns_x=columns_x,
columns_y=columns_y,
Expand All @@ -237,7 +229,7 @@ def add_cells(self, cells: List[Dict[str, Any]]) -> None:
"""
section_header = nbfv4.new_markdown_cell(self.get_title(section_level=1))
cells.append(section_header)
if self.verbosity == 0:
if self.verbosity == Verbosity.LOW:
code = "bivariate_analysis(df=df"
if self.subsections_0 is not None:
arg_subsections_names = [
Expand All @@ -258,7 +250,7 @@ def add_cells(self, cells: List[Dict[str, Any]]) -> None:
code += ")"
cells.append(nbfv4.new_code_cell(code))
for sub in self.subsections:
if sub.verbosity > 0:
if sub.verbosity > Verbosity.LOW:
sub.add_cells(cells)
else:
super().add_cells(cells)
Expand All @@ -272,15 +264,15 @@ def required_imports(self) -> List[str]:
List of import strings to be added at the top of the generated notebook,
e.g. ['import pandas as pd', 'import numpy as np']
"""
if self.verbosity != 0:
if self.verbosity != Verbosity.LOW:
return super().required_imports()

imports = {
"from edvart.report_sections.bivariate_analysis import BivariateAnalysis\n"
"bivariate_analysis = BivariateAnalysis.bivariate_analysis"
}
for subsec in self.subsections:
if subsec.verbosity > 0:
if subsec.verbosity > Verbosity.LOW:
imports.update(subsec.required_imports())

return list(imports)
Expand All @@ -302,7 +294,7 @@ class CorrelationPlot(Section):
Parameters
----------
verbosity : int (default = 0)
verbosity : Verbosity (default = Verbosity.LOW)
Verbosity of the code generated in the exported notebook.
columns : List[str], optional
Columns on which to plot pair-wise correlation plot.
Expand All @@ -326,7 +318,7 @@ class CorrelationPlot(Section):

def __init__(
self,
verbosity: int = 0,
verbosity: Verbosity = Verbosity.LOW,
columns: Optional[List[str]] = None,
columns_x: Optional[List[str]] = None,
columns_y: Optional[List[str]] = None,
Expand Down Expand Up @@ -507,7 +499,7 @@ def required_imports(self) -> List[str]:
List of import strings to be added at the top of the generated notebook,
e.g. ['import pandas as pd', 'import numpy as np'].
"""
if self.verbosity <= 1:
if self.verbosity <= Verbosity.MEDIUM:
return [
total_dedent(
"""
Expand Down Expand Up @@ -546,7 +538,7 @@ def add_cells(self, cells: List[Dict[str, Any]]) -> None:

default_call += ")"

if self.verbosity <= 1:
if self.verbosity <= Verbosity.MEDIUM:
code = default_call
else:
code = (
Expand Down Expand Up @@ -582,7 +574,7 @@ class PairPlot(Section):
Parameters
----------
verbosity : int (default = 0)
verbosity : Verbosity (default = Verbosity.LOW)
Verbosity of the code generated in the exported notebook.
columns : List[str], optional
Columns on which to plot the pairplot.
Expand All @@ -608,7 +600,7 @@ class PairPlot(Section):

def __init__(
self,
verbosity: int = 0,
verbosity: Verbosity = Verbosity.LOW,
columns: Optional[List[str]] = None,
columns_x: Optional[List[str]] = None,
columns_y: Optional[List[str]] = None,
Expand Down Expand Up @@ -689,7 +681,7 @@ def required_imports(self) -> List[str]:
List of import strings to be added at the top of the generated notebook,
e.g. ['import pandas as pd', 'import numpy as np'].
"""
if self.verbosity <= 1:
if self.verbosity <= Verbosity.MEDIUM:
return [
total_dedent(
"""
Expand Down Expand Up @@ -725,7 +717,7 @@ def add_cells(self, cells: List[Dict[str, Any]]) -> None:
default_call += f", color_col='{self.color_col}'"
default_call += ")"

if self.verbosity <= 1:
if self.verbosity <= Verbosity.MEDIUM:
code = default_call
else:
code = get_code(PairPlot.plot_pairplot) + "\n\n" + default_call
Expand Down Expand Up @@ -755,7 +747,7 @@ class ContingencyTable(Section):
Parameters
----------
verbosity : int (default = 0)
verbosity : Verbosity (default = Verbosity.LOW)
Verbosity of the code generated in the exported notebook.
columns : List[str], optional
Columns on which to show contingency tables.
Expand All @@ -782,7 +774,7 @@ class ContingencyTable(Section):

def __init__(
self,
verbosity: int = 0,
verbosity: Verbosity = Verbosity.LOW,
columns: Optional[List[str]] = None,
columns_x: Optional[List[str]] = None,
columns_y: Optional[List[str]] = None,
Expand Down Expand Up @@ -946,7 +938,7 @@ def required_imports(self) -> List[str]:
List of import strings to be added at the top of the generated notebook,
e.g. ['import pandas as pd', 'import numpy as np'].
"""
if self.verbosity <= 1:
if self.verbosity <= Verbosity.MEDIUM:
return [
total_dedent(
"""
Expand Down Expand Up @@ -983,7 +975,7 @@ def add_cells(self, cells: List[Dict[str, Any]]) -> None:
default_call += f", columns={self.columns}"
default_call += ")"

if self.verbosity <= 1:
if self.verbosity <= Verbosity.MEDIUM:
code = default_call
else:
code = (
Expand Down
Loading

0 comments on commit d85be60

Please sign in to comment.