o
    i e                     @   sD   d 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S )z2Extract parse_example op configuration to a proto.    ) example_parser_configuration_pb2)tensor_shape)tensor_utilc                 C   s8   | j dkr
t| |S | j dkrt| |S td| j  )a(  Returns an ExampleParserConfig proto.

  Args:
    parse_example_op: A ParseExample or ParseExampleV2 `Operation`
    sess: A tf.compat.v1.Session needed to obtain some configuration values.
  Returns:
    A ExampleParserConfig proto.

  Raises:
    ValueError: If attributes are inconsistent.
  ParseExampleParseExampleV2z[Found unexpected type when parsing example. Expected `ParseExample` object. Received type: )type_extract_from_parse_example_extract_from_parse_example_v2
ValueError)parse_example_opsess r   b/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/util/example_parser_configuration.py$extract_example_parser_configuration   s   



r   c                 C   s"  t  }| d}| d}|| }| d}| d}| d}t||kr1tdt||f t||krAtdt||f t||krQtdt||f | jd	d
 }	t|	|| krltdt|	|| f ||	}
t|
t|	krtdt|
t|	f d}|| }|| }d}|}|| }|| }t|D ]9}|
||  }|j| }|j	}|j
t|
||   |jt||   || j|_| j||  j|_qt|D ]1}|
||  }|j| }|j}|| j|_| j||  j|_| j||  j|_| j||  j|_q|S )z1Extract ExampleParserConfig from ParseExample op.NsparseNdensesparse_typesTdensedense_shapeszGlen(sparse_types) attribute does not match Nsparse attribute (%d vs %d)zElen(dense_types) attribute does not match Ndense attribute (%d vs %d)zFlen(dense_shapes) attribute does not match Ndense attribute (%d vs %d)   NzDlen(fetch_list) does not match total features + num_dense (%d vs %d)z6len(fetched) does not match len(fetch_list) (%d vs %d)r   )r   ExampleParserConfigurationget_attrlenr
   inputsrunrangefeature_mapfixed_len_featuredefault_valueCopyFromr   make_tensor_protoshaper   TensorShapeas_protoas_datatype_enumdtypeoutputsnamevalues_output_tensor_namevar_len_featureindices_output_tensor_nameshapes_output_tensor_name)r   r   config
num_sparse	num_densetotal_featuresr   dense_typesr   
fetch_listfetchedsparse_keys_startdense_keys_startdense_def_startsparse_indices_startsparse_values_startsparse_shapes_startdense_values_startikeyfeature_configfixed_configr)   r   r   r   r   ,   s   










r   c                 C   s  t  }| d}| d}| d}| d}| d}| d}t|}	t|}
t|t|ks4J t| jd|	 ks?J || jdd	 }|d
  }|d  }|d  }|dd	 }t||ksiJ t||	ksqJ t||
ksyJ d
}|}|| }|| }||	 }||
 }t|	D ]5}|| }|j| }|j	}|j
t||  |jt||   || j|_| j||  j|_qt|D ]/}|| }|j| }|j}|| j|_| j||  j|_| j||  j|_| j||  j|_q|
d
kr~~td|S )z3Extract ExampleParserConfig from ParseExampleV2 op.r   r-   r   ragged_value_typesragged_split_typesr      r   Nr         zKRagged features are not yet supported by example_parser_configuration.proto)r   r   r   r   r   r   tolistr   r   r   r   r   r   r    r!   r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r
   )r   r   r,   r0   r-   r   r>   r?   r   r.   
num_raggedr2   sparse_keys
dense_keysragged_keysdense_defaultsr6   r7   r8   r9   ragged_values_startragged_row_splits_startr:   r;   r<   r=   r)   r   r   r   r	      s   








r	   N)	__doc__tensorflow.core.exampler   tensorflow.python.frameworkr   r   r   r   r	   r   r   r   r   <module>   s   Y