예제 #1
0
    def __init__(self, times, values, id=None):
        """
        Constructor for SMTimeSeries class. Initializes SMTimeSeries with 
        time values given in `times` and corresponding values given in `values`.
        Also stores an `id` for the purposes of identifying the SMTimeSeries
        instance from storage.
        
        Checks that:
          - `times` and `values` are of equal length
          - times in `times` are all distinct
          - data in `times` and `values` are numeric
          - `times` and `values` are sequences
          
        Parameters
        ----------
        times : sequence (numerical)
            A sequence containing the ordered time points
        values : sequence (numerical)
            A sequence containing the values corresponding to the time data. 
        id : int/string (optional)
            id associated with the time series to be initialized. If no id provided,
            then a unique one is generated
        
        """

        # Check length
        if len(times) != len(values):
            raise ValueError(
                'Input times and values must have the same length')

        # Check that all times are distinct
        # (we don't check sortedness due to time complexity)
        if len(times) != len(set(times)):
            raise ValueError('Input times must be distinct!')

        # Check if input data is numeric
        if not all(isinstance(x, numbers.Number) for x in values):
            raise TypeError('Data must be numerical!')

        if not all(isinstance(t, numbers.Number) for t in times):
            raise TypeError('Time values must be numerical!')

        # Check if input data is sequence-like
        try:
            iter(values)
            iter(times)
        except:
            raise TypeError('Data must be a sequence!')

        if id is None:
            # Generate id if unspecified
            # Typecast to string just in case
            self._id = str(FSM_global._autogenerate_id())
        else:
            self._id = str(id)

        FSM_global.store(self._id, ArrayTimeSeries(times, values))
예제 #2
0
 def __setitem__(self, index, val):
     """
     Sets the value in the SMTimeSeries instance corresponding to 
     `index` to new value `val`
     
     Parameters
     ----------
     index : int
       index of interest
     val : numeric
       value to update
     
     """
     t = FSM_global.get(self._id)
     t[index] = val
     FSM_global.store(self._id, t)