
    BVh.                         d Z ddlZddlZddlmZ ddlmZ 	 ddlZd Zd Zd Z	 	 	 	 	 	 	 d
dZ	 	 	 	 	 	 	 dd	Zy# e	$ r' 	 ddl
Zn# e	$ r 	 ddlZn# e	$ r dZY nw xY wY nw xY wY Kw xY w)z)Utilities related to model visualization.    N)path_to_string)nestc                      t         y	 t         j                  j                  t        j                                y# t        t         j                  f$ r Y yw xY w)z1Returns True if PyDot and Graphviz are available.FT)pydotDotcreateOSErrorInvocationException     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/utils/vis_utils.pycheck_pydotr   (   sI    
] 
IIUYY[!
5,,	- s   1; AAc                     ddl m} ddlm} t	        | |j
                        xr  t	        | j                  |j                        S )Nr   
functionalwrappers)tensorflow.python.keras.enginer   tensorflow.python.keras.layersr   
isinstanceWrapperlayer
Functional)r   r   r   s      r   is_wrapped_modelr   5   s6    75
UH,,
- 9
U[[*"7"7
8:r   c                 t    | j                  ||      s&| j                  t        j                  ||             y y )N)get_edgeadd_edger   Edge)dotsrcdsts      r   r   r   <   s,    	c3	LLC%& 
 r   c           
      "   ddl m} ddlm}	 ddlm}
 t               s+d}dt        j                  v rt        |       yt        |      |rPt        j                  d| j                  	      }|j                  d
| j                         |j                  dd       n\t        j                         }|j                  d|       |j                  dd       |j                  d|       |j                  d       i }i }i }i }| j                   }| j"                  sFt        j$                  t'        t)        |             | j                        }|j+                  |       |S t-        | |	j.                        r0| j0                  s| j3                          t4        |	j.                  | B  }t7        |      D ]  \  }}t'        t)        |            }|j                  }|j8                  j:                  }t-        ||j<                        r|rt-        |j>                  |
j@                        rvtC        |j>                  |||||d      }|jE                         }|d   ||j>                  j                  <   |d   ||j>                  j                  <   |jG                  |       nXdjI                  ||j>                  j                        }|j>                  j8                  j:                  }djI                  ||      }|rmt-        ||
j@                        rWtC        ||||||d      }|jE                         }|d   ||j                  <   |d   ||j                  <   |jG                  |       |rdjI                  ||      }n|}|rd }|d ||jJ                        }|r~d }	  ||jL                        } tQ        |d      r ||jR                        }!n>tQ        |d      r0djU                  |jV                  D "cg c]
  }" ||"       c}"      }!nd}!|d|!d | d!}|rt-        ||
j@                        rt        j$                  ||      }|j+                  |        |D ]  }t'        t)        |            }t7        |jX                        D ]k  \  }}|j                  d"z   t'        |      z   }#|#| jZ                  v s1t]        j^                  |j`                        D ]  }$t'        t)        |$            }%|s4|jc                  |%      sJ |jc                  |      sJ te        ||%|       Nt-        |$|
j@                        stg        |$      st-        ||
j@                        s?tg        |      s4|jc                  |%      sJ |jc                  |      sJ te        ||%|       t-        ||
j@                        r*te        ||%||j                     ji                                tg        |      ste        ||%|       ||j>                  j                     ji                         }&te        |||&       Tt-        |$|
j@                        rn||$j                     ji                         }&t-        ||
j@                        r,||j                     ji                         }'te        ||&|'       te        ||&|       tg        |$      s|$j>                  j                  }(te        |||(   ji                         |        n  |S # tN        $ r d} Y ^w xY wc c}"w )#a  Convert a Keras model to dot format.

  Args:
    model: A Keras model instance.
    show_shapes: whether to display shape information.
    show_dtype: whether to display layer dtypes.
    show_layer_names: whether to display layer names.
    rankdir: `rankdir` argument passed to PyDot,
        a string specifying the format of the plot:
        'TB' creates a vertical plot;
        'LR' creates a horizontal plot.
    expand_nested: whether to expand nested models into clusters.
    dpi: Dots per inch.
    subgraph: whether to return a `pydot.Cluster` instance.

  Returns:
    A `pydot.Dot` instance representing the Keras model or
    a `pydot.Cluster` instance representing nested model if
    `subgraph=True`.

  Raises:
    ImportError: if graphviz or pydot are not available.
  r   r   )
sequentialr   )z}You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) z$for plot_model/model_to_dot to work.zIPython.core.magics.namespaceNdashed)style
graph_namelabel	labeljustlrankdirconcentrateTdpirecordshape)r'   )subgraphz{}({})z{}: {}c                     | yt        |       S )N?)str)dtypes    r   format_dtypez"model_to_dot.<locals>.format_dtype   s    =U
r   |c                 J    t        |       j                  t        d       d      S )NNone)r4   replacer.   s    r   format_shapez"model_to_dot.<locals>.format_shape   s    5z!!#d)V44r   r3   input_shapeinput_shapesz, z
|{input:|output:}|{{z}|{z}}z_ib-)5r   r   r   r#   r   r   sysmodulesprintImportErrorr   Clusternamesetr   set_node_defaultslayers_is_graph_networkNoder4   idadd_noder   
Sequentialbuiltbuildsuper	enumerate	__class____name__r   r   r   model_to_dot	get_nodesadd_subgraphformatr5   output_shapeAttributeErrorhasattrr<   joinr=   _inbound_nodes_network_nodesr   flatteninbound_layersget_noder   r   get_name))modelshow_shapes
show_dtypeshow_layer_namesr*   expand_nestedr,   r0   r   r#   r   messager   sub_n_first_nodesub_n_last_nodesub_w_first_nodesub_w_last_noderF   nodeir   layer_id
layer_name
class_namesubmodel_wrappersub_w_nodeschild_class_namesubmodel_not_wrappersub_n_nodesr'   r6   r;   outputlabelsinputlabelsishapenode_keyinbound_layerinbound_layer_idrC   output_nameinbound_layer_names)                                            r   rR   rR   A   s0   > 677	0G
 '#++5 Gn  
--h5::
>CGGGUZZ GGK
))+CGGIwGGM4 GGE3)//<<&		 	 ::c"U)nEJJ7DLLJ%../;;kkm:((%7F F# Tha2e9~H J))J%))*	:ekk&0&;&;='KK '002-8^))*,7O(())*__Z1A1AB
 ;;0099__Z1AB
E:+@+@A)





 )224k%0^uzz"$/Ooejj!	+, ooj*5ee  U[[ 9:e 5#E$6$67 
	&"5#4#455.)ii050B0BCf\&!CE 5:5@5ACe 
z$$!&ZZ.d	ll4iTn  ,!e2e9~HU112 *!4f$s1v-h	U))	)!\\$*=*=> '	!M M!23
<< 0111<<)))S*H5 })446$]3 
(=(=>&u-||$4555||H---.9eZ%:%:;.)%**5>>@B $E*.9'(8(89BBDh-M:+@+@A$]%7%78AACdE:#8#89.uzz:CCEdK0dH-!-0#0#6#6#;#; s&'9:CCE!K'	!*!,!Z 
*A   Ds   [:.\
:\	\	c           	         t        | ||||||      }t        |      }|yt        j                  j	                  |      \  }	}
|
sd}
n|
dd }
|j                  ||
       |
dk7  r	 ddlm} |j                  |	      S y# t        $ r Y yw xY w)
aD  Converts a Keras model to dot format and save to a file.

  Example:

  ```python
  input = tf.keras.Input(shape=(100,), dtype='int32', name='input')
  x = tf.keras.layers.Embedding(
      output_dim=512, input_dim=10000, input_length=100)(input)
  x = tf.keras.layers.LSTM(32)(x)
  x = tf.keras.layers.Dense(64, activation='relu')(x)
  x = tf.keras.layers.Dense(64, activation='relu')(x)
  x = tf.keras.layers.Dense(64, activation='relu')(x)
  output = tf.keras.layers.Dense(1, activation='sigmoid', name='output')(x)
  model = tf.keras.Model(inputs=[input], outputs=[output])
  dot_img_file = '/tmp/model_1.png'
  tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
  ```

  Args:
    model: A Keras model instance
    to_file: File name of the plot image.
    show_shapes: whether to display shape information.
    show_dtype: whether to display layer dtypes.
    show_layer_names: whether to display layer names.
    rankdir: `rankdir` argument passed to PyDot,
        a string specifying the format of the plot:
        'TB' creates a vertical plot;
        'LR' creates a horizontal plot.
    expand_nested: Whether to expand nested models into clusters.
    dpi: Dots per inch.

  Returns:
    A Jupyter notebook Image object if Jupyter is installed.
    This enables in-line display of the model plots in notebooks.
  )ra   rb   rc   r*   rd   r,   Npng   )rU   pdfr   )display)filename)
rR   r   ospathsplitextwriteIPythonr   ImagerA   )r`   to_filera   rb   rc   r*   rd   r,   r   _	extensionr   s               r   
plot_modelr     s    V 	'!
	# 7#'[
!!'*,!Y	I!"I))GI)& %!]]G],,   
s   &A? ?	B
B)FFTTBF`   F)z	model.pngFFTr   Fr   )__doc__r   r>   &tensorflow.python.keras.utils.io_utilsr   tensorflow.python.utilr   pydot_ngr   rA   	pydotplusr   r   r   rR   r   r   r   r   <module>r      s   " 0 	 
 A '
:' #!"&$Ph #  $"Eq  		  e			sW   < A(AA(A"AA"AA"AA"A(!A""A('A(