File size: 4,277 Bytes
587928d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed1544c
587928d
 
 
 
 
 
 
 
 
6acab22
 
ed1544c
 
6acab22
ed1544c
 
6acab22
 
 
 
 
 
587928d
 
 
 
 
 
 
 
 
 
 
 
ce3212c
8e5a7ff
587928d
 
 
4fe9551
 
 
587928d
 
 
4fe9551
587928d
 
 
 
 
 
d9abb83
587928d
 
 
 
 
 
 
 
ed1544c
 
 
 
 
 
 
 
 
 
 
 
587928d
 
 
 
 
ed1544c
587928d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import pickle
import numpy as np
import datasets

# _CITATION = """\
# @inproceedings{your_citation,
#   title={Your Dataset Title},
#   author={Your Name},
#   year={2025},
#   booktitle={Conference or Journal Name}
# }
# """

# _DESCRIPTION = """\
# This dataset contains the entire content of three files loaded as a single example:
# - `languages_list.pkl`: A pickled list of language strings.
# - `average_distances_matrix.npy`: A NumPy matrix converted to a list of lists of floats.
# - `distances_matrices.pkl`: A pickled dict of dicts of NumPy matrices.  
#    It is converted into a list of records where each record corresponds to a dataset with a nested list of models and their associated distance matrices.
# """

# _LICENSE = "Specify your license here."

class DistancesDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        # Define features for the whole dataset as a single example.
        features = datasets.Features({
            "languages_list": datasets.Sequence(datasets.Value("string")),
            "average_distances_matrix": datasets.Sequence(
                datasets.Sequence(datasets.Value("float"))
            ),
            "distances_matrices": datasets.Sequence(
                datasets.Features({
                    "dataset_name": datasets.Value("string"),
                    "models": datasets.Sequence(
                        datasets.Features({
                            "model_name": datasets.Value("string"),
                            "matrix": datasets.Sequence(
                                datasets.Sequence(datasets.Value("float"))
                            )
                        })
                    )
                })
            )
        })
        return datasets.DatasetInfo(
            # description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage="",
            # license=_LICENSE,
            # citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # All files are expected in the data_dir (e.g. repository root)
        # data_dir = self.config.data_dir if hasattr(self.config, "data_dir") else ""
        data_dir = dl_manager.download_and_extract(["languages_list.pkl", "average_distances_matrix.npy", "distances_matrices.pkl"])
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"languages_list_path": data_dir[0],
                            "avg_matrix_path": data_dir[1],
                            "distances_matrices_path": data_dir[2]},
            )
        ]

    def _generate_examples(self, languages_list_path, avg_matrix_path, distances_matrices_path):
        # Load languages_list.pkl (list of strings)
        with open(languages_list_path, "rb") as f:
            languages_list = pickle.load(f)

        # Load average_distances_matrix.npy (NumPy matrix converted to list of lists)
        average_distances_matrix = np.load(avg_matrix_path, allow_pickle=True)
        # average_distances_matrix = average_distances_matrix.tolist()

        # Load distances_matrices.pkl (dict of dicts of NumPy matrices)
        with open(distances_matrices_path, "rb") as f:
            distances_matrices = pickle.load(f)

        # Convert the nested dict structure into a list of dictionaries.
        # Each outer key corresponds to a dataset name.
        # Each inner dict is a mapping from model name to a NumPy matrix.
        distances_matrices_list = []
        for dataset_name, models_dict in distances_matrices.items():
            models_list = []
            for model_name, matrix in models_dict.items():
                models_list.append({
                    "model_name": model_name,
                    "matrix": matrix
                })
            distances_matrices_list.append({
                "dataset_name": dataset_name,
                "models": models_list
            })

        # Yield a single example containing the whole dataset.
        yield 0, {
            "languages_list": languages_list,
            "average_distances_matrix": average_distances_matrix,
            "distances_matrices": distances_matrices_list
        }