
    6Dh6X                     (   S SK Jr  S SKJr  S SKJr  S SKJr  \R                  " S5      r
\R                  " S5      rS SKJr  SS	S
/ SQ4S jrSS jrS rS rS rS rSS jrS rS S jrS rS rS r       S!S jr/ SQSSSSSSSSSS4S jrg)"    N)
graph_objs)
exceptions)optional_importsnumpyzscipy.interpolate)measurezTernary contour ploti&  i  )abcc                     [        U UU[        S[        [        US   S9SSSS9[        [        US   S9SSSS9[        [        US   S9SSSS9S9S	S
9$ )a)  
Layout of ternary contour plot, to be passed to ``go.FigureWidget``
object.

Parameters
==========
title : str or None
    Title of ternary plot
width : int
    Figure width.
height : int
    Figure height.
pole_labels : str, default ['a', 'b', 'c']
    Names of the three poles of the triangle.
   r   )text{Gz?   outside)titlemin	linewidthticks)sumaaxisbaxiscaxisF)r   widthheightternary
showlegend)dict)r   r   r   pole_labelss       X/var/www/html/env/lib/python3.13/site-packages/plotly/figure_factory/_ternary_contour.py_ternary_layoutr       s|    $ A/TQi A/TQi A/TQi
 !     c                     U S:H  n[         R                  USS9nSX-  -
  n[         R                  U5      S:  a  [        S5      e[         R                  X!X@-  5      n U $ )a  
Replaces zero ternary coordinates with delta and normalize the new
triplets (a, b, c).

Parameters
----------

ternary_data : ndarray of shape (N, 3)

delta : float
    Small float to regularize logarithm.

Notes
-----
Implements a method
by J. A. Martin-Fernandez,  C. Barcelo-Vidal, V. Pawlowsky-Glahn,
Dealing with zeros and missing values in compositional data sets
using nonparametric imputation, Mathematical Geology 35 (2003),
pp 253-278.
r   axisr   zMThe provided value of delta led to negativeternary coords.Set a smaller delta)npany
ValueErrorwhere)ternary_datadelta	zero_maskis_any_coord_zerounity_complements        r   _replace_zero_coordsr.   7   so    * !Iyq1544	vv!#1
 	
 88I.>.MNLr!   c                 D   [         R                  U 5      n [         R                  U S   U S   -  5      [         R                  S5      -  nS[         R                  S5      -  [         R                  U S   U S   -  U S   S-  -  5      -  n[         R	                  X45      nU$ )a  
Perform Isometric Log-Ratio on barycentric (compositional) data.

Parameters
----------
barycentric: ndarray of shape (3, N)
    Barycentric coordinates.

References
----------
"An algebraic method to compute isometric logratio transformation and
back transformation of compositional data", Jarauta-Bragulat, E.,
Buenestado, P.; Hervada-Sala, C., in Proc. of the Annual Conf. of the
Intl Assoc for Math Geology, 2003, pp 31-30.
r   r   r         ?   )r%   asarraylogsqrtstack)barycentricx_0x_1	ilr_tdatas       r   _ilr_transformr:   Y   s      **[)K
&&Q+a.0
1BGGAJ
>Cbggaj266+a.;q>"AKPQNVWDW"WXX  #$Ir!   c           
      8   [         R                  U 5      n [         R                  / SQ/ SQ/ SQ/5      n[         R                  S5      S-  n[         R                  S5      n[         R                  S[         R                  X#/U* U//5      U 5      n[         R	                  S[         R                  U5      R                  SS	9-   5      * n[         R                  SU[         R                  SU-  U S   -  X0S   -  U45      5      n[         R                  U5      nU$ )
a  
Perform inverse Isometric Log-Ratio (ILR) transform to retrieve
barycentric (compositional) data.

Parameters
----------
x : array of shape (2, N)
    Coordinates in ILR space.

References
----------
"An algebraic method to compute isometric logratio transformation and
back transformation of compositional data", Jarauta-Bragulat, E.,
Buenestado, P.; Hervada-Sala, C., in Proc. of the Annual Conf. of the
Intl Assoc for Math Geology, 2003, pp 31-30.
)      ?r   r0   )g      r   r0   )        r=   r0   r   g      ?ik, kj -> ijr   r   r#   )r%   arrayr4   einsumr3   expr   r5   )xmatrixstSkZlog_barycentric
iilr_tdatas           r   _ilr_inverserJ   r   s    " 	AXX}noFGF

QA
A	>288aVqb!W,=#>	BB	BFF2JNNN**	++Aii!a%!A$,aD!)D EO (Jr!   c                     [         R                  S[         R                  S5      S-  /SS/SS//5      n [         R                  U SS2S4   U SS2S4   [         R                  S5      /5      nU[         R                  R                  U5      4$ )z]
Returns the transformation matrix from barycentric to Cartesian
coordinates and conversely.
r<      r   r   r   N)r%   r?   r4   oneslinalginv)	tri_vertsMs     r    _transform_barycentric_cartesianrR      sw     3
Q/!Q!Q@AI
)AqD/9QT?BGGAJ?@AbiimmAr!   c                    [        U [        [        R                  45      (       d  [	        S5      e[        R                  U 5      n U R                  S   S;  a  [	        S5      e[        U 5      S:X  a[  [        R                  U R                  SS9SSS	9(       d4  [        R                  U R                  SS9S
SS	9(       d  Sn[	        U5      e[        U 5      S:X  a  U u  p#SX#-   -
  nOX R                  SS9-  u  p#n[        R                  [        R                  X#U45      S:  5      (       a  [	        S5      e[        R                  X#U45      $ )zI
Check ternary coordinates and return the right barycentric coordinates.
zOData  should be either an array of shape (n,m),or a list of n m-lists, m=2 or 3r   )r   rL   zCA point should have  2 (a, b) or 3 (a, b, c)barycentric coordinatesrL   r#   r   r   )rtold   z=The sum of coordinates should be 1 or 100 for all data pointsr   z+Barycentric coordinates should be positive.)
isinstancelistr%   ndarrayr'   r2   shapelenallcloser   r&   r5   )b_coordsmsgABCs        r   _prepare_barycentric_coordra      s)    hrzz 233/
 	
 zz(#H~~a&T
 	
 
X!	HLLaL0!$?HLLaL0#DAMo
8}K\\q\11a	vvbhhay!A%&&FGG88Q1Ir!   ilrc           
         US:X  a#  [        5       u  p4[        R                  SX05      nO(US:X  a  [        U 5      n [	        U 5      nO[        S5      eUSS u  pgUR                  5       UR                  5       pUR                  5       UR                  5       p[        S[        [        R                  [        U5      5      5      5      n[        R                  XU5      n[        R                  XU5      n[        R                  X5      u  nn[        R                  USS R                  XU4SS	9n[        R                  USS R                  XU4S
S	9nUX4$ )aw  
Transform data points with Cartesian or ILR mapping, then Compute
interpolation on a regular grid.

Parameters
==========

coordinates : array-like
    Barycentric coordinates of data points.
values : 1-d array-like
    Data points, field to be represented as contours.
interp_mode : 'ilr' (default) or 'cartesian'
    Defines how data are interpolated to compute contours.
	cartesianr>   rb   z&interp_mode should be cartesian or ilrNr      cubic)methodnearest)rR   r%   r@   r.   r:   r'   r   maxintr4   rZ   linspacemeshgridscipy_interpgriddataT)coordinatesvaluesinterp_moderQ   invMcoord_pointsxxyyx_minx_maxy_miny_maxn_interpgr_xgr_ygrid_xgrid_ygrid_zgrid_z_others                      r   _compute_gridr      s?    k!24yy@		*;7%k2ABB"1FB668RVVX5668RVVX53BGGCK012H;;uX.D;;uX.D[[,NFF ""RaFV$4W # F  ((RaFV$4Y ) L
 4r!   c                     S[         R                  [         R                  U [         R                  US5      5      [         R                  U[         R                  U S5      5      -
  5      -  $ )Nr<   r   )r%   absdotroll)rB   ys     r   _polygon_arear      sE    q"''!Q-0266!RWWQ]3KKLLLr!   c                 V   U[         R                  R                  5       ;   a  [         R                  U   nOA[        R                  " SR                  [         R                  R                  5       5      5      e[        R                  SSU 5      n[        R                  U Vs/ s H  oDS   PM	     sn5      n[        R                  U Vs/ s H  oDS   PM	     sn5      n[        R                  XS5      nSUS   ;   a:  U Vs/ s H-  n[         R                  " [         R                  " U5      5      PM/     nnUS   /n	[        USS USS 5       HG  u  pXZS-
     XZ   pX-
  X-
  -  n[         R                  " XjS-
     Xj   USS9nU	R                  U5        MI     U	$ s  snf s  snf s  snf )zI
Return a list of ``ncontours`` colors from the ``colormap`` colorscale.
zRColorscale must be a valid Plotly Colorscale.The available colorscale names are {}r   r   #Nrgb)	colortype)clrsPLOTLY_SCALESkeysr   PlotlyErrorformatr%   rk   r?   searchsorted	label_rgb
hex_to_rgbzipfind_intermediate_colorappend)	ncontourscolormapcmaprq   pair	vals_cmapcolsindscolcolorsindvalval1val2interms                  r   _colorsr      sy    4%%**,,!!(+$$44:F4;M;M;R;R;T4U
 	
 [[Ay)Fd3ddq'd34I88.!W./D??9-D
d1g~@DEts34E1gYFQR&*-Qw'd*-**qM49f
 	c . M 4. Fs   "F
F!4F&c                     [         R                  [         R                  X S   -
  5      S:  5      =(       a0    [         R                  [         R                  XS   -
  5      S:  5      nU$ )zm
Utility function for _contour_trace

Contours with an area of the order as 1 pixel are considered spurious.
r   r   )r%   allr   )rB   r   	too_smalls      r   _is_invalid_contourr   
  sN     rvvaA$h'!+,MqQ4x8H18L1MIr!   c                    [         R                  U 5      nU [         R                  U5         R                  5       U [         R                  U5         R	                  5       pT[         R                  U 5      nSU-  Xc'   [         R                  U 5      nSU-  Xs'   / / / / 4u  pp/ / / / 4u  pp[        U5       GHa  u  nn[        R                  " UU5      n[        R                  " UU5      nUR                  U5        UR                  U5        U	R                  U/[        U5      -  5        UR                  U/[        U5      -  5        U
R                  U Vs/ s H)  n[        UR                  S   UR                  S   5      PM+     sn5        UR                  U Vs/ s H)  n[        UR                  S   UR                  S   5      PM+     sn5        UR                  UU   /[        U5      -  5        UR                  UU   /[        U5      -  5        GMd     [        U5      [        U5      ::  a  XX4$ XX4$ s  snf s  snf )aC  
Utility function for _contour_trace.

In ``im`` only one part of the domain has valid values (corresponding
to a subdomain where barycentric coordinates are well defined). When
computing contours, we need to assign values outside of this domain.
We can choose a value either smaller than all the values inside the
valid domain, or larger. This value must be chose with caution so that
no spurious contours are added. For example, if the boundary of the valid
domain has large values and the outer value is set to a small one, all
intermediate contours will be added at the boundary.

Therefore, we compute the two sets of contours (with an outer value
smaller of larger than all values in the valid domain), and choose
the value resulting in a smaller total number of contours. There might
be a faster way to do this, but it works...
r   r   r   )r%   isnanlogical_notr   ri   copy	enumerater   find_contoursextendrZ   r   ro   )imrq   r   mask_nanim_minim_maxzz_minzz_maxall_contours1all_values1
all_areas1all_colors1all_contours2all_values2
all_areas2all_colors2ir   contour_level1contour_level2contours                        r   _extract_contoursr     s   $ xx|H
2>>(#$((*
2>>(#$((*  WWR[F6zFWWR[F6zF:<b"b.7M
:<b"b.7M
F#3 ..vs; ..vs;^,^,C53~#667C53~#667FTUn7]799Q<16nU	
 	FTUn7]799Q<16nU	
 	F1I;^)<<=F1I;^)<<= $ =S//:BB:BB V Vs   0H<
0I
c                 
   S[         R                  / SQ/ SQ/5      R                  -  nU/U -   n [         R                  U5      S   n[         R	                  US   U-
  /XDS   U-   /45      n[         R	                  U	/X/45      n[         R                  XE:H  5      S   S   nU[        U5      S-  :  a  US-  nOUS-  nX   /U-   nXM   /U-   nS/U-   nU Vs/ s H  oU;   d  M
  UPM     nn[        U5      n[         R                  SSUS-   5      n/ n[        U5       H;  u  nnUR                  UU   UU   /5        UR                  UUS-      UU   /5        M=     UR                  UU   UUS-
     /5        XX#U4$ s  snf )a  
Utility function for _contour_trace

Adds the background color to fill gaps outside of computed contours.

To compute the background color, the color of the contour with largest
area (``val_outer``) is used. As background color, we choose the next
color value in the direction of the extrema of the colormap.

Then we add information for the outer contour for the different lists
provided as arguments.

A discrete colormap with all used colors is also returned (to be used
by colorscale trace).
   r   r   r   )r   r   r<   r   r   r   )
r%   r?   ro   diffconcatenatenonzerorZ   rk   r   r   )all_contours
all_values	all_areas
all_colorsrq   	val_outerv_minv_maxr   	color_min	color_maxouter_contourdelta_valuesindexcolorused_colorscolor_numberscalediscrete_cmr   s                       r   _add_outer_contourr   F  s   : 9k":;===M!?\1L776?1%L^^
)l
"	#VRj<.G-HIF ^^i[&+>?FJJv*+A.q1Es6{Q

-:-J-:-JiI&,DfU0C5fKD{#LKK1lQ./EKk*5E!Hk!n56E!a%L+a.9: + l+[9I-JKLYKGG Es   	F &F c
                 (   [        US-   U5      n
[        R                  XUS-   5      nU
S   U
S   pU
SS n
USS nUc  SnOU/U-  n
[        X+U
5      u  pnn[        R	                  U5      SSS2   n[        UUUUUUUS      UU	U
UU5      u  pnnn[        R                  S/US-   45      n/ n[        5       u  nnU R                  5       U R                  5       -
  U R                  -  nUR                  5       UR                  5       -
  UR                  -  nU GHp  nUU   R                  u  nnUU   nUS:X  aP  [        R                  U[        R                  UU-  UU-  [        R                  UR                  5      45      5      nONUS:X  aH  [!        [        R                  UU-  U R                  5       -   UU-  UR                  5       -   45      5      nUS:X  aF  [        R#                  / S	Q5      n[        R#                  / S
Q5      n[        R#                  / SQ5      n OWu  nnn [%        UU5      (       a  GM%  US:X  a  UU   OUn!['        SUUU S['        U!SSS9SUU   SSSU-  S9n"US:X  a  SU"S'   UR)                  U"5        GMs     UU4$ )a  
Contour trace in Cartesian coordinates.

Parameters
==========

x, y : array-like
    Cartesian coordinates
z : array-like
    Field to be represented as contours.
ncontours : int or None
    Number of contours to display (determined automatically if None).
colorscale : None or str (Plotly colormap)
    colorscale of the contours.
linecolor : rgb color
    Color used for lines. If ``colorscale`` is not None, line colors are
    determined from ``colorscale`` instead.
interp_mode : 'ilr' (default) or 'cartesian'
    Defines how data are interpolated to compute contours. If 'irl',
    ILR (Isometric Log-Ratio) of compositional data is performed. If
    'cartesian', contours are determined in Cartesian space.
coloring : None or 'lines'
    How to display contour. Filled contours if None, lines if ``lines``.
vmin, vmax : float
    Bounds of interval of values used for the colorspace

Notes
=====
r   r   r   r   Nzrgb(150, 150, 150)rd   rb   )r   r   r   )r   r   r   r   linesscatterternaryspline)r   rY   r   toselfTskipz%.3f)typer   r	   r
   modelinefill	fillcolorr   	hoverinfonamer   )r   r%   rk   r   argsortr   r   rR   ri   r   sizero   r   r5   rM   rY   rJ   r?   r   r   r   )#rB   r   zr   
colorscale	linecolorrr   coloringr   r   r   rq   r   r   r   r   r   r   orderr   tracesrQ   rs   dxdyr   	y_contour	x_contourr   
bar_coordsr   r	   r
   _coltraces#                                      r   _contour_tracer     s   X Y]J/F [[y1}5F!!9fRjyAb\FAb\F (	y( 7H	673Li
 JJy!$B$'E DV58D@Li[ NNQC+,E F.0GAt
%%'AEEG
qvv	%B
%%'AEEG
qvv	%B+E244	9+%"y."y."'')//:RSTJ E!%"y.15572BNQUUW4LMNJ A:#A#A#A GAq!y)44$,$7z% Y!D: '#
 w E&MeK N ;r!   i  FBlueredc                    [         c  [        S5      e[        R                  " S5      nUc  [        S5      eUc  SnUc  Sn[	        U 5      n UR                  5       UR                  5       p[        XUS9u  nnn[        X#XJS9n[        UUUUUU	UUUUS	9
u  nn[        R                  " UUS
9nU(       a  SOSnU u  nnnUS   S-   US   -   S-   US   -   S-   nUR                  UUUSUUS[        US:g  5      S.S.UUS9  U(       aP  U(       d  Un[        SS/S/S/UR                  5       UR                  5       USS.SS.5      nUR                  U5        U$ )a
  
Ternary contour plot.

Parameters
----------

coordinates : list or ndarray
    Barycentric coordinates of shape (2, N) or (3, N) where N is the
    number of data points. The sum of the 3 coordinates is expected
    to be 1 for all data points.
values : array-like
    Data points of field to be represented as contours.
pole_labels : str, default ['a', 'b', 'c']
    Names of the three poles of the triangle.
width : int
    Figure width.
height : int
    Figure height.
ncontours : int or None
    Number of contours to display (determined automatically if None).
showscale : bool, default False
    If True, a colorbar showing the color scale is displayed.
coloring : None or 'lines'
    How to display contour. Filled contours if None, lines if ``lines``.
colorscale : None or str (Plotly colormap)
    colorscale of the contours.
linecolor : None or rgb color
    Color used for lines. ``colorscale`` has to be set to None, otherwise
    line colors are determined from ``colorscale``.
title : str or None
    Title of ternary plot
interp_mode : 'ilr' (default) or 'cartesian'
    Defines how data are interpolated to compute contours. If 'irl',
    ILR (Isometric Log-Ratio) of compositional data is performed. If
    'cartesian', contours are determined in Cartesian space.
showmarkers : bool, default False
    If True, markers corresponding to input compositional points are
    superimposed on contours, using the same colorscale.

Examples
========

Example 1: ternary contour plot with filled contours

>>> import plotly.figure_factory as ff
>>> import numpy as np
>>> # Define coordinates
>>> a, b = np.mgrid[0:1:20j, 0:1:20j]
>>> mask = a + b <= 1
>>> a = a[mask].ravel()
>>> b = b[mask].ravel()
>>> c = 1 - a - b
>>> # Values to be displayed as contours
>>> z = a * b * c
>>> fig = ff.create_ternary_contour(np.stack((a, b, c)), z)
>>> fig.show()

It is also possible to give only two barycentric coordinates for each
point, since the sum of the three coordinates is one:

>>> fig = ff.create_ternary_contour(np.stack((a, b)), z)


Example 2: ternary contour plot with line contours

>>> fig = ff.create_ternary_contour(np.stack((a, b, c)), z, coloring='lines')

Example 3: customize number of contours

>>> fig = ff.create_ternary_contour(np.stack((a, b, c)), z, ncontours=8)

Example 4: superimpose contour plot and original data as markers

>>> fig = ff.create_ternary_contour(np.stack((a, b, c)), z, coloring='lines',
...                                 showmarkers=True)

Example 5: customize title and pole labels

>>> fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
...                                 title='Ternary plot',
...                                 pole_labels=['clay', 'quartz', 'fledspar'])
NzH    The create_ternary_contour figure factory requires the scipy packageskimagezS    The create_ternary_contour figure factory requires the scikit-image
    packageF   )rr   )r   r   r   r   )r   r   r   rr   r   r   r   )datalayoutr   r   z: %{a:.3f}<br>z: %{b:.3f}<br>r   z3: %{c:.3f}<br>z: %{marker.color:.3f}<extra></extra>markerszrgb(120, 120, 120)r   )r   r   )r   r   r   )r   r	   r
   r   markeropacityhovertemplater   T)cmincmaxr   	showscale)r   r   r	   r
   r  r   )rm   ImportErrorr   
get_modulera   r   ri   r   r    r   goFigureadd_scatterternaryrj   r   	add_trace)rp   rq   r   r   r   r   r  r   r   r   r   rr   showmarkers
sk_measurer   r   r   r|   r}   r   contour_tracer   figr  r   r	   r
   r  colorbars                                r   create_ternary_contourr    s   B L
 	
 ",,Y7J
 	

 		,[9K::<5&{TFD$VF "0"M; ))v
6CaAGGAq!A
	
a.	 	 a.		
0	0  


$2SWAT=UV

 #   $J(VVV"JJL"JJL",!%	 "
 	hJr!   )gMb@?)rb   )N)NElectriczrgb(150,150,150)llrNr   r   )plotly.colorsr   r   plotly.graph_objsr   r
  plotlyr   r   r	  r%   rm   r   r   r    r.   r:   rJ   rR   ra   r   r   r   r   r   r   r   r   r!   r   <module>r     s     .  #  )**+>?  !C_#RD2<@(\M:/Cd6Hz  

P  

ur!   