File size: 2,804 Bytes
b33ee2f
 
 
 
8c8ab30
 
 
67508ed
b33ee2f
d8c1348
 
67508ed
b33ee2f
 
 
890f548
0a2304d
 
 
b33ee2f
 
d8c1348
 
 
 
 
59d0a04
b33ee2f
 
4a70941
59d0a04
b33ee2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import datasets
import numpy as np

_DESCRIPTION = """The dataset consists of tuples of (observations, actions, rewards, dones) sampled by agents
    interacting with the CityLearn 2022 Phase 1 environment"""

_BASE_URL = "https://huggingface.co/datasets/TobiTob/CityLearn/resolve/main"
_URLS = {
    "s_test": f"{_BASE_URL}/s_test.pkl",
    "s_8759x5": f"{_BASE_URL}/s_8759x5.pkl",
    "test": f"{_BASE_URL}/test.pkl",
}


class DecisionTransformerCityLearnDataset(datasets.GeneratorBasedBuilder):
    
    # You will be able to load one configuration in the following list with
    # data = datasets.load_dataset('TobiTob/CityLearn', 'data_name')
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="s_test",
            description="Test Data sampled from an expert policy in CityLearn environment",
        ),
        datasets.BuilderConfig(
            name="s_8759x5",
            description="Test Data sampled from an expert policy in CityLearn environment",
        ),
        datasets.BuilderConfig(
            name="halfcheetah-medium-replay-v2",
            description="Test Data sampled from an expert policy in CityLearn environment",
        ),
    ]

    def _info(self):

        features = datasets.Features(
            {
                "observations": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
                "actions": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
                "rewards": datasets.Sequence(datasets.Value("float32")),
                "dones": datasets.Sequence(datasets.Value("bool")),
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
        )

    def _split_generators(self, dl_manager):
        urls = _URLS[self.config.name]
        data_dir = dl_manager.download_and_extract(urls)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": data_dir,
                    "split": "train",
                },
            )
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, split):
        with open(filepath, "rb") as f:
            trajectories = pickle.load(f)

            for idx, traj in enumerate(trajectories):
                yield idx, {
                    "observations": traj["observations"],
                    "actions": traj["actions"],
                    "rewards": np.expand_dims(traj["rewards"], axis=1),
                    "dones": np.expand_dims(traj.get("dones", traj.get("terminals")), axis=1),
                }