Skip to content

Commit f9f8b37

Browse files
committed
Change FeaturesDict.__repr__ method.
Move `pprint_features_dict` func to `features_dict.py`
1 parent a6f2668 commit f9f8b37

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

tensorflow_datasets/core/features/features_dict.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
import six
2424
import tensorflow as tf
25+
import tensorflow_datasets as tfds
2526

2627
from tensorflow_datasets.core import utils
2728
from tensorflow_datasets.core.features import feature as feature_lib
2829
from tensorflow_datasets.core.features import top_level_feature
29-
from tensorflow_datasets.scripts.document_datasets import pprint_features_dict
3030

3131

3232
class FeaturesDict(top_level_feature.TopLevelFeature):
@@ -139,7 +139,13 @@ def __iter__(self):
139139

140140
def __repr__(self):
141141
"""Display the feature dictionary."""
142-
return pprint_features_dict(self._feature_dict, types='FeaturesDict')
142+
lines = ['{}({{'.format(type(self).__name__)]
143+
for key, feature in sorted(list(self._feature_dict.items())):
144+
all_sub_lines = '\'{}\': {},'.format(key, feature)
145+
lines.extend(' ' + l for l in
146+
all_sub_lines.split('\n'))
147+
lines.append('})')
148+
return '\n'.join(lines)
143149

144150
def get_tensor_info(self):
145151
"""See base class for details."""
@@ -232,3 +238,26 @@ def to_feature(value):
232238
return FeaturesDict(value)
233239
else:
234240
raise ValueError('Feature not supported: {}'.format(value))
241+
242+
243+
def pprint_features_dict(features_dict, indent=0, add_prefix=True):
244+
"""Pretty-print tfds.features.FeaturesDict."""
245+
first_last_indent_str = " " * indent
246+
indent_str = " " * (indent + 4)
247+
first_line = "%s%s({" % (
248+
first_last_indent_str if add_prefix else "",
249+
type(features_dict).__name__,
250+
)
251+
lines = [first_line]
252+
for k in sorted(list(features_dict.keys())):
253+
v = features_dict[k]
254+
if isinstance(v, tfds.features.FeaturesDict) or (
255+
isinstance(v, tfds.features.Sequence) and
256+
isinstance(v, tfds.features.FeaturesDict)
257+
):
258+
v_str = pprint_features_dict(v, indent + 4, False)
259+
else:
260+
v_str = str(v)
261+
lines.append("%s'%s': %s," % (indent_str, k, v_str))
262+
lines.append("%s})" % first_last_indent_str)
263+
return "\n".join(lines)

tensorflow_datasets/core/features/features_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ def test_feature_getitem(self):
196196
self.assertEqual(fdict['integer'].dtype, tf.int32)
197197
self.assertEqual(fdict['string'].dtype, tf.string)
198198

199+
def test_feature__repr__(self):
200+
201+
FEATURE_STR = (\
202+
"FeaturesDict({\n"
203+
" 'label': Sequence(ClassLabel(shape=(), dtype=tf.int64, num_classes=2)),\n"
204+
" 'metadata': Sequence(FeaturesDict({\n"
205+
" 'frame': Image(shape=(32, 32, 3), dtype=tf.uint8),\n"
206+
" })),\n"
207+
"})")
208+
209+
feature_dict = features_lib.FeaturesDict({
210+
'metadata': features_lib.Sequence({
211+
'frame': features_lib.Image(shape=(32, 32, 3)),
212+
}),
213+
'label': features_lib.Sequence(features_lib.ClassLabel(names=["m", "f"])),
214+
})
215+
216+
self.assertEqual(str(feature_dict), FEATURE_STR)
217+
199218

200219
class FeatureTensorTest(testing.FeatureExpectationsTestCase):
201220

tensorflow_datasets/scripts/document_datasets.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -311,29 +311,9 @@ def make_module_to_builder_dict(datasets=None):
311311
return module_to_builder
312312

313313

314-
def pprint_features_dict(features_dict, indent=0, add_prefix=True, types=None):
315-
"""Pretty-print tfds.features.FeaturesDict."""
316-
first_last_indent_str = " " * indent
317-
indent_str = " " * (indent + 4)
318-
first_line = "%s%s({" % (
319-
first_last_indent_str if add_prefix else "",
320-
type(features_dict).__name__ if type is None else types,
321-
)
322-
lines = [first_line]
323-
for k in sorted(list(features_dict.keys())):
324-
v = features_dict[k]
325-
if hasattr(v, 'keys'):
326-
v_str = pprint_features_dict(v, indent + 4, False)
327-
else:
328-
v_str = str(v)
329-
lines.append("%s'%s': %s," % (indent_str, k, v_str))
330-
lines.append("%s})" % first_last_indent_str)
331-
return "\n".join(lines)
332-
333-
334314
def make_feature_information(info):
335315
"""Make feature information table."""
336-
return FEATURE_BLOCK % pprint_features_dict(info.features)
316+
return FEATURE_BLOCK % info.features
337317

338318

339319
def make_citation(citation):

0 commit comments

Comments
 (0)