
    2Vhg                         d dl mZ 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Zd Zdd	Z G d
 de      Z G d de      Z G d de      Z	 	 	 	 ddZd Z	 	 	 	 	 	 ddZd Z	 	 ddZy)    )EnumN)backend)ops)squeeze_or_expand_to_same_rank)to_listg    _c                 l    | -| D cg c]  }||dk  s|dkD  s| }}|rt        d|       y y c c}w )Nr      z.Threshold values must be in [0, 1]. Received: )
ValueError)
thresholdstinvalid_thresholdss      O/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/metrics/metrics_utils.pyassert_thresholds_ranger      sd    !
QY!a%1q5A
 
 /02  	 
s   11c                 \    | t        t        |              t        | |      } | S |       } | S N)r   r   )r   default_thresholds     r   parse_init_thresholdsr      sE    
 34'/J  6@J     c                       e Zd ZdZdZdZdZy)ConfusionMatrixtpfptnfnN)__name__
__module____qualname__TRUE_POSITIVESFALSE_POSITIVESTRUE_NEGATIVESFALSE_NEGATIVES r   r   r   r   "   s    NONOr   r   c                   (    e Zd ZdZdZdZed        Zy)AUCCurvezType of AUC Curve (ROC or PR).ROCPRc                 p    | dv rt         j                  S | dv rt         j                  S t        d|  d      )N)prr&   )rocr%   zInvalid AUC curve value: "z$". Expected values are ["PR", "ROC"])r$   r&   r%   r
   keys    r   from_strzAUCCurve.from_str/   sD    ,;;N"<<,SE 24 4 r   N)r   r   r   __doc__r%   r&   staticmethodr,   r"   r   r   r$   r$   )   s!    (
C	B	 	r   r$   c                   ,    e Zd ZdZdZdZdZed        Zy)AUCSummationMethoda:  Type of AUC summation method.

    https://en.wikipedia.org/wiki/Riemann_sum)

    Contains the following values:
    * 'interpolation': Applies mid-point summation scheme for `ROC` curve. For
      `PR` curve, interpolates (true/false) positives but not the ratio that is
      precision (see Davis & Goadrich 2006 for details).
    * 'minoring': Applies left summation for increasing intervals and right
      summation for decreasing intervals.
    * 'majoring': Applies right summation for increasing intervals and left
      summation for decreasing intervals.
    interpolationmajoringminoringc                     | dv rt         j                  S | dv rt         j                  S | dv rt         j                  S t	        d|  d      )N)r1   Interpolation)r2   Majoring)r3   Minoringz%Invalid AUC summation method value: "z@". Expected values are ["interpolation", "majoring", "minoring"])r0   INTERPOLATIONMAJORINGMINORINGr
   r*   s    r   r,   zAUCSummationMethod.from_strO   sa    44%333,,%...,,%...7u =P P r   N)	r   r   r   r-   r8   r9   r:   r.   r,   r"   r   r   r0   r0   <   s*     $MHH r   r0   c           	      
   t        j                  |      d   |d}nat        j                  t        j                  ||j                        t        j                  |            }|st        j
                  |dg      }|d}nXt        j                  |d      }t        j                  |t        j                  |            }|st        j
                  |dg      }t        j                  t        j                  ||      |j                        }t        j                  |dd      }t        j                  t        j                  |d      |j                        }|s.t        j
                  |dg      }t        j
                  |dg      }t        j                  ||      }	t        j                  d|z
  |      }
t        j                  |t        j                  |j                        d	z
  z        d	z
  }|rt        j                  |      }t        j                  |d
      }|rt        j                  |	      }	t        j                  |
      }
t        j                  |      }fd}t        j                  ||	|f      }t        j                  ||
|f      }t        j                  t        j                  t        j                  t        j                  |      d	                  }t        j                  t        j                  t        j                  t        j                  |      d	                  }nt        j                   |	|      }t        j                   |
|      }t        j                  t        j                  t        j                  |                  }t        j                  t        j                  t        j                  |                  }t"        j$                  | v st"        j&                  | v r[|r/t        j(                  |	d	      }t        j(                  |
d	      }n*t        j(                  |	      }t        j(                  |
      }t"        j*                  | v r'| t"        j*                     }|j-                  ||z          t"        j.                  | v r'| t"        j.                     }|j-                  ||z          t"        j$                  | v r,| t"        j$                     }|z
  }|j-                  ||z          t"        j&                  | v r-| t"        j&                     }|z
  }|j-                  ||z          yy)a?  Update confusion matrix variables with memory efficient alternative.

    Note that the thresholds need to be evenly distributed within the list, eg,
    the diff between consecutive elements are the same.

    To compute TP/FP/TN/FN, we are measuring a binary classifier
      C(t) = (predictions >= t)
    at each threshold 't'. So we have
      TP(t) = sum( C(t) * true_labels )
      FP(t) = sum( C(t) * false_labels )

    But, computing C(t) requires computation for each t. To make it fast,
    observe that C(t) is a cumulative integral, and so if we have
      thresholds = [t_0, ..., t_{n-1}];  t_0 < ... < t_{n-1}
    where n = num_thresholds, and if we can compute the bucket function
      B(i) = Sum( (predictions == t), t_i <= t < t{i+1} )
    then we get
      C(t_i) = sum( B(j), j >= i )
    which is the reversed cumulative sum in ops.cumsum().

    We can compute B(i) efficiently by taking advantage of the fact that
    our thresholds are evenly distributed, in that
      width = 1.0 / (num_thresholds - 1)
      thresholds = [0.0, 1*width, 2*width, 3*width, ..., 1.0]
    Given a prediction value p, we can map it to its bucket by
      bucket_index(p) = floor( p * (num_thresholds - 1) )
    so we can use ops.segment_sum() to update the buckets in one pass.

    Consider following example:
    y_true = [0, 0, 1, 1]
    y_pred = [0.1, 0.5, 0.3, 0.9]
    thresholds = [0.0, 0.5, 1.0]
    num_buckets = 2   # [0.0, 1.0], (1.0, 2.0]
    bucket_index(y_pred) = ops.floor(y_pred * num_buckets)
                         = ops.floor([0.2, 1.0, 0.6, 1.8])
                         = [0, 0, 0, 1]
    # The meaning of this bucket is that if any of the label is true,
    # then 1 will be added to the corresponding bucket with the index.
    # Eg, if the label for 0.2 is true, then 1 will be added to bucket 0. If the
    # label for 1.8 is true, then 1 will be added to bucket 1.
    #
    # Note the second item "1.0" is floored to 0, since the value need to be
    # strictly larger than the bucket lower bound.
    # In the implementation, we use ops.ceil() - 1 to achieve this.
    tp_bucket_value = ops.segment_sum(true_labels, bucket_indices,
                                                   num_segments=num_thresholds)
                    = [1, 1, 0]
    # For [1, 1, 0] here, it means there is 1 true value contributed by bucket
    # 0, and 1 value contributed by bucket 1. When we aggregate them to
    # together, the result become [a + b + c, b + c, c], since large thresholds
    # will always contribute to the value for smaller thresholds.
    true_positive = ops.cumsum(tp_bucket_value, reverse=True)
                  = [2, 1, 0]

    This implementation exhibits a run time and space complexity of O(T + N),
    where T is the number of thresholds and N is the size of predictions.
    Metrics that rely on standard implementation instead exhibit a complexity of
    O(T * N).

    Args:
        variables_to_update: Dictionary with 'tp', 'fn', 'tn', 'fp' as valid
            keys and corresponding variables to update as values.
        y_true: A floating point `Tensor` whose shape matches `y_pred`. Will be
            cast to `bool`.
        y_pred: A floating point `Tensor` of arbitrary shape and whose values
            are in the range `[0, 1]`.
        thresholds: A sorted floating point `Tensor` with value in `[0, 1]`.
            It need to be evenly distributed (the diff between each element need
            to be the same).
        multi_label: Optional boolean indicating whether multidimensional
            prediction/labels should be treated as multilabel responses, or
            flattened into a single label. When True, the values of
            `variables_to_update` must have a second dimension equal to the
            number of labels in y_true and y_pred, and those tensors must not be
            RaggedTensors.
        sample_weights: Optional `Tensor` whose rank is either 0, or the same
            rank as `y_true`, and must be broadcastable to `y_true` (i.e., all
            dimensions must be either `1`, or the same as the corresponding
            `y_true` dimension).
        label_weights: Optional tensor of non-negative weights for multilabel
            data. The weights are applied when calculating TP, FP, FN, and TN
            without explicit multilabel handling (i.e. when the data is to be
            flattened).
        thresholds_with_epsilon: Optional boolean indicating whether the leading
            and tailing thresholds has any epsilon added for floating point
            imprecisions.  It will change how we handle the leading and tailing
            bucket.
    r   N      ?dtype        )x_minx_maxboolr	   int32c                 H    | d   | d   }}t        j                  ||      S )Nr   r	   datasegment_idsnum_segments)r   segment_sum)label_and_bucket_indexlabelbucket_indexnum_thresholdss      r   gather_bucketzC_update_confusion_matrix_variables_optimized.<locals>.gather_bucket   s6    &q)&q)  E ??(+ r   axisrF   )r   shapebroadcast_tocastr>   reshapeexpand_dimsmultiplyclipceilrelu	transposer   vectorized_mapflipcumsumrJ   r   r    r!   sumr   assignr   )variables_to_updatey_truey_predr   multi_labelsample_weightslabel_weightsthresholds_with_epsilonweightstrue_labelsfalse_labelsbucket_indicesrO   tp_bucket_vfp_bucket_vr   r   total_true_labelstotal_false_labelsvariabler   r   rN   s                         @r   ,_update_confusion_matrix_variables_optimizedrq   ^   s   D YYz*1-N))HH^6<<8#))F:K
  [[">Nq9((		&8IJKKt<Mhh^]3V\\G XXfCs3FXXchhvv.=FVbT*VbT*,,vw/K<<v8L 	388N&,,G!KLM
	 
  .1XXng6N
 mmK0}}\2~6		 ,,.)
 ,,L.9
 ]]388CJJsxx/D1$MNO]]388CJJsxx/D1$MNOoo&'

 oo&'

 XXcjj+!678XXcjj+!678
 	&&*==**.AA #! <!$A!> # 4!$!6%%)<<&'E'EF2&&&*==&'F'FG2&%%)<<&'E'EF"$2&&&*==&'F'FG#2& >r   c                     t        |       }|dk  ryt        j                  |t        j                        |dz
  z  }t        j                  | |t        j                               S )a  Check if the thresholds list is evenly distributed.

    We could leverage evenly distributed thresholds to use less memory when
    calculate metrcis like AUC where each individual threshold need to be
    evaluated.

    Args:
      thresholds: A python list or tuple, or 1D numpy array whose value is
        ranged in [0, 1].

    Returns:
      boolean, whether the values in the inputs are evenly distributed.
       Fr=   r	   )atol)lennparangefloat32allcloser   epsilon)r   rN   even_thresholdss      r    is_evenly_distributed_thresholdsr|   :  sU     _NiibjjAO ;;z?9JKKr   c
           
         |r|t        d      | yt        d | D              s-t        dt        t               d| j	                          d      t        | j                               d   j                  }
t        j                  ||
      }t        j                  ||
      }|	r|d   d	k  xs |d
   dkD  }t        j                  ||
      }t        j                  |      d   }|r?t        j                  t        j                  dd      t        |j                              }nt        j                  dd      }| D cg c]  }|t        t              vs| }}|rt        d| dt        t               d      t        ||      \  }}|=t        j                   t        j                  ||
      d
      }t        ||d      \  }}|t#        ||      }|@t        |j                        dk(  rt        d|j                         |d|df   }|d|df   }|	rt%        | ||||||      S d|j                  v rt        j                  |      }|d   }t        |j                        dk(  rd}nAt        j                  t        j&                  t        j                  |dd       d      d      }t        j(                  ||d      }nut        j                  |      }|d   }t        |j                        dk(  rd}n)t        j&                  |dd d      j+                  d      }t        j(                  ||d      }|rBt        j                   |d      }t        j                   t        j                  |d      d      }nEt        j,                  |dd
g      }t        j,                  t        j                  |d      dd
g      }|r|dd
g}d||g}|ddg}n|d
g}d||z  g}|dg}t        j.                  t        j,                  ||      |      }t        j.                  ||      }t        j0                  ||      }t        j.                  ||      }|st        j2                  t        j                  ||j                        t        j                  |            }t        j.                  t        j,                  ||      |      }nd}||st        j                   |d      }t        j2                  |t        j                  |            }t        j.                  t        j,                  ||      |      }||}nt        j4                  ||      }d } t        j6                  ||fi}!t        j8                  | v }"t        j:                  | v }#t        j<                  | v }$|$s|"r*t        j>                  |      }%||%f|!t        j<                  <   |#s|"rAt        j>                  |      }&|&|f|!t        j:                  <   |"r|&%f|!t        j8                  <   |!jA                         D ]  \  }'\  }(})|'| v s | |(|)|| |'           yc c}w )a
  Updates the given confusion matrix variables.

    For every pair of values in y_true and y_pred:

    true_positive: y_true == True and y_pred > thresholds
    false_negatives: y_true == True and y_pred <= thresholds
    true_negatives: y_true == False and y_pred <= thresholds
    false_positive: y_true == False and y_pred > thresholds

    The results will be weighted and added together. When multiple thresholds
    are provided, we will repeat the same for every threshold.

    For estimation of these metrics over a stream of data, the function creates
    an `update_op` operation that updates the given variables.

    If `sample_weight` is `None`, weights default to 1.
    Use weights of 0 to mask values.

    Args:
      variables_to_update: Dictionary with 'tp', 'fn', 'tn', 'fp' as valid keys
        and corresponding variables to update as values.
      y_true: A `Tensor` whose shape matches `y_pred`. Will be cast to `bool`.
      y_pred: A floating point `Tensor` of arbitrary shape and whose values are
        in the range `[0, 1]`.
      thresholds: A float value, float tensor, python list, or tuple of float
        thresholds in `[0, 1]`, or NEG_INF (used when top_k is set).
      top_k: Optional int, indicates that the positive labels should be limited
        to the top k predictions.
      class_id: Optional int, limits the prediction and labels to the class
        specified by this argument.
      sample_weight: Optional `Tensor` whose rank is either 0, or the same rank
        as `y_true`, and must be broadcastable to `y_true` (i.e., all dimensions
        must be either `1`, or the same as the corresponding `y_true`
        dimension).
      multi_label: Optional boolean indicating whether multidimensional
        prediction/labels should be treated as multilabel responses, or
        flattened into a single label. When True, the values of
        `variables_to_update` must have a second dimension equal to the number
        of labels in y_true and y_pred, and those tensors must not be
        RaggedTensors.
      label_weights: (optional) tensor of non-negative weights for multilabel
        data. The weights are applied when calculating TP, FP, FN, and TN
        without explicit multilabel handling (i.e. when the data is to be
        flattened).
      thresholds_distributed_evenly: Boolean, whether the thresholds are evenly
        distributed within the list. An optimized method will be used if this is
        the case. See _update_confusion_matrix_variables_optimized() for more
        details.

    Raises:
      ValueError: If `y_pred` and `y_true` have mismatched shapes, or if
        `sample_weight` is not `None` and its shape doesn't match `y_pred`, or
        if `variables_to_update` contains invalid keys.
    Nz`label_weights` for multilabel data should be handled outside of `update_confusion_matrix_variables` when `multi_label` is True.c              3   D   K   | ]  }|t        t              v s|  y wr   )listr   ).0r+   s     r   	<genexpr>z4update_confusion_matrix_variables.<locals>.<genexpr>  s!      cT/5J.Js     zhPlease provide at least one valid confusion matrix variable to update. Valid variable key options are: "z". Received: ""r   r=   r@   r?   r<   r	   rD   TrC   zInvalid keys: "z$". Valid variable key options are: "rP   F)expand_rank_1ziWhen class_id is provided, y_pred must be a 2D array with shape (num_samples, num_classes), found shape: .)rd   re   rf   rg   c                 
   t        j                  t        j                  | |      |j                        }|$|t        j                  ||j                        z  }|j	                  |t        j
                  |d      z          y )Nr=   r	   )r   rT   logical_andr>   r`   r_   )rL   predrh   varlabel_and_preds        r   weighted_assign_addz>update_confusion_matrix_variables.<locals>.weighted_assign_add4  s\    #//%">ciiPchhwcii@@N

3334r   )!r
   anyr   r   keysvaluesr>   r   rT   convert_to_tensorrR   equalrv   arrayru   r   rV   _filter_top_krq   prodwhereastyperU   tilegreaterrS   rW   r   r    r   r!   logical_notitems)*ra   rb   rc   r   top_kclass_idsample_weightrd   rf   thresholds_distributed_evenlyvariable_dtyperg   rN   
one_threshr+   invalid_keys_
pred_shapenum_predictions
num_labelsthresh_label_tilepredictions_extra_dimlabels_extra_dimthresh_pretile_shapethresh_tiles
data_tilesthresh_tiledpreds_tiledpred_is_poslabel_is_posweights_tiledlabel_weights_tiledr   	loop_vars	update_tn	update_fp	update_fnpred_is_neglabel_is_negmatrix_condrL   r   s*                                             r   !update_confusion_matrix_variablesr   R  s;   D }0%
 	

 " *  _%& '-2245Q8
 	
 -4467:@@NXXfN3FXXfN3F$ #-Q-#"5"MB#9M&&zHJYYz*1-NYYHHQg&
  !


 XXd&1
 +co9N.NL  l^ ,004_0E/FaI
 	

 4FFCNFF HH].9
 :M
= vu-v||!G<<."  Xt+,Xt+,$;#('$;	
 		
 v||YYv&
$Q-v||!J:ab>2;WJ  IIj*a@YYv&
$Q-v||!JABa8??HJHHZQ?  # :??388F&+I1M !$FQG <;;sxxf'E2wO  .26?,=>$a+
 .3?Z78$a(
88J 45|L
 ((0*=K ++k<8K 88,j9L ((HH]&,,769J
 KK|4j
  q9((		&8IJ!hhKK|4j
  /MLL8KLM5 	&&{(CI  ..2EEI//3FFI//3FFIIook26BK5P	/112I|46BK5P	/1129Io445
 '0oo&7 "]eT--t],?,L_s   YYc                     t        j                  | |      \  }}t        j                  t        j                  |t        j                  |       d   d      d      }| |z  t
        d|z
  z  z   S )as  Filters top-k values in the last dim of x and set the rest to NEG_INF.

    Used for computing top-k prediction values in dense labels (which has the
    same shape as predictions) for recall and precision top-k metrics.

    Args:
      x: tensor with any dimensions.
      k: the number of values to keep.

    Returns:
      tensor with same shape and dtype as x.
    r?   rP   r	   )r   r   r_   one_hotrR   NEG_INF)xkr   	top_k_idx
top_k_masks        r   r   r   U  s^     99Q?LAyIsyy|B/b9J z>Gq:~666r   c                    t        j                  | |      } t        j                  ||      }t        | |      \  } }t        j                  ||      }t        j                  | |      } |t        j                  ||      }t        j                  | |gd      }|t        j
                  ||      n|}t        j                  |d      }t        j                  ||      }t        |      }t        j                  ||||f      }|S )a  Computes the confusion matrix from predictions and labels.

    The matrix columns represent the prediction labels and the rows represent
    the real labels. The confusion matrix is always a 2-D array of shape
    `(n, n)`, where `n` is the number of valid labels for a given classification
    task. Both prediction and labels must be 1-D arrays of the same shape in
    order for this function to work.

    If `num_classes` is `None`, then `num_classes` will be set to one plus the
    maximum value in either predictions or labels. Class labels are expected to
    start at 0. For example, if `num_classes` is 3, then the possible labels
    would be `[0, 1, 2]`.

    If `weights` is not `None`, then each prediction contributes its
    corresponding weight to the total value of the confusion matrix cell.

    For example:

    ```python
    keras.metrics.metrics_utils.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
        [[0 0 0 0 0]
        [0 0 1 0 0]
        [0 0 1 0 0]
        [0 0 0 0 0]
        [0 0 0 0 1]]
    ```

    Note that the possible labels are assumed to be `[0, 1, 2, 3, 4]`,
    resulting in a 5x5 confusion matrix.

    Args:
        labels: 1-D tensor of real labels for the classification task.
        predictions: 1-D tensor of predictions for a given classification.
        num_classes: The possible number of labels the classification
            task can have.
        weights: An optional tensor whose shape matches `predictions`.
        dtype: Data type of the confusion matrix.

    Returns:
        A tensor of type `dtype` with shape `(n, n)` representing the confusion
        matrix, where `n` is the number of possible labels in the classification
        task.
    r	   rP   int64r=   )r   r   r   rT   stack	ones_likeintscatter)labelspredictionsnum_classesrh   r>   indicesr   confusion_matrixs           r   r   r   i  s    d ""651F''U;K8MFK((;.KXXfe$F''7ii-A6G29/S]];.wFhhwg.GXXfE*Fk"K{{7F[+4NOr   )g      ?)FNNF)NNNFNF)NrD   )enumr   numpyrv   	keras.srcr   r   keras.src.losses.lossr   keras.src.utils.python_utilsr   r   r   r   r   r$   r0   rq   r|   r   r   r   r"   r   r   <module>r      s        @ 0
	d t & N !Y'xL: "'@F70 
Br   