B  @`ËQã@sddlZddlmZmZmZmZmZm Z m Z m Z m Z m Z ejZe jZe ddgeefd\ZZee @e @e@e@Zdd„Zdd „Zd d „Zd(d d „Zd)dd„Zd*dd„Zd+dd„Zdd„Zddgdddddfdd„Zdd„Zdd„Z dd„Z!dd „Z"d!d"d#gdfd$d%„Z#d!d"d#gdfd&d'„Z$dS),éN) Ú matplotlibÚmatplotlib_availableÚnumpyÚnumpy_availableÚpandasÚpandas_availableÚscipyÚscipy_availableÚcheck_min_versionÚattempt_importZseabornÚsns)Z alt_namesZcatch_exceptionsc Csr| ¡}| ¡d}| ¡d}| ¡d}tt ||¡ƒ}tt ||||¡ƒ}||}||} || ||ffS)Néré)Zget_subplotspecZ get_geometryÚintÚnpÚmod) ÚaxÚcolumnsZspsZnxÚnyÚcellÚxlocÚylocÚxvarÚyvar©rúB/tmp/pip-unpacked-wheel-bi3529v6/pyomo/contrib/parmest/graphics.pyÚ_get_variables"s   rcCs¤t t|ƒtt|ƒt|ƒƒdt|ƒtt|ƒt|ƒƒd|¡}t t|ƒtt|ƒt|ƒƒdt|ƒtt|ƒt|ƒƒd|¡}t ||¡\}}||fS)Nr)rZlinspaceÚminÚabsÚmaxZmeshgrid)ÚxÚyÚncellsZxlinZylinÚXÚYrrrÚ _get_XYgrid1sFFr%cCsBi}x8|D]0}|||gkr,|| ¡||<q ||g||<q Wtjttj| ¡Žƒ| ¡d}x†||jD]x}||  ¡||  ¡} | dkrj|  ¡} | dkr¼| |||  ¡d| |<n| |||  ¡| |<|j | dd}qjWt jjt ||¡t |dg¡t ||¡ddd |d<||} ||} |d} | | | fS) N)rg:Œ0âŽyE>ré T)Ú ignore_indexÚobjZlinear)ÚmethodZrescale)ÚuniqueÚpdÚ DataFrameÚlistÚ itertoolsÚproductÚvaluesÚkeysrÚstdÚmeanÚcopyÚappendrZ interpolateZgriddatarÚarray)rrrÚdataÚ theta_starZ search_rangesÚvarÚ data_sliceÚcolZcvÚtempr#r$ÚZrrrÚ_get_data_slice9s2      r>c Cs6t ¡}t||ƒ\}}} |j|||||dddS)Né#)ÚcÚs)ÚpltÚgcarÚscatter) r r!Úcolorrr8ÚlabelrrrÚlocrrrÚ _add_scatterbsrHcCsœt ¡}t||ƒ\}} } ||} || } ||} || }|j| | g| | g|d|j| | g| |g|d|j| | g||g|d|j| | g|| g|ddS)N)rE)rBrCrZplot)r r!rErÚ lower_boundÚ upper_boundrFrrrrGZxminZyminZxmaxZymaxrrrÚ_add_rectangle_CIisrKc Csjt ¡} t| |ƒ\} } } t|||ƒ\} }g}t|tjjƒr®xb|jD]X}|| krZ|  | ¡qB|| krn|  |¡qB|| | gkrB|  t   ||g|g|¡¡qBWt   t |ƒ¡}nŠt|tjjƒr4xh|jD]^}|| krâ|  |  ¡¡qÆ|| krú|  | ¡¡qÆ|| | gkrÆ|  t   ||g||¡¡qÆWt   |¡}ndS| |¡}| ||f¡}| j| |||g|ddS)N)ÚlevelsÚcolors)rBrCrr%Ú isinstanceÚstatsZ _multivariateZmultivariate_normal_frozenÚindexr5rr6ZdstackÚtupleÚkdeÚ gaussian_kdeZravelÚpdfZreshapeZcontour)r r!rErr"ÚalphaÚdistr8rFrrrrGr#r$r:r9r=rrrÚ_add_scipy_dist_CIxs4    $  "  rWcCs|t ¡}t||ƒ\}} } yDt|| |||ƒ\} } } tj | | ¡}tj d¡}tj || |dWnt d|| dƒYnXdS)NZGreys)ÚcmapzObjective contour plot forz slice failed) rBrCrr>rZtriZ TriangulationÚcmZget_cmapZ tricontourfÚprint)r r!rErr7r8rFrrrrGr#r$r=ZtriangrXrrrÚ_add_obj_contourœs r[c Csö|dk r|j|dd}|dkr”i}xp|jD]f}t || ¡|| ¡¡}|dkrd|| ¡d}|| ¡|d|| ¡|dg||<q*Wx\|j ¡D]N}t||jƒ\}}\} } | | krà|  ||¡|  ||¡q |  ||¡q WdS)NT)r'g»½×Ùß|Û=r&é) r5rrrrrÚfigZget_axesrZset_ylimZset_xlim) ÚgÚ axis_limitsZ theta_valsr8r;Z theta_rangerrrrrrrrÚ_set_axis_limitsªs  r`Tc $ Csìt|tjtfƒst‚t|tdƒttjtjfƒs2t‚t|tdƒtt fƒsJt‚t|t ƒsXt‚t |ƒ  t dddgƒ¡stt‚t|tdƒtfƒsŠt‚t|tdƒt fƒs t‚t|tƒs®t‚t|tdƒt fƒsÄt‚t|tƒr8t|ƒdksàt‚|d} |d} |d} t| tƒrt | ¡} | j} t | | ¡} tj| j| dd | d }|jddksLt‚t|tƒrbt |¡}t|tjƒr‚|jddd…f}d d „|jDƒ} ||jkrÄt|ƒdkrÄ|j||d k| f}n|| }|dk rÞ|| }g}t |¡}ttdƒr| tj¡n|jtjdd ddd|jkr>|r>|jt| ||d|jtj dd| !t"j#j$dgdgdddddd¡|dk r²|jt%d| |d| !t"j#j$dgdgdddddd¡dd d!g}|dk rNt|ƒdkrN|dkrît&d"ƒ| '¡}d} d}xTt(|ƒD]F\}}|dkrbt)||ƒ\}}|jt*||| ||d#| !t"j#j$dgdg||d|d$¡næ|dkrÔt+|ƒ} |  ,|¡}t -|d|d%¡}|jt.||| d%|| |d&| !t"j#j$dgdg||d|d$¡nt|dkrt/|ƒ}| ,| 0¡¡}t -|d|d%¡}|jt.||| d%|||d&| !t"j#j$dgdg||d|d$¡qWt1||||ƒxd|j2 3¡D]V}|j4d'd(d)d*|rht5|| ƒ\}}}|t| ƒddfkrh|j6|d+d,d-id.qhW|râ|j7j8d/d0|j7 9|¡d}|rÂxÈ|j2 3¡D]º}t5|| ƒ\}}\}}||krø| :¡| ;|¡| <|¡t =¡} | |_=| j2 !|¡|  >|¡t ?¡\}!}"|" @¡}#| A|#¡|" :¡t B|!¡|jCd d1|rø|j6|d+d,d-id.qøWt B|j7¡|dkrÖt D¡nt E|¡t B¡dS)2aô Plot pairwise relationship for theta values, and optionally alpha-level confidence intervals and objective value contours Parameters ---------- theta_values: DataFrame or tuple * If theta_values is a DataFrame, then it contains one column for each theta variable and (optionally) an objective value column ('obj') and columns that contains Boolean results from confidence interval tests (labeled using the alpha value). Each row is a sample. * Theta variables can be computed from ``theta_est_bootstrap``, ``theta_est_leaveNout``, and ``leaveNout_bootstrap_test``. * The objective value can be computed using the ``likelihood_ratio_test``. * Results from confidence interval tests can be computed using the ``leaveNout_bootstrap_test``, ``likelihood_ratio_test``, and ``confidence_region_test``. * If theta_values is a tuple, then it contains a mean, covariance, and number of samples (mean, cov, n) where mean is a dictionary or Series (indexed by variable name), covariance is a DataFrame (indexed by variable name, one column per variable name), and n is an integer. The mean and covariance are used to create a multivariate normal sample of n theta values. The covariance can be computed using ``theta_est(calc_cov=True)``. theta_star: dict or Series, optional Estimated value of theta. The dictionary or Series is indexed by variable name. Theta_star is used to slice higher dimensional contour intervals in 2D alpha: float, optional Confidence interval value, if an alpha value is given and the distributions list is empty, the data will be filtered by True/False values using the column name whose value equals alpha (see results from ``leaveNout_bootstrap_test``, ``likelihood_ratio_test``, and ``confidence_region_test``) distributions: list of strings, optional Statistical distribution used to define a confidence region, options = 'MVN' for multivariate_normal, 'KDE' for gaussian_kde, and 'Rect' for rectangular. Confidence interval is a 2D slice, using linear interpolation at theta_star. axis_limits: dict, optional Axis limits in the format {variable: [min, max]} title: string, optional Plot title add_obj_contour: bool, optional Add a contour plot using the column 'obj' in theta_values. Contour plot is a 2D slice, using linear interpolation at theta_star. add_legend: bool, optional Add a legend to the plot filename: string, optional Filename used to save the figure NZMVNZKDEZRectérr r)Z random_state)rcSs,g|]$}|dkrt|tƒst|tƒs|‘qS))r()rNÚfloatr)Ú.0r;rrrú s z!pairwise_plot..Tz0.11F)rRÚhistZ norm_histr()rr7r8r&)rAÚoÚwÚthetasZ cadetblueé)ÚmarkerrErFZmarkerfacecolorZ markersizeÚk)rErr8ztheta*éÚrZ mediumblueZdarkgrayzptheta_star is not defined, confidence region slice will be plotted at the mean value of theta)rErrIrJ)rEZlwrFéd)rErr"rUrVr8Zsci)éþÿÿÿrZboth)ÚstyleZ scilimitsZaxisÚbestÚsizeé)ZhandlesrGÚpropgÍÌÌÌÌÌì?)Útop)Úreset)FrNr+r,rQÚAssertionErrorÚtypeÚdictZSeriesrrbr-ÚsetÚissubsetÚstrÚboolÚlenrPrOÚmultivariate_normalZrvsÚshaperGrr ZPairGridr Zmap_diagZhistplotZdistplotZ map_offdiagr[rBrDr5rÚlinesZLine2DrHrZr3Ú enumerateÚ fit_rect_distrKÚ fit_mvn_distrTZscoreatpercentilerWÚ fit_kde_distÚ transposer`ZaxesÚflattenZticklabel_formatrÚlegendr]Zsubplots_adjustZsuptitleÚremoveÚ set_xlabelÚ set_ylabelÚfigureZadd_axesZsubplotsÚ get_positionZ set_positionÚcloseZ tick_paramsÚshowÚsavefig)$Ú theta_valuesr8rUÚ distributionsr_ÚtitleZadd_obj_contourZ add_legendÚfilenamer3ÚcovÚnZ theta_namesZmvn_distrhZlegend_elementsr^rMZkde_distÚirVZlbZubr=ZscorerrrrGZlower_triangle_onlyrrr]ÚfÚdummyZbboxrrrÚ pairwise_plotÀsæ9                                        ršcCstt|tjƒst‚t|ttfƒs"t‚tj dd|dt |ƒd¡}|  ¡}|  ¡}|||}|||}||fS)aR Fit an alpha-level rectangular distribution to theta values Parameters ---------- theta_values: DataFrame Theta values, columns = variable names alpha: float, optional Confidence interval value Returns --------- tuple containing lower bound and upper bound for each variable r r) rNr+r,rwrrbrOÚtZppfr~r3r2)r‘rUZtvalÚmrArIrJrrrrƒšs"  rƒcCs,t|tjƒst‚tj| ¡| ¡dd}|S)zþ Fit a multivariate normal distribution to theta values Parameters ---------- theta_values: DataFrame Theta values, columns = variable names Returns --------- scipy.stats.multivariate_normal distribution T)Zallow_singular)rNr+r,rwrOrr3r•)r‘rVrrrr„´s r„cCs$t|tjƒst‚t | ¡j¡}|S)zû Fit a Gaussian kernel-density distribution to theta values Parameters ---------- theta_values: DataFrame Theta values, columns = variable names Returns --------- scipy.stats.gaussian_kde distribution )rNr+r,rwrOrSr†r0)r‘rVrrrr…Çs r…cCs||r,| ¡}| ¡}|||}|||}t |d||d|i¡}|jddd|jddidd|jd|jdd }|S) Nrr T)ÚlevelÚinplaceZlevel_0rz)rržr)Zid_varsZ value_varsÚvar_name)Zmedianr2r+ÚconcatZ reset_indexÚrenameZmeltr)Údata1Údata2Ú normalizeÚ group_namesZ data_medianZdata_stdr7rrrÚ_get_grouped_dataÚs   r¦Fr¢r£cCsÔt|tjƒst‚t|tjƒs t‚t|tƒs.t‚t|tƒsrHrKrWr[r`ršrƒr„r…r¦r¬r®rrrrÚ s60)   $ Y )