<commit_before># -*- coding: utf-8 -*-

import numpy



class AnalogSignal(object):
     
    definition = """An :class:`AnalogSignal` is a continuous data signal 
    acquired at time ``t_start`` at a certain sampling rate.
    """
    
    __doc__ = """
    Object to represent a continuous, analog signal

    **Definition**
    %s
    
    **Attributes**
    signal : An nd array of the data

    **Methods**    
    t() : Returns time values of each point in `signal`. This is the
        preferred way to access the time values, because it will only
        compute the values once, when it is first called. Subsequent calls
        will return a cached version.
    compute_time_vector() : Recomputes and returns time values of each point
        in `signal`.
    max() : Maximum of `signal`
    min() : Minimum of `signal`
    timeslice() : A new AnalogSignal sliced from this one.
    """ % definition
    


    def __init__(self, *arg, **karg):
        """Initialize a new AnalogSignal.
        
        Parameters
        ----------
        signal : ndarray to put in object
        channel, name, sampling_rate, t_start, t_stop
        """
        self.signal = numpy.array([], )
        if karg.has_key('signal'):
            if type(karg['signal']) == numpy.ndarray or type(karg['signal']) == numpy.memmap :
                self.signal  = karg['signal']
        

        for attr in [  'channel' , 'name', 'sampling_rate' , 't_start', 't_stop']:
            if attr in karg:
                setattr(self, attr, karg[attr])
            else:
                setattr(self, attr, None)
        
        if 'dt' in karg:
            self.sampling_rate = float(1./karg['dt'])
        
        if self.t_start is None:self.t_start = 0.
        if self.sampling_rate is None:self.sampling_rate = 1.
            
        

        if self.t_stop is None and self.sampling_rate !=0.:
            self.t_stop  = self.t_start + len(self.signal)/self.sampling_rate

        self._t = None



    #~ def __len__(self):
        #~ if self.signal is not None :
            #~ return len(self.signal)
        #~ else :
            #~ return 0
        
    def compute_time_vector(self) :
        return numpy.arange(len(self.signal), dtype = 'f8')/self.sampling_rate + self.t_start

    def t(self):
        if self._t==None:
            self._t=self.compute_time_vector()
        return self._t
<commit_msg>Cleaning up initialization of AnalogSignal, adding documentation, forcing sampling rate to float<commit_after># -*- coding: utf-8 -*-

import numpy



class AnalogSignal(object):
     
    definition = """An :class:`AnalogSignal` is a continuous data signal 
    acquired at time ``t_start`` at a certain sampling rate.
    """
    
    __doc__ = """
    Object to represent a continuous, analog signal

    **Definition**
    %s
    
    **Attributes**
    signal : An nd array of the data

    **Methods**    
    t() : Returns time values of each point in `signal`. This is the
        preferred way to access the time values, because it will only
        compute the values once, when it is first called. Subsequent calls
        will return a cached version.
    compute_time_vector() : Recomputes and returns time values of each point
        in `signal`.
    max() : Maximum of `signal`
    min() : Minimum of `signal`
    timeslice() : A new AnalogSignal sliced from this one.
    """ % definition
    


    def __init__(self, signal=None, channel=None, name=None, 
        sampling_rate=1., t_start=0., t_stop=None, dt=None, **kargs):
        """Initialize a new AnalogSignal.
        
        It's best to specify all arguments in keyword format. 
        All other keyword arguments besides those below will be ignored, so
        that they may be used by other objects that inherit from this class.        
        
        Parameters (all optional)
        ----------
        signal : numpy array of raw data trace, default is empty array
        channel : channel number
        name : name of this trace
        sampling_rate : in Hz, default 1.0, will be converted to float
        t_start : beginning of signal, will be converted to flaot
        t_stop : end of signal. If not provided, it will be calculated
            from t_start and sampling_rate. Specifically t_stop will be
            the time of the sample after the last one in `signal`.
        dt : If provided, then sampling rate will be 1/dt. Do not specify
            both dt and sampling_rate.
        
        Usage
        -----
        sig = AnalogSignal(name='My signal', sampling_rate=1000.,
            signal=numpy.array([-1, 0, ..., .1]),
            ignored_keyword='something else')
        """
        self.signal = signal
        self.channel = channel
        self.name = name
        self.sampling_rate = float(sampling_rate)
        self.t_start = float(t_start)
        self.t_stop = t_stop
        
        # Default for signal is empty array
        if self.signal is None:
            self.signal = numpy.array([])
        
        # Override sampling rate if dt is specified
        if dt is not None:            
            self.sampling_rate = 1. / dt        
        if self.sampling_rate == 0.:
            raise(ValueError("sampling rate cannot be zero"))
        
        # Calculate self.t_stop
        if self.t_stop is None:
            self.t_stop = self.t_start + len(self.signal)/self.sampling_rate

        # Initialize variable for time array, to be calculated later
        self._t = None

    def compute_time_vector(self) :
        return numpy.arange(len(self.signal), dtype = 'f8')/self.sampling_rate + self.t_start

    def t(self):
        if self._t==None:
            self._t=self.compute_time_vector()
        return self._t

{'fraction_non_alphanumeric': 0.09489937543372658, 'fraction_numerical': 0.003990284524635669, 'mean_word_length': 1.9731820526044352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5076192', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1566, 'n_words': 562}