1
0
mirror of https://github.com/QData/TextAttack.git synced 2021-10-13 00:05:06 +03:00

Final Update (hopefully)

Log to file by using `--log-to-txt` and `---log-to-csv`

If no argument is given, default file name and path is used

Otherwise, file name/path specified by argument

test command changed to accompany new `--log-to-csv` name

use `--csv-style` to switch between fancy to plain
This commit is contained in:
Hanyu Liu
2020-07-24 17:34:00 -04:00
parent 9c8b9b6b8a
commit ac95db5611
4 changed files with 55 additions and 54 deletions

View File

@@ -423,8 +423,6 @@ def parse_logger_from_args(args):
outputs_dir = os.path.join(
current_dir, os.pardir, os.pardir, os.pardir, "outputs", "attacks"
)
if not os.path.exists(outputs_dir):
os.makedirs(outputs_dir)
out_dir_txt = out_dir_csv = os.path.normpath(outputs_dir)
# Get default txt and csv file names
@@ -435,31 +433,31 @@ def parse_logger_from_args(args):
filename_txt = f"{args.model}_{timestamp}.txt"
filename_csv = f"{args.model}_{timestamp}.csv"
# if "--out-file-txt" is called
if args.out_file_txt:
# if '--log-to-txt' specified with arguments
if args.log_to_txt:
# if user decide to save to a specific directory
if args.out_file_txt[-1] == "/":
out_dir_txt = args.out_file_txt
if args.log_to_txt[-1] == "/":
out_dir_txt = args.log_to_txt
# else if path + filename is given
elif args.out_file_txt[-4:] == ".txt":
out_dir_txt = args.out_file_txt.rsplit("/", 1)[0]
filename_txt = args.out_file_txt.rsplit("/", 1)[-1]
elif args.log_to_txt[-4:] == ".txt":
out_dir_txt = args.log_to_txt.rsplit("/", 1)[0]
filename_txt = args.log_to_txt.rsplit("/", 1)[-1]
# otherwise, customize filename
else:
filename_txt = f"{args.out_file_txt}.txt"
filename_txt = f"{args.log_to_txt}.txt"
# if "--out-file-csv" is called
if args.out_file_csv:
# if "--log-to-csv" is called
if args.log_to_csv:
# if user decide to save to a specific directory
if args.out_file_csv[-1] == "/":
out_dir_csv = args.out_file_csv
if args.log_to_csv[-1] == "/":
out_dir_csv = args.log_to_csv
# else if path + filename is given
elif args.out_file_csv[-4:] == ".csv":
out_dir_csv = args.out_file_csv.rsplit("/", 1)[0]
filename_csv = args.out_file_csv.rsplit("/", 1)[-1]
elif args.log_to_csv[-4:] == ".csv":
out_dir_csv = args.log_to_csv.rsplit("/", 1)[0]
filename_csv = args.log_to_csv.rsplit("/", 1)[-1]
# otherwise, customize filename
else:
filename_csv = f"{args.out_file_csv}.csv"
filename_csv = f"{args.log_to_csv}.csv"
# in case directory specified doesn't exist
if not os.path.exists(out_dir_txt):
@@ -467,13 +465,13 @@ def parse_logger_from_args(args):
if not os.path.exists(out_dir_csv):
os.makedirs(out_dir_csv)
# if "--log-to-file" specified in terminal command, save to a txt file
if args.log_to_file:
# if "--log-to-txt" specified in terminal command (with or without arg), save to a txt file
if args.log_to_txt == "" or args.log_to_txt:
attack_log_manager.add_output_file(os.path.join(out_dir_txt, filename_txt))
# if "--enable-csv" specified in terminal command, save to a csv file
if args.enable_csv:
color_method = None if args.enable_csv == "plain" else "file"
# if "--log-to-csv" specified in terminal command(with or without arg), save to a csv file
if args.log_to_csv == "" or args.log_to_csv:
color_method = None if args.csv_style == "plain" else "file"
csv_path = os.path.join(out_dir_csv, filename_csv)
attack_log_manager.add_output_csv(csv_path, color_method)
textattack.shared.logger.info(f"Logging to CSV at path {csv_path}.")