-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestTopicModeling: Test saved selection
- Loading branch information
1 parent
903293b
commit 039a1d5
Showing
1 changed file
with
38 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,59 @@ | ||
import itertools | ||
import unittest | ||
|
||
from AnyQt.QtCore import QItemSelection, QItemSelectionRange, \ | ||
QItemSelectionModel, QModelIndex | ||
import numpy as np | ||
from AnyQt.QtCore import QItemSelectionModel | ||
|
||
from Orange.widgets.tests.base import WidgetTest | ||
from PyQt5 import Qt | ||
from PyQt5.QtTest import QTest | ||
from orangecontrib.text.corpus import Corpus | ||
from orangecontrib.text.widgets.owtopicmodeling import OWTopicModeling | ||
|
||
|
||
class TestTopicModeling(WidgetTest): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.corpus = Corpus.from_file('deerwester') | ||
|
||
def setUp(self): | ||
self.widget = self.create_widget(OWTopicModeling) | ||
self.corpus = Corpus.from_file('deerwester') | ||
|
||
def test_data(self): | ||
def until(): | ||
return bool(self.get_output(self.widget.Outputs.selected_topic)) | ||
|
||
self.send_signal(self.widget.Inputs.corpus, self.corpus) | ||
# otherwise fails in learning task as if it was sent None | ||
self.wait_until_finished() | ||
self.process_events(until) | ||
|
||
self.send_signal(self.widget.Inputs.corpus, None) | ||
self.wait_until_finished() | ||
|
||
output = self.get_output(self.widget.Outputs.selected_topic) | ||
self.assertIsNone(output) | ||
|
||
def test_saved_selection(self): | ||
def until(widget=self.widget): | ||
return bool(self.get_output(widget.Outputs.selected_topic, | ||
widget=widget)) | ||
|
||
self.send_signal(self.widget.Inputs.corpus, self.corpus) | ||
self.wait_until_finished() | ||
id = self.widget.topic_desc.model().index(2, 0) | ||
self.widget.topic_desc.selectionModel().select(id, | ||
QItemSelectionModel.Rows | | ||
QItemSelectionModel.Select) | ||
ids_1 = self.get_output(self.widget.Outputs.selected_topic).ids | ||
self.process_events(until) | ||
idx = self.widget.topic_desc.model().index(2, 0) | ||
self.widget.topic_desc.selectionModel().select( | ||
idx, QItemSelectionModel.Rows | QItemSelectionModel.ClearAndSelect) | ||
output1 = self.get_output(self.widget.Outputs.selected_topic) | ||
state = self.widget.settingsHandler.pack_data(self.widget) | ||
w = self.create_widget( | ||
OWTopicModeling, stored_settings=state | ||
) | ||
self.send_signal(w.Inputs.corpus, self.corpus, widget=w) | ||
ids_2 = self.get_output(w.Outputs.selected_topic, widget=w).ids | ||
self.assertSequenceEqual(list(ids_1), list(ids_2)) | ||
|
||
w = self.create_widget(OWTopicModeling, stored_settings=state) | ||
self.send_signal(w.Inputs.corpus, self.corpus, widget=w) | ||
self.process_events(lambda: until(w)) | ||
output2 = self.get_output(w.Outputs.selected_topic, widget=w) | ||
# gensim uses quicksort, so sorting is unstable | ||
m1 = output1.metas[output1.metas[:, 0].argsort()] | ||
m2 = output2.metas[output2.metas[:, 0].argsort()] | ||
# test words and weights separately, weights are not exactly equal | ||
self.assertTrue((m1[:, 0] == m2[:, 0]).all()) | ||
np.testing.assert_allclose(m1[:, 1].astype(float), | ||
m2[:, 1].astype(float)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
unittest.main() |