Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to test all the logging capabilities
Currently , there are no tests to test textattack attack logging into files such as csv, text and json. Commit adds tests to test logging in the 3 above mentioned formats.
- Loading branch information
1 parent
646b50d
commit af17300
Showing
5 changed files
with
122 additions
and
0 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 |
---|---|---|
|
@@ -47,3 +47,4 @@ checkpoints/ | |
|
||
.vscode | ||
*.csv | ||
!tests/sample_outputs/csv_attack_log.csv |
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,3 @@ | ||
"original_text","perturbed_text","original_score","perturbed_score","original_output","perturbed_output","ground_truth_output","num_queries","result_type" | ||
"lovingly photographed in the manner of a golden book sprung to [[life]] , stuart little 2 [[manages]] [[sweetness]] largely without stickiness .","lovingly photographed in the manner of a golden book sprung to [[ife]] , stuart little 2 [[manager]] [[seetness]] largely without stickiness .",0.09334743022918701,0.6904040575027466,1,0,1,28,"Successful" | ||
"[[consistently]] [[clever]] and [[suspenseful]] .","[[conisstently]] [[celver]] and [[Huspenseful]] .",0.009427368640899658,0.8219608664512634,1,0,1,16,"Successful" |
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,13 @@ | ||
{ | ||
"Attack Results": { | ||
"Number of successful attacks:": 2, | ||
"Number of failed attacks:": 0, | ||
"Number of skipped attacks:": 0, | ||
"Original accuracy:": 100.0, | ||
"Accuracy under attack:": 0.0, | ||
"Attack success rate:": 100.0, | ||
"Average perturbed word %:": 45.0, | ||
"Average num. words per input:": 12.0, | ||
"Avg num queries:": 22.0 | ||
} | ||
} |
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,21 @@ | ||
--------------------------------------------- Result 1 --------------------------------------------- | ||
[[Positive (91%)]] --> [[Negative (69%)]] | ||
|
||
lovingly photographed in the manner of a golden book sprung to [[life]] , stuart little 2 [[manages]] [[sweetness]] largely without stickiness . | ||
|
||
lovingly photographed in the manner of a golden book sprung to [[ife]] , stuart little 2 [[manager]] [[seetness]] largely without stickiness . | ||
--------------------------------------------- Result 2 --------------------------------------------- | ||
[[Positive (99%)]] --> [[Negative (82%)]] | ||
|
||
[[consistently]] [[clever]] and [[suspenseful]] . | ||
|
||
[[conisstently]] [[celver]] and [[Huspenseful]] . | ||
Number of successful attacks: 2 | ||
Number of failed attacks: 0 | ||
Number of skipped attacks: 0 | ||
Original accuracy: 100.0% | ||
Accuracy under attack: 0.0% | ||
Attack success rate: 100.0% | ||
Average perturbed word %: 45.0% | ||
Average num. words per input: 12.0 | ||
Avg num queries: 22.0 |
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,84 @@ | ||
import json | ||
import os | ||
|
||
from helpers import run_command_and_get_result | ||
import pytest | ||
|
||
DEBUG = False | ||
|
||
""" | ||
Attack command-line tests in the format (name, args, sample_output_file) | ||
""" | ||
|
||
""" | ||
list_test_params data structure requires | ||
1) test name | ||
2) logger filetype - json/text/csv. # Future Work : Tests for Wandb and Visdom | ||
3) logger file name | ||
4) sample log file | ||
""" | ||
|
||
list_test_params = [ | ||
( | ||
"json_summary_logger", | ||
"json", | ||
"textattack attack --recipe deepwordbug --model lstm-mr --num-examples 2 --log-summary-to-json attack_summary.json", | ||
"attack_summary.json", | ||
"tests/sample_outputs/json_attack_summary.json", | ||
), | ||
( | ||
"csv_logger", | ||
"csv", | ||
"textattack attack --recipe deepwordbug --model lstm-mr --num-examples 2 --log-to-csv attack_log.csv", | ||
"attack_log.csv", | ||
"tests/sample_outputs/csv_attack_log.csv", | ||
), | ||
( | ||
"txt_logger", | ||
"txt", | ||
"textattack attack --recipe deepwordbug --model lstm-mr --num-examples 2 --log-to-txt attack_log.txt", | ||
"attack_log.txt", | ||
"tests/sample_outputs/txt_attack_log.txt", | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name, filetype, command, test_log_file, sample_log_file", list_test_params | ||
) | ||
def test_logger(name, filetype, command, test_log_file, sample_log_file): | ||
# Run command and validate outputs. | ||
result = run_command_and_get_result(command) | ||
|
||
assert result.stdout is not None | ||
assert result.stderr is not None | ||
assert result.returncode == 0 | ||
assert os.path.exists(test_log_file), f"{test_log_file} did not get generated" | ||
|
||
if filetype == "json": | ||
with open(sample_log_file) as f: | ||
desired_dictionary = json.load(f) | ||
|
||
with open(test_log_file) as f: | ||
test_dictionary = json.load(f) | ||
|
||
assert ( | ||
desired_dictionary == test_dictionary | ||
), f"{filetype} file {test_log_file} differs from {sample_log_file}" | ||
|
||
elif filetype == "csv" or filetype == "txt": | ||
assert ( | ||
os.system(f"diff {test_log_file} {sample_log_file}") == 0 | ||
), f"{filetype} file {test_log_file} differs from {sample_log_file}" | ||
|
||
# cleanup | ||
os.remove(test_log_file) | ||
|
||
|
||
# result = run_command_and_get_result(command) | ||
# stdout = result.stdout.decode().strip() | ||
# print("stdout =>", stdout) | ||
# stderr = result.stderr.decode().strip() | ||
# print("stderr =>", stderr) | ||
|
||
# assert stdout == desired_text |