Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add format specifier to file writer #118

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nnabla_rl/writers/file_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2020,2021 Sony Corporation.
# Copyright 2021 Sony Group Corporation.
# Copyright 2021,2022,2023,2024 Sony Group Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -22,14 +22,14 @@


class FileWriter(Writer):
def __init__(self, outdir, file_prefix):
def __init__(self, outdir, file_prefix, fmt="%.3f"):
super(FileWriter, self).__init__()
if isinstance(outdir, str):
outdir = pathlib.Path(outdir)
self._outdir = outdir
files.create_dir_if_not_exist(outdir=outdir)
self._file_prefix = file_prefix
self._fmt = '%.3f'
self._fmt = fmt

def write_scalar(self, iteration_num, scalar):
outfile = self._outdir / (self._file_prefix + '_scalar.tsv')
Expand Down
2 changes: 2 additions & 0 deletions test_resources/writers/evaluation_results_scalar%.3f.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
iteration mean std_dev min max median
1 2.000 1.414 0.000 4.000 2.000
2 changes: 2 additions & 0 deletions test_resources/writers/evaluation_results_scalar%.5f.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
iteration mean std_dev min max median
1 2.00000 1.41421 0.00000 4.00000 2.00000
2 changes: 2 additions & 0 deletions test_resources/writers/evaluation_results_scalar%f.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
iteration mean std_dev min max median
1 2.000000 1.414214 0.000000 4.000000 2.000000
25 changes: 23 additions & 2 deletions tests/writers/test_file_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2020,2021 Sony Corporation.
# Copyright 2021 Sony Group Corporation.
# Copyright 2021,2022,2023,2024 Sony Group Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
import tempfile

import numpy as np
import pytest

from nnabla_rl.writers.file_writer import FileWriter

Expand Down Expand Up @@ -62,6 +63,27 @@ def test_write_histogram(self):
os.path.join(test_file_dir, 'evaluation_results_histogram.tsv')
self._check_same_tsv_file(file_path, test_file_path)

@pytest.mark.parametrize("format", ["%f", "%.3f", "%.5f"])
def test_data_formatting(self, format):
with tempfile.TemporaryDirectory() as tmpdir:
test_returns = np.arange(5)
test_results = {}
test_results['mean'] = np.mean(test_returns)
test_results['std_dev'] = np.std(test_returns)
test_results['min'] = np.min(test_returns)
test_results['max'] = np.max(test_returns)
test_results['median'] = np.median(test_returns)

writer = FileWriter(outdir=tmpdir, file_prefix='actual_results', fmt=format)
writer.write_scalar(1, test_results)

actual_file_path = os.path.join(tmpdir, 'actual_results_scalar.tsv')

this_file_dir = os.path.dirname(__file__)
expected_file_dir = this_file_dir.replace('tests', 'test_resources')
expected_file_path = os.path.join(expected_file_dir, f'evaluation_results_scalar{format}.tsv')
self._check_same_tsv_file(actual_file_path, expected_file_path)

def _check_same_tsv_file(self, file_path1, file_path2):
# check each line
with open(file_path1, mode='rt') as data_1, \
Expand All @@ -71,5 +93,4 @@ def _check_same_tsv_file(self, file_path1, file_path2):


if __name__ == "__main__":
import pytest
pytest.main()
Loading