# Path: python/pipeline/exceptions.py
# class PipelineException(Exception):
#     """Base pipeline exception. Prints the message plus any specific info."""
#     def __init__(self, message, info=None):
#         info_message = '\nError info: ' + repr(info) if info else ''
#         super().__init__(message + info_message)
#         self.info = info


import datajoint as dj
import numpy as np
from scipy.signal import hamming, convolve, medfilt
from .exceptions import PipelineException
        Returns:
            filtered_signal: Numpy array of the interpolate signal with no NaNs
        """
        
        
        
        nan_bool_mask = np.isnan(signal)
        nan_idx = nan_bool_mask.nonzero()[0]
        non_nan_bool_mask = ~nan_bool_mask
        non_nan_idx = (non_nan_bool_mask).nonzero()[0]
        
        filtered_signal = np.copy(signal)
        filled_values = np.interp(nan_idx, non_nan_idx, filtered_signal[non_nan_idx])
        filtered_signal[nan_idx] = filled_values
        
        return filtered_signal
        
    
    def run_filter(self, *args, **kwargs):
            
        filter_method = self.fetch1('filter_method')
        filters_allowing_nans = ('NaN Filler',)
        
        ## Check if there are NaNs
        if filter_method not in filters_allowing_nans:
            if 'signal' in kwargs:
                signal_to_test = kwargs['signal']
            else:
                signal_to_test = args[0]
            if np.any(np.isnan(signal_to_test)):

# Path: python/pipeline/exceptions.py
# class PipelineException(Exception):
#     """Base pipeline exception. Prints the message plus any specific info."""
#     def __init__(self, message, info=None):
#         info_message = '\nError info: ' + repr(info) if info else ''
#         super().__init__(message + info_message)
#         self.info = info


# Path: python/pipeline/shared.py
import datajoint as dj
import numpy as np
from scipy.signal import hamming, convolve, medfilt
from .exceptions import PipelineException

        Returns:
            filtered_signal: Numpy array of the interpolate signal with no NaNs
        """
        
        
        
        nan_bool_mask = np.isnan(signal)
        nan_idx = nan_bool_mask.nonzero()[0]
        non_nan_bool_mask = ~nan_bool_mask
        non_nan_idx = (non_nan_bool_mask).nonzero()[0]
        
        filtered_signal = np.copy(signal)
        filled_values = np.interp(nan_idx, non_nan_idx, filtered_signal[non_nan_idx])
        filtered_signal[nan_idx] = filled_values
        
        return filtered_signal
        
    
    def run_filter(self, *args, **kwargs):
            
        filter_method = self.fetch1('filter_method')
        filters_allowing_nans = ('NaN Filler',)
        
        ## Check if there are NaNs
        if filter_method not in filters_allowing_nans:
            if 'signal' in kwargs:
                signal_to_test = kwargs['signal']
            else:
                signal_to_test = args[0]
            if np.any(np.isnan(signal_to_test)):

                raise PipelineException('Given signal contains NaNs. Try using run_filter_with_renan() method instead.')