|
19 | 19 |
|
20 | 20 | import sys |
21 | 21 | import collections |
| 22 | +import uuid |
22 | 23 |
|
23 | 24 | import tensorflow as tf |
24 | 25 | from tensorflow_io.core.python.ops import core_ops |
@@ -457,7 +458,8 @@ def __init__(self, |
457 | 458 | internal=False): |
458 | 459 | with tf.name_scope("AudioIOTensor") as scope: |
459 | 460 | resource, dtypes, shapes, rate = core_ops.wav_indexable_init( |
460 | | - filename, container=scope, shared_name=filename) |
| 461 | + filename, |
| 462 | + container=scope, shared_name="%s/%s" % (filename, uuid.uuid4().hex)) |
461 | 463 | shapes = [ |
462 | 464 | tf.TensorShape( |
463 | 465 | [None if dim < 0 else dim for dim in e.numpy() if dim != 0] |
@@ -493,27 +495,45 @@ class JSONIOTensor(IOTensor): |
493 | 495 | #============================================================================= |
494 | 496 | def __init__(self, |
495 | 497 | filename, |
| 498 | + columns=None, |
496 | 499 | internal=False): |
497 | 500 | with tf.name_scope("JSONIOTensor") as scope: |
| 501 | + metadata = [] |
| 502 | + if columns is not None: |
| 503 | + metadata.extend(["column: "+column for column in columns]) |
498 | 504 | resource, dtypes, shapes, columns = core_ops.json_indexable_init( |
499 | | - filename, container=scope, shared_name=filename) |
| 505 | + filename, metadata=metadata, |
| 506 | + container=scope, shared_name="%s/%s" % (filename, uuid.uuid4().hex)) |
500 | 507 | shapes = [ |
501 | 508 | tf.TensorShape( |
502 | 509 | [None if dim < 0 else dim for dim in e.numpy() if dim != 0] |
503 | 510 | ) for e in tf.unstack(shapes)] |
504 | 511 | dtypes = [tf.as_dtype(e.numpy()) for e in tf.unstack(dtypes)] |
505 | | - spec = [tf.TensorSpec(shape, dtype) for ( |
506 | | - shape, dtype) in zip(shapes, dtypes)] |
| 512 | + columns = [e.numpy() for e in tf.unstack(columns)] |
| 513 | + spec = [tf.TensorSpec(shape, dtype, column) for ( |
| 514 | + shape, dtype, column) in zip(shapes, dtypes, columns)] |
507 | 515 | if len(spec) == 1: |
508 | 516 | spec = spec[0] |
509 | 517 | else: |
510 | 518 | spec = tuple(spec) |
511 | | - columns = [e.numpy() for e in tf.unstack(columns)] |
| 519 | + self._filename = filename |
512 | 520 | super(JSONIOTensor, self).__init__( |
513 | 521 | spec, resource, core_ops.json_indexable_get_item, |
514 | 522 | None, |
515 | 523 | internal=internal) |
516 | 524 |
|
| 525 | + #============================================================================= |
| 526 | + # Accessors |
| 527 | + #============================================================================= |
| 528 | + |
| 529 | + def column(self, name): |
| 530 | + """The `TensorSpec` of column named `name`""" |
| 531 | + return next(e for e in tf.nest.flatten(self.spec) if e.name == name) |
| 532 | + |
| 533 | + def __call__(self, column): |
| 534 | + """Return a new JSONIOTensor with column named `column`""" |
| 535 | + return JSONIOTensor(self._filename, columns=[column], internal=True) |
| 536 | + |
517 | 537 | class KafkaIOTensor(IOIterableTensor): |
518 | 538 | """KafkaIOTensor""" |
519 | 539 |
|
@@ -541,7 +561,7 @@ def func_init(data): |
541 | 561 | """func_init""" |
542 | 562 | resource, _, _ = core_ops.kafka_iterable_init( |
543 | 563 | data["subscription"], metadata=data["metadata"], |
544 | | - container=scope, shared_name=subscription) |
| 564 | + container=scope, shared_name="%s/%s" % (filename, uuid.uuid4().hex)) |
545 | 565 | return resource |
546 | 566 | func_next = core_ops.kafka_iterable_next |
547 | 567 |
|
|
0 commit comments