
    hMh{Q                         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 dlZddlmZmZ d dlZ	 	 	 ddZ	 	 ddZ	 	 ddZ	 	 ddZdS )    N)gridspec)	hierarchy   )nullity_filternullity_sort   
      r         ?r   r      -   Tc           	         t          | |||          } t          | |d          } | j        d         }| j        d         }|                                 j        }t          j        ||dft
          j                  }g d||d	k     <   |||d	k    <   |t          j	        |           |rHt          j        dd|          }|                    d           t          j        |d                   }nt          j        dd          }t          j        |d                   }n|durt          j        d           d}|}|                    |d           |                    d           |                    d           |j                                         |j                            d           |j                            d           |j        d                             d           |j        d                             d           |j        d                             d           |j        d                             d           |	s|	vt3          | j                  dk    r^d}|                    t9          t;          d|                               |                    t9          | j                  |
||           n|                    g            |r+g }t?          | j                   tB          j"        k    rtC          j#        | j         $                                j%        d         | j         $                                j%        d         |          j        }tC          j#        | j         $                                j%        d         | j         $                                j%        d         |          &                    d           }nt?          | j                   tB          j'        k    rttC          j#        | j         d         | j         d         |          j        }tC          j#        | j         d         | j         d         |          &                    d           }ntQ          d           	 |D ]/}|)                    | j         *                    |                     0n# tP          $ r tQ          d!          w xY w|+                    |           |,                    |t[          |d"z  d#z            d$           n]|+                    d| j        d         dz
  g           |,                    d| j        d         gt[          |d"z  d#z            d$           d% t;          d|dz
            D             }|D ]}|.                    |d&d'(           |r|                                 /                    t`                    1                    d)          }t9          t;          d|                    }t9          te          |j                            }tg          |          }ti          |          }|                     |          } |                     |          }!|                    d           |                    d           t[          tj          j6        d                   dk    r|7                    d           n|8                    d           |j        d                             d           |j        d                             d           |j        d                             d           |j        d                             d           |9                    d           |:                    |||*           |	rd+}"tw          | j        d                   <                                r|"=                                }"tw          | j        d                   >                                r|"?                                }"d}|                    |||z
  dz  z   g           |                    |"g|
||           |j                                         |+                    g            n*|                    g            |+                    g            |@                    |||!f|dz   |!ft[          |d"z  d,z            d-d.           |@                    ||| f|dz
  | ft[          |d"z  d,z            d-d.           |A                    |dz
  |dz   g           |:                    |g| gd/|d01           |:                    |g|!gd/|d01           |j                            d           |S )2a  
    A matrix visualization of the nullity of the given DataFrame.

    :param df: The `DataFrame` being mapped.
    :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default).
    :param n: The max number of columns to include in the filtered DataFrame.
    :param p: The max percentage fill of the columns in the filtered DataFrame.
    :param sort: The row sort order to apply. Can be "ascending", "descending", or None.
    :param figsize: The size of the figure to display.
    :param fontsize: The figure's font size. Default to 16.
    :param labels: Whether or not to display the column names. Defaults to the underlying data labels when there are
        50 columns or less, and no labels when there are more than 50 columns.
    :param label_rotation: What angle to rotate the text labels to. Defaults to 45 degrees.
    :param sparkline: Whether or not to display the sparkline. Defaults to True.
    :param width_ratios: The ratio of the width of the matrix to the width of the sparkline. Defaults to `(15, 1)`.
        Does nothing if `sparkline=False`.
    :param color: The color of the filled columns. Default is `(0.25, 0.25, 0.25)`.
    :return: The plot axis.
    filternpcolumnssortaxisr   r      )dtype)r   r   r         ?Nfigsize   )width_ratiosg{Gz?)wspaceFzqPlotting a sparkline on an existing axis is not currently supported. To remove this warning, set sparkline=False.none)interpolationautovisibletoprightbottomleft2   rotationhafontsize)freqc                 ,    |                      d          S Nz%Y-%m-%dstrftimets    S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/missingno/missingno.py<lambda>zmatrix.<locals>.<lambda>f       45JJz4J4J     c                 ,    |                      d          S r3   r4   r6   s    r8   r9   zmatrix.<locals>.<lambda>n   r:   r;   z5Dataframe index must be PeriodIndex or DatetimeIndex.z3Could not divide time index into desired frequency.r      )r/   r-   c                     g | ]}|d z   S )r    ).0xs     r8   
<listcomp>zmatrix.<locals>.<listcomp>   s    ===AC===r;   -white)	linestylecolorr   )rF   zData Completeness   center)xyxytextr/   var.   .g      $@)rF   
markersize)Br   r   shapenotnullvaluesnpzerosfloat32pltfigurer   GridSpecupdatesubplotwarningswarnimshow
set_aspectgridxaxistick_topset_ticks_positionyaxisspinesset_visiblelenr   
set_xtickslistrangeset_xticklabelstypeindexpdPeriodIndex
date_rangeto_timestampdatemapDatetimeIndexKeyErrorappendget_loc
set_yticksset_yticklabelsintaxvlineastypeboolsumreversedminmaxmpl__version__set_axis_bgcolorset_facecolorset_ymarginplotstrislowerlowerisupperupperannotateset_xlim)#dfr   r   r   r   r   r    rF   r/   labelslabel_rotation	sparkliner1   axheightwidthzggsax1ax0r.   ts_listts_arrayts_ticksvaluein_between_pointcompleteness_srsx_domainy_rangemin_completenessmax_completenessmin_completeness_indexmax_completeness_indexlabels#                                      r8   matrixr      s|	   0 
6Q!	4	4	4B	bt)	4	4	4BXa[FHQKE 	

A
&%#2:666AAa#gJAa#gJ 
z
7#### 	)"1alCCCBIITI"""+be$$CC"1a((Bk"Q%  E!!M?   I JJqJ''' NN6HHUHII  (((I  (((Ju!!%(((Jw##E***Jx$$U+++Jv""5)))  &.S__%:%:tE!UOO,,---D,,~"W_````r   \>>R^++}RX%:%:%<%<%A!%D%'X%:%:%<%<%A"%E*.0 0 006  }RX%:%:%<%<%A!%D%'X%:%:%<%<%A"%E*.0 0 003 5K 5K 1L 1L H
 "(^^r///}RXa["(2,*.0 0 006  }RXa["(2,*.0 0 003 5K 5K 1L 1L H RSSS	R! 8 8rx//6677778 	R 	R 	RPQQQ	R 	wHs8b=23E/F/FQRSSSS28A;?+,,,Q,s8b=2;M7N7NYZ[[[ >=q%!))<)<===, D D$7CCCC @-::<<..t4488a8@@a(())x 0 78899w<<w<<!(/?!@!@!(/?!@!@ 	vsq!""a''  ++++i(((
5%%e,,,
7''...
8((///
6&&u--- 	(%000 	'E2:a=!!))++ &2:a=!!))++ & BNN,0@CS0SWX/XXYZZZ.RRZ[[[I   NN2NN2NN2 	%)+AB-13IJ!(R-""455  	 	  	  	  	%)+AB-13IJ!(R-""455  	 	! 	! 	! 	&*,<q,@ABBB"#&<%=s%\`aaa"#&<%=s%\`aaa 		$$V,,,Js   82R+ +SFdimgrayc                     t           |||	           t           |
d           t                                                                                      z
  }|t           j                  dk    rd}nd}|\t          j                    }|Ft           j                  dk    s|dk    s|dk    rd	}n$d
d
t           j                  z   dz
  dz  f}n|}d}|||||d}|dk    r# |t                     z  j        j	        di | n" |t                     z  j        j
        di | |g}|s|t           j                  dk    r|                    |                                |d|           |                                }|                    |           |st|                    ddg           |                    |                                           |                     fd|                                D             |           n<|                    d           |                    |                                           |                     fd|                                D             |           |                                }|                    |           |                    |                                           |                    |                                           |                    |j        ||d           nt|                                }|                    |           |s|                    ddg           |                    |                                           |                     fd|                                D             |           |                    |                                           |                    |j        |d           n|                    d           |                    |                                           |                    |                                           |                     fd|                                D             |           |                    |                                           |                    |j        |d           |                                }|                    |           |                    |                                           |r<|                    d           |                    |                                           |                    |                                           |                    d           |D ]}|j        d                             d           |j        d                             d           |j        d                             d           |j        d                             d           |j                             d           |j!                             d           |S )a  
    A bar chart visualization of the nullity of the given DataFrame.

    :param df: The input DataFrame.
    :param log: Whether or not to display a logarithmic plot. Defaults to False (linear).
    :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default).
    :param n: The cap on the number of columns to include in the filtered DataFrame.
    :param p: The cap on the percentage fill of the columns in the filtered DataFrame.
    :param sort: The column sort order to apply. Can be "ascending", "descending", or None.
    :param figsize: The size of the figure to display.
    :param fontsize: The figure's font size. This default to 16.
    :param labels: Whether or not to display the column names. Would need to be turned off on particularly large
        displays. Defaults to True.
    :param label_rotation: What angle to rotate the text labels to. Defaults to 45 degrees.
    :param color: The color of the filled columns. Default to the RGB multiple `(0.25, 0.25, 0.25)`.
    :param orientation: The way the bar plot is oriented. Defaults to vertical if there are less than or equal to 50
        columns and horizontal if there are more.
    :return: The plot axis.
    r   rowsr   Nr+   r*   r)   r'   r   r	   r   )r   r/   logrF   r   r(   r,   r   r   c                 N    g | ]!}t          |t                    z            "S r?   rx   re   r@   r   r   s     r8   rB   zbar.<locals>.<listcomp>  +     L L LaQR[!1!1 L L Lr;   )r/   r   c                 N    g | ]!}t          |t                    z            "S r?   r   r   s     r8   rB   zbar.<locals>.<listcomp>  s+    HHH!SSWW--HHHr;   )r/   r-   r.   c                 N    g | ]!}t          |t                    z            "S r?   r   r   s     r8   rB   zbar.<locals>.<listcomp>$  r   r;   )r/   r.   c                 N    g | ]!}t          |t                    z            "S r?   r   r   s     r8   rB   zbar.<locals>.<listcomp>2  r   r;   Fr"   r?   )"r   r   re   isnullr|   r   rU   gcar   barbarhri   get_xticklabelstwinxrt   set_ylimrv   
get_yticksrw   
set_yscaleget_ylimtwinyrf   
get_xticksr   get_xlimrQ   
set_xscaler^   rc   rd   r_   ra   rb   )r   r   r/   r   r   r   rF   r   r   r   r   r   orientationnullity_countsr   	plot_argsaxesax2ax3s   `                  r8   r   r      s   . 
6Q!	4	4	4B	bt&	1	1	1BWWryy{{000Nrz??R KK"K	zgii?2:"$$u(<(<x@W@W"S__ 4r 9S@A##PU]`aaIh+#b''	!'+88i8888,#b''	!',99y9995D  C%&.S__%:%:!!NwQY 	 	
 	
 	

 iikkC 		)LL!Q   NN3>>++,,, L L L L3>>;K;K L L LW_````
 NN5!!!LL(((HHHHs~~7G7GHHHS[\\\ iikkCs~~''(((S\\^^$$$!H~RX 	 	
 	
 	
 	

 iikkC 	ULL!Q    NN3>>++,,, L L L L3>>;K;K L L LW_``` NN3>>++,,, 5VTTTT
 NN5!!!LL((( NN3>>++,,, L L L L3>>;K;K L L LW_``` NN3>>++,,, 5VTTT iikkCs~~''((( 	)NN5!!!LL(((S\\^^$$$HHUOOO , ,
	%$$U+++
	'&&u---
	(''...
	&%%e,,,
##F+++
##F++++Jr;   r=      RdBur0   c                    t          | |||          } t          | |d          } |)t          j        |           t          j                    }n|}| j        ddd t          t          j        | 	                                d                    D             f         } | 	                                
                                }t          j        |          }d|t          j        |          <   |r#t          j        |||	||dd	|d
z
  i|
|	  	         nt          j        |||	|||
|           |j                                         |                    |j                                        |d|           |                    |j                                        d|           |j                            d           |j                            d           |j                            d           |j        D ]}t3          |                                          }d|cxk    rdk     rn n|                    d           Id|cxk     rdk    rn n|                    d           o|dk    r|                    d           |dk    r|                    d           d|cxk     rdk     rn n|                    d           |                    t9          |d                     |S )a  
    Presents a `seaborn` heatmap visualization of nullity correlation in the given DataFrame.

    Note that this visualization has no special support for large datasets. For those, try the dendrogram instead.

    :param df: The DataFrame whose completeness is being heatmapped.
    :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default). See
        `nullity_filter()` for more information.
    :param n: The cap on the number of columns to include in the filtered DataFrame. See  `nullity_filter()` for
        more information.
    :param p: The cap on the percentage fill of the columns in the filtered DataFrame. See  `nullity_filter()` for
        more information.
    :param sort: The column sort order to apply. Can be "ascending", "descending", or None.
    :param figsize: The size of the figure to display. This is a `matplotlib` parameter which defaults to (20, 12).
    :param fontsize: The figure's font size.
    :param labels: Whether or not to label each matrix entry with its correlation (default is True).
    :param label_rotation: What angle to rotate the text labels to. Defaults to 45 degrees.
    :param cmap: What `matplotlib` colormap to use. Defaults to `RdBu`.
    :param vmin: The normalized colormap threshold. Defaults to -1, e.g. the bottom of the color scale.
    :param vmax: The normalized colormap threshold. Defaults to 1, e.g. the bottom of the color scale.
    :return: The plot axis.
    r   r   r   Nr   c                 $    g | ]\  }}|d k    |S )r   r?   )r@   ir   s      r8   rB   zheatmap.<locals>.<listcomp>t  s(    XXX41aRSVWRWRWQRWRWRWr;   rG   Tsizer   )maskcmapr   cbarannot	annot_kwsvminvmax)r   r   r   r   r   r   r(   r,   r   )r-   r/   r"   Fgffffff?r   z<1r0   gffffffz>-11z-1gg? )r   r   rU   rV   r   iloc	enumeraterR   varr   corr
zeros_liketriu_indices_fromsnsheatmapr_   tick_bottomri   get_majorticklabelsrw   rb   ra   patchrd   textsfloatget_textset_textround)r   r   r   r   r   r   r/   r   r   r   r   r   r   r   r   corr_matr   textr7   s                      r8   r   r   O  s   6 
6Q!	4	4	4B	bt&	1	1	1B	z
7####gii 
XX9RVBIIKKf-M-M-M#N#NXXXX	YB yy{{!!H=""D'+D	d	#	#$ *H4ds68a<*@D	* 	* 	* 	* 	* 	H4dsD	* 	* 	* 	* I	%%''.WW_     	5577!hWWWI  (((I  (((I%   	 ' '$--//""1====q=====MM$!____u_____MM%    !VVMM#"WWMM$QMM"MM%1++&&&&Jr;   averagec
           	      2   |sFt          | j                  dk    s|dk    s|dk    rd}nddt          | j                  z   dz
  dz  f}|	)t          j        |           t          j                    }
n|	}
t          | |||	          } t          j        |                                 	                    t                    j                  }t          j        ||          }|st          | j                  dk    rd
}nd}t          j        ||| j                                        dd ||
           |
                    d           |
                    d           |dk    r|
j                                         |
j                            d           |
j                            d           |
j        d                             d           |
j        d                             d           |
j        d                             d           |
j        d
                             d           |
j                            d           |dk    r0|
                    |
j                                        |d
           n5|dk    r/|
                    |
j                                        |d           |dk    s|dk    r+|
                    dt          |dz  dz                       n*|
                    dt          |dz  dz                       |
S )a)  
    Fits a `scipy` hierarchical clustering algorithm to the given DataFrame's variables and visualizes the results as
    a `scipy` dendrogram.

    The default vertical display will fit up to 50 columns. If more than 50 columns are specified and orientation is
    left unspecified the dendrogram will automatically swap to a horizontal display to fit the additional variables.

    :param df: The DataFrame whose completeness is being dendrogrammed.
    :param method: The distance measure being used for clustering. This is a parameter that is passed to
        `scipy.hierarchy`.
    :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default).
    :param n: The cap on the number of columns to include in the filtered DataFrame.
    :param p: The cap on the percentage fill of the columns in the filtered DataFrame.
    :param figsize: The size of the figure to display. This is a `matplotlib` parameter which defaults to `(25, 10)`.
    :param fontsize: The figure's font size.
    :param orientation: The way the dendrogram is oriented. Defaults to top-down if there are less than or equal to 50
        columns and left-right if there are more.
    :param label_rotation: What angle to rotate the text labels to. Defaults to 45 degrees.
    :return: The plot axis.
    r+   r'   r)   r   r	   r   Nr   r   r*   
descendingc                     dS )Nblackr?   )cs    r8   r9   zdendrogram.<locals>.<lambda>  s    ' r;   )r   r   distance_sortlink_color_funcleaf_font_sizer   r$   Fr%   r"   r(   )r-   r.   yr   r=   )r   	labelsizerA   )re   r   rU   rV   r   r   rR   	transposer   rz   rx   rQ   r   linkage
dendrogramtolistr]   r^   r_   r`   ra   rb   rc   rd   r   ri   r   tick_params)r   methodr   r   r   r   r   r/   r   r   r   rA   r   s                r8   r   r     s   0  >rz??b  K5$8$8K8<S<SGGBRZ025<=G	z
7####gii	6Q!	4	4	4B 	RYY[['',,344A!V$$A #rz??R KK"K	z  """))    NN6HHUHh	I  (((I  (((Ju!!%(((Jw##E***Jx$$U+++Jv""5)))I%    hCI99;;nY_````			CI99;;nY`aaah+"6"6SC20B,C,CDDDDSC20B,C,CDDDJr;   )Nr   r   Nr   r   r   r   Nr   TNN)Nr   Nr   Fr   Nr   r   NNN)Nr   r   Nr   r   Tr   r   r0   r   TN)	r   Nr   r   NNr   r   N)numpyrR   
matplotlibr   r   matplotlib.pyplotpyplotrU   scipy.clusterr   seabornr   pandasrl   utilsr   r   rZ   r   r   r   r   r?   r;   r8   <module>r     s&                       # # # # # #         / / / / / / / /  JQUYy y y yz U^;?D D D DP QUCGM M M Mb []Q Q Q Q Q Qr;   