Skip to content

Commit

Permalink
Tests for Word Enrichment
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Feb 17, 2020
1 parent cf96f20 commit 58e36d5
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 19 deletions.
12 changes: 8 additions & 4 deletions orangecontrib/text/widgets/owwordenrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def __init__(self):

self.results = Result()

# info box
fbox = gui.widgetBox(self.controlArea, "Info")
self.info_fil = gui.label(fbox, self, 'Words displayed: 0')

# Filtering settings
fbox = gui.widgetBox(self.controlArea, "Filter")
hbox = gui.widgetBox(fbox, orientation=0)
Expand Down Expand Up @@ -175,7 +179,7 @@ def check_data(self):
def clear(self):
self.sig_words.clear()
self.info.set_input_summary(self.info.NoInput)
self.info.set_output_summary(self.info.NoOutput)
self.set_displayed_info(0)

def filter_enabled(self, b):
self.chb_p.setEnabled(b)
Expand All @@ -198,7 +202,7 @@ def filter_and_display(self):

for i in range(len(self.cols)):
self.sig_words.resizeColumnToContents(i)
self.set_output_info(count)
self.set_displayed_info(count)

def build_tree(self) -> int:
count = 0
Expand All @@ -224,8 +228,8 @@ def set_input_info(self) -> None:
f"Total words: {cluster_words}\n"
f"Words in subset: {selected_words}")

def set_output_info(self, count: int) -> None:
self.info.set_output_summary(str(count), f"{count} words displayed")
def set_displayed_info(self, count: int) -> None:
self.info_fil.setText(f"Words displayed: {count}")

def apply(self):
self.sig_words.clear()
Expand Down
204 changes: 189 additions & 15 deletions orangecontrib/text/widgets/tests/test_owwordenrichment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import unittest
from unittest.mock import Mock

import Orange
from Orange.data import Table, Domain
from Orange.widgets.tests.base import WidgetTest

from orangecontrib.text.corpus import Corpus
Expand All @@ -9,49 +12,50 @@

class TestWordEnrichment(WidgetTest):
def setUp(self):
# type: OWWordEnrichment
self.widget = self.create_widget(OWWordEnrichment)
self.corpus = Corpus.from_file('book-excerpts')
corpus = Corpus.from_file('book-excerpts')[::3]
vect = BowVectorizer()
self.corpus_vect = vect.transform(self.corpus)
self.corpus_vect = vect.transform(corpus)
self.subset_corpus = self.corpus_vect[:5]

@unittest.skipIf(
Orange.__version__ < "3.24.0", "wait_until_finished not supported")
def test_filter_fdr(self):
widget = self.widget
subset_corpus = self.corpus_vect[:10]

self.send_signal(widget.Inputs.data, self.corpus_vect)
self.send_signal(widget.Inputs.selected_data, subset_corpus)
self.send_signal(widget.Inputs.selected_data, self.subset_corpus)
self.wait_until_finished(timeout=10000)

# test p-value filter
widget.filter_by_p = True
widget.filter_p_value = 1e-9
widget.filter_p_value = 1e-3
widget.filter_by_fdr = False
widget.filter_fdr_value = 0.01

widget.filter_and_display()
self.assertEqual(widget.sig_words.topLevelItemCount(), 3)
self.assertEqual({widget.sig_words.topLevelItem(i).text(0)
for i in (0, 1, 2)}, {'livesey', 'doctor', 'rum'})
for i in (0, 1, 2)}, {'livesey', 'jim', 'doctor'})

# test fdr filter
widget.filter_by_p = True
widget.filter_p_value = 1e-4
widget.filter_p_value = 1e-1
widget.filter_by_fdr = True
widget.filter_fdr_value = 1e-4
widget.filter_fdr_value = 0.9

widget.filter_and_display()
self.assertEqual(widget.sig_words.topLevelItemCount(), 5)
self.assertEqual({widget.sig_words.topLevelItem(i).text(0)
for i in (0, 1, 2, 3, 4)},
{'livesey', 'doctor', 'rum', 'admiral', 'inn'})
self.assertEqual(widget.sig_words.topLevelItemCount(), 1)
self.assertEqual(widget.sig_words.topLevelItem(0).text(0), "doctor")

# test if different when fdr false
widget.filter_by_p = True
widget.filter_p_value = 1e-4
widget.filter_p_value = 1e-1
widget.filter_by_fdr = False
widget.filter_fdr_value = 1e-4

widget.filter_and_display()
self.assertEqual(widget.sig_words.topLevelItemCount(), 16)
self.assertEqual(widget.sig_words.topLevelItemCount(), 108)

# test no results
widget.filter_by_p = True
Expand All @@ -62,6 +66,176 @@ def test_filter_fdr(self):
widget.filter_and_display()
self.assertEqual(widget.sig_words.topLevelItemCount(), 0)

self.send_signal(widget.Inputs.data, None)
widget.filter_and_display()
self.assertEqual(widget.sig_words.topLevelItemCount(), 0)

def test_empty_selection(self):
w = self.widget

# empty selection
self.send_signal(w.Inputs.data, self.corpus_vect)
self.send_signal(w.Inputs.selected_data, self.subset_corpus[:0])
self.assertTrue(self.widget.Error.empty_selection.is_shown())

# when commands changed on non-valid data
w.controls.filter_by_p.click()

# selection not empty
self.send_signal(w.Inputs.selected_data, self.subset_corpus)
self.assertFalse(self.widget.Error.empty_selection.is_shown())

def test_no_bow_features(self):
w = self.widget

iris = Table("iris")
self.send_signal(w.Inputs.data, iris)
self.send_signal(w.Inputs.selected_data, iris[:10])
self.assertTrue(self.widget.Error.no_bow_features.is_shown())

# when commands changed on non-valid data
w.controls.filter_by_p.click()

self.send_signal(w.Inputs.data, None)
self.send_signal(w.Inputs.selected_data, None)
self.assertFalse(self.widget.Error.no_bow_features.is_shown())

def test_all_selected(self):
w = self.widget

self.send_signal(w.Inputs.data, self.corpus_vect)
self.send_signal(w.Inputs.selected_data, self.corpus_vect)
self.assertTrue(self.widget.Error.all_selected.is_shown())

# when commands changed on non-valid data
w.controls.filter_by_p.click()

self.send_signal(w.Inputs.data, None)
self.send_signal(w.Inputs.selected_data, None)
self.assertFalse(self.widget.Error.all_selected.is_shown())

def test_no_overlapping(self):
w = self.widget

# with one column bow it is easier
corpus_vect = Corpus.from_table(Domain(
self.corpus_vect.domain.attributes[:1],
self.corpus_vect.domain.class_var,
self.corpus_vect.domain.metas
), self.corpus_vect)

self.send_signal(w.Inputs.data, corpus_vect[10:15])
self.send_signal(w.Inputs.selected_data, corpus_vect[4:5])
self.assertTrue(self.widget.Error.no_words_overlap.is_shown())

# when commands changed on non-valid data
w.controls.filter_by_p.click()

self.send_signal(w.Inputs.selected_data, self.subset_corpus)
self.send_signal(w.Inputs.data, self.corpus_vect)
self.assertFalse(self.widget.Error.no_words_overlap.is_shown())

@unittest.skipIf(
Orange.__version__ < "3.24.0", "wait_until_finished not supported")
def test_input_info(self):
w = self.widget
input_sum = w.info.set_input_summary = Mock()

self.send_signal(w.Inputs.selected_data, self.subset_corpus)
self.send_signal(w.Inputs.data, self.corpus_vect)

input_sum.assert_called_with(
"5923|1204", "Total words: 5923\nWords in subset: 1204")

self.wait_until_stop_blocking()
self.send_signal(w.Inputs.selected_data, None)
self.send_signal(w.Inputs.data, None)
input_sum.assert_called_with(w.info.NoInput)

@unittest.skipIf(
Orange.__version__ < "3.24.0", "wait_until_finished not supported")
def test_output_info(self):
w = self.widget
w.filter_p_value = 1e-3
w.filter_by_p = True
w.filter_by_fdr = False

self.send_signal(w.Inputs.selected_data, self.subset_corpus)
self.send_signal(w.Inputs.data, self.corpus_vect)
self.wait_until_finished(timeout=10000)

self.assertEqual(w.info_fil.text(), "Words displayed: 3")

# test fdr filter
w.filter_by_p = True
w.filter_p_value = 1e-4
w.filter_by_fdr = True
w.filter_fdr_value = 1e-4
w.filter_and_display()
self.assertEqual(w.info_fil.text(), "Words displayed: 0")

self.send_signal(w.Inputs.selected_data, None)
self.assertEqual(w.info_fil.text(), "Words displayed: 0")
self.send_signal(w.Inputs.data, None)
self.assertEqual(w.info_fil.text(), "Words displayed: 0")

@unittest.skipIf(
Orange.__version__ < "3.24.0", "wait_until_finished not supported")
def test_filter_changed(self):
"""
This case tests whether function are correctly triggered when
values in filter field changes
"""
w = self.widget

self.send_signal(w.Inputs.data, self.corpus_vect)
self.send_signal(w.Inputs.selected_data, self.subset_corpus)
self.wait_until_finished(timeout=10000)

# test p-value filter
w.controls.filter_by_p.click() # set to true
w.controls.filter_p_value.valueChanged.emit(1e-3)
w.controls.filter_by_fdr.click() # set to false
w.controls.filter_fdr_value.valueChanged.emit(0.1)

self.assertEqual(w.sig_words.topLevelItemCount(), 3)
self.assertEqual({w.sig_words.topLevelItem(i).text(0)
for i in (0, 1, 2)}, {'livesey', 'jim', 'doctor'})

# # test fdr filter
w.controls.filter_p_value.valueChanged.emit(1e-1)
w.controls.filter_by_fdr.click() # set to True
w.controls.filter_fdr_value.valueChanged.emit(0.9)

self.assertEqual(w.sig_words.topLevelItemCount(), 1)
self.assertEqual(w.sig_words.topLevelItem(0).text(0), "doctor")

# test if different when fdr false
w.controls.filter_by_fdr.click() # set to False

self.assertEqual(w.sig_words.topLevelItemCount(), 108)

# # # test no results
w.controls.filter_p_value.valueChanged.emit(1e-11)

self.assertEqual(w.sig_words.topLevelItemCount(), 0)

@unittest.skipIf(
Orange.__version__ < "3.24.0", "wait_until_finished not supported")
def test_report(self):
"""
Just test if report works.
"""
w = self.widget

w.send_report()

self.send_signal(w.Inputs.data, self.corpus_vect)
self.send_signal(w.Inputs.selected_data, self.subset_corpus)
self.wait_until_finished(timeout=10000)

w.send_report()


if __name__ == "__main__":
unittest.main()

0 comments on commit 58e36d5

Please sign in to comment.