peal.data.datasets.SymbolicDataset

class peal.data.datasets.SymbolicDataset[source]

Bases: PealDataset

Tabular (symbolic) dataset read from <dataset_path>/data.csv.

Every row becomes a float tensor in data[key]; by default the last column is the target and all others are inputs, unless task_config selects columns via x_selection/y_selection. A pandas copy of the table is kept in df (used by DiCEExplainer). The config’s tabular_preprocessing list may contain "minmax_-1_1", which scales continuous columns (more than 10 unique values, excluding targets and confounding_factors) to [-1, 1].

Parameters:
  • mode (str) – Split to load: "train", "val", "test" or "all".

  • config (DataConfig) – Data config; uses dataset_path, set_negative_to_zero, output_size, confounding_factors and tabular_preprocessing.

  • transform (callable, optional) – Stored but not applied to the tensors. Defaults to ToTensor().

  • task_config (TaskConfig, optional) – Column selection and output_channels.

  • **kwargs – Ignored.

Variables:
  • attributes (list of str) – Column names.

  • keys (list) – Row identifiers of this split.

  • df (pandas.DataFrame) – The (possibly preprocessed) table.

Methods

disable_groups

Stop returning has_confounder.

disable_hints

Clear the hints_enabled flag.

disable_idx

Stop returning the sample index.

enable_groups

Return has_confounder in dict mode (second confounding factor).

enable_hints

Set the hints_enabled flag (tabular data has no hints to return).

enable_idx

Also return the sample index from __getitem__.

generate_contrastive_collage

Render bar-chart collages of factual vs.

serialize_dataset

Write samples as a CSV next to output_dir.

Attributes

output_size

task_config.output_channels when targets are selected, else config.

__init__(mode, config, transform=torchvision.transforms.ToTensor, task_config=None, **kwargs)[source]

Parse data.csv, build the dataframe and apply tabular preprocessing.

property output_size

task_config.output_channels when targets are selected, else config.

enable_hints()[source]

Set the hints_enabled flag (tabular data has no hints to return).

disable_hints()[source]

Clear the hints_enabled flag.

enable_groups()[source]

Return has_confounder in dict mode (second confounding factor).

disable_groups()[source]

Stop returning has_confounder.

enable_idx()[source]

Also return the sample index from __getitem__.

disable_idx()[source]

Stop returning the sample index.

generate_contrastive_collage(x_list, x_counterfactual_list, y_target_list, y_source_list, y_list, y_target_start_confidence_list, y_target_end_confidence_list, base_path, start_idx=0, y_original_teacher_list=None, y_counterfactual_teacher_list=None, feedback_list=None, **kwargs)[source]

Render bar-chart collages of factual vs. counterfactual feature vectors.

For every pair a figure with three bar charts (original, counterfactual, difference) over the feature names is written to <base_path>/<zero-padded index>_collage.png.

Parameters:
  • x_list (list of torch.Tensor) – Factual and counterfactual feature vectors.

  • x_counterfactual_list (list of torch.Tensor) – Factual and counterfactual feature vectors.

  • y_target_list (list) – Target class, predicted source class and ground truth per pair.

  • y_source_list (list) – Target class, predicted source class and ground truth per pair.

  • y_list (list) – Target class, predicted source class and ground truth per pair.

  • y_target_start_confidence_list (list) – Target-class confidence before and after the edit.

  • y_target_end_confidence_list (list) – Target-class confidence before and after the edit.

  • base_path (str) – Output directory, created if missing.

  • start_idx (int, optional) – Offset added to the running index in the filenames.

  • y_original_teacher_list – Accepted for API compatibility; not drawn for tabular data.

  • y_counterfactual_teacher_list – Accepted for API compatibility; not drawn for tabular data.

  • feedback_list – Accepted for API compatibility; not drawn for tabular data.

  • **kwargs – Ignored.

Returns:

(attribution_list, collage_paths) where attributions are |cf - x| tensors and paths are the written PNG files.

Return type:

tuple

serialize_dataset(output_dir, x_list, y_list, sample_names=None, hint_list=[], classifier=None)[source]

Write samples as a CSV next to output_dir.

The file <output_dir>.csv gets one row per sample with the feature columns (task_config.x_selection or all but the last attribute) followed by the target column(s). Nothing is written for an empty x_list.

Parameters:
  • output_dir (str) – Directory that is created; the CSV is named output_dir + ".csv".

  • x_list (list of torch.Tensor) – Feature vectors.

  • y_list (list) – Scalar targets.

  • sample_names – Accepted for API compatibility; unused for tabular data.

  • hint_list – Accepted for API compatibility; unused for tabular data.

  • classifier – Accepted for API compatibility; unused for tabular data.