class_docstr: Fast generalized subset scan based detector by McFowland, E., Speakman, S., and Neill, D. B. (2013).
  
  | Paper link: https://www.cs.cmu.edu/~neill/papers/mcfowland13a.pdf
class_docstr_tok: ["Fast","generalized","subset","scan","based","detector","by","McFowland",",","E.",",","Speakman",",","S.",",","and","Neill",",","D.","B",".","(","2013",")",".","|","Paper","link",":","https",":","\/\/www.cs.cmu.edu\/~neill\/papers\/mcfowland13a.pdf"]
docstr: Create a `SubsetScanningDetector` instance which is used to the detect the presence of adversarial samples.
  
  :param classifier: The model being evaluated for its robustness to anomalies (e.g. adversarial samples).
  :param bgd_data: The background data used to learn a null model. Typically dataset used to train the classifier.
  :param layer: The layer from which to extract activations to perform scan.
  :param verbose: Show progress bars.
docstr_tok: ["Create","a","`","SubsetScanningDetector","`","instance","which","is","used","to","the","detect","the","presence","of","adversarial","samples",".",":","param","classifier",":","The","model","being","evaluated","for","its","robustness","to","anomalies","(","e.g",".","adversarial","samples",")",".",":","param","bgd_data",":","The","background","data","used","to","learn","a","null","model",".","Typically","dataset","used","to","train","the","classifier",".",":","param","layer",":","The","layer","from","which","to","extract","activations","to","perform","scan",".",":","param","verbose",":","Show","progress","bars","."]
code: def __init__(
      self,
      classifier: "CLASSIFIER_NEURALNETWORK_TYPE",
      bgd_data: np.ndarray,
      layer: Union[int, str],
      scoring_function: Literal[
          "BerkJones", "HigherCriticism", "KolmarovSmirnov"
      ] = "BerkJones",
      verbose: bool = True,
  ) -> None:
      """
      Create a `SubsetScanningDetector` instance which is used to the detect the presence of adversarial samples.
  
      :param classifier: The model being evaluated for its robustness to anomalies (e.g. adversarial samples).
      :param bgd_data: The background data used to learn a null model. Typically dataset used to train the classifier.
      :param layer: The layer from which to extract activations to perform scan.
      :param verbose: Show progress bars.
      """
      super().__init__()
      self.classifier = classifier
      self.bgd_data = bgd_data
      self.layer = layer
      self.verbose = verbose
  
      if scoring_function == "BerkJones":
          self.scoring_function = ScoringFunctions.get_score_bj_fast
      elif scoring_function == "HigherCriticism":
          self.scoring_function = ScoringFunctions.get_score_hc_fast
      elif scoring_function == "KolmarovSmirnov":
          self.scoring_function = ScoringFunctions.get_score_ks_fast
      else:
          raise ValueError(
              "The provided scoring function is not supported:",
              scoring_function,
          )
  
      # Ensure that layer is well-defined
      if classifier.layer_names is None:
          raise ValueError("No layer names identified.")
  
      if isinstance(layer, int):
          if layer < 0 or layer >= len(classifier.layer_names):
              raise ValueError(
                  f"Layer index {layer} is outside of range (0 to {len(classifier.layer_names) - 1} included)."
              )
          self._layer_name = classifier.layer_names[layer]
      else:
          if layer not in classifier.layer_names:
              raise ValueError(
                  f"Layer name {layer} is not part of the graph."
              )
          self._layer_name = layer
  
      # Background data activations
      bgd_activations = self._get_activations(
          bgd_data, self._layer_name, batch_size=128
      )
      if len(bgd_activations.shape) == 4:
          dim2 = (
              bgd_activations.shape[1]
              * bgd_activations.shape[2]
              * bgd_activations.shape[3]
          )
          bgd_activations = np.reshape(
              bgd_activations, (bgd_activations.shape[0], dim2)
          )
      self.sorted_bgd_activations = np.sort(bgd_activations, axis=0)
  
      # Background data scores
      pval_ranges = self._calculate_pvalue_ranges(bgd_data)
      bgd_scores = []
      for pval_range in pval_ranges:
          best_score, _, _, _ = Scanner.fgss_individ_for_nets(
              pval_range, score_function=self.scoring_function
          )
          bgd_scores.append(best_score)
      self.bgd_scores = np.asarray(bgd_scores)
  
file_path: adversarial-robustness-toolbox/art/defences/detector/evasion/subsetscanning/detector.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import sys
  from typing import Optional, Tuple, Union, TYPE_CHECKING
  import numpy
  from sklearn import metrics
  from tqdm.auto import trange, tqdm
  from art.defences.detector.evasion.evasion_detector import EvasionDetector
  from art.defences.detector.evasion.subsetscanning.scanner import Scanner
  from art.defences.detector.evasion.subsetscanning.scoring_functions import ScoringFunctions
label_desc: Use image node_id 1 to create a new SubsetScanningDetector object from inherited base classes: EvasionDetector with example: obj = SubsetScanningDetector(classifier, bgd_data, layer, scoring_function, verbose)