class_docstr: An index structure for filtering (roughly following CSR).
class_docstr_tok: ["An","index","structure","for","filtering","(","roughly","following","CSR",")","."]
docstr: Create index from dataframe.
  
  :param df:
      the dataframe, comprising columns [LABEL_HEAD, LABEL_RELATION, LABEL_TAIL]
  :param target:
      the prediction target
  
  :raises ValueError:
      if some of the expected columns are missing
  
  :return:
      a filter index object
code: def from_df(
      cls, df: pandas.DataFrame, target: Target
  ) -> "FilterIndex":
      """
      Create index from dataframe.
  
      :param df:
          the dataframe, comprising columns [LABEL_HEAD, LABEL_RELATION, LABEL_TAIL]
      :param target:
          the prediction target
  
      :raises ValueError:
          if some of the expected columns are missing
  
      :return:
          a filter index object
      """
      # input verification
      expected_columns = set(COLUMN_LABELS)
      if not expected_columns.issubset(df.columns):
          raise ValueError(
              f"Missing columns: {sorted(expected_columns.difference(df.columns))}"
          )
  
      # group key = everything except the prediction target
      key = [c for c in df.columns if c != target]
      # initialize data structure
      triple_id_to_key_id = numpy.empty_like(df.index)
      indices = []
      bounds = [0]
      # group by key
      for key_id, (_, group) in enumerate(df.groupby(by=key)):
          unique_targets = group[target].unique()
          triple_id_to_key_id[group.index] = key_id
          indices.extend(unique_targets)
          bounds.append(len(indices))
      # convert lists to arrays
      indices = cast(torch.LongTensor, torch.as_tensor(indices))
      bounds = numpy.asarray(bounds)
      # instantiate
      return cls(
          triple_id_to_key_id=triple_id_to_key_id,
          bounds=bounds,
          indices=indices,
      )
  
imports: import dataclasses
  import logging
  from abc import abstractmethod
  from collections import defaultdict
  from typing import Any, Collection, DefaultDict, Generic, Iterable, List, Mapping, Optional, Tuple, TypeVar, Union, cast
  import numpy
  import pandas
  import torch
  from class_resolver import HintOrType, OptionalKwargs
  from torch.utils.data import Dataset
  from torch.utils.data.dataloader import DataLoader
  from torch_max_mem import maximize_memory_utilization
  from tqdm.auto import tqdm
  from typing_extensions import TypeAlias
  from .evaluator import Evaluator, MetricResults, filter_scores_
  from ..constants import COLUMN_LABELS, TARGET_TO_INDEX
  from ..models import Model
  from ..triples import CoreTriplesFactory, get_mapped_triples
  from ..typing import LABEL_HEAD, LABEL_TAIL, InductiveMode, MappedTriples, OneOrSequence, Target
  from ..utils import upgrade_to_sequence
label_desc: Use image node_id 1 for calling the FilterIndex obj's underlying member method code with example usage: obj.from_df(cls, df, target) and returns: cls