Skip to content

Commit

Permalink
add check display tests (deepchecks#735)
Browse files Browse the repository at this point in the history
* add check display tests

* dataset functionality removed

* fix IPython requirements

Co-authored-by: Itay Gabbay <[email protected]>
  • Loading branch information
benisraeldan and Itay Gabbay authored Jan 25, 2022
1 parent 6061902 commit 82b447e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
4 changes: 2 additions & 2 deletions deepchecks/base/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import numpy as np
import ipywidgets as widgets
import plotly.graph_objects as go
from IPython.core.display import display_html
from IPython.display import display_html
from pandas.io.formats.style import Styler
from plotly.basedatatypes import BaseFigure
import plotly
Expand Down Expand Up @@ -110,7 +110,7 @@ def __init__(self, value, header: str = None, display: Any = None):
raise DeepchecksValueError(f'Can\'t display item of type: {type(item)}')

def display_check(self, unique_id: str = None, as_widget: bool = False,
show_additional_outputs=True): # pragma: no cover
show_additional_outputs=True):
"""Display the check result or return the display as widget.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion deepchecks/base/display_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import tqdm
from tqdm.notebook import tqdm as tqdm_notebook
import pandas as pd
from IPython.core.display import display, display_html
from IPython.display import display, display_html
from IPython import get_ipython
import ipywidgets as widgets
from ipywidgets.embed import embed_minimal_html
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ scikit-learn>=0.24.2
jsonpickle>=2

# require for python 3.8+
ipython>=7.15.0; python_version >= '3.8'
ipython>=7.15.0,<8; python_version >= '3.8'
ipykernel>=5.3.0; python_version >= '3.8'
ipywidgets>=7.6.5; python_version >= '3.8'

# google colab requirements (python 3.7)
ipython>=5.5.0; python_version < '3.8'
ipython>=5.5.0,<8; python_version < '3.8'
ipykernel>=4.10.1; python_version < '3.8'
ipywidgets>=7.5.0; python_version < '3.8'

Expand Down
140 changes: 140 additions & 0 deletions tests/base/display_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# ----------------------------------------------------------------------------
# Copyright (C) 2021 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
#
"""display tests"""
import jsonpickle
import pandas as pd
import plotly.express
from plotly.graph_objs import FigureWidget
import plotly.io as pio

from deepchecks import CheckResult
from hamcrest import assert_that, equal_to, instance_of, has_length, is_, calling, raises, not_none
from ipywidgets import VBox

from deepchecks.errors import DeepchecksValueError
from deepchecks.checks import ColumnsInfo, DataDuplicates, MixedNulls

pio.renderers.default = "json"

# display check
def test_check_run_display(iris_dataset):
# Arrange
check_res = ColumnsInfo(n_top_columns=4).run(iris_dataset)
assert_that(check_res.display_check(), is_(None))


def test_check_run_display_as_widget(iris_dataset):
# Arrange
check_res = ColumnsInfo(n_top_columns=4).run(iris_dataset)
dispaly_box = check_res.display_check(as_widget=True)
# Assert
assert_that(dispaly_box, instance_of(VBox))
assert_that(dispaly_box.children, has_length(1))


def test_check_run_display_unique_id(iris_dataset):
# Arrange
check_res = ColumnsInfo(n_top_columns=4).run(iris_dataset)
# Assert
assert_that(check_res.display_check(unique_id=7), is_(None))


def test_check_run_display_condition(iris_dataset):
# Arrange
check_res = DataDuplicates().add_condition_ratio_not_greater_than(0).run(iris_dataset)
# Run
assert_that(check_res.display_check(unique_id=7), is_(None))


def test_check_run_display_nothing_to_show(iris_dataset):
# Arrange
check_res = MixedNulls().run(iris_dataset)

# Assert
check_res.display_check(unique_id=7)


def test_check_result_repr(iris_dataset):
# Arrange
check = MixedNulls()
check_res = check.run(iris_dataset)

# Assert
assert_that(check.__repr__(), is_('MixedNulls'))
assert_that(check_res.__repr__(), is_('Mixed Nulls: defaultdict(<class \'dict\'>, {})'))


def test_check_result_init(iris_dataset):
assert_that(calling(CheckResult).with_args(value=None, display={}),
raises(DeepchecksValueError, 'Can\'t display item of type: <class \'dict\'>'))


def test_check_result_display_plt_func():
# Arrange
def display_func():
return 'test'
check_res = CheckResult(value=7, header='test', display=[display_func])
check_res.check = DataDuplicates

# Assert
assert_that(check_res.display_check(), is_(None))
assert_that(check_res.display_check(as_widget=True), not_none())


def test_check_result_display_plotly(iris):
# Arrange
plot = plotly.express.bar(iris)
check_res = CheckResult(value=7, header='test', display=[plot])
check_res.check = DataDuplicates
display = check_res.display_check(as_widget=True)

# Assert
assert_that(display.children[1], instance_of(FigureWidget))


def test_check_result_to_json():
# Arrange
check_res = CheckResult(value=7, header='test', display=['hi'])
check_res.display = [{}]
check_res.check = DataDuplicates()

# Assert
assert_that(calling(check_res.to_json).with_args(),
raises(Exception, 'Unable to create json for item of type: <class \'dict\'>'))


def test_check_result_from_json(iris):
# Arrange
plot = plotly.express.bar(iris)

json_to_display = jsonpickle.dumps({
'display': [
('html', 'hi'),
('dataframe', pd.DataFrame({'a': [1, 2], 'b': [1, 2]}).to_json()),
('plotly', plot.to_json()),
('plt', 'Base64String')
],
'conditions_table': pd.DataFrame({'a': [1, 2], 'b': [1, 2]}).to_json(),
'header': 'header',
'summary': 'summary'

})

# Assert
assert_that(CheckResult.display_from_json(json_to_display), is_(None))


def test_check_result_show():
# Arrange
cr = CheckResult(value=0, header='test', display=[''])
cr.check = DataDuplicates()
# Assert
assert_that(cr.show(), is_(None))

0 comments on commit 82b447e

Please sign in to comment.