Skip to content

Commit 5d29c6a

Browse files
Merge pull request #692 from us:document_dataset_bug
PiperOrigin-RevId: 257705157
2 parents cb33a61 + f9f8b37 commit 5d29c6a

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

tensorflow_datasets/core/features/features_dict.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ def __iter__(self):
138138

139139
def __repr__(self):
140140
"""Display the feature dictionary."""
141-
return '{}({})'.format(type(self).__name__, self._feature_dict)
141+
lines = ['{}({{'.format(type(self).__name__)]
142+
# Add indentation
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 all_sub_lines.split('\n'))
146+
lines.append('})')
147+
return '\n'.join(lines)
142148

143149
def get_tensor_info(self):
144150
"""See base class for details."""

tensorflow_datasets/core/features/features_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ 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+
label = features_lib.ClassLabel(names=['m', 'f'])
202+
feature_dict = features_lib.FeaturesDict({
203+
'metadata': features_lib.Sequence({
204+
'frame': features_lib.Image(shape=(32, 32, 3)),
205+
}),
206+
'label': features_lib.Sequence(label),
207+
})
208+
209+
self.assertEqual(repr(feature_dict), FEATURE_STR)
210+
211+
212+
FEATURE_STR = """FeaturesDict({
213+
'label': Sequence(ClassLabel(shape=(), dtype=tf.int64, num_classes=2)),
214+
'metadata': Sequence({
215+
'frame': Image(shape=(32, 32, 3), dtype=tf.uint8),
216+
}),
217+
})"""
218+
199219

200220
class FeatureTensorTest(testing.FeatureExpectationsTestCase):
201221

tensorflow_datasets/core/features/sequence_feature.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,13 @@ def __setstate__(self, state):
194194
del state['__getattr__']
195195
self.__dict__.update(state)
196196

197-
def _additional_repr_info(self):
198-
"""Override to return additional info to go into __repr__."""
199-
return {'feature': repr(self._feature)}
197+
def __repr__(self):
198+
"""Display the feature."""
199+
inner_feature_repr = repr(self._feature)
200+
if inner_feature_repr.startswith('FeaturesDict('):
201+
# Minor formatting cleaning: 'Sequence(FeaturesDict({' => 'Sequence({'
202+
inner_feature_repr = inner_feature_repr[len('FeaturesDict('):-len(')')]
203+
return '{}({})'.format(type(self).__name__, inner_feature_repr)
200204

201205

202206
def stack_arrays(*elems):

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):
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__,
321-
)
322-
lines = [first_line]
323-
for k in sorted(list(features_dict.keys())):
324-
v = features_dict[k]
325-
if isinstance(v, tfds.features.FeaturesDict):
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)