-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
89 lines (81 loc) · 2.95 KB
/
Copy pathdatasets.py
File metadata and controls
89 lines (81 loc) · 2.95 KB
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
import abc
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelBinarizer
class BenchmarkDataset(abc.ABC):
"""
Abstract base class for benchmark datasets.
All datasets should inherit from this class and implement its abstract methods.
"""
def __init__(self, name):
self.name = name
self.X_train = None
self.y_train = None
self.X_test = None
self.y_test = None
self.label_binarizer = None
self.label_mapping = None
@abc.abstractmethod
def load_data(self):
"""
Loads and preprocesses the dataset.
This method should populate X_train, y_train, X_test, y_test,
and label_mapping.
"""
pass
def get_dataset_info(self):
"""
Returns information about the dataset.
"""
return {
"name": self.name,
"X_train_shape": self.X_train.shape if self.X_train is not None else None,
"y_train_shape": self.y_train.shape if self.y_train is not None else None,
"X_test_shape": self.X_test.shape if self.X_test is not None else None,
"y_test_shape": self.y_test.shape if self.y_test is not None else None,
"num_classes": self.y_train.shape[1] if self.y_train is not None else None,
"label_mapping": self.label_mapping
}
class DatasetRegistry:
"""
A registry for managing different benchmark datasets.
"""
_datasets = {}
@classmethod
def register_dataset(cls, dataset_class):
"""
Decorator to register a dataset class.
"""
if not issubclass(dataset_class, BenchmarkDataset):
raise ValueError("Registered class must inherit from BenchmarkDataset")
cls._datasets[dataset_class.__name__] = dataset_class
return dataset_class
@classmethod
def get_dataset(cls, dataset_name):
"""
Retrieves an instance of a registered dataset.
"""
dataset_class = cls._datasets.get(dataset_name)
if not dataset_class:
raise ValueError(f"Dataset '{dataset_name}' not registered.")
return dataset_class(dataset_name)
@classmethod
def get_available_datasets(cls):
"""
Returns a list of names of all registered datasets.
"""
return list(cls._datasets.keys())
# Example of how a dataset would be implemented and registered (SignMnistDataset will be next)
# @DatasetRegistry.register_dataset
# class ExampleDataset(BenchmarkDataset):
# def __init__(self, name="ExampleDataset"):
# super().__init__(name)
#
# def load_data(self):
# # Dummy data for example
# self.X_train = np.random.rand(100, 28, 28, 1)
# self.y_train = np.eye(100, 10) # 10 classes
# self.X_test = np.random.rand(20, 28, 28, 1)
# self.y_test = np.eye(20, 10)
# self.label_mapping = {i: str(i) for i in range(10)}
# print(f"{self.name} data loaded.")