B
    ٻd                 @   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G dd dejZG dd dejZG dd dejZG dd dejZdS )z(Experimental API for testing of tf.data.    )text_format)attr_value_pb2)dataset_ops)dtypes)ops)gen_experimental_dataset_opsc                s    fdd}|S )a  A transformation that asserts which transformations happen next.

  Transformations should be referred to by their base name, not including
  version suffix. For example, use "Batch" instead of "BatchV2". "Batch" will
  match any of "Batch", "BatchV1", "BatchV2", etc.

  Args:
    transformations: A `tf.string` vector `tf.Tensor` identifying the
      transformations that are expected to happen next.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c                s
   t |  S )zEFunction from `Dataset` to `Dataset` that applies the transformation.)_AssertNextDataset)dataset)transformations a/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/data/experimental/ops/testing.py	_apply_fn(   s    zassert_next.<locals>._apply_fnr   )r
   r   r   )r
   r   assert_next   s    r   c                s    fdd}|S )aR  Asserts which transformations, with which attributes, happened previously.

    Each transformation is repesented as a tuple in the input.

    The first element is the base op name of the transformation, not including
    version suffix.  For example, use "BatchDataset" instead of
    "BatchDatasetV2".  "BatchDataset" will match any of "BatchDataset",
    "BatchDatasetV1", "BatchDatasetV2", etc.

    The second element is a dict of attribute name-value pairs.  Attributes
    values must be of type bool, int, or string.

    Example usage:

    >>> dataset_ops.Dataset.from_tensors(0) \
    ... .map(lambda x: x) \
    ... .batch(1, deterministic=True, num_parallel_calls=8) \
    ... .assert_prev([("ParallelBatchDataset", {"deterministic": True}), \
    ...               ("MapDataset", {})])

  Args:
    transformations: A list of tuples identifying the (required) transformation
      name, with (optional) attribute name-value pairs, that are expected to
      have happened previously.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c                s
   t |  S )zEFunction from `Dataset` to `Dataset` that applies the transformation.)_AssertPrevDataset)r	   )r
   r   r   r   N   s    zassert_prev.<locals>._apply_fnr   )r
   r   r   )r
   r   assert_prev/   s    r   c              C   s   dd } | S )zA non-serializable identity transformation.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c             S   s   t | S )zEFunction from `Dataset` to `Dataset` that applies the transformation.)_NonSerializableDataset)r	   r   r   r   r   ]   s    z#non_serializable.<locals>._apply_fnr   )r   r   r   r   non_serializableU   s    r   c                s    fdd}|S )a  Sleeps for `sleep_microseconds` before producing each input element.

  Args:
    sleep_microseconds: The number of microseconds to sleep before producing an
      input element.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c                s
   t |  S )N)_SleepDataset)r	   )sleep_microsecondsr   r   r   p   s    zsleep.<locals>._apply_fnr   )r   r   r   )r   r   sleepd   s    r   c                   s    e Zd ZdZ fddZ  ZS )r   z;A `Dataset` that asserts which transformations happen next.c                sX   || _ |dkrtdtj|tjdd| _tj| j j	| jf| j
}tt| || dS )z See `assert_next()` for details.NzAInvalid `transformations`. `transformations` should not be empty.r
   )dtypename)_input_dataset
ValueErrorr   convert_to_tensorr   string_transformationsr   Z experimental_assert_next_dataset_variant_tensor_flat_structuresuperr   __init__)selfinput_datasetr
   variant_tensor)	__class__r   r   r    y   s    z_AssertNextDataset.__init__)__name__
__module____qualname____doc__r    __classcell__r   r   )r$   r   r   v   s   r   c                   s    e Zd ZdZ fddZ  ZS )r   zCA `Dataset` that asserts which transformations happened previously.c                sn   || _ |dkrtddd  tj fdd|D tjdd| _tj| j j	| jf| j
}tt| || dS )	z See `assert_prev()` for details.Nz!`transformations` cannot be emptyc             S   s   t j| d}|d kst|tr$t }xt| D ]h\}}t|trN||j| _q.t|t	rf||j| _
q.t|tr| |j| _q.tdt| dq.W t|S )N)r   zattribute value type (z) must be bool, int, or str)r   ZNameAttrList
isinstancesetdictitemsboolattrbintistrencodesr   typer   ZMessageToString)Zop_name
attributesprotor   valuer   r   r   serialize_transformation   s    


z=_AssertPrevDataset.__init__.<locals>.serialize_transformationc                s   g | ]} | qS r   r   ).0x)r:   r   r   
<listcomp>   s    z/_AssertPrevDataset.__init__.<locals>.<listcomp>r
   )r   r   )r   r   r   r   r   r   r   r   Zassert_prev_datasetr   r   r   r   r    )r!   r"   r
   r#   )r$   )r:   r   r       s    
z_AssertPrevDataset.__init__)r%   r&   r'   r(   r    r)   r   r   )r$   r   r      s   r   c                   s    e Zd ZdZ fddZ  ZS )r   zCA `Dataset` that performs non-serializable identity transformation.c                s0   || _ tj| j jf| j}tt| || dS )z%See `non_serializable()` for details.N)r   r   Z%experimental_non_serializable_datasetr   r   r   r   r    )r!   r"   r#   )r$   r   r   r       s
    z _NonSerializableDataset.__init__)r%   r&   r'   r(   r    r)   r   r   )r$   r   r      s   r   c                   s    e Zd ZdZ fddZ  ZS )r   z?A `Dataset` that sleeps before producing each upstream element.c                s:   || _ || _tj| j j| jf| j}tt| || d S )N)	r   Z_sleep_microsecondsr   Zsleep_datasetr   r   r   r   r    )r!   r"   r   r#   )r$   r   r   r       s    z_SleepDataset.__init__)r%   r&   r'   r(   r    r)   r   r   )r$   r   r      s   r   N)r(   Zgoogle.protobufr   Ztensorflow.core.frameworkr   Ztensorflow.python.data.opsr   Ztensorflow.python.frameworkr   r   Ztensorflow.python.opsr   r   r   r   r   ZUnaryUnchangedStructureDatasetr   r   r   r   r   r   r   r   <module>   s   &%