class_docstr: The Minimal Targeted Counterfactual generator for tabular data.
  
  This generator looks for counterfactuals that are close to the original input
  without using gradients. First, the generator finds counterfactual examples
  from the dataset. Then, it changes features in the original input towards the
  counterfactual input until the decision boundary is found. During the search
  for a closer counterfactual, the algorithm manipulates different subsets of
  features, while keeping other features frozen. Thus, the algorithm tries to
  find the closest counterfactual that differs only in a single feature value,
  two feature values, etc., up to the selected "Maximum number of columns to
  change" configuration parameter. Only scalar and categorical features are
  changed during the search. The features of other types are always assigned the
  values of the original input.
  
  The implementation supports both classification and regression models. In case
  of a regression model, the "Regression threshold" parameter sets a decision
  boundary between the 'true' and 'false' values.
  
  The generator weakly guarantees that the result set of counterfactuals
  is minimal, i.e. for every counterfactual in the set, there exist no other
  counterfactual that differs in the same or smaller set of features and is
  closer to the original input.
class_docstr_tok: ["The","Minimal","Targeted","Counterfactual","generator","for","tabular","data",".","This","generator","looks","for","counterfactuals","that","are","close","to","the","original","input","without","using","gradients",".","First",",","the","generator","finds","counterfactual","examples","from","the","dataset",".","Then",",","it","changes","features","in","the","original","input","towards","the","counterfactual","input","until","the","decision","boundary","is","found",".","During","the","search","for","a","closer","counterfactual",",","the","algorithm","manipulates","different","subsets","of","features",",","while","keeping","other","features","frozen",".","Thus",",","the","algorithm","tries","to","find","the","closest","counterfactual","that","differs","only","in","a","single","feature","value",",","two","feature","values",",","etc.",",","up","to","the","selected","``","Maximum","number","of","columns","to","change","''","configuration","parameter",".","Only","scalar","and","categorical","features","are","changed","during","the","search",".","The","features","of","other","types","are","always","assigned","the","values","of","the","original","input",".","The","implementation","supports","both","classification","and","regression","models",".","In","case","of","a","regression","model",",","the","``","Regression","threshold","''","parameter","sets","a","decision","boundary","between","the","'true","'","and","'false","'","values",".","The","generator","weakly","guarantees","that","the","result","set","of","counterfactuals","is","minimal",",","i.e",".","for","every","counterfactual","in","the","set",",","there","exist","no","other","counterfactual","that","differs","in","the","same","or","smaller","set","of","features","and","is","closer","to","the","original","input","."]
docstr: Calculates L1 distance between two input examples.
  
  Only categorical and scalar example features are considered. For categorical
  features, the distance is calculated as the probability of the feature
  having the same for two random (with replacement) examples. For scalar
  features, the unit of distance is equal to the standard deviation of all
  feature values.
  
  Only features that are in the intersection of the model and dataset features
  are considered.
  
  If a feature value of either of the examples is None, such feature is
  ignored in distance calculation and the name of the feature is not included
  in the result feature list (see Returns description).
  
  Args:
    example_1: a first example to measure distance for.
    example_2: a second example to measure distance for.
    dataset: a dataset that contains the information about the feature types.
    dataset_name: name of the dataset.
    model: a model that contains the information about the input feature
      types.
    field_names: if set then the distance calculation only considers these
      fields.
  
  Returns:
    A tuple that contains the L1 distance and the list of features that were
    used in the distance calculation. The list of features will only contain
docstr_tok: ["Calculates","L1","distance","between","two","input","examples",".","Only","categorical","and","scalar","example","features","are","considered",".","For","categorical","features",",","the","distance","is","calculated","as","the","probability","of","the","feature","having","the","same","for","two","random","(","with","replacement",")","examples",".","For","scalar","features",",","the","unit","of","distance","is","equal","to","the","standard","deviation","of","all","feature","values",".","Only","features","that","are","in","the","intersection","of","the","model","and","dataset","features","are","considered",".","If","a","feature","value","of","either","of","the","examples","is","None",",","such","feature","is","ignored","in","distance","calculation","and","the","name","of","the","feature","is","not","included","in","the","result","feature","list","(","see","Returns","description",")",".","Args",":","example_1",":","a","first","example","to","measure","distance","for",".","example_2",":","a","second","example","to","measure","distance","for",".","dataset",":","a","dataset","that","contains","the","information","about","the","feature","types",".","dataset_name",":","name","of","the","dataset",".","model",":","a","model","that","contains","the","information","about","the","input","feature","types",".","field_names",":","if","set","then","the","distance","calculation","only","considers","these","fields",".","Returns",":","A","tuple","that","contains","the","L1","distance","and","the","list","of","features","that","were","used","in","the","distance","calculation",".","The","list","of","features","will","only","contain"]
code: def _calculate_L1_distance(
      self,
      example_1: JsonDict,
      example_2: JsonDict,
      dataset: lit_dataset.Dataset,
      dataset_name: str,
      model: Optional[lit_model.Model] = None,
      field_names: Optional[list[str]] = None,
  ) -> tuple[float, list[str]]:
      """Calculates L1 distance between two input examples.
  
      Only categorical and scalar example features are considered. For categorical
      features, the distance is calculated as the probability of the feature
      having the same for two random (with replacement) examples. For scalar
      features, the unit of distance is equal to the standard deviation of all
      feature values.
  
      Only features that are in the intersection of the model and dataset features
      are considered.
  
      If a feature value of either of the examples is None, such feature is
      ignored in distance calculation and the name of the feature is not included
      in the result feature list (see Returns description).
  
      Args:
        example_1: a first example to measure distance for.
        example_2: a second example to measure distance for.
        dataset: a dataset that contains the information about the feature types.
        dataset_name: name of the dataset.
        model: a model that contains the information about the input feature
          types.
        field_names: if set then the distance calculation only considers these
          fields.
  
      Returns:
        A tuple that contains the L1 distance and the list of features that were
        used in the distance calculation. The list of features will only contain
      """
      assert model or field_names
      distance = 0
      diff_fields = []
      if field_names is None:
          assert model
          field_names = self._find_all_fields_to_consider(
              ds_spec=dataset.spec(),
              model_input_spec=model.input_spec(),
          )
      for field_name in field_names:
          field_spec = dataset.spec()[field_name]
          field_stats = self._datasets_stats[dataset_name]
          assert self._is_supported(field_spec)
          assert (
              field_name in field_stats
          ), f"{field_name}, {field_stats.keys()}"
          if example_1[field_name] == example_2[field_name]:
              continue
          if (example_1[field_name] is None) or (
              example_2[field_name] is None
          ):
              continue
          diff_fields.append(field_name)
          if self._is_scalar(field_spec):
              std_dev = field_stats[field_name]
              if std_dev != 0:
                  distance += (
                      abs(example_1[field_name] - example_2[field_name])
                      / std_dev
                  )
          else:
              same_prob = field_stats[field_name]
              distance += same_prob
      return distance, diff_fields
  
imports: import collections
  from typing import Any, cast, Optional
  from absl import logging
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.components import cf_utils
  from lit_nlp.lib import caching
  from lit_nlp.lib import utils
  import numpy
label_desc: Use image node_id 12 for calling the TabularMTC obj's underlying member method code with example usage: obj._calculate_L1_distance(example_1, example_2, dataset, dataset_name, model, field_names) and returns: distance,  diff_fields