
    BVh\                     z    d 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 Zd	 Zd
 Zd Zd Zd Zd Zy)z/Python dataset sparse tensor utility functions.    )nest)dtypes)sparse_tensor)tensor)tensor_shape)
sparse_opsc                 L    t        d t        j                  |       D              S )zChecks for sparse tensor.

  Args:
    classes: a structure of objects that identify the dataset item classes

  Returns:
    `True` if `classes` contains a sparse tensor type and `False` otherwise.
  c              3   @   K   | ]  }|t         j                  u   y w)N)r   SparseTensor).0cs     R/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/util/sparse.py	<genexpr>zany_sparse.<locals>.<genexpr>!   s     LQ-,,,Ls   )anyr   flatten)classess    r   
any_sparser      s     
Ldll76KL	LL    c           	         t        j                  | t        t        j                  |       t        j                  |            D cg c]-  \  }}|t        j
                  u rt        j                         n|/ c}}      }|S c c}}w )a  Converts sparse tensor shapes to their physical shapes.

  Args:
    shapes: a structure of shapes to convert.
    classes: a structure of objects that identify the dataset item classes

  Returns:
    a structure matching the nested structure of `shapes`, containing
    `tensor_shape.unknown_shape()` at positions where `classes` contains
    `tf.sparse.SparseTensor` and matching contents of `shapes` otherwise
  )r   pack_sequence_aszipr   r   r   r   unknown_shape)shapesr   shaper   rets        r   as_dense_shapesr   $   sp     	f$,,v.W0EF'
% '(=+E+E&El  "5P' 	# 
*	's   2Bc           	         t        j                  | t        t        j                  |       t        j                  |            D cg c])  \  }}|t        j
                  u rt        j                  n|+ c}}      }|S c c}}w )a  Converts sparse tensor types to `dtypes.variant`.

  Args:
    types: a structure of types to convert.
    classes: a structure of objects that identify the dataset item classes

  Returns:
    a structure matching the nested structure of `types`, containing
    `dtypes.variant` at positions where `classes` contains
    `tf.sparse.SparseTensor` and matching contents of `types` otherwise
  )r   r   r   r   r   r   r   variant)typesr   tyr   r   s        r   as_dense_typesr!   7   sk     	et||E*DLL,AB&
"a ]777fnnR?& 	# 
*	&s   .A>c                    t        j                  |t        t        j                  |       t        j                  |      t        j                  |      t        j                  |            D cg c]=  \  }}}}|t        j
                  u r"t        j                  |||j                        n|? c}}}}      }|S c c}}}}w )a  Deserializes sparse tensors.

  Args:
    tensors: a structure of tensors to deserialize.
    types: a structure that holds information about types of `tensors`
    shapes: a structure that holds information about shapes of `tensors`
    classes: a structure of objects that identify the dataset item classes

  Returns:
    `tensors` with any serialized sparse tensors replaced by their deserialized
    version.
  )dtyperank)	r   r   r   r   r   r   r   deserialize_sparsendims)	tensorsr   r   r   r   r    r   r   r   s	            r   deserialize_sparse_tensorsr(   J   s     	e %(
,,w
e!4dll66J
,,w
%!& & !62ua 
m((	( ##F"5;;G.45& 	# 
*&s   0AB>c           
          t        j                  | t        j                  |       D cg c]<  }t        |t        j
                        rt        j
                  nt        j                  > c}      S c c}w )a/  Gets classes for a structure of tensors.

  Args:
    tensors: the tensor structure to get classes for.

  Returns:
    a structure matching the nested structure of `tensors`, containing
    `tf.sparse.SparseTensor` at positions where `tensors` contains a sparse
    tensor and `tf.Tensor` otherwise.
  )r   r   r   
isinstancer   r   
tensor_libTensor)r'   r   s     r   get_classesr-   a   sh     
		w LL))  
FM66	7   =G=N=NO) 
  )s   AA0
c           
          t        j                  | t        j                  |       D cg c]>  }t        j                  |      r%t        j                  |t        j                        n|@ c}      }|S c c}w )zSerializes many sparse tensors into a batch.

  Args:
    tensors: a tensor structure to serialize.

  Returns:
    `tensors` with any sparse tensors replaced by the serialized batch.
  out_type)	r   r   r   r   	is_sparser   serialize_many_sparser   r   r'   r   r   s      r   serialize_many_sparse_tensorsr4   s   sn     	g LL)(  
	 	 	( &&vG.45( 	#
 
*(s   AA4
c           
          t        j                  | t        j                  |       D cg c]C  }t        |t        j
                        r%t        j                  |t        j                        n|E c}      }|S c c}w )zSerializes sparse tensors.

  Args:
    tensors: a tensor structure to serialize.

  Returns:
    `tensors` with any sparse tensors replaced by their serialized version.
  r/   )
r   r   r   r*   r   r   r   serialize_sparser   r   r3   s      r   serialize_sparse_tensorsr7      sq     	g LL)(  
FM66	7 !!&6>>B=CD( 	#
 
*(s   AA9
N)__doc__tensorflow.python.data.utilr   tensorflow.python.frameworkr   r   r   r+   r   tensorflow.python.opsr   r   r   r!   r(   r-   r4   r7    r   r   <module>r=      s@    6 , . 5 < 4 ,	M&&.$$r   