Dimitri-Sinodinos commited on
Commit
bd99085
·
verified ·
1 Parent(s): 12fb47b

Create multitab.py

Browse files
Files changed (1) hide show
  1. multitab.py +168 -0
multitab.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import h5py
2
+ import datasets
3
+ from typing import List
4
+
5
+ _CITATION = "" # paste your paper bibtex here if you want
6
+ _DESCRIPTION = """
7
+ MultiTab benchmark datasets (HIGGS, ACS Income, AliExpress) preprocessed
8
+ into HDF5 with a multi-task tabular structure: categorical + numerical
9
+ features and multiple labels per sample.
10
+ """
11
+
12
+ _HOMEPAGE = ""
13
+ _LICENSE = ""
14
+
15
+
16
+ class MultitabConfig(datasets.BuilderConfig):
17
+ """Config for one of the MultiTab datasets (e.g. higgs, acs_income, aliexpress)."""
18
+
19
+ def __init__(
20
+ self,
21
+ h5_path: str,
22
+ task_names: List[str],
23
+ **kwargs,
24
+ ):
25
+ """
26
+ Args:
27
+ h5_path: Name of the HDF5 file in the repo (e.g. 'higgs.h5').
28
+ task_names: List of label keys in the HDF5 groups for this dataset
29
+ (e.g. ['Target', 'm_bb', ...] or ['click', 'conversion']).
30
+ """
31
+ super().__init__(**kwargs)
32
+ self.h5_path = h5_path
33
+ self.task_names = task_names
34
+
35
+
36
+ class Multitab(datasets.GeneratorBasedBuilder):
37
+ """MultiTab benchmark datasets hosted under one repo."""
38
+
39
+ BUILDER_CONFIG_CLASS = MultitabConfig
40
+ VERSION = datasets.Version("1.0.0")
41
+
42
+ BUILDER_CONFIGS = [
43
+ MultitabConfig(
44
+ name="higgs",
45
+ description="HIGGS dataset preprocessed for MultiTab.",
46
+ h5_path="higgs.h5",
47
+ task_names=[
48
+ "Target",
49
+ "m_bb",
50
+ "m_jj",
51
+ "m_jjj",
52
+ "m_jlv",
53
+ "m_lv",
54
+ "m_wbb",
55
+ "m_wwbb",
56
+ ],
57
+ ),
58
+ MultitabConfig(
59
+ name="acs_income",
60
+ description="ACS Income dataset preprocessed for MultiTab.",
61
+ h5_path="acs_incom.h5", # matches the upload name
62
+ task_names=[
63
+ "MAR",
64
+ "PINCP",
65
+ ],
66
+ ),
67
+ MultitabConfig(
68
+ name="aliexpress",
69
+ description="AliExpress recommendation dataset preprocessed for MultiTab.",
70
+ h5_path="aliexpress.h5",
71
+ task_names=[
72
+ "click",
73
+ "conversion",
74
+ ],
75
+ ),
76
+ ]
77
+
78
+ def _info(self) -> datasets.DatasetInfo:
79
+ """
80
+ Schema for each example.
81
+
82
+ We keep features as list fields; HF Dataset Viewer will show them as arrays per cell.
83
+ Both 'features_categorical' and 'features_numerical' exist in the schema; if a given
84
+ dataset/split doesn't have one of them, we return an empty list.
85
+ """
86
+ features_dict = {
87
+ "features_categorical": datasets.Sequence(datasets.Value("int64")),
88
+ "features_numerical": datasets.Sequence(datasets.Value("float32")),
89
+ }
90
+
91
+ # One column per task label (float32 for generality; ints are cast)
92
+ for t in self.config.task_names:
93
+ features_dict[t] = datasets.Value("float32")
94
+
95
+ return datasets.DatasetInfo(
96
+ description=_DESCRIPTION,
97
+ features=datasets.Features(features_dict),
98
+ supervised_keys=None,
99
+ homepage=_HOMEPAGE,
100
+ license=_LICENSE,
101
+ citation=_CITATION,
102
+ )
103
+
104
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
105
+ """
106
+ Map HF splits to the 'train', 'val', 'test' groups in your HDF5 file.
107
+ """
108
+ # Download/copy the HDF5 from the repo to the local cache
109
+ h5_path = dl_manager.download(self.config.h5_path)
110
+
111
+ return [
112
+ datasets.SplitGenerator(
113
+ name=datasets.Split.TRAIN,
114
+ gen_kwargs={"filepath": h5_path, "split_name": "train"},
115
+ ),
116
+ datasets.SplitGenerator(
117
+ name=datasets.Split.VALIDATION,
118
+ gen_kwargs={"filepath": h5_path, "split_name": "val"},
119
+ ),
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TEST,
122
+ gen_kwargs={"filepath": h5_path, "split_name": "test"},
123
+ ),
124
+ ]
125
+
126
+ def _generate_examples(self, filepath: str, split_name: str):
127
+ """
128
+ Iterate over one split in the HDF5 file and yield examples.
129
+
130
+ Expected structure per HDF5 group (train/val/test):
131
+ - features_categorical: (N, C) [optional]
132
+ - features_numerical: (N, D) [optional]
133
+ - one array per task in self.config.task_names, shape (N,)
134
+ """
135
+
136
+ cfg = self.config
137
+
138
+ with h5py.File(filepath, "r") as f:
139
+ grp = f[split_name]
140
+
141
+ has_cat = "features_categorical" in grp
142
+ has_num = "features_numerical" in grp
143
+
144
+ first_task = cfg.task_names[0]
145
+ n_samples = grp[first_task].shape[0]
146
+
147
+ for idx in range(n_samples):
148
+ example = {}
149
+
150
+ # Categorical features
151
+ if has_cat:
152
+ cat_row = grp["features_categorical"][idx]
153
+ example["features_categorical"] = cat_row.astype("int64").tolist()
154
+ else:
155
+ example["features_categorical"] = []
156
+
157
+ # Numerical features
158
+ if has_num:
159
+ num_row = grp["features_numerical"][idx]
160
+ example["features_numerical"] = num_row.astype("float32").tolist()
161
+ else:
162
+ example["features_numerical"] = []
163
+
164
+ # Task labels
165
+ for t in cfg.task_names:
166
+ example[t] = float(grp[t][idx])
167
+
168
+ yield idx, example