This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
unit tests for display_data and eval_model #460
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sneaky