Пример #1
0
 def integrate_method(self, how='trapz', unit='s'):
     '''Numerically integrate the time series.
 
     @param how: the method to use (trapz by default)
     @return 
 
     Available methods:
      * trapz - trapezoidal
      * cumtrapz - cumulative trapezoidal
      * simps - Simpson's rule
      * romb - Romberger's rule
 
     See http://docs.scipy.org/doc/scipy/reference/integrate.html for the method details.
     or the source code
     https://github.com/scipy/scipy/blob/master/scipy/integrate/quadrature.py
     '''
     available_rules = set(['trapz', 'cumtrapz', 'simps', 'romb'])
     if how in available_rules:
         rule = integrate.__getattribute__(how)
     else:
         print('Unsupported integration rule: %s' % (how))
         print('Expecting one of these sample-based integration rules: %s' % (str(list(available_rules))))
         raise AttributeError
     
     result = rule(self.values, self.index.astype(np.int64) / 10**9)
     #result = rule(self.values)
     return result
Пример #2
0
    def execute(self, oat, detailedresult=False):
        """ apply selected mode for hysep """
        try:
            from scipy import integrate
        except:
            raise ImportError("scipy is required from hyseo method")

        results = []
        temp_oat = oat.copy()
        tmin = temp_oat.ts.index.min()
        tmax = temp_oat.ts.index.max()
        for t in self.periods:
            t0 = t[0] or tmin
            t1 = t[1] or tmax
            rule = integrate.__getattribute__(self.how)

            result = rule(oat.ts['data'].loc[t0:t1].values, oat.ts.loc[t0:t1].index.astype(np.int64) / 10 ** 9).tolist()
            if self.astext:
                results.append({"from": "%s" % t0, "to": "%s" % t1, "value": result})
                #results.append(("%s" % t0, "%s" % t1, result * self.res * self.factor))
            else:
                results.append({"from": t0, "to": t1, "value": result})
                #results.append((t0, t1, result * self.res * self.factor))

        self.result['type'] = "dict list"
        self.result['data'] = results

        return self.returnResult(detailedresult)
Пример #3
0
    def execute(self, oat, detailedresult=False):
        """ apply selected mode for hysep """
        try:
            from scipy import integrate
        except:
            raise ImportError("scipy is required from hyseo method")

        results = []
        temp_oat = oat.copy()
        tmin = temp_oat.ts.index.min()
        tmax = temp_oat.ts.index.max()
        for t in self.periods:
            t0 = t[0] or tmin
            t1 = t[1] or tmax
            rule = integrate.__getattribute__(self.how)

            result = rule(oat.ts['data'].loc[t0:t1].values,
                          oat.ts.loc[t0:t1].index.astype(np.int64) /
                          10**9).tolist()
            if self.astext:
                results.append({
                    "from": "%s" % t0,
                    "to": "%s" % t1,
                    "value": result
                })
                #results.append(("%s" % t0, "%s" % t1, result * self.res * self.factor))
            else:
                results.append({"from": t0, "to": t1, "value": result})
                #results.append((t0, t1, result * self.res * self.factor))

        self.result['type'] = "dict list"
        self.result['data'] = results

        return self.returnResult(detailedresult)
Пример #4
0
    def execute(self, dataframe):
        df = dataframe
        """ apply selected mode for hysep """
        try:
            from scipy import integrate
        except:
            raise ImportError("scipy is required from hyseo method")

        results = []
        tmin = df.index.min()
        tmax = df.index.max()
        for t in self.periods:
            t0 = t[0] or tmin
            t1 = t[1] or tmax
            rule = integrate.__getattribute__(self.how)

            result = rule(df['data'].loc[t0:t1].values,
                          df.loc[t0:t1].index.astype(np.int64) /
                          10**9).tolist()
            if self.astext:
                results.append({
                    "from": "%s" % t0,
                    "to": "%s" % t1,
                    "value": result
                })
                #results.append(("%s" % t0, "%s" % t1, result * self.res * self.factor))
            else:
                results.append({
                    "from": "%s" % t0,
                    "to": "%s" % t1,
                    "value": result
                })
                # results.append({"from": t0, "to": t1, "value": result})
                #results.append((t0, t1, result * self.res * self.factor))
        return results
Пример #5
0
def integrate(df, how='trapz', window=20, rolling=False):
    """Numerically integrate the time series.

    :type df: pd.DataFrame
    :param df:
    :param how: the method to use (trapz by default)
    :param window: the integration window, tm, in ms (20 by default)

    :return

    Available methods:
     * trapz - trapezoidal
     * cumtrapz - cumulative trapezoidal
     * simps - Simpson's rule
     * romb - Romberger's rule

    See http://docs.scipy.org/doc/scipy/reference/integrate.html for the method details.
    or the source code
    https://github.com/scipy/scipy/blob/master/scipy/integrate/quadrature.py
    """
    from scipy import integrate

    available_rules = set(['trapz', 'cumtrapz', 'simps', 'romb'])
    if how in available_rules:
        rule = integrate.__getattribute__(how)
    else:
        print('Unsupported integration rule: %s' % (how))
        print('Expecting one of these sample-based integration rules: %s' %
              (str(list(available_rules))))
        raise AttributeError

    # df = df-(-80.0)
    # df = df - df.iloc[0,0]
    if rolling:
        rolling_window = df.rolling(window=int(window / h.dt))
        result = rolling_window.apply(rule)  # integrate along the index (time)
    else:
        # shift dataframe to only have points at edge of window (leftmost will always be 0
        t_points = df[::int(window / h.dt)].index.values
        if len(t_points) <= 1:
            t_points = df[::(len(df) - 1)].index.values
            logger.info(
                "window was too big, changing window from {} to [}".format(
                    window, (len(df) - 1) * h.dt))
        result = pd.DataFrame(columns=df.columns, index=t_points)
        for t in range(1, len(t_points)):
            result.loc[t_points[t]] = df[t_points[t -
                                                  1]:t_points[t]].apply(rule)
    return result.apply(
        np.abs)[1::]  # skip the first series (when t=0 and all values are nan)
Пример #6
0
def integrated_change(ts, integrator=integrate.trapz, clip_floor=None, clip_ceil=float('inf')):
    """Total value * time above the starting value within a TimeSeries"""
    if clip_floor is None:
        clip_floor = ts[0]
    if clip_ceil < clip_floor:
        polarity = -1 
        offset, clip_floor, clip_ceil, = clip_ceil, clip_ceil, clip_floor
    else:
        polarity, offset = 1, clip_floor
    clipped_values = np.clip(ts.values - offset, clip_floor, clip_ceil)
    print polarity, offset, clip_floor, clip_ceil
    print clipped_values
    integrator_types = set(['trapz', 'cumtrapz', 'simps', 'romb'])
    if integrator in integrator_types:
        integrator = integrate.__getattribute__(integrator)
    integrator = integrator or integrate.trapz
    # datetime units converted to seconds (since 1/1/1970)
    return integrator(clipped_values, ts.index.astype(np.int64) / 10**9)
Пример #7
0
def _integrate_df(self, how='trapz', dateindex=False, **kwargs):
    """
    Numerically integrate a given dataframe

    Parameters
    -----------
    how: string
        the method to use (trapz by default)
        Available methods:
         * trapz - trapezoidal
         * cumtrapz - cumulative trapezoidal
         * simps - Simpson's rule
         * romb - Romberger's rule
    dateindex: bool
        whether or not to assume index is sequence of dates

    If you get a ValueError when using cumtrapz scheme, use the initial keyword.

    See http://docs.scipy.org/doc/scipy/reference/integrate.html for the method details.
    or the source code
    https://github.com/scipy/scipy/blob/master/scipy/integrate/quadrature.py
    """
    import numpy as np
    from scipy import integrate

    df=self.copy()
    available_rules = set(['trapz', 'cumtrapz', 'simps', 'romb'])
    if how in available_rules:
        rule = integrate.__getattribute__(how)
    else:
        print('Unsupported integration rule: %s' % (how))
        print('Expecting one of these sample-based integration rules: %s' % (str(list(available_rules))))
        raise AttributeError
    if dateindex:
        xaxis=df.index.astype(np.int64)
    else:
        xaxis=np.array(df.index)
        #xaxis=df.index.astype(np.float64)
    for c in df.columns:
        df[c] = rule(df[c].values, xaxis, **kwargs)
    if how in ['trapz', 'simps']:
        return df.iloc[0]
    else:
        return df
Пример #8
0
def integrated_change(ts,
                      integrator=integrate.trapz,
                      clip_floor=None,
                      clip_ceil=float('inf')):
    """Total value * time above the starting value within a TimeSeries"""
    if clip_floor is None:
        clip_floor = ts[0]
    if clip_ceil < clip_floor:
        polarity = -1
        offset, clip_floor, clip_ceil, = clip_ceil, clip_ceil, clip_floor
    else:
        polarity, offset = 1, clip_floor
    clipped_values = np.clip(ts.values - offset, clip_floor, clip_ceil)
    print polarity, offset, clip_floor, clip_ceil
    print clipped_values
    integrator_types = set(['trapz', 'cumtrapz', 'simps', 'romb'])
    if integrator in integrator_types:
        integrator = integrate.__getattribute__(integrator)
    integrator = integrator or integrate.trapz
    # datetime units converted to seconds (since 1/1/1970)
    return integrator(clipped_values, ts.index.astype(np.int64) / 10**9)