
    BVh#                        d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  edd       ej(                  d       G d dej*                  ej,                                      Z G d de      Z edddg       G d dej2                               Z ej6                   ej8                  eej:                  j<                               y)z9A type for representing values that may or may not exist.    N)
struct_pb2)	structure)composite_tensor)dtypes)ops)tensor_spec)	type_spec)gen_optional_ops)nested_structure_coder)deprecation)	tf_exportzexperimental.Optionalzdata.experimental.Optionalc                       e Zd ZdZej
                  dd       Zej
                  dd       Zej                  d        Z	e
d        Ze
d        Zy)	OptionalaX  Represents a value that may or may not be present.

  A `tf.experimental.Optional` can represent the result of an operation that may
  fail as a value, rather than raising an exception and halting execution. For
  example, `tf.data.Iterator.get_next_as_optional()` returns a
  `tf.experimental.Optional` that either contains the next element of an
  iterator if one exists, or an "empty" value that indicates the end of the
  sequence has been reached.

  `tf.experimental.Optional` can only be used with values that are convertible
  to `tf.Tensor` or `tf.CompositeTensor`.

  One can create a `tf.experimental.Optional` from a value using the
  `from_value()` method:

  >>> optional = tf.experimental.Optional.from_value(42)
  >>> print(optional.has_value())
  tf.Tensor(True, shape=(), dtype=bool)
  >>> print(optional.get_value())
  tf.Tensor(42, shape=(), dtype=int32)

  or without a value using the `empty()` method:

  >>> optional = tf.experimental.Optional.empty(
  ...   tf.TensorSpec(shape=(), dtype=tf.int32, name=None))
  >>> print(optional.has_value())
  tf.Tensor(False, shape=(), dtype=bool)
  Nc                     t        d      )aW  Returns a tensor that evaluates to `True` if this optional has a value.

    >>> optional = tf.experimental.Optional.from_value(42)
    >>> print(optional.has_value())
    tf.Tensor(True, shape=(), dtype=bool)

    Args:
      name: (Optional.) A name for the created operation.

    Returns:
      A scalar `tf.Tensor` of type `tf.bool`.
    zOptional.has_value()NotImplementedErrorselfnames     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/ops/optional_ops.py	has_valuezOptional.has_value?   s     4
55    c                     t        d      )a  Returns the value wrapped by this optional.

    If this optional does not have a value (i.e. `self.has_value()` evaluates to
    `False`), this operation will raise `tf.errors.InvalidArgumentError` at
    runtime.

    >>> optional = tf.experimental.Optional.from_value(42)
    >>> print(optional.get_value())
    tf.Tensor(42, shape=(), dtype=int32)

    Args:
      name: (Optional.) A name for the created operation.

    Returns:
      The wrapped value.
    zOptional.get_value()r   r   s     r   	get_valuezOptional.get_valueO   s    $ 4
55r   c                     t        d      )a  The type specification of an element of this optional.

    >>> optional = tf.experimental.Optional.from_value(42)
    >>> print(optional.element_spec)
    tf.TensorSpec(shape=(), dtype=tf.int32, name=None)

    Returns:
      A (nested) structure of `tf.TypeSpec` objects matching the structure of an
      element of this optional, specifying the type of individual components.
    zOptional.element_specr   r   s    r   element_speczOptional.element_specc   s     5
66r   c                 >    t        t        j                         |       S )aU  Returns an `Optional` that has no value.

    NOTE: This method takes an argument that defines the structure of the value
    that would be contained in the returned `Optional` if it had a value.

    >>> optional = tf.experimental.Optional.empty(
    ...   tf.TensorSpec(shape=(), dtype=tf.int32, name=None))
    >>> print(optional.has_value())
    tf.Tensor(False, shape=(), dtype=bool)

    Args:
      element_spec: A (nested) structure of `tf.TypeSpec` objects matching the
        structure of an element of this optional.

    Returns:
      A `tf.experimental.Optional` with no value.
    )_OptionalImplr
   optional_none)r   s    r   emptyzOptional.emptyq   s    & )779<HHr   c                 B   t        j                  d      5 }t        j                  d      5  t        j                  |       }t        j                  ||       }ddd       ddd       t        t        j                              S # 1 sw Y   2xY w# 1 sw Y   6xY w)a  Returns a `tf.experimental.Optional` that wraps the given value.

    >>> optional = tf.experimental.Optional.from_value(42)
    >>> print(optional.has_value())
    tf.Tensor(True, shape=(), dtype=bool)
    >>> print(optional.get_value())
    tf.Tensor(42, shape=(), dtype=int32)

    Args:
      value: A value to wrap. The value must be convertible to `tf.Tensor` or
        `tf.CompositeTensor`.

    Returns:
      A `tf.experimental.Optional` that wraps `value`.
    optionalvalueNr   )r   
name_scoper   type_spec_from_valueto_tensor_listr   r
   optional_from_value)r$   scoper   encoded_values       r   
from_valuezOptional.from_value   s    " 

	# Fu>>'" F 55e<!00uEFF
 ,,]G 	F FF Fs"   B,B	B	B	BBN)__name__
__module____qualname____doc__abcabstractmethodr   r   abstractpropertyr   staticmethodr!   r,    r   r   r   r      s    : 6 6 6 6& 7 7 I I(  r   r   )	metaclassc                   R     e Zd ZdZ fdZddZddZed        Zed        Z	 xZ
S )r   zConcrete implementation of `tf.experimental.Optional`.

  NOTE(mrry): This implementation is kept private, to avoid defining
  `Optional.__init__()` in the public API.
  c                 >    t         |           || _        || _        y r-   )super__init___variant_tensor_element_spec)r   variant_tensorr   	__class__s      r   r;   z_OptionalImpl.__init__   s    	G)D%Dr   c                     t        j                  | j                        5  t        j                  | j                  |      cd d d        S # 1 sw Y   y xY w)Nr%   )r   colocate_withr<   r
   optional_has_valuer   s     r   r   z_OptionalImpl.has_value   sD    			4//	0 00


T  s   !AAc           
         t        j                  |d| j                  g      5 }t        j                  | j                        5  t	        j
                  | j                  |t        j                  | j                        t        j                  | j                              }d d d        t        j                  | j                        cd d d        S # 1 sw Y   2xY w# 1 sw Y   y xY w)NOptionalGetValue)r   output_typesoutput_shapes)r   r&   r<   rA   r
   optional_get_valuer   get_flat_tensor_typesr=   get_flat_tensor_shapesfrom_tensor_list)r   r   r*   results       r   r   z_OptionalImpl.get_value   s     
0--.
0 D38T112 
!44  "889K9KL#::4;M;MN	

 ''(:(:FCD D
 
D Ds$    CAC!'CC	CC'c                     | j                   S r-   r=   r   s    r   r   z_OptionalImpl.element_spec   s    r   c                 ,    t         j                  |       S r-   )OptionalSpecr,   r   s    r   
_type_specz_OptionalImpl._type_spec   s    ""4((r   r-   )r.   r/   r0   r1   r;   r   r   propertyr   rP   __classcell__r?   s   @r   r   r      s@    &
D    ) )r   r   rO   z#data.experimental.OptionalStructure)v1c                   |     e Zd ZdZdgZ fdZed        Zd Zed        Z	d Z
d Zed	        Zd
 Zd Zd Z xZS )rO   a  Type specification for `tf.experimental.Optional`.

  For instance, `tf.OptionalSpec` can be used to define a tf.function that takes
  `tf.experimental.Optional` as an input argument:

  >>> @tf.function(input_signature=[tf.OptionalSpec(
  ...   tf.TensorSpec(shape=(), dtype=tf.int32, name=None))])
  ... def maybe_square(optional):
  ...   if optional.has_value():
  ...     x = optional.get_value()
  ...     return x * x
  ...   return -1
  >>> optional = tf.experimental.Optional.from_value(5)
  >>> print(maybe_square(optional))
  tf.Tensor(25, shape=(), dtype=int32)

  Attributes:
    element_spec: A (nested) structure of `TypeSpec` objects that represents the
      type specification of the optional element.
  r=   c                 0    t         |           || _        y r-   )r:   r;   r=   )r   r   r?   s     r   r;   zOptionalSpec.__init__   s    	G%Dr   c                     t         S r-   )r   r   s    r   
value_typezOptionalSpec.value_type   s    r   c                     | j                   fS r-   rM   r   s    r   
_serializezOptionalSpec._serialize   s      r   c                 L    t        j                  dt        j                        gS )Nr6   )r   
TensorSpecr   variantr   s    r   _component_specszOptionalSpec._component_specs   s    ""2v~~677r   c                     |j                   gS r-   )r<   )r   r$   s     r   _to_componentszOptionalSpec._to_components   s    !!""r   c                 4    t        |d   | j                        S )Nr   )r   r=   )r   
flat_values     r   _from_componentszOptionalSpec._from_components   s    A(:(:;;r   c                 ,    t        | j                        S r-   )rO   r   )r$   s    r   r,   zOptionalSpec.from_value   s    **++r   c                     | S r-   r6   r   s    r   _to_legacy_output_typesz$OptionalSpec._to_legacy_output_types      Kr   c                     | S r-   r6   r   s    r   _to_legacy_output_shapesz%OptionalSpec._to_legacy_output_shapes  rg   r   c                     | S r-   r6   r   s    r   _to_legacy_output_classesz&OptionalSpec._to_legacy_output_classes  rg   r   )r.   r/   r0   r1   	__slots__r;   rQ   rX   rZ   r^   r`   rc   r5   r,   rf   ri   rk   rR   rS   s   @r   rO   rO      sq    * )&  ! 8 8#< , ,r   )r1   r2   tensorflow.core.protobufr   tensorflow.python.data.utilr   tensorflow.python.frameworkr   r   r   r   r	   tensorflow.python.opsr
   tensorflow.python.saved_modelr   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   deprecated_endpointsCompositeTensorABCMetar   r   TypeSpecrO   register_codecBuiltInTypeSpecCodecTypeSpecProtoOPTIONAL_SPECr6   r   r   <module>r|      s    @ 
 / 1 8 . + 3 1 2 @ . 6 "$@A!!!">?~//3;; ~ @ B~B()H ()V (MNP99%% 9P9x &  % %///j..<<r   