
    1Vh#                         d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ dZ G d	 d
ej                        Zy)    N)wrappers)plugin_util)provider)	http_util)base_plugin)metadatad   c                       e Zd ZdZej
                  Zd Zej                  j                  d        Zd Zej                  j                  d        Zd Zd Zd Zd	 Zd
 Zd Zy)PrCurvesPluginz6A plugin that serves PR curves for individual classes.c                     |j                   | _        |j                  xs i j                  t        j
                  t              | _        t        j                  dd      | _
        y)zInstantiates a PrCurvesPlugin.

        Args:
          context: A base_plugin.TBContext instance. A magic container that
            TensorBoard uses to make objects available to the plugin.
        zPR curver   )	data_kindlatest_known_versionN)data_provider_data_providersampling_hintsgetr   PLUGIN_NAME_DEFAULT_DOWNSAMPLING_downsample_tor   _MetadataVersionChecker_version_checker)selfcontexts     ]/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/pr_curve/pr_curves_plugin.py__init__zPrCurvesPlugin.__init__"   sW     &33&55;@@  "7
 !, C C !"!
    c           	         t        j                  |j                        }t        j                  |j                        }|j                  j                  d      }|st        j                  |dd      S |j                  j                  d      }|st        j                  |dd      S 	 t        j                  || j                  ||||      d      }|S # t        $ r+}t        j                  |t        |      dd      cY d}~S d}~ww xY w)	a  A route that returns a JSON mapping between runs and PR curve data.

        Returns:
          Given a tag and a comma-separated list of runs (both stored within GET
          parameters), fetches a JSON object that maps between run name and objects
          containing data required for PR curves for that run. Runs that either
          cannot be found or that lack tags will be excluded from the response.
        runz,No runs provided when fetching PR curve datai  tagz+No tag provided when fetching PR curve dataapplication/jsonz
text/plainN)r   r   environexperiment_idargsgetlistr   Respondr   pr_curves_impl
ValueErrorstr)r   requestctx
experimentrunsr   responsees           r   pr_curves_routezPrCurvesPlugin.pr_curves_route2   s     !!'//2 ..w?
||##E*$$G  llu%$$F 	I ((##CT3?"H   	I$$Wc!flCHH	Is   ()C 	D D<DDc                 l   i }t        j                  ||g      }| j                  j                  ||t        j
                  || j                        }|D ]X  }|j                  |i       j                  |      }	|	t        d|d|      |	D 
cg c]  }
| j                  |
       c}
||<   Z |S c c}
w )a}  Creates the JSON object for the PR curves response for a run-tag
        combo.

        Arguments:
          runs: A list of runs to fetch the curves for.
          tag: The tag to fetch the curves for.

        Raises:
          ValueError: If no PR curves could be fetched for a run and tag.

        Returns:
          The JSON object for the PR curves route response.
        )r"   plugin_namerun_tag_filter
downsamplez$No PR curves could be found for run z	 and tag )
r   RunTagFilterr   read_tensorsr   r   r   r   r'   _process_datum)r   r*   r+   r,   r   response_mappingrtfread_resultr   datads              r   r&   zPrCurvesPlugin.pr_curves_implV   s     ##D3%0))66$ ,,** 7 
  	KC??3+//4D| C!  FJ$JT%8%8%;$JS!	K   %Ks   B1c                     t        j                  |j                        }t        j                  |j                        }t	        j
                  || j                  ||      d      S )a  A route (HTTP handler) that returns a response with tags.

        Returns:
          A response that contains a JSON object. The keys of the object
          are all the runs. Each run is mapped to a (potentially empty) dictionary
          whose keys are tags associated with run and whose values are metadata
          (dictionaries).

          The metadata dictionaries contain 2 keys:
            - displayName: For the display name used atop visualizations in
                TensorBoard.
            - description: The description that appears near visualizations upon the
                user hovering over a certain icon.
        r    )r   r   r!   r"   r   r%   	tags_impl)r   r)   r*   r+   s       r   
tags_routezPrCurvesPlugin.tags_routew   sR      !!'//2 ..w?
  T^^C46H
 	
r   c                    | j                   j                  ||t        j                        }|D ci c]  }|i  }}|j	                         D ]  \  }}|j	                         D ]~  \  }}t        j
                  |j                        }	| j                  j                  |	j                  ||      sM|j                  t        j                  |j                        d||   |<     |S c c}w )zCreates the JSON object for the tags route response.

        Returns:
          The JSON object for the tags route response.
        )r"   r1   )displayNamedescription)r   list_tensorsr   r   itemsparse_plugin_metadataplugin_contentr   okversiondisplay_namer   markdown_to_safe_htmlrA   )
r   r*   r+   mappingr   resulttag_to_time_seriesr   time_seriesmds
             r   r=   zPrCurvesPlugin.tags_impl   s     %%22zx7K7K 3 
 &--c#r'--'.}} 
	#C#$6$<$<$> 	 [33K4N4NO,,//

CE#.#;#;#.#D#D#//$$sC 		
	  .s   
C(c                 4    | j                   | j                  dS )zGets all routes offered by the plugin.

        Returns:
          A dictionary mapping URL path to route that handles it.
        )z/tagsz
/pr_curves)r>   r/   r   s    r   get_plugin_appszPrCurvesPlugin.get_plugin_apps   s     __..
 	
r   c                      y)NF rP   s    r   	is_activezPrCurvesPlugin.is_active   s    r   c                 0    t        j                  dd      S )Nztf-pr-curve-dashboardz	PR Curves)element_nametab_name)r   FrontendMetadatarP   s    r   frontend_metadataz PrCurvesPlugin.frontend_metadata   s    ++0 
 	
r   c                 d    | j                  |j                  |j                  |j                        S )zConverts a TensorDatum into a dict that encapsulates information on
        it.

        Args:
          datum: The TensorDatum to convert.

        Returns:
          A JSON-able dictionary of PR curve data for 1 step.
        )_make_pr_entrystep	wall_timenumpy)r   datums     r   r6   zPrCurvesPlugin._process_datum   s%     ""5::uLLr   c                 n   t         j                  }t         j                  }t         j                  }t         j                  }|||gddf   j                  t              j                  d      }t        |      dz
  }	|	dkD  r||	   dk(  r|	dz  }	|	dkD  r	||	   dk(  r|	dz   }
|j                  d   }t        j                  dd|      }||   D cg c]  }t        |       }}||   D cg c]  }t        |       }}||   D cg c]  }t        |       }}||   D cg c]  }t        |       }}|||t         j                  d|
f   j                         |t         j                  d|
f   j                         |d|
 |d|
 |d|
 |d|
 |d|
 j                         d	S c c}w c c}w c c}w c c}w )a$  Creates an entry for PR curve data. Each entry corresponds to 1
        step.

        Args:
          step: The step.
          wall_time: The wall time.
          data_array: A numpy array of PR curve data stored in the summary format.

        Returns:
          A PR curve entry.
        Nr   )axis   g        g      ?)	r]   r\   	precisionrecalltrue_positivesfalse_positivestrue_negativesfalse_negatives
thresholds)r   TRUE_POSITIVES_INDEXFALSE_POSITIVES_INDEXTRUE_NEGATIVES_INDEXFALSE_NEGATIVES_INDEXastypeintsumlenshapenplinspacePRECISION_INDEXtolistRECALL_INDEX)r   r\   r]   
data_arraytp_indexfp_indextn_indexfn_index	positivesend_index_inclusive	end_indexnum_thresholdsri   vre   rf   rg   rh   s                     r   r[   zPrCurvesPlugin._make_pr_entry   s    00110011 (3Q67>>sCGGQGO	!)nq0!A%)4G*HA*M1$ "A%)4G*HA*M'!+	#))!,[[c>:
*4X*>?Q#a&??+5h+?@a3q6@@*4X*>?Q#a&??+5h+?@a3q6@@ ##((*9*4fh !6!6

!BCJJL,Zi8.z	:,Zi8.z	:$Zi0779
 	
 @@?@s   F#*F(F- F2N)__name__
__module____qualname____doc__r   r   r1   r   r   Requestapplicationr/   r&   r>   r=   rQ   rT   rY   r6   r[   rS   r   r   r   r      s{    @&&K
  !!! "!F B !!
 "
*.	


M.
r   r   )r^   rs   werkzeugr   tensorboardr   tensorboard.datar   tensorboard.backendr   tensorboard.pluginsr   tensorboard.plugins.pr_curver   r   TBPluginr   rS   r   r   <module>r      s8       # % ) + 1  U
[)) U
r   