o
     Ý±iª  ã                   @   s6   d dl mZ d dlZG dd„ dƒZG dd„ deƒZdS )é    )ÚUnionNc                   @   sR   e Zd ZdZdejdejfdd„Zdejdejfdd	„Zdejdejfd
d„ZdS )ÚLinearNormalizerzC
    A class to linearly normalizes data to the range [-1, 1].
    ÚscaleÚoffsetc                 C   s   || _ || _dS )zÙ
        Initializes a new instance of the LinearNormalizer class with given statistics.

        Args:
            scale: The scale factor for normalization.
            offset: The offset for normalization.
        N©r   r   )Úselfr   r   © r   úE/data/cameron/vidgen/unified-world-model/datasets/utils/normalizer.pyÚ__init__
   s   
zLinearNormalizer.__init__ÚxÚreturnc                 C   s   || j  | j S )zH
        Normalizes the input data using the stored statistics.
        )r   r   ©r   r   r   r   r	   Ú__call__   ó   zLinearNormalizer.__call__c                 C   s   || j  | j S )zJ
        Reconstructs the original data from the normalized data.
        r   r   r   r   r	   Úreconstruct   r   zLinearNormalizer.reconstructN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnpÚndarrayr
   r   r   r   r   r   r	   r      s
    r   c                       sf   e Zd ZdZdeeeeej	ej	f ef f f‡ fdd„Z
dedefdd„Zdedefd	d
„Z‡  ZS )ÚNestedDictLinearNormalizerz_
    A class that applies linear normalization to values in a nested dictionary structure.
    Ústatsc                    sZ   t ƒ  ¡  | ¡ D ]!\}}t|tƒrt|ƒ| |< q	tt |d ¡t |d ¡ƒ| |< q	dS )aS  
        Initializes a new instance of the NestedDictLinearNormalizer class with given statistics.

        Args:
            stats: A dictionary containing statistics for each key. The values can either
                be tuples representing scale and offset values or dictionaries that require
                recursive scaling.
        r   é   N)	Úsuperr
   ÚitemsÚ
isinstanceÚdictr   r   r   Úarray)r   r   ÚkÚv©Ú	__class__r   r	   r
   '   s   
	
$üz#NestedDictLinearNormalizer.__init__r   r   c                    ó   ‡ fdd„|  ¡ D ƒS )z`
        Normalizes all values in the input dictionary based on the stored normalizers.
        c                    s.   i | ]\}}||ˆ   ¡ v rˆ | |ƒn|“qS r   )Úkeys©Ú.0r   r    ©r   r   r	   Ú
<dictcomp>;   s   . z7NestedDictLinearNormalizer.__call__.<locals>.<dictcomp>©r   r   r   r'   r	   r   7   s   z#NestedDictLinearNormalizer.__call__c                    r#   )zb
        Reconstructs the original values from normalized values in the input dictionary.
        c                    s0   i | ]\}}||ˆ   ¡ v rˆ |  |¡n|“qS r   )r$   r   r%   r'   r   r	   r(   A   s    $ÿz:NestedDictLinearNormalizer.reconstruct.<locals>.<dictcomp>r)   r   r   r'   r	   r   =   s   
ÿz&NestedDictLinearNormalizer.reconstruct)r   r   r   r   r   Ústrr   Útupler   r   r
   r   r   Ú__classcell__r   r   r!   r	   r   "   s
    .r   )Útypingr   Únumpyr   r   r   r   r   r   r   r	   Ú<module>   s    