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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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import numpy as np


class DatasetNormalizer:
    """
    Overview:
        The `DatasetNormalizer` class provides functionality to normalize and unnormalize data in a dataset.
        It takes a dataset as input and applies a normalizer function to each key in the dataset.

    Interfaces:
        ``__init__``, ``__repr__``, ``normalize``, ``unnormalize``.
    """

    def __init__(self, dataset: np.ndarray, normalizer: str, path_lengths: list = None):
        """
        Overview:
            Initialize the NormalizerHelper object.

        Arguments:
            - dataset (:obj:`np.ndarray`): The dataset to be normalized.
            - normalizer (:obj:`str`): The type of normalizer to be used. Can be a string representing the name of \
                the normalizer class.
            - path_lengths (:obj:`list`): The length of the paths in the dataset. Defaults to None.
        """
        dataset = flatten(dataset, path_lengths)

        self.observation_dim = dataset['observations'].shape[1]
        self.action_dim = dataset['actions'].shape[1]

        if isinstance(normalizer, str):
            normalizer = eval(normalizer)

        self.normalizers = {}
        for key, val in dataset.items():
            try:
                self.normalizers[key] = normalizer(val)
            except:
                print(f'[ utils/normalization ] Skipping {key} | {normalizer}')
            # key: normalizer(val)
            # for key, val in dataset.items()

    def __repr__(self) -> str:
        """
        Overview:
            Returns a string representation of the NormalizerHelper object. \
            The string representation includes the key-value pairs of the normalizers \
            stored in the NormalizerHelper object.
        Returns:
            - ret (:obj:`str`):A string representation of the NormalizerHelper object.
        """
        string = ''
        for key, normalizer in self.normalizers.items():
            string += f'{key}: {normalizer}]\n'
        return string

    def normalize(self, x: np.ndarray, key: str) -> np.ndarray:
        """
        Overview:
            Normalize the input data using the specified key.

        Arguments:
            - x (:obj:`np.ndarray`): The input data to be normalized.
            - key (:obj`str`): The key to identify the normalizer.

        Returns:
            - ret (:obj:`np.ndarray`): The normalized value of the input data.
        """
        return self.normalizers[key].normalize(x)

    def unnormalize(self, x: np.ndarray, key: str) -> np.ndarray:
        """
        Overview:
            Unnormalizes the given value `x` using the specified `key`.

        Arguments:
            - x (:obj:`np.ndarray`): The value to be unnormalized.
            - key (:obj`str`): The key to identify the normalizer.

        Returns:
            - ret (:obj:`np.ndarray`): The unnormalized value.
        """
        return self.normalizers[key].unnormalize(x)


def flatten(dataset: dict, path_lengths: list) -> dict:
    """
    Overview:
        Flattens dataset of { key: [ n_episodes x max_path_length x dim ] } \
        to { key : [ (n_episodes * sum(path_lengths)) x dim ] }

    Arguments:
        - dataset (:obj:`dict`): The dataset to be flattened.
        - path_lengths (:obj:`list`): A list of path lengths for each episode.

    Returns:
        - flattened (:obj:`dict`): The flattened dataset.
    """
    flattened = {}
    for key, xs in dataset.items():
        assert len(xs) == len(path_lengths)
        if key == 'path_lengths':
            continue
        flattened[key] = np.concatenate([x[:length] for x, length in zip(xs, path_lengths)], axis=0)
    return flattened


class Normalizer:
    """
    Overview:
        Parent class, subclass by defining the `normalize` and `unnormalize` methods

    Interfaces:
        ``__init__``, ``__repr__``, ``normalize``, ``unnormalize``.
    """

    def __init__(self, X):
        """
        Overview:
            Initialize the Normalizer object.
        Arguments:
            - X (:obj:`np.ndarray`): The data to be normalized.
        """

        self.X = X.astype(np.float32)
        self.mins = X.min(axis=0)
        self.maxs = X.max(axis=0)

    def __repr__(self) -> str:
        """
        Overview:
            Returns a string representation of the Normalizer object.
        Returns:
            - ret (:obj:`str`): A string representation of the Normalizer object.
        """

        return (
            f"""[ Normalizer ] dim: {self.mins.size}\n    -: """
            f"""{np.round(self.mins, 2)}\n    +: {np.round(self.maxs, 2)}\n"""
        )

    def normalize(self, *args, **kwargs):
        """
        Overview:
            Normalize the input data.
        Arguments:
            - args (:obj:`list`): The arguments passed to the ``normalize`` function.
            - kwargs (:obj:`dict`): The keyword arguments passed to the ``normalize`` function.
        """

        raise NotImplementedError()

    def unnormalize(self, *args, **kwargs):
        """
        Overview:
            Unnormalize the input data.
        Arguments:
            - args (:obj:`list`): The arguments passed to the ``unnormalize`` function.
            - kwargs (:obj:`dict`): The keyword arguments passed to the ``unnormalize`` function.
        """

        raise NotImplementedError()


class GaussianNormalizer(Normalizer):
    """
    Overview:
        A class that normalizes data to zero mean and unit variance.

    Interfaces:
        ``__init__``, ``__repr__``, ``normalize``, ``unnormalize``.
    """

    def __init__(self, *args, **kwargs):
        """
        Overview:
            Initialize the GaussianNormalizer object.
        Arguments:
            - args (:obj:`list`): The arguments passed to the ``__init__`` function of the parent class, \
                i.e., the Normalizer class.
            - kwargs (:obj:`dict`): The keyword arguments passed to the ``__init__`` function of the parent class, \
                i.e., the Normalizer class.
        """

        super().__init__(*args, **kwargs)
        self.means = self.X.mean(axis=0)
        self.stds = self.X.std(axis=0)
        self.z = 1

    def __repr__(self) -> str:
        """
        Overview:
            Returns a string representation of the GaussianNormalizer object.
        Returns:
            - ret (:obj:`str`): A string representation of the GaussianNormalizer object.
        """

        return (
            f"""[ Normalizer ] dim: {self.mins.size}\n    """
            f"""means: {np.round(self.means, 2)}\n    """
            f"""stds: {np.round(self.z * self.stds, 2)}\n"""
        )

    def normalize(self, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Normalize the input data.

        Arguments:
            - x (:obj:`np.ndarray`): The input data to be normalized.

        Returns:
            - ret (:obj:`np.ndarray`): The normalized data.
        """
        return (x - self.means) / self.stds

    def unnormalize(self, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Unnormalize the input data.

        Arguments:
            - x (:obj:`np.ndarray`): The input data to be unnormalized.

        Returns:
            - ret (:obj:`np.ndarray`): The unnormalized data.
        """
        return x * self.stds + self.means


class CDFNormalizer(Normalizer):
    """
    Overview:
        A class that makes training data uniform (over each dimension) by transforming it with marginal CDFs.

    Interfaces:
        ``__init__``, ``__repr__``, ``normalize``, ``unnormalize``.
    """

    def __init__(self, X):
        """
        Overview:
            Initialize the CDFNormalizer object.
        Arguments:
            - X (:obj:`np.ndarray`): The data to be normalized.
        """

        super().__init__(atleast_2d(X))
        self.dim = self.X.shape[1]
        self.cdfs = [CDFNormalizer1d(self.X[:, i]) for i in range(self.dim)]

    def __repr__(self) -> str:
        """
        Overview:
            Returns a string representation of the CDFNormalizer object.
        Returns:
            - ret (:obj:`str`): A string representation of the CDFNormalizer object.
        """

        return f'[ CDFNormalizer ] dim: {self.mins.size}\n' + '    |    '.join(
            f'{i:3d}: {cdf}' for i, cdf in enumerate(self.cdfs)
        )

    def wrap(self, fn_name: str, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Wraps the given function name and applies it to the input data.

        Arguments:
            - fn_name (:obj:`str`): The name of the function to be applied.
            - x (:obj:`np.ndarray`): The input data.

        Returns:
            - ret: The output of the function applied to the input data.
        """
        shape = x.shape
        # reshape to 2d
        x = x.reshape(-1, self.dim)
        out = np.zeros_like(x)
        for i, cdf in enumerate(self.cdfs):
            fn = getattr(cdf, fn_name)
            out[:, i] = fn(x[:, i])
        return out.reshape(shape)

    def normalize(self, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Normalizes the input data.

        Arguments:
            - x (:obj:`np.ndarray`): The input data.

        Returns:
            - ret (:obj:`np.ndarray`): The normalized data.
        """
        return self.wrap('normalize', x)

    def unnormalize(self, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Unnormalizes the input data.

        Arguments:
            - x (:obj:`np.ndarray`): The input data.

        Returns:
            - ret (:obj:`np.ndarray`):: The unnormalized data.
        """
        return self.wrap('unnormalize', x)


class CDFNormalizer1d:
    """
    Overview:
        CDF normalizer for a single dimension. This class provides methods to normalize and unnormalize data \
        using the Cumulative Distribution Function (CDF) approach.
    Interfaces:
        ``__init__``, ``__repr__``, ``normalize``, ``unnormalize``.
    """

    def __init__(self, X: np.ndarray):
        """
        Overview:
            Initialize the CDFNormalizer1d object.
        Arguments:
            - X (:obj:`np.ndarray`): The data to be normalized.
        """

        import scipy.interpolate as interpolate
        assert X.ndim == 1
        self.X = X.astype(np.float32)
        if self.X.max() == self.X.min():
            self.constant = True
        else:
            self.constant = False
            quantiles, cumprob = empirical_cdf(self.X)
            self.fn = interpolate.interp1d(quantiles, cumprob)
            self.inv = interpolate.interp1d(cumprob, quantiles)

            self.xmin, self.xmax = quantiles.min(), quantiles.max()
            self.ymin, self.ymax = cumprob.min(), cumprob.max()

    def __repr__(self) -> str:
        """
        Overview:
            Returns a string representation of the CDFNormalizer1d object.
        """

        return (f'[{np.round(self.xmin, 2):.4f}, {np.round(self.xmax, 2):.4f}')

    def normalize(self, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Normalize the input data.

        Arguments:
            - x (:obj:`np.ndarray`): The data to be normalized.

        Returns:
            - ret (:obj:`np.ndarray`): The normalized data.
        """
        if self.constant:
            return x

        x = np.clip(x, self.xmin, self.xmax)
        # [ 0, 1 ]
        y = self.fn(x)
        # [ -1, 1 ]
        y = 2 * y - 1
        return y

    def unnormalize(self, x: np.ndarray, eps: float = 1e-4) -> np.ndarray:
        """
        Overview:
            Unnormalize the input data.

        Arguments:
            - x (:obj:`np.ndarray`): The data to be unnormalized.
            - eps (:obj:`float`): A small value used for numerical stability. Defaults to 1e-4.

        Returns:
            - ret (:obj:`np.ndarray`): The unnormalized data.
        """
        # [ -1, 1 ] --> [ 0, 1 ]
        if self.constant:
            return x

        x = (x + 1) / 2.

        if (x < self.ymin - eps).any() or (x > self.ymax + eps).any():
            print(
                f"""[ dataset/normalization ] Warning: out of range in unnormalize: """
                f"""[{x.min()}, {x.max()}] | """
                f"""x : [{self.xmin}, {self.xmax}] | """
                f"""y: [{self.ymin}, {self.ymax}]"""
            )

        x = np.clip(x, self.ymin, self.ymax)

        y = self.inv(x)
        return y


def empirical_cdf(sample: np.ndarray) -> (np.ndarray, np.ndarray):
    """
    Overview:
        Compute the empirical cumulative distribution function (CDF) of a given sample.

    Arguments:
        - sample (:obj:`np.ndarray`): The input sample for which to compute the empirical CDF.

    Returns:
        - quantiles (:obj:`np.ndarray`): The unique values in the sample.
        - cumprob (:obj:`np.ndarray`): The cumulative probabilities corresponding to the quantiles.

    References:
        - Stack Overflow: https://stackoverflow.com/a/33346366
    """

    # find the unique values and their corresponding counts
    quantiles, counts = np.unique(sample, return_counts=True)

    # take the cumulative sum of the counts and divide by the sample size to
    # get the cumulative probabilities between 0 and 1
    cumprob = np.cumsum(counts).astype(np.double) / sample.size

    return quantiles, cumprob


def atleast_2d(x: np.ndarray) -> np.ndarray:
    """
    Overview:
        Ensure that the input array has at least two dimensions.

    Arguments:
        - x (:obj:`np.ndarray`): The input array.

    Returns:
        - ret (:obj:`np.ndarray`): The input array with at least two dimensions.
    """
    if x.ndim < 2:
        x = x[:, None]
    return x


class LimitsNormalizer(Normalizer):
    """
    Overview:
        A class that normalizes and unnormalizes values within specified limits. \
        This class maps values within the range [xmin, xmax] to the range [-1, 1].

    Interfaces:
        ``__init__``, ``__repr__``, ``normalize``, ``unnormalize``.
    """

    def normalize(self, x: np.ndarray) -> np.ndarray:
        """
        Overview:
            Normalizes the input values.

        Argments:
            - x (:obj:`np.ndarray`): The input values to be normalized.

        Returns:
            - ret (:obj:`np.ndarray`): The normalized values.

        """
        # [ 0, 1 ]
        x = (x - self.mins) / (self.maxs - self.mins)
        # [ -1, 1 ]
        x = 2 * x - 1
        return x

    def unnormalize(self, x: np.ndarray, eps: float = 1e-4) -> np.ndarray:
        """
        Overview:
            Unnormalizes the input values.

        Arguments:
            - x (:obj:`np.ndarray`): The input values to be unnormalized.
            - eps (:obj:`float`): A small value used for clipping. Defaults to 1e-4.

        Returns:
            - ret (:obj:`np.ndarray`): The unnormalized values.

        """
        if x.max() > 1 + eps or x.min() < -1 - eps:
            # print(f'[ datasets/mujoco ] Warning: sample out of range | ({x.min():.4f}, {x.max():.4f})')
            x = np.clip(x, -1, 1)

        # [ -1, 1 ] --> [ 0, 1 ]
        x = (x + 1) / 2.

        return x * (self.maxs - self.mins) + self.mins