o
    ?e\                     @   s   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d	 Zd
d Zdd Zdd Zdd Zdd Zdd ZdS )z/Python dataset sparse tensor utility functions.    )nest)dtypes)sparse_tensor)tensor)tensor_shape)
sparse_opsc                 C   s   t dd t| 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                 s   s    | ]}|t ju V  qd S )N)r   SparseTensor).0c r   c/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/data/util/sparse.py	<genexpr>!   s    zany_sparse.<locals>.<genexpr>)anyr   flatten)classesr   r   r   
any_sparse   s   	r   c              	   C   ,   t | dd tt | t |D }|S )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
  c                 S   s&   g | ]\}}|t ju rt n|qS r   )r   r   r   Zunknown_shape)r	   shaper
   r   r   r   
<listcomp>0   s    z#as_dense_shapes.<locals>.<listcomp>r   pack_sequence_aszipr   )shapesr   retr   r   r   as_dense_shapes$      r   c              	   C   r   )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
  c                 S   s$   g | ]\}}|t ju rtjn|qS r   )r   r   r   variant)r	   tyr
   r   r   r   r   C   s    z"as_dense_types.<locals>.<listcomp>r   )typesr   r   r   r   r   as_dense_types7   r   r   c                 C   s<   t |dd tt | t |t |t |D }|S )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.
  c                 S   s4   g | ]\}}}}|t ju rtj|||jd n|qS ))ZdtypeZrank)r   r   r   Zdeserialize_sparseZndims)r	   r   r   r   r
   r   r   r   r   W   s    
z.deserialize_sparse_tensors.<locals>.<listcomp>r   )tensorsr   r   r   r   r   r   r   deserialize_sparse_tensorsJ   s   r!   c                 C   s   t | dd t | D S )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.
  c                 S   s$   g | ]}t |tjrtjntjqS r   )
isinstancer   r   
tensor_libZTensorr	   r   r   r   r   r   l   s    
zget_classes.<locals>.<listcomp>r   r   r   )r    r   r   r   get_classesa   s   r&   c                 C       t | dd t | D }|S )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.
  c                 S   s*   g | ]}t |rtj|tjd n|qS )Zout_type)r   Z	is_sparser   Zserialize_many_sparser   r   r$   r   r   r   r   }   s    z1serialize_many_sparse_tensors.<locals>.<listcomp>r%   r    r   r   r   r   serialize_many_sparse_tensorss      
r*   c                 C   r'   )zSerializes sparse tensors.

  Args:
    tensors: a tensor structure to serialize.

  Returns:
    `tensors` with any sparse tensors replaced by their serialized version.
  c                 S   s,   g | ]}t |tjrtj|tjd n|qS r(   )r"   r   r   r   Zserialize_sparser   r   r$   r   r   r   r      s    
z,serialize_sparse_tensors.<locals>.<listcomp>r%   r)   r   r   r   serialize_sparse_tensors   r+   r,   N)__doc__Ztensorflow.python.data.utilr   Ztensorflow.python.frameworkr   r   r   r#   r   Ztensorflow.python.opsr   r   r   r   r!   r&   r*   r,   r   r   r   r   <module>   s   