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

refactor: use Enum for verbosity #66

Merged
merged 3 commits into from
Aug 10, 2023
Merged
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
3 changes: 2 additions & 1 deletion api-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
],
"source": [
"import edvart\n",
"from edvart import Verbosity\n",
"\n",
"import plotly.offline as py\n",
"py.init_notebook_mode()"
Expand Down Expand Up @@ -223,7 +224,7 @@
"source": [
"report = edvart.DefaultReport(\n",
" dataset,\n",
" verbosity=0,\n",
" verbosity=Verbosity.LOW,\n",
" columns_overview=['Name', 'Survived'],\n",
" columns_univariate_analysis=['Name', 'Age', 'Pclass'],\n",
" groupby='Survived',\n",
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