forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for examples display_data.py and eval_model.py. Chan…
…ged repeat_label agent to check for eval_labels
- Loading branch information
Emily Dinan
committed
Dec 19, 2017
1 parent
cc3657c
commit 41175f0
Showing
5 changed files
with
146 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
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
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright (c) 2017-present, Facebook, Inc. | ||
# All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
from examples.display_data import display_data | ||
from parlai.core.params import ParlaiParser | ||
|
||
import sys | ||
import unittest | ||
|
||
|
||
class TestDisplayData(unittest.TestCase): | ||
"""Basic tests on the display_data.py example.""" | ||
|
||
args = [ | ||
'--task', 'babi:task1k:1', | ||
] | ||
parser = ParlaiParser() | ||
opt = parser.parse_args(args, print_args=False) | ||
opt['num_examples'] = 1 | ||
|
||
def test_output(self): | ||
"""Does display_data reach the end of the loop?""" | ||
|
||
class display_output(object): | ||
def __init__(self): | ||
self.data = [] | ||
|
||
def write(self, s): | ||
self.data.append(s) | ||
|
||
def __str__(self): | ||
return "".join(self.data) | ||
|
||
old_out = sys.stdout | ||
output = display_output() | ||
try: | ||
sys.stdout = output | ||
display_data(self.opt) | ||
finally: | ||
# restore sys.stdout | ||
sys.stdout = old_out | ||
|
||
str_output = str(output) | ||
self.assertTrue(len(str_output) > 0, "Output is empty") | ||
self.assertTrue("[babi:task1k:1]:" in str_output, | ||
"Babi task did not print") | ||
self.assertTrue("~~" in str_output, "Example output did not complete") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright (c) 2017-present, Facebook, Inc. | ||
# All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
from examples.eval_model import eval_model | ||
from parlai.core.params import ParlaiParser | ||
|
||
import ast | ||
import unittest | ||
import sys | ||
|
||
|
||
class TestEvalModel(unittest.TestCase): | ||
"""Basic tests on the eval_model.py example.""" | ||
|
||
args = [ | ||
'--task', '#moviedd-reddit', | ||
'--datatype', 'valid', | ||
] | ||
|
||
parser = ParlaiParser() | ||
parser.set_defaults(datatype='valid') | ||
opt = parser.parse_args(args, print_args=False) | ||
opt['model'] = 'repeat_label' | ||
opt['num_examples'] = 5 | ||
opt['display_examples'] = False | ||
|
||
def test_output(self): | ||
"""Test output of running eval_model""" | ||
class display_output(object): | ||
def __init__(self): | ||
self.data = [] | ||
|
||
def write(self, s): | ||
self.data.append(s) | ||
|
||
def __str__(self): | ||
return "".join(self.data) | ||
|
||
old_out = sys.stdout | ||
output = display_output() | ||
try: | ||
sys.stdout = output | ||
eval_model(self.opt, self.parser, printargs=False) | ||
finally: | ||
# restore sys.stdout | ||
sys.stdout = old_out | ||
|
||
str_output = str(output) | ||
self.assertTrue(len(str_output) > 0, "Output is empty") | ||
|
||
# decode the output | ||
scores = str_output.split("\n---\n") | ||
for i in range(1, len(scores)): | ||
score = ast.literal_eval(scores[i]) | ||
# check totals | ||
self.assertTrue(score['total'] == i, | ||
"Total is incorrect") | ||
# accuracy should be one | ||
self.assertTrue(score['accuracy'] == 1, | ||
"accuracy != 1") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |