o
    }oi                     @   s0   d dl mZ d dlZd dlZG dd deZdS )    )OptionalNc                   @   sj   e Zd ZdZ	dded fddZdefdd	Zed
e	j
de	j
fddZdedejfddZdd ZdS )ExternalFeatureLoaderzzFeature loader that load external features store in certain format.
    Currently support pickle, npy and npz format.
    N	augmentorz3nemo.collections.asr.parts.perturb.FeatureAugmentorc                 C   s
   || _ dS )z 
        Feature loader
        N)r   )selfr    r   k/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/preprocessing/feature_loader.py__init__   s   
zExternalFeatureLoader.__init__	file_pathc                 C   sF   | ds
| drtj|dd  }|S tj|dd}| |S )zLoad samples from file_path and convert it to be of type float32
        file_path (str) is the path of the file that stores feature/sample.
        z.ptz.pthcpu)map_locationT)allow_pickle)endswithtorchloadfloatnumpynp_convert_samples_to_float32)r   r	   samplesr   r   r   load_feature_from_file"   s
   
z,ExternalFeatureLoader.load_feature_from_filer   returnc                 C   st   |  d}| jtjtjtjtjfv r%t| jj}|dd|d   9 }|S | jtj	tj
tjfv r3	 |S td| j )z_Convert sample type to float32.
        Integers will be scaled to [-1, 1] in float32.
        float32g      ?      zUnsupported sample type: %s.)astypedtyper   int8int16int32int64iinfobitsfloat16r   float64	TypeError)r   float32_samplesr!   r   r   r   r   0   s   
z1ExternalFeatureLoader._convert_samples_to_float32c                 C   s   |  |}| |}|S )z5Processes the features from the provided `file_path`.)r   process_segment)r   r	   featuresr   r   r   process?   s   

zExternalFeatureLoader.processc                 C   s2   | j r| j | tj|tjdS tj|tjdS )z'Processes the provided feature segment.)r   )r   perturbr   tensorr   )r   feature_segmentr   r   r   r&   E   s   z%ExternalFeatureLoader.process_segment)N)__name__
__module____qualname____doc__r   r   strr   staticmethodr   ndarrayr   r   Tensorr(   r&   r   r   r   r   r      s    
	r   )typingr   r   r   r   objectr   r   r   r   r   <module>   s   