File size: 3,598 Bytes
0b8359d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for google3.image.understanding.object_detection.utils.json_utils."""
import os

import tensorflow.compat.v1 as tf

from object_detection.utils import json_utils


class JsonUtilsTest(tf.test.TestCase):

  def testDumpReasonablePrecision(self):
    output_path = os.path.join(tf.test.get_temp_dir(), 'test.json')
    with tf.gfile.GFile(output_path, 'w') as f:
      json_utils.Dump(1.0, f, float_digits=2)
    with tf.gfile.GFile(output_path, 'r') as f:
      self.assertEqual(f.read(), '1.00')

  def testDumpPassExtraParams(self):
    output_path = os.path.join(tf.test.get_temp_dir(), 'test.json')
    with tf.gfile.GFile(output_path, 'w') as f:
      json_utils.Dump([1.12345], f, float_digits=2, indent=3)
    with tf.gfile.GFile(output_path, 'r') as f:
      self.assertEqual(f.read(), '[\n   1.12\n]')

  def testDumpZeroPrecision(self):
    output_path = os.path.join(tf.test.get_temp_dir(), 'test.json')
    with tf.gfile.GFile(output_path, 'w') as f:
      json_utils.Dump(1.0, f, float_digits=0, indent=3)
    with tf.gfile.GFile(output_path, 'r') as f:
      self.assertEqual(f.read(), '1')

  def testDumpUnspecifiedPrecision(self):
    output_path = os.path.join(tf.test.get_temp_dir(), 'test.json')
    with tf.gfile.GFile(output_path, 'w') as f:
      json_utils.Dump(1.012345, f)
    with tf.gfile.GFile(output_path, 'r') as f:
      self.assertEqual(f.read(), '1.012345')

  def testDumpsReasonablePrecision(self):
    s = json_utils.Dumps(1.12545, float_digits=2)
    self.assertEqual(s, '1.13')

  def testDumpsPassExtraParams(self):
    s = json_utils.Dumps([1.0], float_digits=2, indent=3)
    self.assertEqual(s, '[\n   1.00\n]')

  def testDumpsZeroPrecision(self):
    s = json_utils.Dumps(1.0, float_digits=0)
    self.assertEqual(s, '1')

  def testDumpsUnspecifiedPrecision(self):
    s = json_utils.Dumps(1.012345)
    self.assertEqual(s, '1.012345')

  def testPrettyParams(self):
    s = json_utils.Dumps({'v': 1.012345, 'n': 2}, **json_utils.PrettyParams())
    self.assertEqual(s, '{\n  "n": 2,\n  "v": 1.0123\n}')

  def testPrettyParamsExtraParamsInside(self):
    s = json_utils.Dumps(
        {'v': 1.012345,
         'n': float('nan')}, **json_utils.PrettyParams(allow_nan=True))
    self.assertEqual(s, '{\n  "n": NaN,\n  "v": 1.0123\n}')

    with self.assertRaises(ValueError):
      s = json_utils.Dumps(
          {'v': 1.012345,
           'n': float('nan')}, **json_utils.PrettyParams(allow_nan=False))

  def testPrettyParamsExtraParamsOutside(self):
    s = json_utils.Dumps(
        {'v': 1.012345,
         'n': float('nan')}, allow_nan=True, **json_utils.PrettyParams())
    self.assertEqual(s, '{\n  "n": NaN,\n  "v": 1.0123\n}')

    with self.assertRaises(ValueError):
      s = json_utils.Dumps(
          {'v': 1.012345,
           'n': float('nan')}, allow_nan=False, **json_utils.PrettyParams())


if __name__ == '__main__':
  tf.test.main()