
    2VhE                     (   d Z ddlZddlZddlmZ ddlmZ ddlmZ 	 ddl	Z	d Zd Zd Zd	 Zd
 Zd Z ed      	 	 	 	 	 	 	 	 	 dd       Z ed      	 	 	 	 	 	 	 	 	 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 nw xY w)z)Utilities related to model visualization.    N)tree)keras_export)io_utilsc                      t         duS )z#Returns True if PyDot is available.N)pydot     S/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/utils/model_visualization.pycheck_pydotr      s    r	   c                      t               sy	 t        j                  j                  t        j                                y# t        t        j
                  f$ r Y yw xY w)z6Returns True if both PyDot and Graphviz are available.FT)r   r   DotcreateOSErrorPydotExceptionr   r	   r
   check_graphvizr      sI    = 			%U))* s   1? AAc                     t        t        |            }t        t        |            }| j                  ||      s:t        j                  ||      }|j                  dd       | j                  |       y y )Npenwidth2)stridget_edger   Edgesetadd_edge)dotsrcdstsrc_iddst_idedges         r
   r   r   *   sY    C\FC\F<<'zz&&)S!T (r	   c                     t        | j                  d      r| j                  j                  }|S t        | j                  d      r| j                  j                  }|S t	        | j                        }|S )Nname__name__)hasattr
activationr"   r#   r   )layeractivation_names     r
   get_layer_activation_namer(   3   sn    u(**//
 	 
!!:	.**33  e../r	   c                    | j                   j                  }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|rt        d|       d}t	        d ||fD              }	|r|	d	z  }	t        d
|	      }
|r|d|
 d| j                   d| dz  }n|d|
 d| dz  }|r-t        | d      r!| j                  |d|
 dt        |        dz  }g }|rd }d }	 t        j                  d | j                        }t        j                  d | j                        }d }|dk7  r|j                  d ||       d       |j                  d ||       d       |r=d }	 t        j                  d | j                        }|j                  d|xs d d       |rGt        | d      r;| j                   r/| j"                  r|j                  d       n|j                  d       |rt%        |      }
nd
}
|r|d d!j'                  |      z   d"z   z  }|d#z  }|S # t        t        f$ r Y w xY w# t        t        f$ r Y w xY w)$Nshow_layer_namesshow_layer_activations
show_dtypeshow_shapesshow_trainablezInvalid kwargs: zC<<table border="0" cellborder="1" bgcolor="black" cellpadding="10">c              3   2   K   | ]  }t        |        y wN)int).0xs     r
   	<genexpr>z#make_layer_label.<locals>.<genexpr>L   s     Cc!fCs         z<tr><td colspan="z9" bgcolor="black"><font point-size="16" color="white"><b>z</b> (z)</font></td></tr>z</b></font></td></tr>r%   z!<tr><td bgcolor="white" colspan="z'"><font point-size="14">Activation: <b>c                     | j                   S r0   shaper3   s    r
   <lambda>z"make_layer_label.<locals>.<lambda>p   s
    qww r	   c                     | j                   S r0   r8   r:   s    r
   r;   z"make_layer_label.<locals>.<lambda>q   s
     r	   c           	          | ot        | t              r8dj                  | j                         D cg c]  \  }}| d|  c}}      }n|  }|j	                  dd      j	                  dd      }|S d}|S c c}}w )Nz, z: } {?)
isinstancedictjoinitemsreplace)r9   kv	shape_strs       r
   format_shapez&make_layer_label.<locals>.format_shapeu   s     eT* $		16?AA3b?!I $)'I%--c26>>sBG	   	 @s   A6

InputLayerz:<td bgcolor="white"><font point-size="14">Input shape: <b>z</b></font></td>z;<td bgcolor="white"><font point-size="14">Output shape: <b>c                     | j                   S r0   )dtyper:   s    r
   r;   z"make_layer_label.<locals>.<lambda>   s
     r	   z;<td bgcolor="white"><font point-size="14">Output dtype: <b>rA   	trainablezZ<td bgcolor="forestgreen"><font point-size="14" color="white"><b>Trainable</b></font></td>z\<td bgcolor="firebrick"><font point-size="14" color="white"><b>Non-trainable</b></font></td>z<tr>r?   z</tr>z	</table>>)	__class__r#   pop
ValueErrorsummaxr"   r$   r%   r(   r   map_structureinputoutputAttributeErrorappendweightsrN   lenrD   )r&   kwargs
class_namer*   r+   r,   r-   r.   tablecolspan_maxcolspancolsinput_shapeoutput_shaperJ   rM   s                   r
   make_layer_labelrc   =   s   ))Jzz"45#ZZ(@AL)J**]+KZZ 01N+F8455 	N 
 Cz>&BCCKq![!Gy )**VJ< 0  	
 	y )   	
 	E<((/y 97>? @  	
 D	,,->LK--.?NL	 %KK''3K'@&A B## 	$$0$>#? @	
 	&&'8%,,GE 	$$)LS> 2	
 '%5%--??KK3 KK7 d)"''$-''11	[ELK N+ 		F N+ 		s%   AI  
!I  III('I(c                     t        j                  t        t        |             t	        | fi |      }|j                  dd       |j                  dd       |j                  dd       |S )N)labelfontname	Helveticaborder0margin)r   Noder   r   rc   r   )r&   r[   nodes      r
   	make_noderm      sT    ::c"U)n,<U,Mf,MNDHHZ%HHXsHHXsKr	   zkeras.utils.model_to_dotc
                    ddl m} | j                  st        d      ddlm} ddlm} t               st        d      |rPt        j                  d| j                        }|j                  d	| j                         |j                  d
d       nnt        j                         }|j                  d|       |j                  dd       |j                  d|       |j                  dd       |j                  d       |
j                  dd      t        d      |
rt        d|
       |||||	d}
t!        | |j"                        r| j$                  }nAt!        | |j&                        st)        | fi |
}|j+                  |       |S | j,                  }t/        |      D ]l  \  }}|rHt!        ||j&                  |j"                  f      r&t1        ||||||d||		      }|j3                  |       Pt)        |fi |
}|j+                  |       n t!        | |j"                        rE|s4t5        t7        |      dz
        D ]  }t9        |||   ||dz              |S | j$                  dd }|D ]  }t/        |j:                        D ]  \  }}t!        | |j&                        r |||      | j<                  vr3t/        |j>                        D ]i  \  }}|j@                  }|jB                   |jB                  j:                  |jD                     }|jF                  }|jB                  }|}|st9        |||       mt!        ||j&                  |j"                  f      r@|jH                  |   j@                  \  }}}t!        ||j&                  |j"                  f      r@t!        ||j&                  |j"                  f      rlt!        ||j&                        r$|jJ                  |   j@                  jB                  }n|j$                  d   }t!        ||j&                  |j"                  f      rlt9        |||       l   |S )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 Functional models
            into clusters.
        dpi: Image resolution in dots per inch.
        subgraph: whether to return a `pydot.Cluster` instance.
        show_layer_activations: Display layer activations (only for layers that
            have an `activation` property).
        show_trainable: whether to display if a layer is trainable.

    Returns:
        A `pydot.Dot` instance representing the Keras model or
        a `pydot.Cluster` instance representing nested model if
        `subgraph=True`.
    r   )make_node_keyyThis model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.)
functional)
sequentialzFYou must install pydot (`pip install pydot`) for model_to_dot to work.dashed)style
graph_namere   	labeljustlrankdirconcentrateTdpisplinesorthorecordr8   layer_rangeN.Argument `layer_range` is no longer supported. Unrecognized keyword arguments: )r*   r+   r,   r-   r.   )subgraphr+   r.   r6   )&keras.src.ops.functionro   builtrQ   keras.src.modelsrq   rr   r   ImportErrorr   Clusterr"   r   r   set_node_defaultsrP   rB   
Sequentiallayers
Functionalrm   add_node_operations	enumeratemodel_to_dotadd_subgraphrangerZ   r   _inbound_nodes_nodesinput_tensors_keras_history	operation
node_indextensor_indexoutputsinputs)modelr-   r,   r*   rx   expand_nestedrz   r   r+   r.   r[   ro   rq   rr   r   r   rl   ir&   submodelinbound_indexinbound_nodeinput_indexinput_tensorinput_history
input_nodeoutput_indexsourcedestination_s                                 r
   r   r      sO   J 5;;,
 	
 ,+ =$
 	

 mm(uzzB$S!iik	7#t$s	7#H-zz-&2IJJ;F8DEE -"8 "(F %../z445)&)T
"" f% 5ZJ)):+@+@A
 $ '=-
H X& U-f-DLL). %../3v;?+ 8fQiA78J \\!"%F  M3+4U5I5I+J K	3'M< 5*"7"78!%7u||K .7**. B3)\
 !- ; ; **2 +44CC!,,
  -99 $--#$S&%0
 !**J,A,AB /5nn$/$n ,FA| !**J,A,AB$ !**J,A,AB "+z/D/DE '2&8&8''(. $ '2&8&8&;# !**J,A,AB& fk2EB3K	3M3\ Jr	   zkeras.utils.plot_modelc
                    | j                   st        d      t               s5d}dt        j                  v rt        j                  |       yt        |      t               s5d}dt        j                  v rt        j                  |       yt        |      |
j                  dd      t        d      |
rt        d|
       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)a  Converts a Keras model to dot format and save to a file.

    Example:

    ```python
    inputs = ...
    outputs = ...
    model = keras.Model(inputs=inputs, outputs=outputs)

    dot_img_file = '/tmp/model_1.png'
    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 Functional models
            into clusters.
        dpi: Image resolution in dots per inch.
        show_layer_activations: Display layer activations (only for layers that
            have an `activation` property).
        show_trainable: whether to display if a layer is trainable.

    Returns:
        A Jupyter notebook Image object if Jupyter is installed.
        This enables in-line display of the model plots in notebooks.
    rp   zFYou must install pydot (`pip install pydot`) for `plot_model` to work.zIPython.core.magics.namespaceNznYou must install graphviz (see instructions at https://graphviz.gitlab.io/download/) for `plot_model` to work.r~   r   r   )r-   r,   r*   rx   r   rz   r+   r.   pngr6   )formatpdfr   )display)filename)r   rQ   r   sysmodulesr   	print_msgr   r   rP   r   r   ospathsplitextwriteIPythonr   Image)r   to_filer-   r,   r*   rx   r   rz   r+   r.   r[   messager   r   	extensionr   s                   r
   
plot_modelr     sy   ` ;;,
 	

 =( 	 +ckk9 w'g&&( 	
 +ckk9 w'g&&zz-&2IJJ;F8DEE
)#5%
C 'lG
{77##G,LAy	abM	IIgiI( E	'=='=22	 
  		s   *E 	EE)	FFTTBF   FFF)	z	model.pngFFFr   Fr   FF)__doc__r   r   	keras.srcr   keras.src.api_exportr   keras.src.utilsr   r   r   pydot_ng	pydotplusr   r   r   r(   rc   rm   r   r   r   r	   r
   <module>r      s    / 	 
  - $

zz ()  O *Od &'  r (rU  	  	% 	E		sY   A% %B+A0/B0B6A;:B;BBBBB
BBB