class_docstr: Computes the weak labels of a multi-label text classification dataset by applying a given list of rules.
  
  Args:
      dataset: Name of the dataset to which the rules will be applied.
      rules: A list of rules (labeling functions). They must return a string, list of strings, or ``None`` in case of
          abstention. If None, we will use the rules of the dataset (Default).
      ids: An optional list of record ids to filter the dataset before applying the rules.
      query: An optional ElasticSearch query with the
          `query string syntax <https://docs.argilla.io/en/latest/practical_guides/filter_dataset.html>`_
          to filter the dataset before applying the rules.
  
  Raises:
      NoRulesFoundError: When you do not provide rules, and the dataset has no rules either.
      DuplicatedRuleNameError: When you provided multiple rules with the same name.
      NoRecordsFoundError: When the filtered dataset is empty.
  
  Examples:
      >>> # Get the 3 dimensional weak label matrix from a multi-label classification dataset with rules:
      >>> weak_labels = WeakMultiLabels(dataset="my_dataset")
      >>> weak_labels.matrix()
      >>> weak_labels.summary()
      >>>
      >>> # Get the 3 dimensional weak label matrix from rules defined in Python:
      >>> def awesome_rule(record: TextClassificationRecord) -> str:
      ...     return ["Positive", "Slang"] if "next level" in record.text else None
      >>> another_rule = Rule(query="amped OR psyched", label=["Positive", "Slang"])
      >>> weak_labels = WeakMultiLabels(dataset="my_dataset", rules=[awesome_rule, another_rule])
      >>> weak_labels.matrix()
      >>> weak_labels.summary()
class_docstr_tok: ["Computes","the","weak","labels","of","a","multi-label","text","classification","dataset","by","applying","a","given","list","of","rules",".","Args",":","dataset",":","Name","of","the","dataset","to","which","the","rules","will","be","applied",".","rules",":","A","list","of","rules","(","labeling","functions",")",".","They","must","return","a","string",",","list","of","strings",",","or","``","None","``","in","case","of","abstention",".","If","None",",","we","will","use","the","rules","of","the","dataset","(","Default",")",".","ids",":","An","optional","list","of","record","ids","to","filter","the","dataset","before","applying","the","rules",".","query",":","An","optional","ElasticSearch","query","with","the","`","query","string","syntax","<","https",":","\/\/docs.argilla.io\/en\/latest\/practical_guides\/filter_dataset.html",">","`","_","to","filter","the","dataset","before","applying","the","rules",".","Raises",":","NoRulesFoundError",":","When","you","do","not","provide","rules",",","and","the","dataset","has","no","rules","either",".","DuplicatedRuleNameError",":","When","you","provided","multiple","rules","with","the","same","name",".","NoRecordsFoundError",":","When","the","filtered","dataset","is","empty",".","Examples",":",">",">",">","#","Get","the","3","dimensional","weak","label","matrix","from","a","multi-label","classification","dataset","with","rules",":",">",">",">","weak_labels","=","WeakMultiLabels","(","dataset=","''","my_dataset","''",")",">",">",">","weak_labels.matrix","(",")",">",">",">","weak_labels.summary","(",")",">",">",">",">",">",">","#","Get","the","3","dimensional","weak","label","matrix","from","rules","defined","in","Python",":",">",">",">","def","awesome_rule","(","record",":","TextClassificationRecord",")","-",">","str",":","...","return","[","``","Positive","''",",","``","Slang","''","]","if","``","next","level","''","in","record.text","else","None",">",">",">","another_rule","=","Rule","(","query=","''","amped","OR","psyched","''",",","label=","[","``","Positive","''",",","``","Slang","''","]",")",">",">",">","weak_labels","=","WeakMultiLabels","(","dataset=","''","my_dataset","''",",","rules=","[","awesome_rule",",","another_rule","]",")",">",">",">","weak_labels.matrix","(",")",">",">",">","weak_labels.summary","(",")"]
docstr: Returns following summary statistics for each rule:
  
  - **label**: Set of unique labels returned by the rule, excluding "None" (abstain).
  - **coverage**: Fraction of the records labeled by the rule.
  - **annotated_coverage**: Fraction of annotated records labeled by the rule (if annotations are available).
  - **overlaps**: Fraction of the records labeled by the rule together with at least one other rule.
  - **correct**: Number of labels the rule predicted correctly (if annotations are available).
  - **incorrect**: Number of labels the rule predicted incorrectly or missed (if annotations are available).
  - **precision**: Fraction of correct labels given by the rule (if annotations are available). The precision does not penalize the rule for abstains.
  
  Args:
      normalize_by_coverage: Normalize the overlaps by the respective coverage.
      annotation: An optional matrix with ints holding the annotations (see ``self.annotation``).
          By default, we will use ``self.annotation(include_missing=True)``.
  
  Returns:
      The summary statistics for each rule in a pandas DataFrame.
docstr_tok: ["Returns","following","summary","statistics","for","each","rule",":","-","*","*","label","*","*",":","Set","of","unique","labels","returned","by","the","rule",",","excluding","``","None","''","(","abstain",")",".","-","*","*","coverage","*","*",":","Fraction","of","the","records","labeled","by","the","rule",".","-","*","*","annotated_coverage","*","*",":","Fraction","of","annotated","records","labeled","by","the","rule","(","if","annotations","are","available",")",".","-","*","*","overlaps","*","*",":","Fraction","of","the","records","labeled","by","the","rule","together","with","at","least","one","other","rule",".","-","*","*","correct","*","*",":","Number","of","labels","the","rule","predicted","correctly","(","if","annotations","are","available",")",".","-","*","*","incorrect","*","*",":","Number","of","labels","the","rule","predicted","incorrectly","or","missed","(","if","annotations","are","available",")",".","-","*","*","precision","*","*",":","Fraction","of","correct","labels","given","by","the","rule","(","if","annotations","are","available",")",".","The","precision","does","not","penalize","the","rule","for","abstains",".","Args",":","normalize_by_coverage",":","Normalize","the","overlaps","by","the","respective","coverage",".","annotation",":","An","optional","matrix","with","ints","holding","the","annotations","(","see","``","self.annotation","``",")",".","By","default",",","we","will","use","``","self.annotation","(","include_missing=True",")","``",".","Returns",":","The","summary","statistics","for","each","rule","in","a","pandas","DataFrame","."]
code: def summary(
      self,
      normalize_by_coverage: bool = False,
      annotation: Optional[np.ndarray] = None,
  ) -> pd.DataFrame:
      """Returns following summary statistics for each rule:
  
      - **label**: Set of unique labels returned by the rule, excluding "None" (abstain).
      - **coverage**: Fraction of the records labeled by the rule.
      - **annotated_coverage**: Fraction of annotated records labeled by the rule (if annotations are available).
      - **overlaps**: Fraction of the records labeled by the rule together with at least one other rule.
      - **correct**: Number of labels the rule predicted correctly (if annotations are available).
      - **incorrect**: Number of labels the rule predicted incorrectly or missed (if annotations are available).
      - **precision**: Fraction of correct labels given by the rule (if annotations are available). The precision does not penalize the rule for abstains.
  
      Args:
          normalize_by_coverage: Normalize the overlaps by the respective coverage.
          annotation: An optional matrix with ints holding the annotations (see ``self.annotation``).
              By default, we will use ``self.annotation(include_missing=True)``.
  
      Returns:
          The summary statistics for each rule in a pandas DataFrame.
      """
      annotation = (
          annotation if annotation is not None else self._annotation
      )
      has_weak_label = self.matrix().sum(2) >= 0
  
      # polarity (label)
      polarity = [
          set(
              [
                  self._labels[i]
                  # get indices of votes
                  for i in np.nonzero(
                      # remove abstentions
                      self.matrix()[:, m, :][
                          self.matrix()[:, m, :].sum(1) >= 0
                      ]
                  )[1]
              ]
          )
          for m in range(len(self._rules))
      ]
      polarity.append(set().union(*polarity))
  
      # coverage
      coverage = has_weak_label.sum(axis=0) / len(self._records)
      coverage = np.append(
          coverage,
          (has_weak_label.sum(axis=1) > 0).sum() / len(self._records),
      )
  
      # overlaps
      has_overlaps = has_weak_label.sum(axis=1) > 1
      overlaps = self._compute_overlaps_conflicts(
          has_weak_label, has_overlaps, coverage, normalize_by_coverage
      )
  
      # index for the summary
      index = list(self._rules_name2index.keys()) + ["total"]
  
      # only add annotated_coverage, correct, incorrect and precision if we have annotations
      has_annotation = annotation.sum(1) >= 0
      if any(has_annotation):
          # annotated coverage
          annotated_coverage = (
              has_weak_label[has_annotation].sum(axis=0)
              / has_annotation.sum()
          )
          annotated_coverage = np.append(
              annotated_coverage,
              (has_weak_label[has_annotation].sum(axis=1) > 0).sum()
              / has_annotation.sum(),
          )
  
          # correct/incorrect
          correct, incorrect = self._compute_correct_incorrect(
              annotation
          )
  
          # precision, ignore division by 0 warnings: we allow np.nan and np.inf
          with np.errstate(divide="ignore", invalid="ignore"):
              precision = correct / (correct + incorrect)
  
          return pd.DataFrame(
              {
                  "label": polarity,
                  "coverage": coverage,
                  "annotated_coverage": annotated_coverage,
                  "overlaps": overlaps,
                  "correct": correct,
                  "incorrect": incorrect,
                  "precision": precision,
              },
              index=index,
          )
  
      return pd.DataFrame(
          {
              "label": polarity,
              "coverage": coverage,
              "overlaps": overlaps,
          },
          index=index,
      )
  
imports: import warnings
  from collections import Counter
  from typing import Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import pandas
  from tqdm.auto import tqdm
  from argilla import load
  from argilla.client.datasets import DatasetForTextClassification
  from argilla.client.models import TextClassificationRecord
  from argilla.labeling.text_classification.rule import Rule, load_rules
label_desc: Use image node_id 7 for calling the WeakMultiLabels obj's underlying member method code with example usage: obj.summary(normalize_by_coverage, annotation) and returns: pd, pd